Skip to content

Commit

Permalink
Merge pull request mantidproject#20551 from mantidproject/20508_flake…
Browse files Browse the repository at this point in the history
…8_issues_inelastic_direct

Flake8 issues inelastic direct
  • Loading branch information
quantumsteve authored Oct 1, 2017
2 parents 7f09fd6 + 006f2a2 commit 4e0cfec
Show file tree
Hide file tree
Showing 5 changed files with 224 additions and 331 deletions.
139 changes: 78 additions & 61 deletions scripts/Inelastic/Direct/DirectEnergyConversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,7 @@ def diagnose(self, white,diag_sample=None,**kwargs):
"""
# output workspace name.
try:
#pylint: disable=unused-variable
n,r = funcinspect.lhs_info('both')
_,r = funcinspect.lhs_info('both')
out_ws_name = r[0]
#pylint: disable=bare-except
except:
Expand All @@ -206,9 +205,6 @@ def diagnose(self, white,diag_sample=None,**kwargs):
# something else
white = self.get_run_descriptor(white)

# return all diagnostics parameters
diag_params = self.prop_man.get_diagnostics_parameters()

if self.use_hard_mask_only:
# build hard mask
diag_mask = white.get_masking(1)
Expand Down Expand Up @@ -238,17 +234,21 @@ def diagnose(self, white,diag_sample=None,**kwargs):
return None

# Get the white beam vanadium integrals
whiteintegrals = self.do_white(white, None, None) # No grouping yet
white_integrals = self.do_white(white, None, None) # No grouping yet
#pylint: disable=access-member-before-definition
if self.second_white:
#TODO: fix THIS DOES NOT WORK!
#pylint: disable=unused-variable
# second_white = self.second_white
other_whiteintegrals = self.do_white(PropertyManager.second_white, None, None) # No grouping yet
other_white_integrals = self.do_white(PropertyManager.second_white, None, None) # No grouping yet
#pylint: disable=attribute-defined-outside-init
self.second_white = other_whiteintegrals
self.second_white = other_white_integrals

# return all diagnostics parameters
diag_params = self.prop_man.get_diagnostics_parameters()

# Get the background/total counts from the sample run if present
name_to_clean = None
if diag_sample is not None:
diag_sample = self.get_run_descriptor(diag_sample)
sample_mask = diag_sample.get_masking(1)
Expand All @@ -266,12 +266,11 @@ def diagnose(self, white,diag_sample=None,**kwargs):
if (PropertyManager.incident_energy.multirep_mode() and self.normalise_method == 'monitor-2')\
or self.bleed_test: # bleed test below needs no normalization so normalize cloned workspace
result_ws = diag_sample.get_ws_clone('sample_ws_clone')
wb_normalization_method = whiteintegrals.getRun().getLogData('DirectInelasticReductionNormalisedBy').value
wb_normalization_method = white_integrals.getRun().getLogData('DirectInelasticReductionNormalisedBy').value
result_ws = self.normalise(result_ws, wb_normalization_method)
name_to_clean = result_ws.name()
else:
result_ws = self.normalise(diag_sample, self.normalise_method)
name_to_clean = None

#>>>here result workspace is being processed
#-- not touching result ws
Expand All @@ -285,12 +284,10 @@ def diagnose(self, white,diag_sample=None,**kwargs):
self.prop_man.log("Diagnose: finished convertUnits ",'information')

background_int *= self.scale_factor
diagnostics.normalise_background(background_int, whiteintegrals,
diagnostics.normalise_background(background_int, white_integrals,
diag_params.get('second_white',None))
diag_params['background_int'] = background_int
diag_params['sample_counts'] = total_counts
else: # diag_sample is None
name_to_clean = None

# extract existing white mask if one is defined and provide it for
# diagnose to use instead of constantly diagnosing the same vanadium
Expand All @@ -311,30 +308,38 @@ def diagnose(self, white,diag_sample=None,**kwargs):
# keep white mask workspace for further usage
if diag_spectra_blocks is None:
# Do the whole lot at once
white_masked_ws = diagnostics.diagnose(whiteintegrals, **diag_params)
white_masked_ws = diagnostics.diagnose(white_integrals, **diag_params)
if white_masked_ws:
white.add_masked_ws(white_masked_ws)
DeleteWorkspace(white_masked_ws)
else:
for bank in diag_spectra_blocks:
diag_params['start_index'] = bank[0] - 1
diag_params['end_index'] = bank[1] - 1
white_masked_ws = diagnostics.diagnose(whiteintegrals, **diag_params)
white_masked_ws = diagnostics.diagnose(white_integrals, **diag_params)
if white_masked_ws:
white.add_masked_ws(white_masked_ws)
DeleteWorkspace(white_masked_ws)

if out_ws_name:
if diag_sample is not None:
diag_sample.add_masked_ws(whiteintegrals)
diag_sample.add_masked_ws(white_integrals)
mask = diag_sample.get_masking(1)
diag_mask = CloneWorkspace(mask,OutputWorkspace=out_ws_name)
else: # either WB was diagnosed or WB masks were applied to it
# Extract the mask workspace
diag_mask, _ = ExtractMask(InputWorkspace=whiteintegrals,OutputWorkspace=out_ws_name)
diag_mask, _ = ExtractMask(InputWorkspace=white_integrals,OutputWorkspace=out_ws_name)
else:
diag_mask = None
# Clean up

self.clean_up(diag_params, name_to_clean, white_integrals)

return diag_mask

#-------------------------------------------------------------------------------
# Clean up unrequired workspaces

def clean_up(self, diag_params, name_to_clean, white_integrals):
if 'sample_counts' in diag_params:
DeleteWorkspace(Workspace='background_int')
DeleteWorkspace(Workspace='total_counts')
Expand All @@ -344,9 +349,8 @@ def diagnose(self, white,diag_sample=None,**kwargs):
DeleteWorkspace(name_to_clean)
if name_to_clean+'_monitors' in mtd:
DeleteWorkspace(name_to_clean+'_monitors')
DeleteWorkspace(Workspace=whiteintegrals)
DeleteWorkspace(Workspace=white_integrals)

return diag_mask
#-------------------------------------------------------------------------------
#pylint: disable=too-many-arguments
#pylint: disable=too-many-branches
Expand Down Expand Up @@ -390,43 +394,25 @@ def convert_to_energy(self,wb_run=None,sample_run=None,ei_guess=None,rebin=None,

PropertyManager.sample_run.set_action_suffix('')
sample_ws = PropertyManager.sample_run.get_workspace()
# Check auto-ei mode and calculate incident energies if necessary
if PropertyManager.incident_energy.autoEi_mode():
mon_ws = PropertyManager.sample_run.get_monitors_ws()
# sum monitor spectra if this is requested
ei_mon_spec = self.ei_mon_spectra
if PropertyManager.ei_mon_spectra.need_to_sum_monitors(prop_man):
ei_mon_spec,mon_ws = self.sum_monitors_spectra(mon_ws,ei_mon_spec)
sample_ws.setMonitorWorkspace(mon_ws)
else:
pass

try:
PropertyManager.incident_energy.set_auto_Ei(mon_ws,prop_man,ei_mon_spec)
EiToProcessAvailible = True
except RuntimeError as er:
prop_man.log('*** Error while calculating autoEi: {0}. See algorithm log for details.'.
format(str(er)))
EiToProcessAvailible = False
else:
EiToProcessAvailible = True
ei_to_process_available = self.calc_incident_energies(PropertyManager, prop_man, sample_ws)

# Update reduction properties which may change in the workspace but have
# not been modified from input parameters.
# E.g. detector number have changed
oldChanges = self.prop_man.getChangedProperties()
allChanges = self.prop_man.update_defaults_from_instrument(sample_ws.getInstrument())
workspace_defined_prop = allChanges.difference(oldChanges)
old_changes = self.prop_man.getChangedProperties()
all_changes = self.prop_man.update_defaults_from_instrument(sample_ws.getInstrument())
workspace_defined_prop = all_changes.difference(old_changes)
if len(workspace_defined_prop) > 0:
prop_man.log("****************************************************************")
prop_man.log('*** Sample run {0} properties change default reduction properties: '.
format(PropertyManager.sample_run.get_workspace().name()))
prop_man.log_changed_values('notice',False,oldChanges)
prop_man.log_changed_values('notice',False,old_changes)
prop_man.log("****************************************************************")
# inform user on what parameters have changed from script or gui
# if monovan present, check if abs_norm_ parameters are set
self.prop_man.log_changed_values('notice')
if not EiToProcessAvailible:
if not ei_to_process_available:
prop_man.log("*** NO GUESS INCIDENT ENERGIES IDENTIFIED FOR THIS RUN *********")
prop_man.log("*** NOTHING TO REDUCE ******************************************")
prop_man.log("****************************************************************")
Expand All @@ -453,12 +439,12 @@ def convert_to_energy(self,wb_run=None,sample_run=None,ei_guess=None,rebin=None,
masking = self.spectra_masks

# estimate and report the number of failing detectors
nMaskedSpectra = get_failed_spectra_list_from_masks(masking,prop_man)
n_masked_spectra = get_failed_spectra_list_from_masks(masking,prop_man)
if masking:
nSpectra = masking.getNumberHistograms()
n_spectra = masking.getNumberHistograms()
else:
nSpectra = 0
prop_man.log(header.format(nSpectra,nMaskedSpectra),'notice')
n_spectra = 0
prop_man.log(header.format(n_spectra,n_masked_spectra),'notice')
#--------------------------------------------------------------------------------------------------
# now reduction
#--------------------------------------------------------------------------------------------------
Expand All @@ -475,12 +461,12 @@ def convert_to_energy(self,wb_run=None,sample_run=None,ei_guess=None,rebin=None,
#end
#
if self.monovan_run is not None:
MonovanCashNum = PropertyManager.monovan_run.run_number()
mono_van_cache_num = PropertyManager.monovan_run.run_number()
else:
MonovanCashNum = None
mono_van_cache_num = None
#Set or clear monovan run number to use in cash ID to return correct
#cashed value of monovan integral
PropertyManager.mono_correction_factor.set_cash_mono_run_number(MonovanCashNum)
PropertyManager.mono_correction_factor.set_cash_mono_run_number(mono_van_cache_num)

mono_ws_base = None
if PropertyManager.incident_energy.multirep_mode():
Expand Down Expand Up @@ -541,7 +527,7 @@ def convert_to_energy(self,wb_run=None,sample_run=None,ei_guess=None,rebin=None,
# calculate absolute units integral and apply it to the workspace
# or use previously cashed value
cashed_mono_int = PropertyManager.mono_correction_factor.get_val_from_cash(prop_man)
if MonovanCashNum is not None or self.mono_correction_factor or cashed_mono_int:
if mono_van_cache_num is not None or self.mono_correction_factor or cashed_mono_int:
deltaE_ws_sample,mono_ws_base = self._do_abs_corrections(deltaE_ws_sample,cashed_mono_int,
ei_guess,mono_ws_base,tof_range, cut_ind,num_ei_cuts)
else:
Expand Down Expand Up @@ -572,23 +558,54 @@ def convert_to_energy(self,wb_run=None,sample_run=None,ei_guess=None,rebin=None,
# END Main loop over incident energies
#------------------------------------------------------------------------------------------

self.clean_up_convert_to_energy(start_time)
return result


#------------------------------------------------------------------------------------------
# Handles cleanup of the convert_to_energy method

def clean_up_convert_to_energy(self, start_time):

#Must! clear background ws (if present in multirep) to calculate background
#source for next workspace
if 'bkgr_ws_source' in mtd:
DeleteWorkspace('bkgr_ws_source')

# CLEAR existing workspaces only if it is not run within loop

#prop_man.monovan_run = None
#prop_man.wb_run = None
# clear combined mask
self.spectra_masks = None
end_time = time.time()
prop_man.log("*** ISIS CONVERT TO ENERGY TRANSFER WORKFLOW FINISHED *********")
prop_man.log("*** Elapsed time : {0:>9.2f} sec *********".
format(end_time - start_time),'notice')
prop_man.log("****************************************************************")
return result
self.prop_man.log("*** ISIS CONVERT TO ENERGY TRANSFER WORKFLOW FINISHED *********")
self.prop_man.log("*** Elapsed time : {0:>9.2f} sec *********".
format(end_time - start_time),'notice')
self.prop_man.log("****************************************************************")

#------------------------------------------------------------------------------------------
# Check auto-ei mode and calculate incident energies if necessary
# Returns if there is a processible Ei

def calc_incident_energies(self, PropertyManager, prop_man, sample_ws):
if PropertyManager.incident_energy.autoEi_mode():
mon_ws = PropertyManager.sample_run.get_monitors_ws()
# sum monitor spectra if this is requested
ei_mon_spec = self.ei_mon_spectra
if PropertyManager.ei_mon_spectra.need_to_sum_monitors(prop_man):
ei_mon_spec,mon_ws = self.sum_monitors_spectra(mon_ws,ei_mon_spec)
sample_ws.setMonitorWorkspace(mon_ws)
else:
pass

try:
PropertyManager.incident_energy.set_auto_Ei(mon_ws,prop_man,ei_mon_spec)
return True
except RuntimeError as er:
prop_man.log('*** Error while calculating autoEi: {0}. See algorithm log for details.'.
format(str(er)))
return False
else:
return True

#------------------------------------------------------------------------------------------
#pylint: disable=too-many-arguments

def _do_abs_corrections(self,deltaE_ws_sample,cashed_mono_int,ei_guess,
Expand Down
54 changes: 32 additions & 22 deletions scripts/Inelastic/Direct/ISISDirecInelasticConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,29 +243,11 @@ def userID(self, val):
# pylint: disable=R0912
def check_input(self, instrument, start_date, cycle, rb_folder_or_id):
"""Verify that input is correct"""
if instrument not in INELASTIC_INSTRUMENTS:
raise RuntimeError("Instrument {0} has to be one of "
"ISIS inelastic instruments".format(instrument))
if isinstance(start_date, str):
# the date of express -- let's make it long in the past
if start_date.lower() == 'none':
start_date = '19800101'
error = False
else:
start_date = start_date.replace('-', '')
if len(start_date) != 8:
start_date = '20' + start_date
if len(start_date) == 8:
error = False
else:
error = True
else:
error = True
if error:
raise RuntimeError("Experiment start date should be defined as"
" a sting in the form YYYYMMDD or YYMMDD but it is: {0}".format(start_date))
# Checks if instrument is inelastic and raises RuntimeError if not
self.validate_instrument(instrument)

#
# Checks if the date is valid and raises a RuntimeError if not
start_date = self.validate_date(start_date)

def convert_cycle_int(cycle_int):
if cycle_int > 999: # Full cycle format 20151
Expand Down Expand Up @@ -312,6 +294,34 @@ def convert_cycle_int(cycle_int):

return instrument, start_date, cycle, rb_folder_or_id, rb_exist

#-----------------------------------------------------------------------------------------------

def validate_instrument(self, instrument):
if instrument not in INELASTIC_INSTRUMENTS:
raise RuntimeError("Instrument {0} has to be one of "
"ISIS inelastic instruments".format(instrument))

def validate_date(self, start_date):
if isinstance(start_date, str):
# the date of express -- let's make it long in the past
if start_date.lower() == 'none':
start_date = '19800101'
error = False
else:
start_date = start_date.replace('-', '')
if len(start_date) != 8:
start_date = '20' + start_date
if len(start_date) == 8:
error = False
else:
error = True
else:
error = True
if error:
raise RuntimeError("Experiment start date should be defined as"
" a sting in the form YYYYMMDD or YYMMDD but it is: {0}".format(start_date))
return start_date

def get_all_instruments(self):
""" Return list of all instruments, user is working on during this cycle"""
return list(self._instrument.values())
Expand Down
Loading

0 comments on commit 4e0cfec

Please sign in to comment.