def test_predict_method_returns_gives_values_prior_to_the_value_in_the_survival_function( self): T = [1, 2, 3] kmf = KaplanMeierFitter() kmf.fit(T) assert abs(kmf.predict(0.5) - kmf.survival_function_.ix[0].values) < 10e-8 assert abs(kmf.predict(1.9999) - kmf.survival_function_.ix[1].values) < 10e-8
def test_predict_method_returns_exact_value_if_given_an_observed_time( self): T = [1, 2, 3] kmf = KaplanMeierFitter() kmf.fit(T) time = 1 assert abs(kmf.predict(time) - kmf.survival_function_.ix[time].values) < 10e-8
#median survival in months print("median survival: " + str(kmf.median_) + " months") #print(kmf.survival_function_) coordinates = [] survival_fx = (kmf.survival_function_) coordinates_y = list(survival_fx.values.flatten()) coordinates_x = [] for row in survival_fx.iterrows(): timeline, km_estimate = row coordinates_x.append(timeline.tolist()) for (x, y) in zip(coordinates_x, coordinates_y): coordinates.append([x, y]) print("curve coordinates:") print(coordinates) #calculate the survival probability for t=1 year surv_for_1 = kmf.predict(12) print("probability of survival for 1 year: " + str(surv_for_1)) #caluclate the survival probability for t=3 years surv_for_3 = kmf.predict(36) print("probability of survival for 3 years: " + str(surv_for_3)) #calculate the survival probability for t=5 surv_for_5 = kmf.predict(60) print("probability of survival for 5 years: " + str(surv_for_5))
def test_predict_methods_returns_a_scalar_or_a_array_depending_on_input(self, sample_lifetimes): kmf = KaplanMeierFitter() kmf.fit(sample_lifetimes[0]) assert not isinstance(kmf.predict(1), Iterable) assert isinstance(kmf.predict([1, 2]), Iterable)
def progression_free_analysis(data): list_patients = [] for patient, data in data.items(): v_status = data['VitalStatus'] d_progression = data['DiseaseProgression'] s_time = data['SurvivalTime'] patient_info = [] if d_progression != None: if v_status != None: if v_status == 'Alive' or d_progression == 'False': #patient disease did not progress progression_status = 0 else: progression_status = 1 #patient disease did progress patient_info.append(progression_status) if type(s_time) != str: patient_info.append(s_time) list_patients.append(patient_info) df = pd.DataFrame(list_patients) num_patients = len(df) df.columns = ['Event', 'Duration'] kmf = KaplanMeierFitter() kmf.fit(durations=df.Duration, event_observed=df.Event) coordinates = [] survival_fx = kmf.survival_function_ coordinates_y = list(survival_fx.values.flatten()) coordinates_x = [] for row in survival_fx.iterrows(): timeline, km_estimate = row coordinates_x.append(timeline.tolist()) for (x, y) in zip(coordinates_x, coordinates_y): coordinates.append([x, y]) #calculate the progression free survival probability for t=1 year surv_for_1 = kmf.predict(12) #calculate the progression free survival probability for t=3 years surv_for_3 = kmf.predict(36) #calculate the progression free survival probability for t=5 years surv_for_5 = kmf.predict(60) surv_median = int(round(kmf.median_)) year_1_surv = int(round(surv_for_1 * 100)) year_3_surv = int(round(surv_for_3 * 100)) year_5_surv = int(round(surv_for_5 * 100)) prog_free_stats = {} prog_free_stats['Coordinates'] = coordinates prog_free_stats['Median'] = surv_median prog_free_stats['1Year'] = year_1_surv prog_free_stats['3Year'] = year_3_surv prog_free_stats['5Year'] = year_5_surv return prog_free_stats
def overall_survival_analysis(data): list_patients = [] for patient, data in data.items(): patient_info = [] v_status = data['VitalStatus'] s_time = data['SurvivalTime'] if v_status == 'Alive' or 'Dead': if v_status == 'Alive': v_status = 0 else: v_status = 1 patient_info.append(v_status) if type(s_time) != str: patient_info.append(s_time) list_patients.append(patient_info) df = pd.DataFrame(list_patients) num_patients = len(df) df.columns = ['Event', 'Duration'] kmf = KaplanMeierFitter() kmf.fit(durations=df.Duration, event_observed=df.Event) #print(kmf.survival_function_) coordinates = [] survival_fx = (kmf.survival_function_) coordinates_y = list(survival_fx.values.flatten()) coordinates_x = [] for row in survival_fx.iterrows(): timeline, km_estimate = row coordinates_x.append(timeline.tolist()) for (x, y) in zip(coordinates_x, coordinates_y): coordinates.append([x, y]) #calculate the survival probability for t=1 year surv_for_1 = kmf.predict(12) #caluclate the survival probability for t=3 years surv_for_3 = kmf.predict(36) #calculate the survival probability for t=5 surv_for_5 = kmf.predict(60) surv_median = int(round(kmf.median_)) year_1_surv = int(round(surv_for_1 * 100)) year_3_surv = int(round(surv_for_3 * 100)) year_5_surv = int(round(surv_for_5 * 100)) overall_surv_stats = {} overall_surv_stats['Coordinates'] = coordinates overall_surv_stats['Median'] = surv_median overall_surv_stats['1Year'] = year_1_surv overall_surv_stats['3Year'] = year_3_surv overall_surv_stats['5Year'] = year_5_surv return overall_surv_stats
def test_predict_method_returns_gives_values_prior_to_the_value_in_the_survival_function(self): T = [1, 2, 3] kmf = KaplanMeierFitter() kmf.fit(T) assert abs(kmf.predict(0.5) - kmf.survival_function_.ix[0].values) < 10e-8 assert abs(kmf.predict(1.9999) - kmf.survival_function_.ix[1].values) < 10e-8
def test_predict_method_returns_exact_value_if_given_an_observed_time(self): T = [1, 2, 3] kmf = KaplanMeierFitter() kmf.fit(T) time = 1 assert abs(kmf.predict(time) - kmf.survival_function_.ix[time].values) < 10e-8
def test_predict_methods_returns_a_scalar_or_a_array_depending_on_input(self, sample_lifetimes): kmf = KaplanMeierFitter() kmf.fit(sample_lifetimes[0]) assert not isinstance(kmf.predict(1), Iterable) assert isinstance(kmf.predict([1, 2]), Iterable)