def layout(self, ax):
     if not self.is_leaf:
         [child.layout(ax) for child in self.contents]
     
     plt.box('off')
     self.set_width(ax)
     self.set_height(ax)
     ax.clear()
예제 #2
0
 def plot(label):
     if not GRAPHS:
         return
     for i, f in enumerate(c.codebook):
         plt.plot([i + 1] * len(f), 'k-', lw=0.5, alpha=0.5, aa=True)
         plt.plot(i + 1 + f, 'k-', lw=1, alpha=0.8, aa=True)
     o = rng.randint(0, len(train) - width - 1)
     plt.plot(train[o:o+width], 'r-', aa=True)
     plt.gca().set_yticks([])
     plt.box(False)
     plt.savefig(os.path.join(GRAPHS, '%s-w%03d-c%03d-%05d-basis.png' % (label, width, codebook, seq)))
     plt.clf()
예제 #3
0
        def plot_correlation(self, other_interface, lines=False, bounds=None):
            """ Plot this interface's view on the underlying product vs
            another interface's view.

            Each vulnerability will be represented as a point, with the x coordinate
            representing the likelihood that this interface will discover the
            vulnerability in the next round, and the y coordinate representing
            the likelihood that the other interface will discover it this round.

            This plot is helpful for understanding the discovery correlation that
            arises with learning.

            Parameters
            ----------
            other_interface: Interface object
               The interface object must be built upon the same underlying 'product'
               as the present interface

            lines: True/False
               If true, will overlay horizontal and vertical lines representing the
               mean discovery profile for each actor, and label them with the mean
               value.

            bounds: None or float
               If float will set the x and y limits to this value.

            TODO
            ----
            It might be a good idea to make the bounds an x/y tuple if we want to
            initialize the interfaces with different `max_area` values.

            """
            plt.plot(self.circles.area, other_interface.circles.area, '.', alpha=.1)

            if bounds == None:
                window_size = max(self.circles.area.max(), other_interface.circles.area.max())
            else:
                window_size = bounds

            if lines:
                y_mean = np.mean(other_interface.circles.area)
                x_mean = np.mean(self.circles.area)
                plt.hlines(y_mean, 0, window_size)
                plt.text(window_size, y_mean, 'mean=%f'%y_mean, ha='right', va='bottom')
                plt.vlines(x_mean, 0, window_size)
                plt.text(x_mean, window_size, 'mean=%f'%x_mean, rotation=90, ha='right', va='top')

            plt.xlim(0, window_size)
            plt.ylim(0, window_size)
            plt.box('off')
            plt.xlabel(self.name + ' likelihood of discovery', fontsize=14)
            plt.ylabel(other_interface.name + ' likelihood of discovery', fontsize=14)
예제 #4
0
파일: stroke.py 프로젝트: ygidtu/pysashimi
def plot_stroke(region: SpliceRegion, font_size: float = 5):
    strokes = sorted(region.stroke, key=lambda x: [x.start, x.end])

    for i, stroke in enumerate(strokes):
        pylab.hlines(y=i,
                     xmin=stroke.start,
                     xmax=stroke.end,
                     color=stroke.color,
                     lw=2)
        pylab.text(stroke.center - len(stroke.label) / 2,
                   i + .2,
                   stroke.label,
                   fontsize=font_size)

    pylab.xlim(left=0, right=len(region))
    pylab.ylim(bottom=-1, top=len(strokes))
    pylab.box(on=False)
    pylab.xticks([])
    pylab.yticks([])
예제 #5
0
    def export(self, filename, limits=None):
        import matplotlib.pylab as plt
        plt.style.use('dark_background')
        if limits is None:
            xmin, xmax, ymin, ymax = self.get_limits()
        else:
            xmin, xmax, ymin, ymax = limits
        for x in range(xmin,xmax+1):
            plt.plot([x,x],[ymin,ymax],"w-")
        for y in range(ymin,ymax+1):
            plt.plot([xmin,xmax],[y,y],"w-")
        for x,y in self.alive:
            if xmin <= x < xmax and ymin <= y < ymax:
                plt.fill([x,x+1,x+1,x,x],[y,y,y+1,y+1,y],"w")

        plt.tick_params(axis='x',which='both',bottom='off',top='off',labelbottom='off')
        plt.tick_params(axis='y',which='both',left='off',right='off',labelleft='off')

        plt.axis("equal")
        plt.box("off")
        plt.savefig(filename)
        plt.clf()
예제 #6
0
def generate_MAPE_3D(loop_num, pred_time):
    if pred_time == 5:
        temp = pd.read_csv(
            r'D:\Users\yyh\Pycharm_workspace\CNN_response_simulation\data\CNN\{}min\l'
            r'oop{}_res_error.csv'.format(pred_time, loop_num))
    else:
        temp = pd.read_csv(
            r'D:\Users\yyh\Pycharm_workspace\CNN_response_simulation\data\CNN\{}min\l'
            r'oop{}_res_error{}.csv'.format(pred_time, loop_num, pred_time))
    mape_pd = pd.DataFrame(temp.MAPE.values.reshape(10, 10),
                           index=[
                               'space4', 'space8', 'space12', 'space16',
                               'space20', 'space24', 'space28', 'space32',
                               'space36', 'space40'
                           ],
                           columns=[
                               'time_lag4', 'time_lag8', 'time_lag12',
                               'time_lag16', 'time_lag20', 'time_lag24',
                               'time_lag28', 'time_lag32', 'time_lag36',
                               'time_lag40'
                           ])
    mae_pd = pd.DataFrame(temp.MAE.values.reshape(10, 10),
                          index=[
                              'space4', 'space8', 'space12', 'space16',
                              'space20', 'space24', 'space28', 'space32',
                              'space36', 'space40'
                          ],
                          columns=[
                              'time_lag4', 'time_lag8', 'time_lag12',
                              'time_lag16', 'time_lag20', 'time_lag24',
                              'time_lag28', 'time_lag32', 'time_lag36',
                              'time_lag40'
                          ])
    fig = plt.figure(figsize=(8, 6))
    ax = Axes3D(fig)
    X = np.arange(10)
    Y = np.arange(10)
    X, Y = np.meshgrid(X, Y)
    Z = np.array(mape_pd)[X, Y]
    plt.rcParams['savefig.dpi'] = 100  # 图片像素
    plt.rcParams['figure.dpi'] = 100  # 分辨率
    ax.plot_surface((X + 1) * 4, (Y + 1) * 4,
                    Z,
                    rstride=1,
                    cstride=1,
                    cmap='rainbow')
    ax.set_title("NO.{} Loop Detector {} min Traffic Flow Prediction".format(
        loop_num, pred_time),
                 fontsize=16)
    ax.set_xlabel("Loop Detector Number", fontsize=16)
    ax.set_ylabel("Observation Time Lags", fontsize=16)
    ax.set_zlabel("Mean Absolute Percentage Error(MAPE)", fontsize=16)
    ax.xaxis.set_tick_params(labelsize=12)
    ax.yaxis.set_tick_params(labelsize=12)
    ax.zaxis.set_tick_params(labelsize=12)
    plt.box()
    plt.show()

    fig = plt.figure(figsize=(8, 6))
    ax = Axes3D(fig)
    X = np.arange(10)
    Y = np.arange(10)
    X, Y = np.meshgrid(X, Y)
    Z = np.array(mae_pd)[X, Y]
    plt.rcParams['savefig.dpi'] = 100  # 图片像素
    plt.rcParams['figure.dpi'] = 100  # 分辨率
    ax.plot_surface((X + 1) * 4, (Y + 1) * 4,
                    Z,
                    rstride=1,
                    cstride=1,
                    cmap='rainbow')
    ax.set_title("NO.{} Loop Detector {} min Traffic Flow Prediction".format(
        loop_num, pred_time),
                 fontsize=16)
    ax.set_xlabel("Loop Detector Number", fontsize=16)
    ax.set_ylabel("Observation Time Lags", fontsize=16)
    ax.set_zlabel("Mean Absolute Error(MAE)", fontsize=16)
    ax.xaxis.set_tick_params(labelsize=12)
    ax.yaxis.set_tick_params(labelsize=12)
    ax.zaxis.set_tick_params(labelsize=12)
    plt.box()
    plt.show()

    ax = plt.subplot()
    mape_pd.plot(ax=ax)
    ax.xaxis.set_tick_params(rotation=30, labelsize=12)
    plt.show()

    ax = plt.subplot()
    mape_pd.T.plot(ax=ax)
    ax.xaxis.set_tick_params(rotation=30, labelsize=12)
    plt.show()
예제 #7
0
def x():

    dithering = False
    #% from test_lor3c2
    #% 2) down sampling changes SNR
    tau_s = 25./1.
    std_s = 3.53
    tau_n = 12./1.
    std_n = 5.55
    methid = 0
    #%NTA=2.^[4:5];
    #%NTA=2.^[4:8,9];

    #NTA = np.power(2, np.array(np.hstack((np.arange(4, 7), 8, 10, 12))))
    #NTA = np.power(2, np.array([4, 6, 8])) #12
    NTA = np.power(2, np.array([4])) #12
    nt_generate = np.max(NTA)
    b_msec = 7.  #3,5,1
    fs_Hz = 1000.
    siglen_msec = 10000.
    #%kerlenratio=5;
    siglen_msec = math.floor(siglen_msec / b_msec)* b_msec
    #%uses fs_Hz, and then downsamples. The downsampling is not quite correct.
    downsample_b = int(b_msec/1000. * fs_Hz + 0.00001)
    #print downsample_b #7
    assert downsample_b == 7
    #%happens to be equal to b, if fs=1000
    if False:
        r2_raw = makelorresponse(tau_s, std_s, tau_n, std_n, nt_generate, siglen_msec, fs_Hz, [], downsample_b)
    else:
        #test only
        r2_raw=np.random.randn(nt_generate, int((siglen_msec / b_msec))) #temporary
    if False:
        print    r2_raw.shape #1428x256
        print nt_generate
        print nt_generate
        print nt_generate
    del nt_generate
    
    siglen_b = (siglen_msec / b_msec)
    del siglen_msec
    #%M=4;
    #%M=8;
    #%M=4;
    M = 5
    #%M=8;
    #%M=15;
    #%M=3;
    [resp_dig, dithinfo1] = discr(r2_raw, M)
    #%not dithered
    #assert is_any_int_type(r2_raw[0,0])
    #r2_raw ois float

    [resp_dith, dithinfo2] = dith_unsure(r2_raw, (dithinfo1['centres']))
    #%not dithered
    
    resp_dig = resp_dith
    #del dithinfo

    resp_dig = resp_dig-1
    assert is_any_int_type(resp_dig[0,0])
    resp_cont = r2_raw
    sz=resp_cont.shape
    #print sz #1428 * 256
    resp_cont=resp_cont.flatten(1)
    resp_cont = resp_cont-np.mean(resp_cont.flatten(1))
    #%resp_cont=resp_cont/std(resp_cont(:))/2;
    #%resp_cont=resp_cont/std(resp_cont(:))/2*1.5*4*4;
    #resp_cont = matdiv(np.dot(matdiv(resp_cont, np.std(resp_cont.flatten(1)))/2.*1., M), M-1.)
    #resp_cont=resp_cont/std(resp_cont(:))/2*1*M/(M-1);
    resp_cont =  resp_cont / np.std(resp_cont.flatten()) /2.* M / (M-1.)
    #% [-1,+1]
    resp_cont = (resp_cont+1.)/2.
    #% [0,1]
    resp_cont = resp_cont * (M-1.)
    if 1:
        resp_cont[(resp_cont<0.)] = 0
        resp_cont[(resp_cont > M-1.)] = M-1
    resp_cont = resp_cont.reshape(sz)
    



    #print "HERE"
    #exit(0)
    
    #%clear r2_raw;
    #% analytical formula
    #%fs_Hz is used for Nyquist frequency
    #%ami=analytical_lor(tau_s,std_s, tau_n, std_n, fs_Hz, 0.0001);
    #%ami_perbin=mi/fs_Hz*b_msec;
    #% %WHY?!
    #%a=analytical_lor(tau_s/b_msec,std_s, tau_n/b_msec, std_n, fs_Hz/b_msec, 0.0001);
    #%ami_perbin=a / (fs_Hz/b_msec);
    #% (tau_s_msec,  sigma_s,  tau_n_msec,  sigma_n, fs_Hz, d_freq)
    #%Ia_bits_per_sec
    #% Ia_bps --> ami_bps = analytical mi
    ami_bps = analytical_lor(tau_s, std_s, tau_n, std_n, (1./b_msec/1000.), 0.0001)
    #%ami_perbin=ami_bps* (b_msec/1000);
    #% 1/ (fs_Hz/b_msec) = b_msec / fs = above (bcoz, fs=1000)
    #% previous: 0.2362
    #% new: 2.4512e-04
    binlen_sec = b_msec/1000.
    #%used later for normalization
    
    #del a
    del b_msec

    #%clearvars -except   resp_cont resp_dig M NTA binlen_sec siglen_b tau_s tau_n ami_bps ...
    #%    r2_raw std_* methid;
    don_plot_this = False
    #%size(respcut_dig)
    if don_plot_this:
            #%%
            #%mean(mean(resp_dig,1))
            [c, d] = plt.xcorr((matdiv(np.mean(resp_dig, 1.)-1.-(M-1.)/2.+1., M)*2.), 'unbiased')
            plt.figure(10.)
            plt.clf
            #% / sqrt(std_s^2+std_n^2)
            h = plt.plot((np.dot(d, binlen_sec)*1000.), matdiv(c, std_s**2.), 'k.-')
            set(h, 'linewidth', 2.)
            set(h, 'displayname', 'signal')
            plt.xlim((np.array(np.hstack((-1., 1.)))*100.*1.))
            #%set(gca,'xtick',-200:10:200);
            plt.title('\\tau_s, \\tau_n = %g,%g [msec]'%( tau_s, tau_n))
            #nn = matcompat.size(resp_dig, 1.)
            nn = resp_dig.shape[1-1]
            c1 = 0.
            for tri in np.arange(1., (nn)+1):
                #%mean(resp_dig(tri,:))
                #MISSING CODE!!!
                [c,d]=np.xcorr((resp_dig[tri,:]-1-(M-1)/2+1)/M*2,'unbiased');
                c1=c1+c/nn;
                pass

            del nn
            plt.hold(on)
            h = plt.plot((np.dot(d, binlen_sec)*1000.), matdiv(c1, std_n**2.), 'r.-')
            set(h, 'linewidth', 2.)
            set(h, 'displayname', 'noise')
            tt = np.dot(d, binlen_sec)
            h = plt.plot((tt*1000.), np.dot(binlen_sec, np.exp(matdiv(-np.abs(tt), tau_s/1000.))), 'k--')
            set(h, 'displayname', 'signal analytical')
            h = plt.plot((tt*1000.), np.dot(binlen_sec, np.exp(matdiv(-np.abs(tt), tau_n/1000.))), 'r--')
            set(h, 'displayname', 'noise analytical')
            h = plt.legend('show')
            set(h, 'box', 'off')
            set(h, 'location', 'NorthWest')
            plt.xlabel('Time [msec]')
            plt.title('cross correlation')
            #% my_save_png('test_lor4f1---signal-xcorr-v12-temp',{},[4,3]);
            
    begintime = time.time()
    #tic=time.time()

    #%estims=cell(4,4);
    #%LARR=[1,4,8,10];
    #%LARR=[1,2:2:10];
    #%LARR=[1,2:2:8];
    #%LARR=[1,2,3,6];
    #%LARR=[1,2,3,6,7];
    #%LARR=[1]; %
    #LARR = np.array(np.hstack((1., 3., 6., 7.)))
    #LARR = np.array(np.hstack((1+1, 3, 6, 7)))
    #LARR = [1+1, 3, 6, 7]
    LARR = [1,1+1, 4]
    MAX_L_DIRECT = np.Inf

    #estims_cell=[[],[],[],[]]
    #estims_cell=[[{}],[{}],[{}],[{}]]
    #estims_cell=[[{}]]
    estims_cell=[]
    #len(NTA)
    #print len(LARR),len(NTA), "******************"
    for i in range(0,len(LARR)):
            estims_cell.append([])
            for j in range(0,len(NTA)):
                print i,j
                estims_cell[i].append({})


    #%for nti=10:len(NTA)
    for nti in range(1, len(NTA)+1):
        nt = int(NTA[int(nti)-1])
        for li in range(1, len(LARR)+1):
            L = LARR[int(li)-1]
            #%lenL=floor(siglen_msec/b_msec/L)*L;
            #%lenL=floor(siglen_msec/L/b_msec)*L*b_msec;
            lenL = (int((siglen_b/ L)) * L)
            #%respcut=resp(:,1:lenL)';
            #%  respcut=resp(1:nt,1:lenL)';
            if dithering:
                respcut_cont = resp_cont[0:nt,0:lenL].conj().T


            #print "HERE"
            #exit(0)
            
            #print nt, max(NTA) #16,256
            #print resp_cont.shape  #1428x256
            #print resp_dig.shape
            #print "000"
            respcut_dig = resp_dig[0:nt,0:lenL].conj().T
            #% len x trials
            #%spk0 = zeros(1,LNum,size(signal,1), newlen );
            #%spk=reshape(respcut,  [1,L,size(resp)]);
            #% spk__=reshape(respcut,  1,L,lenL/L,[]);
            #%dithering = False
            if dithering:
                print "Using dithering"
                #spk__cont = np.reshape(respcut_cont, 1., L, matdiv(lenL, L), np.array([]))
                spk__cont = np.reshape(respcut_cont, [1, L, int(lenL/ L), -1])
                #% 1 x L x len x trials
                #spk2_cont = permute(spk__cont, np.array(np.hstack((1., 2., 4., 3.))))
                spk2_cont = spk__cont.transpose([1-1, 2-1, 4-1, 3-1]) #, np.array(np.hstack((1., 2., 4., 3.))))
                assert spk2_cont.shape == (1, L, nt, int(lenL/ L))

                #clear(spk__c)
                # #%slow
            else:
                print "Not using dithering"
            
            
            #% 1 x L x trials x len
            #print respcut_dig.shape, "*1"  #256 x 16  
            #print (lenL,L,nt), "*2" #1428.0, 1.0, 16
            spk__dig = np.reshape(respcut_dig, [1, L, int(lenL/ L), -1])
            spk2_dig = spk__dig.transpose([1-1, 2-1, 4-1, 3-1]) #permute(spk__dig, np.array(np.hstack((1., 2., 4., 3.))))
            assert spk2_dig.shape == (1, L, nt, int(lenL/ L))
            assert is_any_int_type(spk2_dig[0,0,0,0])

            del spk__dig

            #%slow
            
            assert type(nt) is int
            #nts = np.dot(np.ones(1., matdiv(lenL, L)), nt)
            #nts = np.ones([(lenL/ L)]) * nt
            nts = np.array([nt]*(lenL/ L), dtype=int) #np.ones([(lenL/ L)]) * nt
            #[nt]*int(lenL/ L)

            #print nts

            #assert type(nts[0]) is int
            #print type(nts), type(nts[0])
             

            
            print 'spk2: %dx%dx%dx%d ['%spk2_dig.shape
            if L<=MAX_L_DIRECT:
                ns = int(lenL/ L)
                #nta = np.arange(1., (ns)+1)*0.+nt
                nta = np.array([nt]*ns)
                if dithering:
                    #%[prs3,ps3]=my_prs_dith1(spk2, nta);
                    #%   [prs3_dith,ps3]=my_prs_dith1(spk2_c-1, nta);
                    #%[prs3_dith,ps3]=my_prs_dith1(spk2_cont-0, nta);
                    (prs_dith, ps_dith) = my_prs_dith1((spk2_cont-0.), nta)
                    #%    [prs3_dig,ps3]=my_prs(spk1, nta);
                    #%    sum(abs(prs3_dith(:)-prs3_dig(:)))
                    #%   if 0
                    #%       %[prs1,ps1]=my_prs(spk2, nta);
                    #%       [prs1,ps1]=my_prs(spk2_dig, nta);
                    #%   end
                    (prs_dig, ps_dig) = my_prs(spk2_dig, nta)
                    #print(repr((prs_dig, ps_dig)))
                    #%prs=prs3_dith;
                    #%ps=ps3;
                    fprintf('p')
                    hrs0_th = my_hrs(prs_dith, ps_dith, methid)
                    pr_dith = my_pr(prs_dith, ps_dith)
                    hr0_th = my_hr(pr_dith, methid)
                    #%hr1=hr(spk2_dig,nts,methid);
                    #%hrs1=hrs(spk2_dig,nts,methid);
                    hrs_dig = my_hrs(prs_dig, ps_dig, methid)
                    pr_dig = my_pr(prs_dig, ps_dig)
                    hr_dig = my_hr(pr_dig, methid)
                    #%[ my_hr(ps_dith, methid),my_hr(ps_dig, methid)]
                    #%m22= [...
                    #%    my_hr(pr_dith, methid),my_hr(pr_dig, methid);
                    #%    my_hrs(prs_dith,ps_dith, methid),my_hrs(prs_dig,ps_dig, methid)];
                    #%
                    vv1 = my_hr(pr_dith, methid)
                    my_hr(pr_dig, methid)
                    vv2 = my_hrs(prs_dith, ps_dith, methid)
                    my_hrs(prs_dig, ps_dig, methid)
                    m22 = vertcat(vv1, vv2)
                    #m22

                    #print li, nti
                    #%myminmax(spk2_dig(:))  % 1--4
                    #%myminmax(spk2_cont(:)) % 0--3
                    #estims.cell[int(li)-1,int(nti)-1]['hr_d'] = hr_dig
                    estims_cell[int(li)-1][int(nti)-1]['hr_d'] = hr_dig
                    #%hr1;
                    estims_cell[int(li)-1][int(nti)-1]['hrs_d'] = hrs_dig
                    #%hrs1;
                    estims_cell[int(li)-1][int(nti)-1]['hr_th'] = hr0_th
                    estims_cell[int(li)-1][int(nti)-1]['hrs_th'] = hrs0_th


                #print type(nts), type(nts[0])
                hr1_dig = hr(spk2_dig, nts, methid)
                hrs1_dig = hrs(spk2_dig, nts, methid)
                #print li, nti #1.0, 1.0
                #print int(li)-1,int(nti)-1
                #print estims_cell[int(li)-1][int(nti)-1]
                zz=estims_cell[int(li)-1][int(nti)-1]
                zz['hr_d'] = 1
                #print hr1_dig
                estims_cell[int(li)-1][int(nti)-1]['hr_d'] = hr1_dig
                #%hr1;
                estims_cell[int(li)-1][int(nti)-1]['hrs_d'] = hrs1_dig
                #%hrs1;
            
    
                if 1:

                    #print "***********"
                    #print nts
                    #%no dithering
                    estims_cell[int(li)-1][int(nti)-1]['hrs_sh'] = hrs_shuff(spk2_dig, nts, methid)

                    estims_cell[int(li)-1][int(nti)-1]['hrs_ind'] = hrsind(spk2_dig, nts, methid)
                    estims_cell[int(li)-1][int(nti)-1]['_xi'] = xi(spk2_dig, nts, methid)

                    #    
    
            else:
                estims_cell[int(li)-1][int(nti)-1]['_hr'] = 0.
                estims_cell[int(li)-1][int(nti)-1]['_hrs'] = 0.
                estims_cell[int(li)-1][int(nti)-1]['hrs_sh'] = 0.
                estims_cell[int(li)-1][int(nti)-1]['hrs_ind'] = 0.
                estims_cell[int(li)-1][int(nti)-1]['_xi'] = 0.
                
            
            #%we need: q-model in which , up to q0(=0) is shuffled: model=q, shuffle=0
            #%shuffle across:R (not S)
            #%if 1
            #print "HERE2"
            #exit(0)


            q = 1
            estims_cell[int(li)-1][int(nti)-1]['_hqrs'] = hqrs(spk2_dig, nts, q, methid)
            estims_cell[int(li)-1][int(nti)-1]['_hqr'] = hqr(spk2_dig, nts, q, methid)
            estims_cell[int(li)-1][int(nti)-1]['_xiq'] = xiq(spk2_dig, nts, q, methid)
            #%spk_q=q_shuffle(spk,nts,q);

            spk_q = q_shuffle(spk2_dig, nts, q)
            #%estims{li,nti}['hrs_q']=hrs(spk_q,nts,methid);
            estims_cell[int(li)-1][int(nti)-1]['hrs_q_sh'] = hrs(spk_q, nts, methid)
            #%estims{li,nti}.hqrs0=hqrs(spk,nts,q,meth);
            #%estims{li,nti}.hrss0=hrs_shuff(spk,nts,meth);
            #%estims{li,nti}.spks=q_shuffle(spk,nts,q);
            #%estims{li,nti}.hqrss0=hrs(spks,nts,meth);
            #%estims{li,nti}.hr0=hr(spk,nts,meth);
            #%estims{li,nti}.hqr0=hqr(spk,nts,q,meth);
            #%estims{li,nti}.hrs0=hrs(spk,nts,meth);
            #%estims{li,nti}.hrsi0=hrsind(spk,nmiu,meth);
            #%estims{li,nti}.xiq0=xiq(spk,nmiu,q,meth);
            #%estims{li,nti}.xi0=xi(spk,nmiu,meth);
            #%end
            estims_cell[int(li)-1][int(nti)-1]['L'] = L
            estims_cell[int(li)-1][int(nti)-1]['len'] = lenL/ L
            estims_cell[int(li)-1][int(nti)-1]['nt'] = nt
            print(']'),
            #print' (%g sec.)'%(np.floor(toc),)
            print('\n')
            #%estims{li,nti}

        #Takes long up to here. Cleaned up.
        #print "HERE"
        #exit(0)

        #%% -
        #%a2d1_th=[];
        #a2d1_d = [] #np.array([])
        a2d1_d = [] #np.array([])
        a2d2_qd = [] #np.array([])
        a2d3_dsh = [] #np.array([])
        nta1d = [] #np.array([])
        for ntj in range(1, nti+1):
            a2d1_d.append([])
            assert len(a2d1_d)-1 == ntj-1 #assert index number
            a2d3_dsh.append([])
            assert len(a2d3_dsh)-1 == ntj-1 #assert index number
            a2d2_qd.append([])
            assert len(a2d2_qd)-1 == ntj-1 #assert index number

            #ee=estims_cell[li-1][ntj-1]
            #nta1d.append( ee['nt'] )

            nta1d.append( estims_cell[0][ntj-1]['nt'] )
            assert len(nta1d)-1 == ntj-1

            for li in range(1, len(LARR)+1 ):
                #%L=LARR(li);
                #ee=estims{li,ntj};
                print estims_cell  #list of list of dict
                #print estims_cell.shape
                print estims_cell[0][0]
                print li,ntj
                ee=estims_cell[li-1][ntj-1]
                #%if  L<=MAX_L_DIRECT
                if 'hrs_d' in ee: #haskey(ee, 'hrs_d'):
                    #% mi1=(ee['_hr']-ee['_hrs'] -ee['hrs_ind'] + ee['hrs_sh']  )/ee['L'];
                    #%mi1=(ee['hr_th']-ee['hrs_th']  )/ee['L'];
                    #%mi1=(ee['hr_d']-ee['hrs_d'] -ee['hrs_ind'] + ee['hrs_sh']  )/ee['L'];
                    #%mi2=(ee['hr_d']-ee['hrs_d'])/ee['L'];
                    assert not type(ee['hr_d']) is list
                    #mi1_plugin = matdiv(ee['hr_d']-ee['hrs_d'], ee['L'])
                    #mi1_sh = matdiv(ee['hr_d']-ee['hrs_d']-ee['hrs_ind']+ee['hrs_sh'], ee['L'])
                    mi1_plugin = (ee['hr_d']-ee['hrs_d']) / ee['L']
                    mi1_sh = (ee['hr_d']-ee['hrs_d']-ee['hrs_ind']+ee['hrs_sh']) /  ee['L']
                else:
                    mi1_plugin = 0.
                    #%NaN;
                    mi1_sh = 0.
                    
                
                print mi1_plugin
                #%a2d1_th(ntj,li)=mi1;
                #a2d1_d[int(ntj)-1,int(li)-1] = mi1_plugin
                a2d1_d[ntj-1].append(mi1_plugin)
                assert len(a2d1_d[ntj-1])==li-1+1
                #a2d3_dsh[int(ntj)-1,int(li)-1] = mi1_sh
                a2d3_dsh[ntj-1].append( mi1_sh )
                assert len(a2d3_dsh[ntj-1])==li-1+1
                
                #%mi2=(ee['_xi']-ee['hrs_ind'])/ee['L'];
                #%mi2=(ee['_hqr']-ee['_hrs'] -ee['_hqrs'] + ee['hrs_q']  )/ee['L'];
                #%mi2=(ee['_xiq']-ee['hrs_q'] +ee['hrs_sh'] - ee['hrs_ind']  )/ee['L']; %DID NOT
                #%WORK
                #%mi2=(ee['_hqr']-ee['hrs_q'])/ee['L']; %+ee['hrs_sh'] - ee['hrs_ind']  )/ee['L'];
                #%slightly biased (uses hrs_q-sh)
                #% ************ CHECK THIS *************
                #mi2 = matdiv(ee['_xiq']-ee['_hqrs'], ee['L'])
                mi2 = (ee['_xiq']-ee['_hqrs']) /  ee['L']
                #%mi2=(ee['_hqr']-ee['_hqrs'])/ee['L'];
                #% ... %+ee['hrs_sh'] - ee['hrs_ind']  )/ee['L'];
                #%mi2=(ee['_xi']-ee['hrs_ind'])/ee['L'];
                #%mi2=(ee.hr1-ee.hrs1)/ee['L'];
                #%mi2=(ee['hr_d']-ee['hrs_d'])/ee['L'];
                #a2d2_qd[int(ntj)-1,int(li)-1] = mi2
                a2d2_qd[ntj-1].append( mi2 )
                assert len(a2d2_qd[ntj-1]) -1 == li-1
                #%mi3=(ee['_hqr']-ee['hrs_q'])/ee['L']; %+ee['hrs_sh'] - ee['hrs_ind']  )/ee['L'];
                #%mi3=(ee['_xiq']-ee['hrs_q'])/ee['L'];
                #nta1d[int(ntj)-1] = ee['nt']
                #nta1d.append( ee['nt'] )
                #print len(nta1d),ntj
                #print nta1d
                #assert len(nta1d)-1 == ntj-1
                #assert len(nta1d)-1 == ntj-1  ##COMPILE_TIME:CHECK_PATTERN: MATCH LAST_INDEX(nta1d) == ntj-1   #PLANGNOTE
                assert nta1d[ntj-1] == ee['nt']
                #%    end
                #%end
               
        if False:
            plt.figure(5.)
            plt.clf
            ax = np.array([])
            #%ax(1)=subplot(1,3,1:2);
            ax[0] = plt.subplot(2., 3., 1.)
            plt.hold(on)
            #%box on;
            lha = np.array([])
            #%h = plot(nta1d,a2d1_th/binlen_sec);
            h = plt.plot(nta1d, matdiv(a2d1_d, binlen_sec))
            #%if isempty(h)
            #%    h=plot(0,0,'w.');
            #%end
            set(h, 'linewidth', 2.)
            set(h, 'Displayname', 'L-direct')
            #%L-bin
            #%set(h,'Displayname','dithered'); %L-bin
            #%lha(1)=h(1);
            plt.ylabel('Information rate [bits/sec]')
            #% set(gca,'xscale','log');
            plt.title('direct plug-in')
            #%MARKOV
            ax[2] = plt.subplot(2., 3., 3.)
            h = plt.plot(nta1d, matdiv(a2d2_qd, binlen_sec), '-')
            #%if isempty(h)
            #%    h=plot(0,0,'w.');
            #%end
            #%plot(nta1d,a2d3,':');
            set(h, 'Displayname', 'q-Markov')
            #%L-bin
            #%set(h,'Displayname','L-direct');
            set(h, 'linewidth', 2.)
            #%lha(2)=h(1);
            #%  q=NaN;
            #%title('q=1');
            #%plot((ee['nt']),mi,'.');
            #%text((ee['nt']),mi, sprintf(' L=%d',ee['L']));
            #%   set(gca,'xscale','log');
            #%end
            #%  end
            #%hold on;
            #%plot(nta1d, ami_perbin + nta1d*0, 'k:');
            #%nta1d+100
            #%     nta1d_temp=myminmax(nta1d);  nta1d_temp(end)=nta1d(end)+100; nta1d_temp(1)=90;
            #%     h=plot(nta1d_temp, ami_bps + nta1d_temp*0, 'k-.');
            #%     set(h,'Displayname','analytical');
            #%lha(3)=h;
            plt.hold(on)
            #%    plot(nta1d, nta1d*0, 'k:');
            #%    xlabel('Trials');
            #%ylabel('Information [bits/bin]');
            #%xlim([10,(max(NTA)+100)*1.1]);
            #%xlim([min(NTA)-2,(max(NTA)+100)*1.1]);
            plt.title('q-Markov')
            lta=[]
            for li in np.arange(1., (len(LARR))+1):
                #%   text(nta1d(1),a2d1_d(1,li)/binlen_sec, sprintf(' L=%d',LARR(li)));
                lta[li]=' L=%d'%(LARR(li),);
                pass
 
            if False:
                lh = plt.legend(lta)
                set(lh, 'box', 'off')
                set(lh, 'Location', 'SouthWest')
                set(lh, 'FontSize', 7.)
            #% PANEL 2
            #%    h=legend(lha,'location','SouthEast'); set(h,'box','off');
            ax[1] = plt.subplot(2., 3., 2.)
            h = plt.plot(nta1d, matdiv(a2d3_dsh, binlen_sec), '-')
            #%if isempty(h)
            #%    h=plot(0,0,'w.');
            #%end
            set(h, 'Displayname', 'direct-sh')
            #%L-bin
            lha[2] = h[0]
            #% set(gca,'xscale','log');
            set(h, 'linewidth', 2.)
            plt.title('direct-sh')
            for pani in np.arange(1., 4.0):
                set(plt.gcf, 'CurrentAxes', ax[int(pani)-1])
                plt.hold(on)
                nta1d_temp = myminmax(nta1d)
                nta1d_temp[int(0)-1] = nta1d[int(0)-1]+100.
                nta1d_temp[0] = 20.
                #%90;
                #%h=plot(ax(pani),
                h = plt.plot(nta1d_temp, (ami_bps+nta1d_temp*0.), 'k--')
                set(h, 'Displayname', 'analytical')
                set(plt.gca, 'xscale', 'log')
                plt.plot(nta1d, (nta1d*0.), 'k:')
                plt.xlabel('Trials')
                set(plt.gca, 'xtick', np.unique(np.array(np.hstack(((2 ** np.array(np.hstack((5., 7., 9., np.log2(matcompat.max(NTA)))))))))))
                plt.ylim(np.array(np.hstack((-50., 100.))))
                plt.box(off)
                
            linkaxes(ax, 'y')
            linkaxes(ax, 'x')
            nta1d_temp = myminmax(nta1d)
            nta1d_temp[int(0)-1] = nta1d[int(0)-1]+1000.
            nta1d_temp[0] = nta1d_temp[0]-4.
            plt.xlim(nta1d_temp)
            #%a=[a2d3_dsh(:)/binlen_sec ; a2d1_d(:)/binlen_sec ;  a2d2_qd(:)/binlen_sec ];
            a = vertcat(matdiv(a2d3_dsh.flatten(1), binlen_sec), matdiv(a2d1_d.flatten(1), binlen_sec), matdiv(a2d2_qd.flatten(1), binlen_sec))
            #%ylim(myminmax(a)*1.5);
            #%ax(4)=
            plt.subplot(2., 3., 4.)
            plt.hold(on)
            WARR = np.dot(LARR, binlen_sec)*1000.
            #%bar(LARR,a2d2_qd(end,:) /binlen_sec);
            plt.plot(WARR, matdiv(a2d2_qd[int(0)-1,:], binlen_sec), 'ko-', 'displayname', 'q-Markov')
            plt.plot(WARR, matdiv(a2d1_d[int(0)-1,:], binlen_sec), 'r--', 'displayname', 'direct-sh')
            LA2 = np.array(np.hstack((WARR, WARR[int(0)-1]+1.)))
            h = plt.plot(LA2, (ami_bps+LA2*0.), 'k--')
            set(h, 'Displayname', 'analytical')
            #%xlabel('L');
            plt.xlabel('W [msec]')
            lh = plt.legend('show')
            set(lh, 'box', 'off')
            set(lh, 'Location', 'NorthEast')
            #%set(lh,'FontSize',7);
            #%linkaxes(ax, 'y')
            #%ylabel('Information rate [bits/sec]');
            plt.xlim(np.array(np.hstack((0.1, matcompat.max(LA2)+1.))))
            plt.ylabel('Information rate [bits/sec]')
            title_on_top2(sprintf('q=%d, DITHER, QE=%d, W=%g[ms]        \\tau_s,\\tau_n=%g,%g [ms]', q, methid, (1000.*binlen_sec), tau_s, tau_n), 0.)
            #%darbareye hichi be ghatiyat nemirese. va in kheili Pernicious, va
            #%PErvesaive, va daaem va hart dafe hastesh. bayad MM javab bekhad ta
            #%begam.
            #%save test_lor4???_all
            #% my_save_png('test_lor4f1---dither-v2',{},[7,5]);
        time.sleep(0.2)
        print("sleep")
        
    finishtime = time.time() #now()
    #toc=time.time()
    print('.')


    print estims_cell
    maxc = 0
    collect_keys={}
    for i in range(0,len(estims_cell)):
        for j in range(0,len(estims_cell[i])):
            for k in estims_cell[i][j]:
               collect_keys[k]=0
               if maxc < j+1:
                    maxc = j+1
    print collect_keys.keys()
    print
    ka={}
    for k in collect_keys:
       ka[k]=np.zeros((len(estims_cell),maxc))

    for i in range(0,len(estims_cell)):
        for j in range(0,len(estims_cell[i])):
            for k in collect_keys:
               ka[k][i,j]=estims_cell[i][j][k]
    for k in collect_keys:
        print k+":",
        print ka[k].T
    return

    dont_plot_this = False
    #%% - plots
    if dont_plot_this:
        DOUBLEPANEL = 0.
        plt.figure(6.)
        plt.clf
        if DOUBLEPANEL:
            plt.subplot(1., 2., 1.)
        
        
        #plt.hold(on)
        #%box on;
        styc = 'rgbmkckkk'
        for li in np.arange(1., (len(LARR))+1):
            lnt = np.array([])
            mi = np.array([])
            for ntj in np.arange(1., (nti)+1):
                ee = estims_cell[int(li)-1,int(ntj)-1]
                #%mi(ntj)=(ee['_hr']-ee['_hrs'])/ee['L'];
                #%mi(ntj)=(ee['_hr']-ee['_hrs'] -ee['hrs_ind'] + ee['hrs_sh']  )/ee['L'];
                #%mi(ntj)=(ee.hr1-ee.hrs1 -ee['hrs_ind'] + ee['hrs_sh']  )/ee['L'];
                mi[int(ntj)-1] = matdiv(ee['hr_d']-ee['hrs_d']-ee['hrs_ind']+ee['hrs_sh'], ee['L'])
                #%lnt(ntj)=log2(ee['nt']);
                lnt[int(ntj)-1] = matdiv(1., ee['nt'])
                plt.plot(lnt[int(ntj)-1], mi[int(ntj)-1], np.array(np.hstack((styc[int(li)-1], 'o'))))
                plt.text(lnt[int(ntj)-1], mi[int(ntj)-1], sprintf(' L=%d', (ee['L'])))
                
            h = plt.plot(lnt, mi, np.array(np.hstack((styc[int(li)-1], '--'))))
            set(h, 'LineWidth', 2.)
            h1[int(li)-1] = h
            
        #%set(gca,'xlim',[4,1+log2(max(NTA))]);
        ami_perbin = np.dot(ami_bps, binlen_sec)
        plt.plot(lnt, (ami_perbin+lnt*0.), 'k--')
        plt.xlabel('log_2(trials)')
        plt.ylabel('information (bits)')
        ylim = plt.get(plt.gca, 'ylim')
        #%set(gca,'ylim',[0,ylim(2)]);
        #%set(gca,'ylim',[0,0.5]);
        plt.title('direct method')
        #%  legend(h1, cell_sprintf('L=%d',LARR));
    
    
    #%%
    #%prs_dig(1,:)=[];
    #%%
    plt.figure(4.)
    plt.clf
    plt.plot(resp_cont[0,:])
    #plt.hold(on)
    plt.plot((resp_dig[0,:]-0.), 'r')
    plt.figure(10.)
    plt.clf
    mypcolor(prs_dig[0:,199:220.])
    plt.title('trunc')
    plt.figure(11.)
    plt.clf
    mypcolor(prs_dith[:,199:220.])
    plt.title('dith')
    plt.figure(12.)
    plt.clf
    plt.hist(np.array(np.hstack((prs_dith.flatten(1), prs_dig.flatten(1)))))
    #%%
    plt.figure(13.)
    plt.clf
    a = resp_cont[0,:]
    b = resp_dig[0,:]-0.
    c = r2_raw[0,:]
    plt.plot(a, (b+np.dot(plt.randn(matcompat.size(b)), 0.1)), 'k.')
    mm = myminmax((c.flatten(1)+np.dot(plt.randn(matcompat.size(a.flatten(1))), 0.01)))
    cx = np.arange(mm[0], (mm[1])+(np.diff(mm)/40.), np.diff(mm)/40.)
    h1 = histc(c.flatten(1), cx)
    h1 = matdiv(h1, np.sum(h1))
    #% *mean(diff(cx))
    plt.figure(14.)
    plt.clf
    plt.subplot(1., 2., 1.)
    plt.plot(c, (a+np.dot(plt.randn(matcompat.size(a)), 0.05)), 'k.', 'markersize', 1.)
    plt.hold(on)
    plt.plot(cx, (h1*20.+0.5), 'r')
    plt.subplot(1., 2., 2.)
    plt.plot(c, (b+np.dot(plt.randn(matcompat.size(a)), 0.05)), 'k.', 'markersize', 1.)
    plt.hold(on)
    plt.plot(cx, (h1*20.+0.5), 'r')
    for i in np.arange(1., 3.0):
        plt.subplot(1., 2., i)
        for x0 in np.array(np.hstack((-4., 4.))):
            plt.plot((x0+np.array(np.hstack((0., 0.)))), np.array(np.hstack((0., M))), 'b--', 'linewidth', 1.)
            
        
    #% my_save_png('test_lor4e2-v13-meth=1',{},[5,4]);
    #%%
    #%close all
    plt.figure(101.)
    plt.clf
    plt.plot(pr_dig, 'displayname', 'discrete')
    plt.hold(on)
    plt.plot(pr_dith, 'r.-', 'displayname', 'dith')
    np.array(np.hstack((np.sum(pr_dith), np.sum(pr_dig))))
    plt.legend(show)

    return estims_cell
def plot_transcripts(tx_start,
                     transcripts,
                     graph_coords,
                     reverse_minus,
                     font_size,
                     show_gene=False,
                     distance_ratio=0.3):
    """
    [original description]
    draw the gene structure.

    [now]
    due to i changed the mrna class, therefore, this function need be modified

    :param tx_start: the very start of this plot
    :param graph_coords: numpy array, convert the coord of genome to the coord in this plot
    :param reverse_minus:
    :param transcripts: list of Transcript
    :param font_size: the font size of transcript label
    :param show_gene: Boolean value to decide whether to show gene id in this plot
    :param distance_ratio: distance between transcript label and transcript line
    """
    y_loc = 0
    exon_width = .3
    """
    @2018.12.26
    Maybe I'm too stupid for this, using 30% of total length of x axis as the gap between text with axis
    """
    distance = distance_ratio * (max(graph_coords) - min(graph_coords))

    # @2018.12.19
    # @2018.12.21
    # the API of SpliceRegion has changed, the transcripts here should be sorted

    for transcript in transcripts:
        # narrows = math.floor(narrows * (transcript.length / len(graphcoords)))

        # @2018.12.20 add transcript id, based on fixed coordinates
        if show_gene:
            pylab.text(x=-1 * distance,
                       y=y_loc + 0.15,
                       s=transcript.gene,
                       fontsize=font_size)

            pylab.text(x=-1 * distance,
                       y=y_loc - 0.25,
                       s=transcript.transcript,
                       fontsize=font_size)
        else:
            pylab.text(x=-1 * distance,
                       y=y_loc - 0.1,
                       s=transcript.transcript,
                       fontsize=font_size)

        strand = "+"
        # @2018.12.19
        # s and e is the start and end site of single exon
        for exon in transcript.exons:
            s, e, strand = exon.start, exon.end, exon.strand
            s = s - tx_start
            e = e - tx_start
            x = [
                graph_coords[s], graph_coords[e], graph_coords[e],
                graph_coords[s]
            ]
            y = [
                y_loc - exon_width / 2, y_loc - exon_width / 2,
                y_loc + exon_width / 2, y_loc + exon_width / 2
            ]
            pylab.fill(x, y, 'k', lw=.5, zorder=20)

        # @2018.12.21
        # change the intron range
        # Draw intron.
        intron_sites = [
            graph_coords[transcript.start - tx_start],
            graph_coords[transcript.end - tx_start]
        ]
        pylab.plot(intron_sites, [y_loc, y_loc], color='k', lw=0.5)

        # @2018.12.23 fix intron arrows issues
        # Draw intron arrows.
        max_ = graph_coords[transcript.end - tx_start]
        min_ = graph_coords[transcript.start - tx_start]
        length = max_ - min_
        narrows = math.ceil(length / max(graph_coords) * 50)

        spread = .2 * length / narrows

        for i in range(narrows):
            loc = float(i) * length / narrows + graph_coords[transcript.start -
                                                             tx_start]
            if strand == '+' or reverse_minus:
                x = [loc - spread, loc, loc - spread]
            else:
                x = [loc + spread, loc, loc + spread]
            y = [y_loc - exon_width / 5, y_loc, y_loc + exon_width / 5]
            pylab.plot(x, y, lw=.5, color='k')

        y_loc += 1

    pylab.xlim(0, max(graph_coords))
    pylab.ylim(-.5, len(transcripts) + .5)
    pylab.box(on=False)
    pylab.xticks([])
    pylab.yticks([])
# Define synapse filter (i.e. which filters by roi, type, confidence)
ellipsoid_synapses = synapse_criteria = SC(rois='EB', primary_only=True)
'''i.e. get only synapses are found in the ellipsoid body (EB)'''

# Get synapses (from neuron)
synapses = fetch_synapses(fanshapedNeurons, ellipsoid_synapses)

# Plot the synapse positions in a 2D projection
fig, ax = plt.subplots(1, 1)
fig.set_figwidth(7)
fig.set_figheight(8)
ax.scatter(synapses['x'], synapses['z'], s=3)
ax.invert_yaxis()
plt.grid()
plt.box(False)
plt.show()

# ------------------------------------------
# Synapse connections
# ------------------------------------------
''' Fetch all synapse-synapse connections from a set of neurons. Provide a NeuronCriteria for the source
or target neurons (or both) to filter the neurons of interest, and optionally filter the synapses themselves
via SynapseCriteria
'''
ellipsoid_conns = fetch_synapse_connections(
    source_criteria=fanshapedNeurons,
    target_criteria=None,
    synapse_criteria=ellipsoid_synapses)
# ellipsoid_conns.head()
예제 #10
0
            def draw_barchart(Time):
                df_frame = (df[df["date"].eq(Time)].sort_values(
                    by="counts", ascending=True).tail(num_of_elements))
                ax.clear()

                normal_colors = dict(
                    zip(df["country"].unique(), rgb_colors_opacity))
                dark_colors = dict(zip(df["country"].unique(),
                                       rgb_colors_dark))

                ax.barh(
                    df_frame["country"],
                    df_frame["counts"],
                    color=[normal_colors[x] for x in df_frame["country"]],
                    height=0.8,
                    edgecolor=([dark_colors[x] for x in df_frame["country"]]),
                    linewidth="6",
                )

                dx = float(df_frame["counts"].max()) / 200

                for i, (value, name) in enumerate(
                        zip(df_frame["counts"], df_frame["country"])):
                    ax.text(
                        value + dx,
                        i + (num_of_elements / 50),
                        "    " + name,
                        size=14,
                        weight="bold",
                        ha="left",
                        va="center",
                        fontdict={"fontname": "Trebuchet MS"},
                    )
                    ax.text(
                        value + dx * 10,
                        i - (num_of_elements / 50),
                        f"    {value:,.0f}",
                        size=14,
                        ha="left",
                        va="center",
                    )

                time_unit_displayed = re.sub(r"\^(.*)", r"", str(Time))
                ax.text(
                    1.0,
                    1.14,
                    time_unit_displayed,
                    transform=ax.transAxes,
                    color="#666666",
                    size=14,
                    ha="right",
                    weight="bold",
                    fontdict={"fontname": "Trebuchet MS"},
                )
                # ax.text(-0.005, 1.06, 'Number of confirmed cases', transform=ax.transAxes, size=14, color='#666666')
                ax.text(
                    -0.005,
                    1.14,
                    "Number of {} cases ".format(field),
                    transform=ax.transAxes,
                    size=14,
                    weight="bold",
                    ha="left",
                    fontdict={"fontname": "Trebuchet MS"},
                )

                ax.xaxis.set_major_formatter(
                    ticker.StrMethodFormatter("{x:,.0f}"))
                ax.xaxis.set_ticks_position("top")
                ax.tick_params(axis="x", colors="#666666", labelsize=12)
                ax.set_yticks([])
                ax.set_axisbelow(True)
                ax.margins(0, 0.01)
                ax.grid(which="major", axis="x", linestyle="-")

                plt.locator_params(axis="x", nbins=4)
                plt.box(False)
                plt.subplots_adjust(
                    left=0.075,
                    right=0.75,
                    top=0.825,
                    bottom=0.05,
                    wspace=0.2,
                    hspace=0.2,
                )
예제 #11
0
def heatmap(x, y, tfs=12, bkg_color='#F1F1F1', separate_first=0, **kwargs):
    """ Calculate a heatmap

    Based on: https://towardsdatascience.com/better-heatmaps-and-correlation-matrix-plots-in-python-41445d0f2bec
    """
    if 'color' in kwargs:
        color = kwargs['color']
    else:
        color = [1] * len(x)

    if 'palette' in kwargs:
        palette = kwargs['palette']
        n_colors = len(palette)
    else:
        n_colors = 256  # Use 256 colors for the diverging color palette
        palette = sns.diverging_palette(
            359, 122, s=90, n=500)  #sns.color_palette("BrBG", n_colors)

    if 'color_range' in kwargs:
        color_min, color_max = kwargs['color_range']
    else:
        color_min, color_max = min(color), max(
            color
        )  # Range of values that will be mapped to the palette, i.e. min and max possible correlation

    def value_to_color(val):
        if color_min == color_max:
            return palette[-1]
        else:
            val_position = float((val - color_min)) / (
                color_max - color_min
            )  # position of value in the input range, relative to the length of the input range
            val_position = min(max(val_position, 0),
                               1)  # bound the position betwen 0 and 1
            ind = int(val_position *
                      (n_colors - 1))  # target index in the color palette
            return palette[ind]

    if 'size' in kwargs:
        size = kwargs['size']
    else:
        size = [1] * len(x)

    if 'size_range' in kwargs:
        size_min, size_max = kwargs['size_range'][0], kwargs['size_range'][1]
    else:
        size_min, size_max = min(size), max(size)

    size_scale = kwargs.get('size_scale', 500)

    def value_to_size(val):
        if size_min == size_max:
            return 1 * size_scale
        else:
            val_position = (val - size_min) * 0.99 / (
                size_max - size_min
            ) + 0.01  # position of value in the input range, relative to the length of the input range
            val_position = min(max(val_position, 0),
                               1)  # bound the position betwen 0 and 1
            return val_position * size_scale

    if 'x_order' in kwargs:
        x_names = [t for t in kwargs['x_order']]
    else:
        x_names = [t for t in sorted(set([v for v in x]))]
    x_to_num = {p[1]: p[0] for p in enumerate(x_names)}

    if 'y_order' in kwargs:
        y_names = [t for t in kwargs['y_order']]
    else:
        y_names = [t for t in sorted(set([v for v in y]))]
    y_to_num = {p[1]: p[0] for p in enumerate(y_names)}

    plot_grid = plt.GridSpec(1, 30, hspace=0.2,
                             wspace=0.1)  # Setup a 1x10 grid
    ax = plt.subplot(plot_grid[:, :-1]
                     )  # Use the left 14/15ths of the grid for the main plot

    marker = kwargs.get('marker', 's')

    kwargs_pass_on = {
        k: v
        for k, v in kwargs.items() if k not in [
            'color', 'palette', 'color_range', 'size', 'size_range',
            'size_scale', 'marker', 'x_order', 'y_order'
        ]
    }
    ax.scatter(x=[x_to_num[v] for v in x],
               y=[y_to_num[v] for v in y],
               marker=marker,
               s=[value_to_size(v) for v in size],
               c=[value_to_color(v) for v in color],
               **kwargs_pass_on)
    ax.set_xticks([v for k, v in x_to_num.items()])
    ax.set_xticklabels([k for k in x_to_num],
                       rotation=45,
                       horizontalalignment='right',
                       fontsize=tfs)
    ax.set_yticks([v for k, v in y_to_num.items()])
    ax.set_yticklabels([k for k in y_to_num], fontsize=tfs)

    ax.grid(False, 'major')
    ax.grid(True, 'minor')
    ax.set_xticks([t + 0.5 for t in ax.get_xticks()], minor=True)
    ax.set_yticks([t + 0.5 for t in ax.get_yticks()], minor=True)

    ax.set_xlim([-0.5, max([v for v in x_to_num.values()]) + 0.5])
    ax.set_ylim([-0.5, max([v for v in y_to_num.values()]) + 0.5])
    ax.set_facecolor(bkg_color)

    if separate_first:
        l = np.sqrt(len(x))
        plt.axvline(separate_first - .5, color='gray')
        plt.axhline(l - .5 - separate_first, color='gray')

    # Add color legend on the right side of the plot
    if color_min < color_max:
        ax = plt.subplot(plot_grid[:,
                                   -1])  # Use the rightmost column of the plot
        #ax.axis('off')
        plt.box(on=None)
        col_x = [0] * len(palette)  # Fixed x coordinate for the bars
        bar_y = np.linspace(
            color_min, color_max,
            n_colors)  # y coordinates for each of the n_colors bars
        bar_height = bar_y[1] - bar_y[0]
        print(bar_height)
        ax.barh(
            y=bar_y,
            width=[15] * len(palette),  # Make bars 5 units wide
            left=col_x,  # Make bars start at 0
            height=bar_height,
            color=palette,
            linewidth=0)
        ax.set_ylim(-2, 2)
        ax.set_xlim(
            0, 5
        )  # Bars are going from 0 to 5, so lets crop the plot somewhere in the middle
        ax.grid(False)  # Hide grid
        ax.set_facecolor('white')  # Make background white
        ax.set_xticks([])  # Remove horizontal ticks
        ax.set_yticks(
            np.linspace(min(bar_y), max(bar_y),
                        3))  # Show vertical ticks for min, middle and max
        ax.yaxis.tick_right()  # Show vertical ticks on the right
    plt.sca(plt.subplot(plot_grid[:, :-1]))
예제 #12
0
#%% Synopsis of GAN hessian consistency
fig, ax = plt.subplots()
plt.errorbar(GAN_geom_summary.lin_corr_mean,
             GAN_geom_summary.log_corr_mean,
             yerr=GAN_geom_summary.log_corr_std,
             xerr=GAN_geom_summary.lin_corr_std,
             fmt='o')
plt.xlim([0, 1])
for i, GAN in enumerate(GAN_geom_summary.index):
    ax.annotate(
        GAN,
        (GAN_geom_summary.lin_corr_mean[i], GAN_geom_summary.log_corr_mean[i]),
        fontsize=12)
plt.ylabel("log scale corr")
plt.xlabel("lin scale corr")
plt.box(True)
plt.savefig(join(summarydir, "Hess_corrmat_synopsis.png"))
plt.savefig(join(summarydir, "Hess_corrmat_synopsis.pdf"))
plt.show()

#%% Plot an Example of Hessian consistency
BGHpath = "E:\Cluster_Backup\BigGANH"
eigval_col, eigvec_col, feat_col, meta = scan_hess_npz(BGHpath,
                                                       "Hess_cls(\d*).npz",
                                                       evakey='eigvals',
                                                       evckey='eigvects',
                                                       featkey='vect')
#%%
figdir = join(summarydir, "BigGAN")
plot_consistency_example(eigval_col,
                         eigvec_col,
예제 #13
0
        def plot_correlation(self, other_interface, lines=False, bounds=None):
            """ Plot this interface's view on the underlying product vs
            another interface's view.

            Each vulnerability will be represented as a point, with the x coordinate
            representing the likelihood that this interface will discover the
            vulnerability in the next round, and the y coordinate representing
            the likelihood that the other interface will discover it this round.

            This plot is helpful for understanding the discovery correlation that
            arises with learning.

            Parameters
            ----------
            other_interface: Interface object
               The interface object must be built upon the same underlying 'product'
               as the present interface

            lines: True/False
               If true, will overlay horizontal and vertical lines representing the
               mean discovery profile for each actor, and label them with the mean
               value.

            bounds: None or float
               If float will set the x and y limits to this value.

            TODO
            ----
            It might be a good idea to make the bounds an x/y tuple if we want to
            initialize the interfaces with different `max_area` values.

            """
            plt.plot(self.circles.area,
                     other_interface.circles.area,
                     '.',
                     alpha=.1)

            if bounds == None:
                window_size = max(self.circles.area.max(),
                                  other_interface.circles.area.max())
            else:
                window_size = bounds

            if lines:
                y_mean = np.mean(other_interface.circles.area)
                x_mean = np.mean(self.circles.area)
                plt.hlines(y_mean, 0, window_size)
                plt.text(window_size,
                         y_mean,
                         'mean=%f' % y_mean,
                         ha='right',
                         va='bottom')
                plt.vlines(x_mean, 0, window_size)
                plt.text(x_mean,
                         window_size,
                         'mean=%f' % x_mean,
                         rotation=90,
                         ha='right',
                         va='top')

            plt.xlim(0, window_size)
            plt.ylim(0, window_size)
            plt.box('off')
            plt.xlabel(self.name + ' likelihood of discovery', fontsize=14)
            plt.ylabel(other_interface.name + ' likelihood of discovery',
                       fontsize=14)