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 )
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))
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),
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