def plot(self): """ Plot the net's outputs against iterations. You'll need to fetch data first. :return: """ if not self.train_outputs and not self.test_outputs: print("You need to fetch data first using fetch_range() or fetch_last().") return num_graphs = len(self.train_outputs + self.test_outputs) colormap = cm.winter(np.linspace(0, 1, num_graphs)) graph_count = 0 f = plt.figure() iterations = self.cache_log_train[:, 0].astype(int) for train_output in self.train_outputs: plt.plot(iterations, [x[train_output] for x in self.cache_log_train[:, 2]], label=train_output, color=colormap[graph_count]) graph_count += 1 iterations = self.cache_log_test[:, 0].astype(int) for test_output in self.test_outputs: plt.plot(iterations, [x[test_output] for x in self.cache_log_test[:, 2]], label=test_output, color=colormap[graph_count]) graph_count += 1 plt.xlabel('iteration') plt.legend(loc='best') plt.grid()
def plot_months(ax2, azim=-90, elev=10): xs = np.arange(len(xm_settle)) zs = np.arange(0, 8) verts = [] xm_settle_3dplot = xm_settle.copy() xm_settle_3dplot.index = xs for z in zs: ys = xm_settle_3dplot.iloc[:, int(z)].fillna(10) ys.iloc[0] = 10 ys.iloc[-1] = 10 verts.append(list(zip(ys.index.values, ys.values))) poly = PolyCollection( verts, linewidth=2.0, facecolors=[cm.winter(i, 1) for i in np.linspace(0, 1, 8)]) ax2.add_collection3d(poly, zs=zs, zdir='y') ax2.set_xlim3d(0, len(xm_settle)) ax2.set_xticks([(xm_settle.index.year == year).argmax() for year in xm_settle.index.year.unique()[1::2]]) ax2.set_xticklabels(xm_settle.index.year.unique()[1::2]) ax2.set_ylim3d(0.0, 7.5) ax2.set_ylabel("Month") ax2.set_yticklabels(["M1", "M2", "M3", "M4", "M5", "M6", "M7", "M8"]) ax2.set_zlim3d(10, 50) ax2.set_zlabel("Futures price") ax2.view_init(azim=azim, elev=elev) ax2.set_title("Futures prices for the next eight expirations")
def _get_color(self, legend): if 'IN' in legend: self._summer += 80 return cm.summer(self._summer) if 'OUT' in legend: self._spring += 80 return cm.winter(self._spring)
def draw_anomaly_histgram(anomaly_level, key, n_state, dict_threshold): fig = plt.figure(figsize=(15, 10)) fig.suptitle("histgram of anomaly level\nIP: %s states: %d" % (key, n_state)) ax = fig.add_subplot(111) df = pd.DataFrame(anomaly_level) df.columns = ["data"] df.plot(bins=50, alpha=0.5, figsize=(15, 10), kind="hist", ax=ax) ymin, ymax = ax.get_ylim() colors = np.linspace(0, 1, len(dict_threshold.keys())) color_idx = 0 for threshold in dict_threshold: label = "thld(%.3f) = %.3f" % (threshold, dict_threshold[threshold]) color = cm.winter(colors[color_idx]) ax.vlines(dict_threshold[threshold], ymin=ymin, ymax=ymax, linestyle="solid", lw=2, color=color, label=label) color_idx += 1 ax.set_xlabel("anomaly") ax.legend() output_filename = os.path.join("../result/model/", key + "_anomaly.png") fig.savefig(output_filename) plt.close(fig)
def plot(self): """ Plot the net's outputs against iterations. You'll need to fetch data first. :return: """ if not self.train_outputs and not self.test_outputs: print( "You need to fetch data first using fetch_range() or fetch_last()." ) return num_graphs = len(self.train_outputs + self.test_outputs) colormap = cm.winter(np.linspace(0, 1, num_graphs)) graph_count = 0 f = plt.figure() iterations = self.cache_log_train[:, 0].astype(int) for train_output in self.train_outputs: plt.plot(iterations, [x[train_output] for x in self.cache_log_train[:, 2]], label=train_output, color=colormap[graph_count]) graph_count += 1 iterations = self.cache_log_test[:, 0].astype(int) for test_output in self.test_outputs: plt.plot(iterations, [x[test_output] for x in self.cache_log_test[:, 2]], label=test_output, color=colormap[graph_count]) graph_count += 1 plt.xlabel('iteration') plt.legend(loc='best') plt.grid()
def plot2d(self, nsteps=10, srng='all', xlim=(1200, 1500), zlim='Auto'): """ plots a series of traces over the course of the entire run in order to visually locate changes of interest """ import pylab as pl from matplotlib import cm pl.close() fig = pl.figure() ax = fig.add_subplot(111) l = self.locate_in_list(self.x, xlim[0], 'greater') # find bounds based on xlim input r = self.locate_in_list(self.x, xlim[1], 'lesser') x = self.x[l:r] # narrow x list colours = cm.winter(np.linspace(0, 1, nsteps)) #colours = cm.spring(np.linspace(0,1,nsteps)) # use linear colour map spring if srng == 'all': srng = (0, len(self.scans) - 1) for ind, i in enumerate(np.linspace(srng[0], srng[1], nsteps)): ax.plot(x, self.scans[int(i)]['diff'][l:r], color=colours[ind], label='scan #%d' % i) ax.set_xlim(xlim[0], xlim[1]) #if zlim == 'Auto': # zlim = (self.np.amin(z),self.np.amax(z)) #ax.set_zlim(zlim[0],zlim[1]) ax.invert_xaxis() ax.set_xlabel('Wavenumber /cm-1') ax.set_ylabel('Difference from scan #%d' % self.ks['scan_zero']) pl.legend(loc=1) pl.show()
def distributions(self, number_of_agents, row_agents, col_agents, row_strategies, col_strategies,plot): self.row_strategies_distribution = self.proportion_classified_strategies(row_agents, row_strategies) for rs in range(len(row_strategies)): self.row_accumulated_strategies[rs].append(self.row_strategies_distribution[rs]) self.col_strategies_distribution = self.proportion_classified_strategies(col_agents, col_strategies) for cs in range(len(col_strategies)): self.col_accumulated_strategies[cs].append(self.col_strategies_distribution[cs]) self.total_strategies_per_generation = self.total_classified_strategies(row_agents, row_strategies, col_agents, col_strategies) for sh in range(len(self.strategies_history)): self.strategies_history[sh].append(self.total_strategies_per_generation[sh]) print "\n Row players' strategy distribution:" print "\t", self.row_strategies_distribution print "\n Column players' strategy distribution:" print "\t", self.col_strategies_distribution ga.kill_agents(env.row_agents, env.number_of_agents, env.number_of_row_agents, env.row_strategies) ga.kill_agents(env.col_agents, env.number_of_agents, env.number_of_col_agents, env.col_strategies) print "Row agents eliminated", (number_of_agents / 2) - len(env.col_agents) print "Col agents eliminated", (number_of_agents / 2) - len(env.row_agents) ga.reproduce_agents(env.row_agents, env.number_of_agents, env.number_of_row_agents, env.row_strategies) ga.reproduce_agents(env.col_agents, env.number_of_agents, env.number_of_col_agents, env.col_strategies) if plot==True: for k in range(len(env.row_strategies)): c = cm.copper((k + 1) / len(env.row_strategies)) pl.plot(self.row_accumulated_strategies[k], color=c, label="Row strategies: %s" % (k + 1)) for k in range(len(env.col_strategies)): c = cm.winter((k + 1) / len(env.row_strategies)) pl.plot(self.col_accumulated_strategies[k], color=c, label="Col strategies: %s" % (k + 1)) pl.draw()
def _counter_post_per_thread(Thread_list, Post_list, group_n): fig = plt.figure() ax = fig.add_subplot(111) for th_i in range(1, len(Thread_list) + 1): thread = Thread_list[th_i] print(" title", thread.title, th_i) x_times, y_nums = _extract_time(thread.pi_list, Post_list) if len(x_times) == 0: print(" this thread has not post from users.") continue if group_n == "ALPHA": ax.plot(x_times, y_nums, label=th_i, color=cm.spring((th_i - 1) / len(Thread_list))) elif group_n == "BRAVO": ax.plot(x_times, y_nums, label=th_i, color=cm.summer((th_i - 1) / len(Thread_list))) elif group_n == "CHARLIE": ax.plot(x_times, y_nums, label=th_i, color=cm.autumn((th_i - 1) / len(Thread_list))) else: ax.plot(x_times, y_nums, label=th_i, color=cm.winter((th_i - 1) / len(Thread_list))) minutes = mdates.MinuteLocator(interval=5) # 5分間隔で描画 timeFmt = mdates.DateFormatter('%H:%M') # x軸の時刻表示フォーマットの設定 ax.xaxis.set_major_locator(minutes) # 上記の条件をグラフに設定 ax.xaxis.set_major_formatter(timeFmt) # 上記の条件をグラフに設定 plt.xlim(start_t, finish_t) # x軸の範囲を設定 plt.ylim(0, 70) # y軸の範囲を設定 plt.title(group_n) # plt.legend(loc='upper left') #データの名前を表示 fig.autofmt_xdate() # いい感じにx軸の時刻表示を調節 plt.savefig(fn_path + group_n[0] + "_per_counter_post_no_legend" + ".png") plt.show()
def plot(): fig = plt.figure() ax = fig.add_subplot(111) for q in db: if isinstance(q, rtree.Box): verts = q.path() codes = [Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY, ] path = Path(verts, codes) patch = patches.PathPatch(path, facecolor='none', lw=1, ls='dashed', ec='blue') if q.reachability_distance: if q.reachability_distance<100: c = cm.winter(q.reachability_distance/100.,1) else: c='red' plt.plot(q.origin[0], q.origin[1], 'x', color=c) else: plt.plot(q.origin[0], q.origin[1], 'o', color='black') ax.add_patch(patch) ax.set_xlim(0,rt.root.box.origin[0]+rt.root.box.diagonal[0]+10) ax.set_ylim(0,rt.root.box.origin[1]+rt.root.box.diagonal[1]+10) plt.show()
def color_mapping(number_of_wards, number_of_years): """ Create our color map based on the number of wards :param number_of_wards: how many wards in dataframe :param number_of_years: how many years in dataframe :return: """ start = 0.0 stop = 1.0 cm_subsection = np.linspace(start, stop, number_of_wards) gc.pie_colors = [cm.Dark2(x) for x in cm_subsection] # gc.pie_colors = [cm.hsv(x) for x in cm_subsection] cm_subsection = np.linspace(0.2, stop, number_of_years) gc.bar_colors = [cm.winter(x) for x in cm_subsection] gc.line_colors = [cm.winter(x) for x in cm_subsection]
def plot_cube(self, cube, title: str = '', init_angle: int = 0, make_gif: bool = False, path_to_save: str = 'filename.gif' ): """ Plot 3d data. Parameters: cube: 3d data title: title for figure. init_angle: angle for image plot (from 0-360). make_gif: if True create gif from every 5th frames from 3d image plot. path_to_save: path to save GIF file. """ if self.binary: facecolors = cm.winter(cube) print("binary") else: if self.normalizing: cube = self._normalize(cube) facecolors = cm.gist_stern(cube) print("not binary") facecolors[:,:,:,-1] = cube facecolors = self._explode(facecolors) filled = facecolors[:,:,:,-1] != 0 x, y, z = self._expand_coordinates(np.indices(np.array(filled.shape) + 1)) with plt.style.context("dark_background"): fig = plt.figure(figsize=self.figsize) ax = fig.gca(projection='3d') ax.view_init(30, init_angle) ax.set_xlim(right = self.img_dim[0] * 2) ax.set_ylim(top = self.img_dim[1] * 2) ax.set_zlim(top = self.img_dim[2] * 2) ax.set_title(title, fontsize=18, y=1.05) ax.voxels(x, y, z, filled, facecolors=facecolors, shade=False) if make_gif: images = [] for angle in tqdm(range(0, 360, 5)): ax.view_init(30, angle) fname = str(angle) + '.png' plt.savefig(fname, dpi=120, format='png', bbox_inches='tight') images.append(imageio.imread(fname)) #os.remove(fname) imageio.mimsave(path_to_save, images) plt.close() else: plt.show()
def plotMap(map2d_, path_, title_=''): # Plots a map as described in lab2 description containing integer numbers. Each number has a specific meaning. You can check # the example provided at the end of the file for more information import matplotlib.cm as cm plt.interactive(False) greennumber = map2d_.max() #greennumber = len(np.unique(map2d_)) #print(greennumber) colors = cm.winter(np.linspace(0, 1, greennumber)) colorsMap2d = [[[] for x in xrange(map2d_.shape[1])] for y in range(map2d_.shape[0])] # Assign RGB Val for starting point and ending point locStart, locEnd = np.where(map2d_ == -2), np.where(map2d_ == -3) colorsMap2d[locStart[0]][locStart[1]] = [.0, .0, .0, 1.0] # black colorsMap2d[locEnd[0]][locEnd[1]] = [.0, .0, .0, .0] # white # Assign RGB Val for obstacle locObstacle = np.where(map2d_ == -1) for iposObstacle in range(len(locObstacle[0])): colorsMap2d[locObstacle[0][iposObstacle]][ locObstacle[1][iposObstacle]] = [1.0, .0, .0, 1.0] # Assign 0 locZero = np.where(map2d_ == 0) for iposZero in range(len(locZero[0])): colorsMap2d[locZero[0][iposZero]][locZero[1][iposZero]] = [ 1.0, 1.0, 1.0, 1.0 ] # Assign Expanded nodes locExpand = np.where(map2d_ > 0) for iposExpand in range(len(locExpand[0])): colorsMap2d[locExpand[0][iposExpand]][ locExpand[1][iposExpand]] = colors[ map2d_[locExpand[0][iposExpand]][locExpand[1][iposExpand]] - 1] for irow in range(len(colorsMap2d)): for icol in range(len(colorsMap2d[irow])): if colorsMap2d[irow][icol] == []: colorsMap2d[irow][icol] = [1.0, 0.0, 0.0, 1.0] plt.figure() plt.title(title_) plt.imshow(colorsMap2d, interpolation='nearest') plt.colorbar() plt.plot(path_[:][0], path_[:][1], color='magenta', linewidth=2.5) plt.ylim(0, map2d_.shape[0]) plt.xlim(0, map2d_.shape[1]) plt.draw() #plt.savefig('./'+title_) plt.show()
def show(self, columns=None): if columns is None: columns = [f'feature_{i}' for i in range(self.X.shape[1])] plt.figure(figsize=(5, int(len(columns) / 3))) order = np.argsort(self.adv_scores) colors = colormap.winter(np.arange(len(columns))/len(columns)) plt.barh(np.array(columns)[order], self.adv_scores[order], color=colors) plt.show()
def scatterPlot(map2d, path, title=" "): import matplotlib.cm as cm plt.interactive(False) greennumber = map2d.max() #greennumber = len(np.unique(map2d_)) #print(greennumber) colors = cm.winter(np.linspace(0, 1, greennumber)) colorsMap2d = [[[] for x in range(map2d.shape[1])] for y in range(map2d.shape[0])] # Assign RGB Val for starting point and ending point locStart, locEnd = np.where(map2d == -2), np.where(map2d == -3) colorsMap2d[locStart[0][0]][locStart[1][0]] = [.0, .0, .0, 1.0] # black colorsMap2d[locEnd[0][0]][locEnd[1][0]] = [.0, .0, 1.0, 1.0] # white # Assign RGB Val for obstacle locObstacle = np.where(map2d == -1) for iposObstacle in range(len(locObstacle[0])): colorsMap2d[locObstacle[0][iposObstacle]][ locObstacle[1][iposObstacle]] = [1.0, .0, .0, 1.0] # Assign 0 locZero = np.where(map2d == 0) for iposZero in range(len(locZero[0])): colorsMap2d[locZero[0][iposZero]][locZero[1][iposZero]] = [ 1.0, 1.0, 1.0, 1.0 ] # Assign Expanded nodes locExpand = np.where(map2d > 0) for iposExpand in range(len(locExpand[0])): if (iposExpand != 0): colorsMap2d[locExpand[0][iposExpand]][ locExpand[1][iposExpand]] = colors[ map2d[locExpand[0][iposExpand]][locExpand[1][iposExpand]] - 1] for irow in range(len(colorsMap2d)): for icol in range(len(colorsMap2d[irow])): if colorsMap2d[irow][icol] == []: colorsMap2d[irow][icol] = [1.0, 0.0, 0.0, 1.0] plt.scatter(x=map2d[0], y=map2d[1]) plt.title(title) plt.imshow(colorsMap2d, interpolation='nearest') plt.colorbar() plt.plot(path[:][0], path[:][1], color='magenta', linewidth=2.5) plt.ylim(0, map2d.shape[0]) plt.xlim(0, map2d.shape[1]) plt.draw() #plt.savefig() plt.show()
def save_feature_importances(self, path, columns=None): if columns is None: columns = [f'feature_{i}' for i in range(len(self.imps))] plt.figure(figsize=(5, int(len(columns) / 3))) imps_mean = np.mean(self.imps, axis=1) imps_se = np.std(self.imps, axis=1) / np.sqrt(self.imps.shape[0]) order = np.argsort(imps_mean) colors = colormap.winter(np.arange(len(columns))/len(columns)) plt.barh(np.array(columns)[order], imps_mean[order], xerr=imps_se[order], color=colors) plt.savefig(path)
def colorfor(v_0_1, testname): if True: if is_c_code(testname): return cm.spectral(v_0_1) else: return cm.spectral(v_0_1) else: if is_c_code(testname): return cm.winter(v_0_1) else: return cm.autumn(v_0_1)
def cat_spatiale_trajets(prob_dist, prob_duree, data, li_spat): df = data.copy() df['V2_DUREE'] = pd.to_timedelta( df['V2_DUREE'], unit='Min').dt.round('1Min').dt.total_seconds() // 60 df = df[(df['V2_DUREE'] < 200) & (np.floor(df['V2_MMOTIFDES']) == 9) & (df['V2_MORICOM_MDESCOM_indicUU'] == 1)].copy() colors = cm.winter(np.linspace(0, 1, len(li_spat))) colors_outli = cm.inferno(np.linspace(0, 1, len(li_spat))) li_co = {} for i in range(len(li_spat)): # sprint(li_spat[i]) if df[df['SPATIAL'] == i]['V2_DUREE'].count() != 0: df_filt = df[df['SPATIAL'] == i].copy() te = sm.OLS(df_filt['V2_MDISTTOT'].values, df_filt['V2_DUREE'].values.reshape(-1, 1)) res = te.fit() influ = res.get_influence() (cook, p) = influ.cooks_distance df_filt['COOK'] = cook cook_outlier = [(df_filt['V2_DUREE'].values[o], df_filt['V2_MDISTTOT'].values[o]) for o, t in enumerate(cook) if t > 4 / len(cook)] print(cook_outlier) plt.scatter( df_filt[df_filt['COOK'] <= 8 / len(cook)]['V2_DUREE'], df_filt[df_filt['COOK'] <= 8 / len(cook)]['V2_MDISTTOT'], color=colors[i]) plt.plot(df_filt['V2_DUREE'], res.predict(df_filt['V2_DUREE'].values)) plt.scatter( df_filt[df_filt['COOK'] > 8 / len(cook)]['V2_DUREE'], df_filt[df_filt['COOK'] > 8 / len(cook)]['V2_MDISTTOT'], color=colors_outli[i]) modele_sans_outlier = sm.OLS( df_filt[df_filt['COOK'] <= 4 / len(cook)]['V2_MDISTTOT'].values, df_filt[df_filt['COOK'] <= 4 / len(cook)]['V2_DUREE'].values.reshape(-1, 1)) res_ss_outli = modele_sans_outlier.fit() plt.plot(df_filt['V2_DUREE'], res_ss_outli.predict(df_filt['V2_DUREE'].values)) plt.show() li_co[li_spat[i]] = [ np.round(res.params[0] * 60, decimals=1), np.round(res.rsquared * 100, decimals=2), df[df['SPATIAL'] == i]['V2_MDISTTOT'].count() ] print(np.round(res.rsquared * 100, decimals=2), np.round(res_ss_outli.rsquared * 100, decimals=2)) else: li_co[li_spat[i]] = [0] print(li_co) # plt.scatter(df[df['V2_MORICOM_MDESCOM_indicUU']==1]['V2_MDISTTOT'],df[df['V2_MORICOM_MDESCOM_indicUU']==1]['V2_DUREE'],color = 'm') return li_co
def plot_feature_importances(self, columns=None, method='fast'): imps = self.get_feature_importances(method) if columns is None: columns = [f'feature_{i}' for i in range(len(imps))] plt.figure(figsize=(5, int(len(columns) / 3))) order = np.argsort(imps) colors = colormap.winter(np.arange(len(columns))/len(columns)) plt.barh(np.array(columns)[order], imps[order], color=colors) plt.show()
def view_map(map_data, stores=None, paths=None, points=None, grid=True): """ View map with entities like paths, stores, and points. Each entity argument is a list and the z-order (visibility) increases with index in the list. Among entities z-order varies as stores < paths < points. The map shows passable as yellow and impassable as purple. Each entity list cycles through an indepedent color map, i.e. each path in paths will have a different color. All points are displayed in black color. Args: map_data: Boolean numpy array. True for passable, False for impassable. paths: List of paths to be shown. stores: Point stores to be shown. points: Special points to be displayed. grid: Display grid. Defaults to True. Notes: coordinates: click anywhere on the map to view coordinates of that point. """ if stores: colors = cm.autumn(np.linspace(0, 1, len(stores))) for c, store in zip(colors, stores): x, y = get_x_and_y_from_itr(store) plt.scatter(x, y, color=c) if paths: colors = cm.winter(np.linspace(0, 1, len(paths))) for c, path in zip(colors, paths): start, end = path[0], path[-1] x, y = get_x_and_y_from_itr(path) plt.scatter(x, y, color=c) plt.plot(start.x, start.y, marker='x') plt.plot(end.x, end.y, marker='x') if points: x = [point.x for point in points] y = [point.y for point in points] plt.scatter(x, y, color='black', marker='o') m, n = map_data.shape plt.imshow(map_data) plt.xticks(np.arange(0.5, n, 1.0), []) plt.yticks(np.arange(-0.5, m, 1.0), []) plt.grid(grid) plt.connect('button_press_event', mouse_move) plt.show() return
def __define_color_dict(exclusion_thresholds, methods_ts, upper_color=1): # create color_dict n_cmap_lines = 2 + len(methods_ts) - len(exclusion_thresholds) cm_subsection = np.linspace(0, upper_color, n_cmap_lines) colors = [cm.winter(x) for x in cm_subsection] color_dict = { 'learning_method_random': (101 / 255, 101 / 255, 101 / 255, 1) } label_dict = { 'random': 'random', 'uncertainty': 'uncertainty, k=1', 'similar': 'similar, k=∞' } colors = [ (230 / 255, 7 / 255, 26 / 255), (222 / 255, 122 / 255, 160 / 255, 1), (141 / 255, 204 / 255, 235 / 255, 1), (27 / 255, 141 / 255, 204 / 255, 1) ] colors = [ (243 / 255, 56 / 255, 41 / 255, 1), (255 / 255, 119 / 255, 81 / 255, 1), (14 / 255, 185 / 255, 203 / 255, 1), (2 / 255, 95 / 255, 106 / 255, 1) ] # Google Colors: colors = [ (219.0 / 255, 68.0 / 255, 55.0 / 255, 1), (244 / 255.0, 180 / 255.0, 0 / 255.0, 1), (15 / 255.0, 157 / 255.0, 88 / 255.0, 1), (66 / 255.0, 133 / 255.0, 244 / 255.0, 1) ] color_dict['learning_method_uncertainty'] = colors[0] color_dict['learning_method_similar'] = colors[-1] i = 0 for method in methods_ts: label = '_'.join(method.split('_')[2:]) if label in exclusion_thresholds: continue color_dict[method] = colors[i + 1] k = method.split('_')[-1] label_dict['_'.join(method.split('_')[2:])] = 'thresholding, k=' + k i += 1 return color_dict, label_dict
def _counter_post_per_user(User_list, Post_list, group_n): fig = plt.figure() ax = fig.add_subplot(111) sort_uilist = sorted(User_list.keys()) for u_i in sort_uilist: if u_i in except_usr_id: continue user = User_list[u_i] print(" user", user.name, u_i) x_times, y_nums = _extract_time(user.pi_list, Post_list) if len(x_times) == 0: print(" this thread has not post from users.") continue if group_n == "ALPHA": ax.plot(x_times, y_nums, label=user.name, color=cm.spring((u_i - 1) / len(User_list))) ax.plot(x_times[-1], y_nums[-1], marker='o', color=cm.spring((u_i - 1) / len(User_list))) elif group_n == "BRAVO": ax.plot(x_times, y_nums, label=user.name, color=cm.summer((u_i - 1) / len(User_list))) ax.plot(x_times[-1], y_nums[-1], marker='o', color=cm.summer((u_i - 1) / len(User_list))) elif group_n == "CHARLIE": ax.plot(x_times, y_nums, label=user.name, color=cm.autumn((u_i - 1) / len(User_list))) ax.plot(x_times[-1], y_nums[-1], marker='o', color=cm.autumn((u_i - 1) / len(User_list))) else: ax.plot(x_times, y_nums, label=user.name, color=cm.winter((u_i - 1) / len(User_list))) ax.plot(x_times[-1], y_nums[-1], marker='o', color=cm.winter((u_i - 1) / len(User_list))) days = mdates.MinuteLocator(interval=5) daysFmt = mdates.DateFormatter('%H:%M') ax.xaxis.set_major_locator(days) ax.xaxis.set_major_formatter(daysFmt) plt.xlim(start_t, finish_t) plt.ylim(0, 40) plt.title(group_n) plt.legend(loc='upper left') fig.autofmt_xdate() plt.savefig(fn_path + group_n[0] + "_per_user_post_counter" + ".png") plt.show()
def stackEDCsPlot(emap, delY=100, numCuts=20, xlim=(-10, 750), ylim=(-10, 2200)): colors = cm.winter(np.linspace(0, 1, numCuts)) delY = delY plt.figure() for i in range(numCuts): plt.plot(emap[i] + (delY * i), color=colors[i]) plt.ylim(ylim) plt.xlim(xlim) plt.xlabel("Energy (pixel)") plt.ylabel("Angle (pixel)") plt.show()
def plot_transformed_hist(variable, df): fig, ax = plt.subplots(figsize=(5, 5)) df[variable].hist(bins=30, color=cm.winter(0.7), ec='k', ax=ax) ax.set_xlabel(f"{variable}", fontsize=20) ax.set_ylabel("frequency", fontsize=20) ax.grid(False) plt.savefig("../reports/figures/histograms/" + variable + "_hist_transform.jpg", dpi=800, transparent=False, edgecolor='k', facecolor='w', pad_inches=0.1, bbox_inches='tight') return display()
def run(self, plot=False, stack=False): if plot==True: pl.ion() pl.ylim(0, 1) pl.xlabel("Generations") pl.ylabel("Probability") for k in range(len(env.row_strategies)): c = cm.copper((k + 1) / len(env.row_strategies)) pl.plot(self.row_accumulated_strategies[k], color=c, label="Row strategies: %s" % (k + 1)) for k in range(len(env.col_strategies)): c = cm.winter((k + 1) / len(env.row_strategies)) pl.plot(self.col_accumulated_strategies[k], color=c, label="Col strategies: %s" % (k + 1)) #pl.legend(bbox_to_anchor=(0.7, 0.85), loc=2, prop={'size':7}) pl.legend(loc=2, prop={'size':7}) pl.draw() self.generations_passing(ga.generations, ga.rounds_per_generation, env.number_of_agents, env.row_agents, env.col_agents, plot, stack)
def _plot_surface(ax, function): x_range = np.arange(0, 1, 0.01) y_range = np.arange(0, 1, 0.01) X, Y = np.meshgrid(x_range, y_range) Z = function(X, Y) norm = plt.Normalize(Z.min(), Z.max()) colors = cm.winter(norm(Z)) surf = ax.plot_surface(X, Y, Z, rstride=5, cstride=5, facecolors=colors, shade=False) surf.set_facecolor((0, 0, 0, 0)) return ax
def get_points(): x = -2.0 while x <= 2.0: y = -2.0 while y <= 2.0: n = in_set(x,y) x_points.append(x) y_points.append(y) colors.append(cm.winter(n)) y += 0.005 x += 0.005 return x_points , y_points
def plotSegment(self, rawData, segment, half=None): """ Set half to 'left' or 'right' to plot only the data from one leg""" figure(figsize=(11,9)) key = self.__fullKey[2:] if half=="right": rawData = rawData[:,[0,1,5,6,7,8,9,10,11,12,13]] key = key[3:12] if half=="left": rawData = rawData[:,[0,1,14,15,16,17,18,19,20,21,22]] key = key[12:] ColourSeqOriginal = matplotlib.rcParams['axes.color_cycle'] matplotlib.rcParams['axes.color_cycle'] = cm.winter( np.linspace( 0, 1, np.shape(rawData)[1] ) ).tolist() plot(rawData[segment[0]:segment[1],0],rawData[segment[0]:segment[1],2:]) title("Gait Segment") xlabel("Time (Seconds)") ylabel("Rotation (Degrees per second)") leg = legend(key) for legobj in leg.legendHandles: legobj.set_linewidth(10.0) matplotlib.rcParams['axes.color_cycle'] = ColourSeqOriginal show()
def render(self): self.setup_figure() self._ax1.clear() lines = [] labels = [] color_idx = 0 #color_idx_list = np.linspace(0, 1, len(self.subject_dict)) colors = cm.winter(np.linspace(0, 1, len(self.subject_dict))) for name, subject in iteritems(self.subject_dict): self.render_frame(subject, color=colors[color_idx]) labels.append(name) line, = self._ax1.plot([0], [0], color=colors[color_idx], lw=4) lines.append(line) color_idx += 1 self.add_legend(lines, labels, loc='upper left') if self.point_dict is not None: lines, labels = self.plot_point_dict(self.point_dict) self.add_legend(lines, labels, loc='upper right') self.add_time_info(self.subject) self._fig.canvas.draw()
def animate(i): global res_lcs, com_lcs, ax, scat, fig print i fig.suptitle('Timestep: '+str((i+1)*36)) res_data = res_lcsnorm[:,(i+1)*36-36:(i+1)*36+36,:].mean(1) com_data = com_lcsnorm[:,(i+1)*36-36:(i+1)*36+36,:].mean(1) res_newdata = ternaryPlot(res_data) com_newdata = ternaryPlot(com_data) res_scat.set_offsets(res_newdata) com_scat.set_offsets(com_newdata) res_colors = cm.autumn(res_data.sum(1)) res_colors[:,3] = res_data.sum(1) #set opacity equal to intensity (currently not working) res_scat.set_facecolors(res_colors) com_colors = cm.winter(com_data.sum(1)) com_colors[:,3] = com_data.sum(1) com_scat.set_facecolors(com_colors) return res_scat, com_scat
# this figure shows the time levels in each run figTimes = plt.figure(30, facecolor='w') axTimes = figTimes.add_subplot(1, 1, 1) plt.xlabel('Year') axTimesYlabels = [] # ========= print "Done setting up figure axes." # ========= # --- Define colors for lines --- #colors = ['tab:blue', 'tab:orange', 'tab:green', 'tab:red', 'tab:purple', 'tab:brown', 'tab:pink', 'tab:olive', 'tab:cyan'] n150 = sum("amp150" in r for r in runs) colors = [cm.autumn(x) for x in np.linspace(0.0, 1.0, n150)] n300 = sum("amp300" in r for r in runs) colors.extend([cm.winter(x) for x in np.linspace(0.0, 1.0, n300)]) color_index = 0 # ================ # Loop over runs and plot data # ================ runNumber = 0 for run in runs: print "Plotting run: " + run thisRun = runData[run] # Pull needed data for plotting from this run # TODO: replace local variables below in plotting commands with reference to object variables yrs = thisRun.yrs melt = thisRun.melt totalmelt = thisRun.totalmelt
def plot_tuning_PSTH_one_intensity(oneCell, intensity=50.0, timeRange=[-0.5, 1], binWidth=0.010, halfFreqs=True): #calls load_remote_tuning_data(oneCell) to get the data, then plot raster eventOnsetTimes, spikeTimestamps, bdata = load_remote_tuning_data( oneCell, BEHAVDIR_MOUNTED, EPHYSDIR_MOUNTED) freqEachTrial = bdata['currentFreq'] intensityEachTrial = bdata['currentIntensity'] possibleIntensity = np.unique(intensityEachTrial) if len(possibleIntensity) != 1: intensity = intensity #50dB is the stimulus intensity used in 2afc task ###Just select the trials with a given intensity### trialsThisIntensity = [intensityEachTrial == intensity] freqEachTrial = freqEachTrial[trialsThisIntensity] intensityEachTrial = intensityEachTrial[trialsThisIntensity] eventOnsetTimes = eventOnsetTimes[trialsThisIntensity] possibleFreq = np.unique(freqEachTrial) if halfFreqs: possibleFreq = possibleFreq[ 1::3] #slice to get every other frequence presented numFreqs = len(possibleFreq) #print len(intensityEachTrial),len(eventOnsetTimes),len(spikeTimestamps) trialsEachFreq = behavioranalysis.find_trials_each_type( freqEachTrial, possibleFreq) #pdb.set_trace() #for debug #colormap = plt.cm.gist_ncar #colorEachFreq = [colormap(i) for i in np.linspace(0, 0.9, numFreqs)] #from jaratoolbox.colorpalette import TangoPalette as Tango #colorEachFreq = [Tango['Aluminium3'], Tango['Orange2'],Tango['Chameleon1'],Tango['Plum1'],Tango['Chocolate1'],Tango['SkyBlue2'],Tango['ScarletRed1'],'k'] #Creat colorEachCond from color map from matplotlib import cm cm_subsection = np.linspace(0.0, 1.0, numFreqs) colorEachFreq = [cm.winter(x) for x in cm_subsection] #Create legend import matplotlib.patches as mpatches handles = [] for (freq, color) in zip(possibleFreq, colorEachFreq): patch = mpatches.Patch(color=color, label=str(freq) + ' Hz') handles.append(patch) (spikeTimesFromEventOnset,trialIndexForEachSpike,indexLimitsEachTrial) = \ spikesanalysis.eventlocked_spiketimes(spikeTimestamps,eventOnsetTimes,timeRange) timeVec = np.arange(timeRange[0], timeRange[-1], binWidth) spikeCountMat = spikesanalysis.spiketimes_to_spikecounts( spikeTimesFromEventOnset, indexLimitsEachTrial, timeVec) smoothWinSize = 3 #plt.figure() extraplots.plot_psth(spikeCountMat / binWidth, smoothWinSize, timeVec, trialsEachCond=trialsEachFreq, colorEachCond=colorEachFreq, linestyle=None, linewidth=2, downsamplefactor=1) extraplots.set_ticks_fontsize(ax=plt.gca(), fontSize=14) plt.xlim(timeRange[0] + 0.1, timeRange[1]) plt.legend(handles=handles, loc=2) plt.xlabel('Time from sound onset (s)', fontsize=18) plt.ylabel('Firing rate (spk/sec)', fontsize=18)
#result[:,1]=dI_list #np.savetxt('isoscattering_curves_polydispersity_%s.txt'%(str(R)),result) dI_list=np.genfromtxt('isoscattering_curves_polydispersity_%s.txt'%(str(R))) print 'isoscattering_curves_polydispersity_%s.txt'%str(R) qmin_matrix=[] for (j,qmin) in enumerate(qmin_list): q_fit,dI_fit=compact_q(q,dI_list,qmin) dI_fit=np.array(dI_fit) q0=q_fit[np.where(dI_fit==min(dI_fit))] qmin_matrix.append(q0) print qmin_matrix prnt colors = cm.winter(np.linspace(0, 1, len(qmin_matrix[0]))) for (k,c) in zip(range(len(qmin_matrix[0])),colors): #plt.scatter(qmin_matrix[0],map(lambda x,y: (y-x)/y, qmin_matrix[k], qmin_matrix[0]), color=c, label='%.1f'%(sigma_range[k]*2.355*100/R)) plt.plot(sigma_range*2.355*100/R,map(lambda x: (x-qmin_matrix[0,k])/qmin_matrix[0,0], qmin_matrix[:,k]), color=c, label=k) #polydispersity vs q*R0 in % plt.draw() plt.legend() plt.grid(True) plt.show() dir_name=os.path.dirname(os.path.realpath(__file__)) folder='/Calculated Isoscattering Points/' numfiles = len([f for f in os.listdir(dir_name+folder) if os.path.isfile(os.path.join(dir_name+folder, f)) and f[0] != '.']) data = open(dir_name+folder+'Minima_result_%i.txt'%int(numfiles+1), 'a') data.write('#Multi-shell model: Isoscattering point study with mean and standard deviation\n') data.write('#Parameters: R=%.1f, Number of layers=%i, ed_core=%.0f, ed_shell=%.0f, ed_shell2=%.0f, ed_shell3=%.0f, N(shell)=%i, N(shell1)=%i, N(shell2)=%i\n\n'%(R,int(N_layers),ed_core, ed_shell, ed_shell2, ed_shell3, int(N_shell), int(N2), int(N3))) data.write('#Polydispersity\t1st min\t2nd min\t3rd min\t4th min\n ')
def check_ali(fits_list): """Plot the results of alignment.""" message = """Check that any bad fits have been rejected.""" print(message) data = [] x_rms = [] y_rms = [] n_sigma = [] for fits in fits_list: path_list = (fits.telluric_path, fits.fluxcal_path, fits.reduced_path) for path in path_list: try: data_i = pf.getdata(path, 'ALIGNMENT') order = np.argsort( IFU(path, probenum, flag_name=False).name for probenum in data_i['PROBENUM']) date_i = data_i[order] header = pf.getheader(path, 'ALIGNMENT') except (KeyError, IOError): pass else: break else: continue data.append(data_i) x_rms.append(header['X_RMS']) y_rms.append(header['Y_RMS']) n_sigma.append(header['SIGMA']) data = np.array(data) x_rms = np.array(x_rms) y_rms = np.array(y_rms) scale = 200.0 radius_plot = 130000.0 radius_field = 125000.0 radius_fibre = scale * 105.0 / 2.0 x_centroid_plot = ((data['X_CEN'] - data['X_REFMED']) * scale + data['X_REFMED']) y_centroid_plot = ((data['Y_CEN'] - data['Y_REFMED']) * scale + data['Y_REFMED']) x_fit_plot = data['X_SHIFT'] * scale + data['X_REFMED'] y_fit_plot = -data['Y_SHIFT'] * scale + data['Y_REFMED'] n_ifu = data.shape[1] fig = plt.figure(fits_list[0].field_id, figsize=(10., 10.)) axes = fig.add_subplot(111, aspect='equal') plt.xlim((-radius_plot, radius_plot)) plt.ylim((-radius_plot, radius_plot)) axes.add_patch(plt.Circle((0, 0), radius_field, fill=False, lw=0.5)) for ifu, x_cen, y_cen, x_fit, y_fit, good in zip( data[0], x_centroid_plot.T, y_centroid_plot.T, x_fit_plot.T, y_fit_plot.T, data['GOOD'].T): good = good.astype(bool) color = cm.winter(ifu['PROBENUM'] / float(n_ifu - 1)) fibre = plt.Circle((ifu['X_REFMED'], ifu['Y_REFMED']), radius_fibre, fill=False, ls='dashed', color=color) axes.add_patch(fibre) plt.scatter(x_cen[good], y_cen[good], color='k') plt.scatter(x_cen[~good], y_cen[~good], color='r') for index in np.where(~good)[0]: plt.plot((x_fit[index], x_cen[index]), (y_fit[index], y_cen[index]), ':', color=color) plt.plot(x_fit, y_fit, color=color, lw=2.0) plt.annotate('IFS' + str(ifu['PROBENUM']), xy=(ifu['X_REFMED'], ifu['Y_REFMED'] + radius_fibre), xycoords='data', xytext=None, textcoords='data', arrowprops=None, color=color) plt.title('RMS: ' + ', '.join('{:.1f}'.format(rms) for rms in np.sqrt((x_rms**2 + y_rms**2))) + '\nSigma clip: ' + ', '.join('{:.2f}'.format(n) for n in n_sigma)) print("When you're ready to move on...")
def simulate(self, plot=False): """ This runs the simulation. """ # Initialising history lists for distribution of strategies # Some debugging # import pdb # pdb.set_trace() self.row_history = [[] for e in range(len(self.row_strategies))] self.col_history = [[] for e in range(len(self.col_strategies))] if plot: # If plot is true we plot the strategy distributions dynamically import matplotlib.pyplot as plt import matplotlib.cm as cm plt.ion() # Turn interactive mode on (so that we can graph dynamically. plt.ylim(0, 1) # Set ylim to have min 0 and max 1 plt.xlabel("Generations (max=%s)" % self.generations) # Label for x axis plt.ylabel("Probability") # Label for y axis for k in range(len(self.row_strategies)): # Plot all row strategies c = cm.spring((k + 1) / len(self.row_strategies)) plt.plot(self.row_history[k], color=c, label="Row strategy: %s" % (k + 1)) for k in range(len(self.col_strategies)): # Plot all col strategies c = cm.winter((k + 1) / len(self.col_strategies)) plt.plot(self.col_history[k], "--", color=c, label="Col strategy: %s" % (k + 1)) plt.legend(loc="upper left") plt.draw() for g in range(self.generations): # In a loop for every generation print "\n----------------------" print "\nGeneration: %s of %s" % (g + 1, self.generations) for r in range(self.rounds_per_generation): # Loop to repeat tournament for each generation print "\tRound: %s of %s" % (r + 1, self.rounds_per_generation) # Reset all utilities before starting a tournament for k in range(self.number_of_agents): self.row_agents[k].utility = 0 self.col_agents[k].utility = 0 self.play_tournament() # Calculate distributions and update history self.row_distribution = return_current_strategy_distribution(self.row_agents, self.row_strategies) for k in range(len(self.row_strategies)): self.row_history[k].append(self.row_distribution[k]) self.col_distribution = return_current_strategy_distribution(self.col_agents, self.col_strategies) for k in range(len(self.col_strategies)): self.col_history[k].append(self.col_distribution[k]) print "\nRow players strategy distribution:" print "\t", self.row_distribution print "\nCol players strategy distribution:" print "\t", self.col_distribution # Reproduce self.reproduce() if plot: # Update the plot if plot is True for k in range(len(self.row_strategies)): c = cm.spring((k + 1) / len(self.row_strategies)) plt.plot(self.row_history[k], color=c) for k in range(len(self.col_strategies)): c = cm.winter((k + 1) / len(self.col_strategies)) plt.plot(self.col_history[k], "--", color=c) plt.draw() if plot: # Block plot at end of simulation plt.show(block=True)
def worker(lat, lon, cloud, path_row, start_date, end_date, buffer, taskid, ndvi, path): """ Create animated GIF from landsat 8 data""" # Test # lat lon has to be defined if path_row isn't if (not lat) | (not lon): print "No defined lat-lon" if not path_row: print "No defined Path-Row for query as well" print "Cannot perform querry, please make sure to include at least lat and lon options" sys.exit(1) # Query Scenes print print "Building Landsat-API request" landsat_query = query_builder( paths_rows=path_row, lat=lat, lon=lon, start_date=start_date, end_date=end_date, cloud_max=cloud ) print "Searching Landsat 8 images" candidate_scenes = search(landsat_query) if not candidate_scenes.has_key("results"): print "Landsat-API Querry returned with 'Not Found message'" sys.exit(1) im2process = candidate_scenes["results"] all_ids = [i["sceneID"] for i in im2process] print "{} Landsat scene found".format(len(all_ids)) print "landsat ids: {}".format(", ".join(all_ids)) # Construct AOI (square in WebMercator) wgs = osr.SpatialReference() wgs.ImportFromEPSG(4326) wmerc = osr.SpatialReference() wmerc.ImportFromEPSG(3857) wgsTowm = osr.CoordinateTransformation(wgs, wmerc) wmTowgs = osr.CoordinateTransformation(wmerc, wgs) # Create AOI - 10km buffer square (WebMercator) around point pt = ogr.Geometry(ogr.wkbPoint) pt.AddPoint(lon, lat) pt.Transform(wgsTowm) shPt = loads(pt.ExportToWkt()) polB = shPt.buffer(buffer, cap_style=3) aoi = ogr.CreateGeometryFromWkt(polB.wkt) aoi.Transform(wmTowgs) # Transform AOI in WGS84 pt = pol = polB = None print "Excluding Landsat 8 scene not covering the Entire AOI" proc_images = [] for ii in range(len(im2process)): imgMeta = im2process[ii] ring = ogr.Geometry(ogr.wkbLinearRing) ring.AddPoint(imgMeta["lowerLeftCornerLongitude"], imgMeta["lowerLeftCornerLatitude"]) ring.AddPoint(imgMeta["upperLeftCornerLongitude"], imgMeta["upperLeftCornerLatitude"]) ring.AddPoint(imgMeta["upperRightCornerLongitude"], imgMeta["upperRightCornerLatitude"]) ring.AddPoint(imgMeta["lowerRightCornerLongitude"], imgMeta["lowerRightCornerLatitude"]) ring.AddPoint(imgMeta["lowerLeftCornerLongitude"], imgMeta["lowerLeftCornerLatitude"]) poly = ogr.Geometry(ogr.wkbPolygon) poly.AddGeometry(ring) if aoi.Within(poly): proc_images.append(imgMeta) ring = poly = None if len(proc_images) == 0: print "No Image found covering the AOI - change buffer size or change lat-lon" else: # Check Only if ROW is the same (same date) all_pr = ["{:03d},{:03d}".format(int(i["path"]), int(i["row"])) for i in proc_images] all_row = [i["row"] for i in proc_images] if len(list(set(all_row))) > 1: print """AOI covering more than one Row : Please choose one of the following: {} Using --path_row option""".format( " | ".join(list(set(all_pr))) ) sys.exit(1) workdir = os.path.join(path, taskid) if not os.path.exists(workdir): os.makedirs(workdir, 0775) font = ImageFont.load_default().font l8_images = [] date_array = [] for ii in range(len(proc_images)): im = proc_images[ii] print "Processing Landsat image {}".format(im["sceneID"]) out_im = os.path.join(workdir, "{}.tif".format(im["date"])) try: WRSPath = im["path"] WRSRow = im["row"] landsat_address = "http://landsat-pds.s3.amazonaws.com/L8/{path}/{row}/{id}/{id}".format( path=WRSPath, row=WRSRow, id=im["sceneID"] ) meta_file = "{0}_MTL.txt".format(landsat_address) meta_data = urllib2.urlopen(meta_file).readlines() # Get Landsat scene geographic metadata bqa = "/vsicurl/{addr_name}_BQA.TIF".format(addr_name=landsat_address) src_ds = gdal.Open(bqa, gdal.GA_ReadOnly) geoT = src_ds.GetGeoTransform() proj = src_ds.GetProjection() src_ds = None imSpatialRef = osr.SpatialReference() imSpatialRef.ImportFromWkt(proj) aoiSpatialRef = osr.SpatialReference() aoiSpatialRef.ImportFromEPSG(4326) coordTransform = osr.CoordinateTransformation(aoiSpatialRef, imSpatialRef) aoi.Transform(coordTransform) # reproject the aoi in UTM aoi_bounds = aoi.GetEnvelope() x_off = int((aoi_bounds[0] - geoT[0]) / geoT[1]) y_off = int((aoi_bounds[3] - geoT[3]) / geoT[5]) x_size = int(((aoi_bounds[0] - geoT[3]) / geoT[5]) - ((aoi_bounds[1] - geoT[3]) / geoT[5])) y_size = int(((aoi_bounds[2] - geoT[3]) / geoT[5]) - ((aoi_bounds[3] - geoT[3]) / geoT[5])) # Create RGB file ngeo = list(geoT) ngeo[0] = aoi_bounds[0] ngeo[3] = aoi_bounds[3] if ndvi: band5_address = "/vsicurl/{0}_B5.TIF".format(landsat_address) awsim5 = gdal.Open(band5_address, gdal.GA_ReadOnly) arr5 = awsim5.GetRasterBand(1).ReadAsArray(x_off, y_off, x_size, y_size) arr5 = landsat_dnToReflectance_USGS(arr5, 5, meta_data) band4_address = "/vsicurl/{0}_B4.TIF".format(landsat_address) awsim4 = gdal.Open(band4_address, gdal.GA_ReadOnly) arr4 = awsim4.GetRasterBand(1).ReadAsArray(x_off, y_off, x_size, y_size) arr4 = landsat_dnToReflectance_USGS(arr4, 4, meta_data) ratio = np.where(arr5 * arr4 > 0, np.nan_to_num((arr5 - arr4) / (arr5 + arr4)), 0) awsim4 = awsim5 = arr4 = arr5 = None # Use winter colormap (http://matplotlib.org/examples/color/colormaps_reference.html) img = Image.fromarray(np.uint8(cm.winter((ratio + 1.0) / 2.0) * 255)).convert("RGB") ratio = None draw = ImageDraw.Draw(img) xs, ys = draw.textsize(im["date"], font=font) draw.rectangle([(5, 5), (xs + 15, ys + 15)], fill=(255, 255, 255)) draw.text((10, 10), im["date"], (0, 0, 0), font=font) out_jpg = out_im.replace(".tif", ".jpg") img.save(out_jpg) else: driver = gdal.GetDriverByName("GTiff") dst_ds = driver.Create(out_im, x_size, y_size, 3, gdal.GDT_Byte) dst_ds.SetGeoTransform(tuple(ngeo)) dst_ds.SetProjection(proj) rgb = [4, 3, 2] for b in range(len(rgb)): band_address = "/vsicurl/{0}_B{1}.TIF".format(landsat_address, rgb[b]) awsim = gdal.Open(band_address, gdal.GA_ReadOnly) arr = awsim.GetRasterBand(1).ReadAsArray(x_off, y_off, x_size, y_size) p2, p98 = np.percentile(arr[arr > 0], (2, 98)) dst_ds.GetRasterBand(b + 1).WriteArray( np.where( arr > 0, exposure.rescale_intensity(arr, in_range=(p2, p98), out_range=(1, 255)), 0 ) ) dst_ds.GetRasterBand(b + 1).SetNoDataValue(0) awsim = arr = None dst_ds = None # save, close img = Image.open(out_im) draw = ImageDraw.Draw(img) xs, ys = draw.textsize(im["date"], font=font) draw.rectangle([(5, 5), (xs + 15, ys + 15)], fill=(255, 255, 255)) draw.text((10, 10), im["date"], (0, 0, 0), font=font) out_jpg = out_im.replace(".tif", ".jpg") img.save(out_jpg) os.remove(out_im) date_array.append(im["date"]) l8_images.append(out_jpg) except: print "Failed to process Landsat image {}".format(im["sceneID"]) if len(date_array) > 0: # Sort image by date and rename with number sorted_index = np.argsort(date_array) l8sort = [l8_images[i] for i in sorted_index] for i in range(len(l8sort)): os.rename(l8sort[i], os.path.join(workdir, "{:05d}.jpg".format(i))) # This part can be replace in pure python # Create GIF gif_file = os.path.join(path, "%s.gif" % taskid) inJpg = os.path.join(workdir, "*.jpg") os.system("convert -delay 30 -depth 8 -layers optimize -quality 80 -loop 0 {0} {1}".format(inJpg, gif_file)) shutil.rmtree(workdir)
import tables import matplotlib.pyplot as mpl import matplotlib.cm as cm from combinato import SortingManagerGrouped, get_regions, artifact_id_to_name print(artifact_id_to_name) FIGSIZE = (7, 6) REGIONS = ("A", "AH", "MH", "PH", "EC", "PHC", "I") LEFT_COLORS = cm.spectral(np.linspace(0, 1, len(REGIONS))) RIGHT_COLORS = cm.summer(np.linspace(0, 1, len(REGIONS))) NUM_COLORS = cm.winter(np.linspace(0, 1, 8)) COLORDICT = {} for i, region in enumerate(REGIONS): COLORDICT["L" + region] = LEFT_COLORS[i] COLORDICT["R" + region] = RIGHT_COLORS[i] for i in range(1, 9): COLORDICT[str(i)] = NUM_COLORS[i - 1] JOBS = ("thr", "arti") def plotthr(thrplot, fireplot, thr, times, color="r"): """ plot the threshold data
def avaliacao_forca_bruta(self, predicao, diferenca, ignora_borda = False, analise = False): if self.celulas is None: raise Exception("As celulas nao foram anotadas!") contornos, hierarquia = predicao #Separa as regioes da predicao idxs = range(len(contornos)) regioes = [(contornos[i], [contornos[j] for j in idxs if hierarquia[0,j,3] == i]) #seleciona ele e seus filhos for i in idxs if hierarquia[0,i,3] == -1] #para cada pai #Compara uma celula com uma regiao im1, im2 = np.zeros((2,) + shape_patch, dtype=np.uint8) #gera duas imagens pretas para comparacao def compara(celula, regiao): #Desenha a celula na primeira imagem cv2.drawContours(im1, celula.componentes, -1, [255], -1) #preenchimento cv2.drawContours(im1, celula.componentes, -1, [255], 2) #borda cv2.drawContours(im1, celula.buracos, -1, [0], -1) #buracos #Desenha a regiao na segunda imagem componente, buracos = regiao cv2.drawContours(im2, [componente], -1, [255], -1) #preenchimento cv2.drawContours(im2, [componente], -1, [255], 2) #borda cv2.drawContours(im2, buracos, -1, [0], -1) #buracos #Calcula a diferenca com a funcao fornecida dif = diferenca(im1, im2) #Restaura as imagens pretas cv2.drawContours(im1, celula.componentes, -1, [0], -1) #preenchimento cv2.drawContours(im1, celula.componentes, -1, [0], 2) #borda cv2.drawContours(im2, [componente], -1, [0], -1) #preenchimento cv2.drawContours(im2, [componente], -1, [0], 2) #borda return dif #Celulas consideradas. #Se especificado, ignora as celulas que tocam a borda do patch. celulas = [cel for cel in self.celulas if (not ignora_borda or not cel.na_borda())] #Compara as celulas com cada regiao e guarda a menor diferenca. min_diffs = map(lambda cel: min(map(lambda reg: compara(cel, reg), regioes) + [1]), #caso nao haja regioes na predicao celulas) if analise: #Inicia uma imagem branca im_an = np.zeros(shape_patch + (3,), dtype=np.uint8) im_an[:,:,:] = 255 #Desenha as regioes segmentadas em cinza cv2.drawContours(im_an, contornos, -1, [200, 200, 200], -1) cv2.drawContours(im_an, contornos, -1, [200, 200, 200], 2) #Prepara uma imagem que sera sobreposta com transparencia im_alpha = np.zeros(shape_patch + (3,), dtype=np.uint8) #Desenha as celulas import matplotlib.cm as cm for i,cel in enumerate(celulas): #para cada celula #Mapeia a menor diferenca para cores cor_rgba = cm.winter(1 - min_diffs[i], 1, True) cor_bgr = map(lambda c: int(c), list(cor_rgba[2::-1])) #Desenha o contorno da celula cv2.drawContours(im_an, cel.componentes, -1, cor_bgr, 1) cv2.drawContours(im_an, cel.buracos, -1, cor_bgr, 1) #Desenha o preenchimento da celula, que sera transparente cv2.drawContours(im_alpha, cel.componentes, -1, cor_bgr, -1) cv2.drawContours(im_alpha, cel.buracos, -1, [0, 0, 0], -1) #Ajusta a imagem a ser sobreposta com transparencia de maneira a nao afetar o restante da imagem (areas cem celulas) im_alpha_g = cv2.cvtColor(im_alpha, cv2.cv.CV_BGR2GRAY) #tons de cinza t, mask = cv2.threshold(im_alpha_g, 0, 255, cv2.THRESH_BINARY_INV) #apenas a regiao que nao tem celulas mask = cv2.cvtColor(mask, cv2.cv.CV_GRAY2BGR) im_alpha = im_alpha + im_an*mask*255 #copia da imagem com as regioes a area que nao tem celulas #Sobrepoe as imagens im_an = cv2.addWeighted(im_an, 0.7, im_alpha, 0.3, 0) #adiciona com transparencia mostra_imagens([im_an], "Similaridade de regiao encontrada para cada celula") #Retorna a media de menores diferencas entre todas as celulas return np.mean(min_diffs) if len(min_diffs) > 0 else 1
def ikkKO(AA, AB, AC, kv, nftot, **kwargs): TR = kwargs.get('TR', 1) start = kwargs.get('start', 2) ikk = kwargs.get('ikk') ###INITIATION####### sol = init(AA, AB, 1, kv, nftot, 0) ###SIMULATION####### Y0 = sol.y[:, -1] sol1 = sim(AA, AB, 1, kv, TR, Y0, 60 * 60 * 24) ###PROCESSING####### SOL = prc.fuse(sol.t, sol.y, sol1.t, sol1.y) pt = np.stack(prc.hour(SOL[0])) py = np.stack(SOL[1:SOL.shape[0]]) KO = False time = None for i, j in enumerate(sol1.t): if j / 3600 >= start: KO = i time = j / 3600 break py0 = sol1.y[1, KO] ###.00128 1.8; .00125 24### py1 = sol1.y[6, KO] * 1000 pt1 = sol1.t[KO] / 3600 y0 = sol1.y[:, KO] y0[1] = 0 sol3 = sim(AA, AB, AC, kv, TR, y0, (24 - start) * 60 * 60, ikk=True) y0[1] = py0 / 2 sol4 = sim(AA, AB, AC, kv, TR, y0, (24 - start) * 60 * 60, ikk=True) y0[1] = py0 / 4 sol5 = sim(AA, AB, AC, kv, TR, y0, (24 - start) * 60 * 60, ikk=True) y0[1] = py0 sol6 = sim(AA, AB, AC, kv, TR, y0, (24 - start) * 60 * 60, ikk=True) y0[1] = 2 * py0 sol7 = sim(AA, AB, AC, kv, TR, y0, (24 - start) * 60 * 60, ikk=True) plt.style.use('seaborn-paper') MAP = np.linspace(0, 1, 6) fig = plt.figure() gs = GridSpec(9, 1, hspace=0.1) ax = fig.add_subplot(gs[:8, :]) ax3 = fig.add_subplot(gs[-1, :]) colours1 = cm.winter(MAP) ax.plot(pt - 101, 1000 * py[6], c=(0, 0, 128 / 255)) ax.plot(time + sol7.t / 3600, 1000 * sol7.y[6], linestyle=(0, (5, 1)), c=colours1[0]) ax.plot(time + sol6.t / 3600, 1000 * sol6.y[6], linestyle=(0, (5, 5)), c=colours1[1]) ax.plot(time + sol4.t / 3600, 1000 * sol4.y[6], '-.', c=colours1[2]) ax.plot(time + sol5.t / 3600, 1000 * sol5.y[6], linestyle=(0, (3, 1, 1, 1, 1, 1)), c=colours1[3]) ax.plot(time + sol3.t / 3600, 1000 * sol3.y[6], c=colours1[4], linestyle=(0, (1, 1))) handles = [ Line2D([0], [0], color='black', lw=1, linestyle='-'), Line2D([0], [0], color='black', lw=1, linestyle=(0, (5, 1))), Line2D([0], [0], color='black', lw=1, linestyle=(0, (5, 5))), Line2D([0], [0], color='black', lw=1, linestyle='-.'), Line2D([0], [0], color='black', lw=1, linestyle=(0, (3, 1, 1, 1, 1, 1))), Line2D([0], [0], color='black', lw=1, linestyle=(0, (1, 1))) ] PY0 = 1000 * py0 labels = [ 'wild-type', 'IKK={} nM'.format(np.around(2 * PY0, decimals=1)), 'IKK={} nM'.format(np.around(PY0, decimals=1)), 'IKK={} nM'.format(np.around(PY0 / 2, decimals=1)), 'IKK={} nM'.format(np.around(PY0 / 4, decimals=1)), 'IKK=0 nM' ] ax.legend(handles, labels, loc=0, fontsize=10, framealpha=0, prop={'size': 10}, edgecolor=None) ax.set_xlim(-1, 8) ax.set_xticks([]) ax.set_ylabel('$c$ in nM') ax2 = ax.twinx() ax2.set_yticks([]) ax2.set_ylabel('NF$\kappa$B') colours2 = cm.autumn(MAP) ax3.plot(pt - 101, 1000 * py[1], c=(170 / 255, 0, 0)) ax3.plot(time + sol7.t / 3600, 1000 * sol7.y[1], linestyle=(0, (5, 1)), c=colours2[0]) ax3.plot(time + sol6.t / 3600, 1000 * sol6.y[1], linestyle=(0, (5, 5)), c=colours2[1]) ax3.plot(time + sol4.t / 3600, 1000 * sol4.y[1], '-.', c=colours2[2]) ax3.plot(time + sol5.t / 3600, 1000 * sol5.y[1], linestyle=(0, (3, 1, 1, 1, 1, 1)), c=colours2[3]) ax3.plot(time + sol3.t / 3600, 1000 * sol3.y[1], c=colours2[4], linestyle=(0, (1, 1))) ax3.set_ylim(-.1, 3) ax3.set_yticks([0, 1000 * py0, 2000 * py0]) ax3.yaxis.set_major_formatter(FormatStrFormatter('%.1f')) ax3.set_xlabel('$t$ in h') ax4 = ax3.twinx() ax4.set_yticks([]) ax4.set_ylabel('IKK') trans = ax3.transAxes + ax.transData.inverted() ((xmin, _), (xmax, _)) = trans.transform([[0, 1], [1, 1]]) ax3.set_xlim(xmin, xmax) ax3.axvline(pt1, linewidth=0.5, c='gray') ax.axvline(pt1, linewidth=0.5, c='gray') fig.tight_layout() plt.savefig('../../graphics/IKKvar.png', dpi=500) plt.close() return [pt, py, sol1, sol.t.shape[0], sol3, time]
fish_tm_6=cgf.get_fisher_dd(run_name,f_tm6,n_ij_fid) exit(1) #Plot K-L eigenvectors if plot_stuff : plt.figure() ax=plt.gca() ax.imshow([[0.,1.],[0.,1.]],extent=[1.25,1.37,-0.22,-0.17],interpolation='bicubic', cmap=cm.winter,aspect='auto') nbtop=7 i_ell=30 plt.text(1.12,-0.203,'$p\\in[1,%d]$'%nbtop,{'fontsize':16}) for i in np.arange(nbtop) : ax.plot(zbarr,e_o[i_ell,:,i]*np.sqrt(ndens)/np.sqrt(np.sum(e_o[i_ell,:,i]**2*ndens)),'o-', markeredgewidth=0,color=cm.winter((i+0.5)/nbtop)) plt.xlabel('$z_\\alpha$',fontsize=18) plt.ylabel('$\\sqrt{\\bar{n}^\\alpha}\\,({\\sf F}_{%d})^p_\\alpha$'%i_ell,fontsize=18) plt.savefig('../Draft/Figs/kl_modes_gc.pdf',bbox_inches='tight') fisher=(larr+0.5)[:,None]*(c_p_dfn/c_p_fid)**2 fish_permode=np.sum(fisher,axis=0) fish_cum=np.cumsum(fish_permode) if plot_stuff : plt.figure(); imodes=np.arange(nbins)+1 plt.plot(imodes,fish_permode/np.sum(fish_permode),'go-',lw=2, label='${\\rm Information\\,\\,in\\,\\,mode}\\,\\,p_{\\rm KL}$',markeredgewidth=0) plt.plot(imodes[:-1],1-fish_cum[:-1]/fish_cum[-1],'ro-',lw=2,label='${\\rm Information\\,\\,in\\,\\,modes}\\,\\,>p_{\\rm KL}$',markeredgewidth=0)
def plot_energy_density_map(protein, iteration_number ,current_dir, select_path): colors = [('white')] + [(cm.jet(i)) for i in xrange(1,256)] new_map = mpl.colors.LinearSegmentedColormap.from_list('new_map', colors, N=256) colors_low = [('white')] + [(cm.winter(i)) for i in xrange(1,256)] low_map = mpl.colors.LinearSegmentedColormap.from_list('low_map', colors_low, N=256) colors_high = [('white')] + [(cm.summer(i)) for i in xrange(1,256)] high_map = mpl.colors.LinearSegmentedColormap.from_list('high_map', colors_high, N=256) print " Loading BeadBead.dat" beadbead = np.loadtxt(select_path+"BeadBead.dat",dtype=str) sigij = beadbead[:,5].astype(float) epsij = beadbead[:,6].astype(float) deltaij = beadbead[:,7].astype(float) interaction_numbers = beadbead[:,4].astype(str) pairs = beadbead[:,:2].astype(int) pairs -= np.ones(pairs.shape,int) native = beadbead[:,4].astype(int) pairs = pairs[ native != 0 ] epsij = epsij[ native != 0 ] pairs_low = pairs[ epsij<1. ] epsij_low = epsij[ epsij<1. ] pairs_high = pairs[ epsij>=1. ] epsij_high = epsij[ epsij>=1. ] Qref = np.loadtxt(current_dir+'/'+protein+"/Qref_cryst.dat") C = np.zeros(Qref.shape,float) C_low = np.zeros(Qref.shape,float) C_high = np.zeros(Qref.shape,float) print len(pairs_low), len(pairs_high) for k in range(len(pairs)): C[pairs[k][0],pairs[k][1]] = epsij[k] # C_low for k in range(len(pairs_low)): C_low[pairs_low[k][0],pairs_low[k][1]] = epsij_low[k] # C_high for k in range(len(pairs_high)): C_high[pairs_high[k][0],pairs_high[k][1]] = epsij_high[k] print " Plotting..." plt.figure() plt.subplot(1,1,1,aspect=1) ax = plt.subplot(1,1,1,aspect=1) plt.pcolor(C,cmap=new_map, vmin=0, vmax=3) plt.xlim(0,len(Qref)) plt.ylim(0,len(Qref)) ax = plt.gca() cbar = plt.colorbar() # cbar.set_clim(0,4) cbar.set_label("Contact energy density map",fontsize=20) cbar.ax.tick_params(labelsize=20) plt.xlabel("Residue i",fontsize=20) plt.ylabel("Residue j",fontsize=20) plt.title('Contact energy density map for '+protein+', iteration '+iteration_number) for label in ax.get_xticklabels() + ax.get_yticklabels(): label.set_fontsize(15) print " Saving..." plt.savefig(current_dir+'/metrics/stability/'+protein+"_contact_energy_density_map.pdf") plt.clf() print " Plotting..." plt.figure() plt.subplot(1,1,1,aspect=1) ax = plt.subplot(1,1,1,aspect=1) plt.pcolor(C_low,cmap=low_map) plt.xlim(0,len(Qref)) plt.ylim(0,len(Qref)) ax = plt.gca() cbar = plt.colorbar() # cbar.set_clim(0,4) cbar.set_label("Contact energy density map",fontsize=20) cbar.ax.tick_params(labelsize=20) plt.xlabel("Residue i",fontsize=20) plt.ylabel("Residue j",fontsize=20) plt.title('Contact energy density map for '+protein+', iteration '+iteration_number+', eps<1') for label in ax.get_xticklabels() + ax.get_yticklabels(): label.set_fontsize(15) print " Saving..." plt.savefig(current_dir+'/metrics/stability/'+protein+"_low_contact_energy_density_map.pdf") plt.clf() print " Plotting..." plt.figure() plt.subplot(1,1,1,aspect=1) ax = plt.subplot(1,1,1,aspect=1) plt.pcolor(C_high,cmap=high_map, vmin=1, vmax=3) plt.xlim(0,len(Qref)) plt.ylim(0,len(Qref)) ax = plt.gca() cbar = plt.colorbar() cbar.set_label("Contact energy density map",fontsize=20) cbar.ax.tick_params(labelsize=20) plt.xlabel("Residue i",fontsize=20) plt.ylabel("Residue j",fontsize=20) plt.title('Contact energy density map for '+protein+', iteration '+iteration_number+', eps>=1') for label in ax.get_xticklabels() + ax.get_yticklabels(): label.set_fontsize(15) print " Saving..." plt.savefig(current_dir+'/metrics/stability/'+protein+"_high_contact_energy_density_map.pdf") plt.clf()
figTimes = plt.figure(30, facecolor='w') axTimes = figTimes.add_subplot(1, 1, 1) plt.xlabel('Year') axTimesYlabels = [] # ========= print "Done setting up figure axes." # ========= # --- Define colors for lines --- #colors = ['tab:blue', 'tab:orange', 'tab:green', 'tab:red', 'tab:purple', 'tab:brown', 'tab:pink', 'tab:olive', 'tab:cyan'] n150 = sum("amp150" in r for r in runs) colors = [ cm.autumn(x) for x in np.linspace(0.0, 1.0, n150) ] n300 = sum("amp300" in r for r in runs) colors.extend( [ cm.winter(x) for x in np.linspace(0.0, 1.0, n300) ] ) color_index = 0 # ================ # Loop over runs and plot data # ================ runNumber = 0 for run in runs: print "Plotting run: " + run thisRun = runData[run] # Pull needed data for plotting from this run # TODO: replace local variables below in plotting commands with reference to object variables yrs=thisRun.yrs melt=thisRun.melt
data = glob.glob('/backup/yuliya/v30/graphs_largedomain/fixed/*.gpickle') data.sort() d1.append(data1) d.append(data) data1 = glob.glob('/backup/yuliya/v35/breakups_con/fixed/*.npy') data1.sort() data = glob.glob('/backup/yuliya/v35/graphs_largedomain/fixed/*_no_term_br.gpickle') data.sort() d1.append(data1) d.append(data) colors = cm.autumn(np.linspace(0, 1, 114)) colors1 = cm.winter(np.linspace(0, 1, 12)) t = np.load('/backup/yuliya/vsi01/vsi01_TimeInSec.npy') u = 0 ell = np.load('/backup/yuliya/vsi05/dataellg_ellp.npy')[8:-10] ellv34 = np.load('/backup/yuliya/v34/dataellg_ellp.npy')[13:-1][::1] ellv34[7:-1] = ellv34[8:] ellv34[10:-1] = ellv34[11:] ellv34[45:-1] = ellv34[46:] ellvsi05 = np.load('/backup/yuliya/vsi05/dataellg_ellp.npy')[8:-10] ellvsi05[19:-1] = ellvsi05[20:] ellv30 = np.load('/backup/yuliya/v30/dataellg_ellp.npy')[10:-1] ellv30[61:-1] = ellv30[62:] ellv30[63:-1] = ellv30[64:] ellv35 = np.load('/backup/yuliya/v35/dataellg_ellp.npy')[22:-1] ellv35[4:-1] = ellv35[5:] ellv35[6:-4] = ellv35[10:]
cc[n] = (cc[n]-min_cc)/(max_cc-min_cc) min_eb = min(eb.values()) max_eb = max(eb.values()) for e in eb: eb[e] = (eb[e]-min_eb)/(max_eb-min_eb) min_el = min(el.values()) max_el = max(el.values()) for e in el: el[e] = (el[e]-min_el)/(max_el-min_el) for n in G: G.node[n]['color'] = colors.rgb2hex(cm.winter(cc[n])) G.node[n]['border_color'] = colors.rgb2hex(cm.winter(1./(cc[n]+1))) G.node[n]['border_size'] = nd[n]**.5 + 1.5 G.node[n]['size'] = d[n]**.5 + 5 G.node[n]['label'] = str(n) for (u,v) in G.edges(): G.edge[u][v]['width'] = 1.5 + 5*eb[(u,v)] G.edge[u][v]['strength'] = G.edge[u][v]['width'] G.edge[u][v]['color'] = colors.rgb2hex(cm.jet(el[(u,v)])) data = json_graph.node_link_data(G) s = json.dumps(data) f = open('test.json','w') f.write(s)
for particle_id in particle_ids: count = count + 1 particle_data = partextract(extract_exe, work_dir, uda_name, plot_dir, particle_id[0], error_file) plt_color = cm.PiYG(float(count)/float(len(particle_ids))) particle_pos = "Sim: (%0.3f, %0.3f)" % (float(particle_id[1]), float(particle_id[2])) plot_x(particle_data, particle_pos, plt_color, '-') plot_y(particle_data, particle_pos, plt_color, '-') plot_z(particle_data, particle_pos, plt_color, '-') pos_all = exact_solution() count = 0 for pos in pos_all: count = count + 1 plt_color = cm.winter(float(count)/float(len(pos_all))) particle_pos = "Exact: (%0.3f,%0.3f)" % (pos[0][1], pos[0][2]) plot_x(pos, particle_pos, plt_color, '--') plot_y(pos, particle_pos, plt_color, '--') plot_z(pos, particle_pos, plt_color, '--') plt.figure("x") plt.grid(True) plt.legend(bbox_to_anchor=(1.05,1), loc=2, prop={'size':10}) savePDF(fig1, 'x_vs_time', size='1280x960') plt.figure("y") plt.grid(True) plt.legend(bbox_to_anchor=(1.05,1), loc=2, prop={'size':10}) savePDF(fig2, 'y_vs_time', size='1280x960')
aspect='auto') plt.text(1.345, -0.335, '$1^{\\rm st}\\,\\,{\\rm mode},\\,\\,\\ell\\in[2,2000]$', {'fontsize': 16}) plt.text(1.33, -0.41, '$2^{\\rm nd}\\,\\,{\\rm mode},\\,\\,\\ell\\in[2,2000]$', {'fontsize': 16}) for i in (1 + np.arange(199)) * 10: # ax.plot(zbarr,e_o[ i,:,0]*np.sqrt(ndens)/np.sqrt(np.sum(e_o[ i,:,0]**2*ndens)),'o-', # markeredgewidth=0,color=cm.winter((i+0.5)/2001)) ax.plot(zbarr, e_o[i, :, 0] * ndens / np.sqrt(np.sum(e_o[i, :, 0]**2 * ndens**2)), 'o-', markeredgewidth=0, color=cm.winter((i + 0.5) / 2001)) if e_o[i, 2, 1] > 0: sign = -1 else: sign = 1 # ax.plot(zbarr,sign*e_o[ i,:,1]*np.sqrt(ndens)/np.sqrt(np.sum(e_o[ i,:,1]**2*ndens)),'o-', # markeredgewidth=0,color=cm.autumn((i+0.5)/2001)) ax.plot(zbarr, sign * e_o[i, :, 1] * ndens / np.sqrt(np.sum(e_o[i, :, 1]**2 * ndens**2)), 'o-', markeredgewidth=0, color=cm.autumn((i + 0.5) / 2001)) ax.plot(zbarr, f_tm1[0, :, 0] / np.sqrt(np.sum(f_tm1[0, :, 0]**2)), 'ko-',
######### KMeans run_kmeans = False if run_kmeans: space_errs = [] ks = [1,2,3,4,5,10,25,50] outliers = [0,1,2,3,4,5,10,25,50,100] for k in ks: model = KMeans(n_clusters=k) for outlier in outliers: model.fit(X) X2 = remove_outliers(model, X, max_outliers=outlier) model.fit(X2) err = compute_kmeans_err(model, X2, max_outliers=0) space = k_outlier_space(k,outlier,X2) space_errs.append((k,outlier,space,err)) S = pd.DataFrame(space_errs, columns=['k','outlier','space','err']) color=0 for k,S_k in S.groupby('k'): print len(S_k) print cm.winter(int(color)) plt.scatter(S_k['space'], S_k['err'], label="k="+str(k), color=cm.rainbow(color/float(len(ks)))) color += 1 plt.xlabel('space (uncompressed space is %d)'%(X.shape[0]*X.shape[1])) plt.ylabel('err') plt.legend() G = df.groupby('user', as_index=False)[['time','tries']].sum()
# Plotting # # set plot parameters comm1_str = np.vstack([np.sum(a[:, :nNodes_comm1], axis=1, keepdims=True) for a in adjMat_timeseries]) comm2_str = np.vstack([np.sum(a[:, -nNodes_comm2:], axis=1, keepdims=True) for a in adjMat_timeseries]) relativeCommStr = comm1_str - comm2_str relativeCommStr *= 128 / np.max(np.abs(relativeCommStr)) relativeCommStr += 128 nodeColors = [cm.winter(int(r[0])) for r in relativeCommStr] nodeKWargs = [{'markerfacecolor': c, 'marker': 'o', 'markersize': 8, 'linestyle': 'None', 'zorder': 2} for c in nodeColors] edgeKWargs = {'color': 'k', 'marker': 'None', 'linestyle': '-', 'linewidth': 1, 'zorder': 1} # Initialize plots