def read_dump(dump_filename, target_table): sio = StringIO() count = 0 fast_forward = True with open(dump_filename, 'rb') as f: for line in f: line = line.strip() if line.lower().startswith('insert') and target_table in line: fast_forward = False if fast_forward: continue data = re.findall('\([^\)]*\)', line) try: newline = data[0] ### updated this part newlines = re.sub('\),\(', '),\n(', newline) for newline in newlines.split('\n'): newline = newline.strip(' ()') newline = newline.replace('`', '') newline = newline.replace(', ', '_ ') sio.write(newline) sio.write("\n") except IndexError: print newline pass sio.pos = 0 return sio
def fixation_heatmap(fixations, screen_image, alpha=0.7, dot_size=200, cmap=None, dpi=90): points = fixations[["fix_x", "fix_y"]].values heatmap_data = make_heatmap(points, screen_image.size, dot_size) width, height = screen_image.size width_inches = width / float(dpi) height_inches = height / float(dpi) fig = pyplot.figure(figsize=(width_inches, height_inches), dpi=dpi, frameon=False) ax = pyplot.Axes(fig, [0, 0, 1, 1]) ax.set_axis_off() fig.add_axes(ax) if cmap is None: cmap = pyplot.cm.get_cmap("jet") cmap._init() alphas = np.abs(np.linspace(-1.0, 1.0, cmap.N)) cmap._lut[:-3, -1] = alphas ax.imshow(heatmap_data.T, interpolation="none", cmap=cmap) plot_buffer = StringIO() fig.savefig(plot_buffer, format="png", dpi=dpi) pyplot.close(fig) plot_buffer.pos = 0 heatmap_image = Image.open(plot_buffer) heatmap_alpha = heatmap_image.split()[3] heatmap_image.putalpha(ImageEnhance.Brightness(heatmap_alpha).enhance(alpha)) return Image.composite(heatmap_image, screen_image, heatmap_image)
def line_code_image(fixations, code_image, num_lines=None, image_padding=10, image_dpi=120, bar_height=0.75, bar_mult=1.0, width_inches=5, method="time"): """Plots fixation times or counts as bars next to code lines.""" if num_lines is None: num_lines = fixations.line.max() if method == "time": method = lambda frame, lines: [ sum(frame[frame.line == line].duration_ms) for line in lines ] elif method == "count": method = lambda frame, lines: [ sum(frame.line == line) for line in lines ] lines = np.arange(1, num_lines + 1) # Plot bar graph height_inches = (code_image.size[1] - (image_padding * 2)) / float(image_dpi) fig = pyplot.figure(figsize=(width_inches, height_inches), dpi=image_dpi) ax = pyplot.Axes(fig, [0, 0, 1, 1]) ax.set_axis_off() fig.add_axes(ax) ax.barh(lines * bar_mult, method(fixations, lines), height=bar_height) # Show every line ax.set_yticks(lines) # Don't include line 0 ax.set_ylim(1, num_lines + 1) # Lines start at 1 on top ax.invert_yaxis() # Bars go from right to left ax.invert_xaxis() # Combine with code image plot_buffer = StringIO() fig.savefig(plot_buffer, format="png", dpi=image_dpi) pyplot.close(fig) plot_buffer.pos = 0 plot_image = Image.open(plot_buffer) master_image = Image.new( "RGBA", (plot_image.size[0] + code_image.size[0], code_image.size[1]), (255, 255, 255, 255)) master_image.paste(plot_image, (0, image_padding)) master_image.paste(code_image, (plot_image.size[0], 0)) return master_image
def pd_to_csv(df, **kwargs): """ Takes a :class:`pandas.DataFrame` and returns a csv. """ a = StringIO() df.to_csv(a, **kwargs) a.pos = 0 return a.read()
def test_load_dump_roundtrip(): """Integration test for roundtripping par files.""" f = StringIO(sample_par) pydict = runtime_parameters.load(f) newf = StringIO() runtime_parameters.dump(pydict, newf) newf.pos = 0 obs_dumped = newf.read() newf.pos = 0 newpydict = runtime_parameters.load(newf) assert_equal(set(pydict.keys()), set(newpydict.keys())) newnewf = StringIO() runtime_parameters.dump(newpydict, newnewf) newnewf.pos = 0 obs_newdumped = newnewf.read() assert_equal(obs_dumped, obs_newdumped)
def test_dump1(): f = StringIO() params = {'b': 10, 'a': True} # dump the values runtime_parameters.dump(params, f) f.pos = 0 obs = f.read() exp = "a = .true.\nb = 10" assert_equal(obs, exp)
def test_simple_save(self): stm = StringIO() instrs = [dict(type='LINE', dx=10, dy=20, dz=2, spd=4), dict(type='COMMENT', text='test comment')] write(instrs, stm) b = '\x02\x00' + \ '\x04\x0f10.0,20.0,2.0,4' + '\x00' * 15 + \ '\x1b\x0ctest comment' + '\x00' * 18 + \ '\x00\x00' self.assertEqual(stm.getvalue(), b) stm.pos = 0 read_result = parse(stm) self.assertEqual((instrs, []), read_result)
def qrcode_imagem(self): # # box_size=2 dá um tamanho de 4 x 4 cm # codigo = qrcode.QRCode(box_size=2) codigo.add_data(self.infNFeSupl.qrCode.valor) arq = StringIO() codigo.make_image().save(arq) arq.pos = 0 imagem = arq.read().encode('base64') arq.close() return imagem
def line_code_image(fixations, code_image, num_lines=None, image_padding=10, image_dpi=120, bar_height=0.75, bar_mult=1.0, width_inches=5, method="time"): """Plots fixation times or counts as bars next to code lines.""" if num_lines is None: num_lines = fixations.line.max() if method == "time": method = lambda frame, lines: [sum(frame[frame.line == line].duration_ms) for line in lines] elif method == "count": method = lambda frame, lines: [sum(frame.line == line) for line in lines] lines = np.arange(1, num_lines + 1) # Plot bar graph height_inches = (code_image.size[1] - (image_padding * 2)) / float(image_dpi) fig = pyplot.figure(figsize=(width_inches, height_inches), dpi=image_dpi) ax = pyplot.Axes(fig, [0, 0, 1, 1]) ax.set_axis_off() fig.add_axes(ax) ax.barh(lines * bar_mult, method(fixations, lines), height=bar_height) # Show every line ax.set_yticks(lines) # Don't include line 0 ax.set_ylim(1, num_lines + 1) # Lines start at 1 on top ax.invert_yaxis() # Bars go from right to left ax.invert_xaxis() # Combine with code image plot_buffer = StringIO() fig.savefig(plot_buffer, format="png", dpi=image_dpi) pyplot.close(fig) plot_buffer.pos = 0 plot_image = Image.open(plot_buffer) master_image = Image.new("RGBA", (plot_image.size[0] + code_image.size[0], code_image.size[1]), (255, 255, 255, 255)) master_image.paste(plot_image, (0, image_padding)) master_image.paste(code_image, (plot_image.size[0], 0)) return master_image
def MakeQRdata(db, blacklist): """ function that modify sample an and group so to observe difference between observed sample ( with name that start with "QUERY") and reference one used only to build the tree Reference sequence """ totNames = set([x.name for x in db.tree.get_terminals()]) Qlist = blacklist Rlist = totNames.difference(blacklist) OUT = "" for r in Rlist: OUT += "\t".join(["R", "1", r]) + "\n" for q in Qlist: OUT += "\t".join(["Q", "1", q]) + "\n" #handle=open("QvsR.sample","w") handle = StringIO() handle.write(OUT) handle.pos = 0 return handle
def MakeQRdata(db, blacklist): """ function that modify sample an and group so to observe difference between observed sample ( with name that start with "QUERY") and reference one used only to build the tree Reference sequence """ totNames=set([x.name for x in db.tree.get_terminals()]) Qlist=blacklist Rlist=totNames.difference(blacklist) OUT="" for r in Rlist: OUT+="\t".join(["R","1",r])+"\n" for q in Qlist: OUT+="\t".join(["Q","1",q])+"\n" #handle=open("QvsR.sample","w") handle=StringIO() handle.write(OUT) handle.pos=0 return handle
def _read_dump(dump_filename, target_table): sio = StringIO() fast_forward = True with open(dump_filename, 'rb') as f: for line in f: line = line.strip() if line.lower().startswith('insert') and target_table in line: fast_forward = False if fast_forward: continue data = re.findall('\([^\)]*\)', line) try: newline = data[0] newline = newline.strip(' ()') newline = newline.replace('`', '') sio.write(newline) sio.write("\n") except IndexError: pass if line.endswith(';'): break sio.pos = 0 return sio
#-*-coding:utf-8-*- import multiprocessing import time import gzip from StringIO import StringIO import json if __name__ == "__main__": buf = StringIO() file = gzip.GzipFile(fileobj=buf, mode='w') data = [] for i in xrange(30): data.append({'id': i}) json.dump(data, file) file.close() buf.pos = 0 file = gzip.GzipFile(fileobj=buf) data2 = json.load(file) file.close()
#-*-coding:utf-8-*- import multiprocessing import time import gzip from StringIO import StringIO import json if __name__ == "__main__": buf=StringIO() file=gzip.GzipFile(fileobj=buf,mode='w') data=[] for i in xrange(30): data.append({'id':i}) json.dump(data,file) file.close() buf.pos=0 file=gzip.GzipFile(fileobj=buf) data2=json.load(file) file.close()
def aoi_code_image(fixes, screen_img, trial_aois, kind="code-grid", cmap=pyplot.cm.OrRd, syntax_alpha=0.7, code_padding=5): # Crop out code image line_aois = trial_aois[(trial_aois.kind == "line")] env = envelope(line_aois, code_padding).irow(0) crop_rect = [env["x"], env["y"], env["x"] + env["width"], env["y"] + env["height"]] # Hit test against grid AOIs col = kind_to_col(kind) code_aois = trial_aois[(trial_aois.kind == kind)] code_fixes = fixes.dropna(subset=[col]) code_counts = code_fixes.groupby(col).duration_ms.sum() max_code_count = float(max(code_counts)) def color_grid(kind, name, local_id): rel_count = code_counts.get(name, default=0) / max_code_count return matplotlib.colors.rgb2hex(cmap(rel_count)) # Create syntax-based image code_box = trial_aois[(trial_aois.kind == "interface") & (trial_aois.name == "code box")].irow(0) aoi_img = draw_rectangles(code_aois, screen_img, color_func=color_grid, alpha=syntax_alpha, outline=None) code_img = aoi_img.crop(crop_rect) # Add colorbar dpi = 90 width, height = (0.25 * dpi), code_img.size[1] width_inches = width / float(dpi) height_inches = height / float(dpi) fig = pyplot.figure(figsize=(width_inches, height_inches), dpi=dpi, frameon=False) ax = pyplot.Axes(fig, [0, 0, 1, 1]) ax.set_axis_off() fig.add_axes(ax) norm = matplotlib.colors.Normalize(vmin=min(code_counts), vmax=max_code_count) cb = matplotlib.colorbar.ColorbarBase(ax, cmap=cmap, norm=norm) #cb.set_label("Total Fixation Duration (ms)") cb.outline.set_linewidth(0) # Convert plot to image plot_buffer = StringIO() fig.savefig(plot_buffer, format="png", dpi=dpi) pyplot.close(fig) plot_buffer.pos = 0 plot_img = Image.open(plot_buffer) # Combine AOI and colorbar images horz_padding = (0.25 * dpi) width = int(code_img.size[0] + horz_padding + plot_img.size[0]) height = int(code_img.size[1]) final_img = Image.new("RGBA", (width, height), color="white") final_img.paste(code_img, (0, 0)) final_img.paste(plot_img, (int(horz_padding + code_img.size[0]), 0)) return final_img
def read_snapshot(self,f): try: snap = Snap() item = f.readline() snap.time = int(f.readline().split()[0]) # just grab 1st field item = f.readline() snap.natoms = int(f.readline()) snap.aselect = np.zeros(snap.natoms) item = f.readline() words = item.split("BOUNDS ") if len(words) == 1: snap.boxstr = "" else: snap.boxstr = words[1].strip() if "xy" in snap.boxstr: snap.triclinic = 1 else: snap.triclinic = 0 words = f.readline().split() if len(words) == 2: snap.xlo,snap.xhi,snap.xy = float(words[0]),float(words[1]),0.0 else: snap.xlo,snap.xhi,snap.xy = \ float(words[0]),float(words[1]),float(words[2]) words = f.readline().split() if len(words) == 2: snap.ylo,snap.yhi,snap.xz = float(words[0]),float(words[1]),0.0 else: snap.ylo,snap.yhi,snap.xz = \ float(words[0]),float(words[1]),float(words[2]) words = f.readline().split() if len(words) == 2: snap.zlo,snap.zhi,snap.yz = float(words[0]),float(words[1]),0.0 else: snap.zlo,snap.zhi,snap.yz = \ float(words[0]),float(words[1]),float(words[2]) item = f.readline() if len(self.names) == 0: self.scale_original = -1 xflag = yflag = zflag = -1 words = item.split()[2:] if len(words): for i in range(len(words)): if words[i] == "x" or words[i] == "xu": xflag = 0 self.names["x"] = i elif words[i] == "xs" or words[i] == "xsu": xflag = 1 self.names["x"] = i elif words[i] == "y" or words[i] == "yu": yflag = 0 self.names["y"] = i elif words[i] == "ys" or words[i] == "ysu": yflag = 1 self.names["y"] = i elif words[i] == "z" or words[i] == "zu": zflag = 0 self.names["z"] = i elif words[i] == "zs" or words[i] == "zsu": zflag = 1 self.names["z"] = i else: self.names[words[i]] = i if xflag == 0 and yflag == 0 and zflag == 0: self.scale_original = 0 if xflag == 1 and yflag == 1 and zflag == 1: self.scale_original = 1 if snap.natoms: if oldnumeric: words = f.readline().split() ncol = len(words) for i in xrange(1,snap.natoms): words += f.readline().split() floats = map(float,words) atoms = np.zeros((snap.natoms,ncol),np.Float) start = 0 stop = ncol for i in xrange(snap.natoms): atoms[i] = floats[start:stop] start = stop stop += ncol else: ss=StringIO("") for i in xrange(snap.natoms): ss.write(f.readline()) ss.pos=0 atoms=np.loadtxt(ss) print "atoms.shape",atoms.shape else: atoms = None snap.atoms = atoms return snap except: return 0
def line_code_image(line_fixes, code_image, num_lines, method="time", image_padding=10, image_dpi=120, bar_height=0.75, bar_mult=1.0, width_inches=5, color=None, horiz_sep=0, line_numbers=False, **kwargs): """Plots fixation information as bars next to code lines. Parameters ---------- line_fixes : pandas DataFrame DataFrame with one fixation per row + line annotation. Must have columns start_ms, duration_ms, line. code_image : PIL Image Image with highlighted code (see plot.hightlight_code) num_lines : int Number of lines in the code method : str or callable, optional Method for determining size of line bars. May be one of: * 'time' - total fixation duration * 'count' - number of fixations * 'first' - time of first fixation or a callable with 2 argments (frame, lines) where * frame is the line_fixes DataFrame * lines is a list of line numbers that returns a list of bar heights for each line. Default is 'time'. image_padding : int, optional Padding expected around image in pixels (default: 10) image_dpi : int, optional Dots per inch for final rendered image bar_height : float, optional Height (or thickness) of horizontal bars (default: 0.75) bar_mult : float, optional Factor to multiply bars' vertical positions by (default: 1.0) width_inches : float, optional Width of final rendered image in inches color : str or None, optional Color of bars or None for automatic selection horiz_sep : int, optional Separation between bars and code image in pixels (default: 0) **kwargs : keyword arguments Arguments passed through to matplotlib barh function Returns ------- img : PIL Image Image with bars and code combined """ assert num_lines > 0, "Must have more than 0 lines" # Check for known methods (time, count, first) if method == "time": # Total fixation duration per line method = lambda frame, lines: [sum(frame[frame.line == line].duration_ms) for line in lines] color = color or kelly_colors[0] elif method == "count": # Number of fixations per line method = lambda frame, lines: [sum(frame.line == line) for line in lines] color = color or kelly_colors[1] elif method == "first": # Time of first fixation per line method = lambda frame, lines: [frame[frame.line == line].start_ms.min() for line in lines] color = color or kelly_colors[2] lines = np.arange(1, num_lines + 1) # Plot bar graph with no axes or labels height_inches = (code_image.size[1] - (image_padding * 2)) / float(image_dpi) if line_numbers: # Leave a little room for the line numbers fig, ax = pyplot.subplots(figsize=(width_inches, height_inches), dpi=image_dpi) ax.set_position([0, 0, 0.9, 1]) ax.set_frame_on(False) else: # Take the entire figure space fig = pyplot.figure(figsize=(width_inches, height_inches), dpi=image_dpi) ax = pyplot.Axes(fig, [0, 0, 1, 1]) ax.set_axis_off() fig.add_axes(ax) ax.barh(lines * bar_mult, method(line_fixes, lines), height=bar_height, color=color, **kwargs) # Show every line ax.set_yticks(0.5 + lines) # Don't include line 0 ax.set_ylim(1, num_lines + 1) # Lines start at 1 on top ax.invert_yaxis() # Bars go from right to left ax.invert_xaxis() if line_numbers: # Put line numbers on the right ax.set_yticklabels(np.arange(1, num_lines + 1)) ax.yaxis.tick_right() # Align line number labels for label in ax.yaxis.get_ticklabels(): label.set_verticalalignment("center") # Hide tick lines for tic in it.chain(ax.xaxis.get_major_ticks(), ax.yaxis.get_major_ticks()): tic.tick1On = False tic.tick2On = False # Combine with code image plot_buffer = StringIO() fig.savefig(plot_buffer, format="png", dpi=image_dpi) pyplot.close(fig) plot_buffer.pos = 0 plot_image = Image.open(plot_buffer) # Create combined image master_width = plot_image.size[0] + horiz_sep + code_image.size[0] master_image = Image.new("RGBA", (master_width, code_image.size[1]), (255, 255, 255, 255)) # Paste bar plot (left) and code (right) master_image.paste(plot_image, (0, image_padding)) master_image.paste(code_image, (plot_image.size[0] + horiz_sep, 0)) return master_image