Skip to content

Commit

Permalink
Merge branch 'default-config' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur Vigan committed Jun 13, 2022
2 parents 2a73804 + 0f737d5 commit 57a0b69
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 8 deletions.
15 changes: 12 additions & 3 deletions sphere/IFS.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ class Reduction(object):
# Constructor
##################################################

def __new__(cls, path, log_level='info', sphere_handler=None):
def __new__(cls, path, log_level='info', user_config=None, sphere_handler=None):
'''
Custom instantiation for the class
Expand All @@ -407,9 +407,13 @@ def __new__(cls, path, log_level='info', sphere_handler=None):
path : str
Path to the directory containing the dataset
level : {'debug', 'info', 'warning', 'error', 'critical'}
log_level : {'debug', 'info', 'warning', 'error', 'critical'}
The log level of the handler
user_config : str
Path to a user-provided configuration. Default is None, i.e. the
reduction will use the package default configuration parameters
sphere_handler : log handler
Higher-level SPHERE.Dataset log handler
'''
Expand Down Expand Up @@ -496,7 +500,6 @@ def __new__(cls, path, log_level='info', sphere_handler=None):
reduction._default_center = np.array(eval(cfgparser.get('calibration', 'default_center')))
reduction._orientation_offset = eval(cfgparser.get('calibration', 'orientation_offset'))

# reduction parameters
# reduction parameters
cfg = {}
items = dict(cfgparser.items('reduction'))
Expand All @@ -508,6 +511,12 @@ def __new__(cls, path, log_level='info', sphere_handler=None):
cfg[key] = val
reduction._config = utils.Configuration(reduction._path, reduction._logger, cfg)

# load user-provided default configuration parameters
if user_config:
user_config = Path(user_config)

reduction._config.load_from_file(user_config)

#
# reduction adn recipes status
#
Expand Down
14 changes: 12 additions & 2 deletions sphere/IRDIS/ImagingReduction.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class ImagingReduction(object):
# Constructor
##################################################

def __new__(cls, path, log_level='info', sphere_handler=None):
def __new__(cls, path, log_level='info', user_config=None, sphere_handler=None):
'''Custom instantiation for the class and initialization for the
instances
Expand All @@ -66,9 +66,13 @@ def __new__(cls, path, log_level='info', sphere_handler=None):
path : str
Path to the directory containing the dataset
level : {'debug', 'info', 'warning', 'error', 'critical'}
log_level : {'debug', 'info', 'warning', 'error', 'critical'}
The log level of the handler
user_config : str
Path to a user-provided configuration. Default is None, i.e. the
reduction will use the package default configuration parameters
sphere_handler : log handler
Higher-level SPHERE.Dataset log handler
Expand Down Expand Up @@ -170,6 +174,12 @@ def __new__(cls, path, log_level='info', sphere_handler=None):
cfg[key] = val
reduction._config = utils.Configuration(reduction._path, reduction._logger, cfg)

# load user-provided default configuration parameters
if user_config:
user_config = Path(user_config)

reduction._config.load_from_file(user_config)

#
# reduction and recipes status
#
Expand Down
15 changes: 12 additions & 3 deletions sphere/IRDIS/SpectroReduction.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class SpectroReduction(object):
# Constructor
##################################################

def __new__(cls, path, log_level='info', sphere_handler=None):
def __new__(cls, path, log_level='info', user_config=None, sphere_handler=None):
'''Custom instantiation for the class and initialization for the
instances
Expand All @@ -117,9 +117,13 @@ def __new__(cls, path, log_level='info', sphere_handler=None):
path : str
Path to the directory containing the dataset
level : {'debug', 'info', 'warning', 'error', 'critical'}
log_level : {'debug', 'info', 'warning', 'error', 'critical'}
The log level of the handler
user_config : str
Path to a user-provided configuration. Default is None, i.e. the
reduction will use the package default configuration parameters
sphere_handler : log handler
Higher-level SPHERE.Dataset log handler
Expand Down Expand Up @@ -211,7 +215,12 @@ def __new__(cls, path, log_level='info', sphere_handler=None):
cfg[key] = val
reduction._config = utils.Configuration(reduction._path, reduction._logger, cfg)


# load user-provided default configuration parameters
if user_config:
user_config = Path(user_config)

reduction._config.load_from_file(user_config)

#
# reduction and recipes status
#
Expand Down
25 changes: 25 additions & 0 deletions sphere/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,28 @@ def load(self):
self._logger.error('An error occured while loading previous configuration file')
else:
self.save()

def load_from_file(self, filepath):
'''
Load configuration from provided file
Parameters
----------
filepath : str
Path of the configuration file
'''

if filepath.exists():
try:
self._logger.info(f'Load configuration file at path {filepath}')

cfg = None
with open(filepath, 'r') as file:
cfg = json.load(file)

for key in cfg.keys():
self.data[key] = cfg[key]

# self.save()
except:
self._logger.error('An error occured while loading configuration file')

0 comments on commit 57a0b69

Please sign in to comment.