Exemple #1
0
    def write_instrument_to_hexrd_config(self, instr):
        iconfig = HexrdConfig().instrument_config_none_euler_convention

        # Add this so the calibration crystal gets written
        cal_crystal = iconfig.get('calibration_crystal')
        output_dict = instr.write_config(calibration_dict=cal_crystal)

        # Convert back to whatever convention we were using before
        eac = HexrdConfig().euler_angle_convention
        if eac is not None:
            convert_tilt_convention(output_dict, None, eac)

        # Add the saturation levels, as they seem to be missing
        sl = 'saturation_level'
        for det in output_dict['detectors'].keys():
            output_dict['detectors'][det][sl] = iconfig['detectors'][det][sl]

        # Save the previous iconfig to restore the statuses
        prev_iconfig = HexrdConfig().config['instrument']

        # Update the config
        HexrdConfig().config['instrument'] = output_dict

        # This adds in any missing keys. In particular, it is going to
        # add in any "None" detector distortions
        HexrdConfig().set_detector_defaults_if_missing()

        # Add status values
        HexrdConfig().add_status(output_dict)

        # Set the previous statuses to be the current statuses
        HexrdConfig().set_statuses_from_prev_iconfig(prev_iconfig)
Exemple #2
0
    def set_euler_angle_convention(self, new_conv, convert_config=True):

        allowed_conventions = [{
            'axes_order': 'xyz',
            'extrinsic': True
        }, {
            'axes_order': 'zxz',
            'extrinsic': False
        }, None]

        if new_conv not in allowed_conventions:
            default_conv = constants.DEFAULT_EULER_ANGLE_CONVENTION
            print('Warning: Euler angle convention not allowed:', new_conv)
            print('Setting the default instead:', default_conv)
            new_conv = default_conv

        if convert_config:
            # First, convert all the tilt angles
            old_conv = self._euler_angle_convention
            utils.convert_tilt_convention(self.config['instrument'], old_conv,
                                          new_conv)

        # Set the variable
        self._euler_angle_convention = copy.deepcopy(new_conv)
        self.euler_angle_convention_changed.emit()
Exemple #3
0
    def save_instrument_config(self, output_file):
        default = self.filter_instrument_config(self.config['instrument'])
        eac = self.euler_angle_convention
        if eac is not None:
            # Convert it to None convention before saving
            utils.convert_tilt_convention(default, eac, None)

        with open(output_file, 'w') as f:
            yaml.dump(default, f, Dumper=NumPyIncludeDumper)
Exemple #4
0
    def save_instrument_config(self, output_file):
        default = self.filter_instrument_config(self.config['instrument'])
        eac = self.euler_angle_convention
        if eac != (None, None):
            # Convert it to None convention before saving
            new_conv = (None, None)
            utils.convert_tilt_convention(default, eac, new_conv)

        with open(output_file, 'w') as f:
            yaml.dump(default, f)
Exemple #5
0
    def restore_instrument_config_backup(self):
        self.config['instrument'] = copy.deepcopy(
            self.instrument_config_backup)

        old_eac = self.instrument_config_backup_eac
        new_eac = self.euler_angle_convention
        if old_eac != new_eac:
            # Convert it to whatever convention we are using
            utils.convert_tilt_convention(self.config['instrument'], old_eac,
                                          new_eac)

        self.rerender_needed.emit()
        self.update_active_material_energy()
    def set_detector_defaults(self, det):
        if det not in self.detector_defaults.keys():
            return

        for key, value in self.detector_defaults[det].items():
            HexrdConfig().set_instrument_config_val(
                ['detectors', det, 'transform', key, 'value'], value)
            self.detector_defaults[det]
        eac = {'axes_order': 'zxz', 'extrinsic': False}
        convert_tilt_convention(HexrdConfig().config['instrument'], None, eac)
        self.detector_defaults[det]['tilt'] = (
            HexrdConfig().get_instrument_config_val(
                ['detectors', det, 'transform', 'tilt', 'value']))
        self.new_config_loaded.emit()
Exemple #7
0
    def load_instrument_config(self, yml_file):
        with open(yml_file, 'r') as f:
            self.config['instrument'] = yaml.load(f, Loader=yaml.FullLoader)

        eac = self.euler_angle_convention
        if eac != (None, None):
            # Convert it to whatever convention we are using
            old_conv = (None, None)
            utils.convert_tilt_convention(self.config['instrument'], old_conv,
                                          eac)

        # Set any required keys that might be missing to prevent key errors
        self.set_defaults_if_missing()
        self.create_internal_config(self.config['instrument'])

        # Create a backup
        self.backup_instrument_config()

        self.update_active_material_energy()
        return self.config['instrument']
Exemple #8
0
    def load_instrument_config(self, yml_file):
        old_detectors = self.detector_names
        with open(yml_file, 'r') as f:
            self.config['instrument'] = yaml.load(f, Loader=NumPyIncludeLoader)

        eac = self.euler_angle_convention
        if eac is not None:
            # Convert it to whatever convention we are using
            utils.convert_tilt_convention(self.config['instrument'], None, eac)

        # Set any required keys that might be missing to prevent key errors
        self.set_defaults_if_missing()

        # Remove any 'None' distortion dicts from the detectors
        utils.remove_none_distortions(self.config['instrument'])

        self.create_internal_config(self.config['instrument'])

        # Create a backup
        self.backup_instrument_config()

        # Temporarily turn off overlays. They will be updated later.
        self.clear_overlay_data()
        prev = self.show_overlays
        self.config['materials']['show_overlays'] = False
        self.update_visible_material_energies()
        self.update_active_material_energy()
        self.config['materials']['show_overlays'] = prev

        self.instrument_config_loaded.emit()

        new_detectors = self.detector_names
        if old_detectors != new_detectors:
            self.detectors_changed.emit()
        else:
            # Still need a deep rerender
            self.deep_rerender_needed.emit()

        return self.config['instrument']
Exemple #9
0
    def set_euler_angle_convention(self,
                                   axes_order='xyz',
                                   extrinsic=True,
                                   convert_config=True):

        new_conv = (axes_order, extrinsic)

        allowed_combinations = [('xyz', True), ('zxz', False), (None, None)]

        if new_conv not in allowed_combinations:
            print('Warning: Euler angle convention not allowed:', new_conv)
            print('Setting the default instead:', allowed_combinations[0])
            new_conv = allowed_combinations[0]

        if convert_config:
            # First, convert all the tilt angles
            old_conv = self._euler_angle_convention
            utils.convert_tilt_convention(self.config['instrument'], old_conv,
                                          new_conv)

        # Set the variable
        self._euler_angle_convention = new_conv
Exemple #10
0
def run_powder_calibration():
    # Set up the tilt calibration mapping
    rme = HexrdConfig().rotation_matrix_euler()

    # Set up the instrument
    iconfig = HexrdConfig().instrument_config_none_euler_convention
    instr = instrument.HEDMInstrument(instrument_config=iconfig,
                                      tilt_calibration_mapping=rme)

    flags = HexrdConfig().get_statuses_instrument_format()

    if len(flags) != len(instr.calibration_flags):
        msg = 'Length of internal flags does not match instr.calibration_flags'
        raise Exception(msg)

    instr.calibration_flags = flags

    # Plane data and images
    plane_data = HexrdConfig().active_material.planeData
    img_dict = HexrdConfig().current_images_dict()

    # tolerances for patches
    tth_tol = HexrdConfig().config['calibration']['powder']['tth_tol']
    eta_tol = HexrdConfig().config['calibration']['powder']['eta_tol']
    pktype = HexrdConfig().config['calibration']['powder']['pk_type']

    # powder calibrator
    pc = PowderCalibrator(instr, plane_data, img_dict,
                          tth_tol=tth_tol, eta_tol=eta_tol,
                          pktype=pktype)

    # make instrument calibrator
    ic = InstrumentCalibrator(pc)

    use_robust_optimization = False
    ic.run_calibration(use_robust_optimization)

    # We might need to use this at some point
    # data_dict = pc._extract_powder_lines()

    # Add this so the calibration crystal gets written
    cal_crystal = iconfig.get('calibration_crystal')
    output_dict = instr.write_config(calibration_dict=cal_crystal)

    # Convert back to whatever convention we were using before
    eac = HexrdConfig().euler_angle_convention
    if eac is not None:
        convert_tilt_convention(output_dict, None, eac)

    # Add the saturation levels, as they seem to be missing
    sl = 'saturation_level'
    for det in output_dict['detectors'].keys():
        output_dict['detectors'][det][sl] = iconfig['detectors'][det][sl]

    # Save the previous iconfig to restore the statuses
    prev_iconfig = HexrdConfig().config['instrument']

    # Update the config
    HexrdConfig().config['instrument'] = output_dict

    # This adds in any missing keys. In particular, it is going to
    # add in any "None" detector distortions
    HexrdConfig().set_detector_defaults_if_missing()

    # Add status values
    HexrdConfig().add_status(output_dict)

    # Set the previous statuses to be the current statuses
    HexrdConfig().set_statuses_from_prev_iconfig(prev_iconfig)
Exemple #11
0
 def instrument_config_none_euler_convention(self):
     iconfig = self.instrument_config
     eac = self.euler_angle_convention
     utils.convert_tilt_convention(iconfig, eac, None)
     return iconfig
Exemple #12
0
def run_line_picked_calibration(line_data):
    # Set up the tilt calibration mapping
    rme = HexrdConfig().rotation_matrix_euler()

    print('Setting up the instrument...')

    # Set up the instrument
    iconfig = HexrdConfig().instrument_config_none_euler_convention
    instr = instrument.HEDMInstrument(instrument_config=iconfig,
                                      tilt_calibration_mapping=rme)

    flags = HexrdConfig().get_statuses_instrument_format()

    if np.count_nonzero(flags) == 0:
        msg = 'There are no refinable parameters'
        raise Exception(msg)

    if len(flags) != len(instr.calibration_flags):
        msg = 'Length of internal flags does not match instr.calibration_flags'
        raise Exception(msg)

    print('Running optimization...')

    # Run calibration
    opt_result = calibrate_instrument_from_picks(instr,
                                                 line_data,
                                                 param_flags=flags,
                                                 xtol=1e-4,
                                                 ftol=1e-4)

    if not opt_result.success:
        print('Optimization failed!')
        return False

    print('Optimization succeeded!')

    # Add this so the calibration crystal gets written
    cal_crystal = iconfig.get('calibration_crystal')
    output_dict = instr.write_config(calibration_dict=cal_crystal)

    # Convert back to whatever convention we were using before
    eac = HexrdConfig().euler_angle_convention
    if eac is not None:
        convert_tilt_convention(output_dict, None, eac)

    # Add the saturation levels, as they seem to be missing
    sl = 'saturation_level'
    for det in output_dict['detectors'].keys():
        output_dict['detectors'][det][sl] = iconfig['detectors'][det][sl]

    print('Updating the config')

    # Save the previous iconfig to restore the statuses
    prev_iconfig = HexrdConfig().config['instrument']

    # Update the config
    HexrdConfig().config['instrument'] = output_dict

    # This adds in any missing keys. In particular, it is going to
    # add in any "None" detector distortions
    HexrdConfig().set_detector_defaults_if_missing()

    # Add status values
    HexrdConfig().add_status(output_dict)

    # Set the previous statuses to be the current statuses
    HexrdConfig().set_statuses_from_prev_iconfig(prev_iconfig)

    return True