Skip to content

Commit

Permalink
some doc changes, requirements in setup.py changes, closing #679
Browse files Browse the repository at this point in the history
  • Loading branch information
CamDavidsonPilon committed Mar 26, 2019
1 parent 2f5ab3c commit 3d79464
Show file tree
Hide file tree
Showing 14 changed files with 116 additions and 33 deletions.
16 changes: 0 additions & 16 deletions docs/lifelines.fitters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,87 +8,76 @@ lifelines.fitters.aalen\_additive\_fitter module
.. automodule:: lifelines.fitters.aalen_additive_fitter
:members:
:undoc-members:
:show-inheritance:

lifelines.fitters.aalen\_johansen\_fitter module
------------------------------------------------

.. automodule:: lifelines.fitters.aalen_johansen_fitter
:members:
:undoc-members:
:show-inheritance:

lifelines.fitters.breslow\_fleming\_harrington\_fitter module
-------------------------------------------------------------

.. automodule:: lifelines.fitters.breslow_fleming_harrington_fitter
:members:
:undoc-members:
:show-inheritance:

lifelines.fitters.cox\_time\_varying\_fitter module
---------------------------------------------------

.. automodule:: lifelines.fitters.cox_time_varying_fitter
:members:
:undoc-members:
:show-inheritance:

lifelines.fitters.coxph\_fitter module
--------------------------------------

.. automodule:: lifelines.fitters.coxph_fitter
:members:
:undoc-members:
:show-inheritance:

lifelines.fitters.exponential\_fitter module
--------------------------------------------

.. automodule:: lifelines.fitters.exponential_fitter
:members:
:undoc-members:
:show-inheritance:

lifelines.fitters.kaplan\_meier\_fitter module
----------------------------------------------

.. automodule:: lifelines.fitters.kaplan_meier_fitter
:members:
:undoc-members:
:show-inheritance:

lifelines.fitters.log\_logistic\_fitter module
----------------------------------------------

.. automodule:: lifelines.fitters.log_logistic_fitter
:members:
:undoc-members:
:show-inheritance:

lifelines.fitters.log\_normal\_fitter module
--------------------------------------------

.. automodule:: lifelines.fitters.log_normal_fitter
:members:
:undoc-members:
:show-inheritance:

lifelines.fitters.nelson\_aalen\_fitter module
----------------------------------------------

.. automodule:: lifelines.fitters.nelson_aalen_fitter
:members:
:undoc-members:
:show-inheritance:

lifelines.fitters.piecewise\_exponential\_fitter module
-------------------------------------------------------

.. automodule:: lifelines.fitters.piecewise_exponential_fitter
:members:
:undoc-members:
:show-inheritance:



Expand All @@ -98,7 +87,6 @@ lifelines.fitters.weibull\_fitter module
.. automodule:: lifelines.fitters.weibull_fitter
:members:
:undoc-members:
:show-inheritance:


lifelines.fitters.weibull\_aft\_fitter module
Expand All @@ -107,28 +95,24 @@ lifelines.fitters.weibull\_aft\_fitter module
.. automodule:: lifelines.fitters.weibull_aft_fitter
:members:
:undoc-members:
:show-inheritance:

lifelines.fitters.log\_normal\_aft\_fitter module
---------------------------------------------------

.. automodule:: lifelines.fitters.log_normal_aft_fitter
:members:
:undoc-members:
:show-inheritance:

lifelines.fitters.log\_logistic\_aft\_fitter module
-----------------------------------------------------

.. automodule:: lifelines.fitters.log_logistic_aft_fitter
:members:
:undoc-members:
:show-inheritance:




.. automodule:: lifelines.fitters
:members:
:undoc-members:
:show-inheritance:
1 change: 1 addition & 0 deletions lifelines/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@
"WeibullAFTFitter",
"LogLogisticAFTFitter",
"LogNormalAFTFitter",
"PiecewiseExponentialFitter",
]
17 changes: 14 additions & 3 deletions lifelines/fitters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
string_justify,
format_floats,
format_p_value,
format_exp_floats,
coalesce,
check_nans_or_infs,
pass_for_numeric_dtypes_or_raise_array,
Expand Down Expand Up @@ -303,7 +304,7 @@ def __init__(self, *args, **kwargs):

if len(self._bounds) != len(self._fitted_parameter_names) != self._initial_values.shape[0]:
raise ValueError(
"_bounds must be the same shape as _fitted_parameters_names must be the same shape as _initial_values"
"_bounds must be the same shape as _fitted_parameter_names must be the same shape as _initial_values"
)

def _check_cumulative_hazard_is_monotone_and_positive(self, durations, values):
Expand Down Expand Up @@ -598,7 +599,12 @@ def print_summary(self, decimals=2, **kwargs):
print("---")

df = self.summary
print(df.to_string(float_format=format_floats(decimals), formatters={"p": format_p_value(decimals)}))
print(
df.to_string(
float_format=format_floats(decimals),
formatters={"p": format_p_value(decimals), "exp(coef)": format_exp_floats()},
)
)

def fit(
self,
Expand Down Expand Up @@ -1336,7 +1342,12 @@ def print_summary(self, decimals=2, **kwargs):

df = self.summary
# Significance codes as last column
print(df.to_string(float_format=format_floats(decimals), formatters={"p": format_p_value(decimals)}))
print(
df.to_string(
float_format=format_floats(decimals),
formatters={"p": format_p_value(decimals), "exp(coef)": format_exp_floats()},
)
)

# Significance code explanation
print("---")
Expand Down
8 changes: 7 additions & 1 deletion lifelines/fitters/aalen_additive_fitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
_to_list,
format_floats,
format_p_value,
format_exp_floats,
survival_table_from_events,
StatisticalWarning,
)
Expand Down Expand Up @@ -563,7 +564,12 @@ def print_summary(self, decimals=2, **kwargs):
print("---")

df = self.summary
print(df.to_string(float_format=format_floats(decimals), formatters={"p": format_p_value(decimals)}))
print(
df.to_string(
float_format=format_floats(decimals),
formatters={"p": format_p_value(decimals), "exp(coef)": format_exp_floats()},
)
)

# Significance code explanation
print("---")
Expand Down
8 changes: 7 additions & 1 deletion lifelines/fitters/cox_time_varying_fitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
check_nans_or_infs,
string_justify,
format_p_value,
format_exp_floats,
format_floats,
coalesce,
)
Expand Down Expand Up @@ -644,7 +645,12 @@ def print_summary(self, decimals=2, **kwargs):

df = self.summary
# Significance codes last
print(df.to_string(float_format=format_floats(decimals), formatters={"p": format_p_value(decimals)}))
print(
df.to_string(
float_format=format_floats(decimals),
formatters={"p": format_p_value(decimals), "exp(coef)": format_exp_floats()},
)
)

# Significance code explanation
print("---")
Expand Down
8 changes: 7 additions & 1 deletion lifelines/fitters/coxph_fitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
string_justify,
format_p_value,
format_floats,
format_exp_floats,
dataframe_interpolate_at_times,
)

Expand Down Expand Up @@ -1257,7 +1258,12 @@ def print_summary(self, decimals=2, **kwargs):

df = self.summary
# Significance codes as last column
print(df.to_string(float_format=format_floats(decimals), formatters={"p": format_p_value(decimals)}))
print(
df.to_string(
float_format=format_floats(decimals),
formatters={"p": format_p_value(decimals), "exp(coef)": format_exp_floats()},
)
)

# Significance code explanation
print("---")
Expand Down
10 changes: 5 additions & 5 deletions lifelines/fitters/exponential_fitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ class ExponentialFitter(KnownModelParametericUnivariateFitter):
After calling the `.fit` method, you have access to properties like: ``survival_function_``, ``lambda_``, ``cumulative_hazard_``
A summary of the fit is available with the method ``print_summary()``
Parameters
-----------
alpha: float, optional (default=0.05)
the level in the confidence intervals.
Important
----------
The parameterization of this model changed in lifelines 0.19.0. Previously, the cumulative hazard looked like
:math:`\lambda t`. The parameterization is now the reciprocal of :math:`\lambda`.
Notes
-----
Reference: https://www4.stat.ncsu.edu/~dzhang2/st745/chap3.pdf
Attributes
----------
cumulative_hazard_ : DataFrame
Expand Down
5 changes: 5 additions & 0 deletions lifelines/fitters/log_logistic_fitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ class LogLogisticFitter(KnownModelParametericUnivariateFitter):
After calling the `.fit` method, you have access to properties like: ``cumulative_hazard_``, ``plot``, ``survival_function_``, ``alpha_`` and ``beta_``.
A summary of the fit is available with the method 'print_summary()'
Parameters
-----------
alpha: float, optional (default=0.05)
the level in the confidence intervals.
Examples
--------
Expand Down
6 changes: 6 additions & 0 deletions lifelines/fitters/log_normal_fitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ class LogNormalFitter(KnownModelParametericUnivariateFitter):
After calling the `.fit` method, you have access to properties like: ``survival_function_``, ``mu_``, ``sigma_``.
A summary of the fit is available with the method ``print_summary()``
Parameters
-----------
alpha: float, optional (default=0.05)
the level in the confidence intervals.
Attributes
----------
cumulative_hazard_ : DataFrame
Expand Down
12 changes: 9 additions & 3 deletions lifelines/fitters/piecewise_exponential_fitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ class PiecewiseExponentialFitter(KnownModelParametericUnivariateFitter):
After calling the `.fit` method, you have access to properties like: ``survival_function_``, ``plot``, ``cumulative_hazard_``
A summary of the fit is available with the method ``print_summary()``
Parameters
-----------
breakpoints: list
a list of times when a new exponential model is constructed.
alpha: float, optional (default=0.05)
the level in the confidence intervals.
Important
----------
The parameterization of this model changed in lifelines 0.19.1. Previously, the cumulative hazard looked like
Expand Down Expand Up @@ -66,10 +73,10 @@ class PiecewiseExponentialFitter(KnownModelParametericUnivariateFitter):

def __init__(self, breakpoints, *args, **kwargs):
breakpoints = np.sort(breakpoints)
if not (breakpoints[-1] < np.inf):
if len(breakpoints) > 0 and not (breakpoints[-1] < np.inf):
raise ValueError("Do not add inf to the breakpoints.")

if breakpoints[0] < 0:
if len(breakpoints) > 0 and breakpoints[0] < 0:
raise ValueError("First breakpoint must be greater than 0.")

self.breakpoints = np.append(breakpoints, [np.inf])
Expand All @@ -84,7 +91,6 @@ def _cumulative_hazard(self, params, times):

n = times.shape[0]
times = times.reshape((n, 1))

bp = self.breakpoints
M = np.minimum(np.tile(bp, (n, 1)), times)
M = np.hstack([M[:, tuple([0])], np.diff(M, axis=1)])
Expand Down
4 changes: 4 additions & 0 deletions lifelines/fitters/weibull_fitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class WeibullFitter(KnownModelParametericUnivariateFitter):
After calling the `.fit` method, you have access to properties like: ``cumulative_hazard_``, ``survival_function_``, ``lambda_`` and ``rho_``.
A summary of the fit is available with the method ``print_summary()``.
Parameters
-----------
alpha: float, optional (default=0.05)
the level in the confidence intervals.
Important
----------
Expand Down
42 changes: 41 additions & 1 deletion lifelines/generate_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,46 @@
from scipy.integrate import cumtrapz


def piecewise_exponential_survival_data(n, breakpoints, lambdas):
"""
Note
--------
No censoring is present here.
Examples
--------
>>> T = piecewise_exponential_survival_data(100000, [1, 3], [0.2, 3, 1.])
>>> NelsonAalenFitter().fit(T).plot()
"""
assert len(breakpoints) == len(lambdas) - 1

breakpoints = np.append([0], breakpoints)

delta_breakpoints = np.diff(breakpoints)

T = np.empty(n)
for i in range(n):
U = np.random.random()
E = -np.log(U)

running_sum = 0
for delta, lambda_, bp in zip(delta_breakpoints, lambdas, breakpoints):
factor = lambda_ * delta
if E < running_sum + factor:
t = bp + (E - running_sum) / lambda_
break
running_sum += factor
else:
t = breakpoints[-1] + (E - running_sum) / lambdas[-1]

T[i] = t

return T


def exponential_survival_data(n, cr=0.05, scale=1.0):

t = stats.expon.rvs(scale=scale, size=n)
Expand Down Expand Up @@ -273,7 +313,7 @@ def cumulative_integral(fx, x):
def construct_survival_curves(hazard_rates, timelines):
"""
Given hazard rates, reconstruct the survival curves
Parameters
----------
hazard_rates: (n,t) array
Expand Down
Loading

0 comments on commit 3d79464

Please sign in to comment.