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 Path.plot_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'], color_codes=['green', 'blue']) # histograms of survival times set_of_survival_times = [ sim_outcomes_mono.survivalTimes, sim_outcomes_combo.survivalTimes ] # graph histograms Hist.plot_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'], color_codes=['green', 'blue'], transparency=0.6)
def plot_histogram(self, parameter_name, title, x_label=None, y_label=None, x_range=None, folder=''): """ creates a histogram of one parameter """ Fig.plot_histogram( data=self.dictOfParamValues[parameter_name], title=title, x_label=x_label, y_label=y_label, x_range=x_range, figure_size=HISTOGRAM_FIG_SIZE, file_name=folder+'/'+title )
def plot_ratio_hist(self, numerator_par_name, denominator_par_names, title, x_label=None, x_range=None, output_fig_loc='figures_national'): ratio_obss = self.__calculate_ratio_obss(numerator_par_name, denominator_par_names) file_name = output_fig_loc + '/Ratio-' + title # create the histogram of ratio Fig.plot_histogram( data=ratio_obss, title=title, x_label=x_label, x_range=x_range, figure_size=HISTOGRAM_FIG_SIZE, file_name=file_name)
def plot_histogram(self, parameter_name, title, x_lable=None, y_lable=None, x_range=None): """ creates a histogram of one parameter """ Fig.plot_histogram(self.dictOfParams[parameter_name], title, x_lable, y_lable, x_range=x_range, figure_size=HISTOGRAM_FIG_SIZE, output_type='jpg', file_name='figures_national\Par-' + title)
def plot_histograms(self, par_names=None, csv_file_name_prior=None, posterior_fig_loc='figures_national'): """ creates histograms of parameters specified by ids :param par_names: (list) of parameter names to plot :param csv_file_name_prior: (string) filename where parameter prior ranges are located :param posterior_fig_loc: (string) location where posterior figures_national should be located """ raise ValueError('Needs to be debugged.') # clean the directory IO.delete_files('.png', posterior_fig_loc) # read prior distributions dict_of_priors = None if csv_file_name_prior is not None: dict_of_priors = self.get_dict_of_priors(prior_info_csv_file=csv_file_name_prior) if par_names is None: par_names = self.get_all_parameter_names() # for all parameters, read sampled parameter values and create the histogram for par_name in par_names: # get values for this parameter par_values = self.dictOfParamValues[par_name] # get info of this parameter title, multiplier, x_range = self.get_title_multiplier_x_range_decimal_format( par_name=par_name, dict_of_priors=dict_of_priors) # adjust parameter values par_values = [v*multiplier for v in par_values] # find the filename the histogram should be saved as file_name = posterior_fig_loc + '/Par-' + par_name + ' ' + F.proper_file_name(par_name) # plot histogram Fig.plot_histogram( data=par_values, title=title.replace('!', '\n'), x_range=x_range, figure_size=HISTOGRAM_FIG_SIZE, file_name=file_name )
def draw_survival_curves_and_histograms(calibrated_model_no_drug, calibrated_model_with_drug): """ draws the histograms of average survival time :param calibrated_model_no_drug: calibrated model simulated when drug is not available :param calibrated_model_with_drug: calibrated model simulated when drug is available """ # get survival curves of both treatments survival_curves = [ calibrated_model_no_drug.multiCohorts.multiCohortOutcomes. survivalCurves, calibrated_model_with_drug.multiCohorts. multiCohortOutcomes.survivalCurves ] # graph survival curve Path.plot_sets_of_sample_paths(sets_of_sample_paths=survival_curves, title='Survival curve', x_label='Simulation time step', y_label='Number of alive patients', legends=['No Drug', 'With Drug'], color_codes=['blue', 'orange'], transparency=0.25) # histograms of average survival times set_of_survival_times = [ calibrated_model_no_drug.multiCohorts.multiCohortOutcomes. meanSurvivalTimes, calibrated_model_with_drug.multiCohorts. multiCohortOutcomes.meanSurvivalTimes ] # graph histograms Hist.plot_histograms(data_sets=set_of_survival_times, title='Histogram of average patient survival time', x_label='Survival time', y_label='Counts', bin_width=0.5, legends=['No Drug', 'With Drug'], color_codes=['blue', 'orange'], transparency=0.5, x_range=[6, 20])
def plot_survival_curves_and_histograms(sim_outcomes_none, sim_outcomes_anticoag): """ draws the survival curves and the histograms of time until HIV deaths :param sim_outcomes_none: outcomes of a cohort simulated under no therapy :param sim_outcomes_anticoag: outcomes of a cohort simulated under anticoagulation """ # get survival curves of both treatments survival_curves = [ sim_outcomes_none.nLivingPatients, sim_outcomes_anticoag.nLivingPatients ] # graph survival curve Path.plot_sample_paths( sample_paths=survival_curves, title='Survival curve', x_label='Simulation time step (year)', y_label='Number of alive patients', legends=['No Therapy', 'Anticoagulation'], color_codes=['red', 'blue'] ) # histograms of survival times set_of_strokes = [ sim_outcomes_none.nStrokes, sim_outcomes_anticoag.nStrokes ] # graph histograms Hist.plot_histograms( data_sets=set_of_strokes, title='Histogram of number of strokes', x_label='Number of Strokes', y_label='Counts', bin_width=1, legends=['No Therapy', 'Anticoagulation'], color_codes=['red', 'blue'], transparency=0.5 )
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 Path.plot_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 Hist.plot_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=['Mono Therapy', 'Combination Therapy'], color_codes=['green', 'blue'], transparency=0.5)
def draw_survival_curves_and_histograms(cohort_no_drug, cohort_with_drug): """ draws the survival curves and the histograms of survival time :param cohort_no_drug: a cohort simulated when drug is not available :param cohort_with_drug: a cohort simulated when drug is available """ # get survival curves of both treatments survival_curves = [ cohort_no_drug.cohortOutcomes.nLivingPatients, cohort_with_drug.cohortOutcomes.nLivingPatients ] # graph survival curve Path.plot_sample_paths(sample_paths=survival_curves, title='Survival curve', x_label='Simulation time step', y_label='Number of alive patients', legends=['No Drug', 'With Drug'], color_codes=['blue', 'orange'], transparency=0.5) # histograms of survival times set_of_survival_times = [ cohort_no_drug.cohortOutcomes.survivalTimes, cohort_with_drug.cohortOutcomes.survivalTimes ] # graph histograms Hist.plot_histograms(data_sets=set_of_survival_times, title='Histogram of patient survival time', x_label='Survival time', y_label='Counts', bin_width=2, legends=['No Drug', 'With Drug'], color_codes=['blue', 'orange'], transparency=0.5)
import SimPy.InOutFunctions as IO import SimPy.Plots.Histogram as Hist import SimPy.Plots.ProbDist as Plot import SimPy.RandomVariateGenerators as RVGs import SimPy.Statistics as Stat # read weekly number of drinks cols = IO.read_csv_cols(file_name='dataNumOfDrinks.csv', n_cols=1, if_ignore_first_row=True, if_convert_float=True) # make a histogram Hist.plot_histogram(data=cols[0], title='Weekly Number of Drinks', bin_width=1) # mean and standard deviation stat = Stat.SummaryStat(name='Weekly number of drinks', data=cols[0]) print('Mean = ', stat.get_mean()) print('StDev = ', stat.get_stdev()) # fit a Poisson distribution fit_results = RVGs.Poisson.fit_ml(data=cols[0]) print('Fitting a Poisson distribution:', fit_results) # plot the fitted Poisson distribution Plot.plot_poisson_fit(data=cols[0], fit_results=fit_results, x_label='Weekly number of drinks', x_range=(0, 40), bin_width=1)
# simulate all cohorts multiCohort.simulate(Sets.TIME_STEPS) # plot the sample paths Path.plot_sample_paths( sample_paths=multiCohort.multiCohortOutcomes.survivalCurves, title='Survival Curves', x_label='Time-Step (Year)', y_label='Number Survived', transparency=0.5) # plot the histogram of average survival time Hist.plot_histogram( data=multiCohort.multiCohortOutcomes.meanSurvivalTimes, title='Histogram of Mean Survival Time', x_label='Mean Survival Time (Year)', y_label='Count', x_range=[2.5, 21.5], bin_width=0.5) # create the histogram of the mortality probabilities Hist.plot_histogram( data=mortality_samples, title='Histogram of Mortality Probabilities', x_label='Mortality Probability', y_label='Counts', x_range=[Sets.PRIOR_L, Sets.PRIOR_U]) # print projected mean survival time (years) print('Projected mean survival time (years)', multiCohort.multiCohortOutcomes.statMeanSurvivalTime.get_mean())
import SimPy.Plots.Histogram as Hist import SimPy.Plots.SamplePaths as Path # selected therapy therapy = P.Therapies.COMBO # create a cohort myCohort = Cls.Cohort(id=1, pop_size=D.POP_SIZE, parameters=P.Parameters(therapy=therapy)) # simulate the cohort over the specified time steps myCohort.simulate(sim_length=D.SIM_LENGTH) # plot the sample path (survival curve) Path.plot_sample_path(sample_path=myCohort.cohortOutcomes.nLivingPatients, title='Survival Curve', x_label='Time-Step (Year)', y_label='Number Survived') # plot the histogram of survival times Hist.plot_histogram(data=myCohort.cohortOutcomes.survivalTimes, title='Histogram of Patient Survival Time', x_label='Survival Time (Year)', y_label='Count', bin_width=1) # print the outcomes of this simulated cohort Support.print_outcomes(sim_outcomes=myCohort.cohortOutcomes, therapy_name=therapy)
pop_sizes=[COHORT_POP_SIZE] * N_COHORTS, # [COHORT_POP_SIZE, COHORT_POP_SIZE, ..., COHORT_POP_SIZE] mortality_probs=[MORTALITY_PROB] * N_COHORTS # [p, p, ....] ) # simulate all cohorts multiCohort.simulate(TIME_STEPS) # plot the sample paths Path.plot_sample_paths( sample_paths=multiCohort.multiCohortOutcomes.survivalCurves, title='Survival Curves', x_label='Time-Step (Year)', y_label='Number Survived', transparency=0.5) # plot the histogram of average survival time Hist.plot_histogram(data=multiCohort.multiCohortOutcomes.meanSurvivalTimes, title='Histogram of Mean Survival Time', x_label='Mean Survival Time (Year)', y_label='Count') # print projected mean survival time (years) print('Projected mean survival time (years)', multiCohort.multiCohortOutcomes.statMeanSurvivalTime.get_mean()) # print projection interval print( '95% projection (prediction, percentile, or uncertainty) interval of average survival time (years)', multiCohort.multiCohortOutcomes.statMeanSurvivalTime.get_PI(alpha=ALPHA))
def plot_histograms(self, ids=None, csv_file_name_prior=None, posterior_fig_loc='figures_national'): """ creates histograms of parameters specified by ids :param ids: (list) list of parameter ids :param csv_file_name_prior: (string) filename where parameter prior ranges are located :param posterior_fig_loc: (string) location where posterior figures_national should be located """ # clean the directory IO.delete_files('.png', posterior_fig_loc) # read prior distributions if csv_file_name_prior is not None: priors = IO.read_csv_rows(file_name=csv_file_name_prior, if_ignore_first_row=True, delimiter=',', if_convert_float=True) # for all parameters, read sampled parameter values and create the histogram par_id = 0 for key, par_values in self.dictOfParams.items(): # skip these columns if key in ['Simulation Replication', 'Random Seed']: continue # check if the histogram should be created for this parameter if_show = False if ids is None: if_show = True elif par_id in ids: if_show = True # create the histogram if if_show: # find prior range x_range = None if priors is not None: try: x_range = [ float(priors[par_id][Column.LB.value]), float(priors[par_id][Column.UB.value]) ] except: print( 'Could not convert string to float to find the prior distribution of parameter:', par_id) else: x_range = None # find the filename the histogram should be saved as file_name = posterior_fig_loc + '\Par-' + str( par_id) + ' ' + F.proper_file_name(key) # find title if priors[par_id][Column.TITLE.value] in ('', None): title = priors[par_id][Column.NAME.value] else: title = priors[par_id][Column.TITLE.value] # find multiplier if priors[par_id][Column.MULTIPLIER.value] in ('', None): multiplier = 1 else: multiplier = float(priors[par_id][Column.MULTIPLIER.value]) x_range = [x * multiplier for x in x_range] par_values = [v * multiplier for v in par_values] # plot histogram Fig.plot_histogram(data=par_values, title=title.replace('!', '\n'), x_range=x_range, figure_size=HISTOGRAM_FIG_SIZE, file_name=file_name) # move to the next parameter par_id += 1
def plot_pairwise(self, par_names=None, prior_info_csv_file=None, fig_filename='pairwise_correlation.png', figure_size=(10, 10)): """ creates pairwise correlation between parameters specified by ids :param par_names: (list) names of parameter to display :param prior_info_csv_file: (string) filename where parameter prior ranges are located (Note: '!' will be replaced with '\n') :param fig_filename: (string) filename to save the figure as :param figure_size: (tuple) figure size """ # read information about prior distributions dict_of_priors = None if prior_info_csv_file is not None: dict_of_priors = self.get_dict_of_priors(prior_info_csv_file=prior_info_csv_file) # if parameter names are not specified, include all parameters if par_names is None: par_names = self.get_all_parameter_names() # find the info of parameters to include in the analysis info_of_param_info_to_include = [] # for each parameter, read sampled parameter values and create the histogram for par_name in par_names: # get parameter values par_values = self.dictOfParamValues[par_name] # get info title, multiplier, x_range, decimal, form = self.get_title_multiplier_x_range_decimal_format( par_name=par_name, dict_of_priors=dict_of_priors) # adjust parameter values par_values = [v*multiplier for v in par_values] # append the info for this parameter info_of_param_info_to_include.append( ParamInfo(name=par_name, label=title.replace('!', '\n'), values=par_values, value_range=x_range)) # plot pairwise # set default properties of the figure plt.rc('font', size=6) # fontsize of texts plt.rc('axes', titlesize=6) # fontsize of the figure title plt.rc('axes', titleweight='semibold') # fontweight of the figure title # plot each panel n = len(info_of_param_info_to_include) if n == 0: raise ValueError('Values of parameters are not provided. ' 'Make sure the calibration algorithm exports the parameter values.') f, axarr = plt.subplots(nrows=n, ncols=n, figsize=figure_size) for i in range(n): for j in range(n): # get the current axis ax = axarr[i, j] if j == 0: ax.set_ylabel(info_of_param_info_to_include[i].label) if i == n-1: ax.set_xlabel(info_of_param_info_to_include[j].label) if i == j: # plot histogram Fig.add_histogram_to_ax( ax=ax, data=info_of_param_info_to_include[i].values, x_range=info_of_param_info_to_include[i].range ) ax.set_yticklabels([]) ax.set_yticks([]) else: ax.scatter(info_of_param_info_to_include[j].values, info_of_param_info_to_include[i].values, alpha=0.5, s=2) ax.set_xlim(info_of_param_info_to_include[j].range) ax.set_ylim(info_of_param_info_to_include[i].range) # correlation line b, m = polyfit(info_of_param_info_to_include[j].values, info_of_param_info_to_include[i].values, 1) ax.plot(info_of_param_info_to_include[j].values, b + m * info_of_param_info_to_include[j].values, '-', c='black') corr, p = pearsonr(info_of_param_info_to_include[j].values, info_of_param_info_to_include[i].values) ax.text(0.95, 0.95, '{0:.2f}'.format(corr), transform=ax.transAxes, fontsize=6, va='top', ha='right') f.align_ylabels(axarr[:, 0]) f.tight_layout() output_figure(plt=f, filename=fig_filename, dpi=300)
def plot_pairwise(self, ids=None, csv_file_name_prior=None, fig_filename='pairwise_correlation.png', figure_size=(10, 10)): """ creates pairwise corrolation between parameters specified by ids :param ids: (list) list of parameter ids :param csv_file_name_prior: (string) filename where parameter prior ranges are located :param fig_filename: (string) filename to save the figure as :param figure_size: (tuple) figure size """ # read prior distributions priors = None if csv_file_name_prior is not None: priors = IO.read_csv_rows(file_name=csv_file_name_prior, if_ignore_first_row=True, delimiter=',', if_convert_float=True) # find the names of parameters to include in the analysis info_of_params_to_include = [] par_id = 0 for key, par_values in self.dictOfParams.items(): # skip these columns if key in ['Simulation Replication', 'Random Seed']: continue # check if the histogram should be created for this parameter if_show = False if ids is None: if_show = True elif par_id in ids: if_show = True # create the histogram if if_show: # find prior range x_range = None if priors is not None: try: x_range = [ float(priors[par_id][Column.LB.value]), float(priors[par_id][Column.UB.value]) ] except: print( 'Could not convert string to float to find the prior distribution of parameter:', par_id) else: x_range = None # find title if priors[par_id][Column.TITLE.value] in ('', None): label = priors[par_id][Column.NAME.value] else: label = priors[par_id][Column.TITLE.value] # find multiplier if priors[par_id][Column.MULTIPLIER.value] in ('', None): multiplier = 1 else: multiplier = float(priors[par_id][Column.MULTIPLIER.value]) x_range = [x * multiplier for x in x_range] par_values = [v * multiplier for v in par_values] # append the info for this parameter info_of_params_to_include.append( ParamInfo(idx=par_id, name=key, label=label.replace('!', '\n'), values=par_values, range=x_range)) # move to the next parameter par_id += 1 # plot pairwise # set default properties of the figure plt.rc('font', size=6) # fontsize of texts plt.rc('axes', titlesize=6) # fontsize of the figure title plt.rc('axes', titleweight='semibold') # fontweight of the figure title # plot each panel n = len(info_of_params_to_include) f, axarr = plt.subplots(nrows=n, ncols=n, figsize=figure_size) for i in range(n): for j in range(n): # get the current axis ax = axarr[i, j] if j == 0: ax.set_ylabel(info_of_params_to_include[i].label) if i == n - 1: ax.set_xlabel(info_of_params_to_include[j].label) if i == j: # plot histogram Fig.add_histogram_to_ax( ax=ax, data=info_of_params_to_include[i].values, x_range=info_of_params_to_include[i].range) ax.set_yticklabels([]) ax.set_yticks([]) else: ax.scatter(info_of_params_to_include[j].values, info_of_params_to_include[i].values, alpha=0.5, s=2) ax.set_xlim(info_of_params_to_include[j].range) ax.set_ylim(info_of_params_to_include[i].range) # correlation line b, m = polyfit(info_of_params_to_include[j].values, info_of_params_to_include[i].values, 1) ax.plot(info_of_params_to_include[j].values, b + m * info_of_params_to_include[j].values, '-', c='black') corr, p = pearsonr(info_of_params_to_include[j].values, info_of_params_to_include[i].values) ax.text(0.95, 0.95, '{0:.2f}'.format(corr), transform=ax.transAxes, fontsize=6, va='top', ha='right') f.align_ylabels(axarr[:, 0]) f.tight_layout() f.savefig(fig_filename, bbox_inches='tight', dpi=300)
cohort_size=Sets.SIM_POP_SIZE, time_steps=Sets.TIME_STEPS) # plot the sample paths Path.plot_sample_paths(sample_paths=calibrated_model.multiCohorts. multiCohortOutcomes.survivalCurves, title='Survival Curves', x_label='Time-Step (Year)', y_label='Number Survived', transparency=0.5) # plot the histogram of mean survival time Hist.plot_histogram( data=calibrated_model.multiCohorts.multiCohortOutcomes.meanSurvivalTimes, title='Histogram of Mean Survival Time', x_label='Mean Survival Time (Year)', y_label='Count', bin_width=0.25, x_range=[2.5, 21.5]) # create the histogram of the resampled mortality probabilities Hist.plot_histogram(data=calibrated_model.resampledMortalityProb, title='Histogram of Resampled Mortality Probabilities', x_label='Mortality Probability', y_label='Counts', x_range=[Sets.PRIOR_L, Sets.PRIOR_U]) # Estimate of mortality probability and the posterior interval print( 'Estimate of mortality probability ({:.{prec}%} credible interval):'. format(1 - Sets.ALPHA, prec=0),