diff --git a/nexus_constructor/json/load_from_json.py b/nexus_constructor/json/load_from_json.py index eac380d67..c3124354d 100644 --- a/nexus_constructor/json/load_from_json.py +++ b/nexus_constructor/json/load_from_json.py @@ -377,10 +377,11 @@ def _add_transform_and_shape_to_component(self, component, children_dict): transformation_reader.add_transformations_to_component() self.warnings += transformation_reader.warnings depends_on = _find_depends_on_path(children_dict, component.name) - if depends_on not in [".", ""]: + if depends_on not in [".", "", None]: if depends_on[0] != "/": - if depends_on.split("/")[0].startswith('transform'): - depends_on = depends_on.split("/")[1] + # we are always in the NXtransformations group but the path could be anything + if len(depends_on.split("/")) == 2: + depends_on = depends_on.split('/')[1] depends_on = component.absolute_path + "/transformations/" + depends_on if depends_on not in DEPENDS_ON_IGNORE: diff --git a/tests/json/test_load_from_json.py b/tests/json/test_load_from_json.py index f116d111e..663fc1112 100644 --- a/tests/json/test_load_from_json.py +++ b/tests/json/test_load_from_json.py @@ -120,132 +120,43 @@ def json_dict_with_component(): ], "children":[ { - "type":"group", "name":"transformations", - "children":[ - - ] - } - ] - } - ] - }, - { - "name":"sample", - "type":"group", - "attributes":[ - { - "name":"NX_class", - "type":"string", - "values":"NXsample" - } - ], - "children":[ - { - "type":"group", - "name":"transformations", - "children":[ - - ] - } - ] - } - ] - } - ] - } - """ - return json.loads(json_string) - - -@pytest.fixture(scope="function") -def json_dict_with_component_and_transform(): - json_string = """ - { - "children":[ - { - "name":"entry", - "type":"group", - "attributes":[ - { - "name":"NX_class", - "type":"String", - "values":"NXentry" - } - ], - "children":[ - { - "name":"instrument", - "type":"group", - "attributes":[ - { - "name":"NX_class", - "type":"String", - "values":"NXinstrument" - } - ], - "children":[ - { - "name":"test_component", - "type":"group", - "attributes":[ - { - "name":"NX_class", - "type":"String", - "values":"NXaperture" - } - ], - "children":[ - { - "module":"dataset", - "attributes":[], - "config":{ - "name":"depends_on", - "type":"string", - "values": "/entry/instrument/test_component/transformations/location" - } - }, - { "type":"group", - "name":"transformations", "children":[ { - "module":"dataset", - "config":{ - "type":"double", - "values":1.0, - "name":"location" + "module": "dataset", + "config": { + "name": "slit0", + "values": 10.0, + "type": "float" }, - "attributes":[ + "attributes": [ { - "name":"units", - "values":"m" + "name": "vector", + "dtype": "float", + "values": [ + 0.0, + 0.0, + 1.0 + ] }, { - "name":"transformation_type", - "values":"translation" + "name": "depends_on", + "dtype": "string", + "values": "." }, { - "name":"vector", - "values":[ - 0.0, - 0.0, - 0.0 - ], - "type":"double" + "name": "transformation_type", + "dtype": "string", + "values": "translation" }, { - "name":"depends_on", - "values":"." + "name": "units", + "dtype": "string", + "values": "metre" } ] } - ], - "attributes":[ - { - "name":"NX_class", - "values":"NXtransformations" - } ] } ] @@ -258,9 +169,16 @@ def json_dict_with_component_and_transform(): "attributes":[ { "name":"NX_class", - "type":"String", + "type":"string", "values":"NXsample" } + ], + "children":[ + { + "type":"group", + "name":"transformations", + "children":[] + } ] } ] @@ -382,9 +300,7 @@ def test_GIVEN_component_with_nx_class_WHEN_loading_from_json_THEN_new_model_con json_dict_with_component, json_reader ): component_class = "NXcrystal" - json_dict_with_component["children"][0]["children"][0]["children"][0]["attributes"][ - 0 - ]["values"] = component_class + json_dict_with_component["children"][0]["children"][0]["children"][0]["attributes"][0]["values"] = component_class node = json_reader._read_json_object( json_dict_with_component["children"][0]["children"][0] ) @@ -422,6 +338,29 @@ def test_GIVEN_json_with_component_depending_on_non_existent_transform_WHEN_load assert contains_warning_of_type(json_reader.warnings, TransformDependencyMissing) +def test_GIVEN_json_with_component_depending_on_relative_transform_WHEN_loaded_THEN_model_updated(json_dict_with_component, json_reader): + + depends_on_dataset_str = """ + { + "module":"dataset", + "attributes":[], + "config":{ + "type":"string", + "values": "/entry/instrument/test_component/transformations/slit0", + "name":"depends_on" + } + } + """ + depends_on_dataset = json.loads(depends_on_dataset_str) + + # Add depends_on dataset which points to a transformation which does not exist in the JSON + json_dict_with_component["children"][0]["children"][0]["children"][0][ + "children" + ].append(depends_on_dataset) + json_reader._load_from_json_dict(json_dict_with_component) + assert json_reader.model.entry.children[0].children[0].stored_items[0].children[0].attributes[1].name == "depends_on" + + def test_when_experiment_id_in_json_then_it_is_added_to_entry(json_reader): json_string = """ {