def save_current_images(self): model_name, checkpoint = self.weights_filename.split('/')[-2:] epoch = checkpoint.split('_')[0] series_no, _ = os.path.basename(self.pkl_file).split('.') save_dir = os.path.join(self.start_dir, 'saved_images', model_name, 'epoch_{}'.format(epoch)) try: os.makedirs(save_dir) except: pass out_div_file = 'series_{}_output_divergence_{}.png'.format(series_no, '{}') out_div_count = len(glob.glob(os.path.join(save_dir, out_div_file.format('*')))) out_div_file = out_div_file.format('{:03}') out_div_img = self.draw_mask(cm.seismic(((self.div_im + 1) / 2 * 255).astype(np.uint8))) gt_div_file = 'series_{}_gt_divergence.png'.format(series_no) gt_div_img = self.draw_mask(cm.seismic(((self.gt_div + 1) / 2 * 255).astype(np.uint8))) out_disc_file = 'series_{}_output_one_hot_thresh_{:.4}_{}.png'.format(series_no, self.thresh, '{}') out_disc_count = len(glob.glob(os.path.join(save_dir, out_disc_file.format('*')))) out_disc_file = out_disc_file.format('{:03}') out_disc_img = self.draw_mask(self.out_disc) plt.imsave(os.path.join(save_dir, 'series_{}_ground_truth_one_hot.png'.format(series_no)), self.input_display_im) plt.imsave(os.path.join(save_dir, gt_div_file.format(out_div_count)), gt_div_img) plt.imsave(os.path.join(save_dir, out_div_file.format(out_div_count)), out_div_img) plt.imsave(os.path.join(save_dir, out_disc_file.format(out_disc_count)), out_disc_img) print('Saved 3 images to {}'.format(save_dir))
def show_sph_harm(l, m, real=True, N=50, use_sphere=True): ''' Show the spherical harmonics on a unit sphere ''' theta = np.linspace(0, np.pi, N) phi = np.linspace(0, 2*np.pi, N) theta, phi = np.meshgrid(theta, phi) # The Cartesian coordinates of the unit sphere x = np.sin(theta) * np.cos(phi) y = np.sin(theta) * np.sin(phi) z = np.cos(theta) xyz = np.c_[x.ravel(), y.ravel(), z.ravel()] # from time import time # t0 = time() if real: ylm = sph_r(xyz, l, m).reshape(N, N) else: ylm = sph_c(xyz, l, m).reshape(N, N).real # t1 = time() # print(t1 - t0) import matplotlib.pyplot as plt from matplotlib import cm, colors from mpl_toolkits.mplot3d import Axes3D # Calculate the spherical harmonic Y(l,m) and normalize to [0,1] fcolors = ylm fmax, fmin = fcolors.max(), fcolors.min() fcolors = (fcolors - fmin)/(fmax - fmin) # Set the aspect ratio to 1 so our sphere looks spherical fig = plt.figure( figsize=plt.figaspect(1.) ) ax = fig.add_subplot(111, projection='3d') if use_sphere: ax.plot_surface(x, y, z, rstride=1, cstride=1, facecolors=cm.seismic(fcolors)) else: r0 = np.abs(ylm) ax.plot_surface(x*r0, y*r0, z*r0, rstride=1, cstride=1, facecolors=cm.seismic(fcolors)) # Turn off the axis planes ax.set_axis_off() plt.show()
def make_plots(yH, df, num_covars): """ make_plots plots 'LogTotal' and its fitted valus, yH, against the first num_covars (num_covars must be less than 6) in the dataframe, df, while color coding by 'Distributor'. """ distributors = np.sort(np.unique(df['Distributor'])) num_groups = len(distributors) # color code by Distributor. Make colors 'increase' with increaing # average return for the Distributor. color_map = np.exp(df.groupby('Distributor').median()['LogTotal']) color_map -= -min(color_map) color_map /= max(color_map) # now color_map lies in the interval [0,1] # compact graphs sharying the same LogTotal axis fig, ax = plt.subplots(1, num_covars, sharey = True) fig.tight_layout(pad=1.08, h_pad = None, w_pad = None, rect = None) plt.subplots_adjust(wspace = 0, hspace = 0) for i in range(num_covars): ax[i].set_xlabel(df.columns[i+1]) # enumerate though distributors to color code observed values for j in range(num_groups): subdf = df.loc[df['Distributor'] == distributors[j]] ax[i].scatter(subdf[subdf.columns[i+1]], subdf['LogTotal'], color = cm.seismic(color_map[j]), alpha=0.5) # add fitted values all in black ax[i].scatter(df.iloc[:, i+1], yH, color = 'k', alpha = .8, marker = 'x') # eliminate outermost x-tick lables ax[i].xaxis.set_major_locator(MaxNLocator(prune='both')) ax[i].xaxis.label.set_size(20) ax[0].set_ylabel('LogTotal', fontsize = 20) plt.show()
def plot(self): N = self.N theta = np.linspace(0, np.pi, N) phi = np.linspace(0, 2 * np.pi, N) theta, phi = np.meshgrid(theta, phi, indexing='ij') # The Cartesian coordinates of the unit sphere x = np.sin(theta) * np.cos(phi) y = np.sin(theta) * np.sin(phi) z = np.cos(theta) fcolors = self.grid fmax, fmin = fcolors.max(), fcolors.min() fcolors = (fcolors - fmin) / (fmax - fmin) # Set the aspect ratio to 1 so our sphere looks spherical fig = plt.figure(figsize=plt.figaspect(1.)) ax = fig.add_subplot(111, projection='3d') ax.plot_surface(x, y, z, rstride=1, cstride=1, facecolors=cm.seismic(fcolors)) # Turn off the axis planes plt.xlabel('x') plt.ylabel('y') plt.show()
def plot_scatterFeats(self, data, Survival, Censored, fidx1=0, fidx2=1): """ scatter patients by two features and code their survival. Note: for best visual results, at least one of the features should be continuous. """ print("Plotting Features (transformed) vs survival (color)") Ax = np.dot(data, self.A) fig, ax = plt.subplots() keep = (Censored == 0).reshape(data.shape[0]) X1 = Ax[keep, int(self.fvars[fidx1, 0])] X2 = Ax[keep, int(self.fvars[fidx2, 0])] Ys = Survival[keep, :] colors = cm.seismic(np.linspace(0, 1, len(Ys))) ax.scatter(X1, X2, color=colors) plt.title("Features (transformed) vs survival (color)", fontsize=16, fontweight='bold') plt.xlabel(str(self.ranks[fidx1]), fontsize=5) plt.ylabel(self.ranks[fidx2], fontsize=5) plt.savefig(self.RESULTPATH + self.description + "scatterFeats.svg") plt.close()
def display_PM(PM, items_names): """ Plot the PM as a heatmat and the sum over its rows in a barplot Inputs : - PM : a (w,w) numpy array of the PM - items_names : a list of w names of the items, in order of the dataset columns. """ # PREFERENCE MATRIX fig = plt.figure() ax = fig.add_subplot(1, 1, 1) cax = ax.matshow(PM, cmap='seismic') fig.colorbar(cax) ax.set_xticklabels([''] + items_names, rotation=90) ax.set_yticklabels([''] + items_names) ax.xaxis.set_major_locator(ticker.MultipleLocator(1)) ax.yaxis.set_major_locator(ticker.MultipleLocator(1)) plt.show() # BARPLOT OF PREFERENCES fig = plt.figure() ax = fig.add_subplot(1, 1, 1) w = PM.sum(axis=1) norm01 = mc.Normalize(vmin=min(w), vmax=max(w)) plt.barh(list(range(10)), w, tick_label=items_names, color=cm.seismic(norm01(w))) plt.title("Barplot of preferences") plt.show()
def gauge_plot(arrow_index, labels): list_colors = np.linspace(0, 1, int(len(labels) / 2)) size_of_groups = np.ones(len(labels)) white_half = np.ones(len(list_colors)) * .5 color_half = list_colors cs1 = cm.RdYlGn_r(color_half) cs2 = cm.seismic(white_half) cs = np.concatenate([cs1, cs2]) fig, ax = plt.subplots() ax.pie(size_of_groups, colors=cs, labels=labels) my_circle = plt.Circle((0, 0), 0.6, color='white') ax.add_artist(my_circle) arrow_angle = (arrow_index / float(len(list_colors))) * 3.14159 arrow_x = 0.8 * math.cos(arrow_angle) arrow_y = 0.8 * math.sin(arrow_angle) arr = plt.arrow(0,0,-arrow_x,arrow_y, width=.02, head_width=.05, \ head_length=.1, fc='k', ec='k') ax.add_artist(arr) ax.add_artist(plt.Circle((0, 0), radius=0.04, facecolor='k')) ax.add_artist(plt.Circle((0, 0), radius=0.03, facecolor='w', zorder=11)) ax.set_aspect('equal') st.pyplot(fig) return True
def plot_average_pixel_trend(sci_arr, err_arr, mask_arr, scale = 0.13, doPlot = True, doCD = False,factor=1): chi2Map, ubermask = make_chi2_map(sci_arr, err_arr, mask_arr) use = ~ubermask #Negating the bad guys image = np.average(sci_arr,axis=0, weights = 1./err_arr**2) image[ubermask] = -np.inf ''' image_filtered = image * 0. a=0.1 theFilter = np.array([[0.,a, 0.], [a, -4*a, a], [0., a, 0.]]) #Laplacian for i in xrange(3): for j in xrange(3): image_filtered = image_filtered + theFilter[i,j] * image image_filtered-=image ''' image_filtered = apply_cdmodel (galsim.Image(image,scale=scale), factor= factor).array - image image_filtered[ubermask | ~np.isfinite(image_filtered)] = 0. # Bin the filtered image values into quantiles. nq = 10 quant = np.percentile(image_filtered[use].flatten(), np.linspace(0,100,nq)) deviant_arr = sci_arr - np.expand_dims(image,axis=0) max_interval = 0. timeseries = [] for i in xrange(nq-1): these_pixels = ( (image_filtered > quant[i]) & (image_filtered <= quant[i+1]) & (image_filtered != 0) ) these_pixels_3d = np.repeat(np.expand_dims(these_pixels,axis=0),sci_arr.shape[0],axis=0) this_dev_array = deviant_arr.copy() this_dev_array[~these_pixels_3d] = 0. this_npix = np.sum(these_pixels) this_timeseries = np.sum(np.sum(this_dev_array,axis=1),axis=1) * 1./this_npix timeseries.append(this_timeseries) this_interval = np.abs(np.max(this_timeseries) - np.min(this_timeseries)) if this_interval > max_interval: max_interval = this_interval offset_array = (np.arange(nq) - np.mean(np.arange(nq))) * max_interval if doPlot is True: fig,(ax1,ax2,ax3) = plt.subplots(nrows=1,ncols=3,figsize=(28,6)) colors = cm.seismic(np.linspace(0, 1, nq-1)) ax1.imshow(np.arcsinh(image)) ax2.imshow(image_filtered,cmap=cm.seismic,vmin = quant[1],vmax=-quant[1]) ax2.set_title("Laplacian-filtered image") for i in xrange(nq-1): ax3.plot((timeseries[i] + offset_array[i])[::-1],color=colors[i],marker='.') fig.savefig("linearity_timeseries_trend.png") ax3.set_xlabel ("Time (arbitrary units)") ax3.set_ylabel ("Corrected pixel flux (e/sec)") fig.tight_layout() fig.subplots_adjust(wspace=0.3) fig.show() timeseries_offset = [ts + os for ts,os in zip(timeseries,offset_array)] return timeseries_offset
def amp2rgb(Amp, minAmp, maxAmp): if Amp >= maxAmp: cindex = float(255) elif Amp <= minAmp: cindex = float(0) else: cindex = float(((255.0 / (maxAmp - minAmp)) * (Amp - minAmp))) vrgb = cm.seismic(int(cindex)) return vrgb
def plot_category(plot_cat_ind, label_threshold=0.3): fig = plt.figure() ax = fig.add_subplot(111) abridged_corr_matrix = np.asarray(correlation_matrix)[plot_cat_ind] x_arr = np.linspace(0, N_cats - 1, N_cats, endpoint=True) color_range = cm.seismic(np.linspace(0.05, 0.95, 40)) min_abs = abs(np.min(abridged_corr_matrix)) norm = np.max([np.max(abridged_corr_matrix), min_abs]) #print (np.max( np.max(abridged_corr_matrix) , abs(np.min(abridged_corr_matrix)))) for i in range(len(abridged_corr_matrix)): abridged_corr_matrix[i] = abridged_corr_matrix[i] / norm x_arr = np.append(x_arr, 100) x_arr = np.append(x_arr, 101) abridged_corr_matrix = np.append(abridged_corr_matrix, -1.) abridged_corr_matrix = np.append(abridged_corr_matrix, 1.) #plt.grid(linewidth=1.) plt.plot([-10, 60], [0, 0], ':') plt.scatter(x_arr, abridged_corr_matrix, marker='o', s=200, linewidths=4, c=abridged_corr_matrix, cmap=plt.cm.seismic, edgecolors='grey', linewidth=1) #ax.scatter(x_arr, abridged_corr_matrix, 'o', color=abridged_corr_matrix, cmap=plt.cm.seismic) #ax = cb.ax plt.axis([-7, N_cats + 5., -1.5, 1.5]) text = ax.yaxis.label font = mpl.font_manager.FontProperties(size=22) text.set_font_properties(font) plt.title('App usage amongst people who use ' + whatcategory(plot_cat_ind) + ' apps', fontsize=24) plt.ylabel(r'$\leftarrow$ Disfavor~~~~~~~~Favor$\rightarrow$', fontsize=22) for x in range(0, len(x_arr) - 2): if abs(abridged_corr_matrix[x]) > label_threshold: y_sign = abridged_corr_matrix[x] / abs(abridged_corr_matrix[x]) plt.text(x, abridged_corr_matrix[x] + 0.2 * y_sign, whatcategory(x), fontsize=20, horizontalalignment='center', verticalalignment='center') fig.set_size_inches(10, 5)
def plotPCA(df, df_index, ax1, ax2, num_movies, titles, col): max_movies = min( num_movies, len(df)) #--choose the minimum between user input and max allowed plt.figure() ix = np.array(df_index[df_index.columns[0]].index) for i, a in zip(ix[:max_movies], df[:max_movies]): r = cm.seismic(col[i]) plt.scatter(a[ax1], a[ax2], c=r) plt.text(a[ax1], a[ax2], titles[i], color=r, fontsize=8) plt.xlabel('PCA Axis %d' % ax1) plt.ylabel('PCA Axis %d' % ax2) plt.show()
def update_coarse_grained_fig(step, fig_args, data_args): fig = fig_args['fig'] axes = fig_args['axes'] subplots = fig_args['subplots'] bar, subplots_arr = subplots data_list = data_args['data_list'] downsampled = data_args['downsampled'] vminmax_list = data_args['vminmax_list'] extras = data_args['extras'] df_score = data_args['df_score'] df_percent_nonzero = data_args['df_percent_nonzero'] # suptitle if step == extras['best_timepoint']: fig.suptitle("t = {:d} (best timepoint)".format(step), fontsize=20) else: fig.suptitle("t = {:d}".format(step), fontsize=20) # bar plot scores = df_score.loc[df_score.timepoint == step].score.to_numpy() scores = scores.reshape(extras['nb_seeds'], 2) scores_mean = scores.mean(0) for patch, ss in zip(bar, scores_mean): patch.set_height(ss) # scatter for i in range(3): data = data_list[i][step] f = data / vminmax_list[i] color_indxs = np.floor(128 * (1 + f)) color_indxs = np.array(color_indxs, dtype=int) if i == 0: rgba = cm.seismic(color_indxs) else: rgba = cm.PiYG_r(color_indxs) subplots_arr[i, 0].set_color(rgba) subplots_arr[i, 0].set_edgecolor('k') # iamge for j in range(1, len(downsampled) + 1): subplots_arr[i, j].set_data(downsampled[j - 1][i][step]) # y label percent_nonzero = df_percent_nonzero.loc[df_percent_nonzero.timepoint == step].percent_nonzero.to_numpy() percent_nonzero = percent_nonzero.reshape(extras['nb_seeds'], extras['nc']) y_lbl = "coeffs, {:s} nonzero: {:.2f} ± {:.2f}".format( '%', percent_nonzero.mean(), percent_nonzero.std()) axes[1][0, -1].set_ylabel(y_lbl)
def plotPCA3D(df, df_index, num_movies, titles, col): max_movies = min( num_movies, len(df)) #--choose the minimum between user input and max allowed fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ix = np.array(df_index[df_index.columns[0]].index) for i, a in zip(ix[:max_movies], df[:max_movies]): r = cm.seismic(col[i]) ax.scatter(a[0], a[1], a[2], c=r) ax.text(a[0], a[1], a[2], titles[i], fontsize=8, color=r) plt.xlabel('PCA Axis 0') plt.ylabel('PCA Axis 1') plt.show()
def visualize_cluster(self, output_path, dpi): plt.clf() aloc = np.argmax(self.gammas, axis=1) for k in range(self.K): col = cm.seismic(k / (1.0 * self.K)) plot_points = self.data_points[aloc == k] plt.plot(self.mus[k, 0], self.mus[k, 1], color=col, marker='D', markersize=10) plt.scatter(plot_points[:, 0], plot_points[:, 1], color=col) plt.title('iteration: {}, log likelihood: {}'.format( self.itr, self.calc_LL())) plt.xlim(-20, 20) plt.ylim(-20, 20) plt.savefig(output_path, dpi=dpi)
def vsh(l = 3, m = -3): #Visualizing the spherical harmonics (real) import matplotlib.pyplot as plt from matplotlib import cm, colors from mpl_toolkits.mplot3d import Axes3D import numpy as np from scipy.special import sph_harm global x, y, z, sh, phi, theta, fcolors, fmin, fmax phi = np.linspace(0, np.pi, 100) theta = np.linspace(0, 2*np.pi, 100) phi, theta = np.meshgrid(phi, theta) x = np.sin(phi) * np.cos(theta) y = np.sin(phi) * np.sin(theta) z = np.cos(phi) sh = getY(l, m)(x, y, z) #sh = Y0(x, y, z) #sh = Y1(x, y, z) sh = np.sqrt(2) * (-1)**m * sh.real fmax, fmin = sh.max(), sh.min() fcolors = sh.real if fmax == fmin : pass else: fcolors = (fcolors - fmin)/(fmax - fmin) mag = np.abs(sh) z = z * mag x = x * mag y = y * mag fig = plt.figure(figsize=plt.figaspect(1.)) ax = fig.add_subplot(111, projection='3d') ax.plot_surface(x, y, z, rstride=1, cstride=1, facecolors=cm.seismic(fcolors)) #ax.set_axis_off() plt.show()
def plot_category(plot_cat_ind, label_threshold=0.3): fig=plt.figure() ax=fig.add_subplot(111) abridged_corr_matrix= np.asarray(correlation_matrix)[plot_cat_ind] x_arr=np.linspace(0,N_cats-1,N_cats,endpoint=True) color_range=cm.seismic(np.linspace(0.05, 0.95, 40)) min_abs= abs(np.min(abridged_corr_matrix)) norm=np.max( [np.max(abridged_corr_matrix) , min_abs]) #print (np.max( np.max(abridged_corr_matrix) , abs(np.min(abridged_corr_matrix)))) for i in range(len(abridged_corr_matrix)): abridged_corr_matrix[i]=abridged_corr_matrix[i]/norm x_arr=np.append(x_arr, 100) x_arr=np.append(x_arr, 101) abridged_corr_matrix=np.append(abridged_corr_matrix, -1.) abridged_corr_matrix=np.append(abridged_corr_matrix, 1.) #plt.grid(linewidth=1.) plt.plot([-10,60],[0,0],':') plt.scatter(x_arr, abridged_corr_matrix, marker='o', s=200, linewidths=4, c=abridged_corr_matrix, cmap=plt.cm.seismic, edgecolors='grey', linewidth=1) #ax.scatter(x_arr, abridged_corr_matrix, 'o', color=abridged_corr_matrix, cmap=plt.cm.seismic) #ax = cb.ax plt.axis([-7, N_cats+5., -1.5, 1.5]) text = ax.yaxis.label font = mpl.font_manager.FontProperties(size=22) text.set_font_properties(font) plt.title('App usage amongst people who use '+whatcategory(plot_cat_ind)+' apps', fontsize=24) plt.ylabel(r'$\leftarrow$ Disfavor~~~~~~~~Favor$\rightarrow$',fontsize=22) for x in range(0,len(x_arr)-2): if abs(abridged_corr_matrix[x])>label_threshold: y_sign=abridged_corr_matrix[x]/abs(abridged_corr_matrix[x]) plt.text(x , abridged_corr_matrix[x]+0.2*y_sign,whatcategory(x), fontsize=20, horizontalalignment='center', verticalalignment='center') fig.set_size_inches(10,5)
def show_sphere(F): "show function on the sphere" N = F.shape[0] print N theta = np.linspace(0.,np.pi,N) phi = np.linspace(0.,2*np.pi,2*N) phi, theta = np.meshgrid(phi,theta) x = np.sin(theta) * np.cos(phi) y = np.sin(theta) * np.sin(phi) z = np.cos(theta) fig = plt.figure(figsize=plt.figaspect(1.)) ax = fig.add_subplot(111, projection='3d') ax.plot_surface(x, y, z, rstride=1, cstride=1, facecolors=cm.seismic(F)) ax.set_axis_off() plt.show()
def plot_spherical(_img, points): # img = np.double(np.reshape(_img, [64,64])) # phi = np.reshape(points[:, 1], [64, 64]) # theta = np.reshape(points[:, 0], [64, 64]) img = np.array(_img, dtype=float) phi = points[:, 1] theta = points[:, 0] # The Cartesian coordinates of the unit sphere x = np.sin(theta) * np.cos(phi) y = np.sin(theta) * np.sin(phi) z = np.cos(theta) fmax, fmin = img.max(), img.min() fcolors = (img - fmin)/(fmax - fmin) # Set the aspect ratio to 1 so our sphere looks spherical fig = plt.figure(figsize=plt.figaspect(1.)) ax = fig.add_subplot(111, projection='3d') ax.scatter(x, y, z, c=cm.seismic(fcolors)) # Turn off the axis planes ax.set_axis_off() plt.draw()
def visualize_data_points(N, D, K, data_points, cluster_ids): if D != 2: return # make true cluster figure plt.clf() for k in range(K): cluster_points = data_points[cluster_ids == k, :] plt.scatter(cluster_points[:, 0], cluster_points[:, 1], color=cm.seismic(k / (1.0 * K))) plt.xlim(-20, 20) plt.ylim(-20, 20) plt.savefig('fig/true_cluster.png', dpi=300) # make input figure plt.clf() plt.scatter(data_points[:, 0], data_points[:, 1]) plt.xlim(-20, 20) plt.ylim(-20, 20) plt.savefig('fig/input_data.png', dpi=300) return
def __getitem__(self, idx): y, sr = librosa.load("%s/%s" % (args.folder, self.fns[idx]), sr=None) S = librosa.feature.melspectrogram(y, sr=sr, n_mels=100) # Convert to log scale (dB). We'll use the peak power (max) as reference. log_S = librosa.power_to_db(S, ref=np.max) np.save("%s/S_%s_%d.npy" % (args.type, self.fns[idx], self.lbs[idx]), log_S) im = Image.fromarray( np.uint8(cm.seismic(normalize(log_S, -80, 0)) * 255)) arr = np.asarray( im.resize((input_size[0] * 4, input_size[-1]), Image.ANTIALIAS)) np.random.seed(100) indexes = self.num_first_components + \ list(sorted(np.random.choice(range(input_size[0]//3,input_size[0]*3,1),\ self.num_samples[self.lbs[idx]]*args.upsample,replace=False))) for step in range(len(indexes)): from_ = indexes[step] save_file = args.type+"/fea_%s_parts/%d_%s_%s_%d.npy" %\ (args.fea,step,self.fns[idx],args.fea,self.lbs[idx]) s_arr = arr[:, from_:from_ + input_size[-1]] np.save(save_file, s_arr) return self.fns[idx], self.lbs[idx]
def Plot(self): #Clear figure plt.clf() #Read in m and l m = int(self.Entm.get()) l = int(self.Entl.get()) #Define phi and theta theta = np.linspace(0, np.pi, 101) phi = np.linspace(0, 2 * np.pi, 101) theta, phi = np.meshgrid(theta, phi) #Calculate scalar spherical harmonic Y = scp.sph_harm(m, l, phi, theta) if self.ComplexPartBox.get() == 'Real': Y = Y.real elif self.ComplexPartBox.get() == 'Imag': Y = Y.imag elif self.ComplexPartBox.get() == 'Mod': Y = np.sqrt(Y.real**2 + Y.imag**2) #Normalize to between 0 - 1 Y_norm = (Y - Y.min()) / (Y.max() - Y.min()) #Convert to Cartesian x = np.sin(theta) * np.cos(phi) y = np.sin(theta) * np.sin(phi) z = np.cos(theta) #Setup plot for 3D surface ax = self.Fig.gca(projection='3d') ax.plot_surface(x, y, z, rstride=1, cstride=1, facecolors=cm.seismic(Y_norm)) #Plot ax.set_axis_off() plt.gcf().canvas.draw()
XX = 1 * np.outer(np.cos(u), np.sin(v)) YY = 1 * np.outer(np.sin(u), np.sin(v)) ZZ = 1 * np.outer(np.ones(np.size(u)), np.cos(v)) WW = np.zeros(shape=(np.shape(XX))) # populate the colormap and normalize for i in range(len(XX)): for j in range(len(XX[0])): x = XX[i, j] y = YY[i, j] z = ZZ[i, j] WW[i, j] = near(np.array([x, y, z]), pointList) WW = (WW - np.amin([int(np.min(pointList[:, 3]))])) WW = WW / np.amax( [int(np.max(pointList[:, 3])) - int(np.min(pointList[:, 3]))]) mycolors = cm.seismic(WW) # save the figure fig = plt.figure() ax = fig.add_subplot(1, 1, 1, projection='3d') ax.plot_surface(XX, YY, ZZ, cstride=1, rstride=1, facecolors=mycolors, shade=False) #ax.set_aspect(1) ax.set_xlim([-1.2, 1.2]) ax.set_ylim([1.2, -1.2]) ax.set_zlim([-1.2, 1.2])
verts[verts[:, 0] > 0, 0] += 20 verts[verts[:, 0] < 0, 0] -= 20 val = np.real(sig2[ind]) fig = plt.figure() ax = Axes3D(fig) ax.axis('equal') poly1 = plot_trimesh(verts, faces, val) val1 = np.array( val) # To be safe - set_array() will expect a numpy array #val1_norm = (val1-val1.min()) / (val1.max() - val1.min()) #val1_norm = val1 norm = colors.SymLogNorm(vmin=val1.min(), vmax=val1.max(), linthresh=0.1) colormap = cm.seismic(norm(val1)) colormap[:, -1] = 0.5 poly1.set_facecolor(colormap) poly1.set_edgecolor(None) poly1.set_linewidth(0.0) ax.add_collection3d(poly1) ax.set_xlim3d(verts[:, 0].min(), verts[:, 0].max()) ax.set_ylim3d(verts[:, 1].min(), verts[:, 1].max()) ax.set_zlim3d(verts[:, 2].min(), verts[:, 2].max()) ax.autoscale_view() plt.axis('off') plt.savefig(savedir + sim[:-4] + "_charge_at_" + str(int(round(wl[ind]))) + "nm.png", dpi=1200) plt.savefig(savedir + sim[:-4] + "_charge_at_" + str(int(round(wl[ind]))) + "nm.pgf")
fig, ax = plt.subplots(ncols=2) ax[0].scatter(nmr_probe.cells_x / mm, nmr_probe.cells_y / mm, c=(nmr_probe.cells_B0 - 1.45 * T) / T, cmap="jet") ax[0].set_aspect("equal") ax[1].scatter(nmr_probe.cells_z / mm, nmr_probe.cells_y / mm, c=(nmr_probe.cells_B0 - 1.45 * T) / T, cmap="jet") ax[1].set_aspect("equal") fig = plt.figure() ax = fig.add_subplot(111, projection='3d') X = (nmr_probe.cells_B0 - 1.45 * T) / T my_col = mcolors.seismic((X - np.amin(X)) / (np.amax(X) - np.amin(X))) ax.scatter(nmr_probe.cells_x / mm, nmr_probe.cells_y / mm, nmr_probe.cells_z / mm, c=my_col, marker="o") ax.set_xlim(-20, 20) ax.set_ylim(-20, 20) ax.set_zlim(-20, 20) plt.show() # calculate FID # trolly: 1 MSPS # fix probes: 10 MSPS --> totally oversampled times = np.linspace(0 * ms, 10 * ms, 10000) # 1 MSPS t_start = time.time()
def show_color(self, num=-1, displ='sput'): Z_ = self.Z_history[num] X_ = self.X Y_ = self.Y l_z = np.pad(Z_, ((0, 1), (0, 1)), mode='wrap') l_z += self.slope_corr_diff1 l_slopes_x = np.diff(l_z, 1, axis=1)[:-1] / self.dx l_slopes_y = np.diff(l_z, 1, axis=0)[:, :-1] / self.dx l_angles_x = np.arctan(l_slopes_x) l_angles_y = np.arctan(l_slopes_y) angles_x = (np.roll(l_angles_x, 1, axis=1) + l_angles_x) * 0.5 angles_y = (np.roll(l_angles_y, 1, axis=0) + l_angles_y) * 0.5 slopes_x = np.tan(angles_x) slopes_y = np.tan(angles_y) normal_magnitude = np.sqrt( np.power(slopes_x, 2) + np.power(slopes_y, 2) + 1.0) thetas = np.arccos(1.0 / normal_magnitude) if displ == 'moment': omegas = np.arctan2(slopes_y, slopes_x) omegas = np.abs(omegas) omegas[omegas >= np.pi * 0.5] = np.pi - omegas[omegas >= np.pi * 0.5] x_back_mask = slopes_x > 0.0 x_for_mask = np.logical_not(x_back_mask) y_back_mask = slopes_y > 0.0 y_for_mask = np.logical_not(y_back_mask) # ero_00 = (1.0-np.cos(4.0*thetas))*self.moment/(normal_magnitude*np.power(self.dx, 3)) # ANGLE NORMALIZATION INSIDE DEFINITION BELOW ero_00 = (self.mamp * self.flux_const * (1.0 / self.dx) * 0.5) * (1.0 - np.cos(4.0 * thetas)) sin_omega = np.sin(omegas) cos_omega = np.cos(omegas) acc_00 = (1 - sin_omega) * (1 - cos_omega) * ero_00 acc_01 = cos_omega * (1 - sin_omega) * ero_00 acc_10 = (1 - cos_omega) * sin_omega * ero_00 acc_11 = sin_omega * cos_omega * ero_00 # lets roll acc_01 = np.roll(x_for_mask*acc_01, (1, 0), axis=(1, 0)) \ + np.roll(x_back_mask*acc_01, (-1, 0), axis=(1,0)) acc_10 = np.roll(y_for_mask*acc_10, (0, 1), axis=(1,0)) \ + np.roll(y_back_mask*acc_10, (0, -1), axis=(1,0)) """ (-1, -1) | (-1, 0) | (-1, 1) ---------------------------- (0, -1) | | (0, 1) ---------------------------- (1, -1) | (1, 0) | (1, 1) """ acc_11 = np.roll(np.logical_and(x_for_mask, y_for_mask)*acc_11, (1, 1), axis=(1,0)) \ + np.roll(np.logical_and(x_for_mask, y_back_mask)*acc_11, (1, -1), axis=(1,0)) \ + np.roll(np.logical_and(x_back_mask, y_back_mask)*acc_11, (-1, -1), axis=(1,0)) \ + np.roll(np.logical_and(x_back_mask, y_for_mask)*acc_11, (-1, 1), axis=(1,0)) results = -ero_00 + acc_00 + acc_01 + acc_10 + acc_11 elif displ == 'sput': results = (self.yamp * self.flux_const) * yamamura( thetas, self.ytheta, self.f) res_max = np.max(results) res_min = np.min(results) print(res_max, res_min) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.set_aspect('equal') if displ == 'moment': abs_max = max(abs(res_min), abs(res_max)) normalized = (results / (2 * abs_max)) + 0.5 # 0 -> 255 digitized = np.array(255 * normalized, dtype=int) print(np.min(normalized), np.max(normalized)) print(np.min(digitized), np.max(digitized)) my_col = cm.seismic(digitized) elif displ == 'sput': my_col = cm.afmhot((results - res_min) / (res_max - res_min)) max_range = np.array( [X_.max() - X_.min(), Y_.max() - Y_.min(), Z_.max() - Y_.min()]).max() / 2.0 mid_x = (X_.max() + X_.min()) * 0.5 mid_y = (Y_.max() + Y_.min()) * 0.5 mid_z = (Z_.max() + Z_.min()) * 0.5 ax.set_xlim(mid_x - max_range, mid_x + max_range) ax.set_ylim(mid_y - max_range, mid_y + max_range) ax.set_zlim(mid_z - max_range, mid_z + max_range) surf = ax.plot_surface(X_, Y_, Z_, facecolors=my_col) #, lrstride=1, cstride=1, inewidth=0)#,antialiased=False plt.show() f, (a3, a4) = plt.subplots(1, 2) #a1.imshow(l_slopes_x) #a2.imshow(np.degrees(angles_x)) a3.imshow(np.degrees(thetas)) a4.imshow(results) plt.show()
#ind = np.abs(wl - w).argmin() #val = np.real((sig2[ind]+sig1[ind])/2) val = np.real(sig1[ind]) fig = plt.figure() #fig = plt.figure(figsize=plt.figaspect(1) * 1.5) # Adjusts the aspect ratio and enlarges the figure (text does not enlarge) #ax = fig.gca(projection='3d') ax = Axes3D(fig) #ax.set_aspect('equal') ax.axis('equal') poly1 = plot_trimesh(verts1,faces1,val[:len(faces1)]) val1 = np.array(val[:len(faces1)]) # To be safe - set_array() will expect a numpy array val1_norm = (val1-val1.min()) / (val1.max() - val1.min()) colormap = cm.seismic(val1_norm) colormap[:, -1] = 0.499*signal.sawtooth(2 * np.pi * val1_norm,0.5)+0.501 poly1.set_facecolor(colormap) poly1.set_edgecolor(None) ax.add_collection3d(poly1) verts = verts1 #ax.auto_scale_xyz([verts[:,0].max(),verts[:,0].min()],[verts[:,1].max(),verts[:,1].min()],[verts[:,2].max(),verts[:,2].min()]) ax.set_xlim3d(verts[:,0].min(), verts[:,0].max()) ax.set_ylim3d(verts[:,1].min(), verts[:,1].max()) ax.set_zlim3d(verts[:,2].min(), verts[:,2].max()) #ax.auto_scale_xyz(np.vstack((verts1[:, 0],verts2[:, 0])), np.vstack((verts1[:, 1],verts2[:, 1])), np.vstack((verts1[:, 2],verts2[:, 2]))) ax.axis('equal') #ax.auto_scale_xyz(np.vstack((verts1[:, 0],verts2[:, 0])), np.vstack((verts1[:, 1],verts2[:, 1])), np.vstack((verts1[:, 2],verts2[:, 2]))) ax.auto_scale_xyz(verts[:, 0], verts[:, 1], verts[:, 2])
fmax, fmin = fcolors.max(), fcolors.min() #fcolors = (fcolors) #fcolors = fcolors/(fmax-fmin) fcolors = (fcolors - fmin) / (fmax - fmin) fmax_2, fmin_2 = fcolors_2.max(), fcolors_2.min() #fcolors = (fcolors) #fcolors = fcolors/(fmax-fmin) fcolors_2 = (fcolors_2 - fmin_2) / (fmax_2 - fmin_2) # Set the aspect ratio to 1 so our sphere looks spherical #fig = plt.figure(figsize=plt.figaspect(1.)) fig = plt.figure(figsize=(10, 5)) ax = fig.add_subplot(1, 2, 1, projection='3d') ax.plot_surface(x, y, z, rstride=1, cstride=1, facecolors=cm.seismic(fcolors)) ax.view_init(45, 0) ax.set_axis_off() ax = fig.add_subplot(1, 2, 2, projection='3d') ax.plot_surface(x, y, z, rstride=1, cstride=1, facecolors=cm.seismic(fcolors)) ax.view_init(45, 180) ax.set_axis_off() plt.title( 'Parameters are: el_not=%s, tau=%s, lambda3=%s, lambda4=%s\nMinimum Hamiltonian value: %s' % (parameters_list[0], parameters_list[1], parameters_list[2], parameters_list[3], parameters_list[4]), fontsize=8) #plt.savefig("surface_pattern.png") plt.show()
# print esmall # print remove # # sys.exit() # pos=nx.spring_layout(G, k=.5,iterations=20, weight=20) # positions for all nodes # pos = nx.graphviz_layout(G, prog='neato') pos=nx.spring_layout(G, weight=20) # positions for all nodes # pos=nx.circular_layout(G) # positions for all nodes G.remove_nodes_from(remove) # cm.Spectral(betc_value) # node_color1 = [G.degree(v) for v in G] edgeColor = [cm.seismic(G.degree(v)) for v in G] # print node_color # print '!!!!!!!!!' # sys.exit() # nodes # nx.draw_networkx_nodes(G,pos,nodelist=greater1000Coll, node_size=10,alpha=0.6,node_color="#fdae61", with_labels=True, label="hello") node_color = nodeColors(G) nodes = nx.draw_networkx_nodes(G,pos, node_size=[float(G.degree(v)) * 1.5 for v in G], alpha=0.6, node_color=node_color, with_labels=True, label="Amount of collected material") # nx.draw_networkx_nodes(G,pos,nodelist=greater1000Coll, node_size=10,alpha=0.6,node_color="#FFFF00", with_labels=True, label="hello") nodes.set_edgecolor(node_color) # nx.draw_networkx_nodes(G,pos,nodelist=greater500Coll, node_size=[float(G.degree(v)) * 2.5 for v in G],alpha=0.6,node_color="#fdae61", with_labels=True, label=str(collSpecimenBreaks.bins[1]) + ' - ' + str(collSpecimenBreaks.bins[2] - 1)) # nx.draw_networkx_nodes(G,pos,nodelist=greater100Coll, node_size=[float(G.degree(v)) * 2.5 for v in G],alpha=0.6,node_color="#5e3c99", with_labels=True, label=str(collSpecimenBreaks.bins[1] -1) + ' - ' + str(collSpecimenBreaks.bins[0])) # nx.draw_networkx_nodes(G,pos,nodelist=less100Coll,node_size=[float(G.degree(v)) * 2.5 for v in G],alpha=0.6,node_color="#b2abd2", with_labels=True, label='<= ' + str(collSpecimenBreaks.bins[0] - 1))
# ============================================================================= # Charger les donnes pertinentes # ============================================================================= lda_documents_name = 'documents_lda_auto.csv' lda_topics_name = 'topics_lda_auto.csv' nb_topics = 30 lda_topics = pd.read_csv('data/' + lda_topics_name, index_col=0) lda_documents = pd.read_csv('data/' + lda_documents_name) products_table = pd.read_csv('data/produits_achats.csv', encoding="latin1") products_table = clean_table(products_table) products_table = products_table.loc[products_table['product'].isin( list(lda_topics.index.values))] dictionary_description = create_dict_to_keep( sorted(list(products_table['sousgroupe'].drop_duplicates()))) lda_households = np.asarray(lda_documents)[:, 1:] list_households = list(np.asarray(lda_documents)[:, 0]) list_households = [int(i) for i in list_households] households = import_households(['household', 'dpts', 'thab'], list_households) for i in range(30): dpts_dataframe = compute_household_variable_mean_lda( i, 'dpts', lda_households, households, list_households) dpts_dataframe['code_couleur'] = np.round(dpts_dataframe[1], 1) * 10 + 30 colors = cm.seismic(np.linspace(0, 1, 60)) draw_map(dpts_dataframe, i)
Total = np.array(Total) #Coordenadas del número total de células #%% # Plot network of interest plt.style.use('seaborn-whitegrid') #plt.clf() x = Total[:,0] y = Total[:,1] plt.plot([x],[y],'k.',ms=8) for link in zz: plt.plot((x[link[0]],x[link[1]]),(y[link[0]],y[link[1]]),'-',linewidth=0.8, c=cm.seismic(link[2]/2+0.57),lw=np.abs(link[2])*1) ##plt.colorbar() plt.grid(False) #plt.colorbar() #plt.show #%% # Histograma de densidad de la red, con funciones ajustadas plt.style.use('seaborn-whitegrid') fig, axes = plt.subplots( )
ret = sig(factor, a, b, c) if func_num == 39: #consecutive losses sad a = -30.0 * ((11.0 - mental) / 11.0 + 0.5) b = 1.0 / 80.0 c = 0.1 * (3.0 - 0.2 * (11.0 - mental)) ret = sig(factor, a, b, c) return ret if __name__ == '__main__': import matplotlib.pyplot as plt import matplotlib.cm as cm print(func_num[0][0]) print(np.linspace(0.01, 1, 100)) for i in range(40): val = np.zeros((201, 100)) val2 = np.zeros((201, 100)) for m in range(0, 10): for f in range(1, 100): #val[m][f] = func(inv_norm(i,f),m,i) val[m + 100][f] = func(inv_norm(i, f / 100.0), m, i) plt.plot(inv_norm(i, np.linspace(0.01, 1, 99)), val[m + 100, 1:], color=cm.seismic(m / 30.0)) #plt.plot(val2) plt.title(str(i) + func_name_str[i]) plt.show()
# PCA from sklearn.decomposition import PCA n = 3 # number of components we want pca = PCA(n_components=n) pca.fit(X_num_scaled) X2 = pca.transform(X_num_scaled) pca_components = pca.components_ movies_index = np.array(Y.index) ax1 = 0 ax2 = 1 max_movies = min(2500, len(X2)) plt.figure() for i, a in zip(movies_index[:max_movies], X2[:max_movies]): r = cm.seismic(Y_scaled[i]) plt.scatter(a[ax1], a[ax2], color=r) plt.text(a[ax1], a[ax2], X_str['movie_title'][i], color=r, fontsize=8) plt.xlim((min([a[ax1] for a in X2[:max_movies]]), max([a[ax1] for a in X2[:max_movies]]))) plt.ylim((min([a[ax2] for a in X2[:max_movies]]), max([a[ax2] for a in X2[:max_movies]]))) plt.xlabel('PCA Axis %d' % ax1) plt.ylabel('PCA Axis %d' % ax2) plt.show() from mpl_toolkits.mplot3d import Axes3D max_movies = min(500, len(X2)) fig = plt.figure() ax = fig.add_subplot(111, projection='3d')
base_path = sys.argv[1] CLIP = 0.2 SCALE = 1.0 / (2.0 * CLIP) with open(os.path.join(base_path, '0.sgz'), 'rb') as f: iline = InlineAccessor(f) islice_sgz = iline[iline.ilines[len(iline.ilines) // 2]] xline = CrosslineAccessor(f) xslice_sgz = xline[xline.xlines[len(xline.xlines) // 2]] zslice = ZsliceAccessor(f) zslice_sgz = zslice[zslice.zslices[len(zslice.zslices) // 2]] im = Image.fromarray( np.uint8( cm.seismic((islice_sgz.T.clip(-CLIP, CLIP) + CLIP) * SCALE) * 255)) im.save(os.path.join(base_path, 'out_inline-sgz-accessor.png')) im = Image.fromarray( np.uint8( cm.seismic((xslice_sgz.T.clip(-CLIP, CLIP) + CLIP) * SCALE) * 255)) im.save(os.path.join(base_path, 'out_xline-sgz-accessor.png')) im = Image.fromarray( np.uint8( cm.seismic((zslice_sgz.T.clip(-CLIP, CLIP) + CLIP) * SCALE) * 255)) im.save(os.path.join(base_path, 'out_zslice-sgz-accessor.png'))
from mpl_toolkits.mplot3d import Axes3D import numpy as np import scipy.special import spherical_harmonics # generate sampling points thetas = np.linspace( 0, np.pi, 100) phis = np.linspace( 0, 2*np.pi, 100) thetas, phis = np.meshgrid(thetas, phis) # Generate spherical harmonics values ylmvals = spherical_harmonics.ylm( 6, 6, thetas, phis).real # normalize to [0,1] for plotting fmax, fmin = ylmvals.max(), ylmvals.min() ylmvals = (ylmvals - fmin)/(fmax - fmin) # The Cartesian coordinates of the unit sphere x = np.sin(thetas) * np.cos(phis) y = np.sin(thetas) * np.sin(phis) z = np.cos(thetas) # Set the aspect ratio to 1 so our sphere looks spherical fig = plt.figure(figsize=plt.figaspect(1.)) ax = fig.add_subplot(111, projection='3d') ax.plot_surface(x, y, z, rstride=1, cstride=1, facecolors=cm.seismic(ylmvals)) # Turn off the axis planes ax.set_axis_off() plt.show()
import numpy as np from matplotlib import cm base_path = sys.argv[1] LINE_NO = int(sys.argv[2]) CLIP = 0.2 SCALE = 1.0/(2.0*CLIP) with SgzReader(os.path.join(base_path, '0.sgz')) as reader: t0 = time.time() slice_sgz = reader.read_anticorrelated_diagonal(LINE_NO) print("SgzReader took", time.time() - t0) im = Image.fromarray(np.uint8(cm.seismic((slice_sgz.T.clip(-CLIP, CLIP) + CLIP) * SCALE)*255)) im.save(os.path.join(base_path, 'out_ad-sgz.png')) with segyio.open(os.path.join(base_path, '0.sgy')) as segyfile: t0 = time.time() diagonal_length = get_anticorrelated_diagonal_length(LINE_NO, len(segyfile.ilines), len(segyfile.xlines)) slice_segy = np.zeros((diagonal_length, len(segyfile.samples))) if LINE_NO < len(segyfile.xlines): for d in range(diagonal_length): slice_segy[d, :] = segyfile.trace[LINE_NO + d*(len(segyfile.xlines) - 1)] else: for d in range(diagonal_length): slice_segy[d, :] = segyfile.trace[(LINE_NO - len(segyfile.xlines) + 1 + d) * len(segyfile.xlines) + (len(segyfile.xlines) - d - 1)] print("segyio took", time.time() - t0)