def neuron_lever_unsampled(data_file: File, params: Dict[str, float]) -> Tuple[np.ndarray, np.ndarray]: """ Returns: neural_activity, lever """ lever = get_trials(data_file) neurons = fold_by(DataFrame.load(data_file["spike"]), lever, data_file.attrs['frame_rate'], True) neurons, lever = filter_empty_trial_sets(neurons.values, lever.values[0]) mask, filtered = devibrate_trials(lever, params["pre_time"]) return neurons[:, mask, :], lever[mask, :]
def get_linkage(record_file: File, params: Dict[str, float]) -> np.ndarray: """Load record file and get linkage matrix between the trajectories of trials. Args: record_file: the record file with at least the lever file motion_params: param dict including window size and push threshold """ lever = load_mat(record_file['response']) lever.center_on("motion", **params) lever.fold_trials() mask, lever_trials = devibrate_trials(lever.values[0], params['pre_time'], sample_rate=lever.sample_rate) dist_mat = trace_cluster(lever_trials[mask, ...]) return linkage(dist_mat), mask
def run_amp_power(data_file: File) -> Tuple[float, float, float, float]: """Try to decode the max lever trajectory amplitude of each trial. Returns: pre_amp_power: mutual info between predicted (from neuron activity before motor onset) and real amplitude of trials in one session post_amp_power: mutual info between predicted (from neuron activity before motor onset) and real amplitude of trials in one session """ VALIDATE_FOLD = 10 lever = get_trials(data_file, motion_params) neuron = DataFrame.load(data_file['spike']) resampled_onsets = np.rint(lever.trial_anchors * (5 / 256)).astype(np.int_) - 3 folded = np.stack( [take_segment(trace, resampled_onsets, 6) for trace in neuron.values]) mask, filtered = devibrate_trials(lever.values, motion_params['pre_time']) mask &= np.any(folded > 0, axis=(0, 2)) amp = filtered[mask, 25:64].max(axis=1) - filtered[mask, 0:15].mean(axis=1) speed = np.diff(filtered[mask, 5:50], axis=1).max(axis=1) svr_rbf = SVR('rbf', 3, 1E-7, cache_size=1000) X = folded[:, mask, 0:3].swapaxes(0, 1).reshape(mask.sum(), -1) pre_amp_hat = cross_predict( X.T, amp, lambda x, y, y_t: svr_rbf.fit(x.T, y).predict(y_t.T), VALIDATE_FOLD, False) pre_v_hat = cross_predict( X.T, speed, lambda x, y, y_t: svr_rbf.fit(x.T, y).predict(y_t.T), VALIDATE_FOLD, False) X = folded[:, mask, 3:].swapaxes(0, 1).reshape(mask.sum(), -1) post_amp_hat = cross_predict( X.T, amp, lambda x, y, y_t: svr_rbf.fit(x.T, y).predict(y_t.T), VALIDATE_FOLD, False) post_v_hat = cross_predict( X.T, speed, lambda x, y, y_t: svr_rbf.fit(x.T, y).predict(y_t.T), VALIDATE_FOLD, False) return (mutual_info(pre_amp_hat, amp), mutual_info(post_amp_hat, amp), mutual_info(pre_v_hat, speed), mutual_info(post_v_hat, speed))
def _get_bisect(data): mask, filtered = devibrate_trials( get_trials(data[0], motion_params)[0], motion_params["pre_time"]) return pca_bisect(filtered), mask
def lever_corr(record_file, record_info, params: MotionParams): lever = get_trials(record_file, params) mask, filtered = devibrate_trials(lever.values, params['pre_time']) lever_value = lever.values[0, mask, :] indices = np.triu_indices(lever_value.shape[0], 1) return np.corrcoef(lever_value)[indices]
def get_reliability(data_file: File) -> float: mask, filtered = devibrate_trials( get_trials(data_file, motion_params).values, motion_params['pre_time']) return np.corrcoef(filtered[mask, 15:64])[np.triu_indices(mask.sum(), 1)].mean()
def get_speed(data_file: File) -> float: mask, filtered = devibrate_trials( get_trials(data_file, motion_params).values, motion_params['pre_time']) speed = np.diff(filtered[mask, 5:50], axis=1).max(axis=1) return np.mean(speed)
def get_amp(data_file: File) -> float: mask, filtered = devibrate_trials( get_trials(data_file, motion_params).values, motion_params['pre_time']) return np.quantile( filtered[mask, 25:64].max(axis=1) - filtered[mask, 0:15].mean(axis=1), 0.75)