def runTest(self): # Load raw data (bank 1) wsMD = LoadMD( "WISH38237_MD.nxs") # default so doesn't get overwrite van # For each mod vec, predict and integrate peaks and combine qs = [(0.15, 0, 0.3), (-0.15, 0, 0.3)] all_pks = CreatePeaksWorkspace(InstrumentWorkspace=wsMD, NumberOfPeaks=0, OutputWorkspace="all_pks") LoadIsawUB(InputWorkspace=all_pks, Filename='Wish_Diffuse_Scattering_ISAW_UB.mat') # PredictPeaks parent = PredictPeaks(InputWorkspace=all_pks, WavelengthMin=0.8, WavelengthMax=9.3, MinDSpacing=0.5, ReflectionCondition="Primitive") self._pfps = [] self._saved_files = [] for iq, q in enumerate(qs): wsname = f'pfp_{iq}' PredictFractionalPeaks(Peaks=parent, IncludeAllPeaksInRange=True, Hmin=0, Hmax=0, Kmin=1, Kmax=1, Lmin=0, Lmax=1, ReflectionCondition='Primitive', MaxOrder=1, ModVector1=",".join([str(qi) for qi in q]), FracPeaks=wsname) FilterPeaks(InputWorkspace=wsname, OutputWorkspace=wsname, FilterVariable='Wavelength', FilterValue=9.3, Operator='<') # should get rid of one peak in q1 table FilterPeaks(InputWorkspace=wsname, OutputWorkspace=wsname, FilterVariable='Wavelength', FilterValue=0.8, Operator='>') IntegratePeaksMD(InputWorkspace=wsMD, PeakRadius='0.1', BackgroundInnerRadius='0.1', BackgroundOuterRadius='0.15', PeaksWorkspace=wsname, OutputWorkspace=wsname, IntegrateIfOnEdge=False, UseOnePercentBackgroundCorrection=False) all_pks = CombinePeaksWorkspaces(LHSWorkspace=all_pks, RHSWorkspace=wsname) self._pfps.append(ADS.retrieve(wsname)) self._filepath = os.path.join(config['defaultsave.directory'], 'WISH_IntegratedSatellite.int') SaveReflections(InputWorkspace=all_pks, Filename=self._filepath, Format='Jana') self._all_pks = all_pks
def test_save_jana_format_modulated_separate_files(self): # Arrange workspace = self._create_modulated_peak_table() reference_results = [ self._get_reference_result( "jana_format_modulated-m{}.hkl".format(i)) for i in range(1, 3) ] file_name = os.path.join(self._test_dir, "test_jana_modulated.hkl") output_format = "Jana" split_files = True # Act SaveReflections(InputWorkspace=workspace, Filename=file_name, Format=output_format, SplitFiles=split_files) # Assert self._assert_file_content_equal( reference_results[0], os.path.join(self._test_dir, "test_jana_modulated-m1.hkl")) self._assert_file_content_equal( reference_results[1], os.path.join(self._test_dir, "test_jana_modulated-m2.hkl"))
def test_save_jana_with_no_lattice_information(self): peaks = CloneWorkspace(self._workspace) peaks.sample().clearOrientedLattice() file_name = os.path.join(self._test_dir, "test_jana_no_lattice.hkl") # Act SaveReflections(InputWorkspace=peaks, Filename=file_name, Format="Jana", SplitFiles=False)
def test_save_fullprof_format(self): # Arrange reference_result = self._get_reference_result("fullprof_format.hkl") file_name = os.path.join(self._test_dir, "test_fullprof.hkl") output_format = "Fullprof" # Act SaveReflections(InputWorkspace=self._workspace, Filename=file_name, Format=output_format) # Assert self.assertTrue(compare_file(reference_result, file_name))
def test_save_SHELX_format(self): # Arrange reference_result = self._get_reference_result("shelx_format.hkl") file_name = os.path.join(self._test_dir, "test_shelx.hkl") output_format = "SHELX" # Act SaveReflections(InputWorkspace=self._workspace, Filename=file_name, Format=output_format) # Assert self._assert_file_content_equal(reference_result, file_name)
def test_save_jana_format_modulated_single_file(self): # Arrange workspace = self._create_modulated_peak_table() reference_result = self._get_reference_result("jana_format_modulated.hkl") file_name = os.path.join(self._test_dir, "test_jana_modulated.hkl") output_format = "Jana" # Act SaveReflections(InputWorkspace=workspace, Filename=file_name, Format=output_format) # Assert self._assert_file_content_equal(reference_result, file_name)
def _test_helper_scale_large_intensities(self, output_format, file_name): # Arrange DeleteTableRows(TableWorkspace=self._workspace, Rows=f'1-{self._workspace.getNumberPeaks()-1}') # only first pk self._workspace.getPeak(0).setIntensity(2.5E8) # Act SaveReflections(InputWorkspace=self._workspace, Filename=file_name, Format=output_format) # Read lines from file to be asserted in individual tests with open(file_name, 'r') as actual_file: lines = actual_file.readlines() return lines
def test_save_GSAS_format(self): # Arrange reference_result = self._get_reference_result("gsas_format.hkl") file_name = os.path.join(self._test_dir, "test_gsas.hkl") output_format = "GSAS" # Act SaveReflections(InputWorkspace=self._workspace, Filename=file_name, Format=output_format) # Assert #self._assert_file_content_equal(reference_result, file_name)) self._assert_file_content_equal(reference_result, file_name)
def test_save_fullprof_format_constant_wavelength(self): # Arrange # leave only one peak (therefore a single wavelength in table) DeleteTableRows(TableWorkspace=self._workspace, Rows=f'1-{self._workspace.getNumberPeaks() - 1:.0f}') file_name = os.path.join(self._test_dir, "test_fullprof_cw.hkl") output_format = "Fullprof" # Act SaveReflections(InputWorkspace=self._workspace, Filename=file_name, Format=output_format) # Assert wavelength = 0 with open(file_name, 'r') as file: file.readline() # skip file.readline() # skip wavelength = float(file.readline().lstrip().split(' ')[0]) self.assertAlmostEqual(wavelength, self._workspace.getPeak(0).getWavelength(), delta=1e-4)
def PyExec(self): input_workspaces, peak_workspaces = self._expand_groups() output_workspace_name = self.getPropertyValue("OutputWorkspace") peak_radius = self.getProperty("PeakRadius").value inner_radius = self.getProperty("BackgroundInnerRadius").value outer_radius = self.getProperty("BackgroundOuterRadius").value remove_0_intensity = self.getProperty("RemoveZeroIntensity").value use_lorentz = self.getProperty("ApplyLorentz").value multi_ws = len(input_workspaces) > 1 output_workspaces = [] for input_ws, peak_ws in zip(input_workspaces, peak_workspaces): if multi_ws: peaks_ws_name = input_ws + '_' + output_workspace_name output_workspaces.append(peaks_ws_name) else: peaks_ws_name = output_workspace_name IntegratePeaksMD(InputWorkspace=input_ws, PeakRadius=peak_radius, BackgroundInnerRadius=inner_radius, BackgroundOuterRadius=outer_radius, PeaksWorkspace=peak_ws, OutputWorkspace=peaks_ws_name) if multi_ws: peaks_ws_name = output_workspace_name CreatePeaksWorkspace( InstrumentWorkspace=input_workspaces[0], NumberOfPeaks=0, OutputWorkspace=peaks_ws_name, OutputType=mtd[peak_workspaces[0]].id().replace( 'sWorkspace', '')) CopySample(InputWorkspace=output_workspaces[0], OutputWorkspace=peaks_ws_name, CopyName=False, CopyMaterial=False, CopyEnvironment=False, CopyShape=False, CopyLattice=True) for peak_ws in output_workspaces: CombinePeaksWorkspaces(peaks_ws_name, peak_ws, OutputWorkspace=peaks_ws_name) DeleteWorkspace(peak_ws) if use_lorentz: # Apply Lorentz correction: peaks = AnalysisDataService[peaks_ws_name] for p in range(peaks.getNumberPeaks()): peak = peaks.getPeak(p) lorentz = abs( np.sin(peak.getScattering() * np.cos(peak.getAzimuthal()))) peak.setIntensity(peak.getIntensity() * lorentz) if remove_0_intensity: FilterPeaks(InputWorkspace=peaks_ws_name, OutputWorkspace=peaks_ws_name, FilterVariable='Intensity', FilterValue=0, Operator='>') # Write output only if a file path was provided if not self.getProperty("OutputFile").isDefault: out_format = self.getProperty("OutputFormat").value filename = self.getProperty("OutputFile").value if out_format == "SHELX": SaveHKL(InputWorkspace=peaks_ws_name, Filename=filename, DirectionCosines=True, OutputWorkspace="__tmp") DeleteWorkspace("__tmp") elif out_format == "Fullprof": SaveReflections(InputWorkspace=peaks_ws_name, Filename=filename, Format="Fullprof") else: # This shouldn't happen RuntimeError("Invalid output format given") self.setProperty("OutputWorkspace", AnalysisDataService[peaks_ws_name])