diff --git a/Code/Mantid/Framework/PythonInterface/plugins/algorithms/GetNegMuMuonicXRD.py b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/GetNegMuMuonicXRD.py index 03b256417777a1c7609f0f20567d3787bd4b331f..bf156ae1a99422078dedda684c114cde1f834425 100644 --- a/Code/Mantid/Framework/PythonInterface/plugins/algorithms/GetNegMuMuonicXRD.py +++ b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/GetNegMuMuonicXRD.py @@ -5,17 +5,17 @@ from mantid.simpleapi import * #pylint: disable=no-init class GetNegMuMuonicXRD(PythonAlgorithm): #Dictionary of <element>:<peaks> easily extendible by user. - Muonic_XR={'Au' :[8135.2,8090.6,8105.4,8069.4,5764.89,5594.97,3360.2, + muonic_xr ={'Au' :[8135.2,8090.6,8105.4,8069.4,5764.89,5594.97,3360.2, 3206.8,2474.22,2341.21,2304.44,1436.05,1391.58,1104.9, 899.14,869.98,405.654,400.143], 'Ag': [3184.7,3147.7,901.2,869.2,308.428,304.759], 'Cu' :[1512.78,1506.61,334.8,330.26], 'Zn' :[1600.15,1592.97,360.75,354.29], 'Pb' :[8523.3,8442.11,5966.0,5780.1,2641.8,2499.7, - 2459.7,1511.63,1214.12,1028.83,972.3,938.4, - 437.687,431.285], - 'As' : [1866.9,1855.8,436.6,427.5], - 'Sn' : [3457.3,3412.8,1022.6,982.5,349.953,345.226]} + 2459.7,1511.63,1214.12,1028.83,972.3,938.4, + 437.687,431.285], + 'As' :[1866.9,1855.8,436.6,427.5], + 'Sn' :[3457.3,3412.8,1022.6,982.5,349.953,345.226]} def PyInit(self): self.declareProperty(StringArrayProperty("Elements", values=[], @@ -24,39 +24,39 @@ class GetNegMuMuonicXRD(PythonAlgorithm): self.declareProperty(name="YAxisPosition", defaultValue=-0.001, doc="Position for Markers on the y-axis") + self.declareProperty(WorkspaceGroupProperty('OutputWorkspace', '', direction=Direction.Output), + doc='The output workspace will always be a GroupWorkspaces ' + 'that will have the workspaces of each' + ' muonicXR workspace created') - def get_Muonic_XR(self, element): + def get_muonic_xr(self, element): #retrieve peak values from dictionary Muonic_XR - peak_values = self.Muonic_XR[element] + peak_values = self.muonic_xr[element] return peak_values - def Create_MuonicXR_WS(self, element, YPos): + def create_muonic_xr_ws(self, element, y_pos): #retrieve the values from Muonic_XR - xr_peak_values = self.get_Muonic_XR(element) + xr_peak_values = self.get_muonic_xr(element) #Calibrate y-axis for workspace - YPos_WS = [YPos]*len(xr_peak_values) + y_pos_ws = [y_pos]*len(xr_peak_values) xvalues = xr_peak_values - WS_Muon_XR = CreateWorkspace(xvalues, YPos_WS[:]) - RenameWorkspaces(WS_Muon_XR, WorkspaceNames="MuonXRWorkspace_"+element) - return WS_Muon_XR - + muon_xr_ws = CreateWorkspace(xvalues, y_pos_ws[:]) + RenameWorkspaces(muon_xr_ws, WorkspaceNames="MuonXRWorkspace_"+element) + return muon_xr_ws def category(self): - return "Muon" + return "Muon;PythonAlgorithms" def PyExec(self): elements = self.getProperty("Elements").value - yposition = self.getProperty("YAxisPosition").value + y_position = self.getProperty("YAxisPosition").value workspace_list = [None]*len(elements) - for idx,element in enumerate(elements): - curr_workspace = self.Create_MuonicXR_WS(element, yposition) + curr_workspace = self.create_muonic_xr_ws(element, y_position) workspace_list[idx] = curr_workspace - if len(elements) == 1: - MuonXRWorkspace = workspace_list[0] - self.log().information(str("Created: "+MuonXRWorkspace.name())) - else: - MuonicXR_group = GroupWorkspaces(workspace_list) - self.log().information(str("Created Group: "+MuonicXR_group.name())) + + muonic_xr_group = GroupWorkspaces(workspace_list) + self.setProperty('OutputWorkspace', muonic_xr_group) + self.log().information(str("Created Group: "+muonic_xr_group.name())) AlgorithmFactory.subscribe(GetNegMuMuonicXRD) diff --git a/Code/Mantid/Framework/PythonInterface/plugins/algorithms/GetNuMegMuonicXRD.py b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/GetNuMegMuonicXRD.py deleted file mode 100644 index fc6876d6d85f99da66dd38b95cf16800e8de6985..0000000000000000000000000000000000000000 --- a/Code/Mantid/Framework/PythonInterface/plugins/algorithms/GetNuMegMuonicXRD.py +++ /dev/null @@ -1,61 +0,0 @@ -from mantid.api import * # PythonAlgorithm, registerAlgorithm, WorkspaceProperty -from mantid.kernel import * - -#pylint: disable=no-init -class GetNegMuMuonicXRD(PythonAlgorithm): - #Dictionary of <element>:<peaks> easily extendible by user. - Muonic_XR={'Au' :[8135.2,8090.6,8105.4,8069.4,5764.89,5594.97,3360.2, - 3206.8,2474.22,2341.21,2304.44,1436.05,1391.58,1104.9, - 899.14,869.98,405.654,400.143], - 'Ag' :[3184.7,3147.7,901.2,869.2,308.428,304.759], - 'Cu' :[1512.78,1506.61,334.8,330.26], - 'Zn' :[1600.15,1592.97,360.75,354.29], - 'Pb' :[8523.3,8442.11,5966.0,5780.1,2641.8,2499.7, - 2459.7,1511.63,1214.12,1028.83,972.3,938.4, - 437.687,431.285], - 'As' :[1866.9,1855.8,436.6,427.5], - 'Sn' :[3457.3,3412.8,1022.6,982.5,349.953,345.226]} - - def PyInit(self): - element_type = self.Muonic_XR.keys() - self.declareProperty("ElementList", "None", StringListValidator(element_type), - doc="List of available elements") - self.declareProperty(StringArrayProperty("Elements", values=[],direction=Direction.Input)) - self.declareProperty(name="YAxisPosition", - defaultValue=-0.001, - doc="Position for Markers on the y-axis") - - def get_Muonic_XR(self, element): - #retrieve peak values from dictionary Muonic_XR - peak_values = self.Muonic_XR[element] - return peak_values - - def Create_MuonicXR_WS(self, element, YPos): - #retrieve the values from Muonic_XR - xr_peak_values = self.get_Muonic_XR(element) - #Calibrate y-axis for workspace - YPos_WS = [YPos]*len(xr_peak_values) - xvalues = xr_peak_values - WS_Muon_XR = CreateWorkspace(xvalues, YPos_WS[:]) - RenameWorkspaces(WS_Muon_XR, WorkspaceNames="MuonXRWorkspace_"+element) - return WS_Muon_XR - - def category(self): - return "Muon" - - def PyExec(self): - elements = self.getProperty("Elements").value - yposition = self.getProperty("YAxisPosition").value - workspace_list = [None]*len(elements) - for idx,element in enumerate(elements): - curr_workspace = self.Create_MuonicXR_WS(element, yposition) - workspace_list[idx] = curr_workspace - - if len(elements) == 1: - MuonXRWorkspace = workspace_list[0] - self.log().information(str("Created: "+MuonXRWorkspace.name())) - else: - MuonicXR_group = GroupWorkspaces(workspace_list) - self.log().information(str("Created Group: "+MuonicXR_group.name())) - -AlgorithmFactory.subscribe(GetNegMuMuonicXRD) diff --git a/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/CMakeLists.txt b/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/CMakeLists.txt index e28b8e784ac376f6e9c2a83516da5209df2dfb85..a9cc2c323b8e7e70ba314442152bb99cea6ad05e 100644 --- a/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/CMakeLists.txt +++ b/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/CMakeLists.txt @@ -29,6 +29,7 @@ set ( TEST_PY_FILES FindReflectometryLinesTest.py FlatPlatePaalmanPingsCorrectionTest.py GetEiT0atSNSTest.py + GetNegMuMuonicXRDTest.py IndirectAnnulusAbsorptionTest.py IndirectCylinderAbsorptionTest.py IndirectFlatPlateAbsorptionTest.py diff --git a/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/GetNegMuMuonicXRDTest.py b/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/GetNegMuMuonicXRDTest.py new file mode 100644 index 0000000000000000000000000000000000000000..7da244e480e60ea29078e3588e41d690ea28d7bf --- /dev/null +++ b/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/GetNegMuMuonicXRDTest.py @@ -0,0 +1,130 @@ +import unittest +from mantid.kernel import * +from mantid.api import * +from mantid.simpleapi import * + +class GetNegMuMuonicXRDTest(unittest.TestCase): + au_muonic_xr = [8135.2,8090.6,8105.4,8069.4,5764.89,5594.97,3360.2, + 3206.8,2474.22,2341.21,2304.44,1436.05,1391.58,1104.9, + 899.14,869.98,405.654,400.143] + as_muonic_xr = [1866.9,1855.8,436.6,427.5] + + #TESTING FOR ONE WORKSPACE IN GROUP WORKSPACE + def test_muonic_xrd_single_ws_in_group(self): + #Setting up the work space manually + au_peak_values = self.au_muonic_xr + y_position = -0.001 #same as default used by GetNegMuMuonic + y_pos_ws = [y_position]*len(au_peak_values) + au_muon_xr_ws = CreateWorkspace(au_peak_values[:], y_pos_ws[:]) + #Check that au_muon_xr_ws is not null + self.assertFalse(au_muon_xr_ws==None) + au_muon_group = GroupWorkspaces(au_muon_xr_ws) + #Check that au_muon_group is not null + self.assertFalse(au_muon_group==None) + #Get the algorithm to produce the same workspace + neg_mu_xr_group = GetNegMuMuonicXRD("Au") #testing default y-Axis position value + #Check that neg_mu_xr_ws is not null + self.assertFalse(neg_mu_xr_group==None) + #Test number of workspaces in group + self.assertEqual(au_muon_group.getNumberOfEntries(), + neg_mu_xr_group.getNumberOfEntries()) + self.assertTrue(au_muon_group.size() == 1) + self.assertTrue(neg_mu_xr_group.size() == 1) + + #now testing the one workspace in the workspace group + neg_mu_xr_ws = neg_mu_xr_group[0] + au_muon_ws = au_muon_group[0] + #check number of histograms are equal + self.assertEqual(neg_mu_xr_ws.getNumberHistograms(), au_muon_ws.getNumberHistograms()) + #check number of bins is equal + self.assertEqual(au_muon_ws.blocksize(), neg_mu_xr_ws.blocksize()) + + #check length of XValues is the same + self.assertEqual(len(au_muon_ws.readX(0)), len(neg_mu_xr_ws.readX(0))) + #check all the XValues are the same + + #For RHEL6 (running an older version of python) this assert is not yet implemented: + #self.assertItemsEqual(au_muon_ws.readX(0),neg_mu_xr_ws.readX(0)) + #INSTEAD we will use a simple for loop + for x_value in range(len(au_muon_ws.readX(0))): + self.assertEqual(au_muon_ws.readX(0)[x_value], neg_mu_xr_ws.readX(0)[x_value]) + + #check length of YValues is the same + self.assertEqual(len(au_muon_ws.readY(0)), len(neg_mu_xr_ws.readY(0))) + #check all the YValues are the same + + #For RHEL6 (running an older version of python) this assert is not yet implemented: + #self.assertItemsEqual(au_muon_ws.readY(0),neg_mu_xr_ws.readY(0)) + #INSTEAD we will use a simple for loop + for y_value in range(len(au_muon_ws.readY(0))): + self.assertEqual(au_muon_ws.readY(0)[y_value], neg_mu_xr_ws.readY(0)[y_value]) + + #TESTING FOR MORE THAN ONE WORKSPACE IN GROUP WORKSPACE + def test_muonic_xrd_more_than_one_ws_in_group(self): + y_position = 0.2 + #Setting up au_muonic workspace + au_peak_values = self.au_muonic_xr + #check to see if workspace has been set to non-None value + self.assertFalse(au_peak_values == None) + + au_y_pos_ws = [y_position]*len(au_peak_values) + + #setting up as_muonic workspace + as_peak_values = self.as_muonic_xr + #check to see if workspace has been set to non-None value + self.assertFalse(as_peak_values == None) + + as_y_pos_ws = [y_position]*len(as_peak_values) + + au_muon_xr_ws = CreateWorkspace(au_peak_values,au_y_pos_ws[:]) + #check to see if workspace creation was successful + self.assertFalse(au_muon_xr_ws == None) + as_muon_xr_ws = CreateWorkspace(as_peak_values, as_y_pos_ws[:]) + #check to see if workspace creation was successful + self.assertFalse(as_muon_xr_ws == None) + + ws_list = [au_muon_xr_ws,as_muon_xr_ws] + grouped_muon_ws = GroupWorkspaces(ws_list) + #check to see whether grouping workspaces was successful + self.assertFalse(grouped_muon_ws == None) + + #Run algorithm that creates muonic_xr group workspace + group_muonic_xr_ws = GetNegMuMuonicXRD("Au,As", 0.2) + #check that this has assigned value correctly + self.assertFalse(group_muonic_xr_ws == None) + + #Compare histograms for each of the workspaces in GroupWorkspaces created + self.assertEqual(grouped_muon_ws[0].getNumberHistograms(), group_muonic_xr_ws[0].getNumberHistograms()) + self.assertEqual(grouped_muon_ws[1].getNumberHistograms(), group_muonic_xr_ws[1].getNumberHistograms()) + + #Compare length of X values read from each workspace in grouped workspace + self.assertEqual(len(grouped_muon_ws[0].readX(0)), len(group_muonic_xr_ws[0].readX(0))) + self.assertEqual(len(grouped_muon_ws[1].readX(0)), len(group_muonic_xr_ws[1].readX(0))) + + #Compare X values read from each workspace in grouped workspace + + #For RHEL6 (running an older version of python) this assert is not yet implemented: + #self.assertItemsEqual(grouped_muon_ws[0].readX(0), group_muonic_xr_ws[0].readX(0)) + #self.assertItemsEqual(grouped_muon_ws[1].readX(0), group_muonic_xr_ws[1].readX(0)) + #INSTEAD we will use a simple for loop + for x_value in range(len(grouped_muon_ws[0].readX(0))): + self.assertEqual(grouped_muon_ws[0].readX(0)[x_value], group_muonic_xr_ws[0].readX(0)[x_value]) + for x_value in range(len(grouped_muon_ws[1].readX(0))): + self.assertEqual(grouped_muon_ws[1].readX(0)[x_value], group_muonic_xr_ws[1].readX(0)[x_value]) + #Compare length of Y values read from each workspace in grouped workspace + self.assertEqual(len(grouped_muon_ws[0].readY(0)), len(group_muonic_xr_ws[0].readY(0))) + self.assertEqual(len(grouped_muon_ws[1].readY(0)), len(group_muonic_xr_ws[1].readY(0))) + + #Compare Y values read from each workspace in grouped workspace + + #For RHEL6 (running an older version of python) this assert is not yet implemented: + #self.assertItemsEqual(grouped_muon_ws[0].readY(0), group_muonic_xr_ws[0].readY(0)) + #self.assertItemsEqual(grouped_muon_ws[1].readY(0), group_muonic_xr_ws[1].readY(0)) + #INSTEAD we will use a simple for loop + for y_value in range(len(grouped_muon_ws[0].readY(0))): + self.assertEqual(grouped_muon_ws[0].readY(0)[y_value], group_muonic_xr_ws[0].readY(0)[y_value]) + for y_value in range(len(grouped_muon_ws[1].readY(0))): + self.assertEqual(grouped_muon_ws[1].readY(0)[y_value], group_muonic_xr_ws[1].readY(0)[y_value]) + +if __name__ == '__main__': + unittest.main()