def test_survival_events_from_table_with_ties(): T, C = np.array([1, 2, 3, 4, 4, 5]), np.array([1, 0, 1, 1, 1, 1]) d = utils.survival_table_from_events(T, C) T_, C_, W_ = utils.survival_events_from_table(d[["censored", "observed"]]) npt.assert_array_equal([1, 2, 3, 4, 5], T_) npt.assert_array_equal([1, 0, 1, 1, 1], C_) npt.assert_array_equal([1, 1, 1, 2, 1], W_)
def test_survival_events_from_table_no_ties(): T, C = np.array([1, 2, 3, 4, 4, 5]), np.array([1, 0, 1, 1, 0, 1]) d = utils.survival_table_from_events(T, C) T_, C_, W_ = utils.survival_events_from_table(d[["censored", "observed"]]) npt.assert_array_equal(T, T_) npt.assert_array_equal(C, C_) npt.assert_array_equal(W_, np.ones_like(T))
def test_qq_plot_with_weights_and_entry(self, block): from lifelines.utils import survival_events_from_table df = pd.DataFrame(index=[60, 171, 263, 427, 505, 639]) df["death"] = [1, 1, 1, 0, 1, 0] df["censored"] = [0, 0, 0, 3, 0, 330] T, E, W = survival_events_from_table(df, observed_deaths_col="death", censored_col="censored") wf = WeibullFitter().fit(T, E, weights=W, entry=0.0001 * np.ones_like(T)) ax = qq_plot(wf) self.plt.suptitle("test_qq_plot_with_weights_and_entry") self.plt.show(block=block)
def plot3(df): import sys #get_ipython().system('{sys.executable} -m pip install lifelines') #install pandas and matlab plot import pandas as pd import matplotlib.pyplot as plt from lifelines import KaplanMeierFitter # import os # os.chdir("/Users/MDONEGAN/Downloads") #survival= pd.read_csv("/Users/MDONEGAN/Downloads/Book2.csv", sep=',') survival = df from lifelines.statistics import pairwise_logrank_test results = pairwise_logrank_test(survival['time'], survival['group'], survival['event']) results.print_summary() #%% # this util converts a table with "death" and "censored" (alive) into the lifelines format from lifelines import KaplanMeierFitter from lifelines.utils import survival_events_from_table kmf = KaplanMeierFitter() ax = plt.subplot(111) #df = pd.read_csv('/Users/MDONEGAN/Downloads/counts.csv') df = df.set_index('time') T, E, W = survival_events_from_table(df, observed_deaths_col='death', censored_col='censored') kmf.fit(T, E, weights=W) kmf.plot(ax=ax, ci_show=True, marker='o') plt.xlabel("days") plt.ylabel("survival %") plt.ylim(0.4, 1.05) #%% #trying to combine the grouping function and the events from table function from lifelines import KaplanMeierFitter from lifelines.utils import survival_events_from_table kmf = KaplanMeierFitter() ax = plt.subplot(111) #df = pd.read_csv('/Users/MDONEGAN/Downloads/counts.csv') df = df.set_index('time') T, E, W = survival_events_from_table(df, observed_deaths_col='death', censored_col='censored') print(E) #group dataset by treatment and plot all groups (treatments) using kmf fit for name, T_group, E_group, W_group in T, E, W.groupby('group'): kmf.fit(grouped_survival['T'], grouped_survival['E'], label=name) kmf.plot(ax=ax, ci_show=False, marker='o') plt.xlabel("days") plt.ylabel("survival %") plt.ylim(0.4, 1.05) return fig_to_uri(plt)
def test_survival_table_to_events(): T, C = np.array([1, 2, 3, 4, 4, 5]), np.array([1, 0, 1, 1, 1, 1]) d = utils.survival_table_from_events(T, C, np.zeros_like(T)) T_, C_ = utils.survival_events_from_table(d[['censored', 'observed']]) npt.assert_array_equal(T, T_) npt.assert_array_equal(C, C_)
def test_survival_table_to_events(): T, C = np.array([1, 2, 3, 4, 4, 5]), np.array([1, 0, 1, 1, 1, 1]) d = utils.survival_table_from_events(T, C, np.zeros_like(T)) T_, C_ = utils.survival_events_from_table(d[['censored', 'observed']]) npt.assert_array_equal(T, T_) npt.assert_array_equal(C, C_)