Skip to content

Commit

Permalink
fixed something I broke
Browse files Browse the repository at this point in the history
  • Loading branch information
rachelelim committed Nov 10, 2023
1 parent 19c3f14 commit c47e4fc
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 9 deletions.
80 changes: 80 additions & 0 deletions hexrd/nf_config/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
"""Base Config class"""

import logging
import os

from .utils import null

logger = logging.getLogger('hexrd.config')

class Config(object):

_dirty = False

def __init__(self, cfg):
self._cfg = cfg

@property
def dirty(self):
return self._dirty

def get(self, key, default=null):
args = key.split(':')
args, item = args[:-1], args[-1]
temp = self._cfg
for arg in args:
temp = temp.get(arg, {})
# intermediate block may be None:
temp = {} if temp is None else temp
try:
res = temp[item]
except KeyError:
if default is not null:
logger.info(
'%s not specified, defaulting to %s', key, default
)
res = temp.get(item, default)
else:
raise RuntimeError(
'%s must be specified in configuration file' % key
)
return res

def set(self, key, val):
args = key.split(':')
args, item = args[:-1], args[-1]
temp = self._cfg
for arg in args:
temp = temp.get(arg, {})
# intermediate block may be None:
temp = {} if temp is None else temp
if temp.get(item, null) != val:
temp[item] = val
self._dirty = True

def dump(self, filename):
import yaml

with open(filename, 'w') as f:
yaml.dump(self._cfg, f)
self._dirty = False

@staticmethod
def check_filename(fname, wdir):
"""Check whether filename is valid relative to working directory
fname - the name of the file
Returns the absolute path of the filename if the file exists
If fname is an absolute path, use that; otherwise take it as a path
relative to the working directory.
"""
temp = fname
if not os.path.isabs(fname):
temp = os.path.join(wdir, temp)
if os.path.exists(temp):
return temp
raise IOError(
'file: "%s" not found' % temp
)
4 changes: 3 additions & 1 deletion hexrd/nf_config/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
import os
import numpy as np

from hexrd import config as Config
from .config import Config


logger = logging.getLogger('hexrd.config')


class ExperimentConfig(Config):
def __init__(self, cfg):
self._cfg = cfg

@property
def beam_energy(self):
Expand Down
4 changes: 3 additions & 1 deletion hexrd/nf_config/images.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import os

from hexrd import config as Config
from .config import Config


logger = logging.getLogger('hexrd.config')
Expand All @@ -12,6 +12,8 @@


class ImagesConfig(Config):
def __init__(self, cfg):
self._cfg = cfg

@property
def data_folder(self):
Expand Down
6 changes: 4 additions & 2 deletions hexrd/nf_config/input_files.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import logging
import os

from hexrd import config as Config
from .config import Config


logger = logging.getLogger('hexrd.config')


class InputConfig(Config):

def __init__(self, cfg):
self._cfg = cfg

@property
def det_file(self):
temp = self._cfg.get('input_files:det_file')
Expand Down
4 changes: 3 additions & 1 deletion hexrd/nf_config/multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import psutil
import multiprocessing as mp

from hexrd import config as Config
from .config import Config


logger = logging.getLogger('hexrd.config')


class MultiprocessingConfig(Config):
def __init__(self, cfg):
self._cfg = cfg

@property
def num_cpus(self):
Expand Down
6 changes: 4 additions & 2 deletions hexrd/nf_config/nf_root.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from hexrd.constants import shared_ims_key
from hexrd import imageseries

from hexrd import config as Config
from .config import Config
from .multiprocessing import MultiprocessingConfig
from .reconstruction import ReconstructionConfig
from .images import ImagesConfig
Expand All @@ -17,7 +17,9 @@


class NFRootConfig(Config):

def __init__(self, cfg):
self._cfg = cfg

@property
def main_dir(self):
return self._cfg.get('main_dir')
Expand Down
4 changes: 3 additions & 1 deletion hexrd/nf_config/reconstruction.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import logging
import os
from hexrd import config as Config
from .config import Config


logger = logging.getLogger('hexrd.config')


class ReconstructionConfig(Config):
def __init__(self, cfg):
self._cfg = cfg

@property
def tomography(self):
Expand Down
4 changes: 3 additions & 1 deletion hexrd/nf_config/tomography.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import logging
import os

from hexrd import config as Config
from .config import Config


logger = logging.getLogger('hexrd.config')


class TomographyConfig(Config):
def __init__(self, cfg):
self._cfg = cfg

@property
def data_folder(self):
Expand Down

0 comments on commit c47e4fc

Please sign in to comment.