From daff410b46d104600414f2aae0057af9c965dc0f Mon Sep 17 00:00:00 2001 From: edgar Date: Thu, 3 Dec 2026 22:21:58 -0600 Subject: [PATCH] =?UTF-8?q?No=20fallar=20al=20predecir=20un=20grupo=20sin?= =?UTF-8?q?=20categor=C3=ADas=20vistas=20en=20el=20ajuste?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- models/cacu/pim.py | 2 ++ tests/test_pim_unseen_level.py | 15 +++++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 tests/test_pim_unseen_level.py diff --git a/models/cacu/pim.py b/models/cacu/pim.py index eeacab5..048f5ab 100644 --- a/models/cacu/pim.py +++ b/models/cacu/pim.py @@ -262,6 +262,8 @@ class Mixture(BaseEstimator): continue members = X.iloc[index] placed = self._placed(members, self.seen_[key]) + if not placed.any(): # nobody to score: they keep no value + continue prevalence, incidence = self._design(members[placed], self.seen_[key]) design = np.column_stack([prevalence, incidence]) unique, inverse = np.unique(design, axis=0, return_inverse=True) diff --git a/tests/test_pim_unseen_level.py b/tests/test_pim_unseen_level.py new file mode 100644 index 0000000..1bef71c --- /dev/null +++ b/tests/test_pim_unseen_level.py @@ -0,0 +1,15 @@ +"""A stratum in which no row is at a level the fit saw gives no value, not an error.""" + +import numpy as np +import pandas as pd + +from models.cacu.pim import Mixture + + +def test_rows_only_at_unseen_levels_are_left_without_a_risk(): + X = pd.DataFrame({"cytology": ["NILM", "NILM", "ASC-US", "ASC-US"] * 10}) + y = pd.DataFrame({"c": [0, 1] * 20, "l": [1.0, 0.0] * 20, "r": [np.inf, 0.0] * 20}) + y.loc[::5, ["c", "l", "r"]] = [-999, 0.5, 2.0] + mixture = Mixture((1, 5), ["cytology"], (), {"cytology": ["NILM", "ASC-US", "High Grade"]}) + mixture.fit(X, y) + assert np.isnan(mixture.predict(pd.DataFrame({"cytology": ["High Grade"]}))).all()