-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterface.py
1152 lines (923 loc) · 42.2 KB
/
interface.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 astropy.units as u
import matplotlib.pyplot as plt
import numpy as np
import os
import parse
import pickle
from astropy.io import fits
from collections import OrderedDict, namedtuple
from dataclasses import dataclass, field
import xspec
from .config.configuration import XSPECConfiguration
from .custom_models import add_all_custom_models
from .plotter import compute_durbin_watson_statistic
# TODO: Make the interface class use the ModelPlotter class?
add_all_custom_models()
CONSTANT_EXPRESSION_NO_PAREN = '{outer}constant*{inner}'
CONSTANT_EXPRESSION_PAREN = '{outer}constant*({inner})'
PARAMETER_LINK_FORMAT = '= {model_name}:p{param_num}'
EXPRESSIONS = dict(
single = 'const * vapec',
double = 'const * (vapec + vapec)',
broken = 'const * (bknpower + vapec)'
)
MODEL_COMPONENT_MAPPING = dict(
diagnostic = dict(
EXPOSURE = ('exposure' , u.s),
STATISTIC = ('statistic' , u.Unit()),
DOF = ('dof' , u.Unit())
),
constant = dict(
factor = ('factor' , u.Unit())
),
vapec = dict(
kT = ('t' , u.keV),
norm = ('em' , u.Unit())
),
bknpower = dict(
PhoIndx1 = ('lower_index' , u.Unit()),
BreakE = ('break_energy' , u.keV),
PhoIndx2 = ('index' , u.Unit()),
norm = ('specnorm' , u.ph/u.keV/(u.cm**2)/u.s)
),
expmodgauss = dict( # TODO: FIGURE OUT UNITS!!!
norm = ('expmodgauss_norm', u.Unit()),
lam = ('lambda' , u.ct / u.keV),
mu = ('mu' , u.keV),
sigma = ('sigma' , u.keV),
)
)
# TODO: Combine with the dictionary above?
# TODO: This norm parameter conversion is specfically for Earth-based observers... Fix this.
PARAMETER_CONVERSIONS = dict(
vapec = dict(
kT = 11.6045 * u.MK / u.keV,
norm = u.cm**-3 / 3.5557e-42
)
)
COMPONENT_PARAMETER_MAPPING = dict(
constant = dict(
factor = ('factor' , u.Unit())
),
vapec = dict(
kT = ('temperature' , u.keV),
norm = ('emission_measure', u.Unit())
),
bknpower = dict(
norm = ('powerlaw_norm' , u.ph/u.keV/(u.cm**2)/u.s),
BreakE = ('break_energy' , u.keV),
PhoIndx1 = ('lower_index' , u.Unit()),
PhoIndx2 = ('upper_index' , u.Unit()),
),
expmodgauss = dict( # TODO: FIGURE OUT UNITS!!!
norm = ('expmodgauss_norm', u.Unit()),
lam = ('lambda' , u.ct / u.keV),
mu = ('mu' , u.keV),
sigma = ('sigma' , u.keV),
)
)
ModelResult = namedtuple(
'ModelResult',
[
'energy_edges',
'energy',
'energy_err',
'data',
'data_err',
'model',
'components'
]
)
@dataclass
class config:
# abundance_file: str = '/home/reed/feld92a_coronal0.txt'
debug: bool = True
CONFIG = config()
# TODO: Put this in a different file. histogram_tools.py?
def compute_energy_edges(energy: np.ndarray, energy_err: np.ndarray) -> np.ndarray:
return np.append(
energy - energy_err,
[energy[-1] + energy_err[-1]]
)
def gather_model_data(model_name: str, data_group: int) -> ModelResult:
# Configure the PlotManager so we can retrieve model and component spectra.
xspec.Plot.add = True
xspec.Plot.background = False
xspec.Plot.xAxis = "keV"
xspec.Plot("data")
energy = np.array(xspec.Plot.x(data_group)) * u.keV
energy_err = np.array(xspec.Plot.xErr(data_group)) * u.keV
energy_edges = compute_energy_edges(energy, energy_err)
result = ModelResult(
energy_edges = energy_edges,
energy = energy,
energy_err = energy_err,
data = np.array(xspec.Plot.y(data_group)) * u.ct/u.s/u.keV,
data_err = np.array(xspec.Plot.yErr(data_group)) * u.ct/u.s/u.keV,
model = np.array(xspec.Plot.model(data_group)) * u.ct/u.s/u.keV,
components = {}
)
# Add components.
component_names = []
for component_name in xspec.AllModels(data_group, model_name).componentNames:
if 'constant' not in component_name:
component_names.append(component_name)
print('result component names:', component_names)
if len(component_names) > 1:
for component_num, component_name in enumerate(component_names, start=1):
print('adding component to result:', component_name)
comp = xspec.Plot.addComp(component_num, data_group)
result.components[component_name] = np.array(comp) * u.ct/u.s/u.keV
return result
class ArchivedParameter():
def __init__(
self,
parameter: xspec.Parameter,
label: str,
unit: u.Unit
):
self.name = parameter.name
self.values = parameter.values
self.error = parameter.error
self.link = parameter.link
self.frozen = parameter.frozen
self.label = label
self.unit = unit
@property
def quantity(self) -> u.Quantity:
return self.value * self.unit
@property
def lower(self) -> u.Quantity:
return self.error[0] * self.unit
@property
def upper(self) -> u.Quantity:
return self.error[1] * self.unit
@property
def value(self) -> float:
return self.values[0]
@property
def step(self) -> float:
return self.values[1]
@property
def hard_min(self) -> float:
return self.values[2]
@property
def soft_min(self) -> float:
return self.values[3]
@property
def soft_max(self) -> float:
return self.values[4]
@property
def hard_max(self) -> float:
return self.values[5]
class ArchivedComponent():
def __init__(self, component: xspec.Component):
self.name = component.name
self.parameter_names = component.parameterNames
self.parameters = {}
for parameter_name in self.parameter_names:
component_parameter = component.__dict__[parameter_name]
mapping = COMPONENT_PARAMETER_MAPPING[self.base_name]
if parameter_name in mapping:
label, unit = mapping[parameter_name]
else:
label = parameter_name
unit = parameter.unit
print(f'Warning: parameter {parameter_name} not in COMPONENT_PARAMETER_MAPPING.')
parameter = ArchivedParameter(
component_parameter,
label,
unit
)
self.parameters[parameter_name] = parameter
setattr(self, parameter_name, parameter)
@property
def base_name(self) -> str:
"""
The name of the component without any indices.
"""
return (self.name).split('_')[0]
@property
def free_parameters(self) -> list[ArchivedParameter]:
parameters = []
for parameter_name in self.parameters:
parameter = self.parameters[parameter_name]
if not parameter.link and not parameter.frozen:
parameters.append(parameter)
return parameters
# TODO: Add the model components and full model to this. (idr what this means)
class ArchivedModel():
def __init__(
self,
label: str,
source: int,
data_group: int,
model: xspec.Model,
data_file: str = None,
out_dir: str = None,
):
self.label = label
self.source = source
self.data_group = data_group
self.name = model.name
self.expression = model.expression
self.statistic = xspec.AllData(data_group).statistic
self.data_file = data_file
self.out_dir = out_dir
self._archive_components(model)
self._archive_response()
self._archive_arrays()
@property
def free_parameters(self) -> dict[str, list[ArchivedParameter]]:
parameters = {}
for component_name in self.components:
component = self.components[component_name]
parameters[component] = component.free_parameters
return parameters
@property
def free_response_parameters(self) -> list[ArchivedParameter]:
rparameters = []
for rparameter_name in self.response_parameters:
rparameter = self.response_parameters[rparameter_name]
if not rparameter.link and not rparameter.frozen:
rparameters.append(rparameter)
return rparameters
@property
def fit_statistic_string(self) -> str:
return f'{self.label} CSTAT: {self.statistic:0.1f} ({self.bins} bins)'
@property
def parameter_string(self) -> str:
base_component_names = [c.base_name for c in self.components.values()]
unique = np.unique(base_component_names, return_counts=True)
unique_components = {u:{'uses':0, 'counts':c} for u,c in zip(unique[0], unique[1])}
full_string = ''
component_parameters = self.free_parameters
for component, parameters in component_parameters.items():
postfix = ''
if component.base_name in unique_components:
counts = unique_components[component.base_name]['counts']
if counts > 1:
unique_components[component.base_name]['uses'] += 1
postfix = rf'$_{unique_components[component.base_name]["uses"]}$'
for parameter in parameters:
value = parameter.quantity
errors = np.array([*parameter.error[0:2]]) * parameter.unit
errors -= value
if component.base_name in PARAMETER_CONVERSIONS:
conversions = PARAMETER_CONVERSIONS[component.base_name]
if parameter.name in conversions:
value *= conversions[parameter.name]
errors *= conversions[parameter.name]
# Scientific notation is used based on order of magnitude.
if value.value >= 1000:
str_format = '{:0.{prec}E}'
else:
str_format = '{:0.{prec}f}'
# Determine prec, which represents the number of decimal places.
mantissa = errors[1].value % 1
prec = 1
if mantissa != 0:
while (mantissa * 10 ** prec) < 1 and prec < 4:
prec += 1
value_str = str_format.format(value.value, prec=prec)
error_low = str_format.format(errors[0].value, prec=prec)
error_high = str_format.format(errors[1].value, prec=prec)
error_str = rf'$_{{{error_low}}}^{{+{error_high}}}$'
mapping = MODEL_COMPONENT_MAPPING[component.base_name]
param_name = mapping[parameter.name][0]
unit_str = f'[{value.unit}]'
if value.unit == u.Unit():
unit_str = ''
full_string += f'{param_name.upper()+postfix}: {value_str}{error_str} {unit_str}\n'
return full_string
@property
def response_parameter_string(self) -> str:
full_string = ''
if self.response_parameters:
# if not self.gain_slope.frozen and not self.gain_slope.link:
if not self.gain_slope.link:
full_string += f'GAIN SLOPE: {self.gain_slope.value:0.3f}'
if not self.gain_offset.frozen and not self.gain_offset.link:
full_string += f'\nGAIN OFFSET: {self.gain_offset.value:0.3f}'
return full_string
def _archive_components(self, model: xspec.Model):
"""
Archives the model components and their parameters.
"""
component_names = np.array([c.split('_')[0] for c in model.componentNames])
names, counts = np.unique(component_names, return_counts=True)
occurences = {n:{'counts':c, 'uses':0} for n, c in zip(names, counts)}
self.components = {}
for full_component_name in model.componentNames:
component = model.__dict__[full_component_name]
component = ArchivedComponent(model.__dict__[full_component_name])
component_name = full_component_name.split('_')[0]
if occurences[component_name]['counts'] > 1:
occurences[component_name]["uses"] += 1
component_name = f'{component_name}{occurences[component_name]["uses"]}'
self.components[component_name] = component
setattr(self, component_name, component)
def _archive_response(self):
"""
Archives the response parameters, if active.
"""
self.response_parameters = {}
gain = xspec.AllData(self.data_group).multiresponse[self.source-1].gain
if gain.isOn:
for parameter_name in gain.parameterNames:
new_parameter_name = f'gain_{parameter_name}'
parameter = ArchivedParameter(
gain.__dict__[parameter_name],
new_parameter_name,
u.Unit()
)
self.response_parameters[parameter_name] = parameter
setattr(self, new_parameter_name, parameter)
def _archive_arrays(self):
"""
Archives the data arrays, i.e. the data for plotting.
"""
self.arrays = gather_model_data(self.name, self.data_group)
self.bins = int(len(self.arrays.energy))
def _component_is_active(self, component: xspec.Component) -> bool:
is_active = False
parameter_names = component.parameterNames[:]
while not is_active and parameter_names:
name = parameter_names.pop()
parameter = component.__dict__[name]
is_active = not parameter.frozen
return is_active
@dataclass
class Archive():
instruments: dict[str, OrderedDict[str, ArchivedModel]] = field(default_factory=dict)
def last_instrument_model(self, instrument: str) -> ArchivedModel:
last = next(reversed(self.instruments[instrument]))
return self.instruments[instrument][last]
def add_model(self, instrument: str, model: ArchivedModel):
if instrument not in self.instruments:
self.instruments[instrument] = OrderedDict()
if model.name not in self.instruments[instrument]:
self.instruments[instrument][model.name] = model
setattr(self.instruments[instrument], model.name, model) # This is the main reason I want this class
else:
print(f'WARNING: model \'{model.name}\' already in Archive for instrument {instrument}. Not doing anything.')
def save(self, pickle_path: str):
"""
Saves self to the provided pickle path. This is intended to be used
in conjunction with the 'load' class method.
"""
with open(pickle_path, 'wb') as outfile:
pickle.dump(self, outfile, 2)
@classmethod
def load(cls, pickle_path: str):
"""
Creates an instance of the Archive class from the provided pickle.
"""
with open(pickle_path, 'rb') as infile:
return pickle.load(infile)
def make_model_string(self, model: str) -> str:
param_strs, fit_stat_strs, response_strs = [], [], []
for models in self.instruments.values():
if model not in models:
continue
archived_model = models[model]
param_strs.append(archived_model.parameter_string)
fit_stat_strs.append(archived_model.fit_statistic_string)
response_str = archived_model.response_parameter_string
if response_str:
response_strs.append(response_str)
full_str = ''.join(param_strs)
full_str += '\n'.join(fit_stat_strs) + '\n'
full_str += '\n'.join(response_strs)
return full_str
def make_multimodel_string(self, models: list[str]) -> str:
full_str = ''
for model in models:
full_str += f'{model.upper()}:\n{self.make_model_string(model)}\n'
return full_str
@dataclass
class Instrument:
name: str
signal_file: str
signal_data_group: int
signal_source: int
pileup_file: str = None
pileup_data_group: int = None
pileup_source: int = None
# TODO: Better way of getting these keywords?
@property
def signal_response_file(self) -> str:
with fits.open(self.signal_file) as hdu:
respfile = hdu[1].header['RESPFILE']
return respfile
@property
def pileup_response_file(self) -> str | None:
if self.pileup_file is not None:
with fits.open(self.pileup_file) as hdu:
respfile = hdu[1].header['RESPFILE']
return respfile
@property
def signal_spectrum(self) -> xspec.Spectrum:
return xspec.AllData(self.signal_data_group)
@property
def pileup_spectrum(self) -> xspec.Spectrum:
if self.pileup_data_group is not None:
return xspec.AllData(self.pileup_data_group)
@property
def pileup_model_name(self) -> str | None:
if self.pileup_file is not None:
return f'pileup{self.name}'.replace(' ', '')
def get_signal_model(self, model_name: str) -> xspec.Model:
return xspec.AllModels(self.signal_data_group, model_name)
def get_pileup_model(self) -> xspec.Model | None:
if self.pileup_file is not None:
return xspec.AllModels(self.pileup_data_group, self.pileup_model_name)
class XSPECInterface:
"""
This interface has been tested with NuSTAR inputs, but MIGHT work
for other instruments provided that the input files obey the OGIP
standard.
"""
def __init__(self):
self.archive = Archive()
self.instruments = OrderedDict()
self.signal_source = 1
self.signal_groups = []
self.pileup_sources = OrderedDict()
self.pileup_groups = []
self.pileup_expression = ''
@property
def num_groups(self) -> int:
return len(self.signal_groups) + len(self.pileup_groups)
def add_instrument(
self,
name: str,
signal_file: str,
pileup_file: str = None
):
"""
TODO: if we wanted to allow separate pileup models for each
instrument, we would need to set different signal sources
for each instrument. Right now, this cannot be done.
"""
group_num = self.num_groups + 1
self.signal_groups.append(group_num)
signal_kwargs = dict(
signal_file = signal_file,
signal_data_group = group_num,
signal_source = 1
)
pileup_kwargs = {}
if pileup_file is not None:
group_num = self.num_groups + 1
self.pileup_sources[len(self.pileup_sources)+2] = name
self.pileup_groups.append(group_num)
pileup_kwargs = dict(
pileup_file = pileup_file,
pileup_data_group = group_num,
pileup_source = len(self.pileup_sources)+1
)
self.instruments[name] = Instrument(name, **signal_kwargs, **pileup_kwargs)
print('Instrument:', name)
print('\t', signal_kwargs)
print('\t', pileup_kwargs)
def _configure_responses(self):
# Check if ANY instruments have pileup files.
# If there's at least one, then configure the multiresponse
# for each source for each Spectrum object.
pileup = False
for instrument in self.instruments.values():
if instrument.pileup_file is not None:
pileup = True
break
if pileup:
for instrument in self.instruments.values():
signal_spectrum = instrument.signal_spectrum
signal_spectrum.multiresponse[0] = instrument.signal_response_file
for source_num in range(1, len(self.pileup_sources) + 1):
signal_spectrum.multiresponse[source_num] = 'none'
print(f'signal_spectrum.multiresponse[{source_num}] = "none"')
pileup_spectrum = instrument.pileup_spectrum
if pileup_spectrum is not None:
for source_num in range(0, len(self.pileup_sources) + 1):
if source_num != instrument.pileup_source - 1:
pileup_spectrum.multiresponse[source_num] = 'none'
print(f'pileup_spectrum.multiresponse[{source_num}] = "none"')
else:
pileup_spectrum.multiresponse[source_num] = instrument.pileup_response_file
print(f'pileup_spectrum.multiresponse[{source_num}] = {instrument.pileup_response_file}')
def clear_data(self):
xspec.AllData.clear()
self.signal_groups = []
self.pileup_groups = []
self.pileup_sources = OrderedDict()
def read_data(self, data_dir: str):
"""
data_dir should contain all of the relevant PHA files and
corresponding RMF files and ARF files.
XSPEC assumes that the data files referenced within FITS headers (e.g.
the RESPFILE for the RMF) are contained within the directory
specified within the header. If there is no directory specified, i.e.
only the file name is specified, then XSPEC will search the directory
from which XSPEC is executed.
"""
data_str = ''
for instrument in self.instruments.values():
data_str += f'{instrument.signal_data_group}:{instrument.signal_data_group} {instrument.signal_file} '
if instrument.pileup_file is not None:
data_str += f'{instrument.pileup_data_group}:{instrument.pileup_data_group} {instrument.pileup_file} '
orig_dir = os.getcwd()
os.chdir(data_dir)
print(data_str)
xspec.AllData(data_str)
self._configure_responses()
os.chdir(orig_dir)
def archive_previous(self) -> OrderedDict[str, dict[str, ArchivedModel]]:
"""
Archives the currently loaded Model.
TODO: Remove return?
TODO: There's a bug where, if archive_previous is called multiple times, the same model get applied to all instruments.
"""
newly_archived = OrderedDict()
if not hasattr(self, 'current_model') or not hasattr(self, 'results'):
return newly_archived
for instrument in self.instruments.values():
try:
instrument.get_signal_model(self.current_model)
except Exception as e:
continue
archived_signal_model = ArchivedModel(
instrument.name,
instrument.signal_source,
instrument.signal_data_group,
instrument.get_signal_model(self.current_model),
instrument.signal_file,
self.out_dir
)
self.archive.add_model(instrument.name, archived_signal_model)
newly_archived[instrument.name] = {'signal': archived_signal_model}
if instrument.pileup_file is not None:
archived_pileup_model = ArchivedModel(
instrument.name,
instrument.pileup_source,
instrument.pileup_data_group,
instrument.get_pileup_model(),
instrument.pileup_file,
self.out_dir
)
self.archive.add_model(instrument.name, archived_pileup_model)
newly_archived[instrument.name] = {'pileup': archived_pileup_model}
print('newly_archived:', newly_archived)
if CONFIG.debug:
for instrument, models in newly_archived.items():
for name, model in models.items():
print(f'Archived model {instrument} {model.name}, expression {model.expression}')
for component_name, component in model.components.items():
print(f'\tComponent {component_name}')
for parameter_name, parameter in component.parameters.items():
print(f'\t\tParameter {parameter_name}')
print(f'\t\t\tValues: {parameter.values}')
print(f'\t\t\tFrozen: {parameter.frozen}')
return newly_archived
def _configure_new_expression(self, new: str, old: str) -> str:
if 'const*' in new:
new = new.replace('const*', 'constant*')
if old:
if 'constant' in old:
old = f' {old}' # Prefix space to work with formats
p = parse.parse(CONSTANT_EXPRESSION_PAREN, old)
if p is None:
p = parse.parse(CONSTANT_EXPRESSION_NO_PAREN, old)
p.named['inner'] = f'{p.named["inner"]} + {new}'
new = CONSTANT_EXPRESSION_PAREN.format(**p.named)
else:
new = f'{old} + {new}'
if self.pileup_expression:
new = f'{self.pileup_expression} + {new}'
return new
def set_pileup_model(self, expression: str) -> list[xspec.Model]:
"""
Applies the given expression to ALL instrument pileup models.
Currently, individual instrument pileup models cannot be set
due to how the sources are handled.
"""
models = []
self.pileup_expression = expression
for source, instrument_name in self.pileup_sources.items():
instrument = self.instruments[instrument_name]
model = xspec.Model(expression, instrument.pileup_model_name, source)
models.append(model)
return models
def _set_pileup_links(self):
"""
Links the parameters signal's pileup model's component(s) parameters
to the corresponding parameters of the pileup model.
"""
no_pileup_instruments = []
for instrument in self.instruments.values():
signal_model = instrument.get_signal_model(self.current_model)
pileup_model = instrument.get_pileup_model()
if pileup_model is not None:
ref_model = pileup_model
for component_name in pileup_model.componentNames:
component = pileup_model.__dict__[component_name]
for parameter_name in component.parameterNames:
parameter = component.__dict__[parameter_name]
signal_model.__dict__[component_name].__dict__[parameter_name].link = parameter
else:
no_pileup_instruments.append(instrument)
# Set all signal parameters corresponding to the pileup model to zero.
# TODO: See if we can do this better.
for instrument in no_pileup_instruments:
signal_model = instrument.get_signal_model(self.current_model)
pileup_component_names = ref_model.componentNames
for component_name in pileup_component_names:
component = signal_model.__dict__[component_name]
for parameter_name in component.parameterNames:
parameter = component.__dict__[parameter_name]
parameter.values = '0 0 0 0 0 0'
parameter.frozen = True
def add_component(
self,
model_name: str,
expression: str,
parameter_limits_file: str,
out_dir: str,
freeze_previous: bool = False,
tie_data_groups: bool = True,
) -> xspec.Model:
self.out_dir = out_dir
if not os.path.exists(self.out_dir):
os.makedirs(self.out_dir, exist_ok=True)
# Setup log file. Each model name combination gets its own log.
xspec.Xset.chatter = 0
self.logfile = xspec.Xset.openLog(f'{self.out_dir}/model_{model_name}.log')
self.archive_previous()
self.current_model = model_name
# Get the most recent model(s) archived.
# It is done here so that the user can call 'archive_previous()'
# whenever they want, but 'add_component()' is not dependent on
# the user doing so.
archived_models = {}
if self.archive.instruments:
for k in (self.archive.instruments).keys():
model_name = next(reversed(self.archive.instruments[k]))
archived_models[k] = self.archive.instruments[k][model_name]
print('Recently archived model:', k, model_name)
old_expression = archived_models[k].expression
else:
old_expression = ''
# Setup the new model
new_expression = self._configure_new_expression(expression, old_expression)
print(f'Adding new component {self.current_model} with expression {expression}')
print('Full expression is now', new_expression)
model = xspec.Model(new_expression, self.current_model, self.signal_source)
print(model.name)
# TODO: Create some way of setting pileup parameter limits?
print('parameter limits file:', parameter_limits_file)
self._set_parameter_limits(parameter_limits_file, tie_data_groups)
if self.pileup_expression:
self._set_pileup_links()
if archived_models:
archived_models_it = iter(archived_models)
for group_num in self.signal_groups:
current_model = xspec.AllModels(group_num, self.current_model)
archived_model = archived_models[next(archived_models_it)]
for old_component in archived_model.components.values():
if old_component.name == 'constant' and group_num == 1:
continue
new_component = current_model.__dict__[old_component.name]
print(f'reading parameters from component {old_component.name} of archived model', archived_model.name)
for parameter_name in new_component.parameterNames:
print(parameter_name)
if parameter_name in old_component.parameters:
old_parameter = old_component.parameters[parameter_name]
print(parameter_name, old_parameter.link)
parameter = new_component.__dict__[parameter_name]
if old_parameter.link:
print(f'old parameter {parameter_name} in old model is linked. skipping')
continue
parameter.values = old_parameter.values
# parameter.error = old_parameter.error # TODO: Figure out how to set this.
parameter.frozen = old_parameter.frozen or freeze_previous
print(f'parameter {parameter_name} frozen:', old_parameter.frozen)
print('freeze previous:', freeze_previous)
print(f'applying {parameter_name} values to component {new_component.name} of new model {model.name}. frozen: {parameter.frozen}, {parameter.values}')
first_group = self.signal_groups[0]
if 'constant' in xspec.AllModels(first_group, self.current_model).componentNames:
# Freeze the factor for spectrum 1.
xspec.AllModels(first_group, self.current_model).constant.factor.link = ''
xspec.AllModels(first_group, self.current_model).constant.factor.frozen = True
# Free the offset factor between the groups (spectra).
for group_num in self.signal_groups:
if group_num != first_group:
xspec.AllModels(group_num, self.current_model).constant.factor = 1.0
xspec.AllModels(group_num, self.current_model).constant.factor.link = ''
xspec.AllModels(group_num, self.current_model).constant.factor.frozen = False
return model
# TODO: Implement this.
def set_signal_parameter_limits():
return
def set_pileup_parameter_limits():
return
def _set_parameter_limits(
self,
config_file: str,
tie_data_groups: bool
) -> dict[str, tuple]:
"""
# TODO: See if we can clean this up?
"""
self.config_file = config_file
config = XSPECConfiguration(config_file)
conf_dict = config.conf_dict
applied = {}
for instrument in self.instruments.values():
group_num = instrument.signal_data_group
model = xspec.AllModels(group_num, self.current_model)
for component_name in model.componentNames:
component = model.__dict__[component_name]
for parameter_name in conf_dict:
if parameter_name in component.__dict__:
parameter = component.__dict__[parameter_name]
if group_num > 1 and tie_data_groups: # TODO: Confirm if this is correct.
print(f'group_num = {group_num}, and tie_data_groups = True, not setting limits for parameter {parameter_name}')
continue
limits = tuple(conf_dict[parameter_name].values())
parameter.values = limits
applied[parameter_name] = conf_dict[parameter_name]
print(f'setting parameter limits for group {group_num} component {component_name} parameter {parameter_name} ({parameter.index})\n{tuple(conf_dict[parameter_name].values())}\n')
# Untie parameters if necessary.
for parameter_name in component.parameterNames:
parameter = component.__dict__[parameter_name]
if not tie_data_groups:
parameter.link = ''
return applied
def set_gain(
self,
slope: float | tuple,
offset: float | tuple,
fit_slope: bool,
fit_offset: bool,
link_gains: bool = True
):
"""
Assumes that all spectra share the same gain parameters.
If a parameter is not fixed, it will be fitted.
"""
# TODO: Set gain errors.
for group_num in self.signal_groups:
response = xspec.AllData(group_num).response
response.gain.slope = slope
response.gain.offset = offset
if not fit_slope:
response.gain.slope.frozen = True
if not fit_offset:
response.gain.offset.frozen = True
# Link the gain parameters.
if link_gains:
for link_num in self.signal_groups:
if link_num != group_num:
response.gain.slope.link = xspec.AllData(link_num).response.gain.slope
response.gain.offset.link = xspec.AllData(link_num).response.gain.offset
def fit(
self,
num_iterations: int = 1000,
critical_delta: float = 0.01,
fit_statistic: str = 'cstat',
fit_error: bool = True
):
"""
fit_error will fit the errors on all the unfrozen parameters.
"""
xspec.Fit.nIterations = num_iterations
xspec.Fit.criticalDelta = critical_delta
xspec.Fit.statMethod = fit_statistic
xspec.Fit.perform()