Exemple #1
0
 def _load_idf(self, filename):
     try:
         builder = NexusBuilder(
             str(uuid.uuid4()),
             idf_file=filename,
             file_in_memory=True,
             nx_entry_name="entry",
         )
         builder.add_instrument_geometry_from_idf()
         self.instrument.nexus.load_nexus_file(builder.target_file)
         self._update_views()
         QMessageBox.warning(
             self,
             "Mantid IDF loaded",
             "Please manually check the instrument for accuracy.",
         )
     except Exception:
         QMessageBox.critical(self, "IDF Error",
                              "Error whilst loading IDF file")
            ('raw_data_1/monitor_3/time_of_flight',
             nx_entry_name + '/instrument/monitor3/time_of_flight'),
            ('raw_data_1/monitor_4/data',
             nx_entry_name + '/instrument/monitor4/data'),
            ('raw_data_1/monitor_4/time_of_flight',
             nx_entry_name + '/instrument/monitor4/time_of_flight'),
        ]))


if __name__ == '__main__':
    output_filename = 'SANS2D_example.nxs'
    nx_entry_name = 'entry'
    # compress_type=32001 for BLOSC, or don't specify compress_type and opts to get non-compressed datasets
    with NexusBuilder(output_filename,
                      nx_entry_name=nx_entry_name,
                      idf_file='SANS2D_Definition_Tubes.xml',
                      compress_type='gzip',
                      compress_opts=1) as builder:
        builder.add_instrument_geometry_from_idf()

        # Define monitor_1 to have the shape of the Utah teapot as example use of NXshape
        builder.add_shape_from_file('../off_files/teapot.off',
                                    'instrument/monitor1', 'shape')

        add_example_nxlog(builder, '/' + nx_entry_name + '/sample/', 10)

        builder.add_fake_event_data(1000, 100)

    with DetectorPlotter(output_filename, nx_entry_name) as plotter:
        plotter.plot_pixel_positions()
"""

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Process some integers.")
    parser.add_argument(
        "--input-file",
        type=str,
        help="Local filename or full path to input NeXus file from AMOR, "
        "output file will be saved to save directory",
        required=True,
    )
    args = parser.parse_args()
    output_filename = f"{''.join(args.input_file.split('.')[:-1])}_tweaked.nxs"

    with NexusBuilder(output_filename,
                      compress_type="gzip",
                      compress_opts=1,
                      nx_entry_name="entry") as builder:
        instrument_group = builder.add_instrument("AMOR", "instrument")
        detector_group = builder.add_nx_group(instrument_group,
                                              "multiblade_detector",
                                              "NXdetector")
        vertices, voxels, detector_ids = create_detector_shape_info()
        offsets = create_pixel_offsets()
        transforms_group = add_shape_to_detector(builder, detector_group,
                                                 detector_ids, voxels,
                                                 vertices, offsets)

        with h5py.File(args.input_file, "r") as input_file:
            input_file.copy("/instrument/stages", builder.root)
            input_file.copy("/experiment/user", builder.root)
            input_file.copy("/experiment/data", detector_group)
Exemple #4
0
    return data_group


last = "."

if __name__ == "__main__":
    output_filename = "bigfake.nxs"
    input_filename = None
    nx_entry_name = "entry"
    iso_timestamp = datetime.now().isoformat()

    # compress_type=32001 for BLOSC, or don't specify compress_type and opts to get non-compressed datasets
    with NexusBuilder(
            output_filename,
            input_nexus_filename=input_filename,
            nx_entry_name=nx_entry_name,
            idf_file=None,
            compress_type="gzip",
            compress_opts=1,
    ) as builder:
        builder.add_dataset(
            builder.root,
            "title",
            "example to demonstrate a prospective ESS instrument NeXus file",
        )
        instrument_group = builder.add_instrument("bigfake", "instrument")
        slit_group = builder.add_nx_group(instrument_group, "slit1", "NXslit")
        slit_transforms = builder.add_nx_group(slit_group, "transformations",
                                               "NXtransformations")
        xo = add_nxlog(
            builder,
            "x_offset",
Exemple #5
0
from nexusutils.nexusbuilder import NexusBuilder
import numpy as np
"""
Creates an example file containing an NXdisk_chopper
For use in https://github.com/nexusformat/features
"""

if __name__ == '__main__':
    output_filename = 'example_nx_disk_chopper.nxs'
    nx_entry_name = 'entry'
    # compress_type=32001 for BLOSC, or don't specify compress_type and opts to get non-compressed datasets
    with NexusBuilder(output_filename,
                      nx_entry_name=nx_entry_name,
                      idf_file=None,
                      compress_type='gzip',
                      compress_opts=1) as builder:
        inst_group = builder.add_instrument('CHP')

        chopper_group = builder.add_nx_group(inst_group, 'example_chopper',
                                             'NXdisk_chopper')

        builder.add_dataset(chopper_group, 'name',
                            'Juelich, WFM Chopper, Disc 1')
        builder.add_dataset(chopper_group,
                            'slit_edges',
                            np.array([
                                83.71, 94.7, 140.49, 155.79, 193.26, 212.56,
                                242.32, 265.33, 287.91, 314.37, 330.3, 360.
                            ]) + 15.0,
                            attributes={'units': 'deg'})
        builder.add_dataset(chopper_group, 'slits', 6)
Exemple #6
0
from nexusutils.nexusbuilder import NexusBuilder

if __name__ == '__main__':
    output_filename = 'SMALLFAKE_example_geometry.nxs'
    # compress_type=32001 for BLOSC, or don't specify compress_type and opts to get non-compressed datasets
    with NexusBuilder(output_filename,
                      idf_file='SMALLFAKE_Definition.xml',
                      compress_type='gzip',
                      compress_opts=1) as builder:
        builder.add_instrument_geometry_from_idf()
        builder.add_shape_from_file('../off_files/cube.off',
                                    'instrument/monitor1', 'shape')
Exemple #7
0
def add_voxel_detector(nexus_builder: NexusBuilder, n_voxels: int = 3):
    detector_group = nexus_builder.add_detector_minimal(
        "voxel geometry detector", 1)

    vertices = np.array([[0.0, 0.0, 0.0]])
    off_faces = np.empty((0, 4), dtype=int)
    detector_numbers = np.arange(n_voxels)
    detector_faces = np.empty((0, 2), dtype=int)
    for voxel_number in range(n_voxels):
        # Each voxel is a regular octahedron
        new_vertices = np.array([
            [0.0, 0.0, 1.0 - 2 * voxel_number],
            [1.0, 0.0, 0.0 - 2 * voxel_number],
            [0.0, 1.0, 0.0 - 2 * voxel_number],
            [-1.0, 0.0, 0.0 - 2 * voxel_number],
            [0.0, -1.0, 0.0 - 2 * voxel_number],
            [0.0, 0.0, -1.0 - 2 * voxel_number],
        ])
        vertices = np.append(vertices[:-1, :], new_vertices, axis=0)

        # Number of vertices followed by vertex indices for each face
        # the first column doesn't end up in the NeXus file dataset
        new_off_faces = np.array([
            [3, 1 + 5 * voxel_number, 5 * voxel_number, 4 + 5 * voxel_number],
            [3, 4 + 5 * voxel_number, 5 * voxel_number, 3 + 5 * voxel_number],
            [3, 3 + 5 * voxel_number, 5 * voxel_number, 2 + 5 * voxel_number],
            [3, 2 + 5 * voxel_number, 5 * voxel_number, 1 + 5 * voxel_number],
            [
                3, 1 + 5 * voxel_number, 5 + 5 * voxel_number,
                2 + 5 * voxel_number
            ],
            [
                3, 2 + 5 * voxel_number, 5 + 5 * voxel_number,
                3 + 5 * voxel_number
            ],
            [
                3, 3 + 5 * voxel_number, 5 + 5 * voxel_number,
                4 + 5 * voxel_number
            ],
            [
                3, 4 + 5 * voxel_number, 5 + 5 * voxel_number,
                1 + 5 * voxel_number
            ],
        ])
        off_faces = np.append(off_faces, new_off_faces, axis=0)

        detector_number = detector_numbers[voxel_number]
        # Map 8 faces to each detector number
        new_detector_faces = np.array([
            [detector_number * 8, detector_number],
            [1 + detector_number * 8, detector_number],
            [2 + detector_number * 8, detector_number],
            [3 + detector_number * 8, detector_number],
            [4 + detector_number * 8, detector_number],
            [5 + detector_number * 8, detector_number],
            [6 + detector_number * 8, detector_number],
            [7 + detector_number * 8, detector_number],
        ])
        detector_faces = np.append(detector_faces, new_detector_faces, axis=0)

    nexus_builder.add_shape(detector_group, "detector_shape", vertices,
                            off_faces, detector_faces)
    nexus_builder.add_dataset(detector_group, "detector_number",
                              detector_numbers)

    transform_group = nexus_builder.add_nx_group(detector_group,
                                                 "transformations",
                                                 "NXtransformation")
    position = nexus_builder.add_transformation(
        transform_group,
        "translation",
        np.array([2.0]),
        units="m",
        vector=[1.0, 0.0, 1.0],
        name="position",
    )
    nexus_builder.add_dataset(detector_group, "depends_on", position.name)

    # Record the voxel positions
    x_offsets = 1.1 * np.ones(n_voxels, dtype=float)
    y_offsets = 2.2 * np.ones(n_voxels, dtype=float)
    z_offsets = np.arange(-n_voxels, n_voxels, 2.0, dtype=float)
    nexus_builder.add_dataset(detector_group, "x_pixel_offset", x_offsets)
    nexus_builder.add_dataset(detector_group, "y_pixel_offset", y_offsets)
    nexus_builder.add_dataset(detector_group, "z_pixel_offset", z_offsets)

    write_to_off_file(f"{n_voxels}_voxels.off", vertices.shape[0],
                      off_faces.shape[0], vertices, off_faces)
from nexusutils.nexusbuilder import NexusBuilder
import numpy as np

output_filename = 'example_nx_geometry.nxs'
with NexusBuilder(output_filename, compress_type='gzip',
                  compress_opts=1) as builder:
    instrument_group = builder.add_instrument("TEAPOT")
    mon_1_group = builder.add_monitor("monitor_1", 0, np.array([0., 0., 1.]))

    builder.add_shape_from_file("teapot.off", mon_1_group, "shape")
    # Add an icosahedral sample
    sample_group = builder.add_sample("sample")
    builder.add_source("test_source", "source")
    # builder.add_shape_from_file("icosa.off", sample_group, "shape")

    mon_2_group = builder.add_monitor("monitor_2", 1, np.array([0., 0., 10.0]))
    builder.add_shape_from_file("death_star.off", mon_2_group, "shape")
Exemple #9
0
from nexusutils.nexusbuilder import NexusBuilder
from nexusutils.detectorplotter import DetectorPlotter

if __name__ == '__main__':
    output_filename = 'WISH_example_gzip_compress.hdf5'

    with NexusBuilder(output_filename,
                      idf_file='WISH_Definition_10Panels.xml',
                      compress_type='gzip',
                      compress_opts=1) as builder:
        builder.add_instrument_geometry_from_idf()

    with DetectorPlotter(output_filename) as plotter:
        plotter.plot_pixel_positions()
Exemple #10
0
def add_shape_to_detector(
    builder: NexusBuilder,
    detector_group: h5py.Group,
    detector_ids: np.ndarray,
    voxels: np.ndarray,
    vertices: np.ndarray,
    offsets: Tuple[np.ndarray, np.ndarray, np.ndarray],
):
    builder.add_dataset(detector_group, "x_pixel_offset", offsets[0],
                        {"units": "m"})
    builder.add_dataset(detector_group, "y_pixel_offset", offsets[1],
                        {"units": "m"})
    builder.add_dataset(detector_group, "z_pixel_offset", offsets[2],
                        {"units": "m"})

    winding_order = voxels.flatten().astype(np.int32)

    vertices_in_face = 4
    faces = np.arange(0, winding_order.size, vertices_in_face)

    shape_group = builder.add_nx_group(detector_group, "detector_shape",
                                       "NXoff_geometry")
    builder.add_dataset(shape_group, "vertices", vertices.astype(np.float64))
    builder.add_dataset(shape_group, "winding_order", winding_order)
    builder.add_dataset(shape_group, "faces", faces.astype(np.int32))
    builder.add_dataset(shape_group, "detector_faces",
                        detector_ids.astype(np.int32))
    builder.add_dataset(
        detector_group,
        "detector_number",
        np.unique(detector_ids[:, 1]).astype(np.int32),
    )
    transforms_group = builder.add_nx_group(detector_group, "transformations",
                                            "NXtransformations")
    builder.add_transformation(
        transforms_group,
        "translation",
        -4.1,
        "m",
        [0.0, 0.0, 1.0],
        name="translation",
    )
    return transforms_group
Exemple #11
0
def write_to_nexus_file(
    filename: str,
    vertices: np.ndarray,
    voxels: np.ndarray,
    detector_ids: np.ndarray,
    offsets: Tuple[np.ndarray, np.ndarray, np.ndarray],
):
    with NexusBuilder(filename,
                      compress_type="gzip",
                      compress_opts=1,
                      nx_entry_name="entry") as builder:
        instrument_group = builder.add_instrument(INSTRUMENT_NAME)
        detector_group = builder.add_nx_group(instrument_group,
                                              "multiblade_detector",
                                              "NXdetector")

        transforms_group = add_shape_to_detector(builder, detector_group,
                                                 detector_ids, voxels,
                                                 vertices, offsets)
        detector_height = builder.add_nx_group(
            transforms_group,
            "COZ",
            "NXlog",
        )
        __add_attributes_to_group(
            detector_height,
            {
                "depends_on":
                "/entry/instrument/multiblade_detector/transformations/translation",
                "transformation_type": "translation",
                "units": "m",
                "vector": [0.0, -1.0, 0.0],
            },
        )
        detector_orientation = builder.add_nx_group(transforms_group, "COM",
                                                    "NXlog")
        __add_attributes_to_group(
            detector_orientation,
            {
                "depends_on":
                "/entry/instrument/multiblade_detector/transformations/COZ",
                "transformation_type": "rotation",
                "units": "deg",
                "vector": [1.0, 0.0, 0.0],
            },
        )
        detector_pivot_point = builder.add_transformation(
            transforms_group,
            "translation",
            0.1,
            "m",
            [0.0, 0.0, 1.0],
            name="detector_pivot_point",
            depends_on=detector_orientation,
        )

        builder.add_depends_on(detector_group, detector_pivot_point)

        sample_group = builder.add_sample()
        sample_transforms_group = builder.add_nx_group(sample_group,
                                                       "transformations",
                                                       "NXtransformations")
        sample_height = builder.add_nx_group(
            sample_transforms_group,
            "SOZ",
            "NXlog",
        )
        __add_attributes_to_group(
            sample_height,
            {
                "depends_on": ".",
                "transformation_type": "translation",
                "units": "m",
                "vector": [0.0, -1.0, 0.0],
            },
        )
        sample_orientation = builder.add_nx_group(
            sample_transforms_group,
            "SOM",
            "NXlog",
        )
        __add_attributes_to_group(
            sample_orientation,
            {
                "depends_on": "/entry/sample/transformations/SOZ",
                "transformation_type": "rotation",
                "units": "deg",
                "vector": [1.0, 0.0, 0.0],
            },
        )
        builder.add_depends_on(sample_group, sample_orientation.name)

        builder.add_source("virtual_source", position=[0.0, 0.0, 30.0])

        builder.add_fake_event_data(1, 100)
        # Remove link to event data in the NXentry
        del builder.root["event_data_multiblade_detector"]
Exemple #12
0
def write_to_nexus_file(
    filename: str,
    vertices: np.ndarray,
    voxels: np.ndarray,
    detector_ids: np.ndarray,
    x_offsets: np.ndarray,
    y_offsets: np.ndarray,
    z_offsets: np.ndarray,
):
    # Slice off first column of voxels as it contains number of vertices in the face (from OFF format)
    # in NeXus that information is carried by the "faces" dataset
    winding_order = voxels[:, 1:].flatten().astype(np.int32)

    vertices_in_face = 4
    faces = np.arange(0, winding_order.size, vertices_in_face)

    with NexusBuilder(filename,
                      compress_type="gzip",
                      compress_opts=1,
                      nx_entry_name="entry") as builder:
        instrument_group = builder.add_nx_group(builder.root, "DREAM",
                                                "NXinstrument")
        builder.instrument = instrument_group
        builder.add_dataset(instrument_group, "name", "DREAM")
        detector_group = builder.add_nx_group(instrument_group,
                                              "endcap_detector", "NXdetector")
        shape_group = builder.add_nx_group(detector_group, "detector_shape",
                                           "NXoff_geometry")
        builder.add_dataset(shape_group, "vertices",
                            vertices.astype(np.float64))
        builder.add_dataset(shape_group, "winding_order", winding_order)
        builder.add_dataset(shape_group, "faces", faces.astype(np.int32))
        builder.add_dataset(shape_group, "detector_faces",
                            detector_ids.astype(np.int32))

        builder.add_dataset(
            detector_group,
            "detector_number",
            np.unique(detector_ids[:, 1]).astype(np.int32),
        )

        # Record voxel centre positions
        builder.add_dataset(detector_group, "x_pixel_offset", x_offsets,
                            {"units": "m"})
        builder.add_dataset(detector_group, "y_pixel_offset", y_offsets,
                            {"units": "m"})
        builder.add_dataset(detector_group, "z_pixel_offset", z_offsets,
                            {"units": "m"})

        # Add a source, sample, start_time and some events so that we can easily load the file into Mantid
        sample_group = builder.add_sample("test sample")
        builder.add_dataset(sample_group, "name", "test_sample")
        source_group = builder.add_source("source")
        transforms_group = builder.add_nx_group(source_group,
                                                "transformations",
                                                "NXtransformation")
        builder.add_transformation(
            transforms_group,
            "translation",
            np.array([20.0]),
            units="m",
            vector=[0.0, 0.0, -1.0],
            name="position",
        )
        builder.add_fake_event_data(1, 100)
        builder.get_root()["start_time"] = datetime.datetime.now().isoformat()