Пример #1
0
    def import_roi_file(self, roi_file_name):
        """

        :param roi_file_name:
        :return:
        """
        roi_id = self._generate_id(roi_file_name, is_roi=True)

        # import file
        if roi_file_name.lower().endswith('.xml'):
            # XML  file name
            roi_vec = mask_util.load_mantid_mask(self._num_pixels,
                                                 roi_file_name,
                                                 is_mask=False)
        elif roi_file_name.lower().endswith(
                '.h5') or roi_file_name.lower().endswith('.hdf5'):
            roi_vec, two_theta, user_note = mask_util.load_pyrs_mask(
                roi_file_name)
        else:
            raise RuntimeError(
                'ROI file of type {} is not recognized and supported'
                ''.format(roi_file_name.split('.')[-1]))

        # set
        self._mask_array_dict[roi_id] = roi_vec
        self._mask_info_dict[roi_id] = [None] * 2
        self._mask_info_dict[roi_id] = roi_file_name

        return roi_id
Пример #2
0
def _mask_detectors(counts_vec, mask_file=None):
    # ignoring returns two_theta and note
    mask_vec, _, _ = mask_util.load_pyrs_mask(mask_file)
    if counts_vec.shape != mask_vec.shape:
        raise RuntimeError('Counts vector and mask vector has different shpae')

    masked_counts_vec = counts_vec * mask_vec

    return masked_counts_vec
Пример #3
0
    def load_mask_file(self, mask_file_name):
        """ Load mask file to 1D array and auxiliary information
        :param mask_file_name:
        :return:
        """
        mask_vec, two_theta, note = mask_util.load_pyrs_mask(mask_file_name)

        # register the masks
        self._loaded_mask_files.append(mask_file_name)

        mask_id = os.path.basename(mask_file_name).split(
            '.')[0] + '_{}'.format(hash(mask_file_name) % 100)
        self._loaded_mask_dict[mask_id] = mask_vec, two_theta, mask_file_name

        return two_theta, note, mask_id
Пример #4
0
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
Пример #5
0
def compare_reduced_masked(angle, calibrated, pixel_number):
    """
    Compare reduced data without mask
    :param angle: solid angle (integer)
    :param calibrated:
    :param pixel_number:
    :return:
    """
    # create geometry/instrument
    engine, pyrs_reducer, mantid_reducer = compare_geometry_test(calibrated, pixel_number, False)

    # load mask
    test_mask = XRay_Masks[angle]

    # load mask: mask file
    print('Load masking file: {}'.format(test_mask))
    mask_vec, mask_2theta, note = mask_util.load_pyrs_mask(test_mask)
    print('Mask file {}: 2theta = {}'.format(test_mask, mask_2theta))

    NUM_BINS = 1800

    # reduce data
    min_2theta = 15.
    max_2theta = 55.

    # reduce PyRS (pure python)
    curr_id = engine.current_data_id
    pyrs_returns = pyrs_reducer.reduce_to_2theta_histogram(counts_array=engine.get_counts(curr_id),
                                                           mask=mask_vec, x_range=(min_2theta, max_2theta),
                                                           num_bins=NUM_BINS)
    pyrs_vec_x, pyrs_vec_y = pyrs_returns

    # reduce by Mantid
    data_ws = mantid_reducer.get_workspace()
    reduced_data = mantid_reducer.reduce_to_2theta_histogram(data_ws.name(),
                                                             two_theta_min=min_2theta,
                                                             two_theta_max=max_2theta,
                                                             num_2theta_bins=NUM_BINS,
                                                             mask=mask_vec)
    mantid_vec_x = reduced_data[0]
    mantid_vec_y = reduced_data[1]

    # possible convert
    if mantid_vec_x.shape[0] == pyrs_vec_x.shape[0] - 1:
        # partial compare result
        print('Mantid ResampleX return: shape = {}, {}'.format(mantid_vec_x.shape,
                                                               mantid_vec_y.shape))

        diff_y = numpy.sqrt(numpy.sum((pyrs_vec_y - mantid_vec_y)**2))/mantid_vec_y.shape[0]

        print('Diff[X]  =  Not Calculated,  Diff[Y]  =  {}'.format(diff_y))
        plt.plot(pyrs_vec_x[:-1], pyrs_vec_y, color='blue', label='PyRS')
        plt.plot(mantid_vec_x, mantid_vec_y, color='red', label='Mantid')
    else:
        # compare result
        diff_x = numpy.sqrt(numpy.sum((pyrs_vec_x - mantid_vec_x) ** 2)) / mantid_vec_x.shape[0]
        diff_y = numpy.sqrt(numpy.sum((pyrs_vec_y - mantid_vec_y)**2))/mantid_vec_y.shape[0]

        print('Diff[X]  =  {},  Diff[Y]  =  {}'.format(diff_x, diff_y))
        plt.plot(pyrs_vec_x[:-1], pyrs_vec_y, color='blue', label='PyRS')
        plt.plot(mantid_vec_x[:-1], mantid_vec_y, color='red', label='Mantid')

    plt.legend()
    plt.show()

    return
Пример #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
Пример #7
0
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
Пример #8
0
                                              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'

if True:
    for solid_angle in [30,
                        -30]:  # shall be extended to 0, +/- 10, +/- 20, +/-30
        mask_vec, mask_2theta, note = mask_util.load_pyrs_mask(
            'tests/testdata/masks/Chi_30.hdf5')

else:
    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)