def main(): # 2d where some fxpt not on every path. happens at glancing nullclines network_sizes, num_samples, test_data=fe.load_test_data('dl2.npz') N, s = 2, 1 W = test_data['N_%d_W_%d'%(N,s)] samp = 10 ang = np.linspace(0,np.pi,samp) C = np.array([np.sin(ang),np.cos(ang)]) v_ax = plt.subplot(1,2,1) a_ax = plt.subplot(1,2,2) for a in [0]:#range(samp): status, fxV, VA, c, deltas, s_mins = rd.traverse(W, c=C[:,[a]]) print(a, fxV.shape[1]) ptr.plot(v_ax, VA[:N,:], '-', c=str(1.0*a/samp)) ptr.scatter(v_ax, fxV, c='r') a_ax.plot(VA[N,:], '-', c=str(1.0*a/samp)) a_ax.plot(range(VA.shape[1]),np.zeros(VA.shape[1]),'--b') # nullclines v = np.linspace(-1,1,100)[1:-1] v_ax.plot(v, (np.arctanh(v)-W[0,0]*v)/W[0,1], '-b') v_ax.plot((np.arctanh(v)-W[1,1]*v)/W[1,0], v, '-b') ptr.set_lims(v_ax, np.array([[-2,2],[-2,2]])) ptr.set_lims(a_ax, np.array([[0,VA.shape[1]],[-.5,.5]])) plt.show()
def percent_timeout(xlabel, data, fix=None): xs = list(set(int(d[xlabel]) for d in data if d[xlabel] != xlabel)) timeouts = [] distribs = [] averages = [] for x in xs: count = 0 times = [] total = 0 for d in data: if d[xlabel] == xlabel: continue if int(d[xlabel]) != int(x): continue if fix is not None and int(d[fix[0]]) != int(d[fix[0]]): continue total += 1 times.append(min(float(d["time"]), args.timeout)) if d["succeed"] != "true": count += 1 if len(times) == 0: continue timeouts.append(float(count) / float(total)) distribs.append(times) averages.append(sum(times) / len(times)) title_prc = "Percent_Timeout_100Edits_32bits_%s" % xlabel title_avg = "Mean_Time_100Edits_32bits_%s" % xlabel title_viol = "Violin_100Edits_32bits_%s" % xlabel if fix is not None: app_str = str(fix[0]) + str(fix[1]) title_prc += app_str title_avg += app_str assert len(xs) == len(averages) plotter.scatter(args.output, xs, timeouts, title_prc, xlabel, "% timeout", ylim=(0.0, 1.0)) plotter.scatter(args.output, xs, averages, title_avg, xlabel, "time(s)", ylim=(0.0, args.timeout)) plotter.violins(args.output, xs, distribs, title_viol, xlabel, "time (s)", ylim=(0.0, args.timeout))
def plot_scatter(x, y, label, data, maxy=None, fix=None): xs = [] ys = [] for d in data: if d[fix[0]] == fix[0]: continue if fix is None or int(d[fix[0]]) == int(fix[1]): xs.append(int(d[x])) if maxy is not None and float(d[y]) > float(maxy): ys.append(float(maxy)) else: ys.append(float(d[y])) print "data for", fix, len(xs), len(ys) if maxy is None: ylim = None else: ylim = (0, maxy) plotter.scatter(args.output, xs, ys, label, x, y, ylim=ylim)
def scatter_color(xlabel, ylabel, data, maxtime, xlim=None, ylim=None): xs = [] ys = [] cs = [] for d in random.sample(data, k=10000): if d[xlabel] == xlabel: continue xs.append(float(d[xlabel])) ys.append(float(d[ylabel])) time = float(d["time"]) if time > maxtime: time = maxtime cs.append(time) title = "{}_vs_{}_vs_time".format(xlabel, ylabel) print "generating" plotter.scatter(args.output, xs, ys, title, xlabel, ylabel, ylim=ylim, xlim=xlim, colors=cs)
def fiber_fig(): """ Show an example of a directional fiber for a two-neuron network """ # mpl.rcParams['mathtext.default'] = 'regular' mpl.rcParams['mathtext.default'] = 'it' mpl.rcParams.update({'font.size': 18}) mpl.rcParams['pdf.fonttype'] = 42 mpl.rcParams['ps.fonttype'] = 42 W = np.array([[1.5, 0.05], [-0.01, 1.5]]) c = np.array([[1.], [2]]) _, fxV, VA, _, _, _, _ = rfx.traverse(W, c=c) fxV, _ = rfx.post_process_fxpts(W, fxV) VA = np.concatenate((-VA[:2, ::-1], VA[:2, :]), axis=1) plt.figure(figsize=(7, 7)) ptr.plot(plt.gca(), VA[:2, :], '-k') ptr.scatter(plt.gca(), fxV, 75, c=((0, 0, 0), )) V = ptr.lattice([-1.25, -1.25], [1.25, 1.25], 20) C = np.tanh(W.dot(V)) - V ptr.quiver(plt.gca(), V, C, scale=.008, units='dots', width=1, color=((.7, .7, .7), ), headwidth=7) V = VA[:2, :] V = V[:, ((-1.25 < V) & (V < 1.25)).all(axis=0)] C = np.tanh(W.dot(V)) - V ptr.quiver(plt.gca(), V, C, scale=.005, units='dots', width=2, headwidth=5) plt.xlim([-1.25, 1.25]) plt.xlabel('$v_1$', fontsize=24) plt.ylim([-1.25, 1.25]) plt.ylabel('$v_2$', fontsize=24, rotation=0) # plt.tight_layout() plt.show()
def c_space_fig(): """ Plot both the set of bad c and some nearby fibers """ mpl.rcParams['mathtext.default'] = 'regular' # mpl.rcParams.update({'figure.autolayout': True}) mpl.rcParams.update({'font.size': 14}) mpl.rcParams['pdf.fonttype'] = 42 mpl.rcParams['ps.fonttype'] = 42 N = 3 num_C = 8 # should be even or else land exactly on W[i,:]c=0 # known loop: W = np.array([[ 1.20005258, -0.09232104, -0.12745533], [-0.03161879, 1.26916341, -0.11257691], [-0.03246188, 0.08548894, 1.24386724]]) # c1 = np.array([[.7,.5,2]]).T #c2 = np.array([[.1,1.5,2]]).T c1 = np.array([[.3,.7,.7]]).T c2 = np.array([[.7,.5,.7]]).T # # Random W, C # W = 1.1*np.eye(N) + 0.1*np.random.randn(N,N) # c1 = mldivide(W, np.array([[+0.5,0.3,1]]).T) # c2 = mldivide(W, np.array([[-0.5,0.3,1]]).T) interp = np.arange(num_C)/(num_C-1.0) C = c1*interp[np.newaxis,:] + c2*(1-interp[np.newaxis,:]) C = C/np.sqrt((C*C).sum(axis=0)) bright_cap = .7 # show c space partition if not os.path.exists('lork_c_3d_data.npz'): _ = find_low_rank_c_3d_data(W) npz = np.load('lork_c_3d_data.npz') fig = plt.figure(figsize=(4.5,9)) ax = fig.add_subplot(2,1,1,projection='3d') for colorful in range(0): colors = ['b','g','r','c','m','y','k','w'] # numerical for s in npz['signs'].shape[1]: # if colors[s] not in ['b','m','y']: continue # if s not in [0,4,5]: continue # for p in range(len(c_path[s])): for p in range(npz['lens'][s]): c_path_s_p = npz['c_path_%d_%d'%(s,p)] pltr.plot(ax, c_path_s_p, colors[s]) #pltr.quiver(ax, a_path[s][p], c_path_s_p) # pltr.plot(ax, c_path_s_p + 0.05*a_path[s][p], 'k--') pltr.plot(ax, -c_path_s_p, colors[s]) # pltr.plot(ax, -c_path_s_p - 0.05*a_path[s][p], 'k--') # pltr.plot(ax, c_path_inf[s][p], colors[s]) # pltr.plot(ax, -c_path_inf[s][p], colors[s]) # pltr.plot(ax, c[s], colors[s]+'+') # Wc = 0 angles = np.linspace(0,2*np.pi,100) for i in range(N): _, _, Z = np.linalg.svd(W[[i],:]) # for null-space c_i = Z[-2:,:].T.dot(np.array([np.sin(angles), np.cos(angles)])) pltr.plot(ax, c_i, 'k--') for summary in range(1): # numerical for s in range(npz['signs'].shape[1]): if s not in [0,4,5]: continue for p in range(npz['lens'][s]): c_path_s_p = npz['c_path_%d_%d'%(s,p)] pltr.plot(ax, c_path_s_p, 'k') pltr.plot(ax, -c_path_s_p, 'k') # if c_path[s][p].shape[1] > 0: # print(c_path[s][p].shape) # print(int(c_path[s][p].shape[1]/2)) # print(c_path[s][p][:,[int(c_path[s][p].shape[1]/2)]]) # print(['%d,%d'%(s,p)]) # pltr.text(ax, c_path[s][p][:,[int(c_path[s][p].shape[1]/2)]],['+%d,%d'%(s,p)]) # # pltr.text(ax, -c_path[s][p][:,[int(c_path[s][p].shape[1]/2)]],['-%d,%d'%(s,p)]) # Wc = 0 angles = np.linspace(0,2*np.pi,100) for i in range(N): _, _, Z = np.linalg.svd(W[[i],:]) # for null-space c_i = Z[-2:,:].T.dot(np.array([np.sin(angles), np.cos(angles)])) pltr.plot(ax, c_i, 'k--') # plt.xticks([-1,0,1]) plt.yticks([-1,0,1]) # ax.set_zticks([-1,0,1]) pltr.set_lims(ax, 1*np.array([[1],[1],[1]])*np.array([[-1,1]])) # ax.set_xlabel('$c_1$') # ax.set_ylabel('$c_2$') # ax.set_zlabel('$c_3$') # ax.set_title('(a)') mpl.rcParams['mathtext.default'] = 'it' # mpl.rcParams['font.family'] = 'serif' # ax.set_title('$\frac{c}{||c||}\in\mathrm{\mathbb{S}}^2$') ax.set_title('$c/||c||\in\mathrm{\mathbf{S}}^2$') # mathbb incompatible with truetype pltr.scatter(ax, C, c=np.array([[bright_cap*g for _ in range(3)] for g in interp]),edgecolor='face') # pltr.scatter(ax, C, s=20, c=[str(bright_cap*g) for g in interp],edgecolor='face') # pltr.quiver(ax, c1, c1-c2) # ax.azim, ax.elev = 16, 51 ax.azim, ax.elev = -74, 2 # plt.show() # return None # show paths if not os.path.exists('brute_fiber_data.npz'): _ = c_space_fig_fiber_data() npz = np.load('brute_fiber_data.npz') # npz['fxV_%d_%d'%(p,i)] = fxV[i] # npz['VA_%d_%d'%(p,i)] = VA[i] # npz['lens_%d'%p] = len(fxV) # np.savez('brute_fiber_data.npz',**npz) ax = fig.add_subplot(2,1,2,projection='3d') for p in range(0,len(interp),2): for i in range(npz['lens_%d'%p]): fxV_i = npz['fxV_%d_%d'%(p,i)] VA_i = npz['VA_%d_%d'%(p,i)] for s in [1]:#[-1,1]: col = np.array([bright_cap*interp[p] for _ in range(3)]) pltr.plot(ax, s*VA_i[:N, (np.fabs(VA_i[:N,:]) < 3).all(axis=0)], color=col) pltr.plot(ax, s*fxV_i, 'ko') # ax.set_title('c_%d'%p) pltr.set_lims(ax, 1.5*np.array([[1,1,1]]).T*np.array([[-1,1]])) ax.azim, ax.elev = -94, 8 # ax.set_xlabel('$v_1$') #,rotation=0) # ax.set_ylabel('$v_2$') #,rotation=0) # ax.set_zlabel('$v_3$') #,rotation=0) plt.xticks([-1,0,1]) plt.yticks([-1,0,1]) ax.set_zticks([-1,0,1]) ax.set_title('$v\in\mathrm{\mathbf{R}}^3$') plt.tight_layout() plt.show()
def c_space_fig_old(): """ Plot both the set of bad c and some nearby fibers """ mpl.rcParams['mathtext.default'] = 'regular' # mpl.rcParams.update({'figure.autolayout': True}) mpl.rcParams.update({'font.size': 14}) N = 3 num_C = 8 # should be even or else land exactly on W[i,:]c=0 # known loop: W = np.array([[ 1.20005258, -0.09232104, -0.12745533], [-0.03161879, 1.26916341, -0.11257691], [-0.03246188, 0.08548894, 1.24386724]]) # c1 = np.array([[.7,.5,2]]).T #c2 = np.array([[.1,1.5,2]]).T c1 = np.array([[.3,.7,.7]]).T c2 = np.array([[.7,.5,.7]]).T # # Random W, C # W = 1.1*np.eye(N) + 0.1*np.random.randn(N,N) # c1 = mldivide(W, np.array([[+0.5,0.3,1]]).T) # c2 = mldivide(W, np.array([[-0.5,0.3,1]]).T) interp = np.arange(num_C)/(num_C-1.0) C = c1*interp[np.newaxis,:] + c2*(1-interp[np.newaxis,:]) C = C/np.sqrt((C*C).sum(axis=0)) bright_cap = .7 print('low rank c...') # show c space partition fig = plt.figure(figsize=(11,6)) ax = fig.add_subplot(1,2,1,projection='3d') _ = find_low_rank_c_3d(W) # ax.set_title('(a)') mpl.rcParams['mathtext.default'] = 'it' # mpl.rcParams['font.family'] = 'serif' ax.set_title('$c/||c||\in\mathrm{\mathbb{S}}^2$') pltr.scatter(ax, C, c=np.array([[bright_cap*g for _ in range(3)] for g in interp]),edgecolor='face') # pltr.scatter(ax, C, s=20, c=[str(bright_cap*g) for g in interp],edgecolor='face') # pltr.quiver(ax, c1, c1-c2) # ax.azim, ax.elev = 16, 51 ax.azim, ax.elev = -74, 2 # plt.show() # return None # show paths ax = fig.add_subplot(1,2,2,projection='3d') for p in range(2): print('fxpath brute %d...'%p) fxV, VA = rfx.brute_fiber(W, C[:,[p*-1]]) for i in range(len(VA)): for s in [1]:#[-1,1]: pltr.plot(ax, s*VA[i][:N, (np.fabs(VA[i][:N,:]) < 3).all(axis=0)], color=str(bright_cap*interp[p*-1])) pltr.plot(ax, s*fxV[i], 'ko') # ax.set_title('c_%d'%p) pltr.set_lims(ax, 3*np.array([[1,1,1]]).T*np.array([[-1,1]])) ax.azim, ax.elev = -94, 8 # ax.set_xlabel('$v_1$') #,rotation=0) # ax.set_ylabel('$v_2$') #,rotation=0) # ax.set_zlabel('$v_3$') #,rotation=0) ax.set_title('$v\in\mathrm{\mathbb{R}}^3$') plt.tight_layout() plt.show() # ax.xaxis.label.set_rotation(0) # ax.yaxis.label.set_rotation(0) # ax.zaxis.label.set_rotation(0) return fxV, VA, W, C
#if not (person[3:7] == '0713' and int(person[7:]) < 300): # continue if audio not in ['06', '07']: continue print(person, label, filename) cnt += 1 l, r = basic_endpoint_detection(sig, rate) #l,r= 0,len(sig) sig = preemphasis(sig, coeff=0.97) frames = to_frames(sig[l:r], rate) feature = pitch_feature(sig[l:r], rate) p.append((feature[0], feature[-1])) X.append(feature) labels.append(label) if cnt == 200: break scatter(p, labels, '111', True) cnt = 0 p = [] for idx, l, (sig, rate), label, filename in val_itr: reg = re.compile('(\d+)-(\d+)-(\d+).wav') audio = reg.match(filename).group(2) person = reg.match(filename).group(1) if audio not in ['06', '07']: continue print(person, label, filename) cnt += 1 l, r = basic_endpoint_detection(sig, rate) sig = preemphasis(sig, coeff=0.97) frames = to_frames(sig[l:r], rate) feature = pitch_feature(sig[l:r], rate)