44from functools import wraps
55import sys
66import warnings
7- #pylint: disable=wrong-import-position
7+
8+ # pylint: disable=wrong-import-position
89
910warnings .simplefilter (action = "ignore" , category = FutureWarning )
1011from textwrap import dedent
@@ -238,13 +239,13 @@ def __init__(self, *args, **kwargs):
238239 if "alpha" in self ._fitted_parameter_names :
239240 raise NameError ("'alpha' in _fitted_parameter_names is a lifelines reserved word. Try 'alpha_' instead." )
240241
241- def _check_cumulative_hazard_is_monotone_and_positive (self ):
242- test_times = np .linspace (0.01 , 100 , 15 )
242+ def _check_cumulative_hazard_is_monotone_and_positive (self , durations ):
243243 class_name = self .__class__ .__name__
244244
245- cumulative_hazard = self ._cumulative_hazard (self ._initial_values , test_times )
245+ cumulative_hazard = self ._cumulative_hazard (self ._initial_values , durations )
246246 if not np .all (cumulative_hazard > 0 ):
247- raise StatisticalWarning (dedent (
247+ warnings .warn (
248+ dedent (
248249 """\
249250 Cumulative hazard is not strictly positive. For example, try:
250251
@@ -253,11 +254,17 @@ def _check_cumulative_hazard_is_monotone_and_positive(self):
253254 >>> fitter._cumulative_hazard(fitter._initial_values, test_times)
254255
255256 This may harm convergence, or return nonsensical results.
256- """ .format (class_name )))
257+ """ .format (
258+ class_name
259+ )
260+ ),
261+ StatisticalWarning ,
262+ )
257263
258- derivative_of_cumulative_hazard = self ._hazard (self ._initial_values , test_times )
264+ derivative_of_cumulative_hazard = self ._hazard (self ._initial_values , durations )
259265 if not np .all (derivative_of_cumulative_hazard >= 0 ):
260- raise StatisticalWarning (dedent (
266+ warnings .warn (
267+ dedent (
261268 """\
262269 Cumulative hazard is not strictly non-decreasing. For example, try:
263270
@@ -266,7 +273,12 @@ def _check_cumulative_hazard_is_monotone_and_positive(self):
266273 >>> fitter._hazard(fitter._initial_values, test_times)
267274
268275 This may harm convergence, or return nonsensical results.
269- """ .format (class_name )))
276+ """ .format (
277+ class_name
278+ )
279+ ),
280+ StatisticalWarning ,
281+ )
270282
271283 def _initial_values_from_bounds (self ):
272284 for (lb , ub ) in self ._bounds :
@@ -472,16 +484,15 @@ def fit(
472484 if event_observed is not None :
473485 check_nans_or_infs (event_observed )
474486
475- self ._check_cumulative_hazard_is_monotone_and_positive ()
476-
477-
478487 self .durations = np .asarray (durations , dtype = float )
479488 # check for negative or 0 durations - these are not allowed in a weibull model.
480489 if np .any (self .durations <= 0 ):
481490 raise ValueError (
482491 "This model does not allow for non-positive durations. Suggestion: add a small positive value to zero elements."
483492 )
484493
494+ self ._check_cumulative_hazard_is_monotone_and_positive (self .durations )
495+
485496 self .event_observed = (
486497 np .asarray (event_observed , dtype = int ) if event_observed is not None else np .ones_like (self .durations )
487498 )
0 commit comments