def comparingBoxPlots(dictObj, plottedData="R0", saveName="default"): osName = platform.system() files = "images\\" if osName.lower() == "windows" else "images/" osExtension = "win" if osName.lower() == "windows" else "Linux" if plottedData == "R0": if len(dictObj) > 0: und_labels = [] und_R0data = [] R0AnalyzedData = [] reg_labels = [] reg_R0data = [] for key, value in dictObj.items(): if "NC_" in key or "VC_" in key or "SC_" in key: und_labels.append(key) und_R0data.append(value[0]) else: reg_labels.append(key) reg_R0data.append(value[0]) R0AnalyzedData.append(value[1]) flr.saveObjUsingPickle(flr.fullPath("R0"+osExtension+ saveName, "picklefile")+".pkl", dictObj) boxplot(und_R0data,oneD=False, pltTitle="R0 Comparison (box)", xlabel="Model Name", ylabel="Infected Agents (R0)", labels=und_labels, savePlt=True, saveName=osExtension+"9R0_box_"+saveName) #statfile.barChart(und_R0data, oneD=False, pltTitle="R0 Comparison (bar)", xlabel="Model Name", # ylabel="Infected Agents (R0)", labels=und_labels, savePlt=True, saveName=osExtension+"9R0_bar_"+saveName) boxplot(reg_R0data,oneD=False, pltTitle="R0 Comparison (box)", xlabel="Model Name", ylabel="Infected people (R0)", labels=reg_labels, savePlt=True, saveName=osExtension+"restR0_box_"+saveName) #statfile.barChart(reg_R0data, oneD=False, pltTitle="R0 Comparison (bar)", xlabel="Model Name", # ylabel="Infected Agents (R0)", labels=reg_labels, savePlt=True, saveName=osExtension+"restR0_bar_"+saveName) elif plottedData == "inf": if len(dictObj) > 0 or True: labels = [] infectedCounts = [] labels1 = [] infectedCounts1 = [] for key, value in dictObj.items():#InfectedCountDict.items(): if "NC_" in key or "VC_" in key or "SC_" in key: labels.append(key) infectedCounts.append(value) else: labels1.append(key) infectedCounts1.append(value) print(labels, labels1) flr.saveObjUsingPickle(flr.fullPath("infectedCount"+osExtension+saveName, "picklefile")+".pkl", dictObj) boxplot(infectedCounts,oneD=False, pltTitle="Infection Comparison", xlabel="Model Name", ylabel="Total # of Infected Agents", labels=labels, savePlt=True, saveName=osExtension+"9infe_box_"+saveName) #statfile.barChart(infectedCounts, oneD=False, pltTitle="Infection Comparison (bar)", xlabel="Model Name", # ylabel="Total Infected Agents", labels=labels, savePlt=True, saveName=osExtension+"9infe_bar_"+saveName) boxplot(infectedCounts1,oneD=False, pltTitle="Infection Comparison", xlabel="Model Name", ylabel="Total # of Infected Agents", labels=labels1, savePlt=True, saveName=osExtension+"rest_infe_box_"+saveName)
def barChart(data, oneD=False, pltTitle="Some Title", xlabel="Default X", ylabel="Default Y", labels=[], showPlt=True, savePlt=False, saveName="defaultimage.png"): fig1, ax1 = plt.subplots() ax1.set_title(pltTitle) if oneD: mean, _, _, standardDev = analyzeData(data) barLoc = [1] width=0.05 else: # each entry looks like (mean, median, mode, stdDev) dataList = [analyzeData(simData) for simData in data] mean = [int(a[0]*10)/10 for a in dataList] standardDev = [a[3] for a in dataList] barLoc = np.arange( len(data)) width = 0.4 barObject = ax1.bar(barLoc, mean, width, yerr=standardDev) #ax1.yaxis.grid(True) ax1.set_xticks(barLoc) ax1.set_xticklabels(labels, rotation=45, ha="right") ax1.set_xlabel(xlabel) ax1.set_ylabel(ylabel) ax1.spines['top'].set_visible(False) ax1.spines['right'].set_visible(False) for bar in barObject: height = bar.get_height() ax1.annotate('{}'.format(height), xy=(bar.get_x() + bar.get_width() / 2, height), xytext=(0, 3), # 3 points vertical offset textcoords="offset points", ha='center', va='bottom') plt.tight_layout() if savePlt: if not saveName.endswith(".png"): saveName+=".png" plt.savefig(flr.fullPath(saveName, "outputs")) else: plt.show() plt.close()
def boxplot(data, oneD=False, pltTitle="Some Title", xlabel="Default X", ylabel="Default Y", labels=[], showPlt=True, savePlt=False, saveName="defaultimage.png", outputDir="outputs"): """ Parameters: - data: the data to plot, can be a one or two dimentional list, if a 2D list is passed, each row is going to be a data for a separate box plot - oneD: bool to tell if "data" is one dimentional or not - pltTitle: the title of the plot - xlabel: label for the x axis - ylabel: label for the y axis - labels: labels for each box plot, pass a list with one entry if there's one B&W, and a list filled with entries for multiple B&W - showPlt: boolean, show the plot or not - savePlt: boolean, save the plot with the given filename or not - saveName: string, ends with .png o some file format, save the plot with this name """ # nice example of boxplots: # https://matplotlib.org/2.0.1/examples/statistics/boxplot_color_demo.html fig1, ax1 = plt.subplots() ax1.set_title(pltTitle) ax1.boxplot(data, vert=True) ax1.yaxis.grid(True) if oneD: xticks = [1] else: xticks = [a + 1 for a in range(len(data))] ax1.set_xticks(xticks) ax1.set_xticklabels(labels) ax1.set_xlabel(xlabel) ax1.set_ylabel(ylabel) ax1.spines['top'].set_visible(False) ax1.spines['right'].set_visible(False) #ax.spines['bottom'].set_visible(False) #ax.spines['left'].set_visible(False) plt.setp(ax1.get_xticklabels(), rotation=45, ha="right", rotation_mode="anchor") #if labels != [] and len(labels) == len(data): # #plt.setp(ax1, xticks=xticks, xticklabels=labels) # #ax1.set_xticklabels(labels, rotation=45, ha="right") plt.tight_layout() if savePlt: if not saveName.endswith(".png"): saveName += ".png" print("image saved as", saveName) plt.savefig(flr.fullPath(saveName, outputDir)) else: plt.show() plt.close()
def generateVisualByLoading(fileNames, plottedData="inf", saveName='default'): # get all csv in filenames and create the visuals for it nameList = list(fileNames.keys()) fileList = [name+".csv" for name in nameList] dataDict = dict() for name, fileName in zip(nameList, fileList): val = flr.openCsv(flr.fullPath(fileName, folder="outputs")) print(val) break dataDict[name] = val comparingBoxPlots(dataDict, plottedData=plottedData, saveName=saveName)
], ('(npMean, stdev, rangeVal, median)', (0.486, 0.21448543074064494, 1.1, 0.5)))), ('SA_SP', ([ 0.1, 0.2, 0.0, 0.2, 0.0, 0.1, 0.1, 0.1, 0.0, 0.2, 0.0, 0.0, 0.1, 0.1, 0.0, 0.1, 0.1, 0.1, 0.0, 0.1, 0.0, 0.2, 0.1, 0.2, 0.1, 0.1, 0.1, 0.2, 0.2, 0.1, 0.1, 0.1, 0.1, 0.1, 0.2, 0.0, 0.1, 0.0, 0.1, 0.1, 0.1, 0.3, 0.1, 0.0, 0.2, 0.1, 0.0, 0.0, 0.1, 0.2, 0.2, 0.0, 0.0, 0.1, 0.2, 0.2, 0.3, 0.1, 0.0, 0.2, 0.1, 0.3, 0.0, 0.0, 0.0, 0.2, 0.2, 0.1, 0.0, 0.0, 0.0, 0.1, 0.0, 0.0, 0.4, 0.1, 0.1, 0.0, 0.0, 0.1, 0.0, 0.0, 0.4, 0.1, 0.0, 0.0, 0.1, 0.0, 0.0, 0.1, 0.2, 0.2, 0.0, 0.0, 0.1, 0.0, 0.0, 0.0, 0.2, 0.1 ], ('(npMean, stdev, rangeVal, median)', (0.09399999999999999, 0.0925418824100742, 0.4, 0.1)))) ]) for k, v in dataDict.items(): dataDict[k] = v[0] import fileRelated as flr import pandas as pd modelName = "R0export" dfobj = pd.DataFrame.from_dict(dataDict, orient="index") flr.save_df_to_csv(flr.fullPath(modelName + ".csv", "outputs"), dfobj) if __name__ == '__main__': pass stuff = {'a': [1, 1, 1], "b": [2, 2, 2]} df = pd.DataFrame(stuff) print(df) #profile.runctx('model_framework.main()', globals(), {})
def main(): """intialize and run the model, for indepth detail about the config or how to run the code, go to the github page for this code""" # you can control for multiple interventions by adding a case: # [(modified attr1, newVal), (modified attr2, newVal), ...] # simulation name --> simulation controlled variable(s) # dont use . or - in the simulation name because the names are used to save images, or any symbols below modelConfig = main_config.modelConfig R0_controls = { "World": [ ("DynamicCapacity", False), ], "Infection": [ ("baseP", 1.25), ("SeedNumber", 100), ], "HybridClass": [ ("ChangedSeedNumber", 10), ], } # this overrides the previous experiments, since base_p is being chnaged R0_controls = { "World": [ ("DynamicCapacity", False), ], "HybridClass": [ ("ChangedSeedNumber", 10), ], } def cross_scenarios(scenario1, scenario2): experiments = {} for keyname, experiment1 in scenario1.items(): for screenname, screen in scenario2.items(): experiment_name = screenname + "_" + keyname experiments[experiment_name] = screen.copy() for key, value in experiment1.items(): #print(key, value) experiments[experiment_name][key] = value.copy() return copy.deepcopy(experiments) def print_nicely(experiment_scenarios): for ex_name, ex_config in experiment_scenarios.items(): print("\n", "*" * 20, "\n", ex_name) for ex_config_name, ex_config_list in ex_config.items(): print(ex_config_name, ":", ex_config_list) #experiment2 = cross_scenarios(experiment.vaccine3, experiment.low_med) #experiment3 =cross_scenarios(experiment.vaccine4, experiment.facemask3) experiment1 = experiment.original_3x3 experiment2 = cross_scenarios(experiment.different_base_p_jump_025, experiment.medium_student_vary_policy) experiment3 = cross_scenarios(experiment.medium_student_vary_policy, experiment.off_campus_multiplier) #print(len(experiment3)) #print_nicely(experiment3) print(len(experiment1)) basemodel = {"basemodel": {}} multi_experiments = { "request_1": experiment1, "request_2": experiment2, "request_3": experiment3, } for (request_name, modelConfigs) in multi_experiments.items(): R0Dict = dict() InfectedCountDict = dict() output_dir = fileRelated.fullPath(request_name, "outputs") Path(output_dir).mkdir(parents=False, exist_ok=True) output_folder = "outputs/" + request_name for index, (modelName, modelControl) in enumerate(modelConfigs.items()): print("finished", index) configCopy = copy.deepcopy(modelConfig) #print("*"*20) #print(configCopy["Agents"].keys()) #print("*"*20) #print(f"started working on initializing the simualtion for {modelName}") for categoryKey, listOfControls in modelControl.items(): #print(listOfControls) for (specificKey, specificValue) in listOfControls: configCopy[categoryKey][specificKey] = specificValue R0Count, multiCounts = 1, 100 #print(configCopy) if index > -1: #model_framework.simpleCheck(configCopy, days=10, visuals=True, debug=True, modelName=modelName) InfectedCountDict[modelName] = model_framework.multiSimulation( multiCounts, configCopy, days=100, debug=False, modelName=modelName, outputDir=output_folder) R0Dict[modelName] = model_framework.R0_simulation( configCopy, R0_controls, R0Count, debug=False, timeSeriesVisual=False, R0Visuals=True, modelName=modelName, outputDir=output_folder) # the value of the dictionary is ([multiple R0 values], (descriptors, (tuple of useful data like mean and stdev)) print(InfectedCountDict.items()) print(R0Dict.items()) if True: simulationGeneration = "0" saveName = "comparingModels_" + simulationGeneration statfile.comparingBoxPlots(R0Dict, plottedData="R0", saveName=saveName, outputDir=output_folder) statfile.comparingBoxPlots(InfectedCountDict, plottedData="inf", saveName=saveName, outputDir=output_folder) for key, value in R0Dict.items(): if R0Dict[key][1] == "(npMean, stdev, rangeVal, median)": R0Dict[key] = value[0] # else do nothing #print(key, value) print(R0Dict) # check if dict is not empty R0_df = pd.DataFrame(R0Dict) fileRelated.save_df_to_csv( fileRelated.fullPath("R0_data.csv", output_folder), R0_df) else: # never ran after jan 30 #statfile.generateVisualByLoading(ControlledExperiment, plottedData="inf", saveName=saveName) model_framework.createFilledPlot(modelConfig, modelName="baseModel", simulationN=3, outputDir=output_folder) break