Source code for emod_api.spatialreports.plot_spat_means
"""This script assumes local SpatialReport_XXX.bin files which have been downloaded (from COMPS)using pyCOMPS getexpout function or equivalent. Note that this interacts with files on an experimentbasis, not simulation basis. It assumes the files are in a subdirectory named after the experiment id,and then in subdirectories of that named after the simulation id. <exp_id>/ <sim1_id>/ SpatialReport_XXX.bin <sim2_id>/ SpatialReport_XXX.binThe idea is that the data is most interesting not on a simulation basis, but for an experiment,especially aggregated on a certain sweep param and value. This plot calculates means and plots those.Option 1: For each node, plot the mean of the specified channel for all files (values) found in experiment.Option 2: For each node, plot the mean of the specified channel for all files (values) found in experiment _limited_ by specified tag key-value pair.There is little to no assistance here so you need to specify a valid key and value."""importosimportemod_api.spatialreports.spatialassrimportmatplotlib.pyplotaspltimportnumpyasnpimportsqlite3
[docs]defcollect(exp_id,chan="Prevalence",tag=None):node_chan_data={}node_chan_means={}groupby_values={}iftag:iflen(tag.split("="))==1:raiseValueError(f"When passing tag, has to have key=value format.")groupby_key=tag.split("=")[0]groupby_value=tag.split("=")[1]db=os.path.join("latest_experiment","results.db")con=sqlite3.connect(db)cur=con.cursor()query=f"SELECT sim_id FROM results where CAST({groupby_key} AS DECIMAL)-CAST({groupby_value} AS DECIMAL)<0.0001"all_results=cur.execute(query)groupby_values["ref"]=list()forresultinall_results:sim_id=result[0]groupby_values["ref"].append(sim_id)else:groupby_values["ref"]=os.listdir(exp_id)if"results.db"ingroupby_values["ref"]:groupby_values["ref"].remove("results.db")#print( f"groupby_values = {groupby_values}." )forsim_idingroupby_values["ref"]:report_path=os.path.join(str(exp_id),sim_id,"SpatialReport_"+chan+".bin")data=sr.SpatialReport(report_path)fornode_idindata.node_ids:chan_data=data[node_id].dataifnode_idnotinnode_chan_data:node_chan_data[node_id]=list()node_chan_means[node_id]=list()node_chan_data[node_id].append(chan_data)node_chan_means[node_id]=np.zeros(len(node_chan_data[node_id]))fornodeinnode_chan_data.keys():node_chan_means[node]=np.mean(np.array(node_chan_data[node]),axis=0)returnnode_chan_means
[docs]defplot(exp_id,chan="Prevalence",tag=None):node_chan_means=collect(exp_id,chan,tag)fornodeinnode_chan_means.keys():plt.plot(node_chan_means[node],label=f"node={node}")plt.xlabel("Timestep")plt.ylabel(chan)plt.title(f"Mean values of {chan} over time by node.")plt.legend()plt.show()
if__name__=="__main__":importargparseparser=argparse.ArgumentParser(description='Spatial Report Plotting')parser.add_argument('-c','--channel',action='store',default="Prevalence",help='channel(s) to display [Prevalence]')parser.add_argument('-e','--experiment_id',action='store',default=None,help='experiment id to plot, data assumed to be local')parser.add_argument('-t','--tag',action='store',default="",help='tag constraint')args=parser.parse_args()ifnotargs.experiment_id:withopen("COMPS_ID","r")asfp:args.experiment_id=fp.read()ifnotargs.channel:args.channel='Prevalence'# should not be necessary# check that folder with name experiment_id existsifnotos.path.exists(str(args.experiment_id)):raiseValueError(f"Don't see folder for {args.experiment_id}.")plot(str(args.experiment_id),args.channel,tag=args.tag)