def focus_cropped(run_number, van_curves, van_int, full_inst_calib, focus_directory, focus_general, do_pre_process, params, time_period, crop_on, use_spectra): """ focus a partial run, cropping either on banks or on specific spectra @param van_curves :: the path to the vanadium curves file @param van_int :: the path to the integrated vanadium file @param full_inst_calib :: workspace containing the full instrument calibration @param run_number :: the run nuumber to focus @param focus_directory :: the user specific focus directory to save to @param focus_general :: the general focus directory to save to @param do_pre_process :: whether or not to pre-process the run before focussing it @param params :: the rebin parameters for pre-processing @param time_period :: the time period for pre-processing @param crop_on :: the bank or spectra to crop on @param use_spectra :: whether to focus by spectra or banks """ van_curves_ws, van_integrated_ws, ws_to_focus = _prepare_focus(run_number, van_curves, van_int, do_pre_process, params, time_period) tof_output_name = "engg_focus_output{0}{1}" sample_ws_clone = simple.CloneWorkspace(ws_to_focus) curves_ws_clone = simple.CloneWorkspace(van_curves_ws) # check whether to crop on bank or spectra if not use_spectra: # get the bank to crop on, focus and save it out bank = {"North": "1", "South": "2"} bank_no = bank.get(crop_on) cal_file = NORTH_BANK_CAL if bank_no == 1 else SOUTH_BANK_CAL region_calib = 'engg_calibration_bank_1' if bank_no == 1 else 'engg_calibration_bank_2' df_kwarg = {"GroupingFileName": cal_file} tof_output_name = tof_output_name.format("_bank_", bank_no) dspacing_output_name = tof_output_name + "_dSpacing" _run_focus(input_workspace=sample_ws_clone, tof_output_name=tof_output_name, vanadium_integration_ws=van_integrated_ws, vanadium_curves_ws=curves_ws_clone, df_kwarg=df_kwarg, full_calib=full_inst_calib, region_calib=region_calib) _save_out(run_number, focus_directory, focus_general, tof_output_name, "ENGINX_{}_{}{{}}", crop_on) _save_out(run_number, focus_directory, focus_general, dspacing_output_name, "ENGINX_{}_{}{{}}", crop_on) else: # crop on the spectra passed in, focus and save it out tof_output_name = tof_output_name.format("_", "cropped") dspacing_output_name = tof_output_name + "_dSpacing" grp_ws = Utils.create_grouping_workspace_from_spectra_list(crop_on, ws_to_focus) df_kwarg = {"GroupingWorkspace": grp_ws} region_calib = 'engg_calibration_cropped' _run_focus(input_workspace=sample_ws_clone, tof_output_name=tof_output_name, vanadium_integration_ws=van_integrated_ws, vanadium_curves_ws=curves_ws_clone, df_kwarg=df_kwarg, region_calib=region_calib, full_calib=full_inst_calib) _save_out(run_number, focus_directory, focus_general, tof_output_name, "ENGINX_{}_bank_{}{{}}", "cropped") _save_out(run_number, focus_directory, focus_general, dspacing_output_name, "ENGINX_{}_bank_{}{{}}", "cropped") simple.DeleteWorkspace(sample_ws_clone) simple.DeleteWorkspace(curves_ws_clone)
def focus_texture_mode(run_number, van_curves, van_int, full_inst_calib, focus_directory, focus_general, do_pre_process, params, time_period, dg_file): """ perform a texture mode focusing using the grouping csv file @param run_number :: the run number to focus @param van_curves :: the path to the vanadium curves file @param van_int :: the path to the integrated vanadium file @param full_inst_calib :: workspace containing the full instrument calibration @param focus_directory :: the user specific focus directory to save to @param focus_general :: the general focus directory to save to @param do_pre_process :: whether or not to pre-process the run before focussing it @param params :: the rebin parameters for pre-processing @param time_period :: the time period for pre-processing @param dg_file :: the grouping file to use for texture mode """ van_curves_ws, van_integrated_ws, ws_to_focus = _prepare_focus(run_number, van_curves, van_int, do_pre_process, params, time_period) banks = {} # read the csv file to work out the banks # ensure csv reading works on python 2 or 3 with open(dg_file, 'r', newline='', encoding='utf-8') as grouping_file: group_reader = csv.reader(_decomment_csv(grouping_file), delimiter=',') for row in group_reader: banks.update({row[0]: ','.join(row[1:])}) # loop through the banks described in the csv, focusing and saving them out for bank in banks: sample_ws_clone = simple.CloneWorkspace(ws_to_focus) curves_ws_clone = simple.CloneWorkspace(van_curves_ws) tof_output_name = "engg_focusing_output_ws_texture_bank_{}" tof_output_name = tof_output_name.format(bank) dspacing_output_name = tof_output_name + "_dSpacing" grp_ws = Utils.create_grouping_workspace_from_spectra_list(banks[bank], ws_to_focus) df_kwarg = {"GroupingWorkspace": grp_ws} _run_focus(input_workspace=sample_ws_clone, tof_output_name=tof_output_name, region_calib=full_inst_calib, vanadium_curves_ws=curves_ws_clone, full_calib=full_inst_calib, df_kwarg=df_kwarg, vanadium_integration_ws=van_integrated_ws) _save_out(run_number, focus_directory, focus_general, tof_output_name, "ENGINX_{}_texture_{}{{}}", bank) _save_out(run_number, focus_directory, focus_general, dspacing_output_name, "ENGINX_{}_texture_{}{{}}", bank) simple.DeleteWorkspace(sample_ws_clone) simple.DeleteWorkspace(curves_ws_clone)
def run_calibration(ceria_ws, bank, calfile, spectrum_numbers, full_calib): """ Creates Engineering calibration files with PDCalibration :param ceria_ws: The workspace with the ceria data. :param bank: The bank to crop to, both if none. :param calfile: The custom calibration file to crop to, not used if none. :param spectrum_numbers: The spectrum numbers to crop to, no crop if none. :return: dict containing calibrated diffractometer constants, and copy of the raw ceria workspace """ def run_pd_calibration(kwargs_to_pass) -> list: """ Call PDCalibration using the keyword arguments supplied, and return it's default list of output workspaces :param kwargs_to_pass: Keyword arguments to supply to the algorithm :return: List of output workspaces from PDCalibration """ return PDCalibration(**kwargs_to_pass) def calibrate_region_of_interest(ceria_d_ws, roi: str, grouping_kwarg: dict, cal_output: dict) -> None: """ Focus the processed ceria workspace (dSpacing) over the chosen region of interest, and run the calibration using this result :param ceria_d_ws: Workspace containing the processed ceria data converted to dSpacing :param roi: String describing chosen region of interest :param grouping_kwarg: Dict containing kwarg to pass to DiffractionFocussing to select the roi :param cal_output: Dictionary to append with the output of PDCalibration for the chosen roi """ # focus ceria focused_ceria = DiffractionFocussing(InputWorkspace=ceria_d_ws, **grouping_kwarg) ApplyDiffCal(InstrumentWorkspace=focused_ceria, ClearCalibration=True) ConvertUnits(InputWorkspace=focused_ceria, OutputWorkspace=focused_ceria, Target='TOF') # calibration of focused data over chosen region of interest kwargs["InputWorkspace"] = focused_ceria kwargs["OutputCalibrationTable"] = "engggui_calibration_" + roi kwargs["DiagnosticWorkspaces"] = "diag_" + roi cal_roi = run_pd_calibration(kwargs)[0] cal_output[roi] = cal_roi # need to clone the data as PDCalibration rebins ceria_raw = CloneWorkspace(InputWorkspace=ceria_ws) # initial process of ceria ws NormaliseByCurrent(InputWorkspace=ceria_ws, OutputWorkspace=ceria_ws) ApplyDiffCal(InstrumentWorkspace=ceria_ws, CalibrationWorkspace=full_calib) ConvertUnits(InputWorkspace=ceria_ws, OutputWorkspace=ceria_ws, Target='dSpacing') kwargs = { "PeakPositions": EnggUtils.default_ceria_expected_peaks(final=True), "TofBinning": [15500, -0.0003, 52000], # using a finer binning now have better stats "PeakWindow": 0.04, "MinimumPeakHeight": 0.5, "PeakFunction": 'BackToBackExponential', "CalibrationParameters": 'DIFC+TZERO+DIFA', "UseChiSq": True } cal_output = dict() grp_ws = None if (spectrum_numbers or calfile) is None: if bank == '1' or bank is None: grp_ws = EnggUtils.get_bank_grouping_workspace(1, ceria_raw) grouping_kwarg = {"GroupingWorkspace": grp_ws} calibrate_region_of_interest(ceria_ws, "bank_1", grouping_kwarg, cal_output) if bank == '2' or bank is None: grp_ws = EnggUtils.get_bank_grouping_workspace(2, ceria_raw) grouping_kwarg = {"GroupingWorkspace": grp_ws} calibrate_region_of_interest(ceria_ws, "bank_2", grouping_kwarg, cal_output) elif calfile is None: grp_ws = EnggUtils.create_grouping_workspace_from_spectra_list( spectrum_numbers, ceria_raw) grouping_kwarg = {"GroupingWorkspace": grp_ws} calibrate_region_of_interest(ceria_ws, "Cropped", grouping_kwarg, cal_output) else: grp_ws = EnggUtils.create_grouping_workspace_from_calfile( calfile, ceria_raw) grouping_kwarg = {"GroupingWorkspace": grp_ws} calibrate_region_of_interest(ceria_ws, "Custom", grouping_kwarg, cal_output) cal_params = list() # in the output calfile, rows are present for all detids, only read one from the region of interest for bank_cal in cal_output: mask_ws_name = "engggui_calibration_" + bank_cal + "_mask" mask_ws = Ads.retrieve(mask_ws_name) row_no = EnggUtils.get_first_unmasked_specno_from_mask_ws(mask_ws) row = cal_output[bank_cal].row(row_no) current_fit_params = { 'difc': row['difc'], 'difa': row['difa'], 'tzero': row['tzero'] } cal_params.append(current_fit_params) return cal_params, ceria_raw, grp_ws
def run_calibration(sample_ws, vanadium_workspace, van_integration, bank, spectrum_numbers, full_inst_calib): """ Creates Engineering calibration files with PDCalibration :param sample_ws: The workspace with the sample data. :param vanadium_workspace: The workspace with the vanadium data :param van_integration: The integration values from the vanadium corrections :param bank: The bank to crop to, both if none. :param spectrum_numbers: The spectrum numbers to crop to, no crop if none. :param full_inst_calib : workspace containing the full instrument calibration :return: The calibration output files, the vanadium curves workspace(s), and a clone of the sample file """ def run_pd_calibration(kwargs_to_pass): return simple.PDCalibration(**kwargs_to_pass) def focus_and_make_van_curves(ceria_d, vanadium_d, grouping_kwarg): # focus ceria focused_ceria = simple.DiffractionFocussing(InputWorkspace=ceria_d, **grouping_kwarg) simple.ApplyDiffCal(InstrumentWorkspace=focused_ceria, ClearCalibration=True) tof_focused = simple.ConvertUnits(InputWorkspace=focused_ceria, Target='TOF') # focus van data focused_van = simple.DiffractionFocussing(InputWorkspace=vanadium_d, **grouping_kwarg) background_van = simple.EnggEstimateFocussedBackground(InputWorkspace=focused_van, NIterations='15', XWindow=0.03) simple.DeleteWorkspace(focused_ceria) simple.DeleteWorkspace(focused_van) return tof_focused, background_van def ws_initial_process(ws): """Run some processing common to both the sample and vanadium workspaces""" simple.NormaliseByCurrent(InputWorkspace=ws, OutputWorkspace=ws) simple.ApplyDiffCal(InstrumentWorkspace=ws, CalibrationWorkspace=full_inst_calib) simple.ConvertUnits(InputWorkspace=ws, OutputWorkspace=ws, Target='dSpacing') return ws def calibrate_region_of_interest(roi, df_kwarg): focused_roi, curves_roi = focus_and_make_van_curves(ws_d, ws_van_d, df_kwarg) simple.RenameWorkspace(curves_roi, ("curves_" + roi)) curves_output.append(curves_roi) # final calibration of focused data kwargs["InputWorkspace"] = focused_roi kwargs["OutputCalibrationTable"] = "engg_calibration_" + roi kwargs["DiagnosticWorkspaces"] = "diag_" + roi cal_roi = run_pd_calibration(kwargs)[0] cal_output[roi] = cal_roi # need to clone the data as PDCalibration rebins sample_raw = simple.CloneWorkspace(InputWorkspace=sample_ws) ws_van = simple.CloneWorkspace(vanadium_workspace) ws_van_d = ws_initial_process(ws_van) # sensitivity correction ws_van_d /= van_integration simple.ReplaceSpecialValues(InputWorkspace=ws_van_d, OutputWorkspace=ws_van_d, NaNValue=0, InfinityValue=0) ws_d = ws_initial_process(sample_ws) simple.DeleteWorkspace(van_integration) kwargs = { "PeakPositions": Utils.default_ceria_expected_peaks(final=True), "TofBinning": [15500, -0.0003, 52000], # using a finer binning now have better stats "PeakWindow": 0.04, "MinimumPeakHeight": 0.5, "PeakFunction": 'BackToBackExponential', "CalibrationParameters": 'DIFC+TZERO+DIFA', "UseChiSq": True } cal_output = dict() curves_output = list() if spectrum_numbers is None: if bank == '1' or bank is None: df_kwarg = {"GroupingFileName": NORTH_BANK_CAL} calibrate_region_of_interest("bank_1", df_kwarg) if bank == '2' or bank is None: df_kwarg = {"GroupingFileName": SOUTH_BANK_CAL} calibrate_region_of_interest("bank_2", df_kwarg) else: grp_ws = Utils.create_grouping_workspace_from_spectra_list(spectrum_numbers, sample_raw) df_kwarg = {"GroupingWorkspace": grp_ws} calibrate_region_of_interest("Cropped", df_kwarg) simple.DeleteWorkspace(ws_van) simple.DeleteWorkspace("tof_focused") cal_params = list() # in the output calfile, rows are present for all detids, only read one from the region of interest bank_1_read_row = 0 bank_2_read_row = 1200 for bank_cal in cal_output: if bank_cal == "bank_1": read = bank_1_read_row elif bank_cal == "bank_2": read = bank_2_read_row else: read = int(Utils.create_spectrum_list_from_string(spectrum_numbers)[0]) # this can be int64 row = cal_output[bank_cal].row(read) current_fit_params = {'difc': row['difc'], 'difa': row['difa'], 'tzero': row['tzero']} cal_params.append(current_fit_params) return cal_params, sample_raw, curves_output