-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmescal.py
1162 lines (1060 loc) · 38.1 KB
/
mescal.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
import argparse
import atexit
from collections import namedtuple
import configparser
import logging
from os import cpu_count
from pathlib import Path
import sys
import matplotlib
import matplotlib.pyplot as plt
import pandas as pd
from source import paths
from source.calibrate import Calibrate
from source.calibrate import ImportedCalibration
from source.calibrate import PartialCalibration
from source.calibrate import PEAKS_PARAMS
from source.checks import check_results
from source.cli import elementsui as ui
from source.cli.beaupy.beaupy import prompt
from source.cli.beaupy.beaupy import select
from source.cli.beaupy.beaupy import select_multiple
from source.cli.cmd import Cmd
from source.detectors import supported_models
import source.errors as err
from source.eventlist import perchannel_counts
from source.eventlist import preprocess
from source.eventlist import timehist_all
from source.eventlist import timehist_quadch
from source.io import pandas_from_lv0d5
from source.io import read_lightout_report
from source.io import read_nlcorrection_fits
from source.io import read_sdd_calibration_report
from source.plot import histogram
from source.plot import mapcounts
from source.plot import mapenres
from source.plot import spectrum_xs
from source.plot import uncalibrated
from source.radsources import supported_sources
from source.radsources import supported_ssources
from source.radsources import supported_xsources
from source.utils import get_version
commandline_args_parser = argparse.ArgumentParser()
commandline_args_parser.add_argument(
"--filepath",
default=None,
help="input acquisition file in standard 0.5 fits format.\n"
"prompt user by default.",
)
commandline_args_parser.add_argument(
"--model",
default=None,
choices=supported_models(),
help="hermes flight model to calibrate.\n" "prompt user by default.",
)
commandline_args_parser.add_argument(
"--source",
default=None,
action="append",
choices=supported_sources(),
help="radioactive sources used for calibration.\n"
"prompt user by default.",
)
commandline_args_parser.add_argument(
"--adc",
choices=["LYRA-BE", "CAEN-DT5740"],
default="LYRA-BE",
help="select which adc configuration to use.\n" "defaults to LYRA-BE.",
)
commandline_args_parser.add_argument(
"--fmt",
default="xslx",
choices=["xslx", "csv", "fits"],
help="set output format for calibration tables.\n" "defaults to xslx.",
)
commandline_args_parser.add_argument(
"--nofilters",
default=False,
action="store_true",
help="disables all filters.",
)
commandline_args_parser.add_argument(
"--cache",
default=False,
action="store_true",
help="enables loading and saving from cache.",
)
@atexit.register
def end_logger():
"""
Kills loggers on shutdown.
"""
logging.shutdown()
INVALID_ENTRY = 0
class Mescal(Cmd):
"""
A script implementing calibration workflow and a shell loop.
"""
# fmt off
intro = "Type help or ? for a list of commands.\n"
prompt = "[cyan]\[mescalSH] "
spinner_message = "Working.."
unknown_command_message = (
"[red]Unknown command.[/]\n"
"[i]Type help or ? for a list of commands.[/i]\n"
)
invalid_command_message = "[red]Command unavailable.[/]\n"
invalid_channel_message = (
"[red]Invalid channel.[/]\n"
"[i]Channel ID must be in standard form "
"(e.g., d04, A30, B02).[/i]\n"
)
no_counts_message = "[red]No events observed for this channel.[/]\n"
invalid_limits_message = (
"[red]Invalid limits.[/]\n"
"[i]Entries must be two, different, "
"sorted integers (e.g., 19800 20100).[/i]\n"
)
invalid_table_message = (
"[red]Invalid table.[/]\n"
"[i]Make sure the table you are providing has the right columns and format.[/i]"
)
invalid_format_message = (
"[red]The file appears to be in wrong format.[/]\n"
"[i]The command `loadcal` expects .xslx table format.[/i]"
)
invalid_natural_message = (
"[red]Wrong number format.[/]\n"
"[i]Argument must be expressed as a positive integer (e.g. 100).[/i]"
)
invalid_slo_message = (
"[red]Wrong table content.[/]\n"
"[i]The light-output table provided does not match the payload map.[/i]"
)
# fmt on
def __init__(self):
console = ui.hello()
super().__init__(console)
self.commandline_args = commandline_args_parser.parse_args()
self.filepath = self.get_filepath()
self.model = self.get_model()
self.radsources = self.get_radsources()
self.start_logger()
self.config = self.unpack_configuration()
self.threads = self.check_system()
self.failed_tests = {}
self.calibrations = {}
self.calibration = None
self.data = None
self.waste = None
ui.logcal_rule(self.console)
with console.status("Initializing.."):
raw_data = self.fetch_data()
self.data, self.waste = preprocess(
raw_data,
model=self.model,
filter_spurious=self.config["filter_spurious"],
filter_retrigger=self.config["filter_retrigger"],
console=self.console,
)
with console.status("Analyzing data.."):
calibration = Calibrate(
self.model,
self.radsources,
configuration=self.config,
console=self.console,
nthreads=self.threads,
)
self.register(calibration)
self.calibration = calibration
self.calibration(self.data)
with console.status("Processing results.."):
self.export_essentials()
self.failed_tests = check_results(
self.calibration,
self.data,
self.waste,
self.config,
)
if self.failed_tests:
ui.warning_rule(self.console)
self.display_warning()
ui.shell_rule(self.console)
self.cmdloop()
def register(self, calibration):
label = "calib-{}".format(len(self.calibrations))
self.calibrations[label] = calibration
logging.info("added calibration {} to register.".format(label))
return calibration
def start_logger(self):
"""
Starts logger in default output folder and logs user command line arguments.
"""
logfile = paths.LOGFILE(self.filepath)
with open(logfile, "w") as f:
f.write(ui.logo())
len_logo = len(ui.logo().split("\n")[0])
version_message = "version " + get_version()
if len_logo > len(version_message) + 1:
f.write(
" " * (len_logo - len(version_message) + 1)
+ version_message
+ "\n\n"
)
else:
f.write(version_message + "\n\n")
# checks that logging was not used before creating the logfile.
assert len(logging.root.handlers) == 0
logging.basicConfig(
filename=logfile,
level=logging.INFO,
format="[%(funcName)s() @ %(filename)s (L%(lineno)s)] "
"%(levelname)s: %(message)s",
)
logging.info("user args = {}".format(self.commandline_args))
logging.info("logging calibration for file {}".format(self.filepath))
logging.info("selected model = {}".format(self.model))
logging.info("selected sources = {}".format(", ".join(self.radsources)))
return True
@staticmethod
def check_system():
"""
Perfoms system inspection to choose matplotlib backend
and threads number (max 4).
"""
if sys.platform.startswith("win") or sys.platform.startswith("linux"):
if "TkAgg" in matplotlib.rcsetup.all_backends:
matplotlib.use("TkAgg")
elif sys.platform.startswith("mac"):
if "MacOSX" in matplotlib.rcsetup.all_backends:
matplotlib.use("MacOSX")
systhreads = min(4, cpu_count())
logging.info("detected {} os".format(sys.platform))
logging.info(
"using matplotlib backend {}".format(matplotlib.get_backend())
)
logging.info("running over {} threads".format(systhreads))
return systhreads
def unpack_configuration(self):
"""
unpacks ini configuration file parameters into a dict.
"""
config = configparser.ConfigParser()
config.read(paths.CONFIGPATH)
general = config["general"]
adcitems = config[self.commandline_args.adc]
out = {
"filter_retrigger": (
0.0
if self.commandline_args.nofilters
else general.getfloat("filter_retrigger")
),
"filter_spurious": (
False
if self.commandline_args.nofilters
else general.getboolean("filter_spurious")
),
"xbinning": adcitems.getint("xbinning"),
"sbinning": adcitems.getint("sbinning"),
"xpeaks_mincounts": general.getint("xpeaks_mincounts"),
"gain_center": adcitems.getfloat("gain_center"),
"gain_sigma": adcitems.getfloat("gain_sigma"),
"offset_center": adcitems.getfloat("offset_center"),
"offset_sigma": adcitems.getfloat("offset_sigma"),
"lightout_center": adcitems.getfloat("lightout_center"),
"lightout_sigma": adcitems.getfloat("lightout_sigma"),
}
message = "config.ini parameters = " + str(out)[1:-1]
logging.info(message)
return out
# TODO: refactoring. caching should have its own function.
def fetch_data(self, use_cache=False):
"""
deals with data load and caching.
"""
self.console.log(":question_mark: Looking for data..")
cached = (
paths.CACHEDIR().joinpath(self.filepath.name).with_suffix(".pkl.gz")
)
if cached.is_file() and self.commandline_args.cache:
out = pd.read_pickle(cached)
self.console.log(
"[bold yellow]:yellow_circle: Data were loaded from cache."
)
else:
try:
out = pandas_from_lv0d5(self.filepath)
except KeyError:
self.console.print("\n" + self.invalid_table_message)
exit()
except OSError:
self.console.print("\n" + self.invalid_table_message)
exit()
self.console.log(":open_book: Data loaded.")
if self.commandline_args.cache:
# save data to cache
from pickle import DEFAULT_PROTOCOL
out.to_pickle(cached, protocol=DEFAULT_PROTOCOL)
self.console.log(":blue_book: Data saved to cache.")
return out
def get_filepath(self) -> Path:
message = (
"[italic]Which file are you calibrating?\n"
"[yellow]Hint: You can drag & drop.[/yellow]"
"[/italic]\n"
)
message_error = (
"[italic][red]The file you entered does not exists or appears to be in an unexpected format.[/red]\n"
"[italic][red]Make sure your file name terminates either in `.fits` or `.fit`.[/red]\n"
"Which file are you calibrating?\n"
"[yellow]Hint: You can drag & drop.[/yellow]\n"
"[/italic]\n"
)
if self.commandline_args.filepath is not None:
return Path(self.commandline_args.filepath)
filepath = prompt_user_on_filepath(
message,
message_error,
self.console,
supported_formats=[".fits", ".fit"],
)
if filepath is None:
self.console.print("So soon? Ciao :wave:!\n")
exit()
return filepath
def get_model(self) -> str:
def prompt_user_on_model():
# fmt: off
cursor_index = ([0] + [
i for i, d in enumerate(supported_models())
if d in self.filepath.name.lower()
])[-1]
# fmt: on
answer = select(
options=supported_models(),
cursor=":flying_saucer:",
cursor_index=cursor_index,
console=self.console,
intro="[italic]For which model?[/italic]\n\n",
legend="(Confirm with [bold]enter[/bold], exit with esc)",
)
return answer
if self.commandline_args.model is not None:
return self.commandline_args.model
model = prompt_user_on_model()
if model is None:
self.console.print("So soon? Ciao :wave:!\n")
exit()
return model
def get_radsources(self) -> list[str]:
message_x = (
"[italic]Which X radioactive sources you intend to calibrate?\n"
"[yellow]Hint: Pressing esc or selecting no source will cause mescal to skip "
"the calibration process. You will still be able to visualize data.[/yellow]\n"
"[/italic]\n"
)
message_s = (
"[italic]Which gamma radioactive sources you intend to calibrate?\n"
"[yellow]Hint: Pressing esc or selecting no source will cause mescal to skip "
"the calibration process. You will still be able to visualize data.[/yellow]\n"
"[/italic]\n"
)
if self.commandline_args.source is not None:
return self.commandline_args.source
xradsources = prompt_user_on_radsources(
supported_xsources(), message_x, self.console
)
if not xradsources:
return []
sradsources = prompt_user_on_radsources(
supported_ssources(), message_s, self.console
)
return xradsources + sradsources
def display_warning(self):
"""Tells user about channels for which calibration
could not be completed.
"""
if "flagged_channels" in self.failed_tests:
sublists = self.calibration.flagged.values()
num_flagged = len(
set([item for sublist in sublists for item in sublist])
)
num_channels = len(
[
ch
for quad, chs in self.calibration.channels.items()
for ch in chs
]
)
message = (
"[i][yellow]"
"I was unable to complete calibration for {} channels out of {}."
"[/yellow]\n"
"For more details, see the log file.".format(
num_flagged, num_channels
)
)
self.console.print(message)
if "too_many_filtered_events" in self.failed_tests:
message = (
"[i][yellow]"
"A significant fraction of the dataset was filtered away."
"[/yellow]\n"
"Check filter parameters in 'config.ini'."
)
self.console.print(message)
if "filter_retrigger_off" in self.failed_tests:
message = (
"[i][yellow]"
"Retrigger filter is off."
"[/yellow]\n"
"You can enable it through 'config.ini'."
)
self.console.print(message)
if "filter_spurious_off" in self.failed_tests:
message = (
"[i][yellow]"
"Spurious events filter is off."
"[/yellow]\n"
"You can enable it through 'config.ini'."
)
self.console.print(message)
if "time_outliers" in self.failed_tests:
message = (
"[i][yellow]"
"Found large outliers in your time data."
"[/yellow]\n"
"These events will not be displayed through 'timehist' command."
)
self.console.print(message)
return True
def export_essentials(self):
exporter = self.calibration.get_exporter(
self.filepath, self.commandline_args.fmt
)
if exporter.can__write_sdd_calibration_report:
exporter.write_sdd_calibration_report()
self.console.log(":blue_book: Wrote SDD calibration results.")
if exporter.can__write_energy_res_report:
exporter.write_energy_res_report()
self.console.log(":blue_book: Wrote energy resolution results.")
if exporter.can__draw_qlooks_sdd:
exporter.draw_qlooks_sdd()
self.console.log(":chart_increasing: Saved X fit quicklook plots.")
if exporter.can__write_lightoutput_report:
exporter.write_lightoutput_report()
self.console.log(":blue_book: Wrote light output results.")
if exporter.can__draw_qlook_scint:
exporter.draw_qlook_scint()
self.console.log(":chart_increasing: Saved light output plots.")
if exporter.can__draw_spectrum:
exporter.draw_spectrum()
self.console.log(
":chart_increasing: Saved calibrated spectra plots."
)
# shell prompt commands
def can_quit(self, arg):
return True
def do_quit(self, arg):
"""Quits mescal.
It's the only do-command to return True.
"""
self.console.print("Ciao! :wave:\n")
return True
def can_exit(self, arg):
return self.can_quit("")
def do_exit(self, arg):
"""
An alias for quit.
"""
return self.do_quit("")
def can_plotcal(self, arg):
if self.calibration.eventlist is not None:
return True
def do_plotcal(self, arg):
fig, axs = spectrum_xs(
self.calibration.eventlist,
self.calibration.xradsources(),
self.calibration.sradsources(),
)
plt.show(block=False)
return False
def can_plotraw(self, arg):
return True
def do_plotraw(self, arg):
"""Plots uncalibrated data from a channel."""
parsed_arg = parse_chns(arg)
if parsed_arg is INVALID_ENTRY:
self.console.print(self.invalid_channel_message)
return False
quad, ch = parsed_arg
fig, ax = uncalibrated(
self.calibration.xhistograms.bins,
self.calibration.xhistograms.counts[quad][ch],
self.calibration.shistograms.bins,
self.calibration.shistograms.counts[quad][ch],
)
ax.set_title("Uncalibrated plot channel {}{:02d}".format(quad, ch))
plt.show(block=False)
return False
def can_plotlc(self, arg):
if self.calibration.eventlist is not None:
return True
def do_plotlc(self, arg):
"""Plots lightcurve of calibrated events with optional binning,
expressed in units of millisecond.
Example usage: plotlc 100
if binning is not specified, a default binning of 100ms is assumed.
"""
parsed_arg = parse_natural_number(arg)
if parsed_arg is INVALID_ENTRY:
self.console.print(self.invalid_natural_message)
return False
elif parsed_arg is None:
binning = 0.1
else:
binning = parsed_arg / 1000
counts, bins = timehist_all(
self.calibration.eventlist,
binning,
"time_outliers" in self.failed_tests,
)
fig, ax = histogram(
counts,
bins[:-1],
)
ax.set_title(f"Lightcurve for calibrated events, binning {binning} s")
ax.set_xlabel("Time")
ax.set_ylabel("Counts")
plt.show(block=False)
return False
def can_timehist(self, arg):
return True
def do_timehist(self, arg):
"""Plots a histogram of counts in time for selected channel.
Example usage: `timehist D29`
You can call timehist with `all` argument to get a histogram of the sum
of the counts observed over all channels. Note that this will differ
from a lightcurve made with calibrated events, see `plotlc`."""
def plot_lightcurve_all_channels(binning):
counts, bins = timehist_all(
self.calibration.data,
binning,
"time_outliers" in self.failed_tests,
)
fig, ax = histogram(
counts,
bins[:-1],
)
ax.set_title(f"Lightcurve for all channels, binning {binning} s")
ax.set_xlabel("Time")
ax.set_ylabel("Counts")
plt.show(block=False)
def plot_lightcurve_single_channel(quad, ch, binning):
counts, bins = timehist_quadch(
self.calibration.data,
quad,
ch,
binning,
"time_outliers" in self.failed_tests,
)
fig, ax = histogram(counts, bins[:-1])
ax.set_title(f"Lightcurve for {quad}{ch:02d}, binning {binning} s")
ax.set_xlabel("Time")
ax.set_ylabel("Counts")
plt.show(block=False)
binning = 0.1
if arg == "all":
plot_lightcurve_all_channels(binning)
else:
parsed_arg = parse_chns(arg)
if parsed_arg is INVALID_ENTRY:
self.console.print(self.invalid_channel_message)
return False
quad, ch = parsed_arg
plot_lightcurve_single_channel(quad, ch, binning)
return False
def can_retry(self, arg):
if any(self.calibration.radsources) and not isinstance(
self.calibration, ImportedCalibration
):
return True
return False
def do_retry(self, arg):
"""Launches calibration again."""
with self.console.status(self.spinner_message):
ui.logcal_rule(self.console)
self.calibration._calibrate()
self.export_essentials()
ui.shell_rule(self.console)
return False
def can_setlim(self, arg):
if any(self.calibration.radsources) and not isinstance(
self.calibration, ImportedCalibration
):
return True
return False
def do_setlim(self, arg):
"""Reset channel X peaks position for user selected channels."""
parsed_arg = parse_chns(arg)
if parsed_arg is INVALID_ENTRY:
self.console.print(self.invalid_channel_message)
return False
quad, ch = parsed_arg
if (quad not in self.calibration.channels) or (
ch not in self.calibration.channels[quad]
):
self.console.print(self.no_counts_message)
return False
for source, decay in self.calibration.xradsources().items():
arg = input(source + ": ")
parsed_arg = parse_limits(arg)
if parsed_arg is INVALID_ENTRY:
self.console.print(self.invalid_limits_message)
return False
elif parsed_arg is None:
continue
else:
lim_lo, lim_hi = parsed_arg
label_lo, label_hi = PEAKS_PARAMS
self.calibration.xpeaks[quad].loc[ch, (source, label_lo)] = int(
lim_lo
)
self.calibration.xpeaks[quad].loc[ch, (source, label_hi)] = int(
lim_hi
)
for source, decay in self.calibration.sradsources().items():
arg = input(source + ": ")
parsed_arg = parse_limits(arg)
if parsed_arg is INVALID_ENTRY:
self.console.print(self.invalid_limits_message)
return False
elif parsed_arg is None:
continue
else:
lim_lo, lim_hi = parsed_arg
label_lo, label_hi = PEAKS_PARAMS
self.calibration.speaks[quad].loc[ch, (source, label_lo)] = int(
lim_lo
)
self.calibration.speaks[quad].loc[ch, (source, label_hi)] = int(
lim_hi
)
message = "reset fit limits for channel {}{:02d}".format(quad, ch)
logging.info(message)
return False
def can_mapcount(self, arg):
if self.calibration.data is not None:
return True
return False
def do_mapcount(self, arg):
"""Plots a map of counts per-channel."""
counts = self.calibration.count()
fig, ax = mapcounts(
counts,
self.calibration.detector.map,
title="Per-channel events count map",
)
plt.show(block=False)
return False
def can_mapbad(self, arg):
if self.waste is not None and not self.waste.empty:
return True
return False
def do_mapbad(self, arg):
"""Plots a map of counts per-channel from filtered data."""
counts = perchannel_counts(
self.waste, self.calibration.channels, key="all"
)
fig, ax = mapcounts(
counts,
self.calibration.detector.map,
cmap="binary_u",
title="Per-channel filtered events count map",
)
plt.show(block=False)
return False
def can_mapres(self, arg):
if (
self.calibration.xradsources().keys()
and self.calibration.resolution
):
return True
return False
def do_mapres(self, arg):
"""Saves a map of channels' energy resolution."""
decays = self.calibration.xradsources()
source = sorted(decays, key=lambda source: decays[source].energy)[0]
fig, ax = mapenres(
source,
self.calibration.resolution,
self.calibration.detector.map,
)
plt.show(block=False)
return False
def can_corrnl(self, arg):
if self.calibration.eventlist is not None:
return True
def do_corrnl(self, arg):
message_hello = (
"[italic]Enter path for non-linearity correction file.\n"
"[yellow]Hint: You can drag & drop.[/yellow]"
"[/italic]\n"
)
message_error = (
"[italic][red]The file you entered does not exists.[/red]\n"
"[yellow]Hint: You can drag & drop.[/yellow]\n"
"[/italic]\n"
)
answer = prompt_user_on_filepath(
message_hello, message_error, self.console
)
if answer is None:
return False
try:
nl_correction = read_nlcorrection_fits(answer)
except err.WrongTableError:
self.console.print(self.invalid_table_message)
return False
except err.FormatNotSupportedError:
self.console.print(self.invalid_format_message)
return False
self.calibration.apply_nlcorrection(nl_correction)
def can_loadcal(self, arg):
return True
def do_loadcal(self, arg):
"""Loads and existing calibration."""
message_sdd = (
"[italic]Enter path for sdd calibration file.\n"
"[yellow]Hint: You can drag & drop.[/yellow]"
"[/italic]\n"
)
message_lout = (
"[italic]Enter path for light output calibration file.\n"
"[yellow]Hint: You can drag & drop.\n"
"Hint: Pressing esc you will be given an option to try S-calibrate anew.[/yellow]"
"[/italic]\n"
)
message_error = (
"[italic][red]The file you entered does not exists.[/red]\n"
"Which file are you calibrating?\n"
"[yellow]Hint: You can drag & drop.[/yellow]\n"
"[/italic]\n"
)
message_partial = (
"[italic][yellow]Shall I try to calibrate any of these gamma sources?[/yellow]\n"
"[/italic]\n"
)
# we ask the user to tell us where the SDD calibration file is.
answer = prompt_user_on_filepath(
message_sdd, message_error, self.console
)
if answer is None:
return False
try:
sddcal = read_sdd_calibration_report(answer)
except err.WrongTableError:
self.console.print(self.invalid_table_message)
return False
except err.FormatNotSupportedError:
self.console.print(self.invalid_format_message)
return False
# now we ask where the scintillator calibration file is
answer = prompt_user_on_filepath(
message_lout, message_error, self.console
)
if answer is not None:
try:
lightouts = read_lightout_report(answer)
except err.WrongTableError:
self.console.print(self.invalid_table_message)
return False
except err.FormatNotSupportedError:
self.console.print(self.invalid_format_message)
return False
# after making sure the file is there and resembles what we expect. we
# calibrate starting from the values in the provided files.
with self.console.status(self.spinner_message):
ui.logcal_rule(self.console)
try:
newcal = ImportedCalibration(
self.model,
self.config,
sdd_calibration=sddcal,
lightoutput=lightouts,
console=self.console,
nthreads=self.threads,
)
self.register(newcal)
self.calibration = newcal
self.calibration(self.data)
except ValueError:
self.console.print(self.invalid_format_message)
except err.WrongTableError:
self.console.print(self.invalid_slo_message)
finally:
ui.shell_rule(self.console)
else:
# if the user does not provide a file we ask him if we should attempt calibrating from scratches
sradsources = prompt_user_on_radsources(
supported_ssources(), message_partial, self.console
)
with self.console.status(self.spinner_message):
ui.logcal_rule(self.console)
try:
newcal = PartialCalibration(
self.model,
self.config,
ssources=sradsources,
sdd_calibration=sddcal,
console=self.console,
nthreads=self.threads,
)
self.register(newcal)
self.calibration = newcal
self.calibration(self.data)
self.export_essentials()
except ValueError:
self.console.print(self.invalid_format_message)
except err.WrongTableError:
self.console.print(self.invalid_slo_message)
finally:
ui.shell_rule(self.console)
return False
def can_swapcal(self, arg):
if self.calibrations:
return True
return False
def do_swapcal(self, arg):
calib_label = select(
options=list(self.calibrations.keys()),
cursor=":flying_saucer:",
console=self.console,
intro="[italic]Select one calibration.[/italic]\n\n",
)
if calib_label is None:
return False
self.calibration = self.calibrations[calib_label]
return False
def can_export(self, arg):
return True
def do_export(self, arg):
"""Prompts user on optional data product exports."""
exporter = self.calibration.get_exporter(
self.filepath, self.commandline_args.fmt
)
Option = namedtuple(
"Option",
[
"label",
"commands",
"conditions",
"ticked",
],
)
all_options = [
Option(
"uncalibrated plots",
[exporter.draw_rawspectra],
[exporter.can__draw_rawspectra],
True,
),
Option(
"diagnostic plots",
[
exporter.draw_xdiagnostic,
exporter.draw_sdiagnostics,
],
[
exporter.can__draw_xdiagnostic,
exporter.can__draw_sdiagnostics,
],
True,
),
Option(
"linearity plots",
[exporter.draw_linearity],
[exporter.can__draw_linearity],
False,
),
Option(
"spectra plots per channel",
[
exporter.draw_sspectra,
exporter.draw_xspectra,
],
[
exporter.can__draw_sspectra,
exporter.can__draw_xspectra,
],
False,
),
Option(
"timehist per channel",
[
(
exporter.draw_timehists_neglect_outliers
if "time_outliers" in self.failed_tests
else exporter.draw_timehists
),
],
[
(
exporter.can__draw_timehists_neglect_outliers
if "time_outliers" in self.failed_tests
else exporter.can__draw_timehists
),
],
False,
),
Option(
"maps",
[
exporter.draw_map_counts,
exporter.draw_map_resolution,