From 02b28bae710c00901db89b8ea210e85a6db2cb62 Mon Sep 17 00:00:00 2001
From: Dan Nixon <dan@dan-nixon.com>
Date: Thu, 19 Mar 2015 14:06:56 +0000
Subject: [PATCH] Added unit test, fixed number wavelength issue

Refs #11325
---
 .../CylinderPaalmanPingsCorrection.py         |  31 +--
 .../python/plugins/algorithms/CMakeLists.txt  |   1 +
 .../CylinderPaalmanPingsCorrectionTest.py     | 193 ++++++++++++++++++
 3 files changed, 213 insertions(+), 12 deletions(-)
 create mode 100644 Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/CylinderPaalmanPingsCorrectionTest.py

diff --git a/Code/Mantid/Framework/PythonInterface/plugins/algorithms/CylinderPaalmanPingsCorrection.py b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/CylinderPaalmanPingsCorrection.py
index 63194550285..0bd716a44ca 100644
--- a/Code/Mantid/Framework/PythonInterface/plugins/algorithms/CylinderPaalmanPingsCorrection.py
+++ b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/CylinderPaalmanPingsCorrection.py
@@ -44,7 +44,7 @@ class CylinderPaalmanPingsCorrection(PythonAlgorithm):
         self.declareProperty(name='SampleNumberDensity', defaultValue=0.1,
                              validator=FloatBoundedValidator(0.0),
                              doc='Sample number density in atoms/Angstrom3')
-        self.declareProperty(name='SampleInnerRadius', defaultValue=0.09,
+        self.declareProperty(name='SampleInnerRadius', defaultValue=0.05,
                              doc='Sample inner radius')
         self.declareProperty(name='SampleOuterRadius', defaultValue=0.1,
                              doc='Sample outer radius')
@@ -68,12 +68,9 @@ class CylinderPaalmanPingsCorrection(PythonAlgorithm):
         self.declareProperty(name='BeamWidth', defaultValue=0.1,
                              doc='Width of the beam at the sample.')
 
-        self.declareProperty(name='StepSize', defaultValue=0.1,
+        self.declareProperty(name='StepSize', defaultValue=0.002,
                              doc='Step size for calculation')
 
-        self.declareProperty(name='NumberWavelengths', defaultValue=10,
-                             validator=IntBoundedValidator(1),
-                             doc='Number of wavelengths for calculation')
         self.declareProperty(name='Interpolate', defaultValue=True,
                              doc='Interpolate the correction workspaces to match the sample workspace')
 
@@ -124,12 +121,20 @@ class CylinderPaalmanPingsCorrection(PythonAlgorithm):
             can_sample = mtd[self._can_ws_name].sample()
             can_material = can_sample.getMaterial()
 
-        # total scattering x-section for can
-        sigs.append(can_material.totalScatterXSection())
-        sigs.append(can_material.totalScatterXSection())
-        # absorption x-section for can
-        siga.append(can_material.absorbXSection())
-        siga.append(can_material.absorbXSection())
+            # total scattering x-section for can
+            sigs.append(can_material.totalScatterXSection())
+            sigs.append(can_material.totalScatterXSection())
+            # absorption x-section for can
+            siga.append(can_material.absorbXSection())
+            siga.append(can_material.absorbXSection())
+
+        else:
+            # total scattering x-section for can
+            sigs.append(0.0)
+            sigs.append(0.0)
+            # absorption x-section for can
+            siga.append(0.0)
+            siga.append(0.0)
 
         # Holders for the corrected data
         data_ass = []
@@ -220,6 +225,9 @@ class CylinderPaalmanPingsCorrection(PythonAlgorithm):
         Get algorithm properties.
         """
 
+        # This is fixed by the Fortran code
+        self._number_wavelengths = 10
+
         self._sample_ws_name = self.getPropertyValue('SampleWorkspace')
 
         self._sample_chemical_formula = self.getPropertyValue('SampleChemicalFormula')
@@ -239,7 +247,6 @@ class CylinderPaalmanPingsCorrection(PythonAlgorithm):
         self._beam_height = self.getProperty('BeamHeight').value
         self._beam_width = self.getProperty('BeamWidth').value
 
-        self._number_wavelengths = self.getProperty('NumberWavelengths').value
         self._interpolate = self.getProperty('Interpolate').value
 
         self._emode = self.getPropertyValue('Emode')
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 1424514d62e..e225a757fcb 100644
--- a/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/CMakeLists.txt
+++ b/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/CMakeLists.txt
@@ -10,6 +10,7 @@ set ( TEST_PY_FILES
   CreateLeBailFitInputTest.py
   CreateCalibrationWorkspaceTest.py
   CreateWorkspaceTest.py
+  CylinderPaalmanPingsCorrectionTest.py
   DakotaChiSquaredTest.py
   DensityOfStatesTest.py
   DSFinterpTest.py
diff --git a/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/CylinderPaalmanPingsCorrectionTest.py b/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/CylinderPaalmanPingsCorrectionTest.py
new file mode 100644
index 00000000000..27955836344
--- /dev/null
+++ b/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/CylinderPaalmanPingsCorrectionTest.py
@@ -0,0 +1,193 @@
+import unittest
+from mantid.kernel import *
+from mantid.api import *
+from mantid.simpleapi import CreateSampleWorkspace, Scale, DeleteWorkspace, CylinderPaalmanPingsCorrection, CreateSimulationWorkspace
+from IndirectImport import is_supported_f2py_platform
+
+
+class CylinderPaalmanPingsCorrectionTest(unittest.TestCase):
+
+    def setUp(self):
+        """
+        Create sample workspaces.
+        """
+
+        # Create some test data
+        sample = CreateSampleWorkspace(NumBanks=1,
+                                            BankPixelWidth=1,
+                                            XUnit='Wavelength',
+                                            XMin=6.8,
+                                            XMax=7.9,
+                                            BinWidth=0.1)
+        self._sample_ws = sample
+
+        can = Scale(InputWorkspace=sample, Factor=1.2)
+        self._can_ws = can
+
+        self._corrections_ws_name = 'corrections'
+
+
+    def tearDown(self):
+        """
+        Remove workspaces from ADS.
+        """
+
+        DeleteWorkspace(self._sample_ws)
+        DeleteWorkspace(self._can_ws)
+
+        if self._corrections_ws_name in mtd:
+            DeleteWorkspace(self._corrections_ws_name)
+
+
+    def _verify_workspace(self, ws_name):
+        """
+        Do validation on a correction workspace.
+
+        @param ws_name Name of workspace to validate
+        """
+
+        corrections_ws = mtd[self._corrections_ws_name]
+
+        # Check it is in the corrections workspace group
+        self.assertTrue(corrections_ws.contains(ws_name))
+
+        test_ws = mtd[ws_name]
+
+        # Check workspace is in wavelength
+        self.assertEqual(test_ws.getAxis(0).getUnit().unitID(),
+                         'Wavelength')
+
+        # Check it has the same number of spectra as the sample
+        self.assertEqual(test_ws.getNumberHistograms(),
+                         self._sample_ws.getNumberHistograms())
+
+        # Check it has X binning matching sample workspace
+        self.assertEqual(test_ws.blocksize(), self._sample_ws.blocksize())
+
+
+    def _verify_workspaces_for_can(self):
+        """
+        Do validation on the additional correction factors for sample and can.
+        """
+
+        ass_ws_name = self._corrections_ws_name + '_ass'
+        assc_ws_name = self._corrections_ws_name + '_assc'
+        acsc_ws_name = self._corrections_ws_name + '_acsc'
+        acc_ws_name = self._corrections_ws_name + '_acc'
+
+        workspaces = [ass_ws_name, assc_ws_name, acsc_ws_name, acc_ws_name]
+
+        for workspace in workspaces:
+            self._verify_workspace(workspace)
+
+
+    def test_sampleOnly(self):
+        """
+        Test simple run with sample workspace only.
+        """
+
+        # Just pass if we can't actually run the algorithm
+        if not is_supported_f2py_platform():
+            return
+
+        CylinderPaalmanPingsCorrection(OutputWorkspace=self._corrections_ws_name,
+                                       SampleWorkspace=self._sample_ws,
+                                       SampleChemicalFormula='H2-O',
+                                       SampleInnerRadius=0.05,
+                                       SampleOuterRadius=0.1,
+                                       Emode='Indirect',
+                                       Efixed=1.845)
+
+        ass_ws_name = self._corrections_ws_name + '_ass'
+        self. _verify_workspace(ass_ws_name)
+
+
+    def test_sampleAndCan(self):
+        """
+        Test simple run with sample and can workspace.
+        """
+
+        # Just pass if we can't actually run the algorithm
+        if not is_supported_f2py_platform():
+            return
+
+        CylinderPaalmanPingsCorrection(OutputWorkspace=self._corrections_ws_name,
+                                       SampleWorkspace=self._sample_ws,
+                                       SampleChemicalFormula='H2-O',
+                                       SampleInnerRadius=0.05,
+                                       SampleOuterRadius=0.1,
+                                       CanWorkspace=self._can_ws,
+                                       CanChemicalFormula='V',
+                                       CanOuterRadius=0.15,
+                                       BeamHeight=0.1,
+                                       BeamWidth=0.1,
+                                       Emode='Indirect',
+                                       Efixed=1.845)
+
+        self._verify_workspaces_for_can()
+
+
+    def test_sampleAndCanDefaults(self):
+        """
+        Test simple run with sample and can workspace using the default values.
+        """
+
+        # Just pass if we can't actually run the algorithm
+        if not is_supported_f2py_platform():
+            return
+
+        CylinderPaalmanPingsCorrection(OutputWorkspace=self._corrections_ws_name,
+                                       SampleWorkspace=self._sample_ws,
+                                       SampleChemicalFormula='H2-O',
+                                       CanWorkspace=self._can_ws,
+                                       CanChemicalFormula='V')
+
+        self._verify_workspaces_for_can()
+
+
+    def test_InterpolateDisabled(self):
+        """
+        Tests that a workspace with a bin count equal to NumberWavelengths is created
+        when interpolation is disabled.
+        """
+
+        # Just pass if we can't actually run the algorithm
+        if not is_supported_f2py_platform():
+            return
+
+        CylinderPaalmanPingsCorrection(OutputWorkspace=self._corrections_ws_name,
+                                       SampleWorkspace=self._sample_ws,
+                                       SampleChemicalFormula='H2-O',
+                                       CanWorkspace=self._can_ws,
+                                       CanChemicalFormula='V',
+                                       Interpolate=False)
+
+        corrections_ws = mtd[self._corrections_ws_name]
+
+        # Check each correction workspace has X binning matching NumberWavelengths
+        for workspace in corrections_ws:
+            self.assertEqual(workspace.blocksize(), 10)
+
+
+    def test_validationNoCanFormula(self):
+        """
+        Tests validation for no chemical formula for can when a can WS is provided.
+        """
+
+        self.assertRaises(RuntimeError,
+                          CylinderPaalmanPingsCorrection,
+                          OutputWorkspace=self._corrections_ws_name,
+                          SampleWorkspace=self._sample_ws,
+                          SampleChemicalFormula='H2-O',
+                          SampleInnerRadius=0.05,
+                          SampleOuterRadius=0.1,
+                          CanWorkspace=self._can_ws,
+                          CanOuterRadius=0.15,
+                          BeamHeight=0.1,
+                          BeamWidth=0.1,
+                          Emode='Indirect',
+                          Efixed=1.845)
+
+
+if __name__=="__main__":
+    unittest.main()
-- 
GitLab