survivalist
1from importlib.metadata import PackageNotFoundError, version 2import platform 3import sys 4 5from sklearn.pipeline import Pipeline, _final_estimator_has 6from sklearn.utils.metaestimators import available_if 7 8from .util import property_available_if 9 10 11def _get_version(name): 12 try: 13 pkg_version = version(name) 14 except ImportError: 15 pkg_version = None 16 return pkg_version 17 18 19def show_versions(): 20 sys_info = { 21 "Platform": platform.platform(), 22 "Python version": f"{platform.python_implementation()} {platform}", 23 "Python interpreter": sys.executable, 24 } 25 26 deps = [ 27 "survivalist", 28 "scikit-learn", 29 "numpy", 30 "scipy", 31 "pandas", 32 "numexpr", 33 "ecos", 34 "osqp", 35 "joblib", 36 "matplotlib", 37 "pytest", 38 "sphinx", 39 "Cython", 40 "pip", 41 "setuptools", 42 ] 43 minwidth = max( 44 max(map(len, deps)), 45 max(map(len, sys_info.keys())), 46 ) 47 fmt = "{0:<%ds}: {1}" % minwidth 48 49 print("SYSTEM") 50 print("------") 51 for name, version_string in sys_info.items(): 52 print(fmt.format(name, version_string)) 53 54 print("\nDEPENDENCIES") 55 print("------------") 56 for dep in deps: 57 version_string = _get_version(dep) 58 print(fmt.format(dep, version_string)) 59 60 61@available_if(_final_estimator_has("predict_cumulative_hazard_function")) 62def predict_cumulative_hazard_function(self, X, **kwargs): 63 """Predict cumulative hazard function. 64 65 The cumulative hazard function for an individual 66 with feature vector :math:`x` is defined as 67 68 .. math:: 69 70 H(t \\mid x) = \\exp(x^\\top \\beta) H_0(t) , 71 72 where :math:`H_0(t)` is the baseline hazard function, 73 estimated by Breslow's estimator. 74 75 Parameters 76 ---------- 77 X : array-like, shape = (n_samples, n_features) 78 Data matrix. 79 80 Returns 81 ------- 82 cum_hazard : ndarray, shape = (n_samples,) 83 Predicted cumulative hazard functions. 84 """ 85 Xt = X 86 for _, _, transform in self._iter(with_final=False): 87 Xt = transform.transform(Xt) 88 return self.steps[-1][-1].predict_cumulative_hazard_function(Xt, **kwargs) 89 90 91@available_if(_final_estimator_has("predict_survival_function")) 92def predict_survival_function(self, X, **kwargs): 93 """Predict survival function. 94 95 The survival function for an individual 96 with feature vector :math:`x` is defined as 97 98 .. math:: 99 100 S(t \\mid x) = S_0(t)^{\\exp(x^\\top \\beta)} , 101 102 where :math:`S_0(t)` is the baseline survival function, 103 estimated by Breslow's estimator. 104 105 Parameters 106 ---------- 107 X : array-like, shape = (n_samples, n_features) 108 Data matrix. 109 110 Returns 111 ------- 112 survival : ndarray, shape = (n_samples,) 113 Predicted survival functions. 114 """ 115 Xt = X 116 for _, _, transform in self._iter(with_final=False): 117 Xt = transform.transform(Xt) 118 return self.steps[-1][-1].predict_survival_function(Xt, **kwargs) 119 120 121@property_available_if(_final_estimator_has("_predict_risk_score")) 122def _predict_risk_score(self): 123 return self.steps[-1][-1]._predict_risk_score 124 125 126def patch_pipeline(): 127 Pipeline.predict_survival_function = predict_survival_function 128 Pipeline.predict_cumulative_hazard_function = predict_cumulative_hazard_function 129 Pipeline._predict_risk_score = _predict_risk_score 130 131 132try: 133 __version__ = version("survivalist") 134except PackageNotFoundError: # pragma: no cover 135 # package is not installed 136 __version__ = "unknown" 137 138patch_pipeline()
20def show_versions(): 21 sys_info = { 22 "Platform": platform.platform(), 23 "Python version": f"{platform.python_implementation()} {platform}", 24 "Python interpreter": sys.executable, 25 } 26 27 deps = [ 28 "survivalist", 29 "scikit-learn", 30 "numpy", 31 "scipy", 32 "pandas", 33 "numexpr", 34 "ecos", 35 "osqp", 36 "joblib", 37 "matplotlib", 38 "pytest", 39 "sphinx", 40 "Cython", 41 "pip", 42 "setuptools", 43 ] 44 minwidth = max( 45 max(map(len, deps)), 46 max(map(len, sys_info.keys())), 47 ) 48 fmt = "{0:<%ds}: {1}" % minwidth 49 50 print("SYSTEM") 51 print("------") 52 for name, version_string in sys_info.items(): 53 print(fmt.format(name, version_string)) 54 55 print("\nDEPENDENCIES") 56 print("------------") 57 for dep in deps: 58 version_string = _get_version(dep) 59 print(fmt.format(dep, version_string))
62@available_if(_final_estimator_has("predict_cumulative_hazard_function")) 63def predict_cumulative_hazard_function(self, X, **kwargs): 64 """Predict cumulative hazard function. 65 66 The cumulative hazard function for an individual 67 with feature vector :math:`x` is defined as 68 69 .. math:: 70 71 H(t \\mid x) = \\exp(x^\\top \\beta) H_0(t) , 72 73 where :math:`H_0(t)` is the baseline hazard function, 74 estimated by Breslow's estimator. 75 76 Parameters 77 ---------- 78 X : array-like, shape = (n_samples, n_features) 79 Data matrix. 80 81 Returns 82 ------- 83 cum_hazard : ndarray, shape = (n_samples,) 84 Predicted cumulative hazard functions. 85 """ 86 Xt = X 87 for _, _, transform in self._iter(with_final=False): 88 Xt = transform.transform(Xt) 89 return self.steps[-1][-1].predict_cumulative_hazard_function(Xt, **kwargs)
Predict cumulative hazard function.
The cumulative hazard function for an individual with feature vector \( x \) is defined as
$$H(t \mid x) = \exp(x^\top \beta) H_0(t) ,$$
where \( H_0(t) \) is the baseline hazard function, estimated by Breslow's estimator.
Parameters
X : array-like, shape = (n_samples, n_features) Data matrix.
Returns
cum_hazard : ndarray, shape = (n_samples,) Predicted cumulative hazard functions.
92@available_if(_final_estimator_has("predict_survival_function")) 93def predict_survival_function(self, X, **kwargs): 94 """Predict survival function. 95 96 The survival function for an individual 97 with feature vector :math:`x` is defined as 98 99 .. math:: 100 101 S(t \\mid x) = S_0(t)^{\\exp(x^\\top \\beta)} , 102 103 where :math:`S_0(t)` is the baseline survival function, 104 estimated by Breslow's estimator. 105 106 Parameters 107 ---------- 108 X : array-like, shape = (n_samples, n_features) 109 Data matrix. 110 111 Returns 112 ------- 113 survival : ndarray, shape = (n_samples,) 114 Predicted survival functions. 115 """ 116 Xt = X 117 for _, _, transform in self._iter(with_final=False): 118 Xt = transform.transform(Xt) 119 return self.steps[-1][-1].predict_survival_function(Xt, **kwargs)
Predict survival function.
The survival function for an individual with feature vector \( x \) is defined as
$$S(t \mid x) = S_0(t)^{\exp(x^\top \beta)} ,$$
where \( S_0(t) \) is the baseline survival function, estimated by Breslow's estimator.
Parameters
X : array-like, shape = (n_samples, n_features) Data matrix.
Returns
survival : ndarray, shape = (n_samples,) Predicted survival functions.