def understand(image_file): with h5py.File(image_file, "r") as h5_handle: if "metadata/detector" in h5_handle: if "Rayonix" in h5str(h5_handle["metadata/detector"][()]): return False for elem in h5_handle: if elem.startswith("tag-"): return True return False
def understand(image_file): with h5py.File(image_file, "r") as h5_handle: if "metadata/detector" not in h5_handle: return False if h5str(h5_handle["metadata/detector"][()]) != "Rayonix MX300HS": return False if any(elem.startswith("tag-") for elem in h5_handle): return True return False
def understand(image_file): if "xfel" not in sys.modules: return False with h5py.File(image_file, "r") as handle: # Note, in the future this class might support more instruments, # for now it is tested with the SwissFEL one if "name" not in handle["/entry/instrument"]: return False if (h5str(handle["/entry/instrument/name"][()]) != "SwissFEL ARAMIS BEAMLINE ESB"): return False try: data = handle["/entry/data"] if "pedestal" in data and "gains" in data and "raw" in data: return True except (KeyError, AttributeError): pass return False
def load(entry, exp_index): from dxtbx.model.experiment_list import Experiment, ExperimentList print("Loading NXmx") # Check file contains the feature assert "features" in entry assert 6 in entry["features"][()] experiment_list = ExperimentList() # Find all the experiments entries = find_nx_mx_entries(entry, ".") if len(entries) > 1: entries = sorted(entries, key=lambda x: x["dials/index"][()]) assert len(entries) == len(exp_index) for nxmx, name in zip(entries, exp_index): assert np.string_(nxmx.name) == name, (nxmx.name, name) index = [] rotations = [] for name in exp_index: # Get the entry nxmx = entry.file[name] # Get the definition definition = nxmx["definition"] assert h5str(definition[()]) == "NXmx" assert definition.attrs["version"] == 1 # Get dials specific stuff nx_dials = get_nx_dials(nxmx, "dials") # Set index b = nx_dials["index"].attrs["source"] d = nx_dials["index"].attrs["detector"] if "goniometer" in nx_dials["index"].attrs: g = nx_dials["index"].attrs["goniometer"] else: g = None if "scan" in nx_dials["index"].attrs: s = nx_dials["index"].attrs["scan"] else: s = None c = nx_dials["index"].attrs["sample"] index.append((b, d, g, s, c)) # Get the original orientation (dials specific) transformations = get_nx_transformations(nx_dials, "transformations") angle = transformations["angle"][()] assert transformations["angle"].attrs[ "transformation_type"] == "rotation" axis = transformations["angle"].attrs["vector"] assert tuple(transformations["angle"].attrs["offset"]) == (0, 0, 0) assert transformations["angle"].attrs["offset_units"] == "mm" assert transformations["angle"].attrs["depends_on"] == "." rotations.append((axis, angle)) # Get the imageset try: image_range = nx_dials["template"].attrs["range"] except Exception: image_range = None # Create the experiment experiment = Experiment() # Read the models experiment.beam = load_beam(nxmx) experiment.detector = load_detector(nxmx) experiment.goniometer = load_goniometer(nxmx) experiment.scan = load_scan(nxmx) experiment.crystal = load_crystal(nxmx) # Set the image range if image_range is not None and experiment.scan is not None: num = image_range[1] - image_range[0] + 1 assert num == len(experiment.scan) experiment.scan.set_image_range([int(x) for x in image_range]) # Return the experiment list experiment_list.append(experiment) # Convert from nexus beam direction experiment_list = convert_from_nexus_beam_direction( experiment_list, rotations) beam = collections.defaultdict(list) detector = collections.defaultdict(list) goniometer = collections.defaultdict(list) scan = collections.defaultdict(list) crystal = collections.defaultdict(list) for i, ind in enumerate(index): beam[ind[0]].append(i) detector[ind[1]].append(i) goniometer[ind[2]].append(i) scan[ind[3]].append(i) crystal[ind[4]].append(i) # Set all the shared beams for value in beam.values(): b1 = experiment_list[value[0]].beam assert all(experiment_list[v].beam == b1 for v in value[1:]) for v in value[1:]: experiment_list[v].beam = b1 # Set all the shared detectors for value in detector.values(): d1 = experiment_list[value[0]].detector assert all(experiment_list[v].detector == d1 for v in value[1:]) for v in value[1:]: experiment_list[v].detector = d1 # Set all the shared goniometer for value in goniometer.values(): g1 = experiment_list[value[0]].goniometer assert all(experiment_list[v].goniometer == g1 for v in value[1:]) for v in value[1:]: experiment_list[v].goniometer = g1 # Set all the shared scans for value in scan.values(): s1 = experiment_list[value[0]].scan assert all(experiment_list[v].scan == s1 for v in value[1:]) for v in value[1:]: experiment_list[v].scan = s1 # Set all the shared crystals for value in crystal.values(): c1 = experiment_list[value[0]].crystal assert all(experiment_list[v].crystal == c1 for v in value[1:]) for v in value[1:]: experiment_list[v].crystal = c1 return experiment_list
def visitor(name, obj): if "NX_class" in obj.attrs: if h5str(obj.attrs["NX_class"]) in ["NXentry", "NXsubentry"]: if "definition" in obj: if h5str(obj["definition"][()]) == "NXmx": hits.append(obj)
def load_crystal(entry): from cctbx import uctbx from dxtbx.model import Crystal from scitbx.array_family import flex # Get the sample nx_sample = get_nx_sample(entry, "sample") # Set the space group space_group_symbol = nx_sample["unit_cell_group"][()] # Get depends on if h5str(nx_sample["depends_on"][()]) != ".": assert h5str(nx_sample["depends_on"][()]) == h5str( nx_sample["transformations/phi"].name) # Read the average unit cell data average_unit_cell = flex.double(np.array(nx_sample["average_unit_cell"])) assert nx_sample["average_unit_cell"].attrs["angles_units"] == "deg" assert nx_sample["average_unit_cell"].attrs["length_units"] == "angstrom" assert len(average_unit_cell.all()) == 1 assert len(average_unit_cell) == 6 average_orientation_matrix = flex.double( np.array(nx_sample["average_orientation_matrix"])) assert len(average_orientation_matrix.all()) == 2 assert average_orientation_matrix.all()[0] == 3 assert average_orientation_matrix.all()[1] == 3 # Get the real space vectors uc = uctbx.unit_cell(tuple(average_unit_cell)) U = matrix.sqr(average_orientation_matrix) B = matrix.sqr(uc.fractionalization_matrix()).transpose() A = U * B A = A.inverse() real_space_a = A[0:3] real_space_b = A[3:6] real_space_c = A[6:9] # Read the unit cell data unit_cell = flex.double(np.array(nx_sample["unit_cell"])) assert nx_sample["unit_cell"].attrs["angles_units"] == "deg" assert nx_sample["unit_cell"].attrs["length_units"] == "angstrom" # Read the orientation matrix orientation_matrix = flex.double(np.array(nx_sample["orientation_matrix"])) assert len(unit_cell.all()) == 2 assert len(orientation_matrix.all()) == 3 assert unit_cell.all()[0] == orientation_matrix.all()[0] assert unit_cell.all()[1] == 6 assert orientation_matrix.all()[1] == 3 assert orientation_matrix.all()[2] == 3 # Construct the crystal model crystal = Crystal(real_space_a, real_space_b, real_space_c, space_group_symbol) # Sort out scan points if unit_cell.all()[0] > 1: A_list = [] for i in range(unit_cell.all()[0]): uc = uctbx.unit_cell(tuple(unit_cell[i:i + 1, :])) U = matrix.sqr(tuple(orientation_matrix[i:i + 1, :, :])) B = matrix.sqr(uc.fractionalization_matrix()).transpose() A_list.append(U * B) crystal.set_A_at_scan_points(A_list) else: assert unit_cell.all_eq(average_unit_cell) assert orientation_matrix.all_eq(average_orientation_matrix) # Return the crystal return crystal
def load_detector(entry): from dxtbx.model import Detector # Get the detector module object nx_instrument = get_nx_instrument(entry, "instrument") nx_detector = get_nx_detector(nx_instrument, "detector") assert h5str(nx_detector["depends_on"][()]) == "." material = nx_detector["sensor_material"][()] det_type = nx_detector["type"][()] thickness = nx_detector["sensor_thickness"][()] trusted_range = (nx_detector["underload"][()], nx_detector["saturation_value"][()]) # The detector model detector = Detector() i = 0 while True: try: module = get_nx_detector_module(nx_detector, "module%d" % i) except Exception: break # Set the data size image_size = module["data_size"] # Set the module offset offset_length = module["module_offset"][()] assert module["module_offset"].attrs["depends_on"] == "." assert module["module_offset"].attrs[ "transformation_type"] == "translation" assert tuple(module["module_offset"].attrs["offset"]) == (0, 0, 0) offset_vector = matrix.col(module["module_offset"].attrs["vector"]) origin = offset_vector * offset_length # Write the fast pixel direction module_offset_path = str(module["module_offset"].name) pixel_size_x = module["fast_pixel_direction"][()] assert module["fast_pixel_direction"].attrs[ "depends_on"] == module_offset_path assert (module["fast_pixel_direction"].attrs["transformation_type"] == "translation") assert tuple(module["fast_pixel_direction"].attrs["offset"]) == (0, 0, 0) fast_axis = tuple(module["fast_pixel_direction"].attrs["vector"]) # Write the slow pixel direction pixel_size_y = module["slow_pixel_direction"][()] assert module["slow_pixel_direction"].attrs[ "depends_on"] == module_offset_path assert (module["slow_pixel_direction"].attrs["transformation_type"] == "translation") assert tuple(module["slow_pixel_direction"].attrs["offset"]) == (0, 0, 0) slow_axis = tuple(module["slow_pixel_direction"].attrs["vector"]) # Get the pixel size and axis vectors pixel_size = (pixel_size_x, pixel_size_y) # Create the panel panel = detector.add_panel() panel.set_frame(fast_axis, slow_axis, origin) panel.set_pixel_size(pixel_size) panel.set_image_size([int(x) for x in image_size]) panel.set_type(det_type) panel.set_thickness(thickness) panel.set_material(material) panel.set_trusted_range([float(x) for x in trusted_range]) i += 1 # Return the detector and panel return detector
def understand(image_file): # Get the file handle with h5py.File(image_file, "r") as handle: name = h5str(FormatNexusEigerDLS16M.get_instrument_name(handle)) return name and name.upper() in VALID_NAMES