Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_qth_survival_times_with_varying_datatype_inputs():
sf_list = [1.0, 0.75, 0.5, 0.25, 0.0]
sf_array = np.array([1.0, 0.75, 0.5, 0.25, 0.0])
sf_df_no_index = pd.DataFrame([1.0, 0.75, 0.5, 0.25, 0.0])
sf_df_index = pd.DataFrame([1.0, 0.75, 0.5, 0.25, 0.0], index=[10, 20, 30, 40, 50])
sf_series_index = pd.Series([1.0, 0.75, 0.5, 0.25, 0.0], index=[10, 20, 30, 40, 50])
sf_series_no_index = pd.Series([1.0, 0.75, 0.5, 0.25, 0.0])
q = 0.5
assert utils.qth_survival_times(q, sf_list) == 2
assert utils.qth_survival_times(q, sf_array) == 2
assert utils.qth_survival_times(q, sf_df_no_index) == 2
assert utils.qth_survival_times(q, sf_df_index) == 30
assert utils.qth_survival_times(q, sf_series_index) == 30
assert utils.qth_survival_times(q, sf_series_no_index) == 2
def predict_percentile(self, df, *, p=0.5, conditional_after=None) -> pd.DataFrame:
subjects = utils._get_index(df)
return utils.qth_survival_times(
p, self.predict_survival_function(df, conditional_after=conditional_after)[subjects]
).T
"""
Returns the median lifetimes for the individuals.
http://stats.stackexchange.com/questions/102986/percentile-loss-functions
Parameters
----------
X: a (n,d) covariate numpy array or DataFrame
If a DataFrame, columns
can be in any order. If a numpy array, columns must be in the
same order as the training data.
p: float
default: 0.5
"""
index = _get_index(X)
return qth_survival_times(p, self.predict_survival_function(X)[index]).T
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_)
return ax
def predict_percentile(self, df, *, ancillary_df=None, p=0.5, conditional_after=None) -> pd.DataFrame:
return utils.qth_survival_times(
p, self.predict_survival_function(df, ancillary_df=ancillary_df, conditional_after=conditional_after)
)
same order as the training data.
p: float, optional (default=0.5)
the percentile, must be between 0 and 1.
conditional_after: iterable, optional
Must be equal is size to X.shape[0] (denoted `n` above). An iterable (array, list, series) of possibly non-zero values that represent how long the
subject has already lived for. Ex: if :math:`T` is the unknown event time, then this represents
:math:`T | T > s`. This is useful for knowing the *remaining* hazard/survival of censored subjects.
The new timeline is the remaining duration of the subject, i.e. normalized back to starting at 0.
See Also
--------
predict_median
"""
subjects = _get_index(X)
return qth_survival_times(p, self.predict_survival_function(X, conditional_after=conditional_after)[subjects]).T
can be in any order. If a numpy array, columns must be in the
same order as the training data.
p: float, optional (default=0.5)
the percentile, must be between 0 and 1.
Returns
-------
percentiles: DataFrame
See Also
--------
predict_median
"""
subjects = _get_index(X)
return qth_survival_times(p, self.predict_survival_function(X)[subjects]).T