def run_graph(self): fig = plt.figure(1, figsize=(10, 10)) ax = fig.add_subplot(111, projection='3d') plt.ion() plt.show() cmap = cm.plasma norm = col.Normalize(vmin=min(self.t), vmax=max(self.t)) ax.scatter(self.x, self.y, self.z, marker='^', c=self.t, cmap=cmap, norm=norm, alpha=0.2) ax.plot(self.x[self.pool[0]], self.y[self.pool[0]], self.z[self.pool[0]], marker='o', c='lime', fillstyle='none') ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlabel('Z') ax.set_title('Selection algorithm') fig.colorbar(cm.ScalarMappable(norm=norm, cmap=cmap)) plt.draw() plt.pause(0.001) input('Press enter to continue: ') self.first_step() if self.direction == -1: ax.plot(self.x[self.pool[0]], self.y[self.pool[0]], self.z[self.pool[0]], marker='o', c='navy', fillstyle='none') elif self.direction == 1: ax.plot(self.x[self.pool[-1]], self.y[self.pool[-1]], self.z[self.pool[-1]], marker='o', c='navy', fillstyle='none') plt.draw() plt.pause(0.001) input('Press enter to continue: ') while self.search: self.find_next() if self.direction == -1: ax.plot(self.x[self.pool[0]], self.y[self.pool[0]], self.z[self.pool[0]], marker='o', c='navy', fillstyle='none') elif self.direction == 1: ax.plot(self.x[self.pool[-1]], self.y[self.pool[-1]], self.z[self.pool[-1]], marker='o', c='navy', fillstyle='none') print( f'Still looping, already selected {len(self.pool) - 1} new points' ) plt.draw() plt.pause(0.001) input('Press enter to continue: ') if len(self.pool) >= self.max_points: break
norm = colors.Normalize(vmin=-0.03, vmax=0.03) map_reward_change = ax4.pcolor(data[1:, :, -1], cmap=color_map, norm=norm) plt.colorbar(map_reward_change, cmap=color_map, norm=norm) ax4.set_xlim([0, step_num]) ax4.set_ylim([0, period_num - 1]) ax4.set_ylabel('periods') ax4.set_xlabel('steps') ax4.set_title('single step reward') # figure 3 fig3 = plt.figure(3) ax5 = fig3.add_subplot(1, 1, 1, xlim=(0, step_num), ylim=(0, 1)) values = range(asset_num) cNorm = colors.Normalize(vmin=0, vmax=values[-1]) scalarMap = cmx.ScalarMappable(norm=cNorm, cmap=color_map) line_list = () for line0 in list(range(asset_num)): colorVal = scalarMap.to_rgba(values[line0]) line, = ax5.plot([], [], color=colorVal, label="Asset - " + str(line0 + 1)) ax5.legend() line_list += (line, ) def init(): for line_num in list(range(asset_num)): line_list[line_num].set_data([], []) return line_list
def animate_json(i,args): global last_index d_min_cutoff = args.d_min_cutoff spot_count_cutoff = args.spot_count_cutoff log_spot_cutoff = args.log_spot_cutoff input_file = args.input_file print('loading file') print('updating plot') out_array=[] with open(input_file, 'r') as f: data = json.load(f) for i, record in enumerate(data): print_flush(str(i)) image_num=record['image'].split('_')[1].split('.')[0] strong=record['n_spots_total'] intensity=record['total_intensity'] if float(record['total_intensity']) <= 0 or int(record['n_spots_no_ice']) <= 0: ratio=0 else: ratio=(float(record['total_intensity'])/float(record['n_spots_no_ice'])) noise_1 = record['noisiness_method_1'] noise_2 = record['noisiness_method_2'] d_min = record['d_min'] d_min_1 = record['d_min_1'] d_min_2 = record['d_min_2'] out_array.append([image_num, ratio, intensity, strong, noise_1, noise_2, d_min, d_min_1, d_min_2]) np_array = np.array(out_array) cNorm = colors.Normalize(vmin=0, vmax=1) scalarMap = cm.ScalarMappable(norm=cNorm, cmap=cm.seismic) colours = [] hits = [] hit_images = [] hit_count = 0 indexable_images = [] for num, ratio, intensity, strong, noise_1, noise_2, d_min, d_min_1, d_min_2 in np_array: if strong >= spot_count_cutoff: indexable_images.append(num) if ratio >= 10: if 0.0 <= noise_1 <= 0.875: if 0.0 <= noise_2 <= 0.96: hit_images.append([num, strong]) colours.append(scalarMap.to_rgba(1)) hit_count+=1 else: hits.append(0) colours.append(scalarMap.to_rgba(0)) else: hits.append(0) colours.append(scalarMap.to_rgba(0)) else: hits.append(0) colours.append(scalarMap.to_rgba(0)) else: hits.append(0) colours.append(scalarMap.to_rgba(0)) if hit_count > 0 and total > 0: print('estimated counts rate = %f'%(float(hit_count*100/float(total)))) print('number of potential indexable images = %d'%(len(indexable_images))) if len(indexable_images) > 0 and hit_count > 0: print('estimated hit_rate from indexable images = %f'%(float(hit_count)*100/float(len(indexable_images)))) #hit_array = np.array(hit_images) print heapq.nlargest(10, hit_images, key=lambda x: x[1]) ax1.scatter(image_numbers[last_index:], spot_counts[last_index:], c=colours[last_index:], cmap=cm.seismic) last_index=len(image_numbers) #a,b = np.polyfit(np.log(intensities),spot_counts,1, w=np.sqrt(spot_counts)) out_file=input_file.split('.')[0] with open(out_file+'.hits','w') as hit_file: for image, spot in hit_images: hit_file.write(str(int(image)) + '\n')
def __decision_plot_matplotlib( base_value, cumsum, ascending, feature_display_count, features, feature_names, highlight, plot_color, axis_color, y_demarc_color, xlim, alpha, color_bar, auto_size_plot, title, show, legend_labels, legend_location, ): """matplotlib rendering for decision_plot()""" # image size row_height = 0.4 if auto_size_plot: pl.gcf().set_size_inches(8, feature_display_count * row_height + 1.5) # draw vertical line indicating center pl.axvline(x=base_value, color="#999999", zorder=-1) # draw horizontal dashed lines for each feature contribution for i in range(1, feature_display_count): pl.axhline(y=i, color=y_demarc_color, lw=0.5, dashes=(1, 5), zorder=-1) # initialize highlighting linestyle = np.array("-", dtype=np.object) linestyle = np.repeat(linestyle, cumsum.shape[0]) linewidth = np.repeat(1, cumsum.shape[0]) if highlight is not None: linestyle[highlight] = "-." linewidth[highlight] = 2 # plot each observation's cumulative SHAP values. ax = pl.gca() ax.set_xlim(xlim) m = cm.ScalarMappable(cmap=plot_color) m.set_clim(xlim) y_pos = np.arange(0, feature_display_count + 1) lines = [] for i in range(cumsum.shape[0]): o = pl.plot(cumsum[i, :], y_pos, color=m.to_rgba(cumsum[i, -1], alpha), linewidth=linewidth[i], linestyle=linestyle[i]) lines.append(o[0]) # determine font size. if ' *\n' character sequence is found (as in interaction labels), use a smaller # font. we don't shrink the font for all interaction plots because if an interaction term is not # in the display window there is no need to shrink the font. s = next((s for s in feature_names if " *\n" in s), None) fontsize = 13 if s is None else 9 # if there is a single observation and feature values are supplied, print them. if (cumsum.shape[0] == 1) and (features is not None): renderer = pl.gcf().canvas.get_renderer() inverter = pl.gca().transData.inverted() y_pos = y_pos + 0.5 for i in range(feature_display_count): v = features[0, i] if isinstance(v, str): v = "({})".format(str(v).strip()) else: v = "({})".format("{0:,.3f}".format(v).rstrip("0").rstrip(".")) t = ax.text(np.max(cumsum[0, i:(i + 2)]), y_pos[i], " " + v, fontsize=fontsize, horizontalalignment="left", verticalalignment="center_baseline", color="#666666") bb = inverter.transform_bbox( t.get_window_extent(renderer=renderer)) if bb.xmax > xlim[1]: t.set_text(v + " ") t.set_x(np.min(cumsum[0, i:(i + 2)])) t.set_horizontalalignment("right") bb = inverter.transform_bbox( t.get_window_extent(renderer=renderer)) if bb.xmin < xlim[0]: t.set_text(v) t.set_x(xlim[0]) t.set_horizontalalignment("left") # style axes ax.xaxis.set_ticks_position("both") ax.yaxis.set_ticks_position("none") ax.spines["right"].set_visible(False) ax.spines["left"].set_visible(False) ax.tick_params(color=axis_color, labelcolor=axis_color, labeltop=True) pl.yticks(np.arange(feature_display_count) + 0.5, feature_names, fontsize=fontsize) ax.tick_params("x", labelsize=11) pl.ylim(0, feature_display_count) pl.xlabel(labels["MODEL_OUTPUT"], fontsize=13) # draw the color bar - must come after axes styling if color_bar: m = cm.ScalarMappable(cmap=plot_color) m.set_array(np.array([0, 1])) # place the colorbar pl.ylim(0, feature_display_count + 0.25) ax_cb = ax.inset_axes( [xlim[0], feature_display_count, xlim[1] - xlim[0], 0.25], transform=ax.transData) cb = pl.colorbar(m, ticks=[0, 1], orientation="horizontal", cax=ax_cb) cb.set_ticklabels([]) cb.ax.tick_params(labelsize=11, length=0) cb.set_alpha(alpha) cb.outline.set_visible(False) # re-activate the main axis for drawing. pl.sca(ax) if title: # TODO decide on style/size pl.title(title) if ascending: pl.gca().invert_yaxis() if legend_labels is not None: ax.legend(handles=lines, labels=legend_labels, loc=legend_location) if show: pl.show()
def generate_plot(sat_target, result, ifig): scene = sat_target.scene fig = plt.figure() fig.set_size_inches(*self.size_inch) gs = gridspec.GridSpec(2, 3, wspace=.15, hspace=.2, left=.1, right=.975, top=.95, height_ratios=[12, 1]) item = PlotItem(name='fig_%i' % ifig, attributes={'targets': [sat_target.path]}, title=u'Satellite Surface Displacements - %s' % scene.meta.scene_title, description=u''' Surface displacements derived from satellite data. (Left) the input data, (center) the modelled data and (right) the model residual. ''') stat_obs = result.statics_obs['displacement.los'] stat_syn = result.statics_syn['displacement.los'] res = stat_obs - stat_syn if scene.frame.isMeter(): offset_n, offset_e = map( float, latlon_to_ne_numpy(scene.frame.llLat, scene.frame.llLon, source.effective_lat, source.effective_lon)) elif scene.frame.isDegree(): offset_n = source.effective_lat - scene.frame.llLat offset_e = source.effective_lon - scene.frame.llLon im_extent = (scene.frame.E.min() - offset_e, scene.frame.E.max() - offset_e, scene.frame.N.min() - offset_n, scene.frame.N.max() - offset_n) if self.displacement_unit == 'rad': wavelength = scene.meta.wavelength if wavelength is None: raise AttributeError( 'The satellite\'s wavelength is not set') stat_obs = displ2rad(stat_obs, wavelength) stat_syn = displ2rad(stat_syn, wavelength) res = displ2rad(res, wavelength) self.colormap = 'hsv' data_range = (0., num.pi) else: abs_displ = num.abs([ stat_obs.min(), stat_obs.max(), stat_syn.min(), stat_syn.max(), res.min(), res.max() ]).max() data_range = (-abs_displ, abs_displ) cmw = cm.ScalarMappable(cmap=self.colormap) cmw.set_clim(*data_range) cmw.set_array(stat_obs) axes = [ fig.add_subplot(gs[0, 0]), fig.add_subplot(gs[0, 1]), fig.add_subplot(gs[0, 2]) ] ax = axes[0] ax.imshow(get_displacement_rgba(stat_obs, scene, cmw), extent=im_extent, origin='lower') draw_leaves(ax, scene, offset_e, offset_n) draw_source(ax, scene) add_arrow(ax, scene) init_axes(ax, scene, 'Observed') ax.text(.025, .025, 'Scene ID: %s' % scene.meta.scene_id, fontsize=8, alpha=.7, va='bottom', transform=ax.transAxes) if scene.frame.isMeter(): ax.set_ylabel('Northing [km]', fontsize=self.font_size) ax = axes[1] ax.imshow(get_displacement_rgba(stat_syn, scene, cmw), extent=im_extent, origin='lower') draw_leaves(ax, scene, offset_e, offset_n) draw_source(ax, scene) add_arrow(ax, scene) init_axes(ax, scene, 'Model') ax.get_yaxis().set_visible(False) ax = axes[2] ax.imshow(get_displacement_rgba(res, scene, cmw), extent=im_extent, origin='lower') draw_leaves(ax, scene, offset_e, offset_n) draw_source(ax, scene) add_arrow(ax, scene) init_axes(ax, scene, 'Residual', last_axes=True) ax.get_yaxis().set_visible(False) for ax in axes: ax.set_xlim(*im_extent[:2]) ax.set_ylim(*im_extent[2:]) if closeup: if scene.frame.isMeter(): fn, fe = source.outline(cs='xy').T elif scene.frame.isDegree(): fn, fe = source.outline(cs='latlon').T fn -= source.effective_lat fe -= source.effective_lon if fn.size > 1: off_n = (fn[0] + fn[1]) / 2 off_e = (fe[0] + fe[1]) / 2 else: off_n = fn[0] off_e = fe[0] fault_size = 2 * num.sqrt( max(abs(fn - off_n))**2 + max(abs(fe - off_e))**2) fault_size *= self.map_scale if fault_size == 0.0: extent = (scene.frame.N[-1] + scene.frame.E[-1]) / 2 fault_size = extent * .25 for ax in axes: ax.set_xlim(-fault_size / 2 + off_e, fault_size / 2 + off_e) ax.set_ylim(-fault_size / 2 + off_n, fault_size / 2 + off_n) if self.map_limits is not None: xmin, xmax, ymin, ymax = self.map_limits assert xmin < xmax, 'bad map_limits xmin > xmax' assert ymin < ymax, 'bad map_limits ymin > ymax' for ax in axes: ax.set_xlim( xmin / ax.scale_x['scale'] - ax.scale_x['offset'], xmax / ax.scale_x['scale'] - ax.scale_x['offset'], ) ax.set_ylim( ymin / ax.scale_y['scale'] - ax.scale_y['offset'], ymax / ax.scale_y['scale'] - ax.scale_y['offset']) if self.displacement_unit == 'm': def cfmt(x, p): return '%.2f' % x elif self.displacement_unit == 'cm': def cfmt(x, p): return '%.1f' % (x * 1e2) elif self.displacement_unit == 'mm': def cfmt(x, p): return '%.0f' % (x * 1e3) elif self.displacement_unit == 'rad': def cfmt(x, p): return '%.2f' % x else: raise AttributeError('unknown displacement unit %s' % self.displacement_unit) cbar_args = dict(orientation='horizontal', format=FuncFormatter(cfmt), use_gridspec=True) cbar_label = 'LOS Displacement [%s]' % self.displacement_unit if self.common_color_scale: cax = fig.add_subplot(gs[1, 1]) cax.set_aspect(.05) cbar = fig.colorbar(cmw, cax=cax, **cbar_args) cbar.set_label(cbar_label) else: for idata, data in enumerate((stat_syn, stat_obs, res)): cax = fig.add_subplot(gs[1, idata]) cax.set_aspect(.05) if not self.displacement_unit == 'rad': abs_displ = num.abs(data).max() cmw.set_clim(-abs_displ, abs_displ) cbar = fig.colorbar(cmw, cax=cax, **cbar_args) cbar.set_label(cbar_label) return (item, fig)
def work(modelDir, inputDict): ''' Run the model in its directory.''' outData = { 'tableData': { 'volts': [[], []], 'powerReal': [[], []], 'powerReact': [[], []] }, # 'charts' : {'volts' : '', 'powerReact' : '', 'powerReal' : ''}, 'voltsChart': '', 'powerReactChart': '', 'powerRealChart': '', 'stdout': '', 'stderr': '' } # Read feeder and convert to .mat. try: networkName = [x for x in os.listdir(modelDir) if x.endswith('.omt')][0][0:-4] except: networkName = 'case9' networkJson = json.load(open(pJoin(modelDir, networkName + ".omt"))) matStr = network.netToMat(networkJson, networkName) with open(pJoin(modelDir, networkName + ".m"), "w") as outMat: for row in matStr: outMat.write(row) # Build the MATPOWER command. matDir = pJoin(__neoMetaModel__._omfDir, 'solvers', 'matpower5.1') if platform.system() == "Windows": pathSep = ";" else: pathSep = ":" matPath = '"' + pathSep.join( [matDir, pJoin(matDir, 't'), pJoin(matDir, 'extras')]) + '"' algorithm = inputDict.get("algorithm", "NR") pfArg = "'pf.alg', '" + algorithm + "'" modelArg = "'model', '" + inputDict.get("model", "AC") + "'" iterCode = "pf." + algorithm[:2].lower() + ".max_it" pfItArg = "'" + iterCode + "', " + str(inputDict.get("iteration", 10)) pfTolArg = "'pf.tol', " + str(inputDict.get("tolerance", math.pow(10, -8))) pfEnflArg = "'pf.enforce_q_lims', " + str(inputDict.get("genLimits", 0)) if platform.system() == "Windows": # Find the location of octave-cli tool. envVars = os.environ["PATH"].split(';') octavePath = "C:\\Octave\\Octave-4.2.0" for pathVar in envVars: if "octave" in pathVar.lower(): octavePath = pathVar # Run Windows-specific Octave command. mpoptArg = "mpoption(" + pfArg + ", " + modelArg + ", " + pfItArg + ", " + pfTolArg + ", " + pfEnflArg + ") " cmd = "runpf('" + pJoin(modelDir, networkName + '.m') + "'," + mpoptArg + ")" args = [octavePath + '\\bin\\octave-cli', '-p', matPath, "--eval", cmd] myOut = subprocess.check_output(args, shell=True) with open(pJoin(modelDir, "matout.txt"), "w") as matOut: matOut.write(myOut) else: # Run UNIX Octave command. mpoptArg = "mpopt = mpoption(" + pfArg + ", " + modelArg + ", " + pfItArg + ", " + pfTolArg + ", " + pfEnflArg + "); " command = "octave -p " + matPath + "--no-gui --eval \"" + mpoptArg + "runpf('" + pJoin( modelDir, networkName + '.m') + "', mpopt)\" > \"" + pJoin( modelDir, "matout.txt") + "\"" proc = subprocess.Popen([command], stdout=subprocess.PIPE, shell=True) (out, err) = proc.communicate() imgSrc = pJoin(__neoMetaModel__._omfDir, 'scratch', 'transmission', 'inData') # Read matout.txt and parse into outData. gennums = [] todo = None with open(pJoin(modelDir, "matout.txt")) as f: for i, line in enumerate(f): # Determine what type of data is coming up. if "How many?" in line: todo = "count" elif "Generator Data" in line: todo = "gen" lineNo = i elif "Bus Data" in line: todo = "node" lineNo = i elif "Branch Data" in line: todo = "line" lineNo = i # Parse lines. line = line.split(' ') line = filter(lambda a: a != '', line) if todo == "count": if "Buses" in line: busCount = int(line[1]) elif "Generators" in line: genCount = int(line[1]) elif "Loads" in line: loadCount = int(line[1]) elif "Branches" in line: branchCount = int(line[1]) elif "Transformers" in line: transfCount = int(line[1]) todo = None elif todo == "gen": if i > (lineNo + 4) and i < (lineNo + 4 + genCount + 1): # gen bus numbers. gennums.append(line[1]) elif i > (lineNo + 4 + genCount + 1): todo = None elif todo == "node": if i > (lineNo + 4) and i < (lineNo + 4 + busCount + 1): # voltage if line[0] in gennums: comp = "gen" else: comp = "node" outData['tableData']['volts'][0].append(comp + str(line[0])) outData['tableData']['powerReal'][0].append(comp + str(line[0])) outData['tableData']['powerReact'][0].append(comp + str(line[0])) outData['tableData']['volts'][1].append(line[1]) outData['tableData']['powerReal'][1].append(line[3]) outData['tableData']['powerReact'][1].append(line[4]) elif i > (lineNo + 4 + busCount + 1): todo = None elif todo == "line": if i > (lineNo + 4) and i < (lineNo + 4 + branchCount + 1): # power outData['tableData']['powerReal'][0].append("line" + str(line[0])) outData['tableData']['powerReact'][0].append("line" + str(line[0])) outData['tableData']['powerReal'][1].append(line[3]) outData['tableData']['powerReact'][1].append(line[4]) elif i > (lineNo + 4 + branchCount + 1): todo = None # Massage the data. for powerOrVolt in outData['tableData'].keys(): for i in range(len(outData['tableData'][powerOrVolt][1])): if outData['tableData'][powerOrVolt][1][i] != '-': outData['tableData'][powerOrVolt][1][i] = float( outData['tableData'][powerOrVolt][1][i]) #Create chart nodeVolts = outData["tableData"]["volts"][1] minNum = min(nodeVolts) maxNum = max(nodeVolts) norm = matplotlib.colors.Normalize(vmin=minNum, vmax=maxNum, clip=True) cmViridis = plt.get_cmap('viridis') mapper = cm.ScalarMappable(norm=norm, cmap=cmViridis) mapper._A = [] plt.figure(figsize=(10, 10)) plt.colorbar(mapper) plt.axis('off') plt.tight_layout() plt.gca().invert_yaxis( ) # HACK: to make latitudes show up right. TODO: y-flip the transEdit.html and remove this. plt.gca().set_aspect('equal') busLocations = {} i = 0 for bus in networkJson["bus"]: for busName, busInfo in bus.items(): y = float(busInfo["latitude"]) x = float(busInfo["longitude"]) plt.plot([x], [y], marker='o', markersize=12.0, color=mapper.to_rgba(nodeVolts[i]), zorder=5) busLocations[busName] = [x, y] i = i + 1 for gen in networkJson["gen"]: for genName, genInfo in gen.items(): x, y = busLocations[genInfo["bus"]] plt.plot([x], [y], 's', color='gray', zorder=10) for branch in networkJson["branch"]: for branchName, branchInfo in branch.items(): x1, y1 = busLocations[branchInfo["fbus"]] x2, y2 = busLocations[branchInfo["tbus"]] plt.plot([x1, x2], [y1, y2], color='black', marker='', zorder=0) plt.savefig(modelDir + '/output.png') with open(pJoin(modelDir, "output.png"), "rb") as inFile: outData["chart"] = inFile.read().encode("base64") # Stdout/stderr. outData["stdout"] = "Success" outData["stderr"] = "" return outData
# -*- coding: utf-8 -*- """ Created on Tue Aug 14 22:17:33 2018 @author: Chen """ import numpy as np import matplotlib.pyplot as plt import matplotlib.cm as cm import matplotlib.colors as col values = np.random.random_integers(99, size=50) values.sort() cmap = cm.ScalarMappable(col.Normalize(0, 99), cm.binary) plt.bar(np.arange(len(values)), values, color =cmap.to_rgba(values)) plt.show()
def render_mesh_helper(mesh, t_center, rot=np.zeros(3), v_colors=None, errors=None, error_unit='m', min_dist_in_mm=0.0, max_dist_in_mm=3.0, z_offset=0): camera_params = {'c': np.array([400, 400]), 'k': np.array([-0.19816071, 0.92822711, 0, 0, 0]), 'f': np.array([4754.97941935 / 2, 4754.97941935 / 2])} frustum = {'near': 0.01, 'far': 3.0, 'height': 800, 'width': 800} mesh_copy = Mesh(mesh.v, mesh.f) mesh_copy.v[:] = cv2.Rodrigues(rot)[0].dot((mesh_copy.v-t_center).T).T+t_center if errors is not None: intensity = 0.5 unit_factor = get_unit_factor('mm')/get_unit_factor(error_unit) errors = unit_factor*errors norm = mpl.colors.Normalize(vmin=min_dist_in_mm, vmax=max_dist_in_mm) cmap = cm.get_cmap(name='jet') colormapper = cm.ScalarMappable(norm=norm, cmap=cmap) rgba_per_v = colormapper.to_rgba(errors) rgb_per_v = rgba_per_v[:, 0:3] elif v_colors is not None: intensity = 0.5 rgb_per_v = v_colors else: intensity = 1.5 rgb_per_v = None tri_mesh = trimesh.Trimesh(vertices=mesh_copy.v, faces=mesh_copy.f, vertex_colors=rgb_per_v) render_mesh = pyrender.Mesh.from_trimesh(tri_mesh, smooth=True) scene = pyrender.Scene(ambient_light=[.2, .2, .2], bg_color=[255, 255, 255]) camera = pyrender.IntrinsicsCamera(fx=camera_params['f'][0], fy=camera_params['f'][1], cx=camera_params['c'][0], cy=camera_params['c'][1], znear=frustum['near'], zfar=frustum['far']) scene.add(render_mesh, pose=np.eye(4)) camera_pose = np.eye(4) camera_pose[:3,3] = np.array([0, 0, 1.0-z_offset]) scene.add(camera, pose=[[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 1], [0, 0, 0, 1]]) angle = np.pi / 6.0 pos = camera_pose[:3,3] light_color = np.array([1., 1., 1.]) light = pyrender.PointLight(color=light_color, intensity=intensity) light_pose = np.eye(4) light_pose[:3,3] = pos scene.add(light, pose=light_pose.copy()) light_pose[:3,3] = cv2.Rodrigues(np.array([angle, 0, 0]))[0].dot(pos) scene.add(light, pose=light_pose.copy()) light_pose[:3,3] = cv2.Rodrigues(np.array([-angle, 0, 0]))[0].dot(pos) scene.add(light, pose=light_pose.copy()) light_pose[:3,3] = cv2.Rodrigues(np.array([0, -angle, 0]))[0].dot(pos) scene.add(light, pose=light_pose.copy()) light_pose[:3,3] = cv2.Rodrigues(np.array([0, angle, 0]))[0].dot(pos) scene.add(light, pose=light_pose.copy()) flags = pyrender.RenderFlags.SKIP_CULL_FACES r = pyrender.OffscreenRenderer(viewport_width=frustum['width'], viewport_height=frustum['height']) color, _ = r.render(scene, flags=flags) return color[..., ::-1]
def draw_3d_plot(ax: mpl.axes.Axes, x: np.ndarray, y: np.ndarray, z: np.ndarray, plot_type: str = 'contour', marker: str = 'X', marker_size: int = 50, marker_color: str = 'red', interpolation: str = 'linear', cmap: matplotlib.colors.Colormap = matplotlib.cm.RdBu_r, min_level: float = math.nan, max_level: float = math.nan) -> None: '''Draw a 3d plot. See XYZData class for explanation of arguments >>> points = np.random.rand(1000, 2) >>> x = np.random.rand(10) >>> y = np.random.rand(10) >>> z = x ** 2 + y ** 2 >>> if has_display(): ... fig, ax = plt.subplots() ... draw_3d_plot(ax, x = x, y = y, z = z, plot_type = 'contour', interpolation = 'linear'); ''' xi = np.linspace(min(x), max(x)) yi = np.linspace(min(y), max(y)) X, Y = np.meshgrid(xi, yi) Z = griddata((x, y), z, (xi[None, :], yi[:, None]), method=interpolation) Z = np.nan_to_num(Z) if plot_type == 'surface': ax.plot_surface(X, Y, Z, cmap=cmap) if marker is not None: ax.scatter(x, y, z, marker=marker, s=marker_size, c=marker_color) m = cm.ScalarMappable(cmap=cmap) m.set_array(Z) plt.colorbar(m, ax=ax) elif plot_type == 'contour': # extract all colors from the map cmaplist = [cmap(i) for i in range(cmap.N)] # create the new map cmap = cmap.from_list('Custom cmap', cmaplist, cmap.N) Z = np.ma.masked_array(Z, mask=~np.isfinite(Z)) if math.isnan(min_level): min_level = np.min(Z) if math.isnan(max_level): max_level = np.max(Z) # define the bins and normalize and forcing 0 to be part of the colorbar! bounds = np.arange(min_level, max_level, (max_level - min_level) / cmap.N) idx = np.searchsorted(bounds, 0) bounds = np.insert(bounds, idx, 0) norm = BoundaryNorm(bounds, cmap.N) cs = ax.contourf(X, Y, Z, cmap=cmap, norm=norm) if marker is not None: x = x[np.isfinite(z)] y = y[np.isfinite(z)] ax.scatter(x, y, marker=marker, s=marker_size, c=z[np.isfinite(z)], zorder=10, cmap=cmap) LABEL_SIZE = 16 ax.tick_params(axis='both', which='major', labelsize=LABEL_SIZE) ax.tick_params(axis='both', which='minor', labelsize=LABEL_SIZE) cbar = plt.colorbar(cs, ax=ax) cbar.ax.tick_params(labelsize=LABEL_SIZE) else: raise Exception(f'unknown plot type: {plot_type}')
import matplotlib.cm as cmx # suC = fig.add_subplot(3,2,3) gsC = gridspec.GridSpecFromSubplotSpec(3, 3, subplot_spec=outergs[0, 0], hspace=0.2, wspace=0.5) axC = {} labels = ['a\nHD', 'b\nNon-bursty', 'c\nBursty'] titles = ['Wake', 'REM', 'NREM'] viridis = get_cmap('viridis') cNorm = colors.Normalize(vmin=burst['sws'].min(), vmax=burst['sws'].max()) scalarMap = cmx.ScalarMappable(norm=cNorm, cmap=viridis) colors = [ 'red', scalarMap.to_rgba(burst.loc[neurontoplot[1], 'sws']), scalarMap.to_rgba(burst.loc[neurontoplot[2], 'sws']) ] for i in range(3): for l, j in enumerate(['wake', 'rem', 'sws']): tmp = store_autocorr[j][neurontoplot[i]] tmp[0] = 0.0 tmp = tmp.rolling(window=20, win_type='gaussian', center=True, min_periods=1).mean(std=3.0)
zz = [mea.electrodes[e].position[2] + elec_size, mea.electrodes[e].position[2] - elec_size, mea.electrodes[e].position[2] - elec_size, mea.electrodes[e].position[2] + elec_size] xx = [0, 0, 0, 0] verts.append(list(zip(yy, zz, xx))) jet = plt.get_cmap('jet') colors = mea.get_currents() / np.max(np.abs(mea.get_current_matrix())) + 1 curr = ax2.add_collection3d(Poly3DCollection(verts, # zorder=1, alpha=0.8, color=jet(colors))) currents = mea.get_currents()/1000 m = cm.ScalarMappable(cmap=cm.jet) bounds = np.round(np.linspace(np.min(currents), np.max(currents), 7)) norm = mpl_colors.BoundaryNorm(bounds, cm.jet) m.set_array(currents) cbar_ax2 = plt.colorbar(m, cax=cax2, norm=norm, boundaries=bounds) cbar_ax2.set_label('mA', rotation=270) # ghost_axis = ax2.scatter(xx, yy, zz, color=jet(colors)) # cax2 = fig.add_subplot(gs[4, 9:]) # fig.colorbar(ghost_axis, cax=cax2) # ghost_axis.axis('off') # cmap = plt.cm.jet # norm = mpl.colors.BoundaryNorm(colors, cmap) # cb = mpl.colorbar.ColorbarBase(ax2, # cmap=cax2, # norm=norm)
def reconstructContactMap( vector, mapping, truecontacts=None, plot=True, figsize=(7, 7), dpi=80, title=None, outfile=None, colors=None, ): """ Plots a given vector as a contact map Parameters ---------- vector : np.ndarray or list A 1D vector of contacts mapping : pd.DataFrame A pandas DataFrame which describes the dimensions of the projection truecontacts : np.ndarray or list A 1D vector of true contacts plot : bool To plot or not to plot figsize : tuple The size of the final plot in inches dpi : int Dots per inch outfile : str Path of file in which to save the plot Returns ------- cm : np.ndarray The input vector converted into a 2D numpy array Examples -------- >>> reconstructContactMap(contacts, mapping) To use it with distances instead of contacts pass ones as the concat vector >>> reconstructContactMap(np.ones(dists.shape, dtype=bool), mapping, colors=dists) """ from copy import deepcopy from matplotlib import cm as colormaps if np.ndim(vector) != 1: raise RuntimeError( "Please pass a 1D vector to the reconstructContactMap function.") if truecontacts is None: truecontacts = np.zeros(len(vector), dtype=bool) if len(vector) != len(mapping): raise RuntimeError("Vector and map length must match.") # Checking if contacts or distances exist in the data contactidx = mapping.type == "contact" if not np.any(contactidx): contactidx = mapping.type == "distance" if not np.any(contactidx): raise RuntimeError( "No contacts or distances found in the MetricData object. Check the `.map` property of the object for a description of your projection." ) # Creating the 2D contact maps cm, newmapping, uqAtomGroups = contactVecToMatrix(vector, mapping.atomIndexes) cmtrue, _, _ = contactVecToMatrix(truecontacts, mapping.atomIndexes) num = len(uqAtomGroups) if plot: from matplotlib import pylab as plt f = plt.figure(figsize=figsize, dpi=dpi) plt.imshow( cmtrue / 2, interpolation="none", vmin=0, vmax=1, aspect="equal", cmap="Greys", ) # /2 to convert to gray from black rows, cols = np.where(cm) colorbar = False if colors is None: truecms = vector & truecontacts colors = np.array(["r"] * len(vector), dtype=object) colors[truecms] = "#ffff00" elif isinstance(colors, np.ndarray) and isinstance(colors[0], float): mpbl = colormaps.ScalarMappable(cmap=colormaps.jet) mpbl.set_array(colors) colors = mpbl.to_rgba(colors) colorbar = True if len(colors) == len(vector): colors = colors[newmapping[rows, cols]] plt.scatter(rows, cols, s=figsize[0] * 5, marker="o", c=colors, lw=0) if colorbar: plt.colorbar(mpbl) ax = f.axes[0] # Major ticks ax.set_xticks(np.arange(0, num, 1)) ax.set_yticks(np.arange(0, num, 1)) # Labels for major ticks ax.set_xticklabels([x[0] for x in uqAtomGroups]) ax.set_yticklabels([x[0] for x in uqAtomGroups], ) # Minor ticks ax.set_xticks(np.arange(-0.5, num, 1), minor=True) ax.set_yticks(np.arange(-0.5, num, 1), minor=True) plt.setp(ax.xaxis.get_majorticklabels(), rotation=90) # Gridlines based on minor ticks ax.grid(which="minor", color="#969696", linestyle="-", linewidth=1) ax.tick_params(axis="both", which="both", length=0) plt.xlim([-0.5, num - 0.5]) plt.ylim([-0.5, num - 0.5]) plt.xlabel("Atom index") plt.ylabel("Atom index") if title: plt.title(title) if outfile is not None: plt.savefig(outfile, dpi=dpi, bbox_inches="tight", pad_inches=0.2) plt.close() else: plt.show() return cm
def dmap_compare_v2(d_old, yhat, suptitle='Distortion Map Comparison', title1='Original Distortion Map', title2='Distortion Map after error correction'): from dmap import dmap from matplotlib.colors import Normalize import matplotlib.cm as cm import matplotlib.pyplot as plt d_new = d_old.copy() x_, y_ = pol2cart(np.deg2rad(yhat[:, 1]), yhat[:, 0]) ####### d_new["x_img"] = d_old["x_img"] - x_ d_new["y_img"] = d_old["y_img"] - y_ #d_new["x_img"] = d_old["x_img"] - d_old['xdiff'] #d_new["y_img"] = d_old["y_img"] - d_old['ydiff'] d_new["xdiff"] = d_new["x_img"] - d_new["x_act"] d_new["ydiff"] = d_new["y_img"] - d_new["y_act"] d_new["mag"] = np.sqrt(d_new["xdiff"]**2 + d_new["ydiff"]**2) dmap_old = dmap(d_old, show_plot=False) dmap_new = dmap(d_new, show_plot=False) norm = Normalize() norm.autoscale(dmap_old['v_colors']) colormap = cm.jet sm = cm.ScalarMappable(cmap=colormap, norm=norm) sm.set_array([]) fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(14, 5), facecolor='w', edgecolor='k') fig.set_tight_layout(False) ax1 = plt.subplot(121) dm = dmap_old.copy() plt.title(title1) plt.quiver(dm['v_err_loc_x'], dm['v_err_loc_y'], dm['v_err_x'], dm['v_err_y'], angles='xy', scale_units='xy', scale=0.02, color=colormap(norm(dm['v_colors']))) fig.colorbar(sm) ax2 = plt.subplot(122) dm = dmap_new.copy() plt.title(title2) plt.quiver(dm['v_err_loc_x'], dm['v_err_loc_y'], dm['v_err_x'], dm['v_err_y'], angles='xy', scale_units='xy', scale=0.02, color=colormap(norm(dm['v_colors']))) fig.colorbar(sm) plt.suptitle(suptitle, fontsize=16) return
for x, y in zip(X, Y): meanerror = meanerror + (y - Sy/N)**2 residual = residual + (y - a * x - b)**2 RR = 1 - residual/meanerror ss = residual / (N-2) Var_a, Var_b = ss * N / det, ss * Sxx / det return a, b, RR, Var_a, Var_b f=open('urbanization-vs-gdp.csv', 'r',encoding='latin-1') reader=csv.reader(f,delimiter=',') window=30 norm = colors.Normalize(vmin=1, vmax=2*165) sm = cm.ScalarMappable(norm, cmap=cm.Paired) cnt = 0 c=0 nation='' uall=[] gall=[] xall=[] yall=[] nav=20 avxx=np.zeros(nav) avyy=np.zeros(nav) avcount=np.zeros(nav) for row in reader: if (row[1]!='' and row[3]!='' and row[4]!='' and row[4].isdigit() and float(row[3])<99. ):
def apply_alpha(colors, alpha, elem_list, cmap=None, vmin=None, vmax=None): """Apply an alpha (or list of alphas) to the colors provided. Parameters ---------- colors : color string, or array of floats Color of element. Can 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. alpha : float or array of floats Alpha values for elements. This can be a single alpha value, in which case it will be applied to all the elements of color. Otherwise, if it is an array, the elements of alpha will be applied to the colors in order (cycling through alpha multiple times if necessary). elem_list : array of networkx objects The list of elements which are being colored. These could be nodes, edges or labels. cmap : matplotlib colormap Color map for use if colors is a list of floats corresponding to points on a color mapping. vmin, vmax : float Minimum and maximum values for normalizing colors if a color mapping is used. Returns ------- rgba_colors : numpy ndarray Array containing RGBA format values for each of the node colours. """ import numbers from itertools import islice, cycle try: import numpy as np from matplotlib.colors import colorConverter import matplotlib.cm as cm except ImportError: raise ImportError("Matplotlib required for draw()") # If we have been provided with a list of numbers as long as elem_list, # apply the color mapping. if len(colors) == len(elem_list) and isinstance(colors[0], numbers.Number): mapper = cm.ScalarMappable(cmap=cmap) mapper.set_clim(vmin, vmax) rgba_colors = mapper.to_rgba(colors) # Otherwise, convert colors to matplotlib's RGB using the colorConverter # object. These are converted to numpy ndarrays to be consistent with the # to_rgba method of ScalarMappable. else: try: rgba_colors = np.array([colorConverter.to_rgba(colors)]) except ValueError: rgba_colors = np.array( [colorConverter.to_rgba(color) for color in colors]) # Set the final column of the rgba_colors to have the relevant alpha values try: # If alpha is longer than the number of colors, resize to the number of # elements. Also, if rgba_colors.size (the number of elements of # rgba_colors) is the same as the number of elements, resize the array, # to avoid it being interpreted as a colormap by scatter() if len(alpha) > len(rgba_colors) or rgba_colors.size == len(elem_list): rgba_colors.resize((len(elem_list), 4)) rgba_colors[1:, 0] = rgba_colors[0, 0] rgba_colors[1:, 1] = rgba_colors[0, 1] rgba_colors[1:, 2] = rgba_colors[0, 2] rgba_colors[:, 3] = list(islice(cycle(alpha), len(rgba_colors))) except TypeError: rgba_colors[:, -1] = alpha return rgba_colors
"generic": 2, } mDarkOrder = { "1": 0, "2": 1, "5": 2, } tempOrder = { # "1": 0, "2": 0, "5": 1, } jet = cm = plt.get_cmap('jet') cNorm = colors.Normalize(vmin=0, vmax=values[-1]) scalarMap = cmx.ScalarMappable(norm=cNorm, cmap=jet) #mpl.rcParams['lines.linewidth'] = 3 # Set multiplicity linspace bins = 100 max = 600 min = 0 trkMlt_space = np.linspace(min, max, bins) # Select s/sqrt(s+b) for significance significanceMode = 1 mMed = "400" mDark = "2" #temp = "2" decay = "darkPho"
def draw_3d(x, y, z, time, attri, grid_line, touming): print(x,y,z) print(time) print(attri) print("属性↑ 网格线↓") print(grid_line) tmp = [] color = [] colors = [] with open('cutted2csv\\%s'%time, 'r') as f: reader = csv.reader(f) col1 = [row[attri] for row in reader] color = np.array(col1).astype(np.float) norm = plt.Normalize() colors = plt.cm.jet(norm(color)) # colors=get_colors(color,plt.cm.jet) tmp = np.array(col1).reshape(x, y, z) # print(tmp) #print(colors[0]) #colors[:3]=0.5 index=0 for i in colors: colors[index][3]=touming index=index+1 #print(colors[0]) #print(len(colors)) after = np.reshape(colors, (x, y, z, 4)) # after[1:-1, 1:-1, 1:-1, 0:3] = 0 # after[1:-1, 1:-1, 1:-1, 3] = 1 #print(color[0]) #print(after[0]) #print(len(after)) # r, theta, z = np.mgrid[0:19:1j, 0:19:1j, 0:19:1j] # test = np.zeros((20, 20, 20)) # test[..., 0] = np.arange(0, 20) # test[..., 1] = np.arange(0, 20) # test[..., 2] = np.arange(0, 20) # colors=matplotlib.colors.hsv_to_rgb(tmp) # fig = plt.figure() # ax = fig.gca(projection='3d') # #ax.set_aspect('equal') # ax.voxels(tmp, edgecolors='k',color=colors) # plt.title('this is a test for 3d') # #plt.colorbar() # plt.show() spatial_axes = [x, y, z] filled = tmp colors = after # spatial_axes = [5, 5, 5] # filled = np.ones(spatial_axes, dtype=np.bool) # # colors = np.empty(spatial_axes + [4], dtype=np.float32) # alpha = 1 # colors[0] = [1, 0, 0, alpha] # colors[1] = [0, 1, 0, alpha] # colors[2] = [0, 0, 1, alpha] # colors[3] = [1, 1, 0, alpha] # colors[4] = [0, 1, 1, alpha] # # # set all internal colors to black with alpha=1 # colors[1:-1, 1:-1, 1:-1, 0:3] = 0 # 在里面的 # colors[1:-1, 1:-1, 1:-1, 3] = 1 fig = plt.figure() #print() #print(colors[0]) #print(len(colors)) ax = fig.add_subplot('111', projection='3d') if grid_line==0: ax.voxels(filled, facecolors=colors)#, edgecolors='k') elif grid_line==1: ax.voxels(filled, facecolors=colors, edgecolors='k') #fig.colorbar(p) m = cm.ScalarMappable(cmap=plt.cm.jet, norm=norm) m.set_array([]) plt.colorbar(m) #plt.colorbar(colors) # plt.title('this is a test') plt.show() #draw_3d(25,25,15," 1.55000000E+03.csv",1,1,0.5)
def heatmap(shap_values, input_order=order.abs.mean, sample_order=order.hclust, max_display=10, cmap=colors.red_white_blue, show=True): """ Create a heatmap plot of a set of SHAP values. This plot is designed to show the population substructure of a dataset using supervised clustering and a heatmap. Supervised clustering involves clustering data points not by their original feature values but by their explanations. By default we cluster using shap.utils.hclust_ordering but any clustering can be used to order the samples. Parameters ---------- shap_values : shap.Explanation A multi-row Explanation object that we want to visualize in a cluster ordering. input_order : callable or numpy.ndarray A function that returns a sort ordering given a matrix of SHAP values and an axis, or a direct input feature ordering given as an numpy.ndarray. sample_order : callable or numpy.ndarray A function that returns a sort ordering given a matrix of SHAP values and an axis, or a direct sample ordering given as an numpy.ndarray. max_display : int The maximum number of features to display. show : bool If show is set to False then we don't call the matplotlib.pyplot.show() function. This allows further customization of the plot by the caller after the bar() function is finished. """ # sort the SHAP values matrix by rows and columns values = shap_values.values if type(input_order) == type(order): input_order, order_values = input_order.apply(shap_values.values, 1, return_values=True) elif not hasattr(input_order, "__len__"): raise Exception("Unsupported input_order: %s!" % str(input_order)) if type(sample_order) == type(order): sample_order = sample_order.apply(shap_values.values, 0) elif not hasattr(sample_order, "__len__"): raise Exception("Unsupported sample_order: %s!" % str(sample_order)) feature_names = np.array(shap_values.feature_names)[input_order] values = shap_values.values[sample_order][:, input_order] order_values = order_values[input_order] # collapse if values.shape[1] > max_display: new_values = np.zeros((values.shape[0], max_display)) new_values[:, :max_display - 1] = values[:, :max_display - 1] new_values[:, max_display - 1] = values[:, max_display - 1:].sum(1) new_order_values = np.zeros(max_display) new_order_values[:max_display - 1] = order_values[:max_display - 1] new_order_values[max_display - 1] = order_values[max_display - 1:].sum() feature_names = list(feature_names[:max_display]) feature_names[-1] = "%d other features" % (values.shape[1] - max_display + 1) values = new_values order_values = new_order_values # define the plot size row_height = 0.5 pl.gcf().set_size_inches(8, values.shape[1] * row_height + 2.5) # plot the matrix of SHAP values as a heat map vmin = np.nanpercentile(values.flatten(), 1) vmax = np.nanpercentile(values.flatten(), 99) pl.imshow(values.T, aspect=0.7 * values.shape[0] / values.shape[1], interpolation="nearest", vmin=min(vmin, -vmax), vmax=max(-vmin, vmax), cmap=cmap) yticks_pos = np.arange(values.shape[1]) yticks_labels = feature_names pl.yticks([-1.5] + list(yticks_pos), ["f(x)"] + list(yticks_labels), fontsize=13) pl.ylim(values.shape[1] - 0.5, -3) pl.gca().xaxis.set_ticks_position('bottom') pl.gca().yaxis.set_ticks_position('left') pl.gca().spines['right'].set_visible(True) pl.gca().spines['top'].set_visible(False) pl.gca().spines['bottom'].set_visible(False) pl.axhline(-1.5, color="#aaaaaa", linestyle="--", linewidth=0.5) fx = values.T.mean(0) pl.plot(-fx / np.abs(fx).max() - 1.5, color="#000000", linewidth=1) #pl.colorbar() pl.gca().spines['left'].set_bounds(values.shape[1] - 0.5, -0.5) pl.gca().spines['right'].set_bounds(values.shape[1] - 0.5, -0.5) b = pl.barh( yticks_pos, (order_values / np.abs(order_values).max()) * values.shape[0] / 20, 0.7, align='center', color="#000000", left=values.shape[0] * 1.0 - 0.5 #color=[colors.red_rgb if shap_values[feature_inds[i]] > 0 else colors.blue_rgb for i in range(len(y_pos))] ) for v in b: v.set_clip_on(False) pl.xlim(-0.5, values.shape[0] - 0.5) pl.xlabel("Samples ordered by similarity") if True: import matplotlib.cm as cm m = cm.ScalarMappable(cmap=cmap) m.set_array([min(vmin, -vmax), max(-vmin, vmax)]) cb = pl.colorbar(m, ticks=[min(vmin, -vmax), max(-vmin, vmax)], aspect=1000, fraction=0.0090, pad=0.10, panchor=(0, 0.05)) #cb.set_ticklabels([min(vmin,-vmax), max(-vmin,vmax)]) cb.set_label("SHAP value", size=12, labelpad=-10) cb.ax.tick_params(labelsize=11, length=0) cb.set_alpha(1) cb.outline.set_visible(False) bbox = cb.ax.get_window_extent().transformed( pl.gcf().dpi_scale_trans.inverted()) cb.ax.set_aspect((bbox.height - 0.9) * 15) cb.ax.set_anchor((1, 0.2)) #cb.draw_all() for i in [0]: pl.gca().get_yticklines()[i].set_visible(False) if show: pl.show()
def plot_SEDs(ID, hdulist, rows_indices, x_log=False, print_title=True): # From the (previously loaded) observed catalogue select the row # corresponding to the input ID observation = extract_row(observed_catalogue.data, ID) # Check if you need to apply an aperture correction to the catalogue fluxes if 'aper_corr' in observed_catalogue.data.dtype.names: aper_corr = 10.**(-0.4*observation[0]['aper_corr']) else: aper_corr = 1. # Put observed photometry and its error in arrays obs_flux, obs_flux_err = observed_catalogue.extract_fluxes(filters, ID) ok = np.where(obs_flux_err > 0.)[0] obs_flux[ok] *= 1.E+09 obs_flux_err[ok] *= 1.E+09 if x_log: wl_eff = np.log10(filters.data['wl_eff']) else: wl_eff = np.array(filters.data['wl_eff']) # Sort wl_eff array sor = np.argsort(wl_eff) obs_flux, obs_flux_err, wl_eff = obs_flux[sor], obs_flux_err[sor], wl_eff[sor] ok = np.where(obs_flux_err > 0.)[0] # Initialize figure fig = plt.figure() ax = fig.add_subplot(1, 1, 1) # Define axes labels if x_log: ax.set_xlabel("$\\log (\lambda_\\textnormal{eff} / \\textnormal{\AA}$ (observed-frame))") else: ax.set_xlabel("$\lambda_\\textnormal{eff} / \\textnormal{\AA}$ (observed-frame)") ax.set_ylabel("$f_{\\nu}/\\textnormal{nanoJy}$") # Set better location of tick marks set_plot_ticks(ax, n_x=5) kwargs = {'alpha':0.8} dwl = wl_eff[-1] - wl_eff[0] ax.set_xlim(wl_eff[0]-dwl*0.1, wl_eff[-1]+dwl*0.1) # Plot the data with errors bars plt.errorbar(wl_eff[ok], obs_flux[ok], yerr = obs_flux_err[ok], color = "dodgerblue", ls = " ", marker = "D", markeredgewidth = 0., markersize = 8, elinewidth=1.0, capsize=3, **kwargs) cm = plt.get_cmap('Spectral') cNorm = colors.Normalize(vmin=0, vmax=len(rows_indices)) scalarMap = cmx.ScalarMappable(norm=cNorm, cmap=cm) model_mag = np.zeros(filters.n_bands) # Plot the model model_sed = hdulist['marginal photometry'] for j, row in enumerate(rows_indices): color = scalarMap.to_rgba(len(rows_indices)-j) for i, band_name in enumerate((filters.data['label'][sor])): model_mag[i] = model_sed.data['_'+band_name+'_'][row] / nanoJy plt.scatter(wl_eff[ok], model_mag[ok], color = color, marker = "*", s = 34, lw=0.2, edgecolor='black', alpha = 0.7, zorder=3 ) chi_square = np.sum(((model_mag[ok]-obs_flux[ok]) / obs_flux_err[ok])**2) plt.scatter(0.03, 0.95-j*0.03, color = color, marker = "*", s = 34, lw=0.2, edgecolor='black', transform=ax.transAxes) chi_square = np.sum(((model_mag[ok]-obs_flux[ok]) / obs_flux_err[ok])**2) ax.text(0.04, 0.95-j*0.03, "{:.2f}".format(chi_square), horizontalalignment='left', verticalalignment='center', fontsize=10, weight='heavy', color='black', transform=ax.transAxes) # Title of the plot is the object ID if print_title: plt.title(str(ID)) name = os.path.join(plots_folder, str(ID)+'_SED.pdf') fig.savefig(name, dpi=None, facecolor='w', edgecolor='w', orientation='portrait', papertype='a4', format="pdf", transparent=False, bbox_inches="tight", pad_inches=0.1) plt.close(fig)
def prepare_dataset(dataset, normalized_length, num_dstat_features, data_type, features_regex, sample_interval='1s', class_label='status', aggregation_functions=None, visualize=False, data_path=None, target_data_path=None, s3=None): """Takes a dataset and filters and does the magic Loads the run ids from the dataset configuration. Loads the data (dsv + meta) for every run from cache. Builds the unrolled examples as a numpy ndarray. Builds the classes as a numpy array. Saves the data setup to the dataset config. Does some visualization (if enabled). """ if visualize: data_plots_folder = [ os.path.dirname(os.path.realpath(__file__)), os.pardir, 'data', dataset, 'plots' ] os.makedirs(os.sep.join(data_plots_folder), exist_ok=True) # Load the list of runs and base labels runs = gather_results.load_run_uuids(dataset, name=data_type, data_path=target_data_path, s3=s3) # run_uuids are the example_ids sizes = [] # The data for each example. examples = examples_ndarray(len(runs), num_dstat_features, normalized_length) # The test result for each example classes = [] skips = [] print("Loading %s data:" % data_type, end='\r', flush=True) for count, run in enumerate(runs): print("Loading %s data: %d of %d" % (data_type, count + 1, len(runs)), end='\r', flush=True) result = gather_results.get_subunit_results_for_run( run, sample_interval, data_path=data_path, s3=s3) # For one run_uuid we must only get on example (result) # Filtering by columns if not result: skips.append(run.uuid) continue # Apply column filtering result = filter_example(result, features_regex) # Normalize data example = fixed_lenght_example(result, normalized_length, aggregation_functions) vector = unroll_example(example, normalized_length) # Normalize status status = get_class(result, class_label) # Examples is an np ndarrays examples[count] = vector.values classes.append(status) # Plot from figures if visualize and not aggregation_functions: # Prepare some more data if we are going to visualize sizes.append((result['dstat'].shape[0], status)) figure_name = sample_interval + "_%s_" + str(count) # Plot un-normalized data data_plot = result['dstat'].plot() fig = data_plot.get_figure() fig.savefig( os.sep.join(data_plots_folder + [figure_name % "downsampled"])) plt.close(fig) # Plot fixed size data fixed_plot = example.plot() fig = fixed_plot.get_figure() fig.savefig( os.sep.join(data_plots_folder + [figure_name % "fixedsize"])) plt.close(fig) # Plot unrolled data unrolled_plot = pd.Series(vector).plot() fig = unrolled_plot.get_figure() fig.savefig( os.sep.join(data_plots_folder + [figure_name % "unrolled"])) plt.close(fig) print("Loading %s data: %d done!" % (data_type, len(runs))) # Check that everything went well if len(skips) > 0: print('Unable to train model because of missing runs %s' % skips) safe_runs = [run.uuid for run in runs if run.uuid not in skips] gather_results.save_run_uuids(dataset, safe_runs) print('The model has been updated to exclude those runs.') print('Please re-run the training step.') sys.exit(1) classes = np.array(classes) figure_sizes = np.array(sizes) example_ids = np.array(runs) print("%s set: examples: %s, classes: %s, example IDs: %s" % (data_type, str(examples.shape), str( classes.shape), str(example_ids.shape))) data = { 'examples': examples, 'example_ids': example_ids, 'classes': classes } if visualize and aggregation_functions and len(examples) > 0: if len(aggregation_functions) > 3: print('Visualization skipped, cannot represent more than 3D') sys.exit(1) else: fig = plt.figure() if len(aggregation_functions) == 3: ax = fig.add_subplot(111, projection='3d') else: ax = fig.add_subplot(111) # Build a dict [class] -> [int ID] unique_classes = list(set(classes)) dict_classes = dict( zip(unique_classes, list(range(len(unique_classes))))) # Setup colours cm = plt.get_cmap('jet') cNorm = pltcolors.Normalize(vmin=0, vmax=len(unique_classes)) scalarMap = cmx.ScalarMappable(norm=cNorm, cmap=cm) # Scatter the data for ii in range(len(examples)): ax.scatter(*examples[ii], marker='o', c=scalarMap.to_rgba(dict_classes[classes[ii]])) # Set axis labels ax.set_xlabel(aggregation_functions[0].__name__) if len(aggregation_functions) > 1: ax.set_ylabel(aggregation_functions[1].__name__) if len(aggregation_functions) > 2: ax.set_zlabel(aggregation_functions[2].__name__) # scalarMap.set_array(classes) # fig.colorbar(scalarMap) # Save the plot fig.savefig( os.sep.join(data_plots_folder + [data_type + "_3d_plot"])) plt.close(fig) return data, figure_sizes
def plot_traces(log, include=['TN1', 'TN2', 'CL1', 'TB1', 'CPU4', 'CPU1', 'motor'], fig=None, ax=None, colormap='viridis', title_x=-0.15, alpha=0.2, outbound_color='purple', return_color='g', label_font_size=11, unit_font_size=10, dashes=[1, 2, 1, 2], T_almost_home=None, t_start=0): """Generate big plot with all traces of model. Warning: takes long time to save!!""" T, T_outbound, T_inbound = log.T, log.T_outbound, log.T_inbound titles = {'TN1': 'TN1 (Speed)', 'TN2': 'TN2 (Speed)', 'TL2': 'TL2', 'CL1': 'CL1', 'TB1': 'TB1 (Compass)', 'CPU4': 'CPU4 (Memory)', 'CPU1': 'CPU1 (Steering)', 'motor': 'motor'} data = {'TN1': log.tn1, 'TN2': log.tn2, 'TL2': log.tl2, 'CL1': log.cl1, 'TB1': log.tb1, 'CPU4': log.cpu4, 'CPU1': log.cpu1, 'motor': log.motor} colors = {'TL2': tl2_color, 'CL1': cl1_color} # Generate new plot if no axes passed in. if ax is None: fig, ax = plt.subplots(len(include), 1, figsize=(nature_single, nature_single)) N_plots = len(include) for i, cell_type in enumerate(include): ax[i].set_title(titles[cell_type], x=title_x, y=0.3, va='center', ha='right', fontsize=label_font_size, fontweight='heavy') ax[i].set_xticklabels([]) ax[i].tick_params(labelsize=unit_font_size) if cell_type in ['TN1', 'TN2']: filtered_l = sp.ndimage.filters.gaussian_filter1d( data[cell_type][0], sigma=20) filtered_r = sp.ndimage.filters.gaussian_filter1d( data[cell_type][1], sigma=20) tn_l_line, = ax[i].plot(filtered_l, color=flow_color_L, label='L'); tn_r_line, = ax[i].plot(filtered_r, color=flow_color_R, label='R'); handles = [tn_l_line, tn_r_line] ax[i].plot(data[cell_type][0].T, color=flow_color_L, alpha=0.3, lw=0.5); ax[i].plot(data[cell_type][1].T, color=flow_color_R, alpha=0.3, lw=0.5); ax[i].set_yticks([0.05, 0.9]) ax[i].set_yticklabels([0, 1]) # Make a legend but not for both if i % 2 == 0: l = ax[i].legend(handles=handles, bbox_to_anchor=(1.15, 1.2), loc='upper right', ncol=1, fontsize=unit_font_size, handlelength=0, handletextpad=0) colors = [flow_color_L, flow_color_R] for i, text in enumerate(l.get_texts()): text.set_color(colors[i]) for handle in l.legendHandles: handle.set_visible(False) l.draw_frame(False) elif cell_type in ['TL2', 'CL1'] and data[cell_type].shape[0] == 1: ax[i].plot(data[cell_type][0], color=colors[cell_type]); ax[i].set_yticks([-np.pi, np.pi]) ax[i].set_yticklabels([0, 360]) elif cell_type in ['TL2', 'CL1', 'TB1', 'CPU4', 'CPU1']: # Surface plots related to memory generation. p = ax[i].pcolormesh(data[cell_type], vmin=0, vmax=1, cmap=colormap, rasterized=True); ax[i].get_xaxis().set_tick_params(direction='out') if cell_type == 'TB1': ax[i].set_yticks([1, 7]) ax[i].set_yticklabels([1, 8]) else: ax[i].set_yticks([1, 14]) ax[i].set_yticklabels([1, 16]) if cell_type == 'CPU1': # We add alpha to the outbound part fig.savefig('plots/dummy.png') # This is needed to force draw plot p.get_facecolors().reshape(16, -1, 4)[:, :T_outbound, 3] = 0.1 p.set_edgecolor('none') else: p.set_edgecolor('face') else: # Plots related to steering plot_motor_trace(ax[i], log.motor, T_outbound, T_inbound, outbound_color, return_color, alpha, label_font_size, unit_font_size, t_start=t_start); # Add label half way (ish) down plot ax[0].set_ylabel('Activity', fontsize=label_font_size) #ax[1].yaxis.set_label_coords(-0.075, 1.1) ax[3].set_ylabel('Cell indices', fontsize=label_font_size) ax[3].yaxis.set_label_coords(-0.075, 1.1) # Add x labels to bottom plot ax[N_plots-1].set_xlabel('Time (steps)', fontsize=label_font_size) ax[N_plots-1].get_xaxis().set_major_formatter( mpl.ticker.FuncFormatter(lambda x, p: format(int(x), ','))) # Colorbar ax_cb = fig.add_axes([0.92, 0.257, 0.02, 0.410]) m = cm.ScalarMappable(cmap=colormap) m.set_array(np.linspace(0, 1, 100)) ax_cb.tick_params(labelsize=unit_font_size) cb = fig.colorbar(m, ax_cb) cb.set_ticks(np.linspace(0,1,6)) cb.set_label('Firing rate', size=label_font_size) # Top spacer ax_space = fig.add_axes([0, 0.92, 1, 0.01]) ax_space.axis('off') # Dotted bars if T_almost_home is None: T_almost_home = T_outbound + 400 # TODO(tomish) Auto generate this v_indices = np.array([0, T_outbound, T_almost_home, T]) transFigure = fig.transFigure.inverted() for i, v_idx in enumerate(v_indices): y_max = ax[0].get_ylim()[1] coord1 = transFigure.transform(ax[0].transData.transform([v_idx, y_max])) coord2 = transFigure.transform(ax[5].transData.transform([v_idx, -3])) if i == 0 or i == 3: lw = 1 zorder = 0 else: lw = 1 zorder = 1 line = mlines.Line2D((coord1[0], coord2[0]), (coord1[1]+0.06, coord2[1]), transform=fig.transFigure, lw=lw, zorder=zorder, c='w', linestyle='dashed') line.set_dashes(dashes) fig.lines.append(line) line = ax[5].axvline(x=v_idx, lw=lw, c='#333333', linestyle='dashed') line.set_dashes(dashes) # Labels between bars label_indices = (v_indices[:3] + v_indices[1:])/2 labels = ['Outbound', 'Return', 'Search'] for i, label_idx in enumerate(label_indices): y_max = ax[0].get_ylim()[1] ax[0].text(label_idx, y_max*1.2, labels[i], fontsize=label_font_size, va='center', ha='center') return fig, ax
def show(graph, color=None, fig_size=(10, 10), node_size=10, edge_width=2, mode='spring', notshow=False, **kwargs): """ Network visualization of TDA mapper Using matplotlib as basic engine, it is easily add title or others elements. :param tmap.tda.Graph.Graph graph: :param Color/str color: Passing ``tmap.tda.plot.Color`` or just simply color string. :param tuple fig_size: height and width :param int node_size: With given node_size, it will scale all nodes with same size ``node_size/max(node_sizes) * node_size **2``. The size of nodes also depends on the biggest node which contains maxium number of samples. :param int edge_width: Line width of edges. :param str/None mode: Currently, Spring layout is the only one style supported. :param float strength: Optimal distance between nodes. If None the distance is set to ``1/sqrt(n)`` where n is the number of nodes. Increase this value to move nodes farther apart. :return: plt.figure """ # todo: add file path for graph saving node_keys = graph.nodes node_positions = graph.nodePos node_sizes = list(graph.size.values()) # scale node sizes max_node_size = np.max(node_sizes) sizes = (node_sizes / max_node_size) * (node_size**2) # map node colors or init node color if not provided if color is None or type(color) == str: color = 'red' if color is None else color color_map = {node_id: color for node_id in node_keys} target2colors = (np.zeros( (len(node_keys), 1)), [color] * len(node_keys)) else: color_map, target2colors = color.get_colors(node_keys) colorlist = [color_map[it] for it in node_keys] node_target_values, node_colors = target2colors legend_lookup = dict( zip( node_target_values.ravel(), # flattern the node_target_values node_colors)) # if there are indicated color with ``Color``, it need to add some legend for given color. # Legend part if isinstance(color, Color): if color.dtype == "categorical": fig = plt.figure(figsize=fig_size) ax = fig.add_subplot(1, 1, 1) all_cats = legend_lookup.keys() label_color = [ legend_lookup.get(e_cat, None) for e_cat in all_cats ] get_label_color_dict = dict(zip(all_cats, label_color)) # add categorical legend for label in sorted(set(all_cats)): if label_color is not None: ax.plot([], [], 'o', color=get_label_color_dict[label], label=label, markersize=10) legend = ax.legend(numpoints=1, loc="upper right") legend.get_frame().set_facecolor('#bebebe') # add numerical colorbar elif color.dtype == "numerical": fig = plt.figure(figsize=(fig_size[0] * 10 / 9, fig_size[1])) ax = fig.add_subplot(1, 1, 1) legend_values = sorted([val for val in legend_lookup]) legned_colors = [ legend_lookup.get(val, 'blue') for val in legend_values ] # add color bar # TODO: Implement more details of color bar and make it more robust. cmap = mcolors.LinearSegmentedColormap.from_list( 'my_cmap', legned_colors) norm = mcolors.Normalize(min(legend_values), max(legend_values)) sm = cm.ScalarMappable(cmap=cmap, norm=norm) sm.set_array([]) cb = fig.colorbar(sm, shrink=0.5, pad=0.1) cb.ax.yaxis.set_ticks_position('right') if min(legend_values) != 0: # if minimum values of legend is not 0, it need to add a text to point out the minimum values. if min(legend_values) * 100 >= 0.1: # Determine whether the minimum value is too small to visualize pretty. # .2f indicates accurate to the second decimal place. # .2e indicated accurate to the second decimal after scientific notation. cb.ax.text(0.5, -0.02, '{:.2f}'.format(min(legend_values)), ha='center', va='top', weight='bold') else: cb.ax.text(0.5, -0.02, '{:.2e}'.format(min(legend_values)), ha='center', va='top', weight='bold') if max(legend_values) * 100 >= 0.1: # same as the minimum values cb.ax.text(0.5, 1.02, '{:.2f}'.format(max(legend_values)), ha='center', va='bottom', weight='bold') else: cb.ax.text(0.5, 1.02, '{:.2e}'.format(max(legend_values)), ha='center', va='bottom', weight='bold') else: fig = plt.figure(figsize=fig_size) ax = fig.add_subplot(1, 1, 1) if mode == 'spring': ori_pos = graph.data if ori_pos.shape[1] < 2: pos = nx.spring_layout(graph, **kwargs) else: ori_pos = graph.transform_sn(ori_pos[:, :2], type='s2n') ori_pos = {n: tuple(ori_pos.iloc[n, :2]) for n in graph.nodes} pos = nx.spring_layout(graph, pos=ori_pos, **kwargs) # add legend nx.draw_networkx( graph, ax=ax, pos=pos, node_size=sizes, node_color=colorlist, width=edge_width, edge_color=[color_map[edge[0]] for edge in graph.edges], with_labels=False) else: node_idx = dict(zip(node_keys, range(len(node_keys)))) for edge in graph.edges: ax.plot([ node_positions[node_idx[edge[0]], 0], node_positions[node_idx[edge[1]], 0] ], [ node_positions[node_idx[edge[0]], 1], node_positions[node_idx[edge[1]], 1] ], c=color_map[edge[0]], zorder=1) ax.scatter(node_positions[:, 0], node_positions[:, 1], c=colorlist, s=sizes, zorder=2) plt.axis("off") if notshow: return plt else: plt.show()
def visualize_distribution_evolution(env, idx, distributions, alphas, fontsize, xticks, yticks, xlabel, ylabel, bounds_overwrite, ax): jet = plt.get_cmap("hot") c_norm = colors.Normalize(vmin=0, vmax=1) scalar_map = cmx.ScalarMappable(norm=c_norm, cmap=jet) if not isinstance(alphas, list): alphas = [alphas] * len(idx) for dist, alpha, c_val in zip([distributions[i] for i in idx], alphas, np.linspace(0, 0.6, len(idx))): line = dist.init_visualization(ax, alpha=alpha, fill=True, color=scalar_map.to_rgba(c_val)) dist.update_visualization(line) spec = env.get_spec() line = spec.target_dist.init_visualization(ax, alpha=1, fill=False, color="black") spec.target_dist.update_visualization(line) line.set_linestyle("-.") if bounds_overwrite is None: lower_bounds = spec.target_dist.lower_bounds upper_bounds = spec.target_dist.upper_bounds else: lower_bounds = bounds_overwrite[0] upper_bounds = bounds_overwrite[1] ax.set_xlim([lower_bounds[0], upper_bounds[0]]) if lower_bounds.shape[0] > 1: ax.set_ylim([lower_bounds[1], upper_bounds[1]]) ax.set_xlabel(xlabel, fontsize=fontsize) ax.set_ylabel(ylabel, fontsize=fontsize) __ = [ tick.label.set_fontsize(fontsize) for tick in ax.xaxis.get_major_ticks() ] __ = [ tick.label.set_fontsize(fontsize) for tick in ax.xaxis.get_minor_ticks() ] if xticks is not None: ax.xaxis.set_ticks(xticks) ax.xaxis.set_ticklabels(xticks) __ = [ tick.label.set_fontsize(fontsize) for tick in ax.yaxis.get_major_ticks() ] __ = [ tick.label.set_fontsize(fontsize) for tick in ax.yaxis.get_minor_ticks() ] if yticks is not None: ax.yaxis.set_ticks(yticks) ax.yaxis.set_ticklabels(yticks) scalar_map.set_array([]) cbar = plt.colorbar(scalar_map, ax=ax, boundaries=np.arange(0, 0.7, .05), values=np.arange(0, 0.7, .05)[:-1], alpha=1.8 * (alphas[0] if isinstance(alphas, list) else alphas)) cbar.set_ticks(np.linspace(0, 0.6, len(idx))) cbar.set_ticklabels(idx) cbar.ax.tick_params(labelsize=fontsize) ax.xaxis.set_major_formatter(plt.ScalarFormatter()) ax.yaxis.set_major_formatter(plt.ScalarFormatter()) ax.ticklabel_format(axis='x', style='sci', scilimits=(0, 0)) ax.yaxis.get_offset_text().set_fontsize(fontsize) ax.xaxis.get_offset_text().set_fontsize(fontsize)
Y_lm = sph_harmonic(2, 0, theta, phi).real Y_sup = np.sqrt(3./8)*sph_harmonic(2,2,theta,phi) + np.sqrt(3./8)*sph_harmonic(2,-2,theta,phi) -\ 0.5*sph_harmonic(2,0,theta,phi) ################################################################################################## r = np.abs(Y_sup) # The Cartesian coordinates of the spheres x = r * np.sin(theta) * np.cos(phi) y = r * np.sin(theta) * np.sin(phi) z = r * np.cos(theta) N = r/r.max() # Color scaling for mapping on to the harmonics fig, ax = plt.subplots(subplot_kw=dict(projection='3d'), figsize=(12,10)) im = ax.plot_surface(x, y, z, rstride=1, cstride=1, facecolors=cm.jet(N)) m = cm.ScalarMappable(cmap=cm.jet) m.set_array(r) # Assign the unnormalized data array to the mappable #so that the scale corresponds to the values of R fig.colorbar(m, shrink=0.8); ax.set_axis_off() plt.show() ################################################################################################## # Calculate the spherical harmonic Y(l,m) and normalize to [0,1] fcolors = Y_sup.real fmax, fmin = fcolors.max(), fcolors.min() fcolors = (fcolors - fmin)/(fmax - fmin) x = np.sin(theta) * np.cos(phi) y = np.sin(theta) * np.sin(phi) z = np.cos(theta)
plt.title("Best VSLAM ever") ax.set_xlabel("X") ax.set_ylabel("Y") ax.set_zlabel("Z") # Make a user-defined colormap. cm1 = mcol.LinearSegmentedColormap.from_list("RedToBlue", ["r", "b"]) cm2 = mcol.LinearSegmentedColormap.from_list("GreenToYellow", ["green", "black"]) # Make a normalizer that will map the time values from # [start_time,end_time+1] -> [0,1]. cnorm = mcol.Normalize(1, numberOfFrames) # Turn these into an object that can be used to map time values to colors and # can be passed to plt.colorbar(). cpick1 = cm.ScalarMappable(norm=cnorm, cmap=cm1) cpick1.set_array([]) cpick2 = cm.ScalarMappable(norm=cnorm, cmap=cm2) cpick2.set_array([]) cameraMatrix = np.array([[1, 0., 1], [0., 1, 1], [0., 0., 1.]]) cameraMatrix = np.array([[8.6297319278294219e+02, 0., 3.2410970150269952e+02], [0., 8.6289007075149686e+02, 2.3151809145852621e+02], [0., 0., 1.]]) distortionCoeffs = np.array([ 1.3876171595109568e-01, -5.0495233579131149e-01, -1.4364550355797534e-03, 3.0938437583063767e-03, 1.3034844698951493e+00 ])
import sys from random import sample from random import randint from ete2 import Tree, SeqMotifFace, TreeStyle, add_face_to_node, TextFace, faces import numpy as np import colorsys import matplotlib.colors as pcolors import matplotlib.cm as cm from matplotlib.colors import rgb2hex import matplotlib as mpl norm = mpl.colors.Normalize(vmin=70, vmax=100) cmap = cm.OrRd cmap_blue = cm.Blues m = cm.ScalarMappable(norm=norm, cmap=cmap) m2 = cm.ScalarMappable(norm=norm, cmap=cmap_blue) def parse_mlst_results(mlst_file): ''' Parse mlst result from script wrote by torsen seeman return accession2ST_type :return: ''' accession2st_type = {} with open(mlst_file, 'r') as f: for line in f: line = line.rstrip().split('\t') accession2st_type[line[0]] = line[2]
# Google Trends(トレンド)からデータ取得 pytrends = TrendReq(hl='ja-JP', tz=-540) kw_list = [keyword] pytrends.build_payload(kw_list, cat=0, timeframe='today 1-m', geo='JP', gprop='') df = pytrends.interest_by_region(resolution='JP', inc_low_vol=True, inc_geo_code=True) df['geoCode'] = df['geoCode'].str.replace('JP-', '').astype(int) # データ作成 # 共通データ作成 cmap = plt.get_cmap('jet') norm = plt.Normalize(vmin=df[keyword].min(), vmax=df[keyword].max()) # カラーバー用データを作成 mappable = cm.ScalarMappable(cmap=cmap, norm=norm) mappable._A = [] # 日本地図用データを作成 fcol = lambda x: '#' + bytes(cmap(norm(x), bytes=True)[:3]).hex() # 図を描画 # 図のベースを描く plt.figure(figsize=(10, 8)) # 日本地図を描く plt.imshow(picture(df[keyword].apply(fcol))); # カラーバーを描く plt.colorbar(mappable) plt.savefig('{}.png'.format(keyword)) plt.show()
def __init__(self, scipp_obj_dict=None, axes=None, values=None, variances=None, masks=None, cmap=None, log=None, vmin=None, vmax=None, color=None, aspect=None): super().__init__(scipp_obj_dict=scipp_obj_dict, axes=axes, values=values, variances=variances, masks=masks, cmap=cmap, log=log, vmin=vmin, vmax=vmax, color=color, aspect=aspect, button_options=['X', 'Y', 'Z']) self.cube = None self.members.update({"surfaces": {}, "wireframes": {}, "fig": {}}) # Initialise Figure and VBox objects self.fig = ipv.figure(width=config.plot.width, height=config.plot.height, animation=0) self.scalar_map = dict() panels = ["values"] if self.params["variances"][self.name]["show"]: panels.append("variances") for key in panels: self.scalar_map[key] = cm.ScalarMappable( norm=self.params[key][self.name]["norm"], cmap=self.params[key][self.name]["cmap"]) self.members["surfaces"][key] = {} self.members["wireframes"][key] = {} self.permutations = {"x": ["y", "z"], "y": ["x", "z"], "z": ["x", "y"]} # Store min/max for each dimension for invisible scatter self.xminmax = dict() for dim, var in self.slider_x[self.name].items(): self.xminmax[dim] = [var.values[0], var.values[-1]] self.set_axes_range() self.wireframes = dict() self.surfaces = dict() # #==================================================================== # wframes = self.get_outlines() # meshes = self.get_meshes() # surf_args = dict.fromkeys(self.permutations) # wfrm_args = dict.fromkeys(self.permutations) # # print(wframes) # for xyz, perm in self.permutations.items(): # print(xyz, perm) # key = self.button_axis_to_dim[xyz] # wfrm_args[xyz] = np.ones_like(wframes[xyz][perm[0]]) * \ # self.slider_x[key].values[self.slider[key].value] # surf_args[xyz] = np.ones_like(meshes[xyz][perm[0]]) * \ # self.slider_x[key].values[self.slider[key].value] # for p in perm: # wfrm_args[p] = wframes[xyz][p] # surf_args[p] = meshes[xyz][p] # self.wireframes[xyz] = ipv.plot_wireframe(**wfrm_args, # color="red") # self.surfaces[xyz] = ipv.plot_surface(**surf_args, color="red") # #==================================================================== self.mouse_events = dict() self.last_changed_slider_dim = None for dim, sl in self.slider.items(): self.mouse_events[dim] = Event(source=sl, watched_events=['mouseup']) self.mouse_events[dim].on_dom_event(self.update_surface) # Call update_slice once to make the initial image self.update_axes() self.box = [ipv.gcc()] + self.vbox self.box = widgets.VBox(self.box) self.box.layout.align_items = 'center' self.members["fig"]["values"] = self.fig return
def test_simple(args): """Function to predict for a single image or folder of images """ assert args.model_name is not None or args.custom_model_name is not None, \ "You must specify the --model_name parameter; see README.md for an example" if torch.cuda.is_available() and not args.no_cuda: device = torch.device("cuda") else: device = torch.device("cpu") if args.model_name is not None: download_model_if_doesnt_exist(args.model_name) model_path = os.path.join("models", args.model_name) if args.model_name is not None \ else os.path.join("models", args.custom_model_name) print("-> Loading model from ", model_path) encoder_path = os.path.join(model_path, "encoder.pth") depth_decoder_path = os.path.join(model_path, "depth.pth") seg_decoder_path = os.path.join(model_path, "segmentation.pth") # LOADING PRETRAINED MODEL print(" Loading pretrained encoder") encoder = networks.ResnetEncoder(18, False) loaded_dict_enc = torch.load(encoder_path, map_location=device) # extract the height and width of image that this model was trained with feed_height = loaded_dict_enc['height'] feed_width = loaded_dict_enc['width'] filtered_dict_enc = {k: v for k, v in loaded_dict_enc.items() if k in encoder.state_dict()} encoder.load_state_dict(filtered_dict_enc) encoder.to(device) encoder.eval() print(" Loading pretrained decoder") depth_decoder = networks.DepthDecoder( num_ch_enc=encoder.num_ch_enc, scales=range(4)) loaded_dict = torch.load(depth_decoder_path, map_location=device) depth_decoder.load_state_dict(loaded_dict) depth_decoder.to(device) depth_decoder.eval() print(" Loading pretrained segmentation") seg_decoder = networks.SegmentationDecoder( num_ch_enc=encoder.num_ch_enc, scales=range(4)) loaded_dict = torch.load(seg_decoder_path, map_location=device) seg_decoder.load_state_dict(loaded_dict) seg_decoder.to(device) seg_decoder.eval() # FINDING INPUT IMAGES if os.path.isfile(args.image_path): # Only testing on a single image paths = [args.image_path] output_directory = os.path.dirname(args.image_path) elif os.path.isdir(args.image_path): # Searching folder for images paths = glob.glob(os.path.join(args.image_path, '*.{}'.format(args.ext))) output_directory = args.image_path else: raise Exception("Can not find args.image_path: {}".format(args.image_path)) print("-> Predicting on {:d} test images".format(len(paths))) # PREDICTING ON EACH IMAGE IN TURN with torch.no_grad(): for idx, image_path in enumerate(paths): if image_path.endswith("_disp.jpg"): # don't try to predict disparity for a disparity image! continue # Load image and preprocess input_image = pil.open(image_path).convert('RGB') original_width, original_height = input_image.size input_image = input_image.resize((feed_width, feed_height), pil.LANCZOS) input_image = transforms.ToTensor()(input_image).unsqueeze(0) # PREDICTION input_image = input_image.to(device) features = encoder(input_image) outputs = depth_decoder(features) seg_output = seg_decoder(features) # print(seg_output) disp = outputs[("disp", 0)] disp_resized = torch.nn.functional.interpolate( disp, (original_height, original_width), mode="bilinear", align_corners=False) seg_resized = torch.nn.functional.interpolate( seg_output, (original_height, original_width), mode="bilinear", align_corners=False) # print(seg_resized.shape, disp_resized.shape) # Saving numpy file output_name = os.path.splitext(os.path.basename(image_path))[0] name_dest_npy = os.path.join(output_directory, "{}_disp.npy".format(output_name)) scaled_disp, _ = disp_to_depth(disp, 0.1, 100) np.save(name_dest_npy, scaled_disp.cpu().numpy()) # Saving colormapped depth image disp_resized_np = disp_resized.squeeze().cpu().numpy() vmax = np.percentile(disp_resized_np, 95) normalizer = mpl.colors.Normalize(vmin=disp_resized_np.min(), vmax=vmax) mapper = cm.ScalarMappable(norm=normalizer, cmap='magma') colormapped_im = (mapper.to_rgba(disp_resized_np)[:, :, :3] * 255).astype(np.uint8) im = pil.fromarray(colormapped_im) name_dest_im = os.path.join(output_directory, "{}_disp.jpeg".format(output_name)) im.save(name_dest_im) # Saving segmentation result seg_img = pil.fromarray( (seg_resized.squeeze().cpu().numpy() * 255).astype(np.uint8)) name_dest_im_seg = os.path.join(output_directory, "{}_seg.jpeg".format(output_name)) seg_img.save(name_dest_im_seg) print(" Processed {:d} of {:d} images - saved prediction to {}".format( idx + 1, len(paths), name_dest_im)) print('-> Done!')
def draw_objects(objects=np.array([Bivarg()]), replot=False, alpha=0.2, figsize=(10, 10), scale=1.0, show=False, colors="b", label=False): """ scale : allow you to blow up or down the ellipses (defualt: 1.0) alpha: transparency for each ellipse (scalar or vector of length objects) """ ells = [ Ellipse(xy=O.mu, width=scale * 2.0 * np.sqrt(O.E[0, 0]), height=scale * 2.0 * np.sqrt(O.E[1, 1]), angle=O.theta) for O in objects ] jet = cm = plt.get_cmap('jet') # Decide if plot is to be on top of whatever is already plotted if not replot: fig = figure(figsize=figsize) ax = fig.add_subplot(111, aspect='equal') else: ax = gca() if isinstance(alpha, float) or isinstance(alpha, int): alphas = np.ones(len(ells)) * alpha else: if len(alpha) != len(ells): alphas = np.ones(len(ells)) * alpha[0] else: alphas = alpha if not isinstance(colors, list): colors = [colors for x in range(len(ells))] if len(colors) != len(ells): colors = [colors[0] for x in range(len(ells))] cNorm = cols.Normalize(vmin=colors[0], vmax=colors[-1]) scalarMap = cmx.ScalarMappable(norm=cNorm, cmap=jet) for i, e in enumerate(ells): ax.add_artist(e) e.set_clip_box(ax.bbox) e.set_alpha(alphas[i]) e.set_facecolor(scalarMap.to_rgba(colors[i])) if label: ax.set_xlabel("RA offset arcsec") ax.set_ylabel("DEC offset arcsec") #ax.autoscale(enable=None, axis='both', tight=True) draw() if show: show() return ells