Skip to content

Commit

Permalink
catch zero division
Browse files Browse the repository at this point in the history
  • Loading branch information
leoschwarz committed Jun 7, 2024
1 parent 25beb57 commit 68619b3
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/depiction/calibration/spectrum/calibration_method_mcc.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numpy as np
import scipy.stats
import statsmodels.api as sm
from loguru import logger
from numpy.typing import NDArray
from statsmodels.robust.norms import HuberT
from xarray import DataArray
Expand Down Expand Up @@ -39,9 +40,13 @@ def extract_spectrum_features(self, peak_mz_arr: NDArray[float], peak_int_arr: N
# Add a constant term with the intercept set to zero
X = delta.reshape(-1, 1)
# Fit the model
robust_model = sm.RLM(delta_lambda, X, M=HuberT())
results = robust_model.fit()
slope = results.params[0]
try:
robust_model = sm.RLM(delta_lambda, X, M=HuberT())
results = robust_model.fit()
slope = results.params[0]
except ZeroDivisionError:
logger.warning("ZeroDivisionError occurred during fitting. Setting slope to 0.")
slope = 0
peak_mz_corrected = peak_mz_arr * (1 - slope)
delta_intercept = self.compute_distance_from_MCC(peak_mz_corrected, l_none)
intercept_coef = scipy.stats.trim_mean(delta_intercept, 0.25)
Expand Down

0 comments on commit 68619b3

Please sign in to comment.