def plot_survival_curves_and_histograms(sim_outcomes_NO, sim_outcomes_ASP): # get survival curves of both treatments survival_curves = [ sim_outcomes_NO.nLivingPatients, sim_outcomes_ASP.nLivingPatients ] # graph survival curve PathCls.graph_sample_paths(sample_paths=survival_curves, title='Survival curve', x_label='Simulation time step (year)', y_label='Number of alive patients', legends=['No Therapy', 'Aspirin Therapy']) # histograms of survival times set_of_survival_times = [ sim_outcomes_NO.survivalTimes, sim_outcomes_ASP.survivalTimes ] # graph histograms Figs.graph_histograms(data_sets=set_of_survival_times, title='Histogram of patient survival time', x_label='Survival time (year)', y_label='Counts', bin_width=1, legends=['No Therapy', 'Aspirin Therapy'], transparency=0.6)
def plot_survival_curves_and_histograms(sim_outcomes_mono, sim_outcomes_combo): """ draws the survival curves and the histograms of time until HIV deaths :param sim_outcomes_mono: outcomes of a cohort simulated under mono therapy :param sim_outcomes_combo: outcomes of a cohort simulated under combination therapy """ # get survival curves of both treatments survival_curves = [ sim_outcomes_mono.nLivingPatients, sim_outcomes_combo.nLivingPatients ] # graph survival curve PathCls.graph_sample_paths(sample_paths=survival_curves, title='Survival curve', x_label='Simulation time step (year)', y_label='Number of alive patients', legends=['Mono Therapy', 'Combination Therapy']) # histograms of survival times set_of_survival_times = [ sim_outcomes_mono.survivalTimes, sim_outcomes_combo.survivalTimes ] # graph histograms Figs.graph_histograms(data_sets=set_of_survival_times, title='Histogram of patient survival time', x_label='Survival time (year)', y_label='Counts', bin_width=1, legends=['Mono Therapy', 'Combination Therapy'], transparency=0.6)
def graph_sample_path(sample_path, title=None, x_label=None, y_label=None, figure_size=None, output_type='show', legend=None, color_code=None): """ plot a sample path :param sample_path: a sample path :param title: (string) title of the figure :param x_label: (string) x-axis label :param y_label: (string) y-axis label :param figure_size: (tuple) figure size :param output_type: select from 'show', 'pdf' or 'png' :param legend: string for the legend :param color_code: (string) 'b' blue 'g' green 'r' red 'c' cyan 'm' magenta 'y' yellow 'k' black """ if not isinstance(sample_path, _PrevalenceSamplePath): raise ValueError( 'sample_path should be an instance of PrevalencePathRealTimeUpdate or PrevalencePathBatchUpdate.') fig, ax = plt.subplots(figsize=figure_size) ax.set_title(title) # title ax.set_xlabel(x_label) # x-axis label ax.set_ylabel(y_label) # y-axis label # add a sample path to this ax add_sample_path_to_ax(sample_path=sample_path, ax=ax, color_code=color_code, legend=legend) ax.set_ylim(bottom=0) # the minimum has to be set after plotting the values # output figure Fig.output_figure(fig, output_type, title)
def graph_sample_path(sample_path, title, x_label, y_label, output_type='show', legend=None, color_code=None): """ produces a sample path :param sample_path: a sample path :param title: (string) title of the figure :param x_label: (string) x-axis label :param y_label: (string) y-axis label :param output_type: select from 'show', 'pdf' or 'png' :param legend: string for the legend :param color_code: (string) 'b' blue 'g' green 'r' red 'c' cyan 'm' magenta 'y' yellow 'k' black """ fig = plt.figure(title) plt.title(title) # title plt.xlabel(x_label) # x-axis label plt.ylabel(y_label) # y-axis label # x and y values x_values = sample_path.get_times() y_values = sample_path.get_values() # color color_marker_text = '-' if not (color_code is None): color_marker_text = color_code + color_marker_text # plot plt.plot(x_values, y_values, color_marker_text) # add legend if provided if not (legend is None): plt.legend([legend]) # set the minimum of y-axis to zero plt.ylim(bottom=0) # the minimum has to be set after plotting the values # output figure Fig.output_figure(plt, output_type, title)
def graph_sample_paths(sample_paths, title=None, x_label=None, y_label=None, figure_size=None, output_type='show', legends=None, transparency=1, common_color_code=None): """ graphs multiple sample paths :param sample_paths: a list of sample paths :param title: (string) title of the figure :param x_label: (string) x-axis label :param y_label: (string) y-axis label :param figure_size: (tuple) figure size :param output_type: select from 'show', 'pdf' or 'png' :param legends: list of strings for legend :param transparency: float (0.0 transparent through 1.0 opaque) :param common_color_code: (string) color code if all sample paths should have the same color 'b' blue 'g' green 'r' red 'c' cyan 'm' magenta 'y' yellow 'k' black """ if len(sample_paths) == 1: raise ValueError('Only one sample path is provided. Use graph_sample_path instead.') fig, ax = plt.subplots(figsize=figure_size) ax.set_title(title) # title ax.set_xlabel(x_label) # x-axis label ax.set_ylabel(y_label) # y-axis label # add all sample paths add_sample_paths_to_ax(sample_paths=sample_paths, ax=ax, common_color_code=common_color_code, transparency=transparency) # add legend if provided if legends is not None: if common_color_code is None: ax.legend(legends) else: ax.legend([legends]) # set the minimum of y-axis to zero ax.set_ylim(bottom=0) # the minimum has to be set after plotting the values # output figure Fig.output_figure(fig, output_type, title)
def plot_survival_curves_and_histograms(multi_cohort_outcomes_mono, multi_cohort_outcomes_combo): """ plot the survival curves and the histograms of survival times :param multi_cohort_outcomes_mono: outcomes of a multi-cohort simulated under mono therapy :param multi_cohort_outcomes_combo: outcomes of a multi-cohort simulated under combination therapy """ # get survival curves of both treatments sets_of_survival_curves = [ multi_cohort_outcomes_mono.survivalCurves, multi_cohort_outcomes_combo.survivalCurves ] # graph survival curve PathCls.graph_sets_of_sample_paths( sets_of_sample_paths=sets_of_survival_curves, title='Survival Curves', x_label='Simulation Time Step (year)', y_label='Number of Patients Alive', legends=['Mono Therapy', 'Combination Therapy'], transparency=0.4, color_codes=['green', 'blue'] ) # histograms of survival times set_of_survival_times = [ multi_cohort_outcomes_mono.meanSurvivalTimes, multi_cohort_outcomes_combo.meanSurvivalTimes ] # graph histograms Figs.graph_histograms( data_sets=set_of_survival_times, title='Histograms of Average Patient Survival Time', x_label='Survival Time (year)', y_label='Counts', bin_width=0.25, x_range=[5.25, 17.75], legends=['No Therapy', 'Aspirin Therapy'], color_codes=['green', 'blue'], transparency=0.5 )
def graph_sample_paths(sample_paths, title, x_label, y_label, output_type='show', legends=None, transparency=1, common_color_code=None, if_same_color=False): """ graphs multiple sample paths :param sample_paths: a list of sample paths :param title: (string) title of the figure :param x_label: (string) x-axis label :param y_label: (string) y-axis label :param output_type: select from 'show', 'pdf' or 'png' :param legends: list of strings for legend :param transparency: float (0.0 transparent through 1.0 opaque) :param common_color_code: (string) color code if all sample paths should have the same color 'b' blue 'g' green 'r' red 'c' cyan 'm' magenta 'y' yellow 'k' black :param if_same_color: logical, default False, if set True, paint the sample paths the same color """ if len(sample_paths) == 1: raise ValueError( 'Only one sample path is provided. Use graph_sample_path instead.') if if_same_color and common_color_code is None: raise ValueError( "Provide a color code (e.g. 'k') for common_color_code if all sample paths should have the same color ." ) fig = plt.figure(title) plt.title(title) # title plt.xlabel(x_label) # x-axis label plt.ylabel(y_label) # y-axis label # color color_marker_text = '-' if not (common_color_code is None): color_marker_text = common_color_code + color_marker_text # x and y values if if_same_color: for path in sample_paths: x_values = path.get_times() y_values = path.get_values() # plot plt.plot(x_values, y_values, common_color_code, alpha=transparency) else: for path in sample_paths: x_values = path.get_times() y_values = path.get_values() # plot plt.plot(x_values, y_values, color_marker_text, alpha=transparency) # add legend if provided if not (legends is None): if common_color_code is None: plt.legend(legends) else: plt.legend([legends]) # set the minimum of y-axis to zero plt.ylim(bottom=0) # the minimum has to be set after plotting the values # output figure Fig.output_figure(plt, output_type, title)
import CalibrationClasses as Cls import CalibrationSettings as CalibSets import SimPy.FigureSupport as Fig # create a calibration object calibration = Cls.Calibration() # sample the posterior of the mortality probability calibration.sample_posterior(n_samples=CalibSets.POST_N) # create the histogram of the resampled mortality probabilities Fig.graph_histogram(data=calibration.mortalityResamples, title='Histogram of Resampled Mortality Probabilities', x_label='Mortality Probability', y_label='Counts', x_range=[CalibSets.POST_L, CalibSets.POST_U]) # Estimate of mortality probability and the posterior interval print( 'Estimate of mortality probability ({:.{prec}%} credible interval):'. format(1 - CalibSets.ALPHA, prec=0), calibration.get_mortality_estimate_credible_interval( alpha=CalibSets.ALPHA)) # effective sample size # txtEff = 'Effective sample size: {:.1f}'.format(calibration.get_effective_sample_size()) # print(txtEff)
import SimPy.FigureSupport as Fig import numpy as np import matplotlib.pyplot as plt data = [1, 2, 3, 4, 5] print(Fig.get_moving_average(data=data, window=3)) data = np.random.randn(100) fig = plt.figure(figsize=(4, 2)) ax = fig.add_subplot(111) ax.plot(range(100), data, alpha=0.5) ax.plot(range(100), Fig.get_moving_average(data, window=10), alpha=1) plt.show()
def print_histograms(sim_outcomes, diagnostic_name): # plot the sample path (survival curve) PathCls.graph_sample_path(sample_path=sim_outcomes.nLivingPatients, title=f'Survival Curve {diagnostic_name}', x_label='Time-Step (Week)', y_label='Number Survived') # plot the histogram of survival times Figs.graph_histogram( data=sim_outcomes.survivalTimes, title=f'Histogram of Patient Survival Time {diagnostic_name}', x_label='Survival Time (Week)', y_label='Count', bin_width=1) # Figs.graph_histogram( # data=sim_outcomes.timesINFECTEDtoHOSP_TBD, # title=f'Histogram of INFECTED to HOSP_TBD {diagnostic_name}', # x_label='Time (Week)', # y_label='Count', # bin_width=1) # # Figs.graph_histogram( # data=sim_outcomes.timesINFECTEDtoHOSP_TBM, # title=f'Histogram of INFECTED to HOSP_TBM {diagnostic_name}', # x_label='Time (Week)', # y_label='Count', # bin_width=1) # # Figs.graph_histogram( # data=sim_outcomes.timesINFECTEDtoDX_TBD, # title=f'Histogram of INFECTED to DX_TBD {diagnostic_name}', # x_label='Time (Week)', # y_label='Count', # bin_width=1) # # Figs.graph_histogram( # data=sim_outcomes.timesINFECTEDtoCLEARED, # title=f'Histogram of INFECTED to CLEARED {diagnostic_name}', # x_label='Time (Week)', # y_label='Count', # bin_width=1) # # Figs.graph_histogram( # data=sim_outcomes.timesCLEAREDtoDEAD, # title=f'Histogram of CLEARED to DEAD {diagnostic_name}', # x_label='Time (Week)', # y_label='Count', # bin_width=1) # # Figs.graph_histogram( # data=sim_outcomes.timesHOSP_TBDtoDEAD, # title=f'Histogram of HOSP_TBD to DEAD {diagnostic_name}', # x_label='Time (Week)', # y_label='Count', # bin_width=1) # # Figs.graph_histogram( # data=sim_outcomes.timesHOSP_TBDtoDX_TBD, # title=f'Histogram of HOSP_TBD to DX_TBD {diagnostic_name}', # x_label='Survival Time (Week)', # y_label='Count', # bin_width=1) # # Figs.graph_histogram( # data=sim_outcomes.timesHOSP_TBMtoDEAD, # title=f'Histogram of HOSP_TBM to DEAD {diagnostic_name}', # x_label='Survival Time (Week)', # y_label='Count', # bin_width=1) # # Figs.graph_histogram( # data=sim_outcomes.timesHOSP_TBMtoDX_TBM, # title=f'Histogram of HOSP_TBM to DX_TBM {diagnostic_name}', # x_label='Time (Week)', # y_label='Count', # bin_width=1) # # Figs.graph_histogram( # data=sim_outcomes.timesDX_TBDtoDEAD, # title=f'Histogram of DX_TBD to DEAD {diagnostic_name}', # x_label='Time (Week)', # y_label='Count', # bin_width=1) # # Figs.graph_histogram( # data=sim_outcomes.timesDX_TBDtoCLEARED, # title=f'Histogram of DX_TBD to CLEARED {diagnostic_name}', # x_label='Time (Week)', # y_label='Count', # bin_width=1) # # Figs.graph_histogram( # data=sim_outcomes.timesDX_TBMtoDEAD, # title=f'Histogram of DX_TBM to DEAD {diagnostic_name}', # x_label='Time (Week)', # y_label='Count', # bin_width=1) # # Figs.graph_histogram( # data=sim_outcomes.timesDX_TBMtoCLEARED, # title=f'Histogram of DX_TBM to CLEARED {diagnostic_name}', # x_label='Time (Week)', # y_label='Count', # bin_width=1) # # Figs.graph_histogram( # data=sim_outcomes.timesINFECTED, # title=f'Histogram of INFECTED {diagnostic_name}', # x_label='Time (Week)', # y_label='Count', # bin_width=1) # # Figs.graph_histogram( # data=sim_outcomes.timesHOSP_TBM, # title=f'Histogram of HOSP_TBM {diagnostic_name}', # x_label='Time (Week)', # y_label='Count', # bin_width=1) # # Figs.graph_histogram( # data=sim_outcomes.timesHOSP_TBD, # title=f'Histogram of HOSP_TBD {diagnostic_name}', # x_label='Time (Week)', # y_label='Count', # bin_width=1) # # Figs.graph_histogram( # data=sim_outcomes.timesDX_TBD, # title=f'Histogram of DX_TBD {diagnostic_name}', # x_label='Time (Week)', # y_label='Count', # bin_width=1) # # Figs.graph_histogram( # data=sim_outcomes.timesDX_TBM, # title=f'Histogram of DX_TBM {diagnostic_name}', # x_label='Time (Week)', # y_label='Count', # bin_width=1) # # Figs.graph_histogram( # data=sim_outcomes.timesCLEARED, # title=f'Histogram of CLEARED {diagnostic_name}', # x_label='Time (Week)', # y_label='Count', # bin_width=1) # Figs.graph_histogram( # data=sim_outcomes.costs, # title=f'Histogram of Cost {diagnostic_name}', # x_label='Cost (Dollars)', # y_label='Count', # bin_width=1) Figs.graph_histogram( data=sim_outcomes.costsPresenting, title=f'Histogram of Cost (Patients Presenting) {diagnostic_name}', x_label='Cost (Dollars)', y_label='Count', bin_width=1)
parameters=P.ParametersFixed(therapy=therapy_none)) # simulate the cohort over the specified time steps myCohort_0.simulate(sim_length=D.SIM_LENGTH) # plot the sample path (survival curve) Path.graph_sample_path( sample_path=myCohort_0.cohortOutcomes.nLivingPatients, title='Survival Curvel for No Therapy', x_label='Time-Step (Year)', y_label='Number Survived') # plot the histogram of survival times Fig.graph_histogram( data=myCohort_0.cohortOutcomes.survivalTimes, title='Histogram of Patient Survival Times for No Therapy', x_label='Survival Time (Year)', y_label='Count', bin_width=1) # print the outcomes of this simulated cohort Support.print_outcomes(sim_outcomes=myCohort_0.cohortOutcomes,therapy_name=therapy_none) therapy_asp = P.Therapies.ASP # create a cohort myCohort_1 = Cls.Cohort(id=1, pop_size=D.POP_SIZE, parameters=P.ParametersFixed(therapy=therapy_asp)) # simulate the cohort over the specified time steps
pop_size=D.POP_SIZE, parameters=P.ParametersFixed(diagnostic=P.Diagnostic.SOC)) multiCohort.simulate(sim_length=D.SIM_LENGTH) # plot the sample paths Path.graph_sample_paths( sample_paths=multiCohort.multiCohortOutcomes.survivalCurves, title='Survival Curves', x_label='Time-Step (Week)', y_label='Number Survived', transparency=0.5) # plot the histogram of average survival time Fig.graph_histogram(data=multiCohort.multiCohortOutcomes.meanSurvivalTimes, title='Histogram of Mean Survival Time', x_label='Mean Survival Time (Week)', y_label='Count') # print the outcomes of this simulated cohort Support.print_outcomes(multi_cohort_outcomes=multiCohort.multiCohortOutcomes, diagnostic=P.Diagnostic.SOC) # create multiple cohort multiCohort_NSB = Cls.MultiCohort( ids=range(D.N_COHORTS, 2 * D.N_COHORTS), pop_size=D.POP_SIZE, parameters=P.ParametersFixed(diagnostic=P.Diagnostic.NSB)) multiCohort_NSB.simulate(sim_length=D.SIM_LENGTH) # plot the sample paths
pop_size=D.POP_SIZE, therapy=therapy_no) multiCohort_no.simulate(sim_length=D.SIM_LENGTH) # plot the sample paths Path.graph_sample_paths( sample_paths=multiCohort_no.multiCohortOutcomes.survivalCurves, title='Survival Curves', x_label='Time-Step (Year)', y_label='Number Survived', transparency=0.5) # plot the histogram of average survival time Fig.graph_histogram(data=multiCohort_no.multiCohortOutcomes.meanSurvivalTimes, title='Histogram of Mean Survival Time', x_label='Mean Survival Time (Year)', y_label='Count') # print the outcomes of this simulated cohort Support.print_outcomes( multi_cohort_outcomes=multiCohort_no.multiCohortOutcomes, therapy_name=therapy_no) # ASPIRIN THERAPY N_COHORTS = 20 # number of cohorts therapy_asp = P.Therapies.ASP # selected therapy # create multiple cohort multiCohort_asp = Cls.MultiCohort(ids=range(N_COHORTS), pop_size=D.POP_SIZE, therapy=therapy_asp)
import numpy as np import SimPy.FigureSupport as cls obs = np.random.normal(4, 3, 1000) cls.graph_histogram( data=obs, title='Histogram', x_label='Values', y_label='Counts', color='g', bin_width=1, x_range=[-5, 20], y_range=[0, 140], legend='Number of patients') obs_sets = [ np.random.normal(4, 3, 1000), np.random.normal(8, 3, 1000) ] cls.graph_histograms( data_sets=obs_sets, title='Two histograms', x_label='Values', y_label='Counts', legends=['H 1', 'H 2'], bin_width=1, x_range=[-10, 20], #y_range=[0, 100], color_codes=['blue', 'green'],
import numpy as np import SimPy.FigureSupport as cls obs = np.random.normal(4, 3, 1000) cls.graph_histogram( data=obs, title='Histogram', x_label='Values', y_label='Counts', x_range=[-5, 20], y_range=[0, 140], legend='Number of patients') obs_sets = [ np.random.normal(4, 3, 1000), np.random.normal(8, 3, 1000) ] cls.graph_histograms( data_sets=obs_sets, title='Two histograms', x_label='Values', y_label='Counts', legend=['H 1', 'H 2'], bin_width=0.5, x_range=[-10, 20], y_range=[0, 100], transparency=0.6 )