Example #1
0
    def _publish_to_ADS(self, sample_quartiles):
        output_workspaces = []
        for key in sample_quartiles:
            output_workspaces.append(MaskingQuadrant.to_string(key))
            AnalysisDataService.addOrReplace(MaskingQuadrant.to_string(key), sample_quartiles[key])

        return output_workspaces
Example #2
0
def run_integral(integral_ranges, mask, integral, detector, state):
    ranges = parse_range(integral_ranges)
    input_workspaces = load_workspace(state)

    is_multi_range = len(ranges) > 1

    output_workspaces = []
    for input_workspace in input_workspaces:
        input_workspace_name = input_workspace.name()
        if is_multi_range:
            AnalysisDataService.remove(input_workspace_name + '_ranges')
        input_workspace = crop_workspace(DetectorType.to_string(detector),
                                         input_workspace)

        if mask:
            input_workspace = apply_mask(state, input_workspace,
                                         DetectorType.to_string(detector))

        x_dim, y_dim = get_detector_size_from_sans_file(state, detector)

        output_workspace = integrate_ranges(ranges, integral, mask, detector,
                                            input_workspace_name,
                                            input_workspace, x_dim, y_dim,
                                            is_multi_range)
        plot_graph(output_workspace)

        output_workspaces.append(output_workspace)

    return output_workspaces
def run_integral(integral_ranges, mask, integral, detector, state):
    ranges = parse_range(integral_ranges)
    input_workspaces = load_workspace(state)

    is_multi_range = len (ranges) > 1

    output_workspaces = []
    for input_workspace in input_workspaces:
        input_workspace_name = input_workspace.name()
        if is_multi_range:
            AnalysisDataService.remove(input_workspace_name + '_ranges')
        input_workspace = crop_workspace(DetectorType.to_string(detector), input_workspace)

        if mask:
            input_workspace = apply_mask(state, input_workspace, DetectorType.to_string(detector))

        x_dim, y_dim = get_detector_size_from_sans_file(state, detector)

        output_workspace = integrate_ranges(ranges, integral, mask, detector, input_workspace_name, input_workspace, x_dim, y_dim,
                                            is_multi_range)
        plot_graph(output_workspace)

        output_workspaces.append(output_workspace)

    return output_workspaces
    def remove_workspace_info_from_plot(
            self, workspace_plot_info_list: List[WorkspacePlotInformation]):
        # We reverse the workspace info list so that we can maintain a unique color queue
        # See _update_color_queue_on_workspace_removal for more
        workspace_plot_info_list.reverse()
        for workspace_plot_info in workspace_plot_info_list:
            workspace_name = workspace_plot_info.workspace_name
            if not AnalysisDataService.Instance().doesExist(workspace_name):
                continue

            workspace = AnalysisDataService.Instance().retrieve(workspace_name)
            for plotted_information in self._plot_information_list.copy():
                if workspace_plot_info.workspace_name == plotted_information.workspace_name and \
                        workspace_plot_info.axis == plotted_information.axis:
                    self._update_color_queue_on_workspace_removal(
                        workspace_plot_info.axis, workspace_name)
                    axis = self.fig.axes[workspace_plot_info.axis]
                    axis.remove_workspace_artists(workspace)
                    self._plot_information_list.remove(plotted_information)
                    # clear shaded regions from plot
                    if len(
                            axis.collections
                    ) > 0 and plotted_information.workspace_name in self._shaded_regions.keys(
                    ):
                        self._shaded_regions[
                            plotted_information.workspace_name].remove()
        # If we have no plotted lines, reset the color cycle
        if self.num_plotted_workspaces == 0:
            self._reset_color_cycle()
Example #5
0
    def run_integral(self, integral_ranges, mask, integral, detector, state):
        ranges = self._parse_range(integral_ranges)
        input_workspaces = self._load_workspace(state)

        is_multi_range = len(ranges) > 1

        output_workspaces = []
        for input_workspace in input_workspaces:
            input_workspace_name = input_workspace.name()
            if is_multi_range:
                AnalysisDataService.remove(input_workspace_name + '_ranges')
            input_workspace = self._crop_workspace(detector.value,
                                                   input_workspace)

            if mask:
                input_workspace = self._apply_mask(state, input_workspace,
                                                   detector.value)

            x_dim, y_dim = self._get_detector_size_from_sans_file(
                state, detector)

            output_workspace = self._integrate_ranges(ranges, integral, mask,
                                                      detector,
                                                      input_workspace_name,
                                                      input_workspace, x_dim,
                                                      y_dim, is_multi_range)
            self._plot_graph(output_workspace)

            output_workspaces.append(output_workspace)

        return output_workspaces
Example #6
0
    def test_calibrate_bank(self):

        # control bank, it has no problems. Thus, no mask to be created
        calibration, mask = calibrate_bank(self.cases['123455_bank20'], 'bank20', 'calibration_table')
        assert calibration.rowCount() == 256 * 16
        assert calibration.columnCount() == 3
        assert AnalysisDataService.doesExist('calibration_table')
        assert mask is None
        assert AnalysisDataService.doesExist('MaskTable') is False

        # tubes 3, 8, and 13 have very faint wire shadows. Thus, mask these tubes
        calibration, mask = calibrate_bank(self.cases['124023_bank14'], 'bank14',
                                           calibration_table='calibration_table')
        assert calibration.rowCount() == 256 * (16 - 3)
        assert calibration.columnCount() == 2  # Detector ID, Position
        assert AnalysisDataService.doesExist('calibration_table')
        assert mask.rowCount() == 256 * 3
        assert mask.columnCount() == 1
        assert AnalysisDataService.doesExist('MaskTable')

        # check for the summary workspace
        calibrate_bank(self.cases['123455_bank20'], 'bank20', 'calibration_table',
                       criterium_kwargs=dict(summary='summary'))
        assert AnalysisDataService.doesExist('summary')
        workspace = mtd['summary']
        axis = workspace.getAxis(1)
        assert [axis.label(workspace_index) for workspace_index in (0, 1, 2)] == ['success', 'deviation', 'Z-score']
        self.assertEqual(min(workspace.readY(0)), 1.0)
        self.assertAlmostEqual(max(workspace.readY(2)), 1.728, delta=0.001)

        DeleteWorkspaces(['calibration_table', 'MaskTable', 'summary'])  # a bit of clean-up
Example #7
0
    def test_calibrate_bank(self):

        # control bank, it has no problems. Thus, no mask to be created
        calibration, mask = calibrate_bank(self.cases['123455_bank20'], 'bank20', 'calibration_table')
        assert calibration.rowCount() == 256 * 16
        assert calibration.columnCount() == 2
        assert AnalysisDataService.doesExist('calibration_table')
        assert mask is None
        assert AnalysisDataService.doesExist('MaskTable') is False
        DeleteWorkspaces(['calibration_table'])  # clean-up

        # beam center intensity spills over adjacent tubes, tube15 and tube16
        calibration, mask = calibrate_bank(self.cases['123454_bank58'], 'bank58', 'calibration_table')
        assert calibration.rowCount() == 256 * (16 - 2)
        assert calibration.columnCount() == 2  # Detector ID, Position
        assert AnalysisDataService.doesExist('calibration_table')
        assert mask.rowCount() == 256 * 2
        assert mask.columnCount() == 1
        assert AnalysisDataService.doesExist('MaskTable')
        DeleteWorkspaces(['calibration_table', 'MaskTable'])  # clean-up

        # check for the fits workspace
        calibrate_bank(self.cases['123455_bank20'], 'bank20', 'calibration_table', fit_results='fits')
        assert AnalysisDataService.doesExist('fits')
        workspace = mtd['fits']
        axis = workspace.getAxis(1)
        labels = [axis.label(i) for i in range(workspace.getNumberHistograms())]
        assert labels == ['success', 'deviation', 'Z-score', 'A0', 'A1', 'A2']
        assert_allclose(workspace.readY(0), [1.0] * TUBES_IN_BANK)  # success status for first tube
        self.assertAlmostEqual(max(workspace.readY(2)), 2.73, delta=0.01)  # maximum Z-score
        self.assertAlmostEqual(max(workspace.readY(3)), -0.445, delta=0.001)  # maximum A0 value
        self.assertAlmostEqual(max(workspace.readE(3)), 1.251, delta=0.001)  # maximum A0 error
        DeleteWorkspaces(['calibration_table', 'fits'])  # a bit of clean-up
Example #8
0
    def test_fit_bank(self):
        with self.assertRaises(AssertionError) as exception_info:
            fit_bank('I_am_not_here', 'bank42')
        assert 'Input workspace I_am_not_here does not exists' in str(exception_info.exception)

        control = self.cases['123455_bank20']  # a bank with a reasonable wire scan
        with self.assertRaises(AssertionError) as exception_info:
            fit_bank(control, 'bank20', shadow_height=-1000)
        assert 'shadow height must be positive' in str(exception_info.exception)

        with self.assertRaises(AssertionError) as exception_info:
            fit_bank(control, 'tree/bank51')
        assert 'The bank name must be of the form' in str(exception_info.exception)

        with self.assertRaises(AssertionError) as exception_info:
            fit_bank(self.cases['123555_bank20'], 'bank20')
        assert 'Insufficient counts per pixel in workspace CORELLI_123555_bank20' in str(exception_info.exception)

        fit_bank(control, 'bank20')
        # Expect default name for calibration table
        assert AnalysisDataService.doesExist('CalibTable')
        # Expect default name for peak pixel position table
        assert AnalysisDataService.doesExist('PeakTable')
        DeleteWorkspaces(['CalibTable', 'PeakTable'])

        fit_bank(control, 'bank20', calibration_table='table_20', peak_pixel_positions_table='pixel_20')
        assert AnalysisDataService.doesExist('table_20')
        assert AnalysisDataService.doesExist('pixel_20')

        DeleteWorkspaces(['CalibTable', 'PeakTable'])  # a bit of clean-up
    def _publish_to_ADS(sample_quartiles):
        output_workspaces = []
        for key in sample_quartiles:
            assert isinstance(key, MaskingQuadrant)
            output_workspaces.append(key.value)
            AnalysisDataService.addOrReplace(key.value, sample_quartiles[key])

        return output_workspaces
    def PyExec(self):
        # Read the state
        state_property_manager = self.getProperty("SANSState").value
        state = create_deserialized_sans_state_from_property_manager(
            state_property_manager)

        # --------------------------------------
        # Get the monitor normalization workspace
        # --------------------------------------
        monitor_normalization_workspace = self._get_monitor_normalization_workspace(
            state)

        # --------------------------------------
        # Get the calculated transmission
        # --------------------------------------
        calculated_transmission_workspace, unfitted_transmission_workspace =\
            self._get_calculated_transmission_workspace(state)

        # --------------------------------------
        # Get the wide angle correction workspace
        # --------------------------------------
        wave_length_and_pixel_adjustment_workspace = self._get_wide_angle_correction_workspace(
            state, calculated_transmission_workspace)  # noqa

        # --------------------------------------------
        # Get the full wavelength and pixel adjustment
        # --------------------------------------------
        wave_length_adjustment_workspace, \
        pixel_length_adjustment_workspace = self._get_wavelength_and_pixel_adjustment_workspaces(state,
                                                                            monitor_normalization_workspace,  # noqa
                                                                            calculated_transmission_workspace)  # noqa

        if wave_length_adjustment_workspace:
            self.setProperty("OutputWorkspaceWavelengthAdjustment",
                             wave_length_adjustment_workspace)
        if pixel_length_adjustment_workspace:
            self.setProperty("OutputWorkspacePixelAdjustment",
                             pixel_length_adjustment_workspace)
        if wave_length_and_pixel_adjustment_workspace:
            self.setProperty("OutputWorkspaceWavelengthAndPixelAdjustment",
                             wave_length_and_pixel_adjustment_workspace)

        if calculated_transmission_workspace and unfitted_transmission_workspace and state.adjustment.show_transmission:
            data_type = self.getProperty("DataType").value
            output_workspace_name, output_workspace_base_name = get_standard_output_workspace_name(
                state,
                state.reduction.reduction_mode,
                transmission=True,
                data_type=data_type)
            AnalysisDataService.addOrReplace(
                output_workspace_base_name, calculated_transmission_workspace)
            AnalysisDataService.addOrReplace(
                output_workspace_base_name + '_unfitted',
                unfitted_transmission_workspace)
Example #11
0
 def tearDown(self) -> None:
     to_delete = [
         w for w in [self.workspace, self.table]
         if AnalysisDataService.doesExist(w)
     ]
     if len(to_delete) > 0:
         DeleteWorkspaces(to_delete)
Example #12
0
def mask_bank(bank_name: str, tubes_fit_success: np.ndarray, output_table: str) -> Optional[TableWorkspace]:
    r"""
    Creates a single-column `TableWorkspace` object containing the detector ID's of the
    unsuccessfully fitted tubes

    If all tubes were fit successfully, no `TableWorkspace` is created, and `None` is returned.

    :param bank_name: a string of the form 'bankI' where 'I' is a bank number
    :param tubes_fit_success: array of boolean containing a True/False entry for each tube, indicating wether
    the tube was successfully calibrated.
    :param output_table: name of the output TableWorkspace containing one column for detector ID from tubes
    not successfully calibrated.

    :raise AssertionError: the string bank_name does not follow the pattern 'bankI' where 'I' in an integer
    :return: name of the mask TableWorkspace. Returns `None` if no TableWorkspace is created.
    """
    assert re.match(r'^bank\d+$', bank_name), 'The bank name must be of the form "bankI" where "I" in an integer'
    if False not in tubes_fit_success:
        return None  # al tubes were fit successfully
    bank_number = bank_name[4:]  # drop 'bank' from bank_name
    tube_numbers = 1 + np.where(tubes_fit_success == False)[0]  # noqa E712 unsuccessfully fitted tube numbers
    tube_numbers = ','.join([str(n) for n in tube_numbers])  # failing tubes as a string
    detector_ids = MaskBTP(Instrument='CORELLI', Bank=bank_number, Tube=tube_numbers)
    table = CreateEmptyTableWorkspace(OutputWorkspace=output_table)
    table.addColumn('long64', 'Detector ID')
    [table.addRow([detector_id]) for detector_id in detector_ids.tolist()]
    if AnalysisDataService.doesExist('CORELLIMaskBTP'):
        DeleteWorkspaces(['CORELLIMaskBTP'])
    return mtd[output_table]
Example #13
0
def _confirm_all_workspaces_loaded(workspaces_to_confirm):
    current_workspaces = ADS.getObjectNames()
    for ws in workspaces_to_confirm:
        if ws not in current_workspaces:
            logger.warning("Project Loader was unable to load back all of project workspaces")
            return False
    return True
 def get_workspaces(self):
     simple_list = self._view.get_selected_workspaces()
     for workspace_name in simple_list:
         workspace = AnalysisDataService.retrieve(workspace_name)
         if issubclass(type(workspace), WorkspaceGroup):
             simple_list.remove(workspace_name)
             simple_list += list(workspace.getNames())
     return list(set(simple_list))
 def get_workspaces(self):
     simple_list = self._view.get_selected_workspaces()
     for workspace_name in simple_list:
         workspace = AnalysisDataService.retrieve(workspace_name)
         if issubclass(type(workspace),WorkspaceGroup):
             simple_list.remove(workspace_name)
             simple_list += list(workspace.getNames())
     return list(set(simple_list))
    def PyExec(self):
        # Read the state
        state_property_manager = self.getProperty("SANSState").value
        state = create_deserialized_sans_state_from_property_manager(state_property_manager)

        # --------------------------------------
        # Get the monitor normalization workspace
        # --------------------------------------
        monitor_normalization_workspace = self._get_monitor_normalization_workspace(state)

        # --------------------------------------
        # Get the calculated transmission
        # --------------------------------------
        calculated_transmission_workspace, unfitted_transmission_workspace =\
            self._get_calculated_transmission_workspace(state)

        # --------------------------------------
        # Get the wide angle correction workspace
        # --------------------------------------
        wave_length_and_pixel_adjustment_workspace = self._get_wide_angle_correction_workspace(state,
                                                                   calculated_transmission_workspace)  # noqa

        # --------------------------------------------
        # Get the full wavelength and pixel adjustment
        # --------------------------------------------
        wave_length_adjustment_workspace, \
        pixel_length_adjustment_workspace = self._get_wavelength_and_pixel_adjustment_workspaces(state,
                                                                            monitor_normalization_workspace,  # noqa
                                                                            calculated_transmission_workspace)  # noqa

        if wave_length_adjustment_workspace:
            self.setProperty("OutputWorkspaceWavelengthAdjustment", wave_length_adjustment_workspace)
        if pixel_length_adjustment_workspace:
            self.setProperty("OutputWorkspacePixelAdjustment", pixel_length_adjustment_workspace)
        if wave_length_and_pixel_adjustment_workspace:
            self.setProperty("OutputWorkspaceWavelengthAndPixelAdjustment", wave_length_and_pixel_adjustment_workspace)

        if calculated_transmission_workspace and unfitted_transmission_workspace and state.adjustment.show_transmission:
            data_type = self.getProperty("DataType").value
            output_workspace_name, output_workspace_base_name = get_standard_output_workspace_name(state,
                                                                                                   state.reduction.reduction_mode
                                                                                                   , transmission = True,
                                                                                                   data_type = data_type)
            AnalysisDataService.addOrReplace(output_workspace_base_name, calculated_transmission_workspace)
            AnalysisDataService.addOrReplace(output_workspace_base_name + '_unfitted', unfitted_transmission_workspace)
Example #17
0
    def test_fit_bank(self):
        control = self.cases['123455_bank20']  # a bank with a reasonable wire scan

        with self.assertRaises(AssertionError) as exception_info:
            fit_bank('I_am_not_here', 'bank42')
        with self.assertRaises(AssertionError) as exception_info:
            fit_bank(control, 'bank20', shadow_height=-1000)
        assert 'shadow height must be positive' in str(exception_info.exception)

        with self.assertRaises(AssertionError) as exception_info:
            fit_bank(control, 'tree/bank51')
        assert 'The bank name must be of the form' in str(exception_info.exception)

        with self.assertRaises(AssertionError) as exception_info:
            fit_bank(self.cases['123555_bank20'], 'bank20')
        assert 'Insufficient counts per pixel in workspace CORELLI_123555_bank20' in str(exception_info.exception)

        fit_bank(control, 'bank20', parameters_table_group='parameters_table')
        # Expect default name for calibration table
        assert AnalysisDataService.doesExist('CalibTable')
        # Expect default name for peak pixel position table
        assert AnalysisDataService.doesExist('PeakTable')
        # Expect default name for peak pixel position table
        assert AnalysisDataService.doesExist('PeakYTable')
        # assert the parameters tables are created
        assert AnalysisDataService.doesExist('parameters_table')
        for tube_number in range(TUBES_IN_BANK):
            assert AnalysisDataService.doesExist(f'parameters_table_{tube_number}')
        DeleteWorkspaces(['CalibTable', 'PeakTable', 'PeakYTable', 'parameters_table'])

        fit_bank(control, 'bank20', calibration_table='table_20', peak_pixel_positions_table='pixel_20')
        assert AnalysisDataService.doesExist('table_20')
        assert AnalysisDataService.doesExist('pixel_20')
        DeleteWorkspaces(['table_20', 'pixel_20', 'PeakYTable', 'ParametersTable'])  # a bit of clean-up
Example #18
0
def _confirm_all_workspaces_loaded(workspaces_to_confirm):
    current_workspaces = ADS.getObjectNames()
    for ws in workspaces_to_confirm:
        if ws not in current_workspaces:
            logger.warning(
                "Project Loader was unable to load back all of project workspaces"
            )
            return False
    return True
 def remove_workspace_names_from_plot(self, workspace_names: List[str]):
     """Removes the input workspace names from the plot"""
     for workspace_name in workspace_names:
         try:
             workspace = AnalysisDataService.Instance().retrieve(workspace_name)
         except RuntimeError:
             continue
         self._view.remove_workspace_from_plot(workspace)
     self._view.redraw_figure()
Example #20
0
    def test_new_corelli_calibration_and_load_calibration(self):
        r"""Creating a database is time consuming, thus we test both new_corelli_calibration and load_calibration"""
        # populate a calibration database with a few cases. There should be at least one bank with two calibrations
        database = tempfile.TemporaryDirectory()
        cases = [('124016_bank10', '10'), ('124023_bank10', '10'), ('124023_banks_14_15', '14-15')]
        for bank_case, bank_selection in cases:
            # Produce workspace groups 'calibrations', 'masks', 'fits'
            calibrate_banks(self.cases[bank_case], bank_selection)
            masks = 'masks' if AnalysisDataService.doesExist('masks') else None
            save_calibration_set(self.cases[bank_case], database.name, 'calibrations', masks, 'fits')
            DeleteWorkspaces(['calibrations', 'fits'])
            if AnalysisDataService.doesExist('masks'):
                DeleteWorkspaces(['masks'])

        # invoque creation of  new corelli calibration without a date
        calibration_file, mask_file, manifest_file = new_corelli_calibration(database.name)
        for file_path in (calibration_file, mask_file, manifest_file):
            assert path.exists(file_path)
        assert open(manifest_file).read() == 'bankID, timestamp\n10, 20200109\n14, 20200109\n15, 20200109\n'

        # load latest calibration and mask (day-stamp of '124023_bank10' is 20200109)
        calibration, mask = load_calibration_set(self.cases['124023_bank10'], database.name,
                                                 mask_format='TableWorkspace')
        calibration_expected = LoadNexusProcessed(Filename=calibration_file)
        mask_expected = LoadNexusProcessed(Filename=mask_file)
        assert_allclose(calibration.column(1), calibration_expected.column(1), atol=1e-4)
        assert mask.column(0) == mask_expected.column(0)

        # invoque a new corelli calibration with a date falling in between the bank (bank10) in
        # in our small dataset having two calibrations
        calibration_file, mask_file, manifest_file = new_corelli_calibration(database.name, date='20200108')
        for file_path in (calibration_file, mask_file, manifest_file):
            assert path.exists(file_path)
        assert open(manifest_file).read() == 'bankID, timestamp\n10, 20200106\n'

        # load oldest calibration and mask(day-stamp of '124023_bank10' is 20200106)
        calibration, mask = load_calibration_set(self.cases['124016_bank10'], database.name,
                                                 mask_format='TableWorkspace')
        calibration_expected = LoadNexusProcessed(Filename=calibration_file)
        mask_expected = LoadNexusProcessed(Filename=mask_file)
        assert_allclose(calibration.column(1), calibration_expected.column(1), atol=1e-4)
        assert mask.column(0) == mask_expected.column(0)

        database.cleanup()
def integrate_ranges(ranges, integral, mask, detector, input_workspace_name, input_workspace, x_dim, y_dim, is_multi_range):
    for integral_range in ranges:
        output_workspace = generate_output_workspace_name(integral_range, integral, mask, detector, input_workspace_name)
        output_workspace = run_algorithm(input_workspace, integral_range, integral, output_workspace, x_dim, y_dim)

        if is_multi_range:
            add_to_group(output_workspace, input_workspace_name + '_ranges')

    if is_multi_range:
        return AnalysisDataService.retrieve(input_workspace_name + '_ranges')
    else:
        return output_workspace
Example #22
0
def integrate_ranges(ranges, integral, mask, detector, input_workspace_name, input_workspace, x_dim, y_dim, is_multi_range):
    for integral_range in ranges:
        output_workspace = generate_output_workspace_name(integral_range, integral, mask, detector, input_workspace_name)
        output_workspace = run_algorithm(input_workspace, integral_range, integral, output_workspace, x_dim, y_dim)

        if is_multi_range:
            add_to_group(output_workspace, input_workspace_name + '_ranges')

    if is_multi_range:
        return AnalysisDataService.retrieve(input_workspace_name + '_ranges')
    else:
        return output_workspace
Example #23
0
 def _validate_workspaces(workspaces):
     """
     This method checks if any of the workspaces to plot contain NaN values.
     :param workspaces: A list of workspace names
     :return: A list of workspaces (used in matplotlib plotting). Raises if NaN values present.
     """
     workspaces = AnalysisDataService.Instance().retrieveWorkspaces(workspaces, unrollGroups=True)
     for ws in workspaces:
         if np.isnan(ws.readY(0)).any():
             # All data can be NaN if bounds are too close together
             # this makes the data unplottable
             raise ValueError("Workspace contains NaN values.")
     return workspaces
Example #24
0
def purge_table(workspace: WorkspaceTypes, calibration_table: TableWorkspace,
                tubes_fit_success: np.ndarray,  output_table: str = None) -> None:
    r"""
    Remove the detectorID's corresponding to the failing tubes from the calibration table

    Assumptions:
    - Each tube has PIXELS_PER_TUBE number of pixels
    - detector ID's in the calibration table are sorted according to tube number

    :param workspace: input Workspace2D containing total neutron counts per pixel
    :param calibration_table: input TableWorkspace containing one column for detector ID and one column
    for its calibrated Y coordinates, in meters
    :param tubes_fit_success: array of booleans of length TUBES_IN_BANK. `False` if a tube was unsuccessfully fitted.
    :param output_table: name of the purged table. If `None`, the input `calibration_table` is purged.
    """
    # validate input arguments
    if False not in tubes_fit_success:
        return  # nothing to do
    # validate the input workspace
    message = f'Cannot process workspace {workspace}. Pass the name of an existing workspace or a workspace handle'
    assert isinstance(workspace, (str, Workspace2D)), message
    workspace_name = str(workspace)
    assert AnalysisDataService.doesExist(workspace_name), f'Input workspace {workspace_name} does not exists'
    # validate the input calibraton table
    message = f'Cannot process table {calibration_table}. Pass the name of an existing TableWorkspace' \
              ' or a TableWorkspace handle'
    assert isinstance(calibration_table, (str, TableWorkspace)), message
    assert AnalysisDataService.doesExist(str(calibration_table)), f'Input table {calibration_table} does not exists'
    if output_table is not None:
        CloneWorkspace(InputWorkspace=calibration_table, OutputWorkspace=output_table)
    else:
        output_table = str(calibration_table)
    tube_fail_indexes = np.where(tubes_fit_success == False)[0]  # noqa E712 indexes of tubes unsuccessfully fitted
    row_indexes_of_first_tube = np.arange(PIXELS_PER_TUBE)  # 0, 1, ... 255
    fail_rows = [row_indexes_of_first_tube + (i * PIXELS_PER_TUBE) for i in tube_fail_indexes]
    fail_rows = np.array(fail_rows, dtype=int).flatten().tolist()
    DeleteTableRows(output_table, fail_rows)
Example #25
0
    def test_save_calibration_set(self) -> None:
        calibrations, masks = calibrate_banks(self.cases['124023_banks_14_15'], '14-15')
        for w in ('calibrations', 'masks', 'fits'):
            assert AnalysisDataService.doesExist(w)

        # Save everything (typical case)
        database = tempfile.TemporaryDirectory()
        save_calibration_set(self.cases['124023_banks_14_15'], database.name, 'calibrations', 'masks', 'fits')
        for bn in ('014', '015'):  # bank number
            for ct in ('calibration', 'mask', 'fit'):  # table type
                assert path.exists(path.join(database.name, f'bank{bn}', f'{ct}_corelli_bank{bn}_20200109.nxs.h5'))
        database.cleanup()

        #  Save only the calibration tables
        database = tempfile.TemporaryDirectory()
        save_calibration_set(self.cases['124023_banks_14_15'], database.name, 'calibrations')
        for bn in ('014', '015'):  # bank number
            assert path.exists(path.join(database.name, f'bank{bn}', f'calibration_corelli_bank{bn}_20200109.nxs.h5'))
        database.cleanup()

        #  Save only the calibration tables as a list of strings
        database = tempfile.TemporaryDirectory()
        save_calibration_set(self.cases['124023_banks_14_15'], database.name, ['calib14', 'calib15'])
        for bn in ('014', '015'):  # bank number
            assert path.exists(path.join(database.name, f'bank{bn}', f'calibration_corelli_bank{bn}_20200109.nxs.h5'))
        database.cleanup()

        #  Save only the calibration tables as a list of workspaces
        database = tempfile.TemporaryDirectory()
        save_calibration_set(self.cases['124023_banks_14_15'], database.name, [mtd['calib14'], mtd['calib15']])
        for bn in ('014', '015'):  # bank number
            assert path.exists(path.join(database.name, f'bank{bn}', f'calibration_corelli_bank{bn}_20200109.nxs.h5'))
        database.cleanup()

        # Save only one table of each type, passing strings
        database = tempfile.TemporaryDirectory()
        save_calibration_set(self.cases['124023_banks_14_15'], database.name, 'calib14', 'mask14', 'fit14')
        for ct in ('calibration', 'mask', 'fit'):  # table type
            assert path.exists(path.join(database.name, 'bank014', f'{ct}_corelli_bank014_20200109.nxs.h5'))
        database.cleanup()

        # Save only one table of each type, passing workspaces
        database = tempfile.TemporaryDirectory()
        save_calibration_set(self.cases['124023_banks_14_15'], database.name,
                             mtd['calib14'], mtd['mask14'], mtd['fit14'])
        for ct in ('calibration', 'mask', 'fit'):  # table type
            assert path.exists(path.join(database.name, 'bank014', f'{ct}_corelli_bank014_20200109.nxs.h5'))
        database.cleanup()
Example #26
0
    def test_criterion_peak_vertical_position(self):

        # control bank, it has no problems
        fit_bank(self.cases['123455_bank20'], 'bank20')
        expected = np.ones(16, dtype=bool)
        actual = criterion_peak_vertical_position('PeakYTable', zscore_threshold=2.5, deviation_threshold=0.0035)
        assert_equal(actual, expected)
        DeleteWorkspaces(['CalibTable', 'ParametersTable', 'PeakTable', 'PeakYTable'])  # a bit of clean-up

        # beam center intensity spills over adjacent tubes, tube15 and tube16
        fit_bank(self.cases['123454_bank58'], 'bank58')
        expected = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], dtype=bool)
        actual = criterion_peak_vertical_position('PeakYTable', zscore_threshold=2.5, deviation_threshold=0.0035)
        assert_equal(actual, expected)
        DeleteWorkspaces(['CalibTable', 'ParametersTable', 'PeakTable', 'PeakYTable'])  # a bit of clean-up

        # tube11 is not working at all
        fit_bank(self.cases['124018_bank45'], 'bank45')
        expected = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1], dtype=bool)
        actual = criterion_peak_vertical_position('PeakYTable', zscore_threshold=2.5, deviation_threshold=0.0035)
        assert_equal(actual, expected)
        DeleteWorkspaces(['CalibTable', 'ParametersTable', 'PeakTable', 'PeakYTable'])  # a bit of clean-up

        # tube 13 has shadows at pixel numbers quite different from the rest, but similar vertical positions
        fit_bank(self.cases['124023_bank10'], 'bank10')
        expected = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=bool)
        actual = criterion_peak_vertical_position('PeakYTable', zscore_threshold=2.5, deviation_threshold=0.0035)
        assert_equal(actual, expected)
        DeleteWorkspaces(['CalibTable', 'ParametersTable', 'PeakTable', 'PeakYTable'])  # a bit of clean-up

        # one spurious shadow in tube14 throws away the fit
        fit_bank(self.cases['124023_bank15'], 'bank15')
        expected = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1], dtype=bool)
        actual = criterion_peak_vertical_position('PeakYTable', zscore_threshold=2.5, deviation_threshold=0.0035)
        assert_equal(actual, expected)
        DeleteWorkspaces(['CalibTable', 'ParametersTable', 'PeakTable', 'PeakYTable'])  # a bit of clean-up

        # check for the summary workspace
        fit_bank(self.cases['123455_bank20'], 'bank20')
        criterion_peak_vertical_position('PeakYTable', summary='summary',
                                         zscore_threshold=2.5, deviation_threshold=0.0035)
        assert AnalysisDataService.doesExist('summary')
        workspace = mtd['summary']
        axis = workspace.getAxis(1)
        assert [axis.label(workspace_index) for workspace_index in (0, 1, 2)] == ['success', 'deviation', 'Z-score']
        self.assertEqual(min(workspace.readY(0)), 1.0)  # check success of first tube
        self.assertAlmostEqual(max(workspace.readY(2)), 2.73, delta=0.01)  # check maximum Z-score
        DeleteWorkspaces(['CalibTable', 'ParametersTable', 'PeakTable', 'PeakYTable', 'summary'])  # a bit of clean-up
Example #27
0
    def remove_workspace_info_from_plot(
            self, workspace_plot_info_list: List[WorkspacePlotInformation]):
        for workspace_plot_info in workspace_plot_info_list:
            workspace_name = workspace_plot_info.workspace_name
            try:
                workspace = AnalysisDataService.Instance().retrieve(
                    workspace_name)
            except RuntimeError:
                continue
            for plotted_information in self._plot_information_list.copy():
                if workspace_plot_info == plotted_information:
                    axis = self.fig.axes[workspace_plot_info.axis]
                    axis.remove_workspace_artists(workspace)
                    self._plot_information_list.remove(plotted_information)

        # If we have no plotted lines, reset the color cycle
        if self.num_plotted_workspaces == 0:
            self._reset_color_cycle()
Example #28
0
 def _make_plot(self, workspace_plot_info: WorkspacePlotInformation):
     workspace_name = workspace_plot_info.workspace_name
     try:
         workspace = AnalysisDataService.Instance().retrieve(workspace_name)
     except (RuntimeError, KeyError):
         return -1
     self._plot_information_list.append(workspace_plot_info)
     errors = workspace_plot_info.errors
     ws_index = workspace_plot_info.index
     axis_number = workspace_plot_info.axis
     ax = self.fig.axes[axis_number]
     plot_kwargs = self._get_plot_kwargs(workspace_plot_info)
     plot_kwargs['color'] = self._color_queue[axis_number]()
     _do_single_plot(ax,
                     workspace,
                     ws_index,
                     errors=errors,
                     plot_kwargs=plot_kwargs)
     return axis_number
Example #29
0
    def test_criterium_peak_pixel_position(self):
        # control bank, it has no problems
        fit_bank(self.cases['123455_bank20'], 'bank20')
        expected = np.ones(16, dtype=bool)
        assert_equal(criterium_peak_pixel_position('PeakTable', zscore_threshold=2.5, deviation_threshold=3), expected)

        # beam center intensity spills over adjacent tubes, tube15 and tube16
        fit_bank(self.cases['123454_bank58'], 'bank58')
        expected = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], dtype=bool)
        assert_equal(criterium_peak_pixel_position('PeakTable', zscore_threshold=2.5, deviation_threshold=3), expected)

        # tube11 is not working at all
        fit_bank(self.cases['124018_bank45'], 'bank45')
        expected = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1], dtype=bool)
        assert_equal(criterium_peak_pixel_position('PeakTable', zscore_threshold=2.5, deviation_threshold=3), expected)

        # tube 13 has shadows at pixel numbers quite different from the rest
        fit_bank(self.cases['124023_bank10'], 'bank10')
        expected = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1], dtype=bool)
        assert_equal(criterium_peak_pixel_position('PeakTable', zscore_threshold=2.5, deviation_threshold=3), expected)

        # tubes 3, 8, and 13 have very faint wire shadows
        fit_bank(self.cases['124023_bank14'], 'bank14')
        expected = np.array([1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1], dtype=bool)
        assert_equal(criterium_peak_pixel_position('PeakTable', zscore_threshold=2.5, deviation_threshold=3), expected)

        # one spurious shadow in tube14, not enough to flag a discrepancy
        fit_bank(self.cases['124023_bank15'], 'bank15')
        expected = np.ones(16, dtype=bool)
        assert_equal(criterium_peak_pixel_position('PeakTable', zscore_threshold=2.5, deviation_threshold=3), expected)

        # check for the summary workspace
        fit_bank(self.cases['123455_bank20'], 'bank20')
        criterium_peak_pixel_position('PeakTable', summary='summary', zscore_threshold=2.5, deviation_threshold=3)
        assert AnalysisDataService.doesExist('summary')
        workspace = mtd['summary']
        axis = workspace.getAxis(1)
        assert [axis.label(workspace_index) for workspace_index in (0, 1, 2)] == ['success', 'deviation', 'Z-score']
        self.assertEqual(min(workspace.readY(0)), 1.0)
        self.assertAlmostEqual(max(workspace.readY(2)), 1.728, delta=0.001)

        DeleteWorkspaces(['CalibTable', 'PeakTable', 'summary'])  # a bit of clean-up
Example #30
0
 def add_workspaces_to_plot(
         self, workspace_plot_info_list: List[WorkspacePlotInformation]):
     """Add a list of workspaces to the plot - The workspaces are contained in a list PlotInformation
     The PlotInformation contains the workspace name, workspace index and target axis."""
     for workspace_plot_info in workspace_plot_info_list:
         workspace_name = workspace_plot_info.workspace_name
         try:
             workspace = AnalysisDataService.Instance().retrieve(
                 workspace_name)
         except RuntimeError:
             continue
         self._plot_information_list.append(workspace_plot_info)
         errors = workspace_plot_info.errors
         ws_index = workspace_plot_info.index
         axis_number = workspace_plot_info.axis
         ax = self.fig.axes[axis_number]
         _do_single_plot(
             ax,
             workspace,
             ws_index,
             errors=errors,
             plot_kwargs=self._get_plot_kwargs(workspace_plot_info))
 def add_maxEnt(self, run, ws_freq):
     self._maxEnt_freq[ws_freq] = MaxEnt(
         run, AnalysisDataService.retrieve(ws_freq))
 def __init__(self, parent_presenter=None):
     self._parent_presenter = parent_presenter
     self._view = None
     self.current_directory = ConfigService['defaultsave.directory']
     self.filename = ''
     self.workspace_list = AnalysisDataService.getObjectNames()
 def __init__(self, parent_presenter=None):
     self._parent_presenter = parent_presenter
     self._view = None
     self.current_directory = ConfigService['defaultsave.directory']
     self.filename = ''
     self.workspace_list = AnalysisDataService.getObjectNames()
Example #34
0
    def test_peak_y_table(self) -> None:
        # Mock PeakTable with two tubes and three peaks. Simple, integer values
        def peak_pixels_table(table_name,
                              peak_count,
                              tube_names=None,
                              pixel_positions=None):
            table = CreateEmptyTableWorkspace(OutputWorkspace=table_name)
            table.addColumn(type='str', name='TubeId')
            for i in range(peak_count):
                table.addColumn(type='float', name='Peak%d' % (i + 1))
            if tube_names is not None and pixel_positions is not None:
                assert len(tube_names) == len(
                    pixel_positions
                ), 'tube_names and pixel_positions have different length'
                for tube_index in range(len(tube_names)):
                    # tube_names is a list of str values; pixel_positions is a list of lists of float values
                    table.addRow([tube_names[tube_index]] +
                                 pixel_positions[tube_index])
            return table

        # Create a peak table with only one tube
        peak_table = peak_pixels_table('PeakTable', 3, ['tube1'], [[0, 1, 2]])

        # Mock ParametersTableGroup with one parameter table. Simple parabola
        def parameters_optimized_table(table_name, values=None, errors=None):
            table = CreateEmptyTableWorkspace(OutputWorkspace=table_name)
            for column_type, column_name in [('str', 'Name'),
                                             ('float', 'Value'),
                                             ('float', 'Error')]:
                table.addColumn(type=column_type, name=column_name)
            if values is not None and errors is not None:
                assert len(values) == 4 and len(
                    errors) == 4  # A0, A1, A2, 'Cost function value'
                for index, row_name in enumerate(
                    ['A0', 'A1', 'A2', 'Cost function value']):
                    table.addRow([row_name, values[index], errors[index]])
            return table

        # Create two tables with optimized polynomial coefficients, then group them
        parameters_optimized_table('parameters_table_0', [0, 0, 1, 1.3],
                                   [0, 0, 0, 0])  # first tube
        parameters_optimized_table('parameters_table_1', [1, 1, 1, 2.3],
                                   [0, 0, 0, 0])  # second tube
        parameters_table = GroupWorkspaces(
            InputWorkspaces=['parameters_table_0', 'parameters_table_1'],
            OutputWorkspace='parameters_table')

        # Check we raise an assertion error since the number of tubes is different than number of tables
        with self.assertRaises(AssertionError) as exception_info:
            calculate_peak_y_table(peak_table,
                                   parameters_table,
                                   output_workspace='PeakYTable')
        assert 'number of rows in peak_table different than' in str(
            exception_info.exception)
        # Add another parameter table to ParametersTableGroup, the create peak_vertical_table
        peak_table.addRow(['tube2', 0, 1, 2])
        table = calculate_peak_y_table(peak_table,
                                       parameters_table,
                                       output_workspace='PeakYTable')
        assert AnalysisDataService.doesExist('PeakYTable')
        assert_allclose(list(table.row(0).values())[1:], [0, 1, 4])
        assert_allclose(list(table.row(1).values())[1:], [1, 3, 7])
Example #35
0
 def test_apply_calibration(self) -> None:
     table = calibrate_tube(self.workspace, 'bank42/sixteenpack/tube8')
     apply_calibration(self.workspace, table)
     assert AnalysisDataService.doesExist('uncalibrated_calibrated')
     DeleteWorkspaces(['uncalibrated_calibrated', str(table)])
 def tearDown(self):
     AnalysisDataService.Instance().clear()