def traces(): from lever.script.steps import log from algorithm.utils import quantize index = 1 cluster = get_result(mice.name.to_list()[index:index + 1], [res_cluster]) trials = get_result(mice.name.to_list()[index:index + 1], [log.res_trial_log]) result = list() for trial, y in zip(trials[0].values, quantize(cluster[0])): for idx, item in enumerate(trial): result.append((item, y, idx)) df = pd.DataFrame(result, columns=('value', 'cluster', 'time')) df.to_csv(proj_folder.joinpath("data", "analysis", "cluster_traces.csv"))
def example_curve(): xy, predicts = get_result([x.name for x in mice[1:2]], [res_align_xy, res_predict], "astrocyte") with Figure() as ax: ax = ax[0] ax.plot(xy[0][1].values, color='blue') ax.plot(predicts[0], color='orange')
def check_data_size(): neurons = get_result(mice.name.to_list(), [trial_neuron.res_trial_neuron])[0] res = pd.DataFrame([x.shape[0:2] for x in neurons], index=mice.index, columns=["x", "y"]).join(grouping, how="inner") print(res.groupby("group").aggregate(sum))
def check_length(): behavior = get_result(mice.name.to_list(), [res_behavior], "check_length")[0] for (_, speed, delay, hit_rate, _), mouse in zip(behavior, mice.name): print( f"case: {mouse}, \tdelay: {len(delay)}, \thitlen: {len(hit_rate)}, \thit: {hit_rate.sum()}," f"\tspeed: {len(speed)}")
def check_predictor_size(): predictors = get_result(mice.name.to_list(), [res_predictor])[0] lookup = grouping.join(mice.set_index(["id", "session" ])).set_index("name").sort_index() res = {"wt": [], "dredd": [], "glt1": [], "gcamp6f": []} for (_, mouse), predictor in zip(mice.iterrows(), predictors): if mouse['name'] in lookup.index: res[lookup.loc[mouse['name'], "group"]].append(predictor[1].shape[0:2]) res = {key: np.sum(value, axis=0) for key, value in res.items()}
def main(): trial_neurons = get_result([x.name for x in mice][0:1], [res_trial_neuron], 'trial-neuron-2s-run') values = trial_neurons[0][0].values with Figure(fig_folder.joinpath("classifier", "example-neurons.svg"), show=True) as axes: for id_neuron, neuron in enumerate(values[:20, 0:4, :]): for id_trial, trial in enumerate(neuron): axes[0].plot(range(id_trial * 11, id_trial * 11 + 10), trial / trial.max() * 5 + id_neuron * 6, color='red')
def save_example(): xy, predicts = get_result([x.name for x in mice[1:2]], [res_align_xy, res_predict], "astrocyte") dataframe = list() for (idx, trace), predict in zip(enumerate(xy[0][1].values), predicts[0]): dataframe.append((idx / 5.0, trace, predict)) df = pd.DataFrame(dataframe, columns=["time", "trajectory", "predicted"]) df.to_csv(proj_folder.joinpath("data", "analysis", "decoder_example.csv")) with Figure() as ax: ax = ax[0] ax.plot(df["time"], df["trajectory"], color="blue") ax.plot(df["time"], df["predicted"], color="orange")
def composite_trial(): mice_df = pd.DataFrame([[x.id, x.fov, x.session, x.name] for x in mice], columns=["id", "fov", "session", "name"]) mice_df = mice_df.set_index(["id", "fov", "session"]).sort_index() choices = [ mice_df.loc[x[0], :, x[1]].name.values[0] for x in (('19286', 1), ('14029', 8), ('27215', 13)) ] xy, predicts = get_result(choices, [res_trial_xy, res_predict_trial], "astrocyte") example_select = { 'wt': ((-6, 6), ((0, 1500), (680, 730))), 'glt1': ((-3, 6), ((0, 1500), (120, 170))), 'dredd': ((-2, 7), ((0, 1500), (330, 380))) } fig = plt.figure(figsize=(9, 12)) gs = fig.add_gridspec(3, 3) color_indices = [3, 1, 2] for idx, (key, value) in enumerate(example_select.items()): ax = fig.add_subplot(gs[idx, 0]) ax.plot(scale_features(xy[idx][1].values), color=colors[0], alpha=0.7, linewidth=1) ax.plot(scale_features(predicts[idx]), color=colors[color_indices[idx]], alpha=0.7, linewidth=1) ax.set_ylim(*value[0]) ax.set_xlim(*value[1][0]) ax.axes.get_yaxis().set_visible(False) ax2 = fig.add_subplot(gs[idx, 1:3]) ax2.plot(scale_features(xy[idx][1].values), color=colors[0], alpha=0.7, linewidth=2) ax2.plot(scale_features(predicts[idx]), color=colors[color_indices[idx]], alpha=0.7, linewidth=2) ax2.set_ylim(*value[0]) ax2.set_xlim(*value[1][1]) ax2.axes.get_yaxis().set_visible(False) add_scalebar(ax, (500, 5, 1000, 5.4), 1) add_scalebar(ax2, (360, 5, 370, 5.4), 1) plt.tight_layout(0) plt.savefig(fig_folder.joinpath("decoder-example-trial.svg")) plt.show()
def example_validation(xy, predicts): xy, predicts = get_result([x.name for x in mice[1:2]], [res_align_xy, res_predict], "astrocyte") with Figure(fig_folder.joinpath("decoder_validation.svg"), (9, 6)) as ax: ax = ax[0] ax.plot(np.arange(1800), xy[0][1].values[0:1800], color='blue') ax.plot(np.arange(1800, 2100), xy[0][1].values[1800:2100] - 5.0, color='blue') ax.plot(np.arange(2100, 3000), xy[0][1].values[2100:3000], color='blue') ax.plot(np.arange(1800, 2100), predicts[0][1800:2100] - 10.0, color='orange') for idx, neuron in enumerate(xy[0][0].values[:20, :]): scaled = neuron * 2 / neuron.max() ax.plot(np.arange(1800), scaled[:1800] + 5.0 + idx, color='red') ax.plot(np.arange(1800, 2100), scaled[1800:2100] + 10.0 + idx, color='red') ax.plot(np.arange(2100, 3000), scaled[2100:3000] + 5.0 + idx, color='red')
def main(): result = get_result(mice.name.to_list(), [res_behavior], "astrocyte_exp_log")[0] return result
merged.to_csv( proj_folder.joinpath("data", "analysis", "single_power_slope.csv")) def order_slope2_merge(result: List[PieceLinear2]): merged = pd.DataFrame(result, index=mice.set_index(["id", "session"]).index, columns=["x0", "y0", "k0", "k1"]).join(grouping, how="inner") merged.to_csv( proj_folder.joinpath("data", "analysis", "single_power_slope2.csv")) def single_power_merge(result: List[np.ndarray]): pd.DataFrame([x for y in result for x in y], columns=["mutual_info"], index=np.repeat(mice.set_index(['id', 'session']).index, list(map(len, result))))\ .join(grouping, how="inner")\ .to_csv(proj_folder.joinpath("data", "analysis", "single_power.csv")) ## if __name__ == '__main__': names = mice.name.to_list() # result = get_result(names, [res_neuron_info])[0] # single_power_merge(result) # order_slope_merge(get_result(names, [res_single_order])[0]) # order_slope2_merge(get_result(names, [res_single_order2])[0]) merge(get_result(names, [res_decode_power])[0]) merge_trial(get_result(names, [res_decode_power_trial])[0]) ##
def main(): result = get_result(mice.name.to_list(), [res_related_neurons])[0] merge(result)
def main(): from pypedream import get_result result = get_result(mice.name.to_list(), [res_filter_log], "filtered-log")[0] return result
def main(): result = get_result(mice.name.to_list(), [res_classifier_power])[0] merge(result)
append=True).reorder_levels(["group", "id", "session"]) mean.to_csv( proj_folder.joinpath("data", "analysis", "encoding_mean_minimal.csv")) r2s = [x[0] for x in result] res = pd.DataFrame([x for y in r2s for x in y], index=np.repeat(mice.index, [len(x) for x in r2s]), columns=predictor_names) merged = res.join(mice, how="inner").set_index(["id", "session"]).join(grouping, how="inner") merged.to_csv( proj_folder.joinpath("data", "analysis", "encoding_minimal.csv")) def check_predictor_size(): predictors = get_result(mice.name.to_list(), [res_predictor])[0] lookup = grouping.join(mice.set_index(["id", "session" ])).set_index("name").sort_index() res = {"wt": [], "dredd": [], "glt1": [], "gcamp6f": []} for (_, mouse), predictor in zip(mice.iterrows(), predictors): if mouse['name'] in lookup.index: res[lookup.loc[mouse['name'], "group"]].append(predictor[1].shape[0:2]) res = {key: np.sum(value, axis=0) for key, value in res.items()} if __name__ == '__main__': result = get_result(mice.name.to_list(), [res_encoding], "log-encoding")[0] merge(result)
def main(): result = get_result(mice.name.to_list(), [res_align], "alignment")[0] return result
def single_scatter(): powers = get_result(mice.name.to_list(), [res_neuron_info])[0] x0 = np.linspace(0, 40, 240, endpoint=False) labeled_powers = pd.DataFrame(powers, index=mice.index).join(grouping) colors = {"wt": "#619CFFFF", "glt1": "#00BA38FF", "dredd": "#F8766DFF"} models: Dict[str, List[Tuple[PieceLinear3, np.ndarray]]] = { "wt": list(), "dredd": list(), "glt1": list() } for power in labeled_powers: grp_str = power[-1] power = np.array(power[0:-2]) transformed = np.log(np.flip(np.sort(power[np.greater(power, 0)]))) x = np.arange(transformed.shape[0]) model = PieceLinear2.fit( x, transformed, [[10, -8, -np.inf, -np.inf], [np.inf, np.inf, np.inf, 0]]) # model = PieceLinear3.fit(x, transformed, [[0, -np.inf, 10, -np.inf, -np.inf, -np.inf], # [5, np.inf, 40, 0, 0, 0]]) models[grp_str].append((model, transformed)) with Figure( proj_folder.joinpath("report", "fig", "decoder-single-power.svg"), (6, 8)) as axes: ax = axes[0] for grp_str, values in models.items(): color = colors[grp_str] for model, power in values: x = np.arange(len(power)) ax.scatter(x, power, alpha=0.5, s=4, color=color) ax.plot(x0, model.predict(x0), linewidth=0.75, alpha=0.5, color=color) ax.scatter(model.x0, model.y0, marker='x', color=color) # ax.scatter(model.x1, model.y0 + (model.k1 * (model.x1 - model.x0)), marker='x', color=color) ax.set_xlim(0, 40) ax.set_ylim(-8, -2) ax.xaxis.set_ticks([0, 9, 19, 29, 39]) ax.xaxis.set_ticklabels(["1st", "10th", "20th", "30th", "40th"]) ax.set_xlabel("order of neurons in same field of view") ax.set_ylabel("log(mutual information)") rcParams.update({ "axes.labelsize": 10, "xtick.labelsize": 8, "ytick.labelsize": 8 }) with open( proj_folder.joinpath("data", "analysis", "single_power_piecelinear_fit.pkl"), 'wb') as fpb: dump(models, fpb) k0s = {x: np.array([y[0].k0 for y in v]) for x, v in models.items()} with Figure( proj_folder.joinpath("report", "fig", "decoder-single-power-comp.svg"), (4, 8)) as axes: ax = axes[0] sns.boxplot(data=[k0s['dredd'], k0s['glt1'], k0s['wt']], width=0.75, notch=False, whis=1.5, fliersize=0, ax=ax) sns.stripplot(data=[k0s['dredd'], k0s['glt1'], k0s['wt']], color='black', ax=ax) ax.set_ylabel("Slope In Mutual Information\nvs. Neuron ordering")
def composite_example(): mice_df = pd.DataFrame([[x.id, x.fov, x.session, x.name] for x in mice], columns=["id", "fov", "session", "name"]) mice_df = mice_df.set_index(["id", "fov", "session"]).sort_index() choices = [ mice_df.loc[x[0], :, x[1]].name.values[0] for x in (('19286', 1), ('14029', 8), ('27215', 13)) ] xy, predicts, log = get_result(choices, [res_align_xy, res_predict, res_trial_log], "astrocyte") example_select = { 'wt': ((-6, 6), ((0, 1500), (510, 560))), 'glt1': ((-3, 6), ((0, 1500), (80, 130))), 'dredd': ((-2, 7), ((0, 1500), (1000, 1050))) } fig = plt.figure(figsize=(9, 12)) gs = fig.add_gridspec(3, 3) color_indices = [3, 1, 2] for idx, (key, value) in enumerate(example_select.items()): ax = fig.add_subplot(gs[idx, 0]) ax.plot(scale_features(xy[idx][1].values), color=colors[0], alpha=0.7, linewidth=1) ax.plot(scale_features(predicts[idx]), color=colors[color_indices[idx]], alpha=0.7, linewidth=1) ax.set_ylim(*value[0]) ax.set_xlim(*value[1][0]) ax.axes.get_yaxis().set_visible(False) ax2 = fig.add_subplot(gs[idx, 1:3]) ax2.plot(scale_features(xy[idx][1].values), color=colors[0], alpha=0.7, linewidth=2) ax2.plot(scale_features(predicts[idx]), color=colors[color_indices[idx]], alpha=0.7, linewidth=2) trial_start = clip(log[idx].timestamps / log[idx].sample_rate * 5, value[1][1]) motion_onset = clip(log[idx].trial_anchors / log[idx].sample_rate * 5, value[1][1]) ax2.plot(motion_onset, np.full(motion_onset.shape, 1), marker='v', color=colors[0], linestyle='', markersize=16) ax2.plot(trial_start, np.full(trial_start.shape, 1), marker='v', color=colors[0], linestyle='', markersize=16, fillstyle='none') ax2.set_ylim(*value[0]) ax2.set_xlim(*value[1][1]) ax2.axes.get_yaxis().set_visible(False) add_scalebar(ax, (900, 4, 1400, 4.4), 1) add_scalebar(ax2, (1030, 4, 1040, 4.4), 1) plt.tight_layout(0) plt.savefig(fig_folder.joinpath("decoder-example.svg")) plt.show()