@@ -19,7 +19,7 @@ def __init__(self, workspace, ip_file, number_of_iterations, mask_spectra,
19
19
gamma_correction , mode_running , transmission_guess = None , multiple_scattering_order = None ,
20
20
number_of_events = None , results_path = None ):
21
21
22
- self ._name = workspace .name ()
22
+ self ._name = workspace .name () + '_'
23
23
self ._ip_file = ip_file
24
24
self ._number_of_iterations = number_of_iterations
25
25
spectrum_list = workspace .getSpectrumNumbers ()
@@ -141,6 +141,11 @@ def _update_workspace_data(self):
141
141
self ._fit_profiles_workspaces [element ] = self ._create_emtpy_ncp_workspace (f'_{ element } _ncp' )
142
142
self ._fit_profiles_workspaces ['total' ] = self ._create_emtpy_ncp_workspace (f'_total_ncp' )
143
143
144
+ self ._mean_widths = None
145
+ self ._std_widths = None
146
+ self ._mean_intensity_ratios = None
147
+ self ._std_intensity_ratios = None
148
+
144
149
145
150
def _create_emtpy_ncp_workspace (self , suffix ):
146
151
return CreateWorkspace (
@@ -175,60 +180,46 @@ def run(self):
175
180
# InputWorkspace=ic.sampleWS, OutputWorkspace=initialWs.name()
176
181
# )
177
182
178
- CloneWorkspace (
183
+ RenameWorkspace (
179
184
InputWorkspace = self ._workspace_being_fit .name (),
180
- OutputWorkspace = self ._workspace_being_fit . name () + "0"
185
+ OutputWorkspace = self ._name + '0'
181
186
)
182
187
183
188
for iteration in range (self ._number_of_iterations + 1 ):
184
- # Workspace from previous iteration
185
- self ._workspace_being_fit = mtd [self ._name + str (iteration )]
186
189
190
+ self ._workspace_being_fit = mtd [self ._name + str (iteration )]
187
191
self ._update_workspace_data ()
188
192
189
193
self ._fit_neutron_compton_profiles ()
190
194
191
195
self ._create_summed_workspaces ()
192
-
193
- mWidths , stdWidths , mIntRatios , stdIntRatios = self .extractMeans ()
194
-
195
- self .createMeansAndStdTableWS (
196
- mWidths , stdWidths , mIntRatios , stdIntRatios
197
- )
196
+ self ._set_means_and_std ()
198
197
199
198
# When last iteration, skip MS and GC
200
199
if iteration == self ._number_of_iterations :
201
200
break
202
201
202
+ # Do this because MS and Gamma corrections do not accept zero columns
203
203
if iteration == 0 :
204
204
self ._replace_zero_columns_with_ncp_fit ()
205
205
206
- CloneWorkspace (InputWorkspace = self ._name , OutputWorkspace = "tmpNameWs" )
207
-
208
- if self ._gamma_correction :
209
- wsGC = self .createWorkspacesForGammaCorrection (mWidths , mIntRatios )
210
- Minus (
211
- LHSWorkspace = "tmpNameWs" , RHSWorkspace = wsGC , OutputWorkspace = "tmpNameWs"
212
- )
213
-
214
- if self ._multiple_scattering_correction :
215
- wsMS = self .createWorkspacesForMSCorrection (mWidths , mIntRatios )
216
- Minus (
217
- LHSWorkspace = "tmpNameWs" , RHSWorkspace = wsMS , OutputWorkspace = "tmpNameWs"
218
- )
206
+ CloneWorkspace (
207
+ InputWorkspace = self ._workspace_for_corrections .name (),
208
+ OutputWorkspace = "next_iteration"
209
+ )
210
+ self ._correct_for_gamma_and_multiple_scattering ("next_iteration" )
211
+ self ._remask_columns_with_zeros ("next_iteration" )
219
212
220
- self ._remask_columns_with_zeros ("tmpNameWS" )
221
213
RenameWorkspace (
222
- InputWorkspace = "tmpNameWs" , OutputWorkspace = self ._name + str (iteration + 1 )
214
+ InputWorkspace = "next_iteration" ,
215
+ OutputWorkspace = self ._name + str (iteration + 1 )
223
216
)
224
217
225
- self ._set_up_results_mehtods ()
218
+ self ._set_results ()
226
219
self .save_results ()
227
220
return self
228
221
229
222
230
-
231
-
232
223
def createTableInitialParameters (self ):
233
224
meansTableWS = CreateEmptyTableWorkspace (
234
225
OutputWorkspace = self ._name + "_Initial_Parameters"
@@ -265,9 +256,7 @@ def _fit_neutron_compton_profiles(self):
265
256
self ._fit_neutron_compton_profiles_to_row ()
266
257
self ._row_being_fit += 1
267
258
268
- assert ~ np .all (
269
- self ._fit_parameters == 0
270
- ), "Fitting parameters cannot be zero for all spectra!"
259
+ assert np .any (self ._fit_parameters ), "Fitting parameters cannot be zero for all spectra!"
271
260
return
272
261
273
262
@@ -396,8 +385,8 @@ def _create_summed_workspaces(self):
396
385
OutputWorkspace = ws .name () + "_Sum"
397
386
)
398
387
399
- def extractMeans (self ):
400
- """Extract widths and intensities from tableWorkspace"""
388
+ def _set_means_and_std (self ):
389
+ """Calculate mean widths and intensities from tableWorkspace"""
401
390
402
391
fitParsTable = self ._table_fit_results
403
392
widths = np .zeros ((self ._masses .size , fitParsTable .rowCount ()))
@@ -413,35 +402,40 @@ def extractMeans(self):
413
402
) = self .calculateMeansAndStds (widths , intensities )
414
403
415
404
assert (
416
- len (widths ) == self ._masses .size
417
- ), "Widths and intensities must be in shape (noOfMasses, noOfSpec)"
418
- return meanWidths , stdWidths , meanIntensityRatios , stdIntensityRatios
405
+ len (meanWidths ) == len (self ._profiles )
406
+ ), "Number of mean widths must match number of profiles!"
407
+
408
+ self ._mean_widths = meanWidths
409
+ self ._std_widths = stdWidths
410
+ self ._mean_intensity_ratios = meanIntensityRatios
411
+ self ._std_intensity_ratios = stdIntensityRatios
419
412
413
+ self .createMeansAndStdTableWS ()
414
+ return
420
415
421
- def createMeansAndStdTableWS (
422
- self , meanWidths , stdWidths , meanIntensityRatios , stdIntensityRatios
423
- ):
416
+
417
+ def createMeansAndStdTableWS (self ):
424
418
meansTableWS = CreateEmptyTableWorkspace (
425
- OutputWorkspace = self ._workspace_being_fit .name () + "_Mean_Widths_And_Intensities "
419
+ OutputWorkspace = self ._workspace_being_fit .name () + "_MeanWidthsAndIntensities "
426
420
)
427
- meansTableWS .addColumn (type = "float " , name = "Mass" )
421
+ meansTableWS .addColumn (type = "str " , name = "Mass" )
428
422
meansTableWS .addColumn (type = "float" , name = "Mean Widths" )
429
423
meansTableWS .addColumn (type = "float" , name = "Std Widths" )
430
424
meansTableWS .addColumn (type = "float" , name = "Mean Intensities" )
431
425
meansTableWS .addColumn (type = "float" , name = "Std Intensities" )
432
426
433
427
print ("\n Created Table with means and std:" )
434
428
print ("\n Mass Mean \u00B1 Std Widths Mean \u00B1 Std Intensities\n " )
435
- for m , mw , stdw , mi , stdi in zip (
436
- self ._masses . astype ( float ),
437
- meanWidths ,
438
- stdWidths ,
439
- meanIntensityRatios ,
440
- stdIntensityRatios ,
429
+ for p , mean_width , std_width , mean_intensity , std_intensity in zip (
430
+ self ._profiles . values ( ),
431
+ self . _mean_widths ,
432
+ self . _std_widths ,
433
+ self . _mean_intensity_ratios ,
434
+ self . _std_intensity_ratios ,
441
435
):
442
- meansTableWS .addRow ([m , mw , stdw , mi , stdi ])
443
- print (f"{ m :5.2f } { mw :10.5f} \u00B1 { stdw :7.5f} { mi :10.5f } \u00B1 { stdi :7.5f } " )
444
- print ( " \n " )
436
+ meansTableWS .addRow ([p . label , mean_width , std_width , mean_intensity , std_intensity ])
437
+ print (f"{ p . label :5s } { mean_width :10.5f} \u00B1 { std_width :7.5f} \
438
+ { mean_intensity :10.5f } \u00B1 { std_intensity :7.5f } \n " )
445
439
return
446
440
447
441
@@ -737,15 +731,14 @@ def _replace_zero_columns_with_ncp_fit(self):
737
731
738
732
self ._zero_columns_mask = np .all (dataY == 0 , axis = 0 ) # Masked Cols
739
733
740
- ws = CloneWorkspace (
734
+ self . _workspace_for_corrections = CloneWorkspace (
741
735
InputWorkspace = self ._workspace_for_corrections .name (),
742
736
OutputWorkspace = self ._workspace_for_corrections .name () + "_CorrectionsInput"
743
737
)
744
- for row in range (ws .getNumberHistograms ()):
738
+ for row in range (self . _workspace_for_corrections .getNumberHistograms ()):
745
739
# TODO: Once the option to chage point to hist is removed, remove [:len(ncp)]
746
- ws .dataY (row )[self ._zero_columns_mask ] = ncp [row , self ._zero_columns_mask [:len (ncp [row ])]]
740
+ self . _workspace_for_corrections .dataY (row )[self ._zero_columns_mask ] = ncp [row , self ._zero_columns_mask [:len (ncp [row ])]]
747
741
748
- self ._workspace_for_corrections = ws
749
742
SumSpectra (
750
743
InputWorkspace = self ._workspace_for_corrections .name (),
751
744
OutputWorkspace = self ._workspace_for_corrections .name () + "_Sum"
@@ -759,29 +752,39 @@ def _remask_columns_with_zeros(self, ws_to_remask_name):
759
752
initial workspace to set these columns again to zero on the
760
753
workspace resulting from the multiple scattering or gamma correction.
761
754
"""
762
- ws = mtd [ws_to_remask_name ]
763
- for row in range (ws .getNumberHistograms ()):
764
- ws .dataY (row )[self ._zero_columns_mask ] = 0
765
- ws .dataE (row )[self ._zero_columns_mask ] = 0
766
- # dataX, dataY, dataE = extractWS(ws)
767
- # mask = np.all(dataY == 0, axis=0)
768
- #
769
- # wsM = mtd[wsToMaskName]
770
- # dataXM, dataYM, dataEM = extractWS(wsM)
771
- # dataYM[:, mask] = 0
772
- # if np.all(dataE == 0):
773
- # dataEM = np.zeros(dataEM.shape)
774
- #
775
- # passDataIntoWS(dataXM, dataYM, dataEM, wsM)
755
+ ws_to_remask = mtd [ws_to_remask_name ]
756
+ for row in range (ws_to_remask .getNumberHistograms ()):
757
+ ws_to_remask .dataY (row )[self ._zero_columns_mask ] = 0
758
+ ws_to_remask .dataE (row )[self ._zero_columns_mask ] = 0
759
+ return
760
+
761
+
762
+ def _correct_for_gamma_and_multiple_scattering (self , ws_name ):
763
+
764
+ if self ._gamma_correction :
765
+ gamma_correction_ws = self .create_gamma_workspaces ()
766
+ Minus (
767
+ LHSWorkspace = ws_name ,
768
+ RHSWorkspace = gamma_correction_ws .name (),
769
+ OutputWorkspace = ws_name
770
+ )
771
+
772
+ if self ._multiple_scattering_correction :
773
+ multiple_scattering_ws = self .create_multiple_scattering_workspaces ()
774
+ Minus (
775
+ LHSWorkspace = ws_name ,
776
+ RHSWorkspace = multiple_scattering_ws .name (),
777
+ OutputWorkspace = ws_name
778
+ )
776
779
return
777
780
778
781
779
- def createWorkspacesForMSCorrection (self , meanWidths , meanIntensityRatios ):
782
+ def create_multiple_scattering_workspaces (self ):
780
783
"""Creates _MulScattering and _TotScattering workspaces used for the MS correction"""
781
784
782
785
self .createSlabGeometry (self ._workspace_for_corrections ) # Sample properties for MS correction
783
786
784
- sampleProperties = self .calcMSCorrectionSampleProperties (meanWidths , meanIntensityRatios )
787
+ sampleProperties = self .calcMSCorrectionSampleProperties (self . _mean_widths , self . _mean_intensity_ratios )
785
788
print (
786
789
"\n The sample properties for Multiple Scattering correction are:\n \n " ,
787
790
sampleProperties ,
@@ -884,12 +887,12 @@ def createMulScatWorkspaces(self, ws, sampleProperties):
884
887
return mtd [ws .name () + "_MulScattering" ]
885
888
886
889
887
- def createWorkspacesForGammaCorrection (self , meanWidths , meanIntensityRatios ):
890
+ def create_gamma_workspaces (self ):
888
891
"""Creates _gamma_background correction workspace to be subtracted from the main workspace"""
889
892
890
893
inputWS = self ._workspace_for_corrections .name ()
891
894
892
- profiles = self .calcGammaCorrectionProfiles (meanWidths , meanIntensityRatios )
895
+ profiles = self .calcGammaCorrectionProfiles (self . _mean_widths , self . _mean_intensity_ratios )
893
896
894
897
# Approach below not currently suitable for current versions of Mantid, but will be in the future
895
898
# background, corrected = VesuvioCalculateGammaBackground(InputWorkspace=inputWS, ComptonFunction=profiles)
@@ -937,7 +940,7 @@ def calcGammaCorrectionProfiles(self, meanWidths, meanIntensityRatios):
937
940
return profiles
938
941
939
942
940
- def _set_up_results_mehtods (self ):
943
+ def _set_results (self ):
941
944
"""Used to collect results from workspaces and store them in .npz files for testing."""
942
945
943
946
self .wsFinal = mtd [self ._name + str (self ._number_of_iterations )]
@@ -981,7 +984,7 @@ def _set_up_results_mehtods(self):
981
984
allIterNcp .append (allNCP )
982
985
983
986
# Extract Mean and Std Widths, Intensities
984
- meansTable = mtd [wsIterName + "_Mean_Widths_And_Intensities " ]
987
+ meansTable = mtd [wsIterName + "_MeanWidthsAndIntensities " ]
985
988
allMeanWidhts .append (meansTable .column ("Mean Widths" ))
986
989
allStdWidths .append (meansTable .column ("Std Widths" ))
987
990
allMeanIntensities .append (meansTable .column ("Mean Intensities" ))
0 commit comments