def split_into_tof_d_spacing_groups(run_details, processed_spectra): """ Splits a processed list containing all focused banks into TOF and d-spacing groups. It also sets the names of the output workspaces to the run number(s) - Result<Unit>-<Bank Number> e.g. 123-130-ResultTOF-3 :param run_details: The run details associated with this run :param processed_spectra: A list containing workspaces, one entry per focused bank. :return: A workspace group for dSpacing and TOF in that order """ d_spacing_output = [] tof_output = [] run_number = str(run_details.output_run_string) ext = run_details.file_extension if run_details.file_extension else "" for name_index, ws in enumerate(processed_spectra, start=1): d_spacing_out_name = run_number + ext + "-ResultD_" + str(name_index) tof_out_name = run_number + ext + "-ResultTOF_" + str(name_index) d_spacing_output.append(mantid.ConvertUnits(InputWorkspace=ws, OutputWorkspace=d_spacing_out_name, Target="dSpacing")) tof_output.append(mantid.ConvertUnits(InputWorkspace=ws, OutputWorkspace=tof_out_name, Target="TOF")) # Group the outputs d_spacing_group_name = run_number + ext + "-ResultD" d_spacing_group = mantid.GroupWorkspaces(InputWorkspaces=d_spacing_output, OutputWorkspace=d_spacing_group_name) tof_group_name = run_number + ext + "-ResultTOF" tof_group = mantid.GroupWorkspaces(InputWorkspaces=tof_output, OutputWorkspace=tof_group_name) return d_spacing_group, tof_group
def runTest(self): _make_test_directories() main(vanadium_run="236516", user="******", focus_run=None, force_cal=True, directory=focus_directory, full_inst_calib_path=WHOLE_INST_CALIB) simple.mtd.clear() csv_file = os.path.join(root_directory, "EnginX.csv") location = os.path.join(focus_directory, "User", "test", "Calibration") shutil.copy2(csv_file, location) csv_file = os.path.join(location, "EnginX.csv") main(vanadium_run="236516", user="******", focus_run="299080", force_cal=True, directory=focus_directory, grouping_file=csv_file, full_inst_calib_path=WHOLE_INST_CALIB) output = "engg_focusing_output_ws_texture_bank_{}{}" group = "" for i in range(1, 11): group = group + output.format(i, ",") simple.GroupWorkspaces(InputWorkspaces=group, OutputWorkspace="test")
def test_DNSTwoTheta_Groups(self): outputWorkspaceName = "DNSMergeRunsTest_Test3" group = api.GroupWorkspaces(self.workspaces) alg_test = run_algorithm("DNSMergeRuns", WorkspaceNames='group', OutputWorkspace=outputWorkspaceName, HorizontalAxis='2theta') self.assertTrue(alg_test.isExecuted()) # check whether the data are correct ws = AnalysisDataService.retrieve(outputWorkspaceName) # dimensions self.assertEqual(96, ws.blocksize()) self.assertEqual(2, ws.getNumDims()) self.assertEqual(1, ws.getNumberHistograms()) # data array # read the merged values dataX = ws.extractX()[0] for i in range(len(self.angles)): self.assertAlmostEqual(self.angles[i], dataX[i]) # check that the intensity has not been changed dataY = ws.extractY()[0] for i in range(len(dataY)): self.assertAlmostEqual(1.0, dataY[i]) run_algorithm("DeleteWorkspace", Workspace=outputWorkspaceName) return
def test_valid_save(self): path_to_ipf = self._find_file_or_die(self.IPF_FILE) temp_file_path = tempfile.mkdtemp() self._folders_to_remove.add(temp_file_path) wsgroup = [] for i in range(2): wsgroup.append( mantid.CreateSampleWorkspace(OutputWorkspace=str(i), NumBanks=1, BankPixelWidth=1, XMin=-0.5, XMax=0.5, BinWidth=0.01, XUnit='DSpacing', StoreInADS=True)) gem_output_test_ws_group = mantid.GroupWorkspaces(wsgroup) gem_output.save_gda(d_spacing_group=gem_output_test_ws_group, gsas_calib_filename=path_to_ipf, grouping_scheme=self.GROUPING_SCHEME, output_path=temp_file_path + '\\test.gda', raise_warning=False) self._find_file_or_die(temp_file_path + '\\test.gda') with open(temp_file_path + '\\test.gda') as read_file: first_line = read_file.readline() self.assertEquals(first_line, self.CHECK_AGAINST)
def _pre_process_corrections(self): """ If the sample is not in wavelength then convert the corrections to whatever units the sample is in. """ unit_id = s_api.mtd[self._sample_ws_wavelength].getAxis( 0).getUnit().unitID() logger.information('x-unit is ' + unit_id) factor_types = ['ass'] if self._use_can: factor_types.extend(['acc', 'acsc', 'assc']) for factor_type in factor_types: input_name = self._get_correction_factor_ws_name(factor_type) output_name = self._corrections + '_' + factor_type s_api.CloneWorkspace(InputWorkspace=input_name, OutputWorkspace=output_name) # Group the temporary factor workspaces (for easy removal later) s_api.GroupWorkspaces(InputWorkspaces=[ self._corrections + '_' + f_type for f_type in factor_types ], OutputWorkspace=self._corrections)
def merge_workspaces(run, workspaces): """ where workspaces is a tuple of form: (filepath, ws name) """ d_string = "{}; Detector {}" # detectors is a dictionary of {detector_name : [names_of_workspaces]} detectors = {d_string.format(run, x): [] for x in range(1, 5)} # fill dictionary for workspace in workspaces: detector_number = get_detector_num_from_ws(workspace) detectors[d_string.format(run, detector_number)].append(workspace) # initialise a group workspace tmp = mantid.CreateSampleWorkspace() overall_ws = mantid.GroupWorkspaces(tmp, OutputWorkspace=str(run)) # merge each workspace list in detectors into a single workspace for detector, workspace_list in iteritems(detectors): if workspace_list: # sort workspace list according to type_index sorted_workspace_list = [None] * num_files_per_detector # sort workspace list according to type_index for workspace in workspace_list: data_type = workspace.rsplit("_")[1] sorted_workspace_list[spectrum_index[data_type] - 1] = workspace workspace_list = sorted_workspace_list # create merged workspace merged_ws = create_merged_workspace(workspace_list) # add merged ws to ADS mantid.mtd.add(detector, merged_ws) mantid.ConvertToHistogram(InputWorkspace=detector, OutputWorkspace=detector) overall_ws.add(detector) mantid.AnalysisDataService.remove("tmp") # return list of [run; Detector detectorNumber], in ascending order of detector number detector_list = sorted(list(detectors)) return detector_list
def group_workspaces(self, exp_number, group_name): """ :return: """ # Find out the input workspace name ws_names_str = '' for key in self._myRawDataWSDict.keys(): if key[0] == exp_number: ws_names_str += '%s,' % self._myRawDataWSDict[key].name() for key in self._mySpiceTableDict.keys(): if key[0] == exp_number: ws_names_str += '%s,' % self._mySpiceTableDict[key].name() # Check if len(ws_names_str) == 0: return False, 'No workspace is found for experiment %d.' % exp_number # Remove last ',' ws_names_str = ws_names_str[:-1] # Group api.GroupWorkspaces(InputWorkspaces=ws_names_str, OutputWorkspace=group_name) return
def _define_corrections(self): """ Defines all the corrections that are required """ self._correction_workspaces = list() if self._container_ws != "": container_name = str(self._correction_wsg) + "_Container" self._container_ws = ms.ExtractSingleSpectrum( InputWorkspace=self._container_ws, OutputWorkspace=container_name, WorkspaceIndex=self._spec_idx) self._correction_workspaces.append(self._container_ws.name()) # Do gamma correction if self.getProperty( "GammaBackground").value and not self._back_scattering: self._correction_workspaces.append(self._gamma_correction()) # Do multiple scattering correction if self.getProperty("MultipleScattering").value: self._correction_workspaces.extend(self._ms_correction()) # Output correction workspaces as a WorkspaceGroup if self._correction_wsg != "": ms.GroupWorkspaces(InputWorkspaces=self._correction_workspaces, OutputWorkspace=self._correction_wsg) self.setProperty("CorrectionWorkspaces", self._correction_wsg)
def generate_ts_pdf(run_number, focus_file_path, merge_banks=False, q_lims=None, cal_file_name=None, sample_details=None, delta_r=None, delta_q=None, pdf_type="G(r)", lorch_filter=None, freq_params=None, debug=False): focused_ws = _obtain_focused_run(run_number, focus_file_path) focused_ws = mantid.ConvertUnits(InputWorkspace=focused_ws, Target="MomentumTransfer", EMode='Elastic') raw_ws = mantid.Load(Filename='POLARIS'+str(run_number)+'.nxs') sample_geometry = common.generate_sample_geometry(sample_details) sample_material = common.generate_sample_material(sample_details) self_scattering_correction = mantid.TotScatCalculateSelfScattering( InputWorkspace=raw_ws, CalFileName=cal_file_name, SampleGeometry=sample_geometry, SampleMaterial=sample_material, CrystalDensity=sample_details.material_object.crystal_density) ws_group_list = [] for i in range(self_scattering_correction.getNumberHistograms()): ws_name = 'correction_' + str(i) mantid.ExtractSpectra(InputWorkspace=self_scattering_correction, OutputWorkspace=ws_name, WorkspaceIndexList=[i]) ws_group_list.append(ws_name) self_scattering_correction = mantid.GroupWorkspaces(InputWorkspaces=ws_group_list) self_scattering_correction = mantid.RebinToWorkspace(WorkspaceToRebin=self_scattering_correction, WorkspaceToMatch=focused_ws) focused_ws = mantid.Subtract(LHSWorkspace=focused_ws, RHSWorkspace=self_scattering_correction) if delta_q: focused_ws = mantid.Rebin(InputWorkspace=focused_ws, Params=delta_q) if merge_banks: q_min, q_max = _load_qlims(q_lims) merged_ws = mantid.MatchAndMergeWorkspaces(InputWorkspaces=focused_ws, XMin=q_min, XMax=q_max, CalculateScale=False) fast_fourier_filter(merged_ws, freq_params=freq_params) pdf_output = mantid.PDFFourierTransform(Inputworkspace="merged_ws", InputSofQType="S(Q)-1", PDFType=pdf_type, Filter=lorch_filter, DeltaR=delta_r, rho0=sample_details.material_object.crystal_density) else: for ws in focused_ws: fast_fourier_filter(ws, freq_params=freq_params) pdf_output = mantid.PDFFourierTransform(Inputworkspace='focused_ws', InputSofQType="S(Q)-1", PDFType=pdf_type, Filter=lorch_filter, DeltaR=delta_r, rho0=sample_details.material_object.crystal_density) pdf_output = mantid.RebinToWorkspace(WorkspaceToRebin=pdf_output, WorkspaceToMatch=pdf_output[4], PreserveEvents=True) if not debug: common.remove_intermediate_workspace('self_scattering_correction') # Rename output ws if 'merged_ws' in locals(): mantid.RenameWorkspace(InputWorkspace='merged_ws', OutputWorkspace=run_number + '_merged_Q') mantid.RenameWorkspace(InputWorkspace='focused_ws', OutputWorkspace=run_number+'_focused_Q') if isinstance(focused_ws, WorkspaceGroup): for i in range(len(focused_ws)): mantid.RenameWorkspace(InputWorkspace=focused_ws[i], OutputWorkspace=run_number+'_focused_Q_'+str(i+1)) mantid.RenameWorkspace(InputWorkspace='pdf_output', OutputWorkspace=run_number+'_pdf_R') if isinstance(pdf_output, WorkspaceGroup): for i in range(len(pdf_output)): mantid.RenameWorkspace(InputWorkspace=pdf_output[i], OutputWorkspace=run_number+'_pdf_R_'+str(i+1)) return pdf_output
def PyExec(self): # Input vana_input = self.getProperty("VanadiumWorkspaces").value bg_input = self.getProperty("BackgroundWorkspaces").value vana_workspaces = self._expand_groups(vana_input) bg_workspaces = self._expand_groups(bg_input) self.log().notice("Input Vanadium workspaces: " + str(vana_workspaces)) self.log().notice("Input Background workspaces: " + str(bg_workspaces)) # number of vanadium and background workspaces must match if len(vana_workspaces) != len(bg_workspaces): raise RuntimeError("Number of Vanadium and background workspaces doe not match!") # compare optional sample logs, throw warnings result = api.CompareSampleLogs(vana_workspaces+bg_workspaces, self.properties_to_compare, 5e-3) if result: self.log().warning("Following properties do not match: " + result) # split input workspaces to groups SF/NSF and detector angles deterota = self._get_detector_positions(vana_workspaces) sfvana, nsfvana = self._sort_workspaces(vana_workspaces, deterota) sfbg, nsfbg = self._sort_workspaces(bg_workspaces, deterota) # subract background sfv = self._subtract_background(sfvana, sfbg, deterota) nsfv = self._subtract_background(nsfvana, nsfbg, deterota) total = self._sum_signal(sfv, nsfv, deterota) # compute vmean _mean_ws_ = api.Mean(",".join(list(total.values()))) # Mean takes string self.toremove.append(_mean_ws_.name()) num = self._get_notmasked_detectors_number(_mean_ws_) if num == 0: self.cleanup(self.toremove) raise RuntimeError("All detectors are masked! Cannot compute coefficients.") _vana_mean_ = api.SumSpectra(_mean_ws_)/num self.toremove.append(_vana_mean_.name()) # compute coefficients k_i = (VSF_i + VNSF_i)/Vmean outws_name = self.getPropertyValue("OutputWorkspace") # for only one detector position only one workspace will be created if len(deterota) == 1: api.Divide(list(total.values())[0], _vana_mean_, OutputWorkspace=outws_name) else: # for many detector positions group of workspaces will be created results = [] for angle in deterota: wsname = outws_name + '_2theta' + str(angle) api.Divide(total[angle], _vana_mean_, OutputWorkspace=wsname) results.append(wsname) api.GroupWorkspaces(results, OutputWorkspace=outws_name) self.cleanup(self.toremove) outws = api.AnalysisDataService.retrieve(outws_name) self.setProperty("OutputWorkspace", outws) return
def test_flatten_run_data(self): workspaces = [] for i in range(0, len(self.test_workspaces), 2): name = str(i) mantid.GroupWorkspaces(self.test_workspaces[i:i + 2], OutputWorkspace=name) workspaces.append(name) self.assertEquals(lutils.flatten_run_data(workspaces), [self.test_ws_names])
def makeWs(self): simpleapi.CreateWorkspace(OutputWorkspace='test1', DataX='1,2,3,4,5,1,2,3,4,5', DataY='1,2,3,4,2,3,4,5', DataE='1,2,3,4,2,3,4,5', NSpec='2', UnitX='dSpacing', Distribution='1', YUnitlabel="S(q)") simpleapi.CreateWorkspace(OutputWorkspace='test2', DataX='1,2,3,4,5,1,2,3,4,5', DataY='1,2,3,4,2,3,4,5', DataE='1,2,3,4,2,3,4,5', NSpec='2', UnitX='Momentum', VerticalAxisUnit='TOF', VerticalAxisValues='1,2', Distribution='1', YUnitLabel='E', WorkspaceTitle='x') simpleapi.GroupWorkspaces("test1,test2", OutputWorkspace="group") self.plotfile = os.path.join(config.getString('defaultsave.directory'), 'plot.png')
def split_to_single_bank(self, gss_ws_name): """ Split a multiple-bank GSAS workspace to a set of single-spectrum MatrixWorkspace Parameters ---------- gss_ws_name Returns ------- Name of grouped workspace and list """ # check assert isinstance(gss_ws_name, str) assert AnalysisDataService.doesExist(gss_ws_name) # get workspace gss_ws = AnalysisDataService.retrieve(gss_ws_name) ws_list = list() angle_list = list() if gss_ws.getNumberHistograms() == 1: # input is already a single-spectrum workspace ws_list.append(gss_ws_name) else: num_spec = gss_ws.getNumberHistograms() for i_ws in range(num_spec): # split this one to a single workspace out_ws_name = '%s_bank%d' % (gss_ws_name, i_ws + 1) # also can use ExtractSpectra() simpleapi.CropWorkspace(InputWorkspace=gss_ws_name, OutputWorkspace=out_ws_name, StartWorkspaceIndex=i_ws, EndWorkspaceIndex=i_ws) assert AnalysisDataService.doesExist(out_ws_name) ws_list.append(out_ws_name) # END-FOR # END-IF # calculate bank angles for ws_name in ws_list: bank_angle = calculate_bank_angle(ws_name) angle_list.append(bank_angle) # group all the workspace ws_group_name = gss_ws_name + '_group' simpleapi.GroupWorkspaces(InputWorkspaces=ws_list, OutputWorkspace=ws_group_name) self._braggDataDict[ws_group_name] = (gss_ws_name, ws_list) return ws_group_name, ws_list, angle_list
def addOutput(self, name): if name in self.inputs: mantid.AnalysisDataService.addOrReplace( self.inputs[name], self.alg.getProperty(name).value) else: return if mantid.AnalysisDataService.doesExist(self.run): group = mantid.AnalysisDataService.retrieve(self.run) else: mantid.GroupWorkspaces(OutputWorkspace=self.run) group.add(self.inputs[name])
def group_by_detector(run, workspaces): """ where workspaces is a tuple of form: (filepath, ws name) """ d_string = "{}; Detector {}" detectors = {d_string.format(run, x): [] for x in range(1, 5)} for workspace in workspaces: detector_number = get_detector_num_from_ws(workspace) detectors[d_string.format(run, detector_number)].append(workspace) for detector, workspace_list in iteritems(detectors): mantid.GroupWorkspaces(workspace_list, OutputWorkspace=str(detector)) detector_list = sorted(list(detectors)) group_grouped_workspaces(run, detector_list) return detector_list
def _create_vanadium_splines(focused_spectra, instrument, run_details): splined_ws_list = instrument._spline_vanadium_ws(focused_spectra) out_spline_van_file_path = run_details.splined_vanadium_file_path append = False for ws in splined_ws_list: mantid.SaveNexus(Filename=out_spline_van_file_path, InputWorkspace=ws, Append=append) append = True # Group for user convenience group_name = "Van_spline_data" tt_mode = instrument._get_current_tt_mode() if tt_mode: group_name = group_name + '_' + tt_mode mantid.GroupWorkspaces(InputWorkspaces=splined_ws_list, OutputWorkspace=group_name)
def save_unsplined_vanadium(vanadium_ws, output_path): converted_workspaces = [] for ws_index in range(vanadium_ws.getNumberOfEntries()): ws = vanadium_ws.getItem(ws_index) previous_units = ws.getAxis(0).getUnit().unitID() if previous_units != WORKSPACE_UNITS.tof: ws = mantid.ConvertUnits(InputWorkspace=ws, Target=WORKSPACE_UNITS.tof) ws = mantid.RenameWorkspace(InputWorkspace=ws, OutputWorkspace="van_bank_{}".format(ws_index + 1)) converted_workspaces.append(ws) converted_group = mantid.GroupWorkspaces(",".join(ws.name() for ws in converted_workspaces)) mantid.SaveNexus(InputWorkspace=converted_group, Filename=output_path, Append=False) mantid.DeleteWorkspace(converted_group)
def output(self): mantid.AnalysisDataService.addOrReplace( self.inputs["EvolChi"],self.alg.getProperty("EvolChi").value) mantid.AnalysisDataService.addOrReplace( self.inputs["EvolAngle"],self.alg.getProperty("EvolAngle").value) mantid.AnalysisDataService.addOrReplace( self.inputs["ReconstructedImage"],self.alg.getProperty("ReconstructedImage").value) mantid.AnalysisDataService.addOrReplace( self.inputs["ReconstructedData"],self.alg.getProperty("ReconstructedData").value) if mantid.AnalysisDataService.doesExist(self.run): group=mantid.AnalysisDataService.retrieve(self.run) else: mantid.GroupWorkspaces(OutputWorkspace=self.run) group.add(self.inputs["EvolChi"]) group.add(self.inputs["EvolAngle"]) group.add(self.inputs["ReconstructedImage"]) group.add(self.inputs["ReconstructedData"])
def _output_focused_ws(self, processed_spectra, run_details, output_mode=None): if not output_mode: output_mode = self._inst_settings.focus_mode attenuation_path = None if self._inst_settings.perform_atten: name_key = 'name' path_key = 'path' if isinstance(self._inst_settings.attenuation_files, str): self._inst_settings.attenuation_files = eval( self._inst_settings.attenuation_files) atten_file_found = False for atten_file in self._inst_settings.attenuation_files: if any(required_key not in atten_file for required_key in [name_key, path_key]): logger.warning( "A dictionary in attenuation_files has been ignored because " f"it doesn't contain both {name_key} and {path_key} entries" ) elif atten_file[ name_key] == self._inst_settings.attenuation_file: if atten_file_found: raise RuntimeError( f"Duplicate name {self._inst_settings.attenuation_file} found in attenuation_files" ) attenuation_path = atten_file[path_key] atten_file_found = True if attenuation_path is None: raise RuntimeError( f"Unknown attenuation_file {self._inst_settings.attenuation_file} specified for attenuation" ) output_spectra = \ pearl_output.generate_and_save_focus_output(self, processed_spectra=processed_spectra, run_details=run_details, focus_mode=output_mode, attenuation_filepath=attenuation_path) group_name = "PEARL{0!s}_{1}{2}-Results-D-Grp" mode = "_long" if self._inst_settings.long_mode else "" group_name = group_name.format(run_details.output_run_string, self._inst_settings.tt_mode, mode) grouped_d_spacing = mantid.GroupWorkspaces( InputWorkspaces=output_spectra, OutputWorkspace=group_name) return grouped_d_spacing, None
def _output_focused_ws(self, processed_spectra, run_details, output_mode=None): if not output_mode: output_mode = self._inst_settings.focus_mode if self._inst_settings.perform_atten: attenuation_path = self._inst_settings.attenuation_file_path else: attenuation_path = None output_spectra = \ pearl_output.generate_and_save_focus_output(self, processed_spectra=processed_spectra, run_details=run_details, focus_mode=output_mode, attenuation_filepath=attenuation_path) group_name = "PEARL" + str(run_details.output_run_string) group_name += '_' + self._inst_settings.tt_mode + "-Results-D-Grp" grouped_d_spacing = mantid.GroupWorkspaces(InputWorkspaces=output_spectra, OutputWorkspace=group_name) return grouped_d_spacing, None
def _calculate_partial_dos(self, ions, frequencies, eigenvectors, weights): """ Calculate the partial Density of States for all the ions of interest to the user @param frequencies :: frequency data from file @param eigenvectors :: eigenvector data from file @param weights :: weight data from file """ # Build a dictionary of ions that the user cares about # systemtests check order so use OrderedDict partial_ions = OrderedDict() calc_ion_index = self.getProperty('CalculateIonIndices').value if not calc_ion_index: for ion in self._ions_of_interest: partial_ions[ion] = [ i['index'] for i in ions if i['species'] == ion ] else: for ion in ions: if ion['species'] in self._ions_of_interest: ion_identifier = ion['species'] + str(ion['index']) partial_ions[ion_identifier] = ion['index'] partial_workspaces, sum_workspace = self._compute_partial_ion_workflow( partial_ions, frequencies, eigenvectors, weights) if self.getProperty('SumContributions').value: # Discard the partial workspaces for partial_ws in partial_workspaces: s_api.DeleteWorkspace(partial_ws) # Rename the summed workspace, this will be the output s_api.RenameWorkspace(InputWorkspace=sum_workspace, OutputWorkspace=self._out_ws_name) else: s_api.DeleteWorkspace(sum_workspace) partial_ws_names = [ws.name() for ws in partial_workspaces] # Sort workspaces if calc_ion_index: # Sort by index after '_' partial_ws_names.sort( key=lambda item: (int(item[(item.rfind('_') + 1):]))) group = ','.join(partial_ws_names) s_api.GroupWorkspaces(group, OutputWorkspace=self._out_ws_name)
def generate_ts_pdf(run_number, focus_file_path, merge_banks=False, q_lims=None, cal_file_name=None, sample_details=None, output_binning=None, pdf_type="G(r)", freq_params=None): focused_ws = _obtain_focused_run(run_number, focus_file_path) focused_ws = mantid.ConvertUnits(InputWorkspace=focused_ws, Target="MomentumTransfer", EMode='Elastic') raw_ws = mantid.Load(Filename='POLARIS'+str(run_number)+'.nxs') sample_geometry = common.generate_sample_geometry(sample_details) sample_material = common.generate_sample_material(sample_details) self_scattering_correction = mantid.TotScatCalculateSelfScattering(InputWorkspace=raw_ws, CalFileName=cal_file_name, SampleGeometry=sample_geometry, SampleMaterial=sample_material) ws_group_list = [] for i in range(self_scattering_correction.getNumberHistograms()): ws_name = 'correction_' + str(i) mantid.ExtractSpectra(InputWorkspace=self_scattering_correction, OutputWorkspace=ws_name, WorkspaceIndexList=[i]) ws_group_list.append(ws_name) self_scattering_correction = mantid.GroupWorkspaces(InputWorkspaces=ws_group_list) self_scattering_correction = mantid.RebinToWorkspace(WorkspaceToRebin=self_scattering_correction, WorkspaceToMatch=focused_ws) focused_ws = mantid.Subtract(LHSWorkspace=focused_ws, RHSWorkspace=self_scattering_correction) if merge_banks: q_min, q_max = _load_qlims(q_lims) merged_ws = mantid.MatchAndMergeWorkspaces(InputWorkspaces=focused_ws, XMin=q_min, XMax=q_max, CalculateScale=False) fast_fourier_filter(merged_ws, freq_params) pdf_output = mantid.PDFFourierTransform(Inputworkspace="merged_ws", InputSofQType="S(Q)-1", PDFType=pdf_type, Filter=True) else: for ws in focused_ws: fast_fourier_filter(ws, freq_params) pdf_output = mantid.PDFFourierTransform(Inputworkspace='focused_ws', InputSofQType="S(Q)-1", PDFType=pdf_type, Filter=True) pdf_output = mantid.RebinToWorkspace(WorkspaceToRebin=pdf_output, WorkspaceToMatch=pdf_output[4], PreserveEvents=True) common.remove_intermediate_workspace('self_scattering_correction') if output_binning is not None: try: pdf_output = mantid.Rebin(InputWorkspace=pdf_output, Params=output_binning) except RuntimeError: return pdf_output return pdf_output
def add_co_load_to_group(self, to_add, run): overall_ws = mantid.GroupWorkspaces(to_add[0], OutputWorkspace=str(run)) for index in range(1, len(to_add)): overall_ws.add(to_add[index])
def merge_pts_in_scan(self, exp_no, scan_no, target_ws_name, target_frame): """ Merge Pts in Scan All the workspaces generated as internal results will be grouped :param exp_no: :param scan_no: :param target_ws_name: :param target_frame: :return: (merged workspace name, workspace group name) """ # Check if exp_no is None: exp_no = self._expNumber assert isinstance(exp_no, int) assert isinstance(scan_no, int) assert isinstance(target_frame, str) assert isinstance(target_ws_name, str) ub_matrix_1d = None # Target frame if target_frame.lower().startswith('hkl'): target_frame = 'hkl' ub_matrix_1d = self._myUBMatrixDict[self._expNumber].reshape(9, ) elif target_frame.lower().startswith('q-sample'): target_frame = 'qsample' else: raise RuntimeError('Target frame %s is not supported.' % target_frame) # Process data and save status, pt_num_list = self.get_pt_numbers(exp_no, scan_no, True) if status is False: err_msg = pt_num_list return False, err_msg else: print '[DB] Number of Pts for Scan %d is %d' % (scan_no, len(pt_num_list)) print '[DB] Data directory: %s' % self._dataDir max_pts = 0 ws_names_str = '' ws_names_to_group = '' for pt in pt_num_list: try: self.download_spice_xml_file(scan_no, pt, overwrite=False) api.CollectHB3AExperimentInfo( ExperimentNumber=exp_no, ScanList='%d' % scan_no, PtLists='-1,%d' % pt, DataDirectory=self._dataDir, GenerateVirtualInstrument=False, OutputWorkspace='ScanPtInfo_Exp%d_Scan%d' % (exp_no, scan_no), DetectorTableWorkspace='MockDetTable') out_q_name = 'HB3A_Exp%d_Scan%d_Pt%d_MD' % (exp_no, scan_no, pt) api.ConvertCWSDExpToMomentum( InputWorkspace='ScanPtInfo_Exp406_Scan%d' % scan_no, CreateVirtualInstrument=False, OutputWorkspace=out_q_name, Directory=self._dataDir) ws_names_to_group += out_q_name + ',' if target_frame == 'hkl': out_hkl_name = 'HKL_Scan%d_Pt%d' % (scan_no, pt) api.ConvertCWSDMDtoHKL(InputWorkspace=out_q_name, UBMatrix=ub_matrix_1d, OutputWorkspace=out_hkl_name) ws_names_str += out_hkl_name + ',' ws_names_to_group += out_hkl_name + ',' else: ws_names_str += out_q_name + ',' except RuntimeError as e: print '[Error] Reducing scan %d pt %d due to %s' % (scan_no, pt, str(e)) continue else: max_pts = pt # END-FOR # Merge if target_frame == 'qsample': out_ws_name = target_ws_name + '_QSample' elif target_frame == 'hkl': out_ws_name = target_ws_name + '_HKL' else: raise RuntimeError('Impossible to have target frame %s' % target_frame) ws_names_str = ws_names_str[:-1] api.MergeMD(InputWorkspaces=ws_names_str, OutputWorkspace=out_ws_name, SplitInto=max_pts) # Group workspaces group_name = 'Group_Exp406_Scan%d' % scan_no api.GroupWorkspaces(InputWorkspaces=ws_names_to_group, OutputWorkspace=group_name) spice_table_name = get_spice_table_name(exp_no, scan_no) api.GroupWorkspaces(InputWorkspaces='%s,%s' % (group_name, spice_table_name), OutputWorkspace=group_name) ret_tup = out_ws_name, group_name return ret_tup
def PyExec(self): ms.ExtractSingleSpectrum(InputWorkspace=self._input_ws, OutputWorkspace=self._output_ws, WorkspaceIndex=self._spec_idx) # Performs corrections self._define_corrections() # The workspaces to fit for correction scale factors fit_corrections = [ wks for wks in self._correction_workspaces if 'MultipleScattering' not in wks ] # Perform fitting of corrections fixed_params = {} fixed_gamma_factor = self.getProperty("GammaBackgroundScale").value if fixed_gamma_factor != 0.0 and not self._back_scattering: fixed_params['GammaBackground'] = fixed_gamma_factor fixed_container_scale = self.getProperty("ContainerScale").value if fixed_container_scale != 0.0: fixed_params['Container'] = fixed_container_scale params_ws = self._fit_corrections(fit_corrections, self._linear_fit_table, **fixed_params) self.setProperty("LinearFitResult", params_ws) # Scale gamma background if self.getProperty( "GammaBackground").value and not self._back_scattering: gamma_correct_ws = self._get_correction_workspace( 'GammaBackground')[1] gamma_factor = self._get_correction_scale_factor( 'GammaBackground', fit_corrections, params_ws) ms.Scale(InputWorkspace=gamma_correct_ws, OutputWorkspace=gamma_correct_ws, Factor=gamma_factor) # Scale multiple scattering if self.getProperty("MultipleScattering").value: # Use factor of total scattering as this includes single and multiple scattering multi_scatter_correct_ws = self._get_correction_workspace( 'MultipleScattering')[1] total_scatter_correct_ws = self._get_correction_workspace( 'TotalScattering')[1] total_scatter_factor = self._get_correction_scale_factor( 'TotalScattering', fit_corrections, params_ws) ms.Scale(InputWorkspace=multi_scatter_correct_ws, OutputWorkspace=multi_scatter_correct_ws, Factor=total_scatter_factor) ms.Scale(InputWorkspace=total_scatter_correct_ws, OutputWorkspace=total_scatter_correct_ws, Factor=total_scatter_factor) # Scale by container if self._container_ws != "": container_correct_ws = self._get_correction_workspace( 'Container')[1] container_factor = self._get_correction_scale_factor( 'Container', fit_corrections, params_ws) ms.Scale(InputWorkspace=container_correct_ws, OutputWorkspace=container_correct_ws, Factor=container_factor) # Calculate and output corrected workspaces as a WorkspaceGroup if self._corrected_wsg != "": corrected_workspaces = [ ws_name.replace(self._correction_wsg, self._corrected_wsg) for ws_name in self._correction_workspaces ] for corrected, correction in zip(corrected_workspaces, self._correction_workspaces): ms.Minus(LHSWorkspace=self._output_ws, RHSWorkspace=correction, OutputWorkspace=corrected) ms.GroupWorkspaces(InputWorkspaces=corrected_workspaces, OutputWorkspace=self._corrected_wsg) self.setProperty("CorrectedWorkspaces", self._corrected_wsg) # Apply corrections for correction in self._correction_workspaces: if 'TotalScattering' not in correction: ms.Minus(LHSWorkspace=self._output_ws, RHSWorkspace=correction, OutputWorkspace=self._output_ws) self.setProperty("OutputWorkspace", self._output_ws) # Remove correction workspaces if they are no longer required if self._correction_wsg == "": for wksp in self._correction_workspaces: ms.DeleteWorkspace(wksp)
def fit_tof_iteration(sample_data, container_data, runs, flags): """ Performs a single iterations of the time of flight corrections and fitting workflow. :param sample_data: Loaded sample data workspaces :param container_data: Loaded container data workspaces :param runs: A string specifying the runs to process :param flags: A dictionary of flags to control the processing :return: Tuple of (workspace group name, pre correction fit parameters, final fit parameters, chi^2 values) """ # Transform inputs into something the algorithm can understand if isinstance(flags['masses'][0], list): mass_values = _create_profile_strs_and_mass_list( copy.deepcopy(flags['masses'][0]))[0] profiles_strs = [] for mass_spec in flags['masses']: profiles_strs.append( _create_profile_strs_and_mass_list(mass_spec)[1]) else: mass_values, profiles_strs = _create_profile_strs_and_mass_list( flags['masses']) background_str = _create_background_str(flags.get('background', None)) intensity_constraints = _create_intensity_constraint_str( flags['intensity_constraints']) ties = _create_user_defined_ties_str(flags['masses']) num_spec = sample_data.getNumberHistograms() pre_correct_pars_workspace = None pars_workspace = None fit_workspace = None max_fit_iterations = flags.get('max_fit_iterations', 5000) output_groups = [] chi2_values = [] data_workspaces = [] result_workspaces = [] group_name = runs + '_result' for index in range(num_spec): if isinstance(profiles_strs, list): profiles = profiles_strs[index] else: profiles = profiles_strs suffix = _create_fit_workspace_suffix(index, sample_data, flags['fit_mode'], flags['spectra'], flags.get('iteration', None)) # Corrections corrections_args = dict() # Need to do a fit first to obtain the parameter table pre_correction_pars_name = runs + "_params_pre_correction" + suffix corrections_fit_name = "__vesuvio_corrections_fit" ms.VesuvioTOFFit(InputWorkspace=sample_data, WorkspaceIndex=index, Masses=mass_values, MassProfiles=profiles, Background=background_str, IntensityConstraints=intensity_constraints, Ties=ties, OutputWorkspace=corrections_fit_name, FitParameters=pre_correction_pars_name, MaxIterations=max_fit_iterations, Minimizer=flags['fit_minimizer']) ms.DeleteWorkspace(corrections_fit_name) corrections_args['FitParameters'] = pre_correction_pars_name # Add the multiple scattering arguments corrections_args.update(flags['ms_flags']) corrected_data_name = runs + "_tof_corrected" + suffix linear_correction_fit_params_name = runs + "_correction_fit_scale" + suffix if flags.get('output_verbose_corrections', False): corrections_args[ "CorrectionWorkspaces"] = runs + "_correction" + suffix corrections_args[ "CorrectedWorkspaces"] = runs + "_corrected" + suffix if container_data is not None: corrections_args["ContainerWorkspace"] = container_data ms.VesuvioCorrections( InputWorkspace=sample_data, OutputWorkspace=corrected_data_name, LinearFitResult=linear_correction_fit_params_name, WorkspaceIndex=index, GammaBackground=flags.get('gamma_correct', False), Masses=mass_values, MassProfiles=profiles, IntensityConstraints=intensity_constraints, MultipleScattering=True, GammaBackgroundScale=flags.get('fixed_gamma_scaling', 0.0), ContainerScale=flags.get('fixed_container_scaling', 0.0), **corrections_args) # Final fit fit_ws_name = runs + "_data" + suffix pars_name = runs + "_params" + suffix fit_result = ms.VesuvioTOFFit( InputWorkspace=corrected_data_name, WorkspaceIndex=0, Masses=mass_values, MassProfiles=profiles, Background=background_str, IntensityConstraints=intensity_constraints, Ties=ties, OutputWorkspace=fit_ws_name, FitParameters=pars_name, MaxIterations=max_fit_iterations, Minimizer=flags['fit_minimizer']) chi2_values.append(fit_result[-1]) ms.DeleteWorkspace(corrected_data_name) # Process parameter tables if pre_correct_pars_workspace is None: pre_correct_pars_workspace = _create_param_workspace( num_spec, mtd[pre_correction_pars_name]) if pars_workspace is None: pars_workspace = _create_param_workspace(num_spec, mtd[pars_name]) if fit_workspace is None: fit_workspace = _create_param_workspace( num_spec, mtd[linear_correction_fit_params_name]) spec_num_str = str(sample_data.getSpectrum(index).getSpectrumNo()) current_spec = 'spectrum_' + spec_num_str _update_fit_params(pre_correct_pars_workspace, index, mtd[pre_correction_pars_name], current_spec) _update_fit_params(pars_workspace, index, mtd[pars_name], current_spec) _update_fit_params(fit_workspace, index, mtd[linear_correction_fit_params_name], current_spec) ms.DeleteWorkspace(pre_correction_pars_name) ms.DeleteWorkspace(pars_name) ms.DeleteWorkspace(linear_correction_fit_params_name) # Process spectrum group # Note the ordering of operations here gives the order in the WorkspaceGroup output_workspaces = [] data_workspaces.append(fit_ws_name) if flags.get('output_verbose_corrections', False): output_workspaces += mtd[ corrections_args["CorrectionWorkspaces"]].getNames() output_workspaces += mtd[ corrections_args["CorrectedWorkspaces"]].getNames() ms.UnGroupWorkspace(corrections_args["CorrectionWorkspaces"]) ms.UnGroupWorkspace(corrections_args["CorrectedWorkspaces"]) for workspace in output_workspaces: group_name = runs + '_iteration_' + str( flags.get('iteration', None)) name = group_name + '_' + workspace.split( '_')[1] + '_' + workspace.split('_')[-1] result_workspaces.append(name) if index == 0: ms.RenameWorkspace(InputWorkspace=workspace, OutputWorkspace=name) else: ms.ConjoinWorkspaces(InputWorkspace1=name, InputWorkspace2=workspace) # Output the parameter workspaces params_pre_corr = runs + "_params_pre_correction_iteration_" + str( flags['iteration']) params_name = runs + "_params_iteration_" + str(flags['iteration']) fit_name = runs + "_correction_fit_scale_iteration_" + str( flags['iteration']) AnalysisDataService.Instance().addOrReplace( params_pre_corr, pre_correct_pars_workspace) AnalysisDataService.Instance().addOrReplace(params_name, pars_workspace) AnalysisDataService.Instance().addOrReplace(fit_name, fit_workspace) if result_workspaces: output_groups.append( ms.GroupWorkspaces(InputWorkspaces=result_workspaces, OutputWorkspace=group_name)) if data_workspaces: output_groups.append( ms.GroupWorkspaces(InputWorkspaces=data_workspaces, OutputWorkspace=group_name + '_data')) else: output_groups.append(fit_ws_name) if len(output_groups) > 1: result_ws = output_groups else: result_ws = output_groups[0] return result_ws, pre_correct_pars_workspace, pars_workspace, chi2_values
def PyExec(self): self.check_platform_support() from IndirectBayes import (CalcErange, GetXYE) setup_prog = Progress(self, start=0.0, end=0.3, nreports=5) self.log().information('BayesQuasi input') erange = [self._e_min, self._e_max] nbins = [self._sam_bins, self._res_bins] setup_prog.report('Converting to binary for Fortran') # convert true/false to 1/0 for fortran o_el = int(self._elastic) o_w1 = int(self._width) o_res = int(self._res_norm) # fortran code uses background choices defined using the following numbers setup_prog.report('Encoding input options') o_bgd = ['Zero', 'Flat', 'Sloping'].index(self._background) fitOp = [o_el, o_bgd, o_w1, o_res] setup_prog.report('Establishing save path') workdir = config['defaultsave.directory'] if not os.path.isdir(workdir): workdir = os.getcwd() logger.information('Default Save directory is not set. Defaulting to current working Directory: ' + workdir) array_len = 4096 # length of array in Fortran setup_prog.report('Checking X Range') CheckXrange(erange, 'Energy') nbin, nrbin = nbins[0], nbins[1] logger.information('Sample is ' + self._samWS) logger.information('Resolution is ' + self._resWS) # Check for trailing and leading zeros in data setup_prog.report('Checking for leading and trailing zeros in the data') first_data_point, last_data_point = IndentifyDataBoundaries(self._samWS) self.check_energy_range_for_zeroes(first_data_point, last_data_point) # update erange with new values erange = [self._e_min, self._e_max] setup_prog.report('Checking Analysers') CheckAnalysers(self._samWS, self._resWS) setup_prog.report('Obtaining EFixed, theta and Q') efix = getEfixed(self._samWS) theta, Q = GetThetaQ(self._samWS) nsam, ntc = CheckHistZero(self._samWS) totalNoSam = nsam # check if we're performing a sequential fit if not self._loop: nsam = 1 nres = CheckHistZero(self._resWS)[0] setup_prog.report('Checking Histograms') if self._program == 'QL': if nres == 1: prog = 'QLr' # res file else: prog = 'QLd' # data file CheckHistSame(self._samWS, 'Sample', self._resWS, 'Resolution') elif self._program == 'QSe': if nres == 1: prog = 'QSe' # res file else: raise ValueError('Stretched Exp ONLY works with RES file') logger.information('Version is {0}'.format(prog)) logger.information(' Number of spectra = {0} '.format(nsam)) logger.information(' Erange : {0} to {1} '.format(erange[0], erange[1])) setup_prog.report('Reading files') Wy, We = self._read_width_file(self._width, self._wfile, totalNoSam) dtn, xsc = self._read_norm_file(self._res_norm, self._resnormWS, totalNoSam) setup_prog.report('Establishing output workspace name') fname = self._samWS[:-4] + '_' + prog probWS = fname + '_Prob' fitWS = fname + '_Fit' wrks = os.path.join(workdir, self._samWS[:-4]) logger.information(' lptfile : ' + wrks + '_' + prog + '.lpt') lwrk = len(wrks) wrks.ljust(140, ' ') wrkr = self._resWS wrkr.ljust(140, ' ') setup_prog.report('Initialising probability list') # initialise probability list if self._program == 'QL': prob0, prob1, prob2, prob3 = [], [], [], [] xQ = np.array([Q[0]]) for m in range(1, nsam): xQ = np.append(xQ, Q[m]) xProb = xQ xProb = np.append(xProb, xQ) xProb = np.append(xProb, xQ) xProb = np.append(xProb, xQ) eProb = np.zeros(4 * nsam) group = '' workflow_prog = Progress(self, start=0.3, end=0.7, nreports=nsam * 3) for spectrum in range(0, nsam): logger.information('Group {0} at angle {1} '.format(spectrum, theta[spectrum])) nsp = spectrum + 1 nout, bnorm, Xdat, Xv, Yv, Ev = CalcErange(self._samWS, spectrum, erange, nbin) Ndat = nout[0] Imin = nout[1] Imax = nout[2] if prog == 'QLd': mm = spectrum else: mm = 0 Nb, Xb, Yb, Eb = GetXYE(self._resWS, mm, array_len) # get resolution data numb = [nsam, nsp, ntc, Ndat, nbin, Imin, Imax, Nb, nrbin] rscl = 1.0 reals = [efix, theta[spectrum], rscl, bnorm] if prog == 'QLr': workflow_prog.report('Processing Sample number {0} as Lorentzian'.format(spectrum)) nd, xout, yout, eout, yfit, yprob = QLr.qlres(numb, Xv, Yv, Ev, reals, fitOp, Xdat, Xb, Yb, Wy, We, dtn, xsc, wrks, wrkr, lwrk) logger.information(' Log(prob) : {0} {1} {2} {3}'.format(yprob[0], yprob[1], yprob[2], yprob[3])) elif prog == 'QLd': workflow_prog.report('Processing Sample number {0}'.format(spectrum)) nd, xout, yout, eout, yfit, yprob = QLd.qldata(numb, Xv, Yv, Ev, reals, fitOp, Xdat, Xb, Yb, Eb, Wy, We, wrks, wrkr, lwrk) logger.information(' Log(prob) : {0} {1} {2} {3}'.format(yprob[0], yprob[1], yprob[2], yprob[3])) elif prog == 'QSe': workflow_prog.report('Processing Sample number {0} as Stretched Exp'.format(spectrum)) nd, xout, yout, eout, yfit, yprob = Qse.qlstexp(numb, Xv, Yv, Ev, reals, fitOp, Xdat, Xb, Yb, Wy, We, dtn, xsc, wrks, wrkr, lwrk) dataX = xout[:nd] dataX = np.append(dataX, 2 * xout[nd - 1] - xout[nd - 2]) yfit_list = np.split(yfit[:4 * nd], 4) dataF1 = yfit_list[1] workflow_prog.report('Processing data') dataG = np.zeros(nd) datX = dataX datY = yout[:nd] datE = eout[:nd] datX = np.append(datX, dataX) datY = np.append(datY, dataF1[:nd]) datE = np.append(datE, dataG) res1 = dataF1[:nd] - yout[:nd] datX = np.append(datX, dataX) datY = np.append(datY, res1) datE = np.append(datE, dataG) nsp = 3 names = 'data,fit.1,diff.1' res_plot = [0, 1, 2] if self._program == 'QL': workflow_prog.report('Processing Lorentzian result data') dataF2 = yfit_list[2] datX = np.append(datX, dataX) datY = np.append(datY, dataF2[:nd]) datE = np.append(datE, dataG) res2 = dataF2[:nd] - yout[:nd] datX = np.append(datX, dataX) datY = np.append(datY, res2) datE = np.append(datE, dataG) nsp += 2 names += ',fit.2,diff.2' dataF3 = yfit_list[3] datX = np.append(datX, dataX) datY = np.append(datY, dataF3[:nd]) datE = np.append(datE, dataG) res3 = dataF3[:nd] - yout[:nd] datX = np.append(datX, dataX) datY = np.append(datY, res3) datE = np.append(datE, dataG) nsp += 2 names += ',fit.3,diff.3' res_plot.append(4) prob0.append(yprob[0]) prob1.append(yprob[1]) prob2.append(yprob[2]) prob3.append(yprob[3]) # create result workspace fitWS = fname + '_Workspaces' fout = fname + '_Workspace_' + str(spectrum) workflow_prog.report('Creating OutputWorkspace') s_api.CreateWorkspace(OutputWorkspace=fout, DataX=datX, DataY=datY, DataE=datE, Nspec=nsp, UnitX='DeltaE', VerticalAxisUnit='Text', VerticalAxisValues=names) # append workspace to list of results group += fout + ',' comp_prog = Progress(self, start=0.7, end=0.8, nreports=2) comp_prog.report('Creating Group Workspace') s_api.GroupWorkspaces(InputWorkspaces=group, OutputWorkspace=fitWS) if self._program == 'QL': comp_prog.report('Processing Lorentzian probability data') yPr0 = np.array([prob0[0]]) yPr1 = np.array([prob1[0]]) yPr2 = np.array([prob2[0]]) yPr3 = np.array([prob3[0]]) for m in range(1, nsam): yPr0 = np.append(yPr0, prob0[m]) yPr1 = np.append(yPr1, prob1[m]) yPr2 = np.append(yPr2, prob2[m]) yPr3 = np.append(yPr3, prob3[m]) yProb = yPr0 yProb = np.append(yProb, yPr1) yProb = np.append(yProb, yPr2) yProb = np.append(yProb, yPr3) prob_axis_names = '0 Peak, 1 Peak, 2 Peak, 3 Peak' s_api.CreateWorkspace(OutputWorkspace=probWS, DataX=xProb, DataY=yProb, DataE=eProb, Nspec=4, UnitX='MomentumTransfer', VerticalAxisUnit='Text', VerticalAxisValues=prob_axis_names) outWS = self.C2Fw(fname) elif self._program == 'QSe': comp_prog.report('Running C2Se') outWS = self.C2Se(fname) # Sort x axis s_api.SortXAxis(InputWorkspace=outWS, OutputWorkspace=outWS, EnableLogging=False) log_prog = Progress(self, start=0.8, end=1.0, nreports=8) # Add some sample logs to the output workspaces log_prog.report('Copying Logs to outputWorkspace') s_api.CopyLogs(InputWorkspace=self._samWS, OutputWorkspace=outWS) log_prog.report('Adding Sample logs to Output workspace') self._add_sample_logs(outWS, prog, erange, nbins) log_prog.report('Copying logs to fit Workspace') s_api.CopyLogs(InputWorkspace=self._samWS, OutputWorkspace=fitWS) log_prog.report('Adding sample logs to Fit workspace') self._add_sample_logs(fitWS, prog, erange, nbins) log_prog.report('Finalising log copying') self.setProperty('OutputWorkspaceFit', fitWS) self.setProperty('OutputWorkspaceResult', outWS) log_prog.report('Setting workspace properties') if self._program == 'QL': s_api.SortXAxis(InputWorkspace=probWS, OutputWorkspace=probWS, EnableLogging=False) self.setProperty('OutputWorkspaceProb', probWS)
def generate_ts_pdf(run_number, focus_file_path, sample_details, merge_banks=False, q_lims=None, cal_file_name=None, delta_r=None, delta_q=None, pdf_type="G(r)", lorch_filter=None, freq_params=None, debug=False): if sample_details is None: raise RuntimeError( "A SampleDetails object was not set. Please create a SampleDetails object and set the " "relevant properties it. Then set the new sample by calling set_sample_details()" ) focused_ws = _obtain_focused_run(run_number, focus_file_path) focused_ws = mantid.ConvertUnits(InputWorkspace=focused_ws, Target="MomentumTransfer", EMode='Elastic') raw_ws = mantid.Load(Filename='POLARIS' + str(run_number)) sample_geometry_json = sample_details.generate_sample_geometry() sample_material_json = sample_details.generate_sample_material() self_scattering_correction = mantid.TotScatCalculateSelfScattering( InputWorkspace=raw_ws, CalFileName=cal_file_name, SampleGeometry=sample_geometry_json, SampleMaterial=sample_material_json) ws_group_list = [] for i in range(self_scattering_correction.getNumberHistograms()): ws_name = 'correction_' + str(i) mantid.ExtractSpectra(InputWorkspace=self_scattering_correction, OutputWorkspace=ws_name, WorkspaceIndexList=[i]) ws_group_list.append(ws_name) self_scattering_correction = mantid.GroupWorkspaces( InputWorkspaces=ws_group_list) self_scattering_correction = mantid.RebinToWorkspace( WorkspaceToRebin=self_scattering_correction, WorkspaceToMatch=focused_ws) if not compare_ws_compatibility(focused_ws, self_scattering_correction): raise RuntimeError( "To use create_total_scattering_pdf you need to run focus with " "do_van_normalisation=true first.") focused_ws = mantid.Subtract(LHSWorkspace=focused_ws, RHSWorkspace=self_scattering_correction) if debug: dcs_corrected = mantid.CloneWorkspace(InputWorkspace=focused_ws) # convert diff cross section to S(Q) - 1 material_builder = MaterialBuilder() sample = material_builder.setFormula( sample_details.material_object.chemical_formula).build() sample_total_scatter_cross_section = sample.totalScatterXSection() sample_coh_scatter_cross_section = sample.cohScatterXSection() focused_ws = focused_ws - sample_total_scatter_cross_section / (4 * math.pi) focused_ws = focused_ws * 4 * math.pi / sample_coh_scatter_cross_section if debug: s_of_q_minus_one = mantid.CloneWorkspace(InputWorkspace=focused_ws) if delta_q: focused_ws = mantid.Rebin(InputWorkspace=focused_ws, Params=delta_q) if merge_banks: q_min, q_max = _load_qlims(q_lims) merged_ws = mantid.MatchAndMergeWorkspaces(InputWorkspaces=focused_ws, XMin=q_min, XMax=q_max, CalculateScale=False) fast_fourier_filter(merged_ws, rho0=sample_details.material_object.number_density, freq_params=freq_params) pdf_output = mantid.PDFFourierTransform( Inputworkspace="merged_ws", InputSofQType="S(Q)-1", PDFType=pdf_type, Filter=lorch_filter, DeltaR=delta_r, rho0=sample_details.material_object.number_density) else: for ws in focused_ws: fast_fourier_filter( ws, rho0=sample_details.material_object.number_density, freq_params=freq_params) pdf_output = mantid.PDFFourierTransform( Inputworkspace='focused_ws', InputSofQType="S(Q)-1", PDFType=pdf_type, Filter=lorch_filter, DeltaR=delta_r, rho0=sample_details.material_object.number_density) pdf_output = mantid.RebinToWorkspace(WorkspaceToRebin=pdf_output, WorkspaceToMatch=pdf_output[4], PreserveEvents=True) if not debug: common.remove_intermediate_workspace('self_scattering_correction') # Rename output ws if 'merged_ws' in locals(): mantid.RenameWorkspace(InputWorkspace='merged_ws', OutputWorkspace=run_number + '_merged_Q') mantid.RenameWorkspace(InputWorkspace='focused_ws', OutputWorkspace=run_number + '_focused_Q') target_focus_ws_name = run_number + '_focused_Q_' target_pdf_ws_name = run_number + '_pdf_R_' if isinstance(focused_ws, WorkspaceGroup): for i in range(len(focused_ws)): if str(focused_ws[i]) != (target_focus_ws_name + str(i + 1)): mantid.RenameWorkspace(InputWorkspace=focused_ws[i], OutputWorkspace=target_focus_ws_name + str(i + 1)) mantid.RenameWorkspace(InputWorkspace='pdf_output', OutputWorkspace=run_number + '_pdf_R') if isinstance(pdf_output, WorkspaceGroup): for i in range(len(pdf_output)): if str(pdf_output[i]) != (target_pdf_ws_name + str(i + 1)): mantid.RenameWorkspace(InputWorkspace=pdf_output[i], OutputWorkspace=target_pdf_ws_name + str(i + 1)) return pdf_output
def PyExec(self): run_f2py_compatibility_test() from IndirectBayes import (CalcErange, GetXYE) from IndirectCommon import (CheckXrange, CheckAnalysersOrEFixed, getEfixed, GetThetaQ, CheckHistZero) setup_prog = Progress(self, start=0.0, end=0.3, nreports=5) logger.information('BayesStretch input') logger.information('Sample is %s' % self._sam_name) logger.information('Resolution is %s' % self._res_name) setup_prog.report('Converting to binary for Fortran') fitOp = self._encode_fit_ops(self._elastic, self._background) setup_prog.report('Establishing save path') workdir = self._establish_save_path() setup_prog.report('Checking X Range') CheckXrange(self._erange, 'Energy') setup_prog.report('Checking Analysers') CheckAnalysersOrEFixed(self._sam_name, self._res_name) setup_prog.report('Obtaining EFixed, theta and Q') efix = getEfixed(self._sam_name) theta, Q = GetThetaQ(self._sam_name) setup_prog.report('Checking Histograms') nsam, ntc = CheckHistZero(self._sam_name) # check if we're performing a sequential fit if not self._loop: nsam = 1 logger.information('Version is Stretch') logger.information('Number of spectra = %s ' % nsam) logger.information('Erange : %f to %f ' % (self._erange[0], self._erange[1])) setup_prog.report('Creating FORTRAN Input') fname = self._sam_name[:-4] + '_Stretch' wrks = os.path.join(workdir, self._sam_name[:-4]) logger.information('lptfile : %s_Qst.lpt' % wrks) lwrk = len(wrks) wrks.ljust(140, ' ') wrkr = self._res_name wrkr.ljust(140, ' ') eBet0 = np.zeros(self._nbet) # set errors to zero eSig0 = np.zeros(self._nsig) # set errors to zero rscl = 1.0 Qaxis = '' workflow_prog = Progress(self, start=0.3, end=0.7, nreports=nsam * 3) # Empty arrays to hold Sigma and Bet x,y,e values xSig, ySig, eSig = [], [], [] xBet, yBet, eBet = [], [], [] for m in range(nsam): logger.information('Group %i at angle %f' % (m, theta[m])) nsp = m + 1 nout, bnorm, Xdat, Xv, Yv, Ev = CalcErange(self._sam_name, m, self._erange, self._nbins[0]) Ndat = nout[0] Imin = nout[1] Imax = nout[2] # get resolution data (4096 = FORTRAN array length) Nb, Xb, Yb, _ = GetXYE(self._res_name, 0, 4096) numb = [ nsam, nsp, ntc, Ndat, self._nbins[0], Imin, Imax, Nb, self._nbins[1], self._nbet, self._nsig ] reals = [efix, theta[m], rscl, bnorm] workflow_prog.report('Processing spectrum number %i' % m) xsout, ysout, xbout, ybout, zpout = Que.quest( numb, Xv, Yv, Ev, reals, fitOp, Xdat, Xb, Yb, wrks, wrkr, lwrk) dataXs = xsout[:self._nsig] # reduce from fixed FORTRAN array dataYs = ysout[:self._nsig] dataXb = xbout[:self._nbet] dataYb = ybout[:self._nbet] zpWS = fname + '_Zp' + str(m) if m > 0: Qaxis += ',' Qaxis += str(Q[m]) dataXz = [] dataYz = [] dataEz = [] for n in range(self._nsig): yfit_list = np.split(zpout[:self._nsig * self._nbet], self._nsig) dataYzp = yfit_list[n] dataXz = np.append(dataXz, xbout[:self._nbet]) dataYz = np.append(dataYz, dataYzp[:self._nbet]) dataEz = np.append(dataEz, eBet0) zpWS = fname + '_Zp' + str(m) self._create_workspace(zpWS, [dataXz, dataYz, dataEz], self._nsig, dataXs, True) xSig = np.append(xSig, dataXs) ySig = np.append(ySig, dataYs) eSig = np.append(eSig, eSig0) xBet = np.append(xBet, dataXb) yBet = np.append(yBet, dataYb) eBet = np.append(eBet, eBet0) if m == 0: groupZ = zpWS else: groupZ = groupZ + ',' + zpWS # create workspaces for sigma and beta workflow_prog.report('Creating OutputWorkspace') self._create_workspace(fname + '_Sigma', [xSig, ySig, eSig], nsam, Qaxis) self._create_workspace(fname + '_Beta', [xBet, yBet, eBet], nsam, Qaxis) group = fname + '_Sigma,' + fname + '_Beta' fit_ws = fname + '_Fit' s_api.GroupWorkspaces(InputWorkspaces=group, OutputWorkspace=fit_ws) contour_ws = fname + '_Contour' s_api.GroupWorkspaces(InputWorkspaces=groupZ, OutputWorkspace=contour_ws) # Add some sample logs to the output workspaces log_prog = Progress(self, start=0.8, end=1.0, nreports=6) log_prog.report('Copying Logs to Fit workspace') copy_log_alg = self.createChildAlgorithm('CopyLogs', enableLogging=False) copy_log_alg.setProperty('InputWorkspace', self._sam_name) copy_log_alg.setProperty('OutputWorkspace', fit_ws) copy_log_alg.execute() log_prog.report('Adding Sample logs to Fit workspace') self._add_sample_logs(fit_ws, self._erange, self._nbins[0]) log_prog.report('Copying logs to Contour workspace') copy_log_alg.setProperty('InputWorkspace', self._sam_name) copy_log_alg.setProperty('OutputWorkspace', contour_ws) copy_log_alg.execute() log_prog.report('Adding sample logs to Contour workspace') self._add_sample_logs(contour_ws, self._erange, self._nbins[0]) log_prog.report('Finialising log copying') # sort x axis s_api.SortXAxis(InputWorkspace=fit_ws, OutputWorkspace=fit_ws, EnableLogging=False) s_api.SortXAxis(InputWorkspace=contour_ws, OutputWorkspace=contour_ws, EnableLogging=False) self.setProperty('OutputWorkspaceFit', fit_ws) self.setProperty('OutputWorkspaceContour', contour_ws) log_prog.report('Setting workspace properties')
def group_grouped_workspaces(name, workspaces): tmp = mantid.CreateSampleWorkspace() overall = mantid.GroupWorkspaces(tmp, OutputWorkspace=str(name)) for workspace in workspaces: overall.add(workspace) mantid.AnalysisDataService.remove("tmp")