def __init__(self, attr, value, guard=None, mode='xml'): super(AttrNode, self).__init__(value) self.attr = attr # self.value = value self.guard = guard self.mode = mode self.attrname, self.genname = gen_name(), gen_name()
def save_all_paths(gen_paths, g, fname, tag, num_bins, measure): # Create a histogram of all paths in the graph, save it to `fname` plt.xlabel("Runtime (ms)") plt.ylabel("Num. Paths") plt.title("%s-%s" % (fname.rsplit(".", 1)[0].rsplit("/", 1)[-1], tag)) gen_runtimes = lambda: (measure(p, g) for p in gen_paths()) min_runtime = min(gen_runtimes()) max_runtime = max(gen_runtimes()) med_runtime = statistics.median(gen_runtimes()) bkt_width = (max_runtime - min_runtime) / num_bins bins = ((min_runtime + int(i * bkt_width), min_runtime + int((1 + i) * bkt_width)) for i in range(0, num_bins)) for (lo, hi) in bins: plt.bar(lo, sum((1 for rt in gen_runtimes() if lo <= rt <= hi)), alpha=0.6, width=bkt_width) # Max, Min, Median path lines plt.axvline(med_runtime, color="k", linestyle="solid", linewidth=5, label="median = %s" % int(med_runtime)) # invisible lines plt.axvline(min_runtime, alpha=0, color="0", linestyle="dashed", linewidth=0, label="min = %s" % int(min_runtime)) plt.axvline(max_runtime, alpha=0, color="0", linestyle="dotted", linewidth=0, label="max = %s" % int(max_runtime)) # Save figure new_name = util.gen_name(fname, tag, "png") lgd = plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.0) plt.savefig(new_name, bbox_extra_artists=(lgd,), bbox_inches="tight") print("Saved plot to '%s'" % new_name) ## Save data save_runtimes(zip(gen_paths(), gen_runtimes()), util.gen_name(fname, tag, "tab")) plt.clf() return new_name
def create_npc(npc_names, demon = False): if not demon: name = util.gen_name(check_unique=world.npc_names) else: name = util.gen_name('demon', check_unique=world.npc_names) if npc_names is not None: npc_names.append(name) return name
def profile_group(fname, gname, *group): # Get information about the modules `group` with respect to the other files. # TODO group = list(group) d = util.dict_of_file(gname) print("Profiling group '%s'" % group) print("Key:") with open(gname, "r") as f: next(f) for line in f: print(" " + line.strip()) tr_list = [(title, int(statistics.mean(row))) for (title, row) in rows_where_group_is_boundary(fname, group, d)] tr_list.sort(key=lambda x: x[1]) for title, val in tr_list: print("Config '%s' has avg. runtime %d" % (title, val)) ## Plot bar graph wd = 200 num_bars = len(tr_list) ind = [i * wd for i in range(0, num_bars)] xlabels = [x[0] for x in tr_list] xvals = [x[1] for x in tr_list] plt.axis([0, wd * (num_bars - 1), 0, max(xvals)]) fig, ax = plt.subplots() ax.bar(ind, xvals, width=100) ax.set_xticks(ind) ax.set_xticklabels(xlabels) fig.set_size_inches(25, 10) plt.xlabel("Config. (%s)" % (sorted([(key, int(val[0])) for (key, val) in d.items()], key=lambda x: x[1]))) plt.ylabel("Avg. Runtime (ms)") plt.title("%s, where group %s is a boundary" % (fname.rsplit("/", 1)[-1], group)) new_name = util.gen_name(fname, "+".join((x.rsplit(".", 1)[0] for x in group)), "png") plt.savefig(new_name) plt.clf() print("Saved figure as '%s'" % new_name) return
def main(fname, num_modules): num_samples = min(num_modules, MAX_SAMPLES) data = sample_by_num_typed_modules(fname, num_modules, num_samples) fig, ax1 = plt.subplots() # ax1.set_ylim(0, 18000) #funkytown ax1.set_ylim(400, 900) # tetris ## Graph data. For each number of modules, graph the mean & ci. for i in range(len(data)): ci_rect = Rectangle( [i + 1 - 0.25, data[i]["ci"][0]], 0.5, data[i]["ci"][1] - data[i]["ci"][0], facecolor="royalblue" ) ax1.add_patch(ci_rect) plt.plot(i + 1, [data[i]["mean"]], color="w", marker="*", markeredgecolor="k") ## Save graph plt.xticks(range(1, 1 + len(data)), range(0, len(data))) # plt.yticks([5000,10000,15000]) #funkytown plt.yticks([100 * i for i in range(4, 10)]) ax1.yaxis.grid(True, linestyle="-", which="major", color="lightgrey", alpha=0.5) ax1.set_axisbelow(True) ax1.set_title(fname.rsplit("/", 1)[-1].rsplit(".")[0]) ax1.set_xlabel("Num. Typed Modules") ax1.set_ylabel("Runtime (ms)") new_name = util.gen_name(fname, "sampling-%s" % num_samples, "png") plt.savefig(new_name) plt.clf() print("Saved results to '%s'" % new_name) return
def process_string(s): substs = util.LambdaMap() result = s.strip() #first parse name with random one substs['name'] = lambda: util.gen_name() substs['enemy'] = lambda: create_enemy() substs['adventurer'] = lambda: create_adventurer() substs['npc'] = lambda: create_npc(None) substs['good_npc'] = lambda: create_npc(world.good_npc_names) substs['bad_npc'] = lambda: create_npc(world.bad_npc_names) substs['demon_npc'] = lambda: create_npc(world.demon_npc_names, demon=True) substs['deity_npc'] = lambda: create_npc(world.deity_npc_names) substs['birth_city'] = lambda: create_city(birth=True, city=True) substs['birth_village'] = lambda: create_city(birth=True, city=False) substs['city'] = lambda: create_city(city=True) substs['village'] = lambda: create_city() substs['depart_village'] = lambda: create_city(depart=True, city=False) substs['depart_city'] = lambda: create_city(depart=True, city=True) substs['noun'] = lambda: random.choice(nouns) substs['Noun'] = lambda: random.choice(nouns).capitalize() substs['Adjective'] = lambda: random.choice(adjectives).capitalize() substs['adjective'] = lambda: random.choice(adjectives) substs['skill'] = lambda: random.choice(skills) substs['Skill'] = lambda: random.choice(skills).capitalize() substs['profession'] = lambda: random.choice(professions) substs['Profession'] = lambda: random.choice(professions).capitalize() substs['age'] = lambda: create_age() res = string.Template(s).safe_substitute(substs) return res
def FIcompare(folder, cells, currents = [], freqs = [],\ firing_rate_data = 'firing_rate_data.txt'): ''' f = FIcompare(folder, cells, currents = [], freqs = [],\ firing_rate_data = 'firing_rate_data.txt'): Plot current clamp firing traces with certain currents input and with firing frequencies in a certain range. parameters: folder (string) - directory to the folder with raw data cells (array_like) - indices of neurons to plot currents (array_like) - list of input currents freqs (list) - of two scalars, range of the firing rates to be included firing_rate_data (string) - firing rate data file directory return: f (list) - list of figure windows ''' data = util.read_dict(firing_rate_data, 'int') f = [] for cell in cells: for trial, stim, fr in zip(*data[cell][1]): if (len(currents) == 0 or stim in currents) and \ (len(freqs) == 0 or (freqs[0] <= fr and fr < freqs[1])): trace, sr, st = util.load_wave(folder + util.gen_name(cell, trial)) f.append(plot.plot_trace_v(trace, sr)) f[-1].setWindowTitle('Cell {0:d}, Trial {1:d}, I = {2:.2e}'.\ format(cell, trial, st[2])) return f
def main(fname): data = data_by_numtyped(fname) fig,ax1 = plt.subplots() #add figsize? draw_violin(data, color='royalblue') ## add a light-colored horizontal grid ax1.yaxis.grid(True, linestyle='-', which='major', color='lightgrey', alpha=0.5) ## Shortest-path line path = shortestpath.least_sum_path(fname) plt.plot(range(1,1+len(path)), runtimes_of_path(fname, path), color='r', linestyle="--", linewidth="1") ## plot axis: runtime + num types ax1.set_axisbelow(True) ax1.set_title(fname.rsplit("/",1)[-1].rsplit(".")[0]) ax1.set_xlabel("Num. Typed Modules") ax1.set_ylabel("Runtime (ms)") plt.xticks(range(1,1+len(data)), range(0, len(data))) ## Legend # Reset y limit ymin,ymax = ax1.get_ylim() ax1.set_ylim(ymin-5, ymax) plt.figtext(0.80, 0.04, "---", color='r', weight='roman', size='x-small') plt.figtext(0.82, 0.04, "Least-sum path", color='k', weight='roman', size='x-small') plt.figtext(0.80, 0.01, '*', color='white', backgroundcolor='royalblue',weight='roman', size='medium') plt.figtext(0.82, 0.01, ' Average Value', color='black', weight='roman', size='x-small') ## Save & clear new_name = util.gen_name(fname, "violin", "png") plt.savefig(new_name) plt.clf() print("Saved figure to %s" % new_name) return
def browse(self, row = 3, col = 3): ''' Plot the raw traces grouped close to each other based on the groups of cells parameters: row (int) - number of rows of subplots col (int) - number of columns of subplots return: f (list) - list of image windows ''' f = [] for t in np.unique(self.data['group']): cell_ind = self.data.index[np.nonzero(self.data['group'] == t)] ft = [] counter = 0 for c in cell_ind: for trial in self.trial_data[c, :, :]: trace, sr, stim = util.load_wave(self.folder + \ util.gen_name(self.data['No'][c], trial[0])) if not len(trace): continue sub_num = counter - row * col * (len(ft) - 1) if row * col <= sub_num: ft.append(pg.GraphicsWindow(title = \ 'Image {:d}'.format(len(ft) + 1) + ' in ' + t)) sub_num -= row * col axis = ft[-1].addPlot(int(sub_num / col), np.mod(sub_num, col)) axis.setTitle('Cell {0:d}, rate = {1:.0f} Hz, I = {2:.0f} pA'.format(\ self.data['No'][c], trial[1], trial[2] * 1e12)) plot.plot_trace_v(trace, sr, ax = axis) counter += 1 f.extend(ft) return f
def calc_sahp(self, baseline_win, lat, win, output_file='slow_ahps.txt'): ''' Calculate the slow ahp in chosen trials. In current clamp AP trains, slow AHP is defined as the hyperpolarization at lat time point after the end of the current step relative to the baseline before the current step. Try to avoid the Ih current. Parameters: baseline_win (float) - window size of baseline ahead of the current step lat (float) - latency of sAHP measurement point after the current step win (float) - window size of the sAHP measurement Return: mean_ahps (array_like) - mean slow ahps for each cell ''' cell_num = self.trial_data.shape[0] trial_num = self.trial_data.shape[1] ahps = np.zeros((cell_num, trial_num)) for c in range(cell_num): for t in range(trial_num): cind = self.data['No'][c] tind = self.trial_data[c][t][0] if tind != 0: trace, sr, stim = util.load_wave(self.folder + \ util.gen_name(cind, tind)) baseline = np.mean(trace[int((stim[0] - baseline_win) * sr):\ int(stim[0] * sr)]) ahps[c][t] = baseline - np.mean(trace[int((stim[0] + stim[1] + lat) * sr):\ int((stim[0] + stim[1] + lat + win) * sr)]) util.write_arrays(output_file, ['sahp', ahps]) mean_ahps = np.zeros(cell_num) for i in range(cell_num): mean_ahps[i] = np.mean(ahps[i][np.nonzero(ahps[i])]) return mean_ahps
def calc_v(self, win, output_file='vhold.txt'): ''' ave_vhold = calc_v(self, output_file = 'vhold.txt'): Calculate holding membrane potential before the spike train. parameter: win (float) - window size before the depolarization to average. output_file (string) - output file directory. return: ave_vhold (array_like) - averaged holding potential for each neuron ''' cell_num = self.trial_data.shape[0] trial_num = self.trial_data.shape[1] vhold = np.zeros((cell_num, trial_num)) for c in range(cell_num): for t in range(trial_num): cind = self.data['No'][c] tind = self.trial_data[c][t][0] if tind != 0: trace, sr, stim = util.load_wave(self.folder + \ util.gen_name(cind, tind)) vhold[c, t] = np.mean(trace[int((stim[0] - win) * sr):\ int(stim[0] * sr)]) util.write_arrays(output_file, ['vhold', vhold]) ave_vhold = np.zeros(cell_num) for i in range(cell_num): ave_vhold[i] = np.mean(vhold[i][np.nonzero(vhold[i])]) return ave_vhold
def make_plot(fname, edge_list): # `edge_list` is a list of edges to check # Compare performance when these edges are not boundaries vs. # when any one edge is a boundary fnames = "+".join(("-".join(util.infer_module_names(fname, *edge)) for edge in edge_list)) data_with = violinplot.data_by_numtyped(fname, lambda cfg: any((util.is_boundary(cfg, edge) for edge in edge_list))) data_without = violinplot.data_by_numtyped(fname, lambda cfg: all((not util.is_boundary(cfg, edge) for edge in edge_list))) data_with, data_without, posns = remove_empty(data_with, data_without) ## Make sure data's worth graphing if do_not_make_graph(data_with, data_without, fnames): return ## Draw violins fig,ax1 = plt.subplots() #add figsize? violinplot.draw_violin(data_with, alpha=0.8, color='royalblue', meanmarker='*', positions=posns) violinplot.draw_violin(data_without, alpha=0.8, color='darkorange', meanmarker='o', positions=posns) ## add a light-colored horizontal grid ax1.yaxis.grid(True, linestyle='-', which='major', color='lightgrey', alpha=0.5) ## Legend ymin,ymax = ax1.get_ylim() ax1.set_ylim(ymin-5, ymax) plt.figtext(0.80, 0.04, "-", color='royalblue', backgroundcolor='royalblue', weight='roman', size='x-small') plt.figtext(0.82, 0.04, "Any edge is a boundary", color='k', weight='roman', size='x-small') plt.figtext(0.80, 0.01, '-', color='darkorange', backgroundcolor='darkorange', weight='roman', size='x-small') plt.figtext(0.82, 0.01, 'No edges are boundaries', color='black', weight='roman', size='x-small') ax1.set_axisbelow(True) ax1.set_title("%s=%s" % (fname.rsplit("/",1)[-1].rsplit(".")[0], fnames)) ax1.set_xlabel("Num. Typed Modules") ax1.set_ylabel("Runtime (ms)") plt.xticks(posns, posns) new_name = util.gen_name(fname, "edge=%s" % fnames, "png") plt.savefig(new_name) plt.clf() plt.close(fig) print("Saved figure to %s" % new_name) return
def py(self): gen = self.p.genname x = gen_name() yield self.line("%s = self.__kj__.collect(%s())" % (gen, gen)) yield self.line( 'for %s in self.__kj__.render_attrs({%r:%s}, %r):' % (x, self.p.attr, gen, self.p.mode)) yield self.line(' yield %s' % x)
def align_ahp(self, spikes = [0], max_trace = 30, param_file = 'param_spike_detect'): ''' Align and plot specified action potentials from chosen trials in the same graphs. parameters: spikes (list) - indices of spikes to plot, 0 is the first max_trace (int) - maximum number of traces in each graph, not to be too crowded param_file (String) - directory of spike detection parameter file return: f (list) - list of image windows ''' # Traverse all the trials, find the spikes and store the trace, time range # of the spikes and the type of the cell which the trace belong to in # separated lists sequentially traces = [[] for d in range(len(spikes))] limits = [[] for d in range(len(spikes))] cell_types = [[] for d in range(len(spikes))] spike_params = ap.get_params(param_file) for c in self.data.index: for trial in self.trial_data[c, :, :]: trace, sr, stim = util.load_wave(self.folder + \ util.gen_name(self.data['No'][c], trial[0])) if sr > 0: starts = ap.spike_detect(trace, sr, spike_params) # start of all spikes for i, spike in enumerate(spikes): if spike < len(starts) - 1: traces[i].append(trace) cell_types[i].append(self.data['group'][c]) limits[i].append([starts[spike], starts[spike + 1]]) f = [] for i, spike in enumerate(spikes): types = np.sort(np.unique(cell_types[i])) image_num = int(np.ceil(len(cell_types[i]) / max_trace)) # number of images fs = [pg.GraphicsWindow(title = 'Image {0:d} in spike {1:d}'.format(d, spike)) \ for d in range(image_num)] ax = [d.addPlot(0, 0) for d in fs] cl = [pg.intColor(i, hues = len(types)) for i in range(len(types))] # colors # Add legend to the plotItems lgit = [pg.PlotDataItem(pen = cl[d]) for d in range(len(cl))] for ax_ in ax: lg = ax_.addLegend() for t, l in zip(types, lgit): lg.addItem(l, t) group_max = [] # maximum number of traces in one image for each groups for t in types: group_max.append(np.ceil(np.count_nonzero(np.array(cell_types[i]) == t) \ / image_num)) group_trial = [0 for d in range(len(types))] # keep track of trials # plotted in each groups for j in range(len(cell_types[i])): _group = np.nonzero(types == cell_types[i][j])[0][0] plot.plot_trace_v(traces[i][j], 1, win = limits[i][j], ax = \ ax[int(np.floor(group_trial[_group] / group_max[_group]))], \ shift = [-limits[i][j][0], -traces[i][j][limits[i][j][0]]], \ cl = cl[_group]) group_trial[_group] += 1 f.extend(fs) return f
def firing_rate(folder, cells, trials = [], out = 'firing_rate_data.txt', \ param_file = 'param_spike_detect'): ''' data = firing_rate(folder, cells, trials = [], data_out = 'firing_rate_data.txt') Measure firing rate data and output in two forms, one with firing rate of the same current input averaged and the other with raw firing rate for each trial. output a dictionary with each element having data for one cell, the key is the cell index. data = {cell_idx: [[[stims], [averaged firing rate]], [[trial indices], [stims], [firing rate]]]} parameters: folder (String) - directory to the folder of the trace data files cells (array_like) - indices of cells to analyze trials (array_like) - trial numbers, if not provided analyze all the trials in the folder out (String) - output data file directory param_file (String) - spike detection parameter file return: data (dictionary) - firing rate data ''' data = {} params = get_params(param_file) for cell in cells: print('Cell: ', cell) ctrials = trials[:] _stims = [] _rates = [] if not len(ctrials): data_files = os.listdir(folder) for data_file in data_files: matched = re.match('Cell' + \ '_{:04d}_0*([1-9][0-9]*)\.ibw'.format(int(cell)), data_file) if matched: ctrials.append(int(matched.group(1))) for trial in ctrials: file_dir = folder + os.sep + util.gen_name(cell, trial) trace, sr, stim_i = util.load_wave(file_dir) _stims.append(stim_i[2]) _rates.append(len(spike_detect(trace, sr, params, stim_i[1], \ stim_i[0] + stim_i[1]))) raw = [ctrials, _stims, _rates] _stims = np.array(_stims) _rates = np.array(_rates) stims = np.unique(_stims) ave = np.zeros(len(stims)) for i, current in enumerate(stims): ave[i] = sum(_rates[np.nonzero(_stims == current)[0]]) / \ len(np.nonzero(_stims == current)[0]) ave_data = [stims.tolist(), ave.tolist()] data[cell] = [ave_data, raw] util.write_dict(out, data) return data
def save_file(data, fname, tag): # Save data to a file, using fname and tag to create a hopefully-unique name. new_name = util.gen_name(fname, tag, "tab") with open(new_name, "w") as f: f.write("Avg.Runtime\tKeys\n") for bkt in ((x for x in data if x["data"])): f.write(str(int(bkt["mean"]))) f.write("\t") f.write("\t".join([obj["key"] for obj in bkt["data"]])) f.write("\n") print("Saved file to '%s'" % new_name) return new_name
def py(self): x = gen_name() def _body(): yield self.line( 'for %s in self.__kj__.render_attrs(%s, %r):' % (x, self.attrs, self.mode)) yield self.line(' yield %s' % x) if self.guard: yield self.line('if %s:' % self.guard) for l in _body(): yield l.indent() else: for l in _body(): yield l
def main(fname): data = data_by_numtyped(fname) ## Add data fig, ax1 = plt.subplots() # add figsize? bp = plt.boxplot(data, notch=0, sym="+", vert=True, whis=1) plt.setp(bp["boxes"], color="black") plt.setp(bp["whiskers"], color="black") plt.setp(bp["fliers"], color="red", marker="+") ## add a light-colored horizontal grid ax1.yaxis.grid(True, linestyle="-", which="major", color="lightgrey", alpha=0.5) ## Fancier boxes for i in range(len(data)): box = bp["boxes"][i] coords = [] ## Color the boxes for j in range(0, 5): coords.append((box.get_xdata()[j], box.get_ydata()[j])) boxPolygon = Polygon(coords, facecolor="royalblue") ax1.add_patch(boxPolygon) ## Re-draw median lines med = bp["medians"][i] mx, my = [], [] for j in range(2): mx.append(med.get_xdata()[j]) my.append(med.get_ydata()[j]) plt.plot(mx, my, "black") ## Draw avg. dot plt.plot([np.average(med.get_xdata())], [np.average(data[i])], color="w", marker="*", markeredgecolor="k") ## Shortest-path line path = shortestpath.least_sum_path(fname) plt.plot(range(1, 1 + len(path)), runtimes_of_path(fname, path), color="r", linestyle="--", linewidth="1") ## plot axis: runtime + num types ax1.set_axisbelow(True) ax1.set_title(fname.rsplit("/", 1)[-1].rsplit(".")[0]) ax1.set_xlabel("Num. Typed Modules") ax1.set_ylabel("Runtime (ms)") ax1.set_xticklabels(range(0, len(path))) ## Legend # Reset y limit ymin, ymax = ax1.get_ylim() ax1.set_ylim(ymin - 5, ymax) plt.figtext(0.80, 0.04, "---", color="r", weight="roman", size="x-small") plt.figtext(0.82, 0.04, "Least-sum path", color="k", weight="roman", size="x-small") plt.figtext(0.80, 0.01, "*", color="white", backgroundcolor="royalblue", weight="roman", size="medium") plt.figtext(0.82, 0.01, " Average Value", color="black", weight="roman", size="x-small") ## Save & clear new_name = util.gen_name(fname, "boxplot", "png") plt.savefig(new_name) plt.clf() print("Saved figure to %s" % new_name) return
class ManaPotion(Potion): base_price = 130 common = 5 unided_name = util.gen_name( 'potion', check_unique=potion_names).capitalize() + ' potion' name = 'Healing potion' description_past = 'Potion will restore depleted mana when quaffed' description_present = 'This potion grants temporaly boost to mental activity, which results in quick MP restoration' description_future = 'You have no idea about the effect of this liquid.' def quaff(self, player): super(HealingPotion, self).quaff(player) player.hp = util.cap(player.hp + util.roll(2, 10, 1), player.base_hp) gl.message('You feel somewhat better')
class HealingPotion(Potion): base_price = 100 common = 5 unided_name = util.gen_name( 'potion', check_unique=potion_names).capitalize() + ' potion' name = 'Healing potion' description_past = 'Brewed by some artful alchemist this potion heal minor wound of those who drink it.' description_present = 'Strange potion that seems to heal your wounds as soon as you quaff it' description_future = 'Unknow chemical substance that has regenerative effect when quaffed' def quaff(self, player): super(HealingPotion, self).quaff(player) player.hp = util.cap(player.hp + util.roll(2, 10, 1), player.base_hp) gl.message('You feel somewhat better')
def calc_mahp(self, lat_win, spk_win, param_file = 'param_spike_detect', \ output_file = 'medium_ahps.txt'): ''' Calculate medium ahps in chosen trials after spikes in the spk_win. mAHPs are defined as hyperpolarization peak value between two spikes relative to the spiking threshold of the prior spike, excluding the fast AHPs range right after the prior spike. Choose spikes to avoid Ih current and sAHP. Parameters: lat_win (array_like) - 2 scalars, mininum latency for mAHP, window for fAHP, and minimum interspike interval to include ahp. spk_win (array_like) - 2 scalars, begin and end of the window of spikes param_file (String) - directory to parameter files for spike detection output_file (String) - dirctory to output file of medium ahp data of all spikes Returns: mean_ahps (array_like) - 1D array of mean ahps for each cell ''' cell_num = self.trial_data.shape[0] trial_num = self.trial_data.shape[1] spike_num = spk_win[1] - spk_win[0] ahps = np.zeros((cell_num, trial_num, spike_num)) spike_params = get_params(param_file) for c in range(cell_num): for t in range(trial_num): cind = self.data['No'][c] tind = self.trial_data[c][t][0] if tind != 0: trace, sr, stim = util.load_wave(self.folder + \ util.gen_name(cind, tind)) starts = spike_detect(trace, sr, spike_params) # start of all spikes for s in range(spike_num): if spk_win[0] + s < len(starts): # only calculate if it is not the last spike if sr * lat_win[1] < \ starts[spk_win[0] + s + 1] - starts [spk_win[0] + s]: # and the ISI is larger that the minumum threshold ahps[c][t][s] = trace[starts[spk_win[0] + s]] - \ np.min(trace[(starts[spk_win[0] + s] + \ int(sr * lat_win[0])):starts[spk_win[0] + s + 1]]) else: print('Irregular trial: cell', cind, ',trial', tind) util.write_arrays(output_file, ['mahp', ahps]) mean_ahps = np.zeros(cell_num) for i in range(cell_num): mean_ahps[i] = np.mean(ahps[i][np.nonzero(ahps[i])]) return mean_ahps
def create_city(birth = False, city=False, depart = False): name = util.gen_name('city', world.cities_names) if birth: world.birth_place = name if city: world.birth_place_rank = util.roll(2, 3, 3) else: world.birth_place_rank = util.roll(1, 3, 1) if depart: world.depart_place = name world.current_city = name if city: world.depart_place_rank = util.roll(1, 3, 3) else: world.depart_place_rank = util.roll(1, 3) return name
def get_spike_time(self, param_file='param_spike_detect'): ''' Calculate the spike start time for all the chosen traces. Parameters: param_file (string) - directory to spike detection parameter file ''' spike_params = get_params(param_file) cell_num = self.trial_data.shape[0] trial_num = self.trial_data.shape[1] self.starts = [] # start times of all spikes in all trials for c in range(cell_num): _starts = [] for t in range(trial_num): cind = self.data['No'][c] tind = self.trial_data[c][t][0] if tind != 0: trace, sr, stim = util.load_wave(self.folder + \ util.gen_name(cind, tind)) _starts.append(spike_detect(trace, sr, spike_params)) self.starts.append(_starts)
def save_graph(g, fname, tag, edge_labels=None, edge_widths=None): new_name = util.gen_name(fname, tag, "png") plt.title(new_name.rsplit(".", 1)[0].rsplit("/", 1)[-1]) pos = gen_positions(g, tag, edge_labels) ## draw nodes & labels nx.draw_networkx_nodes(g, pos, node_color="b", node_size=1000, alpha=0.6) node_labels = dict(( (str(node), str(node) ) for node in g.nodes_iter())) nx.draw_networkx_labels(g, pos, node_labels) ## draw edges (optionally with widths) if edge_widths is not None: nx.draw_networkx_edges(g, pos, edge_color='k', alpha=0.5, arrows=False, width=edge_widths) else: nx.draw_networkx_edges(g, pos, edge_color='k', alpha=0.5, arrows=False) ## draw edge labels if edge_labels is not None: nx.draw_networkx_edge_labels(g, pos, edge_labels, label_pos=0.2) ## Save figure plt.axis("off") plt.savefig(new_name) plt.clf() print("Saved graph to '%s'" % new_name) return new_name
def save_path(fname, g, path): ## Save a picture of the graph g, with `path` highlighted new_name = util.gen_name(fname, "dijkstra", "png") positions, max_x = positions_of_file(fname) p = dict([(n, positions[n]) for n in g.nodes()]) ## Add labels title_str = fname.rsplit(".", 1)[0].rsplit("/", 1)[-1] plt.title("%s\n\n%s" % (title_str, "\n\n".join(path[::-1])), loc="left", position=(0, 0)) keylen = len(str(path[0])) ## Draw graph pos = nx.spring_layout(g, pos=p, fixed=g.nodes()) nx.draw(g, pos, node_color="k", font_size=80) ## draw path in RED path_edges = [x for x in zip(path, path[1:])] nx.draw_networkx_nodes(g, pos, nodelist=path, node_color="r") nx.draw_networkx_edges(g, pos, edgelist=path_edges, edge_color="r", width=2, arrows=False) ## Save plot plt.axis("equal") plt.savefig(new_name) plt.clf() print("Saved graph to '%s'" % new_name) return new_name
def align(self, baseline_win = 0.2, max_trace = 30): ''' Plot the chosen trials in the same graphs, aligned to baseline and colored based on groups parameters: baseline_win (float) - baseline window size before the start of the stimulation max_trace (int) - maximum number of traces in each graph retrun: f (list) - list of image windows ''' types = np.sort(np.unique(self.data['group'])) trial_num = np.count_nonzero(self.trial_data[:, :, 0]) # trial_num = self.trial_data.shape[0] * self.trial_data.shape[1] image_num = int(np.ceil(trial_num / max_trace)) # number of images f = [pg.GraphicsWindow(title = 'Image {0:d}'.format(d)) for d in range(image_num)] ax = [d.addPlot(0, 0) for d in f] cl = [pg.intColor(i, hues = len(types)) for i in range(len(types))] # colors # Add legend to the plotItems lgit = [pg.PlotDataItem(pen = cl[d]) for d in range(len(cl))] for ax_ in ax: lg = ax_.addLegend() for t, l in zip(types, lgit): lg.addItem(l, t) counter = 0 for c in self.data.index: for t in self.trial_data[c]: if t[0] == 0: break trace, sr, stim = util.load_wave(self.folder + \ util.gen_name(self.data['No'][c], t[0])) if sr > 0: _group = np.nonzero(types == self.data['group'][c])[0][0] plot.plot_trace_v(trace, sr, ax = \ ax[int(np.floor(counter / max_trace))], shift = \ [0, -np.mean(trace[int(stim[0] - baseline_win):int(stim[0])])], \ cl = cl[_group]) counter += 1 return f
def calc_slope(self, spk_win, output_file='ap_slope.txt'): ''' ave_slopes = calc_slope(self, spk_win, output_file = 'ap_slope.txt'): Caclulate largest slope of the rising of an action potential Parameters spk_win (array_like) - 2 scalars, begin and end of the window of spikes Return ave_slopes (array_like) - average slopes for each cell ''' cell_num = self.trial_data.shape[0] trial_num = self.trial_data.shape[1] spike_num = spk_win[1] - spk_win[0] slopes = np.zeros((cell_num, trial_num, spike_num)) for c in range(cell_num): for t in range(trial_num): cind = self.data['No'][c] tind = self.trial_data[c][t][0] if tind != 0: starts = self.starts[c][t] trace, sr, stim = util.load_wave(self.folder + \ util.gen_name(cind, tind)) for s in range(spk_win[0], spk_win[1]): if s < len(starts) - 1: peak_point = np.argmax(trace[starts[s]:starts[s + 1]]) slopes[c, t, s - spk_win[0]] = np.max(np.diff(trace[starts[s]: \ starts[s] + peak_point])) * sr elif s == len(starts) - 1: peak_point = np.argmax(trace[starts[s]:\ int((stim[0] + stim[1]) * sr)]) slopes[c, t, s - spk_win[0]] = np.max(np.diff(trace[starts[s]: \ starts[s] + peak_point])) * sr util.write_arrays(output_file, ['slopes', slopes]) ave_slopes = np.zeros(cell_num) for i in range(cell_num): ave_slopes[i] = np.mean(slopes[i][np.nonzero(slopes[i])]) return ave_slopes
def save_graph(data, fname, tag): # `data` are the groups (by ID) # `fname` is the raw data file # `tag` helps create a uniquely-named output file new_name = util.gen_name(fname, tag, "png") plt.xlabel("Avg. Runtime (ms)", ) plt.ylabel("Num. Configs") plt.title(new_name.rsplit(".", 1)[0].rsplit("/", 1)[-1]) num_configs = util.count_lines(fname) - 1 bin_width = data[0]["max"] - data[0]["min"] # Plot bins for bkt in data: # x-position is average runtime, height is count plt.bar(bkt["min"], bkt["count"],# / num_configs, alpha=0.6, bottom=0, # label="%s - %s" % (bkt["min"], bkt["max"]), width=bin_width) # Add median line (take median of alread-computed rows' medians) md = statistics.median([bkt["median"] for bkt in data if bkt["median"] is not None]) plt.axvline(md, color='k', linestyle="solid", linewidth=5, label="median = %s" % int(md)) # Add UNTYPED & TYPED configuration line arbitrary_key = next((bkt["data"][0]["key"] for bkt in data if bkt["data"])) untyped = statistics.mean(get_row(fname, "".join(("0" for _ in arbitrary_key)))) typed = statistics.mean(get_row(fname, "".join(("1" for _ in arbitrary_key)))) plt.axvline(untyped, color='g', linestyle='dashed', linewidth=5, label="untyped = %s" % int(untyped)) plt.axvline(typed, color='r', linestyle='dotted', linewidth=5, label="typed = %s" % int(typed)) plt.xticks(sorted([bkt["min"] for bkt in data] + [data[-1]["max"]]))#, rotation="vertical") plt.xlim(xmax=data[-1]["max"]) #plt.ylim(ymax=0.5) #50% lgd = plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.) plt.savefig(new_name,bbox_extra_artists=(lgd,), bbox_inches='tight') print("Saved plot to '%s'" % new_name) plt.clf() return new_name
world.quest_givers = filter( lambda x: x.__class__ in QuestNPC.quest_giver_NPC, result) #now let's roll for immobile NPCs. we don't want many of them. let em be 0-3 at 50% chance for now immobile_npc = 0 if util.coinflip(): to_roll = util.roll(*IMMOBILE_NPC_ROLL) for i in range(0, to_roll): immobile_npc += util.coinflip() #for now let's place immobile NPCs together with main NPCs for i in xrange(0, immobile_npc): world.mNPC.append(ImmobileNPC()) unique_npc = util.roll(*UNIQUES_ROLL) #now let's generate a king - why not? king = KingNPC() world.king = king king.name = util.gen_name(check_unique=world.npc_names) logger.debug('Generated king %s' % (king.name)) for x in range(2, util.roll(2, 3, 1)): guard = GoodNPC() guard.name = util.gen_name(check_unique=world.npc_names) king.guards.append(guard) crown = acquire.acquire_armor(king, world.artefact_names, items.Crown, True) king.became_owner_of(crown) queen = RoyaltyNPC("queen") queen.name = util.gen_name('female', check_unique=world.npc_names) world.royalties.append(queen) heir_count = randrange(0, 3) logger.debug('King will have %d heir/s' % (heir_count)) for x in range(0, heir_count):
world.quest_givers = filter(lambda x: x.__class__ in QuestNPC.quest_giver_NPC, result) #now let's roll for immobile NPCs. we don't want many of them. let em be 0-3 at 50% chance for now immobile_npc = 0 if util.coinflip(): to_roll = util.roll(*IMMOBILE_NPC_ROLL) for i in range(0, to_roll): immobile_npc += util.coinflip() #for now let's place immobile NPCs together with main NPCs for i in xrange(0, immobile_npc): world.mNPC.append(ImmobileNPC()) unique_npc = util.roll(*UNIQUES_ROLL) #now let's generate a king - why not? king = KingNPC() world.king = king king.name = util.gen_name(check_unique=world.npc_names) logger.debug('Generated king %s' % (king.name)) for x in range(2, util.roll(2, 3, 1)): guard = GoodNPC() guard.name = util.gen_name(check_unique=world.npc_names) king.guards.append(guard) crown = acquire.acquire_armor(king, world.artefact_names, items.Crown, True) king.became_owner_of(crown) queen = RoyaltyNPC("queen") queen.name = util.gen_name('female', check_unique=world.npc_names) world.royalties.append(queen) heir_count = randrange(0, 3) logger.debug('King will have %d heir/s'% (heir_count)) for x in range(0, heir_count): heir = RoyaltyNPC("princess")
def create_adventurer(): name = util.gen_name(check_unique=world.npc_names) world.adventurer_names.append(name) return name
def __init__(self, caller, callee, *body): super(CallNode, self).__init__(body) fname = gen_name() self.decl = caller.replace('$caller', fname) self.call = callee.replace('$caller', fname)
def py(self): gen = gen_name() yield self.line('%s = local.__kj__.pop_with()' % gen) for v in self.vars: yield self.line('%s = %s.get(%r)' % (v, gen, v))
def _make_randart(wep, unique=None): wep.randart = True wep.unique_name = util.gen_name('artefact', unique) return 100
def sample_aps(folder, cell_num, trial_num, train_win = [], spike_win = [-0.5e-3, 3e-3], \ freq_range = [], ap_ind = [], type_file = None, ave = False, cl = [], lw = 1, \ units = None, scale = [1, 1], scalebar = [0, 0], interp = 0, fname = 'tmp.png'): ''' sample_aps(folder, cell_num, trial_num, train_win, spike_win = [-0.5e-3, 3e-3], \ freq_range = [], ap_ind = [], type_file = None, ave = False, cl = [], \ fname = 'tmp.png'): plot action potentials of each cells with all the qualified action potentials averaged and depending on the input action potentials of cells in the same groups averages. In the latter case, plot shade standard of error. parameters: folder (String) - directory of the folder with the data cell_num (array_o) - indices of cells to plot trial_num (list) - indices of trails in each cell train_win (list) - of two scalar values for the time window where there are spikes spike_win (list) - of two scalar values for the time windoe for the spikes freq_range (list) - of two scalar, range of frequencies to use ap_ind (list) - index of action potentials in each trial to use type_file (String) - directory of the csv file recording the type of cells, the first column has the cell indices and the last column has the cells' types ave (boolean) - whether to average across cells with same type cl (list) - color traces in each type, use default color sequences if not provided interp (float) - time of number of interpolated points to plot over current number of points scale (list) - 2 scalar element [sx, sy], scaling factor for time and value axis sx and sy respectively. units (list) - 2 string elements list [ux, uy], units after scaling. scalebar (list) - 2 scalar elements list [sx, sy] - scale bar lengh of the two axis. fname ('String') - directory of the file to save the image ''' if folder[-1] != '/': folder += '/' spike_params = ap.get_params('param_spike_detect') spikes = [] for cell in cell_num: print('cell: ', cell) _spikes = 0 count = 0 for trial in trial_num: _trace, _sr, _stim_i = util.load_wave(folder + util.gen_name(cell, trial)) if len(_trace) == 0: continue trace, sr, stim_i = _trace, _sr, _stim_i if len(train_win): spike_start = ap.spike_detect(trace, sr, spike_params, train_win[0], \ train_win[1]) freq = len(spike_start) / (train_win[1] - train_win[0]) else: spike_start = ap.trace_detect(trace, sr, spike_params) freq = len(spike_start) / len(trace) * sr if not (len(freq_range) and (freq < freq_range[0] or freq_range[1] < freq)): if len(ap_ind): for i in ap_ind: pre_ind = max(spike_start[i] + int(spike_win[0] * sr), 0) post_ind = min(spike_start[i] + int(spike_win[1] * sr), len(trace)) single = trace[pre_ind:post_ind] - trace[ spike_start[i]] else: for t in spike_start: pre_ind = max(int((t + spike_win[0]) * sr), 0) post_ind = min(int((t + spike_win[1]) * sr), len(trace)) single = trace[pre_ind:post_ind] - trace[t] if interp: interf = interpolate.interp1d(np.arange(pre_ind, post_ind), \ single, 'cubic') single = interf(np.arange(interp * pre_ind, interp * (post_ind - 1)) \ / interp) _spikes = _spikes + single count += 1 print('spike count: ', count) spikes.append(_spikes / count) spikes = np.array(spikes) f = plt.figure() ax = f.add_subplot(111) ax.axis('off') if interp: xt = np.arange(spikes.shape[1]) / sr / interp else: xt = np.arange(spikes.shape[1]) / sr if type_file != None: type_data = util.read_csv(type_file) type_data = type_data[np.nonzero(type_data[:, [0]] == \ np.ones((type_data.shape[0], 1)) * np.array(cell_num))[0], :] types = np.unique(type_data[:, -1]) if len(cl) != len(types): ncolors = len(types) cm = plt.get_cmap('gist_rainbow') cl = [cm(1 * i / ncolors) for i in range(ncolors)] if ave: for i, t in enumerate(types): group_spikes = spikes[np.nonzero(type_data[:, -1] == t)[0], :] m = np.mean(group_spikes, 0) se = np.std(group_spikes, 0) / np.sqrt(group_spikes.shape[0]) # sd = np.std(group_spikes, 0) ax.plot(xt, m, color=cl[i], lw=lw) ax.fill_between(xt, m - se, m + se, facecolor = cl[i], edgecolor = 'none', \ alpha = 0.3) else: for i, cspikes in enumerate(spikes): ax.plot(xt, cspikes, color = cl[np.nonzero(types == type_data[i, -1])[0][0]], \ lw = lw) else: ax.plot(xt, spikes.T, lw=lw) if units is not None: xscale_text = '{0:d} '.format(scalebar[0]) + units[0] yscale_text = '{0:d} '.format(scalebar[1]) + units[1] else: yscale_text, xscale_text = '', '' xscalebar = scalebar[0] / scale[0] yscalebar = scalebar[1] / scale[1] sc = [xt[-1] - xscalebar * 0.5, ax.get_ylim()[0] - yscalebar * 0.5] # coordinate of top right corner of scale bar if yscalebar != 0: yscale = ax.plot([sc[0] - xscalebar] * 2, [sc[1] - yscalebar, sc[1]], \ color = [0, 0, 0, 1], linewidth = 2.0) t2 = ax.text(sc[0] - xscalebar * 1.01, sc[1] - yscalebar * 0.8, \ yscale_text, ha = 'right', va = 'bottom', rotation = 'vertical', size = 20) if xscalebar != 0: xscale = ax.plot([sc[0] - xscalebar, sc[0]], [sc[1] - yscalebar] * 2, \ color = [0, 0, 0, 1], linewidth = 2.0) t1 = ax.text(sc[0] - xscalebar * 0.8, sc[1] - yscalebar * 1.05, \ xscale_text, va = 'top', size = 20) f.set_size_inches(2, 7) f.savefig(fname, dpi=200, transparent=True) plt.close(f) del (f) return 0
def create_enemy(): name = util.gen_name() world.antagonist = name return name