PageRenderTime 3ms CodeModel.GetById 26ms app.highlight 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Testing/SystemTests/tests/framework/ISISDirectReductionComponents.py

https://github.com/mantidproject/mantid
Python | 320 lines | 202 code | 62 blank | 56 comment | 11 complexity | e0bae17d65ce3391b293360df3c324bc MD5 | raw file
  1# Mantid Repository : https://github.com/mantidproject/mantid
  2#
  3# Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
  4#   NScD Oak Ridge National Laboratory, European Spallation Source,
  5#   Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
  6# SPDX - License - Identifier: GPL - 3.0 +
  7#pylint: disable=invalid-name
  8import os
  9import sys
 10import systemtesting
 11from mantid.simpleapi import *
 12from mantid.api import Workspace,IEventWorkspace
 13
 14from Direct.PropertyManager import PropertyManager
 15import ISIS_MariReduction as mr
 16try:
 17    from importlib import reload
 18except ImportError:
 19    pass
 20
 21#----------------------------------------------------------------------
 22
 23
 24class ISIS_ReductionWebLike(systemtesting.MantidSystemTest):
 25    def __init__(self):
 26        systemtesting.MantidSystemTest.__init__(self)
 27
 28       # prepare reduction variable
 29        self.rd = mr.ReduceMARIFromFile()
 30        self.rd.def_main_properties()
 31        self.rd.def_advanced_properties()
 32
 33        save_folder = config['defaultsave.directory']
 34
 35        self.rd.save_web_variables(os.path.join(save_folder,'reduce_vars.py'))
 36
 37    def runTest(self):
 38        # run reduction using saved variables like web variables
 39        web_var_folder = config['defaultsave.directory']
 40        sys.path.insert(0,web_var_folder)
 41        reload(mr)
 42
 43        # change these variables to save result as nxs workspace
 44        mr.web_var.advanced_vars['save_format']='nxs'
 45        # web services currently needs input file to be defined
 46        input_file = 'MAR11001.RAW'
 47        mr.main(input_file,web_var_folder)
 48
 49        #  verify if result was indeed written
 50        self.rd.reducer.sample_run = input_file
 51        saveFileName = self.rd.reducer.save_file_name
 52        oputputFile = os.path.join(web_var_folder,saveFileName+'.nxs')
 53
 54        self.assertTrue(os.path.exists(oputputFile))
 55
 56        web_var_file = os.path.join(web_var_folder,'reduce_vars')
 57        if os.path.exists(web_var_file+'.py'):
 58            os.remove(web_var_file+'.py')
 59        if os.path.exists(web_var_file+'.pyc'):
 60            os.remove(web_var_file+'.pyc')
 61
 62    def get_result_workspace(self):
 63        """Returns the result workspace to be checked"""
 64        if 'outWS' in mtd:
 65            return 'outWS'
 66        saveFileName = self.rd.reducer.save_file_name
 67        Load(Filename=saveFileName+'.nxs', OutputWorkspace="outWS")
 68        #outWS *= 0.997979227566217
 69        fullRezPath =FileFinder.getFullPath(saveFileName+'.nxs')
 70        os.remove(fullRezPath)
 71        return 'outWS'
 72
 73    def get_reference_file(self):
 74        return "MARIReduction.nxs"
 75
 76    def validate(self):
 77        """Returns the name of the workspace & file to compare"""
 78        # tolerance defined outside of init
 79#pylint: disable=W0201
 80        self.tolerance = 1e-6
 81        # tolerance_is_rel_err defined outside of init
 82#pylint: disable=W0201
 83        self.tolerance_is_rel_err = True
 84        self.disableChecking.append('SpectraMap')
 85        self.disableChecking.append('Instrument')
 86        self.disableChecking.append('Sample')
 87        result = self.get_result_workspace()
 88        reference = self.get_reference_file()
 89        return result, reference
 90
 91
 92class ISIS_ReductionWrapperValidate(systemtesting.MantidSystemTest):
 93    def __init__(self):
 94        systemtesting.MantidSystemTest.__init__(self)
 95        self.result = False
 96
 97    def runTest(self):
 98       # prepare reduction variable
 99        # At the moment MARI reduction differs from it original by
100        # less then 1% due to changes in the procedure. At the moment
101        # we have to account for this but when we make it the same,
102        # the code below should be commented. Meanwhile it tests workspace
103        # workflow
104        #------------------------------------------------------
105        #ref_file = 'MARIReduction.nxs'
106        #file = FileFinder.getFullPath(ref_file)
107        #etalon_ws = Load(file)
108        #etalon_ws/=0.997979227566217
109        #------------------------------------------------------
110        rd = mr.ReduceMARIFromFile()
111        rd.def_main_properties()
112        rd.def_advanced_properties()
113        # this is correct workflow for the ref file
114        #rd.reducer.prop_man.save_file_name = ref_file
115        # temporary workflow, until we fix workspace adjustment
116        # disable pylint -- access to protected member
117#pylint: disable=W0212
118        rd._tolerr =1.e-6
119        rd.reducer.prop_man.save_file_name = 'MARIReduction.nxs'
120        rd.validate_run_number=11001
121        try:
122            rez,mess = rd.run_reduction()
123            self.result=rez
124            if not rez:
125                print("*** Validation failed: {0}".format(mess))
126            if mess.find('Created')>-1: # validation still failed due to missing validation file
127                print("*** Validation failed: {0}".format(mess))
128                self.result=False
129        except RuntimeError as err:
130            print("*** Validation failed with error: {0}".format(err.message))
131            self.result=False
132        rd.reducer.prop_man.save_file_name = None
133
134    def validate(self):
135        """Returns the name of the workspace & file to compare"""
136        return self.result
137
138
139#----------------------------------------------------------------------
140class ISISLoadFilesRAW(systemtesting.MantidSystemTest):
141
142    def __init__(self):
143        systemtesting.MantidSystemTest.__init__(self)
144        self.valid = False
145
146    def runTest(self):
147        propman = PropertyManager('MAR')
148
149        propman.sample_run = 11001
150        propman.load_monitors_with_workspace = True
151
152        mon_ws = PropertyManager.sample_run.get_monitors_ws()
153        ws = PropertyManager.sample_run.get_workspace()
154
155        self.assertTrue(isinstance(ws,Workspace))
156        self.assertEqual(ws.getNumberHistograms(),922)
157
158        DeleteWorkspace(ws)
159
160        propman.load_monitors_with_workspace = False
161        propman.sample_run = 11001
162        ws = PropertyManager.sample_run.get_workspace()
163        mon_ws = PropertyManager.sample_run.get_monitors_ws()
164
165        self.assertEqual(ws.getNumberHistograms(),919)
166        self.assertEqual(mon_ws.getNumberHistograms(),3)
167
168        #
169        propman = PropertyManager('MAPS')
170        propman.sample_run = 17186
171        propman.load_monitors_with_workspace = False
172
173        mon_ws = PropertyManager.sample_run.get_monitors_ws()
174        ws = PropertyManager.sample_run.get_workspace()
175        self.assertTrue(isinstance(ws,Workspace))
176        self.assertEqual(ws.getNumberHistograms(),41472)
177        self.assertEqual(mon_ws.getNumberHistograms(),4)
178
179        #
180        self.valid = True
181
182    def validate(self):
183        return self.valid
184
185
186class ISISLoadFilesMER(systemtesting.MantidSystemTest):
187
188    def __init__(self):
189        systemtesting.MantidSystemTest.__init__(self)
190        self.valid = False
191
192    def runTest(self):
193        #
194        propman = PropertyManager('MER')
195        propman.sample_run = 6398 # (raw file)
196        propman.det_cal_file = 6399
197        propman.load_monitors_with_workspace = False
198        propman.mon1_norm_spec =  69633
199        propman.ei_mon1_spec = [ 69634,69635,69636,69637]
200        propman.ei_mon2_spec = [ 69638,69639,69640,69641]
201
202        mon_ws = PropertyManager.sample_run.get_monitors_ws()
203        self.assertNotEqual(mon_ws, None)
204
205        ws = PropertyManager.sample_run.get_workspace()
206        self.assertTrue(isinstance(ws,Workspace))
207        self.assertEqual(ws.getNumberHistograms(),69632)
208        self.assertEqual(mon_ws.getNumberHistograms(),9)
209
210        propman.sample_run = None # (clean things up)
211        propman.load_monitors_with_workspace = True
212        propman.sample_run  = 6398
213
214        mon_ws = PropertyManager.sample_run.get_monitors_ws()
215        self.assertNotEqual(mon_ws, None)
216        ws = PropertyManager.sample_run.get_workspace()
217        self.assertTrue(isinstance(ws,Workspace))
218        self.assertEqual(ws.getNumberHistograms(),69641)
219        self.assertEqual(mon_ws.getNumberHistograms(),69641)
220
221        propman.sample_run = 18492 # (histogram nxs file )
222        propman.det_cal_file = None
223        mon_ws = PropertyManager.sample_run.get_monitors_ws()
224        self.assertTrue('SR_MER018492' in mtd)
225        self.assertNotEqual(mon_ws, None)
226        ws = PropertyManager.sample_run.get_workspace()
227        self.assertTrue(isinstance(ws,Workspace))
228        self.assertEqual(ws.getNumberHistograms(),69641)
229        self.assertEqual(mon_ws.getNumberHistograms(),69641)
230        self.assertEqual(mon_ws.getIndexFromSpectrumNumber(69638),69637)
231        det = mon_ws.getDetector(69632)
232        self.assertTrue(det.isMonitor())
233        det = mon_ws.getDetector(69631)
234        self.assertFalse(det.isMonitor())
235
236        #  enable when bug #10980 is fixed
237        propman.sample_run = None # delete all
238        self.assertFalse('SR_MER018492' in mtd)
239        propman.sample_run = 18492 # (histogram nxs file )
240        propman.load_monitors_with_workspace = False
241        propman.det_cal_file = None
242        mon_ws = PropertyManager.sample_run.get_monitors_ws()
243        self.assertNotEqual(mon_ws, None)
244        self.assertTrue('SR_MER018492_monitors' in mtd)
245
246        ws = PropertyManager.sample_run.get_workspace()
247        self.assertTrue(isinstance(ws,Workspace))
248        self.assertEqual(ws.getNumberHistograms(),69632)
249        self.assertEqual(mon_ws.getNumberHistograms(),9)
250        self.assertEqual(mon_ws.getIndexFromSpectrumNumber(69633),0)
251        det = mon_ws.getDetector(0)
252        self.assertTrue(det.isMonitor())
253
254        ei_ws = GetAllEi(mon_ws,69634,69638,IgnoreSecondMonitor=False)
255        self.assertTrue(isinstance(ei_ws,Workspace))
256
257        en_peaks = ei_ws.readX(0)
258        self.assertAlmostEqual(len(en_peaks),1)
259        self.assertAlmostEqual(en_peaks[0],108.94,2)
260
261        self.valid = True
262
263    def validate(self):
264        return self.valid
265
266
267class ISISLoadFilesLET(systemtesting.MantidSystemTest):
268
269    def __init__(self):
270        systemtesting.MantidSystemTest.__init__(self)
271        self.valid = False
272
273    def runTest(self):
274
275        #
276        propman = PropertyManager('LET')
277
278        propman.sample_run = 6278 #event nexus file
279        propman.load_monitors_with_workspace = False
280
281        # Here we have known problem of propman loading new IDF, and
282        # workspace is written using old IDF. New IDF has mon1_norm_spec =73729
283        # and ei_mon1_spec=73734   (on January 2015) and old
284        # IDF -- mon1_norm_spec =40961 and 40966 (forever)
285        # Normalized by monitor-1. -- need monitor1 and ei needs ei_mon1_spec
286        # This problem is hopefully fixed in reduction now, but here
287        # we have to specify these values manually to guard against
288        # changes in a future. This issue should be now fixed through varions means
289        propman.normalise_method='monitor-1'
290        # Adjust old IDF to new changes. This may change when loader is modified.
291        propman.ei_mon2_spec  = [5506,5505,5507]
292        propman.spectra_to_monitors_list=[5506,5505,5507]
293        #
294        ws = PropertyManager.sample_run.get_workspace()
295        # apply IDF property, correspondent to this particular time interval
296        propman.update_defaults_from_instrument(ws.getInstrument())
297        self.assertEqual(int(propman.mon1_norm_spec),40961)
298        self.assertEqual(propman.ei_mon1_spec,40966)
299
300        mon_ws = PropertyManager.sample_run.get_monitors_ws()
301        self.assertNotEqual(mon_ws, None)
302
303        self.assertTrue(isinstance(ws,IEventWorkspace))
304        self.assertEqual(ws.getNumberHistograms(),40960)
305        self.assertTrue(isinstance(mon_ws,Workspace))
306        #
307        self.assertEqual(mon_ws.getNumberHistograms(),11)
308
309        ei_ws = GetAllEi(mon_ws,40966,40967,IgnoreSecondMonitor=True)
310        self.assertTrue(isinstance(ei_ws,Workspace))
311
312        self.valid = True
313
314    def validate(self):
315        return self.valid
316
317
318if __name__=="__main__":
319    tester = ISISLoadFilesMER()
320    tester.runTest()