def _cloneTestWorkspace(self, wsName=None):
     if not wsName:
         # Cannot use as default parameter as 'self' is not know in argument list.
         wsName = self._TEST_WS_NAME
     tempName = 'temp_testWS_'
     mtd.addOrReplace(tempName, self._testIN5WS)
     ws = CloneWorkspace(InputWorkspace=tempName,
                         OutputWorkspace=wsName)
     mtd.remove(tempName)
     return ws
    def test_SimpleAlgorithm_Accepts_Group_Handle(self):
        from mantid.simpleapi import Scale
        self.create_matrix_workspace_in_ADS("First")
        self.create_matrix_workspace_in_ADS("Second")
        run_algorithm('GroupWorkspaces', InputWorkspaces='First,Second', OutputWorkspace='grouped')
        group = mtd['grouped']

        try:
            w = Scale(group, 1.5)
            mtd.remove(str(w))
        except Exception as exc:
            self.fail("Algorithm raised an exception with input as WorkspaceGroup: '" + str(exc) + "'")
        mtd.remove(str(group))
Example #3
0
    def test_SimpleAlgorithm_Accepts_Group_Handle(self):
        from mantid.simpleapi import Scale

        run_algorithm('CreateWorkspace', OutputWorkspace='First',DataX=[1.,2.,3.], DataY=[2.,3.], DataE=[2.,3.],UnitX='TOF')
        run_algorithm('CreateWorkspace', OutputWorkspace='Second',DataX=[1.,2.,3.], DataY=[2.,3.], DataE=[2.,3.],UnitX='TOF')
        run_algorithm('GroupWorkspaces',InputWorkspaces='First,Second',OutputWorkspace='group')
        group = mtd['group']
        self.assertEquals(group.getName(), "group")
        self.assertEquals(type(group), WorkspaceGroup)
        try:
            w = Scale(group, 1.5)
            mtd.remove(str(w))
        except Exception, exc:
            self.fail("Algorithm raised an exception with input as WorkspaceGroup: '" + str(exc) + "'")
 def setUp(self):
     if DirectILLDiagnosticsTest._TEST_WS is None:
         DirectILLDiagnosticsTest._TEST_WS = illhelpers.create_poor_mans_in5_workspace(self._BKG_LEVEL,
                                                                                       illhelpers.default_test_detectors)
     inWSName = 'inputWS'
     mtd.addOrReplace(inWSName, DirectILLDiagnosticsTest._TEST_WS)
     kwargs = {
         'InputWorkspace': DirectILLDiagnosticsTest._TEST_WS,
         'OutputWorkspace': self._TEST_WS_NAME,
         'EPPCreationMethod': 'Fit EPP',
         'FlatBkg': 'Flat Bkg ON',
         'OutputEPPWorkspace': self._EPP_WS_NAME,
         'OutputRawWorkspace': self._RAW_WS_NAME
     }
     run_algorithm('DirectILLCollectData', **kwargs)
     mtd.remove(inWSName)
Example #5
0
    def test_optional_workspaces_are_ignored_if_not_present_in_output_even_if_given_as_input(self):
        # Test algorithm
        from mantid.api import (
            AlgorithmManager,
            PropertyMode,
            PythonAlgorithm,
            MatrixWorkspaceProperty,
            WorkspaceFactory,
        )
        from mantid.kernel import Direction

        class OptionalWorkspace(PythonAlgorithm):
            def PyInit(self):
                self.declareProperty(MatrixWorkspaceProperty("RequiredWorkspace", "", Direction.Output))
                self.declareProperty(
                    MatrixWorkspaceProperty("OptionalWorkspace", "", Direction.Output, PropertyMode.Optional)
                )

            def PyExec(self):
                ws = WorkspaceFactory.create("Workspace2D", NVectors=1, YLength=1, XLength=1)
                ws.dataY(0)[0] = 5
                self.setProperty("RequiredWorkspace", ws)
                self.getLogger().notice("done!")

        AlgorithmFactory.subscribe(OptionalWorkspace)

        # temporarily attach it to simpleapi module
        name = "OptionalWorkspace"
        algm_object = AlgorithmManager.createUnmanaged(name, 1)
        algm_object.initialize()
        simpleapi._create_algorithm_function(name, 1, algm_object)  # Create the wrapper

        # Call with no optional output specified
        result = simpleapi.OptionalWorkspace(RequiredWorkspace="required")
        self.assertTrue(isinstance(result, MatrixWorkspace))
        self.assertAlmostEqual(5, result.readY(0)[0], places=12)
        mtd.remove("required")

        # Call with both outputs specified
        result = simpleapi.OptionalWorkspace(RequiredWorkspace="required", OptionalWorkspace="optional")
        self.assertTrue(isinstance(result, MatrixWorkspace))
        self.assertAlmostEqual(5, result.readY(0)[0], places=12)
        mtd.remove("required")

        # Tidy up simple api function
        del simpleapi.OptionalWorkspace
Example #6
0
    def test_interface(self):
        """ Rudimentary test to get peak and get/set some values """
        pws = mtd["peaks"]
        self.assertTrue(isinstance(pws, IPeaksWorkspace))
        self.assertEqual(pws.getNumberPeaks(), 1)
        p = pws.getPeak(0)

        # Try a few IPeak get/setters. Not everything.
        p.setH(234)
        self.assertEqual(p.getH(), 234)
        p.setHKL(5, 6, 7)
        self.assertEqual(p.getH(), 5)
        self.assertEqual(p.getK(), 6)
        self.assertEqual(p.getL(), 7)

        hkl = p.getHKL()
        self.assertEquals(hkl, V3D(5, 6, 7))

        p.setIntensity(456)
        p.setSigmaIntensity(789)
        self.assertEqual(p.getIntensity(), 456)
        self.assertEqual(p.getSigmaIntensity(), 789)

        # Finally try to remove a peak
        pws.removePeak(0)
        self.assertEqual(pws.getNumberPeaks(), 0)

        # Create a new peak at some Q in the lab frame
        qlab = V3D(1, 2, 3)
        p = pws.createPeak(qlab, 1.54)
        self.assertAlmostEquals(p.getQLabFrame().X(), 1.0, 3)

        # Now try to add the peak back
        pws.addPeak(p)
        self.assertEqual(pws.getNumberPeaks(), 1)

        # Check that it is what we added to it
        p = pws.getPeak(0)
        self.assertAlmostEquals(p.getQLabFrame().X(), 1.0, 3)

        # Peaks workspace will not be integrated by default.
        self.assertTrue(not pws.hasIntegratedPeaks())

        mtd.remove("cncs")
        mtd.remove("peaks")
Example #7
0
    def test_group_index_access_returns_correct_workspace(self):
        run_algorithm('CreateWorkspace', OutputWorkspace='First',DataX=[1.,2.,3.], DataY=[2.,3.], DataE=[2.,3.],UnitX='TOF')
        run_algorithm('CreateWorkspace', OutputWorkspace='Second',DataX=[4.,5.,6.], DataY=[4.,5.], DataE=[2.,3.],UnitX='TOF')
        run_algorithm('CreateWorkspace', OutputWorkspace='Third',DataX=[7.,8.,9.], DataY=[6.,7.], DataE=[2.,3.],UnitX='TOF')
        run_algorithm('GroupWorkspaces',InputWorkspaces='First,Second,Third',OutputWorkspace='grouped')
        group = mtd['grouped']

        self.assertRaises(IndexError, group.__getitem__, 3) # Index out of bounds
        for i in range(3):
            member = group[i]
            self.assertTrue(isinstance(member, MatrixWorkspace))

        # Clearing the data should leave the handle unusable
        member = group[0]
        mtd.remove("First")
        try:
            member.name()
            self.fail("Handle for item extracted from WorkspaceGroup is still usable after ADS has been cleared, it should be a weak reference and raise an error.")
        except RuntimeError as exc:
            self.assertEquals(str(exc), 'Variable invalidated, data has been deleted.')
Example #8
0
    def test_later_store_in_ADS(self):
        from mantid.api import MatrixWorkspaceProperty
        from mantid.kernel import Direction

        class SimpleAPIPythonAlgorithm6(PythonAlgorithm):
            def PyInit(self):
                self.declareProperty(MatrixWorkspaceProperty("OutputWorkspace", "", Direction.Output))

            def PyExec(self):
                from mantid.simpleapi import CreateSampleWorkspace
                workspaceNotInADS = CreateSampleWorkspace(StoreInADS=False)
                assert(workspaceNotInADS)
                self.setProperty("OutputWorkspace", workspaceNotInADS)
        AlgorithmFactory.subscribe(SimpleAPIPythonAlgorithm6)

        alg = SimpleAPIPythonAlgorithm6()
        alg.initialize()
        alg.setPropertyValue("OutputWorkspace", "out")
        alg.execute()
        self.assertTrue('out' in mtd)
        mtd.remove('out')
 def setUp(self):
     if not self._testIN5WS:
         self._testIN5WS = illhelpers.create_poor_mans_in5_workspace(self._BKG_LEVEL,
                                                                     illhelpers.default_test_detectors)
     inWSName = 'inputWS'
     mtd.addOrReplace(inWSName, self._testIN5WS)
     kwargs = {
         'InputWorkspace': self._testIN5WS,
         'OutputWorkspace': self._TEST_WS_NAME,
         'OutputEPPWorkspace': self._EPP_WS_NAME
     }
     run_algorithm('DirectILLCollectData', **kwargs)
     kwargs = {
         'InputWorkspace': self._TEST_WS_NAME,
         'OutputWorkspace': self._VANADIUM_WS_NAME,
         'EPPWorkspace': self._EPP_WS_NAME
     }
     run_algorithm('DirectILLIntegrateVanadium', **kwargs)
     vanadiumWS = mtd[self._VANADIUM_WS_NAME]
     for i in range(vanadiumWS.getNumberHistograms()):
         vanadiumYs = vanadiumWS.dataY(i)
         vanadiumYs.fill(1.0)
     mtd.remove(inWSName)
Example #10
0
    def test_complex_binary_operations_with_group_do_not_leave_temporary_workspaces_in_ADS(self):
        run_algorithm('CreateWorkspace', OutputWorkspace='grouped_1',DataX=[1.,2.,3.], DataY=[2.,3.], DataE=[2.,3.],UnitX='TOF')
        run_algorithm('CreateWorkspace', OutputWorkspace='grouped_2',DataX=[1.,2.,3.], DataY=[2.,3.], DataE=[2.,3.],UnitX='TOF')
        run_algorithm('GroupWorkspaces',InputWorkspaces='grouped_1,grouped_2',OutputWorkspace='grouped')

        w1=(mtd['grouped']*0.0)+1.0

        self.assertTrue('w1' in mtd)
        self.assertTrue('grouped' in mtd)
        self.assertTrue('grouped_1' in mtd)
        self.assertTrue('grouped_2' in mtd)
        self.assertTrue('__python_op_tmp0' not in mtd)
        self.assertTrue('__python_op_tmp0_1' not in mtd)
        self.assertTrue('__python_op_tmp0_2' not in mtd)

        mtd.remove('w1')
        mtd.remove('grouped')
        mtd.remove('grouped_1')
        mtd.remove('grouped_2')
def cleanFit():
    """Removes workspaces created during the fit"""
    mtd.remove('data')
    mtd.remove('fit_NormalisedCovarianceMatrix')
    mtd.remove('fit_Parameters')
    mtd.remove('fit_Workspace')
Example #12
0
 def tearDown(self):
     if self.wsData_name in mtd:
         mtd.remove(self.wsData_name)
     if self.wsVana_name in mtd:
         mtd.remove(self.wsVana_name)
Example #13
0
 def test_vanilla_python_default_in_ADS(self):
     from mantid.simpleapi import CreateSampleWorkspace
     out = CreateSampleWorkspace()
     self.assertTrue(out)
     self.assertTrue('out' in mtd)
     mtd.remove('out')
 def _clear(self, expected):
     for workspace in expected:
         mtd.remove(workspace)
Example #15
0
 def tearDown(self):
     if self.ws_name in mtd:
         mtd.remove(self.ws_name)
Example #16
0
 def tearDown(self):
     for wsName in mtd.getObjectNames():
         if wsName.startswith(self.prefix):
             mtd.remove(wsName)
Example #17
0
 def test_same_var_name_with_and_without_ADS(self):
     from mantid.simpleapi import CreateSampleWorkspace
     ws = CreateSampleWorkspace() # in ADS
     ws = CreateSampleWorkspace(StoreInADS=False) # not in ADS
     mtd.remove('ws')
     self.assertTrue(ws)
Example #18
0
 def test_vanilla_python_explicit_in_ADS(self):
     from mantid.simpleapi import CreateSampleWorkspace
     out = CreateSampleWorkspace(StoreInADS=True)
     self.assertTrue(out)
     self.assertTrue('out' in mtd)
     mtd.remove('out')
Example #19
0
 def test_vanilla_python_default_in_ADS(self):
     from mantid.simpleapi import CreateSampleWorkspace
     out = CreateSampleWorkspace()
     self.assertTrue(out)
     self.assertTrue('out' in mtd)
     mtd.remove('out')
Example #20
0
 def test_vanilla_python_explicit_in_ADS(self):
     from mantid.simpleapi import CreateSampleWorkspace
     out = CreateSampleWorkspace(StoreInADS=True)
     self.assertTrue(out)
     self.assertTrue('out' in mtd)
     mtd.remove('out')
Example #21
0
    def test_SimpleAlgorithm_Accepts_Group_Handle(self):
        from mantid.simpleapi import Scale

        run_algorithm('CreateWorkspace', OutputWorkspace='First',DataX=[1.,2.,3.], DataY=[2.,3.], DataE=[2.,3.],UnitX='TOF')
        run_algorithm('CreateWorkspace', OutputWorkspace='Second',DataX=[1.,2.,3.], DataY=[2.,3.], DataE=[2.,3.],UnitX='TOF')
        run_algorithm('GroupWorkspaces',InputWorkspaces='First,Second',OutputWorkspace='group')
        group = mtd['group']
        self.assertEquals(group.getName(), "group")
        self.assertEquals(type(group), WorkspaceGroup)
        try:
            w = Scale(group, 1.5)
            mtd.remove(str(w))
        except Exception, exc:
            self.fail("Algorithm raised an exception with input as WorkspaceGroup: '" + str(exc) + "'")
        mtd.remove(str(group))

    def test_complex_binary_operations_with_group_do_not_leave_temporary_workspaces_in_ADS(self):
        run_algorithm('CreateWorkspace', OutputWorkspace='grouped_1',DataX=[1.,2.,3.], DataY=[2.,3.], DataE=[2.,3.],UnitX='TOF')
        run_algorithm('CreateWorkspace', OutputWorkspace='grouped_2',DataX=[1.,2.,3.], DataY=[2.,3.], DataE=[2.,3.],UnitX='TOF')
        run_algorithm('GroupWorkspaces',InputWorkspaces='grouped_1,grouped_2',OutputWorkspace='grouped')

        w1=(mtd['grouped']*0.0)+1.0

        self.assertTrue('w1' in mtd)
        self.assertTrue('grouped' in mtd)
        self.assertTrue('grouped_1' in mtd)
        self.assertTrue('grouped_2' in mtd)
        self.assertTrue('__python_op_tmp0' not in mtd)
        self.assertTrue('__python_op_tmp0_1' not in mtd)
        self.assertTrue('__python_op_tmp0_2' not in mtd)
Example #22
0
 def test_same_var_name_with_and_without_ADS(self):
     from mantid.simpleapi import CreateSampleWorkspace
     ws = CreateSampleWorkspace()  # in ADS
     ws = CreateSampleWorkspace(StoreInADS=False)  # not in ADS
     mtd.remove('ws')
     self.assertTrue(ws)
Example #23
0
 def tearDown(self):
     if self.ws_name in mtd:
         mtd.remove(self.ws_name)
     monitor_name = self.ws_name + '_monitors'
     if monitor_name in mtd:
         mtd.remove(monitor_name)
Example #24
0
 def _removeWorkspace(self, name):
     if name in mtd:
         mtd.remove(name)
Example #25
0
 def tearDown(self):
     if self.ws_name in mtd:
         mtd.remove(self.ws_name)
     monitor_name = self.ws_name + '_monitors'
     if monitor_name in mtd:
         mtd.remove(monitor_name)
Example #26
0
 def tearDown(self):
     for wsName in mtd.getObjectNames():
         if wsName.startswith(self.prefix):
             mtd.remove(wsName)
Example #27
0
 def _clear(self, expected):
     for workspace in expected:
         mtd.remove(workspace)
Example #28
0
 def tearDown(self):
     if self.ws_name in mtd:
         mtd.remove(self.ws_name)
Example #29
0
 def tearDown(self):
     if self.wsData_name in mtd:
         mtd.remove(self.wsData_name)
     if self.wsVana_name in mtd:
         mtd.remove(self.wsVana_name)