Skip to content

Commit

Permalink
added __iter__ to StageConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
eacharles committed Jan 18, 2022
1 parent ed21ef8 commit a66676d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
11 changes: 10 additions & 1 deletion ceci/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,15 @@ def __repr__(self):
s += self.__str__()
return s

def to_dict(self):
""" Forcibly return a dict where the values have been cast from StageParameter """
return {key:cast_to_streamable(value) for key, value in dict.items(self)}

def __iter__(self):
""" Override the __iter__ to work with `StageParameter` """
d = self.to_dict()
return iter(d)

def __getitem__(self, key):
""" Override the __getitem__ to work with `StageParameter` """
attr = dict.__getitem__(self, key)
Expand Down Expand Up @@ -187,7 +196,7 @@ def items(self):
def values(self):
""" Override values() to get the parameters values instead of the objects """
return [cast_to_streamable(value) for value in dict.values(self)]

def set_config(self, input_config, args):
""" Utility function to load configuration
Expand Down
36 changes: 24 additions & 12 deletions tests/test_interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_config():

config.free = 'dog'
config.free = 42

try:
config.chunk_rows = 'a'
except TypeError:
Expand All @@ -42,16 +42,28 @@ def test_config():
config['new_par'] = 'abc'
assert config['new_par'] == 'abc'
assert config.get_type('new_par') == str

config.reset()
assert config.chunk_rows == 5000

assert config.get_type('chunk_rows') == int

values = config.values()
for key, value in config.items():
#assert value == config[key].value
assert value in values

def check_func(cfg, **kwargs):
for k, v in kwargs.items():
check_type = cfg.get_type(k)
if k is not None and v is not None:
assert check_type == type(v)

check_func(config, **config)

for k in iter(config):
assert k in config




Expand All @@ -64,14 +76,14 @@ def test_interactive_pipeline():

dry_pipe = Pipeline.read('tests/test.yml', dry_run=True)
dry_pipe.run()

pipe2 = Pipeline.interactive()
overall_inputs = {'DM':'./tests/inputs/dm.txt',
'fiducial_cosmology':'./tests/inputs/fiducial_cosmology.txt'}
inputs = overall_inputs.copy()
inputs['metacalibration'] = True
inputs['config'] = None

pipe2.pipeline_files.update(**inputs)
pipe2.build_stage(PZEstimationPipe)
pipe2.build_stage(shearMeasurementPipe, apply_flag=False)
Expand All @@ -93,11 +105,11 @@ def test_interactive_pipeline():
assert pipe2['WLGCCov'] == pipe2.WLGCCov

rpr = repr(pipe2.WLGCCov.config)

path = pipe2.pipeline_files.get_path('covariance_copy')
assert pipe2.pipeline_files.get_tag(path) == 'covariance_copy'
assert pipe2.pipeline_files.get_type('covariance_copy') == pipe2.WLGCCov.get_output_type('covariance')

pipe2.run()


Expand All @@ -111,19 +123,19 @@ def test_inter_pipe():
inputs['config'] = None

pipe2.pipeline_files.update(**inputs)

pipe2.build_stage(PZEstimationPipe, name='bob')
assert isinstance(pipe2.bob, PZEstimationPipe)
pipe2.remove_stage('bob')

assert not hasattr(pipe2, 'bob')











if __name__ == "__main__":
test_config()
test_interactive()

0 comments on commit a66676d

Please sign in to comment.