def __init__(self, name, x_origin, y_origin, z_origin, height, x_width, y_width, color='grey', active_surfaces=None): if active_surfaces is None: self._active_surfaces = ('+x', '-x', '+y', '-y', '+z', '-z') else: for surface in active_surfaces: assert surface in ['+x', '-x', '+y', '-y', '+z', '-z'], 'not a valid surface!' self._active_surfaces = active_surfaces self._center = (x_origin, y_origin, z_origin) self._color = colors.to_rgb(color) self._name = name self._build_cube(origin=(x_origin - x_width / 2., z_origin - height / 2., y_origin - y_width / 2.), width=x_width, depth=y_width, height=height) self._intersections = None
def get_color(*args): """Convert color name of XKCD color name survey to RGBA tuple. Args: List of color names. If the list is empty, a full list of available color names is returned. Otherwise the first valid color in the list is returned as RGBA tuple. If no color is valid None is returned. """ try: from matplotlib import colors except ImportError as err: raise ImportError( "requires package matplotlib: " "https://matplotlib.org") from err if not args: clist = list(colors.get_named_colors_mapping().keys()) return sorted([cname[5:].title() \ for cname in clist if cname[:5] == 'xkcd:']) rgb = None for cname in args: try: rgb = colors.to_rgb('xkcd:%s' % cname) break except ValueError: continue return rgb
def plot_colortable(colors, title, sort_colors=True, emptycols=0): cell_width = 212 cell_height = 22 swatch_width = 48 margin = 12 topmargin = 40 # Sort colors by hue, saturation, value and name. if sort_colors is True: by_hsv = sorted((tuple(mcolors.rgb_to_hsv(mcolors.to_rgb(color))), name) for name, color in colors.items()) names = [name for hsv, name in by_hsv] else: names = list(colors) n = len(names) ncols = 4 - emptycols nrows = n // ncols + int(n % ncols > 0) width = cell_width * 4 + 2 * margin height = cell_height * nrows + margin + topmargin dpi = 72 fig, ax = plt.subplots(figsize=(width / dpi, height / dpi), dpi=dpi) fig.subplots_adjust(margin/width, margin/height, (width-margin)/width, (height-topmargin)/height) ax.set_xlim(0, cell_width * 4) ax.set_ylim(cell_height * (nrows-0.5), -cell_height/2.) ax.yaxis.set_visible(False) ax.xaxis.set_visible(False) ax.set_axis_off() ax.set_title(title, fontsize=24, loc="left", pad=10) for i, name in enumerate(names): row = i % nrows col = i // nrows y = row * cell_height swatch_start_x = cell_width * col swatch_end_x = cell_width * col + swatch_width text_pos_x = cell_width * col + swatch_width + 7 ax.text(text_pos_x, y, name, fontsize=14, horizontalalignment='left', verticalalignment='center') ax.hlines(y, swatch_start_x, swatch_end_x, color=colors[name], linewidth=18) return fig
def test_shadow(fig_test, fig_ref): xy = np.array([.2, .3]) dxy = np.array([.1, .2]) # We need to work around the nonsensical (dpi-dependent) interpretation of # offsets by the Shadow class... plt.rcParams["savefig.dpi"] = "figure" # Test image. a1 = fig_test.subplots() rect = mpatches.Rectangle(xy=xy, width=.5, height=.5) shadow = mpatches.Shadow(rect, ox=dxy[0], oy=dxy[1]) a1.add_patch(rect) a1.add_patch(shadow) # Reference image. a2 = fig_ref.subplots() rect = mpatches.Rectangle(xy=xy, width=.5, height=.5) shadow = mpatches.Rectangle( xy=xy + fig_ref.dpi / 72 * dxy, width=.5, height=.5, fc=np.asarray(mcolors.to_rgb(rect.get_facecolor())) * .3, ec=np.asarray(mcolors.to_rgb(rect.get_facecolor())) * .3, alpha=.5) a2.add_patch(shadow) a2.add_patch(rect)
def tint(col, factor=1.0): """Tint a color (make it darker), returning a new RGB array """ # this method is more complicated than it need be to # support matplotlib-1.x. # for matplotlib-2.x this would just be # h, s, v = colors.rgb_to_hsv(colors.to_rgb(c)) # v *= factor # return colors.hsv_to_rgb((h, s, v)) rgb = numpy.array(to_rgb(col), ndmin=3) hsv = colors.rgb_to_hsv(rgb) hsv[-1][-1][2] *= factor return colors.hsv_to_rgb(hsv)[-1][-1]
def plot_data_samples(ax, x, gamma, colors): """Plot data samples. :param ax: axes. :param x: (n_samples, n_features) features. :param gamma: (n_samples, n_components) responsibilities. :param colors: component colors. """ color_values = np.array([to_rgb(c) for c in colors]) n_samples, n_components = gamma.shape mixed_plot_colors = np.sum([gamma[:, k:k + 1] * color_values[k] for k in range(n_components)], axis=0) ax.scatter(x[:, 0], x[:, 1], c=mixed_plot_colors)
def test_cn(): matplotlib.rcParams['axes.prop_cycle'] = cycler('color', ['blue', 'r']) assert mcolors.to_hex("C0") == '#0000ff' assert mcolors.to_hex("C1") == '#ff0000' matplotlib.rcParams['axes.prop_cycle'] = cycler('color', ['xkcd:blue', 'r']) assert mcolors.to_hex("C0") == '#0343df' assert mcolors.to_hex("C1") == '#ff0000' matplotlib.rcParams['axes.prop_cycle'] = cycler('color', ['8e4585', 'r']) assert mcolors.to_hex("C0") == '#8e4585' # if '8e4585' gets parsed as a float before it gets detected as a hex # colour it will be interpreted as a very large number. # this mustn't happen. assert mcolors.to_rgb("C0")[0] != np.inf
def change_color_brightness(color, amount=0.5): """ Lightens the given color by multiplying (1-luminosity) by the given amount. Input can be matplotlib color string, hex string, or RGB tuple. An amount of 1 equals to no change. 0 is very bright (white) and 2 is very dark. By Ian Hincks Source: https://stackoverflow.com/questions/37765197/darken-or-lighten-a-color-in-matplotlib """ if not (0<=amount<=2): raise ValueError("The brightness change has to be between 0 and 2." " Instead it was {}".format(amount)) try: c = mc.cnames[color] except KeyError: c = color try: c = colorsys.rgb_to_hls(*mc.ColorConverter().to_rgb(c)) # matplotlib 1.5 except AttributeError: c = colorsys.rgb_to_hls(*mc.to_rgb(c)) # matplotlib > 2 return colorsys.hls_to_rgb(c[0], 1-amount * (1-c[1]), c[2])
def woesc_plot(WOE_df, compute_stats=True, sort_values=False, orig_scale=False, logbase=np.e, figsize=(5, 3)): """Make a weight of evidence plot. """ ## Re-scale values... WOE_df.loc[:, ['WOE', 'Score']] = WOE_df.loc[:, ['WOE', 'Score']] / np.log( logbase) p_var = WOE_df.index.get_level_values('var_name')[0] n_bins = WOE_df.index.get_level_values('bins')[0] fig, axs = plt.subplots(1, 1, figsize=figsize) axs.axhline(0, color='.6', lw=2) xticks = np.arange(0, len(WOE_df.WOE)) ave_bin_width = 1 woeCol = 'purple' # '#7849B8' woeAlpha = .6 if isinstance(WOE_df.bin_min[1], set): if sort_values: mask = (WOE_df.bin == 'Other') WOE_df = pd.concat([ WOE_df[~mask].sort_values('bin', na_position='first'), WOE_df[mask] ]) xlabel = "bin category" xticklabels = WOE_df.bin barwidth = .8 xticks = xticks[:-1] axs.bar(xticks, WOE_df.WOE[1:], width=barwidth, ec=mpl_colors.to_rgb(woeCol) + (woeAlpha, ), fill=False, label="WOE") axs.bar(xticks, WOE_df.Score[1:], width=barwidth, fc=(0, 0, 0, 0.2), ec='k', label="Score") else: if WOE_df.bin_min[1:].apply(float.is_integer).all(): xticklabels = WOE_df.bin_min.map( '{:.0f}'.format).tolist() + ['max'] else: first_dec_digit = np.floor( np.log10(WOE_df.bin_min[1:].abs().tolist(), where=(WOE_df.bin_min[1:] != 0))) xtickdigits = np.min( [3, np.nanmax([1, 1 - first_dec_digit.min().astype(int)])]) xticklabels = WOE_df.bin_min.apply( lambda x: "{:.{}f}".format(x, xtickdigits)).tolist() + ['max'] xlabel = "bin cutpoints" if orig_scale and len(WOE_df.bin_min) > 2: ave_bin_width = np.nanmax([ (WOE_df.bin_min[2:] - WOE_df.bin_min[2:].shift()).mean(), .5 ]) x_init = np.max( [WOE_df.bin_min[1], WOE_df.bin_min[2] - 2 * ave_bin_width]) xticks = np.array([x_init] + WOE_df.bin_min[2:].tolist() + [WOE_df.bin_min[-1] + 2 * ave_bin_width]) xticklabels[1] = 'min' axs.step(xticks, [WOE_df.WOE[1]] + WOE_df.WOE[1:].tolist(), color=woeCol, label='WOE', alpha=woeAlpha) axs.step(xticks, [WOE_df.Score[1]] + WOE_df.Score[1:].tolist(), c='k', label='Score') axs.axhline(WOE_df.WOE[0], linestyle=':', color=woeCol, alpha=woeAlpha) axs.axhline(WOE_df.Score[0], linestyle=':', color='k') axs.set_title(p_var, size=18) axs.set_ylabel("Additive Evidence (est. marginal log-odds)") axs.set_xlabel(xlabel) axs.set_xticks(xticks) axs.set_xticklabels(xticklabels[1:], rotation=45, ha='right', rotation_mode='anchor') axs.set_xlim(left=axs.get_xlim()[0] - .7 * ave_bin_width) axs.set_ylim((np.where(axs.get_ylim()[0] > -1, -1, axs.get_ylim()[0]), np.where(axs.get_ylim()[1] < 1, 1, axs.get_ylim()[1]))) if compute_stats: num = WOE_df.bin_ct.sum() n_targ = sum(WOE_df.Y1) MetrList = [ "# of Records = {:.0f}".format(num), "# of Targets = {:.0f}".format(n_targ), "# Missing = {:.0f}".format(WOE_df.iloc[0].bin_ct), "Base Rate = {:.3f}%".format(100 * (n_targ / num)), "Coefficient = {:.3f}".format(WOE_df.Coef[1]) ] ylocs = np.arange(.97, 0, -.05)[:len(MetrList)] for yloc, sm in zip(ylocs, MetrList): axs.annotate(sm, xy=(1.02, yloc), xycoords='axes fraction') plt.legend(bbox_to_anchor=(1, 0), loc='lower left') plt.show() return (WOE_df)
def read_cpt_file(cpt_file, cmap_lut=256): """Read *.cpt file into colorDict Modified from Scipy Cookbook originally written by James Boyle. Link: http://scipy-cookbook.readthedocs.io/items/Matplotlib_Loading_a_colormap_dynamically.html """ if not os.path.isfile(cpt_file): raise FileNotFoundError("file {} not found".format(cpt_file)) # read file into list of strings with open(cpt_file) as f: lines = f.readlines() # list of string --> x/r/g/b x, r, g, b = [], [], [], [] colorModel = "RGB" for line in lines: ls = re.split(' |\t|\n|/', line) # skip empty lines if not ls: continue # remove empty element ls = [i for i in ls if i] # parse header info if line[0] == "#": if ls[-1] == "HSV": colorModel = "HSV" continue else: continue # skip BFN info if ls[0] in ["B", "F", "N"]: continue # convert color name (in GMT cpt file sometimes) to rgb values if not isnumber(ls[1]): ls0 = list(ls) + [0, 0] ls0[1:4] = [i * 255. for i in to_rgb(ls[1])] ls0[4:] = ls[2:] ls = list(ls0) if not isnumber(ls[5]): ls0 = list(ls) + [0, 0] ls0[5:8] = [i * 255. for i in to_rgb(ls[5])] ls = list(ls0) # convert str to float ls = [float(i) for i in ls] # parse color vectors x.append(ls[0]) r.append(ls[1]) g.append(ls[2]) b.append(ls[3]) # save last row xtemp = ls[4] rtemp = ls[5] gtemp = ls[6] btemp = ls[7] x.append(xtemp) r.append(rtemp) g.append(gtemp) b.append(btemp) x = np.array(x, np.float32) r = np.array(r, np.float32) g = np.array(g, np.float32) b = np.array(b, np.float32) if colorModel == "HSV": # convert HSV to RGB for i in range(r.shape[0]): r[i], g[i], b[i] = colorsys.hsv_to_rgb(r[i] / 360., g[i], b[i]) elif colorModel == "RGB": r /= 255. g /= 255. b /= 255. # x/r/g/b --> colorDict red, blue, green = [], [], [] xNorm = (x - x[0]) / (x[-1] - x[0]) for i in range(len(x)): red.append((xNorm[i], r[i], r[i])) green.append((xNorm[i], g[i], g[i])) blue.append((xNorm[i], b[i], b[i])) # return colormap cmap_name = os.path.splitext(os.path.basename(cpt_file))[0] colorDict = { "red": tuple(red), "green": tuple(green), "blue": tuple(blue) } colormap = LinearSegmentedColormap(cmap_name, colorDict, N=cmap_lut) return colormap
def test_color_dict_guesser_cmap(self): colors = _color_dict_guesser("Set2", ["sys1", "sys2", "sys3"]) colors_rgb_array = _np.vstack([to_rgb(col) for key, col in colors.items()]) ref_color = _np.array(list(_cm.get_cmap("Set2")([0, 1, 2])))[:, :-1] _np.testing.assert_array_equal(colors_rgb_array, ref_color) self.assertListEqual(list(colors.keys()), ["sys1", "sys2", "sys3"])
b += 1 #ss.binom(size, 2) return a + b / ss.binom(size, 2) arg = sys.argv path = "s2.txt" # arg[2] k = 20 # int(arg[1]) data = read_dataset(path) centers1, clusters1 = kmeans(1, data) for i in range(2, k): centers2, clusters2 = kmeans(i, data) score = rand_index(len(data), centers1, centers2) print(score) for c in range(len(clusters1)): # colors = ['bo', 'go', 'co', 'mo', 'yo', 'ko', 'wo'] r = random.random() g = random.random() b = random.random() color = pltc.to_rgb((r, g, b)) d = np.array([data[i] for i in clusters1[c]]) plt.plot(d[:, 0], d[:, 1], color=color, marker='o', ls="") cen = np.array(centers1[c]) plt.plot(cen[0], cen[1], 'ro') plt.show()
r_max = np.max(beta_r) mmin = np.minimum(l_min, r_min) mmax = np.maximum(l_max, r_max) mmin = mmin - (mmax - mmin) / 15 mmax = mmax + (mmax - mmin) / 15 if mmin >= 0: mmin = -(mmax - mmin) / 15 elif mmax <= 0: mmax = (mmax - mmin) / 15 fig, ax = plt.subplots(2, 1) plt.sca(ax[0]) for k in range(k_): plt.plot(lambdagrid_ridge, b_r_plot[:, k], color=to_rgb(col[k].squeeze()), lw=1.5) plt.xlabel('$\lambda_{ridge}$') plt.ticklabel_format(style='sci', scilimits=(0, 0), axis='x') plt.ylabel('Ridge loadings') plt.grid(True) plt.xlim(np.array([lambdagrid_ridge[0], lambdagrid_ridge[-1]])) plt.ylim([mmin, mmax]) plt.sca(ax[1]) for k in range(k_): plt.plot(lambdagrid_lasso, b_l_plot[:, k], color=to_rgb(col[k].squeeze()),
def plot_svg(): global current_idx, axes_max fname = "snapshot%08d.svg" % current_idx if (os.path.isfile(fname) == False): print("File does not exist: ", fname) return xlist = deque() ylist = deque() rlist = deque() rgb_list = deque() # print('\n---- ' + fname + ':') tree = ET.parse(fname) root = tree.getroot() # print('--- root.tag ---') # print(root.tag) # print('--- root.attrib ---') # print(root.attrib) # print('--- child.tag, child.attrib ---') numChildren = 0 for child in root: # print(child.tag, child.attrib) # print("keys=",child.attrib.keys()) if use_defaults and ('width' in child.attrib.keys()): axes_max = float(child.attrib['width']) # print("--- found width --> axes_max =", axes_max) if child.text and "Current time" in child.text: svals = child.text.split() title_str = "(" + str(current_idx) + ") Current time: " + svals[ 2] + "d, " + svals[4] + "h, " + svals[7] + "m" # print("width ",child.attrib['width']) # print('attrib=',child.attrib) # if (child.attrib['id'] == 'tissue'): if ('id' in child.attrib.keys()): # print('-------- found tissue!!') tissue_parent = child break # print('------ search tissue') cells_parent = None for child in tissue_parent: # print('attrib=',child.attrib) if (child.attrib['id'] == 'cells'): # print('-------- found cells, setting cells_parent') cells_parent = child break numChildren += 1 num_cells = 0 svg_xrange = svg_xmax - svg_xmin svg_yrange = svg_ymax - svg_ymin x_range = xmax - xmin y_range = ymax - ymin # print('------ search cells') for child in cells_parent: # print(child.tag, child.attrib) # print('attrib=',child.attrib) for circle in child: # two circles in each child: outer + nucleus # circle.attrib={'cx': '1085.59','cy': '1225.24','fill': 'rgb(159,159,96)','r': '6.67717','stroke': 'rgb(159,159,96)','stroke-width': '0.5'} # print(' --- cx,cy=',circle.attrib['cx'],circle.attrib['cy']) xval = float(circle.attrib['cx']) # map into desired coord sys (same as substrate mesh) xval = (xval - svg_xmin) / svg_xrange * x_range + xmin s = circle.attrib['fill'] # print("s=",s) # print("type(s)=",type(s)) if (s[0:3] == "rgb"): # if an rgb string, e.g. "rgb(175,175,80)" rgb = list(map(int, s[4:-1].split(","))) rgb[:] = [x / 255. for x in rgb] else: # otherwise, must be a color name rgb_tuple = mplc.to_rgb(mplc.cnames[s]) # a tuple rgb = [x for x in rgb_tuple] # test for bogus x,y locations (rwh TODO: use max of domain?) too_large_val = 10000. if (math.fabs(xval) > too_large_val): print("bogus xval=", xval) break yval = float(circle.attrib['cy']) if (math.fabs(yval) > too_large_val): print("bogus xval=", xval) break # map into desired coord sys (same as substrate mesh) yval = (yval - svg_ymin) / svg_yrange * y_range + ymin rval = float(circle.attrib['r']) # if (rgb[0] > rgb[1]): # print(num_cells,rgb, rval) xlist.append(xval) ylist.append(yval) rlist.append(rval) rgb_list.append(rgb) # For .svg files with cells that *have* a nucleus, there will be a 2nd if (show_nucleus == 0): break num_cells += 1 # if num_cells > 3: # for debugging # print(fname,': num_cells= ',num_cells," --- debug exit.") # sys.exit(1) # break print(fname, ': num_cells= ', num_cells) xvals = np.array(xlist) yvals = np.array(ylist) rvals = np.array(rlist) rgbs = np.array(rgb_list) #print("xvals[0:5]=",xvals[0:5]) #print("rvals[0:5]=",rvals[0:5]) # print("rvals.min, max=",rvals.min(),rvals.max()) # plt.cla() title_str += " (" + str(num_cells) + " agents)" plt.title(title_str) # axes range labels plt.xlim(xmin, xmax) plt.ylim(ymin, ymax) plt.scatter(xvals, yvals, s=rvals * scale_radius, c=rgbs) #plt.xlim(0,2000) # TODO - get these values from width,height in .svg at top #plt.ylim(0,2000) plt.pause(time_delay)
def str2color(color_string): """ Convert color string to uint8 array representation """ return (np.array(to_rgb(color_string)) * 255).astype(np.uint8)
def _render_structure(self, change=None): """Render the structure with POVRAY.""" if not isinstance(self.structure, Atoms): return self.render_btn.disabled = True omat = np.array(self._viewer._camera_orientation).reshape( 4, 4).transpose() zfactor = norm(omat[0, 0:3]) omat[0:3, 0:3] = omat[0:3, 0:3] / zfactor bb = deepcopy(self.structure) bb.pbc = (False, False, False) for i in bb: ixyz = omat[0:3, 0:3].dot(np.array([i.x, i.y, i.z]) + omat[0:3, 3]) i.x, i.y, i.z = -ixyz[0], ixyz[1], ixyz[2] vertices = [] cell = bb.get_cell() vertices.append(np.array([0, 0, 0])) vertices.extend(cell) vertices.extend([ cell[0] + cell[1], cell[0] + cell[2], cell[1] + cell[2], cell[0] + cell[1] + cell[2], ]) for n, i in enumerate(vertices): ixyz = omat[0:3, 0:3].dot(i + omat[0:3, 3]) vertices[n] = np.array([-ixyz[0], ixyz[1], ixyz[2]]) bonds = [] cutOff = neighborlist.natural_cutoffs( bb) # Takes the cutoffs from the ASE database neighborList = neighborlist.NeighborList(cutOff, self_interaction=False, bothways=False) neighborList.update(bb) matrix = neighborList.get_connectivity_matrix() for k in matrix.keys(): i = bb[k[0]] j = bb[k[1]] v1 = np.array([i.x, i.y, i.z]) v2 = np.array([j.x, j.y, j.z]) midi = v1 + (v2 - v1) * Radius[i.symbol] / (Radius[i.symbol] + Radius[j.symbol]) bond = Cylinder( v1, midi, 0.2, Pigment("color", np.array(Colors[i.symbol])), Finish("phong", 0.8, "reflection", 0.05), ) bonds.append(bond) bond = Cylinder( v2, midi, 0.2, Pigment("color", np.array(Colors[j.symbol])), Finish("phong", 0.8, "reflection", 0.05), ) bonds.append(bond) edges = [] for x, i in enumerate(vertices): for j in vertices[x + 1:]: if (norm(np.cross(i - j, vertices[1] - vertices[0])) < 0.001 or norm(np.cross(i - j, vertices[2] - vertices[0])) < 0.001 or norm(np.cross(i - j, vertices[3] - vertices[0])) < 0.001): edge = Cylinder( i, j, 0.06, Texture( Pigment("color", [212 / 255.0, 175 / 255.0, 55 / 255.0])), Finish("phong", 0.9, "reflection", 0.01), ) edges.append(edge) camera = Camera( "perspective", "location", [0, 0, -zfactor / 1.5], "look_at", [0.0, 0.0, 0.0], ) light = LightSource([0, 0, -100.0], "color", [1.5, 1.5, 1.5]) spheres = [ Sphere( [i.x, i.y, i.z], Radius[i.symbol], Texture(Pigment("color", np.array(Colors[i.symbol]))), Finish("phong", 0.9, "reflection", 0.05), ) for i in bb ] objects = ( [light] + spheres + edges + bonds + [Background("color", np.array(to_rgb(self._viewer.background)))]) scene = Scene(camera, objects=objects) fname = bb.get_chemical_formula() + ".png" scene.render( fname, width=2560, height=1440, antialiasing=0.000, quality=11, remove_temp=False, ) with open(fname, "rb") as raw: payload = base64.b64encode(raw.read()).decode() self._download(payload=payload, filename=fname) self.render_btn.disabled = False
points_path = os.path.join("out", "mnist", "dspn", "detections", f"{index}-step{i}.txt") points = list(load_file(points_path)) progress.append(points) groundtruths_path = os.path.join("out", "mnist", "base", "groundtruths", f"{index}.txt") progress.append(list(load_file(groundtruths_path))) detections_path = os.path.join("out", "mnist", "base", "detections", f"{index}.txt") progress.append(list(load_file(detections_path))) point_color = colors.to_rgb("#34495e") for i, step in enumerate(progress): plt.subplot(len(args.n), len(progress), i + 1 + j * len(progress), aspect="equal") score, x, y = zip(*step) x, y = y, x y = 1 - np.array(y) color = np.zeros((y.size, 4)) color[:, :3] = point_color color[:, 3] = np.clip(score, 0, 1) # remove elements with too low score keep = color[:, 3] > 0.2
def add_alpha(p_cols, alphas): cols = [to_rgb(c) for c in p_cols] return [(c[0], c[1], c[2], a) for c, a in zip(cols, alphas[-len(p_cols):])]
def plot_svg(self, frame): # global current_idx, axes_max global current_frame current_frame = frame fname = "snapshot%08d.svg" % frame full_fname = os.path.join(self.output_dir, fname) # with debug_view: # print("plot_svg:", full_fname) if not os.path.isfile(full_fname): print("Once output files are generated, click the slider.") return xlist = deque() ylist = deque() rlist = deque() rgb_list = deque() # print('\n---- ' + fname + ':') # tree = ET.parse(fname) tree = ET.parse(full_fname) root = tree.getroot() # print('--- root.tag ---') # print(root.tag) # print('--- root.attrib ---') # print(root.attrib) # print('--- child.tag, child.attrib ---') numChildren = 0 for child in root: # print(child.tag, child.attrib) # print("keys=",child.attrib.keys()) if self.use_defaults and ('width' in child.attrib.keys()): self.axes_max = float(child.attrib['width']) # print("debug> found width --> axes_max =", axes_max) if child.text and "Current time" in child.text: svals = child.text.split() # title_str = "(" + str(current_idx) + ") Current time: " + svals[2] + "d, " + svals[4] + "h, " + svals[7] + "m" # title_str = "Current time: " + svals[2] + "d, " + svals[4] + "h, " + svals[7] + "m" title_str = svals[2] + "d, " + svals[4] + "h, " + svals[7] + "m" # print("width ",child.attrib['width']) # print('attrib=',child.attrib) # if (child.attrib['id'] == 'tissue'): if ('id' in child.attrib.keys()): # print('-------- found tissue!!') tissue_parent = child break # print('------ search tissue') cells_parent = None for child in tissue_parent: # print('attrib=',child.attrib) if (child.attrib['id'] == 'cells'): # print('-------- found cells, setting cells_parent') cells_parent = child break numChildren += 1 num_cells = 0 # print('------ search cells') for child in cells_parent: # print(child.tag, child.attrib) # print('attrib=',child.attrib) for circle in child: # two circles in each child: outer + nucleus # circle.attrib={'cx': '1085.59','cy': '1225.24','fill': 'rgb(159,159,96)','r': '6.67717','stroke': 'rgb(159,159,96)','stroke-width': '0.5'} # print(' --- cx,cy=',circle.attrib['cx'],circle.attrib['cy']) xval = float(circle.attrib['cx']) s = circle.attrib['fill'] # print("s=",s) # print("type(s)=",type(s)) if (s[0:3] == "rgb" ): # if an rgb string, e.g. "rgb(175,175,80)" rgb = list(map(int, s[4:-1].split(","))) rgb[:] = [x / 255. for x in rgb] else: # otherwise, must be a color name rgb_tuple = mplc.to_rgb(mplc.cnames[s]) # a tuple rgb = [x for x in rgb_tuple] # test for bogus x,y locations (rwh TODO: use max of domain?) too_large_val = 10000. if (np.fabs(xval) > too_large_val): print("bogus xval=", xval) break yval = float(circle.attrib['cy']) if (np.fabs(yval) > too_large_val): print("bogus xval=", xval) break rval = float(circle.attrib['r']) # if (rgb[0] > rgb[1]): # print(num_cells,rgb, rval) xlist.append(xval) ylist.append(yval) rlist.append(rval) rgb_list.append(rgb) # For .svg files with cells that *have* a nucleus, there will be a 2nd if (self.show_nucleus == 0): #if (not self.show_nucleus): break num_cells += 1 # if num_cells > 3: # for debugging # print(fname,': num_cells= ',num_cells," --- debug exit.") # sys.exit(1) # break # print(fname,': num_cells= ',num_cells) xvals = np.array(xlist) yvals = np.array(ylist) rvals = np.array(rlist) rgbs = np.array(rgb_list) # print("xvals[0:5]=",xvals[0:5]) # print("rvals[0:5]=",rvals[0:5]) # print("rvals.min, max=",rvals.min(),rvals.max()) # rwh - is this where I change size of render window?? (YES - yipeee!) # plt.figure(figsize=(6, 6)) # plt.cla() title_str += " (" + str(num_cells) + " agents)" # plt.title(title_str) # plt.xlim(axes_min,axes_max) # plt.ylim(axes_min,axes_max) # plt.scatter(xvals,yvals, s=rvals*scale_radius, c=rgbs) # self.fig = plt.figure(figsize=(6, 6)) self.fig = plt.figure(figsize=(7, 7)) # axx = plt.axes([0, 0.05, 0.9, 0.9]) # left, bottom, width, height # axx = fig.gca() # print('fig.dpi=',fig.dpi) # = 72 # im = ax.imshow(f.reshape(100,100), interpolation='nearest', cmap=cmap, extent=[0,20, 0,20]) # ax.xlim(axes_min,axes_max) # ax.ylim(axes_min,axes_max) # convert radii to radii in pixels # ax2 = fig.gca() ax2 = self.fig.gca() N = len(xvals) rr_pix = (ax2.transData.transform(np.vstack([rvals, rvals]).T) - ax2.transData.transform( np.vstack([np.zeros(N), np.zeros(N)]).T)) rpix, _ = rr_pix.T markers_size = (144. * rpix / self.fig.dpi)**2 # = (2*rpix / fig.dpi * 72)**2 # markers_size = (2*rpix / fig.dpi * 72)**2 markers_size = markers_size / 4000000. # print('max=',markers_size.max()) # ax.scatter(xvals,yvals, s=rvals*self.scale_radius, c=rgbs) # axx.scatter(xvals,yvals, s=markers_size, c=rgbs) #rwh - temp fix - Ah, error only occurs when "edges" is toggled on if (self.show_edge): try: plt.scatter(xvals, yvals, s=markers_size, c=rgbs, edgecolor='black', linewidth=0.5) except (ValueError): pass else: plt.scatter(xvals, yvals, s=markers_size, c=rgbs) plt.xlim(self.axes_min, self.axes_max) plt.ylim(self.axes_min, self.axes_max) # ax.grid(False) # axx.set_title(title_str) plt.title(title_str)
def main(**kwargs): """ Process equilibrium path sampling output file kwargs['i'] to obtain free energy profile. This is the main function of boltzmann_weight.py, which converts each EPS window into a stretch of the energy profile and stitches these stretches together into a continuous plot. Simply put, this function descretizes and reweights the data according to the Boltzmann weight (E = kT*ln(p), where p is the probability of a given state). Parameters ---------- kwargs : dict Dictionary object containing arguments passed in from command line in "if __name__ == '__main__'" section. Returns ------- None """ # Change format of items in kwargs from one-length lists of inputs to just the inputs for item in kwargs.keys(): if isinstance(kwargs[item], list): kwargs[item] = kwargs[item][0] # First, check that each input argument is valid if not os.path.exists(kwargs['i']): raise FileNotFoundError('could not find input file: ' + kwargs['i']) if not kwargs['t'] >= 0: raise RuntimeError('temperature argument \'t\' must be greater than or equal to zero') if not kwargs['n'] >= 2: raise RuntimeError('bins per window argument \'n\' must be greater than or equal to two') if not kwargs['c'] >= 0: raise RuntimeError('bootstrap cycles argument \'c\' must be greater than or equal to zero') if kwargs['b'] == 0: raise RuntimeError('bootstrap samples per window argument \'b\' cannot be equal to zero') if not 0 <= kwargs['e'] <= 1: raise RuntimeError('exclusion fraction argument \'e\' must be between zero and one') # Set Bolztmann factor for this temperature kT = kwargs['t'] * 0.001987 # kcal/mol-K # Load in data from input file file = open(kwargs['i'], 'r').readlines() open(kwargs['i'], 'r').close() windows = [] # nested list of format [[lower0, upper0], [lower1, upper1], ...] data = [] # nested list with indices corresponding windows, format [[x00, x01, ...], [x10, x11,...], ...] # alldata = [] # simple list [x00, x01, ... x0N, x10, x11, ...] # Determine the window boundaries for line in file: line = line.strip('\n') split = line.split() if [float('%.3f' % float(split[0])), float('%.3f' % (float(split[1])))] not in windows: windows.append([float('%.3f' % (float(split[0]))), float('%.3f' % (float(split[1])))]) data.append([]) windows.sort(key=lambda x: x[0]) # need to be sorted for building the PMF # Build the data nested list by sorting each line from the input file into the appropriate sublist of data for line in file: line = line.strip('\n') split = line.split(' ') if float('%.3f' % float(split[0])) <= float(split[2]) <= float('%.3f' % float(split[1])): data[windows.index([float('%.3f' % float(split[0])), float('%.3f' % float(split[1]))])].append(float(split[2])) # alldata.append(float(split[2])) else: raise RuntimeError('Impossible line in input file ' + kwargs['i'] + ': sampled value is not within window ' 'boundaries.\n The offending line: ' + line) # Implement exclusion of the first 'e' of the data in each window for window_index in range(len(windows)): data[window_index] = data[window_index][int(len(data[window_index]) * kwargs['e']):] std_err = [] # initialize standard error of the mean within each window subsampled_fullPMFs = [] # initialize list of PMFs from each bootstrapping cycle error_index = 0 # initialize index to keep track of index within std_err when plotting if not kwargs['noplot']: fig0 = plt.figure() # initialize first matplotlib figure ax0 = fig0.add_subplot(111) # add axes to the first figure plt.ylabel('Free Energy (kcal/mol)', weight='bold') plt.xlabel('Reaction Coordinate', weight='bold') fig1 = plt.figure() # initialize second matplotlib figure ax1 = fig1.add_subplot(111) # add axes to the second figure plt.ylabel('Relative Frequency', weight='bold') plt.xlabel('Reaction Coordinate', weight='bold') cycle_index = 0 # initialize index of cycle for progress bar # 'c' cycles, plus one for final complete run for cycle in range(kwargs['c'] + 1): # Initialize list of last and second-to-last energy and RC values for stitching together adjacent windows boundary_values = [0, 0, 0, 0] # Also initialize list of full PMF energy values and RC values fullPMF = [] fullRCs = [] # Subsample data, if appropriate if cycle < kwargs['c']: temp_data = copy.deepcopy(data) # necessary to avoid overwriting data in next line this_data = temp_data # initialize for i in range(len(windows)): if kwargs['b'] > 0: bootstrapN = kwargs['b'] else: bootstrapN = len(data[i]) this_data[i] = np.random.choice(temp_data[i], bootstrapN) else: this_data = data # full data set for final cycle # Calculate std_err from subsampled_fullPMFs std_err = [np.std(list) for list in np.transpose(subsampled_fullPMFs)] if std_err == []: # no data to bootstrap, so we'll use "zero" for every error value std_err = [0 for null in range(kwargs['n'] * len(windows))] # Main processing step; for each window, evaluate the shape of the energy profile and adjust it vertically based on # the previous window to obtain the full free energy profile for window_index in range(len(windows)): cycle_index += 1 update_progress(cycle_index / ((kwargs['c'] + 1) * len(windows)), 'Evaluating PMF') bin_edges = np.linspace(windows[window_index][0], windows[window_index][1], kwargs['n'] + 1) # list RC value of each bin RC_values = [np.mean([bin_edges[i], bin_edges[i+1]]) for i in range(len(bin_edges) - 1)] probs = [0 for null in range(kwargs['n'])] # initialize probabilities by bin # Check that we have at least two data points in this window if min(this_data[window_index]) == max(this_data[window_index]): raise RuntimeError('Found a window containing only a single sampled value. The boundaries of the offending ' 'window are: ' + str(windows[window_index]) + '. Sample more in this window or remove ' 'it from the input file.') # Build "probs" for this window for value in this_data[window_index]: reduced = (value - min(this_data[window_index]))/(max(this_data[window_index]) - min(this_data[window_index])) # reduce to between 0 and 1 local_index = int(np.floor(reduced * kwargs['n'])) # appropriate index within probs if local_index == kwargs['n']: local_index -= 1 # handle case where reduced == 1 probs[local_index] += 1 # increment probability count in the appropriate window # Scale probability counts to get fractions for i in range(len(probs)): probs[i] = probs[i]/len(this_data[window_index]) if probs[i] == 0: if not kwargs['slope_only']: if cycle == kwargs['c']: raise RuntimeError('at least one window contains a bin with zero samples. Either sample more in' ' this window or use fewer bins.\n The offending window is: ' + str(windows[window_index][0]) + ' to ' + str(windows[window_index][1])) else: raise RuntimeError('bootstrapping encountered a window containing a bin with zero samples. This' ' is most commonly caused by a window that has not been sampled across its ' 'full range of possible values. The offending window is: ' + str(windows[window_index][0]) + ' to ' + str(windows[window_index][1]) + '\n' 'If you do not encounter a similar error when running without bootstrapping,' ' then you probably set the number of bootstrapping samples per window too ' 'low; increase it or set it to -1. Otherwise, please remove all of the data ' 'from this window from the input file.') # Calculate energy corresponding to each probability in this window U = [0 for null in range(kwargs['n'])] # initialize energy values for this window local_index = 0 offset = 0 # offset is used to anchor the first value in each window to zero before adjusting the whole window in the next step if not kwargs['slope_only']: for prob in probs: if local_index == 0: offset = -1 * kT * np.log(prob) U[local_index] = -1 * kT * np.log(prob) - offset local_index += 1 else: this_result = optimize.minimize(objective_function, 0, (probs, RC_values, kT)) for rc in RC_values: if local_index == 0: offset = this_result.x[0] * rc U[local_index] = this_result.x[0] * rc - offset local_index += 1 # Adjust energy values uniformly up or down to stitch adjacent windows together smoothly if window_index == 0 or cycle < kwargs['c']: # turn off boundary value matching during bootstrapping to avoid propagating errors in this step into PMF error in final step left_boundary = 0 else: # calculate the adjustment; left_boundary1 == left_boundary2 iff the slopes between the first two points of this window and the last two points of the previous window are equal; otherwise, we compromise to get the best fit f1 = (RC_values[0] - boundary_values[3]) / (boundary_values[2] - boundary_values[3]) # fraction of distance from last two points of previous window at which the first point of this window falls (e.g., 0.5 is exactly between, 1.2 is 20% further, etc.) left_boundary1 = ((boundary_values[0] - boundary_values[1]) * f1) + boundary_values[1] # vertical shift to lower this window so that its first point intersects the line connecting the last two of the previous window f2 = (boundary_values[2] - RC_values[0]) / (RC_values[1] - RC_values[0]) # fraction of distance from last first two points of this window at which the last point of the previous window falls left_boundary2 = boundary_values[0] - ((U[1] - U[0]) * f2) + U[0] # vertical shift to lower this window so that the last point of the previous window intersects the line connecting the first two points of this window left_boundary = np.mean([left_boundary1, left_boundary2]) # average of shift amounts (the amount we'll actually shift by) U = [item + left_boundary for item in U] # apply the adjustment # Set boundary_values for next window boundary_values = [U[-1], U[-2], RC_values[-1], RC_values[-2]] # [x2, x1, r2, r1]; store data for this step to calculate left_boundary for next step fullPMF += list(U) # append the just-calculated data to the full energy profile fullRCs += list(RC_values) # append list of RC values corresponding to fullPMF energy values if cycle == kwargs['c'] and not kwargs['noplot']: # final cycle, so put together the first plot but don't show it yet ax0.errorbar(list(RC_values), list(U), std_err[error_index:error_index + len(U)]) fig0.canvas.draw() nextcolor = list(colors.to_rgb(next(ax1._get_patches_for_fill.prop_cycler).get('color'))) + [0.75] ax1.bar(np.linspace(windows[window_index][0], windows[window_index][1], len(probs)), probs, width=(RC_values[1] - RC_values[0]), color=nextcolor) fig1.canvas.draw() error_index += len(U) if cycle < kwargs['c']: subsampled_fullPMFs.append(fullPMF) # this is the last line of this bootstrapping cycle elif not kwargs['noplot']: plt.show() # For smoothing the PMF into a single continuous line smoothPMF = [] smoothRC = [] smoothErr = [] i = 0 while i < len(fullPMF): if (i + 1) % kwargs['n'] == 0 and i + 1 < len(fullPMF): i += 1 # skip next point else: smoothPMF.append(fullPMF[i]) smoothRC.append(fullRCs[i]) smoothErr.append(std_err[i]) i += 1 with open(kwargs['o'], 'w') as f: for i in range(len(smoothRC)): f.write(str(smoothRC[i]) + ' ' + str(smoothPMF[i]) + ' ' + str(smoothErr[i]) + '\n') if not kwargs['noplot']: fig = plt.figure() ax1 = fig.add_subplot(111) ax1.plot(smoothRC,smoothPMF,color='#0072BD',lw=2) plt.fill_between(np.asarray(smoothRC), np.asarray(smoothPMF) - np.asarray(smoothErr), np.asarray(smoothPMF) + np.asarray(smoothErr), alpha=0.5, facecolor='#0072BD') plt.ylabel('Free Energy (kcal/mol)', weight='bold') plt.xlabel('Reaction Coordinate', weight='bold') fig.canvas.draw() plt.show()
def to_rgb(x): alpha = 0.8 return [ alpha * int(y * 255) + int((1 - alpha) * 255) for y in colors.to_rgb(x) ]
def plot_sky(pts, contour=True, filled=False, ax=None, trueloc=None, cmap='Reds', col='red'): cls = kde.Clustered2DSkyKDE skypost = cls(pts, trials=5, jobs=8) # make up some data on a regular lat/lon grid. nlats = 145 nlons = 291 delta = 2. * np.pi / (nlons - 1) lats = (0.5 * np.pi - delta * np.indices((nlats, nlons))[0, :, :]) lons = (delta * np.indices((nlats, nlons))[1, :, :]) locs = np.column_stack((lons.flatten(), lats.flatten())) prob = skypost(locs).reshape(nlats, nlons) p1 = greedy(prob) # compute mean location of samples nx = np.cos(pts[:, 1]) * np.cos(pts[:, 0]) ny = np.cos(pts[:, 1]) * np.sin(pts[:, 0]) nz = np.sin(pts[:, 1]) mean_n = [np.mean(nx), np.mean(ny), np.mean(nz)] bestloc = [ np.remainder(np.arctan2(mean_n[1], mean_n[0]), 2.0 * np.pi), np.arctan2(mean_n[2], np.sqrt(mean_n[0]**2 + mean_n[1]**2)) ] if ax is None: map = Basemap(projection='ortho', lon_0=-bestloc[0] * 180 / np.pi, lat_0=bestloc[1] * 180 / np.pi, resolution=None, celestial=True) map.drawmapboundary(fill_color='white') # draw lat/lon grid lines every 30 degrees. map.drawmeridians(np.arange(0, 360, 30)) map.drawparallels(np.arange(-90, 90, 30)) else: map = ax # compute native map projection coordinates of lat/lon grid. x, y = map(lons * 180. / np.pi, lats * 180. / np.pi) # contour data over the map. if filled: base_color = np.array(to_rgb(col)) opp_color = 1.0 - base_color cs1 = map.contourf(x, y, 1.0 - p1, levels=[0.0, 0.1, 0.5, 1.0], colors=[ base_color + opp_color, base_color + 0.8 * opp_color, base_color + 0.6 * opp_color, base_color ]) cs2 = map.contour(x, y, p1, levels=[0.5, 0.9], linewidths=2.0, colors=col) if trueloc is not None: xx, yy = map(trueloc[0] * 180. / np.pi, trueloc[1] * 180. / np.pi) map.plot(xx, yy, marker='+', markersize=20, linewidth=5, color='orange') return map
def plot_mean_timecourses(results, sex=None): ''' Create a figure with time courses. ''' fig = plt.figure(figsize=(16, 9)) subplots = {} nrows = 5 ncols = 4 cols = ['', 'sens', 'uv', 'green', 'nostim', 'uv_erg', 'green_erg'] rows = ['colnames', 'Rh1', 'Rh3', 'Rh4', 'Rh5', 'Rh6'] maxval = 12.5 animator = Animator(fig) for row in rows[::-1]: for col in cols: irow = rows.index(row) icol = cols.index(col) ax = fig.add_subplot(len(rows), len(cols), 1 + len(cols) * irow + icol) if row == rows[-1] and col == 'sens': ax.set_xlabel('Wavelength (nm)') ax.get_yaxis().set_visible(False) ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) ax.spines['left'].set_visible(False) else: ax.set_axis_off() if col == 'sens' or row == 'colnames': ax.set_xlim(300, 650) ax.set_ylim(0, 1.05) subplots[row + col] = ax if icol == 1 and row != 'colnames': color = RESCUE_COLORS.get(row, 'black') ax.text(0, 0.5, row, va='center', ha='right', color=color, transform=ax.transAxes) if irow == 0: color = STIMULUS_COLORS.get(col, 'black') ax.text(1 - 0.618, 0.618, STIMULUS_FANCY_NAMES.get(col, col), va='bottom', ha='center', color=color, transform=ax.transAxes) # Set titles ERG and DPP subplots['colnames' + 'green'].text( 0.5, 1.3, 'DPP microsaccades', ha='center', va='bottom', fontsize='large', backgroundcolor=EXPERIMENT_COLORS['dpp'], transform=subplots['colnames' + 'green'].transAxes) subplots['colnames' + 'green_erg'].text( 0, 1.3, 'ERGs', ha='center', va='bottom', fontsize='large', backgroundcolor=EXPERIMENT_COLORS['ergs'], transform=subplots['colnames' + 'green_erg'].transAxes) # Title lines #box1 = subplots['colnames'+'uv'].get_position() #box2 = subplots['colnames'+'nostim'].get_position() #arrow = patches.FancyArrow(box1.x0, box1.y1+box1.height/10, box2.x1-box1.x0, 0, # shape='right', hatch='|', transform=fig.transFigure, figure=fig) #fig.patches.extend([arrow]) # Plot norpA rescue illustrative images imagedir = '/home/joni/Pictures/NorpA rescues/' for fn in os.listdir(imagedir): for row in rows: if row in fn: image = mati.imread(os.path.join(imagedir, fn)) h, w, d = image.shape im = subplots[row + ''].imshow(image) im.set_clip_path( patches.Circle((int(w / 2), int(h / 2)), int(min(w, h) / 2), transform=subplots[row].transData)) continue led_wavelengths, led_spectrums = _load_led_spectrums(LED_SPECTRUM_FNS) # Plot spectral sensitivities wave_axis, sensitivities = _load_spectral_sensitivities() for rescue, sensitivity in zip([row for row in rows if row in RESCUES], sensitivities): subplots[rescue + 'sens'].plot(wave_axis, sensitivity, color=RESCUE_COLORS[rescue]) # Plot LED spectrums in each figure subplots[rescue + 'sens'].plot(led_wavelengths[0], led_spectrums[0], '--', color='green', lw=1) subplots[rescue + 'sens'].plot(led_wavelengths[0], led_spectrums[1], '--', color='purple', lw=1) # Plot stimulus/LED spectral curves subplots['colnames' + 'green'].plot(led_wavelengths[0], led_spectrums[0], color='green') subplots['colnames' + 'uv'].plot(led_wavelengths[0], led_spectrums[1], color='purple') subplots['colnames' + 'nostim'].plot( [led_wavelengths[0][0], led_wavelengths[0][-1]], [0, 0], color='black') subplots['colnames' + 'green_erg'].plot(led_wavelengths[0], led_spectrums[2], color='green') subplots['colnames' + 'uv_erg'].plot(led_wavelengths[0], led_spectrums[3], color='purple') # Plot ERGs erg_data = _load_ergs() ergs_min = np.inf ergs_max = -np.inf for rescue in results.keys(): for stimtype in results[rescue].keys(): erg_traces = [] # For this rescue/stimtype combination, go through every erg for specimen_name in erg_data.keys(): if specimen_name in BLOCKDICT.keys(): print('Blocked {}'.format(specimen_name)) continue # Looking for correct specimen if rescue in specimen_name: # and for for ergs in erg_data[specimen_name]: # correct stimulus type if stimtype in ergs and '25' in ergs: erg = ergs[0][0] erg = erg - erg[0] color = np.array( mplcolors.to_rgb(RESCUE_COLORS[rescue])) color = np.mean([color, (1, 1, 1)], axis=0) subplots[rescue + stimtype + '_erg'].plot( erg, color=color, lw=1) erg_traces.append(erg) ergs_min = min(ergs_min, np.min(erg)) ergs_max = max(ergs_max, np.max(erg)) if erg_traces: color = np.array(mplcolors.to_rgb( RESCUE_COLORS[rescue])) * 0.75 subplots[rescue + stimtype + '_erg'].plot(np.mean(erg_traces, axis=0), color=color) # Set ERG axis limits for rescue in results.keys(): for stimtype in results[rescue].keys(): if rescue + stimtype + '_erg' in subplots.keys(): subplots[rescue + stimtype + '_erg'].set_ylim( ergs_min, ergs_max) # Set DPP and ERG plots background color box1 = subplots['Rh6' + 'uv_erg'].get_position() box2 = subplots['Rh1' + 'green_erg'].get_position() rect = patches.Rectangle((box1.x0, box1.y0), (box2.x0 - box1.x0) + box2.width, (box2.y0 - box1.y0) + box2.height, color=EXPERIMENT_COLORS['ergs'], zorder=-1) fig.add_artist(rect) box1 = subplots['Rh6' + 'uv'].get_position() box2 = subplots['Rh1' + 'nostim'].get_position() rect = patches.Rectangle((box1.x0, box1.y0), (box2.x0 - box1.x0) + box2.width, (box2.y0 - box1.y0) + box2.height, color=EXPERIMENT_COLORS['dpp'], zorder=-1) fig.add_artist(rect) # Add scale bars add_scalebar(subplots['Rh6' + 'nostim'], 50, 5, position=(1, 5), xunits='ms', yunits='µm') add_scalebar(subplots['Rh6' + 'green_erg'], 300, -3, position=(100, -3), xunits='ms', yunits='mV') # Plot DPP data for rescue in results.keys(): for stimtype in results[rescue].keys(): ax = subplots[rescue + stimtype] traces = results[rescue][stimtype]['traces'] # How many fps images were taken by camera image_interval = results[rescue][stimtype]['image_interval'] color = None for i in range(len(traces)): if sex is not None: print(results[rescue][stimtype]['sexes'][i]) if sex == results[rescue][stimtype]['sexes'][i]: color = 'red' else: color = 'gray' x, y = interpolate( np.arange(0, len(traces[i]) * image_interval[i] * 1000, image_interval[i] * 1000), traces[i] * PXLSIZE, len(traces[i])) color = np.array(mplcolors.to_rgb(RESCUE_COLORS[rescue])) color = np.mean([color, (1, 1, 1)], axis=0) line = ax.plot(x, y, label=results[rescue][stimtype]['ages'][i], lw=1, color=color) animator.add_animation(line[0], [x, y], hide=False) color = np.array(mplcolors.to_rgb(RESCUE_COLORS[rescue])) * 0.75 ax.plot(x, np.mean(traces, axis=0) * PXLSIZE, label='mean', lw=2, color=color) #ax.set_title(rescue+'_'+stimtype) ax.set_ylim(0, maxval) animator.frames += len(x) os.makedirs(THIS_SAVEDIR, exist_ok=True) animation = animator.get_animation(interval=40) #animation.save(os.path.join(THIS_SAVEDIR, 'timecourses.mp4'), dpi=600) plt.subplots_adjust(wspace=0.1, hspace=0.1)
def map_minst(net, data, features, colors): cm = class_map(net, data, features) return [[clr.to_rgb(colors[v]) for v in r] for r in cm]
def _get_synced_params(key, value): """ Return dictionaries for updating the `rcParamsShort`, `rcParamsLong`, and `rcParams` properties associated with this key. """ kw = {} # builtin properties that global setting applies to kw_long = {} # custom properties that global setting applies to kw_short = {} # short name properties # Skip full name keys key = _sanitize_key(key) if '.' in key: pass # Backend elif key == 'inlinefmt': inline_backend_fmt(value) # Cycler elif key in ('cycle', 'rgbcycle'): if key == 'rgbcycle': cycle, rgbcycle = rcParamsShort['cycle'], value else: cycle, rgbcycle = value, rcParamsShort['rgbcycle'] try: colors = mcm.cmap_d[cycle].colors except (KeyError, AttributeError): cycles = sorted(name for name, cmap in mcm.cmap_d.items() if isinstance(cmap, mcolors.ListedColormap)) raise ValueError(f'Invalid cycle name {cycle!r}. Options are: ' + ', '.join(map(repr, cycles)) + '.') if rgbcycle and cycle.lower() == 'colorblind': regcolors = colors + [(0.1, 0.1, 0.1)] elif mcolors.to_rgb('r') != (1.0, 0.0, 0.0): # reset regcolors = [(0.0, 0.0, 1.0), (1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.75, 0.75, 0.0), (0.75, 0.75, 0.0), (0.0, 0.75, 0.75), (0.0, 0.0, 0.0)] else: regcolors = [] # no reset necessary for code, color in zip('brgmyck', regcolors): rgb = mcolors.to_rgb(color) mcolors.colorConverter.colors[code] = rgb mcolors.colorConverter.cache[code] = rgb kw['patch.facecolor'] = colors[0] kw['axes.prop_cycle'] = cycler.cycler('color', colors) # Zero linewidth almost always means zero tick length elif key == 'linewidth' and _to_points(key, value) == 0: _, ikw_long, ikw = _get_synced_params('ticklen', 0) kw.update(ikw) kw_long.update(ikw_long) # Tick length/major-minor tick length ratio elif key in ('ticklen', 'ticklenratio'): if key == 'ticklen': ticklen = _to_points(key, value) ratio = rcParamsShort['ticklenratio'] else: ticklen = rcParamsShort['ticklen'] ratio = value kw['xtick.minor.size'] = ticklen * ratio kw['ytick.minor.size'] = ticklen * ratio # Spine width/major-minor tick width ratio elif key in ('linewidth', 'tickratio'): if key == 'linewidth': tickwidth = _to_points(key, value) ratio = rcParamsShort['tickratio'] else: tickwidth = rcParamsShort['linewidth'] ratio = value kw['xtick.minor.width'] = tickwidth * ratio kw['ytick.minor.width'] = tickwidth * ratio # Gridline width elif key in ('grid.linewidth', 'gridratio'): if key == 'grid.linewidth': gridwidth = _to_points(key, value) ratio = rcParamsShort['gridratio'] else: gridwidth = rcParams['grid.linewidth'] ratio = value kw_long['gridminor.linewidth'] = gridwidth * ratio # Gridline toggling, complicated because of the clunky way this is # implemented in matplotlib. There should be a gridminor setting! elif key in ('grid', 'gridminor'): ovalue = rcParams['axes.grid'] owhich = rcParams['axes.grid.which'] # Instruction is to turn off gridlines if not value: # Gridlines are already off, or they are on for the particular # ones that we want to turn off. Instruct to turn both off. if not ovalue or (key == 'grid' and owhich == 'major') or ( key == 'gridminor' and owhich == 'minor'): which = 'both' # disable both sides # Gridlines are currently on for major and minor ticks, so we # instruct to turn on gridlines for the one we *don't* want off elif owhich == 'both': # and ovalue is True, as we already tested # if gridminor=False, enable major, and vice versa value = True which = 'major' if key == 'gridminor' else 'minor' # Gridlines are on for the ones that we *didn't* instruct to turn # off, and off for the ones we do want to turn off. This just # re-asserts the ones that are already on. else: value = True which = owhich # Instruction is to turn on gridlines else: # Gridlines are already both on, or they are off only for the ones # that we want to turn on. Turn on gridlines for both. if owhich == 'both' or (key == 'grid' and owhich == 'minor') or ( key == 'gridminor' and owhich == 'major'): which = 'both' # Gridlines are off for both, or off for the ones that we # don't want to turn on. We can just turn on these ones. else: which = owhich kw['axes.grid'] = value kw['axes.grid.which'] = which # Update setting in dictionary, detect invalid keys value = _to_points(key, value) if key in rcParamsShort: kw_short[key] = value elif key in rcParamsLong: kw_long[key] = value elif key in rcParams: kw[key] = value else: raise KeyError(f'Invalid key {key!r}.') # Update linked settings for name in _rc_children.get(key, ()): if name in rcParamsLong: kw_long[name] = value else: kw[name] = value return kw_short, kw_long, kw
def scatter(x, y, color, alpha, label): r, g, b = to_rgb(color) color = [(r, g, b, a) for a in alpha] ax.scatter(x, y, marker="D", s=10, label=label, color=color)
def discrete_colormap(num_colors, alpha=255, prioritize_default=True, seed=None, min_val=0, max_val=255, min_any=0, symmetric_colors=False, dup_offset=0, jitter=0, mode=DiscreteModes.RANDOMN): """Make a discrete colormap using :attr:``config.colors`` as the starting colors and filling in the rest with randomly generated RGB values. Args: num_colors (int): Number of discrete colors to generate. alpha (int): Transparency level, from 0-255; defaults to 255. prioritize_default (bool, str): If True, the default colors from :attr:``config.colors`` will replace the initial colormap elements; defaults to True. Alternatively, `cn` can be given to use the "CN" color spec instead. seed (int): Random number seed; defaults to None, in which case no seed will be set. min_val (int, float): Minimum value for random numbers; defaults to 0. max_val (int, float): Maximum value for random numbers; defaults to 255. For floating point ranges such as 0.0-1.0, set as a float. min_any (int, float): Minimum value above which at least one value must be in each set of RGB values; defaults to 0. If all values in an RGB set are below this value, the lowest RGB value will be scaled up by the ratio ``max_val:min_any``. Assumes a range of ``min_val < min_any < max_val``; defaults to 0 to ignore. symmetric_colors (bool): True to create a symmetric set of colors, assuming the first half of ``num_colors`` mirror those of the second half; defaults to False. dup_offset (int): Amount by which to offset duplicate color values if ``dup_for_neg`` is enabled; defaults to 0. jitter (int): In :obj:`DiscreteModes.GRID` mode, coordinates are randomly shifted by half this value above or below their original value; defaults to 0. mode (:obj:`DiscreteModes`): Mode given as an enumeration; defaults to :obj:`DiscreteModes.RANDOMN` mode. Returns: :obj:`np.ndaarry`: 2D Numpy array in the format ``[[R, G, B, alpha], ...]`` on a scale of 0-255. This colormap will need to be converted into a Matplotlib colormap using ``LinearSegmentedColormap.from_list`` to generate a map that can be used directly in functions such as ``imshow``. """ if symmetric_colors: # make room for offset when duplicating colors max_val -= dup_offset # generate random combination of RGB values for each number of colors, # where each value ranges from min-max if mode is DiscreteModes.GRID: # discrete colors taken from an evenly spaced grid for min separation # between color values jitters = None if jitter > 0: if seed is not None: np.random.seed(seed) jitters = np.multiply(np.random.random((num_colors, 3)), jitter - jitter / 2).astype(int) max_val -= np.amax(jitters) min_val -= np.amin(jitters) # TODO: weight chls or scale non-linearly for better visual distinction space = (max_val - min_val) // np.cbrt(num_colors) sl = slice(min_val, max_val, space) grid = np.mgrid[sl, sl, sl] coords = np.c_[grid[0].ravel(), grid[1].ravel(), grid[2].ravel()] if min_any > 0: # remove all coords where all vals are below threshold # TODO: account for lost coords in initial space size determination coords = coords[~np.all(np.less(coords, min_any), axis=1)] if seed is not None: np.random.seed(seed) rand = np.random.choice(len(coords), num_colors, replace=False) rand_coords = coords[rand] if jitters is not None: rand_coords = np.add(rand_coords, jitters) rand_coords_shape = list(rand_coords.shape) rand_coords_shape[-1] += 1 cmap = np.zeros(rand_coords_shape, dtype=libmag.dtype_within_range(min_val, max_val)) cmap[:, :-1] = rand_coords else: # randomly generate each color value; 4th values only for simplicity # in generating array with shape for alpha channel if seed is not None: np.random.seed(seed) cmap = (np.random.random( (num_colors, 4)) * (max_val - min_val) + min_val).astype( libmag.dtype_within_range(min_val, max_val)) if min_any > 0: # if all vals below threshold, scale up lowest value below_offset = np.all(np.less(cmap[:, :3], min_any), axis=1) axes = np.argmin(cmap[below_offset, :3], axis=1) cmap[below_offset, axes] = np.multiply(cmap[below_offset, axes], max_val / min_any) if symmetric_colors: # invert latter half onto former half, assuming that corresponding # labels are mirrored (eg -5, 3, 0, 3, 5), with background centered as 0 cmap_len = len(cmap) mid = cmap_len // 2 cmap[:mid] = cmap[:cmap_len - mid - 1:-1] + dup_offset cmap[:, -1] = alpha # set transparency if prioritize_default is not False: # prioritize default colors by replacing first colors with default ones colors_default = config.colors if prioritize_default == "cn": # "CN" color spec colors_default = np.multiply( [colors.to_rgb("C{}".format(i)) for i in range(10)], 255) end = min((num_colors, len(colors_default))) cmap[:end, :3] = colors_default[:end] return cmap
def plot_loo_pit( idata=None, y=None, y_hat=None, log_weights=None, ecdf=False, ecdf_fill=True, n_unif=100, use_hpd=False, credible_interval=0.94, figsize=None, textsize=None, color="C0", legend=True, ax=None, plot_kwargs=None, plot_unif_kwargs=None, hpd_kwargs=None, fill_kwargs=None, ): """Plot Leave-One-Out (LOO) probability integral transformation (PIT) predictive checks. Parameters ---------- idata : InferenceData InferenceData object. y : array, DataArray or str Observed data. If str, idata must be present and contain the observed data group y_hat : array, DataArray or str Posterior predictive samples for ``y``. It must have the same shape as y plus an extra dimension at the end of size n_samples (chains and draws stacked). If str or None, idata must contain the posterior predictive group. If None, y_hat is taken equal to y, thus, y must be str too. log_weights : array or DataArray Smoothed log_weights. It must have the same shape as ``y_hat`` ecdf : bool, optional Plot the difference between the LOO-PIT Empirical Cumulative Distribution Function (ECDF) and the uniform CDF instead of LOO-PIT kde. In this case, instead of overlaying uniform distributions, the beta ``credible_interval`` interval around the theoretical uniform CDF is shown. This approximation only holds for large S and ECDF values not vary close to 0 nor 1. For more information, see `Vehtari et al. (2019)`, `Appendix G <https://avehtari.github.io/rhat_ess/rhat_ess.html>`_. ecdf_fill : bool, optional Use fill_between to mark the area inside the credible interval. Otherwise, plot the border lines. n_unif : int, optional Number of datasets to simulate and overlay from the uniform distribution. use_hpd : bool, optional Use plot_hpd to fill between hpd values instead of overlaying the uniform distributions. credible_interval : float, optional Credible interval of the hpd or of the ECDF theoretical credible interval figsize : figure size tuple, optional If None, size is (8 + numvars, 8 + numvars) textsize: int, optional Text size for labels. If None it will be autoscaled based on figsize. color : str or array_like, optional Color of the LOO-PIT estimated pdf plot. If ``plot_unif_kwargs`` has no "color" key, an slightly lighter color than this argument will be used for the uniform kde lines. This will ensure that LOO-PIT kde and uniform kde have different default colors. legend : bool, optional Show the legend of the figure. ax : axes, optional Matplotlib axes plot_kwargs : dict, optional Additional keywords passed to ax.plot for LOO-PIT line (kde or ECDF) plot_unif_kwargs : dict, optional Additional keywords passed to ax.plot for overlaid uniform distributions or for beta credible interval lines if ``ecdf=True`` hpd_kwargs : dict, optional Additional keywords passed to az.plot_hpd fill_kwargs : dict, optional Additional kwargs passed to ax.fill_between Returns ------- axes : axes Matplotlib axes References ---------- * Gabry et al. (2017) see https://arxiv.org/abs/1709.01449 * https://mc-stan.org/bayesplot/reference/PPC-loo.html * Gelman et al. BDA (2014) Section 6.3 Examples -------- Plot LOO-PIT predictive checks overlaying the KDE of the LOO-PIT values to several realizations of uniform variable sampling with the same number of observations. .. plot:: :context: close-figs >>> import arviz as az >>> idata = az.load_arviz_data("centered_eight") >>> az.plot_loo_pit(idata=idata, y="obs") Fill the area containing the 94% credible interval of the difference between uniform variables empirical CDF and the real uniform CDF. A LOO-PIT ECDF clearly outside of these theoretical boundaries indicates that the observations and the posterior predictive samples do not follow the same distribution. .. plot:: :context: close-figs >>> az.plot_loo_pit(idata=idata, y="obs", ecdf=True) """ if ecdf and use_hpd: raise ValueError("use_hpd is incompatible with ecdf plot") (figsize, _, _, xt_labelsize, linewidth, _) = _scale_fig_size(figsize, textsize, 1, 1) if ax is None: _, ax = plt.subplots(1, 1, figsize=figsize, constrained_layout=True) if plot_kwargs is None: plot_kwargs = {} plot_kwargs["color"] = color plot_kwargs.setdefault("linewidth", linewidth * 1.4) plot_kwargs.setdefault("label", "LOO-PIT ECDF" if ecdf else "LOO-PIT") plot_kwargs.setdefault("zorder", 5) if plot_unif_kwargs is None: plot_unif_kwargs = {} light_color = rgb_to_hsv(to_rgb(plot_kwargs.get("color"))) light_color[1] /= 2 light_color[2] += (1 - light_color[2]) / 2 plot_unif_kwargs.setdefault("color", hsv_to_rgb(light_color)) plot_unif_kwargs.setdefault("alpha", 0.5) plot_unif_kwargs.setdefault("linewidth", 0.6 * linewidth) loo_pit = _loo_pit(idata=idata, y=y, y_hat=y_hat, log_weights=log_weights) loo_pit = loo_pit.flatten() if isinstance( loo_pit, np.ndarray) else loo_pit.values.flatten() if ecdf: loo_pit.sort() n_data_points = loo_pit.size loo_pit_ecdf = np.arange(n_data_points) / n_data_points # ideal unnormalized ECDF of uniform distribution with n_data_points points # it is used indistinctively as x or p(u<x) because for u~U(0,1) they are equal unif_ecdf = np.arange(n_data_points + 1) p975 = stats.beta.ppf(0.5 + credible_interval / 2, unif_ecdf + 1, n_data_points - unif_ecdf + 1) p025 = stats.beta.ppf(0.5 - credible_interval / 2, unif_ecdf + 1, n_data_points - unif_ecdf + 1) unif_ecdf = unif_ecdf / n_data_points plot_kwargs.setdefault( "drawstyle", "steps-mid" if n_data_points < 100 else "default") plot_unif_kwargs.setdefault( "drawstyle", "steps-mid" if n_data_points < 100 else "default") ax.plot(np.hstack((0, loo_pit, 1)), np.hstack((0, loo_pit - loo_pit_ecdf, 0)), **plot_kwargs) if ecdf_fill: if fill_kwargs is None: fill_kwargs = {} fill_kwargs.setdefault("color", hsv_to_rgb(light_color)) fill_kwargs.setdefault("alpha", 0.5) fill_kwargs.setdefault( "step", "mid" if plot_kwargs["drawstyle"] == "steps-mid" else None) fill_kwargs.setdefault( "label", "{:.3g}% credible interval".format(credible_interval)) ax.fill_between(unif_ecdf, p975 - unif_ecdf, p025 - unif_ecdf, **fill_kwargs) else: ax.plot(unif_ecdf, p975 - unif_ecdf, unif_ecdf, p025 - unif_ecdf, **plot_unif_kwargs) else: loo_pit_kde, _, _ = _fast_kde(loo_pit, xmin=0, xmax=1) unif = np.random.uniform(size=(n_unif, loo_pit.size)) x_vals = np.linspace(0, 1, len(loo_pit_kde)) if use_hpd: if hpd_kwargs is None: hpd_kwargs = {} hpd_kwargs.setdefault("color", hsv_to_rgb(light_color)) hpd_fill_kwargs = hpd_kwargs.pop("fill_kwargs", {}) hpd_fill_kwargs.setdefault("label", "Uniform HPD") hpd_kwargs["fill_kwargs"] = hpd_fill_kwargs hpd_kwargs["credible_interval"] = credible_interval unif_densities = np.empty((n_unif, len(loo_pit_kde))) for idx in range(n_unif): unif_densities[idx, :], _, _ = _fast_kde(unif[idx, :], xmin=0, xmax=1) plot_hpd(x_vals, unif_densities, **hpd_kwargs) else: for idx in range(n_unif): unif_density, _, _ = _fast_kde(unif[idx, :], xmin=0, xmax=1) ax.plot(x_vals, unif_density, **plot_unif_kwargs) ax.plot(x_vals, loo_pit_kde, **plot_kwargs) ax.tick_params(labelsize=xt_labelsize) if legend: if not (use_hpd or (ecdf and ecdf_fill)): label = "{:.3g}% credible interval".format( credible_interval) if ecdf else "Uniform" ax.plot([], label=label, **plot_unif_kwargs) ax.legend() return ax
def volume2vispy(x, **kwargs): """Convert Volume(s) to vispy visuals.""" # Must not use make_iterable here as this will turn into list of keys! if not isinstance(x, (list, np.ndarray)): x = [x] # List to fill with vispy visuals visuals = [] for i, v in enumerate(x): if not isinstance(v, core.Volume): raise TypeError(f'Expected navis.Volume, got "{type(v)}"') object_id = uuid.uuid4() if 'color' in kwargs or 'c' in kwargs: color = kwargs.get('color', kwargs.get('c', (.95, .95, .95, .1))) else: color = getattr(v, 'color', (.95, .95, .95, .1)) # Colors might be list, need to pick the correct color for this volume if isinstance(color, list): if all([isinstance(c, (tuple, list, np.ndarray)) for c in color]): color = color[i] if isinstance(color, str): color = mcl.to_rgb(color) color = np.array(color, dtype=float) # Add alpha if len(color) < 4: color = np.append(color, [.1]) if max(color) > 1: color[:3] = color[:3] / 255 s = scene.visuals.Mesh(vertices=v.vertices, faces=v.faces, color=color, shading=kwargs.get('shading', 'smooth')) # Set some aesthetic parameters s.shininess = 0 # Possible presets are "additive", "translucent", "opaque" s.set_gl_state('additive' if color[3] < 1 else 'opaque', cull_face=True, depth_test=False if color[3] < 1 else True) # Make sure volumes are always drawn after neurons s.order = kwargs.get('order', 10) # Add custom attributes s.unfreeze() s._object_type = 'volume' s._volume_name = getattr(v, 'name', None) s._object = v s._object_id = object_id s.freeze() visuals.append(s) return visuals
def _create_colors( base_color: Union[str, Tuple[float, float, float]], n: int, hue_range: Optional[Tuple[float, float]] = (-0.1, 0.1), saturation_range: Optional[Tuple[float, float]] = (-0.3, 0.3), value_range: Optional[Tuple[float, float]] = (-0.3, 0.3), convert_to_rgb: bool = True, as_hex: bool = True, ) -> List[Any]: """ Create variations of colors from base color. Parameters ---------- base_color Base color which serves as a starting point. n Number of colors to create. hue_range Minimum and maximum value to add to the base color's hue. If `None`, don't adjust the hue. saturation_range Minimum and maximum value to add to the base color's saturation. If `None`, don't adjust the saturation. value_range Minimum and maximum value to add to the base color's value. If `None`, don't adjust the value. convert_to_rgb Whether to convert colors from HSV to RGB. as_hex: Whether to return colors as hex string. Returns ------- :class:`list` List of colors, either as a hex string or an RGB array. """ if not mcolors.is_color_like(base_color): raise ValueError("Base color is not color-like.") if n <= 0: raise ValueError(f"Number of colors must be > 0, found `{n}`.") base_color = mcolors.rgb_to_hsv(mcolors.to_rgb(base_color)) if n == 1: colors = [base_color] else: n *= 2 # sometimes the colors are too similar, we take every 2nd one colors = np.repeat(base_color[..., np.newaxis], n, axis=1).T for i, r in enumerate((hue_range, saturation_range, value_range)): if r is None: continue r_low, r_high = sorted(r) c = base_color[i] colors[:, i] = np.linspace(max(c + r_low, 0), min(c + r_high, 1), n) if convert_to_rgb: colors = map(mcolors.hsv_to_rgb, colors) if as_hex: colors = map(mcolors.to_hex, colors) return list( colors)[::2] # we've created twice as much colors, select every other
def plot_loo_pit( ax, figsize, ecdf, loo_pit, loo_pit_ecdf, unif_ecdf, p975, p025, fill_kwargs, ecdf_fill, use_hdi, x_vals, hdi_kwargs, hdi_odds, n_unif, unif, plot_unif_kwargs, loo_pit_kde, legend, # pylint: disable=unused-argument y_hat, y, color, textsize, credible_interval, plot_kwargs, backend_kwargs, show, ): """Bokeh loo pit plot.""" if backend_kwargs is None: backend_kwargs = {} backend_kwargs = { **backend_kwarg_defaults(("dpi", "plot.bokeh.figure.dpi"),), **backend_kwargs, } dpi = backend_kwargs.pop("dpi") (figsize, *_, linewidth, _) = _scale_fig_size(figsize, textsize, 1, 1) plot_kwargs = {} if plot_kwargs is None else plot_kwargs plot_kwargs.setdefault("color", to_hex(color)) plot_kwargs.setdefault("linewidth", linewidth * 1.4) if isinstance(y, str): label = ("{} LOO-PIT ECDF" if ecdf else "{} LOO-PIT").format(y) elif isinstance(y, DataArray) and y.name is not None: label = ("{} LOO-PIT ECDF" if ecdf else "{} LOO-PIT").format(y.name) elif isinstance(y_hat, str): label = ("{} LOO-PIT ECDF" if ecdf else "{} LOO-PIT").format(y_hat) elif isinstance(y_hat, DataArray) and y_hat.name is not None: label = ("{} LOO-PIT ECDF" if ecdf else "{} LOO-PIT").format(y_hat.name) else: label = "LOO-PIT ECDF" if ecdf else "LOO-PIT" plot_kwargs.setdefault("legend_label", label) plot_unif_kwargs = {} if plot_unif_kwargs is None else plot_unif_kwargs light_color = rgb_to_hsv(to_rgb(plot_kwargs.get("color"))) light_color[1] /= 2 # pylint: disable=unsupported-assignment-operation light_color[2] += (1 - light_color[2]) / 2 # pylint: disable=unsupported-assignment-operation plot_unif_kwargs.setdefault("color", to_hex(hsv_to_rgb(light_color))) plot_unif_kwargs.setdefault("alpha", 0.5) plot_unif_kwargs.setdefault("linewidth", 0.6 * linewidth) if ecdf: n_data_points = loo_pit.size plot_kwargs.setdefault("drawstyle", "steps-mid" if n_data_points < 100 else "default") plot_unif_kwargs.setdefault("drawstyle", "steps-mid" if n_data_points < 100 else "default") if ecdf_fill: if fill_kwargs is None: fill_kwargs = {} fill_kwargs.setdefault("color", to_hex(hsv_to_rgb(light_color))) fill_kwargs.setdefault("alpha", 0.5) fill_kwargs.setdefault( "step", "mid" if plot_kwargs["drawstyle"] == "steps-mid" else None ) fill_kwargs.setdefault( "legend_label", "{:.3g}% credible interval".format(credible_interval) ) elif use_hdi: if hdi_kwargs is None: hdi_kwargs = {} hdi_kwargs.setdefault("color", to_hex(hsv_to_rgb(light_color))) hdi_kwargs.setdefault("alpha", 0.35) if ax is None: backend_kwargs.setdefault("width", int(figsize[0] * dpi)) backend_kwargs.setdefault("height", int(figsize[1] * dpi)) ax = bkp.figure(x_range=(0, 1), **backend_kwargs) if ecdf: if plot_kwargs.get("drawstyle") == "steps-mid": ax.step( np.hstack((0, loo_pit, 1)), np.hstack((0, loo_pit - loo_pit_ecdf, 0)), line_color=plot_kwargs.get("color", "black"), line_alpha=plot_kwargs.get("alpha", 1.0), line_width=plot_kwargs.get("linewidth", 3.0), mode="center", ) else: ax.line( np.hstack((0, loo_pit, 1)), np.hstack((0, loo_pit - loo_pit_ecdf, 0)), line_color=plot_kwargs.get("color", "black"), line_alpha=plot_kwargs.get("alpha", 1.0), line_width=plot_kwargs.get("linewidth", 3.0), ) if ecdf_fill: if fill_kwargs.get("drawstyle") == "steps-mid": # use step patch when you find out how to do that ax.patch( np.concatenate((unif_ecdf, unif_ecdf[::-1])), np.concatenate((p975 - unif_ecdf, (p025 - unif_ecdf)[::-1])), fill_color=fill_kwargs.get("color"), fill_alpha=fill_kwargs.get("alpha", 1.0), ) else: ax.patch( np.concatenate((unif_ecdf, unif_ecdf[::-1])), np.concatenate((p975 - unif_ecdf, (p025 - unif_ecdf)[::-1])), fill_color=fill_kwargs.get("color"), fill_alpha=fill_kwargs.get("alpha", 1.0), ) else: if fill_kwargs is not None and fill_kwargs.get("drawstyle") == "steps-mid": ax.step( unif_ecdf, p975 - unif_ecdf, line_color=plot_unif_kwargs.get("color", "black"), line_alpha=plot_unif_kwargs.get("alpha", 1.0), line_width=plot_kwargs.get("linewidth", 1.0), mode="center", ) ax.step( unif_ecdf, p025 - unif_ecdf, line_color=plot_unif_kwargs.get("color", "black"), line_alpha=plot_unif_kwargs.get("alpha", 1.0), line_width=plot_unif_kwargs.get("linewidth", 1.0), mode="center", ) else: ax.line( unif_ecdf, p975 - unif_ecdf, line_color=plot_unif_kwargs.get("color", "black"), line_alpha=plot_unif_kwargs.get("alpha", 1.0), line_width=plot_unif_kwargs.get("linewidth", 1.0), ) ax.line( unif_ecdf, p025 - unif_ecdf, line_color=plot_unif_kwargs.get("color", "black"), line_alpha=plot_unif_kwargs.get("alpha", 1.0), line_width=plot_unif_kwargs.get("linewidth", 1.0), ) else: if use_hdi: ax.add_layout( BoxAnnotation( bottom=hdi_odds[1], top=hdi_odds[0], fill_alpha=hdi_kwargs.pop("alpha"), fill_color=hdi_kwargs.pop("color"), **hdi_kwargs ) ) else: for idx in range(n_unif): x_s, unif_density = _kde(unif[idx, :]) ax.line( x_s, unif_density, line_color=plot_unif_kwargs.get("color", "black"), line_alpha=plot_unif_kwargs.get("alpha", 0.1), line_width=plot_unif_kwargs.get("linewidth", 1.0), ) ax.line( x_vals, loo_pit_kde, line_color=plot_kwargs.get("color", "black"), line_alpha=plot_kwargs.get("alpha", 1.0), line_width=plot_kwargs.get("linewidth", 3.0), ) show_layout(ax, show) return ax
def adjust_hsv(color, h=0., s=0., v=0.): hsv = convert_color(sRGBColor(*to_rgb(color)), HSVColor) hsv.hsv_h += h hsv.hsv_s += s hsv.hsv_v += v return convert_color(hsv, sRGBColor).get_value_tuple()
def on_draw(self): pyglet.gl.glEnable(pyglet.gl.GL_BLEND) pyglet.gl.glBlendFunc(pyglet.gl.GL_SRC_ALPHA, pyglet.gl.GL_ONE_MINUS_SRC_ALPHA) if self.running: if self.collided: if self.blink_counter >= (self.fps / 5): self.blink_counter = 0 if self.front[self.g.collisions[1]] == 'orange': self.front[self.g.collisions[1]] = 'yellow' self.front[self.g.collisions[2]] = 'yellow' self.back[self.g.collisions[1]] = 'red' self.back[self.g.collisions[2]] = 'red' else: self.front[self.g.collisions[1]] = 'orange' self.front[self.g.collisions[2]] = 'orange' self.back[self.g.collisions[1]] = 'orange' self.back[self.g.collisions[2]] = 'orange' self.blink_counter += 1 t1 = time.time() # self.show_interpolated_shape() # self.show_shape() # self.show_shape_optimized() self.show_shape_optimized_new() self.show_waypoints() for car in self.g.cars: if car.ghost: opacity = 0.4 else: opacity = 1 i = self.g.cars.index(car) if self.start: x0 = car.spawn[0] y0 = car.spawn[1] angle = car.start_direction wheelangles = [0, 0] x1 = car.spawn[0] - car.length / 2 y1 = car.spawn[1] - car.width / 2 x2 = car.spawn[0] + car.length / 2 y2 = car.spawn[1] - car.width / 2 x3 = car.spawn[0] + car.length / 2 y3 = car.spawn[1] + car.width / 2 x4 = car.spawn[0] - car.length / 2 y4 = car.spawn[1] + car.width / 2 self.coordinates[lib.carList.index(car)] = [ x1, y1, x2, y2, x3, y3, x4, y4, x0, y0, wheelangles, angle ] try: glColor4f( colors.to_rgb(car.color)[0], colors.to_rgb(car.color)[1], colors.to_rgb(car.color)[2], opacity) except ValueError: clr = self.hex_to_rgb(car.color) glColor4f(clr[0], clr[1], clr[2], opacity) angle = self.coordinates[i][-1] wheelangles = self.coordinates[i][-2] x0 = self.coordinates[i][-4] y0 = self.coordinates[i][-3] glBegin(GL_QUADS) x, y = self.rotate(self.coordinates[i][0], self.coordinates[i][1], x0, y0, angle) glVertex2f(x, y) # x1, y1 x, y = self.rotate(self.coordinates[i][2], self.coordinates[i][3], x0, y0, angle) glVertex2f(x, y) # x2, y2 x, y = self.rotate(self.coordinates[i][4], self.coordinates[i][5], x0, y0, angle) glVertex2f(x, y) # x3, y3 x, y = self.rotate(self.coordinates[i][6], self.coordinates[i][7], x0, y0, angle) glVertex2f(x, y) # x4, y4 glEnd() coords = self.coordinates[i] # Wheels if lib.holonom: radius = min(car.length, car.width) / 6 x, y = self.rotate(coords[0] + 1.5 * radius, coords[1] + 1.5 * radius, x0, y0, angle) self.circle(x, y, radius * self.pxm, opacity) x, y = self.rotate(coords[2] - 1.5 * radius, coords[3] + 1.5 * radius, x0, y0, angle) self.circle(x, y, radius * self.pxm, opacity) x, y = self.rotate(coords[4] - 1.5 * radius, coords[5] - 1.5 * radius, x0, y0, angle) self.circle(x, y, radius * self.pxm, opacity) x, y = self.rotate(coords[6] + 1.5 * radius, coords[7] - 1.5 * radius, x0, y0, angle) self.circle(x, y, radius * self.pxm, opacity) else: glColor4f(1, 1, 1, opacity) w = car.width # Right Back glBegin(GL_QUADS) x, y = self.rotate(coords[0] + w / 8, coords[1] + w / 6, x0, y0, angle) glVertex2f(x, y) x, y = self.rotate(coords[0] + w / 8, coords[1] + w / 3, x0, y0, angle) glVertex2f(x, y) x, y = self.rotate(coords[0] + 3 * w / 8, coords[1] + w / 3, x0, y0, angle) glVertex2f(x, y) x, y = self.rotate(coords[0] + 3 * w / 8, coords[1] + w / 6, x0, y0, angle) glVertex2f(x, y) glEnd() # Left Back glBegin(GL_QUADS) x, y = self.rotate(coords[6] + w / 8, coords[7] - w / 6, x0, y0, angle) glVertex2f(x, y) x, y = self.rotate(coords[6] + w / 8, coords[7] - w / 3, x0, y0, angle) glVertex2f(x, y) x, y = self.rotate(coords[6] + 3 * w / 8, coords[7] - w / 3, x0, y0, angle) glVertex2f(x, y) x, y = self.rotate(coords[6] + 3 * w / 8, coords[7] - w / 6, x0, y0, angle) glVertex2f(x, y) glEnd() # Left Front x_wheel = coords[4] - w / 4 y_wheel = coords[5] - w / 4 glBegin(GL_QUADS) x, y = self.rotate(coords[4] - w / 8, coords[5] - w / 6, x_wheel, y_wheel, wheelangles[0]) x, y = self.rotate(x / self.pxm, y / self.pxm, x0, y0, angle) glVertex2f(x, y) x, y = self.rotate(coords[4] - w / 8, coords[5] - w / 3, x_wheel, y_wheel, wheelangles[0]) x, y = self.rotate(x / self.pxm, y / self.pxm, x0, y0, angle) glVertex2f(x, y) x, y = self.rotate(coords[4] - 3 * w / 8, coords[5] - w / 3, x_wheel, y_wheel, wheelangles[0]) x, y = self.rotate(x / self.pxm, y / self.pxm, x0, y0, angle) glVertex2f(x, y) x, y = self.rotate(coords[4] - 3 * w / 8, coords[5] - w / 6, x_wheel, y_wheel, wheelangles[0]) x, y = self.rotate(x / self.pxm, y / self.pxm, x0, y0, angle) glVertex2f(x, y) glEnd() # Right Front x_wheel = coords[2] - w / 4 y_wheel = coords[3] + w / 4 glBegin(GL_QUADS) x, y = self.rotate(coords[2] - w / 8, coords[3] + w / 6, x_wheel, y_wheel, wheelangles[1]) x, y = self.rotate(x / self.pxm, y / self.pxm, x0, y0, angle) glVertex2f(x, y) x, y = self.rotate(coords[2] - w / 8, coords[3] + w / 3, x_wheel, y_wheel, wheelangles[1]) x, y = self.rotate(x / self.pxm, y / self.pxm, x0, y0, angle) glVertex2f(x, y) x, y = self.rotate(coords[2] - 3 * w / 8, coords[3] + w / 3, x_wheel, y_wheel, wheelangles[1]) x, y = self.rotate(x / self.pxm, y / self.pxm, x0, y0, angle) glVertex2f(x, y) x, y = self.rotate(coords[2] - 3 * w / 8, coords[3] + w / 6, x_wheel, y_wheel, wheelangles[1]) x, y = self.rotate(x / self.pxm, y / self.pxm, x0, y0, angle) glVertex2f(x, y) glEnd() x, y = self.rotate(coords[0] + (coords[2] - coords[0]) / 2, coords[1] + (coords[7] - coords[1]) / 2, x0, y0, angle) pyglet.text.Label(str(car.id), font_name='Times New Roman', font_size=8, x=x, y=y, anchor_x='center', anchor_y='center').draw() # Lights size = (car.width / 10) # Front glColor4f( colors.to_rgb(self.front[i])[0], colors.to_rgb(self.front[i])[1], colors.to_rgb(self.front[i])[2], opacity) glBegin(GL_QUADS) x, y = self.rotate(coords[2] - size / 2, coords[5] - 2 * size, x0, y0, angle) glVertex2f(x, y) x, y = self.rotate(coords[2] + size / 2, coords[5] - 2 * size, x0, y0, angle) glVertex2f(x, y) x, y = self.rotate(coords[2] + size / 2, coords[5] - size, x0, y0, angle) glVertex2f(x, y) x, y = self.rotate(coords[2] - size / 2, coords[5] - size, x0, y0, angle) glVertex2f(x, y) glEnd() # Lightbeams x1, y1 = self.rotate(coords[2] + size / 2, coords[5] - 2 * size, x0, y0, angle) x2, y2 = self.rotate(coords[2] + 5 * size, coords[5] - 5 * size, x0, y0, angle) x3, y3 = self.rotate(coords[2] + 5 * size, coords[5] + 2 * size, x0, y0, angle) x4, y4 = self.rotate(coords[2] + size / 2, coords[5] - size, x0, y0, angle) pyglet.graphics.draw( 4, pyglet.gl.GL_QUADS, ('v2f', (x1, y1, x2, y2, x3, y3, x4, y4)), ('c4f', (colors.to_rgb( self.front[i])[0], colors.to_rgb(self.front[i])[1], colors.to_rgb(self.front[i])[2], opacity, colors.to_rgb( self.front[i])[0], colors.to_rgb(self.front[i])[1], colors.to_rgb(self.front[i])[2], 0, colors.to_rgb( self.front[i])[0], colors.to_rgb(self.front[i])[1], colors.to_rgb(self.front[i])[2], 0, colors.to_rgb( self.front[i])[0], colors.to_rgb(self.front[i])[1], colors.to_rgb(self.front[i])[2], opacity))) glBegin(GL_QUADS) x, y = self.rotate(coords[2] - size / 2, coords[3] + 2 * size, x0, y0, angle) glVertex2f(x, y) x, y = self.rotate(coords[2] + size / 2, coords[3] + 2 * size, x0, y0, angle) glVertex2f(x, y) x, y = self.rotate(coords[2] + size / 2, coords[3] + size, x0, y0, angle) glVertex2f(x, y) x, y = self.rotate(coords[2] - size / 2, coords[3] + size, x0, y0, angle) glVertex2f(x, y) glEnd() x1, y1 = self.rotate(coords[2] + size / 2, coords[3] + 2 * size, x0, y0, angle) x2, y2 = self.rotate(coords[2] + 5 * size, coords[3] + 5 * size, x0, y0, angle) x3, y3 = self.rotate(coords[2] + 5 * size, coords[3] - 2 * size, x0, y0, angle) x4, y4 = self.rotate(coords[2] + size / 2, coords[3] + size, x0, y0, angle) pyglet.graphics.draw( 4, pyglet.gl.GL_QUADS, ('v2f', (x1, y1, x2, y2, x3, y3, x4, y4)), ('c4f', (colors.to_rgb( self.front[i])[0], colors.to_rgb(self.front[i])[1], colors.to_rgb(self.front[i])[2], opacity, colors.to_rgb( self.front[i])[0], colors.to_rgb(self.front[i])[1], colors.to_rgb(self.front[i])[2], 0, colors.to_rgb( self.front[i])[0], colors.to_rgb(self.front[i])[1], colors.to_rgb(self.front[i])[2], 0, colors.to_rgb( self.front[i])[0], colors.to_rgb(self.front[i])[1], colors.to_rgb(self.front[i])[2], opacity))) # Back glColor4f( colors.to_rgb(self.back[i])[0], colors.to_rgb(self.back[i])[1], colors.to_rgb(self.back[i])[2], opacity) glBegin(GL_QUADS) x, y = self.rotate(coords[0] + size / 2, coords[5] - 2 * size, x0, y0, angle) glVertex2f(x, y) x, y = self.rotate(coords[0] - size / 2, coords[5] - 2 * size, x0, y0, angle) glVertex2f(x, y) x, y = self.rotate(coords[0] - size / 2, coords[5] - size, x0, y0, angle) glVertex2f(x, y) x, y = self.rotate(coords[0] + size / 2, coords[5] - size, x0, y0, angle) glVertex2f(x, y) glEnd() x1, y1 = self.rotate(coords[0] - size / 2, coords[5] - 2 * size, x0, y0, angle) x2, y2 = self.rotate(coords[0] - size / 2, coords[5] - size, x0, y0, angle) x3, y3 = self.rotate(coords[0] - 5 * size, coords[5] + 2 * size, x0, y0, angle) x4, y4 = self.rotate(coords[0] - 5 * size, coords[5] - 5 * size, x0, y0, angle) pyglet.graphics.draw( 4, pyglet.gl.GL_QUADS, ('v2f', (x1, y1, x2, y2, x3, y3, x4, y4)), ('c4f', (colors.to_rgb( self.back[i])[0], colors.to_rgb(self.back[i])[1], colors.to_rgb( self.back[i])[2], opacity, colors.to_rgb( self.back[i])[0], colors.to_rgb(self.back[i])[1], colors.to_rgb( self.back[i])[2], opacity, colors.to_rgb( self.back[i])[0], colors.to_rgb(self.back[i])[1], colors.to_rgb( self.back[i])[2], 0, colors.to_rgb( self.back[i])[0], colors.to_rgb(self.back[i])[1], colors.to_rgb(self.back[i])[2], 0))) glBegin(GL_QUADS) x, y = self.rotate(coords[0] + size / 2, coords[3] + 2 * size, x0, y0, angle) glVertex2f(x, y) x, y = self.rotate(coords[0] - size / 2, coords[3] + 2 * size, x0, y0, angle) glVertex2f(x, y) x, y = self.rotate(coords[0] - size / 2, coords[3] + size, x0, y0, angle) glVertex2f(x, y) x, y = self.rotate(coords[0] + size / 2, coords[3] + size, x0, y0, angle) glVertex2f(x, y) glEnd() x1, y1 = self.rotate(coords[0] - size / 2, coords[3] + 2 * size, x0, y0, angle) x2, y2 = self.rotate(coords[0] - size / 2, coords[3] + size, x0, y0, angle) x3, y3 = self.rotate(coords[0] - 5 * size, coords[3] - 2 * size, x0, y0, angle) x4, y4 = self.rotate(coords[0] - 5 * size, coords[3] + 5 * size, x0, y0, angle) pyglet.graphics.draw( 4, pyglet.gl.GL_QUADS, ('v2f', (x1, y1, x2, y2, x3, y3, x4, y4)), ('c4f', (colors.to_rgb( self.back[i])[0], colors.to_rgb(self.back[i])[1], colors.to_rgb( self.back[i])[2], opacity, colors.to_rgb( self.back[i])[0], colors.to_rgb(self.back[i])[1], colors.to_rgb( self.back[i])[2], opacity, colors.to_rgb( self.back[i])[0], colors.to_rgb(self.back[i])[1], colors.to_rgb( self.back[i])[2], 0, colors.to_rgb( self.back[i])[0], colors.to_rgb(self.back[i])[1], colors.to_rgb(self.back[i])[2], 0))) ########################################################## # Debugging ############################################################ #if not car.ghost: # if self.timestamp > 0.02: # # Reference point for lateral controlling # #self.circle(self.debugging[int(car.id)][7].real * self.pxm, self.debugging[int(car.id)][7].imag * self.pxm, 2, 1) # # Tangent on reference point for lateral controlling # line = complex(-self.debugging[int(car.id)][5], self.debugging[int(car.id)][4]) # # Refrence point for longitudinal controlling # point = self.debugging[int(car.id)][7] # Position of car # pos = self.debugging[int(car.id)][2] # line_end = (point + 100 * line) * self.pxm # line_start = (point - 100 * line) * self.pxm # # glBegin(GL_LINES) # glVertex2f(line_start.real, line_start.imag) # glVertex2f(line_end.real, line_end.imag) # glEnd() # heading_end = (pos + 2 * complex(cos(angle), sin(angle))) * self.pxm # heading_start = pos * self.pxm # glBegin(GL_LINES) # glVertex2f(heading_start.real, heading_start.imag) # glVertex2f(heading_end.real, heading_end.imag) # glEnd() # steering_angle = self.debugging[int(car.id)][3] + angle # steering_end = (pos + 2 * complex(cos(steering_angle), sin(steering_angle))) * self.pxm # e = self.debugging[int(car.id)][8] # if e > 0: # glColor4f(1, 0, 0, 1) # else: # glColor4f(0, 1, 0, 1) #glBegin(GL_LINES) #glVertex2f(heading_start.real, heading_start.imag) #glVertex2f(steering_end.real, steering_end.imag) #glEnd() label = pyglet.text.Label(str(self.timestamp), font_name='Times New Roman', font_size=10, x=3, y=3, anchor_x='left', anchor_y='bottom') label.draw() # while animating save current frame if self.timestamp <= self.g.last_timestamp: pyglet.image.get_buffer_manager().get_color_buffer().save( 'video/' + str(self.counter) + '.png') self.counter += 1 # t2 = time.time() # self.time.append(t2-t1) # self.currentfps.append(clock.get_fps()) pyglet.text.Label("Animating", font_name='Times New Roman', font_size=10, x=self.width - 80, y=3, anchor_x='left', anchor_y='bottom').draw() else: self.clear()
def plot_pattern_diagram_markers(X, Y, option): """ Plot color markers on a pattern diagram. Plots color markers on a target diagram according their (X,Y) locations. The symbols and colors are chosen automatically with a limit of 70 symbol & color combinations. The color bar is titled using the content of option['titleColorBar'] (if non-empty string). INPUTS: x : x-coordinates of markers y : y-coordinates of markers z : z-coordinates of markers (used for color shading) option : dictionary containing option values. (Refer to GET_TARGET_DIAGRAM_OPTIONS function for more information.) option['axismax'] : maximum for the X & Y values. Used to limit maximum distance from origin to display markers option['markerlabel'] : labels for markers OUTPUTS: None Created on Nov 30, 2016 Author: Peter A. Rochford Symplectic, LLC www.thesymplectic.com [email protected] """ # Set face color transparency alpha = option['alpha'] # Set marker size markerSize = option['markersize'] if option['markerlegend'] == 'on': # Check that marker labels have been provided if option['markerlabel'] == '': raise ValueError('No marker labels provided.') # Plot markers of different color and shapes with labels # displayed in a legend # Define markers kind = ['+', 'o', 'x', 's', 'd', '^', 'v', 'p', 'h', '*'] colorm = ['b', 'r', 'g', 'c', 'm', 'y', 'k'] if len(X) > 70: _disp('You must introduce new markers to plot more than 70 cases.') _disp( 'The ' 'marker' ' character array need to be extended inside the code.') if len(X) <= len(kind): # Define markers with specified color marker = [] markercolor = [] for color in colorm: for symbol in kind: marker.append(symbol + option['markercolor']) rgba = clr.to_rgb(option['markercolor']) + (alpha,) markercolor.append(rgba) else: # Define markers and colors using predefined list marker = [] for color in colorm: for symbol in kind: marker.append(symbol + color) rgba = clr.to_rgb(color) + (alpha,) markercolor.append(rgba) # Plot markers at data points limit = option['axismax'] hp = () markerlabel = [] for i, xval in enumerate(X): if abs(X[i]) <= limit and abs(Y[i]) <= limit: h = plt.plot(X[i], Y[i], marker[i], markersize=markerSize, markerfacecolor=markercolor[i], markeredgecolor=marker[i][1], markeredgewidth=2) hp += tuple(h) markerlabel.append(option['markerlabel'][i]) # Add legend if len(markerlabel) > 0: markerlabel = tuple(markerlabel) plt.legend(hp, markerlabel, loc='upper right', fontsize='medium', numpoints=1) else: warnings.warn('No markers within axis limit ranges.') else: # Plot markers as dots of a single color with accompanying labels # and no legend # Plot markers at data points limit = option['axismax'] rgba = clr.to_rgb(option['markercolor']) + (alpha,) for i, xval in enumerate(X): if abs(X[i]) <= limit and abs(Y[i]) <= limit: # Plot marker plt.plot(X[i], Y[i], '.', markersize=markerSize, markerfacecolor=rgba, markeredgecolor=option['markercolor']) # Check if marker labels provided if len(option['markerlabel']) > 0: # Label marker xtextpos = X[i] # ToDo: convert to double? ytextpos = Y[i] plt.text(xtextpos, ytextpos, option['markerlabel'][i], color=option['markerlabelcolor'], verticalalignment='bottom', horizontalalignment='right', fontsize='medium')
def draw_binary_mask(self, binary_mask, color=None, *, edge_color=None, text=None, alpha=0.5, area_threshold=0): """ Args: binary_mask (ndarray): numpy array of shape (H, W), where H is the image height and W is the image width. Each value in the array is either a 0 or 1 value of uint8 type. color: color of the mask. Refer to `matplotlib.colors` for a full list of formats that are accepted. If None, will pick a random color. edge_color: color of the polygon edges. Refer to `matplotlib.colors` for a full list of formats that are accepted. text (str): if None, will be drawn in the object's center of mass. alpha (float): blending efficient. Smaller values lead to more transparent masks. area_threshold (float): a connected component small than this will not be shown. Returns: output (VisImage): image object with mask drawn. """ if color is None: color = random_color(rgb=True, maximum=1) color = mplc.to_rgb(color) has_valid_segment = False binary_mask = binary_mask.astype("uint8") # opencv needs uint8 mask = GenericMask(binary_mask, self.output.height, self.output.width) shape2d = (binary_mask.shape[0], binary_mask.shape[1]) if not mask.has_holes: # draw polygons for regular masks for segment in mask.polygons: area = mask_util.area( mask_util.frPyObjects([segment], shape2d[0], shape2d[1])) if area < (area_threshold or 0): continue has_valid_segment = True segment = segment.reshape(-1, 2) self.draw_polygon(segment, color=color, edge_color=edge_color, alpha=alpha) else: rgba = np.zeros(shape2d + (4, ), dtype="float32") rgba[:, :, :3] = color rgba[:, :, 3] = (mask.mask == 1).astype("float32") * alpha has_valid_segment = True self.output.ax.imshow(rgba) if text is not None and has_valid_segment: # TODO sometimes drawn on wrong objects. the heuristics here can improve. lighter_color = self._change_color_brightness( color, brightness_factor=0.7) _num_cc, cc_labels, stats, centroids = cv2.connectedComponentsWithStats( binary_mask, 8) largest_component_id = np.argmax(stats[1:, -1]) + 1 # draw text on the largest component, as well as other very large components. for cid in range(1, _num_cc): if cid == largest_component_id or stats[ cid, -1] > _LARGE_MASK_AREA_THRESH: # median is more stable than centroid # center = centroids[largest_component_id] center = np.median((cc_labels == cid).nonzero(), axis=1)[::-1] self.draw_text(text, center, color=lighter_color) return self.output
def plot_loo_pit( ax, figsize, ecdf, loo_pit, loo_pit_ecdf, unif_ecdf, p975, p025, fill_kwargs, ecdf_fill, use_hdi, x_vals, hdi_kwargs, hdi_odds, n_unif, unif, plot_unif_kwargs, loo_pit_kde, legend, y_hat, y, color, textsize, credible_interval, plot_kwargs, backend_kwargs, show, ): """Matplotlib loo pit plot.""" if backend_kwargs is None: backend_kwargs = {} backend_kwargs = { **backend_kwarg_defaults(), **backend_kwargs, } (figsize, _, _, xt_labelsize, linewidth, _) = _scale_fig_size(figsize, textsize, 1, 1) backend_kwargs.setdefault("figsize", figsize) backend_kwargs["squeeze"] = True if ax is None: _, ax = create_axes_grid(1, backend_kwargs=backend_kwargs) plot_kwargs = matplotlib_kwarg_dealiaser(plot_kwargs, "plot") plot_kwargs["color"] = to_hex(color) plot_kwargs.setdefault("linewidth", linewidth * 1.4) if isinstance(y, str): label = ("{} LOO-PIT ECDF" if ecdf else "{} LOO-PIT").format(y) elif isinstance(y, DataArray) and y.name is not None: label = ("{} LOO-PIT ECDF" if ecdf else "{} LOO-PIT").format(y.name) elif isinstance(y_hat, str): label = ("{} LOO-PIT ECDF" if ecdf else "{} LOO-PIT").format(y_hat) elif isinstance(y_hat, DataArray) and y_hat.name is not None: label = ("{} LOO-PIT ECDF" if ecdf else "{} LOO-PIT").format( y_hat.name) else: label = "LOO-PIT ECDF" if ecdf else "LOO-PIT" plot_kwargs.setdefault("label", label) plot_kwargs.setdefault("zorder", 5) plot_unif_kwargs = matplotlib_kwarg_dealiaser(plot_unif_kwargs, "plot") light_color = rgb_to_hsv(to_rgb(plot_kwargs.get("color"))) light_color[1] /= 2 # pylint: disable=unsupported-assignment-operation light_color[2] += (1 - light_color[2]) / 2 # pylint: disable=unsupported-assignment-operation plot_unif_kwargs.setdefault("color", to_hex(hsv_to_rgb(light_color))) plot_unif_kwargs.setdefault("alpha", 0.5) plot_unif_kwargs.setdefault("linewidth", 0.6 * linewidth) if ecdf: n_data_points = loo_pit.size plot_kwargs.setdefault( "drawstyle", "steps-mid" if n_data_points < 100 else "default") plot_unif_kwargs.setdefault( "drawstyle", "steps-mid" if n_data_points < 100 else "default") if ecdf_fill: if fill_kwargs is None: fill_kwargs = {} fill_kwargs.setdefault("color", to_hex(hsv_to_rgb(light_color))) fill_kwargs.setdefault("alpha", 0.5) fill_kwargs.setdefault( "step", "mid" if plot_kwargs["drawstyle"] == "steps-mid" else None) fill_kwargs.setdefault( "label", "{:.3g}% credible interval".format(credible_interval)) elif use_hdi: if hdi_kwargs is None: hdi_kwargs = {} hdi_kwargs.setdefault("color", to_hex(hsv_to_rgb(light_color))) hdi_kwargs.setdefault("alpha", 0.35) hdi_kwargs.setdefault("label", "Uniform HDI") if ecdf: ax.plot(np.hstack((0, loo_pit, 1)), np.hstack((0, loo_pit - loo_pit_ecdf, 0)), **plot_kwargs) if ecdf_fill: ax.fill_between(unif_ecdf, p975 - unif_ecdf, p025 - unif_ecdf, **fill_kwargs) else: ax.plot(unif_ecdf, p975 - unif_ecdf, unif_ecdf, p025 - unif_ecdf, **plot_unif_kwargs) else: x_ss = np.empty((n_unif, len(loo_pit_kde))) u_dens = np.empty((n_unif, len(loo_pit_kde))) if use_hdi: ax.axhspan(*hdi_odds, **hdi_kwargs) # Adds horizontal reference line ax.axhline(1, color="w", zorder=1) else: for idx in range(n_unif): x_s, unif_density = kde(unif[idx, :]) x_ss[idx] = x_s u_dens[idx] = unif_density ax.plot(x_ss.T, u_dens.T, **plot_unif_kwargs) ax.plot(x_vals, loo_pit_kde, **plot_kwargs) ax.set_xlim(0, 1) ax.set_ylim(0, None) ax.tick_params(labelsize=xt_labelsize) if legend: if not (use_hdi or (ecdf and ecdf_fill)): label = "{:.3g}% credible interval".format( credible_interval) if ecdf else "Uniform" ax.plot([], label=label, **plot_unif_kwargs) ax.legend() if backend_show(show): plt.show() return ax
def plot_rna(cg, ax=None, offset=(0, 0), text_kwargs={}, backbone_kwargs={}, basepair_kwargs={}, color=True, lighten=0, annotations={}): ''' Plot an RNA structure given a set of nucleotide coordinates .. note:: This function calls set_axis_off on the axis. You can revert this by using ax.set_axis_on() if you like to see the axis. :param cg: A forgi.threedee.model.coarse_grain.CoarseGrainRNA structure :param ax: A matplotlib plotting area :param offset: Offset the plot by these coordinates. If a simple True is passed in, then offset by the current width of the plot :param text_kwargs: keyword arguments passed to matplotlib.pyplot.annotate for plotting of the sequence :param backbone_kwargs: keyword arguments passed to matplotlib.pyplot.plot for plotting of the backbone links :param basepair_kwargs: keyword arguments passed to matplotlib.pyplot.plot for plotting of the basepair links :param lighten: Make circles lighter. A percent value where 1 makes everything white and 0 leaves the colors unchanged :param annotations: A dictionary {elem_name: string} or None. By default, the element names (e.g. "s0") are plotted next to the element. This dictionary can be used to override the default element names by costum strings. To remove individual annotations, assign an empty string to the key. To remove all annotations, set this to None. .. warning:: Annotations are not shown, if there is not enough space. Annotations not shown are logged with level INFO :return: (ax, coords) The axes and the coordinates for each nucleotide ''' log.info("Starting to plot RNA...") import RNA import matplotlib.colors as mc RNA.cvar.rna_plot_type = 1 coords = [] #colors = [] #circles = [] bp_string = cg.to_dotbracket_string() # get the type of element of each nucleotide el_string = cg.to_element_string() # i.e. eeesssshhhhsssseeee el_to_color = {'f': 'orange', 't': 'orange', 's': 'green', 'h': 'blue', 'i': 'yellow', 'm': 'red'} if ax is None: ax = plt.gca() if offset is None: offset = (0, 0) elif offset is True: offset = (ax.get_xlim()[1], ax.get_ylim()[1]) else: pass vrna_coords = RNA.get_xy_coordinates(bp_string) # TODO Add option to rotate the plot for i, _ in enumerate(bp_string): coord = (offset[0] + vrna_coords.get(i).X, offset[1] + vrna_coords.get(i).Y) coords.append(coord) coords = np.array(coords) # First plot backbone bkwargs = {"color":"black", "zorder":0} bkwargs.update(backbone_kwargs) ax.plot(coords[:,0], coords[:,1], **bkwargs) # Now plot basepairs basepairs = [] for s in cg.stem_iterator(): for p1, p2 in cg.stem_bp_iterator(s): basepairs.append([coords[p1-1], coords[p2-1]]) if basepairs: basepairs = np.array(basepairs) if color: c = "red" else: c = "black" bpkwargs = {"color":c, "zorder":0, "linewidth":3} bpkwargs.update(basepair_kwargs) ax.plot(basepairs[:,:,0].T, basepairs[:,:,1].T, **bpkwargs) # Now plot circles for i, coord in enumerate(coords): if color: c = el_to_color[el_string[i]] h,l,s = colorsys.rgb_to_hls(*mc.to_rgb(c)) if lighten>0: l += (1-l)*min(1,lighten) else: l +=l*max(-1, lighten) if l>1 or l<0: print(l) c=colorsys.hls_to_rgb(h,l,s) circle = plt.Circle((coord[0], coord[1]), color=c) else: circle = plt.Circle((coord[0], coord[1]), edgecolor="black", facecolor="white") ax.add_artist(circle) if cg.seq: if "fontweight" not in text_kwargs: text_kwargs["fontweight"]="bold" ax.annotate(cg.seq[i+1],xy=coord, ha="center", va="center", **text_kwargs ) all_coords=list(coords) ntnum_kwargs = {"color":"gray"} ntnum_kwargs.update(text_kwargs) for nt in range(10, cg.seq_length, 10): # We try different angles annot_pos = _find_annot_pos_on_circle(nt, all_coords, cg) if annot_pos is not None: ax.annotate(str(nt), xy=coords[nt-1], xytext=annot_pos, arrowprops={"width":1, "headwidth":1, "color":"gray"}, ha="center", va="center", zorder=0, **ntnum_kwargs) all_coords.append(annot_pos) _annotate_rna_plot(ax, cg, all_coords, annotations, text_kwargs) datalim = ((min(list(coords[:, 0]) + [ax.get_xlim()[0]]), min(list(coords[:, 1]) + [ax.get_ylim()[0]])), (max(list(coords[:, 0]) + [ax.get_xlim()[1]]), max(list(coords[:, 1]) + [ax.get_ylim()[1]]))) ''' min_coord = min(datalim[0][0], datalim[0][1]) max_coord = max(datalim[1][0], datalim[1][1]) datalim = ((min_coord, min_coord), (max_coord, max_coord)) print "min_coord:", min_coord print "max_coord:", max_coord print "datalime:", datalim ''' width = datalim[1][0] - datalim[0][0] height = datalim[1][1] - datalim[0][1] #ax.set_aspect(width / height) ax.set_aspect('equal', 'datalim') ax.update_datalim(datalim) ax.autoscale_view() ax.set_axis_off() return (ax, coords)
def uwoesc_plot(WOE_df, targ_label, sort_values=False, var_scale='def', top_n=None, sep_bar=False, textsize=9, figsize=(8.2, 4)): """Create a plot of a univariate scorecard using a WOE_df object. """ p_var = WOE_df.index[0][0] compute_stats = 'IV' in WOE_df.columns if top_n: WOE_df = WOE_df.iloc[:top_n + 1, :] n_bins = WOE_df.index.get_level_values('bins')[0] fig, axs = plt.subplots(1, 2, figsize=figsize) fig.tight_layout(pad=4.0) fig.text(0.5, 0.92, p_var, ha='center', size=20) xticks = np.arange(0, len(WOE_df.WOE)) ax1_xticks = xticks axs[1].axhline(0, color='.8', lw=2.5) ave_bin_width = 1 woeCol = 'purple' woeAlpha = .6 if isinstance(WOE_df.bin_min[1], set): if sort_values: mask = (WOE_df.bin == 'Other') WOE_df = pd.concat([ WOE_df[~mask].sort_values('bin', na_position='first'), WOE_df[mask] ]) xlabel = "bin category" xticklabels = WOE_df.bin.tolist() ax1_xticklabels = xticklabels[1:] xtick_offset = 0 text_offset = -.4 barwidth = .8 axs[1].bar(ax1_xticks[:-1], WOE_df.WOE[1:], width=barwidth, lw=1.5, fc=mpl_colors.to_rgb(woeCol) + (.3, ), ec=mpl_colors.to_rgb(woeCol) + (woeAlpha, )) axs[1].set_xticks(ax1_xticks[:-1]) else: if WOE_df.bin_min[1:].apply(float.is_integer).all(): xticklabels = WOE_df.bin_min.map('{:.0f}'.format).tolist() else: first_dec_digit = np.floor( np.log10(WOE_df.bin_min[1:].abs(), where=(WOE_df.bin_min[1:] != 0))) xtickdigits = np.min( [3, np.nanmax([1, 1 - first_dec_digit.min().astype(int)])]) xticklabels = WOE_df.bin_min.apply( lambda x: '{:.{}f}'.format(x, xtickdigits)).tolist() xlabel = "bin cutpoints" ax1_xticklabels = xticklabels[1:] + ['max'] xtick_offset = -.5 text_offset = 0 barwidth = 1 if var_scale == 'orig' and len(WOE_df.bin_min) > 2: ave_bin_width = np.nanmin([ (WOE_df.bin_min[2:] - WOE_df.bin_min[2:].shift()).mean(), (WOE_df.bin_min[-1] - WOE_df.bin_min[1]) / (n_bins - 1) ]) x_init = np.max( [WOE_df.bin_min[1], WOE_df.bin_min[2] - 2 * ave_bin_width]) ax1_xticks = np.array([x_init] + WOE_df.bin_min[2:].tolist() + [WOE_df.bin_min[-1] + 2 * ave_bin_width]) ax1_xticklabels[0] = 'min' axs[1].step(ax1_xticks, [WOE_df.WOE[1]] + WOE_df.WOE[1:].tolist(), color=woeCol, label='WOE', alpha=woeAlpha) axs[1].set_xticks(ax1_xticks) ra = .42 if sep_bar: axs[0].bar(xticks + .12, WOE_df['p_XgY1'], width=.6, label=targ_label + '=1', facecolor=(1, 0, 0, ra)) axs[0].bar(xticks - .08, WOE_df['p_XgY0'], width=.6, label=targ_label + '=0', fc=(0, 0, 1, ra / (ra + 1))) else: axs[0].bar(xticks, WOE_df['p_XgY1'], width=barwidth, label=targ_label + '=1', facecolor=(1, 0, 0, ra)) #, ec='w') axs[0].bar(xticks, WOE_df['p_XgY0'], width=barwidth, label=targ_label + '=0', fc='b', alpha=ra / (ra + 1)) axs[0].set_xticks(xticks + xtick_offset) axs[0].set_xticklabels(xticklabels, rotation=45, ha='right', rotation_mode='anchor') axs[0].set_xlabel(xlabel) axs[0].set_ylim((0, np.where(axs[0].get_ylim()[1] < 2 / (n_bins - 1), 2 / (n_bins - 1), axs[0].get_ylim()[1]))) axs[0].set_ylabel("Probability Density") axs[0].tick_params(labelsize=textsize) axs[0].legend(frameon=True, framealpha=1, fontsize=textsize) axs[1].axhline(WOE_df.WOE[0], linestyle=':', color=woeCol, alpha=woeAlpha) axs[1].set_xticklabels(ax1_xticklabels, rotation=45, ha='right', rotation_mode='anchor') axs[1].set_xlabel(xlabel) axs[1].set_xlim(left=axs[1].get_xlim()[0] - .7 * ave_bin_width) axs[1].set_ylim( (np.where(axs[1].get_ylim()[0] > -1, -1, axs[1].get_ylim()[0]), np.where(axs[1].get_ylim()[1] < 1, 1, axs[1].get_ylim()[1]))) axs[1].set_ylabel("WOE (est. marginal log-odds)") axs[1].tick_params(labelsize=textsize) if compute_stats: for i, x_pt in enumerate(ax1_xticks[:-1]): axs[1].text( x_pt + text_offset, WOE_df.WOE.values[i + 1] + .01 * np.diff(axs[1].get_ylim()), str(np.round((100 * WOE_df.bin_pred.values)[i + 1], 2)) + '%', color=woeCol, size=textsize - 1) axs[1].text(axs[1].get_xlim()[0], WOE_df.WOE.values[0] + .01 * np.diff(axs[1].get_ylim()), str(np.round((100 * WOE_df.bin_pred.values)[0], 2)) + '%', color=woeCol, size=textsize - 1) num = WOE_df.bin_ct.sum() n_targ = sum(WOE_df.Y1) MetricList = [ "# of Records = {:.0f}".format(num), "# of Targets = {:.0f}".format(n_targ), "# Missing = {:.0f}".format(WOE_df.iloc[0].bin_ct), "Base Rate = {:.3f}%".format(100 * (n_targ / num)), "RelEntropyY1Y0 = {:.5f}".format(sum(WOE_df.KLY1Y0)), "RelEntropyY0Y1 = {:.5f}".format(sum(WOE_df.KLY0Y1)), "InfoVal = {:.5f}".format(sum(WOE_df.IV)), "nInfoVal = {:.5f}".format(sum(WOE_df.nIV)) ] ylocs = np.arange(.97, 0, -.05)[:len(MetricList)] for yloc, sm in zip(ylocs, MetricList): axs[1].annotate(sm, xy=(1.02, yloc), xycoords='axes fraction', size=textsize) plt.show() return (WOE_df)
def _default_color(method, hue, color, kws): """FROM SEABORN. If needed, get a default key by using the matplotlib property cycle.""" from matplotlib.colors import to_rgb if hue is not None: # This warning is probably user-friendly, but it's currently triggered # in a FacetGrid context and I don't want to mess with that logic right now # if key is not None: # msg = "`key` is ignored when `hue` is assigned." # warnings.warn(msg) return None if color is not None: return color elif method.__name__ == "plot": (scout, ) = method([], [], **kws) color = scout.get_color() scout.remove() elif method.__name__ == "scatter": # Matplotlib will raise if the size of x/y don't match s/c, # and the latter might be in the kws dict scout_size = max( np.atleast_1d(kws.get(key, [])).shape[0] for key in ["s", "c", "fc", "facecolor", "facecolors"]) scout_x = scout_y = np.full(scout_size, np.nan) scout = method(scout_x, scout_y, **kws) facecolors = scout.get_facecolors() if not len(facecolors): # Handle bug in matplotlib <= 3.2 (I think) # This will limit the ability to use non key= kwargs to specify # a key in versions of matplotlib with the bug, but trying to # work out what the user wanted by re-implementing the broken logic # of inspecting the kwargs is probably too brittle. single_color = False else: single_color = np.unique(facecolors, axis=0).shape[0] == 1 # Allow the user to specify an array of colors through various kwargs if "c" not in kws and single_color: color = to_rgb(facecolors[0]) scout.remove() elif method.__name__ == "bar": # bar() needs masked, not empty data, to generate a patch (scout, ) = method([np.nan], [np.nan], **kws) color = to_rgb(scout.get_facecolor()) scout.remove() elif method.__name__ == "fill_between": # There is a bug on matplotlib < 3.3 where fill_between with # datetime units and empty data will set incorrect autoscale limits # To workaround it, we'll always return the first key in the cycle. # https://github.com/matplotlib/matplotlib/issues/17586 ax = method.__self__ datetime_axis = any([ isinstance(ax.xaxis.converter, mpl.dates.DateConverter), isinstance(ax.yaxis.converter, mpl.dates.DateConverter), ]) kws = _normalize_kwargs(kws, mpl.collections.PolyCollection) scout = method([], [], **kws) facecolor = scout.get_facecolor() color = to_rgb(facecolor[0]) scout.remove() return color
def draw_bbox_mask(image, results, threshold=0.5, color_map=None): import matplotlib matplotlib.use('Agg') import matplotlib as mpl import matplotlib.figure as mplfigure import matplotlib.colors as mplc from matplotlib.backends.backend_agg import FigureCanvasAgg # refer to https://github.com/facebookresearch/detectron2/blob/master/detectron2/utils/visualizer.py def _change_color_brightness(color, brightness_factor): assert brightness_factor >= -1.0 and brightness_factor <= 1.0 color = mplc.to_rgb(color) polygon_color = colorsys.rgb_to_hls(*mplc.to_rgb(color)) modified_lightness = polygon_color[1] + (brightness_factor * polygon_color[1]) modified_lightness = 0.0 if modified_lightness < 0.0 else modified_lightness modified_lightness = 1.0 if modified_lightness > 1.0 else modified_lightness modified_color = colorsys.hls_to_rgb(polygon_color[0], modified_lightness, polygon_color[2]) return modified_color _SMALL_OBJECT_AREA_THRESH = 1000 # setup figure width, height = image.shape[1], image.shape[0] scale = 1 fig = mplfigure.Figure(frameon=False) dpi = fig.get_dpi() fig.set_size_inches( (width * scale + 1e-2) / dpi, (height * scale + 1e-2) / dpi, ) canvas = FigureCanvasAgg(fig) ax = fig.add_axes([0.0, 0.0, 1.0, 1.0]) ax.axis("off") ax.set_xlim(0.0, width) ax.set_ylim(height) default_font_size = max(np.sqrt(height * width) // 90, 10 // scale) linewidth = max(default_font_size / 4, 1) labels = list() for dt in np.array(results): if dt['category'] not in labels: labels.append(dt['category']) if color_map is None: color_map = get_color_map_list(len(labels) + 2)[2:] else: color_map = np.asarray(color_map) if color_map.shape[0] != len(labels) or color_map.shape[1] != 3: raise Exception( "The shape for color_map is required to be {}x3, but recieved shape is {}x{}." .format(len(labels), color_map.shape)) if np.max(color_map) > 255 or np.min(color_map) < 0: raise ValueError( " The values in color_map should be within 0-255 range.") keep_results = [] areas = [] for dt in np.array(results): cname, bbox, score = dt['category'], dt['bbox'], dt['score'] if score < threshold: continue keep_results.append(dt) areas.append(bbox[2] * bbox[3]) areas = np.asarray(areas) sorted_idxs = np.argsort(-areas).tolist() keep_results = [keep_results[k] for k in sorted_idxs] if len(keep_results) > 0 else [] for dt in np.array(keep_results): cname, bbox, score = dt['category'], dt['bbox'], dt['score'] xmin, ymin, w, h = bbox xmax = xmin + w ymax = ymin + h color = tuple(color_map[labels.index(cname)]) color = [c / 255. for c in color] # draw bbox ax.add_patch( mpl.patches.Rectangle( (xmin, ymin), w, h, fill=False, edgecolor=color, linewidth=linewidth * scale, alpha=0.8, linestyle="-", )) # draw mask if 'mask' in dt: mask = dt['mask'] mask = np.ascontiguousarray(mask) res = cv2.findContours(mask.astype("uint8"), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE) hierarchy = res[-1] alpha = 0.5 if hierarchy is not None: has_holes = (hierarchy.reshape(-1, 4)[:, 3] >= 0).sum() > 0 res = res[-2] res = [x.flatten() for x in res] res = [x for x in res if len(x) >= 6] for segment in res: segment = segment.reshape(-1, 2) edge_color = mplc.to_rgb(color) + (1, ) polygon = mpl.patches.Polygon( segment, fill=True, facecolor=mplc.to_rgb(color) + (alpha, ), edgecolor=edge_color, linewidth=max(default_font_size // 15 * scale, 1), ) ax.add_patch(polygon) # draw label text_pos = (xmin, ymin) horiz_align = "left" instance_area = w * h if (instance_area < _SMALL_OBJECT_AREA_THRESH * scale or h < 40 * scale): if ymin >= height - 5: text_pos = (xmin, ymin) else: text_pos = (xmin, ymax) height_ratio = h / np.sqrt(height * width) font_size = (np.clip((height_ratio - 0.02) / 0.08 + 1, 1.2, 2) * 0.5 * default_font_size) text = "{} {:.2f}".format(cname, score) color = np.maximum(list(mplc.to_rgb(color)), 0.2) color[np.argmax(color)] = max(0.8, np.max(color)) color = _change_color_brightness(color, brightness_factor=0.7) ax.text( text_pos[0], text_pos[1], text, size=font_size * scale, family="sans-serif", bbox={ "facecolor": "black", "alpha": 0.8, "pad": 0.7, "edgecolor": "none" }, verticalalignment="top", horizontalalignment=horiz_align, color=color, zorder=10, rotation=0, ) s, (width, height) = canvas.print_to_buffer() buffer = np.frombuffer(s, dtype="uint8") img_rgba = buffer.reshape(height, width, 4) rgb, alpha = np.split(img_rgba, [3], axis=2) try: import numexpr as ne visualized_image = ne.evaluate( "image * (1 - alpha / 255.0) + rgb * (alpha / 255.0)") except ImportError: alpha = alpha.astype("float32") / 255.0 visualized_image = image * (1 - alpha) + rgb * alpha visualized_image = visualized_image.astype("uint8") return visualized_image
def __init__(self, A, border_width, border_color, colormap): self.A = A.tocsr() self.border_width = border_width rgb = numpy.array(colors.to_rgb(border_color)) border_color_is_bw = numpy.all(rgb[0] == rgb) and rgb[0] in [0, 1] border_color_is_gray = numpy.all(rgb[0] == rgb) if colormap is None and (border_width == 0 or border_color_is_bw): self.mode = 'binary' self.border_color = False self.bitdepth = 1 self.dtype = numpy.bool elif colormap is None and border_color_is_gray: self.mode = 'grayscale' self.bitdepth = 8 self.dtype = numpy.uint8 self.border_color = numpy.uint8(numpy.round(rgb[0]*255)) else: self.mode = 'rgb' self.border_color = numpy.round(rgb*255).astype(numpy.uint8) self.dtype = numpy.uint8 self.bitdepth = 8 if colormap is None: if self.mode == 'binary': # pylint: disable=unused-argument def convert_values(idx, vals): out = numpy.ones(self.A.shape[1], dtype=self.dtype) out[idx] = False return out elif self.mode == 'grayscale': # pylint: disable=unused-argument def convert_values(idx, vals): out = numpy.full(self.A.shape[1], 255, dtype=self.dtype) out[idx] = 0 return out else: assert self.mode == 'rgb' # pylint: disable=unused-argument def convert_values(idx, vals): out = numpy.full( (self.A.shape[1], 3), 255, dtype=self.dtype ) out[idx, :] = 0 return out.flatten() else: assert self.mode == 'rgb' # Convert the string into a colormap object with `to_rgba()`, # <https://stackoverflow.com/a/15140118/353337>. import matplotlib.cm as cmx cm = plt.get_cmap(colormap) c_norm = colors.Normalize( vmin=min(0.0, self.A.data.min()), vmax=max(0.0, self.A.data.max()) ) scalar_map = cmx.ScalarMappable(norm=c_norm, cmap=cm) def convert_values(idx, vals): x = numpy.zeros(self.A.shape[1]) x[idx] = vals out = scalar_map.to_rgba(x)[:, :3] * 255 out = numpy.round(out).astype(self.dtype) return out.flatten() self.convert_values = convert_values self.current = 0 return
def plot_pattern_diagram_markers(X,Y,option): ''' Plots color markers on a pattern diagram. Plots color markers on a target diagram according their (X,Y) locations. The symbols and colors are chosen automatically with a limit of 70 symbol & color combinations. The color bar is titled using the content of option['titleColorBar'] (if non-empty string). INPUTS: x : x-coordinates of markers y : y-coordinates of markers z : z-coordinates of markers (used for color shading) option : dictionary containing option values. (Refer to GET_TARGET_DIAGRAM_OPTIONS function for more information.) option['axismax'] : maximum for the X & Y values. Used to limit maximum distance from origin to display markers option['markerlabel'] : labels for markers OUTPUTS: None Created on Nov 30, 2016 Revised on Jan 6, 2019 Author: Peter A. Rochford Symplectic, LLC www.thesymplectic.com [email protected] ''' # Set face color transparency alpha = option['alpha'] # Set font and marker size fontSize = matplotlib.rcParams.get('font.size') - 2 markerSize = option['markersize'] # Check enough labels provided if markerlabel provided numberLabel = len(option['markerlabel']) if numberLabel > 0: if isinstance(option['markerlabel'], list) and numberLabel < len(X): raise ValueError('Insufficient number of marker labels provided.\n' + 'target: No. labels=' + str(numberLabel) + ' < No. markers=' + str(len(X)) + '\n' + 'taylor: No. labels=' + str(numberLabel+1) + ' < No. markers=' + str(len(X)+1)) elif isinstance(option['markerlabel'], dict) and numberLabel > 70: raise ValueError('Insufficient number of marker labels provided.\n' + 'target: No. labels=' + str(numberLabel) + ' > No. markers= 70') if option['markerlegend'] == 'on': # Check that marker labels have been provided if option['markerlabel'] == '': raise ValueError('No marker labels provided.') # Plot markers of different color and shapes with labels # displayed in a legend # Define markers kind = ['+','o','x','s','d','^','v','p','h','*'] colorm = ['r','b','g','c','m','y','k','gray'] if len(X) > 80: _disp('You must introduce new markers to plot more than 70 cases.') _disp('The ''marker'' character array need to be extended inside the code.') if len(X) <= len(kind): # Define markers with specified color marker = [] markercolor = [] for color in colorm: if option['markercolor'] == 'r': rgba = clr.to_rgb(color) + (alpha,) else: rgba = clr.to_rgb(option['markercolor']) + (alpha,) markercolor.append(rgba) for symbol in kind: marker.append(symbol + option['markercolor']) else: # Define markers and colors using predefined list marker = [] markercolor = [] #Bug Fix: missing array initialization for color in colorm: for symbol in kind: marker.append(symbol + color) rgba = clr.to_rgb(color) + (alpha,) markercolor.append(rgba) # Plot markers at data points limit = option['axismax'] hp = () markerlabel = [] for i, xval in enumerate(X): if abs(X[i]) <= limit and abs(Y[i]) <= limit: h = plt.plot(X[i],Y[i],marker[i], markersize = markerSize, markerfacecolor = markercolor[i], markeredgecolor = markercolor[i][0:3] + (1.0,), markeredgewidth = 2) hp += tuple(h) markerlabel.append(option['markerlabel'][i]) # Add legend if len(markerlabel) == 0: warnings.warn('No markers within axis limit ranges.') else: add_legend(markerlabel, option, rgba, markerSize, fontSize, hp) else: # Plot markers as dots of a single color with accompanying labels # and no legend # Plot markers at data points limit = option['axismax'] rgba = clr.to_rgb(option['markercolor']) + (alpha,) for i,xval in enumerate(X): if abs(X[i]) <= limit and abs(Y[i]) <= limit: # Plot marker marker = option['markersymbol'] plt.plot(X[i],Y[i],marker, markersize = markerSize, markerfacecolor = rgba, markeredgecolor = option['markercolor']) # Check if marker labels provided if type(option['markerlabel']) is list: # Label marker xtextpos = X[i] ytextpos = Y[i] plt.text(xtextpos,ytextpos,option['markerlabel'][i], color = option['markerlabelcolor'], verticalalignment = 'bottom', horizontalalignment = 'right', fontsize = fontSize) # Add legend if labels provided as dictionary markerlabel = option['markerlabel'] if type(markerlabel) is dict: add_legend(markerlabel, option, rgba, markerSize, fontSize)