def plot_reg(df, file, result_dir):
    densities = df["density"].unique()
    estimators = df["estimator"].unique()
    df["param_kl_weight_scale"] = df.param_kl_weight_scale * df.n_datapoints

    kl_params = df["param_kl_weight_scale"].unique()
    map_params = df["param_map_mode"].unique()

    test_score_columns = [
        column
        for column in df.columns
        if column.startswith("split") and column.endswith("_test_score")
    ]
    layout = (1, 2)
    n_curves_to_plot = len(kl_params) * len(map_params)

    for estimator in estimators:
        fig, axarr = plt.subplots(*layout, figsize=(11 * layout[1], 5 * layout[0]))
        axarr = [axarr] if len(densities) == 1 else axarr.flatten()
        for i, density in enumerate(densities):
            color_iter = iter(cm.gist_rainbow(np.linspace(0, 1, n_curves_to_plot)))
            for kl in kl_params:
                for map in map_params:
                    sub_df = df.loc[
                        (df["density"] == density)
                        & (df["estimator"] == estimator)
                        & (df["param_map_mode"] == map)
                        & (df["param_kl_weight_scale"] == kl)
                    ]
                    n_datapoints = sorted(sub_df["n_datapoints"].unique())
                    means = np.array([], dtype=np.float32)
                    stds = np.array([], dtype=np.float32)

                    for n_data in n_datapoints:
                        scores = []
                        for c in test_score_columns:
                            scores += list(sub_df.loc[sub_df["n_datapoints"] == n_data][c].values)
                        scores = np.array(scores, dtype=np.float32)
                        means = np.append(means, scores.mean())
                        stds = np.append(stds, scores.std())

                    c = next(color_iter)

                    label = "{}: kl_scale: {}, map_mode: {}".format(estimator, kl, map)
                    axarr[i].plot(n_datapoints, means, color=c, label=label)
                    axarr[i].fill_between(
                        n_datapoints, means - stds, means + stds, alpha=0.2, color=c
                    )

                axarr[i].set_xlabel("n_observations")
                axarr[i].set_ylabel("score")
                axarr[i].set_title(density)
                axarr[i].legend()
                axarr[i].set_xscale("log")

        plt.savefig(
            os.path.join(
                result_dir, file.name.split(".")[0] + "_" + "noise_reg_{}.png".format(estimator)
            )
        )
Example #2
0
def plot_fit(y, X, weights, t, channel_list, i_pas=0, save_dir=None):

    pl.figure()
    pl.plot(t, y, 'k', label='$c_m \cdot dV/dt - i_{inj} + i_{pas}$'
            if np.any(i_pas != 0) else '$c_m \cdot dV/dt - i_{inj}$')
    pl.plot(t, np.dot(X, weights).T, 'r', label='$-g \cdot \sum_{ion} i_{ion}$')
    pl.plot(t, np.zeros(len(y)), c='0.5')
    pl.ylabel('Current (A)', fontsize=16)
    pl.xlabel('Time (ms)', fontsize=16)
    pl.legend(loc='upper right', fontsize=16)
    pl.tight_layout()
    if save_dir is not None: pl.savefig(save_dir+'bestfit_derivative.png')
    pl.show()

    # plot current trace and derivative
    pl.figure()
    color = iter(cmap.gist_rainbow(np.linspace(0, 1, len(channel_list))))
    pl.plot(t, y, 'k', label='$c_m \cdot dV/dt - i_{inj} + i_{pas}$'
            if np.any(i_pas != 0) else '$c_m \cdot dV/dt - i_{inj}$')
    for j, current in enumerate(channel_list):
        pl.plot(t, (X.T[j]*weights[j]).T, c=next(color), label=channel_list[j])
    pl.plot(t, np.zeros(len(y)), c='0.5')
    pl.ylabel('Current (A)', fontsize=16)
    pl.xlabel('Time (ms)', fontsize=16)
    pl.legend(loc='upper right', fontsize=16)
    pl.tight_layout()
    if save_dir is not None: pl.savefig(save_dir+'bestfit_currents.png')
    pl.show()
Example #3
0
def udział_nuclear_UE():
    list_countries = nuclear_prod["country_or_area"].unique()

    n = len(list_countries)  #ilość kolorów potrzebna do wykresu

    plt.figure(figsize=(15, 10))
    plt.xticks(fontsize=16)
    plt.yticks(fontsize=16)

    #definiowanie linii kolorystycznej
    #https://matplotlib.org/3.3.3/gallery/color/colormap_reference.html

    color = iter(cm.gist_rainbow(np.linspace(0, 1, n)))

    for country in list(list_countries):
        country_data = nuclear_prod[nuclear_prod.country_or_area.isin(
            [country])].sort_values('year')
        c = next(color)
        plt.plot(country_data["year"],
                 country_data["% udział"] * 100,
                 label=country,
                 c=c)

    plt.legend(bbox_to_anchor=(1.05, 1),
               loc='upper left',
               borderaxespad=0.,
               fontsize=15)
    plt.ylabel("nuclear energy %", fontsize=20)
    plt.xlabel('Year', fontsize=20)
    plt.title('% of nuclear energy production per country per year',
              fontsize=24)
    plt.xlim(1990, 2014)
    plt.show()
def plot_map_mle(df, file, result_dir):
    densities = df["density"].unique()
    test_score_columns = [
        column
        for column in df.columns
        if column.startswith("split") and column.endswith("_test_score")
    ]
    param_columns = mle_columns if "mle" in file.name else bayes_columns
    if len(densities) == 1:
        layout = (1, 1)
    else:
        layout = (len(densities) // 2 + 1, 2)

    fig, axarr = plt.subplots(*layout, figsize=(11 * layout[1], 5 * layout[0]))
    axarr = [axarr] if len(densities) == 1 else axarr.flatten()
    n_curves_to_plot = 2

    for i, density in enumerate(densities):
        color_iter = iter(cm.gist_rainbow(np.linspace(0, 1, n_curves_to_plot)))

        for name, estimator in [("MAP", "bayesian"), ("MLE", "mle")]:
            if estimator == "bayesian":
                sub_df = df.loc[
                    (df["density"] == density)
                    & (df["estimator"] == estimator)
                    & (df["param_map_mode"])
                    & (df["param_noise_reg"] == "['fixed_rate', 0.0]")
                ]
            else:
                sub_df = df.loc[
                    (df["density"] == density)
                    & (df["estimator"] == estimator)
                    & (df["param_noise_reg"] == "['fixed_rate', 0.0]")
                ]
            n_datapoints = sorted(sub_df["n_datapoints"].unique())
            means = np.array([], dtype=np.float32)
            stds = np.array([], dtype=np.float32)

            for n_data in n_datapoints:
                scores = []
                for c in test_score_columns:
                    scores += list(sub_df.loc[sub_df["n_datapoints"] == n_data][c].values)
                scores = np.array(scores, dtype=np.float32)
                means = np.append(means, scores.mean())
                stds = np.append(stds, scores.std())

            c = next(color_iter)

            axarr[i].plot(n_datapoints, means, color=c, label=name)
            axarr[i].fill_between(n_datapoints, means - stds, means + stds, alpha=0.2, color=c)

            axarr[i].set_xlabel("n_observations")
            axarr[i].set_ylabel("score")
            axarr[i].set_title(density)
            axarr[i].legend()
            axarr[i].set_xscale("log")

        plt.savefig(os.path.join(result_dir, file.name.split(".")[0] + "_" + "map_vs_mle.png"))
def plot_single_param(
    densities, df, file, param, result_dir, test_score_columns, name_prefix="", log_scale_y=False
):
    if len(densities) == 1:
        layout = (1, 1)
    else:
        layout = (len(densities) // 2 + 1, 2)
    fig, axarr = plt.subplots(*layout, figsize=(11 * layout[1], 5 * layout[0]))
    axarr = [axarr] if len(densities) == 1 else axarr.flatten()

    n_curves_to_plot = len(df[param].unique())
    for i, density in enumerate(densities):
        color_iter = iter(cm.gist_rainbow(np.linspace(0, 1, n_curves_to_plot)))
        for param_instance in df[param].unique():
            sub_df = df.loc[(df["density"] == density) & (df[param] == param_instance)]

            n_datapoints = sorted(sub_df["n_datapoints"].unique())
            means = np.array([], dtype=np.float32)
            stds = np.array([], dtype=np.float32)
            for n_data in n_datapoints:
                scores = []
                for c in test_score_columns:
                    scores += list(sub_df.loc[sub_df["n_datapoints"] == n_data][c].values)
                scores = np.array(scores, dtype=np.float32)
                means = np.append(means, scores.mean())
                stds = np.append(stds, scores.std())

            c = next(color_iter)

            axarr[i].plot(
                n_datapoints,
                means,
                color=c,
                label="{}: {}".format(param.replace("param_", ""), param_instance),
            )
            axarr[i].fill_between(n_datapoints, means - stds, means + stds, alpha=0.2, color=c)

        axarr[i].set_xlabel("n_observations")
        axarr[i].set_ylabel("score")
        axarr[i].set_title(density)
        axarr[i].legend()
        axarr[i].set_xscale("log")
        if log_scale_y:
            axarr[i].set_yscale("log")
    plt.savefig(
        os.path.join(
            result_dir,
            file.name.split(".")[0] + "_" + name_prefix + "_metric_scores_" + param + ".png",
        )
    )
Example #6
0
def plot_err(data,saveplot=True):
    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.pyplot import cm

    methods = data['methods']
    err = data['error']
    dx = data['dx']
    norm = data['norm']

    n = len(methods)
    
    for i,method in enumerate(methods):
        if 'SSPLMM' in method:
            methods[i] = 'SSPMSV' + methods[i][-2:]

    fig = plt.figure()
    ax = fig.add_subplot(111)
    if n > 4:
        marks = iter(cm.gist_rainbow(np.linspace(0,1,n)))
        ls = np.zeros([n,4])
    else:
        marks = ['b','r','k','m']
        ls = marks
    mid_index = err.shape[0]/2
    for j in xrange(n):
        ls[j] = next(marks) if n > 4 else marks[j]
        plt.loglog(dx,err[:,j],'s',c=ls[j],linewidth=2)
    for j in xrange(n):
        if 'SSPMSV' in methods[j]:
            p = int(methods[j][-1])
            ax.loglog(dx,dx**p*err[mid_index,j]/dx[mid_index]**p,'--',c=ls[j],linewidth=2)
        elif 'SSP33' in methods:
            ax.loglog(dx,dx**3*err[mid_index,j]/dx[mid_index]**3,'--',c=ls[j],linewidth=2)
        elif 'SSP104' in methods:
            ax.loglog(dx,dx**4*err[mid_index,j]/dx[mid_index]**4,'--',c=ls[j],linewidth=2)
    ax.legend(methods,loc='best',fontsize=16)    
    ax.set_xlabel('$\Delta x$',fontsize=18)
    ax.set_ylabel('$\||error\||_'+str(norm)+'$',fontsize=18,rotation=0,position=(0,.5))
    ax.yaxis.labelpad = 35
    ax.tick_params(labelsize=14)

    if saveplot:
        plt.savefig('figures/conv_advection_1d.pdf',bbox_inches='tight')
    else:
        plt.show()

    plt.close('all')
def plot_armijo():
    maxIters = 2000
    fixed_F_size = 1000

    # will consider corresponding pairs of these:
    b_G = [10000, 10000, 10000, 10000, 10000, 10000]
    b_H = [0,0,1000,1000,4000, 4000]
    update_rule = ['1', '1_armijo','1', '1_armijo','1', '1_armijo']
    #make color cycle
    color_cycle=iter(cm.gist_rainbow(np.linspace(0,1,len(b_G))))

    logreg = LR()
    F = get_fixed_F(fixed_F_size)

    plt.figure()

    for bg, bh, upd in zip(b_G, b_H, update_rule):
        print bg, bh
        """Load the results """
        filepath, filepath_w = get_filepaths(bg, bh,upd)
        iters, fevals, gevals, adp, f_S, g_norm_S, time = load_result_file(filepath)
        w = load_result_file_w(filepath_w)

        """Plot the results """ 
        # next color
        c = next(color_cycle)
        if bh == 0:
            l = 'SGD, b: '+str(bg)
            ls = '--'
        elif bg <1000:
            l = 'SQN, bG '+str(bg)+' bH '+str(bh)
            ls = '-'
        else:
            l = 'SQN, bG '+str(bg/1000)+'k bH '+str(bh)
            ls = '-'

        if upd == '1_armijo':
            l = l+ ' Armijo'

        # get vals on fixed set
        plt.plot(iters[:maxIters], f_S[:maxIters], label = l, c=c, ls=ls)
        print "fvals" + str(bg), str(bh)
        Fvals = [F(w_i) for w_i in w[:maxIters]]
    plt.legend()
    plt.yscale('log')
    plt.show()
#titles for the plots
titles = ["EEG: Sample Objective vs. Iterations",
            "EEG: Sample Objective vs. CPU time",
            "EEG: Sample Objective vs. Accessed Data Points",
            "EEG: Sample Objective vs. Function Evaluations",
            "EEG: Fixed Subset Objective vs. Iterations",
            "EEG: Fixed Subset Objective vs. CPU time",
            "EEG: Fixed Subset Objective vs. Accessed Data Points",
            "EEG: Fixed Subset Objective vs. Function Evaluations"
            ]
#Defines, which graphs will be printed to .tikz
active = [True, False, False, False, True, True, True, False]

#make color cycle

color_cycle=iter(cm.gist_rainbow(np.linspace(0,1,len(b_G))))


logreg = LR()
F = get_fixed_F(fixed_F_size)

""" Initialize plots: """
stochF_vs_iters = plt.figure(1)
plt.title(titles[0])
plt.ylabel(r'$F_{S_k}(\omega^k)$')
plt.xlabel("Iterations")
plt.ylim(.8,100)
plt.xlim(0,200)

stochF_vs_time = plt.figure(2)
plt.title(titles[1])
def plot_best(df, file, result_dir):
    densities = df["density"].unique()
    test_score_columns = [
        column
        for column in df.columns
        if column.startswith("split") and column.endswith("_test_score")
    ]
    param_columns = mle_columns if "mle" in file.name else bayes_columns
    if len(densities) == 1:
        layout = (1, 1)
    else:
        layout = (len(densities) // 2 + 1, 2)

    fig, axarr = plt.subplots(*layout, figsize=(11 * layout[1], 5 * layout[0]))
    axarr = [axarr] if len(densities) == 1 else axarr.flatten()
    n_curves_to_plot = 3

    for i, density in enumerate(densities):
        color_iter = iter(cm.gist_rainbow(np.linspace(0, 1, n_curves_to_plot)))
        best_bay = df.loc[
            (df["density"] == density)
            & (df["estimator"] == "bayesian")
            & (df["param_map_mode"] == False)
            & (df["n_datapoints"] == 800)
        ].sort_values(by="mean_test_score", ascending=False)
        best_map = df.loc[
            (df["density"] == density)
            & (df["estimator"] == "bayesian")
            & (df["param_map_mode"] == True)
            & (df["n_datapoints"] == 800)
        ].sort_values(by="mean_test_score", ascending=False)
        best_mle = df.loc[
            (df["density"] == density) & (df["estimator"] == "mle") & (df["n_datapoints"] == 800)
        ].sort_values(by="mean_test_score", ascending=False)

        for name, map_mode, estimator, best in [
            ("full bayesian", False, "bayesian", best_bay),
            ("MAP", True, "bayesian", best_map),
            ("MLE", False, "mle", best_mle),
        ]:
            if estimator == "bayesian":
                sub_df = df.loc[
                    (df["density"] == density)
                    & (df["estimator"] == estimator)
                    & (df["param_map_mode"] == map_mode)
                    & (df["param_noise_reg"] == best["param_noise_reg"].values[0])
                ]
            else:
                sub_df = df.loc[
                    (df["density"] == density)
                    & (df["estimator"] == estimator)
                    & (df["param_noise_reg"] == best["param_noise_reg"].values[0])
                ]
            n_datapoints = sorted(sub_df["n_datapoints"].unique())
            means = np.array([], dtype=np.float32)
            stds = np.array([], dtype=np.float32)

            for n_data in n_datapoints:
                scores = []
                for c in test_score_columns:
                    scores += list(sub_df.loc[sub_df["n_datapoints"] == n_data][c].values)
                scores = np.array(scores, dtype=np.float32)
                means = np.append(means, scores.mean())
                stds = np.append(stds, scores.std())

            c = next(color_iter)

            actual_columns = [p for p in param_columns if p in df.columns]
            label = " ".join(
                [name, ":"]
                + [
                    "{}: {},".format(col.replace("param_", ""), best[col].values[0])
                    for col in actual_columns
                ]
            )
            axarr[i].plot(n_datapoints, means, color=c, label=label)
            axarr[i].fill_between(n_datapoints, means - stds, means + stds, alpha=0.2, color=c)

            axarr[i].set_xlabel("n_observations")
            axarr[i].set_ylabel("score")
            axarr[i].set_title(density)
            axarr[i].legend()
            axarr[i].set_xscale("log")

        plt.savefig(os.path.join(result_dir, file.name.split(".")[0] + "_" + "best_scorers.png"))
Example #10
0
# Let's investigate how the number, position, and magnification of images changes when we change the position of the source.  In the below routine, we test a range of source positions;  for each, we plot the source and resulting image positions in a single color.  
# 
# If you play with the values of the lens model and source position, you may notice that some inputs return errors.  This is because the lens equation can pass through singularities, where the magnification is formally infinity.  These sort of solutions have been observed in real life in "Einstein ring" systems.

# In[11]:


plt.figure()
plt.grid(True,linestyle='dotted',alpha=0.5)
plt.xlabel('x')
plt.ylabel('y')
plt.title('Strong Gravitational Lens Source and Images')
x = np.array([1, .7, .3, 1,.12,.12])

nruns=20
colors = cm.gist_rainbow(np.linspace(0, 1, nruns))
c=zip(colors)
for ii in range(nruns):
    x[4]=x[4]+.012
    x[5]=x[5]+.012
    mod_mag,mod_x=raytrace(x)
    plt.scatter(mod_x[:,0],mod_x[:,1],s=mod_mag*100,alpha=0.5,c=c[ii])
    plt.scatter(x[4],x[5],s=100,alpha=0.5,c=c[ii],marker=(5, 2))


# The raytracing code presented above has two components:  a model for the gravitational lens and the algorithm that solves the lens equation on a grid.  We can use the structure of this code for a different application.
# 
# While we didn't specify any units on the distances in the simple strong lens example, in general, the distance from the source position to the images will be on order of an arcsecond.  The simple model we used for the lensing galaxy describes the majority of the mass in the galaxy -- which is in cold dark matter.  It doesn't, however, include effects from the stars in the lensing galaxy.  Those stars represent point mass perturbers in the lensing galaxy.  Stars will alter the positions of the lensed images by approximately a micro-arcsecond but can have large effects on the magnification of the images.  
# 
# We can characterize the effects of stars in the lensing galaxy by creating a magnification map, which zooms into a very small patch of the source plane and describes the total magnification of the image for a source at that position.  
# 
def big_test():
    """
    ########################
    Here be the action:
    ########################


    Init Params:
    """
    maxIters = 5000
    fixed_F_size = 1000

    # will consider corresponding pairs of these:
    b_G = [100, 100, 10000, 15000]
    b_H = [4000, 10000, 4000, 6000]

    #make color cycle

    color_cycle=iter(cm.gist_rainbow(np.linspace(0,1,len(b_G))))


    logreg = LR()
    F = get_fixed_F(fixed_F_size)

    """ Initialize plots: """
    stochF_vs_iters = plt.figure(1)
    plt.title("Sample Objective vs. Iterations")
    plt.ylabel("Stochastic objective on batch")
    plt.xlabel("Iterations")


    stochF_vs_time = plt.figure(2)
    plt.title("Sample Objective vs. CPU time (s)")

    stochF_vs_adp = plt.figure(3)
    plt.title("Sample Objective vs. Accessed Data Points")
    plt.xscale('log')

    stochF_vs_fevals = plt.figure(4)
    plt.title("Sample Objective vs. Function Evaluations")
    plt.xscale('log')



    fixed_vs_iters = plt.figure(5)
    plt.title("Fixed Subset Objective vs. Iterations")

    fixed_vs_time = plt.figure(6)
    plt.title("Fixed Subset Objective vs. CPU time (s)")

    fixed_vs_adp = plt.figure(7)
    plt.title("Fixed Subset Objective vs. Accessed Data Points")
    plt.xscale('log')

    fixed_vs_fevals = plt.figure(8)
    plt.title("Fixed Subset Objective vs. Function Evaluations")
    plt.xscale('log')

    for i in range(8):
        plt.figure(i+1)
        plt.yscale('log')


    for bg, bh in zip(b_G, b_H):
        print bg, bh
        """Load the results """
        filepath, filepath_w = get_filepaths(bg, bh)
        iters, fevals, gevals, adp, f_S, g_norm_S, time = load_result_file(filepath)
        if not (bg == 100 and bh == 4000):
            w = load_result_file_w(filepath_w)

        """Plot the results """ 
        # next color
        c = next(color_cycle)

        if bh == 0:
            l = 'SGD, b: '+str(bg)
            ls = '--'
        elif bg <1000:
            l = 'SQN, bG '+str(bg)+' bH '+str(bh)
            ls = '-'
        else:
            l = 'SQN, bG '+str(bg/1000)+'k bH '+str(bh)
            ls = '-'

        plt.figure(1)
        plt.plot(iters[:maxIters], f_S[:maxIters], label = l, c=c, ls=ls)
        # plot moving averages
        #plt.plot(iters[:maxIters], get_moving_average(f_S,100)[:maxIters], label = ('Avg bG '+str(bg)+' bH '+str(bh)))

        plt.figure(2)
        plt.plot(time[:maxIters], f_S[:maxIters], label = l, c=c, ls=ls)

        plt.figure(3)
        plt.plot(adp[:maxIters], f_S[:maxIters], label = l, c=c, ls=ls)

        plt.figure(4)
        plt.plot(fevals[:maxIters], f_S[:maxIters], label = l, c=c, ls=ls)

        
        
        if not (bg==100 and bh == 4000):
            # get vals on fixed set
            Fvals = [F(w_i) for w_i in w[1:maxIters+1]]
            plt.figure(5)
            plt.plot(iters[:maxIters], Fvals, label = l, c=c, ls=ls)

            plt.figure(6)
            plt.plot(time[:maxIters], Fvals, label = l, c=c, ls=ls)

            plt.figure(7)
            plt.plot(adp[:maxIters], Fvals, label = l, c=c, ls= ls)

            plt.figure(8)
            plt.plot(fevals[:maxIters], Fvals, label = l, c=c, ls=ls)

    for i in range(8):
        plt.figure(i+1)
        plt.legend()

    plt.show()
Example #12
0
    def plot_metric(self,
                    plot_dicts,
                    metric='hellinger_distance',
                    keys_of_interest=None,
                    figsize=(20, 8),
                    layout=None,
                    fig=None,
                    color=None,
                    log_scale_x=True,
                    log_scale_y=True):
        """
    Generates a plot for a metric with axis x representing the n_observations and y representing the metric.
    Args:

      graph_dicts: a list of dicts, each element representing the data for one curve on the plot, example:
                    graph_dicts = [
                      { "estimator": "KernelMixtureNetwork", "x_noise_std": 0.01, "y_noise_std": 0.01},
                      { ... },
                      ...
                      ]

      metric: must be one of the available metrics (e.g. hellinger_distance, kl_divergence etc.)
      simulator: specifies the simulator, e.g. EconDensity
    """

        assert self.results_df is not None, "first generate results df"
        assert metric in self.results_df
        assert plot_dicts is not None
        assert 'estimator' in self.results_df
        if keys_of_interest is not None:
            assert all(
                key in self.results_df for key in keys_of_interest
            ), "at least one key of interest not in the results data frame"

        if layout is None:
            layout = (1, len(plot_dicts.keys()))
        if fig is not None:
            axarr = fig.axes
        else:
            fig, axarr = plt.subplots(*layout, figsize=figsize)
            if isinstance(axarr, np.ndarray):
                axarr = axarr.flatten()
            else:
                axarr = np.array([axarr])
        for i, (ax_title, graph_dicts) in enumerate(plot_dicts.items()):

            n_curves_to_plot = len(graph_dicts)
            color_iter = iter(
                cm.gist_rainbow(np.linspace(0, 1, n_curves_to_plot))
            ) if color is None else copy.deepcopy(color)

            # d_keys = list(graph_dicts.values()[0].keys())
            # d_keys = " ".join(str(x) if x != 'estimator' and x != 'simulator' else "" for x in d_keys)

            for label, graph_dict in graph_dicts.items():
                """ data """

                sub_df = self.results_df.loc[(self.results_df[list(
                    graph_dict)] == pd.Series(graph_dict)).all(axis=1)]

                metric_values_mean = sub_df.groupby(
                    by='n_observations')[metric].mean()
                metric_values_std = sub_df.groupby(
                    by='n_observations')[metric].std()
                n_obs = metric_values_mean.index

                if keys_of_interest is not None:
                    intersect = graph_dict.keys() & keys_of_interest
                    intersect.add("estimator")
                    intersect.add("simulator")
                    sub_dict = OrderedDict(
                        (k, graph_dict[k]) for k in intersect)
                else:
                    sub_dict = OrderedDict(sorted(graph_dict.items()))

                label = label if label is not None else ', '.join(
                    "{}={}".format(k, v) for k, v in sub_dict.items())

                " visual settings "
                c = next(color_iter)

                axarr[i].plot(n_obs, metric_values_mean, color=c, label=label)
                axarr[i].fill_between(n_obs,
                                      metric_values_mean - metric_values_std,
                                      metric_values_mean + metric_values_std,
                                      alpha=0.2,
                                      color=c)

            if log_scale_x: axarr[i].set_xscale('log')
            if log_scale_y: axarr[i].set_yscale('log')
            axarr[i].set_xlabel('n_observations')
            axarr[i].set_ylabel(metric)
            axarr[i].set_title(ax_title)
            axarr[i].legend()
        return fig
Example #13
0
    def __init__(self, gui, caseComb=None):
        """ 
        Checks if file was loaded by user or case combination was entered.
        Creates the colormap for the plots. Creates one figure, a canvas, 
        and two axes. Triggers initial plots.
        """
        self.gui = gui
        self.fixZoom = 0
        self.tempMargin = 0.5
        self.plots = {}
        self.extrPlots = {}
        self.caseComb = caseComb
        self.fileLoaded = self.gui.fileLoaded
        self.path = self.gui.parentPath + 'Case_' + str(self.caseComb)
        self.ignoreValues = self.gui.ignoreValues
        self.visiblePlots = []
        self.handles = []
        self.labels = []

        # If thresholds contains a None, sorting will return None
        if self.gui.thresholds.sort() == None:
            self.thresholds = self.gui.thresholds
        else:
            self.thresholds = self.gui.thresholds.sort()

        # If no file has been specified, take caseComb from GUI and search in default folder
        if not self.fileLoaded:
            # On startup, case combination is set to 103
            if self.caseComb == None:
                self.caseComb = 103
                self.gui.caseEdit.setText(str(self.caseComb))
            # If case combination is invalid, abort
            if not self.checkComb():
                return
        elif self.fileLoaded:
            self.filePath = self.gui.filePath

        # If data hasn't been read from simulation output file, do that
        try:
            self.data
        except AttributeError:
            self.fetchTemp()
        else:
            pass

        # Create color map with one color for each component
        self.colors = {}
        for color, comp in zip(
                cm.gist_rainbow(np.linspace(0, 1, len(self.data))),
                self.data.keys()):
            self.colors[comp] = color

        # Create figure and canvas
        self.fig = Figure()
        self.tempAxes = self.fig.add_subplot(121)
        self.extrAxes = self.fig.add_subplot(122)
        self.canvas = FigureCanvas(self.fig)

        # Set figure title based on if case was specified or file was loaded
        if not self.fileLoaded:
            figTitle = "Maximum and minimum temperatures - Case {}".format(
                self.caseComb)
        else:
            if self.getModel():
                figTitle = "Maximum and minimum temperatures - Model {}".format(
                    self.model)
            else:
                figTitle = "Maximum and minimum temperatures"

        self.fig.suptitle(figTitle)

        self.tempAxes.set_title(
            "Temporal Evolution of Hottest and Coldest Points")
        self.tempAxes.set_xlabel("Time [s]")
        self.tempAxes.set_ylabel("Temperature [$^\circ$C]")

        self.extrAxes.set_title("Absolute Extrema in Time and Space")

        self.updateTemps()
        self.updateExtrema()

        # Display temperature statistics
        self.showTempStats()
Example #14
0
def plot_gantt(df_task,
               reason_str,
               articlename,
               startdate='StartDateUTC',
               enddate='EndDateUTC',
               order=False,
               downtimes=None,
               duedate=None):
    ''' 
    Make a gantt plot of the production file
    - df_task is the pandas DataFrame
    - Reason_str determines which tasks get a separate color
    - Articlename determines if a task is put in a separate line
    - Startdate determines the start date of a production
    - Enddate determines the end date of a production
    - Order can give a custom order to the productions
    - Downtimes is an optional dataframe indicating downtimes of the production line
    '''
    df_task = df_task.reset_index(drop=False)  # make index unique (necessary)

    first_index = df_task.index[0]
    #print(first_index)
    try:
        firstdate = df_task.loc[first_index, startdate].floor('D')
        df_task.loc[:, 'Vis_Start'] = (df_task.loc[:, startdate] -
                                       firstdate).dt.total_seconds() / 3600
        df_task.loc[:, 'Vis_End'] = (df_task.loc[:, enddate] -
                                     firstdate).dt.total_seconds() / 3600
    except:
        df_task.loc[:, 'Vis_Start'] = df_task.loc[:, startdate]
        df_task.loc[:, 'Vis_End'] = df_task.loc[:, enddate]
    if isinstance(downtimes,
                  pd.DataFrame):  # if downtimes included in the correct format
        #print(downtimes)
        downtimes.loc[:, 'Vis_Start'] = (downtimes.loc[:, 'StartDateUTC'] -
                                         firstdate).dt.total_seconds() / 3600
        downtimes.loc[:, 'Vis_End'] = (downtimes.loc[:, 'EndDateUTC'] -
                                       firstdate).dt.total_seconds() / 3600
    # Plot a line for every line of data in your file

    # from cycler import cycler
    # cy = cycler(color=['b','g','orange','c','m','yellow','steelblue', 'tan',
    #                                           'grey', 'cyan', 'lightgreen', 'crimson', 'greenyellow', 'darkviolet', 'fuchsia',
    #                                           'palevioletred', 'moccasin',
    #                                           'rosybrown', 'coral', 'wheat',
    #                                           'linen']*10).by_key()['color']
    if order:
        reasons = list(order)
    else:
        reasons = list(df_task[reason_str].unique())
        reasons.sort()
    #color = plt.cm.get_cmap('gist_rainbow', len(reasons))
    color = cm.gist_rainbow(np.linspace(0, 1, len(reasons)))
    color_dict = dict(zip(reasons, color))

    articles = np.sort(df_task[articlename].unique()).tolist()
    exception = 'NONE'
    if exception in articles:
        i = articles.index(exception)
        articles.pop(i)
        articles.insert(0, exception)
    i = 0
    # plot the downtimes as grey zones
    if isinstance(downtimes, pd.DataFrame):
        for item in downtimes.T:
            entry = downtimes.loc[item]
            plt.axvspan(entry['Vis_Start'],
                        entry['Vis_End'],
                        alpha=0.3,
                        facecolor='k')

    # plot the jobs in the Gantt chart
    for article in articles:
        df_temp = df_task[df_task[articlename] == article]
        for item in df_temp.T:
            entry = df_temp.loc[item]
            plt.hlines(i,
                       entry['Vis_Start'],
                       entry['Vis_End'],
                       lw=11,
                       colors=color_dict[entry[reason_str]])
            if duedate != None:
                #import pdb; pdb.set_trace()
                plt.hlines(i,
                           entry[duedate],
                           entry[duedate] + 3,
                           lw=11,
                           colors='k')
        i += 1
    plt.yticks(range(0, i), articles, fontsize='x-small')
    plt.margins(0.1)

    # make a custom legend
    lines = []
    import matplotlib.lines as mlines
    #import matplotlib.patches as mpatches
    for item in color_dict:
        line = mlines.Line2D([], [],
                             color=color_dict[item],
                             label=item,
                             linewidth=12,
                             solid_capstyle='butt')
        lines.append(line)
    plt.legend(bbox_to_anchor=(0, 1.05, 1, 1.05),
               loc='lower left',
               borderaxespad=0.,
               handles=lines,
               mode='expand',
               ncol=len(color_dict))

    plt.ylabel('Job')
    plt.yticks(fontsize='xx-small')
    try:
        timerange = np.arange(0, np.max(df_task['Vis_End']) + 24, 24)
        label = pd.date_range(df_task[startdate].iloc[0].floor('D'),
                              periods=len(timerange))
        label = label.strftime("%Y-%m-%d")
        plt.xticks(timerange, label, rotation=90)
        plt.xlim(timerange.min(), timerange.max())
        plt.xlabel('Time[h]')
        # from matplotlib.ticker import
        # plt.xaxis.set_major_formatter()
        #import matplotlib.ticker as ticker
        #ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00))
        return label
    except:
        #plt.xticks(np.linspace(df_task[startdate].min(), df_task[enddate].max(), 10), rotation=90)
        pass