From b030a92c00c874ffb851a4cba94f02f4c75963fe Mon Sep 17 00:00:00 2001 From: Dan Nixon Date: Tue, 17 Feb 2015 11:00:04 +0000 Subject: [PATCH] Refactor more Python algorithms Refs #10706 --- .../algorithms/CalculateSampleTransmission.py | 28 +++--- .../algorithms/CreateEmptyTableWorkspace.py | 6 +- .../plugins/algorithms/LoadVesuvio.py | 90 +++++++++++-------- .../plugins/algorithms/SortDetectors.py | 31 +++---- .../plugins/algorithms/VesuvioResolution.py | 5 +- .../WorkflowAlgorithms/IndirectResolution.py | 39 +++++--- 6 files changed, 117 insertions(+), 82 deletions(-) diff --git a/Code/Mantid/Framework/PythonInterface/plugins/algorithms/CalculateSampleTransmission.py b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/CalculateSampleTransmission.py index c88d3bf4f803..4f759e7a7b0e 100644 --- a/Code/Mantid/Framework/PythonInterface/plugins/algorithms/CalculateSampleTransmission.py +++ b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/CalculateSampleTransmission.py @@ -12,14 +12,17 @@ def category(self): def summary(self): - return 'Calculates the scattering & transmission for a given sample material and size over a given wavelength range.' + return "Calculates the scattering & transmission for a given sample \ + material and size over a given wavelength range." def PyInit(self): - self.declareProperty(name='WavelengthRange', defaultValue='', validator=StringMandatoryValidator(), + self.declareProperty(name='WavelengthRange', defaultValue='', + validator=StringMandatoryValidator(), doc='Wavelength range to calculate transmission for.') - self.declareProperty(name='ChemicalFormula', defaultValue='', validator=StringMandatoryValidator(), + self.declareProperty(name='ChemicalFormula', defaultValue='', + validator=StringMandatoryValidator(), doc='Sample chemical formula') self.declareProperty(name='NumberDensity', defaultValue=0.1, @@ -29,18 +32,19 @@ def PyInit(self): doc='Sample thickness (cm). Default=0.1') self.declareProperty(MatrixWorkspaceProperty('OutputWorkspace', '', Direction.Output), - doc='Outputs the sample transmission over the wavelength range as a function of wavelength.') + doc='Outputs the sample transmission over the wavelength range ' + 'as a function of wavelength.') def validateInputs(self): issues = dict() density = self.getProperty('NumberDensity').value - if(density < 0.0): + if density < 0.0: issues['NumberDensity'] = 'NumberDensity must be positive' thickness = self.getProperty('Thickness').value - if(thickness < 0.0): + if thickness < 0.0: issues['Thickness'] = 'Thickness must be positive' return issues @@ -51,12 +55,13 @@ def PyExec(self): # Create the workspace and set the sample material CreateWorkspace(OutputWorkspace=self._output_ws, NSpec=2, DataX=[0, 0], DataY=[0, 0]) - Rebin(InputWorkspace=self._output_ws, OutputWorkspace=self._output_ws, Params=self._bin_params) + Rebin(InputWorkspace=self._output_ws, OutputWorkspace=self._output_ws, + Params=self._bin_params) SetSampleMaterial(InputWorkspace=self._output_ws, ChemicalFormula=self._chamical_formula) ConvertToPointData(InputWorkspace=self._output_ws, OutputWorkspace=self._output_ws) - ws = mtd[self._output_ws] - wavelengths = ws.readX(0) + workspace = mtd[self._output_ws] + wavelengths = workspace.readX(0) transmission_data = np.zeros(len(wavelengths)) scattering_data = np.zeros(len(wavelengths)) @@ -66,8 +71,8 @@ def PyExec(self): transmission_data[idx] = transmission scattering_data[idx] = scattering - ws.setY(0, transmission_data) - ws.setY(1, scattering_data) + workspace.setY(0, transmission_data) + workspace.setY(1, scattering_data) self.setProperty('OutputWorkspace', self._output_ws) @@ -103,6 +108,5 @@ def _calculate_at_wavelength(self, wavelength): return transmission, scattering - # Register algorithm with Mantid AlgorithmFactory.subscribe(CalculateSampleTransmission) diff --git a/Code/Mantid/Framework/PythonInterface/plugins/algorithms/CreateEmptyTableWorkspace.py b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/CreateEmptyTableWorkspace.py index 162f572d05da..7de26b454077 100644 --- a/Code/Mantid/Framework/PythonInterface/plugins/algorithms/CreateEmptyTableWorkspace.py +++ b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/CreateEmptyTableWorkspace.py @@ -5,11 +5,13 @@ class CreateEmptyTableWorkspace(PythonAlgorithm): def summary(self): - return "Creates an empty TableWorkspace which can be populated with various types of information." + return "Creates an empty TableWorkspace which can be populated with various "\ + "types of information." def PyInit(self): # Declare properties - self.declareProperty(ITableWorkspaceProperty("OutputWorkspace", "", Direction.Output), "The name of the table workspace that will be created.") + self.declareProperty(ITableWorkspaceProperty("OutputWorkspace", "", Direction.Output), + "The name of the table workspace that will be created.") def PyExec(self): tableWS = WorkspaceFactory.createTable() diff --git a/Code/Mantid/Framework/PythonInterface/plugins/algorithms/LoadVesuvio.py b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/LoadVesuvio.py index 0d23197cc062..0e5d0de4fbe9 100644 --- a/Code/Mantid/Framework/PythonInterface/plugins/algorithms/LoadVesuvio.py +++ b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/LoadVesuvio.py @@ -34,7 +34,7 @@ class LoadVesuvio(PythonAlgorithm): def summary(self): - return "Loads raw data produced by the Vesuvio instrument at ISIS." + return "Loads raw data produced by the Vesuvio instrument at ISIS." def PyInit(self): self.declareProperty(RUN_PROP, "", StringMandatoryValidator(), @@ -53,12 +53,13 @@ def PyInit(self): self.declareProperty(FileProperty(INST_PAR_PROP,"",action=FileAction.OptionalLoad, extensions=["dat"]), doc="An optional IP file. If provided the values are used to correct " - "the default instrument values and attach the t0 values to each detector") + "the default instrument values and attach the t0 values to each " + "detector") self.declareProperty(SUM_PROP, False, - doc="If true then the final output is a single spectrum containing the sum " - "of all of the requested spectra. All detector angles/parameters are " - "averaged over the individual inputs") + doc="If true then the final output is a single spectrum containing " + "the sum of all of the requested spectra. All detector angles/" + "parameters are averaged over the individual inputs") self.declareProperty(WorkspaceProperty(WKSP_PROP, "", Direction.Output), doc="The name of the output workspace.") @@ -114,13 +115,13 @@ def _exec_single_foil_state_mode(self): """ runs = self._get_runs() if len(runs) > 1: - raise RuntimeError("Single soil state mode does not currently support summing multiple files") + raise RuntimeError("Single soil state mode does not currently support summing " + "multiple files") isis = config.getFacility("ISIS") inst_prefix = isis.instrument("VESUVIO").shortName() try: - run = int(runs[0]) run_str = inst_prefix + runs[0] except ValueError: run_str = runs[0] @@ -138,7 +139,7 @@ def _exec_single_foil_state_mode(self): foil_map = SpectraToFoilPeriodMap(self._nperiods) for ws_index, spectrum_no in enumerate(all_spectra): self._set_spectra_type(spectrum_no) - foil_out_periods, foil_thin_periods, foil_thick_periods = self._get_foil_periods() + foil_out_periods, foil_thin_periods, _ = self._get_foil_periods() if self._diff_opt == "FoilOut": raw_grp_indices = foil_map.get_indices(spectrum_no, foil_out_periods) @@ -252,13 +253,15 @@ def _setup_raw(self, spectra): # Cache delta_t values raw_t = first_ws.readX(0) delay = raw_t[2] - raw_t[1] - raw_t = raw_t - delay # The original EVS loader, raw.for/rawb.for, does this. Done here to match results + # The original EVS loader, raw.for/rawb.for, does this. Done here to match results + raw_t = raw_t - delay self.pt_times = raw_t[1:] self.delta_t = (raw_t[1:] - raw_t[:-1]) mon_raw_t = self._raw_monitors[0].readX(0) delay = mon_raw_t[2] - mon_raw_t[1] - mon_raw_t = mon_raw_t - delay # The original EVS loader, raw.for/rawb.for, does this. Done here to match results + # The original EVS loader, raw.for/rawb.for, does this. Done here to match results + mon_raw_t = mon_raw_t - delay self.mon_pt_times = mon_raw_t[1:] self.delta_tmon = (mon_raw_t[1:] - mon_raw_t[:-1]) @@ -310,7 +313,8 @@ def _get_runs(self): # Load is not doing the right thing when summing. The numbers don't look correct if "-" in run_str: lower,upper = run_str.split("-") - runs = range(int(lower),int(upper)+1) #range goes lower to up-1 but we want to include the last number + # Range goes lower to up-1 but we want to include the last number + runs = range(int(lower),int(upper)+1) elif "," in run_str: runs = run_str.split(",") @@ -322,9 +326,9 @@ def _get_runs(self): def _set_spectra_type(self, spectrum_no): """ - Set whether this spectrum no is forward/backward scattering - and set the normalization range appropriately - @param spectrum_no The current spectrum no + Set whether this spectrum no is forward/backward scattering + and set the normalization range appropriately + @param spectrum_no The current spectrum no """ if spectrum_no >= self._backward_spectra_list[0] and spectrum_no <= self._backward_spectra_list[-1]: self._spectra_type=BACKWARD @@ -385,7 +389,8 @@ def _create_foil_workspaces(self): nhists = first_ws.getNumberHistograms() data_kwargs = {'NVectors':nhists,'XLength':ndata_bins,'YLength':ndata_bins} - self.foil_out = WorkspaceFactory.create(first_ws, **data_kwargs) # This will be used as the result workspace + # This will be used as the result workspace + self.foil_out = WorkspaceFactory.create(first_ws, **data_kwargs) self.foil_out.setDistribution(True) self.foil_thin = WorkspaceFactory.create(first_ws, **data_kwargs) @@ -417,20 +422,21 @@ def _sum_foil_periods(self): foil_out_periods, foil_thin_periods, foil_thick_periods = self._get_foil_periods() if self._nperiods == 6 and self._spectra_type == FORWARD: - mon_out_periods = (5,6) - mon_thin_periods = (3,4) - mon_thick_periods = foil_thick_periods + mon_out_periods = (5,6) + mon_thin_periods = (3,4) + mon_thick_periods = foil_thick_periods else: # None indicates same as standard foil mon_out_periods, mon_thin_periods, mon_thick_periods = (None,None,None) -# + # Foil out self._sum_foils(self.foil_out, self.mon_out, IOUT, foil_out_periods, mon_out_periods) # Thin foil self._sum_foils(self.foil_thin, self.mon_thin, ITHIN, foil_thin_periods, mon_thin_periods) # Thick foil if foil_thick_periods is not None: - self._sum_foils(self.foil_thick, self.mon_thick, ITHICK, foil_thick_periods, mon_thick_periods) + self._sum_foils(self.foil_thick, self.mon_thick, ITHICK, + foil_thick_periods, mon_thick_periods) #---------------------------------------------------------------------------------------- def _get_foil_periods(self): @@ -467,13 +473,14 @@ def _get_foil_periods(self): #---------------------------------------------------------------------------------------- def _sum_foils(self, foil_ws, mon_ws, sum_index, foil_periods, mon_periods=None): """ - Sums the counts from the given foil periods in the raw data group - @param foil_ws :: The workspace that will receive the summed counts - @param mon_ws :: The monitor workspace that will receive the summed monitor counts - @param sum_index :: An index into the sum3 array where the integrated counts will be accumulated - @param foil_periods :: The period numbers that contribute to this sum - @param mon_periods :: The period numbers of the monitors that contribute to this monitor sum - (if None then uses the foil_periods) + Sums the counts from the given foil periods in the raw data group + @param foil_ws :: The workspace that will receive the summed counts + @param mon_ws :: The monitor workspace that will receive the summed monitor counts + @param sum_index :: An index into the sum3 array where the integrated counts will be + accumulated + @param foil_periods :: The period numbers that contribute to this sum + @param mon_periods :: The period numbers of the monitors that contribute to this monitor sum + (if None then uses the foil_periods) """ raw_grp_indices = self.foil_map.get_indices(self._spectrum_no, foil_periods) wsindex = self._ws_index @@ -511,7 +518,9 @@ def _normalise_by_monitor(self): wsindex = self._ws_index # inner function to apply normalization def monitor_normalization(foil_ws, mon_ws): - """Applies monitor normalization to the given foil spectrum from the given monitor spectrum + """ + Applies monitor normalization to the given foil spectrum from the given + monitor spectrum. """ mon_values = mon_ws.readY(wsindex) mon_values_sum = np.sum(mon_values[indices_in_range]) @@ -542,7 +551,8 @@ def normalise_to_out(foil_ws, foil_type): values = foil_ws.dataY(wsindex) sum_values = np.sum(values[range_indices]) if sum_values == 0.0: - self.getLogger().warning("No counts in %s foil spectrum %d." % (foil_type,self._spectrum_no)) + self.getLogger().warning("No counts in %s foil spectrum %d." % ( + foil_type,self._spectrum_no)) sum_values = 1.0 norm_factor = (sum_out/sum_values) values *= norm_factor @@ -611,7 +621,8 @@ def _calculate_double_difference(self, ws_index): eout = self.foil_out.dataE(ws_index) ethin = self.foil_thin.readE(ws_index) ethick = self.foil_thick.readE(ws_index) - np.sqrt((one_min_beta*eout)**2 + ethin**2 + (self._beta**2)*ethick**2, eout) # The second argument makes it happen in place + # The second argument makes it happen in place + np.sqrt((one_min_beta*eout)**2 + ethin**2 + (self._beta**2)*ethick**2, eout) #---------------------------------------------------------------------------------------- def _calculate_thick_difference(self, ws_index): @@ -693,7 +704,8 @@ def _get_header_format(self, ip_filename): try: return IP_HEADERS[len(columns)] except KeyError: - raise ValueError("Unknown format for IP file. Currently support 5/6 column variants. ncols=%d" % (len(columns))) + raise ValueError("Unknown format for IP file. Currently support 5/6 column " + "variants. ncols=%d" % (len(columns))) #---------------------------------------------------------------------------------------- def _store_results(self): @@ -794,11 +806,13 @@ def get_foil_periods(self, spectrum_no, state): return foil_periods def get_indices(self, spectrum_no, foil_state_numbers): - """Returns a tuple of indices that can be used to access the Workspace within + """ + Returns a tuple of indices that can be used to access the Workspace within a WorkspaceGroup that corresponds to the foil state numbers given - @param spectrum_no :: A spectrum number (1->nspectra) - @param foil_state_no :: A number between 1 & 9(inclusive) that defines which foil state is required - @returns A tuple of indices in a WorkspaceGroup that gives the associated Workspace + @param spectrum_no :: A spectrum number (1->nspectra) + @param foil_state_no :: A number between 1 & 9(inclusive) that defines which foil + state is required + @returns A tuple of indices in a WorkspaceGroup that gives the associated Workspace """ indices = [] for state in foil_state_numbers: @@ -834,11 +848,13 @@ def get_index(self, spectrum_no, foil_state_no): def _validate_foil_number(self, foil_number): if foil_number < 1 or foil_number > 9: - raise ValueError("Invalid foil state given, expected a number between 1 and 9. number=%d" % foil_number) + raise ValueError("Invalid foil state given, expected a number between " + "1 and 9. number=%d" % foil_number) def _validate_spectrum_number(self, spectrum_no): if spectrum_no < 1 or spectrum_no > 198: - raise ValueError("Invalid spectrum given, expected a number between 3 and 198. spectrum=%d" % spectrum_no) + raise ValueError("Invalid spectrum given, expected a number between 3 " + "and 198. spectrum=%d" % spectrum_no) ######################################################################################### diff --git a/Code/Mantid/Framework/PythonInterface/plugins/algorithms/SortDetectors.py b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/SortDetectors.py index 700ec5b13b40..08652064d2c2 100644 --- a/Code/Mantid/Framework/PythonInterface/plugins/algorithms/SortDetectors.py +++ b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/SortDetectors.py @@ -1,9 +1,8 @@ -from mantid.api import PythonAlgorithm, AlgorithmFactory,WorkspaceProperty,PropertyMode -from mantid.kernel import Direction,IntArrayProperty, FloatArrayProperty +from mantid.api import PythonAlgorithm, AlgorithmFactory +from mantid.kernel import Direction, IntArrayProperty, FloatArrayProperty import mantid,math,numpy - class SortDetectors(PythonAlgorithm): """ Sort detectors by distance """ @@ -25,21 +24,23 @@ def summary(self): def PyInit(self): """ Declare properties """ - self.declareProperty(mantid.api.WorkspaceProperty("Workspace","",direction=mantid.kernel.Direction.Input, validator=mantid.api.InstrumentValidator()), "Input workspace") + self.declareProperty(mantid.api.WorkspaceProperty("Workspace", "", + direction=mantid.kernel.Direction.Input, + validator=mantid.api.InstrumentValidator()), + "Input workspace") - self.declareProperty(IntArrayProperty("UpstreamSpectra",Direction.Output)) - self.declareProperty(FloatArrayProperty("UpstreamDetectorDistances",Direction.Output)) - self.declareProperty(IntArrayProperty("DownstreamSpectra",Direction.Output)) - self.declareProperty(FloatArrayProperty("DownstreamDetectorDistances",Direction.Output)) - return + self.declareProperty(IntArrayProperty("UpstreamSpectra", Direction.Output)) + self.declareProperty(FloatArrayProperty("UpstreamDetectorDistances", Direction.Output)) + self.declareProperty(IntArrayProperty("DownstreamSpectra", Direction.Output)) + self.declareProperty(FloatArrayProperty("DownstreamDetectorDistances", Direction.Output)) def PyExec(self): """ Main execution body """ - w = self.getProperty("Workspace").value - samplePos=w.getInstrument().getSample().getPos() - moderatorPos=w.getInstrument().getSource().getPos() - incident=samplePos-moderatorPos + workspace = self.getProperty("Workspace").value + samplePos = workspace.getInstrument().getSample().getPos() + moderatorPos = workspace.getInstrument().getSource().getPos() + incident = samplePos - moderatorPos upstream=[] upinds=[] @@ -47,8 +48,8 @@ def PyExec(self): downstream=[] downinds=[] downdist=[] - for i in range(w.getNumberHistograms()): - detPos=w.getDetector(i).getPos() + for i in range(workspace.getNumberHistograms()): + detPos=workspace.getDetector(i).getPos() scattered=detPos-samplePos if abs(scattered.angle(incident))>0.999*math.pi: upstream.append((i,scattered.norm())) diff --git a/Code/Mantid/Framework/PythonInterface/plugins/algorithms/VesuvioResolution.py b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/VesuvioResolution.py index dd677756799f..cd8608d1a65e 100644 --- a/Code/Mantid/Framework/PythonInterface/plugins/algorithms/VesuvioResolution.py +++ b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/VesuvioResolution.py @@ -111,8 +111,9 @@ def _calculate_resolution(self, workspace, output_ws_name): fit.initialize() fit.setChild(True) mantid.simpleapi._set_properties(fit, function, InputWorkspace=workspace, MaxIterations=0, - CreateOutput=True, Output=fit_naming_stem, WorkspaceIndex=self._spectrum_index, - OutputCompositeMembers=True) + CreateOutput=True, Output=fit_naming_stem, + WorkspaceIndex=self._spectrum_index, + OutputCompositeMembers=True) fit.execute() fit_ws = fit.getProperty('OutputWorkspace').value diff --git a/Code/Mantid/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/IndirectResolution.py b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/IndirectResolution.py index 3fe3e42c49c3..4e8c1750c553 100644 --- a/Code/Mantid/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/IndirectResolution.py +++ b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/IndirectResolution.py @@ -1,7 +1,7 @@ from mantid.simpleapi import * from mantid.api import * from mantid.kernel import * -from mantid import config, logger +from mantid import logger class IndirectResolution(DataProcessorAlgorithm): @@ -36,17 +36,20 @@ def PyInit(self): self.declareProperty(FloatArrayProperty(name='BackgroundRange', values=[0.0, 0.0]), doc='Energy range to use as background') - self.declareProperty(name='RebinParam', defaultValue='', doc='Rebinning parameters (min,width,max)') - self.declareProperty(name='ScaleFactor', defaultValue=1.0, doc='Factor to scale resolution curve by') - self.declareProperty(name='Smooth', defaultValue=False, doc='Apply WienerSmooth to resolution') + self.declareProperty(name='RebinParam', defaultValue='', + doc='Rebinning parameters (min,width,max)') + self.declareProperty(name='ScaleFactor', defaultValue=1.0, + doc='Factor to scale resolution curve by') + self.declareProperty(name='Smooth', defaultValue=False, + doc='Apply WienerSmooth to resolution') self.declareProperty(name='Plot', defaultValue=False, doc='Plot resolution curve') - self.declareProperty(name='Save', defaultValue=False, doc='Save resolution workspace as a Nexus file') + self.declareProperty(name='Save', defaultValue=False, + doc='Save resolution workspace as a Nexus file') def PyExec(self): from IndirectCommon import StartTime, EndTime, getWSprefix - import inelastic_indirect_reducer StartTime('IndirectResolution') self._setup() @@ -113,20 +116,28 @@ def _post_process(self): """ use_scale_factor = self._scale_factor == 1.0 - AddSampleLog(Workspace=self._out_ws, LogName='scale', LogType='String', LogText=str(use_scale_factor)) + AddSampleLog(Workspace=self._out_ws, LogName='scale', + LogType='String', LogText=str(use_scale_factor)) if use_scale_factor: - AddSampleLog(Workspace=self._out_ws, LogName='scale_factor', LogType='Number', LogText=str(self._scale_factor)) + AddSampleLog(Workspace=self._out_ws, LogName='scale_factor', + LogType='Number', LogText=str(self._scale_factor)) - AddSampleLog(Workspace=self._out_ws, LogName='res_smoothing_applied', LogType='String', LogText=str(self._smooth)) + AddSampleLog(Workspace=self._out_ws, LogName='res_smoothing_applied', + LogType='String', LogText=str(self._smooth)) - AddSampleLog(Workspace=self._out_ws, LogName='back_start', LogType='Number', LogText=str(self._background[0])) - AddSampleLog(Workspace=self._out_ws, LogName='back_end', LogType='Number', LogText=str(self._background[1])) + AddSampleLog(Workspace=self._out_ws, LogName='back_start', + LogType='Number', LogText=str(self._background[0])) + AddSampleLog(Workspace=self._out_ws, LogName='back_end', + LogType='Number', LogText=str(self._background[1])) rebin_params = self._rebin_string.split(',') if len(rebin_params) == 3: - AddSampleLog(Workspace=self._out_ws, LogName='rebin_low', LogType='Number', LogText=rebin_params[0]) - AddSampleLog(Workspace=self._out_ws, LogName='rebin_width', LogType='Number', LogText=rebin_params[1]) - AddSampleLog(Workspace=self._out_ws, LogName='rebin_high', LogType='Number', LogText=rebin_params[2]) + AddSampleLog(Workspace=self._out_ws, LogName='rebin_low', + LogType='Number', LogText=rebin_params[0]) + AddSampleLog(Workspace=self._out_ws, LogName='rebin_width', + LogType='Number', LogText=rebin_params[1]) + AddSampleLog(Workspace=self._out_ws, LogName='rebin_high', + LogType='Number', LogText=rebin_params[2]) self.setProperty('OutputWorkspace', self._out_ws)