示例#1
0
def test_GIVEN_stream_in_group_children_WHEN_handling_group_THEN_stream_is_appended_to_children(
    file, ):
    group_name = "test_group"
    group = file.create_group(group_name)
    group.attrs["NX_class"] = "NCstream"

    group_contents = {
        "writer_module": "f142",
        "topic": "topic1",
        "source": "SIMPLE:DOUBLE",
        "type": "double",
        "value_units": "cubits",
        "array_size": 32,
    }

    for name, value in group_contents.items():
        group.create_dataset(name, data=value)

    converter = NexusToDictConverter()
    root_dict = converter.convert(file)

    assert len(root_dict["children"]) == 1, "The stream group has been omitted"
    assert group_name == root_dict["children"][0]["name"]
    assert group_contents == root_dict["children"][0]["children"][0]["stream"]
    assert "attributes" not in root_dict["children"][0]
示例#2
0
def test_GIVEN_nx_class_and_attributes_are_bytes_WHEN_output_to_json_THEN_they_are_written_as_utf8(
    file, ):
    dataset_name = "test_ds"
    dataset_value = 1
    dataset_dtype = np.int32

    dataset = file.create_dataset(dataset_name,
                                  data=dataset_value,
                                  dtype=dataset_dtype)
    test_nx_class = b"NXpinhole"
    test_string_attr = b"some_string"
    dataset.attrs["NX_class"] = test_nx_class
    dataset.attrs["string_attr"] = test_string_attr

    converter = NexusToDictConverter()
    root_dict = converter.convert(file)

    ds = root_dict["children"][0]

    for attribute in ds["attributes"]:
        assert attribute["name"] in ["NX_class", "string_attr"]
        if attribute["name"] == "NX_class":
            assert attribute["values"] == test_nx_class.decode("utf8")
        elif attribute["name"] == "string_attr":
            assert attribute["values"] == test_string_attr.decode("utf8")
示例#3
0
def test_GIVEN_group_with_multiple_attributes_WHEN_converting_nexus_to_dict_THEN_attributes_end_up_in_file(
    file, ):
    group_name = "test_group"
    group = file.create_group(group_name)
    group.attrs["NX_class"] = "NXgroup"

    field1name = "field1"
    field1value = "field1val"

    field2name = "field2"
    field2value = 3

    arbitrary_field_name = "arbitrary_field"
    arbitrary_field_value = "something"

    field1 = group.create_dataset(field1name, data=field1value)
    field1.attrs["NX_class"] = "NXfield"
    field1.attrs[arbitrary_field_name] = arbitrary_field_value

    field2 = group.create_dataset(field2name, data=field2value)
    field2.attrs["NX_class"] = "NXfield"

    converter = NexusToDictConverter()
    root_dict = converter.convert(file)

    assert group.name.split("/")[-1] == root_dict["children"][0]["name"]

    assert field1.name.split(
        "/")[-1] == root_dict["children"][0]["children"][0]["name"]
    assert field1value == root_dict["children"][0]["children"][0]["values"]
    assert ("NX_class" == root_dict["children"][0]["children"][0]["attributes"]
            [0]["name"])
    assert (field1.attrs["NX_class"] == root_dict["children"][0]["children"][0]
            ["attributes"][0]["values"])

    assert (arbitrary_field_name == root_dict["children"][0]["children"][0]
            ["attributes"][1]["name"])
    assert (field1.attrs[arbitrary_field_name] == root_dict["children"][0]
            ["children"][0]["attributes"][1]["values"])

    assert field2.name.split(
        "/")[-1] == root_dict["children"][0]["children"][1]["name"]
    assert field2value == root_dict["children"][0]["children"][1]["values"]
示例#4
0
def test_GIVEN_multiple_values_WHEN_handling_dataset_THEN_size_field_does_exist_in_root_dict(
    file, ):
    dataset_name = "test_ds"
    dataset_value = [1.1, 1.2, 1.3]
    dataset_dtype = np.float

    dataset = file.create_dataset(dataset_name,
                                  data=dataset_value,
                                  dtype=dataset_dtype)
    dataset.attrs["NX_class"] = "NXpinhole"

    converter = NexusToDictConverter()
    root_dict = converter.convert(file)
    ds = root_dict["children"][0]

    assert ds["name"].lstrip("/") == dataset_name
    assert ds["type"] == "dataset"
    assert ds["values"] == dataset_value
    assert ds["dataset"]["size"] == (len(dataset_value), )
示例#5
0
def test_GIVEN_dataset_with_an_array_attribute_WHEN_output_to_json_THEN_attribute_is_present_in_json(
        file, test_input):
    dataset_name = "test_ds"
    dataset_value = 1
    dataset_dtype = np.int32

    dataset = file.create_dataset(dataset_name,
                                  data=dataset_value,
                                  dtype=dataset_dtype)
    test_attr_name = "test_attr"
    dataset.attrs[test_attr_name] = test_input

    converter = NexusToDictConverter()
    root_dict = converter.convert(file)

    ds = root_dict["children"][0]

    assert ds["attributes"][0]["name"] == test_attr_name
    assert ds["attributes"][0]["values"].tolist() == test_input
示例#6
0
def test_GIVEN_link_in_group_children_that_is_a_dataset_WHEN_handling_group_THEN_link_is_appended_to_children(
    file, ):
    root_group = file.create_group("root")
    ds_to_be_linked_name = "test_linked_dataset"
    dataset_to_be_linked = root_group.create_dataset(ds_to_be_linked_name,
                                                     data=1)
    dataset_to_be_linked.attrs["NX_class"] = "NXgroup"

    group_name = "test_group_with_link"
    root_group[group_name] = h5py.SoftLink(dataset_to_be_linked.name)
    root_group[group_name].attrs["NX_class"] = "NXgroup"

    converter = NexusToDictConverter()
    root_dict = converter.convert(file)

    assert root_dict["children"][0]["children"][0]["type"] == "link"
    assert (root_group[group_name].name.split("/")[-1] == root_dict["children"]
            [0]["children"][0]["name"])
    assert (dataset_to_be_linked.name == root_dict["children"][0]["children"]
            [0]["target"])
示例#7
0
def test_GIVEN_link_in_group_children_WHEN_handling_group_THEN_link_is_appended_to_children(
    file, ):
    root_group = file.create_group("root")

    group_to_be_linked_name = "test_linked_group"
    group_to_be_linked = root_group.create_group(group_to_be_linked_name)
    group_to_be_linked.attrs["NX_class"] = "NXgroup"

    group_name = "test_group_with_link"
    root_group[group_name] = h5py.SoftLink(group_to_be_linked.name)
    root_group[group_name].attrs["NX_class"] = "NXgroup"

    converter = NexusToDictConverter()
    root_dict = converter.convert(file)

    assert len(root_dict["children"]) == 1, "The link group has been omitted"
    assert root_dict["children"][0]["children"][0]["type"] == "link"
    assert (root_group[group_name].name.split("/")[-1] == root_dict["children"]
            [0]["children"][0]["name"])
    assert group_to_be_linked.name == root_dict["children"][0]["children"][0][
        "target"]
示例#8
0
 def send_command(self):
     if self.command_producer is not None:
         (
             nexus_file_name,
             broker,
             start_time,
             stop_time,
             service_id,
             abort_on_uninitialised_stream,
         ) = self.command_widget.get_arguments()
         self.command_producer.send_command(
             bytes(
                 run_start_pl72.serialise_pl72(
                     job_id=str(uuid.uuid4()),
                     filename=nexus_file_name,
                     start_time=start_time,
                     stop_time=stop_time,
                     broker=broker,
                     nexus_structure=generate_nexus_string(
                         NexusToDictConverter(), self.instrument),
                     service_id=service_id,
                 )))
         self.command_widget.ok_button.setEnabled(False)