Esempio n. 1
0
 def update_counts_workspace(self,
                             run: MuonRun,
                             counts_workspace,
                             rebin=False):
     if rebin:
         self._counts_workspace_rebin.update(
             {run: MuonWorkspaceWrapper(counts_workspace)})
     else:
         self._counts_workspace.update(
             {run: MuonWorkspaceWrapper(counts_workspace)})
Esempio n. 2
0
 def update_asymmetry_workspace(
         self,
         asymmetry_workspace,
         run,
         rebin=False):
     run_object = MuonRun(run)
     if not rebin:
         self._workspace.update(
             {run_object: MuonWorkspaceWrapper(asymmetry_workspace)})
     else:
         self.workspace_rebin.update(
             {run_object: MuonWorkspaceWrapper(asymmetry_workspace)})
Esempio n. 3
0
 def update_workspaces(self,
                       counts_workspace,
                       rebin,
                       rebin_index=0,
                       rebin_option=None):
     if rebin:
         self._counts_workspace_rebin = MuonWorkspaceWrapper(
             counts_workspace)
         self.rebin_index = rebin_index
         self.rebin_option = rebin_option
     else:
         self._counts_workspace = MuonWorkspaceWrapper(counts_workspace)
Esempio n. 4
0
def load_workspace_from_filename(filename,
                                 input_properties=DEFAULT_INPUTS,
                                 output_properties=DEFAULT_OUTPUTS):
    try:
        alg, psi_data = create_load_algorithm(filename, input_properties)
        alg.execute()
    except:
        alg, psi_data = create_load_algorithm(
            filename.split(os.sep)[-1], input_properties)
        alg.execute()

    # The filename given to the loading algorithm can be different to the file that was actually loaded.
    # Pulling the filename back out of the algorithm after loading ensures that the path is accurate.
    filename = alg.getProperty("Filename").value[0]
    workspace = AnalysisDataService.retrieve(
        alg.getProperty("OutputWorkspace").valueAsStr)
    if is_workspace_group(workspace):
        # handle multi-period data
        load_result = _get_algorithm_properties(alg, output_properties)
        load_result["OutputWorkspace"] = [
            MuonWorkspaceWrapper(ws) for ws in workspace.getNames()
        ]
        run = get_run_from_multi_period_data(workspace)

        deadtime_tables = AnalysisDataService.retrieve(
            load_result["DeadTimeTable"]).getNames()
        load_result["DataDeadTimeTable"] = deadtime_tables[0]
        for table in deadtime_tables[1:]:
            DeleteWorkspace(Workspace=table)

        load_result["FirstGoodData"] = round(
            load_result["FirstGoodData"] - load_result['TimeZero'], 3)
        UnGroupWorkspace(load_result["DeadTimeTable"])
        load_result["DeadTimeTable"] = None
        UnGroupWorkspace(workspace.name())

    else:
        # single period data
        load_result = _get_algorithm_properties(alg, output_properties)
        load_result["OutputWorkspace"] = [
            MuonWorkspaceWrapper(load_result["OutputWorkspace"])
        ]
        run = int(workspace.getRunNumber())

        load_result["DataDeadTimeTable"] = load_result["DeadTimeTable"]
        load_result["DeadTimeTable"] = None
        load_result["FirstGoodData"] = round(
            load_result["FirstGoodData"] - load_result['TimeZero'], 3)

    return load_result, run, filename, psi_data
Esempio n. 5
0
 def update_asymmetry_workspace(self,
                                run: MuonRun,
                                asymmetry_workspace,
                                asymmetry_workspace_unnorm,
                                rebin=False):
     if rebin:
         self._asymmetry_estimate_rebin.update(
             {run: MuonWorkspaceWrapper(asymmetry_workspace)})
         self._asymmetry_estimate_rebin_unormalised.update(
             {run: MuonWorkspaceWrapper(asymmetry_workspace_unnorm)})
     else:
         self._asymmetry_estimate.update(
             {run: MuonWorkspaceWrapper(asymmetry_workspace)})
         self._asymmetry_estimate_unormalised.update(
             {run: MuonWorkspaceWrapper(asymmetry_workspace_unnorm)})
Esempio n. 6
0
    def show_raw_data(self):
        self.ads_observer.observeRename(False)
        for run in self.data_context.current_runs:
            with WorkspaceGroupDefinition():
                run_string = run_list_to_string(run)
                loaded_workspace = self.data_context._loaded_data.get_data(run=run, instrument=self.data_context.instrument)['workspace'][
                                       'OutputWorkspace']
                loaded_workspace_deadtime_table = self.corrections_context.get_default_dead_time_table_name_for_run(
                    self.data_context.instrument, run)
                directory = get_base_data_directory(
                    self,
                    run_string)

                deadtime_name = get_deadtime_data_workspace_name(self.data_context.instrument,
                                                                 str(run[0]), workspace_suffix=self.workspace_suffix)
                MuonWorkspaceWrapper(loaded_workspace_deadtime_table).show(directory + deadtime_name)
                self.corrections_context.set_default_dead_time_table_name_for_run(self.data_context.instrument, run,
                                                                                  deadtime_name)

                if len(loaded_workspace) > 1:
                    # Multi-period data
                    for i, single_ws in enumerate(loaded_workspace):
                        name = directory + get_raw_data_workspace_name(self.data_context.instrument, run_string,
                                                                       multi_period=True,
                                                                       period=str(i + 1),
                                                                       workspace_suffix=self.workspace_suffix)
                        single_ws.show(name)
                else:
                    # Single period data
                    name = directory + get_raw_data_workspace_name(self.data_context.instrument, run_string,
                                                                   multi_period=False,
                                                                   workspace_suffix=self.workspace_suffix)
                    loaded_workspace[0].show(name)

        self.ads_observer.observeRename(True)
Esempio n. 7
0
    def test_that_workspaces_in_existing_folders_are_not_moved_by_directory_manipulation(
            self):
        workspace1 = create_simple_workspace(data_x=[1, 2, 3, 4],
                                             data_y=[10, 10, 10, 10])
        workspace2 = create_simple_workspace(data_x=[1, 2, 3, 4],
                                             data_y=[10, 10, 10, 10])
        workspace_handle1 = MuonWorkspaceWrapper(workspace=workspace1)
        workspace_handle2 = MuonWorkspaceWrapper(workspace=workspace2)

        workspace_handle1.show("group2/ws1")
        workspace_handle2.show("group3/ws2")

        self.assert_group_workspace_exists("group2")
        self.assert_group_workspace_exists("group3")
        self.assert_workspace_in_group("ws1", "group2")
        self.assert_workspace_in_group("ws2", "group3")
Esempio n. 8
0
    def add_fitting_info_to_ADS_if_required(self, base_name,
                                            fit_workspace_name):
        if not self.view.output_fit_information:
            return

        muon_workspace_wrapper = MuonWorkspaceWrapper(fit_workspace_name)
        muon_workspace_wrapper.show()
Esempio n. 9
0
 def add_phase_table_to_ADS(self, base_name):
     run = get_run_numbers_as_string_from_workspace_name(
         base_name, self.context.data_context.instrument)
     directory = get_base_data_directory(self.context, run)
     muon_workspace_wrapper = MuonWorkspaceWrapper(directory + base_name)
     muon_workspace_wrapper.show()
     self.context.phase_context.add_phase_table(muon_workspace_wrapper)
    def test_that_workspace_property_returns_workspace_when_not_in_ADS(self):
        workspace_handle = MuonWorkspaceWrapper(workspace=self.workspace)

        ws_property = workspace_handle.workspace

        self.assertCountEqual(ws_property.readX(0), [1, 2, 3, 4])
        self.assertCountEqual(ws_property.readY(0), [10, 10, 10, 10])
Esempio n. 11
0
    def add_fft_workspace_to_ADS(self, input_workspace, imaginary_input_workspace, fft_workspace_label):
        run = re.search('[0-9]+', input_workspace).group()
        fft_workspace = mantid.AnalysisDataService.retrieve(fft_workspace_label)
        Im_run = ""
        if imaginary_input_workspace != "":
            Im_run = re.search('[0-9]+', imaginary_input_workspace).group()
        fft_workspace_name = get_fft_workspace_name(input_workspace, imaginary_input_workspace)
        directory = get_fft_workspace_group_name(fft_workspace_name, self.load.data_context.instrument,
                                                 self.load.workspace_suffix)
        Re = get_group_or_pair_from_name(input_workspace)
        Im = get_group_or_pair_from_name(imaginary_input_workspace)
        shift = 3 if fft_workspace.getNumberHistograms() == 6 else 0
        spectra = {"_" + FREQUENCY_EXTENSIONS["RE"]: 0 + shift, "_" + FREQUENCY_EXTENSIONS["IM"]: 1 + shift,
                   "_" + FREQUENCY_EXTENSIONS["MOD"]: 2 + shift}

        for spec_type in list(spectra.keys()):
            extracted_ws = extract_single_spec(fft_workspace, spectra[spec_type], fft_workspace_name + spec_type)

            self.load._frequency_context.add_FFT(fft_workspace_name + spec_type, run, Re, Im_run, Im)

            muon_workspace_wrapper = MuonWorkspaceWrapper(extracted_ws)
            muon_workspace_wrapper.show(directory + fft_workspace_name + spec_type)

        # This is a small hack to get the output name to a location where it can be part of the calculation finished
        # signal.
        self._output_workspace_name = fft_workspace_name + '_mod'
    def test_that_hiding_the_workspace_removes_it_from_ADS(self):
        workspace_handle = MuonWorkspaceWrapper(workspace=self.workspace)
        workspace_handle.show("test")

        workspace_handle.hide()

        self.assertEqual(workspace_handle.is_hidden, True)
        self.assertFalse(simpleapi.mtd.doesExist("test"))
 def populate_ADS(self):
     self._calculate_all_data()
     CreateWorkspace(
         [0], [0],
         OutputWorkspace='EMU19489; PhaseQuad; PhaseTable EMU19489')
     self.context.phase_context.add_phase_quad(
         MuonWorkspaceWrapper('EMU19489; PhaseQuad; PhaseTable EMU19489'),
         '19489')
Esempio n. 14
0
    def test_that_can_set_workspace_if_MuonWorkspace_object(self):
        group = MuonGroup(group_name="group1")
        dataX = [0, 1, 2, 3, 4, 5]
        dataY = [10, 20, 30, 20, 10]
        input_workspace = CreateWorkspace(dataX, dataY)

        self.assertEqual(group.workspace, {})
        group.workspace = MuonWorkspaceWrapper(input_workspace)
        self.assertIsNotNone(group.workspace)
Esempio n. 15
0
 def add_optional_outputs_to_ADS(self, alg, output_options, base_name, directory):
     for key in output_options:
         if key == GROUPINGTABLE and output_options[key]:
             output = GROUPINGTABLE
             self.context.ads_observer.observeRename(False)
             wrapped_workspace = MuonWorkspaceWrapper(output)
             name = directory + base_name + " " + GROUPINGTABLE
             self._optional_output_names[key] = name
             wrapped_workspace.show(name)
             self.context.ads_observer.observeRename(True)
         elif output_options[key]:
             output = alg.getProperty(key).valueAsStr
             self.context.ads_observer.observeRename(False)
             wrapped_workspace = MuonWorkspaceWrapper(output)
             name = directory + base_name + "(" + str(self.get_num_groups)+" groups) "+ optional_output_suffixes[key]
             self._optional_output_names[key] = name
             wrapped_workspace.show(name)
             self.context.ads_observer.observeRename(True)
Esempio n. 16
0
    def test_that_workspace_added_correctly_for_single_nested_structure(self):
        workspace = create_simple_workspace(data_x=[1, 2, 3, 4],
                                            data_y=[10, 10, 10, 10])
        workspace_handle = MuonWorkspaceWrapper(workspace=workspace)

        workspace_handle.show("group1/ws1")

        self.assert_group_workspace_exists("group1")
        self.assert_workspace_in_group("ws1", "group1")
Esempio n. 17
0
    def test_that_if_workspace_exists_with_same_name_as_group_then_it_is_replaced(
            self):
        simpleapi.CreateWorkspace([1, 2, 3, 4], [10, 10, 10, 10],
                                  OutputWorkspace="group")

        workspace_handle = MuonWorkspaceWrapper('group')
        workspace_handle.show("group/ws1")

        self.assert_group_workspace_exists("group")
    def test_that_showing_the_workspace_puts_it_in_ADS(self):
        workspace_handle = MuonWorkspaceWrapper(workspace=self.workspace)

        workspace_handle.show("test")

        self.assertTrue(simpleapi.mtd.doesExist("test"))
        ads_workspace = simpleapi.mtd["test"]
        self.assertCountEqual(ads_workspace.readX(0), [1, 2, 3, 4])
        self.assertCountEqual(ads_workspace.readY(0), [10, 10, 10, 10])
Esempio n. 19
0
def __default_workspace():
    default_instrument = get_default_instrument()
    workspace = api.WorkspaceFactoryImpl.Instance().create(
        "Workspace2D", 2, 10, 10)
    workspace = run_LoadInstrument({
        "Workspace": workspace,
        "RewriteSpectraMap": True,
        "InstrumentName": default_instrument
    })
    return MuonWorkspaceWrapper(workspace)
Esempio n. 20
0
    def test_that_can_only_set_workspace_if_MuonWorkspace_object(self):
        pair = MuonPair(pair_name="pair1")
        self.assertEqual(pair.workspace, {})
        dataX = [0, 1, 2, 3, 4, 5]
        dataY = [10, 20, 30, 20, 10]
        input_workspace = CreateWorkspace(dataX, dataY)

        workspace_wrapper = MuonWorkspaceWrapper(input_workspace)
        pair.workspace = workspace_wrapper
        self.assertEqual(pair.workspace, workspace_wrapper)
    def test_that_setting_a_new_workspace_removes_the_previous_one_from_the_ADS(
            self):
        workspace_handle = MuonWorkspaceWrapper(workspace=self.workspace)
        workspace_handle.show("name1")

        workspace2 = create_simple_workspace(data_x=[5, 6, 7, 8],
                                             data_y=[20, 20, 20, 20])

        self.assertTrue(simpleapi.mtd.doesExist("name1"))
        workspace_handle.workspace = workspace2
        self.assertFalse(simpleapi.mtd.doesExist("name1"))
Esempio n. 22
0
    def create_fake_workspace(self, name):
        workspace_mock = CreateSampleWorkspace(StoreInADS=False)
        LoadInstrument(Workspace=workspace_mock,
                       InstrumentName='EMU',
                       RewriteSpectraMap=False,
                       StoreInADS=False)

        return {
            'OutputWorkspace': [MuonWorkspaceWrapper(workspace_mock)],
            'MainFieldDirection': 'transverse'
        }
Esempio n. 23
0
def combine_loaded_runs(model, run_list, delete_added=False):
    period_list = [model._data_context.num_periods([run]) for run in run_list]
    if max(period_list) != min(period_list):
        raise RuntimeError(
            'Inconsistent periods across co-added runs. This is not supported.'
        )
    return_ws = model._loaded_data_store.get_data(
        run=[run_list[0]])["workspace"]
    running_total = []

    for index in range(min(period_list)):
        workspace = return_ws["OutputWorkspace"][index]
        running_total_item = workspace.workspace.name() + 'CoAdd'
        CloneWorkspace(InputWorkspace=workspace.workspace.name(),
                       OutputWorkspace=running_total_item)
        for run in run_list[1:]:
            ws = model._loaded_data_store.get_data(
                run=[run])["workspace"]["OutputWorkspace"][index].workspace
            Plus(LHSWorkspace=running_total_item,
                 RHSWorkspace=ws,
                 AllowDifferentNumberSpectra=False,
                 OutputWorkspace=running_total_item)

        running_total.append(running_total_item)

    return_ws_actual = {
        key: return_ws[key]
        for key in ['MainFieldDirection', 'TimeZero', 'FirstGoodData']
    }
    try:
        return_ws_actual['DetectorGroupingTable'] = return_ws[
            'DetectorGroupingTable']
    except KeyError:
        pass  # PSI Data does not include Detector Grouping table as it's read from sample logs instead
    try:
        return_ws_actual['DeadTimeTable'] = return_ws['DeadTimeTable']
    except KeyError:
        pass  # Again, PSI data does not always include DeadTimeTable either
    return_ws_actual["OutputWorkspace"] = [
        MuonWorkspaceWrapper(running_total_period)
        for running_total_period in running_total
    ]
    return_ws_actual['DataDeadTimeTable'] = CloneWorkspace(
        InputWorkspace=return_ws['DataDeadTimeTable'],
        OutputWorkspace=return_ws['DataDeadTimeTable'] + 'CoAdd').name()
    model._loaded_data_store.remove_data(
        run=flatten_run_list(run_list),
        instrument=model._data_context.instrument)
    model._loaded_data_store.add_data(
        run=flatten_run_list(run_list),
        workspace=return_ws_actual,
        filename="Co-added",
        instrument=model._data_context.instrument)
Esempio n. 24
0
    def handleFinished(self):
        self.activate()
        self.calculation_finished_notifier.notify_subscribers(
            self._maxent_output_workspace_name)
        # if phase table is outputed
        if self.view.output_reconstructed_spectra and SPECTRA in self._optional_output_names.keys(
        ):
            ws = MuonWorkspaceWrapper(
                self._optional_output_names[SPECTRA]).workspace
            if self.use_groups:
                table = MuonWorkspaceWrapper(
                    self._optional_output_names[GROUPINGTABLE]).workspace
                self.new_reconstructed_data.notify_subscribers({
                    "ws":
                    ws.name(),
                    "table":
                    table.name()
                })
            else:
                self.new_reconstructed_data.notify_subscribers({
                    "ws": ws.name(),
                    "table": None
                })

        if self.view.output_phase_table and PHASETABLE in self._optional_output_names.keys(
        ):
            name = self._optional_output_names[PHASETABLE]
            if self.use_groups:
                num_groups = self.get_num_groups
                self.context.frequency_context.add_group_phase_table(
                    MuonWorkspaceWrapper(name), num_groups)
            else:
                self.context.phase_context.add_phase_table(
                    MuonWorkspaceWrapper(name))
                self.new_phase_table.notify_subscribers()
            self.update_phase_table_options()
        # clear optional outputs
        self._optional_output_names = {}
Esempio n. 25
0
    def _calculate_phasequads(self, phasequad_obj, rebin):
        for run in self._data_context.current_runs:
            if self._data_context.num_periods(run) > 1:
                raise ValueError("Cannot support multiple periods")

            ws_list = self.calculate_phasequad(phasequad_obj, run, rebin)
            run_string = run_list_to_string(run)
            directory = get_base_data_directory(self, run_string)
            for ws in ws_list:
                muon_workspace_wrapper = MuonWorkspaceWrapper(directory + ws)
                muon_workspace_wrapper.show()

            phasequad_obj.update_asymmetry_workspaces(ws_list,
                                                      run,
                                                      rebin=rebin)
Esempio n. 26
0
    def add_maxent_workspace_to_ADS(self, input_workspace, maxent_workspace, alg):
        run = re.search('[0-9]+', input_workspace).group()
        base_name = get_maxent_workspace_name(input_workspace, self.view.get_method)
        directory = get_maxent_workspace_group_name(base_name, self.context.data_context.instrument, self.context.workspace_suffix)

        muon_workspace_wrapper = MuonWorkspaceWrapper(directory + base_name)
        muon_workspace_wrapper.show()

        maxent_output_options = self.get_maxent_output_options()
        self.context.frequency_context.add_maxEnt(run, maxent_workspace)
        self.add_optional_outputs_to_ADS(alg, maxent_output_options, base_name, directory)

        # Storing this on the class so it can be sent as part of the calculation
        # finished signal.
        self._maxent_output_workspace_name = base_name
    def test_that_any_multi_period_data_will_mark_everything_as_multiperiod(
            self):
        multi_period_worspace_list = [
            MuonWorkspaceWrapper(f'raw_data_{period_index + 1}')
            for period_index in range(4)
        ]
        load_result = {'OutputWorkspace': multi_period_worspace_list}
        self.loaded_data.add_data(workspace=load_result,
                                  run=[84447],
                                  filename='workspace_filename',
                                  instrument='EMU')
        self.context._current_runs = [[84447], [19489]]

        is_multi_period = self.context.is_multi_period()

        self.assertTrue(is_multi_period)
    def test_that_initialized_object_starts_with_empty_string_for_name(self):
        workspace_handle = MuonWorkspaceWrapper(workspace=self.workspace)

        self.assertEqual(workspace_handle.name, "")
    def test_that_initialized_object_is_not_in_ADS_by_default(self):
        workspace_handle = MuonWorkspaceWrapper(workspace=self.workspace)

        self.assertEqual(workspace_handle.is_hidden, True)
        self.assertEqual(simpleapi.mtd.size(), 0)
    def test_that_can_initialize_with_TableWorkspace_object(self):
        table_workspace = create_simple_table_workspace()
        assert isinstance(table_workspace, ITableWorkspace)

        MuonWorkspaceWrapper(workspace=table_workspace)