Repository navigation
Expand file tree
/
Copy pathpreprocessing.py
More file actions
1434 lines (1227 loc) · 54.4 KB
/
Copy pathpreprocessing.py
File metadata and controls
1434 lines (1227 loc) · 54.4 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
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from typing import Tuple, Optional, Dict, List, Union
import os
import logging
from datetime import datetime
class EnhancedDataPreprocessor:
"""
Enhanced version of DataPreprocessor with detailed logging and visualization
for each preprocessing step.
This class follows the same processing sequence as the original DataPreprocessor:
1. Signal balancing
2. Feature transformations (GeV scaling, log transformation)
3. Outlier clipping
4. Normalization
"""
def __init__(
self,
gev_vars: Tuple[str, ...] = None,
log_vars: Tuple[str, ...] = None,
tenx_vars: Tuple[str, ...] = None,
unchanged_vars: Tuple[str, ...] = None,
background_channel: str = "minbias",
clip_quantiles: Tuple[float, float] = (0.0001, 0.9999),
normalize: bool = True,
log_dir: str = "./preprocessing_logs",
verbose: bool = True,
):
"""
Initialize the preprocessor with feature transformation specifications and logging.
Parameters:
-----------
gev_vars : tuple of str
Variables to be converted from MeV to GeV (divide by 1000)
log_vars : tuple of str
Variables to be log-transformed
unchanged_vars : tuple of str
Variables to include in normalization without any prior transformation
background_channel : str
Name of the background channel in the dataset
clip_quantiles : tuple of (lower, upper)
Quantiles to use for clipping values
normalize : bool
Whether to normalize features to [0,1] range
log_dir : str
Directory for saving logs and visualizations
verbose : bool
Whether to print detailed logs to stdout
"""
self.gev_vars = gev_vars or ()
self.log_vars = log_vars or ()
self.tenx_vars = tenx_vars or ()
self.unchanged_vars = unchanged_vars or ()
self.background_channel = background_channel
self.clip_quantiles = clip_quantiles
self.normalize = normalize
# Set up logging
self.log_dir = log_dir
self.verbose = verbose
self.snapshots = {} # Store data snapshots at different stages
# Create log directory
os.makedirs(log_dir, exist_ok=True)
# Set up logger
self.logger = self._setup_logger()
# Build transformation map
self.transformations = {}
for var in self.gev_vars:
self.transformations[var] = "gev"
for var in self.log_vars:
self.transformations[var] = "log"
for var in self.tenx_vars:
self.transformations[var] = "tenx"
for var in self.unchanged_vars:
self.transformations[var] = "none"
# Statistics from training data (to be filled during fit)
self.feature_stats = {}
# Log initialization
self.logger.info("Initialized EnhancedDataPreprocessor:")
self.logger.info(f"GeV variables: {self.gev_vars}")
self.logger.info(f"Log variables: {self.log_vars}")
self.logger.info(f"Tenx variables: {self.tenx_vars}")
self.logger.info(f"Unchanged variables: {self.unchanged_vars}")
self.logger.info(f"Clipping quantiles: {self.clip_quantiles}")
self.logger.info(f"Normalization enabled: {self.normalize}")
def _setup_logger(self):
"""Set up logging to file and console."""
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
log_file = os.path.join(self.log_dir, f"preprocessing_{timestamp}.log")
# Create logger
logger = logging.getLogger(f"preprocessor_{timestamp}")
logger.setLevel(logging.DEBUG)
# Create file handler
file_handler = logging.FileHandler(log_file)
file_handler.setLevel(logging.DEBUG)
# Create console handler with higher level if verbose is False
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO if self.verbose else logging.WARNING)
# Create formatter
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
file_handler.setFormatter(formatter)
console_handler.setFormatter(formatter)
# Add handlers to logger
logger.addHandler(file_handler)
logger.addHandler(console_handler)
return logger
def fit(
self,
df: pd.DataFrame,
balance_first: bool = True,
min_count: Optional[int] = None,
) -> "EnhancedDataPreprocessor":
"""
Calculate necessary statistics from training data with enhanced logging and visualization.
Parameters:
-----------
df : pandas.DataFrame
Training data
balance_first : bool
Whether to balance signal channels before calculating transformation statistics
min_count : int, optional
Minimum count to use for all signal channels if balancing
Returns:
--------
self : EnhancedDataPreprocessor
The fitted preprocessor
"""
self.logger.info("Starting fit process...")
# Store the original data snapshot
feature_cols = list(self.transformations.keys())
self.snapshots["raw"] = df[feature_cols].copy()
self._plot_feature_distributions(
df[feature_cols], "raw", "Raw Data Distributions"
)
# Optionally balance the dataset first for more representative statistics
if balance_first:
self.logger.info("Balancing data before fitting...")
fit_df = self.balance_signal(df, min_count=min_count)
# Log balancing results
self.logger.info(
f"Original data size: {len(df)} -> Balanced data size: {len(fit_df)}"
)
if "channel" in df.columns:
channel_before = df["channel"].value_counts()
channel_after = fit_df["channel"].value_counts()
for channel in sorted(
set(channel_before.index) | set(channel_after.index)
):
before = channel_before.get(channel, 0)
after = channel_after.get(channel, 0)
self.logger.info(f"Channel '{channel}': {before} -> {after}")
# Save balanced data snapshot
self.snapshots["balanced"] = fit_df[feature_cols].copy()
self._plot_feature_distributions(
fit_df[feature_cols], "balanced", "Balanced Data Distributions"
)
else:
fit_df = df
self.logger.info("Using unbalanced data for fitting")
# Calculate statistics for each feature to be transformed
self.logger.info("Calculating feature statistics...")
# Create a copy for tracking transformations step by step
transformed_df = fit_df.copy()
for column, transform_type in self.transformations.items():
if column not in fit_df.columns:
self.logger.warning(f"Column {column} not found in dataset")
continue
self.logger.info(
f"Processing feature '{column}' with transform '{transform_type}'"
)
# Get the feature values
values = fit_df[column].values.copy()
original_stats = {
"min": np.min(values),
"max": np.max(values),
"mean": np.mean(values),
"median": np.median(values),
"std": np.std(values),
}
self.logger.info(f"Original stats for '{column}': {original_stats}")
# Apply primary transformation
if transform_type == "gev":
self.logger.info(f"Applying GeV scaling to '{column}'")
values = values / 1000.0
transformed_df[column] = transformed_df[column] / 1000.0
elif transform_type == "log":
# Find minimum non-zero value to determine appropriate epsilon
min_positive = (
np.min(values[values > 0]) if np.any(values > 0) else 1e-6
)
epsilon = min(min_positive / 10.0, 1e-6)
self.logger.info(
f"Applying log transform to '{column}' with epsilon={epsilon}"
)
values = np.log(values + epsilon)
transformed_df[column] = np.log(transformed_df[column] + epsilon)
# Store epsilon for this feature
self.feature_stats[f"{column}_epsilon"] = epsilon
elif transform_type == "tenx":
self.logger.info(f"Applying 10x scaling to '{column}'")
values = values * 10.0
transformed_df[column] = transformed_df[column] * 10.0
elif transform_type == "none":
self.logger.info(f"No transformation for '{column}'")
# Log transformed statistics
transformed_stats = {
"min": np.min(values),
"max": np.max(values),
"mean": np.mean(values),
"median": np.median(values),
"std": np.std(values),
}
self.logger.info(
f"After transform stats for '{column}': {transformed_stats}"
)
# Calculate clipping bounds
lower_bound = np.nanquantile(values, self.clip_quantiles[0])
upper_bound = np.nanquantile(values, self.clip_quantiles[1])
self.logger.info(
f"Clipping bounds for '{column}': lower={lower_bound}, upper={upper_bound}"
)
# Store bounds for this feature
self.feature_stats[f"{column}_lower"] = lower_bound
self.feature_stats[f"{column}_upper"] = upper_bound
# Calculate normalization parameters if needed
if self.normalize:
# Apply clipping for calculating normalization bounds
clipped_values = np.clip(values, lower_bound, upper_bound)
min_val = np.min(clipped_values)
max_val = np.max(clipped_values)
self.logger.info(
f"Normalization range for '{column}': min={min_val}, max={max_val}"
)
# Store normalization parameters
self.feature_stats[f"{column}_min"] = min_val
self.feature_stats[f"{column}_max"] = max_val
# Save transformed data snapshot
self.snapshots["transformed"] = transformed_df[feature_cols].copy()
self._plot_feature_distributions(
transformed_df[feature_cols],
"transformed",
"After Initial Transformations (GeV/Log)",
)
# Apply clipping to visualization
clipped_df = transformed_df.copy()
for column in feature_cols:
if column in self.feature_stats:
lower = self.feature_stats.get(f"{column}_lower")
upper = self.feature_stats.get(f"{column}_upper")
if lower is not None and upper is not None:
clipped_df[column] = clipped_df[column].clip(lower, upper)
# Save clipped data snapshot
self.snapshots["clipped"] = clipped_df[feature_cols].copy()
self._plot_feature_distributions(
clipped_df[feature_cols], "clipped", "After Clipping (Quantiles)"
)
# Create a final visualization after full transformation
self.logger.info("Fit process completed")
return self
def transform(
self, df: pd.DataFrame, preserve_originals: bool = True
) -> pd.DataFrame:
"""
Apply transformations to the data with enhanced logging.
Parameters:
-----------
df : pandas.DataFrame
Data to transform
preserve_originals : bool
Whether to keep original values in '_original' columns
Returns:
--------
pandas.DataFrame
Transformed DataFrame
"""
self.logger.info("Starting transform process...")
# Create copy to work with
result_df = df.copy()
feature_cols = list(self.transformations.keys())
# Save input snapshot if we don't have the raw data already
if "raw" not in self.snapshots:
self.snapshots["raw"] = df[feature_cols].copy()
self._plot_feature_distributions(
df[feature_cols], "raw", "Raw Input Data Distributions"
)
# Process each feature with logging
processed_features = 0
for column, transform_type in self.transformations.items():
if column not in result_df.columns:
self.logger.warning(f"Column {column} not found in dataset")
continue
processed_features += 1
self.logger.info(f"Transforming feature '{column}' with '{transform_type}'")
# Preserve original values if requested
if preserve_originals:
result_df[f"{column}_original"] = result_df[column].copy()
# Record original statistics
orig_stats = {
"min": result_df[column].min(),
"max": result_df[column].max(),
"mean": result_df[column].mean(),
"std": result_df[column].std(),
}
self.logger.info(f"Original stats for '{column}': {orig_stats}")
# Apply transformation
if transform_type == "gev":
result_df[column] = result_df[column] / 1000.0
self.logger.info(f"Applied GeV scaling to '{column}'")
elif transform_type == "log":
epsilon = self.feature_stats.get(f"{column}_epsilon", 1e-6)
result_df[column] = np.log(result_df[column] + epsilon)
self.logger.info(
f"Applied log transform to '{column}' with epsilon={epsilon}"
)
elif transform_type == "tenx":
result_df[column] = result_df[column] * 10.0
self.logger.info(f"Applied 10x scaling to '{column}'")
elif transform_type == "none":
self.logger.info(f"No transformation needed for '{column}'")
# Apply clipping
lower = self.feature_stats.get(f"{column}_lower")
upper = self.feature_stats.get(f"{column}_upper")
if lower is not None and upper is not None:
before_clip = result_df[column].copy()
result_df[column] = result_df[column].clip(lower, upper)
# Calculate how many values were clipped
lower_clipped = (before_clip < lower).sum()
upper_clipped = (before_clip > upper).sum()
perc_clipped = (
(lower_clipped + upper_clipped) / len(before_clip)
) * 100
self.logger.info(f"Clipped '{column}' to [{lower}, {upper}]")
self.logger.info(
f" - Values clipped below: {lower_clipped} ({lower_clipped/len(before_clip)*100:.2f}%)"
)
self.logger.info(
f" - Values clipped above: {upper_clipped} ({upper_clipped/len(before_clip)*100:.2f}%)"
)
self.logger.info(
f" - Total clipped: {lower_clipped + upper_clipped} ({perc_clipped:.2f}%)"
)
# Apply normalization if needed
if self.normalize:
min_val = self.feature_stats.get(f"{column}_min")
max_val = self.feature_stats.get(f"{column}_max")
if min_val is not None and max_val is not None and max_val > min_val:
before_norm = result_df[column].copy()
result_df[column] = (result_df[column] - min_val) / (
max_val - min_val
)
self.logger.info(
f"Normalized '{column}' from [{min_val}, {max_val}] to [0, 1]"
)
# Check transformed range
actual_min = result_df[column].min()
actual_max = result_df[column].max()
if actual_min < 0 or actual_max > 1:
self.logger.warning(
f" ⚠️ Normalized range outside [0,1]: [{actual_min}, {actual_max}]"
)
# Log transformed statistics
final_stats = {
"min": result_df[column].min(),
"max": result_df[column].max(),
"mean": result_df[column].mean(),
"std": result_df[column].std(),
}
self.logger.info(f"Final stats for '{column}': {final_stats}")
# Save a final snapshot
if processed_features > 0:
self.snapshots["final"] = result_df[feature_cols].copy()
self._plot_feature_distributions(
result_df[feature_cols], "final", "Final Transformed Data"
)
self._plot_transformation_stages()
self._plot_before_after_comparisons()
self._generate_report()
self.logger.info(
f"Transform completed: {processed_features} features processed"
)
return result_df
def fit_transform(
self,
df: pd.DataFrame,
balance: bool = True,
min_count: Optional[int] = None,
preserve_originals: bool = True,
) -> pd.DataFrame:
"""
Fit and transform in one step with detailed logging and visualization.
Parameters:
-----------
df : pandas.DataFrame
Training data
balance : bool
Whether to balance signal channels
min_count : int, optional
Minimum count to use for all signal channels if balancing
preserve_originals : bool
Whether to keep original values in '_original' columns
Returns:
--------
pandas.DataFrame
Processed DataFrame
"""
self.logger.info("Starting fit_transform process...")
# Step 1: Balance for fitting (always balance for fitting parameters)
if balance:
self.logger.info("Balancing data for fitting...")
balanced_df = self.balance_signal(df, min_count=min_count)
# Log balancing results
self.logger.info(
f"Original size: {len(df)} -> Balanced size: {len(balanced_df)}"
)
if "channel" in df.columns:
channel_counts = (
pd.DataFrame(
{
"Original": df["channel"].value_counts(),
"Balanced": balanced_df["channel"].value_counts(),
}
)
.fillna(0)
.astype(int)
)
for channel, row in channel_counts.iterrows():
self.logger.info(
f"Channel '{channel}': {row['Original']} -> {row['Balanced']}"
)
# Plot channel distributions
self._plot_channel_distribution(df, balanced_df)
else:
balanced_df = df
self.logger.info("Using unbalanced data (balance=False)")
# Step 2: Fit on balanced data
self.logger.info("Fitting preprocessor on data...")
self.fit(balanced_df, balance_first=False)
# Step 3: Process using the fitted parameters
self.logger.info("Applying transformation...")
if balance:
# Already balanced, just transform
return self.transform(balanced_df, preserve_originals=preserve_originals)
else:
# Transform without additional balancing
return self.transform(df, preserve_originals=preserve_originals)
def balance_signal(
self, df: pd.DataFrame, min_count: Optional[int] = None, random_seed: int = 42
) -> pd.DataFrame:
"""
Balance signal channels with enhanced logging.
Parameters:
-----------
df : pandas.DataFrame
DataFrame containing the data with a 'channel' column
min_count : int, optional
Minimum count to use for all signal channels. If None, uses the minimum
count found in the signal channels
random_seed : int
Random seed for reproducibility
Returns:
--------
pandas.DataFrame
A new DataFrame with balanced signal channels
"""
self.logger.info("Balancing signal channels...")
# Check if channel column exists
if "channel" not in df.columns:
self.logger.warning(
"No 'channel' column found in dataframe, returning original data"
)
return df
# Get the count of each channel
channel_counts = df["channel"].value_counts()
self.logger.info("Original channel counts:")
for channel, count in channel_counts.items():
self.logger.info(f" {channel}: {count}")
# Identify signal channels (all except background_channel)
signal_channels = [
ch for ch in channel_counts.index if ch != self.background_channel
]
self.logger.info(
f"Identified {len(signal_channels)} signal channels: {signal_channels}"
)
# Determine the target count for each signal channel
if min_count is None:
min_count = min(channel_counts[signal_channels]) if signal_channels else 0
self.logger.info(f"Using minimum count across signal channels: {min_count}")
else:
self.logger.info(f"Using provided minimum count: {min_count}")
# Create a new balanced DataFrame
balanced_df = pd.DataFrame()
# Keep all background channel entries
if self.background_channel in df["channel"].unique():
background_df = df[df["channel"] == self.background_channel]
balanced_df = pd.concat([balanced_df, background_df])
self.logger.info(
f"Keeping all {len(background_df)} background ({self.background_channel}) entries"
)
# For each signal channel, take only min_count entries
for channel in signal_channels:
channel_df = df[df["channel"] == channel]
# Check if this channel has enough entries
if len(channel_df) >= min_count:
# Take min_count random samples
sampled_df = channel_df.sample(n=min_count, random_state=random_seed)
balanced_df = pd.concat([balanced_df, sampled_df])
self.logger.info(
f"Channel '{channel}': sampled {min_count} from {len(channel_df)}"
)
else:
# If not enough entries, take all available
balanced_df = pd.concat([balanced_df, channel_df])
self.logger.warning(
f"Channel '{channel}' has only {len(channel_df)} entries, less than target {min_count}"
)
# Reset index of the final DataFrame
balanced_df = balanced_df.reset_index(drop=True)
self.logger.info(f"Balanced data size: {len(balanced_df)}")
self.logger.info("New channel counts:")
new_counts = balanced_df["channel"].value_counts()
for channel, count in new_counts.items():
self.logger.info(f" {channel}: {count}")
return balanced_df
def save(self, filename: str) -> None:
"""Save the preprocessor configuration with enhanced logging."""
import pickle
self.logger.info(f"Saving preprocessor configuration to {filename}")
try:
with open(filename, "wb") as f:
pickle.dump(
{
"gev_vars": self.gev_vars,
"log_vars": self.log_vars,
"tenx_vars": self.tenx_vars,
"unchanged_vars": self.unchanged_vars,
"background_channel": self.background_channel,
"clip_quantiles": self.clip_quantiles,
"normalize": self.normalize,
"transformations": self.transformations,
"feature_stats": self.feature_stats,
},
f,
)
self.logger.info("Preprocessor saved successfully")
except Exception as e:
self.logger.error(f"Error saving preprocessor: {str(e)}")
@classmethod
def load(
cls, filename: str, log_dir: str = "./preprocessing_logs"
) -> "EnhancedDataPreprocessor":
"""Load a preprocessor configuration with enhanced logging."""
import pickle
try:
with open(filename, "rb") as f:
config = pickle.load(f)
# Initialize with basic parameters
preprocessor = cls(
gev_vars=config["gev_vars"],
log_vars=config["log_vars"],
tenx_vars=config["tenx_vars"],
unchanged_vars=config.get("unchanged_vars", ()),
background_channel=config["background_channel"],
clip_quantiles=config["clip_quantiles"],
normalize=config["normalize"],
log_dir=log_dir,
)
# Restore additional attributes
preprocessor.transformations = config["transformations"]
preprocessor.feature_stats = config["feature_stats"]
preprocessor.logger.info(f"Loaded preprocessor from {filename}")
preprocessor.logger.info(
f"Loaded {len(preprocessor.feature_stats)} feature statistics"
)
return preprocessor
except Exception as e:
# Create basic logger to report error
logger = logging.getLogger("preprocessor_loader")
logger.setLevel(logging.ERROR)
handler = logging.StreamHandler()
logger.addHandler(handler)
logger.error(f"Error loading preprocessor from {filename}: {str(e)}")
raise
def _plot_feature_distributions(
self, df: pd.DataFrame, stage: str, title: str
) -> None:
"""Plot distributions of all features at a specific stage."""
if len(df.columns) == 0:
self.logger.warning(f"No features to plot for stage '{stage}'")
return
# Determine grid layout
n_features = len(df.columns)
n_cols = min(3, n_features)
n_rows = (n_features + n_cols - 1) // n_cols
fig, axes = plt.subplots(n_rows, n_cols, figsize=(15, 3 * n_rows))
if n_rows == 1 and n_cols == 1:
axes = np.array([axes])
axes = axes.flatten()
for i, feature in enumerate(df.columns):
if i < len(axes):
# Calculate histogram
sns.histplot(df[feature].dropna(), ax=axes[i], kde=False)
axes[i].set_title(f"{feature}")
# Add min/max/mean as text
stats_text = f"Min: {df[feature].min():.2g}\nMax: {df[feature].max():.2g}\nMean: {df[feature].mean():.2g}"
axes[i].text(
0.95,
0.95,
stats_text,
transform=axes[i].transAxes,
verticalalignment="top",
horizontalalignment="right",
bbox=dict(boxstyle="round", facecolor="white", alpha=0.7),
)
# Hide unused subplots
for j in range(i + 1, len(axes)):
axes[j].axis("off")
plt.suptitle(title, fontsize=16)
plt.tight_layout(rect=[0, 0, 1, 0.97])
# Save the figure
plot_path = os.path.join(self.log_dir, f"distribution_{stage}.png")
plt.savefig(plot_path, dpi=150)
plt.close()
self.logger.info(
f"Saved feature distributions for '{stage}' stage to {plot_path}"
)
def _plot_channel_distribution(
self, original_df: pd.DataFrame, balanced_df: pd.DataFrame
) -> None:
"""Plot channel distribution before and after balancing."""
if "channel" not in original_df.columns or "channel" not in balanced_df.columns:
return
# Get channel counts
original_counts = original_df["channel"].value_counts().sort_index()
balanced_counts = balanced_df["channel"].value_counts().sort_index()
# Combine into a DataFrame
counts_df = (
pd.DataFrame({"Original": original_counts, "Balanced": balanced_counts})
.fillna(0)
.astype(int)
)
# Create plot
fig, ax = plt.subplots(figsize=(12, 6))
counts_df.plot(kind="bar", ax=ax)
ax.set_title("Channel Distribution: Original vs. Balanced", fontsize=14)
ax.set_ylabel("Count")
ax.set_xlabel("Channel")
# Add data labels
for i, p in enumerate(ax.patches):
width, height = p.get_width(), p.get_height()
x, y = p.get_xy()
ax.annotate(
f"{height:g}",
(x + width / 2, y + height + 0.1),
ha="center",
va="center",
fontsize=8,
)
plt.xticks(rotation=45, ha="right")
plt.tight_layout()
# Save the figure
plot_path = os.path.join(self.log_dir, "channel_distribution.png")
plt.savefig(plot_path, dpi=150)
plt.close()
self.logger.info(f"Saved channel distribution plot to {plot_path}")
def _plot_transformation_stages(self) -> None:
"""Plot feature statistics across different transformation stages."""
# Get the stages we have
stages = [
stage
for stage in ["raw", "balanced", "transformed", "clipped", "final"]
if stage in self.snapshots
]
if len(stages) < 2:
self.logger.warning("Not enough stages to plot transformation progression")
return
feature_cols = list(self.transformations.keys())
for feature in feature_cols:
if feature not in self.snapshots[stages[0]].columns:
continue
# Collect statistics for this feature across stages
stats = []
for stage in stages:
if feature in self.snapshots[stage].columns:
feat_data = self.snapshots[stage][feature]
stats.append(
{
"Stage": stage.capitalize(),
"Min": feat_data.min(),
"Max": feat_data.max(),
"Mean": feat_data.mean(),
"Median": feat_data.median(),
"StdDev": feat_data.std(),
}
)
if not stats:
continue
stats_df = pd.DataFrame(stats)
# Plot the statistics
fig, axes = plt.subplots(2, 2, figsize=(12, 8))
axes = axes.flatten()
# Plot min/max
axes[0].plot(stats_df["Stage"], stats_df["Min"], "o-", label="Min")
axes[0].plot(stats_df["Stage"], stats_df["Max"], "s-", label="Max")
axes[0].set_title(f"{feature} - Min/Max")
axes[0].set_xticks(range(len(stats_df)))
axes[0].set_xticklabels(stats_df["Stage"], rotation=45)
axes[0].legend()
# Plot mean/median
axes[1].plot(stats_df["Stage"], stats_df["Mean"], "o-", label="Mean")
axes[1].plot(stats_df["Stage"], stats_df["Median"], "s-", label="Median")
axes[1].set_title(f"{feature} - Mean/Median")
axes[1].set_xticks(range(len(stats_df)))
axes[1].set_xticklabels(stats_df["Stage"], rotation=45)
axes[1].legend()
# Plot standard deviation
axes[2].plot(stats_df["Stage"], stats_df["StdDev"], "o-")
axes[2].set_title(f"{feature} - Standard Deviation")
axes[2].set_xticks(range(len(stats_df)))
axes[2].set_xticklabels(stats_df["Stage"], rotation=45)
# Plot range (max - min)
range_vals = stats_df["Max"] - stats_df["Min"]
axes[3].plot(stats_df["Stage"], range_vals, "o-")
axes[3].set_title(f"{feature} - Range")
axes[3].set_xticks(range(len(stats_df)))
axes[3].set_xticklabels(stats_df["Stage"], rotation=45)
plt.suptitle(f"Statistics Changes for {feature}", fontsize=16)
plt.tight_layout(rect=[0, 0, 1, 0.97])
# Save the figure
plot_path = os.path.join(self.log_dir, f"stats_changes_{feature}.png")
plt.savefig(plot_path, dpi=150)
plt.close()
self.logger.info(
f"Saved transformation statistics for '{feature}' to {plot_path}"
)
def _plot_before_after_comparisons(self) -> None:
"""Create before-after comparison plots for each feature."""
# Determine which stages to compare
if "raw" in self.snapshots and "final" in self.snapshots:
before_stage = "raw"
after_stage = "final"
elif "balanced" in self.snapshots and "final" in self.snapshots:
before_stage = "balanced"
after_stage = "final"
else:
self.logger.warning("Not enough stages to create before-after comparisons")
return
feature_cols = list(self.transformations.keys())
# For each feature, create a before-after plot
for feature in feature_cols:
if (
feature not in self.snapshots[before_stage].columns
or feature not in self.snapshots[after_stage].columns
):
continue
before_data = self.snapshots[before_stage][feature]
after_data = self.snapshots[after_stage][feature]
fig, axes = plt.subplots(1, 2, figsize=(12, 5))
# Before histogram
sns.histplot(before_data.dropna(), kde=False, ax=axes[0])
axes[0].set_title(f"{feature} - Before ({before_stage.capitalize()})")
# Add statistics
stats_text = f"Min: {before_data.min():.2g}\nMax: {before_data.max():.2g}\nMean: {before_data.mean():.2g}\nStd: {before_data.std():.2g}"
axes[0].text(
0.95,
0.95,
stats_text,
transform=axes[0].transAxes,
verticalalignment="top",
horizontalalignment="right",
bbox=dict(boxstyle="round", facecolor="white", alpha=0.7),
)
# After histogram
sns.histplot(after_data.dropna(), kde=False, ax=axes[1])
axes[1].set_title(f"{feature} - After (Processed)")
# Add statistics
stats_text = f"Min: {after_data.min():.2g}\nMax: {after_data.max():.2g}\nMean: {after_data.mean():.2g}\nStd: {after_data.std():.2g}"
axes[1].text(
0.95,
0.95,
stats_text,
transform=axes[1].transAxes,
verticalalignment="top",
horizontalalignment="right",
bbox=dict(boxstyle="round", facecolor="white", alpha=0.7),
)
plt.suptitle(f"Before vs After Preprocessing - {feature}", fontsize=16)
plt.tight_layout(rect=[0, 0, 1, 0.97])
# Save the figure
plot_path = os.path.join(self.log_dir, f"before_after_{feature}.png")
plt.savefig(plot_path, dpi=150)
plt.close()
self.logger.info(
f"Saved before-after comparison for '{feature}' to {plot_path}"
)
def _generate_report(self) -> None:
"""Generate a comprehensive markdown report with all preprocessing information."""
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
report_path = os.path.join(self.log_dir, "preprocessing_report.md")
with open(report_path, "w") as f:
f.write(f"# Preprocessing Validation Report\n\n")
f.write(f"**Generated:** {timestamp}\n\n")
f.write("## Configuration\n\n")
f.write(f"- GeV scaling variables: {', '.join(self.gev_vars) or 'None'}\n")
f.write(
f"- Log transform variables: {', '.join(self.log_vars) or 'None'}\n"
)
f.write(
f"- Tenx scaling variables: {', '.join(self.tenx_vars) or 'None'}\n"
)
f.write(
f"- Unchanged variables: {', '.join(self.unchanged_vars) or 'None'}\n"
)
f.write(f"- Background channel: {self.background_channel}\n")
f.write(f"- Clipping quantiles: {self.clip_quantiles}\n")
f.write(f"- Normalization enabled: {self.normalize}\n\n")
f.write("## Transformation Parameters\n\n")
f.write(
"| Feature | Transform | Epsilon | Lower Bound | Upper Bound | Min Value | Max Value |\n"
)
f.write(
"|---------|-----------|---------|-------------|-------------|-----------|----------|\n"
)
for feature in sorted(self.transformations.keys()):
transform = self.transformations.get(feature, "none")
epsilon = self.feature_stats.get(f"{feature}_epsilon", "N/A")
lower = self.feature_stats.get(f"{feature}_lower", "N/A")
upper = self.feature_stats.get(f"{feature}_upper", "N/A")
min_val = self.feature_stats.get(f"{feature}_min", "N/A")
max_val = self.feature_stats.get(f"{feature}_max", "N/A")
f.write(
f"| {feature} | {transform} | {epsilon} | {lower} | {upper} | {min_val} | {max_val} |\n"
)
f.write("\n## Visualization Links\n\n")
# Data distribution links
f.write("### Distribution Visualizations\n\n")
stages = ["raw", "balanced", "transformed", "clipped", "final"]
for stage in stages:
if os.path.exists(
os.path.join(self.log_dir, f"distribution_{stage}.png")
):
f.write(
f"- [{stage.capitalize()} Data Distributions](distribution_{stage}.png)\n"
)
# Feature transformation links
f.write("\n### Transformation Statistics\n\n")
for feature in sorted(self.transformations.keys()):
if os.path.exists(
os.path.join(self.log_dir, f"stats_changes_{feature}.png")
):
f.write(f"- [{feature} Statistics](stats_changes_{feature}.png)\n")
# Before-after comparison links
f.write("\n### Before-After Comparisons\n\n")
for feature in sorted(self.transformations.keys()):
if os.path.exists(