-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_processing.py
More file actions
1218 lines (1025 loc) · 45.5 KB
/
data_processing.py
File metadata and controls
1218 lines (1025 loc) · 45.5 KB
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
"""
Data processing for XGBoost analysis.
Provides common functions for flattening and preprocessing telescope array data.
"""
import logging
from concurrent.futures import ThreadPoolExecutor
import awkward as ak
import numpy as np
import pandas as pd
import uproot
from scipy.spatial import ConvexHull, QhullError
from eventdisplay_ml import features as features_module
from eventdisplay_ml import utils
from eventdisplay_ml.geomag import calculate_geomagnetic_angles
_logger = logging.getLogger(__name__)
# Default fill value for missing telescope-dependent data
DEFAULT_FILL_VALUE = np.nan
def read_telescope_config(root_file):
"""
Read telescope configuration from ROOT file.
Parameters
----------
root_file : uproot file handle
Open ROOT file containing the telconfig tree.
Returns
-------
dict
Dictionary with telescope configuration:
- 'n_tel': Total number of telescopes
- 'tel_ids': Array of telescope IDs
- 'mirror_area': Array of mirror areas for each telescope
- 'tel_x': Array of telescope X positions
- 'tel_y': Array of telescope Y positions
- 'max_tel_id': Maximum telescope ID
- 'tel_types': Dictionary mapping mirror area to list of telescope IDs
"""
telconfig_tree = root_file["telconfig"]
telconfig_data = telconfig_tree.arrays(
["NTel", "TelID", "MirrorArea", "TelX", "TelY"], library="np"
)
n_tel = int(telconfig_data["NTel"][0])
tel_ids = telconfig_data["TelID"]
# Keep array of mirror areas; avoid shadowing this name later
mirror_area_arr = telconfig_data["MirrorArea"]
tel_x = telconfig_data["TelX"]
tel_y = telconfig_data["TelY"]
max_tel_id = int(np.max(tel_ids))
# Group telescopes by mirror area (telescope type)
tel_types = {}
for tel_id, area in zip(tel_ids, mirror_area_arr):
key = round(area, 2)
if key not in tel_types:
tel_types[key] = []
tel_types[key].append(int(tel_id))
_logger.info(f"Telescope configuration: {n_tel} telescopes, max TelID: {max_tel_id}")
_logger.info(f"Telescope types by mirror area: {tel_types}")
return {
"n_tel": n_tel,
"tel_ids": tel_ids,
# Provide both singular and plural keys for compatibility
"mirror_area": mirror_area_arr,
"mirror_areas": mirror_area_arr,
"tel_x": tel_x,
"tel_y": tel_y,
"max_tel_id": max_tel_id,
"tel_types": tel_types,
}
def _resolve_branch_aliases(tree, branch_list):
"""
Resolve branch name aliases (e.g. R_core vs R) and drop missing optional branches.
The 'data' tree in the mscw files differ slightly between CTAO and VERITAS
Eventdisplay versions:
- 'R_core': VERITAS-style fixed telescope ID indexing
- 'R': CTAO-style variable-length ImgSel_list indexing
Parameters
----------
tree : uproot tree
Uproot tree containing the data branches.
branch_list : list of str
List of desired branch names.
Returns
-------
tuple
(resolved_branch_list, rename_map)
- resolved_branch_list: List of actual branch names to read.
- rename_map: Dict mapping old names to new names for renaming after reading.
"""
keys = set(tree.keys())
resolved = []
rename = {}
# R_core vs R
for b in branch_list:
if b == "R_core" and b not in keys:
if "R" in keys:
resolved.append("R")
rename["R"] = "R_core"
_logger.info("Branch 'R_core' not found; using 'R'")
else:
_logger.warning("Branches 'R_core' and fallback 'R' not found")
else:
resolved.append(b)
# Drop synthesized branches
synthesized = {
"mirror_area",
"tel_rel_x",
"tel_rel_y",
"tel_active",
}
resolved = [b for b in resolved if b not in synthesized]
# Drop missing optional branches
optional = {"fpointing_dx", "fpointing_dy", "E", "Erec", "ErecS", "nlowgain"}
final = [b for b in resolved if b not in optional or b in keys]
return final, rename
def _ensure_fpointing_fields(arr):
"""Ensure fpointing_dx and fpointing_dy exist; fill zeros if missing."""
fields = set(getattr(arr, "fields", []) or [])
if "DispTelList_T" in fields:
zeros_like_tel = ak.values_astype(ak.zeros_like(arr["DispTelList_T"]), np.float32)
if "fpointing_dx" not in fields:
arr = ak.with_field(arr, zeros_like_tel, "fpointing_dx")
if "fpointing_dy" not in fields:
arr = ak.with_field(arr, zeros_like_tel, "fpointing_dy")
return arr
def _rename_fields(arr, rename_map):
"""Rename record fields present in an Awkward Array."""
for old, new in (rename_map or {}).items():
fields = ak.fields(arr)
if old in fields and old != new:
arr = ak.with_field(arr, arr[old], new)
arr = ak.without_field(arr, old)
return arr
def _make_mirror_area_columns(tel_config, max_tel_id, n_evt, sort_indices=None):
"""Build constant mirror area columns from tel_config with optional sorting."""
base = np.full((n_evt, max_tel_id + 1), DEFAULT_FILL_VALUE, dtype=np.float32)
# Accept both 'mirror_area' (singular) and 'mirror_areas' (plural) for compatibility
mirror_areas = tel_config.get("mirror_area")
if mirror_areas is None:
mirror_areas = tel_config.get("mirror_areas")
if mirror_areas is None:
raise KeyError("tel_config must provide 'mirror_area' or 'mirror_areas' array")
tel_id_to_mirror = dict(zip(tel_config["tel_ids"], mirror_areas))
for tel_idx, mirror_val in tel_id_to_mirror.items():
if tel_idx <= max_tel_id:
mirror_val = float(mirror_val) if mirror_val != 0.0 else 100.0
base[:, tel_idx] = mirror_val
if sort_indices is not None:
base = base[np.arange(n_evt)[:, np.newaxis], sort_indices]
return {f"mirror_area_{i}": base[:, i] for i in range(max_tel_id + 1)}
def _make_tel_active_columns(tel_list_matrix, max_tel_id, n_evt, sort_indices=None):
"""Build binary telescope active columns, optionally sorting."""
columns = {}
active_matrix = np.zeros((n_evt, max_tel_id + 1), dtype=np.float32)
row_indices, col_indices = np.where(~np.isnan(tel_list_matrix))
tel_ids = tel_list_matrix[row_indices, col_indices].astype(int)
valid_mask = tel_ids <= max_tel_id
active_matrix[row_indices[valid_mask], tel_ids[valid_mask]] = 1.0
if sort_indices is not None:
active_matrix = active_matrix[np.arange(n_evt)[:, np.newaxis], sort_indices]
for tel_idx in range(max_tel_id + 1):
columns[f"tel_active_{tel_idx}"] = active_matrix[:, tel_idx]
return columns
def _ground_to_shower_coords(x, y, sin_azim, cos_azim, sin_elev):
"""Transform ground coordinates to shower-plane coordinates.
Rotates around vertical axis by azimuth, then projects onto shower plane.
Parameters
----------
x : numpy.ndarray
X coordinate(s), shape (...,).
y : numpy.ndarray
Y coordinate(s), shape (...,).
sin_azim : numpy.ndarray
Sine of azimuth angle, shape (...,).
cos_azim : numpy.ndarray
Cosine of azimuth angle, shape (...,).
sin_elev : numpy.ndarray
Sine of elevation angle, shape (...,).
Returns
-------
shower_x : numpy.ndarray
Shower-plane X coordinate.
shower_y : numpy.ndarray
Shower-plane Y coordinate scaled by sin(elevation).
Notes
-----
Original formula (azimuth definition of Eventdisplay is relevant)
shower_x = -sin_azim * x + cos_azim * y
shower_y = sin_elev * (cos_azim * x + sin_azim * y)
return shower_x, shower_y
"""
s_az = -sin_azim
c_az = -cos_azim
shower_x = c_az * x + s_az * y
shower_y = sin_elev * (-s_az * x + c_az * y)
return shower_x, shower_y
def _make_relative_coord_columns(
var,
tel_config,
max_tel_id,
n_evt,
core_x,
core_y,
elev_rad,
azim_rad,
sort_indices=None,
):
"""Build relative/shower coordinate columns for a single synthetic variable."""
columns = {}
all_tel_x = np.full(max_tel_id + 1, np.nan)
all_tel_y = np.full(max_tel_id + 1, np.nan)
for tid, tx, ty in zip(tel_config["tel_ids"], tel_config["tel_x"], tel_config["tel_y"]):
if tid <= max_tel_id:
all_tel_x[tid] = tx
all_tel_y[tid] = ty
rel_x = all_tel_x[:, np.newaxis] - core_x
rel_y = all_tel_y[:, np.newaxis] - core_y
if var == "tel_rel_x":
results = rel_x
elif var == "tel_rel_y":
results = rel_y
else:
results = np.full((max_tel_id + 1, n_evt), DEFAULT_FILL_VALUE)
# results shape: (max_tel_id + 1, n_evt) -> transpose to (n_evt, max_tel_id + 1)
event_matrix = results.T
# Apply validity mask and default fill
valid_mask = np.isfinite(core_x) & np.isfinite(core_y)
event_matrix = np.where(
valid_mask[:, np.newaxis] & ~np.isnan(event_matrix), event_matrix, DEFAULT_FILL_VALUE
)
# Reorder if needed
if sort_indices is not None:
event_matrix = event_matrix[np.arange(n_evt)[:, np.newaxis], sort_indices]
for tel_idx in range(max_tel_id + 1):
columns[f"{var}_{tel_idx}"] = event_matrix[:, tel_idx].astype(np.float32)
return columns
def _normalize_telescope_variable_to_tel_id_space(data, index_list, max_tel_id, n_evt):
"""Remap telescope variable from any index list to telescope-ID space.
Takes data indexed by an arbitrary index list (DispTelList_T, ImgSel_list, or fixed positions)
and remaps it to telescope-ID-indexed space (column i = telescope ID i).
Modes:
- VERITAS (R_core): Fixed indexing, index_list=None
- CTAO (ImgSel_list): Variable indexing, index_list=ImgSel_list or DispTelList_T
Parameters
----------
data : numpy.ndarray
Data array, shape (n_evt, n_active) from variable-length or (n_evt, n_tel) from fixed.
index_list : numpy.ndarray or None
Index remapping list, shape (n_evt, n_active). Each entry is a telescope ID or position.
If None, data is already in telescope-ID space (VERITAS R_core fixed indexing).
max_tel_id : int
Maximum telescope ID.
n_evt : int
Number of events.
Returns
-------
numpy.ndarray
Data remapped to telescope-ID space, shape (n_evt, max_tel_id + 1).
"""
if index_list is None:
# Already in telescope-ID space (VERITAS R_core fixed indexing)
full_matrix = np.full((n_evt, max_tel_id + 1), DEFAULT_FILL_VALUE, dtype=np.float32)
col_cap = min(data.shape[1], max_tel_id + 1)
full_matrix[:, :col_cap] = data[:, :col_cap]
return full_matrix
# Variable-length indexed (CTAO ImgSel_list or DispTelList_T mode)
full_matrix = np.full((n_evt, max_tel_id + 1), DEFAULT_FILL_VALUE, dtype=np.float32)
row_indices, col_indices = np.where(~np.isnan(index_list))
tel_ids = index_list[row_indices, col_indices].astype(int)
# Filter for valid telescope IDs and valid column indices in data array
valid_mask = (tel_ids <= max_tel_id) & (col_indices < data.shape[1])
full_matrix[row_indices[valid_mask], tel_ids[valid_mask]] = data[
row_indices[valid_mask], col_indices[valid_mask]
]
return full_matrix
def _clip_size_array(size_array):
"""Clip size array to specified min/max values."""
vmin, vmax = features_module.clip_intervals().get("size", (None, None))
clipped = size_array.copy()
mask = ~np.isnan(clipped)
if vmin is not None:
clipped[mask] = np.maximum(clipped[mask], vmin)
mask = ~np.isnan(clipped)
if vmax is not None:
clipped[mask] = np.minimum(clipped[mask], vmax)
return clipped
def _compute_telescope_indices_to_keep(tel_config, max_tel_id, max_tel_per_type):
"""
Determine which telescope position indices to keep based on mirror area grouping.
After sorting by (mirror_area desc, size desc), telescopes are grouped by mirror area.
This function returns position indices to keep, limiting each mirror area group to
max_tel_per_type telescopes.
Parameters
----------
tel_config : dict
Telescope configuration with 'tel_ids' and 'mirror_area'/'mirror_areas'.
max_tel_id : int
Maximum telescope ID.
max_tel_per_type : int or None
Maximum telescopes to keep per mirror area type. If None, keep all.
Returns
-------
list[int]
List of telescope position indices (0, 1, 2, ...) to keep after truncation.
"""
if max_tel_per_type is None:
# Keep all telescope positions
return list(range(max_tel_id + 1))
# Get mirror areas from tel_config
mirror_areas = tel_config.get("mirror_area")
if mirror_areas is None:
mirror_areas = tel_config.get("mirror_areas")
if mirror_areas is None:
_logger.warning("No mirror_area found in tel_config; keeping all telescopes")
return list(range(max_tel_id + 1))
# Group telescope IDs by mirror area and sort by area (descending)
area_to_tids = {}
for tid, area in zip(tel_config["tel_ids"], mirror_areas):
if tid <= max_tel_id:
area_key = round(float(area), 2) # Round to avoid floating point issues
if area_key not in area_to_tids:
area_to_tids[area_key] = []
area_to_tids[area_key].append(int(tid))
# Sort area types by size (descending)
sorted_area_keys = sorted(area_to_tids.keys(), reverse=True)
# Calculate positions to keep: for each area type, keep first max_tel_per_type positions
indices_to_keep = []
current_position = 0
for area_key in sorted_area_keys:
n_tels_this_type = len(area_to_tids[area_key])
n_to_keep = min(n_tels_this_type, max_tel_per_type)
# Keep positions current_position to current_position + n_to_keep - 1
indices_to_keep.extend(range(current_position, current_position + n_to_keep))
current_position += n_tels_this_type
_logger.info(
f"Feature reduction: {len(sorted_area_keys)} mirror area types, "
f"keeping {len(indices_to_keep)} out of {max_tel_id + 1} telescope positions "
f"(max {max_tel_per_type} per type)"
)
return sorted(indices_to_keep)
def flatten_telescope_data_vectorized(
df,
n_tel,
features,
analysis_type,
training=True,
tel_config=None,
observatory="veritas",
max_tel_per_type=None,
preview_rows=20,
):
"""
Vectorized flattening of telescope array columns.
Converts per-telescope arrays into individual feature columns sorted by mirror area and
size.
Parameters
----------
df : pandas.DataFrame
Input DataFrame containing telescope data.
n_tel : int
Number of telescopes to flatten for (maximum telescope index).
features : list[str]
List of training variable names to flatten.
analysis_type : str
Type of analysis (e.g., "stereo_analysis").
training : bool, optional
If True, indicates training mode. Default is True.
tel_config : dict, optional
Telescope configuration dictionary with 'max_tel_id' and 'tel_types'.
observatory : str, optional
Observatory name for indexing mode detection. Default is "veritas".
max_tel_per_type : int, optional
Maximum number of telescopes to keep per mirror area type. If None, keep all.
preview_rows : int, optional
Number of events to include in the sorting preview log. Set to 0 to disable.
Returns
-------
pandas.DataFrame
Flattened DataFrame with per-telescope columns suffixed by ``_{i}``
"""
flat_features = {}
tel_list_matrix = _to_dense_array(df["DispTelList_T"])
n_evt = len(df)
max_tel_id = tel_config["max_tel_id"] if tel_config else (n_tel - 1)
# Index remapping mode for CTAO-style variable-length indexing
index_list_for_remapping = None # None = telescope-ID-indexed (VERITAS R_core mode)
if observatory.lower() == "veritas":
_logger.info("Detected VERITAS R_core fixed telescope-ID indexing mode.")
else:
_logger.info("Detected CTAO ImgSel_list variable-length indexing mode.")
index_list_for_remapping = _to_dense_array(df["ImgSel_list"])
active_mask = np.zeros((n_evt, max_tel_id + 1), dtype=bool)
row_indices, col_indices = np.where(~np.isnan(tel_list_matrix))
tel_ids = tel_list_matrix[row_indices, col_indices].astype(int)
valid_tel_mask = tel_ids <= max_tel_id
active_mask[row_indices[valid_tel_mask], tel_ids[valid_tel_mask]] = True
# Pre-load and normalize size to telescope-ID space for sorting
size_data = _normalize_telescope_variable_to_tel_id_space(
_to_dense_array(df["size"]), index_list_for_remapping, max_tel_id, n_evt
)
size_data = _clip_size_array(size_data)
core_x, core_y = _get_core_arrays(df)
# Sorting by mirror area (desc; proxy for telescope type), then size (desc)
sort_indices = _compute_size_area_sort_indices(size_data, active_mask, tel_config, max_tel_id)
# Determine which telescope positions to keep (for feature reduction)
if max_tel_per_type is not None and tel_config is not None:
tel_indices_to_keep = _compute_telescope_indices_to_keep(
tel_config, max_tel_id, max_tel_per_type
)
else:
tel_indices_to_keep = list(range(max_tel_id + 1))
for var in features:
if var == "mirror_area" and tel_config:
flat_features.update(
_make_mirror_area_columns(tel_config, max_tel_id, n_evt, sort_indices)
)
continue
if var == "tel_active":
_logger.info(f"Computing synthetic feature: {var}")
flat_features.update(
_make_tel_active_columns(tel_list_matrix, max_tel_id, n_evt, sort_indices)
)
continue
if var in ("tel_rel_x", "tel_rel_y") and tel_config:
_logger.info(f"Computing synthetic feature: {var}")
flat_features.update(
_make_relative_coord_columns(
var,
tel_config,
max_tel_id,
n_evt,
core_x,
core_y,
np.radians(_to_numpy_1d(df["ArrayPointing_Elevation"], np.float32)),
np.radians(_to_numpy_1d(df["ArrayPointing_Azimuth"], np.float32)),
sort_indices,
)
)
continue
data = _to_dense_array(df[var]) if _has_field(df, var) else np.full((n_evt, n_tel), np.nan)
# Disp_* variables always use DispTelList_T, regardless of mode
if var.startswith("Disp") and "_T" in var:
data_normalized = _normalize_telescope_variable_to_tel_id_space(
data, tel_list_matrix, max_tel_id, n_evt
)
else:
# All other variables use the mode-dependent index
# (None for VERITAS R_core fixed, ImgSel_list for CTAO variable)
data_normalized = _normalize_telescope_variable_to_tel_id_space(
data, index_list_for_remapping, max_tel_id, n_evt
)
# All variables are now in telescope-ID space; apply sorting and flatten uniformly
data_normalized = data_normalized[np.arange(n_evt)[:, np.newaxis], sort_indices]
for tel_idx in tel_indices_to_keep:
flat_features[f"{var}_{tel_idx}"] = data_normalized[:, tel_idx]
# Also filter synthetic features to only keep selected indices
# This applies to features like mirror_area, tel_active, etc. that were added above
if max_tel_per_type is not None:
filtered_features = {}
for key, value in flat_features.items():
# Extract the telescope index from feature names like "size_5", "mirror_area_10", etc.
parts = key.rsplit("_", 1)
if len(parts) == 2 and parts[1].isdigit():
tel_idx = int(parts[1])
if tel_idx in tel_indices_to_keep:
filtered_features[key] = value
else:
# Keep features without telescope index suffix
filtered_features[key] = value
flat_features = filtered_features
index = _get_index(df, n_evt)
df_flat = flatten_telescope_variables(
n_tel,
flat_features,
index,
tel_config=tel_config,
analysis_type=analysis_type,
preview_rows=preview_rows,
)
return pd.concat(
[df_flat, extra_columns(df, analysis_type, training, index, tel_config, observatory)],
axis=1,
)
def _to_dense_array(col):
"""
Convert a column of variable-length telescope data to a dense 2D numpy array.
Handles uproot's awkward-style variable-length arrays from ROOT files
by converting to plain Python lists first to avoid per-element iteration overhead.
- right-pad each event to equal length with NaN
- return as 2D numpy array of shape (n_events, max_telescopes)
Parameters
----------
col : pandas.Series
Column containing variable-length arrays.
Returns
-------
numpy.ndarray
2D numpy array with shape (n_events, max_telescopes), padded with NaN.
"""
if isinstance(col, ak.Array):
padded = ak.pad_none(col, target=int(ak.max(ak.num(col))), axis=1)
return ak.to_numpy(ak.fill_none(padded, np.nan))
if isinstance(col, pd.Series):
col = col.values
try:
ak_arr = ak.from_iter(col)
padded = ak.pad_none(ak_arr, target=ak.max(ak.num(ak_arr)), axis=1)
return ak.to_numpy(ak.fill_none(padded, np.nan))
except (ValueError, TypeError) as exc:
raise ValueError("Input column must be convertible to an Awkward Array.") from exc
def _get_core_arrays(df):
"""Extract core position arrays from DataFrame."""
# Make copies to ensure arrays are writable (some sources return read-only views)
core_x = _to_numpy_1d(df["Xcore"], np.float32).copy()
core_y = _to_numpy_1d(df["Ycore"], np.float32).copy()
# Filter out sentinel values and apply physical bounds
# shower cores beyond +-10 km are cut
core_x[(core_x <= -90000) | (np.abs(core_x) > 10000)] = np.nan
core_y[(core_y <= -90000) | (np.abs(core_y) > 10000)] = np.nan
return core_x, core_y
def _compute_size_area_sort_indices(size_data, active_mask, tel_config, max_tel_id):
"""Compute sorting indices: mirror area (desc) then size (desc).
Missing telescopes (NaN size or no mirror area) are sorted to the end.
"""
n_evt = active_mask.shape[0]
# Map mirror areas to a dense lookup by telescope ID
mirror_lookup = np.full(max_tel_id + 1, np.nan, dtype=np.float32)
# Accept both legacy ('mirror_area') and plural ('mirror_areas') keys
mirror_areas = tel_config.get("mirror_area")
if mirror_areas is None:
mirror_areas = tel_config.get("mirror_areas")
if mirror_areas is None:
raise KeyError("tel_config must provide 'mirror_area' (array) or 'mirror_areas'")
for tid, area in zip(tel_config["tel_ids"], mirror_areas):
if tid <= max_tel_id:
mirror_lookup[int(tid)] = float(area)
# size_data is already in tel_id space (shape: n_evt x (max_tel_id + 1))
sizes = size_data
# Sort per-event: primary = mirror area (desc), secondary = size (desc)
# Area has highest priority ALWAYS, even if size is NaN.
# NaN mirror areas go to the very end; within equal area groups,
# valid sizes come before NaN sizes, and then by size descending.
sort_indices = np.zeros((n_evt, max_tel_id + 1), dtype=int)
for evt_idx in range(n_evt):
tel_entries = []
for tel_idx in range(max_tel_id + 1):
area = mirror_lookup[tel_idx]
size_val = sizes[evt_idx, tel_idx]
# Build sort key:
# 1) valid area first (0), NaN area last (1)
# 2) area descending via negative value
# 3) within same area: valid size first (0), NaN last (1)
# 4) size descending via negative value
area_valid = 0 if not np.isnan(area) else 1
size_valid = 0 if not np.isnan(size_val) else 1
area_key = -area if area_valid == 0 else 0.0
size_key = -size_val if size_valid == 0 else 0.0
tel_entries.append((tel_idx, area_valid, area_key, size_valid, size_key))
tel_entries.sort(key=lambda x: (x[1], x[2], x[3], x[4]))
sort_indices[evt_idx] = np.array([t[0] for t in tel_entries])
return sort_indices
def _to_numpy_1d(x, dtype=np.float32):
"""Convert Series/array/ak.Array to a 1D numpy array with dtype."""
if hasattr(x, "to_numpy"):
try:
return x.to_numpy(dtype=dtype)
except TypeError:
return np.asarray(x).astype(dtype)
if isinstance(x, ak.Array):
return ak.to_numpy(x).astype(dtype)
return np.asarray(x, dtype=dtype)
def _has_field(df_like, name):
"""Check presence of a field/column in pandas DataFrame or Awkward Array."""
if isinstance(df_like, pd.DataFrame):
return name in df_like.columns
if isinstance(df_like, (ak.Array, ak.Record)):
return name in (getattr(df_like, "fields", []) or [])
try:
return name in df_like
except (TypeError, AttributeError):
return False
def _get_index(df_like, n):
"""Get an index for DataFrame construction from pandas or fallback to RangeIndex."""
if isinstance(df_like, pd.DataFrame):
return df_like.index
return pd.RangeIndex(n)
def flatten_feature_data(
group_df,
ntel,
analysis_type,
training,
tel_config=None,
observatory="veritas",
max_tel_per_type=None,
preview_rows=20,
):
"""
Get flattened features for events.
All events are flattened with features indexed by actual telescope ID.
Parameters
----------
group_df : pandas.DataFrame
DataFrame with events to flatten.
ntel : int
Maximum telescope index.
analysis_type : str
Type of analysis.
training : bool
Whether in training mode.
tel_config : dict, optional
Telescope configuration dictionary.
observatory : str, optional
Observatory name for indexing mode detection.
max_tel_per_type : int, optional
Maximum number of telescopes to keep per mirror area type. If None, keep all.
preview_rows : int, optional
Number of events to include in the sorting preview log. Set to 0 to disable.
"""
df_flat = flatten_telescope_data_vectorized(
group_df,
ntel,
features_module.telescope_features(analysis_type),
analysis_type=analysis_type,
training=training,
tel_config=tel_config,
observatory=observatory,
max_tel_per_type=max_tel_per_type,
preview_rows=preview_rows,
)
max_tel_id = tel_config["max_tel_id"] if tel_config else ntel - 1
excluded_columns = set(features_module.target_features(analysis_type)) | set(
features_module.excluded_features(analysis_type, max_tel_id + 1)
)
return df_flat.drop(columns=excluded_columns, errors="ignore")
def load_training_data(model_configs, file_list, analysis_type):
"""
Load and flatten training data from the mscw file.
Processes all events regardless of telescope multiplicity. Features are created
for all telescopes with default value DEFAULT_FILL_VALUE for missing telescopes.
Reads telescope configuration from the ROOT file to determine the number
and types of telescopes.
Parameters
----------
model_configs : dict
Dictionary containing model configuration parameters.
file_list : str
Path to text file containing list of input mscw files.
analysis_type : str
Type of analysis (e.g., "stereo_analysis").
Returns
-------
pandas.DataFrame
Flattened DataFrame ready for training.
"""
max_events = model_configs.get("max_events", None)
random_state = model_configs.get("random_state", None)
_logger.info(f"--- Loading and Flattening Data for {analysis_type} ---")
_logger.info("Processing all events regardless of multiplicity")
_logger.info(
"Max events to process: "
f"{max_events if max_events is not None and max_events > 0 else 'All available'}"
)
if analysis_type == "classification":
_logger.info(f"Adding zenith binning: {model_configs.get('zenith_bins_deg', [])}")
input_files = utils.read_input_file_list(file_list)
branch_list = features_module.features(analysis_type, training=True)
_logger.info(f"Branch list: {branch_list}")
if max_events is not None and max_events > 0:
max_events_per_file = max_events // len(input_files)
else:
max_events_per_file = None
_logger.info(f"Max events per file: {max_events_per_file}")
tel_config = None # Will be read from first file
dfs = []
executor = ThreadPoolExecutor(max_workers=model_configs.get("max_cores", 1))
total_files = len(input_files)
for file_idx, f in enumerate(input_files, start=1):
try:
with uproot.open(f) as root_file:
if "data" not in root_file:
_logger.warning(f"File: {f} does not contain a 'data' tree.")
continue
if tel_config is None:
tel_config = read_telescope_config(root_file)
model_configs["tel_config"] = tel_config
else:
# Check if current file has a larger max_tel_id and update if needed
current_tel_config = read_telescope_config(root_file)
if current_tel_config["max_tel_id"] > tel_config["max_tel_id"]:
_logger.info(
f"Updating telescope configuration: max_tel_id from "
f"{tel_config['max_tel_id']} to {current_tel_config['max_tel_id']} "
f"(file: {f})"
)
# Replace the full telescope configuration to keep all fields consistent
tel_config = current_tel_config
model_configs["tel_config"] = tel_config
_logger.info(f"Processing file: {f} (file {file_idx}/{total_files})")
tree = root_file["data"]
resolved_branch_list, rename_map = _resolve_branch_aliases(tree, branch_list)
df = tree.arrays(
resolved_branch_list,
cut=model_configs.get("pre_cuts", None),
library="ak",
decompression_executor=executor,
)
if rename_map:
rename_present = {k: v for k, v in rename_map.items() if k in df.fields}
if rename_present:
df = _rename_fields(df, rename_present)
df = _ensure_fpointing_fields(df)
if len(df) == 0:
continue
if max_events_per_file and len(df) > max_events_per_file:
rng = np.random.default_rng(random_state)
indices = rng.choice(len(df), max_events_per_file, replace=False)
df = df[indices]
n_before = tree.num_entries
_logger.info(
f"Number of events before / after event cut: {n_before} / {len(df)}"
f" (fraction retained: {len(df) / n_before:.4f})"
)
df_flat = flatten_telescope_data_vectorized(
df,
tel_config["max_tel_id"] + 1,
features_module.telescope_features(analysis_type),
analysis_type,
training=True,
tel_config=tel_config,
observatory=model_configs.get("observatory", "veritas"),
max_tel_per_type=model_configs.get("max_tel_per_type", None),
preview_rows=model_configs.get("preview_rows", 20),
)
if analysis_type == "stereo_analysis":
new_cols = {
"MCxoff": _to_numpy_1d(df["MCxoff"], np.float32),
"MCyoff": _to_numpy_1d(df["MCyoff"], np.float32),
"MCe0": np.log10(_to_numpy_1d(df["MCe0"], np.float32)),
}
elif analysis_type == "classification":
new_cols = {
"ze_bin": zenith_in_bins(
90.0 - _to_numpy_1d(df["ArrayPointing_Elevation"], np.float32),
model_configs.get("zenith_bins_deg", []),
)
}
for col_name, values in new_cols.items():
df_flat[col_name] = values
dfs.append(df_flat)
del df
except Exception as e:
raise FileNotFoundError(f"Error opening or reading file {f}: {e}") from e
df_final = pd.concat(dfs, ignore_index=True)
df_final.dropna(axis=1, how="all", inplace=True)
_logger.info(f"Total events loaded: {len(df_final)}")
# Log multiplicity distribution
if "DispNImages" in df_final.columns:
mult_counts = df_final["DispNImages"].value_counts().sort_index()
for mult, count in mult_counts.items():
_logger.info(f"\tDispNImages={int(mult)}: {count} events")
if analysis_type == "classification":
counts = df_final["ze_bin"].value_counts().sort_index()
for zb, n in counts.items():
_logger.info(f"\tze_bin={zb}: {n} events")
if len(df_final) == 0:
raise ValueError("No data loaded from input files.")
print_variable_statistics(df_final)
return df_final
def apply_clip_intervals(df, n_tel=None, apply_log10=None):
"""
Apply clip intervals to matching columns.
Modifies the dataframe in place. Handles NaN default values for missing telescopes
by preserving them throughout clipping and log10 transformation.
Parameters
----------
df : pandas.DataFrame
DataFrame to apply clipping to (modified in place).
n_tel : int, optional
Number of telescopes. If provided, applies to per-telescope columns (var_0, var_1, etc.).
apply_log10 : list, optional
List of variable base names to apply log10 transformation after clipping.
"""
if apply_log10 is None:
apply_log10 = []
clip_intervals = features_module.clip_intervals()
for var_base, (vmin, vmax) in clip_intervals.items():
if n_tel is not None:
for i in range(n_tel):
col_name = f"{var_base}_{i}"
if col_name in df.columns:
mask_valid = df[col_name].notna()
df.loc[mask_valid, col_name] = df.loc[mask_valid, col_name].clip(vmin, vmax)
if var_base in apply_log10:
mask_to_log = mask_valid & (df[col_name] > 0)
df.loc[mask_to_log, col_name] = np.log10(df.loc[mask_to_log, col_name])
else:
if var_base in df.columns:
mask_valid = df[var_base].notna()
df.loc[mask_valid, var_base] = df.loc[mask_valid, var_base].clip(vmin, vmax)
if var_base in apply_log10:
mask_to_log = mask_valid & (df[var_base] > 0)
df.loc[mask_to_log, var_base] = np.log10(df.loc[mask_to_log, var_base])
def flatten_telescope_variables(
n_tel,
flat_features,
index,
tel_config=None,
analysis_type=None,
preview_rows=20,
):
"""Generate dataframe for telescope variables flattened for all telescopes.
Creates features for all telescope IDs, using NaN as default value for missing data.
Parameters
----------
n_tel : int
Maximum telescope index (for backward compatibility).
flat_features : dict
Dictionary of flattened feature arrays.
index : pandas.Index
DataFrame index.
tel_config : dict, optional
Telescope configuration with 'max_tel_id' key.
analysis_type : str, optional
Type of analysis, e.g. "classification" or "stereo_analysis".
preview_rows : int, optional
Number of events to include in the sorting preview log. Set to 0 to disable.
"""
df_flat = pd.DataFrame(flat_features, index=index)
df_flat = df_flat.astype(np.float32)
# Determine max telescope ID from config or use n_tel
max_tel_id = tel_config["max_tel_id"] if tel_config else (n_tel - 1)
keep_size_vars = analysis_type == "stereo_analysis"
if not keep_size_vars:
_logger.info(f"Dropping 'size'-related variables for {analysis_type} analysis.")
new_cols = {}
for i in range(max_tel_id + 1): # Iterate over all possible telescopes
if f"Disp_T_{i}" in df_flat:
new_cols[f"disp_x_{i}"] = df_flat[f"Disp_T_{i}"] * df_flat[f"cosphi_{i}"]
new_cols[f"disp_y_{i}"] = df_flat[f"Disp_T_{i}"] * df_flat[f"sinphi_{i}"]