def overwrite_from_phil(params, detector, beam=None): ''' Overwrite from phil parameters ''' from cctbx.eltbx import attenuation_coefficient # Override any panel parameters for panel_params in params.detector.panel: panel = detector[panel_params.id] if panel_params.name is not None: panel.set_name(panel_params.name) if panel_params.type is not None: panel.set_type(panel_params.type) if panel_params.gain is not None: panel.set_gain(panel_params.gain) if panel_params.pixel_size is not None: panel.set_pixel_size(panel_params.pixel_size) if panel_params.image_size is not None: panel.set_image_size(panel_params.image_size) if panel_params.trusted_range is not None: panel.set_trusted_range(panel_params.trusted_range) if panel_params.thickness is not None: panel.set_thickness(panel_params.thickness) if panel_params.material is not None: panel.set_material(panel_params.material) if panel_params.parallax_correction is None: if isinstance(panel.get_px_mm_strategy(), ParallaxCorrectedPxMmStrategy): panel_params.parallax_correction = True else: panel_params.parallax_correction = False if panel_params.parallax_correction is True: if beam is None: raise RuntimeError("No beam for parallax correction") table = attenuation_coefficient.get_table(panel.get_material()) mu = table.mu_at_angstrom(beam.get_wavelength()) / 10.0 t0 = panel.get_thickness() panel.set_px_mm_strategy(ParallaxCorrectedPxMmStrategy(mu, t0)) else: panel.set_px_mm_strategy(SimplePxMmStrategy()) axes = [ panel_params.fast_axis, panel_params.slow_axis, panel_params.origin ] if axes.count(None) != 3: if panel_params.fast_axis is None: panel_params.fast_axis = panel.get_local_fast_axis() if panel_params.slow_axis is None: panel_params.slow_axis = panel.get_local_slow_axis() if panel_params.origin is None: panel_params.origin = panel.get_local_origin() panel.set_local_frame(panel_params.fast_axis, panel_params.slow_axis, panel_params.origin) # Create the hierarchy if params.detector.hierarchy is not None: root = detector.hierarchy() if params.detector.hierarchy.name is not None: root.set_name(params.detector.hierarchy.name) if (params.detector.hierarchy.fast_axis is not None or params.detector.hierarchy.slow_axis is not None or params.detector.hierarchy.origin is not None): if params.detector.hierarchy.fast_axis is None: params.detector.hierarchy.fast_axis = root.get_fast_axis() if params.detector.hierarchy.slow_axis is None: params.detector.hierarchy.slow_axis = root.get_slow_axis() if params.detector.hierarchy.origin is None: params.detector.hierarchy.origin = root.get_origin() root.set_frame(params.detector.hierarchy.fast_axis, params.detector.hierarchy.slow_axis, params.detector.hierarchy.origin) def get_group(node, index): if len(index) == 0: return node return get_group(node[index[0]], index[1:]) for group_params in params.detector.hierarchy.group: group = get_group(root, group_params.id) if group_params.name is not None: group.set_name(group_params.name) if (group_params.fast_axis is not None or group_params.slow_axis is not None or group_params.origin is not None): if group_params.fast_axis is None: group_params.fast_axis = group.get_local_fast_axis() if group_params.slow_axis is None: group_params.slow_axis = group.get_local_slow_axis() if group_params.origin is None: group_params.origin = group.get_local_origin() group.set_local_frame(group_params.fast_axis, group_params.slow_axis, group_params.origin) if len(group_params.panel) != 0: raise RuntimeError("Can't reassign panels in groups") # Return the detector return detector
def generate_from_phil(params, beam=None): ''' Create a new detector model from phil parameters ''' from cctbx.eltbx import attenuation_coefficient detector = Detector() # Create a list of panels panel_list = {} for panel_params in params.detector.panel: panel = Panel() if panel_params.name is not None: panel.set_name(panel_params.name) if panel_params.type is not None: panel.set_type(panel_params.type) if panel_params.gain is not None: panel.set_gain(panel_params.gain) if panel_params.pixel_size is not None: panel.set_pixel_size(panel_params.pixel_size) else: raise RuntimeError('No pixel size set') if panel_params.image_size is not None: panel.set_image_size(panel_params.image_size) else: raise RuntimeError('No image size set') if panel_params.trusted_range is not None: panel.set_trusted_range(panel_params.trusted_range) else: raise RuntimeError('No trusted range set') if panel_params.thickness is not None: panel.set_thickness(panel_params.thickness) if panel_params.material is not None: panel.set_material(panel_params.material) if panel_params.parallax_correction is True: if panel_params.material is None: raise RuntimeError("No material for parallax correction") if panel_params.thickness is None: raise RuntimeError("No thickness for parallax correction") if beam is None: raise RuntimeError("No beam for parallax correction") table = attenuation_coefficient.get_table( panel_params.material) mu = table.mu_at_angstrom(beam.get_wavelength()) / 10.0 t0 = panel_params.thickness panel.set_px_mm_strategy(ParallaxCorrectedPxMmStrategy(mu, t0)) if panel_params.fast_axis is None: panel_params.fast_axis = (1, 0, 0) if panel_params.slow_axis is None: panel_params.slow_axis = (0, 1, 0) if panel_params.origin is None: panel_params.origin = (0, 0, 0) panel.set_local_frame(panel_params.fast_axis, panel_params.slow_axis, panel_params.origin) if panel_params.id in panel_list: raise RuntimeError('Multiple panels with id=%d' % panel_params.id) panel_list[panel_params.id] = panel # Create the hierarchy panel_counter = 0 root = detector.hierarchy() if params.detector.hierarchy.name is not None: root.set_name(params.detector.hierarchy.name) if params.detector.hierarchy.fast_axis is None: params.detector.hierarchy.fast_axis = (1, 0, 0) if params.detector.hierarchy.slow_axis is None: params.detector.hierarchy.slow_axis = (0, 1, 0) if params.detector.hierarchy.origin is None: params.detector.hierarchy.origin = (0, 0, 0) root.set_frame(params.detector.hierarchy.fast_axis, params.detector.hierarchy.slow_axis, params.detector.hierarchy.origin) def get_parent(node, index): if len(index) == 0: return node return get_parent(node[index[0]], index[1:]) for group_params in params.detector.hierarchy.group: parent = get_parent(root, group_params.id[:-1]) assert len(parent) == group_params.id[-1] group = parent.add_group() if group_params.name is not None: group.set_name(group_params.name) if group_params.fast_axis is None: group_params.fast_axis = (1, 0, 0) if group_params.slow_axis is None: group_params.slow_axis = (0, 1, 0) if group_params.origin is None: group_params.origin = (0, 0, 0) group.set_local_frame(group_params.fast_axis, group_params.slow_axis, group_params.origin) for panel_id in group_params.panel: assert panel_id == panel_counter group.add_panel(panel_list[panel_id]) panel_counter += 1 if panel_counter == 0: for panel_id in range(max(panel_list.keys()) + 1): detector.add_panel(panel_list[panel_id]) elif panel_counter != len(panel_list): raise RuntimeError("Inconsistent number of panels in hierarchy") # Return detector return detector