def generate_cdf(fb, hk): cdf_fb = Cdf(degrees(fb)) cdf_hk = Cdf(degrees(hk)) thinkplot.Cdf(cdf_fb, color='gray', label="Facebook CDF") thinkplot.Cdf(cdf_hk, label='RPA CDF') thinkplot.config(xlabel='degree', xscale='log', ylabel='CDF') plt.savefig('CDFGraphs_Modified.png')
def generate_ccdf(fb, hk): cdf_fb = Cdf(degrees(fb)) cdf_hk = Cdf(degrees(hk)) thinkplot.Cdf(cdf_fb, label='Facebook CCDF', color='gray', complement=True) thinkplot.Cdf(cdf_hk, label="RPA CCDF", complement=True) thinkplot.config(xlabel='degree', xscale='log', ylabel='CCDF', yscale='log') plt.savefig("CCDFGraphs_Modified.png")
# viewer = SugarscapeViewer(env) # anim = viewer.animate(frames=100) # # plt.show() RandomSeed(17) env = Sugarscape(50, num_agents=250, min_lifespan=60, max_lifespan=100, replace=True) cdfs = [] for i in range(5): [env.step() for i in range(100)] cdf = Cdf(agent.sugar for agent in env.agents) cdfs.append(cdf) thinkplot.preplot(cols=2) thinkplot.Cdfs(cdfs[:-1], color='gray', alpha=0.3) thinkplot.Cdf(cdfs[-1]) thinkplot.config(xlabel='Wealth', ylabel='CDF') thinkplot.bigger_text() thinkplot.subplot(2) thinkplot.Cdfs(cdfs[:-1], color='gray', alpha=0.3) thinkplot.Cdf(cdfs[-1]) thinkplot.config(xlabel='Wealth', ylabel='CDF', xscale='log') thinkplot.bigger_text() thinkplot.save('chap09-4')
thinkplot.Plot(history[0, 0, :], label="Susceptible") thinkplot.Plot(history[0, 1, :], label="Infected") thinkplot.Plot(history[0, 2, :], label="Dead") thinkplot.Plot(history[0, 3, :], label="R") plt.title("Population distribution in one city") plt.xlabel("Timestep") plt.ylabel("Population") thinkplot.config(legend=True) thinkplot.show() print("num infected", history[:, 4, :]) thinkplot.plot(np.sum(history[:, 3, :], axis=1)) thinkplot.show() """ Plot CDF of infections number """ infection_cdf = Cdf(np.sum(history[:, 3, :], axis=1)) thinkplot.Cdf(infection_cdf) plt.title("Sum Reinfections Cdf") plt.xlabel("Total Reinfections") plt.ylabel("CDF") thinkplot.show() # print("G Len nodes:", len(G.nodes())) # print("G Len edges:", len(G.edges())) # print("G transitivity:", nx.transitivity(G)) # print("G Degree Centrality:", np.mean(list(nx.degree_centrality(G).values()))) # # print("G Closeness Centrality:", np.mean(list(nx.closeness_centrality(G).values()))) # print("G Clustering:", nx.average_clustering(G)) """ Plot infections vs degree and closeness """
def vel_distribution(filename, fps, speed_limit=25, dir=None, only_vehicle=True): """ Arguments --------- filename: str, path to database fps: frame rate of the video, in frames per second speed_limit: speed limit of the intersection dir: directory to save image, if None don't automatically save only_vehicle: only show velocities of vehicles, default True """ connection = sqlite3.connect(filename) cursor = connection.cursor() if only_vehicle: queryStatement = '''SELECT object_trajectories.object_id AS object_id, frame, x, y, x_v, y_v FROM object_trajectories INNER JOIN objects ON object_trajectories.object_id = objects.object_id WHERE road_user_type = 1 ORDER BY object_id, frame''' else: queryStatement = 'SELECT object_id, frame, x, y, x_v, y_v FROM object_trajectories ORDER BY object_id, frame' cursor.execute(queryStatement) obj_id = 0 obj_vels = [] xvels = [] yvels = [] for row in cursor: xvel = row[4] yvel = row[5] xvels.append(xvel * fps * MPS_MPH_CONVERSION) yvels.append(yvel * fps * MPS_MPH_CONVERSION) # reading new object if (row[0] != obj_id): # save velocity information for old object before moving onward xvels = [abs(x) for x in xvels] yvels = [abs(y) for y in yvels] speeds = [ math.sqrt(vels[0]**2 + vels[1]**2) for vels in zip(xvels, yvels) ] avg_xv = sum(xvels) / len(xvels) avg_yv = sum(yvels) / len(yvels) avg_vel = math.sqrt(avg_xv**2 + avg_yv**2) obj_vels.append(avg_vel) obj_id = row[0] xvels = [] yvels = [] cdf = Cdf(obj_vels) kdepdf = EstimatedPdf(obj_vels) pr = cdf.PercentileRank(speed_limit) titlestring = "{:0.1f} % of {} are exceeding the {} mph limit".format( 100 - pr, 'vehicles' if only_vehicle else 'road users', speed_limit) thinkplot.PrePlot(1) thinkplot.Pdf(kdepdf) thinkplot.Vlines(speed_limit, 0, 0.05) thinkplot.Config(title=titlestring, xlabel='Velocity (mph)', ylabel='PDF') if dir is not None: thinkplot.Save(os.path.join(dir, 'velocityPDF'), formats=['jpg'], bbox_inches='tight') else: thinkplot.Show()
plt.xlabel("Degree t") plt.ylabel("Mortality %") # plt.yscale('log') thinkplot.show() thinkplot.Scatter(degree_vs_infections[:, 0], degree_vs_infections[:, 1]) plt.title("Degree vs Reinfections") plt.xlabel("Degree t") plt.ylabel("Reinfections") thinkplot.show() thinkplot.plot(np.sum(history[:, 4, :], axis=0)) plt.title("Infections across t") thinkplot.show() thinkplot.Cdf(Cdf(history[:, 0, -2]), label="S") thinkplot.Cdf(Cdf(history[:, 1, -2]), label="I") thinkplot.Cdf(Cdf(history[:, 2, -2]), label="Dead") thinkplot.Cdf(Cdf(history[:, 3, -2]), label="R") plt.title("D in each city") thinkplot.show() city_i = 5 thinkplot.plot(history[city_i, 4, :]) plt.title("Infected at each timestep, degree" + str(len(G[node_list[city_i]]))) thinkplot.show() # thinkplot.plot(history[1,:3,:100].T) # thinkplot.show() thinkplot.Plot(np.sum(history[:, 3, :], axis=0), label="NYS")