Skip to content

Commit

Permalink
Re #10684 MonoCorrectionFactor property with cash option and unit test
Browse files Browse the repository at this point in the history
for this property
  • Loading branch information
abuts committed Feb 9, 2015
1 parent dacfc0a commit 19a1e49
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 20 deletions.
16 changes: 13 additions & 3 deletions Code/Mantid/scripts/Inelastic/Direct/DirectEnergyConversion.py
Expand Up @@ -394,8 +394,17 @@ def convert_to_energy(self,wb_run=None,sample_run=None,ei_guess=None,rebin=None,
#--------------------------------------------------------------------------------------------------
# SNS or GUI motor stuff
self.calculate_rotation(PropertyManager.sample_run.get_workspace())
#
calculate_abs_units = (self.monovan_run != None and self.mono_correction_factor == None)
#
if self.monovan_run:
MonovanCashNum=PropertyManager.monovan_run.run_number()
if self.mono_correction_factor:
calculate_abs_units = False # correction factor given, so no calculations
else:
calculate_abs_units = True
else:
MonovanCashNum=None
calculate_abs_units = False


if PropertyManager.incident_energy.multirep_mode():
self._multirep_mode = True
Expand Down Expand Up @@ -440,7 +449,8 @@ def convert_to_energy(self,wb_run=None,sample_run=None,ei_guess=None,rebin=None,
PropertyManager.sample_run.synchronize_ws(deltaE_wkspace_sample)
#
ei = (deltaE_wkspace_sample.getRun().getLogData("Ei").value)
PropertyManager.incident_energy.set_current(ei)
# PropertyManager.incident_energy.set_current(ei) # let's not do it -- this makes subsequent calls to this method depend on
# # previous calls

prop_man.log("*** Incident energy found for sample run: {0} meV".format(ei),'notice')
#
Expand Down
62 changes: 48 additions & 14 deletions Code/Mantid/scripts/Inelastic/Direct/PropertiesDescriptors.py
Expand Up @@ -274,9 +274,7 @@ def multirep_mode(self):
def get_current(self):
""" Return current energy out of range of energies"""
if isinstance(self._incident_energy,list):
ind = self._cur_iter_en-1
if ind<0:
ind = 0
ind = self._cur_iter_en
return self._incident_energy[ind]
else:
return self._incident_energy
Expand All @@ -286,24 +284,22 @@ def set_current(self,value):
"""
if isinstance(self._incident_energy,list):
ind = self._cur_iter_en-1
if ind<0:
ind = 0
self._incident_energy[ind] = value
ind = self._cur_iter_en
self._incident_energy[ind]=value
else:
self._incident_energy = value


def __iter__(self):
""" iterator over energy range, initializing iterations over energies """
self._cur_iter_en = 0
self._cur_iter_en = -1
return self

def next(self): # Python 3: def __next__(self)
""" part of iterator """
if self._cur_iter_en < self._num_energies:
ind = self._cur_iter_en
self._cur_iter_en += 1
self._cur_iter_en += 1
ind = self._cur_iter_en
if ind < self._num_energies:
if isinstance(self._incident_energy,list):
return self._incident_energy[ind]
else:
Expand Down Expand Up @@ -718,7 +714,7 @@ def __set__(self,instance,value):
#-----------------------------------------------------------------------------------------
class MonovanIntegrationRange(prop_helpers.ComplexProperty):
""" integration range for monochromatic vanadium. The final integral is used to estimate
relative detector's efficiency
relative detector's efficiency
Defined either directly or as the function of the incident energy(s)
Expand Down Expand Up @@ -986,10 +982,24 @@ def __set__(self,instance,value):
#end MultirepTOFSpectraList

class MonoCorrectionFactor(PropDescriptor):
def __init__(self,ei_prop,monovan_run):
""" property contains correction factor, used to convert
experimental scattering cross-section into absolute
units ( mb/str/mev/fu)
There are independent two sources for this factor:
1) if user explicitly specifies correction value.
This value then will be applied to all subsequent runs
without any checks if the correction is appropriate
2) set/get cashed value correspondent to current monovan
run number, incident energy and integration range.
This value is cashed at first run and reapplied if
no changes to the values it depends on were identified
"""
def __init__(self,ei_prop):
self._cor_factor = None
self._mono_run_number=None
self._ei_prop = ei_prop
self._mono_run = monovan_run
self.cashed_values={}

def __get__(self,instance,type=None):
if instance is None:
Expand All @@ -1000,8 +1010,32 @@ def __get__(self,instance,type=None):
def __set__(self,instance,value):
if value is None:
self._cor_factor = None
self.cashed_values={}
return
self._cor_factor = value
#
def set_val_to_cash(self,mono_int_range,value):
""" """
cash_id = self._build_cash_val_id(mono_int_range)
self.cashed_values[cash_id] = value

def get_val_from_cash(self,mono_int_range):
cash_id = self._build_cash_val_id(mono_int_range)
if cash_id in self.cashed_values:
return self.cashed_values[cash_id]
else:
return None

def set_cash_mono_run_number(self,new_value):
if self._mono_run_number != int(new_value):
self.cashed_values={}
self._mono_run_number = int(new_value)

def _build_cash_val_id(self,mono_int_range):
ei = self._ei_prop.get_current()
cash_id = "Ei={0:0>9.4e}:Int({1:0>9.4e}:{2:0>9.5e}):Run{3}".\
format(ei,mono_int_range[0],mono_int_range[1],self._mono_run_number)
return cash_id


#-----------------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion Code/Mantid/scripts/Inelastic/Direct/PropertyManager.py
Expand Up @@ -229,7 +229,7 @@ def __getattr__(self,name):
#
multirep_tof_specta_list=MultirepTOFSpectraList()
#
mono_correction_factor = MonoCorrectionFactor(NonIDF_Properties.incident_energy,NonIDF_Properties.monovan_run)
mono_correction_factor = MonoCorrectionFactor(NonIDF_Properties.incident_energy)

#----------------------------------------------------------------------------------------------------------------
def getChangedProperties(self):
Expand Down
3 changes: 1 addition & 2 deletions Code/Mantid/scripts/test/DirectEnergyConversionTest.py
@@ -1,5 +1,5 @@
import os, sys
#os.environ["PATH"] = r"c:/Mantid/Code/builds/br_master/bin/Release;"+os.environ["PATH"]
os.environ["PATH"] = r"c:/Mantid/Code/builds/br_master/bin/Release;"+os.environ["PATH"]
from mantid.simpleapi import *
from mantid import api
import unittest
Expand Down Expand Up @@ -433,7 +433,6 @@ def test_multirep_abs_units_mode(self):
self.assertAlmostEqual(x[0],-2*122.)
self.assertAlmostEqual(x[-1],0.8*122.)

return
# test another ws
# rename samples from previous workspace to avoid deleting them on current run
for ind,item in enumerate(result):
Expand Down
21 changes: 21 additions & 0 deletions Code/Mantid/scripts/test/DirectPropertyManagerTest.py
Expand Up @@ -875,7 +875,28 @@ def test_multirep_tof_specta_list(self):
self.assertTrue(len(sp)==4)
self.assertEqual(sp[3],15)

def test_mono_correction_factor(self):
propman = self.prop_man
propman.incident_energy=[10,20]

PropertyManager.mono_correction_factor.set_cash_mono_run_number(11015)


self.assertTrue(propman.mono_correction_factor is None)
propman.mono_correction_factor = 66.
self.assertAlmostEqual(propman.mono_correction_factor,66)

self.assertTrue(PropertyManager.mono_correction_factor.get_val_from_cash([10.,20.]) is None)
PropertyManager.mono_correction_factor.set_val_to_cash([10.,20.],100)
self.assertAlmostEqual(PropertyManager.mono_correction_factor.get_val_from_cash([10.,20.]),100)

PropertyManager.incident_energy.next()
self.assertTrue(PropertyManager.mono_correction_factor.get_val_from_cash([10.,20.]) is None)
PropertyManager.mono_correction_factor.set_val_to_cash([10.,20.],50)
self.assertAlmostEqual(PropertyManager.mono_correction_factor.get_val_from_cash([10.,20.]),50)

PropertyManager.mono_correction_factor.set_cash_mono_run_number(11060)
self.assertTrue(PropertyManager.mono_correction_factor.get_val_from_cash([10.,20.]) is None)

if __name__=="__main__":
unittest.main()
Expand Down

0 comments on commit 19a1e49

Please sign in to comment.