Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
header_df = pd.DataFrame.from_records(headers).set_index(0)
header_html = header_df.to_html(header=False, notebook=True, index_names=False)
summary_html = summary_df.to_html(
float_format=utils.format_floats(decimals),
formatters={
**{c: utils.format_exp_floats(decimals) for c in columns if "exp(" in c},
**{"p": utils.format_p_value(decimals)},
},
)
footers = []
with np.errstate(invalid="ignore", divide="ignore"):
try:
if utils.CensoringType.is_right_censoring(self.model) and self.model._KNOWN_MODEL:
footers.append(("Concordance", "{:.{prec}f}".format(self.model.score_, prec=decimals)))
except AttributeError:
pass
try:
sr = self.model.log_likelihood_ratio_test()
footers.append(
(
"Log-likelihood ratio test",
"{:.{prec}f} on {} df, -log2(p)={:.{prec}f}".format(
sr.test_statistic, sr.degrees_freedom, -np.log2(sr.p_value), prec=decimals
),
)
)
except AttributeError:
pass
def _create_initial_point(self, Ts, E, entries, weights, Xs):
# detect constant columns
constant_col = (Xs.df.var(0) < 1e-8).idxmax()
import lifelines
uni_model = lifelines.GeneralizedGammaFitter()
with warnings.catch_warnings():
warnings.simplefilter("ignore")
if utils.CensoringType.is_right_censoring(self):
uni_model.fit_right_censoring(Ts[0], event_observed=E, entry=entries, weights=weights)
elif utils.CensoringType.is_interval_censoring(self):
uni_model.fit_interval_censoring(Ts[0], Ts[1], event_observed=E, entry=entries, weights=weights)
elif utils.CensoringType.is_left_censoring(self):
uni_model.fit_left_censoring(Ts[1], event_observed=E, entry=entries, weights=weights)
# we may use this later in print_summary
self._ll_null_ = uni_model.log_likelihood_
d = {}
d["mu_"] = np.array([0.0] * (len(Xs.mappings["mu_"])))
if constant_col in Xs.mappings["mu_"]:
d["mu_"][Xs.mappings["mu_"].index(constant_col)] = uni_model.mu_
d["sigma_"] = np.array([0.0] * (len(Xs.mappings["sigma_"])))
def _fit_model(self, Ts, E, entry, weights, show_progress=True):
if utils.CensoringType.is_left_censoring(self):
negative_log_likelihood = self._negative_log_likelihood_left_censoring
elif utils.CensoringType.is_interval_censoring(self):
negative_log_likelihood = self._negative_log_likelihood_interval_censoring
elif utils.CensoringType.is_right_censoring(self):
negative_log_likelihood = self._negative_log_likelihood_right_censoring
with warnings.catch_warnings():
warnings.simplefilter("ignore")
results = minimize(
value_and_grad(negative_log_likelihood), # pylint: disable=no-value-for-parameter
self._initial_values,
jac=True,
method=self._scipy_fit_method,
args=(Ts, E, entry, weights),
bounds=self._bounds,
options={**{"disp": show_progress}, **self._scipy_fit_options},
)
# convergence successful.
"""
from lifelines import KaplanMeierFitter
if ax is None:
ax = plt.gca()
if timeline is None:
timeline = model.timeline
COL_EMP = "empirical CDF"
if CensoringType.is_left_censoring(model):
empirical_kmf = KaplanMeierFitter().fit_left_censoring(
model.durations, model.event_observed, label=COL_EMP, timeline=timeline
)
elif CensoringType.is_right_censoring(model):
empirical_kmf = KaplanMeierFitter().fit_right_censoring(
model.durations, model.event_observed, label=COL_EMP, timeline=timeline
)
elif CensoringType.is_interval_censoring(model):
raise NotImplementedError("lifelines does not have a non-parametric interval model yet.")
empirical_kmf.plot_cumulative_density(ax=ax, **plot_kwargs)
dist = get_distribution_name_of_lifelines_model(model)
dist_object = create_scipy_stats_model_from_lifelines_model(model)
ax.plot(timeline, dist_object.cdf(timeline), label="fitted %s" % dist, **plot_kwargs)
ax.legend()
return ax
def _create_initial_point(self, Ts, E, *args):
if CensoringType.is_right_censoring(self):
log_data = log(Ts[0])
elif CensoringType.is_left_censoring(self):
log_data = log(Ts[1])
elif CensoringType.is_interval_censoring(self):
# this fails if Ts[1] == Ts[0], so we add a some fudge factors.
log_data = log(Ts[1] - Ts[0] + 0.01)
return np.array([log_data.mean(), log(log_data.std() + 0.01), 0.1])
"""
from lifelines.utils import qth_survival_times
from lifelines import KaplanMeierFitter
if ax is None:
ax = plt.gca()
dist = get_distribution_name_of_lifelines_model(model)
dist_object = create_scipy_stats_model_from_lifelines_model(model)
COL_EMP = "empirical quantiles"
COL_THEO = "fitted %s quantiles" % dist
if CensoringType.is_left_censoring(model):
kmf = KaplanMeierFitter().fit_left_censoring(model.durations, model.event_observed, label=COL_EMP)
elif CensoringType.is_right_censoring(model):
kmf = KaplanMeierFitter().fit_right_censoring(model.durations, model.event_observed, label=COL_EMP)
elif CensoringType.is_interval_censoring(model):
raise NotImplementedError("lifelines does not have a non-parametric interval model yet.")
q = np.unique(kmf.cumulative_density_.values[:, 0])
# this is equivalent to the old code `qth_survival_times(q, kmf.cumulative_density, cdf=True)`
quantiles = qth_survival_times(1 - q, kmf.survival_function_)
quantiles[COL_THEO] = dist_object.ppf(q)
quantiles = quantiles.replace([-np.inf, 0, np.inf], np.nan).dropna()
max_, min_ = quantiles[COL_EMP].max(), quantiles[COL_EMP].min()
quantiles.plot.scatter(COL_THEO, COL_EMP, c="none", edgecolor="k", lw=0.5, ax=ax)
ax.plot([min_, max_], [min_, max_], c="k", ls=":", lw=1.0)
ax.set_ylim(min_, max_)
ax.set_xlim(min_, max_)