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
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
def main(): # # ----------------s----------------------------------------------------------- # wavelength = 1.296 # A # two_theta = 30. # Set up # data, mask and etc test_file_name = 'tests/testdata/Simulated_LaB6_rotate.tiff' # test_file_name = 'tests/testdata/LaB6_10kev_35deg-00004_Rotated_TIF.h5' # 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 False: # check instrument = calibration_file_io.import_instrument_setup(idf_name) calibration = [-7.86738387e-05, 9.18988206e-05, -5.55805387e-05, -1.44470481e-01, -6.45203851e-01, 1.33199903e+00] calibration = [6.03052044e-05, -5.21351130e-04, 2.45533336e-04, 7.71328703e-01, -5.45174980e-01, 1.24916005e+00] # reduction engine engine = reductionengine.HB2BReductionManager() test_data_id = engine.load_data(data_file_name=test_file_name, target_dimension=2048, load_to_workspace=False) roi_vec_list = [30, -30, 10, -10] 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 = reductionengine.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) HB2B = instrument_geometry.AnglerCameraDetectorGeometry( instrument.detector_rows, instrument.detector_columns, instrument.pixel_size_x, instrument.pixel_size_y, instrument.arm_length, False) start_calibration = [-3.90985615e-05, -2.72036598e-04, 3.91642084e-04, 5.99667751e-03, -8.15624721e-01, 1.42673120e+00] roi_vec_list = [30, -30, 20, -20, 10, -10] start_calibration = np.array([0.00026239, 0.0055485, 0.00048748, 0.00130224, -0.00025911, -0.0006577]) start_calibration = np.array([0] * 6) out = minimize(peaks_alignment_score, start_calibration, args=(engine, HB2B, two_theta, roi_vec_list, False, True), method='L-BFGS-B', jac=None, bounds=None, tol=None, callback=None, options={'disp': None, 'maxcor': 10, 'ftol': 2.220446049250313e-05, 'gtol': 1e-03, 'eps': 1e-03, 'maxfun': 100, 'maxiter': 100, 'iprint': -1, 'maxls': 20}) t_stop1 = time.time() out2 = minimize(peaks_alignment_score, out.x, args=(engine, HB2B, two_theta, roi_vec_list, False, True), method='L-BFGS-B', jac=None, bounds=None, tol=None, callback=None, options={'disp': None, 'maxcor': 10, 'ftol': 2.220446049250313e-09, 'gtol': 1e-05, 'eps': 1e-07, 'maxfun': 100, 'maxiter': 100, 'iprint': -1, 'maxls': 20}) t_stop2 = time.time() out3 = least_squares(peaks_alignment_score, out2.x, jac='2-point', bounds=([-.1, -.1, -.1, -np.pi, -np.pi, -np.pi], [.1, .1, .1, np.pi, np.pi, np.pi]), method='trf', ftol=1e-08, xtol=1e-08, gtol=1e-08, x_scale=1.0, loss='linear', f_scale=1.0, diff_step=None, tr_solver=None, tr_options={}, jac_sparsity=None, max_nfev=None, verbose=0, args=(engine, HB2B, two_theta, roi_vec_list, False, False), kwargs={}) # # 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, ftol=1e-12, gtol=1e-12, maxfev=30000, epsfcn=1e-2) t_stop = time.time() print('Global Refine: {}'.format(t_stop1 - t_start)) print('Local Refine: {}'.format(t_stop2 - t_start)) print('Total Time: {}'.format(t_stop - t_start)) print(out3.x) peaks_alignment_score(out3.x, engine, instrument, two_theta, roi_vec_list, True, True) # DE_Res = leastsq(MinDifference, [-1], xtol=1e-15, maxfev=3000) # END-IF-ELSE return
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
def main(): # # ----------------s----------------------------------------------------------- # wavelength = 1.296 # A # two_theta = 30. # Set up # data, mask and etc test_file_name1 = 'tests/testdata/SimulatedData_60.tiff' test_file_name2 = 'tests/testdata/SimulatedData_61.tiff' test_file_name3 = 'tests/testdata/SimulatedData_62.tiff' test_file_name4 = 'tests/testdata/SimulatedData_63.tiff' test_file_name5 = 'tests/testdata/SimulatedData_64.tiff' test_file_name6 = 'tests/testdata/SimulatedData_65.tiff' # test_file_name = 'tests/testdata/LaB6_10kev_35deg-00004_Rotated_TIF.h5' # reduction engine DataSets = [] two_theta = [] TTHS = np.arange(60, 65.1, 1) engine = reductionengine.HB2BReductionManager() TTHS = np.array([56., 60.0, 65., 70., 81.]) for TTH in TTHS: engine.load_data( data_file_name='tests/testdata/SimulatedData_%.0f.tiff' % TTH, target_dimension=1024, load_to_workspace=False) DataSets.append('SimulatedData_%.0f' % TTH) two_theta.append(-1 * TTH) # instrument geometry idf_name = 'tests/testdata/xray_data/XRay_Definition_2K.txt' idf_name = 'tests/testdata/xray_data/XRay_Definition_1K.txt' t_start = time.time() # instrument instrument = calibration_file_io.import_instrument_setup(idf_name) HB2B = instrument_geometry.AnglerCameraDetectorGeometry( instrument.detector_rows, instrument.detector_columns, instrument.pixel_size_x, instrument.pixel_size_y, instrument.arm_length, False) # roi_vec_list = [30, -30, 20, -20, 10, -10] roi_vec_list = [5, 2, 0, -2, -5] roi_vec_list = [5, 0, -5] start_calibration = np.array([0.0] * 7, dtype=np.float) start_calibration[6] = 1.452 GlobalParameter.global_curr_sequence = 0 t_stop1 = time.time() t_stop2 = time.time() start_calibration = np.array([0.0] * 3, dtype=np.float) #out = minimize(peak_alignment_shift, start_calibration, args=(engine, HB2B, two_theta, roi_vec_list, [0, 0, 0], 1.452, True), method='L-BFGS-B', jac=None, bounds=([-.05, .05], [-.05, .05], [ -.05, .05 ] ), tol=None, callback=None, options={'disp': None, 'maxcor': 10, 'ftol': 2.220446049250313e-05, 'gtol': 1e-03, 'eps': 1e-03, 'maxfun': 100, 'maxiter': 100, 'iprint': -1, 'maxls': 20}) peak_alignment_shift(start_calibration, engine, HB2B, two_theta, roi_vec_list, [0, 0, 0], 1.452, False) out1 = least_squares(peak_alignment_shift, start_calibration, jac='3-point', bounds=([-.05, -.05, -.05], [.05, .05, .05]), method='dogbox', ftol=1e-08, xtol=1e-08, gtol=1e-08, x_scale=1.0, loss='linear', f_scale=1.0, diff_step=None, tr_solver=None, tr_options={}, jac_sparsity=None, max_nfev=None, verbose=0, args=(engine, HB2B, two_theta, roi_vec_list, [0, 0, 0], 1.452, False), kwargs={}) start_calibration = np.array([0.0] * 3, dtype=np.float) out20 = minimize(peak_alignment_rotation, start_calibration, args=(engine, HB2B, two_theta, roi_vec_list, out1.x, 1.452, True), method='L-BFGS-B', jac=None, bounds=([-.05, .05], [-.05, .05], [-.05, .05]), tol=None, callback=None, options={ 'disp': None, 'maxcor': 10, 'ftol': 2.220446049250313e-05, 'gtol': 1e-03, 'eps': 1e-03, 'maxfun': 100, 'maxiter': 100, 'iprint': -1, 'maxls': 20 }) out2 = least_squares(peak_alignment_rotation, start_calibration, jac='3-point', bounds=([-np.pi / 20, -np.pi / 20, -np.pi / 20], [np.pi / 20, np.pi / 20, np.pi / 20]), method='dogbox', ftol=1e-08, xtol=1e-08, gtol=1e-08, x_scale=1.0, loss='linear', f_scale=1.0, diff_step=None, tr_solver=None, tr_options={}, jac_sparsity=None, max_nfev=None, verbose=0, args=(engine, HB2B, two_theta, roi_vec_list, out1.x, 1.452, False), kwargs={}) # start_calibration = np.array( [0.0] * 7, dtype = np.float) # start_calibration[0:3] = out1.x # start_calibration[3:6] = out2.x # start_calibration[6] = 1.452 out3 = least_squares( peaks_alignment_all, start_calibration, jac='3-point', bounds=([-.05, -.05, -.05, -np.pi / 20, -np.pi / 20, -np.pi / 20, 1.4], [.05, .05, .05, np.pi / 20, np.pi / 20, np.pi / 20, 1.5]), method='dogbox', ftol=1e-08, xtol=1e-08, gtol=1e-08, x_scale=1.0, loss='linear', f_scale=1.0, diff_step=None, tr_solver=None, tr_options={}, jac_sparsity=None, max_nfev=None, verbose=0, args=(engine, HB2B, two_theta, roi_vec_list, False, False), kwargs={}) t_stop = time.time() print('Global Refine: {}'.format(t_stop1 - t_start)) print('Local Refine: {}'.format(t_stop2 - t_start)) print('Total Time: {}'.format(t_stop - t_start)) print(out1.x) print(out3.x) # if start_calibration.shape[0] == 7:CalibData = dict( zip( ['Shift_x', 'Shift_y', 'Shift_z', 'Rot_x', 'Rot_y', 'Rot_z', 'Lambda'], out3.x ) ) # else: CalibData = dict( zip( ['Shift_x', 'Shift_y', 'Shift_z', 'Rot_x', 'Rot_y', 'Rot_z'], out3.x ) ) # import json # Year, Month, Day, Hour, Min = time.localtime()[0:5] # Mono = 'Si511' # with open('/HFIR/HB2B/shared/CAL/%s/HB2B_CAL_%d%d%d%d%d.json'%( Mono, Year, Month, Day, Hour, Min), 'w') as outfile: # json.dump(CalibData, outfile) return
def main(): # # ----------------s----------------------------------------------------------- # wavelength = 1.296 # A # two_theta = 30. # Set up # data, mask and etc test_file_name = 'tests/testdata/Simulated_LaB6_rotate.tiff' # test_file_name = 'tests/testdata/LaB6_10kev_35deg-00004_Rotated_TIF.h5' Van_file_name = 'tests/testdata/Simulated_V_rotate.tiff' # instrument geometry idf_name = 'tests/testdata/xray_data/XRay_Definition_2K.txt' # 2theta two_theta = -35. # TODO - TONIGHT 1 - Make this user specified value # instrument instrument = calibration_file_io.import_instrument_setup(idf_name) # reduction engine engine = reductionengine.HB2BReductionManager() test_data_id = engine.load_data(data_file_name=test_file_name, target_dimension=2048, load_to_workspace=False) # Load, mask and reduce data if False: calibration = [ -7.86738387e-05, 9.18988206e-05, -5.55805387e-05, -1.44470481e-01, -6.45203851e-01, 1.33199903e+00 ] calibration = [ 6.03052044e-05, -5.21351130e-04, 2.45533336e-04, 7.71328703e-01, -5.45174980e-01, 1.24916005e+00 ] roi_vec_list = [30, -30, 10, -10] peaks_alignment_score(calibration, engine, instrument, two_theta, roi_vec_list, plot=True) print('RESULT EXAMINATION IS OVER') else: t_start = time.time() start_calibration = [ -3.90985615e-05, -2.72036598e-04, 3.91642084e-04, 5.99667751e-03, -8.15624721e-01, 1.42673120e+00 ] #start_calibration = [0] * 6 roi_vec_list = np.arange(-30, 30.1, 15) params = lmfit.Parameters() params.add('center_shift_x', value=start_calibration[0], min=-.1, max=.1) params.add('center_shift_y', value=start_calibration[1], min=-.1, max=.1) params.add('center_shift_z', value=start_calibration[2], min=-.1, max=.1) params.add('rotation_x', value=start_calibration[3], min=-np.pi, max=np.pi) params.add('rotation_y', value=start_calibration[4], min=-np.pi, max=np.pi) params.add('rotation_z', value=start_calibration[5], min=-np.pi, max=np.pi) #out = lmfit.minimize( peaks_alignment_score, params, method='basinhopping', args=(engine, instrument, two_theta, roi_vec_list, False) ) out = lmfit.minimize(peaks_alignment_score, params, method='lbfgsb', args=(engine, instrument, two_theta, roi_vec_list, False)) #out1 = lmfit.minimize( peaks_alignment_score, out.params, method='lbfgsb', args=(engine, instrument, two_theta, roi_vec_list, False) ) # out1 = lmfit.minimize( peaks_alignment_score, out.params, method='least_squares', args=(engine, instrument, two_theta, roi_vec_list, False) ) t_stop = time.time() print('Total Time: {}'.format(t_stop - t_start)) print(out.params) # print (out1.params) peaks_alignment_score(out.params, engine, instrument, two_theta, roi_vec_list, True) # END-IF-ELSE return