def plot_fitness_sim_results(df, params, ymin=None, ymax=None, yticks=None, title=None, x_step=10): sns.set_style("ticks") # plot population size popsizes = fitness.str_popsizes_to_array(df["log_pop_sizes"]) # take the total population size: sum of populations tuned to any # nutrient state df["log2_pop_size"] = np.log2(np.exp(popsizes).sum(axis=1)) policy_colors = params["policy_colors"] # only plot policies we have colors for policies_with_colors = policy_colors.keys() df = df[df["policy"].isin(policies_with_colors)] init_pop_size = sum(params["init_pop_sizes"]) # group results by time and by policy grouped = df.groupby(["t", "policy"], as_index=False) summary_df = grouped.agg({"log2_pop_size": [np.mean, np.std], "growth_rates": [np.mean, np.std]}) rand_summary = summary_df[summary_df["policy"] == "Random"] time_obj = time_unit.Time(params["t_start"], params["t_end"], step_size=params["step_size"]) step_size = params["step_size"] final_df = summary_df[summary_df["t"] == time_obj.t[-1]] ## plot population size across time def plot_pop_size_across_time(params, ymin=ymin, ymax=ymax): offset = step_size / 250. num_xticks = 11 ax = sns.tsplot(time="t", value="log2_pop_size", unit="sim_num", condition="policy", color=policy_colors, err_style="ci_band", ci=95, data=df, legend=False) for policy_num, policy in enumerate(policy_colors): error_df = summary_df[summary_df["policy"] == policy] c = policy_colors[policy] assert (len(error_df["t"]) == len(time_obj.t) == \ len(error_df["log2_pop_size"]["mean"])) plt.xlabel("Time step", fontsize=10) plt.ylabel("Pop. size ($\log_{2}$)", fontsize=10) # assuming glucose is listed first gluc_growth_rate = params["nutr_growth_rates"][0] galac_growth_rate = params["nutr_growth_rates"][1] if title is not None: plt.title(title, fontsize=8) else: plt.title(r"$P_{0} = %d$, " \ r"$\mu_{\mathsf{Glu}} = %.2f, \mu_{\mathsf{Gal}} = %.2f$, " \ r"$\mu_{\mathsf{Mis}} = %.2f$, lag = %d, " \ r"%d iters" %(init_pop_size, gluc_growth_rate, galac_growth_rate, params["mismatch_growth_rate"], params["decision_lag_time"], params["num_sim_iters"]), fontsize=8) c = 0.5 plt.xlim([min(df["t"]) - c, max(df["t"]) + c]) if ymin is None: ymin = int(np.log2(init_pop_size)) plt.ylim(ymin=ymin) plt.xlim([time_obj.t.min(), time_obj.t.max()]) plt.xticks(range(int(time_obj.t.min()), int(time_obj.t.max()) + x_step, x_step), fontsize=8) if yticks is not None: plt.yticks(yticks, fontsize=8) plt.ylim(yticks[0], yticks[-1]) sns.despine(trim=True, offset=2*time_obj.step_size) plt.tick_params(axis='both', which='major', labelsize=8, pad=2) # make plot plot_pop_size_across_time(params)
Get spline fit. """ spl = scipy.interpolate.UnivariateSpline(x, y, k=4, s=1) return spl(x) def get_spline_derivs_fit(x, y): """ Get spline derivatives fit. """ spl = scipy.interpolate.UnivariateSpline(x, y, k=4, s=1) derivs = spl.derivative() return derivs(x) sim_to_plot = sims_to_plot[0] df = results[sim_to_plot]["data"] popsizes = fitness.str_popsizes_to_array(df["log_pop_sizes"]) # take the total population size: sum of populations tuned to any # nutrient state df["log2_pop_size"] = np.log2(np.exp(popsizes).sum(axis=1)) print df g = df.groupby(["policy", "sim_num"]) new_df = [] for name, group in g: curr_df = group.copy() print "fitting to: ", curr_df["t"], curr_df["growth_rates"] curr_df["fitted_growth_rate"] = \ get_spline_fit(curr_df["t"], curr_df["growth_rates"]) curr_df["fitted_growth_rate_from_pop"] = \ get_spline_derivs_fit(curr_df["t"], curr_df["log2_pop_size"]) plt.figure() plt.plot(curr_df["t"], curr_df["growth_rates"])