lifelines 0.27.7__py3-none-any.whl → 0.28.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- lifelines/datasets/__init__.py +2 -2
- lifelines/exceptions.py +4 -0
- lifelines/fitters/__init__.py +33 -20
- lifelines/fitters/aalen_johansen_fitter.py +44 -0
- lifelines/fitters/breslow_fleming_harrington_fitter.py +9 -1
- lifelines/fitters/cox_time_varying_fitter.py +15 -10
- lifelines/fitters/coxph_fitter.py +17 -13
- lifelines/fitters/generalized_gamma_fitter.py +6 -5
- lifelines/fitters/kaplan_meier_fitter.py +9 -3
- lifelines/fitters/mixins.py +8 -3
- lifelines/fitters/nelson_aalen_fitter.py +2 -2
- lifelines/plotting.py +163 -91
- lifelines/utils/__init__.py +5 -7
- lifelines/version.py +1 -1
- {lifelines-0.27.7.dist-info → lifelines-0.28.0.dist-info}/METADATA +9 -11
- {lifelines-0.27.7.dist-info → lifelines-0.28.0.dist-info}/RECORD +19 -22
- {lifelines-0.27.7.dist-info → lifelines-0.28.0.dist-info}/WHEEL +1 -1
- lifelines/datasets/ACTG175.csv +0 -2140
- lifelines/metrics.py +0 -60
- lifelines/utils/sklearn_adapter.py +0 -135
- {lifelines-0.27.7.dist-info → lifelines-0.28.0.dist-info}/LICENSE +0 -0
- {lifelines-0.27.7.dist-info → lifelines-0.28.0.dist-info}/top_level.txt +0 -0
lifelines/metrics.py
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
|
|
3
|
-
import numpy as np
|
|
4
|
-
from lifelines.utils import concordance_index, ConvergenceError
|
|
5
|
-
|
|
6
|
-
__all__ = ["concordance_index", "uncensored_l2_log_loss", "uncensored_l1_log_loss"]
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
def uncensored_l1_log_loss(event_times, predicted_event_times, event_observed=None):
|
|
10
|
-
r"""
|
|
11
|
-
Calculates the l1 log-loss of predicted event times to true event times for *non-censored*
|
|
12
|
-
individuals only.
|
|
13
|
-
|
|
14
|
-
.. math:: 1/N \sum_{i} |log(t_i) - log(q_i)|
|
|
15
|
-
|
|
16
|
-
Parameters
|
|
17
|
-
----------
|
|
18
|
-
event_times:
|
|
19
|
-
a (n,) array of observed survival times.
|
|
20
|
-
predicted_event_times:
|
|
21
|
-
a (n,) array of predicted survival times.
|
|
22
|
-
event_observed:
|
|
23
|
-
a (n,) array of censored flags, 1 if observed, 0 if not. Default None assumes all observed.
|
|
24
|
-
|
|
25
|
-
Returns
|
|
26
|
-
-------
|
|
27
|
-
l1-log-loss: a scalar
|
|
28
|
-
"""
|
|
29
|
-
if event_observed is None:
|
|
30
|
-
event_observed = np.ones_like(event_times, dtype=bool)
|
|
31
|
-
|
|
32
|
-
ix = event_observed.astype(bool)
|
|
33
|
-
return np.abs(np.log(event_times[ix]) - np.log(predicted_event_times[ix])).mean()
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
def uncensored_l2_log_loss(event_times, predicted_event_times, event_observed=None):
|
|
37
|
-
r"""
|
|
38
|
-
Calculates the l2 log-loss of predicted event times to true event times for *non-censored*
|
|
39
|
-
individuals only.
|
|
40
|
-
|
|
41
|
-
.. math:: 1/N \sum_{i} (log(t_i) - log(q_i))**2
|
|
42
|
-
|
|
43
|
-
Parameters
|
|
44
|
-
----------
|
|
45
|
-
event_times:
|
|
46
|
-
a (n,) array of observed survival times.
|
|
47
|
-
predicted_event_times:
|
|
48
|
-
a (n,) array of predicted survival times.
|
|
49
|
-
event_observed:
|
|
50
|
-
a (n,) array of censored flags, 1 if observed, 0 if not. Default None assumes all observed.
|
|
51
|
-
|
|
52
|
-
Returns
|
|
53
|
-
-------
|
|
54
|
-
l2-log-loss: a scalar
|
|
55
|
-
"""
|
|
56
|
-
if event_observed is None:
|
|
57
|
-
event_observed = np.ones_like(event_times, dtype=bool)
|
|
58
|
-
|
|
59
|
-
ix = event_observed.astype(bool)
|
|
60
|
-
return np.power(np.log(event_times[ix]) - np.log(predicted_event_times[ix]), 2).mean()
|
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
import inspect
|
|
3
|
-
import pandas as pd
|
|
4
|
-
|
|
5
|
-
try:
|
|
6
|
-
from sklearn.base import BaseEstimator, RegressorMixin, MetaEstimatorMixin
|
|
7
|
-
except ImportError:
|
|
8
|
-
raise ImportError("scikit-learn must be installed on the local system to use this utility class.")
|
|
9
|
-
from . import concordance_index
|
|
10
|
-
|
|
11
|
-
__all__ = ["sklearn_adapter"]
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
def filter_kwargs(f, kwargs):
|
|
15
|
-
s = inspect.signature(f)
|
|
16
|
-
res = {k: kwargs[k] for k in s.parameters if k in kwargs}
|
|
17
|
-
return res
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
class _SklearnModel(BaseEstimator, MetaEstimatorMixin, RegressorMixin):
|
|
21
|
-
def __init__(self, **kwargs):
|
|
22
|
-
self._params = kwargs
|
|
23
|
-
self.lifelines_model = self.lifelines_model(**filter_kwargs(self.lifelines_model.__init__, self._params))
|
|
24
|
-
self._params["duration_col"] = "duration_col"
|
|
25
|
-
self._params["event_col"] = self._event_col
|
|
26
|
-
|
|
27
|
-
@property
|
|
28
|
-
def _yColumn(self):
|
|
29
|
-
return self._params["duration_col"]
|
|
30
|
-
|
|
31
|
-
@property
|
|
32
|
-
def _eventColumn(self):
|
|
33
|
-
return self._params["event_col"]
|
|
34
|
-
|
|
35
|
-
def fit(self, X, y=None):
|
|
36
|
-
"""
|
|
37
|
-
|
|
38
|
-
Parameters
|
|
39
|
-
-----------
|
|
40
|
-
|
|
41
|
-
X: DataFrame
|
|
42
|
-
must be a pandas DataFrame (with event_col included, if applicable)
|
|
43
|
-
|
|
44
|
-
"""
|
|
45
|
-
if not isinstance(X, pd.DataFrame):
|
|
46
|
-
raise ValueError("X must be a DataFrame. Got type: {}".format(type(X)))
|
|
47
|
-
|
|
48
|
-
X = X.copy()
|
|
49
|
-
|
|
50
|
-
if y is not None:
|
|
51
|
-
X.insert(len(X.columns), self._yColumn, y, allow_duplicates=False)
|
|
52
|
-
|
|
53
|
-
fit = getattr(self.lifelines_model, self._fit_method)
|
|
54
|
-
self.lifelines_model = fit(df=X, **filter_kwargs(fit, self._params))
|
|
55
|
-
return self
|
|
56
|
-
|
|
57
|
-
def set_params(self, **params):
|
|
58
|
-
for key, value in params.items():
|
|
59
|
-
setattr(self.lifelines_model, key, value)
|
|
60
|
-
return self
|
|
61
|
-
|
|
62
|
-
def get_params(self, deep=True):
|
|
63
|
-
out = {}
|
|
64
|
-
for name, p in inspect.signature(self.lifelines_model.__init__).parameters.items():
|
|
65
|
-
if p.kind < 4: # ignore kwargs
|
|
66
|
-
out[name] = getattr(self.lifelines_model, name)
|
|
67
|
-
return out
|
|
68
|
-
|
|
69
|
-
def predict(self, X, **kwargs):
|
|
70
|
-
"""
|
|
71
|
-
Parameters
|
|
72
|
-
------------
|
|
73
|
-
X: DataFrame or numpy array
|
|
74
|
-
|
|
75
|
-
"""
|
|
76
|
-
predictions = getattr(self.lifelines_model, self._predict_method)(X, **kwargs).squeeze().values
|
|
77
|
-
return predictions
|
|
78
|
-
|
|
79
|
-
def score(self, X, y, **kwargs):
|
|
80
|
-
"""
|
|
81
|
-
|
|
82
|
-
Parameters
|
|
83
|
-
-----------
|
|
84
|
-
|
|
85
|
-
X: DataFrame
|
|
86
|
-
must be a pandas DataFrame (with event_col included, if applicable)
|
|
87
|
-
|
|
88
|
-
"""
|
|
89
|
-
rest_columns = list(set(X.columns) - {self._yColumn, self._eventColumn})
|
|
90
|
-
|
|
91
|
-
x = X.loc[:, rest_columns]
|
|
92
|
-
e = X.loc[:, self._eventColumn] if self._eventColumn else None
|
|
93
|
-
|
|
94
|
-
if y is None:
|
|
95
|
-
y = X.loc[:, self._yColumn]
|
|
96
|
-
|
|
97
|
-
if callable(self._scoring_method):
|
|
98
|
-
res = self._scoring_method(y, self.predict(x, **kwargs), event_observed=e)
|
|
99
|
-
else:
|
|
100
|
-
raise ValueError()
|
|
101
|
-
return res
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
def sklearn_adapter(fitter, event_col=None, predict_method="predict_expectation", scoring_method=concordance_index):
|
|
105
|
-
"""
|
|
106
|
-
This function wraps lifelines models into a scikit-learn compatible API. The function returns a
|
|
107
|
-
class that can be instantiated with parameters (similar to a scikit-learn class).
|
|
108
|
-
|
|
109
|
-
Parameters
|
|
110
|
-
----------
|
|
111
|
-
|
|
112
|
-
fitter: class
|
|
113
|
-
The class (not an instance) to be wrapper. Example: ``CoxPHFitter`` or ``WeibullAFTFitter``
|
|
114
|
-
event_col: string
|
|
115
|
-
The column in your DataFrame that represents (if applicable) the event column
|
|
116
|
-
predict_method: string
|
|
117
|
-
Can be the string ``"predict_median", "predict_expectation"``
|
|
118
|
-
scoring_method: function
|
|
119
|
-
Provide a way to produce a ``score`` on the scikit-learn model. Signature should look like (durations, predictions, event_observed=None)
|
|
120
|
-
|
|
121
|
-
"""
|
|
122
|
-
name = "SkLearn" + fitter.__name__
|
|
123
|
-
klass = type(
|
|
124
|
-
name,
|
|
125
|
-
(_SklearnModel,),
|
|
126
|
-
{
|
|
127
|
-
"lifelines_model": fitter,
|
|
128
|
-
"_event_col": event_col,
|
|
129
|
-
"_predict_method": predict_method,
|
|
130
|
-
"_fit_method": "fit",
|
|
131
|
-
"_scoring_method": staticmethod(scoring_method),
|
|
132
|
-
},
|
|
133
|
-
)
|
|
134
|
-
globals()[klass.__name__] = klass
|
|
135
|
-
return klass
|
|
File without changes
|
|
File without changes
|