예제 #1
0
def plot_trace_sample(fig,ax,U, SVT, behavior, sync_behavior, allen_area, atlas,localdisk, plot_sec=15):
    # first get all the frames for all the events and store inf frame_df
    use_trials = behavior.iloc[:90]
    frame_df = time_to_frameDF(use_trials,sync_behavior,localdisk)
    # for the first plot, get the first 10 seconds, and the events that happen then
    first_event = frame_df['stimOn_times'][0]
    first_10 = reconstruct(U,SVT[:,first_event:first_event+plot_sec*30]) # ten seconds at 30fps
    first_events = frame_df[frame_df <= first_event+plot_sec*30] - first_event
    first_events.drop(['goCue_times','goCueTrigger_times', 'feedback_times'],axis='columns',inplace=True)

    x = np.arange(plot_sec*30)
    area_r = allen_area.label*-1
    area_l = allen_area.label
    first_10_l = first_10[:,atlas==area_l].mean(axis=1)
    first_10_r = first_10[:,atlas==area_r].mean(axis=1)
    
    norm = mplc.Normalize(vmin=0,vmax=len(area_list))
    color1 = cm.tab20(norm(0))
    color2 = cm.tab20(norm(1))
    fig.suptitle(allen_area.acronym)
    ax.plot(x/30,first_10_l,color=color1)
    ax.plot(x/30,first_10_r,color=color2)

    event_colors = ['c','g','orange','k']
    cnt=0
    for (event,times) in first_events.iteritems():
        ax.vlines(times/30,np.min(first_10_r),np.max(first_10_l),colors = event_colors[cnt])
        cnt+=1

    colors = event_colors + [color1,color2]
    labels = first_events.keys().tolist()+['Right_hem','Left_hem']
    make_legend(ax,colors,labels)
    # ax.set_xlabel('time (s)')
    ax.set_ylabel('df/f')
    ax.set_xlim(0,plot_sec)
예제 #2
0
def plot_sequence_from_df(dataframe):
    """Function to plot sequences from dataframes created by sequence_to_dataframe
        dataframe --dataframe object or path to csv file
    """
    #read in csv
    if type(dataframe)==str:
        df = pd.read_csv(dataframe,index_col=0)
    else:
        df = dataframe
    plt.rcParams.update({'font.size':8})
    keys = df.keys()
    for key in keys:
        if "time" in key:
            timebase = df[key]
            timeunits = key.split('-')[1]

    ### Figure set up ###
    SequenceFig , axes = plt.subplots(len(keys)-2,1,sharex=True)
    axes[-1].set_xlabel('Time/'+timeunits) # Set label on bottom plot
    color=iter(cm.tab20(np.linspace(0,1,10))) # An iterable of colors 

    i = 0 #iterator for plots
    for key in keys:
        #look for channels in csv file
        if "SAO" in key or "FAO" in key: 
            channel_name = key.split(": ")[1]
            axes[i].plot(timebase,df[key],color = next(color))
            axes[i].set_ylabel(channel_name.replace(" ","\n"),rotation = 'horizontal',ha = "right")
            i+=1 #next plot

    print(df.head())

    
    plt.show()
예제 #3
0
def plot_multi_psth(ax,x, psth, ste, atlas, area_list,ccf_regions,format_axis=True):
    '''
    Takes the output of whole_brain_peth and plots psth traces for a subset of areas.
    x,psth,std: the output of whole_brain_peth
    atlas: (numpy array) the transformed atlas returned by 
                      atlas_from_landmarks_file(<landmarks_file>,do_transform=True)
    area_list: the list off integer area codes to index into the atlas
    '''
    
    # fig,ax = plt.subplots(1,1)
    norm = mplc.Normalize(vmin=0,vmax=len(area_list))
    cnt=0
    clist = []
    labels = []
    for area in area_list:
        color = cm.tab20(norm(cnt))
        temp_df = psth[:,atlas==area].mean(axis=1)
        temp_ste = ste[:,atlas==area].mean(axis=1)
        ax.plot(x,temp_df,color=color)
        ax.fill_between(x,temp_df+temp_ste,temp_df-temp_ste,color=color,alpha=.5)
        clist.append(color)
        if area > 0:
            labels.append(ccf_regions[ccf_regions.label==abs(area)]['acronym'].iloc[0] + ' left')
        elif area < 0:
            labels.append(ccf_regions[ccf_regions.label==abs(area)]['acronym'].iloc[0] + ' right')
        cnt+=1
    if format_axis:
        make_legend(ax,clist,labels, location='upper right',bbox_to_anchor=(.99,.99))
        ax.set_xlim(np.min(x),np.max(x))
        ax.set_xticks([0,15,30,45,60])
        ax.set_xticklabels([0,.5,1,1.5,2])
        ax.axvline(0,0,1,color='k')
        ax.set_xlabel('time (s)')
        ax.set_ylabel('df/f')
예제 #4
0
def plot_blocks(domain, tileslist, blocks, filename=None):
    """ produce the control plot showing tiles and blocks"""
    plt.figure()
    ax = plt.axes()
    plot_subdomains(tileslist, fill=True, fc="#555555")
    tl = set(tileslist)
    n = 0
    for block in blocks:
        tiles = topo.tilesfromblock(block)
        intersection = tl.intersection(set(tiles.flat))
        if len(intersection) == 0:
            pass
        else:
            color = cm.tab20(n % 20)
            n += 1
            plot_subdomains(tiles, fill=True, color=color, alpha=0.5)
    reso = 2
    lltr = (domain[0], domain[2])
    plt.axis([
        lltr[0][0] - reso, lltr[1][0] + reso, lltr[0][1] - reso,
        lltr[1][1] + reso
    ])
    plt.grid(True)
    if not (filename is None):
        plt.savefig(filename)
def clusterMajors(df, n):
    """
    clusters majors based on how they change 
    majors that change together
    
    n - number of different clusters
    """
    
    # convert to percentage change year over year 
    global yoy 
    yoy = df.pct_change(fill_method='bfill')
    
    global smoothed_yoy
    win_len = 10
    smoothed_yoy = pd.DataFrame(np.ndarray([df.shape[0]-win_len-1, df.shape[1]]), df.index[win_len:-1], df.columns)
        
    
    for major in yoy.columns:
        major_smoothed = np.convolve(yoy.loc[:,major].values, np.hanning(win_len))[win_len*2-1:-1]
        smoothed_yoy[major] = major_smoothed
    
   
    global clusters
    _, clusters = kmeans2(smoothed_yoy.to_numpy().transpose(),n, iter=20, minit='++')
    
    c_map = cm.tab20(np.arange(7))
    
   
    return clusters
예제 #6
0
    def plot_isophotes(self,
                       L,
                       c,
                       epsilon=0.01,
                       x_param=None,
                       y_param=None,
                       cmap='viridis'):
        """
        Computes and plots the isophote line for the given direction L, where L is an 3D vector, and
        for the given brightnesses c (c is a vector of brignesses).

        x_param and y_param are custom coordinates array that can be passed. If passed, the isophotes
        will be calculated on the cartesian product of those arrays, instead of on [0, 0.01, ..., 1]².
        
        cmap is the color map used to display the surface. If None is passed, the surface will be
        displayed in gray.
        """
        if x_param is None:
            x_param = np.arange(0, 1.01, 0.01)
        if y_param is None:
            y_param = np.arange(0, 1.01, 0.01)

        ax = plt.axes(projection='3d')
        ax.set_xlabel('x')
        ax.set_ylabel('y')
        ax.set_zlabel('z')

        colors = cm.tab20(c)

        for brightness, color in zip(c, colors):
            self.draw_isophote_to(ax, color, L, brightness, epsilon, x_param,
                                  y_param)
        self.draw_to(ax, cmap=cmap, alpha=0.5)

        plt.show()
예제 #7
0
    def plot_all_rawspecs(singlespec_class):
        fig = plt.figure(2, figsize=(10, 10))
        spec_list = singlespec_class.get_spec_list()
        # set the scale
        mid_spec = SingleSpectrumClass(snname=singlespec_class.snname,
                                       spec_file=spec_list[int(
                                           0.5 * len(spec_list))],
                                       verbose=True)
        mid_spec.load_raw_spec()
        scale = 0.8 * np.median(mid_spec.raw_spec['flux'])
        #
        colors = cm.tab20(np.linspace(0, 1, len(spec_list)))
        for num_file in range(len(spec_list)):
            file = spec_list[num_file]
            new_spec = SingleSpectrumClass(snname=singlespec_class.snname,
                                           spec_file=file,
                                           verbose=False)
            new_spec.load_raw_spec()
            plt.plot(new_spec.raw_spec['wls'],
                     new_spec.raw_spec['flux'] + scale * (100. - num_file),
                     color=colors[num_file],
                     label=file[10:].replace('%s/' % singlespec_class.snname,
                                             '').replace('.txt', ''))
            plt.ylabel('Flux erg s-1 cm-2 A-1')
            plt.xlabel('wls')
            # plt.legend()
            plt.xlim(2000., 10200.)
            plt.title(new_spec.snname + ' RAW SPECTRA')

        fig.savefig(join(singlespec_class.results_mainpath,
                         'all_raw_spectra.pdf'),
                    bbox_inches='tight')
        plt.show()
        plt.close()
예제 #8
0
def color(idx):
    import matplotlib.cm as cm
    import numpy as np
    if idx == 0:
        colors = cm.tab10(np.linspace(0, 1, 10))
    elif idx == 1:
        colors = cm.tab20(np.linspace(0, 1, 20))
    return colors
def plot_fwsoil_SM(fcables, layers, case_labels, ring):

    fig = plt.figure(figsize=[12, 9])
    fig.subplots_adjust(hspace=0.1)
    fig.subplots_adjust(wspace=0.05)
    plt.rcParams['text.usetex'] = False
    plt.rcParams['font.family'] = "sans-serif"
    plt.rcParams['font.sans-serif'] = "Helvetica"
    plt.rcParams['axes.labelsize'] = 14
    plt.rcParams['font.size'] = 14
    plt.rcParams['legend.fontsize'] = 14
    plt.rcParams['xtick.labelsize'] = 14
    plt.rcParams['ytick.labelsize'] = 14

    almost_black = '#262626'
    # change the tick colors also to the almost black
    plt.rcParams['ytick.color'] = almost_black
    plt.rcParams['xtick.color'] = almost_black

    # change the text colors also to the almost black
    plt.rcParams['text.color'] = almost_black

    # Change the default axis colors from black to a slightly lighter black,
    # and a little thinner (0.5 instead of 1)
    plt.rcParams['axes.edgecolor'] = almost_black
    plt.rcParams['axes.labelcolor'] = almost_black

    ax = fig.add_subplot(111)
    colors = cm.tab20(np.linspace(0, 1, len(case_labels)))
    #rainbow nipy_spectral Set1
    for case_num in np.arange(len(fcables)):
        SM = read_cable_SM(fcables[case_num], layers[case_num])
        fw = read_cable_var(fcables[case_num], "Fwsoil")
        print(SM)
        if layers[case_num] == "6":
            sm =(  SM.iloc[:,0]*0.022 + SM.iloc[:,1]*0.058 \
                 + SM.iloc[:,2]*0.154 + SM.iloc[:,3]*0.409 \
                 + SM.iloc[:,4]*(1.5-0.022-0.058-0.154-0.409) )/1.5
        elif layers[case_num] == "31uni":
            sm = SM.iloc[:, 0:10].mean(axis=1)

        ax.scatter(sm,
                   fw,
                   s=3.,
                   marker='o',
                   c=colors[case_num],
                   label=case_labels[case_num])

    ax.set_xlim(0.1, 0.45)
    ax.set_ylim(0., 1.1)
    ax.set_ylabel("β (-)")
    ax.set_xlabel("volumetric water content in top 1.5 m (m3/m3)")
    ax.legend(numpoints=1, loc='lower right')

    fig.savefig("../plots/EucFACE_fwsoil_vs_SM_%s.png" % ring,
                bbox_inches='tight',
                pad_inches=0.1)
예제 #10
0
    def draw_instance_predictions(self, predictions, track_ids):
        """
        Draw instance-level prediction results on an image.

        Args:
            predictions (Instances): the output of an instance detection/segmentation
                model. Following fields will be used to draw:
                "pred_boxes", "pred_classes", "scores", "pred_masks" (or "pred_masks_rle").

        Returns:
            output (VisImage): image object with visualizations.
        """
        boxes = predictions.pred_boxes if predictions.has(
            "pred_boxes") else None
        scores = predictions.scores if predictions.has("scores") else None
        classes = predictions.pred_classes if predictions.has(
            "pred_classes") else None
        labels = _create_text_labels(classes, scores,
                                     self.metadata.get("thing_classes", None))
        keypoints = predictions.pred_keypoints if predictions.has(
            "pred_keypoints") else None

        if predictions.has("pred_masks"):
            masks = np.asarray(predictions.pred_masks)
            masks = [
                GenericMask(x, self.output.height, self.output.width)
                for x in masks
            ]
        else:
            masks = None

        # set the color according to the track ids
        colors = [cm.tab20(id_) for id_ in track_ids]
        alpha = 0.6

        labels = [
            f'Track {id_} {label}' for label, id_ in zip(labels, track_ids)
        ]

        # increase font size
        if self._default_font_size < 20: self._default_font_size *= 1.3

        if self._instance_mode == ColorMode.IMAGE_BW:
            assert predictions.has(
                "pred_masks"), "ColorMode.IMAGE_BW requires segmentations"
            self.output.img = self._create_grayscale_image(
                (predictions.pred_masks.any(dim=0) > 0).numpy())

        self.overlay_instances(
            masks=masks,
            boxes=boxes,
            labels=labels,
            keypoints=keypoints,
            assigned_colors=colors,
            alpha=alpha,
        )
        return self.output
예제 #11
0
    def test_categorical(self):
        """Categorical maps"""
        # auto detection
        m = self.world.explore(column="continent")
        out_str = self._fetch_map_string(m)
        assert 'color":"#9467bd","continent":"Europe"' in out_str
        assert 'color":"#c49c94","continent":"NorthAmerica"' in out_str
        assert 'color":"#1f77b4","continent":"Africa"' in out_str
        assert 'color":"#98df8a","continent":"Asia"' in out_str
        assert 'color":"#ff7f0e","continent":"Antarctica"' in out_str
        assert 'color":"#9edae5","continent":"SouthAmerica"' in out_str
        assert 'color":"#7f7f7f","continent":"Oceania"' in out_str
        assert 'color":"#dbdb8d","continent":"Sevenseas(openocean)"' in out_str

        # forced categorical
        m = self.nybb.explore(column="BoroCode", categorical=True)
        out_str = self._fetch_map_string(m)
        assert 'color":"#9edae5"' in out_str
        assert 'color":"#c7c7c7"' in out_str
        assert 'color":"#8c564b"' in out_str
        assert 'color":"#1f77b4"' in out_str
        assert 'color":"#98df8a"' in out_str

        # pandas.Categorical
        df = self.world.copy()
        df["categorical"] = pd.Categorical(df["name"])
        m = df.explore(column="categorical")
        out_str = self._fetch_map_string(m)
        for c in np.apply_along_axis(colors.to_hex, 1, cm.tab20(range(20))):
            assert f'"fillColor":"{c}"' in out_str

        # custom cmap
        m = self.nybb.explore(column="BoroName", cmap="Set1")
        out_str = self._fetch_map_string(m)
        assert 'color":"#999999"' in out_str
        assert 'color":"#a65628"' in out_str
        assert 'color":"#4daf4a"' in out_str
        assert 'color":"#e41a1c"' in out_str
        assert 'color":"#ff7f00"' in out_str

        # custom list of colors
        cmap = ["#333432", "#3b6e8c", "#bc5b4f", "#8fa37e", "#efc758"]
        m = self.nybb.explore(column="BoroName", cmap=cmap)
        out_str = self._fetch_map_string(m)
        for c in cmap:
            assert f'"fillColor":"{c}"' in out_str

        # shorter list (to make it repeat)
        cmap = ["#333432", "#3b6e8c"]
        m = self.nybb.explore(column="BoroName", cmap=cmap)
        out_str = self._fetch_map_string(m)
        for c in cmap:
            assert f'"fillColor":"{c}"' in out_str

        with pytest.raises(ValueError, match="'cmap' is invalid."):
            self.nybb.explore(column="BoroName", cmap="nonsense")
예제 #12
0
def plot(delta_iters, iters):
    sns.set_style('ticks')
    plt.plot(iters, delta_iters, linewidth=2.2, c=cm.tab20(3))
    sns.despine()
    plt.ylim(delta_iters.min() * (0.5), delta_iters.max() * (1.4))

    plt.xlabel("Iterações " + r'(l)')
    plt.ylabel(r'$\Delta^l(\bar{v}^l)$')
    file = str(plots_path) + "/dispersion_graph.pdf"
    plt.savefig(file, transparent=True)
예제 #13
0
def plot(delta_iters, iters):
    sns.set_style('ticks')
    plt.plot(iters, delta_iters, linewidth = 2.2, c = cm.tab20(3))
    sns.despine()
    plt.ylim(delta_iters.min()*(0.5), delta_iters.max()*(1.4))

    plt.xlabel("Iterações " + r'(l)')
    plt.ylabel(r'$\Delta^l(\bar{v}^l)$')
    file = str(plots_path) + "/dispersion_graph.pdf"
    plt.savefig(file, transparent = True)
예제 #14
0
def plot_by_group_simple(file_name,
                         coordinates,
                         column,
                         original_table,
                         labels,
                         with_legend=True,
                         hot_colors=False):

    original_table_labels = original_table[column].loc[labels]

    df = pd.DataFrame(
        dict(x=coordinates[:, 0],
             y=coordinates[:, 1],
             label=list(original_table_labels)))
    grouped_labels = sorted(list(set(original_table_labels)))
    # Plot
    plt.figure(figsize=(20, 12), dpi=200)

    fig, ax = plt.subplots()
    ax.margins(0.05)  # Optional, just adds 5% padding to the autoscaling

    if hot_colors == False:
        colors = cm.tab20(np.linspace(0, 1, len(grouped_labels)))
    else:
        colors = cm.YlOrRd(np.linspace(0, 1, len(grouped_labels)))

    plt.ylim((-80, 80))
    plt.xlim((-80, 80))

    for i, l in enumerate(grouped_labels):

        subset = df.loc[df['label'] == l]

        if with_legend == True:
            ax.scatter(subset['x'],
                       subset['y'],
                       marker='o',
                       label=l,
                       s=0.05,
                       c=colors[i, :],
                       alpha=0.5)
        else:
            ax.scatter(subset['x'],
                       subset['y'],
                       marker='o',
                       s=0.05,
                       c=colors[i, :],
                       alpha=0.5)

    if with_legend == True:
        ax.legend(loc='upper right', prop={'size': 7})
    path = 'results_lsa/' + file_name
    plt.savefig(path, dpi=200)
예제 #15
0
def callback(msg):
    raio = 0
    i = 0
    c = 1
    marker_array = MarkerArray()
    n_color = 0
    color_map = cm.tab20(
        range(0, len(msg.ranges))
    )  #Vou criar o colormap com o tamanho do msg.ranges para depois o percorrer
    for r in msg.ranges:  # Vou ver a variação do raio

        raio_inicial = raio
        raio = r

        if abs(raio - raio_inicial) > 1:  # Condição
            global color_r, color_b, color_g
            color_r = color_map[n_color, 0]
            color_b = color_map[n_color, 1]
            color_g = color_map[n_color, 2]
            n_color += 1

        theta = msg.angle_min + i * msg.angle_increment

        x = r * cos(theta)  # r é o raio
        y = r * sin(theta)

        # Marcadores a mudar de cor
        Sphere = Marker()
        Sphere.header.stamp = rospy.Time.now()
        Sphere.header.frame_id = "base_laser_link"
        Sphere.type = Marker.SPHERE
        Sphere.action = Marker.ADD
        Sphere.id = c
        Sphere.ns = "SPHERE"
        Sphere.scale.x = 0.5
        Sphere.scale.y = 0.5
        Sphere.scale.z = 0.5
        Sphere.color.r = color_r
        Sphere.color.g = color_g
        Sphere.color.b = color_b
        Sphere.color.a = 0.3
        Sphere.pose.orientation.w = 1.0
        Sphere.pose.position.x = x
        Sphere.pose.position.y = y
        Sphere.pose.position.z = 0

        marker_array.markers.append(Sphere)
        i += 1
        c += 1

    global pub
    pub.publish(marker_array)
 def set_animate_gbar(self, ax_num, rpm_index, inj_col, width=0.1):
     ax = self.ax[ax_num]
     x = np.arange(len(rpm_index))
     bar_list = []
     for i, inj in enumerate(inj_col):
         bar_list.append(
             ax.bar(x + i * width, [0] * len(rpm_index),
                    width,
                    label=inj,
                    color=cm.tab20(i * 0.1)))
         plt.legend()
         plt.xlabel('rpm')
     self.gbars.append(bar_list)
     self.gbar_nums = self.gbar_nums + 1
예제 #17
0
def laserMsgCalback(msg, publisher):
    rospy.loginfo('Received LaserScan msg')

    # initialize a marker for publishing
    marker = Marker()
    marker.header.frame_id = msg.header.frame_id
    marker.header.stamp = msg.header.stamp
    marker.ns = "clusters"
    marker.id = 0
    marker.type = Marker.POINTS
    marker.action = Marker.ADD
    marker.pose.orientation.w = 1.0
    marker.scale.x = 0.1
    marker.scale.y = 0.1
    marker.scale.z = 1

    threshold_max_range_dif = 0.5
    cluster_idx = 0
    points_cluster = []

    # Calculate number of clusters
    for idx, r in enumerate(msg.ranges):
        if abs(r - msg.ranges[idx - 1]) > threshold_max_range_dif and idx != 0:
            cluster_idx += 1
        points_cluster.append(cluster_idx)

    # initialize clusters and color map

    colormap = cm.tab20(range(0, cluster_idx + 1))

    for idx, r in enumerate(msg.ranges):
        theta = msg.angle_min + idx * msg.angle_increment
        x, y = r * cos(theta), r * sin(theta)

        point = Point(x=x, y=y, z=0)
        marker.points.append(point)

        color = ColorRGBA(r=colormap[points_cluster[idx], 0],
                          g=colormap[points_cluster[idx], 1],
                          b=colormap[points_cluster[idx], 2],
                          a=1)
        marker.colors.append(color)

    marker_array = MarkerArray()
    marker_array.markers.append(marker)
    publisher.publish(marker_array)
    rospy.loginfo('Published MarkerArray with clusters')
예제 #18
0
def plot_data(chars, timelines, deaths, scenes, annotations):
    """
    Draws a nice xkcd like movie timeline
     - chars: {character_id: character_name}
    - timelines: {character_id: [y0, y1, y2, ...],
    - deaths: {character_id: (x, y)}
    - scenes: [[s0], [s1], [s2], ...]
    - annotation: [a0, a1, a2, ...]
    when a is the location of the according scenes s0.
    """
    plt.xkcd()  # because python :)

    fig = plt.figure(figsize=(60, 20))
    ax = fig.add_subplot(111)
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
    ax.set_xlim([0, max(map(len, timelines.values()))])

    # find a different color to every characters:
    color_floats = np.linspace(0, 1, len(chars))
    color_of = lambda char_id: cm.tab20(color_floats[char_id])

    # draw the characters line using a linear interpolation between the according timelines:
    for char_id in sorted(chars):
        y = timelines[char_id]
        f = interp1d(np.linspace(0, len(y)-1, len(y)), y, kind='linear')
        x = np.linspace(0, len(y)-1, len(y))
        y_max = find_max(timelines, scenes)
        for i in range(0, len(x) - 1, 2):
            plt.annotate(annotations[i//2], (0.5 + x[i], y_max[i]), fontsize='medium',
                     fontweight='bold')
        ax.plot(x, f(x), c=color_of(char_id))

    # add the special notation the the location when some characters died:
    x, y = zip(*(deaths[char_id] for char_id in sorted(deaths)))
    ax.scatter(x, y, c=np.array(list(map(color_of, sorted(deaths)))), zorder=99, s=40)

    for char_id in deaths.keys():
        point = deaths.get(char_id)
        plt.annotate(chars[char_id] + " DEAD", (point[0] + 0.15, point[1]), fontsize='small',
                     fontstyle='italic')

    ax.legend(list(map(chars.get, sorted(chars))), loc='best', ncol=4)
    print ('testplot.png saved')
    fig.savefig('testplot.png')
예제 #19
0
    def plot_all_mangledspecs(singlespec_class):
        spec_list = singlespec_class.get_spec_list()
        # set the scale
        mid_spec = SingleSpectrumClass(snname=singlespec_class.snname,
                                       spec_file=spec_list[int(
                                           0.5 * len(spec_list))],
                                       verbose=False)
        mid_spec.load_raw_spec()
        mid_spec.mangle_spectrum_function()

        scale = 0.3 * np.median(mid_spec.mangled_spec['flux'])
        #
        from itertools import cycle
        colors = cycle(cm.tab20(np.linspace(0, 1, 10)))

        figure_main = plt.figure(10, figsize=(10, 10))
        ax = figure_main.add_subplot(111)
        for num_file in range(len(spec_list)):
            file = spec_list[num_file]
            new_spec = SingleSpectrumClass(snname=singlespec_class.snname,
                                           spec_file=file,
                                           verbose=False)
            new_spec.load_raw_spec()
            result = new_spec.mangle_spectrum_function()
            if result is not None:
                ax.plot(
                    new_spec.mangled_spec['wls'],
                    new_spec.mangled_spec['flux'] + scale * (10. - num_file),
                    color=next(colors),
                    lw=1.,
                    label=file[30:].replace('%s/' % singlespec_class.snname,
                                            '').replace('.txt', ''))
                ax.set_ylabel('Flux erg s-1 cm-2 A-1')
                ax.set_xlabel('wls')
                ax.legend(fontsize=4, ncol=2)
                ax.set_xlim(2000., 10200.)
                ax.set_title(new_spec.snname + ' MANGLED SPECTRA')
                # new_spec.save_mangled_spectrum()
        plt.grid()
        figure_main.savefig(join(singlespec_class.results_mainpath,
                                 'all_mangled_spectra.pdf'),
                            bbox_inches='tight')
        plt.show()
        plt.close(figure_main)
예제 #20
0
def draw_many_tracks(imL, imR, tracks, indices):
    if imL is None:
        imL = (np.ones((720, 1680, 3)) * 255).astype('uint8')
        imR = imL
    import matplotlib.cm as cm
    # define colors, get frame and features
    colors = (cm.tab20(np.linspace(0.0, 1.0, len(indices)))[:, :3] *
              255).astype('uint8')
    border = np.zeros((720, 20, 3), dtype='uint8')
    j = 0
    for i, c in zip(indices, colors):
        j += 1
        track = tracks[i]
        imL, imR = draw_track(imL,
                              imR,
                              track,
                              color=colors[j % 20],
                              together=False)
    return np.hstack((imL, border, imR))
예제 #21
0
def scatter_plot(m_l, y_l, title, path):
    """
    scatter plot for 2D matrix m

    Args:
        m_l(list): list of n-by-2 matrix
        y_l(list): list of len(m) labels
        path(str): path to save image
    """
    fig = plt.figure(figsize=(8, 8))
    ax = fig.add_subplot(1, 1, 1)
    p_l = []
    colors = cm.tab20(np.linspace(0, 1, len(y_l)))
    for m, y, c in zip(m_l, y_l, colors):
        p_l.append(plt.scatter(m[:, 0], m[:, 1], alpha=.8, c=c))
    ax.legend(p_l, y_l)
    ax.set_title(title)
    plt.savefig(path, bbox_inches="tight")
    plt.close()
예제 #22
0
def get_row_colors(df):
    '''
    Use latest time point to assign a color to all rows in df
    so that a row's bar color persist across the time series.
    The colormap is tab20, so it will cycle over the rows.
    df: a pandas.DataFrame whose index are strings and columns
    datetime or compatible with `df.columns.max()`.
    Return: a dict.
    '''
    from matplotlib import cm
    import matplotlib as mpl

    if 'row_color' in df.columns:
        return

    n = df.shape[0]
    t20 = [mpl.colors.to_hex(c) for c in cm.tab20(range(1, n + 1))]
    # create color dict:
    idx = df[df.columns.max()].sort_values(ascending=False).index

    return dict(zip(idx, t20))
def draw_node_amount(df, column):
    # Kopioidaan taulu
    dftest = df.copy()
    # Alustetaan Counter
    _count = Counter()
    # Annetaan Counterille node_id kolumni
    _count.update(dftest[column])

    cart_name = []
    cart_amount = []

    #Täytetään listat noden nimellä ja lasketaan kuinka monta kertaa kyseinen node esiintyy taulussa
    for i in _count:
        #print('%s : %d' % (i, _count[i]))
        cart_name.append(i)
        cart_amount.append(_count[i])

    #Järjestetään node_id:t
    y_pos = np.arange(len(cart_name))

    N = 20
    cmap = cm.tab20(np.linspace(0, 1, N))
    plt.figure(figsize=(10, 7))

    line = plt.barh(y_pos, cart_amount, color=cmap)
    plt.yticks(y_pos, cart_name)
    plt.xlabel('Määrä')
    plt.title('Nodejen käyttömäärä')

    # Lisätään palkkien perään palkin pituus.
    for p in line.patches:
        width = p.get_width()
        plt.text(5+p.get_width(), p.get_y()+0.55*p.get_height(),
                 '{:1.2f}'.format(width), va='center', fontsize=9)

    plt.tight_layout()
    #plt.savefig("Nodejen käyttömäärä.png")
    plt.show()
def plot_fluorocarbons(model_df, model='GCAM'):
    out_path = r"C:\Users\nich980\data\global_ar6"

    plt.style.use('ggplot')

    scenarios = get_scenarios(model_df)

    colors = cm.tab20(np.linspace(0, 1, len(scenarios)))

    figsize = (10, 8)
    cols = 4
    rows = 4

    #    fig, axs = plt.subplots(rows, cols, figsize=figsize, constrained_layout=True)

    hfc = model_df[model_df['EM_Species'] == 'HFC']
    pfc = model_df[model_df['EM_Species'] == 'PFC']

    species_dict = {'HFC': hfc, 'PFC': pfc}

    plot_dims = {'HFC': (3, 3), 'PFC': (2, 2)}

    legend_coords = {'HFC': (0.97, 0.01), 'PFC': (0.9, 0.07)}

    figsize = (10, 8)

    for species in ['HFC']:

        cols = plot_dims[species][0]
        rows = plot_dims[species][1]

        fig, axs = plt.subplots(rows,
                                cols,
                                figsize=figsize,
                                dpi=150,
                                constrained_layout=True)
        fig.suptitle('{} Scenarios for {} Sub-species'.format(model, species),
                     fontsize=16)

        # HFC or PFC DataFrame
        species_df = species_dict[species]

        sub_species = species_df['EM_SubSpecies'].unique().tolist()

        units = species_df['Unit'].tolist()[0]

        #        if (not isinstance(units, str)):
        #            units = units[0]

        units = '{}/yr'.format(units[:2])

        axs = trim_axs(axs, len(sub_species))

        for ax, sub_s in zip(axs, sub_species):

            # Sub-species DataFrame
            subs_df = species_df[species_df['EM_SubSpecies'] == sub_s]
            #
            #            units = data_df['Unit'].tolist()
            #            if (not isinstance(units, str)):
            #                    units = units[0]
            #
            #            units = '{}/yr'.format(units[:2])

            ax.set_title('{}'.format(sub_s))

            for scenario_idx, scenario in enumerate(scenarios):
                scenario_df = subs_df[subs_df['Scenario'] == scenario]

                x = [
                    int(col) for col in scenario_df.columns.tolist()
                    if col.isdigit()
                ]

                # If sub-species exist, sum their values
                y = scenario_df.iloc[:, 7:].sum().values

                ax.plot(x,
                        y,
                        c=colors[scenario_idx],
                        ls='-',
                        lw=1.5,
                        label=scenario)

            ax.set_ylabel('{}'.format(units))
            ax.set_xticks([2025, 2050, 2075, 2100])
            ax.set_xlim(2015, 2100)

        # End species loop

        handles, labels = ax.get_legend_handles_labels()
        #    fig.legend(handles, labels, loc='lower right', ncol=2)
        leg = fig.legend(handles,
                         labels,
                         loc=4,
                         bbox_to_anchor=legend_coords[species],
                         ncol=2,
                         title='GCAM Scenarios')

        for legobj in leg.legendHandles:
            legobj.set_linewidth(3.0)

        f_name = '{}-{}-facet.png'.format(model, scenario)
        out_path = join(out_path, f_name)
        print(f_name)

        figManager = plt.get_current_fig_manager()
        figManager.window.showMaximized()

        plt.show()
        break
def plot_gcam_scanarios(model_df, model='GCAM'):
    """
    Plot all GCAM scenarios for each species (except HFC & PFC) on a facet plot
    """
    out_path = r"C:\Users\nich980\data\global_ar6"

    plt.style.use('ggplot')

    scenarios = get_scenarios(model_df)

    colors = cm.tab20(np.linspace(0, 1, len(scenarios)))

    figsize = (10, 8)
    cols = 4
    rows = 4

    #    fig, axs = plt.subplots(rows, cols, figsize=figsize, constrained_layout=True)

    em_species = model_df['EM_Species'].unique().tolist()
    em_species.remove('HFC')
    em_species.remove('PFC')

    figsize = (10, 8)
    cols = 4
    rows = 4

    fig, axs = plt.subplots(rows,
                            cols,
                            figsize=figsize,
                            dpi=150,
                            constrained_layout=True)
    fig.suptitle('{} Emission Species & Scenarios'.format(model), fontsize=16)

    axs = trim_axs(axs, len(em_species))

    for ax, species in zip(axs, em_species):

        data_df = model_df[model_df['EM_Species'] == species]

        units = data_df['Unit'].tolist()
        if (not isinstance(units, str)):
            units = units[0]

        units = '{}/yr'.format(units[:2])

        ax.set_title('species = {}'.format(species))

        for scenario_idx, scenario in enumerate(scenarios):
            scenario_df = data_df[data_df['Scenario'] == scenario]

            x = [
                int(col) for col in scenario_df.columns.tolist()
                if col.isdigit()
            ]

            # If sub-species exist, sum their values
            y = scenario_df.iloc[:, 7:].sum().values

            ax.plot(x, y, c=colors[scenario_idx], ls='-', lw=1, label=scenario)

        ax.set_ylabel('{}'.format(units))
        ax.set_xticks([2025, 2050, 2075, 2100])
        ax.set_xlim(2015, 2100)

    # End species loop

    handles, labels = ax.get_legend_handles_labels()
    #    fig.legend(handles, labels, loc='lower right', ncol=2)
    leg = fig.legend(handles,
                     labels,
                     loc=4,
                     bbox_to_anchor=(0.98, 0.17),
                     prop={'size': 8},
                     ncol=2,
                     title='GCAM Scenarios')

    for legobj in leg.legendHandles:
        legobj.set_linewidth(3.0)

    f_name = '{}-{}-facet.png'.format(model, scenario)
    out_path = join(out_path, f_name)
    print(f_name)

    figManager = plt.get_current_fig_manager()
    figManager.window.showMaximized()

    plt.show()
예제 #26
0
def plot_GPP(fcables, ring, case_labels):
    '''
    plot GPP
    '''

    fig = plt.figure(figsize=[9, 5])
    fig.subplots_adjust(hspace=0.1)
    fig.subplots_adjust(wspace=0.05)
    plt.rcParams['text.usetex'] = False
    plt.rcParams['font.family'] = "sans-serif"
    plt.rcParams['font.sans-serif'] = "Helvetica"
    plt.rcParams['axes.labelsize'] = 14
    plt.rcParams['font.size'] = 14
    plt.rcParams['legend.fontsize'] = 14
    plt.rcParams['xtick.labelsize'] = 14
    plt.rcParams['ytick.labelsize'] = 14

    almost_black = '#262626'
    # change the tick colors also to the almost black
    plt.rcParams['ytick.color'] = almost_black
    plt.rcParams['xtick.color'] = almost_black

    # change the text colors also to the almost black
    plt.rcParams['text.color'] = almost_black

    # Change the default axis colors from black to a slightly lighter black,
    # and a little thinner (0.5 instead of 1)
    plt.rcParams['axes.edgecolor'] = almost_black
    plt.rcParams['axes.labelcolor'] = almost_black

    ax = fig.add_subplot(111)

    case_sum = len(fcables)

    colors = cm.tab20(np.linspace(0, 1, case_sum))

    umol_2_gC = 12.0107 * 1.0E-6
    for case_num in np.arange(case_sum):
        GPP = read_cable_GPP_year(fcables[case_num], "GPP")

        x = GPP.index

        ax.plot(x,
                GPP['cable'] * umol_2_gC,
                c=colors[case_num],
                lw=1.0,
                ls="-",
                label=case_labels[case_num]
                )  #.rolling(window=5).mean() .rolling(window=7).mean()
        # .rolling(window=30).mean()*umol_2_gC

    #cleaner_dates = ["2013","2014","2015","2016","2017","2018","2019"]
    #xtickslocs    = [367,732,1097,1462,1828,2193,2558]

    #plt.setp(ax.get_xticklabels(), visible=True)
    #ax.set(xticks=xtickslocs, xticklabels=cleaner_dates) ####
    #ax.set_ylabel("GPP (umol m$^{-2}$)")
    ax.set_ylabel("GPP (gC m$^{-2}$ yr$^{-1}$)")
    ax.axis('tight')
    #ax.set_ylim(0.,3.0)
    #ax.set_xlim(367,2739) # MMY
    # ax.set_xlim(367,1098) MMY
    ax.legend()

    fig.savefig("../plots/EucFACE_GPP_%s" % ring,
                bbox_inches='tight',
                pad_inches=0.1)
예제 #27
0
def plot_ET(fcables, ring, case_labels):
    '''
    plot Trans and ESoil during 2014-1-15 ~ 2014-1-20 heatwave
    '''

    fig = plt.figure(figsize=[9, 5])
    fig.subplots_adjust(hspace=0.1)
    fig.subplots_adjust(wspace=0.05)
    plt.rcParams['text.usetex'] = False
    plt.rcParams['font.family'] = "sans-serif"
    plt.rcParams['font.sans-serif'] = "Helvetica"
    plt.rcParams['axes.labelsize'] = 14
    plt.rcParams['font.size'] = 14
    plt.rcParams['legend.fontsize'] = 12
    plt.rcParams['xtick.labelsize'] = 14
    plt.rcParams['ytick.labelsize'] = 14

    almost_black = '#262626'
    # change the tick colors also to the almost black
    plt.rcParams['ytick.color'] = almost_black
    plt.rcParams['xtick.color'] = almost_black

    # change the text colors also to the almost black
    plt.rcParams['text.color'] = almost_black

    # Change the default axis colors from black to a slightly lighter black,
    # and a little thinner (0.5 instead of 1)
    plt.rcParams['axes.edgecolor'] = almost_black
    plt.rcParams['axes.labelcolor'] = almost_black

    ax = fig.add_subplot(111)

    subs_Esoil = read_obs_esoil(ring)
    subs_Trans = read_obs_trans(ring)
    case_sum = len(fcables)

    colors = cm.tab20(np.linspace(0, 1, case_sum))
    ax.scatter(subs_Trans.index,
               subs_Trans['obs'],
               marker='o',
               c='',
               edgecolors="green",
               s=4.,
               label="Trans Obs")  # subs['EfloorPred'] 'blue'
    ax.scatter(subs_Esoil.index,
               subs_Esoil['obs'],
               marker='o',
               c='',
               edgecolors="red",
               s=4.,
               label="ESoil Obs")  # subs['EfloorPred'] 'red'
    for case_num in np.arange(case_sum):
        TVeg = read_cable_var(fcables[case_num], "TVeg")
        ESoil = read_cable_var(fcables[case_num], "ESoil")
        x = TVeg.index

        ax.plot(x,
                TVeg['cable'],
                c=colors[case_num],
                lw=1.0,
                ls="-",
                label=case_labels[case_num]
                )  #.rolling(window=5).mean() .rolling(window=7).mean()
        ax.plot(x, ESoil['cable'], c=colors[case_num], lw=1.0,
                ls="-.")  #.rolling(window=7).mean()

    #cleaner_dates = ["2013","2014","2015","2016","2017","2018","2019"] MMY
    #xtickslocs    = [367,732,1097,1462,1828,2193,2558] MMY
    cleaner_dates = [
        "2014-1-13", "1-14", "1-15", "1-16", "1-17", "1-18", "1-19", "1-20"
    ]
    xtickslocs = [744, 745, 746, 747, 748, 749, 750, 751]

    plt.setp(ax.get_xticklabels(), visible=True)
    ax.set(xticks=xtickslocs, xticklabels=cleaner_dates)  ####
    ax.set_ylabel("Trans, Esoil (mm d$^{-1}$)")
    ax.axis('tight')
    ax.set_ylim(0., 3.0)
    ax.set_xlim(744, 751)  #(367,2739) # MMY
    # ax.set_xlim(367,1098) MMY
    ax.legend()

    fig.savefig("../plots/EucFACE_ET_%s" % ring,
                bbox_inches='tight',
                pad_inches=0.1)
예제 #28
0
def plot_check_ET(fcables, case_labels, ring):
    '''
    Check ET
    '''

    # ======================= PLOTTING  ==========================
    fig = plt.figure(figsize=[9, 7])

    fig.subplots_adjust(hspace=0.15)
    fig.subplots_adjust(wspace=0.05)

    plt.rcParams['text.usetex'] = False
    plt.rcParams['font.family'] = "sans-serif"
    plt.rcParams['font.serif'] = "Helvetica"
    plt.rcParams['axes.linewidth'] = 1.5
    plt.rcParams['axes.labelsize'] = 14
    plt.rcParams['font.size'] = 14
    plt.rcParams['legend.fontsize'] = 12
    plt.rcParams['xtick.labelsize'] = 14
    plt.rcParams['ytick.labelsize'] = 14

    almost_black = '#262626'
    # change the tick colors also to the almost black
    plt.rcParams['ytick.color'] = almost_black
    plt.rcParams['xtick.color'] = almost_black

    # change the text colors also to the almost black
    plt.rcParams['text.color'] = almost_black

    # Change the default axis colors from black to a slightly lighter black,
    # and a little thinner (0.5 instead of 1)
    plt.rcParams['axes.edgecolor'] = almost_black
    plt.rcParams['axes.labelcolor'] = almost_black

    props = dict(boxstyle="round", facecolor='white', alpha=0.0, ec='white')

    ax1 = fig.add_subplot(211)
    ax2 = fig.add_subplot(212)

    case_sum = len(fcables)
    colors = cm.tab20(np.linspace(0, 1, case_sum))

    # set x-axis values
    cleaner_dates1 = ["2013", "2014", "2015", "2016", "2017", "2018", "2019"]
    xtickslocs1 = [367, 732, 1097, 1462, 1828, 2193, 2558]

    # ========================= ET FLUX  ============================
    # ===== Obs   =====
    subs_Esoil = read_obs_esoil(ring)
    subs_Trans = read_obs_trans(ring)

    ax1.scatter(subs_Trans.index,
                subs_Trans['obs'].rolling(window=3).mean(),
                marker='o',
                c='',
                edgecolors='red',
                s=2.,
                label="Obs")
    ax2.scatter(subs_Esoil.index,
                subs_Esoil['obs'].rolling(window=3).mean(),
                marker='o',
                c='',
                edgecolors='red',
                s=2.,
                label="Obs")
    # .rolling(window=3).mean()

    # ===== CABLE =====
    for i in np.arange(case_sum):
        cable = nc.Dataset(fcables[i], 'r')
        Time = nc.num2date(cable.variables['time'][:],
                           cable.variables['time'].units)

        TVeg = pd.DataFrame(cable.variables['TVeg'][:, 0, 0], columns=['TVeg'])
        TVeg = TVeg * 1800.
        TVeg['dates'] = Time
        TVeg = TVeg.set_index('dates')
        TVeg = TVeg.resample("D").agg('sum')
        TVeg.index = TVeg.index - pd.datetime(2011, 12, 31)
        TVeg.index = TVeg.index.days

        ESoil = pd.DataFrame(cable.variables['ESoil'][:, 0, 0],
                             columns=['ESoil'])
        ESoil = ESoil * 1800.
        ESoil['dates'] = Time
        ESoil = ESoil.set_index('dates')
        ESoil = ESoil.resample("D").agg('sum')
        ESoil.index = ESoil.index - pd.datetime(2011, 12, 31)
        ESoil.index = ESoil.index.days

        x = TVeg.index

        #ax1.plot(x, TVeg['TVeg'].rolling(window=3).mean(),   c=colors[i], lw=1.0, ls="-", label=case_labels[i]) #
        #ax2.plot(x, ESoil['ESoil'].rolling(window=3).mean(), c=colors[i], lw=1.0, ls="-", label=case_labels[i]) #.rolling(window=7).mean()

        print(len(subs_Trans))
        print(len(TVeg[TVeg.index.isin(subs_Trans.index)]['TVeg']))
        print(len(x))
        print(len(TVeg['TVeg']))

        ax1.scatter(subs_Trans.index,
                    TVeg[TVeg.index.isin(
                        subs_Trans.index)]['TVeg'].rolling(window=3).mean(),
                    marker='o',
                    c='',
                    edgecolors=colors[i],
                    s=2.,
                    label=case_labels[i])

        ax2.scatter(subs_Esoil.index,
                    ESoil[ESoil.index.isin(
                        subs_Esoil.index)]['ESoil'].rolling(window=3).mean(),
                    marker='o',
                    c='',
                    edgecolors=colors[i],
                    s=2.,
                    label=case_labels[i])

        subs_Esoil['obs'].fillna(0., inplace=True)

        print(ESoil[np.all([
            ESoil.index.isin(subs_Esoil.index),
            np.isnan(subs_Esoil.values) == False
        ],
                           axis=0)]['ESoil'])
        print(subs_Esoil[np.isnan(subs_Esoil.values) == False]['obs'])
        print(
            stats.pearsonr(
                subs_Esoil[np.isnan(subs_Esoil.values) == False]['obs'],
                ESoil[np.all([
                    ESoil.index.isin(subs_Esoil.index),
                    np.isnan(subs_Esoil.values) == False
                ],
                             axis=0)]['ESoil']))

    ax1.text(0.02,
             0.95,
             '(a)',
             transform=ax1.transAxes,
             fontsize=14,
             verticalalignment='top',
             bbox=props)
    ax2.text(0.02,
             0.95,
             '(b)',
             transform=ax2.transAxes,
             fontsize=14,
             verticalalignment='top',
             bbox=props)

    # this order of the setting can affect plot x & y axis
    plt.setp(ax1.get_xticklabels(), visible=True)
    ax1.set(xticks=xtickslocs1, xticklabels=cleaner_dates1)  ####
    ax1.set_ylabel("T (mm d$^{-1}$)")
    ax1.axis('tight')
    ax1.set_ylim(0., 3.5)
    ax1.set_xlim(367, 1097)
    ax1.legend(loc='best', frameon=False)

    # this order of the setting can affect plot x & y axis
    plt.setp(ax2.get_xticklabels(), visible=True)
    ax2.set(xticks=xtickslocs1, xticklabels=cleaner_dates1)  ####
    ax2.set_ylabel("Es (mm d$^{-1}$)")
    ax2.axis('tight')
    ax2.set_ylim(0., 4.5)
    ax2.set_xlim(367, 1097)
    ax2.legend(loc='best', frameon=False)

    fig.savefig("../plots/EucFACE_Check_ET_%s.png" % ring,
                bbox_inches='tight',
                pad_inches=0.1)
예제 #29
0
                                                                 '&amp;',
                                                                 regex=False)
    df['MessageBody'] = df['MessageBody'].fillna('').str.replace('>',
                                                                 '&gt;',
                                                                 regex=False)
    df['MessageBody'] = df['MessageBody'].fillna('').str.replace('<',
                                                                 '&lt;',
                                                                 regex=False)
    df['MessageBody'] = df['MessageBody'].str.replace('[\?#]utm_.*', '')
    df['MessageBody'] = df['MessageBody'].fillna('').str.replace('\\n',
                                                                 '<br/>',
                                                                 regex=False)
    df = df.set_index('DateTime', drop=False)

    # colors for individual users
    user_colors = cm.tab20(range(len(df['UserID'].unique())))
    user_colors[:, 3] = 0.2
    user_colors[:, :3] *= 255
    user_colors[:, :3] //= 1
    # ptají se na kolik tramvají má DP, naprgat fibbonaciho posloupnost, binární strom, databáz. struktura konzultant, projekty, hodin. sazby, jak spočítám ziskovost DP?
    user_to_color = {
        user_id: [str(i) for i in user_colors[i]]
        for i, user_id in enumerate(df['UserID'].unique())
    }

    # if user was missing for some time, he has Removed User XYZ instead of his name. This is fixed here.
    id_and_name = df.groupby(
        ['UserID', 'UserName']).count().index.to_frame().reset_index(drop=True)
    id_to_name = id_and_name.groupby('UserID').agg(select_name)

    # I can not pair images to text, so I exclude empty messages
예제 #30
0
names = get_names()
datas, length = get_datas_and_length(names)

l_results = get_results_of_letter(names, length)
w_results = get_results_of_word(names, length)
d_results = get_results_of_duration(names, length)

log_likelihood = np.loadtxt("summary_files/log_likelihood.txt")
resample_times = np.loadtxt("summary_files/resample_times.txt")
print("Done!")

train_iter = l_results[0].shape[0]

#%%
lcolors = ListedColormap(
    [cm.tab20(float(i) / letter_num) for i in range(letter_num)])
wcolors = ListedColormap(
    [cm.tab20(float(i) / word_num) for i in range(word_num)])

#%%
print("Plot results...")
for i, name in enumerate(tqdm(names)):
    plt.clf()
    _plot_discreate_sequence(datas[i], name + "_l", l_results[i], cmap=lcolors)
    plt.savefig("figures/" + name + "_l.png")
    plt.clf()
    _plot_discreate_sequence(datas[i], name + "_s", w_results[i], cmap=wcolors)
    plt.savefig("figures/" + name + "_s.png")
    plt.clf()
    _plot_discreate_sequence(datas[i],
                             name + "_d",
예제 #31
0
def plot_diff(
    diff_mean,
    diff_std,
    eps_values,
    legends,
    prune_ratios,
    title,
    ylabel,
    noise_type,
    name_filter,
    pr_filters,
    percentage_y,
    plots_dir,
    plots_tag,
):
    """Plot the mean differences with std dev."""

    # let's filter now with legends and prune_ratios
    filtered_name = np.array([name_filter in leg for leg in legends])

    # use those to determine best indices
    prune_ratios_filt = np.array(prune_ratios)[filtered_name]
    idxs_pr = [
        np.argmin(np.abs(prune_ratios_filt - pr_desired)).item()
        for pr_desired in pr_filters
    ]

    # full filter now
    filtered_pr = np.zeros_like(filtered_name)
    filtered_pr[np.where(filtered_name)[0][idxs_pr]] = True

    # make sure we also have reference networks included now
    filtered_ref = np.array(["network" in leg.lower() for leg in legends])

    legends_filtered = []
    colors_filtered = []
    ls_filtered = []
    for i in range(len(diff_mean)):
        if filtered_ref[i]:

            legend = legends[i].split()[0]
            color = "black" if "Unpruned" in legend else "magenta"
            ls = "--"
        elif filtered_pr[i]:
            legend = f"PR={prune_ratios[i]*100.0:.0f}%"
            color = cm.tab20(prune_ratios[i])
            ls = "-"
        else:
            continue
        legends_filtered.append(legend)
        colors_filtered.append(color)
        ls_filtered.append(ls)

    # convert data into the desired data format for the grapher and filter
    # (num_rep, num_y, num_rep, num_lines)
    diff = np.stack((diff_mean.T - diff_std.T, diff_mean.T + diff_std.T))
    diff = diff[:, :, None, np.logical_or(filtered_pr, filtered_ref)]

    # same for eps
    eps_values = np.broadcast_to(eps_values[None, :, None, None], diff.shape)

    grapher_diff = util.grapher.Grapher(
        x_values=eps_values,
        y_values=diff,
        folder=plots_dir,
        file_name=get_fig_name(title, plots_tag),
        ref_idx=None,
        x_min=-10.0,
        x_max=10.0,
        legend=legends_filtered,
        colors=colors_filtered,
        xlabel=f"{noise_type} noise level".capitalize(),
        ylabel=ylabel,
        title=title,
        linestyles=ls_filtered,
    )
    img_diff = grapher_diff.graph(percentage_y=percentage_y,
                                  remove_outlier=False,
                                  store=False)

    # customize legend
    img_diff.gca().legend(
        legends_filtered,
        bbox_to_anchor=(1.05, 1),
        loc="upper left",
        ncol=1,
        borderaxespad=0,
    )

    # store it in the end
    grapher_diff.store_plot()

    return grapher_diff
예제 #32
0
def stacked_bar_plot(data, labels, legend=None, table_header=None, title=None,
                     ax_names=None, normalize=False, normalize_factor=None,
                     width=0.8):
    """Creates a stacked bar plot.

    Parameters
    ----------
    data : numpy.array
        Multi-dimensional array.
    labels : list
        List of xtick labels. Should have the same length as `data`.
    title : str
        Title of the plot.
    table_header : list
        List with the header of the table object. Each element represents
        a column.
    title : str
        Title of the plot.
    ax_names : list
        List with the labels for the x-axis (first element) and y-axis
        (second element).
    normalize : bool
        If True, values of the `data` array will be normalized by the
        `normalize_factor`
    normalize_factor : int or float
        Number used to normalize values of the `data` array.

    Returns
    -------
    fig : matplotlib.Figure
        Figure object of the plot.
    lgd : matplotlib.Legend
        Legend object of the plot.
    table : list
        Table data in list format. Each item in the list corresponds to a
        table row.
    """

    plt.rcParams["figure.figsize"] = (8, 6)

    if normalize:

        data_original = np.copy(data)
        data = np.array([[y / normalize_factor for y in x] for x in data])

    if len(labels) > 10:
        plt.rcParams["figure.figsize"] = (len(labels) / 3, 6)
    else:
        plt.rcParams["figure.figsize"] = (8, 6)

    # Use ggpot style
    plt.style.use("ggplot")

    fig, ax = plt.subplots()

    # Get bottom positions for stacked bar
    bottoms = np.cumsum(np.vstack((np.zeros(data.shape[1]), data)),
                        axis=0)[:-1]

    # Get x positions
    xpos = [x + (width / 2) for x in range(len(labels))]

    for c, d in enumerate(data):

        if len(data) <= 10:
            c1 = c if c < 9 else c - 10
            clr = cm.tab10(c1, 1)
        else:
            c1 = c if c < 19 else c - 20
            clr = cm.tab20(c1, 1)

        if legend:
            current_lgd = legend[c]
        else:
            current_lgd = None

        if c == 0:
            bplot = ax.bar(xpos, d, width, color=clr, label=current_lgd,
                           alpha=.9)
        else:
            bplot = ax.bar(xpos, d, width, color=clr, label=current_lgd,
                           alpha=.9, bottom=bottoms[c])

    # Set x labels
    plt.xticks([x + (width / 2) for x in xpos], labels, ha="right",
               rotation=45)

    # Set legend
    if legend:
        if len(legend) <= 4:
            cols = 1
        else:
            cols = len(legend) if len(legend) < 3 else 3
        borderpad = cols * -6
        lgd = plt.legend(loc=7, fancybox=True,
                         shadow=True, framealpha=.8, ncol=cols,
                         borderaxespad=borderpad)
    else:
        lgd = None

    # Generate table structure
    if table_header:
        table = [table_header]
    else:
        table = []
    if normalize:
        for i, lbl in enumerate(labels):
            table.append([lbl] + list(chain.from_iterable((int(x[i]), y[i])
                                      for x, y in zip(*[data_original, data]))))
    else:
        for i, lbl in enumerate(labels):
            table.append([lbl] + [x[i] for x in data])

    return fig, lgd, table