Beispiel #1
0
    def test_purge_table(self):
        r"""We use either of two criterion functions"""
        with self.assertRaises(AssertionError) as exception_info:
            purge_table('I am not here', 'table', [True, False])
        assert 'Input workspace I am not here does not exists' in str(exception_info.exception)

        with self.assertRaises(AssertionError) as exception_info:
            purge_table(self.cases['123455_bank20'], 'I am not here', [True, False])
        assert 'Input table I am not here does not exists' in str(exception_info.exception)

        # control bank, it has no problems. Thus, no tubes to purge
        fit_bank(self.cases['123455_bank20'], 'bank20')
        tube_fit_success = criterion_peak_pixel_position('PeakTable', zscore_threshold=2.5, deviation_threshold=3)
        unpurged_row_count = mtd['CalibTable'].rowCount()
        purge_table(self.cases['123455_bank20'], 'CalibTable', tube_fit_success)
        assert mtd['CalibTable'].rowCount() == unpurged_row_count
        DeleteWorkspaces(['CalibTable', 'PeakTable', 'PeakYTable', 'ParametersTable'])  # a bit of clean-up

        # tube11 is not working at all. Thus, purge only one tube
        fit_bank(self.cases['124018_bank45'], 'bank45')
        tube_fit_success = criterion_peak_vertical_position('PeakYTable', zscore_threshold=2.5,
                                                            deviation_threshold=0.0035)
        unpurged_row_count = mtd['CalibTable'].rowCount()
        purge_table(self.cases['124018_bank45'], 'CalibTable', tube_fit_success)
        assert mtd['CalibTable'].rowCount() == unpurged_row_count - 256
        self.assert_missing_tube('CalibTable', 11)
        DeleteWorkspaces(['CalibTable', 'PeakTable', 'PeakYTable', 'ParametersTable'])  # a bit of clean-up
Beispiel #2
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
Beispiel #3
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
Beispiel #4
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
Beispiel #5
0
 def test_calibrate(self):
     data = self.corelli
     calibrate(data['workspace'],
               data['bank_name'],
               data['wire_positions'],
               data['peaks_form'],
               fitPar=data['fit_parameters'],
               outputPeak=True,
               parameters_table_group='parameters_table_group')
     # Check the table workspaces containing the polynomial coefficients
     assert AnalysisDataService.doesExist('parameters_table_group')
     workspace = mtd['parameters_table_group']
     for tube_number, name in enumerate(workspace.getNames()):
         assert name == f'parameters_table_group_{tube_number}'
         assert AnalysisDataService.doesExist(name)
     # Check the values of the coefficients for the first and last tube
     for tube_index, expected in [(0, {
             'A0': -0.446804,
             'A1': 0.003513,
             'A2': 0.0
     }), (15, {
             'A0': -0.452107,
             'A1': 0.003528,
             'A2': 0.00
     })]:
         workspace = mtd[f'parameters_table_group_{tube_index}']
         for row in workspace:
             if row['Name'] in expected:
                 self.assertAlmostEqual(expected[row['Name']],
                                        row['Value'],
                                        delta=1.e-6)
     DeleteWorkspaces(['CalibTable', 'parameters_table_group', 'PeakTable'])
    def test_correct_tube_to_ideal_tube(self):
        # Verify the quadratic fit works
        data = self.y_quad_data

        # fit the Y-coordinates to the pixel positions with a default quadratic function
        fitted_coordinates = correct_tube_to_ideal_tube(
            data['tube_points'],
            data['ideal_tube_coordinates'],
            data['detector_count'],
            parameters_table='parameters')

        # Verify the fitted coordinates are the ideal_tube_coordinates
        assert_allclose(
            [fitted_coordinates[int(n)] for n in data['tube_points']],
            data['ideal_tube_coordinates'],
            atol=0.0001)
        # Compare fitting coefficients
        assert AnalysisDataService.doesExist('parameters')
        # here retrieve the fitting coefficients from the 'parameters' table and compare to the expected values
        expected = data['coefficients']
        for row in mtd['parameters']:
            if row['Name'] in expected:
                self.assertAlmostEqual(row['Value'],
                                       expected[row['Name']],
                                       places=6)
        # a bit of clean-up
        DeleteWorkspaces([
            'PolyFittingWorkspace', 'QF_NormalisedCovarianceMatrix',
            'QF_Parameters', 'QF_Workspace', 'parameters'
        ])
Beispiel #7
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]
Beispiel #8
0
 def spacings_recovered(self, input_workspace, calibrate=True):
     r"""Compare the input_workspace to the reference workspace 'test_workspace_dSpacing' after being
     converted to TOF units. If calibrate=True, calibrate first and then convert units"""
     if calibrate:
         CorelliPowderCalibrationCreate(
             InputWorkspace='perturbed',
             OutputWorkspacesPrefix='cal_',
             TofBinning=[300, 1.0, 16666.7],
             PeakPositions=self.spacings_reference,
             SourceToSampleDistance=10.0,
             ComponentList='bank1',
             FixY=False,
             ComponentMaxTranslation=0.03,
             FixYaw=False,
             ComponentMaxRotation=6,
             Minimizer='differential_evolution',
             MaxIterations=10)
     ConvertUnits(InputWorkspace='perturbed',
                  Target='dSpacing',
                  EMode='Elastic',
                  OutputWorkspace='perturbed_dS')
     results = CompareWorkspaces(Workspace1='test_workspace_dSpacing',
                                 Workspace2='perturbed_dS',
                                 Tolerance=0.001,
                                 CheckInstrument=False)
     DeleteWorkspaces(['perturbed_dS'])
     return results.Result
Beispiel #9
0
 def test_collect_peaks_diff(self):
     test_diff = diagnostics.collect_peaks('diag_dspacing',
                                           'test_diff',
                                           infotype='difference')
     result = CompareWorkspaces(test_diff, "difference", Tolerance=1e-6)
     self.assertTrue(result)
     DeleteWorkspaces(test_diff, result)
Beispiel #10
0
    def __testRaggedBoundaries(self, histogram):
        inwsname = 'MatchSpectraTest_raggedBoundaries'
        if histogram:
            inwsname += 'Histogram'
        else:
            inwsname += 'Frequency'
        outwsname = inwsname + '_out'
        self.__createRaggedWorkspace(inwsname, histogram)

        ##### offset only
        results = MatchSpectra(InputWorkspace=inwsname, OutputWorkspace=outwsname,
                               ReferenceSpectrum=2,
                               CalculateOffset=True, CalculateScale=False)
        self.__checkReference(results, 1)
        self.__checkValues(results, 0, 1., 10.)

        ##### scale only
        results = MatchSpectra(InputWorkspace=inwsname, OutputWorkspace=outwsname,
                               ReferenceSpectrum=3,
                               CalculateOffset=False, CalculateScale=True)
        self.__checkReference(results, 2)
        self.__checkValues(results, 0, 10., 0.)

        ##### both
        results = MatchSpectra(InputWorkspace=inwsname, OutputWorkspace=outwsname,
                               ReferenceSpectrum=4,
                               CalculateOffset=True, CalculateScale=True)
        self.__checkReference(results, 3)
        self.__checkValues(results, 0, 10., 10.)

        DeleteWorkspaces(WorkspaceList=[inwsname, outwsname])
Beispiel #11
0
    def runTest(self):
        ws_name = 'SingleFilePredictPeaksUBFromFindPeaksIntegrate'
        HB3AAdjustSampleNorm(Filename="HB3A_exp0724_scan0183.nxs",
                             OutputWorkspace=ws_name + '_data')

        HB3AFindPeaks(InputWorkspace=ws_name + '_data',
                      CellType="Orthorhombic",
                      Centering="F",
                      OutputWorkspace=ws_name + '_found_peaks')

        HB3APredictPeaks(InputWorkspace=ws_name + "_data",
                         UBWorkspace=ws_name + '_found_peaks',
                         OutputWorkspace=ws_name + '_peaks')

        HB3AIntegratePeaks(InputWorkspace=ws_name + '_data',
                           PeaksWorkspace=ws_name + '_peaks',
                           PeakRadius=0.25,
                           OutputWorkspace=ws_name + '_integrated')

        peaks = mtd[ws_name + '_integrated']

        self.assertEqual(peaks.getNumberPeaks(), 114)
        ol = peaks.sample().getOrientedLattice()
        self.assertDelta(ol.a(), 5.231258554, 1.e-7)
        self.assertDelta(ol.b(), 5.257701834, 1.e-7)
        self.assertDelta(ol.c(), 19.67041036, 1.e-7)
        self.assertEqual(ol.alpha(), 90)
        self.assertEqual(ol.beta(), 90)
        self.assertEqual(ol.gamma(), 90)

        DeleteWorkspaces([
            ws_name + '_data', ws_name + '_found_peaks', ws_name + '_peaks',
            ws_name + '_integrated'
        ])
Beispiel #12
0
    def runTest(self):
        ws_name = 'SingleFilePredictPeaksIntegrate'
        HB3AAdjustSampleNorm(Filename="HB3A_exp0724_scan0183.nxs",
                             OutputWorkspace=ws_name + '_data')

        HB3APredictPeaks(InputWorkspace=ws_name + "_data",
                         OutputWorkspace=ws_name + "_peaks")

        HB3AIntegratePeaks(InputWorkspace=ws_name + '_data',
                           PeaksWorkspace=ws_name + '_peaks',
                           PeakRadius=0.25,
                           OutputWorkspace=ws_name + '_integrated')

        peaks = mtd[ws_name + '_integrated']

        self.assertEqual(peaks.getNumberPeaks(), 112)
        ol = peaks.sample().getOrientedLattice()
        self.assertDelta(ol.a(), 5.238389802, 1.e-7)
        self.assertDelta(ol.b(), 5.238415883, 1.e-7)
        self.assertDelta(ol.c(), 19.65194598, 1.e-7)
        self.assertDelta(ol.alpha(), 89.9997207, 1.e-7)
        self.assertDelta(ol.beta(), 89.9997934, 1.e-7)
        self.assertDelta(ol.gamma(), 89.9999396, 1.e-7)

        DeleteWorkspaces(
            [ws_name + '_data', ws_name + '_peaks', ws_name + '_integrated'])
Beispiel #13
0
    def test_purge_table(self):
        with self.assertRaises(AssertionError) as exception_info:
            purge_table('I am not here', 'table', [True, False])
        assert 'Input workspace I am not here does not exists' in str(exception_info.exception)

        with self.assertRaises(AssertionError) as exception_info:
            purge_table(self.cases['123455_bank20'], 'I am not here', [True, False])
        assert 'Input table I am not here does not exists' in str(exception_info.exception)

        # control bank, it has no problems. Thus, no tubes to purge
        fit_bank(self.cases['123455_bank20'], 'bank20')
        tube_fit_success = criterium_peak_pixel_position('PeakTable', zscore_threshold=2.5, deviation_threshold=3)
        unpurged_row_count = mtd['CalibTable'].rowCount()
        purge_table(self.cases['123455_bank20'], 'CalibTable', tube_fit_success)
        assert mtd['CalibTable'].rowCount() == unpurged_row_count

        # tube11 is not working at all. Thus, purge only one tube
        fit_bank(self.cases['124018_bank45'], 'bank45')
        tube_fit_success = criterium_peak_pixel_position('PeakTable', zscore_threshold=2.5, deviation_threshold=3)
        unpurged_row_count = mtd['CalibTable'].rowCount()
        purge_table(self.cases['124018_bank45'], 'CalibTable', tube_fit_success)
        assert mtd['CalibTable'].rowCount() == unpurged_row_count - 256
        self.assert_missing_tube('CalibTable', 11)

        # tubes 3, 8, and 13 have very faint wire shadows. Thus, purge three tubes
        fit_bank(self.cases['124023_bank14'], 'bank14')
        tube_fit_success = criterium_peak_pixel_position('PeakTable', zscore_threshold=2.5, deviation_threshold=3)
        unpurged_row_count = mtd['CalibTable'].rowCount()
        purge_table(self.cases['124023_bank14'], 'CalibTable', tube_fit_success)
        assert mtd['CalibTable'].rowCount() == unpurged_row_count - 256 * 3
        for tube_number in (3, 8, 13):
            self.assert_missing_tube('CalibTable', tube_number)

        DeleteWorkspaces(['CalibTable', 'PeakTable'])  # a bit of clean-up
Beispiel #14
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)
Beispiel #15
0
 def test_collect_peaks_strain(self):
     test_strain = diagnostics.collect_peaks('diag_dspacing',
                                             'test_strain',
                                             infotype='strain')
     result = CompareWorkspaces(test_strain, "strain", Tolerance=1e-6)
     self.assertTrue(result)
     DeleteWorkspaces(test_strain, result)
Beispiel #16
0
    def __testCommonBoundaries(self, histogram):
        inwsname = 'MatchSpectraTest_commonBoundaries'
        if histogram:
            inwsname += 'Histogram'
        else:
            inwsname += 'Frequency'
        outwsname = inwsname + '_out'
        self.__createWorkspace(inwsname, histogram, True)

        ##### offset only
        results = MatchSpectra(InputWorkspace=inwsname, OutputWorkspace=outwsname,
                               ReferenceSpectrum=2,
                               CalculateOffset=True, CalculateScale=False)
        self.__checkReference(results, 1)
        self.__checkValues(results, 0, 1., 10.)
        self.assertTrue(np.alltrue(mtd[outwsname].readY(0) == mtd[outwsname].readY(1)))

        ##### scale only
        results = MatchSpectra(InputWorkspace=inwsname, OutputWorkspace=outwsname,
                               ReferenceSpectrum=3,
                               CalculateOffset=False, CalculateScale=True)
        self.__checkReference(results, 2)
        self.__checkValues(results, 0, 10., 0.)
        self.assertTrue(np.alltrue(mtd[outwsname].readY(0) == mtd[outwsname].readY(2)))

        ##### both
        results = MatchSpectra(InputWorkspace=inwsname, OutputWorkspace=outwsname,
                               ReferenceSpectrum=4,
                               CalculateOffset=True, CalculateScale=True)
        self.__checkReference(results, 3)
        self.__checkValues(results, 0, 10., 10.)
        self.assertTrue(np.alltrue(mtd[outwsname].readY(0) == mtd[outwsname].readY(3)))

        DeleteWorkspaces(WorkspaceList=[inwsname, outwsname])
Beispiel #17
0
    def test_calibrate_tube(self) -> None:
        # Check the validators are doing what they're supposed to do
        with self.assertRaises(AssertionError) as exception_info:
            calibrate_tube(None, 'bank42/sixteenpack/tube8')
        assert 'Cannot process workspace' in str(exception_info.exception)
        with self.assertRaises(AssertionError) as exception_info:
            calibrate_tube(self.workspace, 'tube42')
        assert 'tube42 does not uniquely specify one tube' in str(
            exception_info.exception)
        with self.assertRaises(AssertionError) as exception_info:
            calibrate_tube(self.workspace,
                           'bank42/sixteenpack/tube8',
                           fit_domain=20)
        assert 'The fit domain cannot be larger than the distance between consecutive' in str(
            exception_info.exception)

        table = calibrate_tube(self.workspace, 'bank42/sixteenpack/tube8')
        calibrated_y = table.column(1)
        assert_allclose(calibrated_y, self.calibrated_y, atol=1e-3)

        # Check for existence of all table workspaces
        calibrate_tube(self.cases['124023_bank10'],
                       'bank10/sixteenpack/tube13')
        DeleteWorkspaces(
            ['CalibTable', 'PeakTable', 'ParametersTable', 'PeakYTable'])
Beispiel #18
0
 def test_fix_y(self) -> None:
     CloneWorkspace(InputWorkspace='test_workspace_TOF',
                    OutputWorkspace='perturbed')
     y = -0.0042  # desired fixed position
     MoveInstrumentComponent(Workspace='perturbed',
                             ComponentName='bank1',
                             X=0,
                             y=y,
                             z=0,
                             RelativePosition=False)
     r"""Pass option FixY=True"""
     CorelliPowderCalibrationCreate(InputWorkspace='perturbed',
                                    OutputWorkspacesPrefix='cal_',
                                    TofBinning=[300, 1.0, 16666.7],
                                    PeakPositions=self.spacings_reference,
                                    SourceToSampleDistance=10.0,
                                    ComponentList='bank1',
                                    FixY=True,
                                    ComponentMaxTranslation=0.2,
                                    ComponentMaxRotation=10,
                                    Minimizer='L-BFGS-B')
     # Check Y-position of first bank hasn't changed
     row = mtd['cal_adjustments'].row(1)
     self.assertAlmostEquals(row['Yposition'], y, places=5)
     DeleteWorkspaces(['perturbed'])
Beispiel #19
0
 def test_fix_yaw(self) -> None:
     CloneWorkspace(InputWorkspace='test_workspace_TOF',
                    OutputWorkspace='perturbed')
     RotateInstrumentComponent(Workspace='perturbed',
                               ComponentName='bank1',
                               X=0,
                               Y=0,
                               z=1,
                               Angle=5,
                               RelativeRotation=True)
     r"""Pass option FixYaw=True"""
     CorelliPowderCalibrationCreate(InputWorkspace='perturbed',
                                    OutputWorkspacesPrefix='cal_',
                                    TofBinning=[300, 1.0, 16666.7],
                                    PeakPositions=self.spacings_reference,
                                    SourceToSampleDistance=10.0,
                                    ComponentList='bank1',
                                    ComponentMaxTranslation=0.2,
                                    FixYaw=True,
                                    ComponentMaxRotation=10,
                                    Minimizer='L-BFGS-B')
     # Check no change in the rotations around Z-axis of first bank
     row = mtd['cal_displacements'].row(0)
     self.assertAlmostEquals(row['DeltaGamma'], 0.0, places=5)
     DeleteWorkspaces(['perturbed'])
Beispiel #20
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
Beispiel #21
0
def norm(sample, blank, masks):
    """
    Normalise the neutron polarisation on a tube by tube basis

    Parameters
    ----------
    sample: string
      The name of the sample to be normalised.  The individual tubes are
      assumed to be in workspaces with names like sample_2
    blank: string
      The name of the blank to be normalised agains.  The individual tubes are
      assumed to be in workspaces with names like blank_2
    masks: list of string
      The file names for the masks used for the individual tubes.
    """
    for t, _ in enumerate(masks):
        wtemp = mtd[sample + "_{}".format(t)] / \
            mtd[blank + "_{}".format(t)]

        y = mtd[blank + "_{}".format(t)].extractY()
        e = wtemp.extractE()

        e[np.abs(y) < 0.2] *= 1e9
        wtemp.setE(0, e[0])

        RenameWorkspace("wtemp", sample + "_{}_norm".format(t))
    wtemp = WeightedMean(sample + "_0_norm", sample + "_1_norm")
    for tube in range(2, len(masks)):
        wtemp = WeightedMean(wtemp, sample + "_{}_norm".format(tube))
    RenameWorkspace(wtemp, OutputWorkspace=sample + "_Norm")
    DeleteWorkspaces(
        ["{}_{}_norm".format(sample, tube) for tube, _ in enumerate(masks)])
Beispiel #22
0
    def test_extract_peak_info(self):
        test_single_strain = diagnostics.extract_peak_info(
            'strain', 'test_single_strain', self.PEAK)
        result = CompareWorkspaces(test_single_strain,
                                   "single_strain",
                                   Tolerance=1e-6)
        self.assertTrue(result)
        DeleteWorkspaces(test_single_strain, result)

        test_single_diff = diagnostics.extract_peak_info(
            'difference', 'test_single_diff', self.PEAK)
        result = CompareWorkspaces(test_single_diff,
                                   "single_diff",
                                   Tolerance=1e-6)
        self.assertTrue(result)
        DeleteWorkspaces(test_single_diff, result)
Beispiel #23
0
 def test_collect_fit_result(self):
     test_center = diagnostics.collect_fit_result('diag_fitparam',
                                                  'test_center',
                                                  [self.PEAK],
                                                  infotype='centre')
     result = CompareWorkspaces(test_center, "center_tof", Tolerance=1e-6)
     self.assertTrue(result)
     DeleteWorkspaces(test_center, result)
Beispiel #24
0
    def test_collect_bank_fit_results(self):

        def spectra_labels(workspace_name):
            workspace = mtd[str(workspace_name)]
            axis = workspace.getAxis(1)
            return [axis.label(spectrum_index) for spectrum_index in range(workspace.getNumberHistograms())]

        fit_bank(self.cases['123455_bank20'], 'bank20', parameters_table_group='parameters_tables')
        criterion_peak_vertical_position('PeakTable', summary='summary',
                                         zscore_threshold=2.5, deviation_threshold=0.0035)

        with self.assertRaises(AssertionError) as exception_info:
            collect_bank_fit_results('fit_results', acceptance_summary=None, parameters_table_group=None)
        assert 'fit results should be different than None' in str(exception_info.exception)

        # collect only the acceptance criteria results
        collect_bank_fit_results('fit_results', acceptance_summary='summary', parameters_table_group=None)
        assert spectra_labels('fit_results') == ['success', 'deviation', 'Z-score']
        assert_allclose(mtd['fit_results'].readY(0), mtd['summary'].readY(0))
        DeleteWorkspaces(['fit_results'])

        # collect only the polynomial coefficients
        collect_bank_fit_results('fit_results', acceptance_summary=None, parameters_table_group='parameters_tables')
        assert spectra_labels('fit_results') == ['A0', 'A1', 'A2']
        for coefficient_idx in range(3):  # we have three polynomial coefficients
            for tube_idx in range(TUBES_IN_BANK):
                self.assertAlmostEqual(mtd['fit_results'].readY(coefficient_idx)[tube_idx],
                                       mtd[f'parameters_tables_{tube_idx}'].row(coefficient_idx)['Value'], delta=1e-6)
                self.assertAlmostEqual(mtd['fit_results'].readE(coefficient_idx)[tube_idx],
                                       mtd[f'parameters_tables_{tube_idx}'].row(coefficient_idx)['Error'], delta=1e-6)
        DeleteWorkspaces(['fit_results'])

        # collect both acceptance criteria and polynomial coefficients
        collect_bank_fit_results('fit_results', acceptance_summary='summary',
                                 parameters_table_group='parameters_tables')
        assert spectra_labels('fit_results') == ['success', 'deviation', 'Z-score', 'A0', 'A1', 'A2']
        for spectrum_idx in [0, 1, 2]:
            assert_allclose(mtd['fit_results'].readY(spectrum_idx), mtd['summary'].readY(spectrum_idx))
            assert_allclose(mtd['fit_results'].readE(spectrum_idx), mtd['summary'].readE(spectrum_idx))
        for coefficient_idx, spectrum_idx in [(0, 3), (1, 4), (2, 5)]:  # we have three polynomial coefficients
            for tube_idx in range(TUBES_IN_BANK):
                self.assertAlmostEqual(mtd['fit_results'].readY(spectrum_idx)[tube_idx],
                                       mtd[f'parameters_tables_{tube_idx}'].row(coefficient_idx)['Value'], delta=1e-6)
                self.assertAlmostEqual(mtd['fit_results'].readE(spectrum_idx)[tube_idx],
                                       mtd[f'parameters_tables_{tube_idx}'].row(coefficient_idx)['Error'], delta=1e-6)
        DeleteWorkspaces(['CalibTable', 'fit_results', 'parameters_tables', 'PeakTable', 'PeakYTable', 'summary'])
Beispiel #25
0
 def performOperation(self):
     lhs_valid, rhs_valid, err_msg = self.validateInputs()
     if err_msg != str():
         return lhs_valid, rhs_valid, err_msg
     lhs_ws, rhs_ws = self._scale_input_workspaces()
     try:
         if self._operation == '+':
             if self._md_lhs or self._md_rhs:
                 PlusMD(LHSWorkspace=lhs_ws,
                        RHSWorkspace=rhs_ws,
                        OutputWorkspace=self._output_ws)
             else:
                 Plus(LHSWorkspace=lhs_ws,
                      RHSWorkspace=rhs_ws,
                      OutputWorkspace=self._output_ws)
         elif self._operation == '-':
             if self._md_lhs or self._md_rhs:
                 MinusMD(LHSWorkspace=lhs_ws,
                         RHSWorkspace=rhs_ws,
                         OutputWorkspace=self._output_ws)
             else:
                 Minus(LHSWorkspace=lhs_ws,
                       RHSWorkspace=rhs_ws,
                       OutputWorkspace=self._output_ws)
         elif self._operation == '*':
             if self._md_lhs or self._md_rhs:
                 MultiplyMD(LHSWorkspace=lhs_ws,
                            RHSWorkspace=rhs_ws,
                            OutputWorkspace=self._output_ws)
             else:
                 Multiply(LHSWorkspace=lhs_ws,
                          RHSWorkspace=rhs_ws,
                          OutputWorkspace=self._output_ws)
         elif self._operation == 'WM':
             if self._md_lhs or self._md_rhs:
                 WeightedMeanMD(LHSWorkspace=lhs_ws,
                                RHSWorkspace=rhs_ws,
                                OutputWorkspace=self._output_ws)
             else:
                 WeightedMean(InputWorkspace1=lhs_ws,
                              InputWorkspace2=rhs_ws,
                              OutputWorkspace=self._output_ws)
         else:
             if self._md_lhs or self._md_rhs:
                 DivideMD(LHSWorkspace=lhs_ws,
                          RHSWorkspace=rhs_ws,
                          OutputWorkspace=self._output_ws)
             else:
                 Divide(LHSWorkspace=lhs_ws,
                        RHSWorkspace=rhs_ws,
                        OutputWorkspace=self._output_ws)
     except (RuntimeError, ValueError) as err:
         return False, False, str(err)
     else:
         self._regularize_output_names(self._output_ws)
     finally:
         DeleteWorkspaces(WorkspaceList=[lhs_ws, rhs_ws])
     return True, True, ""
Beispiel #26
0
    def tearDownClass(cls):
        """
        Remove workspaces from ADS.
        """

        DeleteWorkspaces([
            'sample_ws', 'can_ws', 'corrections_ws', 'fws_corrections_ass',
            'fws_corrections', 'fapi'
        ])
Beispiel #27
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()
Beispiel #28
0
    def runTest(self):
        ws_name = 'MultiFilePredictPeaksIntegrate'
        HB3AAdjustSampleNorm(
            Filename="HB3A_exp0724_scan0182.nxs, HB3A_exp0724_scan0183.nxs",
            NormaliseBy='None',
            OutputWorkspace=ws_name + '_data')

        HB3APredictPeaks(InputWorkspace=ws_name + "_data",
                         OutputWorkspace=ws_name + "_peaks")

        peaks = mtd[ws_name + "_peaks"]

        self.assertEqual(peaks.getNumberOfEntries(), 2)

        HB3AIntegratePeaks(InputWorkspace=ws_name + '_data',
                           PeaksWorkspace=ws_name + '_peaks',
                           PeakRadius=0.25,
                           OutputWorkspace=ws_name + '_integrated')

        integrated = mtd[ws_name + '_integrated']

        self.assertEqual(
            peaks.getItem(0).getNumberPeaks() +
            peaks.getItem(1).getNumberPeaks(), integrated.getNumberPeaks())

        # Should be the same as the UB from the data
        ol = integrated.sample().getOrientedLattice()
        self.assertDelta(ol.a(), 5.238389802, 1.e-7)
        self.assertDelta(ol.b(), 5.238415883, 1.e-7)
        self.assertDelta(ol.c(), 19.65194598, 1.e-7)
        self.assertDelta(ol.alpha(), 89.9997207, 1.e-7)
        self.assertDelta(ol.beta(), 89.9997934, 1.e-7)
        self.assertDelta(ol.gamma(), 89.9999396, 1.e-7)

        peak0 = integrated.getPeak(0)
        self.assertEqual(peak0.getH(), 0)
        self.assertEqual(peak0.getK(), 0)
        self.assertEqual(peak0.getL(), -11)
        self.assertEqual(peak0.getRunNumber(), 182)
        self.assertAlmostEqual(peak0.getWavelength(), 1.008, places=5)
        self.assertAlmostEqual(peak0.getIntensity(), 127.20005, places=5)
        self.assertEqual(peak0.getBinCount(), 0)

        peak1 = integrated.getPeak(integrated.getNumberPeaks() - 1)
        self.assertEqual(peak1.getH(), 5)
        self.assertEqual(peak1.getK(), 0)
        self.assertEqual(peak1.getL(), 1)
        self.assertEqual(peak1.getRunNumber(), 183)
        self.assertAlmostEqual(peak1.getWavelength(), 1.008, places=5)
        self.assertAlmostEqual(peak1.getIntensity(), 66.0945249, places=5)
        self.assertEqual(peak1.getBinCount(), 0)

        DeleteWorkspaces(
            [ws_name + '_data', ws_name + '_peaks', ws_name + '_integrated'])
Beispiel #29
0
    def test_calibrate_banks(self):
        calibrations, masks = calibrate_banks(self.cases['124023_banks_14_15'], '14-15')
        assert list(calibrations.getNames()) == ['calib14', 'calib15']
        assert list(masks.getNames()) == ['mask14']
        assert mtd['calib14'].rowCount() == 256 * (16 - 3)
        assert mtd['mask14'].rowCount() == 256 * 3
        assert mtd['calib15'].rowCount() == 256 * 16
        self.assertEqual(mtd['acceptance14'].readY(0).tolist(), [1, 1, 0., 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1])
        self.assertEqual(mtd['acceptance15'].readY(0).tolist(), [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])

        DeleteWorkspaces(['calibrations', 'masks', 'acceptances'])
Beispiel #30
0
    def runTest(self):
        ws_name = 'MultiFileFindPeaksIntegrate'
        HB3AAdjustSampleNorm(
            Filename="HB3A_exp0724_scan0182.nxs, HB3A_exp0724_scan0183.nxs",
            NormaliseBy='None',
            OutputWorkspace=ws_name + '_data')

        HB3AFindPeaks(InputWorkspace=ws_name + '_data',
                      CellType="Orthorhombic",
                      Centering="F",
                      OutputWorkspace=ws_name + '_peaks')

        peaks = mtd[ws_name + '_peaks']
        self.assertEqual(peaks.getNumberOfEntries(), 2)

        HB3AIntegratePeaks(InputWorkspace=ws_name + '_data',
                           PeaksWorkspace=ws_name + '_peaks',
                           PeakRadius=0.25,
                           OutputWorkspace=ws_name + '_integrated')

        integrated = mtd[ws_name + '_integrated']

        self.assertEqual(
            peaks.getItem(0).getNumberPeaks() +
            peaks.getItem(1).getNumberPeaks(), integrated.getNumberPeaks())

        ol = integrated.sample().getOrientedLattice()
        self.assertDelta(ol.a(), 5.261051697, 1.e-7)
        self.assertDelta(ol.b(), 5.224167511, 1.e-7)
        self.assertDelta(ol.c(), 19.689643636, 1.e-7)
        self.assertEqual(ol.alpha(), 90)
        self.assertEqual(ol.beta(), 90)
        self.assertEqual(ol.gamma(), 90)

        peak0 = integrated.getPeak(0)
        self.assertEqual(peak0.getH(), 0)
        self.assertEqual(peak0.getK(), 0)
        self.assertEqual(peak0.getL(), 10)
        self.assertEqual(peak0.getRunNumber(), 182)
        self.assertAlmostEqual(peak0.getWavelength(), 1.008, places=5)
        self.assertAlmostEqual(peak0.getIntensity(), 6581.5580392, places=5)
        self.assertEqual(peak0.getBinCount(), 827)

        peak1 = integrated.getPeak(integrated.getNumberPeaks() - 1)
        self.assertEqual(peak1.getH(), 0)
        self.assertEqual(peak1.getK(), 4)
        self.assertEqual(peak1.getL(), 0)
        self.assertEqual(peak1.getRunNumber(), 183)
        self.assertAlmostEqual(peak1.getWavelength(), 1.008, places=5)
        self.assertAlmostEqual(peak1.getIntensity(), 11853.7538139, places=5)
        self.assertEqual(peak1.getBinCount(), 134)

        DeleteWorkspaces(
            [ws_name + '_data', ws_name + '_peaks', ws_name + '_integrated'])