Skip to content

Commit

Permalink
Merge 9cf84d0 into fab70f7
Browse files Browse the repository at this point in the history
  • Loading branch information
CamDavidsonPilon committed Sep 27, 2019
2 parents fab70f7 + 9cf84d0 commit e046b8d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
16 changes: 8 additions & 8 deletions lifelines/fitters/__init__.py
Expand Up @@ -620,10 +620,10 @@ def print_summary(self, decimals=2, **kwargs):
multiple outputs.
"""
justify = utils.string_justify(18)
justify = utils.string_justify(25)
print(self)
print("{} = {}".format(justify("number of observations"), self.weights.sum()))
print("{} = {}".format(justify("number of events observed"), self.weights[self.event_observed > 0].sum()))
print("{} = {:g}".format(justify("number of observations"), self.weights.sum()))
print("{} = {:g}".format(justify("number of events observed"), self.weights[self.event_observed > 0].sum()))
print("{} = {:.{prec}f}".format(justify("log-likelihood"), self.log_likelihood_, prec=decimals))
print(
"{} = {}".format(
Expand Down Expand Up @@ -1721,7 +1721,7 @@ def print_summary(self, decimals=2, **kwargs):
"""

# Print information about data first
justify = utils.string_justify(18)
justify = utils.string_justify(25)
print(self)
if self.event_col:
print("{} = '{}'".format(justify("event col"), self.event_col))
Expand All @@ -1733,8 +1733,8 @@ def print_summary(self, decimals=2, **kwargs):
if self.robust:
print("{} = {}".format(justify("robust variance"), True))

print("{} = {}".format(justify("number of observations"), self.weights.sum()))
print("{} = {}".format(justify("number of events observed"), self.weights[self.event_observed > 0].sum()))
print("{} = {:g}".format(justify("number of observations"), self.weights.sum()))
print("{} = {:g}".format(justify("number of events observed"), self.weights[self.event_observed > 0].sum()))
print("{} = {:.{prec}f}".format(justify("log-likelihood"), self.log_likelihood_, prec=decimals))
print("{} = {}".format(justify("time fit was run"), self._time_fit_was_called))

Expand Down Expand Up @@ -1879,7 +1879,7 @@ def predict_cumulative_hazard(self, df, times=None, conditional_after=None):
"""
df = df.copy().astype(float)
times = utils.coalesce(times, self.timeline, np.unique(self.durations))
times = utils.coalesce(times, self.timeline)
times = np.atleast_1d(times).astype(float)

if isinstance(df, pd.Series):
Expand Down Expand Up @@ -2946,7 +2946,7 @@ def predict_cumulative_hazard(self, df, ancillary_df=None, times=None, condition
predict_percentile, predict_expectation, predict_survival_function
"""
df = df.copy().astype(float)
times = utils.coalesce(times, self.timeline, np.unique(self.durations))
times = utils.coalesce(times, self.timeline)
times = np.atleast_1d(times).astype(float)

if isinstance(df, pd.Series):
Expand Down
6 changes: 3 additions & 3 deletions lifelines/fitters/coxph_fitter.py
Expand Up @@ -1267,7 +1267,7 @@ def print_summary(self, decimals=2, **kwargs):
"""

# Print information about data first
justify = string_justify(18)
justify = string_justify(25)
print(self)
print("{} = '{}'".format(justify("duration col"), self.duration_col))

Expand All @@ -1288,8 +1288,8 @@ def print_summary(self, decimals=2, **kwargs):
if self.penalizer > 0:
print("{} = {}".format(justify("penalizer"), self.penalizer))

print("{} = {}".format(justify("number of observations"), self.weights.sum()))
print("{} = {}".format(justify("number of events observed"), self.weights[self.event_observed > 0].sum()))
print("{} = {:g}".format(justify("number of observations"), self.weights.sum()))
print("{} = {:g}".format(justify("number of events observed"), self.weights[self.event_observed > 0].sum()))
print("{} = {:.{prec}f}".format(justify("partial log-likelihood"), self.log_likelihood_, prec=decimals))
print("{} = {}".format(justify("time fit was run"), self._time_fit_was_called))

Expand Down
27 changes: 21 additions & 6 deletions tests/test_estimation.py
Expand Up @@ -295,6 +295,14 @@ def test_parameteric_models_all_can_do_interval_censoring(self, known_parametric
for fitter in known_parametric_univariate_fitters:
f = fitter().fit_interval_censoring(df["left"], df["right"])

def test_parameteric_models_all_can_do_interval_censoring_with_prediction(
self, known_parametric_univariate_fitters
):
df = load_diabetes()
for fitter in known_parametric_univariate_fitters:
f = fitter().fit_interval_censoring(df["left"], df["right"])
f.predict(3.0)

def test_parameteric_models_fail_if_passing_in_bad_event_data(self, known_parametric_univariate_fitters):
df = load_diabetes()
for fitter in known_parametric_univariate_fitters:
Expand Down Expand Up @@ -2195,7 +2203,6 @@ def test_weibull_interval_censoring_inference_on_known_R_output(self, aft):
df["E"] = df["left"] == df["right"]

aft.fit_interval_censoring(df, "left", "right", "E")
print(aft.summary)
npt.assert_allclose(aft.summary.loc[("lambda_", "gender"), "coef"], 0.04576, rtol=1e-3)
npt.assert_allclose(aft.summary.loc[("lambda_", "_intercept"), "coef"], np.log(18.31971), rtol=1e-4)
npt.assert_allclose(aft.summary.loc[("rho_", "_intercept"), "coef"], np.log(2.82628), rtol=1e-4)
Expand Down Expand Up @@ -2228,7 +2235,6 @@ def test_aft_weibull_with_weights(self, rossi, aft):
npt.assert_allclose(aft.summary.loc[("lambda_", "_intercept"), "coef"], 4.93041526, rtol=1e-2)
npt.assert_allclose(aft.summary.loc[("rho_", "_intercept"), "coef"], 0.28612353, rtol=1e-4)

@pytest.mark.xfail()
def test_aft_weibull_with_ancillary_model_and_with_weights(self, rossi):
"""
library('flexsurv')
Expand All @@ -2237,10 +2243,19 @@ def test_aft_weibull_with_ancillary_model_and_with_weights(self, rossi):
"""
wf = WeibullAFTFitter(penalizer=0).fit(rossi, "week", "arrest", weights_col="age", ancillary_df=rossi[["prio"]])

npt.assert_allclose(wf.summary.loc[("lambda_", "fin"), "coef"], 0.360792647, rtol=1e-3)
npt.assert_allclose(wf.summary.loc[("rho_", "prio"), "coef"], -0.025505680, rtol=1e-4)
npt.assert_allclose(wf.summary.loc[("lambda_", "_intercept"), "coef"], 4.707300215, rtol=1e-2)
npt.assert_allclose(wf.summary.loc[("rho_", "_intercept"), "coef"], 0.367701670, rtol=1e-4)
npt.assert_allclose(wf.summary.loc[("lambda_", "fin"), "coef"], 0.39347, rtol=1e-3)
npt.assert_allclose(wf.summary.loc[("lambda_", "_intercept"), "coef"], np.log(140.55112), rtol=1e-2)
npt.assert_allclose(wf.summary.loc[("rho_", "_intercept"), "coef"], np.log(1.25981), rtol=1e-4)
npt.assert_allclose(wf.summary.loc[("rho_", "prio"), "coef"], 0.01485, rtol=1e-4)

def test_aft_weibull_can_do_interval_prediction(self, aft):
# https://github.com/CamDavidsonPilon/lifelines/issues/839
df = load_diabetes()
df["gender"] = df["gender"] == "male"
df["E"] = df["left"] == df["right"]

aft.fit_interval_censoring(df, "left", "right", "E")
aft.predict_survival_function(df)


class TestCoxPHFitter:
Expand Down

0 comments on commit e046b8d

Please sign in to comment.