def test_generate_spline_vanadium_name(self):
        reference_vanadium_name = "foo_123"
        sample_arg_one = "arg1"
        sample_arg_two = 987

        # Check that it correctly processes unnamed args
        output = common.generate_splined_name(reference_vanadium_name, sample_arg_one, sample_arg_two)
        expected_output = "VanSplined_" + reference_vanadium_name + '_' + sample_arg_one + '_' + str(sample_arg_two)
        expected_output += '.nxs'
        self.assertEqual(expected_output, output)

        # Check it can handle lists to append
        sample_arg_list = ["bar", "baz", 567]

        expected_output = "VanSplined_" + reference_vanadium_name
        for arg in sample_arg_list:
            expected_output += '_' + str(arg)
        expected_output += '.nxs'
        output = common.generate_splined_name(reference_vanadium_name, sample_arg_list)
        self.assertEqual(expected_output, output)

        # Check is can handle mixed inputs
        expected_output = "VanSplined_" + reference_vanadium_name + '_' + sample_arg_one
        for arg in sample_arg_list:
            expected_output += '_' + str(arg)
        expected_output += '_' + str(sample_arg_two) + '.nxs'

        output = common.generate_splined_name(reference_vanadium_name, sample_arg_one, sample_arg_list, sample_arg_two)
        self.assertEqual(expected_output, output)
Example #2
0
    def test_generate_spline_vanadium_name(self):
        reference_vanadium_name = "foo_123"
        sample_arg_one = "arg1"
        sample_arg_two = 987

        # Check that it correctly processes unnamed args
        output = common.generate_splined_name(reference_vanadium_name,
                                              sample_arg_one, sample_arg_two)
        expected_output = "VanSplined_" + reference_vanadium_name + '_' + sample_arg_one + '_' + str(
            sample_arg_two)
        expected_output += '.nxs'
        self.assertEqual(expected_output, output)

        # Check it can handle lists to append
        sample_arg_list = ["bar", "baz", 567]

        expected_output = "VanSplined_" + reference_vanadium_name
        for arg in sample_arg_list:
            expected_output += '_' + str(arg)
        expected_output += '.nxs'
        output = common.generate_splined_name(reference_vanadium_name,
                                              sample_arg_list)
        self.assertEqual(expected_output, output)

        # Check is can handle mixed inputs
        expected_output = "VanSplined_" + reference_vanadium_name + '_' + sample_arg_one
        for arg in sample_arg_list:
            expected_output += '_' + str(arg)
        expected_output += '_' + str(sample_arg_two) + '.nxs'

        output = common.generate_splined_name(reference_vanadium_name,
                                              sample_arg_one, sample_arg_list,
                                              sample_arg_two)
        self.assertEqual(expected_output, output)
Example #3
0
def _get_customisable_attributes(cal_dict, inst_settings, empty_run_call,
                                 grouping_name_call, vanadium_run_call,
                                 splined_name_list):
    dict_to_return = {}
    if empty_run_call:
        empty_runs = empty_run_call.get_result()
    else:
        empty_runs = common.cal_map_dictionary_key_helper(
            dictionary=cal_dict, key="empty_run_numbers")
    dict_to_return["empty_runs"] = empty_runs

    if vanadium_run_call:
        vanadium_runs = vanadium_run_call.get_result()
    else:
        vanadium_runs = common.cal_map_dictionary_key_helper(
            dictionary=cal_dict, key="vanadium_run_numbers")
    dict_to_return["vanadium_runs"] = vanadium_runs

    if grouping_name_call:
        grouping_name = grouping_name_call.get_result()
    else:
        grouping_name = inst_settings.grouping_file_name
    dict_to_return["grouping_file_name"] = grouping_name

    dict_to_return["splined_van_name"] = common.generate_splined_name(
        vanadium_runs, splined_name_list)

    return dict_to_return
Example #4
0
    def update_file_paths(self, inst_settings, new_splined_name_list):
        """Updates the file path for splined, unsplined and summed_empty files using a new splined name list,
        this is necessary on instruments where the path may change e.g. Pearl due to long-mode
        :param inst_settings The current Instrument settings
        :param new_splined_name_list  List of unique properties to generate a splined vanadium name from
        """

        cal_map_dict = get_cal_mapping_dict(
            run_number_string=self.output_run_string,
            cal_mapping_path=inst_settings.cal_mapping_path)
        offset_file_name = common.cal_map_dictionary_key_helper(
            dictionary=cal_map_dict, key="offset_file_name")

        # Prepend the properties used for creating a van spline so we can fingerprint the output files
        splined_list = new_splined_name_list if new_splined_name_list else []
        splined_list.append(os.path.basename(offset_file_name))

        splined_van_name = common.generate_splined_name(
            self.vanadium_run_numbers, splined_list)
        self.splined_vanadium_file_path = os.path.join(self.van_paths,
                                                       splined_van_name)

        unsplined_van_name = common.generate_unsplined_name(
            self.vanadium_run_numbers, splined_list)
        self.unsplined_vanadium_file_path = os.path.join(
            self.van_paths, unsplined_van_name)

        summed_empty_name = common.generate_summed_empty_name(
            self.empty_runs, splined_list)
        self.summed_empty_file_path = os.path.join(self.van_paths,
                                                   summed_empty_name)
Example #5
0
    def create_solid_angle_corrections(self, vanadium, run_details):
        """
        Creates the solid angle corrections from a vanadium run, only applicable on HRPD otherwise return None
        :param vanadium: The vanadium used to create this
        :param run_details: the run details of to use
        """
        settings = self._inst_settings
        if not settings.do_solid_angle:
            return
        solid_angle = mantid.SolidAngle(InputWorkspace=vanadium)
        solid_angle = mantid.Scale(InputWorkspace=solid_angle, Factor=100, Operation='Multiply')

        eff = mantid.Divide(LHSWorkspace=vanadium, RHSWorkspace=solid_angle)
        eff = mantid.ConvertUnits(InputWorkspace=eff, Target='Wavelength')
        integration_range = settings.eff_integration_range
        # use full range if no range is supplied
        integration_range = integration_range if integration_range is not None else (None, None)
        eff = mantid.Integration(InputWorkspace=eff,
                                 RangeLower=integration_range[0],
                                 RangeUpper=integration_range[1])

        correction = mantid.Multiply(LHSWorkspace=solid_angle, RHSWorkspace=eff)
        correction = mantid.Scale(InputWorkspace=correction, Factor=1e-5,
                                  Operation='Multiply')
        name = "sac" + common.generate_splined_name(run_details.run_number, [])
        path = run_details.van_paths

        mantid.SaveNexus(InputWorkspace=correction, Filename=os.path.join(path, name))
        common.remove_intermediate_workspace(eff)
        common.remove_intermediate_workspace(correction)
Example #6
0
    def create_solid_angle_corrections(self, vanadium, run_details):
        """
        Creates the solid angle corrections from a vanadium run, only applicable on HRPD otherwise return None
        :param vanadium: The vanadium used to create this
        :param run_details: the run details of to use
        """
        if not self._inst_settings.do_solid_angle:
            return
        solid_angle = mantid.SolidAngle(InputWorkspace=vanadium)

        scale = mantid.CreateSingleValuedWorkspace(DataValue='100')
        correction = mantid.Multiply(LHSWorkspace=solid_angle,
                                     RHSWorkspace=scale)

        eff = mantid.Divide(LHSWorkspace=vanadium, RHSWorkspace=correction)
        eff = mantid.ConvertUnits(InputWorkspace=eff, Target='Wavelength')
        eff = mantid.Integration(InputWorkspace=eff,
                                 RangeLower='1.3999999999999999',
                                 RangeUpper='3')

        correction = mantid.Multiply(LHSWorkspace=correction, RHSWorkspace=eff)
        scale = mantid.CreateSingleValuedWorkspace(DataValue='100000')
        correction = mantid.Divide(LHSWorkspace=correction, RHSWorkspace=scale)

        name = "sac" + common.generate_splined_name(run_details.run_number, [])
        path = run_details.van_paths

        mantid.SaveNexus(InputWorkspace=correction,
                         Filename=os.path.join(path, name))

        common.remove_intermediate_workspace(solid_angle)
        common.remove_intermediate_workspace(scale)
        common.remove_intermediate_workspace(eff)
        common.remove_intermediate_workspace(correction)
Example #7
0
 def get_solid_angle_corrections(self, vanadium, run_details):
     if not self._inst_settings.do_solid_angle:
         return
     name = "sac" + common.generate_splined_name(vanadium, [])
     path = run_details.van_paths
     try:
         solid_angle = mantid.Load(Filename=os.path.join(path,name))
         return solid_angle
     except ValueError:
         raise RuntimeError("Could not find " + os.path.join(path, name)+" please run create_vanadium with "
                                                                         "\"do_solid_angle_corrections=True\"")
Example #8
0
def create_run_details_object(run_number_string,
                              inst_settings,
                              is_vanadium_run,
                              empty_run_number,
                              grouping_file_name,
                              vanadium_string,
                              splined_name_list=None,
                              van_abs_file_name=None):
    """
    Creates and returns a run details object which holds various
    properties about the current run.
    :param run_number_string: The user string for the current run
    :param inst_settings: The current instrument object
    :param is_vanadium_run: Boolean of if the current run is a vanadium run
    :param empty_run_number: Empty run number(s) from mapping file
    :param grouping_file_name: Filename of the grouping file found in the calibration folder
    :param vanadium_string: Vanadium run number(s) from mapping file
    :param splined_name_list: (Optional) List of unique properties to generate a splined vanadium name from
    :param van_abs_file_name: (Optional) The name of the vanadium absorption file
    :return: RunDetails object with attributes set to applicable values
    """
    cal_map_dict = get_cal_mapping_dict(
        run_number_string=run_number_string,
        cal_mapping_path=inst_settings.cal_mapping_path)

    run_number = common.get_first_run_number(
        run_number_string=run_number_string)

    # Get names of files we will be using
    calibration_dir = os.path.normpath(
        os.path.expanduser(inst_settings.calibration_dir))
    label = common.cal_map_dictionary_key_helper(dictionary=cal_map_dict,
                                                 key="label")
    offset_file_name = common.cal_map_dictionary_key_helper(
        dictionary=cal_map_dict, key="offset_file_name")

    # Prepend the properties used for creating a van spline so we can fingerprint the file
    new_splined_list = splined_name_list if splined_name_list else []
    new_splined_list.append(os.path.basename(offset_file_name))

    splined_van_name = common.generate_splined_name(vanadium_string,
                                                    new_splined_list)
    unsplined_van_name = common.generate_unsplined_name(
        vanadium_string, new_splined_list)

    if is_vanadium_run:
        # The run number should be the vanadium number in this case
        run_number = vanadium_string

    output_run_string = vanadium_string if is_vanadium_run else run_number_string

    # Get the file extension if set
    file_extension = getattr(inst_settings, "file_extension")
    if file_extension:
        # Prefix dot if user has forgotten to
        file_extension = file_extension if file_extension.startswith(
            '.') else '.' + file_extension

    # Get the output name suffix if set
    suffix = getattr(inst_settings, "suffix", None)

    # Sample empty if there is one as this is instrument specific
    sample_empty = getattr(inst_settings, "sample_empty", None)

    # By default, offset file sits in the calibration folder, but it can also be given as an absolute path
    if os.path.exists(offset_file_name):
        offset_file_path = offset_file_name
    else:
        offset_file_path = os.path.join(calibration_dir, label,
                                        offset_file_name)

    # Generate the paths
    grouping_file_path = os.path.join(calibration_dir, grouping_file_name)
    van_paths = os.path.join(calibration_dir, label)
    splined_van_path = os.path.join(van_paths, splined_van_name)
    unsplined_van_path = os.path.join(van_paths, unsplined_van_name)
    van_absorb_path = os.path.join(
        calibration_dir, van_abs_file_name) if van_abs_file_name else None

    return _RunDetails(empty_run_number=empty_run_number,
                       file_extension=file_extension,
                       run_number=run_number,
                       output_run_string=output_run_string,
                       label=label,
                       offset_file_path=offset_file_path,
                       grouping_file_path=grouping_file_path,
                       splined_vanadium_path=splined_van_path,
                       vanadium_run_number=vanadium_string,
                       sample_empty=sample_empty,
                       vanadium_abs_path=van_absorb_path,
                       unsplined_vanadium_path=unsplined_van_path,
                       output_suffix=suffix,
                       van_paths=van_paths)
Example #9
0
def create_run_details_object(run_number_string, inst_settings, is_vanadium_run, empty_run_number,
                              grouping_file_name, vanadium_string, splined_name_list=None, van_abs_file_name=None):
    """
    Creates and returns a run details object which holds various
    properties about the current run.
    :param run_number_string: The user string for the current run
    :param inst_settings: The current instrument object
    :param is_vanadium_run: Boolean of if the current run is a vanadium run
    :param empty_run_number: Empty run number(s) from mapping file
    :param grouping_file_name: Filename of the grouping file found in the calibration folder
    :param vanadium_string: Vanadium run number(s) from mapping file
    :param splined_name_list: (Optional) List of unique properties to generate a splined vanadium name from
    :param van_abs_file_name: (Optional) The name of the vanadium absorption file
    :return: RunDetails object with attributes set to applicable values
    """
    cal_map_dict = get_cal_mapping_dict(run_number_string=run_number_string,
                                        cal_mapping_path=inst_settings.cal_mapping_path)

    run_number = common.get_first_run_number(run_number_string=run_number_string)

    # Get names of files we will be using
    calibration_dir = os.path.normpath(os.path.expanduser(inst_settings.calibration_dir))
    label = common.cal_map_dictionary_key_helper(dictionary=cal_map_dict, key="label")
    offset_file_name = common.cal_map_dictionary_key_helper(dictionary=cal_map_dict, key="offset_file_name")

    # Prepend the properties used for creating a van spline so we can fingerprint the file
    new_splined_list = splined_name_list if splined_name_list else []
    new_splined_list.append(os.path.basename(offset_file_name))

    splined_van_name = common.generate_splined_name(vanadium_string, new_splined_list)
    unsplined_van_name = common.generate_unsplined_name(vanadium_string, new_splined_list)

    if is_vanadium_run:
        # The run number should be the vanadium number in this case
        run_number = vanadium_string

    output_run_string = vanadium_string if is_vanadium_run else run_number_string

    # Get the file extension if set
    file_extension = getattr(inst_settings, "file_extension")
    if file_extension:
        # Prefix dot if user has forgotten to
        file_extension = file_extension if file_extension.startswith('.') else '.' + file_extension

    # Get the output name suffix if set
    suffix = getattr(inst_settings, "suffix", None)

    # Sample empty if there is one as this is instrument specific
    sample_empty = getattr(inst_settings, "sample_empty", None)

    # By default, offset file sits in the calibration folder, but it can also be given as an absolute path
    if os.path.exists(offset_file_name):
        offset_file_path = offset_file_name
    else:
        offset_file_path = os.path.join(calibration_dir, label, offset_file_name)

    # Generate the paths
    grouping_file_path = os.path.join(calibration_dir,  grouping_file_name)
    splined_van_path = os.path.join(calibration_dir, label, splined_van_name)
    unsplined_van_path = os.path.join(calibration_dir, label, unsplined_van_name)
    van_absorb_path = os.path.join(calibration_dir, van_abs_file_name) if van_abs_file_name else None

    return _RunDetails(empty_run_number=empty_run_number, file_extension=file_extension,
                       run_number=run_number, output_run_string=output_run_string, label=label,
                       offset_file_path=offset_file_path, grouping_file_path=grouping_file_path,
                       splined_vanadium_path=splined_van_path, vanadium_run_number=vanadium_string,
                       sample_empty=sample_empty, vanadium_abs_path=van_absorb_path,
                       unsplined_vanadium_path=unsplined_van_path, output_suffix=suffix)