def plot_rv(mod): lvs = mod.right_eigenvectors_ nstates = min(5, lvs.shape[1]) fig, axes = plt.subplots(nrows=nstates, sharey=False, sharex=True) for idx, ax in enumerate(axes): ax.bar(range(lvs.shape[0]), lvs[:, idx]) plt.savefig('msm-rvs.png')
def example_image(graph, filename, layout='spring', edge_labels=False, node_labels=False, show=False): """Generates example image with graph. Uses nx.draw_networkx_* methods, and matplotlib to draw and save the image. """ # positions for all nodes pos = LAYOUT_DICT[layout](graph) # configure the image plt.figure(figsize=(2, 2)) plt.axis('off') # draw all of the things! nx.draw_networkx_nodes(graph, pos, nodelist=graph.nodes(), node_color='r') nx.draw_networkx_edges(graph, pos, width=1.0, alpha=0.5, arrows=True) if node_labels: nlabels = {node: str(node) for node in graph.nodes()} nx.draw_networkx_labels(graph, pos, nlabels, font_size=16) if edge_labels: elabels = {edge: str(idx) for idx, edge in enumerate(graph.edges())} nx.draw_networkx_edge_labels(graph, pos, elabels) # place the file where it belongs path = os.path.join(os.environ['ERDOS_PATH'], "content/images", filename) plt.savefig(path) if show: plt.show()
def saveMagPhaseImage3(self, image1, image2, image3, filename): mag = absolute(image1).astype('float') phase = angle(image1).astype('float') mag2 = absolute(image2).astype('float') phase2 = angle(image2).astype('float') mag3 = absolute(image3).astype('float') phase3 = angle(image3).astype('float') plt.subplot(321) plt.imshow(mag, cmap=cm.Greys_r) plt.title('Magnitude') plt.axis('off') plt.subplot(322) plt.imshow(phase, cmap=cm.Greys_r) plt.title('Phase') plt.axis('off') plt.subplot(323) plt.imshow(mag2, cmap=cm.Greys_r) plt.title('Magnitude') plt.axis('off') plt.subplot(324) plt.imshow(phase2, cmap=cm.Greys_r) plt.title('Phase') plt.axis('off') plt.subplot(325) plt.imshow(mag3, cmap=cm.Greys_r) plt.title('Magnitude') plt.axis('off') plt.subplot(326) plt.imshow(phase3, cmap=cm.Greys_r) plt.title('Phase') plt.axis('off') plt.savefig(filename)
def compare_MC_exact(): max_error = [] sum_error = [] walkers = [] for i in range(500,20000,500): d1 = Diffusion(t=0.2) t_exact,u_exact = d1.exact() t_uniform , mc_uniform = d1.MC_uniform(i) print len(u_exact) print len(mc_uniform) diff = u_exact[:] - mc_uniform[:] temp = max(abs(diff)) max_error.append(temp) temp = sum(diff) sum_error.append(temp) temp = i print temp walkers.append(temp) from matplotlib.pylab import plt plt.figure(2) plt.plot(walkers,max_error,'o-') plt.xlabel('Number of walkers') plt.ylabel('Maximum error') plt.savefig('mcuniform_error1.eps') plt.figure(3) plt.plot(walkers,sum_error,'o-') plt.xlabel('Number of walkers') plt.ylabel('Accumulated error') plt.savefig('mcuniform_error2.eps')
def plot_metrics(name, data): # for metric in data.columns: for i in range(2, len(data.columns) - 1): plt.ylim([0, 1]) plt.plot(data[data.columns[i]], label=data.columns[i]) plt.savefig('./run/' + name + data.columns[i] + '.png') plt.clf()
def pretty_plot_metrics(name, data): plt.plot(data['labeled_acc'], '-', color='b', label='Labeled Accuracy') plt.plot(data['labeled_loss'], ':', color='b', label='Labeled Loss') plt.plot(data['unlabeled_acc'], '-', color='r', label='Unsupervised Accuracy') plt.plot(data['unlabeled_loss'], ':', color='r', label='Unsupervised Loss') plt.legend() plt.savefig(name + '.png')
def multiplot_gen_property_type(): # # font = {'family': 'Liberation Serif', # 'weight': 'normal', # 'size': 15 # } # # # play around with the font size if it is too big or small # matplotlib.rcParams['axes.titlesize'] = 12 # matplotlib.rcParams['axes.labelsize'] = 12 # matplotlib.rc('font', **font) # # matplotlib.rcParams['text.usetex'] = True # matplotlib.rcParams['pdf.fonttype'] = 42 # matplotlib.rcParams['pdf.use14corefonts'] = True x = list(data.keys()) y1=[] y2=[] y3=[] y4=[] y5=[] y6=[] for year in data.keys(): for option_name, count in data[year].items(): if option_name == 'domain': y1.append(count) if option_name == 'sitekey': y2.append(count) if option_name == 'third-party': y3.append(count) if option_name == 'websocket': y4.append(count) if option_name == 'webrtc': y5.append(count) if option_name == 'csp': y6.append(count) plt.plot(x, y1,'-o',label='domain') plt.plot(x, y2,'-v',label='sitekey') plt.plot(x, y3,'-^',label='third-party') plt.plot(x, y4,'-<',label='websocket') plt.plot(x, y5,'->',label='webrtc') plt.plot(x, y6,'-1',label='csp') plt.xticks(rotation='vertical') plt.xlabel('Year') plt.ylabel('Count') plt.legend(ncol=2) plt.tight_layout() plt.savefig('easylist-property-type.pdf ', format='pdf', dpi=1200)
def Brightness_map(image, image_name): # map = (image[:, :, 0] + image[:, :, 1] + image[:, :, 2]) / 3 map = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) map = map.reshape(image.shape[0] * image.shape[1], 1) km = KMeans(n_clusters=16).fit(map) map = km.labels_.reshape(image.shape[0], image.shape[1]) plt.imshow(map / map.max(), cmap='gray') plt.axis('off') plt.savefig(image_name + '_brightness_map') np.save(image_name + '_brightness_map.npy', map)
def plot(ranks): plt.figure(figsize=(20, 10)) plt.title("A2.3 PageRank (s-Sensibility)") plt.xlabel("Node ID") plt.ylabel("Pagerank") for row in ranks: plt.plot(row) plt.legend(['s = %.2f' % s for s in numpy.arange(0.0, 1.0, 0.05)], loc='upper right', prop={'size': 7}) plt.savefig("submission/pagerank.png")
def plot(data, fname): plt.clf() width = 10 data = np.array(data) bins = np.arange(0, max(data), step=width) sns.distplot(data, norm_hist=True, kde=True, bins=bins, label='No ones') plt.ylim((0, 0.001)) plt.xlabel('First passage time (ps)') plt.text( 1000, 0.0005, 'MFPT = {0:4.2f} +/- {1:4.2f} ps'.format(data.mean(), 2 * data.std())) plt.savefig(fname, transparanet=True)
def saveMagPhaseImage(self, image, filename): mag = absolute(image).astype('float') phase = angle(image).astype('float') plt.subplot(211) plt.imshow(mag, cmap=cm.Greys_r) plt.title('Magnitude') plt.axis('off') plt.subplot(212) plt.imshow(phase, cmap=cm.Greys_r) plt.title('Phase') plt.axis('off') plt.savefig(filename)
def texture_ID(image, map, image_name): for ch in range(3): channel = map[:, :, ch, :] channel = channel.reshape(image.shape[0] * image.shape[1], map.shape[-1]) km = KMeans(n_clusters=64).fit(channel) T_channel = km.labels_.reshape(image.shape[0], image.shape[1]) image[:, :, ch] = T_channel plt.imshow(image/image.max()) plt.axis('off') plt.savefig(image_name + '_texture_map') np.save(image_name + '_texture_map.npy', image)
def save_plot(filter_bank, name): rows , cols = filter_bank.shape[0:2] plt.figure() sub = 1 for row in range(rows): for col in range(cols): plt.subplot(rows, cols, sub) plt.imshow(filter_bank[row][col], cmap='gray') plt.axis('off') sub += 1 plt.savefig(name)
def Color_map(image, image_name): for ch in range(3): channel = image[:, :, ch] channel = channel.reshape(image.shape[0] * image.shape[1], 1) km = KMeans(n_clusters=16).fit(channel) T_channel = km.labels_.reshape(image.shape[0], image.shape[1]) image[:, :, ch] = T_channel plt.imshow(image / image.max()) plt.axis('off') plt.savefig(image_name + '_color_map') np.save(image_name + '_color_map.npy', image)
def graph(train_df, test_df, p_forecast, f_forecast, metric, key): fig = plt.figure(figsize=(40,10)) forecast_ds = np.array(f_forecast["ds"]) print(len(forecast_ds)) print(len(train_df)) forecast_ds = forecast_ds[int(train_df["values"].count()):] plt.plot(np.array(train_df["ds"]), np.array(train_df["y"]),'b', label="train", linewidth=3) plt.plot(np.array(test_df["ds"]), np.array(test_df["y"]), 'k', label="test", linewidth=3) plt.savefig( "../testing/compare_fourier_prophet/" + str(key) + "_raw_" + metric + ".png", transparent=True) prophet = np.array(p_forecast["yhat"]) prophet_upper = np.array(p_forecast["yhat_upper"]) prophet_lower = np.array(p_forecast["yhat_lower"]) fourier = f_forecast["yhat"] fourier = fourier[len(train_df["values"]):] print(len(forecast_ds)) print(len(fourier)) plt.plot(forecast_ds, fourier, 'g', label="fourier_yhat", linewidth=3) plt.savefig( "../testing/compare_fourier_prophet/" + str(key) + "_fourier_" + metric + ".png", transparent=True) prophet = prophet[len(train_df["values"]):] prophet_upper = prophet_upper[len(train_df["values"]):] prophet_lower = prophet_lower[len(train_df["values"]):] plt.plot(forecast_ds, prophet, '*y', label="prophet_yhat", linewidth=3) plt.plot(forecast_ds, prophet_upper, 'y', label="yhat_upper", linewidth=3) plt.plot(forecast_ds, prophet_lower, 'y', label="yhat_lower", linewidth=3) plt.plot() plt.xlabel("Timestamp") plt.ylabel("Value") plt.legend(loc=1) plt.title("Prophet Model Forecast") plt.savefig( "../testing/compare_fourier_prophet/" + str(key) + "_compare_" + metric + ".png", transparent=True) plt.close() fig = plt.figure(figsize=(40,10)) forecast_ds = np.array(f_forecast["ds"]) forecast_ds = forecast_ds[len(train_df["values"]):] plt.plot(np.array(train_df["ds"]), np.array(train_df["y"]),'b', label="train", linewidth=3) plt.plot(np.array(test_df["ds"]), np.array(test_df["y"]), 'k', label="test", linewidth=3) prophet = np.array(p_forecast["yhat"]) prophet_upper = np.array(p_forecast["yhat_upper"]) prophet_lower = np.array(p_forecast["yhat_lower"]) prophet = prophet[len(train_df["values"]):] prophet_upper = prophet_upper[len(train_df["values"]):] prophet_lower = prophet_lower[len(train_df["values"]):] plt.plot(forecast_ds, prophet, '*y', label="prophet_yhat", linewidth=3) plt.plot(forecast_ds, prophet_upper, 'y', label="yhat_upper", linewidth=3) plt.plot(forecast_ds, prophet_lower, 'y', label="yhat_lower", linewidth=3) plt.savefig( "../testing/compare_fourier_prophet/" + str(key) + "_prophet_" + metric + ".png", transparent=True) plt.close()
def plot_average(collected_results, versions, args, plot_std=True): test_type = args.test_type model_name = args.model means, stds = [], [] for version in versions: data = collected_results[version] if (plot_std): means.append(np.mean(data)) stds.append(np.std(data)) else: means.append(data) means = np.array(means) stds = np.array(stds) if (test_type == "size" or test_type == "allsize"): x = ["0%", "20%", "40%", "60%", "80%", "100%"] elif (test_type == "accdomain" or test_type == "moredomain"): x = [0, 1, 2, 3, 4] else: x = versions color = 'blue' plt.plot(x, means, color=color) if (plot_std): plt.fill_between(x, means - stds, means + stds, alpha=0.1, edgecolor=color, facecolor=color, linewidth=1, antialiased=True) plt.xticks(np.arange(len(x)), x, fontsize=18) plt.yticks(fontsize=18) plt.xlabel(XLABELS[test_type], fontsize=18) plt.ylabel('average absolute effect size', fontsize=18) plt.title("Influence of {} on bias removal \nfor {}".format( TITLES[test_type], MODEL_FORMAL_NAMES[model_name]), fontsize=18) plt.tight_layout() plot_path = os.path.join( args.eval_results_dir, "plots", "{}-{}-avg{}.png".format(model_name, test_type, "-std" if plot_std else "")) plt.savefig(plot_path)
def saveMagPhasePlot(self, t, x, filename): mag = absolute(x).astype('float') phase = angle(x).astype('float') plt.subplot(211) plt.plot(t, mag) plt.ylabel('Magitude') plt.title('SSPF Sequence') plt.grid(True) plt.subplot(212) plt.plot(t, phase) plt.xlabel('Off-Resonance (Hz)') plt.ylabel('Phase') plt.grid(True) plt.savefig(filename)
def plot_score_dist(spacing, std_along, prob_miss, max_distance): from matplotlib.pylab import plt plt.close("Score Dist") plt.figure("Score Dist") d = np.linspace(0, max_distance, 500) plt.plot(d, [score_dist(di, spacing, std_along, prob_miss) for di in d]) plt.vlines(spacing, 0, 1) plt.vlines(spacing * 2, 0, 1, ls='--') plt.annotate("Miss-detect the next mine", (spacing * 2, 0.5), (12, 0), textcoords='offset points') plt.ylabel('$p(d)$') plt.xlabel('$d$') plt.grid() plt.xticks(np.arange(max_distance)) plt.xlim(0, max_distance) plt.savefig('score_dist.pdf')
def Gradient(image_name, map_name): mask_filters = Half_disk_masks(3, 8, plot=False) map = np.load(image_name + '_' + map_name + '_map.npy') # plt.imshow(map / map.max(), cmap='gray') # plt.savefig(image_name + '_map') gradients = [] for row in range(3): for col in range(4): left_mask = mask_filters[row][2 * col] right_mask = mask_filters[row][2 * col + 1] chi_sqr_dist = map * 0 k = map.max() + 1 for bin in range(k): bin_chi_dist = map * 0 temp = np.sign(-1 * (map - bin)) + 1 g_i = cv2.filter2D(temp, -1, left_mask) h_i = cv2.filter2D(temp, -1, right_mask) num = np.square(h_i - g_i) denom = 1. / (g_i + h_i + 0.000005) bin_chi_dist = np.multiply(num, denom) # for x in range(temp.shape[0]): # for y in range(temp.shape[1]): # for z in range(temp.shape[2]): # if g_i[x][y][z] + h_i[x][y][z] != 0: # bin_chi_dist[x][y][z] = (g_i[x][y][z] - h_i[x][y][z]) ** 2 / (g_i[x][y][z] + h_i[x][y][z]) chi_sqr_dist = chi_sqr_dist + bin_chi_dist / k gradients.append(chi_sqr_dist) gradient_map = np.mean(np.array(gradients), axis=0) if map_name == 'brightness': plt.imshow(gradient_map / gradient_map.max(), cmap='gray') else: plt.imshow(gradient_map / gradient_map.max()) plt.axis('off') plt.savefig(image_name + '_' + map_name + '_gradient_map') np.save(image_name + '_' + map_name + '_gradient.npy', gradient_map)
def plot_rmsd_trajectory(): cluster_labels = np.arange(1,num_clusters,2) # Plot trajectories print('Charting trajectories') sample = df.sample(frac=0.1, axis=0) sample.sort_values(by=['Prod_ID', 'Site_ID', 'Time_ps'], inplace=True) with sns.plotting_context("notebook", font_scale=2): g = sns.FacetGrid(sample, col='Prod_ID',hue='Site_ID', col_wrap=10) g.map(plt.scatter, 'Time_ps', 'Trajectory', alpha=0.5) g.set(ylim=(-0.5,num_clusters), yticks=cluster_labels) g.set_ylabels("Cluster") g.set_xlabels("Time $ps$") g.set_titles("") g.fig.subplots_adjust(wspace=0.05, hspace=0.05) plt.savefig('figures/rmsd_cluster_trajectory.png', transparent=True)
def plt_score(history): plt.figure() plt.plot() plt.plot(history.history['acc']) plt.plot(history.history['val_acc']) plt.title('model accuracy') plt.ylabel('accuracy') plt.xlabel('epoch') plt.legend(['train', 'test'], loc='upper left') plt.savefig('acc.png') # loss plt.figure() plt.plot(history.history['loss']) plt.plot(history.history['val_loss']) plt.title('model loss') plt.ylabel('loss') plt.xlabel('epoch') plt.legend(['train', 'test'], loc='upper left') plt.savefig('loss.png')
def pb_lite(Sobel, Canny, image_name): B = np.load(image_name + '_brightness_gradient.npy') T = np.load(image_name + '_texture_gradient.npy') C = np.load(image_name + '_color_gradient.npy') B / B.max() T / T.max() C / C.max() w1 = 0.5 w2 = 0.5 features = (B + rgb2gray(T) + rgb2gray(C))/3 average = w1 * Canny + w2 * Sobel pb_edge = features * rgb2gray(average) plt.imshow(pb_edge, cmap='gray') plt.savefig(image_name + '_pb_edge') s = 1
def bar_graph(category, age_grp, sex, x, y, year=None, country=None): plt.figure() plt.ylabel('ATE = Y1 - Y0') plt.xlabel('Years') plt.bar(range(len(x)), x, align='center') plt.xticks(range(len(x)), y, rotation='vertical') if country: plt.title("%s Suicide Rates for WC; %s ages %s" % (country, sex, age_grp)) name = country + sex + age_grp + '.png' # plt.show() plt.tight_layout() plt.savefig('./graphs/Countries' + '/' + sex + '/' + name.replace(' ', '_')) elif year: plt.title("Change in Suicide Rates per Country in %s; %s ages %s" % (year, sex, age_grp)) name = category + sex + str(year) + age_grp + '.png' # plt.show() plt.tight_layout() plt.savefig('./graphs/' + category + '/' + sex + '/' + str(year) + '/' + name.replace(' ', '')) else: plt.title("Change in Suicide Rates in %s Countries; %s ages %s" % (category, sex, age_grp)) name = category + sex + age_grp + '.png' # plt.show() plt.tight_layout() plt.savefig('./graphs/' + category + '/' + sex + '/' + name.replace(' ', ''))
def visualize(img, G, vertices, fn): plt.imshow(img) # draw edges by pts for (s, e) in G.edges(): vals = flatten([[v] for v in G[s][e].values()]) for val in vals: ps = val.get('pts', []) plt.plot(ps[:, 1], ps[:, 0], 'green') # draw node by o #node, nodes = G.node(), G.nodes # deg = G.degree # ps = np.array([node[i]['o'] for i in nodes]) ps = np.array(vertices) plt.plot(ps[:, 1], ps[:, 0], 'r.') # title and show img_name = fn.split('.')[0] + ".png" map_name = fn.split('.')[0] + "_roads.png" io.imsave(img_name, img) plt.savefig(map_name)
def print_all_results(collected_results, versions, args): test_type = args.test_type model_name = args.model for test_name in collected_results: test_results = collected_results[test_name] x, y = [], [] for version in versions: if (version in test_results): x.append(version) y.append(test_results[version]['mean']) plt.plot(x, y, label=test_name) plt.xticks(np.arange(len(x)), x) plt.xlabel(XLABELS[test_type]) plt.ylabel('average absolute effect size') plt.legend(loc='best') plt.title("SEAT effect sizes on {} with {}".format(model_name, TITLES[test_type])) plot_path = os.path.join(args.eval_results_dir, "plots", "{}-{}.png".format(model_name, test_type)) plt.savefig(plot_path)
def make_radar(skill: int, strength: int, defence: int, willpower: int, attack: int, stamina: int): ''' :return: PNG type image binary content ''' value = [skill, strength, defence, willpower, attack, stamina] if not all(map(lambda x: isinstance(x, int) and 0 < x <= 100, value)): return font = FontProperties(fname=settings.PINGFANG_FONT, size=23) plt.figure(figsize=(4.8, 4.8)) # 图片大小 name = [ '技术\n ', '力量 ', '防守 ', '\n意志力', ' 进攻 ', ' 耐力 ' ] # 标签 theta = np.linspace(0, 2 * np.pi, len(name), endpoint=False) # 将圆周根据标签的个数等比分 theta = np.concatenate((theta, [theta[0]])) # 闭合 value = np.concatenate((value, [value[0]])) # 闭合 ax = plt.subplot(111, projection='polar') # 构建图例 ax.set_theta_zero_location('N') # 设置极轴方向 ax.fill(theta, value, color="#EF2D55", alpha=0.35) # 填充色,透明度 for i in [20, 40, 60, 80, 100]: # 绘等分线 ax.plot(theta, [i] * (6 + 1), 'k-', lw=1, color='#8989A3') # 之所以 n +1,是因为要闭合! ax.plot(theta, value, 'ro-', 'k-', lw=1, alpha=0.75, color='#FF465C') # 绘数据图 ax.set_thetagrids(theta * 180 / np.pi, name, fontproperties=font, color='#8989A3') # 替换标签 ax.set_ylim(0, 100) # 设置极轴的区间 ax.spines['polar'].set_visible(False) # 去掉最外围的黑圈 ax.grid(True, color='#8989A3', linestyle='-', linewidth=1) ax.set_yticks([]) buf = io.BytesIO() plt.savefig(buf, transparent=True) # 透明 plt.close('all') # 关闭所有绘图 buf.seek(0) return buf
def plot_result(y, result, threshold, title): """ :param title: :return: 蓝色为原始数据, 亮绿色为均值, 绿色为正常范围上界,蓝色为正常范围下界 """ avg = result[AVG_FILTER] std = result[STD_FILTER] upper_bound = avg + threshold * std lower_bound = avg - threshold * std print("upper_bound: %s, lower_bound: %s" % (upper_bound, lower_bound)) signals = result[Z_SCORE_SIGNALS] data_points_number = np.arange(1, len(y) + 1) plt.subplot(211) plt.plot(data_points_number, y) plt.plot(data_points_number, avg, color="cyan", lw=2) plt.plot(data_points_number, upper_bound, color="green", lw=2) plt.plot(data_points_number, lower_bound, color="blue", lw=2) plt.subplot(212) plt.step(data_points_number, signals, color="red", lw=2) plt.ylim(-1.5, 1.5) plt.savefig(title) plt.show()
def plot_cluster_trajectory(df): cluster_labels = df['Trajectory'].unique() ymin = np.min(cluster_labels) - 0.5 ymax = np.max(cluster_labels) + 0.5 # Plot trajectories print('Charting trajectories') # sample = df.iloc[0:-1:2,:] long_trajs = ['{}.1'.format(x) for x in range(1, 11)] sample = df.ix[df['Prod_ID'].isin(long_trajs), :] sample.sort_values(by=['Prod_ID', 'Site_ID', 'Frame'], inplace=True) print(sample.head()) with sns.plotting_context("notebook", font_scale=2): g = sns.FacetGrid(sample, col='Prod_ID', hue='Site_ID', col_wrap=5) g.map(plt.scatter, 'Frame', 'Trajectory', alpha=0.5, marker='o', s=5) g.set(ylim=(ymin, ymax), yticks=cluster_labels) g.set_ylabels("Cluster") g.set_xlabels("Time $ps$") g.set_titles("") g.fig.subplots_adjust(wspace=0.05, hspace=0.05) plt.savefig('figures/pcca_cluster_trajectory.png', transparent=True)
def saveWaterFatImage(self, filename): mag = np.absolute(self.water).astype('float') phase = np.angle(self.water).astype('float') mag2 = np.absolute(self.fat).astype('float') phase2 = np.angle(self.fat).astype('float') plt.subplot(221) plt.imshow(mag, cmap=cm.Greys_r) plt.title('Water Magnitude') plt.axis('off') plt.subplot(222) plt.imshow(phase, cmap=cm.Greys_r) plt.title('Water Phase') plt.axis('off') plt.subplot(223) plt.imshow(mag2, cmap=cm.Greys_r) plt.title('Fat Magnitude') plt.axis('off') plt.subplot(224) plt.imshow(phase2, cmap=cm.Greys_r) plt.title('Fat Phase') plt.axis('off') plt.savefig(filename)
def example_pic(): """Generate the example graph picture.""" # create graph from edge list graph = nx.Graph([(0, 1), (0, 2), (1, 3), (3, 0)]) # positions for all nodes pos = nx.spring_layout(graph) # each node is labaled by its own name labels = {node: str(node) for node in graph.node.keys()} # configure the image plt.figure(figsize=(2, 2)) plt.axis('off') # draw all of the things! nx.draw_networkx_nodes(graph, pos, nodelist=[0, 1, 2, 3], node_color='r') nx.draw_networkx_edges(graph, pos, width=1.0, alpha=0.5) nx.draw_networkx_labels(graph, pos, labels, font_size=16) # place the file where it belongs path = os.path.join(os.environ['ERDOS_PATH'], "content/images", "nodes_edges_example.png") plt.savefig(path)
def example_image(graph, filename, layout='spring', edge_labels=False, node_labels=False, show=False): """Generates example image with graph. Uses nx.draw_networkx_* methods, and matplotlib to draw and save the image. """ # positions for all nodes pos = LAYOUT_DICT[layout](graph) # configure the image plt.figure(figsize=(2, 2)) plt.axis('off') # draw all of the things! nx.draw_networkx_nodes(graph, pos, nodelist=graph.nodes(), node_color='r') nx.draw_networkx_edges(graph, pos, width=1.0, alpha=0.5, arrows=True) if node_labels: nlabels = {node: str(node) for node in graph.nodes()} nx.draw_networkx_labels(graph, pos, nlabels, font_size=16) if edge_labels: elabels = {edge: str(idx) for idx, edge in enumerate(graph.edges())} nx.draw_networkx_edge_labels(graph, pos, elabels) # place the file where it belongs path = os.path.join(os.environ['ERDOS_PATH'], "content/images", filename) plt.savefig(path) if show: plt.show()
def pretty_plot_compare3_loss(name): path = './run/' file_name = 'model_007' data1 = pd.read_csv(path + file_name + '.log') file_name = 'model_010' data2 = pd.read_csv(path + file_name + '.log') file_name = 'model_021' data3 = pd.read_csv(path + file_name + '.log') metrics = [ 'labeled_loss', 'labeled_acc', 'unlabeled_loss', 'unlabeled_acc' ] plt.plot(data1['labeled_loss'], ':', color='b', label='Alelic Frequency 0.01 Labeled Loss') plt.plot(data2['labeled_loss'], ':', color='r', label='Alelic Frequency 0.10 Labeled Loss') plt.plot(data3['labeled_loss'], ':', color='g', label='Alelic Frequency 0.21 Labeled Loss') plt.plot(data1['unlabeled_loss'], '-', color='b', label='Alelic Frequency 0.01 Unsupervised Loss') plt.plot(data2['unlabeled_loss'], '-', color='r', label='Alelic Frequency 0.10 Unsupervised Loss') plt.plot(data3['unlabeled_loss'], '-', color='g', label='Alelic Frequency 0.21 Unsupervised Loss') # plt.legend() plt.savefig(name + '.png')
def rvc_analysis(random_seed, save_path): # Load the data # TODO: change the path save_path = os.path.join(save_path, 'random_seed_%03d' %random_seed) print('Random seed: %03d' %random_seed) # Load the saved validation dataset project_ukbio_wd, project_data_ukbio, _ = get_paths(debug, dataset) with open(os.path.join(save_path, 'splitted_dataset_%s.pickle' %dataset), 'rb') as handle: splitted_dataset = pickle.load(handle) # Train the model model = RVC(kernel='linear') model.fit(splitted_dataset['Xtrain_scaled'], splitted_dataset['Ytrain']) # make cross validated predictions print('Perform prediction in test data') y_prediction_test = model.predict(splitted_dataset['Xtest_scaled']) y_prediction_validation = model.predict(splitted_dataset['Xvalidate_scaled']) # ----------------------------------------------------------------------------- # Do some statistics. Calculate the confusion matrix # Test dataset # Look at the confusion matrix for test data class_name = np.array(['young', 'old', 'adult'], dtype='U10') ax, cm_test = plot_confusion_matrix(splitted_dataset['Ytest'], y_prediction_test, classes=class_name, normalize=True) # Look at accuracy accuracy_test = accuracy_score(splitted_dataset['Ytest'], y_prediction_test) plt.savefig(os.path.join(save_path, 'confusion_matrix_test_rvc.eps')) # Predict on the validation dataset ax, cm_validation = plot_confusion_matrix(splitted_dataset['Yvalidate'], y_prediction_validation, classes=class_name, normalize=True) plt.savefig(os.path.join(save_path, 'confusion_matrix_validation_rvc.eps')) # Look at accuracy accuracy_val = accuracy_score(splitted_dataset['Yvalidate'], y_prediction_validation) plt.savefig(os.path.join(save_path, 'confusion_matrix_test_rvc.eps')) return cm_test, cm_validation, accuracy_test, accuracy_val
emotion: per_emotion_data[emotion][feature] for emotion in EMOTIONS } data = { emotion: [convert_to_list(vid) for vid in data[emotion]] for emotion in EMOTIONS } data = {emotion: np.array(data[emotion]) for emotion in EMOTIONS} for emotion in EMOTIONS: print(emotion) print("Plotting for emotion " + emotion) emotion_data = data[emotion] for vid in emotion_data: plt.plot(np.arange(len(vid)), vid, label=emotion, color=colors[emotion]) plt.title(feature + " over time for subject " + subject) # plt.legend([colors[emotion] for emotion in EMOTIONS], EMOTIONS) plt.legend() print('Saving plot...') if 'Neck/Nose' in feature: feature_name = feature.replace('Neck/Nose', 'Neck') else: feature_name = feature plt.savefig(plots_dir + "/" + subject + "/" + feature_name + '.png') plt.close() print('Done.')
from sklearn.metrics import accuracy_score, f1_score, recall_score, precision_score accuracy_score(predictions,truth) f1_score(predictions,truth,average="weighted") recall_score(predictions,truth,average="weighted") precision_score(predictions,truth,average="weighted") from sklearn.metrics import roc_curve, auc from matplotlib.pylab import plt import seaborn as sns all_categories = np.unique(articles["category"]) fig=plt.figure(figsize=(8,8)) ax=fig.add_subplot(1,1,1) plt.tick_params(labelsize=20) for i in range(len(all_categories)): mask = y_test == all_categories[i] prob_category = clf.predict_proba(X_test)[:,i] dummy_y = np.ones(len(y_test)) dummy_y[~mask] = 0 fpr, tpr, thresholds = roc_curve(dummy_y, prob_category) ax.plot(fpr,tpr,label=all_categories[i]+", AUC = "+str(round(auc(fpr,tpr),2)),lw=2) ax.legend(loc=4,fontsize="large") ax.plot([0, 1], [0, 1], "k--") ax.set_xlim([0.0, 1.0]);ax.set_ylim([0.0, 1.05]) ax.set_xlabel("False Positive Rate",fontsize=20); ax.set_ylabel("True Positive Rate",fontsize=20) plt.savefig("roc_curve_body.png",bbox_inches="tight") plt.show()
def plot(self, **kwargs): """A wrapper function for plotting a NetworkX graph This function will draw a provided NetworkX graph using either the spring layout algorithm, or by the positions provided. Parameters ---------- pos: dict, optional, default: networkx.drawing.layout.spring_layout A dictionary of the network's neurons as keys and their (x, y) coordinates as corresponding values. nodelist: list, optional, default: self.neurons Draw only specified neurons (nodes). node_size: int or list, optional, default: 300 The size of the plotted neurons in the network. node_color: (color string, or array of floats), optional, default: 'r' Can either be a single color format string (default=’r’), or a sequence of colors with the same length as nodelist. If numeric values are specified they will be mapped to colors using the cmap and vmin, vmax parameters. See matplotlib.scatter for more details. cmap: Matplotlib colormap, optional, default: None Colormap for mapping intensities of nodes. alpha: float, optional, default: 1.0 The transparency of the nodes. node_borders: (None, scalar, or sequence), optional, default: 'black' The color(s) of the node borders. edgelist: (collection of edge tuples), optional, default: self.connections Draw only specified edges. By default, the edges between all nodes will be drawn. If `[]` (i.e. empty list), then the edges between all nodes will be omitted from the figure. edge_alpha: float, optional, default is 1.0 The transparency of the edges. edge_color: (color string, or array of floats), default: 'r' Can either be a single color format string, or a sequence of colors with the same length as edgelist. If numeric values are specified they will be mapped to colors using the edge_cmap and edge_vmin, edge_vmax parameters. edge_cmap : Matplotlib colormap, optional, default: None Colormap for mapping intensities of edges. width: float, optional, default: 1.0 The width of the edges. labels: bool, optional, default: True If False, then omit node labels and edge labels. font_size: int, optional, default: 10 The size of the font for text labels. figsize: tuple, optional, default: (20, 20) The size of the network figure to be plotted. savefig: bool, optional, default: False When True, the plotted figure will be saved to the current working directory, in PDF format, at the default (or specified) DPI. dpi: int, optional, default: 600 The amount of dots per inch to use when saving the figure. In accordance with Nature's guidelines, the default is 600. Source: https://www.nature.com/nature/for-authors/final-submission title: str, optional, default: None The title of the plotted graph/network. Returns ------- pos: dict A dictionary of the network's neurons as keys and their (x, y) coordinates as corresponding values. """ # Get positions for all nodes pos = kwargs.get("pos", None) if pos is None: print( "A neuron position dictionary was not provided! The spring_layout function will be used to plot the network.", file=sys.stderr) pos = nx.spring_layout(self.network, weight="weight") # Size of the plot plt.figure(figsize=kwargs.get("figsize", (20, 20))) # Nodes cmap = kwargs.get("cmap", None) alpha = kwargs.get("alpha", 1.0) node_size = kwargs.get("node_size", 600) nodelist = kwargs.get("nodelist", self.neurons) node_color = kwargs.get("node_color", 'r') node_borders = kwargs.get("node_borders", "black") nx.draw_networkx_nodes(self.network, pos, nodelist=nodelist, alpha=alpha, node_size=node_size, cmap=cmap, node_color=node_color, edgecolors=node_borders) # Draw edges width = kwargs.get("width", 1.0) edge_alpha = kwargs.get("edge_alpha", 1.0) edge_color = kwargs.get("edge_color", 'r') edge_cmap = kwargs.get("edge_cmap", None) edgelist = kwargs.get("edgelist", self.connections) nx.draw_networkx_edges(self.network, pos, edgelist=edgelist, alpha=edge_alpha, width=width, edge_color=edge_color, edge_cmap=edge_cmap) # Draw labels if kwargs.get("labels", True): nx.draw_networkx_labels(self.network, pos, font_size=kwargs.get("font_size", 10)) plt.title(kwargs.get("title", None)) plt.axis("off") if kwargs.get("savefig", False): plt.savefig(kwargs.get("title", "my_neuron_network.pdf"), format="pdf", dpi=kwargs.get("dpi", 600)) plt.show() return pos
def classification_metrics(y_true, y_pred, label2tag=label2tag, labels_list=labels_list, path=Config.output_path, gstep=1000): """ y_target : list of numpy array or list with integers representing classes. y_pred : samea as y_target labels_list: in order to print the scores. label2tag : is dict like {0:others, ...} prints """ y_true_tag = map(label2tag.get, y_true) y_pred_tag = map(label2tag.get, y_pred) f1 = f1_score(y_true_tag, y_pred_tag, labels=labels_list, average=None) pm = precision_score(y_true_tag, y_pred_tag, labels=labels_list, average=None) rm = recall_score(y_true_tag, y_pred_tag, labels=labels_list, average=None) # Just for pretty printing using pandas metric_df = pd.DataFrame({'f1': f1, 'pm': pm, 'rm': rm}, index=labels_list) print metric_df metric_df.to_csv(path + "f1score_" + str(gstep) + ".csv") # HERE WE HAVE SERIOUS BUG. ONLY WORKS WHEN ALL OF THEM ARE PREDICTED PROPERLY. # Could be possible :if there is no some tag's data in dev set. # checking dataproportion error here.. but its fixed now I jesss y_true_set = set(y_true) y_pred_set = set(y_pred) # print y_true_set # print y_pred_set data_proportion_error = y_true_set - y_pred_set if len(data_proportion_error) > 0: print "Some classes are not predicted by classifier." # set(y_true) labels_list = map(label2tag.get, sorted(list(y_true_set))) # print labels_list confu = confusion_matrix(y_true, y_pred) # print confu # plotting business fig = plt.figure(gstep, figsize=(20, 20)) sns_plot = sns.heatmap(confu, annot=True, xticklabels=labels_list, yticklabels=labels_list, fmt=".0f") confu_path = path + "confusion_matrix" + str(gstep) + ".png" # print path, confu_path plt.savefig(confu_path)
plt.grid() plt.fill_between(train_sizes, train_scores_mean - train_scores_std, train_scores_mean + train_scores_std, alpha=0.1, color="r") plt.fill_between(train_sizes, test_scores_mean - test_scores_std, test_scores_mean + test_scores_std, alpha=0.1, color="g") plt.plot(train_sizes, train_scores_mean, '-', color="r", label="Training score") plt.plot(train_sizes, test_scores_mean, '-', color="g", label="Cross-validation score") plt.ylim(0,1.2) plt.xlim(0,200) plt.legend(loc="best") plt.xlabel("Train test size") plt.savefig("learning_curves.png") plt.close("all") # plot the model vs the predictions for what_plot in [0,1,2,3]: fig=plt.figure(figsize=(16,8)) # ax1 = fig.add_subplot(1,2,1); ax2 = fig.add_subplot(1,2,2) ax1.tick_params(labelsize=20); ax2.tick_params(labelsize=20) # if(what_plot==3): # actual data ax1.scatter(X_test[models_features[i]]["Budget"],X_test[models_response[i]],c=X_test[models_features[i]]["AveStudioShare"],s=2.**((X_test[models_features[i]]["TheatersOpening"])+4),cmap="Reds",alpha=0.8) #
def plotting(self,pressurelattice,invasionlattice,pressure,number_of_clusters): from matplotlib.pylab import plt #Plotting the invasionlattice plt.figure(2) plt.imshow(invasionlattice, cmap='Greys',interpolation='nearest') plt.title("Invasion lattice") plt.colorbar() plt.savefig(self.pathsimulation + "invasionlattice.png", bbox_inches="tight") plt.close() #plotting the pressure plt.figure(5) plt.plot(pressure) plt.xlabel('Time') plt.ylabel('Pressure') plt.title('P(t)') plt.savefig(self.pathsimulation +"pressure.png", bbox_inches="tight") plt.close() #plotting the clusterlattice plt.figure(6) plt.imshow(self.lw, interpolation='nearest') plt.title("Clusterlattice") plt.colorbar() plt.savefig(self.pathsimulation +"clusterlattice.png", bbox_inches="tight") plt.close() #Temporal diagram plt.figure(7) plt.imshow(self.temporalplot,interpolation='nearest') plt.title('Temporal diagram') plt.colorbar() plt.savefig(self.pathsimulation +"temporal.png", bbox_inches="tight") plt.close() #Plotting pressure distribution in the cell. plt.figure(3) plt.hist(pressurelattice.ravel(), bins=30, fc='k', ec='k') plt.savefig(self.pathsimulation +"pressuredistribution.png", bbox_inches="tight") plt.close() #Plotting the number of clusters as a function of interation. plt.figure(1) plt.plot(number_of_clusters) plt.xlabel('Iteration number/time') plt.ylabel('Number of clusters') plt.title('Number_of_cluster(t)') plt.savefig(self.pathsimulation +"clusterN.png", bbox_inches="tight") plt.close()