def GSBS_helper(X, human_bounds, song_idx, song_bounds, srm_k, hrf): """perform GSBS Fit GSBS to average data and cross-validate with leftout subject using within song and between song average correlations Parameters ---------- A: voxel by time ndarray (2D) B: voxel by time ndarray (2D) C: voxel by time ndarray (2D) D: voxel by time ndarray (2D) K: # of events for GSBS (scalar) Returns ------- z: z-score after performing permuted cross-validation analysis """ w = 3 nPerm = 1000 n_iter = 10 run1 = [X[i] for i in np.arange(0, int(len(X) / 2))] run2 = [X[i] for i in np.arange(int(len(X) / 2), len(X))] # run SRM on sl data if runNum == 0: shared_data = SRM_V1(run2, run1, srm_k, n_iter) elif runNum == 1: shared_data = SRM_V1(run1, run2, srm_k, n_iter) shared_data = stats.zscore(np.dstack(shared_data), axis=1, ddof=1) data = np.mean(shared_data[:, song_bounds[song_idx]:song_bounds[song_idx + 1]], axis=2) nTR = data.shape[1] # fit model K = len(human_bounds) + 1 ev = GSBS(x=data.T, kmax=K) ev.fit() # get boundaries bounds = np.round(np.nonzero(ev.get_bounds(K))[0]) match = np.zeros(nPerm + 1) perm_bounds = bounds.copy() for p in range(nPerm + 1): for hb in human_bounds: if np.any(np.abs(perm_bounds - hb) <= w): match[p] += 1 match[p] /= len(human_bounds) np.random.seed(p) perm_bounds = np.random.choice(nTR, K - 1, replace=False) return match
def HMM(X,human_bounds,GSBS_bounds,song_idx,song_bounds,srm_k,hrf): """fit hidden markov model Fit HMM to average data and cross-validate with leftout subject using within song and between song average correlations Parameters ---------- A: voxel by time ndarray (2D) B: voxel by time ndarray (2D) C: voxel by time ndarray (2D) D: voxel by time ndarray (2D) K: # of events for HMM (scalar) Returns ------- z: z-score after performing permuted cross-validation analysis """ w = 3 nPerm = 1000 n_iter = 10 run1 = [X[i] for i in np.arange(0, int(len(X)/2))] run2 = [X[i] for i in np.arange(int(len(X)/2), len(X))] # run SRM on sl data if runNum == 0: shared_data = SRM_V1(run2,run1,srm_k,n_iter) elif runNum == 1: shared_data = SRM_V1(run1,run2,srm_k,n_iter) shared_data = stats.zscore(np.dstack(shared_data),axis=1,ddof=1) data = np.mean(shared_data[:,song_bounds[song_idx]:song_bounds[song_idx + 1]],axis=2) nTR = data.shape[1] # Fit to all but one subject K = len(human_bounds) + 1 ev = brainiak.eventseg.event.EventSegment(K,split_merge=True,split_merge_proposals=3) ev.fit(data.T) bounds = np.where(np.diff(np.argmax(ev.segments_[0],axis=1)))[0] match = np.zeros(nPerm+1) perm_bounds = bounds.copy() for p in range(nPerm+1): for gs in GSBS_bounds: if np.any(np.abs(perm_bounds - gs) <= w): match[p] += 1 match[p] /= len(GSBS_bounds) np.random.seed(p) perm_bounds = np.random.choice(nTR,K-1,replace=False) return match
def RSA(X,song_features,song_idx,song_bounds,srm_k,hrf): """compute RSA for music features and neural data compute similarity matrices for music and neural features then compute RSA between the two Parameters ---------- A: voxel by time ndarray (2D) B: voxel by time ndarray (2D) C: voxel by time ndarray (2D) D: voxel by time ndarray (2D) K: # of events for HMM (scalar) Returns ------- z: z-score after permuting music feature vectors """ nPerm = 1000 n_iter = 10 run1 = [X[i] for i in np.arange(0, int(len(X)/2))] run2 = [X[i] for i in np.arange(int(len(X)/2), len(X))] # run SRM on sl data if runNum == 0: shared_data = SRM_V1(run2,run1,srm_k,n_iter) elif runNum == 1: shared_data = SRM_V1(run1,run2,srm_k,n_iter) shared_data = stats.zscore(np.dstack(shared_data),axis=1,ddof=1) data = np.mean(shared_data[:,song_bounds[song_idx]:song_bounds[song_idx + 1]],axis=2) song_features = song_features[:,song_bounds[song_idx]:song_bounds[song_idx + 1]] rsa_scores = np.zeros(nPerm+1) perm_features = song_features.copy() songCorr = np.corrcoef(song_features.T) songVec = squareform(songCorr, checks=False) voxCorr = np.corrcoef(data.T) voxVec = squareform(voxCorr,checks=False) for p in range(nPerm+1): spearmanCorr = stats.spearmanr(voxVec,songVec,nan_policy='omit') rsa_scores[p] = spearmanCorr[0] song_features = phase_randomize(song_features.T, voxelwise=False, random_state=p).T songCorr = np.corrcoef(song_features.T) songVec = squareform(songCorr, checks=False) return rsa_scores
'_split_merge_run2_n25.npy'), axis=1, ddof=1) # Convert data into lists where each element is voxels by samples run1_list = [] run2_list = [] for i in range(0, run1.shape[2]): run1_list.append(run1[:, :, i]) run2_list.append(run2[:, :, i]) n_iter = 10 features = 10 ## run SRM on ROIs shared_data_test1 = SRM_V1(run2_list, run1_list, features, n_iter) shared_data_test2 = SRM_V1(run1_list, run2_list, features, n_iter) avg_response_test_run1 = sum(shared_data_test1) / len(shared_data_test1) avg_response_test_run2 = sum(shared_data_test2) / len(shared_data_test2) song1 = avg_response_test_run1[:, start_run1:end_run1] song2 = avg_response_test_run2[:, start_run2:end_run2] #run1DataAvg = np.mean(run1,axis=2) #run2DataAvg = np.mean(run2,axis=2) #song1 = run1DataAvg[:,start_run1:end_run1] #song2 = run2DataAvg[:,start_run2:end_run2] data = (song1 + song2) / 2
def HMM(X, human_bounds, song_idx, song_bounds, hrf, srm_k): """fit hidden markov model Fit HMM to average data and cross-validate with leftout subjects using within song and between song average correlations Parameters ---------- A: list of 50 (contains 2 runs per subject) 2D (voxels x full time course) arrays B: # of events for HMM (scalar) song_idx: song index (scalar) C: voxel by time ndarray (2D) D: array of song boundaries (1D) Returns ------- wVa score: final score after performing cross-validation of leftout subjects """ nPerm = 1000 within_across = np.zeros(nPerm + 1) run1 = [X[i] for i in np.arange(0, int(len(X) / 2))] run2 = [X[i] for i in np.arange(int(len(X) / 2), len(X))] n_iter = 10 # run SRM on masked data if runNum == 0: shared_data = SRM_V1(run2, run1, srm_k, n_iter) elif runNum == 1: shared_data = SRM_V1(run1, run2, srm_k, n_iter) shared_data = stats.zscore(np.dstack(shared_data), axis=1, ddof=1) others = np.mean( shared_data[:, song_bounds[song_idx]:song_bounds[song_idx + 1], :13], axis=2) loo = np.mean(shared_data[:, song_bounds[song_idx]:song_bounds[song_idx + 1], 13:], axis=2) nTR = loo.shape[1] # Fit to all but one subject K = len(human_bounds) + 1 ev = brainiak.eventseg.event.EventSegment(K) ev.fit(others.T) events = np.argmax(ev.segments_[0], axis=1) _, event_lengths = np.unique(events, return_counts=True) max_event_length = stats.mode(events)[1][0] # compute timepoint by timepoint correlation matrix cc = np.corrcoef(loo.T) # Should be a time by time correlation matrix # Create a mask to only look at values up to max_event_length local_mask = np.zeros(cc.shape, dtype=bool) for k in range(1, max_event_length): local_mask[np.diag(np.ones(cc.shape[0] - k, dtype=bool), k)] = True # Compute within vs across boundary correlations, for real and permuted bounds for p in range(nPerm + 1): same_event = events[:, np.newaxis] == events within = fisher_mean(cc[same_event * local_mask]) across = fisher_mean(cc[(~same_event) * local_mask]) within_across[p] = within - across np.random.seed(p) perm_lengths = np.random.permutation(event_lengths) events = np.zeros(nTR, dtype=np.int) events[np.cumsum(perm_lengths[:-1])] = 1 events = np.cumsum(events) return within_across
run1_regress = [] run2_regress = [] for s in range(run1.shape[0]): # remove music features from run1 regr1 = linear_model.LinearRegression() regr1.fit(fullRegs1.T,run1[s,:,:].T) run1_regress.append(run1[s,:,:] - np.dot(regr1.coef_, fullRegs1) - regr1.intercept_[:, np.newaxis]) # remove music features from run 2 regr2 = linear_model.LinearRegression() regr2.fit(fullRegs2.T,run2[s,:,:].T) run2_regress.append(run2[s,:,:] - np.dot(regr2.coef_, fullRegs2) - regr2.intercept_[:, np.newaxis]) # run SRM on masked data if runNum == 0: shared_data = SRM_V1(run2_regress,run1_regress,srm_k,n_iter) elif runNum == 1: shared_data = SRM_V1(run1_regress,run2_regress,srm_k,n_iter) data = np.mean(stats.zscore(np.dstack(shared_data),axis=1,ddof=1),axis=2)[:,start_idx:end_idx] # average data without doing SRM #if runNum == 0: # data = np.mean(run1,axis=0)[:,start_idx:end_idx] #elif runNum == 1: # data = np.mean(run2,axis=0)[:,start_idx:end_idx] # fit HMM to song data and return match data where first entry is true match score and all others are permutation scores print("Fitting HMM") SL_match = HMM(data,human_bounds)
# get indices where mask and parcels overlap indices = np.where((mask_img.get_data() > 0) & (parcels == i + 1)) # initialize list for storing masked data across subjects run1 = np.load(parcel_dir + "parcel" + str(i + 1) + "_run1.npy") run2 = np.load(parcel_dir + "parcel" + str(i + 1) + "_run2.npy") # add lines to check whether number of voxels is less than initial srm K if len(indices[0]) < initial_srm_k: srm_k = len(indices[0]) elif len(indices[0]) >= initial_srm_k: srm_k = initial_srm_k # run SRM on masked data if runNum == 0: shared_data = SRM_V1(run1, run2, srm_k, n_iter) elif runNum == 1: shared_data = SRM_V1(run2, run1, srm_k, n_iter) # perform cross-validation style HMM for n_folds for n in range(n_folds): np.random.seed(n) subj_list_shuffle = np.random.permutation(shared_data) # convert data from list to numpy array and z-score in time shared_data_stack = stats.zscore(np.dstack(subj_list_shuffle), axis=1, ddof=1) # split subjects into two groups others = np.mean(shared_data_stack[:, start_idx:end_idx, :13], axis=2)