Пример #1
0
    def plot_diff(self, x=None, ax=None, kwargs={}, offset = 0.):
        """
        Plots the difference between the Feature fit and the original Spectrum
        on an axis.
        If x is undefined, x is taken as inside the window region.
        If ax is undefined, a new axis is generated
        Kwargs must be a dictionary of legal matplotlib keywords
        """
        if x == None:
            x = self.x
        if not ax:
            fig = plt.figure()
            ax = fig.add_subplot(111)
            
        kill_label = False
        if 'label' not in kwargs.keys():
            kwargs['label'] = 'Difference'
            kill_label = True
        

        y = self(x) + offset

        window = self.window
        self.set_window([x.min(),x.max()])
        
        y -= self.y
        ax.plot(x,y,**kwargs)
        plt.locator_params(axis = 'y', nbins = 4)
        self.set_window(window)
        
        if kill_label:
            del kwargs['label']
Пример #2
0
def plot_KDE(sol, var1, var2, fig=None, ax=None, draw=True, save=False, save_as_png=False, dpi=None):
    """
    Like the hexbin plot but a 2D KDE
    Pass mcmcinv object and 2 variable names as strings
    """
    ext = ['png' if save_as_png else 'pdf'][0]
    if fig == None or ax == None:
        fig, ax = plt.subplots(figsize=(3,3))
    MDL = sol.MDL
    if var1 == "R0":
        stoc1 = "R0"
    else:
        stoc1 =  ''.join([i for i in var1 if not i.isdigit()])
        stoc_num1 = [int(i) for i in var1 if i.isdigit()]
    try:
        x = MDL.trace(stoc1)[:,stoc_num1[0]-1]
    except:
        x = MDL.trace(stoc1)[:]
    if var2 == "R0":
        stoc2 = "R0"
    else:
        stoc2 =  ''.join([i for i in var2 if not i.isdigit()])
        stoc_num2 = [int(i) for i in var2 if i.isdigit()]
    try:
        y = MDL.trace(stoc2)[:,stoc_num2[0]-1]
    except:
        y = MDL.trace(stoc2)[:]
    xmin, xmax = min(x), max(x)
    ymin, ymax = min(y), max(y) 
    # Peform the kernel density estimate
    xx, yy = np.mgrid[xmin:xmax:100j, ymin:ymax:100j]
    positions = np.vstack([xx.ravel(), yy.ravel()])
    values = np.vstack([x, y])
    kernel = gaussian_kde(values)
    kernel.set_bandwidth(bw_method='silverman')
#        kernel.set_bandwidth(bw_method=kernel.factor * 2.)
    f = np.reshape(kernel(positions).T, xx.shape)

    ax.set_xlim(xmin, xmax)
    ax.set_ylim(ymin, ymax)
    plt.sca(ax)
    # Contourf plot
    plt.grid(None)
    plt.ticklabel_format(style='sci', axis='both', scilimits=(0,0))
    plt.xticks(rotation=90)
    plt.locator_params(axis = 'y', nbins = 7)
    plt.locator_params(axis = 'x', nbins = 7)
    ax.contourf(xx, yy, f, cmap=plt.cm.viridis, alpha=0.8)
    ax.scatter(x, y, color='k', s=1, zorder=2)

    plt.ylabel("%s" %var2)
    plt.xlabel("%s" %var1)

    if save: 
        fn = 'KDE-%s-%s.%s'%(sol.model_type_str,sol.filename,ext)
        save_figure(fig, subfolder='2D-KDE', fname=fn, dpi=dpi)
    
    plt.close(fig)        
    if draw:    return fig
    else:       return None
    def make_es_verbose_profile_graph(self,frame, taskid, status=None, daterange=None):
        #plt.switch_backend('Cairo')
        plt.style.use('fivethirtyeight')
        fig = plt.figure(figsize=(20, 15))
        plt.locator_params(axis='x', nbins=30)
        plt.locator_params(axis='y', nbins=30)
        plt.title('Execution profile for task {0}'.format(taskid), fontsize=24)
        plt.xlabel("Event Merge time", fontsize=18)
        plt.ylabel("Number of merged events", fontsize=18)
        starttime = self.get_task_start(taskid)['starttime']
        plt.axvline(x=starttime, color='b', linewidth=4, label="Task start time")

        if not starttime is None:
            min = starttime
        else:
            min = frame.values[:,0].min()

        max = frame.values[:,0].max()
        plt.xlim(xmax=max)
        plt.xlim(xmin=min)
        plt.xticks(rotation=25)

        ax = plt.gca()
        xfmt = md.DateFormatter('%m-%d %H:%M:%S')
        ax.xaxis.set_major_formatter(xfmt)

        plt.plot(frame.modificationtime, frame.nevents, '.r', label='# merged events')
        plt.legend(loc='lower right')
        return fig
Пример #4
0
def save_records_plot(file_path, ls_monitors, name, n_train_batches, legend_loc="upper right"):
    """
    Save a plot of a list of monitors' history.
    Args:
        file_path (string): the folder path where to save the plot
        ls_monitors: the list of statistics to plot
        name: name of file to be saved
        n_train_batches: the total number of training batches
    """

    lines = ["--", "-", "-.",":"]
    linecycler = cycle(lines)

    plt.figure()
    for m in ls_monitors:
        X = [i/float(n_train_batches) for i in m.history_minibatch]
        Y = m.history_value
        a, b = zip(*sorted(zip(X, Y)))
        plt.plot(a, b, next(linecycler), label=m.name)

    plt.xlabel('Training epoch')
    plt.ylabel(ls_monitors[0].type)
    plt.legend(loc=legend_loc)
    plt.locator_params(axis='y', nbins=7)
    plt.locator_params(axis='x', nbins=10)
    plt.savefig(file_path + name + ".png")
    tikz_save(file_path + name + ".tikz", figureheight = '\\figureheighttik', figurewidth = '\\figurewidthtik')
Пример #5
0
def plot_results(dists):
    for i, d in enumerate(dists):
        ax = plt.subplot(3,3,(4*i)+1)
        N, bins, patches = plt.hist(d.data, color="b",ec="k", bins=30, \
                                    range=tuple(d.lims), normed=True, \
                                    edgecolor="k", histtype='bar',linewidth=1.)
        fracs = N.astype(float)/N.max()
        norm = Normalize(-.2* fracs.max(), 1.5 * fracs.max())
        for thisfrac, thispatch in zip(fracs, patches):
            color = cm.gray_r(norm(thisfrac))
            thispatch.set_facecolor(color)
            thispatch.set_edgecolor("w")
        x = np.linspace(d.data.min(), d.data.max(), 100)
        ylim = ax.get_ylim()
        plt.plot(x, d.best.pdf(x), "-r", lw=1.5, alpha=0.7)
        ax.set_ylim(ylim)
        plt.axvline(d.best.MAPP, c="r", ls="--", lw=1.5)
        plt.tick_params(labelright=True, labelleft=False, labelsize=10)
        plt.xlim(d.lims)
        plt.locator_params(axis='x',nbins=10)
        if i < 2:
            plt.setp(ax.get_xticklabels(), visible=False)
        else:
            plt.xlabel(r"[$\mathregular{\alpha}$ / Fe]")
        plt.minorticks_on()
    def hist2D(dist1, dist2):
        """ Plot distribution and confidence contours. """
        X, Y = np.mgrid[dist1.lims[0] : dist1.lims[1] : 20j,
                        dist2.lims[0] : dist2.lims[1] : 20j]
        extent = [dist1.lims[0], dist1.lims[1], dist2.lims[0], dist2.lims[1]]
        positions = np.vstack([X.ravel(), Y.ravel()])
        values = np.vstack([dist1.data, dist2.data])
        kernel = stats.gaussian_kde(values)
        Z = np.reshape(kernel(positions).T, X.shape)
        ax.imshow(np.rot90(Z), cmap="gray_r", extent=extent, aspect="auto",
                  interpolation="spline16")
        plt.axvline(dist1.best.MAPP, c="r", ls="--", lw=1.5)
        plt.axhline(dist2.best.MAPP, c="r", ls="--", lw=1.5)
        plt.tick_params(labelsize=10)
        ax.minorticks_on()
        plt.locator_params(axis='x',nbins=10)
        return
    ax = plt.subplot(3,3,4)
    hist2D(dists[0], dists[1])
    plt.setp(ax.get_xticklabels(), visible=False)
    plt.ylabel("[Z/H]")
    plt.xlim(dists[0].lims)
    plt.ylim(dists[1].lims)
    ax = plt.subplot(3,3,7)
    hist2D(dists[0], dists[2])
    plt.ylabel(r"[$\mathregular{\alpha}$ / Fe]")
    plt.xlabel("log Age (yr)")
    plt.xlim(dists[0].lims)
    plt.ylim(dists[2].lims)
    ax = plt.subplot(3,3,8)
    plt.xlabel("[Z/H]")
    hist2D(dists[1], dists[2])
    plt.xlim(dists[1].lims)
    plt.ylim(dists[2].lims)
    return
Пример #6
0
def main():
  global algorithms
  global datadir
  global history
  global legend_location
  args = argparser.parse_args()
  algorithms = set(algorithms) - set(args.exclude_algorithm)
  datadir = args.datadir
  history = args.history
  legend_location = args.legend_location
  plt.rc('font', size=5)
  plt.rc('axes', color_cycle=['r','g','b','y'])
  plt.figure(figsize=(2,2))
  plt.locator_params(axis='x', nbins=5)
  if args.plot_all or args.steps_frequency:
    plot_steps_frequency()
  if args.plot_all or args.stepssum_vs_opcount:
    plot_stepssum_vs_opcount()
  if args.plot_all or args.nodesmax_vs_opcount:
    plot_nodesmax_vs_opcount()
  if args.plot_all or args.stepssum_vs_history:
    plot_stepssum_vs_history()
  if args.plot_all or args.nodesmax_vs_history:
    plot_nodesmax_vs_history()
  if args.plot_all or args.stepsavg_vs_history:
    plot_stepsavg_vs_history()
  if args.plot_all or args.stepsmed_vs_history:
    plot_stepsmed_vs_history()
  if args.plot_all or args.stepsavgdev_vs_history:
    plot_stepsavgdev_vs_history()
  if args.plot_all or args.stepsmedmax_vs_history:
    plot_stepsmedmax_vs_history()
Пример #7
0
def init_plotting():

    plt.rcParams['figure.figsize']       = (16, 9)
    plt.rcParams['figure.dpi']           = 75
    plt.rcParams['font.size']            = 16
    plt.rcParams['font.family']          = 'Times New Roman'
    plt.rcParams['axes.labelsize']       = 18
    plt.rcParams['axes.titlesize']       = 20
    plt.rcParams['legend.fontsize']      = plt.rcParams['font.size']
    plt.rcParams['xtick.labelsize']      = plt.rcParams['font.size']
    plt.rcParams['ytick.labelsize']      = plt.rcParams['font.size']
    plt.rcParams['xtick.major.size']     = 3
    plt.rcParams['xtick.minor.size']     = 3
    plt.rcParams['xtick.major.width']    = 1
    plt.rcParams['xtick.minor.width']    = 1
    plt.rcParams['ytick.major.size']     = 3
    plt.rcParams['ytick.minor.size']     = 3
    plt.rcParams['ytick.major.width']    = 1
    plt.rcParams['ytick.minor.width']    = 1
    plt.rcParams['legend.frameon']       = True
    plt.rcParams['legend.shadow']        = True
    plt.rcParams['legend.loc']           = 'lower left'
    plt.rcParams['legend.numpoints']     = 1
    plt.rcParams['legend.scatterpoints'] = 1
    plt.rcParams['axes.linewidth']       = 1
    plt.rcParams['savefig.dpi']          = 300
    plt.rcParams['xtick.minor.visible']  = 'False'
    plt.rcParams['ytick.minor.visible']  = 'False'
    plt.gca().xaxis.set_ticks_position('bottom')
    plt.gca().yaxis.set_ticks_position('left')
    plt.locator_params(nticks=4)
Пример #8
0
def plot_posterior_hist(ax, variable, samples, validation_data=None):
    pyplot.locator_params(axis = 'x', nbins = 4)
    histogram = ax.hist(samples, bins=12)
    if validation_data:
        axvline(x=validation_data[variable])
    ax.set_xlabel(variable)
    ax.set_ylim(0, max(histogram[0])*1.1)
Пример #9
0
    def plot(self, fig=None):
        import matplotlib.pyplot as plt
        if fig == None and not isinstance(self.fig, plt.Figure):
            self.fig = plt.figure()
        else:
            self.fig = fig
        # plt.gcf(self.fig)
        plt.figure(self.fig.number)
        plt.hold(True)

        from numpy import max as npmax

        mode = "logpow"
        if mode == "logpow":
            gdata = self.get_logpow()
            plt.ylabel("power [db]")

        plt.plot(self.get_xdata() / 1000., gdata)

        ymax = int(npmax(gdata[1:]) / 10.) * 10 + 10
        ymin = ymax - 80

        plt.ylim(ymin, ymax)

        plt.xlabel('Frequency [kHz]')
        plt.locator_params(nbins=20, axis='x', tight=True)
        # plt.locator_params(nbins=15, axis='y', tight=True, fontsize=1)
        plt.grid()
        plt.title(self.name)
        return self
Пример #10
0
def graph_drugs_line(items,nic):

	months = Config.filenames
	months = [x.strip('.csv') for x in months]
	months.reverse()

	for drug in items.keys():
		items_list = []
		nics=[]
		for month in months:
			try:
				items_list.append(items[drug][month])
				nics.append(nic[drug][month])
			except KeyError:
				print drug + ' not all information available'				
				break
		else:						 
			
			index = numpy.arange(len(months))
			graph = plt.plot(index, items_list, 'r.-', index, nics, 'g.-')
			
			lessMonths = [months[int(i)] for i in numpy.linspace(0,len(months)-1,num=6)]
			ax = plt.gca()
			plt.locator_params(nbins=len(lessMonths))
			ax.set_xticklabels(lessMonths)
			
			ax.set_ylabel('Branded/Generic')
			ax.set_title('Percent branded for chemical: '+drug)

			ax.legend( ('items', 'nic') )

			plt.savefig('Time_ChemPercents_figures/' + drug)
			plt.clf()
Пример #11
0
def main():
     #The url is made up of the prefix, day, and suffix:
     prefix = "http://www.wunderground.com/history/airport/KLGA/2016/1/"
     suffix = "/DailyHistory"
     days = []          #Sets up a list to store days
     mins = []           #Sets up a list so store min values
     for day in range(1,32): #For each day
          days.append(day)       #Add the day to the list
          url = prefix+str(day)+suffix      #Make the url
          M = getTempFromWeb("Min",url)     #Call the function to extract temp
          mins.append(M) #Add the temp to the list
          print day, M 
 ##    days = [i for i in range(1,32)]   Used for debugging programin
 ##    mins = [36,34,36,15,13,26,32,34,41,42,28,26,24,24,34,42,31,20,18,30,27,22,25,24,30,35,34,29,32,30,37]
     ave = float(sum(mins))/ len(mins)
     print ave
     print len(mins)
     

     
     scaled= [i*100/ave-100 for i in mins]
     
     
   
     plt.plot(days, scaled, color='r', label="Variation of Temp")       #Plot max as red
     plt.axhline(y=0,color='b')
     plt.legend(['% Difference From Avg'], loc='upper left',prop={'size':10})
     plt.title("Variation of January Min Temps from Average Min", y=1.02)       #Title for plot
     plt.xlabel('Days', fontsize=16).set_color("white")                     #Label for x-axis
     plt.ylabel('% Fluctuation from Average', fontsize=16).set_color("white")                   #Label for the y-axis
     plt.axis([0,32,-60,60])
     plt.locator_params(axis = 'x', nbins = 16)
     plt.locator_params(axis = 'y', nbins = 20)

     plt.show()
Пример #12
0
def plot_dshift(data, sample, fout, dshift_ind=0, close=True):
    """ 
    PLOT THE POSTERIOR FOR THE DOPPLER SHIFT
    """
    
    fig = plt.figure(figsize=(12,9))
    plt.locator_params(axis = 'x', nbins = 6)

    # Plot a historgram and kernel density estimate
    ax = fig.add_subplot(111)
    sns.distplot(sample[:,11+dshift_ind], hist_kws={"histtype":"stepfilled"}, ax=ax)
    plt.xticks(rotation=45)


    _, ymax = ax.get_ylim()
    ax.vlines(data["dshift"], 0, ymax, lw=3, color="black")

    ax.set_xlabel(r"Doppler shift $d=v/c$")
    ax.set_ylabel("$N_{\mathrm{samples}}$")
    plt.tight_layout()

    plt.savefig(fout+"_dshift%i.png"%dshift_ind, format="png")
    if close:
        plt.close()
    return
Пример #13
0
def plot3D(data_3d, name='3D Plot'):

    X = [point[0] for point in data_3d]
    Y = [point[1] for point in data_3d]
    Z = [point[2] for point in data_3d]

    fig = plt.figure(name)
    ax = fig.add_subplot(111, projection='3d')
    ax.scatter(X, Y, Z, marker='o', c='b', zdir='y')

    # Create cubic bounding box to simulate equal aspect ratio
    max_range = np.array(
        [max(X) - min(X), max(Y) - min(Y), max(Z) - min(Z)]).max()
    Xb = 0.5 * max_range * \
        np.mgrid[-1:2:2, -1:2:2, -1:2:2][0].flatten() + 0.5 * (max(X) + min(X))
    Yb = 0.5 * max_range * \
        np.mgrid[-1:2:2, -1:2:2, -1:2:2][1].flatten() + 0.5 * (max(Y) + min(Y))
    Zb = 0.5 * max_range * \
        np.mgrid[-1:2:2, -1:2:2, -1:2:2][2].flatten() + 0.5 * (max(Z) + min(Z))

    # Comment or uncomment following both lines to test the fake bounding box:
    for xb, yb, zb in zip(Xb, Yb, Zb):
        ax.plot([xb], [yb], [zb], 'w')

    ax.set_xlabel('X (m)')
    ax.set_ylabel('Z (m)')
    ax.set_zlabel('Y (m)')

    plt.locator_params(nbins=4)
    plt.show()
Пример #14
0
def show_trand_chart(stats):
    try:
        import matplotlib.pyplot as plt
    except ImportError:
        sys.exit(0)

    # prepare data
    net_lines = 0
    xlabels, axisx, axisy = [], [], []
    for stat in stats.items():
        weeks_ago, added, deleted = stat[0], stat[1]['add'], stat[1]['del']
        net_lines += added - deleted
        if net_lines == 0:
            continue

        xlabels.append(date_of_weeks_ago(weeks_ago))
        axisx.append(weeks_ago)
        axisy.append(net_lines)

    # start to plot
    plt.locator_params(axis = 'x', nbins = 4)
    plt.grid(True)
    plt.xlabel('weeks ago')
    plt.ylabel('LoC')
    plt.title('LineOfCode trend')
    plt.xticks(axisx, xlabels)
    plt.plot(axisx, axisy)
    plt.show()
Пример #15
0
def drawGiantHistogram(deltaTimes, numberOfBins, leftRange, rightRange, saveDirectory):
     
    print("generating histogram...")

    plt.figure(figsize=(16,6.5))
    plt.hist(deltaTimes, bins=numberOfBins, range=[leftRange,rightRange])
    
    # control spacing of x axis
    plt.locator_params(nbins=32,axis='x')
    
    plt.title("All Differences in Phone vs. Scintillator Timestamps")
    plt.xlabel(r'$\Delta$' "time in ms")
    plt.ylabel("Number of Hits")
    plt.xticks(rotation='vertical')
    
    # tweak spacing
    plt.subplots_adjust(left=0.05, right=0.95, top=0.9, bottom=0.21)
   
    # specify file naming and save location
    fileName = str(leftRange)+"to"+str(rightRange)+"_"+str(numberOfBins)+"bins"+".png"
    saveLocation = saveDirectory+"/giantHistograms/"+fileName

    # make save directory if it doesn't exist
    if not os.path.exists(saveDirectory+"giantHistograms/"):
        os.makedirs(saveDirectory+"giantHistograms/")

    # save the image
    plt.savefig(saveLocation, bbox_inches='tight')
    
    print("saved figure at", (saveLocation))

    plt.show()
    def plot_kmeans_components(self, fig1, gs, kmeans_clusters, clrs, plot_title='Hb', num_subplots=1,
                               flag_separate=1, gridspecs=[0, 0]):
        with sns.axes_style('darkgrid'):
            if flag_separate:
                ax1 = fig1.add_subplot(2, 1, num_subplots)
            else:
                ax1 = eval('fig1.add_subplot(gs' + gridspecs + ')')

            plt.gca().set_color_cycle(clrs)
            for ii in xrange(0, size(kmeans_clusters, 1)):
                plt.plot(kmeans_clusters[:, ii], lw=4, label=str(ii))

            plt.locator_params(axis='y', nbins=4)
            sns.axlabel("Time (seconds)", "a.u")
            ax1.legend(prop={'size': 14}, loc='center left', bbox_to_anchor=(1, 0.5), ncol=1, fancybox=True,
                       shadow=True)

            plt.title(plot_title, fontsize=14)

            plt.ylim((min(kmeans_clusters) - 0.0001,
                      max(kmeans_clusters) + 0.0001))
            plt.xlim((0, size(kmeans_clusters, 0)))
            plt.axhline(y=0, linestyle='-', color='k', linewidth=1)
            self.plot_vertical_lines_onset()
            self.plot_vertical_lines_offset()
def main():
    dir='/Users/ph290/Public/mo_data/ostia/' # on ELD140
    filename = dir + '*.nc'

    cube = iris.load_cube(filename,'sea_surface_temperature',callback=my_callback)
    #reads in data using a special callback, because it is a nasty netcdf file
    cube.data=cube.data-273.15


    sst_mean = cube.collapsed('time', iris.analysis.MEAN)
    #average all 12 months together
    sst_stdev=cube.collapsed('time', iris.analysis.STD_DEV)

    caribbean = iris.Constraint(
                                    longitude=lambda v: 260 <= v <= 320,
                                    latitude=lambda v: 0 <= v <= 40,
                                    name='sea_surface_temperature'
                                    )

    caribbean_sst_mean = sst_mean.extract(caribbean)
    caribbean_sst_stdev = sst_stdev.extract(caribbean)
    #extract the Caribbean region

    fig = plt.figure()
    ax = plt.axes(projection=ccrs.PlateCarree())
    data=caribbean_sst_mean.data
    data2=caribbean_sst_stdev.data
    lons = caribbean_sst_mean.coord('longitude').points
    lats = caribbean_sst_mean.coord('latitude').points
    lo = np.floor(data.min())
    hi = np.ceil(data.max())
    levels = np.linspace(lo,hi,100)
    lo2 = np.floor(data2.min())
    hi2 = np.ceil(data2.max())
    levels2 = np.linspace(lo2,5,10)
    cube_label = 'latitude: %s' % caribbean_sst_mean.coord('latitude').points
    contour=plt.contourf(lons, lats, data,transform=ccrs.PlateCarree(),levels=levels,xlabel=cube_label)
    #filled contour the annually averaged temperature
    contour2=plt.contour(lons, lats, data2,transform=ccrs.PlateCarree(),levels=levels2,colors='k')
    #contour the standard deviations
    plt.clabel(contour2, inline=0.5, fontsize=12,fmt='%1.1f' )
    ax.add_feature(cartopy.feature.LAND)
    ax.coastlines()
    ax.add_feature(cartopy.feature.RIVERS)
    ax.add_feature(cartopy.feature.BORDERS, linestyle=':')
    #ax.add_feature(cartopy.feature.LAKES, alpha=0.5)
    cbar = plt.colorbar(contour, ticks=np.linspace(lo,hi,7), orientation='horizontal')
    cbar.set_label('Sea Surface Temperature ($^\circ$C)')
    # enable axis ticks
    ax.xaxis.set_visible(True)
    ax.yaxis.set_visible(True)
    # fix 10-degree increments and don't clip range
    plt.locator_params(steps=(1,10), tight=False)
    # add gridlines
    plt.grid(True)
    # add axis labels
    plt.xlabel('longitude')
    plt.ylabel('latitude')
    #plt.show()
    plt.savefig('/home/h04/hador/public_html/twiki_figures/carib_sst_and_stdev.png')
def triangular_wave_plot(ax=None):
    """Plot of a triangular wave function.
    """
    import numpy as np
    import matplotlib.pyplot as plt

    if ax is None:
        fig, ax = plt.subplots(1, 1, figsize=(9, 3))
    t = np.array([-3, -2, -1, 0, 1, 2, 3])*np.pi
    x = np.array([0, 1, 0, 1, 0, 1, 0])
    ax.plot(t, x, linewidth=3, label=r'Square wave')
    ax.spines['bottom'].set_position('zero')
    ax.spines['top'].set_color('none')
    ax.spines['left'].set_position('zero')
    ax.spines['right'].set_color('none')
    ax.xaxis.set_ticks_position('bottom')
    ax.yaxis.set_ticks_position('left')
    ax.tick_params(axis='both', direction='inout', which='both', length=5)
    ax.set_xlim((-3*np.pi, 3*np.pi))
    ax.set_ylim((-0.1, 1.1))
    ax.set_xticks(np.linspace(-3*np.pi-0.1, 3*np.pi+0.1, 7))
    ax.set_xticklabels(['$-3\pi$', '$-2\pi$', '$-\pi$', '$0$', '$\pi$', '$2\pi$', '$3\pi$'],
                       fontsize=16)
    plt.locator_params(axis='y', nbins=3)
    ax.annotate(r'$t$', xy=(3*np.pi, 0.1), xycoords = 'data', xytext=(0, 0),
                textcoords = 'offset points', size=18, color='k')
    ax.annotate(r'$x[t]$', xy=(.1, 1.03), xycoords = 'data', xytext=(0, 0),
                textcoords = 'offset points', size=18, color='k')
    ax.grid()
    fig.tight_layout()

    return ax
Пример #19
0
def plot_rejection_sampling(thetas, posteriors, x_accepts, bins):
	""" Plot analytical solution and rejection sampling solution on same graph """
	fig, ax = plt.subplots()
	plt.plot(thetas, posteriors, linewidth=3)
	
	#Rejection sampling plot
	hist, bin_edges = numpy.histogram(x_accepts, bins)	
	bin_width = bin_edges[1] - bin_edges[0]
	hist = hist / max(hist)
	ax.bar(bin_edges[:-1], hist, bin_width, color='green')
	ax.tick_params(axis='both', which='major', labelsize=30)

	# Create strings to show numerical mean and standard deviation on graphs
	mean = numpy.mean(x_accepts)
	stdev = numpy.std(x_accepts)
	display_string = ('$\mu_{{MC}} = {0:.3f} $\n$\sigma_{{MC}} = {1:.3f}$').format(mean, stdev)

	plt.xlabel(r'$\theta$', fontsize=30)
	plt.ylabel(r'$\propto P(\theta|x)$', fontsize=30)
	plt.text(0.6, 0.8, display_string, transform=ax.transAxes, fontsize=30)
	plt.locator_params(axis='x', nbins=5)
	plt.savefig('rejection.png', bbox_inches='tight')

	# Plot log
	fig, ax = plt.subplots()
	plt.plot(thetas, -numpy.log(posteriors), linewidth=3)
	ax.bar(bin_edges[:-1], -numpy.log(hist), bin_width, color='green')
	ax.tick_params(axis='both', which='major', labelsize=30)

	plt.xlabel(r'$\theta$', fontsize=30)
	plt.ylabel(r'$\propto log(P(\theta|x))$', fontsize=30)
	plt.locator_params(axis='x', nbins=5)
	plt.text(0.3, 0.5, display_string, transform=ax.transAxes, fontsize=30)

	plt.savefig('rejlog.png', bbox_inches='tight')
Пример #20
0
def square_wave_plot(ax=None):
    """Plot of a square wave function.
    """
    import numpy as np
    import matplotlib.pyplot as plt

    if ax is None:
        fig, ax = plt.subplots(1, 1, figsize=(9, 3))
    N = np.nan
    t = np.array([-3, -2, N, -2, -1, N, -1, 0, N, 0, 1, N, 1, 2, N, 2, 3])
    x = np.array([-1, -1, N, 1, 1, N, -1, -1, N, 1, 1, N, -1, -1, N, 1, 1])
    ax.plot(t, x, linewidth=3, label=r"Square wave")
    ax.spines["bottom"].set_position("zero")
    ax.spines["top"].set_color("none")
    ax.spines["left"].set_position("zero")
    ax.spines["right"].set_color("none")
    ax.xaxis.set_ticks_position("bottom")
    ax.yaxis.set_ticks_position("left")
    ax.tick_params(axis="both", direction="inout", which="both", length=5)
    ax.set_xlim((-3, 3))
    ax.set_ylim((-1.4, 1.4))
    plt.locator_params(axis="y", nbins=3)
    ax.annotate(r"$t[s]$", xy=(3, 0.1), xycoords="data", xytext=(0, 0), textcoords="offset points", size=18, color="k")
    ax.annotate(
        r"$x[t]$", xy=(-0.3, 1.2), xycoords="data", xytext=(0, 0), textcoords="offset points", size=18, color="k"
    )
    for label in ax.get_xticklabels() + ax.get_yticklabels():
        label.set_bbox(dict(facecolor="white", edgecolor="None", alpha=0.65))
    ax.grid()
    fig.tight_layout()

    return ax
Пример #21
0
def format_and_label_axes(var, posys, axes, ylabel=True):
    "Formats and labels axes"
    for posy, ax in zip(posys, axes):
        if ylabel:
            if hasattr(posy, "key"):
                ylabel = (posy.key.descr.get("label", posy.key.name)
                          + " " + posy.key.unitstr(dimless="-"))
            else:
                ylabel = str(posy)
            ax.set_ylabel(ylabel)
        ax.grid(color="0.6")
        # ax.set_frame_on(False)
        for item in [ax.xaxis.label, ax.yaxis.label]:
            item.set_fontsize(9)
        for item in ax.get_xticklabels() + ax.get_yticklabels():
            item.set_fontsize(7)
        ax.tick_params(length=0)
        ax.spines['left'].set_visible(False)
        ax.spines['top'].set_visible(False)
        for i in ax.spines.itervalues():
            i.set_linewidth(0.4)
            i.set_color("0.6")
            i.set_linestyle("dotted")
    xlabel = (var.key.descr.get("label", var.key.name)
              + " " + var.key.unitstr(dimless="-"))
    ax.set_xlabel(xlabel)  # pylint: disable=undefined-loop-variable
    plt.locator_params(nbins=4)
    plt.subplots_adjust(wspace=0.15)
Пример #22
0
def main():
     #The url is made up of the prefix, day, and suffix:
     prefix = "http://www.wunderground.com/history/airport/KLGA/2016/1/"
     suffix = "/DailyHistory"
     days = []
     depths = []
     mins = [36,34,36,15,13,26,32,34,41,42,28,26,24,24,34,42,31,20,18,30,27,22,25,24,30,35,34,29,32,30,37] #min temps for January 2016
 #    days = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31]          #Sets up a list to store days
 #    depths = []           #Sets up a list so store snow depths
     for day in range(1,32): #For each day
          days.append(day)       #Add the day to the list
          url = prefix+str(day)+suffix      #Make the url
          M = getDepthFromWeb(url)     #Call the function to extract Snow Depth
          
          depths.append(M) #Add the temp to the list
          print day, M 
 #    depths = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,27,20,15,9,6,4,3,2]
     depthScale = [(8)*((n*(n*.09))+1) for n in depths]
     plt.scatter(days, mins, s = depthScale, color = 'r', alpha=0.5)
     plt.title("Minimum Temps in January with Snow Depths as Point Size", y=1.02)       #Title for plot
     plt.xlabel('Days', fontsize=16).set_color("white")                     #Label for x-axis
     plt.ylabel('Minimum Temperature of the Day', fontsize=16).set_color("white")                   #Label for the y-axis
     plt.axis([0,32,10,50])
     plt.locator_params(axis = 'x', nbins = 16)
     plt.locator_params(axis = 'y', nbins = 20)
     l1 = plt.scatter([],[], s=8, edgecolors='none', color = 'r')
     labels = [" = 0 in. or Trace Amounts of Snow"]
     leg = plt.legend([l1], labels, ncol=4, frameon=True, fontsize=12,
     handlelength=1, loc = 'upper left', borderpad = .5,
     handletextpad=1, title='Size of Points Corresponds to Snow Depth', scatterpoints = 1)
     plt.show()
Пример #23
0
def plot_diagnostics(image,data_filtered,pl_image,smooth_pl,inter_image,inter_pl,final_image,vgridsize,lg,ilng,flng,IV,output_file):
    fig3 = plt.figure(figsize=(8,vgridsize*2))
    gs3 = gridspec.GridSpec(vgridsize,2)
    for i in range(0,vgridsize):
        pl = np.power(lg,abs(IV[i,1]))    
        ax1 = fig3.add_subplot(gs3[i,0])
        [a,b,c,d] = ax1.plot(lg,np.multiply(pl,image[i,:]),lg,np.multiply(pl,data_filtered[i,:]),lg,np.multiply(pl,pl_image[i,:]),lg,np.multiply(pl,smooth_pl[i,:]))
        plt.setp(ax1.get_xticklabels(), visible=False)
        ax1.set_ylim([min(np.multiply(pl,image[i,:])),max(np.multiply(pl,image[i,:]))]) 
        plt.locator_params(axis='y',nbins=5)    
        if i < 1:
            ax1.set_title('Raw Images')
            plt.legend([a,b,c,d], ['Data', 'Data boxcar', 'Power Law','Power Law boxcar'],bbox_to_anchor=(0, 2.0, 1., .102),loc=9,
               borderaxespad=0.)
        ax2 = fig3.add_subplot(gs3[i,1],sharex=ax1)
        [e,f,g] = ax2.plot(lg,inter_image[i,:],lg,inter_pl[i,:],lg,final_image[i,:])
        plt.setp(ax2.get_xticklabels(), visible=False) 
        ax2.set_xlim([ilng+3,flng-3])
        ax2.set_ylim([min(inter_image[i,:]),max(final_image[i,:])])     
        ax2.hlines(0,ilng,flng,linestyles='dashed',color='0.75')
        #plt.locator_params(axis='y',nbins=5)     
        if i <1 :
            ax2.set_title('Subtracted Images')
            plt.legend([e,f,g], ['Subtracted Image', 'Subtracted Power Law', 'Final Image'],bbox_to_anchor=(0, 1.8, 1., .102),loc=9,
               borderaxespad=0.)
    plt.setp(ax1.get_xticklabels(),visible=True)
    plt.setp(ax2.get_xticklabels(),visible=True)
    plt.savefig(output_file+'_diagnostics.png')
    plt.close()
def main(plot_type):
    per_size = 5
    nrow, ncol = len(graphs), len(params)
    fig = plt.figure(figsize=(ncol * per_size, nrow * per_size))

    if plot_type.startswith('dist'):
        angle = (10, 45)
    else:
        angle = (15, 210)

    for i, gname in enumerate(graphs):
        for j, param in enumerate(params):
            idx = i * ncol + j + 1
            ax = fig.add_subplot(nrow, ncol, idx, projection='3d')
            plot_surface(gname, param, plot_type,
                         fig, ax=ax,
                         dirname=dirname,
                         angle=angle,
                         use_colorbar=False)
            ax.set_title('{}({})'.format(gname, param))
            plt.locator_params(axis='y', nbins=5)
            plt.locator_params(axis='x', nbins=5)

    fig_dir = 'figs/{}'.format(fig_dirname)
    if not os.path.exists(fig_dir):
        os.makedirs(fig_dir)
    figpath = '{}/{}.pdf'.format(fig_dir, plot_type)
    print(figpath)
    fig.savefig(figpath)
Пример #25
0
	def plot_histogram(self, values, bins, img_out, masked, median, mean, std, name):
		"""
		Statistical plotting and output function.

		Input: Value list, Bin list, Name of output, Masked image, median value, mean value, standard dev, image name
		Output: png image file with the masked image, statistical and image information, and histogram plot
		"""

		plt.clf()

		#Fill in the masked image for processing

		scaled_img = self.scale_img(masked, median, std)

		img = Image.fromarray(scaled_img)


		#Set up plotting environment
		fig, ax = plt.subplots(2,1)
		fig.set_size_inches(8,11)       # width, height
		fig.tight_layout()
		gs = gridspec.GridSpec(2,1,height_ratios=[4,1], wspace=0.0, hspace=0.0)

		#Find timestamp, change this to use header info instead
		timestamp = name.split('_')
		try:
			timetest = timestamp[1]
		except:
			timetest = 'NA'

		#Plot the masked image, allow for arbitrary rotation
		ax0 = plt.subplot(gs[0])
		ax0.axis('off')

		img = img.rotate(90).resize((int(img.size[1]),int(img.size[0])), Image.ANTIALIAS)

		# Insert statistical information into the image
		ax0.text(0, 1240, name[0:4]+'-'+name[4:6]+'-'+name[6:8]+'   '+name[9:11]+':'+name[11:13]+':'+name[13:15], size = 16, color="white", horizontalalignment='left')
		ax0.text(0, 1280, 'Exposure = '+str(timetest)+' [s]', size = 16, color="white", horizontalalignment='left', )
		ax0.text(1100, 1200 , 'Median = %.1f' % (median), size = 16, color="white", horizontalalignment='right')
		ax0.text(1100, 1240, "Mean = %.2f" % (mean), size = 16, color="white", horizontalalignment='right')
		ax0.text(1100, 1280, 'Standard Dev = %.2f' % (std), size = 16, color="white", horizontalalignment='right')
		ax0.imshow(img, cmap="gray")

		#Plot the histogram
		ax1 = plt.subplot(gs[1])
		ax1.bar(bins, (values*100.0), alpha=1.0)
		ax1.set_xlabel('Pixel Value', size=16)
		ax1.xaxis.label.set_color('white')
		plt.locator_params(axis='y',nbins=6)
		ax1.tick_params(axis='x', colors='white', labelsize=12)
		#ax1.yaxis().set_visible(False)
		plt.draw()

		#Save the figure as a png
		gs.tight_layout(fig, h_pad=None)
		fig.savefig(img_out, cmap="grey", transparent=True, facecolor="black", edgecolor='none')
		plt.close("all")
		return
Пример #26
0
def plot_pr_curve(precision, recall, title):
    plt.rcParams['figure.figsize'] = 7, 5
    plt.locator_params(axis='x', nbins=5)
    plt.plot(precision, recall, 'b-', linewidth=4.0, color='#B0017F')
    plt.title(title)
    plt.xlabel('Precision')
    plt.ylabel('Recall')
    plt.rcParams.update({'font.size': 16})
def plot_scores(matched_signals, unique_clrs, ind, stimulus_on_time, stimulus_off_time):
######Plot mean signals according to color and boxplot of number of pixels in each plane
    sns.tsplot(np.array(matched_signals[ind].clr_grped_signal), linewidth=3, ci=95, err_style="ci_band", color=unique_clrs[ind])  
    plt.locator_params(axis = 'y', nbins = 4)            
    sns.axlabel("Time (seconds)","a.u")            
    plot_vertical_lines_onset(stimulus_on_time)
    plot_vertical_lines_offset(stimulus_off_time)
    plt.axhline(y=0, linestyle='-', color='k', linewidth=1)
Пример #28
0
def pdf_norm_plot(m=0, s=1, fig=None, ax=None):
    """Plot the probability density function of the normal distribution.
    """
    import numpy as np
    import matplotlib.pyplot as plt
    from scipy.stats import norm
    
    m, s = float(m), float(s)  # in case they were passed as command line args
    if s <= 0:
        s = 1
    plt.rc('font', size=16)
    plt.rc(('xtick.major','ytick.major'), pad=8)
    n = 1000
    if fig is None:
        fig, ax = plt.subplots(1, 1, figsize=(10, 5))
    
    x = np.linspace(m-4*s, m+4*s, n)
    # pdf at x for a random variable with normal distribution
    f = norm.pdf(x, loc=m, scale=s)  
    ones = np.arange(np.round(3/8*n)+1, np.round(5/8*n)+1, dtype=int)
    twos = np.arange(np.round(2/8*n)+1, np.round(6/8*n)+1, dtype=int)
    threes = np.arange(np.round(1/8*n)+1, np.round(7/8*n)+1, dtype=int)
    ax.fill_between(x[ones], y1=f[ones], y2=0, color=[0, 0.2, .5, .4])
    ax.fill_between(x[twos], y1=f[twos], y2=0, color=[0, 0.2, .5, .3])
    ax.fill_between(x[threes], y1=f[threes], y2=0, color=[0, 0.2, .5, .3])
    ax.plot(x, f, color=[1, 0, 0, .8], linewidth=4)
    ax.set_ylim(0, ax.get_ylim()[1]*1.1)
    ymax = ax.get_ylim()[1]/0.45
    for i in range(-3, 4):
        ax.axvline(i*s+m, ymin=0, ymax=f[n/2-i/8*n]/ax.get_ylim()[1], c='w', lw=3)
    ax.axvline(-2.5*s+m, ymin=0.01, ymax=.15, c='k', lw=1)
    ax.axvline(2.5*s+m, ymin=0.01, ymax=.15, c='k', lw=1)
    ax.axvline(-3.5*s+m, ymin=0.01, ymax=.05, c='k', lw=1)
    ax.axvline(3.5*s+m, ymin=0.01, ymax=.05, c='k', lw=1)
    ax.text(-3.8*s+m, .03*ymax, '0.1%', color='k')
    ax.text(3.2*s+m, .03*ymax, '0.1%', color='k')
    ax.text(-2.8*s+m, .08*ymax, '2.1%', color='k')
    ax.text(2.2*s+m, .08*ymax, '2.1%', color='k')
    ax.text(-1.85*s+m, .03*ymax, '13.6%',  color='w')
    ax.text(1.15*s+m, .03*ymax, '13.6%', color='w')
    ax.text(-.85*s+m, .08*ymax, '34.1%', color='w')
    ax.text(.15*s+m, .08*ymax, '34.1%', color='w')
    plt.locator_params(axis = 'y', nbins = 5)
    ax.set_xticks(np.linspace(m-4*s, m+4*s, 9))
    xtl = [r'%+d$\sigma$' %i for i in range(-4, 5, 1)]
    xtl[4] = r'$\mu$'
    ax.set_xticklabels(xtl)
    #ax2 = ax.twiny()
    #ax2.xaxis.set_ticks_position('bottom')
    #ax2.spines["bottom"].set_position(("axes", -0.1))
    #ax2.set_xlim(-4*s, 4*s)
    #ax2.set_xticks(np.linspace(m-4*s, m+4*s, 9))
    title='Probability density function of the normal distribution ' +\
    '~ $N(\mu=%.1f,\ \sigma^2=%.1f)$' %(m, s**2)
    plt.title(title, fontsize=14)
    plt.show()

    return fig, ax
Пример #29
0
def evaluation_comparison(repo_name, version,
                          pre_fetch_size=0.1, distance_to_fetch=0.5,
                          branch='master', **kwargs):

    fontsize = 18
    figsize = 18, 9
    n = 30
    f_beta = 2
    (tp_01, fp_01, tn_01, fn_01, fc_01, counter_01) = _get_statistics(
        repo_name=repo_name, cache_ratio=0.1,
        pre_fetch_size=pre_fetch_size, distance_to_fetch=distance_to_fetch,
        version=version, n=n, branch=branch, **kwargs)

    (tp_02, fp_02, tn_02, fn_02, fc_02, counter_02) = _get_statistics(
        repo_name=repo_name, cache_ratio=0.2,
        pre_fetch_size=pre_fetch_size, distance_to_fetch=distance_to_fetch,
        version=version, n=n, branch=branch, **kwargs)

    ind = np.arange(len(counter_01))
    linewidth = 3
    fig, ax = plt.subplots(figsize=figsize)
    plt.locator_params(axis='x', nbins=n)
    ax.set_ylim(0.0, 1.1)
    precision_01 = tp_01 / (tp_01 + fp_01)
    recall_01 = tp_01 / (tp_01 + fn_01)
    f1_01 = (1 + f_beta**2) * tp_01 / (
        (1 + f_beta**2) * tp_01 + f_beta**2 * fn_01 + fp_01)

    precision_02 = tp_02 / (tp_02 + fp_02)
    recall_02 = tp_02 / (tp_02 + fn_02)
    f1_02 = (1 + f_beta**2) * tp_02 / (
        (1 + f_beta**2) * tp_02 + f_beta**2 * fn_02 + fp_02)

    ax.set_xticklabels(counter_01)
    plt.plot(ind, precision_01, '--', label='Precision (cr=0.1)',
             color='green', linewidth=linewidth)
    plt.plot(ind, precision_02,
             color='green', linewidth=linewidth, label='Precision (cr=0.2)')

    plt.plot(ind, f1_01, '--', color='orange', linewidth=linewidth,
             label=r'$F_2$ score (cr=0.1)')
    plt.plot(ind, f1_02, color='orange', linewidth=linewidth,
             label=r'$F_2$ score (cr=0.2)')

    plt.plot(ind, recall_01, '--', color='blue', linewidth=linewidth,
             label='Recall (cr=01)')
    plt.plot(ind, recall_02, color='blue', linewidth=linewidth,
             label='Recall (cr=0.2)')

    plt.grid(True)
    plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3,
               ncol=3, mode="expand", borderaxespad=0., fontsize=fontsize)
    plt.title('Evaluating %s.git' % (repo_name,), fontsize=fontsize, y=1.15)
    fig.subplots_adjust(top=0.8)
    plt.tick_params(labelsize=fontsize)
    plt.xlabel('Fixing commit numbers after tNow*0.9', fontsize=fontsize)
    fig.tight_layout()
    plt.show()
    def Responses(self,PlotStartTime,PlotEndTime,DoSkip=[]):
        NotSkippedGroups = [G for G in self.final_data['SortedGroupsList'] if not any(ext in G for ext in DoSkip)]
        # spio.savemat(os.path.join(self.CXOutputPath,'SpikesToPlot.mat'),GroupsSpikes)
        # else:
        #     GroupsSpikes = self.loadmat(os.path.join(self.CXOutputPath,'SpikesToPlot.mat'))

        f, axarr = plt.subplots(int(round(len(NotSkippedGroups)/2.)), 2, sharex=True, figsize=(8, 17))
        axarr_twins = [col.twinx() for row in axarr for col in row]
        axarr_twins = np.reshape(axarr_twins, (int(round(len(NotSkippedGroups)/2.)), 2))
        PSTH_bin = 0.005
        statistics = []
        for idx, Group in enumerate(NotSkippedGroups):
            plt_y_idx = idx % (int(round(len(NotSkippedGroups)/2.)))
            plt_x_idx = int(idx / (int(round(len(NotSkippedGroups)/2.))))
            Scatter_Xs = np.concatenate(self.final_data['GroupsSpikes'][Group])
            Scatter_Ys = np.concatenate(
                [(np.ones(len(TimePoint)) * (int(TimePoint_idx) + 1)).astype(int) for TimePoint_idx, TimePoint in
                 enumerate(self.final_data['GroupsSpikes'][Group])])
            axarr[plt_y_idx, plt_x_idx].scatter(Scatter_Xs, Scatter_Ys, s=1, color='0.5')
            Step_Xs = np.arange(0, self.final_data['runtime'], PSTH_bin)
            Step_Ys = np.zeros_like(Step_Xs)
            for X in np.unique(Scatter_Xs):
                bin_idx = np.where(Step_Xs == min(Step_Xs, key=lambda x: abs(x - X)))
                Step_Ys[bin_idx] += len(np.where(Scatter_Xs == X)[0])
            axarr_twins[plt_y_idx, plt_x_idx].step(Step_Xs, Step_Ys, where='mid', c='b')
            axarr[plt_y_idx, plt_x_idx].plot([self.final_data['InputTime'], self.final_data['InputTime']], [0, len(self.final_data['FileList'])], color='r', linestyle='dotted',
                                             linewidth=2)
            axarr[plt_y_idx, plt_x_idx].spines['top'].set_color('none')
            axarr_twins[plt_y_idx, plt_x_idx].spines['top'].set_color('none')
            axarr[plt_y_idx, plt_x_idx].xaxis.set_ticks_position('bottom')
            axarr_twins[plt_y_idx, plt_x_idx].xaxis.set_ticks_position('bottom')
            axarr[plt_y_idx, plt_x_idx].set_ylim(0, len(self.final_data['FileList']))
            CurrentTitle = Group[Group.index('_') + 1:].replace('_L',' Layer ').replace('toL',' to Layer ')
            axarr[plt_y_idx, plt_x_idx].set_title(CurrentTitle)
            axarr[plt_y_idx, plt_x_idx].set_xlim(PlotStartTime, PlotEndTime)
            axarr_twins[plt_y_idx, plt_x_idx].set_xlim(PlotStartTime, PlotEndTime)
            if max(Step_Ys[int(np.where(np.array(Step_Xs)>=PlotStartTime)[0][0]):int(np.where(np.array(Step_Xs)<=PlotEndTime)[0][-1])]) == 0:
                axarr_twins[plt_y_idx, plt_x_idx].set_ylim(0, 1)
            else:
                axarr_twins[plt_y_idx, plt_x_idx].set_ylim(0, max(Step_Ys[int(np.where(np.array(Step_Xs)>=PlotStartTime)[0][0]):int(np.where(np.array(Step_Xs)<=PlotEndTime)[0][-1])]) )
            y_lower,y_upper = axarr_twins[plt_y_idx, plt_x_idx].get_ylim()
            if y_upper > 5 :
                axarr_twins[plt_y_idx, plt_x_idx].yaxis.set_ticks(np.arange(int(y_lower), int(y_upper)+1, (int(y_upper) - int(y_lower)) / 5))
            else:
                axarr_twins[plt_y_idx, plt_x_idx].yaxis.set_ticks(np.arange(int(y_lower), int(y_upper)+1))
            if plt_x_idx == 0 :
                axarr[plt_y_idx, plt_x_idx].set_ylabel('Trials')
            cropped_steps = Step_Ys[int(np.where(np.array(Step_Xs) >= PlotStartTime)[0][0]):int(np.where(np.array(Step_Xs) <= PlotEndTime)[0][-1])]
            statistics.append([CurrentTitle,st.chisquare(cropped_steps)[0],st.chisquare(cropped_steps)[1]])
        axarr[-1,0].set_xlabel('time (s)')
        axarr[-1,1].set_xlabel('time (s)')
        plt.locator_params(axis='x', nbins=5)
        plt.tight_layout()
        plt.show()
        f.savefig(os.path.join(self.IllustratorOutputFolder,'cell_type_response.eps'))
        with open (os.path.join(self.IllustratorOutputFolder,'table.txt'),'w') as table_file:
            table_file.write(tabulate(statistics,headers=['Group Name','Chi-Square','p-Value']))
Пример #31
0
def experiment(task, reg_fact=0., simulate=True, outdir=None, title=''):
    """
  Perform an LSM experiment.

  :param task: target task for learning
  :param reg_fact: regularization parameter for readout training, default: 0
  :param simulate: whether SNN should be simulated, otherwise load data from pickle
  :param outdir: output directory for plots
  :param title: title for plots
  """

    pklfile = 'data.pkl'  # file name for data
    if simulate:
        # setup nest

        nest.ResetKernel()

        num_cpu = mp.cpu_count()
        nest.SetKernelStatus({
            'local_num_threads': num_cpu,
            'print_time': True
        })
        seeds = np.random.choice(10000, size=num_cpu + 1)
        #nest.SetKernelStatus({'grng_seed': seeds[0], 'rng_seeds': seeds[1:].tolist()})
        nest.SetKernelStatus({'grng_seed': seeds[0]})

        # parameters

        sim_time = 500e3  # simulation time
        N_rec = 500  # number of neurons from which we will extract liquid states

        # ----------------------------------------------------------------------
        # create recurrent network nodes

        # TODO: create excitatory and inhibitory neurons, a noise generator,
        # and spike detectors using the parameters in the assignment sheet

        E_population = nest.Create('iaf_psc_exp',
                                   1000,
                                   params={
                                       'C_m': 30.0,
                                       'tau_m': 30.0,
                                       'E_L': 0.0,
                                       'V_th': 15.0,
                                       'V_reset': 13.8,
                                       'tau_syn_ex': 3.0,
                                       'tau_syn_in': 2.0,
                                       'I_e': 14.5
                                   })

        I_population = nest.Create('iaf_psc_exp',
                                   250,
                                   params={
                                       'C_m': 30.0,
                                       'tau_m': 30.0,
                                       'E_L': 0.0,
                                       'V_th': 15.0,
                                       'V_reset': 13.8,
                                       'tau_syn_ex': 3.0,
                                       'tau_syn_in': 2.0,
                                       'I_e': 14.5
                                   })

        population = E_population + I_population

        noise_generator = nest.Create('poisson_generator',
                                      params={'rate': 25.0})
        parrot_neuron = nest.Create('parrot_neuron')
        nest.Connect(noise_generator, parrot_neuron)

        E_spike_detector = nest.Create('spike_detector')
        nest.Connect(E_population, E_spike_detector)
        I_spike_detector = nest.Create('spike_detector')
        nest.Connect(I_population, I_spike_detector)

        # ----------------------------------------------------------------------
        # create stimulus and input nodes

        # create input generators
        num_sig = 3
        stim_len = 50.
        stim_dt = 500.

        # inputs: holds the actual inputs (3 bits per stimulus)
        # input_spikes: holds the spike times of 3 input neurons
        inputs, input_spikes = generate_stimulus(num_sig, sim_time, stim_len,
                                                 stim_dt)

        # create spike generators

        # TODO: create spike_generators and set spike times as given in input_spikes
        input_neurons = nest.Create('parrot_neuron', 3)
        for i in range(3):
            spike_generator = nest.Create(
                'spike_generator', 1, params={'spike_times': input_spikes[i]})
            nest.Connect(spike_generator, (input_neurons[i], ))

        in_spike_detector = nest.Create('spike_detector')
        nest.Connect(input_neurons, in_spike_detector)

        # ----------------------------------------------------------------------
        # connect nodes

        # TODO: connect nodes with given statistics

        delay_params = {
            'distribution': 'normal_clipped',
            'low': 3.0,
            'high': 200.0,
            'mu': 10.0,
            'sigma': 20.0
        }

        syn_spec_EE = {
            'model': 'tsodyks_synapse',
            'weight': 50.0,
            'delay': delay_params,
            'tau_psc': 2.,
            'tau_fac': 1.,
            'tau_rec': 813.,
            'U': .59,
            'u': .59,
            'x': 0.
        }

        syn_spec_EI = {
            'model': 'tsodyks_synapse',
            'weight': 250.0,
            'delay': delay_params,
            'tau_psc': 2.,
            'tau_fac': 1790.,
            'tau_rec': 399.,
            'U': .049,
            'u': .049,
            'x': 0.
        }

        syn_spec_IE = {
            'model': 'tsodyks_synapse',
            'weight': -200.0,
            'delay': delay_params,
            'tau_psc': 2.,
            'tau_fac': 376.,
            'tau_rec': 45.,
            'U': .016,
            'u': .016,
            'x': 0.
        }

        syn_spec_II = {
            'model': 'tsodyks_synapse',
            'weight': -200.0,
            'delay': delay_params,
            'tau_psc': 2.,
            'tau_fac': 21.,
            'tau_rec': 706.,
            'U': .25,
            'u': .25,
            'x': 0.
        }

        J = (0.5 * 50.0 + 1.5 * 50.0) / 2.0
        syn_spec_inp = {
            'model': 'static_synapse',
            'weight': {
                'distribution': 'normal_clipped',
                'low': 0.5 * 50.0,
                'high': 1.5 * 50.0,
                'mu': J,
                'sigma': 0.7
            },
            'delay': delay_params
        }

        syn_spec_noise = {'weight': 5.0, 'delay': delay_params}

        nest.Connect(parrot_neuron, population, {'rule': 'all_to_all'},
                     syn_spec_noise)
        nest.Connect(E_population, E_population, {
            'rule': 'fixed_indegree',
            'indegree': 2
        }, syn_spec_EE)
        nest.Connect(I_population, I_population, {
            'rule': 'fixed_indegree',
            'indegree': 1
        }, syn_spec_II)
        nest.Connect(I_population, E_population, {
            'rule': 'fixed_indegree',
            'indegree': 1
        }, syn_spec_IE)
        nest.Connect(E_population, I_population, {
            'rule': 'fixed_indegree',
            'indegree': 2
        }, syn_spec_EI)

        nest.Connect(input_neurons, E_population, {
            'rule': 'fixed_outdegree',
            'outdegree': 100
        }, syn_spec_inp)

        # ----------------------------------------------------------------------
        # simulate

        nest.Simulate(sim_time)

        # ----------------------------------------------------------------------
        # analysis

        stat_X = nest.GetStatus(in_spike_detector, 'events')[0]
        stat_E = nest.GetStatus(E_spike_detector, 'events')[0]
        stat_I = nest.GetStatus(I_spike_detector, 'events')[0]

        spikes_X = stat_X['times'], stat_X['senders']
        spikes_E = stat_E['times'], stat_E['senders']
        spikes_I = stat_I['times'], stat_I['senders']

        # compute mean firing rates

        # TODO: compute rates
        rate_ex = ((len(spikes_E[0]) / 1000) / sim_time) * 1000
        rate_in = ((len(spikes_I[0]) / 250) / sim_time) * 1000

        # plot network activity
        # TODO: plot
        t_max_plot = sim_time

        fig, [ax1, ax2, ax3] = plt.subplots(3, 1, sharex=True)
        plt.locator_params(nbins=6)

        ax1.scatter(spikes_X[0] / 1000, spikes_X[1], c='red', marker='.', s=1.)
        ax1.set_xlim(0., t_max_plot / 1000)
        ax1.set_yticks([])
        ax1.set_ylabel(r'X')

        E_samples = np.random.choice(E_population, size=100, replace=False)
        I_samples = np.random.choice(I_population, size=100, replace=False)

        E_sample_spike_t, E_sample_neurons = [], []
        for i in range(len(spikes_E[0])):
            n = spikes_E[1][i]
            if n in E_samples:
                E_sample_spike_t.append(spikes_E[0][i] / 1000)
                E_sample_neurons.append(spikes_E[1][i])

        I_sample_spike_t, I_sample_neurons = [], []
        for i in range(len(spikes_I[0])):
            n = spikes_I[1][i]
            if n in I_samples:
                I_sample_spike_t.append(spikes_I[0][i] / 1000)
                I_sample_neurons.append(spikes_I[1][i])

        ax2.scatter(E_sample_spike_t,
                    E_sample_neurons,
                    c='green',
                    marker='.',
                    s=1.)
        ax2.set_xlim(0., t_max_plot / 1000)
        ax2.set_yticks([])
        ax2.set_ylabel(r'E')

        ax3.scatter(I_sample_spike_t,
                    I_sample_neurons,
                    c='blue',
                    marker='.',
                    s=1.)
        ax3.set_xlim(0., t_max_plot / 1000)
        ax3.set_yticks([])
        ax3.set_xlabel(r'$t$ / s')
        ax3.set_ylabel(r'I')

        if outdir and title:
            plt.savefig(join(outdir, title + '_spikes.pdf'))

        # extract spike times in an array per neuron
        spike_times = get_spike_times_by_id(spikes_E, E_population)

        if len(spike_times) == 0:
            print('no spikes, cannot extract liquid states')
            return

        with open(pklfile, 'wb') as f:
            pkl.dump([
                num_sig, inputs, spike_times, stim_dt, stim_len, N_rec,
                sim_time, rate_ex, rate_in
            ], f)

    else:
        with open(pklfile, 'rb') as f:
            num_sig, inputs, spike_times, stim_dt, stim_len, N_rec, sim_time, rate_ex, rate_in = pkl.load(
                f)

    print('mean excitatory rate: {0:.2f} Hz'.format(rate_ex))
    print('mean inhibitory rate: {0:.2f} Hz'.format(rate_in))

    # generate targets
    if task == 'none':
        targets = None

    elif task == 'sum':
        targets = np.sum(inputs, axis=1)  # TODO: define targets using inputs

    elif task == 'xor':
        targets = np.logical_xor(
            inputs[:, 0], inputs[:, 1])  # TODO: define targets using inputs

    elif task == 'mem1':
        targets = inputs[:, 0]  # TODO: define targets using inputs

    elif task == 'memall':
        targets = inputs  # TODO: define targets using inputs

    else:
        raise ValueError()

    assert task == 'none' or len(targets) == inputs.shape[0]

    # ----------------------------------------------------------------------
    # train readouts

    # drop initial responses to allow dynamics to settle
    num_discard = 5

    # number of classifiers to train
    num_trained_classifiers = 20

    # liquid state: time constant for filtering spikes
    tau_lsm = 20.  # ms

    if task == 'none':
        pass

    elif task == 'xor' or task == 'sum':
        readout_delay = 20.  # ms
        rec_time_start = stim_dt + stim_len + readout_delay  # time of first liquid state
        times = np.arange(rec_time_start, sim_time, stim_dt)  # all times

        # extract liquid states at given times
        states = get_liquid_states(spike_times[:N_rec], times, tau_lsm)

        # discard first few states
        states = states[num_discard:, :]
        targets = targets[num_discard:]

        print(
            'training readouts using regularization = {0:e}'.format(reg_fact))

        train_err_, test_err_ = [], []

        for i in range(num_trained_classifiers):
            states_train, states_test, targets_train, targets_test = train_test_split(
                states, targets, train_size=.8, test_size=.2)

            train_err, test_err = train_readout(states_train,
                                                targets_train,
                                                states_test,
                                                targets_test,
                                                reg_fact=reg_fact)

            train_err_ += [train_err]
            test_err_ += [test_err]

        train_mean, train_std = np.mean(train_err_), np.std(train_err_)
        test_mean, test_std = np.mean(test_err_), np.std(test_err_)

        print('  train error mean: {0:.3f}, std: {1:.3f}'.format(
            train_mean, train_std))
        print('  test error mean: {0:.3f}, std: {1:.3f}'.format(
            test_mean, test_std))

    elif task == 'mem1' or task == 'memall':
        readout_delays = np.arange(10., stim_dt - stim_len, 20.)

        # TODO: for each readout delay, extract the hidden states using a time
        # constant of tau_lsm, discard the first num_discard states, train
        # num_trained_classifiers classifiers on the states, and add the mean
        # errors to the lists

        targets_orig = targets
        train_mean_list, train_std_list, test_mean_list, test_std_list = [], [], [], []

        for readout_delay in readout_delays:
            rec_time_start = stim_dt + stim_len + readout_delay
            times = np.arange(rec_time_start, sim_time, stim_dt)
            states = get_liquid_states(spike_times[:N_rec], times, tau_lsm)

            states = states[num_discard:, :]
            targets = targets_orig[num_discard:]
            print('training readouts using regularization = {0:e}'.format(
                reg_fact))

            train_err_, test_err_ = [], []
            for i in range(num_trained_classifiers):
                states_train, states_test, targets_train, targets_test = train_test_split(
                    states, targets, train_size=.8, test_size=.2)

                train_err, test_err = train_readout(states_train,
                                                    targets_train,
                                                    states_test,
                                                    targets_test,
                                                    reg_fact=reg_fact)

                train_err_ += [train_err]
                test_err_ += [test_err]

            train_mean, train_std = np.mean(train_err_), np.std(train_err_)
            test_mean, test_std = np.mean(test_err_), np.std(test_err_)

            print("readout delay %d" % readout_delay)
            print('  train error mean: {0:.3f}, std: {1:.3f}'.format(
                train_mean, train_std))
            print('  test error mean: {0:.3f}, std: {1:.3f}'.format(
                test_mean, test_std))
            print()

            train_mean_list.append(train_mean)
            train_std_list.append(train_std)
            test_mean_list.append(test_mean)
            test_std_list.append(test_std)

        # plot results

        assert len(readout_delays) == len(train_mean_list)
        assert len(readout_delays) == len(test_mean_list)

        plt.figure(figsize=(12., 4.))

        plt.subplot(1, 2, 1)
        plt.errorbar(readout_delays, train_mean_list, train_std_list)
        plt.xlabel('$\Delta t$')
        plt.ylabel('train error')
        plt.tight_layout()

        plt.subplot(1, 2, 2)
        plt.errorbar(readout_delays, test_mean_list, test_std_list)
        plt.xlabel('$\Delta t$')
        plt.ylabel('test error')
        plt.tight_layout()

        if outdir and title:
            plt.savefig(join(outdir, title + '_errors.pdf'))
Пример #32
0
path6 = 'logs/2019.11.06-164702/quantized_checkpoint.pth.tar'  # symetric int8 with  per-channel quantization (FR=5e-4 and seed =675)
path7 = 'logs/2019.11.06-164317/quantized_checkpoint.pth.tar'  # asymetric int8 with no per-channel quantization (FR=5e-4 and seed =675)
path8 = 'logs/2019.11.06-164231/quantized_checkpoint.pth.tar'  # asymetric int8 with  per-channel quantization (FR=5e-4 and seed =675)

model_1 = torch.load(path1, map_location='cpu')
# model_2 = torch.load(path8, map_location = 'cpu')

for module, layer in zip(modules, layers):
    data_1 = model_1['state_dict'][module].numpy().flatten()
    # data_2 = model_2['state_dict'][module].numpy().flatten()
    plt.figure(figsize=(20, 20))

    a1 = plt.hist(data_1.flatten(),
                  bins=50,
                  alpha=0.5,
                  density=False,
                  label='Fault-free execution')
    # a2 = plt.hist(data_2.flatten(),bins=50, alpha=0.5, density=False, label='Faulty execution', color='brown')

    # a1 = plt.hist(data_1.flatten(),bins=50, alpha=0.5, density=False, label='no_per_channel')
    # a2 = plt.hist(data_2.flatten(),bins=50, alpha=0.5, density=False, label='per_channel', color = 'red')

    #plt.xlabel("Values", fontsize=70)
    #plt.ylabel("Frequency", fontsize=80)
    plt.xticks(fontsize=30, rotation=0)
    plt.yticks(fontsize=30, rotation=0)
    plt.locator_params(axis='x', nbins=6)  # For data point 1
    plt.title(layer, fontsize=30)
    plt.legend(loc='upper right', fontsize=30)
    plt.savefig('alexnet/' + layer + '.png', format='png')
Пример #33
0
    def plot_kmeans_components(self,
                               fig1,
                               gs,
                               kmeans_clusters,
                               uncentered_clrs,
                               plot_title='Hb',
                               num_subplots=1,
                               flag_separate=1,
                               gridspecs=[0, 0],
                               model_center=0,
                               removeclusters=0):

        with sns.axes_style('darkgrid'):
            if flag_separate:
                ax1 = fig1.add_subplot(2, 1, num_subplots)
            else:
                ax1 = eval('fig1.add_subplot(gs' + gridspecs + ')')

            if model_center == 1:  # Models colors according to trace
                clrs_cmap = Colorize.optimize(kmeans_clusters.T, asCmap=True)
                clrs = clrs_cmap.colors
            else:
                clrs_cmap = uncentered_clrs
                clrs = clrs_cmap.colors

            # Update kmeans if clusters were removed
            kmeans_clusters_new = copy(kmeans_clusters)
            if removeclusters != 0:
                newclrs_updated = copy(clrs_cmap)
                for index, value in enumerate(clrs_cmap.colors):
                    if index in removeclusters:
                        newclrs_updated.colors[index] = [0, 0, 0]
                clrs_cmap = newclrs_updated
                clrs = clrs_cmap.colors

                for ii in removeclusters:
                    print ii
                    kmeans_clusters_new[:,
                                        ii] = zeros(size(kmeans_clusters, 0))

            plt.gca().set_color_cycle(clrs)
            # remove those clusters that are ignored before plotting
            for ii in xrange(0, size(kmeans_clusters_new, 1)):
                plt.plot(kmeans_clusters_new[:, ii], lw=4, label=str(ii))

            plt.locator_params(axis='y', nbins=4)
            ax1.set(xlabel="Time (seconds)", ylabel="a.u")
            ax1.legend(prop={'size': 14},
                       loc='center left',
                       bbox_to_anchor=(1, 0.5),
                       ncol=1,
                       fancybox=True,
                       shadow=True)

            plt.title(plot_title, fontsize=14)

            plt.ylim((min(kmeans_clusters_new) - 0.0001,
                      max(kmeans_clusters_new) + 0.0001))
            plt.xlim((0, size(kmeans_clusters_new, 0)))
            plt.axhline(y=0, linestyle='-', color='k', linewidth=1)
            self.plot_vertical_lines_onset()
            self.plot_vertical_lines_offset()
            self.plot_stimulus_patch(ax1)

        return clrs_cmap
Пример #34
0
def plot_lightcurve(dbid,
                    mjd,
                    mag,
                    magerr,
                    bands,
                    survey,
                    trueredshift,
                    DBdir,
                    psfpage=None,
                    fname=None,
                    DESfname=None,
                    connectpoints=True,
                    specfile=None,
                    WavLL=3000,
                    WavUL=10500,
                    outlierflag=0,
                    zoominband=None,
                    outlierarr=None,
                    sdsscutoutradec=None,
                    psfmetric=None):
    try:
        test1 = mjd.index
        plotmacleod = True
        mjd, mag, magerr, bands, mjdmc, magmc, magerrmc, bandsmc = mjd[0], mag[
            0], magerr[0], bands[0], mjd[1], mag[1], magerr[1], bands[1]
    except:
        plotmacleod = False
    bcens = {
        'u': 3876.63943790537,
        'g': 4841.83358196563,
        'r': 6438 / 534828217,
        'i': 7820.99282740933,
        'z': 9172.34266385718,
        'Y': 9877.80238651117
    }
    VBfile = '%s/VanderBerk_datafile1.txt' % DBdir
    crv = np.loadtxt(VBfile, skiprows=23)
    redshift = np.copy(trueredshift)
    if redshift < 0: redshift = 0
    gdes, gsdss, gposs = np.where(survey == 'DES')[0], np.where(
        survey == 'SDSS')[0], np.where(survey == 'POSS')[0]
    POSSbands = np.array(['g', 'r', 'i'])
    if len(gposs) > 0:
        POSSmagdict, POSSmagerrdict, POSSmjddict = {
            b: mag[gposs][bands[gposs] == b]
            for b in POSSbands
        }, {b: np.zeros(0)
            for b in POSSbands}, {b: np.zeros(0)
                                  for b in POSSbands}
        for band in POSSbands:
            if len(POSSmagdict[band]) > 0:
                POSSmagdict[band] = np.array([np.median(POSSmagdict[band])])
                POSSmagerrdict[band] = np.array([np.mean(POSSmagdict[band])])
        if (len(POSSmagdict['g']) > 0) & (len(POSSmagdict['r']) > 0):
            mag[gposs][bands[gposs] == 'g'], mag[gposs][
                bands[gposs] ==
                'r'] = mag[gposs][bands[gposs] == 'g'] + 0.392 * (
                    POSSmagdict['g'] - POSSmagdict['r']) - 0.28, mag[gposs][
                        bands[gposs] == 'r'] + 0.127 * (POSSmagdict['g'] -
                                                        POSSmagdict['r']) + 0.1
            magerr[gposs][bands[gposs] == 'g'], magerr[gposs][
                bands[gposs] == 'r'] = np.sqrt(
                    1.392**2 * magerr[gposs][bands[gposs] == 'g']**2 +
                    0.392**2 * magerr[gposs][bands[gposs] == 'r']**2), np.sqrt(
                        magerr[gposs][bands[gposs] == 'r']**2 + 0.127**2 *
                        (magerr[gposs][bands[gposs] == 'g']**2 +
                         magerr[gposs][bands[gposs] == 'r']**2))
        else:
            if len(POSSmagdict['g']) > 0: mag[gposs][bands[gposs] == 'g'] = 0
            if len(POSSmagdict['r']) > 0: mag[gposs][bands[gposs] == 'r'] = 0
        if (len(POSSmagdict['i']) > 0) & (len(POSSmagdict['r']) > 0):
            mag[gposs][bands[gposs] ==
                       'i'] = mag[gposs][bands[gposs] == 'i'] + 0.27 * (
                           POSSmagdict['r'] - POSSmagdict['i']) + 0.32
            magerr[gposs][bands[gposs] == 'i'] = np.sqrt(
                magerr[gposs][bands[gposs] == 'i']**2 + 0.27**2 *
                (magerr[gposs][bands[gposs] == 'r']**2 +
                 magerr[gposs][bands[gposs] == 'i']**2))
        else:
            if len(POSSmagdict['i']) > 0: mag[gposs][bands[gposs] == 'i'] = 0
    bestdiff = {
        b: {
            'diff': 0,
            'ihi': 0,
            'ilo': 0
        }
        for b in ['g', 'r', 'i', 'z']
    }
    #for b in ['g','r','i','z']:
    for b in ['g']:
        if np.shape(outlierarr) != ():
            gb = np.where((outlierarr == 0) & (bands == b)
                          & (survey != 'POSS'))[0]
        else:
            gb = np.where((bands == b) & (survey != 'POSS'))[0]
        #if ((len(gsdss)>0)&(len(gdes)>0)):
        #    gsdssb,gdesb=np.where(bands[gsdss]==b)[0],np.where(bands[gdes]==b)[0]
        #    if ((len(gsdssb)>0)&(len(gdesb)>0)):
        if len(gb) > 0:
            magpairs = np.zeros([len(gb) * len(gb), 2])
            magpairs[:, 1], magpairs[:, 0] = np.repeat(mag[gb],
                                                       len(gb)), np.tile(
                                                           mag[gb], len(gb))
            magerrpairs = np.zeros([len(gb) * len(gb), 2])
            magerrpairs[:, 1], magerrpairs[:, 0] = np.repeat(
                magerr[gb], len(gb)), np.tile(magerr[gb], len(gb))
            magdiffs, differrs = magpairs[:, 0] - magpairs[:, 1], np.max(
                magerrpairs, axis=1)  #,np.sqrt(np.sum(magerrpairs**2,axis=1))
            ipairs = np.zeros([len(gb) * len(gb), 2])
            ipairs[:, 1], ipairs[:,
                                 0] = np.repeat(gb,
                                                len(gb)), np.tile(gb, len(gb))
            diffsigs = magdiffs / differrs
            gsig = np.where(differrs < 0.15)[0]
            if len(gsig) > 0:
                gmax = np.argsort(magdiffs[gsig])[-1]
                imax, imin = ipairs[:, 1][gsig[gmax]], ipairs[:, 0][gsig[gmax]]
                bestdiff[b]['diff'] = np.max(magdiffs[gsig])
                bestdiff[b]['ihi'], bestdiff[b]['ilo'] = imax, imin
    fig = plt.figure(1)
    fig.clf()
    ax3 = plt.subplot2grid((2, 10), (1, 0), colspan=6)
    plt.rc('axes', linewidth=2)
    plt.fontsize = 14
    plt.tick_params(which='major', length=8, width=2, labelsize=14)
    plt.tick_params(which='minor', length=4, width=1.5, labelsize=14)
    plt.locator_params(nbins=4)
    if zoominband == None:
        totdiffs = np.zeros(4)
        for ib, b in zip(np.arange(4), ['g', 'r', 'i', 'z']):
            totdiffs[ib] = bestdiff[b]['diff']
        ibest = np.argsort(totdiffs)[-1]
        bbest = ['g', 'r', 'i', 'z'][ibest]
        gbbest = np.where(bands == bbest)[0]
        imax, imin = bestdiff[bbest]['ihi'], bestdiff[bbest]['ilo']
        maxfluxes, minfluxes = np.zeros(4), np.zeros(4)
        maxfluxerrs, minfluxerrs = np.zeros(4), np.zeros(4)
        for ib, b in zip(np.arange(4), ['g', 'r', 'i', 'z']):
            gbt = np.where(bands == b)[0]
            maxfluxes[ib], maxfluxerrs[ib] = calc_flux(mjd, mag, magerr, bands,
                                                       b, mjd[imax])
            minfluxes[ib], minfluxerrs[ib] = calc_flux(mjd, mag, magerr, bands,
                                                       b, mjd[imin])
        if specfile != None:
            try:
                shdu = py.open(specfile)
                specdata = shdu[1].data
                sflux, swav = specdata['flux'], 10**(specdata['loglam'])
                s_closei = np.where(np.abs(swav - bcens['i']) < 20)[0]
                smwid = 10
                swav = swav[smwid:-smwid]
                normflux = sflux / np.mean(sflux[s_closei])
                smoothflux = [
                    np.mean(normflux[x - smwid:x + smwid + 1])
                    for x in np.arange(smwid,
                                       len(normflux) - smwid)
                ]
                ax3.plot(swav, smoothflux, lw=1, color='magenta', zorder=1)
            except IOError:
                specfile = None
        v_closei = np.where(
            np.abs(crv[:, 0] * (1. + redshift) - bcens['i']) < 20)[0]
        gvrange = np.where((crv[:, 0] * (1. + redshift) > WavLL)
                           & (crv[:, 0] * (1. + redshift) < WavUL))[0]
        if len(gvrange) > 0:
            vmax = np.max(crv[:, 1][gvrange] / np.mean(crv[:, 1][v_closei]))
        else:
            vmax = np.max(crv[:, 1])
        if trueredshift > 0:
            ax3.plot(crv[:, 0] * (1. + redshift),
                     crv[:, 1] / np.mean(crv[:, 1][v_closei]),
                     color='k',
                     lw=1,
                     zorder=2)
        plot_flux(ax3, maxfluxes, maxfluxerrs, label='Max', curcol='r')
        maxplot = np.max(maxfluxes)
        plot_flux(ax3, minfluxes, minfluxerrs, label='Min', curcol='b')
        if np.max(minfluxes) > maxplot: maxplot = np.max(minfluxes)
        ax3.set_xlabel('Wavelength (A)')
        ax3.set_ylabel('Flux (Arb. Units)')
        #ax3.legend(loc='lower right')
        plt.xlim(WavLL, WavUL)
        plt.ylim(-0.05, vmax * 1.05)
        if trueredshift <= 0:
            plt.ylim(-0.05, 1.15 * maxplot)
    else:
        plot_band(ax3,
                  mjd,
                  mag,
                  magerr,
                  bands,
                  zoominband,
                  connectpoints=connectpoints,
                  nolabels=False,
                  outlierarr=outlierarr,
                  psfmetric=psfmetric)
        if plotmacleod:
            plot_band(ax3,
                      mjdmc,
                      magmc,
                      magerrmc,
                      bandsmc,
                      zoominband,
                      connectpoints=connectpoints,
                      nolabels=False,
                      overridecolor='magenta')
        plt.axvline(mjd[imax], ls='dashed', lw=1, color='r')
        plt.axvline(mjd[imin], ls='dashed', lw=1, color='b')
        ylim = plt.ylim()
        if ylim[1] > 30:
            ylim = (ylim[0], np.max(mag) + 0.1)
        if ylim[1] > 30: ylim = (ylim[0], 30)
        if ylim[0] < 15:
            ylim = (np.min(mag) - 0.1, ylim[1])
        if ylim[0] < 15: ylim = (15, ylim[1])
        plt.ylim(ylim[1], ylim[0])
        xlim = plt.xlim()
        mjdcheck = mjd
        if plotmacleod: mjdcheck = np.append(mjdcheck, mjdmc)
        plt.xlim(
            np.min(mjdcheck) - 0.05 * (np.max(mjdcheck) - np.min(mjdcheck)),
            np.max(mjdcheck) + 0.05 * (np.max(mjdcheck) - np.min(mjdcheck)))
        ax3.set_xlabel('MJD')
        ax3.set_ylabel('%s_PSF' % zoominband)
    ax1 = plt.subplot2grid((2, 10), (0, 0), colspan=6)
    plt.rc('axes', linewidth=2)
    plt.fontsize = 14
    plt.tick_params(which='major', length=8, width=2, labelsize=14)
    plt.tick_params(which='minor', length=4, width=1.5, labelsize=14)
    plt.locator_params(nbins=4)
    mjdcheck = np.zeros(0)
    for b in ['g', 'r', 'i', 'z', 'Y']:
        plot_band(ax1,
                  mjd,
                  mag,
                  magerr,
                  bands,
                  b,
                  connectpoints=connectpoints,
                  nolabels=False)
        mjdcheck = np.append(mjdcheck, mjd)
    plt.xlim(
        np.min(mjdcheck) - 0.05 * (np.max(mjdcheck) - np.min(mjdcheck)),
        np.max(mjdcheck) + 0.05 * (np.max(mjdcheck) - np.min(mjdcheck)))
    xlim = plt.xlim()
    plt.xlim(xlim[0], xlim[1] + 0.2 * (xlim[1] - xlim[0]))
    ylim = plt.ylim()
    plt.axvline(mjd[imax], ls='dashed', lw=1, color='r')
    plt.axvline(mjd[imin], ls='dashed', lw=1, color='b')
    if ylim[1] > 30:
        ylim = (ylim[0], np.max(mag) + 0.1)
    if ylim[1] > 30: ylim = (ylim[0], 30)
    if ylim[0] < 15:
        ylim = (np.min(mag) - 0.1, ylim[1])
    if ylim[0] < 15: ylim = (15, ylim[1])
    plt.ylim(ylim[1], ylim[0])
    ax1.legend(frameon=False, prop={'size': 12}, scatterpoints=1)
    xlim = plt.xlim()
    if outlierflag == 1:
        ax1.text(0.5 * (xlim[0] + xlim[1]),
                 15. / 16 * ylim[0] + ylim[1] / 16.,
                 'OUTLIER',
                 color='r',
                 horizontalalignment='center')
    elif outlierflag == 2:
        ax1.text(0.5 * (xlim[0] + xlim[1]),
                 15. / 16 * ylim[0] + ylim[1] / 16.,
                 'BAD PHOTOMETRY',
                 color='r',
                 horizontalalignment='center')
    if zoominband == None: ax1.set_xlabel('MJD')
    ax1.set_ylabel('Mag_PSF')
    if redshift > 0:
        ax1.set_title('%s, z=%.4f' % (dbid, trueredshift))
    else:
        ax1.set_title(dbid)
    if not (DESfname in [None, 'False']):
        ax4 = plt.subplot2grid((2, 10), (1, 6),
                               colspan=4,
                               xticks=[],
                               yticks=[])
        img4 = mpimg.imread('%s/imagestamps/%s' % (DBdir, DESfname))
        ax4.imshow(img4)
        ax4.plot(np.array([0.5, 0.5]),
                 np.array([0.55, 0.8]),
                 color='yellow',
                 transform=ax4.transAxes)
        ax4.plot(np.array([0.5, 0.5]),
                 np.array([0.45, 0.2]),
                 color='yellow',
                 transform=ax4.transAxes)
        ax4.plot(np.array([0.45, 0.2]),
                 np.array([0.5, 0.5]),
                 color='yellow',
                 transform=ax4.transAxes)
        ax4.plot(np.array([0.55, 0.8]),
                 np.array([0.5, 0.5]),
                 color='yellow',
                 transform=ax4.transAxes)
        ax4.text(0.5,
                 0.9,
                 DESfname[3:-4],
                 color='white',
                 horizontalalignment='center',
                 transform=ax4.transAxes)
    if len(gsdss) > 0:
        SDSSfname = '%s/imagestamps/%s_SDSScutout.jpeg' % (DBdir, dbid)
        try:
            img3 = mpimg.imread(SDSSfname)
            ax3 = plt.subplot2grid((2, 10), (0, 6),
                                   colspan=4,
                                   xticks=[],
                                   yticks=[])
            ax3.imshow(img3)
            if sdsscutoutradec != None:
                ax3.text(0.5,
                         0.9,
                         sdsscutoutradec,
                         color='k',
                         horizontalalignment='center',
                         transform=ax3.transAxes)
        except:
            pass
    if psfpage != None: plt.savefig(psfpage, format='pdf')
    return
Пример #35
0
def make_scatter_plot_analysis(all_metrics, plot_param, out_file=None):
    if 'Regularization' in plot_param['title']:
        model_to_legend = {
            '25_nor_no_single_ctrl_bal_regr_all': 'No regularization',
            '25_nor_ndrop_single_ctrl_bal_regr_all': 'Dropout',
            '25_nor_saug_single_ctrl_bal_regr_all': 'Dropout + mild aug.',
            '25_nor_maug_single_ctrl_bal_regr_all': 'Dropout + heavy aug.'
        }
        model_to_id = {
            '25_nor_no_single_ctrl_bal_regr_all': 1,
            '25_nor_ndrop_single_ctrl_bal_regr_all': 2,
            '25_nor_saug_single_ctrl_bal_regr_all': 3,
            '25_nor_maug_single_ctrl_bal_regr_all': 4
        }
    elif 'Data distribution' in plot_param['title']:
        model_to_legend = {
            '25_nor_ndrop_single_ctrl_bal_regr_all':
            'Three cameras with noise',
            '25_nor_ndrop_single_ctrl_bal_regr_jcen':
            'Central camera with noise',
            '25_nor_ndrop_single_ctrl_bal_regr_nnjc':
            'Central camera, no noise',
            '25_nor_ndrop_single_ctrl_seq_regr_all':
            'Three cameras with noise, no balancing'
        }
        model_to_id = {
            '25_nor_ndrop_single_ctrl_bal_regr_nnjc': 1,
            '25_nor_ndrop_single_ctrl_bal_regr_jcen': 2,
            '25_nor_ndrop_single_ctrl_bal_regr_all': 3,
            '25_nor_ndrop_single_ctrl_seq_regr_all': 4
        }
    elif 'Model architecture' in plot_param['title']:
        model_to_legend = {
            '25_small_ndrop_single_ctrl_bal_regr_all': 'Shallow CNN',
            '25_nor_ndrop_single_ctrl_bal_regr_all': 'Standard CNN',
            '25_deep_ndrop_single_ctrl_bal_regr_all': 'Deep CNN',
            '25_nor_ndrop_lstm_ctrl_bal_regr_all': 'Standard LSTM'
        }
        model_to_id = {
            '25_small_ndrop_single_ctrl_bal_regr_all': 1,
            '25_nor_ndrop_single_ctrl_bal_regr_all': 2,
            '25_deep_ndrop_single_ctrl_bal_regr_all': 3,
            '25_nor_ndrop_lstm_ctrl_bal_regr_all': 4
        }
    else:
        model_to_legend = {
            '1_nor_maug_single_ctrl_bal_regr_all': '1 hour',
            '5_nor_maug_single_ctrl_bal_regr_all': '5 hours',
            '25_nor_maug_single_ctrl_bal_regr_all': '25 hours',
            '80_nor_maug_single_ctrl_bal_regr_all': '80 hours'
        }
        model_to_id = {
            '1_nor_maug_single_ctrl_bal_regr_all': 1,
            '5_nor_maug_single_ctrl_bal_regr_all': 2,
            '25_nor_maug_single_ctrl_bal_regr_all': 3,
            '80_nor_maug_single_ctrl_bal_regr_all': 4
        }

    town_to_legend = {'Town01_1': 'Town 1', 'Town02_14': 'Town 2'}
    town_to_id = {'Town01_1': 1, 'Town02_14': 2}

    def exp_to_legend_and_idx(exp):
        legend = exp
        idx = []
        for model in model_to_legend:
            if exp.startswith(model):
                legend = legend.replace(model, model_to_legend[model])
                idx.append(model_to_id[model])
                break
        for town in town_to_legend:
            if exp.endswith(town):
                legend = legend.replace(town, town_to_legend[town])
                idx.append(town_to_id[town])
                break
        legend = legend.replace('_', ', ')
        return legend, idx

    #3 Prepare the axes
    fig, ax = plt.subplots(figsize=(8, 8))

    # Color map
    plt.set_cmap('jet')
    cm = plt.get_cmap()
    cNorm = colors.Normalize(vmin=0, vmax=50)
    scalarMap = cmx.ScalarMappable(norm=cNorm, cmap=cm)

    #  Font size
    plt.rc('font', size=16)  # controls default text sizes
    plt.rc('axes', titlesize=16)  # fontsize of the axes title
    plt.rc('axes', labelsize=20)  # fontsize of the x and y labels
    plt.rc('xtick', labelsize=16)  # fontsize of the tick labels
    plt.rc('ytick', labelsize=16)  # fontsize of the tick labels
    plt.rc('legend', fontsize=14)  # legend fontsize
    plt.rc('figure', titlesize=16)  # fontsize of the figure title

    # compute limits so that x and y scaled w.r.t. their std
    print(
        np.log(plot_param['x_lim']
               ) if plot_param['x']['log'] else plot_param['x_lim'])
    ax.set_xlim(
        np.log(plot_param['x_lim']) /
        np.log(10.) if plot_param['x']['log'] else plot_param['x_lim'])
    ax.set_ylim(
        np.log(plot_param['y_lim']) /
        np.log(10.) if plot_param['y']['log'] else plot_param['y_lim'])

    # set num ticks
    plt.locator_params(axis='x', nbins=4)
    plt.locator_params(axis='y', nbins=8)

    # axes labels and log scaling
    x_label = 'Steering error'
    y_label = 'Success rate'
    if plot_param['x']['log']:
        x_label += ' (log)'
        ax.set_xticklabels(
            ['%.1e' % np.power(10, float(t)) for t in ax.get_xticks()])
    if plot_param['y']['log']:
        y_label += ' (log)'
        ax.set_yticklabels(
            ['%.1e' % np.power(10, float(t)) for t in ax.get_yticks()])
    ax.set_xlabel(x_label)
    ax.set_ylabel(y_label)

    # Plotting
    scatter_handles = {}
    for experiment, metrics in all_metrics.items():
        print(experiment)
        data = {'x': [], 'y': [], 'size': [], 'color': []}
        for key in data:
            data[key] = np.array(metrics[plot_param[key]['data']])

        # remove nans
        # if np.any(np.isnan(data['x'])) or np.any(np.isnan(data['y']) or np.any(np.isinf(data['x']) or np.any(np.isinf(data['y']))
        nans = np.logical_or.reduce((np.isnan(data['x']), np.isnan(data['y']),
                                     np.isinf(data['x']), np.isinf(data['y'])))
        print('\n ** Removing %d NaNs and infs before log **' % np.sum(nans))
        for key in data:
            data[key] = data[key][np.invert(nans)]

        data_x = np.log(
            data['x']) / np.log(10) if plot_param['x']['log'] else np.copy(
                data['x'])
        data_y = np.log(
            data['y']) / np.log(10) if plot_param['y']['log'] else np.copy(
                data['y'])

        nans = np.logical_or.reduce((np.isnan(data_x), np.isnan(data_y),
                                     np.isinf(data_x), np.isinf(data_y)))
        print('\n ** Removing %d NaNs and infs after log **' % np.sum(nans))
        for key in data:
            data[key] = data[key][np.invert(nans)]
        data_x = data_x[np.invert(nans)]
        data_y = data_y[np.invert(nans)]

        # the actual plotting
        color_val = scalarMap.to_rgba(hash(experiment) % 50)
        color_vec = [color_val] * len(data_x)
        # print('color_vec', color_vec)
        # print('data[\'color\']', data['color'])
        # print(len(data_x))
        scatter_handles[experiment] = ax.scatter(data_x,
                                                 data_y,
                                                 s=data['size'],
                                                 c=color_vec,
                                                 alpha=0.5)
        ax.plot(data_x, data_y, color=color_val)

    sorted_keys = sorted(scatter_handles.keys(),
                         key=lambda x: exp_to_legend_and_idx(x)[1])
    ax.legend([scatter_handles[k] for k in sorted_keys],
              [exp_to_legend_and_idx(k)[0] for k in sorted_keys])
    plt.title(plot_param['title'])

    # Save to out_file
    if plot_param['print']:
        fig.savefig(out_file, bbox_inches='tight')
    k += 1

Gen_error_Tree = Error_Test_Tree.mean()
Gen_error_KNearest = Error_Test_KNearest.mean()

# knearest neighbor plot
################################################
plt.figure()
plt.plot(minNumNeighbours)
plt.xlabel('model number')
plt.ylabel('optimal number of neighbours')
plt.show()

plt.figure()
plt.locator_params(nticks=KOuter)
plt.plot(100 * Error_Test_Tree)
plt.plot(100 * Error_Test_KNearest)
plt.xlabel('Modelnumber')
plt.ylabel('Classification error rate (%)')
plt.legend(['Decision Tree', 'K Kearest Neighbours'])
plt.show()
################################################

[tstatistic, pvalue] = stats.ttest_ind(Ttest_K_Nearest, Ttest_DTree)

if pvalue > 0.05:
    print('Classifiers are not significantly different')
else:
    print('Classifiers are significantly different.')
def PolyReg3D(x,
              y,
              deg,
              seed=0,
              splits_start=2,
              splits_stop=8,
              savefig=False,
              elasticnet=False,
              trd_val_name="Ângulo de Lode",
              trd_val_lim=(-1, 1)):
    splits_range = range(splits_start, splits_stop + 1)
    rows = len(splits_range) // 2
    fig = plt.figure(figsize=(12, 10))

    if elasticnet:
        poly_reg = ElasticNetRegression(degree=deg,
                                        alpha=elasticnet[0],
                                        l1_ratio=elasticnet[1])
        if deg == 1:
            fig.suptitle('Regressão Linear (alfa={},beta={})'.format(
                elasticnet[0], elasticnet[1]),
                         fontsize=16)
        else:
            fig.suptitle(
                'Regressão Polinomial Grau {} (alfa={},beta={})'.format(
                    deg, elasticnet[0], elasticnet[1]),
                fontsize=16)
    else:
        poly_reg = PolynomialRegression(degree=deg)
        if deg == 1:
            fig.suptitle('Regressão Linear', fontsize=16)
        else:
            fig.suptitle('Regressão Polinomial Grau {}'.format(deg),
                         fontsize=16)

    k = 0
    pred_color = [1, 0.9 - (deg % 5) / 6, (deg % 5) / 6]
    for i in range(rows):
        for j in range(2):
            cv_splits = splits_range[k]
            k += 1
            cv = KFold(n_splits=cv_splits, random_state=seed, shuffle=True)
            best_score = -10000
            poly_reg_scores = []
            for train_index, test_index in cv.split(x):
                X_train, X_test, y_train, y_test = x[train_index], x[
                    test_index], y[train_index], y[test_index]
                poly_reg.fit(X_train, y_train)
                r2_score = poly_reg.score(X_test, y_test)
                poly_reg_scores.append(r2_score)

                if r2_score > best_score or (i, j) == (0, 0):
                    X_train_best, X_test_best, y_train_best, y_test_best = (
                        X_train, X_test, y_train, y_test)
                    best_score = r2_score

            poly_reg.fit(X_train_best, y_train_best)
            y_pred = poly_reg.predict(X_test_best)
            if elasticnet:
                coef = poly_reg.named_steps['elasticnet'].coef_
                coef[0] = poly_reg.named_steps['elasticnet'].intercept_[0]
            else:
                coef = poly_reg.named_steps['linearregression'].coef_[0]
                coef[0] = poly_reg.named_steps['linearregression'].intercept_[
                    0]

            ax = fig.add_subplot(rows, 2, k, projection='3d')
            # m=0
            # n=0
            # for i in range(X_train_best.shape[0]):
            #     if X_train_best[i,0] < 0:
            #         m=i
            #     elif X_train_best[i,0] < 0.4:
            #         n=i

            # ax.scatter(X_train_best[n+1:,0], X_train_best[n+1:,1], y_train_best[n+1:], marker='.', color='magenta',s=100, label="Triax. > 0.4")
            # ax.scatter(X_train_best[m+1:n+1,0], X_train_best[m+1:n+1,1], y_train_best[m+1:n+1], marker='.', color='darkviolet',s=100, label="Triax. > 0")
            # ax.scatter(X_train_best[:m+1,0], X_train_best[:m+1,1], y_train_best[:m+1], marker='.', color='red',s=100, label="Dados de Treino")
            ax.scatter(X_train_best[:, 0],
                       X_train_best[:, 1],
                       y_train_best,
                       marker='.',
                       color='red',
                       s=100,
                       label="Dados do treino")
            ax.scatter(X_test_best[:, 0],
                       X_test_best[:, 1],
                       y_test_best,
                       marker='.',
                       color='green',
                       s=100,
                       label="Dados do teste")
            ax.scatter(X_test_best[:, 0],
                       X_test_best[:, 1],
                       y_pred,
                       marker='.',
                       color='blue',
                       s=100,
                       label="Previsões do modelo")
            ax.set_xlabel("Triaxialidade")
            ax.set_ylabel(trd_val_name)
            ax.set_zlabel("Deformação Plástica Equiv.")
            xs = np.linspace(-0.6, 0.5, 50)
            ys = np.linspace(trd_val_lim[0], trd_val_lim[1], 50)
            X, Y = np.meshgrid(xs, ys)
            inpt = np.column_stack((X.ravel(), Y.ravel()))
            poly_reg.fit(X_train_best, y_train_best)
            Z = poly_reg.predict(inpt)
            Z[Z < -1] = np.nan
            Z[Z < -0.5] = -0.5
            Z = Z.reshape((50, 50))
            plt.locator_params(axis='y', nbins=5)
            ax.plot_surface(X, Y, Z, alpha=0.5)
            ax.view_init(azim=40)
            ax.legend(loc="upper right", bbox_to_anchor=(0.9, 0.95))
            ax.set_title('Divisões: {}  R²: {:.5f}'.format(
                cv_splits, best_score))

    plt.tight_layout(rect=[0, 0, 1, 0.95])
    if savefig:
        if elasticnet:
            if trd_val_name == "Ângulo de Lode":
                plt.savefig('polyreg3d_lode_deg{}_alpha{}_beta{}.png'.format(
                    deg, elasticnet[0], elasticnet[1]),
                            bbox_inches='tight',
                            pad_inches=0.2)
            else:
                plt.savefig('polyreg3d_deg{}_alpha{}_beta{}.png'.format(
                    deg, elasticnet[0], elasticnet[1]),
                            bbox_inches='tight',
                            pad_inches=0.2)
        else:
            if trd_val_name == "Ângulo de Lode":
                plt.savefig('polyreg3d_lode_deg{}.png'.format(deg),
                            bbox_inches='tight',
                            pad_inches=0.2)
            else:
                plt.savefig('polyreg3d_deg{}.png'.format(deg),
                            bbox_inches='tight',
                            pad_inches=0.2)
    plt.show()
    return None
Пример #38
0
def trajectory_gif(model, inputs, targets, timesteps, dpi=200, alpha=0.9,
                   alpha_line=1, filename='trajectory.gif'):
    
    from matplotlib import rc
    from scipy.interpolate import interp1d
    rc("text", usetex = True)
    font = {'size'   : 18}
    rc('font', **font)

    if not filename.endswith(".gif"):
        raise RuntimeError("Name must end in with .gif, but ends with {}".format(filename))
    base_filename = filename[:-4]

    ## We focus on 3 colors at most
    if False in (t < 2 for t in targets): 
        color = ['mediumpurple' if targets[i] == 2.0 else 'gold' if targets[i] == 0.0 else 'mediumseagreen' for i in range(len(targets))]
    else:
        #color = ['crimson' if targets[i, 0] > 0.0 else 'dodgerblue' for i in range(len(targets))]
        color = ['crimson' if targets[i] > 0.0 else 'dodgerblue' for i in range(len(targets))]

    trajectories = model.flow.trajectory(inputs, timesteps).detach()
    num_dims = trajectories.shape[2]

    x_min, x_max = trajectories[:, :, 0].min(), trajectories[:, :, 0].max()
    y_min, y_max = trajectories[:, :, 1].min(), trajectories[:, :, 1].max()
    if num_dims == 3:
        z_min, z_max = trajectories[:, :, 2].min(), trajectories[:, :, 2].max()
    margin = 0.1
    x_range = x_max - x_min
    y_range = y_max - y_min
    x_min -= margin * x_range
    x_max += margin * x_range
    y_min -= margin * y_range
    y_max += margin * y_range
    if num_dims == 3:
        z_range = z_max - z_min
        z_min -= margin * z_range
        z_max += margin * z_range
        
    T = model.T 
    integration_time = torch.linspace(0.0, T, timesteps)
    
    interp_x = []
    interp_y = []
    interp_z = []
    for i in range(inputs.shape[0]):
        interp_x.append(interp1d(integration_time, trajectories[:, i, 0], kind='cubic', fill_value='extrapolate'))
        interp_y.append(interp1d(integration_time, trajectories[:, i, 1], kind='cubic', fill_value='extrapolate'))
        if num_dims == 3:
            interp_z.append(interp1d(integration_time, trajectories[:, i, 2], kind='cubic', fill_value='extrapolate'))
    
    #interp_time = 150
    interp_time = 5
    _time = torch.linspace(0., T, interp_time)

    plt.rc('grid', linestyle="dotted", color='lightgray')
    for t in range(interp_time):
        if num_dims == 2:
            fig = plt.figure()
            ax = fig.add_subplot(1, 1, 1)
            label_size = 13
            plt.rcParams['xtick.labelsize'] = label_size
            plt.rcParams['ytick.labelsize'] = label_size 
            ax.set_axisbelow(True)
            ax.xaxis.grid(color='lightgray', linestyle='dotted')
            ax.yaxis.grid(color='lightgray', linestyle='dotted')
            ax.set_facecolor('whitesmoke')
            
            plt.xlim(x_min, x_max)
            plt.ylim(y_min, y_max)
            plt.rc('text', usetex=True)
            plt.rc('font', family='serif')
            plt.xlabel(r'$x_1$', fontsize=12)
            plt.ylabel(r'$x_2$', fontsize=12)
            plt.scatter([x(_time)[t] for x in interp_x], 
                         [y(_time)[t] for y in interp_y], 
                         c=color, alpha=alpha, marker = 'o', linewidth=0.65, edgecolors='black', zorder=3)

            if t > 0:
                for i in range(inputs.shape[0]):
                    x_traj = interp_x[i](_time)[:t+1]
                    y_traj = interp_y[i](_time)[:t+1]
                    plt.plot(x_traj, y_traj, c=color[i], alpha=alpha_line, linewidth = 0.75, zorder=1)
            
        elif num_dims == 3:
            fig = plt.figure()
            ax = Axes3D(fig)
            label_size = 12
            plt.rcParams['xtick.labelsize'] = label_size
            plt.rcParams['ytick.labelsize'] = label_size 
            
            ax.scatter([x(_time)[t] for x in interp_x], 
                        [y(_time)[t] for y in interp_y],
                        [z(_time)[t] for z in interp_z],
                        c=color, alpha=alpha, marker = 'o', linewidth=0.65, edgecolors='black')
            plt.rc('text', usetex=True)
            plt.rc('font', family='serif')           
            if t > 0:
                for i in range(inputs.shape[0]):
                    x_traj = interp_x[i](_time)[:t+1]
                    y_traj = interp_y[i](_time)[:t+1]
                    z_traj = interp_z[i](_time)[:t+1]
                    ax.plot(x_traj, y_traj, z_traj, c=color[i], alpha=alpha_line, linewidth = 0.75)

            ax.set_xlim3d(x_min, x_max)
            ax.set_ylim3d(y_min, y_max)
            ax.set_zlim3d(z_min, z_max)
            
            plt.rc('grid', linestyle="dotted", color='lightgray')
            ax.grid(True)
            plt.locator_params(nbins=4)

        plt.savefig(base_filename + "{}.png".format(t),
                    format='png', dpi=dpi, bbox_inches='tight')
        # Save only 3 frames (.pdf for paper)
        if t in [0, interp_time//5, interp_time//2, interp_time-1]:
            plt.savefig(base_filename + "{}.pdf".format(t), format='pdf', bbox_inches='tight')
        plt.clf()
        plt.close()

    imgs = []
    for i in range(interp_time):
        img_file = base_filename + "{}.png".format(i)
        imgs.append(imageio.imread(img_file))
        os.remove(img_file) 
    imageio.mimwrite(filename, imgs)
Пример #39
0
    plt.xlim(0, 100)
    plt.xticks(np.arange(0, 101, 20), np.arange(0, 11, 2))
    plt.ylim(0, 0.7)
    plt.yticks(np.arange(0, 0.7, 0.2))
    plt.text(35, 0.5, 'Station ' + cell)

    if ii % 2 == 0:
        plt.ylabel('h (m)')
    if ii >= 4:
        plt.xlabel('time (s)')

    plt.legend([mpaso[0], measured, roms], ['MPAS-O', 'Measured', 'ROMS'],
               fontsize='xx-small',
               frameon=False)

#station location map
im = Image.open('dam_break.png')
im2 = im.resize((650, 300))
plt.subplot(3, 2, 1)
plt.imshow(im2, interpolation='bicubic')
#plt.axis('off')
plt.xlabel('x (m)')
plt.ylabel('y (m)')
plt.locator_params(axis='x', nbins=3)
plt.xticks([0, 325, 650], [4, 2, 0])
plt.locator_params(axis='y', nbins=5)
plt.yticks([0, 75, 150, 220, 300], reversed([2, 1.5, 1, 0.5, 0]))
#plt.show()
plt.savefig('dam_break_comparison.pdf', dpi=600)
Пример #40
0
def ramachandran(file_name_list):
    """
    Main calculation and plotting definition
    :param file_name_list: List of PDB files to plot
    :return: Nothing
    """
    # General variable for the background preferences
    rama_preferences = {
        "General": {
            "file": "data/pref_general.data",
            "cmap": colors.ListedColormap(['#FFFFFF', '#B3E8FF', '#7FD9FF']),
            "bounds": [0, 0.0005, 0.02, 1],
        },
        "GLY": {
            "file": "data/pref_glycine.data",
            "cmap": colors.ListedColormap(['#FFFFFF', '#FFE8C5', '#FFCC7F']),
            "bounds": [0, 0.002, 0.02, 1],
        },
        "PRO": {
            "file": "data/pref_proline.data",
            "cmap": colors.ListedColormap(['#FFFFFF', '#D0FFC5', '#7FFF8C']),
            "bounds": [0, 0.002, 0.02, 1],
        },
        "PRE-PRO": {
            "file": "data/pref_preproline.data",
            "cmap": colors.ListedColormap(['#FFFFFF', '#B3E8FF', '#7FD9FF']),
            "bounds": [0, 0.002, 0.02, 1],
        }
    }

    # Read in the expected torsion angles
    __location__ = os.path.realpath(os.getcwd())
    rama_pref_values = {}
    for key, val in rama_preferences.items():
        rama_pref_values[key] = np.full((360, 360), 0, dtype=np.float64)
        with open(os.path.join(__location__, val["file"])) as fn:
            for line in fn:
                if not line.startswith("#"):
                    # Preference file has values for every second position only
                    rama_pref_values[key][int(float(line.split()[1])) +
                                          180][int(float(line.split()[0])) +
                                               180] = float(line.split()[2])
                    rama_pref_values[key][int(float(line.split()[1])) +
                                          179][int(float(line.split()[0])) +
                                               179] = float(line.split()[2])
                    rama_pref_values[key][int(float(line.split()[1])) +
                                          179][int(float(line.split()[0])) +
                                               180] = float(line.split()[2])
                    rama_pref_values[key][int(float(line.split()[1])) +
                                          180][int(float(line.split()[0])) +
                                               179] = float(line.split()[2])

    normals = {}
    outliers = {}
    for key, val in rama_preferences.items():
        normals[key] = {"x": [], "y": []}
        outliers[key] = {"x": [], "y": []}

    # Calculate the torsion angle of the inputs
    for inp in file_name_list:
        if not os.path.isfile(inp):
            print("{} not found!".format(inp))
            continue
        structure = PDB.PDBParser().get_structure('input_structure', inp)
        for model in structure:
            for chain in model:
                polypeptides = PDB.PPBuilder().build_peptides(chain)
                for poly_index, poly in enumerate(polypeptides):
                    phi_psi = poly.get_phi_psi_list()
                    for res_index, residue in enumerate(poly):
                        res_name = "{}".format(residue.resname)
                        res_num = residue.id[1]
                        phi, psi = phi_psi[res_index]
                        if phi and psi:
                            if str(poly[res_index + 1].resname) == "PRO":
                                aa_type = "PRE-PRO"
                            elif res_name == "PRO":
                                aa_type = "PRO"
                            elif res_name == "GLY":
                                aa_type = "GLY"
                            else:
                                aa_type = "General"
                            if rama_pref_values[aa_type][
                                    int(math.degrees(psi)) +
                                    180][int(math.degrees(phi)) +
                                         180] < rama_preferences[aa_type][
                                             "bounds"][1]:
                                print("{} {} {} {}{} is an outlier".format(
                                    inp, model, chain, res_name, res_num))
                                outliers[aa_type]["x"].append(
                                    math.degrees(phi))
                                outliers[aa_type]["y"].append(
                                    math.degrees(psi))
                            else:
                                normals[aa_type]["x"].append(math.degrees(phi))
                                normals[aa_type]["y"].append(math.degrees(psi))

    # Generate the plots
    for idx, (key, val) in enumerate(
            sorted(rama_preferences.items(), key=lambda x: x[0].lower())):
        plt.subplot(2, 2, idx + 1)
        plt.title(key)
        plt.imshow(rama_pref_values[key],
                   cmap=rama_preferences[key]["cmap"],
                   norm=colors.BoundaryNorm(rama_preferences[key]["bounds"],
                                            rama_preferences[key]["cmap"].N),
                   extent=(-180, 180, 180, -180))
        plt.scatter(normals[key]["x"], normals[key]["y"])
        plt.scatter(outliers[key]["x"], outliers[key]["y"], color="red")
        plt.xlim([-180, 180])
        plt.ylim([-180, 180])
        plt.plot([-180, 180], [0, 0], color="black")
        plt.plot([0, 0], [-180, 180], color="black")
        plt.locator_params(axis='x', nbins=7)
        plt.xlabel(r'$\phi$')
        plt.ylabel(r'$\psi$')
        plt.grid()

    plt.tight_layout()
    plt.savefig("{0}.png".format(
        file_name_list[0][:int(len(file_name_list) - 4)]),
                dpi=300)
    plt.show()
Пример #41
0
    def plot_detuning_energy_levels(
            self,
            plot_state_names: bool,
            fig_kwargs: dict = None,
            plot_title: bool = True,
            ylim: Tuple[float, float] = None,
            highlight_states_by_label: List[str] = None):
        if self.Omega_zero_energies is None:
            self.get_energies()

        if highlight_states_by_label is None:
            highlight_states_by_label = ''.join(['e' for _ in range(self.N)])

        if fig_kwargs is None:
            fig_kwargs = {}
        fig_kwargs = {
            **dict(figsize=(15, 7), num="Energy Levels"),
            **fig_kwargs
        }

        plt.figure(**fig_kwargs)

        plot_points = len(self.Delta)
        for i in reversed(range(len(self.states))):
            label = states_quimb.get_label_from_state(self.states[i])
            is_highlight_state = label in highlight_states_by_label
            is_ground_state = 'e' not in label
            color = 'g' if is_ground_state else 'r' if is_highlight_state else 'grey'
            linewidth = 5 if is_ground_state or is_highlight_state else 1
            z_order = 2 if is_ground_state or is_highlight_state else 1
            # color = f'C{i}'
            plt.plot(self.Delta,
                     self.Omega_zero_energies[:, i],
                     color=color,
                     label=label,
                     alpha=0.6,
                     lw=linewidth,
                     zorder=z_order)
            if self.Omega != 0:
                plt.plot(self.Delta,
                         self.Omega_non_zero_energies[:, i],
                         color=f'C{i}',
                         ls=':',
                         alpha=0.6)

            if plot_state_names:
                Delta_index = int(plot_points / len(self.states)) * i + int(
                    plot_points / 2 / len(self.states))
                text_x = self.Delta[Delta_index]
                text_y = self.Omega_zero_energies[Delta_index, i]
                plt.text(text_x,
                         text_y,
                         label,
                         ha='center',
                         color=f'C{i}',
                         fontsize=16,
                         fontweight='bold')

        if plot_state_names:
            plt.legend()

        plt.grid()
        ax = plt.gca()
        scaled_xaxis_ticker = ticker.EngFormatter(unit="Hz")
        scaled_yaxis_ticker = ticker.EngFormatter(unit="Hz")
        ax.xaxis.set_major_formatter(scaled_xaxis_ticker)
        ax.yaxis.set_major_formatter(scaled_yaxis_ticker)
        plt.locator_params(nbins=4)

        # plt.title(rf"Energy spectrum with $N = {self.N}$, $V = {self.V:0.2e}$, $\Omega = {self.Omega:0.2e}$")
        _m, _s = f"{self.V:0.2e}".split('e')
        if plot_title:
            V_text = rf"{_m:s} \times 10^{{{int(_s):d}}}"
            plt.title(
                rf"Energy spectrum with $N = {self.N}$, $V = {V_text:s}$ Hz")
        plt.xlabel(r"Detuning $\Delta$")
        plt.ylabel("Eigenenergy")
        plt.xlim((self.Delta.min(), self.Delta.max()))
        if ylim:
            plt.ylim(ylim)
        plt.tight_layout()
Пример #42
0
def scatterEnhacnment(pairsDf, paper=False, figNames=None):
    MAX_TIME = 3750

    plotDfArrivedFrac = pd.DataFrame({'Strain': [], 'ATR+': [], 'ATR-': []})
    plotDfProjection = pd.DataFrame({'Strain': [], 'ATR+': [], 'ATR-': []})
    plotDfSpeed = pd.DataFrame({'Strain': [], 'ATR+': [], 'ATR-': []})

    linePlotDf = pd.DataFrame({
        'Strain': [],
        'time': [],
        'FrationArrived': [],
        'Exp': []
    })

    print(pairsDf.shape)
    for i in range(pairsDf.shape[0]):
        try:
            current_row = pairsDf.iloc[i]
            files = np.array(current_row['files'])

            noAtrInd = np.array([file.find('NO_ATR') >= 0 for file in files])

            if np.sum(noAtrInd) != 1:
                print('*** Error: Invalid pair ***')
            else:
                atrFile = files[np.logical_not(noAtrInd)][0]
                noAtrFile = files[noAtrInd][0]

                print(atrFile)
                print(noAtrFile)

                atrRoi = Artifacts(expLocation=atrFile).getArtifact('roi')
                noAtrRoi = Artifacts(expLocation=noAtrFile).getArtifact('roi')

                if atrRoi['wormCount'] < 30 or noAtrRoi['wormCount'] < 30:
                    print('Not enough worms. Continuing.')
                    continue

                plotDfArrivedFrac = plotDfArrivedFrac.append(
                    {
                        'Strain': current_row['Strain'],
                        'ATR+': np.minimum(atrRoi['arrivedFrac'][MAX_TIME], 1),
                        'ATR-': np.minimum(noAtrRoi['arrivedFrac'][MAX_TIME],
                                           1),
                        'ExpId': i
                    },
                    ignore_index=True)

                atrProj = Artifacts(
                    expLocation=atrFile).getArtifact('proj')['proj']
                noAtrProj = Artifacts(
                    expLocation=noAtrFile).getArtifact('proj')['proj']

                plotDfProjection = plotDfProjection.append(
                    {
                        'Strain': current_row['Strain'],
                        'ATR+': np.mean(atrProj),
                        'ATR-': np.mean(noAtrProj),
                        'ExpId': i
                    },
                    ignore_index=True)

                atrSpeed = Artifacts(
                    expLocation=atrFile).getArtifact('speed')['speed']
                noAtrSpeed = Artifacts(
                    expLocation=noAtrFile).getArtifact('speed')['speed']

                plotDfSpeed = plotDfSpeed.append(
                    {
                        'Strain': current_row['Strain'],
                        'ATR+': np.mean(atrSpeed),
                        'ATR-': np.mean(noAtrSpeed),
                        'ExpId': i
                    },
                    ignore_index=True)

                print(
                    "Arrival vals: ATR+:%f, ATR-:%f, Projection mean: ATR+:%f, ATR-:%f, Speed mean ATR+:%f, ATR-:%f"
                    % (atrRoi['arrivedFrac'][MAX_TIME],
                       noAtrRoi['arrivedFrac'][MAX_TIME], np.mean(atrProj),
                       np.mean(noAtrProj), np.mean(atrSpeed),
                       np.mean(noAtrSpeed)))

                print(atrRoi['arrivedFrac'][0:MAX_TIME].shape)
                print(np.array(list(range(MAX_TIME))).shape)

                currentLinePlot = pd.DataFrame({
                    'Strain':
                    current_row['Strain'],
                    'time':
                    np.array(list(range(MAX_TIME))) * 0.5,
                    'FractionArrived':
                    atrRoi['arrivedFrac'][0:MAX_TIME],
                    'Exp':
                    'ATR'
                })

                linePlotDf = pd.concat((linePlotDf, currentLinePlot))

                currentLinePlot = pd.DataFrame({
                    'Strain':
                    current_row['Strain'],
                    'time':
                    np.array(list(range(MAX_TIME))) * 0.5,
                    'FractionArrived':
                    noAtrRoi['arrivedFrac'][0:MAX_TIME],
                    'Exp':
                    'NO ATR'
                })

                linePlotDf = pd.concat((linePlotDf, currentLinePlot))
        except Exception as e:
            raise e

    #print(plotDf)
    #DEBUG
    #plotDfArrivedFrac.to_pickle('/home/itskov/Dropbox/tempBundle_arrival.pkl')
    #plotDfProjection.to_pickle('/home/itskov/Dropbox/tempBundle_proj.pkl')
    #plotDfSpeed.to_pickle('/home/itskov/Dropbox/tempBundle_speed.pkl')
    #linePlotDf.to_pickle('/home/itskov/Dropbox/tempBundle2.pkl')
    #DEBUG

    if paper == False:
        plt.style.use("dark_background")
        sns.set_context('talk')
        cp = sns.dark_palette("purple", 7)
    else:
        cp = sns.cubehelix_palette(8, start=.5, rot=-.75)

    if paper == False:
        ax = sns.scatterplot(x='ATR-',
                             y='ATR+',
                             data=plotDfArrivedFrac,
                             linewidth=0,
                             alpha=0.85,
                             color=cp[6])
        ax.plot([0, 1.1], [0, 1.1], ":")
    else:
        ax = sns.scatterplot(x='ATR-',
                             y='ATR+',
                             data=plotDfArrivedFrac,
                             linewidth=0,
                             alpha=0.85,
                             color=cp[6])
        ax.plot([0, 1.1], [0, 1.1], ":", color='k')

    plt.xlim(0, 1.1)
    plt.ylim(0, 1.1)
    plt.gca().grid(alpha=0.2)
    plt.title('Arrival Fracion, n = %d' % (plotDfArrivedFrac.shape[0], ),
              loc='left')
    if figNames is None:
        plt.show()
    else:
        plt.gcf().savefig(figNames[0], format='svg')

    # Plotting the projection plot.
    #plt.style.use("dark_background")
    #sns.set_context('talk')
    #cp = sns.dark_palette("purple", 7)

    ax = sns.scatterplot(x='ATR-',
                         y='ATR+',
                         data=plotDfProjection,
                         linewidth=0,
                         alpha=0.85,
                         color=cp[6])

    xmin = np.min(plotDfProjection['ATR-'])
    xmax = np.max(plotDfProjection['ATR-'])
    ymin = np.min(plotDfProjection['ATR-'])
    ymax = np.max(plotDfProjection['ATR+'])

    if paper == False:
        ax.plot([0, 1], [0, 1], ":")
    else:
        ax.plot([0, 1], [0, 1], ":", color='k')

    plt.xlim(0, 0.35)
    plt.ylim(0, 0.35)
    plt.gca().grid(alpha=0.2)
    plt.locator_params(axis='y', nbins=6)
    plt.locator_params(axis='x', nbins=6)
    plt.title('Mean Projection, n = %d' % (plotDfArrivedFrac.shape[0], ),
              loc='left')
    if figNames is None:
        plt.show()
    else:
        plt.gcf().savefig(figNames[1], format='svg')

    # Plotting the speed plot.
    #plt.style.use("dark_background")
    #sns.set_context('talk')
    #cp = sns.dark_palette("purple", 7)
    ax = sns.scatterplot(x='ATR-',
                         y='ATR+',
                         data=plotDfSpeed,
                         linewidth=0,
                         alpha=0.85,
                         color=cp[6])

    xmin = np.min(plotDfSpeed['ATR-'])
    xmax = np.max(plotDfSpeed['ATR-'])
    ymin = np.min(plotDfSpeed['ATR+'])
    ymax = np.max(plotDfSpeed['ATR+'])

    if paper == False:
        ax.plot([0, 0.0018], [0, 0.0018], ":")
    else:
        ax.plot([0, 0.0018], [0, 0.0018], ":", color='k')

    plt.xlim(0.0008, 0.0018)
    plt.ylim(0.0008, 0.0018)
    plt.gca().grid(alpha=0.2)
    plt.locator_params(axis='y', nbins=6)
    plt.locator_params(axis='x', nbins=6)
    plt.title('Mean Speed, n = %d' % (plotDfSpeed.shape[0], ), loc='left')
    if figNames is None:
        plt.show()
    else:
        plt.gcf().savefig(figNames[2], format='svg')

    #ax = sns.lineplot(x='time', y='FractionArrived', hue='Exp', data=linePlotDf, ci=68, estimator=np.median)
    #plt.gca().grid(alpha=0.2)
    #ax.set(xlabel='Time [s]')

    #plt.show()
    return (plotDfArrivedFrac, plotDfProjection, plotDfSpeed)
Пример #43
0
def _one_plot(
            data: pd.DataFrame,
            avg_time: pd.DataFrame,
            data_size: int,
            cur_ax: matplotlib.axis,
            data_name: str = "SST5",
            linestyle: str = "-",
            linewidth: int = 3,
            logx: bool = False,
            plot_errorbar: bool = False,
            errorbar_kind: str = 'shade',
            errorbar_alpha: float = 0.1,
            x_axis_time: bool = False,
            legend_location: str = 'lower right',
            relabel_logx_scalar: List[int] = None,
            rename_labels: Dict[str, str] = None,
            reported_accuracy: List[float] = None,
            encoder_name: str = None,
            show_xticks: bool = False,
            fontsize: int = 16,
            xlim: List[int] = None,
            model_order: List[str] = None,
            performance_metric: str = "accuracy",
            x_axis_rot: int = 0,
            line_colors: List[str] = ["#8c564b", '#1f77b4', '#ff7f0e', '#17becf'],
            errorbar_colors: List[str] = ['#B22222', "#089FFF", "#228B22"]):

    cur_ax.set_title(data_name, fontsize=fontsize)
    if model_order:
        models = model_order
    else:
        models = data.index.levels[0].tolist()
        models.sort()
    max_first_point = 0
    cur_ax.set_ylabel("Expected validation " + performance_metric, fontsize=fontsize)
    
    if x_axis_time:
        cur_ax.set_xlabel("Training duration",fontsize=fontsize)
    else:
        cur_ax.set_xlabel("Hyperparameter assignments",fontsize=fontsize)
    
    if logx:
        cur_ax.set_xscale('log')
    
    for ix, model in enumerate(models):
        means = data[model]['mean']
        vars = data[model]['var']
        max_acc = data[model]['max']
        
        if x_axis_time:
            x_axis = [avg_time[model] * (i+1) for i in range(len(means))]
        else:
            x_axis = [i+1 for i in range(len(means))]

        if rename_labels:
            model_name = rename_labels.get(model, model)
        else:
            model_name = model
        if reported_accuracy:
            cur_ax.plot([0, 6.912e+6],
                        [reported_accuracy[model],
                        reported_accuracy[model]],
                        linestyle='--',
                        linewidth=linewidth,
                        color=line_colors[ix])
            plt.text(6.912e+6-3600000,
                    reported_accuracy[model] + 0.01,
                    f'reported {model_name} {performance_metric}',
                    ha='right',
                    style='italic',
                    fontsize=fontsize-5,
                    color=line_colors[ix])

        if encoder_name:
            model_name = encoder_name + " " + model_name

        if plot_errorbar:
            if errorbar_kind == 'shade':
                minus_vars = np.array(means)-np.array(vars)
                plus_vars = [x + y if (x + y) <= max_acc else max_acc for x,y in zip(means, vars)]
                plt.fill_between(x_axis,
                                    minus_vars,
                                    plus_vars,
                                    alpha=errorbar_alpha,
                                    facecolor=errorbar_colors[ix])
            else:
                line = cur_ax.errorbar(x_axis,
                                means,
                                yerr=vars,
                                label=model_name,
                                linestyle=linestyle,
                                linewidth=linewidth,
                                color=line_colors[ix])
        line = cur_ax.plot(x_axis,
                            means,
                            label=model_name,
                            linestyle=linestyle,
                            linewidth=linewidth,
                            color=line_colors[ix])
    
    left, right = cur_ax.get_xlim()
    if xlim:
        cur_ax.set_xlim(xlim)
        # cur_ax.xaxis.set_ticks(np.arange(xlim[0], xlim[1]+5, 10))

    for tick in cur_ax.xaxis.get_major_ticks():
        tick.label.set_fontsize(fontsize) 
    for tick in cur_ax.yaxis.get_major_ticks():
        tick.label.set_fontsize(fontsize) 
    plt.locator_params(axis='y', nbins=10)
    if relabel_logx_scalar:
        for axis in [cur_ax.xaxis]:
            axis.set_ticks(relabel_logx_scalar)
            axis.set_major_formatter(ScalarFormatter())
    plt.xticks(rotation=x_axis_rot)
    
    if show_xticks:
        cur_ax.tick_params(which="both", bottom=True)
    if x_axis_time:
        def timeTicks(x, pos):                                                                                                                                                                                                                                                         
            d = datetime.timedelta(seconds=float(x))
            d = td_format(d)
            return str(d)                                                                                                                                                                                                                                                          
        formatter = matplotlib.ticker.FuncFormatter(timeTicks)                                                                                                                                                                                                                         
        cur_ax.xaxis.set_major_formatter(formatter)
    cur_ax.legend(loc=legend_location, fontsize=fontsize)
    
    plt.tight_layout()
Пример #44
0
else:

    # Retrieve

    Z = fac.Retrieve('Z.p', path_here)

#### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### ####
### Plot

fac.SetPlotParams()

fg = plt.figure()
ax0 = plt.axes(frameon=True)

for i in range(Nsample):
    plt.plot(t, (Z[:, i]), color='0.6')

ax0.spines['top'].set_visible(False)
ax0.spines['right'].set_visible(False)
ax0.yaxis.set_ticks_position('left')
ax0.xaxis.set_ticks_position('bottom')
plt.locator_params(nbins=4)

plt.xlabel('time (norm.)')
plt.ylabel('Activation $x_i$')
plt.savefig('trial.pdf')
plt.show()

#
Пример #45
0
    # Ajuste polinomio temperatura COzir
    F_temp4 = np.polyfit(data1.index.values , data1[nombre_columnas[6]], 90)
    BestFit_temp4 = np.polyval(F_temp4, data1.index.values)
    #plt.plot(data1["Tiempo"], BestFit_temp3,"g-")
    plt.plot(data1["Tiempo"], data1[nombre_columnas[6]],"orange")
    orange_patch = mpatches.Patch(color='orange', label='Temperatura COZIR')  

    plt.ylabel('Temperatura [°C]',rotation=0,labelpad=30)
    plt.xlabel('Hora')
    plt.axhline(22, color='k',linestyle='--')
    plt.axhline(15, color='k', linestyle='--')
    black_patch = mpatches.Patch(color='k', label='Rango recomendado')
    plt.legend(fontsize=10,handles=[red_patch,blue_patch,green_patch,orange_patch,black_patch],loc='best')
    plt.xlim(0,86400)
    plt.ylim(0,35)
    plt.locator_params(axis='both', nbins=5)
    plt.title('Temperatura',loc='center')
    plt.savefig('temp.png',bbox_inches = 'tight')
    #plt.show()
    plt.clf()


    # Ajuste polinomio humedad interior
    F_hum1 = np.polyfit(data1.index.values , data1[nombre_columnas[5]], 90)
    BestFit_hum1 = np.polyval(F_hum1, data1.index.values)
    #plt.plot(data1["Tiempo"], BestFit_hum1,"r-")
    plt.plot(data1["Tiempo"], data1[nombre_columnas[5]],"r-")
    red_patch = mpatches.Patch(color='red', label='Humedad Interior')

    # Ajuste polinomio humedad COzir
    F_hum2 = np.polyfit(data1.index.values , data1[nombre_columnas[7]], 90)
Пример #46
0
def plot_activity(data, out_dir='out', filename='motor_control'):
    timeseries = timeseries_from_data(data)

    CheY_vec = timeseries['internal']['CheY']
    CheY_P_vec = timeseries['internal']['CheY_P']
    cw_bias_vec = timeseries['internal']['cw_bias']
    motile_state_vec = timeseries['internal']['motile_state']
    thrust_vec = timeseries['boundary']['thrust']
    flagella_activity = timeseries.get('flagella', {})
    time_vec = timeseries['time']

    # make flagella activity grid
    flagella_ids = list(flagella_activity.keys())
    flagella_indexes = {}
    next_index = 0
    activity_grid = np.zeros((len(flagella_ids), len(time_vec)))
    total_CW = np.zeros((len(time_vec)))
    for time_index, (time, time_data) in enumerate(data.items()):
        time_flagella = time_data.get('flagella', {})

        for flagella_id, rotation_states in time_flagella.items():

            # get flagella_index by order of appearance
            if flagella_id not in flagella_indexes:
                flagella_indexes[flagella_id] = next_index
                next_index += 1
            flagella_index = flagella_indexes[flagella_id]

            modified_rotation_state = 0
            CW_rotation_state = 0
            if rotation_states == -1:
                modified_rotation_state = 1
            elif rotation_states == 1:
                modified_rotation_state = 2
                CW_rotation_state = 1

            activity_grid[flagella_index, time_index] = modified_rotation_state
            total_CW += np.array(CW_rotation_state)

    # grid for cell state
    motile_state_grid = np.zeros((1, len(time_vec)))
    motile_state_grid[0, :] = motile_state_vec

    # set up colormaps
    # cell motile state
    cmap1 = colors.ListedColormap(['steelblue', 'lightgray', 'darkorange'])
    bounds1 = [-1, -1/3, 1/3, 1]
    norm1 = colors.BoundaryNorm(bounds1, cmap1.N)
    motile_legend_elements = [
        Patch(facecolor='steelblue', edgecolor='k', label='Run'),
        Patch(facecolor='darkorange', edgecolor='k', label='Tumble'),
        Patch(facecolor='lightgray', edgecolor='k', label='N/A')]

    # rotational state
    cmap2 = colors.ListedColormap(['lightgray', 'steelblue', 'darkorange'])
    bounds2 = [0, 0.5, 1.5, 2]
    norm2 = colors.BoundaryNorm(bounds2, cmap2.N)
    rotational_legend_elements = [
        Patch(facecolor='steelblue', edgecolor='k', label='CCW'),
        Patch(facecolor='darkorange', edgecolor='k', label='CW'),
        Patch(facecolor='lightgray', edgecolor='k', label='N/A')]

    # plot results
    cols = 1
    rows = 5
    plt.figure(figsize=(4 * cols, 1.5 * rows))

    # define subplots
    ax1 = plt.subplot(rows, cols, 1)
    ax2 = plt.subplot(rows, cols, 2)
    ax3 = plt.subplot(rows, cols, 3)
    ax4 = plt.subplot(rows, cols, 4)
    ax5 = plt.subplot(rows, cols, 5)

    # plot Che-P state
    ax1.plot(time_vec, CheY_vec, label='CheY')
    ax1.plot(time_vec, CheY_P_vec, label='CheY_P')
    ax1.legend(loc='center left', bbox_to_anchor=(1, 0.5))
    ax1.set_xticks([])
    ax1.set_xlim(time_vec[0], time_vec[-1])
    ax1.set_ylabel('concentration \n (uM)')

    # plot CW bias
    ax2.plot(time_vec, cw_bias_vec)
    ax2.set_xticks([])
    ax2.set_xlim(time_vec[0], time_vec[-1])
    ax2.set_ylabel('CW bias')

    # plot flagella states in a grid
    if len(activity_grid) > 0:
        ax3.imshow(activity_grid,
                   interpolation='nearest',
                   aspect='auto',
                   cmap=cmap2,
                   norm=norm2,
                   # extent=[-1,1,-1,1]
                   extent=[time_vec[0], time_vec[-1], len(flagella_ids)+0.5, 0.5]
                   )
        plt.locator_params(axis='y', nbins=len(flagella_ids))
        ax3.set_yticks(list(range(1, len(flagella_ids) + 1)))
        ax3.set_xticks([])
        ax3.set_ylabel('flagella #')

        # legend
        ax3.legend(
            handles=rotational_legend_elements,
            loc='center left',
            bbox_to_anchor=(1, 0.5))
    else:
        # no flagella
        ax3.set_axis_off()

    # plot cell motile state
    ax4.imshow(motile_state_grid,
               interpolation='nearest',
               aspect='auto',
               cmap=cmap1,
               norm=norm1,
               extent=[time_vec[0], time_vec[-1], 0, 1])
    ax4.set_yticks([])
    ax4.set_xticks([])
    ax4.set_ylabel('cell motile state')

    # legend
    ax4.legend(
        handles=motile_legend_elements,
        loc='center left',
        bbox_to_anchor=(1, 0.5))

    # plot motor thrust
    ax5.plot(time_vec, thrust_vec)
    ax5.set_xlim(time_vec[0], time_vec[-1])
    ax5.set_ylabel('total motor thrust (pN)')
    ax5.set_xlabel('time (sec)')


    # save figure
    fig_path = os.path.join(out_dir, filename)
    plt.subplots_adjust(wspace=0.7, hspace=0.3)
    plt.savefig(fig_path + '.png', bbox_inches='tight')
Пример #47
0
            cellFreqs[cellType], cellSIs[cellType])
        xvals = np.linspace(10, 16, 200)
        yvals = slope * xvals + intercept
        plt.plot(xvals, yvals, '-', color=cellTypeColours[cellType], zorder=-1)

        print "SI vs Pref freq"
        print "Linear regression over {0} cells: \ncorrelation coefficient (r): {1}\np Value: {2}".format(
            cellTypeLabels[cellType], rVal, pVal)

        #         labels = ['%.1f' % f for f in possibleFreqs/1000]
        #         labels[1::4] = ['']*len(labels[1::4])
        #         labels[2::4] = ['']*len(labels[2::4])
        #         labels[3::4] = ['']*len(labels[2::4])
        #         thisAx.set_xticks(np.log2(possibleFreqs))
        #         thisAx.set_xticklabels(labels)
        plt.locator_params(axis='x', nbins=6)
        labels = ['%.1f' % freq for freq in (2**plt.xticks()[0]) / 1000]
        thisAx.set_xticks(plt.xticks()[0])
        thisAx.set_xticklabels(labels)

        plt.xlim(11.5, 15.5)
        plt.ylim(-0.1, 1.1)
        plt.title("{} cells".format(cellTypeLabels[cellType]),
                  color=cellTypeColours[cellType],
                  fontsize=fontSizeLabels)
        if cellType == 0:
            plt.ylabel('Suppression Index', fontsize=fontSizeLabels)
        else:
            thisAx.set_yticklabels([''])
        if cellType == 1:
            plt.xlabel('Preferred frequency (kHz)', fontsize=fontSizeLabels)
               top='off',
               right='off',
               direction='out',
               length=8,
               width=3,
               labelsize=26)
ax.tick_params(axis='y',
               pad=10,
               which='both',
               top='off',
               right='off',
               direction='out',
               length=8,
               width=3,
               labelsize=26)
plt.locator_params(axis='x', nbins=2)
plt.locator_params(axis='y', nbins=4)
ax.axhline(-0.1, linewidth=3, color="black")
ax.axvline(0.5, linewidth=3, color="black")
ax.set_ylim(-0.1, 0.81)
ax.set_xlim(0.5, 2.5)
plt.locator_params(axis='y', nbins=6)
plt.xticks(n_groups + bar_width, ('', '', '', ''))
plt.locator_params(axis='x', nbins=3)
plt.subplots_adjust(hspace=1,
                    wspace=.7,
                    bottom=0.25,
                    left=0.1,
                    right=0.9,
                    top=.9)
fig.savefig('Plots/Figure6/ObjectRec_0100' + ' .png', dpi=200)
Пример #49
0
    'ytick.labelsize': 8,
    'axes.titlesize': 12,
    'axes.labelsize': 10
}
plt.rcParams.update(paras)
# fig = plt.figure(figsize=(8, 6))
with open('data.csv', newline='') as csvfile:
    rows = csv.reader(csvfile, delimiter=',')
    for row in rows:
        handoff = np.array(row)
        for j in range(0, len(handoff)):
            handoff[i] = int(handoff[i])
        # 設置 y 軸
        plt.figure(figsize=(8, 6))
        plt.axis('auto')
        plt.locator_params(tight=True)
        plt.plot(x, handoff, colors[int(i / 3)])
        ax = plt.gca()
        if i < 3:
            ax.set_title('BestPolicy Handoff per Hour')
        elif i < 6:
            ax.set_title('Threshold Handoff per Hour')
        elif i < 9:
            ax.set_title('Entropy Handoff per Hour')
        elif i < 12:
            ax.set_title('MyPolicy Handoff per Hour')

        ax.set_xlabel('Hour')
        ax.set_ylabel('Handoff')
        ax.set_xticks(np.linspace(0, 86400, 24))
        ax.set_xticklabels([str(i + 1) for i in range(24)])
def kf_do(id):
    print(sys.executable)
    # Setting fixed threshold criteria
    USE_THRESH = False
    # fixed threshold value
    THRESH = 0.6
    # Setting fixed threshold criteria
    USE_TOP_ORDER = False
    # Setting local maxima criteria
    USE_LOCAL_MAXIMA = True
    # Number of top sorted frames
    NUM_TOP_FRAMES = 50

    # Video path of the source file
    videopath = DATAPATH + "videos/" + str(id) + "_video.mp4"
    # Directory to store the processed frames
    dir = DATAPATH + "extract_result/"
    # smoothing window size
    len_window = int(50)

    print("target video :" + videopath)
    print("frame save directory: " + dir)
    # load video and compute diff between frames
    cap = cv2.VideoCapture(str(videopath))
    curr_frame = None
    prev_frame = None
    frame_diffs = []
    frames = []
    success, frame = cap.read()
    i = 0
    while (success):
        luv = cv2.cvtColor(frame, cv2.COLOR_BGR2LUV)
        curr_frame = luv
        if curr_frame is not None and prev_frame is not None:
            # logic here
            diff = cv2.absdiff(curr_frame, prev_frame)
            diff_sum = np.sum(diff)
            diff_sum_mean = diff_sum / (diff.shape[0] * diff.shape[1])
            frame_diffs.append(diff_sum_mean)
            frame = kf.Frame(i, diff_sum_mean)
            frames.append(frame)
        prev_frame = curr_frame
        i = i + 1
        success, frame = cap.read()
    cap.release()

    # compute keyframe
    keyframe_id_set = set()
    if USE_TOP_ORDER:
        # sort the list in descending order
        frames.sort(key=operator.attrgetter("diff"), reverse=True)
        for keyframe in frames[:NUM_TOP_FRAMES]:
            keyframe_id_set.add(keyframe.id)
    if USE_THRESH:
        print("Using Threshold")
        for i in range(1, len(frames)):
            if (kf.rel_change(np.float(frames[i - 1].diff),
                              np.float(frames[i].diff)) >= THRESH):
                keyframe_id_set.add(frames[i].id)
    if USE_LOCAL_MAXIMA:
        print("Using Local Maxima")
        diff_array = np.array(frame_diffs)
        sm_diff_array = kf.smooth(diff_array, len_window)
        frame_indexes = np.asarray(kf.argrelextrema(sm_diff_array,
                                                    np.greater))[0]
        for i in frame_indexes:
            keyframe_id_set.add(frames[i - 1].id)

        plt.figure(figsize=(40, 20))
        plt.locator_params(numticks=100)
        plt.stem(sm_diff_array)
        plt.savefig(dir + 'plot.png')

    # "keyframe_id_set"保存关键帧的帧数,如果>16,随机选取16帧;如果<16,再随机生成几个直到满足条件
    if len(keyframe_id_set) > 16:
        tmp = random.sample(keyframe_id_set, 16)
        keyframe_id_set = set(tmp)
    elif len(keyframe_id_set) < 16:
        tmp = list(keyframe_id_set)
        tmp.sort()
        maxframe = tmp[len(tmp) - 1]
        while len(keyframe_id_set) < 16:
            newframe = random.randint(0, maxframe)
            keyframe_id_set.add(newframe)

    # save all keyframes as image
    cap = cv2.VideoCapture(str(videopath))
    curr_frame = None
    keyframes = []
    success, frame = cap.read()
    idx = 0
    num = 0
    while (success):
        if idx in keyframe_id_set:
            # name = str(id) + "_keyframe_" + str(idx) + ".jpg"
            name = str(id) + "_keyframe_" + str(num) + ".jpg"
            rframe = cv2.resize(frame, (112, 112))  # 压缩为112*112
            cv2.imwrite(dir + name, rframe)
            keyframe_id_set.remove(idx)
            num += 1
        idx = idx + 1
        success, frame = cap.read()
    cap.release()
    print(id, "over!")
    [x11, x12, x1N], [y11, y12, y1N], [z11, z12, z1N], [x21, x22, x2N],
    [y21, y22, y2N], [z21, z22, z2N], [xM1, xM2, xMN], [yM1, yM2, yMN],
    [zM1, zM2, zMN
     ])  # Crear grafico con varios conjuntos de valores en x y z especificados
plt.plot(
    [x11, x12, x1N], [y11, y12, y1N], [z11, z12, z1N], formato1,
    [x21, x22, x2N], [y21, y22, y2N], [z21, z22, z2N], formato2,
    [xM1, xM2, xMN], [yM1, yM2, yMN], [zM1, zM2, zMN], formatoM
)  # Crear grafico con varios conjuntos de valores en x y z especificados con formato

# Personalización del grafico
plt.gcf().gca().set_xlim(x_o, x_f)  # Establecer longitud del eje x
plt.gcf().gca().set_ylim(y_o, y_f)  # Establecer longitud del eje y
plt.gcf().gca().set_zlim(z_o, z_f)  # Establecer longitud del eje z
plt.locator_params(
    axis='x', nbins=num_x
)  # Establecer el numero de marcadores aproximados igualmente espaciados en el eje x (La apximación se realiza por debajo del numero especificado)
plt.locator_params(
    axis='y', nbins=num_y
)  # Establecer el numero de marcadores aproximados igualmente espaciados en el eje y (La apximación se realiza por debajo del numero especificado)
plt.locator_params(
    axis='z', nbins=num_z
)  # Establecer el numero de marcadores aproximados igualmente espaciados en el eje z (La apximación se realiza por debajo del numero especificado)
plt.gcf().gca().set_xticks(
    lista)  # Establecer los marcadores especificados en el eje x
plt.gcf().gca().set_yticks(
    lista)  # Establecer los marcadores especificados en el eje y
plt.gcf().gca().set_zticks(
    lista)  # Establecer los marcadores especificados en el eje z
plt.gcf().gca().set_xlabel('texto')  # Texto para el eje en x
plt.gcf().gca().set_ylabel('texto')  # Texto para el eje en y
Пример #52
0
def plot_traces(sol, ax, color="b"):
    MDL = sol.MDL
    #    model = get_model_type(sol)
    filename = sol.filename.replace("\\", "/").split("/")[-1].split(".")[0]
    keys = sorted([x.__name__ for x in MDL.deterministics]) + sorted(
        [x.__name__ for x in MDL.stochastics])
    sampler = MDL.get_state()["sampler"]
    try:
        keys.remove("zmod")
        keys.remove("m_")
    except:
        pass
    keys.remove("R0")
    for (i, k) in enumerate(keys):
        vect = old_div((MDL.trace(k)[:].size), (len(MDL.trace(k)[:])))
        if vect > 1:
            keys[i] = [k + "%d" % n for n in range(0, int(vect))]
    labels = [
        r"$log_{10}\bar{\tau}$", "$\Sigma m$", "$R_0$", "$a_0$", "$a_1$",
        "$a_2$", "$a_3$", "$a_4$"
    ]
    #    labels = [r"$R_0$", "$c_1$", "$c_2$", "$c_3$", r"$log_{10}\/\tau_1$", r"$log_{10}\/\tau_2$", r"$log_{10}\/\tau_3$", "$m_1$", "$m_2$", "$m_3$"]
    #    labels = [r"$c_1$", "$c_2$", r"$log_{10}\/\tau_1$", r"$log_{10}\/\tau_2$", "$m_1$", "$m_2$"]
    keys = list(flatten(keys))
    ncols = 2
    nrows = int(ceil(len(keys) * 1.0 / ncols))
    for c, (a, k) in enumerate(zip(ax.flat, keys)):
        if k == "R0":
            stoc = "R0"
        else:
            stoc = ''.join([i for i in k if not i.isdigit()])
            stoc_num = [int(i) for i in k if i.isdigit()]
        try:
            maxi = np.argmax(MDL.trace("log_tau")[-1])
            mini = np.argmin(MDL.trace("log_tau")[-1])
            if stoc_num[0] == 0:
                data = MDL.trace(stoc)[:][:, maxi]
                #prendre plus petit
            elif stoc_num[0] == 1:
                #prendre plus gros
                data = MDL.trace(stoc)[:][:, mini]


#            else:
#                #prendre plus moyen
#                remaining = [x for x in range(0,vect) if x != mini and x != maxi][0]
#                data = MDL.trace(stoc)[:][:,remaining]
#        except:
#            pass
#        try:
#            data = MDL.trace(stoc)[:][:,stoc_num[0]-1]
        except:
            data = MDL.trace(stoc)[:]

        print((np.mean(data[-100000:]), np.std(data[-100000:])))

        x = np.arange(sampler["_burn"] + 1, sampler["_iter"] + 1,
                      sampler["_thin"])
        plt.axes(a)
        plt.ticklabel_format(style='sci', axis='both', scilimits=(0, 0))
        plt.locator_params(axis='x', nbins=3)
        ty = a.yaxis.get_offset_text()
        ty.set_size(12)
        tx = a.xaxis.get_offset_text()
        tx.set_size(12)
        plt.locator_params(axis='y', nbins=6)
        plt.yticks(fontsize=14)
        plt.xticks(fontsize=14)
        plt.ylabel(labels[c], fontsize=14)
        plt.plot(x, data, '-', color=color, label=filename, linewidth=1.0)
        a.grid(False)
Пример #53
0
def make_scatter_plot(all_metrics, plot_param, out_file=None):
    # Rearrange the data
    data = {'x': [], 'y': [], 'size': [], 'color': []}
    for experiment, metrics in all_metrics.items():
        for key in data:
            data[key] += metrics[plot_param[key]['data']]

    # convert to numpy
    for key in data:
        data[key] = np.array(data[key])

    # remove nans
    nans = np.logical_or.reduce((np.isnan(data['x']), np.isnan(data['y']),
                                 np.isinf(data['x']), np.isinf(data['y'])))
    print('\n ** Removing %d NaNs and infs before log **' % np.sum(nans))
    for key in data:
        data[key] = data[key][np.invert(nans)]

    # take log if needed
    data_x = np.log(
        data['x']) / np.log(10) if plot_param['x']['log'] else np.copy(
            data['x'])
    data_y = np.log(
        data['y']) / np.log(10) if plot_param['y']['log'] else np.copy(
            data['y'])

    # Remove nans after log again - these typically come from LSTM TODO may need to make this more principled
    nans = np.logical_or.reduce((np.isnan(data_x), np.isnan(data_y),
                                 np.isinf(data_x), np.isinf(data_y)))

    print('\n ** Removing %d NaNs and infs after log **' % np.sum(nans))
    for key in data:
        # print(key, data[key])
        data[key] = data[key][np.invert(nans)]
    data_x = data_x[np.invert(nans)]
    data_y = data_y[np.invert(nans)]

    ###
    ### Plotting
    ###
    fig, ax = plt.subplots(figsize=(8, 8))

    # Font size
    SMALL_SIZE = 8
    MEDIUM_SIZE = 10
    BIGGER_SIZE = 12

    plt.set_cmap('jet')
    plt.rc('font', size=16)  # controls default text sizes
    plt.rc('axes', labelsize=20)  # fontsize of the x and y labels

    # compute limits so that x and y scaled w.r.t. their std
    x_lim, y_lim = compute_lims(data_x, data_y)
    ax.set_xlim(x_lim)
    ax.set_ylim(y_lim)

    # set num ticks
    plt.locator_params(axis='x', nbins=4)
    plt.locator_params(axis='y', nbins=8)

    # axes labels and log scaling
    x_label = plot_param['x']['data']
    y_label = plot_param['y']['data']
    if plot_param['x']['log']:
        x_label += ' (log)'
        ax.set_xticklabels(
            ['%.1e' % np.power(10, float(t)) for t in ax.get_xticks()])
    if plot_param['y']['log']:
        y_label += ' (log)'
        ax.set_yticklabels(
            ['%.1e' % np.power(10, float(t)) for t in ax.get_yticks()])
    ax.set_xlabel(x_label)
    ax.set_ylabel(y_label)

    # the actual plotting
    # print('data[\'color\']', data['color'])
    ax.scatter(data_x, data_y, s=data['size'], c=data['color'], alpha=0.5)

    # Correlation
    corr = np.corrcoef(data_x, data_y)[0, 1]
    print('Correlation %f' % corr)
    if 'title' in plot_param:
        title = plot_param['title']
    else:
        title = plot_param['y']['data'] + ' vs ' + plot_param['x']['data']
    plt.title(title + '\ncorrelation %.2f' % corr)

    # Save to out_file
    if plot_param['print']:
        fig.savefig(out_file, bbox_inches='tight')
Пример #54
0
def plot_all_chains_KDE(sol):
    #    MDL = sol["pymc_model"]

    MDLs = [m["pymc_model"] for m in sol]

    #    model = get_model_type(sol)
    keys = sorted([x.__name__ for x in MDLs[0].deterministics]) + sorted(
        [x.__name__ for x in MDLs[0].stochastics])
    sampler = MDLs[0].get_state()["sampler"]

    last_iter = sampler["_iter"] - sampler["_burn"]

    try:
        keys.remove("zmod")
        keys.remove("m_")
#        keys.remove("log_mean_tau")
#        keys.remove("total_m")
    except:
        pass
    for (i, k) in enumerate(keys):
        vect = old_div((MDLs[0].trace(k)[:].size), (len(MDLs[0].trace(k)[:])))
        if vect > 1:
            keys[i] = [k + "%d" % n for n in range(0, vect)]
    keys = list(flatten(keys))
    ncols = len(keys)
    nrows = len(keys)
    #    labels = [r"$R_0$", "$c_1$", "$c_2$", "$c_3$", r"$log_{10}\/\tau_1$", r"$log_{10}\/\tau_2$", r"$log_{10}\/\tau_3$", "$m_1$", "$m_2$", "$m_3$"]
    labels = [
        r"$R_0$", "$c_1$", "$c_2$", r"$log_{10}\/\tau_1$",
        r"$log_{10}\/\tau_2$", "$m_1$", "$m_2$"
    ]

    fig = plt.figure(figsize=(9, 9))
    #    plt.ticklabel_format(style='sci', axis='both', scilimits=(0,0))
    plotz = len(keys)
    for i in range(plotz):
        for j in range(plotz):
            if j < i:
                var1 = keys[j]
                var2 = keys[i]
                label1 = labels[j]
                label2 = labels[i]

                print((label1, label2))
                ax = plt.subplot2grid((plotz - 1, plotz - 1), (i - 1, j))
                ax.ticklabel_format(axis='y', style='sci', scilimits=(-2, 0))
                ax.ticklabel_format(axis='x', style='sci', scilimits=(-2, 0))
                if var1 == "R0":
                    stoc1 = "R0"
                else:
                    stoc1 = ''.join([k for k in var1 if not k.isdigit()])
                    stoc_num1 = [int(k) for k in var1 if k.isdigit()]

                x = np.array([])
                try:
                    for mod in MDLs:
                        maxi = np.argmax(mod.trace("log_tau")[-1])
                        mini = np.argmin(mod.trace("log_tau")[-1])
                        if stoc_num1[0] == 0:
                            data1 = mod.trace(stoc1)[:][:, maxi]
                            #prendre plus petit
                        elif stoc_num1[0] == 1:
                            #prendre plus gros
                            data1 = mod.trace(stoc1)[:][:, mini]
#                        else:
#                            #prendre plus moyen
#                            remaining = [re for re in range(0,vect) if re != mini and re != maxi][0]
#                            data1 = mod.trace(stoc1)[:][:,remaining]
                        x = np.hstack((x, data1))
                except:
                    for mod in MDLs:
                        x = np.hstack((x, mod.trace(stoc1)[:]))

                if var2 == "R0":
                    stoc2 = "R0"
                else:
                    stoc2 = ''.join([k for k in var2 if not k.isdigit()])
                    stoc_num2 = [int(k) for k in var2 if k.isdigit()]

                y = np.array([])
                try:
                    for mod in MDLs:
                        maxi = np.argmax(mod.trace("log_tau")[-1])
                        mini = np.argmin(mod.trace("log_tau")[-1])
                        if stoc_num2[0] == 0:
                            data2 = mod.trace(stoc2)[:][:, maxi]
                            #prendre plus petit
                        elif stoc_num2[0] == 1:
                            #prendre plus gros
                            data2 = mod.trace(stoc2)[:][:, mini]


#                        else:
#                            #prendre plus moyen
#                            remaining = [re for re in range(0,vect) if re != mini and re != maxi][0]
#                            data2 = mod.trace(stoc2)[:][:,remaining]
                        y = np.hstack((y, data2))
                except:
                    for mod in MDLs:
                        y = np.hstack((y, mod.trace(stoc2)[:]))

                xmin, xmax = min(x), max(x)
                ymin, ymax = min(y), max(y)
                # Peform the kernel density estimate
                xx, yy = np.mgrid[xmin:xmax:50j, ymin:ymax:50j]
                positions = np.vstack([xx.ravel(), yy.ravel()])
                values = np.vstack([x, y])
                kernel = gaussian_kde(values)
                kernel.set_bandwidth(bw_method='silverman')
                kernel.set_bandwidth(bw_method=kernel.factor * 1.0)
                f = np.reshape(kernel(positions).T, xx.shape)
                ax.set_xlim(xmin, xmax)
                ax.set_ylim(ymin, ymax)
                plt.axes(ax)
                # Contourf plot
                plt.grid(None)
                ax.scatter(x, y, color='k', s=1)
                #                plt.title("           r = %.2f"%(np.corrcoef(x,y)[0,1]), fontsize=14)
                plt.title("r = %.2f" % (np.corrcoef(x, y)[0, 1]), fontsize=14)
                plt.xticks(rotation=90)
                plt.locator_params(axis='y', nbins=6)
                plt.locator_params(axis='x', nbins=6)
                cfset = ax.contourf(xx, yy, f, cmap=plt.cm.Blues, alpha=0.7)
                ## Or kernel density estimate plot instead of the contourf plot
                # Contour plot
                #                cset = ax.contour(xx, yy, f, levels=cfset.levels[2::2], colors='k', alpha=0.8)
                # Label plot
                #    ax.clabel(cset, cset.levels[::1], inline=1, fmt='%.1E', fontsize=10)
                plt.yticks(fontsize=14)
                plt.xticks(fontsize=14)
                if j == 0:
                    plt.ylabel("%s" % label2, fontsize=14)
                if i == len(keys) - 1:
                    plt.xlabel("%s" % label1, fontsize=14)
                if j != 0:
                    ax.yaxis.set_ticklabels([])
                if i != len(keys) - 1:
                    ax.xaxis.set_ticklabels([])

                ty = ax.yaxis.get_offset_text()
                ty.set_size(12)
                tx = ax.xaxis.get_offset_text()
                tx.set_size(12)

                plt.suptitle("Double Cole-Cole inversion\n" + title +
                             " step method",
                             fontsize=14)

    fig.tight_layout(pad=0, w_pad=1, h_pad=0)
    fig.savefig('KDE_ColeCole-3_Matrix_Adaptive_False.png', dpi=300)

    return fig
Пример #55
0
def Bio_plotfeatures(X, d, Xn=[], hold=True):
    """Bio_plotfeatures(X, d, Xn)

     Toolbox: Balu

     Plot features X according classification d. If the feature names are
     given in Xn then they will labeled in each axis.

     For only one feature, histograms are ploted.
     For two( or three) features, plots in 2D( or 3D) are given.
     For m > 3 features, m x m 2D plots are given(feature i vs.feature j)

     Example 1: 1D & 2D
        from balu.InputOutput import Bio_plotfeatures
        from balu.ImagesAndData import balu_load
        from matplotlib.pyplot import figure

        data = balu_load('datagauss')          # simulated data(2 classes, 2 features)
        X = data['X']
        d = data['d']
        figure(1)
        Bio_plotfeatures(X[:, 0], d, 'x1')     # histogram of feature 1
        figure(2)
        Bio_plotfeatures(X, d)                 # plot feature space in 2D(2 features)

     Example 2: 3D
        from balu.InputOutput import Bio_plotfeatures
        from balu.ImagesAndData import balu_load

        data = balu_load('datareal')          # real data
        f = data['f']
        d = data['d']
        X = f[:, [220, 174, 234]]             # only three features are choosen
        Bio_plotfeatures(X, d)                # plot feature space in 3D(3 features)

     Example 3: 5D(using feature selection)
         from balu.InputOutput import Bio_plotfeatures
         from balu.ImagesAndData import balu_load

         data = balu_load('datareal ')             # real data
         f = data['f']
         d = data['d']
         op['m'] = 5;                              # 5 features will be selected
         op['s'] = 0.75;                           # only 75 % of sample will be used
         op['show'] = False                        # display results
         op['b']['name'] = 'fisher';               # definition SFS with Fisher
         s = Bfs_balu(f, d, op);                   # feature selection
         Bio_plotfeatures(f[:, s], d)              # plot feature space for 5 features

     See also Bev_roc.

     (c)
     GRIMA - DCCUC, 2011
     http: // grima.ing.puc.cl

     With collaboration from:
     Diego Patiño ([email protected]) -> Translated implementation into python (2016)
    """

    clf()

    if len(X.shape) > 1:
        m = X.shape[1]
    else:
        m = 1

    if m > 9:
        print('Bio_plotfeatures for {0} features makes {1} plots.'.format(
            m, m * m))
        exit()

    scflag = False
    if len(d.shape) > 1:
        if d.shape[1] == 2:
            sc = d[:, 1]
            d = d[:, 0]
            scflag = True
        else:
            d = np.squeeze(d)

    dmin = int(np.min(d))
    dmax = int(np.max(d))

    col = 'gbrcmykbgrcmykbgrcmykbgrcmykgbrcmykbgrcmykbgrcmykbgrcmykgbrcmykbgrcmykbgrcmykbgrcmyk'
    mar = 'ox+v^ox+v^ox+v^ox+v^ox+v^ox+v^ox+v^ox+v^ox+v^ox+v^ox+v^ox+v^ox+v^ox+v^ox+v^ox+v^ox+v^ox+v^'

    # clf
    warnings.filterwarnings('ignore')
    s = []
    for k in range(dmin, dmax + 1):
        s.append('class {0}'.format(k - 1))

    if m < 4:
        if m == 1:
            for k in range(dmin, dmax + 1):
                idx = np.where(d == k)
                h, x = np.histogram(X[idx], 100)
                dx = x[1] - x[0]
                A = np.sum(h) * dx
                x = np.hstack((x - dx / 2.0, x[-1] + dx))
                h = np.hstack((0, h, 0))
                plot(x, h / A, color=col[k])

            if len(Xn) != 0:
                xlabel(Xn)
            else:
                xlabel('feature value')

            ylabel('PDF')
        elif m == 2:
            if len(Xn) == 0:
                Xn = ['feature value 1', 'feature value 2']

            for k in range(dmin, dmax + 1):
                ii = np.where(d == k)
                scatter(X[ii, 0], X[ii, 1], c=col[k], marker=mar[k])
                if scflag:
                    for ic in range(1, ii[0].size):
                        text(X[ii[0][ic], 0], X[ii[0][ic], 1],
                             '  ' + str(sc[ii[0][ic]]))

            title('feature space')
            xlabel(Xn[0])
            ylabel(Xn[1])
        elif m == 3:

            fig = gcf()
            ax = fig.add_subplot(111, projection='3d')

            for k in range(dmin, dmax + 1):
                ii = np.where(d == k)
                ax.scatter(X[ii, 0],
                           X[ii, 1],
                           X[ii, 2],
                           c=col[k],
                           marker=mar[k])

                if scflag:
                    for ic in range(1, ii[0].size):
                        ax.text(X[ii[0][ic], 0], X[ii[0][ic], 1],
                                X[ii[0][ic], 2], '  ' + str(sc[ii[0][ic]]))

            if len(Xn) > 0:
                ax.set_xlabel(Xn[0])
                ax.set_ylabel(Xn[1])
                ax.set_zlabel(Xn[2])
            else:
                ax.set_xlabel('feature value 1')
                ax.set_ylabel('feature value 2')
                ax.set_zlabel('feature value 3')
        legend(s)
    else:

        l = 1
        for j in range(1, m + 1):
            for i in range(1, m + 1):
                zi = X[:, i - 1]
                zj = X[:, j - 1]
                subplot(m, m, l)
                l += 1

                for k in range(dmin, dmax + 1):
                    ii = np.where(d == k)
                    scatter(zi[ii], zj[ii], c=col[k], marker=mar[k])
                    locator_params(nbins=3)

                    if scflag:
                        for ic in range(1, ii[0].size):
                            text(zi[ii[0][ic]], zj[ii[0][ic]],
                                 '  ' + str(sc[ii[0][ic]]))

                if len(Xn) > 0:
                    xl = Xn[i - 1]
                    yl = Xn[j - 1]
                else:
                    xl = 'z_{0}'.format(i - 1)
                    yl = 'z_{0}'.format(j - 1)

                if i == 1:
                    ylabel(yl)

                if j == m:
                    xlabel(xl)

    if hold:
        show()
Пример #56
0
def plot_histo(sol, no_subplots=False, save=False, save_as_png=True):
    MDL = sol["pymc_model"]
    filename = sol["path"].replace("\\", "/").split("/")[-1].split(".")[0]
    keys = sorted([x.__name__ for x in MDL.deterministics]) + sorted(
        [x.__name__ for x in MDL.stochastics])
    try:
        keys.remove("zmod")
        keys.remove("m_")
    except:
        pass
#    keys.remove("R0")
    for (i, k) in enumerate(keys):
        vect = old_div((MDL.trace(k)[:].size), (len(MDL.trace(k)[:])))
        if vect > 1:
            keys[i] = [k + "%d" % n for n in range(0, vect)]
    keys = list(flatten(keys))
    #    labels = [r"$c_1$", "$c_2$", r"$log_{10}\/\tau_1$", r"$log_{10}\/\tau_2$", "$m_1$", "$m_2$"]
    labels = [
        r"$log_{10}\bar{\tau}$", "$\Sigma m$", "$R_0$", "$a_0$", "$a_1$",
        "$a_2$", "$a_3$", "$a_4$"
    ]
    ncols = 2
    nrows = int(ceil(len(keys) * 1.0 / ncols))
    fig, ax = plt.subplots(nrows, ncols, figsize=(10, nrows * 2.2))
    for c, (a, k) in enumerate(zip(ax.flat, keys)):
        if k == "R0":
            stoc = "R0"
        else:
            stoc = ''.join([i for i in k if not i.isdigit()])
            stoc_num = [int(i) for i in k if i.isdigit()]
        try:
            maxi = np.argmax(MDL.trace("log_tau")[-1])
            mini = np.argmin(MDL.trace("log_tau")[-1])
            if stoc_num[0] == 0:
                data = MDL.trace(stoc)[:][:, maxi]
                #prendre plus petit
            elif stoc_num[0] == 1:
                #prendre plus gros
                data = MDL.trace(stoc)[:][:, mini]


#            else:
#                #prendre plus moyen
#                remaining = [x for x in range(0,vect) if x != mini and x != maxi][0]
#                data = MDL.trace(stoc)[:][:,remaining]
        except:
            pass
        try:
            data = MDL.trace(stoc)[:][:, stoc_num[0] - 1]
        except:
            data = MDL.trace(stoc)[:]
        fit = norm.pdf(sorted(data), np.mean(data), np.std(data))
        print((np.mean(data), np.std(data)))
        plt.axes(a)
        #            plt.gca().get_xaxis().get_major_formatter().set_useOffset(False)
        #            plt.gca().get_yaxis().get_major_formatter().set_useOffset(False)
        plt.locator_params(axis='y', nbins=7)
        plt.locator_params(axis='x', nbins=7)
        plt.yticks(fontsize=14)
        plt.xticks(fontsize=14)
        plt.xlabel(labels[c], fontsize=14)
        plt.ylabel("Frequency", fontsize=14)
        ty = a.yaxis.get_offset_text()
        ty.set_size(12)
        tx = a.xaxis.get_offset_text()
        tx.set_size(12)
        hist = plt.hist(data,
                        bins=20,
                        normed=False,
                        label=filename,
                        linewidth=1.0,
                        color="white")
        xh = [
            0.5 * (hist[1][r] + hist[1][r + 1])
            for r in range(len(hist[1]) - 1)
        ]
        binwidth = old_div((max(xh) - min(xh)), len(hist[1]))
        fit *= len(data) * binwidth
        plt.plot(sorted(data), fit, "-b", linewidth=1.5)
        plt.grid(None)
        plt.ticklabel_format(style='sci', axis='both', scilimits=(0, 0))
    fig.tight_layout(w_pad=0, h_pad=0)
    return fig
Пример #57
0
def main(argv):
    slice_size = 60
    opt_func = "cost"

    if len(argv) != 3:
        print("Not enough arguments: using default values, 15 cost off")
    else:
        slice_size = int(argv[0])
        opt_func = argv[1]
        print(opt_func)

    sload_flag = 1
    suchp_flag = 1
    sgas_chp_flag = 1

    if argv[2] == 'off':
        sload_flag = 0
        suchp_flag = 0
        sgas_chp_flag = 0

    debug = False

    # Data preprocessing
    load_profiles_df = pd.read_excel(
        "Summer_Load_Profiles.xlsx", header=None) + pd.read_excel(
            "Winter_Load_Profiles.xlsx", header=None)
    pv_profiles_df = pd.read_excel("Summer_PV_Profiles.xlsx", header=None)
    uchp_profiles_df = pd.read_excel("Winter_uCHP_Profiles.xlsx", header=None)
    prices_df = pd.read_csv("pricesGME.csv", usecols=[1])

    # Adding noise
    load_profiles_df = load_profiles_df + np.random.normal(
        0, 0.1, [load_profiles_df.shape[0], load_profiles_df.shape[1]])
    load_profiles_df = load_profiles_df.clip(lower=0)

    pv_profiles_df = pv_profiles_df + np.random.normal(
        0, 0.1, [pv_profiles_df.shape[0], pv_profiles_df.shape[1]])
    pv_profiles_df = pv_profiles_df.clip(
        lower=0)  # per fare in modo che non ci siano valori negativi

    prices_df = prices_df + np.random.normal(
        0, 0.1, [prices_df.shape[0], prices_df.shape[1]])

    scaled_load_df = change_scale(5, slice_size, load_profiles_df, debug)
    scaled_pv_df = change_scale(5, slice_size, pv_profiles_df, debug)
    scaled_prices_df = change_scale(60, slice_size, prices_df, debug)
    scaled_prices_df.columns = ['prices']

    pod_list_conf = parse_config('config.conf')
    init_pods(pod_list_conf, scaled_load_df, scaled_pv_df)

    # definizione delle costanti
    uchp_min = uchp_profiles_df.values.min()  # kW
    uchp_max = uchp_profiles_df.values.max()  # kW
    cuchp = 0.9
    cchp_gas = 0.039
    gas_chp_min = 0
    gas_chp_max = 2
    eta = 0.9
    gin_min = 0
    gout_min = 0
    gin_max = 4
    gout_max = 4
    sin_max = 4
    sout_max = 4
    charge_init = 2  ## inizializzato il valore della batteria
    charge_max = 6

    T = int(scaled_load_df[0].count())  # in questo caso 96
    tildeload_df = pd.DataFrame()
    sload_df = pd.DataFrame()
    suchp_df = pd.DataFrame()
    sgas_chp_df = pd.DataFrame()

    fixed_index_list = []
    fixed_time_list = range(T)
    previous_charge = [None] * len(pod_list_conf)
    previous_uchp = [None] * len(pod_list_conf)
    previous_gaschp = [None] * len(pod_list_conf)

    # liste per salvare l'output ad ogni ciclo di t
    tildeloadlist = []
    pvlist = []
    tildeuchplist = []
    tildegaschplist = []
    gridINlist = []
    gridOUTlist = []
    stINlist = []
    stOUTlist = []
    tot_time = []

    # pod
    for pod in pod_list_conf:
        fixed_index_list.append(pod[0])

    # pv
    pv_index_list = []
    for pod in pod_list_conf:
        for el in pod[2]:
            if el == 'pv':
                pv_index_list.append(pod[0])

    # uchp
    uchp_index_list = []
    for pod in pod_list_conf:
        for el in pod[2]:
            if el == 'uchp':
                uchp_index_list.append(pod[0])

    # se considero la fase offline, vado a cercare i valori shiftati del chp, altrimenti metto il df tutto a zero
    if uchp_index_list:
        if suchp_flag == 1:
            suchp_df = pd.read_csv('suchp.csv')
        else:
            for t in fixed_time_list:
                for i in uchp_index_list:
                    suchp_df.loc[t, str(i)] = 0

    # gas_chp
    gas_chp_index_list = []
    for pod in pod_list_conf:
        for el in pod[2]:
            if el == 'gas_chp':
                gas_chp_index_list.append(pod[0])

    if gas_chp_index_list:
        if sgas_chp_flag == 1:
            sgas_chp_df = pd.read_csv('sgas_chp.csv')
        else:
            for t in fixed_time_list:
                for i in gas_chp_index_list:
                    sgas_chp_df.loc[t, str(i)] = 0

    # load
    load_index_list = []
    for pod in pod_list_conf:
        for el in pod[2]:
            if el == 'load':
                load_index_list.append(pod[0])

    if load_index_list:
        if sload_flag == 1:
            sload_df = pd.read_csv('sload.csv')
        else:
            for t in fixed_time_list:
                for i in load_index_list:
                    sload_df.loc[t, str(i)] = 0

    # inizializzo la scrittura del file di output

    f = open("output_online.txt", "a")

    obj_value_list = []

    ### INIZIO MODELLO - PER OGNI T

    for t in fixed_time_list:

        model = ConcreteModel()

        # variabili sempre presenti
        model.Pgin = Var(fixed_index_list, domain=NonNegativeReals)
        model.Pgout = Var(fixed_index_list, domain=NonNegativeReals)

        # variabili dipendenti dalla costruzione del pod
        # charge
        charge_index_list = []
        for pod in pod_list_conf:
            for el in pod[2]:
                if el == 'storage':
                    charge_index_list.append(
                        pod[0]
                    )  #prendiamo la lista dei pod, e controlliamo quali hanno st, quindi aggiungiamo il loro indice a charge_index_list

        if charge_index_list:  #se la lista non è vuota
            model.charge = Var(charge_index_list, domain=NonNegativeReals)
            model.Psin = Var(charge_index_list, domain=NonNegativeReals)
            model.Psout = Var(charge_index_list, domain=NonNegativeReals)

        # uchp
        if uchp_index_list:
            model.Puchp = Var(uchp_index_list)
            model.tildeuchp = Var()

        # gas_chp
        if gas_chp_index_list:
            model.Pgas_chp = Var(gas_chp_index_list)
            model.tildegas_chp = Var()

        load_index_norm = []
        for el in load_index_list:
            load_index_norm.append(el % 100)

        reduced_scaled_load_df = scaled_load_df[
            load_index_norm]  # andiamo a prendere da load_df solo le colonne dei pod che effettivamente ci servono - e hanno il load
        # In questo modo noi preleviamo le colonne corrispondenti tra loro per la somma
        # a prescindere dall'indice; ciò dovrebbe coprirci anche nei casi in cui abbiamo
        # più pod con più load: le colonne saranno sempre disposte sempre nello stesso ordine
        # tra i due df
        reduced_scaled_load_df.columns = load_index_list
        #print(reduced_scaled_load_df)

        for col1, col2, i in zip(reduced_scaled_load_df.columns,
                                 sload_df.columns, load_index_list):
            # print(reduced_scaled_load_df[col1]) # colonna del primo, a prescindere dall'indice
            # print(sload_df[col2]) # colonna del secondo a prescindere dall'indice
            # print(i) # indice corretto di riferimento per il df finale sommato
            array = reduced_scaled_load_df[col1].values + sload_df[col2].values
            tildeload_df[i] = array

        tildeload = tildeload_df[load_index_list].sum(axis=1).clip(
            lower=0).values.tolist()

        def objective(model):
            result = 0

            for i, pod in enumerate(pod_list_conf):
                if opt_func == "cost":
                    result += model.Pgin[i] * scaled_prices_df[
                        'prices'].values.tolist()[t] - model.Pgout[
                            i] * scaled_prices_df['prices'].values.tolist()[t]
                    if 'uchp' in pod[2]:
                        result += cuchp * model.tildeuchp
                    if 'gas_chp' in pod[2]:
                        result += cchp_gas * model.tildegas_chp
                    if 'storage' in pod[2]:
                        result += model.Psout[i] * scaled_prices_df[
                            'prices'].values.tolist()[t]
                elif opt_func == "grid":
                    result += model.Pgin[i] * scaled_prices_df[
                        'prices'].values.tolist()[t] + model.Pgout[
                            i] * scaled_prices_df['prices'].values.tolist()[t]

            return result

        model.OBJ = Objective(rule=objective)

        #### CONSTRAINTS

        # UCHP:

        if uchp_index_list:
            model.bound_uchp = ConstraintList()
            model.bound_tildeuchp = ConstraintList()
            model.bound_time_uchp = ConstraintList()

        for i in uchp_index_list:
            model.bound_uchp.add(
                inequality(uchp_min, model.tildeuchp, uchp_max))

        if uchp_index_list:
            left_side = model.tildeuchp
            right_side = 0
            for i in uchp_index_list:
                right_side += model.Puchp[i] + suchp_df[str(
                    i)].values.tolist()[t]
            model.bound_tildeuchp.add(left_side == right_side)

        if uchp_index_list:
            multipl_constant = int(60 / slice_size)
            for i in uchp_index_list:
                if t in range(
                        0 * multipl_constant + 1,
                        4 * multipl_constant) and t < 4 * multipl_constant - 1:
                    model.bound_time_uchp.add(
                        previous_uchp[i] == model.tildeuchp)

                if t in range(
                        4 * multipl_constant,
                        8 * multipl_constant) and t < 8 * multipl_constant - 1:
                    model.bound_time_uchp.add(
                        previous_uchp[i] == model.tildeuchp)

                if t in range(
                        8 * multipl_constant, 12 *
                        multipl_constant) and t < 12 * multipl_constant - 1:
                    model.bound_time_uchp.add(
                        previous_uchp[i] == model.tildeuchp)

                if t in range(
                        12 * multipl_constant, 16 *
                        multipl_constant) and t < 16 * multipl_constant - 1:
                    model.bound_time_uchp.add(
                        previous_uchp[i] == model.tildeuchp)

                if t in range(
                        16 * multipl_constant, 20 *
                        multipl_constant) and t < 20 * multipl_constant - 1:
                    model.bound_time_uchp.add(
                        previous_uchp[i] == model.tildeuchp)

                if t in range(
                        20 * multipl_constant, 24 *
                        multipl_constant) and t < 24 * multipl_constant - 1:
                    model.bound_time_uchp.add(
                        previous_uchp[i] == model.tildeuchp)

        #multipl_constant = int(60/slice_size)

        # GAS_CHP:

        if gas_chp_index_list:
            model.bound_gaschp = ConstraintList()
            model.bound_tildegaschp = ConstraintList()
            model.bound_time_gaschp = ConstraintList()

        for i in gas_chp_index_list:
            model.bound_gaschp.add(
                inequality(gas_chp_min, model.tildegas_chp, gas_chp_max))

        if gas_chp_index_list:
            left_side = model.tildegas_chp
            right_side = 0
            for i in gas_chp_index_list:
                right_side += model.Pgas_chp[i] + sgas_chp_df[str(
                    i)].values.tolist()[t]
            model.bound_tildegaschp.add(left_side == right_side)

        if gas_chp_index_list:
            multipl_constant = int(60 / slice_size)
            for i in gas_chp_index_list:
                if t in range(
                        0 * multipl_constant + 1,
                        4 * multipl_constant) and t < 4 * multipl_constant - 1:
                    model.bound_time_gaschp.add(
                        previous_gaschp[i] == model.tildegas_chp)

                if t in range(
                        4 * multipl_constant,
                        8 * multipl_constant) and t < 8 * multipl_constant - 1:
                    model.bound_time_gaschp.add(
                        previous_gaschp[i] == model.tildegas_chp)

                if t in range(
                        8 * multipl_constant, 12 *
                        multipl_constant) and t < 12 * multipl_constant - 1:
                    model.bound_time_gaschp.add(
                        previous_gaschp[i] == model.tildegas_chp)

                if t in range(
                        12 * multipl_constant, 16 *
                        multipl_constant) and t < 16 * multipl_constant - 1:
                    model.bound_time_gaschp.add(
                        previous_gaschp[i] == model.tildegas_chp)

                if t in range(
                        16 * multipl_constant, 20 *
                        multipl_constant) and t < 20 * multipl_constant - 1:
                    model.bound_time_gaschp.add(
                        previous_gaschp[i] == model.tildegas_chp)

                if t in range(
                        20 * multipl_constant, 24 *
                        multipl_constant) and t < 24 * multipl_constant - 1:
                    model.bound_time_gaschp.add(
                        previous_gaschp[i] == model.tildegas_chp)

        #multipl_constant = int(60/slice_size)
        #for i in gas_chp_index_list:
        #model.bound_tildegaschp.add( previous_gaschp[i] == model.Pgas_chp[i])

        # STORAGE:

        if charge_index_list:
            model.bound_charge = ConstraintList()

        for i in charge_index_list:
            model.bound_charge.add(model.Psin[i] <= sin_max)
            model.bound_charge.add(model.Psout[i] <= sout_max)

            #constraint per l'istante iniziale
            if t == 0:
                model.bound_charge.add(model.charge[i] == charge_init)
                model.bound_charge.add(model.charge[i] <= charge_max)
                model.bound_charge.add(model.charge[i] >= 0)

                model.bound_charge.add(
                    model.Psin[i] <= charge_max - charge_init)
                model.bound_charge.add(model.Psout[i] <= charge_init)
            else:
                model.bound_charge.add(model.charge[i] == previous_charge[i] -
                                       eta * model.Psin[i] +
                                       eta * model.Psout[i])

                model.bound_charge.add(model.charge[i] <= charge_max)
                model.bound_charge.add(model.charge[i] >= 0)
                model.bound_charge.add(
                    model.Psin[i] <= charge_max - previous_charge[i])
                model.bound_charge.add(model.Psout[i] <= previous_charge[i])

        # GRID:

        model.bound_gin = ConstraintList()
        model.bound_gout = ConstraintList()
        for i, pod in enumerate(pod_list_conf):
            model.bound_gin.add(inequality(gin_min, model.Pgin[i], gin_max))
            model.bound_gout.add(inequality(gout_min, model.Pgout[i],
                                            gout_max))

        # BILANCIAMENTO:
        # Potrebbe contenere un termine relativo ad un componente load

        model.bound_tildeload = ConstraintList()

        right_side = 0
        left_side = tildeload[t]
        for i, pod in enumerate(pod_list_conf):
            right_side += model.Pgin[i] - model.Pgout[i]
            if 'uchp' in pod[2]:
                right_side += model.Puchp[i] + suchp_df[str(
                    i)].values.tolist()[t]
            if 'gas_chp' in pod[2]:
                right_side += model.Pgas_chp[i] + sgas_chp_df[str(
                    i)].values.tolist()[t]
            if 'storage' in pod[2]:
                right_side += model.Psin[i] - model.Psout[i]
            if 'pv' in pod[2]:
                right_side += pod[3]['pv'].values.tolist()[t]

        model.bound_tildeload.add(left_side == right_side)

        #model.pprint()

        opt = SolverFactory('gurobi')
        results = opt.solve(model, tee=True)

        # in alternativa model.load(results)
        model.solutions.store_to(results)
        #results.write()

        # per salvare il valore dell'iterazione precedente di charge, per usarlo nel constraint
        for i, pod in enumerate(pod_list_conf):
            if 'storage' in pod[2]:
                previous_charge[i] = model.charge[i].value

        for i, pod in enumerate(pod_list_conf):
            if 'uchp' in pod[2]:
                previous_uchp[i] = model.tildeuchp.value
                #print(previous_uchp[i])
            if 'gas_chp' in pod[2]:
                previous_gaschp[i] = model.tildegas_chp.value
                #print(previous_gaschp[i])
        '''# per salvare tutti i valori delle variabili di ogni pod
        # PER OGNI POD i:
        #       var_1[i] ... var_N[i]
        # 0      valore  ...  valore
        # 1      valore  ...  valore
        # ...    valore  ...  valore
        # T      valore  ...  valore
        # 
        # PER COSTRUIRLI:
        # - Iteriamo ad ogni t, quindi ad ogni t noi possiamo costruire già tante strutture quanti sono i pod
        #   e in particolare aggiungere una riga ad ogni t.
        # - Per fare ciò ci serviranno dei df di appoggio (uno per ogni pod), che istanziamo all'inizio del programma
        #   e riempiamo alla fine di ogni iterazione in T.
        # - Alla fine del ciclo principale in T, salveremo (in un ciclo ausiliario) tutti questi df come csv.

        for i, pod in enumerate(pod_list_conf):
            labels = []
            row_as_list = []
            # temp_df = temp_df.append(model.Pgin[i], model.Pgout[i])
            labels.append('Pgin_{}'.format(pod[0]))
            labels.append('Pgout_{}'.format(pod[0]))
            row_as_list.append(model.Pgin[i].value)
            row_as_list.append(model.Pgout[i].value)
            if 'chp' in pod[2]:
                # temp_df = temp_df.append(model.Pchp[i])
                labels.append('Pchp_{}'.format(pod[0]))
                row_as_list.append(model.Pchp[i].value)
            if 'storage' in pod[2]:
                # temp_df = temp_df.append(model.Sout[i], model.Sin[i])
                labels.append('Psin_{}'.format(pod[0]))
                labels.append('Psout_{}'.format(pod[0]))
                row_as_list.append(model.Psin[i].value)
                row_as_list.append(model.Psout[i].value)
            if 'pv' in pod[2]:
                # temp_df = temp_df.append(model.Ppv[i])
                labels.append('Ppv_{}'.format(pod[0]))
                row_as_list.append(pod[3]['pv'].values.tolist()[t])
            if 'load' in pod[2]:
                # temp_df = temp_df.append(model.Sload[i]) 
                labels.append('Pload_{}'.format(pod[0]))
                row_as_list.append(pod[3]['load'].values.tolist()[t])
            
            row_as_series = pd.Series(row_as_list)

            pod[4] = pod[4].append(row_as_series, ignore_index = True)
            
            if t == T-1:
                pod[4].columns = labels
                pod[4].to_csv('pod_{}.csv'.format(pod[0]), index=False)
                print("PRINTING RESULTS FOR POD " + str(i))
                print(pod[4])'''

        # TEMPO DI RISOLUZIONE
        solver_time = get_info_from_results(results, 'Time: ')
        tot_time.append(float(solver_time))

        obj_value_list.append(float(model.OBJ()))

        # CONTROLLO SULLA TERMINAZIONE
        if (results.solver.termination_condition ==
                TerminationCondition.optimal):
            print("Modello risolto correttamente")
        elif (results.solver.termination_condition ==
              TerminationCondition.infeasible):
            print("La condizione di terminazione è INFEASIBLE")
        else:
            print("Errore: Solver Status", results.solver.status)
        '''stdout_backup = sys.stdout

        with open('results_online_step_{}.yml'.format(t), 'a') as f:
            sys.stdout = f
            results.write()

        sys.stdout = stdout_backup'''

        # PLOT OUTPUT

        if pv_index_list:
            acc = 0
            for i in pv_index_list:
                acc += pod_list_conf[i][3]['pv'].values.tolist()[t]
            pvlist.append(acc)

        if load_index_list:
            tildeloadlist.append(tildeload[t])

        if uchp_index_list:
            tildeuchplist.append(model.tildeuchp.value)

        if gas_chp_index_list:
            tildegaschplist.append(model.tildegas_chp.value)

        acc = 0
        for i in range(len(pod_list_conf)):
            acc += model.Pgin[i].value
        gridINlist.append(acc)

        acc = 0
        for i in range(len(pod_list_conf)):
            acc += model.Pgout[i].value
        gridOUTlist.append(-acc)

        if charge_index_list:
            acc = 0
            for i in charge_index_list:
                acc += model.Psin[i].value
            stINlist.append(acc)

        if charge_index_list:
            acc = 0
            for i in charge_index_list:
                acc += model.Psout[i].value
            stOUTlist.append(-acc)
    # Fine ciclo

    tot_time_sum = sum(tot_time)
    #print("SOLVER TIME: " + str(tot_time_sum))
    #print(obj_value_list)

    f.write("Components: {}\n".format(
        len(pv_index_list) + len(load_index_list) + len(gas_chp_index_list) +
        len(uchp_index_list) + len(charge_index_list)))
    f.write("    # Pv: {}\n".format(len(pv_index_list)))
    f.write("    # Load: {}\n".format(len(load_index_list)))
    f.write("    # Gas Chp: {}\n".format(len(gas_chp_index_list)))
    f.write("    # Uchp: {}\n".format(len(uchp_index_list)))
    f.write("    # Storage: {}\n".format(len(charge_index_list)))
    f.write("SOLVER TIME:" + str(tot_time_sum) + "\n")
    f.write("OBJ function {}:".format(argv[1]) + "\n")
    f.write("MIN OBJ: {}, MAX OBJ: {}, MEAN OBJ: {}\n".format(
        min(obj_value_list), max(obj_value_list), np.mean(obj_value_list)))
    f.write("TOTAL OBJ: {}\n".format(sum(obj_value_list)))
    f.write("FULL LIST: \n")
    for el in obj_value_list:
        if el < 0:
            f.write("     {}\n".format(str(el)))
        else:
            f.write("      {}\n".format(str(el)))
    f.write("\n")
    f.close()

    resultimg, result = plt.subplots(figsize=(20, 10))
    images, = result.plot(tildeloadlist, linestyle='-', color='red')
    images, = result.plot(pvlist, linestyle='-', color='green')
    images, = result.plot(tildeuchplist, linestyle='-', color='purple')
    images, = result.plot(tildegaschplist, linestyle='-', color='magenta')
    images, = result.plot([sum(x) for x in zip(gridINlist, gridOUTlist)],
                          linestyle='-',
                          color='#3a55a1',
                          linewidth=2)
    #images, = result.plot(gridOUTlist, linestyle='-', color='blue')
    images, = result.plot([sum(x) for x in zip(stINlist, stOUTlist)],
                          linestyle='-',
                          color='#fa7e25',
                          linewidth=2)
    #images, = result.plot(stOUTlist, linestyle='-', color='orange')
    result.legend(['Load', 'PV', 'UChp', 'GASChp', 'Grid', 'Storage'],
                  fancybox=True,
                  framealpha=0.5)

    tilde = np.interp(np.arange(0.0, 96.0, 0.1), fixed_time_list,
                      tildeloadlist)
    result.fill_between(np.arange(0.0, 96.0, 0.1),
                        tilde,
                        0,
                        facecolor='red',
                        alpha=0.3)

    if pv_index_list:
        pv = np.interp(np.arange(0.0, 96.0, 0.1), fixed_time_list, pvlist)
        result.fill_between(np.arange(0.0, 96.0, 0.1),
                            pv,
                            0,
                            facecolor='green',
                            alpha=0.5)

    if uchp_index_list:
        uchp = np.interp(np.arange(0.0, 96.0, 0.1), fixed_time_list,
                         tildeuchplist)
        result.fill_between(np.arange(0.0, 96.0, 0.1),
                            uchp,
                            0,
                            facecolor='purple',
                            alpha=0.1)

    if gas_chp_index_list:
        gaschp = np.interp(np.arange(0.0, 96.0, 0.1), fixed_time_list,
                           tildegaschplist)
        result.fill_between(np.arange(0.0, 96.0, 0.1),
                            gaschp,
                            0,
                            facecolor='purple',
                            alpha=0.1)

    #plt.plot(results_list, linestyle='--', marker='o', color='b')
    plt.ylabel('Energy value (kW)')
    plt.xlabel('Time instant')
    plt.locator_params(axis='x', nbins=96)
    plt.grid(True)
    resultimg = plt.savefig('results_online.png', dpi=200)

    plt.close(resultimg)

    # GRAFICO A CIAMBELLA CON PERCENTUALI

    fig, ax = plt.subplots(figsize=(6, 3), subplot_kw=dict(aspect="equal"))

    data = [
        sum(pvlist),
        sum(tildeuchplist),
        sum(tildegaschplist),
        abs(sum([sum(x) for x in zip(gridINlist, gridOUTlist)])),
        abs(sum([sum(x) for x in zip(stINlist, stOUTlist)]))
    ]
    labels = ['PV', 'UChp', 'GASChp', 'Grid', 'Storage']

    def func(pct, allvals):
        absolute = int(pct / 100. * np.sum(allvals))
        return "{:.1f}%\n({:d} kWh)".format(pct, absolute)

    wedges, texts, autotexts = ax.pie(data,
                                      wedgeprops=dict(width=0.5),
                                      startangle=-40,
                                      autopct=lambda pct: func(pct, data),
                                      pctdistance=0.8,
                                      textprops={
                                          'color': "w",
                                          'fontsize': 7
                                      })

    bbox_props = dict(boxstyle="square,pad=0.3", fc="w", ec="k", lw=0.72)
    kw = dict(arrowprops=dict(arrowstyle="-"),
              bbox=bbox_props,
              zorder=0,
              va="center")

    for i, p in enumerate(wedges):
        ang = (p.theta2 - p.theta1) / 2. + p.theta1
        y = np.sin(np.deg2rad(ang))
        x = np.cos(np.deg2rad(ang))
        horizontalalignment = {-1: "right", 1: "left"}[int(np.sign(x))]
        connectionstyle = "angle,angleA=0,angleB={}".format(ang)
        kw["arrowprops"].update({"connectionstyle": connectionstyle})
        ax.annotate(labels[i],
                    xy=(x, y),
                    xytext=(1.35 * np.sign(x), 1.4 * y),
                    horizontalalignment=horizontalalignment,
                    **kw)

    ax = plt.savefig('pie_online.png', dpi=200)

    plt.close(ax)
Пример #58
0
def plot_all_KDE(sol):
    MDL = sol["pymc_model"]
    #    model = get_model_type(sol)
    filename = sol["path"].replace("\\", "/").split("/")[-1].split(".")[0]
    keys = sorted([x.__name__ for x in MDL.deterministics]) + sorted(
        [x.__name__ for x in MDL.stochastics])
    sampler = MDL.get_state()["sampler"]
    try:
        keys.remove("zmod")
        keys.remove("m_")
#        keys.remove("log_mean_tau")
#        keys.remove("total_m")
    except:
        pass
    for (i, k) in enumerate(keys):
        vect = old_div((MDL.trace(k)[:].size), (len(MDL.trace(k)[:])))
        if vect > 1:
            keys[i] = [k + "%d" % n for n in range(0, vect)]
    keys = list(flatten(keys))
    ncols = len(keys)
    nrows = len(keys)
    labels = [
        r"$log_{10}\bar{\tau}$", "$\Sigma m$", "$R_0$", "$a_0$", "$a_1$",
        "$a_2$", "$a_3$", "$a_4$"
    ]
    fig = plt.figure(figsize=(13, 13))
    #    plt.ticklabel_format(style='sci', axis='both', scilimits=(0,0))
    plotz = len(keys)
    for i in range(plotz):
        for j in range(plotz):
            if j < i:
                var1 = keys[j]
                var2 = keys[i]
                label1 = labels[j]
                label2 = labels[i]

                print((label1, label2))
                ax = plt.subplot2grid((plotz - 1, plotz - 1), (i - 1, j))
                ax.ticklabel_format(axis='y', style='sci', scilimits=(-2, 0))
                ax.ticklabel_format(axis='x', style='sci', scilimits=(-2, 0))
                if var1 == "R0":
                    stoc1 = "R0"
                else:
                    stoc1 = ''.join([k for k in var1 if not k.isdigit()])
                    stoc_num1 = [int(k) for k in var1 if k.isdigit()]
                try:
                    x = MDL.trace(stoc1)[:, stoc_num1[0] - 1]
                except:
                    x = MDL.trace(stoc1)[:]
                if var2 == "R0":
                    stoc2 = "R0"
                else:
                    stoc2 = ''.join([k for k in var2 if not k.isdigit()])
                    stoc_num2 = [int(k) for k in var2 if k.isdigit()]
                try:
                    y = MDL.trace(stoc2)[:, stoc_num2[0] - 1]
                except:
                    y = MDL.trace(stoc2)[:]


#                y = all_chains

                xmin, xmax = min(x), max(x)
                ymin, ymax = min(y), max(y)
                # Peform the kernel density estimate
                xx, yy = np.mgrid[xmin:xmax:50j, ymin:ymax:50j]
                positions = np.vstack([xx.ravel(), yy.ravel()])
                values = np.vstack([x, y])
                kernel = gaussian_kde(values)
                kernel.set_bandwidth(bw_method='silverman')
                kernel.set_bandwidth(bw_method=kernel.factor * 2.)
                f = np.reshape(kernel(positions).T, xx.shape)
                ax.set_xlim(xmin, xmax)
                ax.set_ylim(ymin, ymax)
                plt.axes(ax)
                # Contourf plot
                plt.grid(None)
                ax.scatter(x, y, color='k', s=1)
                plt.title("           r = %.2f" % (np.corrcoef(x, y)[0, 1]),
                          fontsize=14)
                plt.xticks(rotation=90)
                plt.locator_params(axis='y', nbins=6)
                plt.locator_params(axis='x', nbins=6)
                cfset = ax.contourf(xx, yy, f, cmap=plt.cm.Blues, alpha=0.7)
                ## Or kernel density estimate plot instead of the contourf plot
                # Contour plot
                #                cset = ax.contour(xx, yy, f, levels=cfset.levels[2::2], colors='k', alpha=0.8)
                # Label plot
                #    ax.clabel(cset, cset.levels[::1], inline=1, fmt='%.1E', fontsize=10)
                plt.yticks(fontsize=14)
                plt.xticks(fontsize=14)
                if j == 0:
                    plt.ylabel("%s" % label2, fontsize=14)
                if i == len(keys) - 1:
                    plt.xlabel("%s" % label1, fontsize=14)
                if j != 0:
                    ax.yaxis.set_ticklabels([])
                if i != len(keys) - 1:
                    ax.xaxis.set_ticklabels([])

                ty = ax.yaxis.get_offset_text()
                ty.set_size(12)
                tx = ax.xaxis.get_offset_text()
                tx.set_size(12)

                plt.suptitle(title + " step method", fontsize=14)

    fig.tight_layout(pad=0, w_pad=0.0, h_pad=0)
    fig.savefig('KDE_Warburg_Matrix_Adaptive_%s.png' % (adapt), dpi=300)
    return fig
Пример #59
0
def plot_all_profiles(id, time, z0, fields, fieldsExpected, folder=sys.path[0], plotExpected=False, fillSigma=False, showTitle=False):
    #Show axis in km
    z = 0.001*z0
    
    ax = []
    plt.figure(figsize=(8,4.5))
    gs = gridspec.GridSpec(1, 4, height_ratios=[1, 1])
    
    ##############################
    # Plot sigma                 #
    ##############################
    ax.append(plt.subplot(gs[0]))
    
    sigmaBuoyant = fields.sigma.buoyant.mean
    sigmaBuoyantColor = "black"
    
    if fillSigma:
        sigmaBuoyantColor = "white"
        ax[0].fill_betweenx(z, 0*sigmaBuoyant, sigmaBuoyant,   facecolor="red", linewidth=0.)
        ax[0].fill_betweenx(z, sigmaBuoyant, 0*sigmaBuoyant+1, facecolor="blue", linewidth=0.)
    
    if plotExpected:
        ax[0].plot(fieldsExpected.sigma.buoyant.mean, z, color=sigmaBuoyantColor, linewidth=1., linestyle="--")
    
    ax[0].plot(sigmaBuoyant, z, color=sigmaBuoyantColor, linewidth=1.)
    
    plt.xlim(0., 1.)
    plt.ylim(0., 10.)
    plt.xlabel("$\\sigma_1$")
    plt.ylabel("Height (km)")
    if showTitle:
        plt.title("Vol. fraction")
        
    
    ##############################
    # Plot buoyancy              #
    ##############################
    ax.append(plt.subplot(gs[1]))
    
    if plotExpected:
        b = fieldsExpected.b.mean.mean
        bStable = fieldsExpected.b.stable.mean
        bBuoyant = fieldsExpected.b.buoyant.mean
        
        ax[1].plot(bStable, z, color="blue", linewidth=1., linestyle="--")
        ax[1].plot(bBuoyant, z, color="red", linewidth=1., linestyle="--")
        ax[1].plot(b, z, color="black", linewidth=1., linestyle="--")
    
    b = fields.b.mean.mean
    bStable = fields.b.stable.mean
    bBuoyant = fields.b.buoyant.mean
    
    ax[1].plot(bStable, z, color="blue", linewidth=1.)
    ax[1].plot(bBuoyant, z, color="red", linewidth=1.)
    ax[1].plot(b, z, color="black", linewidth=1.)
    
    plt.xlim(-0.005, 0.025)
    plt.ylim(0., 10.)
    plt.xlabel("$b_i$")
    plt.locator_params(nbins=4,axis="x")
    if showTitle:
        plt.title("Buoyancy")
    
    
    ##############################
    # Plot vertical velocity     #
    ##############################
    ax.append(plt.subplot(gs[2]))
    
    if plotExpected:
        w = fieldsExpected.w.mean.mean
        wStable = fieldsExpected.w.stable.mean
        wBuoyant = fieldsExpected.w.buoyant.mean
        
        ax[2].plot(wStable, z, color="blue", linewidth=1., linestyle="--")
        ax[2].plot(wBuoyant, z, color="red", linewidth=1., linestyle="--")
        ax[2].plot(w, z, color="black", linewidth=1., linestyle="--")
    
    w = fields.w.mean.mean
    wStable = fields.w.stable.mean
    wBuoyant = fields.w.buoyant.mean
    
    ax[2].plot(wStable, z, color="blue", linewidth=1.)
    ax[2].plot(wBuoyant, z, color="red", linewidth=1.)
    ax[2].plot(w, z, color="black", linewidth=1.)
    
    plt.xlim(-9, 9)
    plt.ylim(0., 10.)
    plt.xlabel("$w_i$")
    plt.locator_params(nbins=4,axis="x")
    if showTitle:
        plt.title("Vert. velocity")
    
    
    ##############################
    # Plot pressure              #
    ##############################
    ax.append(plt.subplot(gs[3]))
    
    if plotExpected:
        PiStable = fieldsExpected.Pi.stable.mean
        PiBuoyant = fieldsExpected.Pi.buoyant.mean
        
        ax[3].plot(PiStable, z, color="blue", linewidth=1., linestyle="--")
        ax[3].plot(PiBuoyant, z, color="red", linewidth=1., linestyle="--")
    
    PiStable = fields.Pi.stable.mean
    PiBuoyant = fields.Pi.buoyant.mean
    
    ax[3].plot(PiStable, z, color="blue", linewidth=1.)
    ax[3].plot(PiBuoyant, z, color="red", linewidth=1.)
    
    plt.xlim(-50, 20)
    plt.ylim(0., 10.)
    plt.xlabel("$P_i$")
    plt.locator_params(nbins=4,axis="x")
    if showTitle:
        plt.title("Pressure anom.")
    
    
    ##############################
    # Format figure              #
    ##############################
    # if showTitle:
        # plt.suptitle( "Profiles after {}s".format(time), fontsize=18 )
    # plt.gca().get_xaxis().get_major_formatter().set_useOffset(False)
    
    for i in xrange(1, len(ax)):
        plt.setp(ax[i].get_yticklabels(), visible=False)
    
    for i in xrange(len(ax)):
        xticks = ax[i].xaxis.get_major_ticks()
        xticks[0].label1.set_visible(False)
        xticks[-1].label1.set_visible(False)
        
        for tick in ax[i].xaxis.get_major_ticks():
            tick.label.set_fontsize(8) 
        
    plt.tight_layout()
    plt.subplots_adjust(wspace=.0)
    plt.savefig( os.path.join( folder, "profiles_{}_{}.png".format(time,id) ), bbox_inches="tight", dpi=200 )
    plt.close()
Пример #60
0
        # plt.scatter(x_data, top_5, s=20, color=color[acc_index])
        handles1.append(ln1)
        # handles2.append(ln2)
        # labels1.append("{} top-1".format(acc_key))
        # labels2.append("{} top-5".format(acc_key))
        labels1.append("{}".format(acc_key))
        pass

    plt.legend(handles=handles1 + handles2,
               labels=labels1 + labels2,
               loc='best',
               ncol=1,
               fontsize=14)
    plt.grid(linestyle='--')
    plt.ylim(0.35, 0.7)
    plt.locator_params("y", nbins=10)
    plt.xlabel('dimension', fontsize=18)
    plt.ylabel('accuracy', fontsize=18)
    plt.tick_params(labelsize=16)
    plt.subplots_adjust(top=0.96,
                        bottom=0.10,
                        left=0.12,
                        right=0.99,
                        hspace=0,
                        wspace=0)

    plt.savefig(
        Tools.new_dir(
            os.path.join("plot_new", "ic",
                         "abl_acc_ic_{}2.pdf".format(split))))
    plt.show()