forked from AmbaPant/mantid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SNSPowderReduction.py
1632 lines (1416 loc) · 79.4 KB
/
SNSPowderReduction.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Mantid Repository : https://github.com/mantidproject/mantid
#
# Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
# NScD Oak Ridge National Laboratory, European Spallation Source,
# Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
# SPDX - License - Identifier: GPL - 3.0 +
#pylint: disable=invalid-name,no-init,too-many-lines
import os
from pathlib import Path
import mantid.simpleapi as api
from mantid.api import mtd, AlgorithmFactory, AnalysisDataService, DistributedDataProcessorAlgorithm, \
FileAction, FileProperty, ITableWorkspaceProperty, MultipleFileProperty, PropertyMode, WorkspaceProperty, \
ITableWorkspace, MatrixWorkspace
from mantid.kernel import (
ConfigService, Direction, EnabledWhenProperty, FloatArrayProperty, FloatBoundedValidator, IntArrayBoundedValidator,
IntArrayProperty, MaterialBuilder, Property, PropertyCriterion, PropertyManagerDataService, StringListValidator,
StringTimeSeriesProperty)
from mantid.dataobjects import SplittersWorkspace # SplittersWorkspace
from mantid.utils import absorptioncorrutils
if AlgorithmFactory.exists('GatherWorkspaces'):
HAVE_MPI = True
from mpi4py import MPI
mpiRank = MPI.COMM_WORLD.Get_rank()
else:
HAVE_MPI = False
mpiRank = 0 # simplify if clauses
EVENT_WORKSPACE_ID = "EventWorkspace"
EXTENSIONS_NXS = ["_event.nxs", ".nxs.h5"]
def noRunSpecified(runs):
""" Check whether any run is specified
Purpose:
Check whether any run, which is larger than 0, is specified in the input numpy array
Requirements:
Input is a numpy array
Guarantees:
A boolean is returned
:param runs:
:return:
"""
# return True if runs is of size zero
if len(runs) <= 0:
return True
# return True if there is one and only one run in runs and it is not greater than 0.
if len(runs) == 1:
# break off the instrument part
value = int(runs[0].split('_')[-1])
# -1 turns off the runnumber
return value <= 0
return False
def get_workspace(workspace_name):
"""
Purpose: Get the reference of a workspace
Requirements:
1. workspace_name is a string
Guarantees:
The reference of the workspace with same is returned.
:exception RuntimeError: a RuntimeError from Mantid will be thrown
:param workspace_name:
:return:
"""
assert isinstance(workspace_name, str), 'Input workspace name {0} must be a string but not a {1}.' \
''.format(workspace_name, type(workspace_name))
return AnalysisDataService.retrieve(workspace_name)
def is_event_workspace(workspace):
if isinstance(workspace, str):
return get_workspace(workspace).id() == EVENT_WORKSPACE_ID
else:
return workspace.id() == EVENT_WORKSPACE_ID
def allEventWorkspaces(*args):
"""
Purpose:
Check whether all the inputs are event workspace
Requirements:
Each element in the args is a string as name of workspace
Guarantees:
return boolean
@param args:
@return:
"""
result = True
args = [is_event_workspace(arg) for arg in args]
for arg in args:
result = result and arg
return result
def getBasename(filename):
if type(filename) == list:
filename = filename[0]
name = os.path.split(filename)[-1]
for extension in EXTENSIONS_NXS:
name = name.replace(extension, '')
return name
#pylint: disable=too-many-instance-attributes
class SNSPowderReduction(DistributedDataProcessorAlgorithm):
COMPRESS_TOL_TOF = .01
_resampleX = None
_binning = None
_bin_in_dspace = None
_instrument = None
_filterBadPulses = None
_removePromptPulseWidth = None
_LRef = None
_DIFCref = None
_wavelengthMin = None
_wavelengthMax = None
_vanPeakFWHM = None
_vanSmoothing = None
_vanRadius = None
_scaleFactor = None
_outDir = None
_outPrefix = None
_outTypes = None
_chunks = None
_splittersWS = None
_splitinfotablews = None
_normalisebycurrent = None
_lowResTOFoffset = None
_focusPos = None
_charTable = None
iparmFile = None
_info = None
_absMethod = None
_sampleFormula = None
_massDensity = None
_containerShape = None
def category(self):
return "Diffraction\\Reduction"
def seeAlso(self):
return [ "DiffractionFocussing","AlignAndFocusPowder" ]
def name(self):
return "SNSPowderReduction"
def summary(self):
"""
summary of the algorithm
:return:
"""
return "The algorithm used for reduction of powder diffraction data obtained on SNS instruments (e.g. PG3) "
def PyInit(self):
self.copyProperties('AlignAndFocusPowderFromFiles', ['Filename', 'PreserveEvents', 'DMin', 'DMax', 'DeltaRagged'])
self.declareProperty("Sum", False,
"Sum the runs. Does nothing for characterization runs")
self.declareProperty("PushDataPositive", "None",
StringListValidator(["None", "ResetToZero", "AddMinimum"]),
"Add a constant to the data that makes it positive over the whole range.")
arrvalidator = IntArrayBoundedValidator(lower=-1)
self.declareProperty(IntArrayProperty("BackgroundNumber", values=[0], validator=arrvalidator),
doc="If specified overrides value in CharacterizationRunsFile If -1 turns off correction.")
self.declareProperty(IntArrayProperty("VanadiumNumber", values=[0], validator=arrvalidator),
doc="If specified overrides value in CharacterizationRunsFile. If -1 turns off correction.")
self.declareProperty(IntArrayProperty("VanadiumBackgroundNumber", values=[0], validator=arrvalidator),
doc="If specified overrides value in CharacterizationRunsFile. If -1 turns off correction.")
self.declareProperty(FileProperty(name="CalibrationFile",defaultValue="",action=FileAction.OptionalLoad,
extensions=[".h5", ".hd5", ".hdf", ".cal"])) # CalFileName
self.declareProperty(FileProperty(name="GroupingFile",defaultValue="",action=FileAction.OptionalLoad,
extensions=[".xml"]), "Overrides grouping from CalibrationFile")
self.declareProperty(MultipleFileProperty(name="CharacterizationRunsFile",
action=FileAction.OptionalLoad,
extensions=["txt"]), "File with characterization runs denoted")
self.declareProperty(FileProperty(name="ExpIniFilename", defaultValue="", action=FileAction.OptionalLoad,
extensions=[".ini"]))
self.copyProperties('AlignAndFocusPowderFromFiles',
['LorentzCorrection', 'UnwrapRef', 'LowResRef', 'CropWavelengthMin', 'CropWavelengthMax',
'RemovePromptPulseWidth', 'MaxChunkSize'])
self.declareProperty(FloatArrayProperty("Binning", values=[0., 0., 0.],
direction=Direction.Input),
"Positive is linear bins, negative is logorithmic") # Params
self.copyProperties('AlignAndFocusPowderFromFiles', ['ResampleX'])
self.declareProperty("BinInDspace", True,
"If all three bin parameters a specified, whether they are in dspace (true) or "
"time-of-flight (false)") # DSpacing
# section of vanadium run processing
self.declareProperty("StripVanadiumPeaks", True,
"Subtract fitted vanadium peaks from the known positions.")
self.declareProperty("VanadiumFWHM", 7, "Default=7")
self.declareProperty("VanadiumPeakTol", 0.05,
"How far from the ideal position a vanadium peak can be during StripVanadiumPeaks. "
"Default=0.05, negative turns off")
self.declareProperty("VanadiumSmoothParams", "20,2", "Default=20,2")
self.declareProperty("VanadiumRadius", .3175, "Radius for CarpenterSampleCorrection")
self.declareProperty("BackgroundSmoothParams", "", "Default=off, suggested 20,2")
# filtering
self.declareProperty("FilterBadPulses", 95., # different default value
doc="Filter out events measured while proton charge is more than 5% below average")
self.declareProperty("ScaleData", defaultValue=1., validator=FloatBoundedValidator(lower=0., exclusive=True),
doc="Constant to multiply the data before writing out. This does not apply to "
"PDFgetN files.")
self.declareProperty("OffsetData", defaultValue=0., validator=FloatBoundedValidator(lower=0., exclusive=False),
doc="Constant to add to the data before writing out. This does not apply to "
"PDFgetN files.")
self.declareProperty("SaveAs", "gsas",
"List of all output file types. Allowed values are 'fullprof', 'gsas', 'nexus', "
"'pdfgetn', and 'topas'")
self.declareProperty("OutputFilePrefix", "", "Overrides the default filename for the output file (Optional).")
self.declareProperty(FileProperty(name="OutputDirectory", defaultValue="",action=FileAction.Directory))
# Caching options
self.declareProperty( 'CacheDir', "", 'comma-delimited ascii string representation of a list of candidate cache directories')
self.declareProperty('CleanCache', False, 'Remove all cache files within CacheDir')
self.setPropertySettings('CleanCache', EnabledWhenProperty('CacheDir', PropertyCriterion.IsNotDefault))
property_names = ('CacheDir', 'CleanCache')
[self.setPropertyGroup(name, 'Caching') for name in property_names]
self.declareProperty("FinalDataUnits", "dSpacing", StringListValidator(["dSpacing","MomentumTransfer"]))
# absorption correction
self.declareProperty("TypeOfCorrection", "None",
StringListValidator(["None", "SampleOnly", "SampleAndContainer", "FullPaalmanPings"]),
doc="Specifies the Absorption Correction terms to calculate, if any.")
self.declareProperty("SampleFormula", "", doc="Chemical formula of the sample")
self.declareProperty("SampleGeometry", {}, doc="A dictionary of geometry parameters for the sample.")
self.declareProperty("MeasuredMassDensity", defaultValue=0.1,
validator=FloatBoundedValidator(lower=0., exclusive=True),
doc="Measured mass density of sample in g/cc") # in g/cc, way to validate?
self.declareProperty("SampleNumberDensity", defaultValue=Property.EMPTY_DBL,
doc="Number density of the sample in number of atoms per cubic Angstrom will be used instead of calculated")
self.declareProperty("ContainerShape", defaultValue="PAC06", doc="Defines the container geometry")
self.declareProperty("ContainerScaleFactor", defaultValue=1.0,
validator=FloatBoundedValidator(lower=0),
doc="Factor to scale the container data")
self.copyProperties("AbsorptionCorrection", "ElementSize")
self.declareProperty("NumWavelengthBins", defaultValue=1000,
doc="Number of wavelength bin to calculate the for absorption correction")
workspace_prop = WorkspaceProperty('SplittersWorkspace', '', Direction.Input, PropertyMode.Optional)
self.declareProperty(workspace_prop, "Splitters workspace for split event workspace.")
# replaced for matrix workspace, SplittersWorkspace and table workspace
# tableprop = ITableWorkspaceProperty("SplittersWorkspace", "", Direction.Input, PropertyMode.Optional)
# self.declareProperty(tableprop, "Splitters workspace for split event workspace.")
infotableprop = ITableWorkspaceProperty("SplitInformationWorkspace", "", Direction.Input, PropertyMode.Optional)
self.declareProperty(infotableprop, "Name of table workspace containing information for splitters.")
self.declareProperty("LowResolutionSpectraOffset", -1,
"If larger and equal to 0, then process low resolution TOF and offset is the spectra "
"number. Otherwise, ignored.") # LowResolutionSpectraOffset
self.declareProperty("NormalizeByCurrent", True, "Normalize by current")
self.declareProperty("CompressTOFTolerance", 0.01, "Tolerance to compress events in TOF.")
self.copyProperties('AlignAndFocusPowderFromFiles', ['FrequencyLogNames', 'WaveLengthLogNames'])
return
def validateInputs(self):
issues = dict()
# If doing absorption correction, make sure the sample formula is correct
if self.getProperty("TypeOfCorrection").value != "None":
if self.getProperty("SampleFormula").value.strip() != '':
try:
MaterialBuilder().setFormula(self.getProperty("SampleFormula").value.strip())
except ValueError as ex:
issues['SampleFormula'] = "Invalid SampleFormula: '{}'".format(str(ex))
# The provided cache directory does not exist
cache_dir_string = self.getProperty('CacheDir').value # comma-delimited string representation of list
if bool(cache_dir_string):
cache_dirs = [candidate.strip() for candidate in cache_dir_string.split(',')]
for cache_dir in cache_dirs:
if bool(cache_dir) and Path(cache_dir).exists() is False:
issues['CacheDir'] = f'Directory {cache_dir} does not exist'
# We cannot clear the cache if property "CacheDir" has not been set
if self.getProperty('CleanCache').value and not bool(self.getProperty('CacheDir').value):
issues['CleanCache'] = 'Property "CacheDir" must be set in order to clean the cache'
return issues
#pylint: disable=too-many-locals,too-many-branches,too-many-statements
def PyExec(self): # noqa
""" Main execution body
"""
# get generic information
self._loadCharacterizations()
self._resampleX = self.getProperty("ResampleX").value
if self._resampleX != 0.:
self._binning = [0.]
else:
self._binning = self.getProperty("Binning").value
if len(self._binning) != 1 and len(self._binning) != 3:
raise RuntimeError("Can only specify (width) or (start,width,stop) for binning. Found %d values." % len(self._binning))
if len(self._binning) == 3:
if self._binning[0] == 0. and self._binning[1] == 0. and self._binning[2] == 0.:
raise RuntimeError("Failed to specify the binning")
self._bin_in_dspace = self.getProperty("BinInDspace").value
self._filterBadPulses = self.getProperty("FilterBadPulses").value
self._removePromptPulseWidth = self.getProperty("RemovePromptPulseWidth").value
self._lorentz = self.getProperty("LorentzCorrection").value
self._LRef = self.getProperty("UnwrapRef").value
self._DIFCref = self.getProperty("LowResRef").value
self._wavelengthMin = self.getProperty("CropWavelengthMin").value
self._wavelengthMax = self.getProperty("CropWavelengthMax").value
self._vanPeakFWHM = self.getProperty("VanadiumFWHM").value
self._vanSmoothing = self.getProperty("VanadiumSmoothParams").value
self._vanRadius = self.getProperty("VanadiumRadius").value
self.calib = self.getProperty("CalibrationFile").value
self._scaleFactor = self.getProperty("ScaleData").value
self._offsetFactor = self.getProperty("OffsetData").value
self._outDir = self.getProperty("OutputDirectory").value
# Caching options
self._cache_dirs = [os.path.abspath(me.strip()) for me in self.getProperty("CacheDir").value.split(',')
if me.strip()] # filter out empty elements
self._cache_dir = self._cache_dirs[0] if self._cache_dirs else ""
self._clean_cache = self.getProperty("CleanCache").value
self._outPrefix = self.getProperty("OutputFilePrefix").value.strip()
self._outTypes = self.getProperty("SaveAs").value.lower()
self._absMethod = self.getProperty("TypeOfCorrection").value
self._sampleFormula = self.getProperty("SampleFormula").value
self._sampleGeometry = self.getProperty("SampleGeometry").value
self._massDensity = self.getProperty("MeasuredMassDensity").value
self._numberDensity = self.getProperty("SampleNumberDensity").value
self._containerShape = self.getProperty("ContainerShape").value
self._containerScaleFactor = self.getProperty("ContainerScaleFactor").value
self._elementSize = self.getProperty("ElementSize").value
self._num_wl_bins = self.getProperty("NumWavelengthBins").value
samRuns = self._getLinearizedFilenames("Filename")
self._determineInstrument(samRuns[0])
preserveEvents = self.getProperty("PreserveEvents").value
if HAVE_MPI and preserveEvents:
self.log().warning("preserveEvents set to False for MPI tasks.")
preserveEvents = False
self._info = None
self._chunks = self.getProperty("MaxChunkSize").value
# define splitters workspace and filter wall time
self._splittersWS = self.getProperty("SplittersWorkspace").value
if self._splittersWS is not None:
# user specifies splitters workspace
self.log().information("SplittersWorkspace is %s" % (str(self._splittersWS)))
if len(samRuns) != 1:
# TODO/FUTURE - This should be supported
raise RuntimeError("Reducing data with splitters cannot happen when there are more than 1 sample run.")
# define range of wall-time to import data
sample_time_filter_wall = self._get_time_filter_wall(self._splittersWS.name(), samRuns[0])
self.log().information("The time filter wall is %s" %(str(sample_time_filter_wall)))
else:
sample_time_filter_wall = (0.0, 0.0)
self.log().information("SplittersWorkspace is None, and thus there is NO time filter wall. ")
self._splitinfotablews = self.getProperty("SplitInformationWorkspace").value
self._normalisebycurrent = self.getProperty("NormalizeByCurrent").value
# Tolerance for compress TOF event. If given a negative value, then use default 0.01
self.COMPRESS_TOL_TOF = float(self.getProperty("CompressTOFTolerance").value)
if self.COMPRESS_TOL_TOF < -0.:
self.COMPRESS_TOL_TOF = 0.01
# Clean the cache directory if so requested
if self._clean_cache:
api.CleanFileCache(CacheDir=self._cache_dir, AgeInDays=0)
# Process data
# List stores the workspacename of all data workspaces that will be converted to d-spacing in the end.
workspacelist = []
samwksplist = []
self._lowResTOFoffset = self.getProperty("LowResolutionSpectraOffset").value
focuspos = self._focusPos
if self._lowResTOFoffset >= 0:
# Dealing with the parameters for editing instrument parameters
if "PrimaryFlightPath" in focuspos:
l1 = focuspos["PrimaryFlightPath"]
if l1 > 0:
specids = focuspos['SpectrumIDs'][:]
l2s = focuspos['L2'][:]
polars = focuspos['Polar'][:]
phis = focuspos['Azimuthal'][:]
specids.extend(specids)
l2s.extend(l2s)
polars.extend(polars)
phis.extend(phis)
focuspos['SpectrumIDs'] = specids
focuspos['L2'] = l2s
focuspos['Polar'] = polars
focuspos['Azimuthal'] = phis
# ENDIF
# calculate absorption from first sample run
metaws = None
if self._absMethod != "None" and self._info is None:
absName = '__{}_abs'.format(getBasename(samRuns[0]))
api.Load(Filename=samRuns[0], OutputWorkspace=absName, MetaDataOnly=True)
self._info = self._getinfo(absName)
metaws = absName
if self._sampleFormula == '' and "SampleFormula" in mtd[metaws].run():
# Do a quick check to see if the sample formula in the logs is correct
try:
MaterialBuilder().setFormula(mtd[metaws].run()["SampleFormula"].lastValue().strip())
except ValueError:
self.log().warning(
"Sample formula '{}' found in sample logs does not have a valid format - specify manually in "
"algorithm input.".format(mtd[metaws].run()["SampleFormula"].lastValue().strip()))
# NOTE: inconsistent naming among different methods
# -> adding more comments to help clarify
a_sample, a_container = absorptioncorrutils.calculate_absorption_correction(
samRuns[0], # filename: File to be used for absorption correction
self._absMethod, # [None, SampleOnly, SampleAndContainer, FullPaalmanPings]
self._info, # PropertyManager of run characterizations
self._sampleFormula, # Material for absorption correction
self._massDensity, # Mass density of the sample
self._sampleGeometry, # Geometry parameters for the sample
self._numberDensity, # Optional number density of sample to be added
self._containerShape, # Shape definition of container
self._num_wl_bins, # Number of bins: len(ws.readX(0))-1
self._elementSize, # Size of one side of the integration element cube in mm
metaws, # Optional workspace containing metadata
self._cache_dirs, # Cache dir for absorption correction workspace
)
if self.getProperty("Sum").value and len(samRuns) > 1:
self.log().information('Ignoring value of "Sum" property')
# Sum input sample runs and then do reduction
if self._splittersWS is not None:
raise NotImplementedError("Summing spectra and filtering events are not supported simultaneously.")
sam_ws_name = self._focusAndSum(samRuns, preserveEvents=preserveEvents, absorptionWksp=a_sample)
assert isinstance(sam_ws_name, str), 'Returned from _focusAndSum() must be a string but not' \
'%s. ' % str(type(sam_ws_name))
workspacelist.append(sam_ws_name)
samwksplist.append(sam_ws_name)
# ENDIF (SUM)
else:
# Process each sample run
for sam_run_number in samRuns:
# first round of processing the sample
self._info = None
if sample_time_filter_wall[0] == 0. and sample_time_filter_wall[-1] == 0. \
and self._splittersWS is None:
returned = self._focusAndSum([sam_run_number], preserveEvents=preserveEvents, absorptionWksp=a_sample)
else:
returned = self._focusChunks(sam_run_number, sample_time_filter_wall,
splitwksp=self._splittersWS,
preserveEvents=preserveEvents)
if isinstance(returned, list):
# Returned with a list of workspaces
focusedwksplist = returned
for sam_ws_name in focusedwksplist:
assert isinstance(sam_ws_name, str), 'Impossible to have a non-string value in ' \
'returned focused workspaces\' names.'
samwksplist.append(sam_ws_name)
workspacelist.append(sam_ws_name)
# END-FOR
else:
# returned as a single workspace
sam_ws_name = returned
assert isinstance(sam_ws_name, str)
samwksplist.append(sam_ws_name)
workspacelist.append(sam_ws_name)
# ENDIF
# END-FOR
# ENDIF (Sum data or not)
for (samRunIndex, sam_ws_name) in enumerate(samwksplist):
assert isinstance(sam_ws_name, str), 'Assuming that samRun is a string. But it is %s' % str(type(sam_ws_name))
if is_event_workspace(sam_ws_name):
self.log().information('Sample Run %s: starting number of events = %d.' % (
sam_ws_name, get_workspace(sam_ws_name).getNumberEvents()))
# Get run information
self._info = self._getinfo(sam_ws_name)
# process the container
can_run_numbers = self._info["container"].value
can_run_numbers = ['%s_%d' % (self._instrument, value) for value in can_run_numbers]
# Check if existing container
# - has history and is using SNSPowderReduction
# - was created using the same method
# -> carry on as usual
# - was created with a different method
# -> delete the current one, then carry on
# - no history (processing list of runs)
# -> carry on as a single call wiht list of runs ensures that same method is used.
can_run_ws_name, _ = self._generate_container_run_name(can_run_numbers, samRunIndex)
if can_run_ws_name in mtd:
hstry = mtd[can_run_ws_name].getHistory()
if not hstry.empty():
alg = hstry.getAlgorithmHistory(0)
if alg.name() == "SNSPowderReduction":
if alg.getPropertyValue("TypeOfCorrection") != self._absMethod:
self.log().information(
f"Remove {can_run_ws_name} as it is generated with a different method"
)
mtd.remove(can_run_ws_name)
can_run_ws_name = self._process_container_runs(can_run_numbers,
samRunIndex,
preserveEvents,
absorptionWksp=a_container)
if can_run_ws_name is not None:
workspacelist.append(can_run_ws_name)
# process the vanadium run
van_run_number_list = self._info["vanadium"].value
van_run_number_list = ['%s_%d' % (self._instrument, value) for value in van_run_number_list]
van_specified = not noRunSpecified(van_run_number_list)
if van_specified:
van_run_ws_name = self._process_vanadium_runs(van_run_number_list, samRunIndex)
workspacelist.append(van_run_ws_name)
else:
van_run_ws_name = None
# return if MPI is used and there is more than 1 processor
if mpiRank > 0:
return
# return if there is no sample run
# Note: sample run must exist in logic
# VZ: Discuss with Pete
# if sam_ws_name == 0:
# return
# the final bit of math to remove container run and vanadium run
if can_run_ws_name is not None and self._containerScaleFactor != 0:
# must convert the sample to a matrix workspace if the can run isn't one
if not allEventWorkspaces(can_run_ws_name, sam_ws_name):
api.ConvertToMatrixWorkspace(InputWorkspace=sam_ws_name,
OutputWorkspace=sam_ws_name)
# remove container run
api.RebinToWorkspace(WorkspaceToRebin=can_run_ws_name,
WorkspaceToMatch=sam_ws_name,
OutputWorkspace=can_run_ws_name)
api.Scale(InputWorkspace=can_run_ws_name,
OutputWorkspace=can_run_ws_name,
Factor=self._containerScaleFactor)
api.Minus(LHSWorkspace=sam_ws_name,
RHSWorkspace=can_run_ws_name,
OutputWorkspace=sam_ws_name)
# compress event if the sample run workspace is EventWorkspace
if is_event_workspace(sam_ws_name) and self.COMPRESS_TOL_TOF > 0.:
api.CompressEvents(InputWorkspace=sam_ws_name,
OutputWorkspace=sam_ws_name,
Tolerance=self.COMPRESS_TOL_TOF) # 10ns
# canRun = str(canRun)
if van_run_ws_name is not None:
# subtract vanadium run from sample run by division
num_hist_sam = get_workspace(sam_ws_name).getNumberHistograms()
num_hist_van = get_workspace(van_run_ws_name).getNumberHistograms()
assert num_hist_sam == num_hist_van, \
'Number of histograms of sample %d is not equal to van %d.' % (num_hist_sam, num_hist_van)
api.Divide(LHSWorkspace=sam_ws_name,
RHSWorkspace=van_run_ws_name,
OutputWorkspace=sam_ws_name)
normalized = True
van_run_ws = get_workspace(van_run_ws_name)
sam_ws = get_workspace(sam_ws_name)
sam_ws.getRun()['van_number'] = van_run_ws.getRun()['run_number'].value
# van_run_number = str(van_run_number)
else:
normalized = False
# Compress the event again
if is_event_workspace(sam_ws_name) and self.COMPRESS_TOL_TOF > 0.:
api.CompressEvents(InputWorkspace=sam_ws_name,
OutputWorkspace=sam_ws_name,
Tolerance=self.COMPRESS_TOL_TOF) # 5ns/
# write out the files
# FIXME - need documentation for mpiRank
if mpiRank == 0:
if self._scaleFactor != 1.:
api.Scale(sam_ws_name, Factor=self._scaleFactor, OutputWorkspace=sam_ws_name)
if self._offsetFactor != 0.:
api.ConvertToMatrixWorkspace(InputWorkspace=sam_ws_name,
OutputWorkspace=sam_ws_name)
api.Scale(sam_ws_name, Factor=self._offsetFactor, OutputWorkspace=sam_ws_name,
Operation='Add')
# make sure there are no negative values - gsas hates them
if self.getProperty("PushDataPositive").value != "None":
addMin = self.getProperty("PushDataPositive").value == "AddMinimum"
api.ResetNegatives(InputWorkspace=sam_ws_name,
OutputWorkspace=sam_ws_name,
AddMinimum=addMin,
ResetValue=0.)
self._save(sam_ws_name, self._info, normalized, False)
# ENDFOR
# convert everything into d-spacing as the final units
if mpiRank == 0:
workspacelist = set(workspacelist) # only do each workspace once
for wksp in workspacelist:
api.ConvertUnits(InputWorkspace=wksp, OutputWorkspace=wksp,
Target=self.getProperty("FinalDataUnits").value)
propertyName = "OutputWorkspace%s" % wksp
if not self.existsProperty(propertyName):
self.declareProperty(WorkspaceProperty(propertyName, wksp, Direction.Output))
self.setProperty(propertyName, wksp)
return
def _getLinearizedFilenames(self, propertyName):
runnumbers = self.getProperty(propertyName).value
linearizedRuns = []
for item in runnumbers:
if type(item) == list:
linearizedRuns.extend(item)
else:
linearizedRuns.append(item)
return linearizedRuns
def _determineInstrument(self, filename):
name = getBasename(filename)
parts = name.split('_')
self._instrument = ConfigService.getInstrument(parts[0]).shortName() # only works for instruments without '_'
def _loadCharacterizations(self):
self._focusPos = {}
charFilename = self.getProperty("CharacterizationRunsFile").value
charFilename = ','.join(charFilename)
expIniFilename = self.getProperty("ExpIniFilename").value
self._charTable = ''
if charFilename is None or len(charFilename) <= 0:
self.iparmFile = None
return
self._charTable = 'characterizations'
results = api.PDLoadCharacterizations(Filename=charFilename,
ExpIniFilename=expIniFilename,
OutputWorkspace=self._charTable)
# export the characterizations table
charTable = results[0]
if not self.existsProperty("CharacterizationsTable"):
self.declareProperty(ITableWorkspaceProperty("CharacterizationsTable", self._charTable, Direction.Output))
self.setProperty("CharacterizationsTable", charTable)
# get the focus positions from the properties
self.iparmFile = results[1]
self._focusPos['PrimaryFlightPath'] = results[2]
self._focusPos['SpectrumIDs'] = results[3]
self._focusPos['L2'] = results[4]
self._focusPos['Polar'] = results[5]
self._focusPos['Azimuthal'] = results[6]
#pylint: disable=too-many-branches
def _load_event_data(self, filename, filter_wall=None, out_ws_name=None, **chunk):
""" Load data optionally by chunk strategy
Purpose:
Load a complete or partial run, filter bad pulses.
Output workspace name is formed as
- user specified (highest priority)
- instrument-name_run-number_0 (no chunking)
- instrument-name_run-number_X: X is either ChunkNumber of SpectrumMin
Requirements:
1. run number is integer and larger than 0
Guarantees:
A workspace is created with name described above
:param filename:
:param filter_wall:
:param out_ws_name: name of output workspace specified by user. it will override the automatic name
:param chunk:
:return:
"""
# Check requirements
assert len(filename) > 0, "Input file '%s' does not exist" % filename
assert (chunk is None) or isinstance(chunk, dict), 'Input chunk must be either a dictionary or None.'
# get event file's base name
base_name = getBasename(filename)
# give out the default output workspace name
if out_ws_name is None:
if chunk:
if "ChunkNumber" in chunk:
out_ws_name = base_name + "__chk%d" % (int(chunk["ChunkNumber"]))
elif "SpectrumMin" in chunk:
seq_number = 1 + int(chunk["SpectrumMin"])/(int(chunk["SpectrumMax"])-int(chunk["SpectrumMin"]))
out_ws_name = base_name + "__chk%d" % seq_number
else:
out_ws_name = "%s_%d" % (base_name, 0)
# END-IF
# Specify the other chunk information including Precount, FilterByTimeStart and FilterByTimeStop.
if filename.endswith("_event.nxs") or filename.endswith(".nxs.h5"):
chunk["Precount"] = True
if filter_wall is not None:
if filter_wall[0] > 0.:
chunk["FilterByTimeStart"] = filter_wall[0]
if filter_wall[1] > 0.:
chunk["FilterByTimeStop"] = filter_wall[1]
# Call Mantid's Load algorithm to load complete or partial data
api.Load(Filename=filename, OutputWorkspace=out_ws_name, **chunk)
# Log output
if is_event_workspace(out_ws_name):
self.log().debug("Load run %s: number of events = %d. " % (os.path.split(filename)[-1],
get_workspace(out_ws_name).getNumberEvents()))
if HAVE_MPI:
msg = "MPI Task = %s ;" % (str(mpiRank))
if is_event_workspace(out_ws_name):
msg += "Number Events = " + str(get_workspace(out_ws_name).getNumberEvents())
self.log().debug(msg)
# filter bad pulses
if self._filterBadPulses > 0.:
# record number of events of the original workspace
if is_event_workspace(out_ws_name):
# Event workspace: record original number of events
num_original_events = get_workspace(out_ws_name).getNumberEvents()
else:
num_original_events = -1
# filter bad pulse
api.FilterBadPulses(InputWorkspace=out_ws_name, OutputWorkspace=out_ws_name,
LowerCutoff=self._filterBadPulses)
if is_event_workspace(out_ws_name):
# Event workspace
message = "FilterBadPulses reduces number of events from %d to %d (under %s percent) " \
"of workspace %s." % (num_original_events, get_workspace(out_ws_name).getNumberEvents(),
str(self._filterBadPulses), out_ws_name)
self.log().information(message)
# END-IF (filter bad pulse)
return out_ws_name
def _getStrategy(self, filename):
"""
Get chunking strategy by calling mantid algorithm 'DetermineChunking'
:param filename:
:return: a list of dictionary. Each dictionary is a row in table workspace
"""
# Determine chunk strategy can search in archive.
# Therefore this will fail: assert os.path.exists(file_name), 'NeXus file %s does not exist.' % file_name
self.log().debug("[Fx116] Run file Name : %s,\t\tMax chunk size: %s" %
(filename, str(self._chunks)))
chunks = api.DetermineChunking(Filename=filename, MaxChunkSize=self._chunks)
strategy = []
for row in chunks:
strategy.append(row)
# For table with no rows
if len(strategy) == 0:
strategy.append({})
# delete chunks workspace
chunks = str(chunks)
mtd.remove(chunks)
return strategy
def __logChunkInfo(self, chunk):
keys = sorted(chunk.keys())
keys = [str(key) + "=" + str(chunk[key]) for key in keys]
self.log().information("Working on chunk [" + ", ".join(keys) + "]")
def checkInfoMatch(self, left, right):
if (left["frequency"].value is not None) and (right["frequency"].value is not None) \
and (abs(left["frequency"].value - right["frequency"].value)/left["frequency"].value > .05):
raise RuntimeError("Cannot add incompatible frequencies (%f!=%f)"
% (left["frequency"].value, right["frequency"].value))
if (left["wavelength"].value is not None) and (right["wavelength"].value is not None) \
and abs(left["wavelength"].value - right["wavelength"].value)/left["wavelength"].value > .05:
raise RuntimeError("Cannot add incompatible wavelengths (%f != %f)"
% (left["wavelength"].value, right["wavelength"].value))
#pylint: disable=too-many-arguments
def _focusAndSum(self, filenames, preserveEvents=True, final_name=None, absorptionWksp=''):
"""Load, sum, and focus data in chunks
Purpose:
Load, sum and focus data in chunks;
Requirements:
1. input run numbers are in numpy array or list
Guarantees:
The experimental runs are focused and summed together
@param run_number_list:
@param extension:
@param preserveEvents:
@param absorptionWksp: will be divided from the data at a per-pixel level
@return: string as the summed workspace's name
"""
if final_name is None:
final_name = getBasename(filenames[0])
# only pass in the characterizations if the values haven't already been determined
characterizations = self._charTable
if self._info is not None:
characterizations = ''
# put together a list of the other arguments
otherArgs = self._focusPos.copy()
# use the workspaces if they already exists, or pass the filenames down
# this assumes that AlignAndFocusPowderFromFiles will give the workspaces the canonical names
cal, grp, msk = [self._instrument + name for name in ['_cal', '_group', '_mask']]
if mtd.doesExist(cal) and mtd.doesExist(grp) and mtd.doesExist(msk):
otherArgs['CalibrationWorkspace'] = cal
otherArgs['GroupingWorkspace'] = grp
otherArgs['MaskWorkspace'] = msk
else:
otherArgs['CalFileName'] = self.calib
otherArgs['GroupFilename'] = self.getProperty("GroupingFile").value
api.AlignAndFocusPowderFromFiles(Filename=','.join(filenames),
OutputWorkspace=final_name,
AbsorptionWorkspace=absorptionWksp,
MaxChunkSize=self._chunks,
FilterBadPulses=self._filterBadPulses,
Characterizations=characterizations,
CacheDir=self._cache_dir,
Params=self._binning,
ResampleX=self._resampleX,
Dspacing=self._bin_in_dspace,
PreserveEvents=preserveEvents,
RemovePromptPulseWidth=self._removePromptPulseWidth,
CompressTolerance=self.COMPRESS_TOL_TOF,
LorentzCorrection=self._lorentz,
UnwrapRef=self._LRef,
LowResRef=self._DIFCref,
LowResSpectrumOffset=self._lowResTOFoffset,
CropWavelengthMin=self._wavelengthMin,
CropWavelengthMax=self._wavelengthMax,
FrequencyLogNames=self.getProperty("FrequencyLogNames").value,
WaveLengthLogNames=self.getProperty("WaveLengthLogNames").value,
ReductionProperties="__snspowderreduction",
**otherArgs)
#TODO make sure that this funny function is called
#self.checkInfoMatch(info, tempinfo)
# allow for not normalizing by current for this particular one
if self._normalisebycurrent:
api.NormaliseByCurrent(InputWorkspace=final_name,
OutputWorkspace=final_name,
RecalculatePCharge=True)
get_workspace(final_name).getRun()['gsas_monitor'] = 1
return final_name
#pylint: disable=too-many-arguments,too-many-locals,too-many-branches
def _focusChunks(self, filename, filter_wall=(0.,0.), # noqa
splitwksp=None, preserveEvents=True):
"""
Load, (optional) split and focus data in chunks
@param filename: integer for run number
@param filter_wall: Enabled if splitwksp is defined
@param splitwksp: SplittersWorkspace (if None then no split)
@param preserveEvents:
@return: a string as the returned workspace's name or a list of strings as the returned workspaces' names
in the case that split workspace is used.
"""
# generate the workspace name
self.log().information("_focusChunks(): run = %s" % (filename))
# get chunk strategy for parallel processing (MPI)
strategy = self._getStrategy(filename)
# determine event splitting by checking filterWall and number of output workspaces from _focusChunk
do_split_raw_wksp, num_out_wksp = self._determine_workspace_splitting(splitwksp, filter_wall)
# Set up the data structure to hold and control output workspaces
output_wksp_list = [None] * num_out_wksp
is_first_chunk_list = [True] * num_out_wksp
self.log().debug("F1141A: Number of workspace to process = %d" % num_out_wksp)
# reduce data by chunks
chunk_index = -1
base_name = getBasename(filename)
for chunk in strategy:
# progress on chunk index
chunk_index += 1
# Load chunk, i.e., partial data into Mantid
raw_ws_name_chunk = self._load_event_data(filename, filter_wall,
out_ws_name=None,
**chunk)
if self._info is None:
self._info = self._getinfo(raw_ws_name_chunk)
# Log information for current chunk
self.__logChunkInfo(chunk)
if is_event_workspace(raw_ws_name_chunk):
# Event workspace
self.log().debug("F1141C: There are %d events after data is loaded in workspace %s." % (
get_workspace(raw_ws_name_chunk).getNumberEvents(), raw_ws_name_chunk))
# Split the workspace if it is required
if do_split_raw_wksp is True:
output_ws_name_list = self._split_workspace(raw_ws_name_chunk, splitwksp.name())
else:
# Histogram data
output_ws_name_list = [raw_ws_name_chunk]
# ENDIF
# check
if num_out_wksp != len(output_ws_name_list):
self.log().warning('Projected number of output workspaces %d must be same as '
'that of real output workspaces %d.' % (num_out_wksp, len(output_ws_name_list)))
num_out_wksp = output_ws_name_list
# log
msg = "[Fx1142] Workspace of chunk %d is %d (vs. estimated %d). \n" % (
chunk_index, len(output_ws_name_list), num_out_wksp)
for iws in range(len(output_ws_name_list)):
ws = output_ws_name_list[iws]
msg += "%s\t\t" % (str(ws))
if iws %5 == 4:
msg += "\n"
self.log().debug(msg)
# Do align and focus
num_out_wksp = len(output_ws_name_list)
for split_index in range(num_out_wksp):
# Get workspace name
out_ws_name_chunk_split = output_ws_name_list[split_index]
# Align and focus
# focuspos = self._focusPos
self.log().notice('Align and focus workspace %s' % out_ws_name_chunk_split)
api.AlignAndFocusPowder(InputWorkspace=out_ws_name_chunk_split,
OutputWorkspace=out_ws_name_chunk_split,
CalFileName=self.calib,
GroupFilename=self.getProperty("GroupingFile").value,
Params=self._binning,
ResampleX=self._resampleX,
Dspacing=self._bin_in_dspace,
PreserveEvents=preserveEvents,
RemovePromptPulseWidth=self._removePromptPulseWidth,
CompressTolerance=self.COMPRESS_TOL_TOF,
LorentzCorrection=self._lorentz,
UnwrapRef=self._LRef,
LowResRef=self._DIFCref,
LowResSpectrumOffset=self._lowResTOFoffset,
CropWavelengthMin=self._wavelengthMin,
CropWavelengthMax=self._wavelengthMax,
ReductionProperties="__snspowderreduction",
**self._focusPos)
# logging (ignorable)
if is_event_workspace(out_ws_name_chunk_split):
self.log().information('After being aligned and focused, workspace %s: Number of events = %d '
'of chunk %d ' % (out_ws_name_chunk_split,
get_workspace(out_ws_name_chunk_split).getNumberEvents(),
chunk_index))
# END-FOR-Splits
# Merge among chunks
for split_index in range(num_out_wksp):
# determine the final workspace name
final_out_ws_name = base_name
if num_out_wksp > 1:
final_out_ws_name += '_%d' % split_index
if is_first_chunk_list[split_index] is True:
# Rename if it is the first chunk that is finished
self.log().debug("[F1145] Slot %d is renamed to %s" % (split_index, final_out_ws_name))
api.RenameWorkspace(InputWorkspace=output_ws_name_list[split_index],
OutputWorkspace=final_out_ws_name)
output_wksp_list[split_index] = final_out_ws_name
is_first_chunk_list[split_index] = False
else:
# Add this chunk of workspace to the final one
clear_rhs_ws = allEventWorkspaces(output_wksp_list[split_index], output_ws_name_list[split_index])
api.Plus(LHSWorkspace=output_wksp_list[split_index],
RHSWorkspace=output_ws_name_list[split_index],
OutputWorkspace=output_wksp_list[split_index],
ClearRHSWorkspace=clear_rhs_ws)