77
88LOGGER = logging .getLogger (__name__ )
99
10+
1011class SplineTransformerCalibration :
1112 """Spline Transformer Calibration for Retention Time Prediction."""
1213
@@ -19,7 +20,12 @@ def __init__(self):
1920 self ._spline_model = None
2021 self ._linear_model_right = None
2122
22- def fit (self , measured_tr : np .ndarray , predicted_tr : np .ndarray , simplified : bool = False ):
23+ def fit (
24+ self ,
25+ measured_tr : np .ndarray ,
26+ predicted_tr : np .ndarray ,
27+ simplified : bool = False ,
28+ ):
2329 """
2430 Fit a SplineTransformer model to the measured and predicted retention times.
2531
@@ -40,8 +46,12 @@ def fit(self, measured_tr: np.ndarray, predicted_tr: np.ndarray, simplified: boo
4046
4147 # Check if the lengths match
4248 if len (measured_tr ) != len (predicted_tr ):
43- LOGGER .error ("Measured and predicted retention times must have the same length." )
44- raise ValueError ("Measured and predicted retention times must have the same length." )
49+ LOGGER .error (
50+ "Measured and predicted retention times must have the same length."
51+ )
52+ raise ValueError (
53+ "Measured and predicted retention times must have the same length."
54+ )
4555
4656 # Fit a SplineTransformer model
4757 if simplified :
@@ -54,7 +64,9 @@ def fit(self, measured_tr: np.ndarray, predicted_tr: np.ndarray, simplified: boo
5464 linear_model_right = linear_model
5565 else :
5666 LOGGER .info ("Using SplineTransformer with more knots for calibration." )
57- spline = SplineTransformer (degree = 4 , n_knots = int (len (measured_tr ) / 500 ) + 5 )
67+ spline = SplineTransformer (
68+ degree = 4 , n_knots = int (len (measured_tr ) / 500 ) + 5
69+ )
5870 spline_model = make_pipeline (spline , LinearRegression ())
5971 spline_model .fit (predicted_tr .reshape (- 1 , 1 ), measured_tr )
6072
@@ -83,7 +95,6 @@ def fit(self, measured_tr: np.ndarray, predicted_tr: np.ndarray, simplified: boo
8395 self ._fit = True
8496 LOGGER .info ("Calibration fitting completed successfully." )
8597
86-
8798 def transform (self , tr : np .ndarray ) -> np .ndarray :
8899 """
89100 Transform the predicted retention times using the fitted SplineTransformer model.
@@ -99,13 +110,17 @@ def transform(self, tr: np.ndarray) -> np.ndarray:
99110 The calibrated retention times.
100111 """
101112 if not self ._fit :
102- LOGGER .error ("Calibration model has not been fitted yet. Call fit() before transform()." )
103- raise RuntimeError ("Calibration model has not been fitted yet. Call fit() before transform()." )
113+ LOGGER .error (
114+ "Calibration model has not been fitted yet. Call fit() before transform()."
115+ )
116+ raise RuntimeError (
117+ "Calibration model has not been fitted yet. Call fit() before transform()."
118+ )
104119
105120 # if tr.shape[0] == 0:
106121 # return np.array([])
107122 tr_array = np .array (tr )
108- tr = tr_array .reshape (- 1 ,1 )
123+ tr = tr_array .reshape (- 1 , 1 )
109124
110125 # Get spline predictions and linear extrapolation predictions
111126 y_pred_spline = self ._spline_model .predict (tr )
@@ -120,10 +135,10 @@ def transform(self, tr: np.ndarray) -> np.ndarray:
120135 cal_preds = np .copy (y_pred_spline )
121136 cal_preds [~ within_range & (tr .ravel () < self ._calibrate_min )] = y_pred_left [
122137 ~ within_range & (tr .ravel () < self ._calibrate_min )
123- ]
138+ ]
124139 cal_preds [~ within_range & (tr .ravel () > self ._calibrate_max )] = y_pred_right [
125140 ~ within_range & (tr .ravel () > self ._calibrate_max )
126- ]
141+ ]
127142
128143 LOGGER .info ("Calibration transformation completed successfully." )
129- return np .array (cal_preds )
144+ return np .array (cal_preds )
0 commit comments