Exemple #1
0
    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
Exemple #2
0
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