Exemple #1
0
def create_instrument(test_data_file, calibrated, pixel_number):
    """
    Create instruments: PyRS and Mantid
    :param calibrated:
    :param pixel_number:
    :return:
    """
    # instrument
    instrument = calibration_file_io.import_instrument_setup(
        xray_2k_instrument_file)

    # 2theta
    two_theta = 35.
    arm_length_shift = 0.
    center_shift_x = 0.
    center_shift_y = 0.
    rot_x_flip = 0.
    rot_y_flip = 0.
    rot_z_spin = 0.

    if False:
        center_shift_x = 1.0 * (random.random() - 0.5) * 2.0
        center_shift_y = 1.0 * (random.random() - 0.5) * 2.0
        arm_length_shift = (random.random() -
                            0.5) * 2.0  # 0.416 + (random.random() - 0.5) * 2.0
        # calibration
        rot_x_flip = 2.0 * (random.random() - 0.5) * 2.0
        rot_y_flip = 2.0 * (random.random() - 0.5) * 2.0
        rot_z_spin = 2.0 * (random.random() - 0.5) * 2.0
    # END-IF: arbitrary calibration

    test_calibration = AnglerCameraDetectorShift()
    test_calibration.center_shift_x = center_shift_x
    test_calibration.center_shift_y = center_shift_y
    test_calibration.center_shift_z = arm_length_shift
    test_calibration.rotation_x = rot_x_flip
    test_calibration.rotation_y = rot_y_flip
    test_calibration.rotation_z = rot_z_spin

    # reduction engine
    engine = reduction_manager.HB2BReductionManager()
    test_data_id, two_the_tmp = engine.load_data(data_file_name=test_data_file,
                                                 target_dimension=pixel_number,
                                                 load_to_workspace=True)

    # load instrument
    pyrs_reducer = reduce_hb2b_pyrs.PyHB2BReduction(instrument)
    pyrs_reducer.build_instrument(two_theta, arm_length_shift, center_shift_x,
                                  center_shift_y, rot_x_flip, rot_y_flip,
                                  rot_z_spin)

    mantid_reducer = None
    # mantid_reducer = reduce_hb2b_mtd.MantidHB2BReduction()
    # data_ws_name = engine.get_raw_data(test_data_id, is_workspace=True)
    # mantid_reducer.set_workspace(data_ws_name)
    # mantid_reducer.load_instrument(two_theta, xray_idf_name, test_calibration)

    return engine, pyrs_reducer, mantid_reducer
Exemple #2
0
    def __init__(self):
        """
        initialization
        """
        self._reduction_manager = reduction_manager.HB2BReductionManager()
        self._hydra_ws = None  # HidraWorkspace used for reduction

        # initialize reduction session with a general name (single session script)
        self._session = 'GeneralHB2BReduction'
        self._hydra_file_name = None

        return
Exemple #3
0
    def __init__(self):
        """
        initialization
        """
        # Declare services
        # reduction service
        self._reduction_service = reduction_manager.HB2BReductionManager()

        # pole figure calculation
        self._pole_figure_calculator_dict = dict()
        self._last_pole_figure_calculator = None

        # strain and stress calculator
        self._ss_calculator_dict = dict()   # [session name][strain/stress type: 1/2/3] = ss calculator
        self._curr_ss_session = None
        self._curr_ss_type = None

        # Working environment
        self._working_dir = None

        return
def main():

    # # ----------------s-----------------------------------------------------------
    # wavelength = 1.296  # A
    # two_theta = 30.

    # Set up
    # data, mask and etc
    test_file_name = 'tests/testdata/LaB6_10kev_35deg-00004_Rotated_TIF.h5'
    pos_mask_h5 = 'tests/testdata/masks/Chi_30.hdf5'
    neg_mask_h5 = 'tests/testdata/masks/Chi_Neg30.hdf5'
    mask_p10_h5 = 'tests/testdata/masks/Chi_10.hdf5'
    mask_n10_h5 = 'tests/testdata/masks/Chi_Neg10.hdf5'
    mask_p20_h5 = 'tests/testdata/masks/Chi_20.hdf5'
    mask_n20_h5 = 'tests/testdata/masks/Chi_Neg20.hdf5'

    # instrument geometry
    idf_name = 'tests/testdata/xray_data/XRay_Definition_2K.txt'

    # 2theta
    two_theta = -35.  # TODO - TONIGHT 1 - Make this user specified value

    roi_vec_pos, mask_2theta, note = mask_util.load_pyrs_mask(pos_mask_h5)
    roi_vec_neg, mask_2thetA, notE = mask_util.load_pyrs_mask(neg_mask_h5)
    roi_p10_vec, mask_2theta, note = mask_util.load_pyrs_mask(mask_p10_h5)
    roi_n10_vec, mask_2thetA, notE = mask_util.load_pyrs_mask(mask_n10_h5)
    roi_p20_vec, mask_2theta, note = mask_util.load_pyrs_mask(mask_p20_h5)
    roi_n20_vec, mask_2thetA, notE = mask_util.load_pyrs_mask(mask_n20_h5)

    # Load, mask and reduce data
    if True:
        # check
        instrument = calibration_file_io.import_instrument_setup(idf_name)

        # calibration = [-2.28691912e-04, 3.42766839e-06, -1.99762398e-03, -5.59805308e-02, -8.32593462e-01,
        #                7.66556036e-04]
        # calibration = [0.] * 6

        # Result from [0, 0, -0.002, -0.007, -0.922, 0] and +/- 30 degrees
        # calibration = [2.33235126e-05,  -1.85337378e-04, -1.87855142e-03,  -2.20924269e-02,
        #                -1.64058209e+00, 1.41293750e+00]

        # From [0...]
        # calibration = [-3.90985615e-05, -2.72036598e-04, 3.91642084e-04, 5.99667751e-03,
        #                -8.15624721e-01, 1.42673120e+00]

        # From [previous]
        # calibration = [-3.81356557e-05,  -2.70589009e-04, 3.91001832e-04, 5.84042046e-03,
        #                -8.09033785e-01, 1.42465336e+00]

        # From [0] with +/- 30 and +/- 10
        calibration = [-7.86738387e-05, 9.18988206e-05, -5.55805387e-05, -1.44470481e-01,
                       -6.45203851e-01, 1.33199903e+00]

        # reduction engine
        engine = reduction_manager.HB2BReductionManager()
        test_data_id = engine.load_data(data_file_name=test_file_name, target_dimension=2048,
                                        load_to_workspace=False)

        roi_vec_list = [roi_vec_pos, roi_vec_neg, roi_p10_vec, roi_n10_vec]
        peaks_alignment_score(calibration, engine, instrument, two_theta, roi_vec_list, plot=True)

        print('RESULT EXAMINATION IS OVER')

    else:
        t_start = time.time()

        # reduction engine
        engine = reduction_manager.HB2BReductionManager()
        test_data_id = engine.load_data(data_file_name=test_file_name, target_dimension=2048,
                                        load_to_workspace=False)
        # instrument
        instrument = calibration_file_io.import_instrument_setup(idf_name)

        # set starting values
        if False:
            start_calibration = [0.] * 6    # [0, 0, -0.002, -0.007, -0.922, 0]
            roi_vec_list = [roi_vec_pos, roi_vec_neg]

        if False:
            # start value from +/- 30
            start_calibration = [-3.90985615e-05, -2.72036598e-04, 3.91642084e-04, 5.99667751e-03,
                                 -8.15624721e-01, 1.42673120e+00]
            roi_vec_list = [roi_vec_pos, roi_vec_neg, roi_p10_vec, roi_n10_vec]

        if False:
            start_calibration = [0.] * 6
            roi_vec_list = [roi_vec_pos, roi_vec_neg, roi_p10_vec, roi_n10_vec]
            # result:
            # Residual = 0.104224211772
            # Total Time: 488.894177198

        start_calibration = [0.] * 6
        roi_vec_list = [roi_vec_pos, roi_vec_neg, roi_p10_vec, roi_n10_vec, roi_p20_vec, roi_n20_vec]

        # optimize
        GlobalParameter.global_curr_sequence = 0  # reset output
        DE_Res = leastsq(peaks_alignment_score, np.array(start_calibration),
                         args=(engine, instrument, two_theta, roi_vec_list, False),
                         xtol=1e-15, maxfev=3000, epsfcn=1e-2)

        t_stop = time.time()
        print('Total Time: {}'.format(t_stop - t_start))
        print(DE_Res[0])
        print(DE_Res[1])

        DD = 0.0
        D_Shift = 0
        Center_X = -0.002
        Center_Y = -0.007
        Flip = -1
        Spin = 0.0

        # DE_Res = leastsq(MinDifference, [-1], xtol=1e-15, maxfev=3000)
    # END-IF-ELSE

    return
Exemple #5
0
def create_instrument_load_data(instrument, calibrated, pixel_number):
    """ Create instruments: PyRS and Mantid and load data
    :param instrument: name of instrument
    :param calibrated:
    :param pixel_number:
    :return:
    """
    # instrument
    if instrument == 'XRay-XRayMock':
        hydra_idf = xray_2k_instrument_file
        mantid_idf = xray_idf_name
        two_theta = xray_2theta
        test_data_file = test_xray_data
    elif instrument == 'HBZ':
        hydra_idf = hbz_instrument_file
        mantid_idf = hbz_idf
        two_theta = hbz_2theta
        test_data_file = test_hbz_data
        print('Loaded {} and {} to compare @ 2theta = {} degree'
              ''.format(hydra_idf, mantid_idf, two_theta))
    else:
        raise RuntimeError('Instrument {} does not have test data and IDF'.format(instrument))

    print('----------- IDF in Test: {} vs {} ---------------'.format(hydra_idf, mantid_idf))

    instrument = calibration_file_io.import_instrument_setup(hydra_idf)

    # instrument geometry calibration
    if calibrated:
        # Note: README/TODO-ING: ONLY shift Y
        center_shift_x = int(1000. * (random.random() - 0.5) * 2.0) / 1000.
        center_shift_y = int(1000. * (random.random() - 0.5) * 2.0) / 1000.
        arm_length_shift = int(1000. * (random.random() - 0.5) * 2.0) / 1000.  # 0.416 + (random.random() - 0.5) * 2.0
        # calibration  FIXME - Disable rotation calibration to find out the source of difference:  10-17 vs 10-7
        rot_x_flip = int(1000. * 2.0 * (random.random() - 0.5) * 5.0) / 1000.
        rot_y_flip = int(1000. * 2.0 * (random.random() - 0.5) * 5.0) / 1000.
        rot_z_spin = int(1000. * 2.0 * (random.random() - 0.5) * 5.0) / 1000.
        print('[(Random) Calibration Setup]\n    Shift Linear (X, Y, Z) = {}, {}, {}\n    Shift Rotation '
              '(X, Y, Z) = {}, {}, {}'
              ''.format(center_shift_x, center_shift_y, arm_length_shift, rot_x_flip, rot_y_flip,
                        rot_z_spin))
    else:
        arm_length_shift = center_shift_x = center_shift_y = 0.
        rot_x_flip = rot_y_flip = rot_z_spin = 0.
    # END-IF: arbitrary calibration

    test_calibration = AnglerCameraDetectorShift()
    test_calibration.center_shift_x = center_shift_x
    test_calibration.center_shift_y = center_shift_y
    test_calibration.center_shift_z = arm_length_shift
    test_calibration.rotation_x = rot_x_flip
    test_calibration.rotation_y = rot_y_flip
    test_calibration.rotation_z = rot_z_spin

    # reduction engine
    engine = reduction_manager.HB2BReductionManager()
    test_data_id, two_theta_tmp = engine.load_data(data_file_name=test_data_file, target_dimension=pixel_number,
                                                   load_to_workspace=True)

    # load instrument
    pyrs_reducer = reduce_hb2b_pyrs.PyHB2BReduction(instrument)
    pyrs_reducer.build_instrument(two_theta, arm_length_shift, center_shift_x, center_shift_y,
                                  rot_x_flip, rot_y_flip, rot_z_spin)

    mantid_reducer = reduce_hb2b_mtd.MantidHB2BReduction()
    data_ws_name = engine.get_raw_data(test_data_id, is_workspace=True)
    mantid_reducer.set_workspace(data_ws_name)
    mantid_reducer.build_instrument(two_theta, mantid_idf, test_calibration)

    return engine, pyrs_reducer, mantid_reducer
Exemple #6
0
def main():
    import os

    # Set up
    # data file
    if False:
        # pyrs_root = '/SNS/users/hcf/HFIR_TESTING/'
        # test_file_name = 'LaB6_10kev_35deg-00004.xml'
        # Vanadium = 'Vanadium.xml'
        test_file_name = 'tests/testdata/BNT_7BT_2KNN_6kV_mm-03425-001.xml'
        pos_mask_h5 = None
        neg_mask_h5 = None
    else:
        test_file_name = 'tests/testdata/LaB6_10kev_35deg-00004_Rotated_TIF.h5'
        pos_mask_h5 = 'tests/testdata/masks/Chi_30.hdf5'
        neg_mask_h5 = 'tests/testdata/masks/Chi_Neg30.hdf5'

    # instrument geometry
    if False:
        idf_name = os.path.join(pyrs_root, 'XRay_Definition_1K.xml')
    else:
        idf_name = 'tests/testdata/xray_data/XRay_Definition_2K.txt'

    # Load, mask and reduce data
    if False:
        # old way
        os.system('cp ' + idf_name +
                  ' ~/.mantid/instrument/HB2B_Definition.xml')
        # Load data
        LoadSpice2D(Filename=hb2b_file_name, OutputWorkspace='hb2b')
        LoadSpice2D(Filename=hb2b_Vfile_name, OutputWorkspace='hb2b_V')

        ws_name = test_rotate_2theta(idf_name, 'hb2b', 'hb2b_rotate')
        vanadium = test_rotate_2theta(idf_name, 'hb2b_V', 'hb2b_V_rotate')
        vanadium_line = convert_to_2thetaVanadium(vanadium,
                                                  num_bins=1900,
                                                  Mask=None)
        convert_to_2theta(ws_name, vanadium_line)

        NegMask = 'NegMask'
        PosMask = 'PosMask'
        ZeMask = 'ZeroMask'
        p10Mask = 'p10Mask'
        p20Mask = 'p20Mask'
        p30Mask = 'p30Mask'
        n30Mask = 'n30Mask'

        LoadMask(Instrument='HB2B',
                 InputFile='/SNS/users/hcf/HFIR_TESTING/NegZ_Mask.xml',
                 OutputWorkspace=NegMask)
        LoadMask(Instrument='HB2B',
                 InputFile='/SNS/users/hcf/HFIR_TESTING/Chi_30_Mask.xml',
                 OutputWorkspace=p30Mask)

        InvertMask(InputWorkspace=NegMask, OutputWorkspace=PosMask)
        InvertMask(InputWorkspace=p30Mask, OutputWorkspace=p30Mask)

        CloneWorkspace(InputWorkspace=p30Mask, OutputWorkspace=n30Mask)

        MaskDetectors(Workspace=p30Mask, MaskedWorkspace=PosMask)
        MaskDetectors(Workspace=n30Mask, MaskedWorkspace=NegMask)

        WS_p30deg = test_rotate_2theta(idf_name, 'hb2b', 'hb2b_rotate_p30deg')
        WS_n30deg = test_rotate_2theta(idf_name, 'hb2b', 'hb2b_rotate_n30deg')

        MaskDetectors(WS_p30deg, MaskedWorkspace=p30Mask)
        MaskDetectors(WS_n30deg, MaskedWorkspace=n30Mask)

        vanadium_P30 = convert_to_2thetaVanadium(vanadium,
                                                 num_bins=1900,
                                                 Mask=p30Mask)
        vanadium_N30 = convert_to_2thetaVanadium(vanadium,
                                                 num_bins=1900,
                                                 Mask=n30Mask)

    elif False:
        # build instrument: for FUN
        instrument = calibration_file_io.import_instrument_setup(idf_name)

        # 2theta
        two_theta = -35.  # TODO - TONIGHT 1 - Make this user specified value

        calibration = [
            -2.28691912e-04, 3.42766839e-06, -1.99762398e-03, -5.59805308e-02,
            -8.32593462e-01, 7.66556036e-04
        ]

        arm_length_shift = calibration[2]
        center_shift_x = calibration[0]
        center_shift_y = calibration[1]
        rot_x_flip = calibration[3]
        rot_y_flip = calibration[4]
        rot_z_spin = calibration[5]

        # reduction engine
        engine = reduction_manager.HB2BReductionManager()
        test_data_id = engine.load_data(data_file_name=test_file_name,
                                        target_dimension=2048,
                                        load_to_workspace=False)

        # load instrument
        pyrs_reducer = reduce_hb2b_pyrs.PyHB2BReduction(instrument, 1.239)
        pyrs_reducer.build_instrument(two_theta, arm_length_shift,
                                      center_shift_x, center_shift_y,
                                      rot_x_flip, rot_y_flip, rot_z_spin)

        # reduce data
        min_2theta = 8.
        max_2theta = 64.
        num_bins = 1800

        # reduce PyRS (pure python)
        curr_id = engine.current_data_id
        # mask
        roi_vec_pos, mask_2theta, note = mask_util.load_pyrs_mask(pos_mask_h5)
        roi_vec_neg, mask_2thetA, notE = mask_util.load_pyrs_mask(neg_mask_h5)
        pos_2theta, pos_hist = pyrs_reducer.reduce_to_2theta_histogram(
            counts_array=engine.get_counts(curr_id),
            mask=roi_vec_pos,
            x_range=(min_2theta, max_2theta),
            num_bins=num_bins,
            is_point_data=True,
            use_mantid_histogram=False)
        neg_2theta, neg_hist = pyrs_reducer.reduce_to_2theta_histogram(
            counts_array=engine.get_counts(curr_id),
            mask=roi_vec_neg,
            x_range=(min_2theta, max_2theta),
            num_bins=num_bins,
            is_point_data=True,
            use_mantid_histogram=False)
        plt.plot(pos_2theta, pos_hist, color='red')
        plt.plot(neg_2theta, neg_hist, color='blue')
        plt.show()

        print('RESULT EXAMINATION IS OVER')

    else:
        import time
        t_start = time.time()

        # reduction engine
        engine = reduction_manager.HB2BReductionManager()
        test_data_id = engine.load_data(data_file_name=test_file_name,
                                        target_dimension=2048,
                                        load_to_workspace=False)
        # instrument
        instrument = calibration_file_io.import_instrument_setup(idf_name)
        # mask
        roi_vec_pos, mask_2theta, note = mask_util.load_pyrs_mask(pos_mask_h5)
        roi_vec_neg, mask_2thetA, notE = mask_util.load_pyrs_mask(neg_mask_h5)

        x0 = [0, 0, -0.002, -0.007, -0.922, 0]
        # x0 = [-1.]
        # engine, hb2b_setup, positive_roi_vec, negative_roi_vec
        DE_Res = leastsq(MinDifference,
                         np.array(x0),
                         args=(engine, instrument, roi_vec_pos, roi_vec_neg),
                         xtol=1e-15,
                         maxfev=3000,
                         epsfcn=1e-2)

        t_stop = time.time()
        print('Total Time: {}'.format(t_stop - t_start))
        print(DE_Res[0])
        print(DE_Res[1])

        DD = 0.0
        D_Shift = 0
        Center_X = -0.002
        Center_Y = -0.007
        Flip = -1
        Spin = 0.0

        # DE_Res = leastsq(MinDifference, [-1], xtol=1e-15, maxfev=3000)
    # END-IF-ELSE

    return
def main():

    # # ----------------s-----------------------------------------------------------
    # wavelength = 1.296  # A
    # two_theta = 30.

    # Set up
    # data, mask and etc
    test_file_name = 'tests/testdata/LaB6_10kev_35deg-00004_Rotated_TIF.h5'
    pos_mask_h5 = 'tests/testdata/masks/Chi_30.hdf5'
    neg_mask_h5 = 'tests/testdata/masks/Chi_Neg30.hdf5'

    # instrument geometry
    idf_name = 'tests/testdata/xray_data/XRay_Definition_2K.txt'

    # 2theta
    two_theta = -35.  # TODO - TONIGHT 1 - Make this user specified value

    # Load, mask and reduce data
    if True:
        # check
        instrument = calibration_file_io.import_instrument_setup(idf_name)

        # calibration = [-2.28691912e-04, 3.42766839e-06, -1.99762398e-03, -5.59805308e-02, -8.32593462e-01,
        #                7.66556036e-04]
        calibration = [0.] * 6
        calibration = [
            2.33235126e-05, -1.85337378e-04, -1.87855142e-03, -2.20924269e-02,
            -1.64058209e+00, 1.41293750e+00
        ]

        roi_vec_pos, mask_2theta, note = mask_util.load_pyrs_mask(pos_mask_h5)
        roi_vec_neg, mask_2thetA, notE = mask_util.load_pyrs_mask(neg_mask_h5)

        # reduction engine
        engine = reduction_manager.HB2BReductionManager()
        test_data_id = engine.load_data(data_file_name=test_file_name,
                                        target_dimension=2048,
                                        load_to_workspace=False)

        CostFunction(calibration,
                     engine,
                     instrument,
                     two_theta,
                     roi_vec_pos,
                     roi_vec_neg,
                     plot=True)

        print('RESULT EXAMINATION IS OVER')

    else:
        t_start = time.time()

        # reduction engine
        engine = reduction_manager.HB2BReductionManager()
        test_data_id = engine.load_data(data_file_name=test_file_name,
                                        target_dimension=2048,
                                        load_to_workspace=False)
        # instrument
        instrument = calibration_file_io.import_instrument_setup(idf_name)
        # mask
        roi_vec_pos, mask_2theta, note = mask_util.load_pyrs_mask(pos_mask_h5)
        roi_vec_neg, mask_2thetA, notE = mask_util.load_pyrs_mask(neg_mask_h5)

        start_calibration = [0, 0, -0.002, -0.007, -0.922, 0]
        GlobalParameter.global_curr_sequence = 0  # reset output
        DE_Res = leastsq(CostFunction,
                         np.array(start_calibration),
                         args=(engine, instrument, two_theta, roi_vec_pos,
                               roi_vec_neg, False),
                         xtol=1e-15,
                         maxfev=3000,
                         epsfcn=1e-2)

        t_stop = time.time()
        print('Total Time: {}'.format(t_stop - t_start))
        print(DE_Res[0])
        print(DE_Res[1])

        DD = 0.0
        D_Shift = 0
        Center_X = -0.002
        Center_Y = -0.007
        Flip = -1
        Spin = 0.0

        # DE_Res = leastsq(MinDifference, [-1], xtol=1e-15, maxfev=3000)
    # END-IF-ELSE

    return