def config(): s1 = StepConfig("some.StepClass", "Step1", {"woo": "hoo"}, []) s2 = StepConfig("some.other.StepClass", "Step2", {"yee": "haw"}, []) return StepConfig("some.PipelineClass", "Pipeline", { "foo": "bar", "baz": 42 }, [s1, s2])
def test_step_from_commandline_par_precedence(command_line_pars, command_line_config_pars, reference_pars, expected_pars, tmp_path, monkeypatch): args = [] class_name = "jwst.stpipe.tests.steps.WithDefaultsStep" config_name = "WithDefaultsStep" reference_type = f"pars-{config_name.lower()}" input_path = join(dirname(__file__), "data", "science.fits") if command_line_config_pars: command_line_config_path = tmp_path / "with_defaults_step.cfg" config = ConfigObj(str(command_line_config_path)) config["class"] = class_name config["name"] = config_name for key, value in command_line_config_pars.items(): config[key] = value config.write() args.append(str(command_line_config_path.absolute())) else: args.append(class_name) args.append(input_path) if command_line_pars: for key, value in command_line_pars.items(): args.append(f"--{key}={value}") reference_file_map = {} if reference_pars: reference_path = tmp_path / f"{reference_type}.asdf" reference_config = StepConfig(class_name, config_name, reference_pars, []) with reference_config.to_asdf() as af: af.write_to(reference_path) reference_file_map[reference_type] = str(reference_path) def mock_get_reference_file(dataset, reference_file_type, observatory=None, asn_exptypes=None): if reference_file_type in reference_file_map: return reference_file_map[reference_file_type] else: raise CrdsLookupError(f"Error determining best reference for '{reference_file_type}' = \ Unknown reference type '{reference_file_type}'") monkeypatch.setattr(crds_client, "get_reference_file", mock_get_reference_file) step = Step.from_cmdline(args) for key, value in expected_pars.items(): assert getattr(step, key) == value
def test_step_config_from_legacy_asdf(config): parameters = { "class": config.class_name, "name": config.name, } parameters.update(config.parameters) steps = {} for step in config.steps: # The older config files don't include "name" or "class" # for the child steps. steps[step.name] = step.parameters parameters["steps"] = steps asdf_file = asdf.AsdfFile( {"parameters": parameters}, custom_schema="http://stsci.edu/schemas/stpipe/step_config-0.1.0") # We lose the step class names so the configs won't be equal: legacy_config = StepConfig.from_asdf(asdf_file) assert legacy_config.name == config.name assert legacy_config.class_name == config.class_name assert legacy_config.parameters == config.parameters assert len(legacy_config.steps) == len(config.steps) for legacy_step, step in zip(legacy_config.steps, config.steps): assert legacy_step.name == step.name assert legacy_step.class_name is None assert legacy_step.parameters == step.parameters assert legacy_step.steps == step.steps
def test_step_config_equality(config): other = StepConfig(config.class_name, config.name, config.parameters, config.steps) assert config == other other = StepConfig("some.other.pipeline.Name", config.name, config.parameters, config.steps) assert config != other other = StepConfig(config.class_name, "Hildegaard", config.parameters, config.steps) assert config != other other = StepConfig(config.class_name, config.name, {"foo": "foz"}, config.steps) assert config != other other = StepConfig(config.class_name, config.name, config.parameters, [config.steps[0]]) assert config != other assert config != object()
def test_saving_pars(tmpdir): """Save the step parameters from the commandline""" cfg_path = t_path(join('steps', 'jwst_generic_pars-makeliststep_0002.asdf')) saved_path = tmpdir.join('savepars.asdf') step = Step.from_cmdline([ cfg_path, '--save-parameters', str(saved_path) ]) assert saved_path.check() with asdf.open(t_path(join('steps', 'jwst_generic_pars-makeliststep_0002.asdf'))) as af: original_config = StepConfig.from_asdf(af) original_config.parameters["par3"] = False with asdf.open(str(saved_path)) as af: config = StepConfig.from_asdf(af) assert config.parameters == original_config.parameters step.closeout()
def test_export_config(step_obj, expected, tmp_path): """Test retrieving of configuration parameters""" config_path = tmp_path / "config.asdf" step_obj.export_config(config_path) with asdf.open(config_path) as af: # StepConfig has an __eq__ implementation but we can't use it # due to differences between asdf 2.7 and 2.8 in serializing None # values. This can be simplified once the minimum asdf requirement # is changed to >= 2.8. # assert StepConfig.from_asdf(af) == expected config = StepConfig.from_asdf(af) assert config.class_name == expected.class_name assert config.name == expected.name assert config.steps == expected.steps parameters = set(expected.parameters.keys()).union(set(config.parameters.keys())) for parameter in parameters: assert config.parameters.get(parameter) == expected.parameters.get(parameter)
def test_step_config_from_asdf(config): asdf_file = config.to_asdf() assert StepConfig.from_asdf(asdf_file) == config with pytest.raises(asdf.ValidationError): StepConfig.from_asdf(asdf.AsdfFile())
@pytest.mark.parametrize( 'step_obj, expected', [ ( MakeListStep(par1=0., par2='from args'), StepConfig( 'jwst.stpipe.tests.steps.MakeListStep', 'MakeListStep', { 'pre_hooks': [], 'post_hooks': [], 'output_ext': '.fits', 'output_use_model': False, 'output_use_index': True, 'save_results': False, 'skip': False, 'search_output_file': True, 'input_dir': '', 'par1': 0.0, 'par2': 'from args', 'par3': False, }, [] ), ), ] ) def test_export_config(step_obj, expected, tmp_path): """Test retrieving of configuration parameters""" config_path = tmp_path / "config.asdf"