Skip to content

Commit c0d568c

Browse files
committed
Fix UnboundLocalError when collect() is called before fit()
By deriving outcome and compevent model lists under a single guard with a None fallback
1 parent 592da92 commit c0d568c

2 files changed

Lines changed: 32 additions & 5 deletions

File tree

pySEQTarget/SEQuential.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -453,13 +453,17 @@ def collect(self) -> SEQoutput:
453453
"collection_time": self._time_collected,
454454
}
455455

456-
if self.compevent_colname is not None:
457-
compevent_models = [model["compevent"] for model in self.outcome_model]
458-
else:
459-
compevent_models = None
460-
461456
if self.outcome_model is not None:
462457
outcome_models = [model["outcome"] for model in self.outcome_model]
458+
if self.compevent_colname is not None:
459+
compevent_models = [model["compevent"] for model in self.outcome_model]
460+
else:
461+
compevent_models = None
462+
else:
463+
# collect() before fit(): no models to report, but the rest of the
464+
# output (diagnostics, timings) is still valid.
465+
outcome_models = None
466+
compevent_models = None
463467

464468
if self.risk_estimates is None:
465469
risk_ratio = risk_difference = None

tests/test_accessor.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,29 @@ def test_ITT_collector():
3131
collector.retrieve_data("unique_switches")
3232

3333

34+
def test_collect_before_fit():
35+
# collect() without fit() must return an SEQoutput with None models rather
36+
# than raising UnboundLocalError. Diagnostics from expand() still come
37+
# through.
38+
s = SEQuential(
39+
load_data("SEQdata"),
40+
id_col="ID",
41+
time_col="time",
42+
eligible_col="eligible",
43+
treatment_col="tx_init",
44+
outcome_col="outcome",
45+
time_varying_cols=["N", "L", "P"],
46+
fixed_cols=["sex"],
47+
method="ITT",
48+
parameters=SEQopts(),
49+
)
50+
s.expand()
51+
collector = s.collect()
52+
assert collector.outcome_models is None
53+
assert collector.compevent_models is None
54+
assert collector.retrieve_data("unique_outcomes").height > 0
55+
56+
3457
def test_censoring_collector_switch_diagnostics():
3558
# Under method="censoring" the switch diagnostics exist and must be
3659
# retrievable (regression for dict.has_key).

0 commit comments

Comments
 (0)