def concatenate_runs(subj_num): """ Concatenates data from all runs for a particular subject and saves that data into a designated file. If file already exists, uses that cached file instead. Parameters ---------- subj_num : int Returns ------- None """ npy_file_name = dp.get_concatenated_path(subj_num) if not exists(npy_file_name) or not USE_CACHED_DATA: run_data = [] for j in range(NUM_RUNS): img = nib.load(dp.get_raw_path(subj_num, j + 1)) data = img.get_data() if j == 0: run_data.append(data[..., :-4]) elif j >= 1 and j <= NUM_RUNS - 2: run_data.append(data[..., 4:-4]) else: run_data.append(data[..., 4:]) concatenated_run_data = np.concatenate(run_data, axis=3) np.save(npy_file_name, concatenated_run_data) print("Saved {0}".format(npy_file_name)) else: print("Using cached version of {0}".format(npy_file_name))
def test_get_concatenated_path(): paths = [dp.get_concatenated_path(subject) for subject in SUBJECTS] assert paths[0] == '{0}/data/processed/sub1_rcds.npy'.format(REPO_HOME_PATH) assert paths[1] == '{0}/data/processed/sub2_rcds.npy'.format(REPO_HOME_PATH) assert paths[2] == '{0}/data/processed/sub3_rcds.npy'.format(REPO_HOME_PATH) assert paths[3] == '{0}/data/processed/sub4_rcds.npy'.format(REPO_HOME_PATH) assert paths[4] == '{0}/data/processed/sub5_rcds.npy'.format(REPO_HOME_PATH)
def gaussian_smooth_subj(subj_num, fwhm_mm): """ Retrieves the absolute path to the smoothed data for a particular subject and full width half maximum smoothed version. Loads the data and saves it in the retrieved path. If file already exists, then uses cached version instead. Parameters ---------- subj_num : int fwhm_mm : int Returns ------- None """ smoothed_data_path = dp.get_smoothed_path(subj_num, fwhm_mm) if not exists(smoothed_data_path) or not USE_CACHED_DATA: data = np.load(dp.get_concatenated_path(subj_num)).astype(np.float32) smoothed_data = apply_gaussian_smooth(data, fwhm_mm) del data np.save(smoothed_data_path, smoothed_data) print("Saved {0}".format(smoothed_data_path)) else: print("Using cached version of {0}".format(smoothed_data_path))
def concatenate_runs(subj_num): """ Concatenates data from all runs for a particular subject and saves that data into a designated file. If file already exists, uses that cached file instead. Parameters ---------- subj_num : int Returns ------- None """ npy_file_name = dp.get_concatenated_path(subj_num) if not exists(npy_file_name) or not USE_CACHED_DATA: run_data = [] for j in range(NUM_RUNS): img = nib.load(dp.get_raw_path(subj_num, j + 1)) data = img.get_data() if j == 0: run_data.append(data[..., :-4]) elif j >= 1 and j <= NUM_RUNS - 2: run_data.append(data[..., 4:-4]) else: run_data.append(data[..., 4:]) concatenated_run_data = np.concatenate(run_data, axis=3) np.save(npy_file_name, concatenated_run_data) print('Saved {0}'.format(npy_file_name)) else: print('Using cached version of {0}'.format(npy_file_name))
def gaussian_smooth_subj(subj_num, fwhm_mm): """ Retrieves the absolute path to the smoothed data for a particular subject and full width half maximum smoothed version. Loads the data and saves it in the retrieved path. If file already exists, then uses cached version instead. Parameters ---------- subj_num : int fwhm_mm : int Returns ------- None """ smoothed_data_path = dp.get_smoothed_path(subj_num, fwhm_mm) if not exists(smoothed_data_path) or not USE_CACHED_DATA: data = np.load(dp.get_concatenated_path(subj_num)).astype(np.float32) smoothed_data = apply_gaussian_smooth(data, fwhm_mm) del data np.save(smoothed_data_path, smoothed_data) print('Saved {0}'.format(smoothed_data_path)) else: print('Using cached version of {0}'.format(smoothed_data_path))
def get_brain_mask(): brain_mask_path = dp.get_brain_mask_path() if not exists(brain_mask_path): data = np.load(dp.get_concatenated_path(1)) mean_vol = np.mean(data, axis=-1) in_brain_mask = mean_vol > 150 np.save(brain_mask_path, in_brain_mask) return np.load(brain_mask_path)
def plot_vol_mean_histogram(subj_num): data = np.load(dp.get_concatenated_path(subj_num))[..., NUM_OFFSET_VOLUMES:] mean_vol = np.mean(data, axis=-1) plt.hist(np.ravel(mean_vol), bins=100) plot_path = '{0}/figures/subj_{1}_vol_mean_histogram.png'.format( REPO_HOME_PATH, subj_num) plt.savefig(plot_path) print('Saved {0}'.format(plot_path))
def gaussian_smooth_subj(subj_num, fwhm_mm): smoothed_data_path = dp.get_smoothed_path(subj_num, fwhm_mm) if not exists(smoothed_data_path) or not USE_CACHED_DATA: data = np.load(dp.get_concatenated_path(subj_num)).astype(np.float32) smoothed_data = apply_gaussian_smooth(data, fwhm_mm) del data np.save(smoothed_data_path, smoothed_data) print('Saved {0}'.format(smoothed_data_path)) else: print('Using cached version of {0}'.format(smoothed_data_path))
def test_get_concatenated_path(): paths = [dp.get_concatenated_path(subject) for subject in SUBJECTS] assert paths[0] == '{0}/data/processed/sub1_rcds.npy'.format( REPO_HOME_PATH) assert paths[1] == '{0}/data/processed/sub2_rcds.npy'.format( REPO_HOME_PATH) assert paths[2] == '{0}/data/processed/sub3_rcds.npy'.format( REPO_HOME_PATH) assert paths[3] == '{0}/data/processed/sub4_rcds.npy'.format( REPO_HOME_PATH) assert paths[4] == '{0}/data/processed/sub5_rcds.npy'.format( REPO_HOME_PATH)
def concatenate_runs(subj_num): npy_file_name = dp.get_concatenated_path(subj_num) if not exists(npy_file_name) or not USE_CACHED_DATA: run_data = [] for j in range(NUM_RUNS): img = nib.load(dp.get_raw_path(subj_num, j + 1)) data = img.get_data() if j == 0: run_data.append(data[..., :-4]) elif j >= 1 and j <= NUM_RUNS - 2: run_data.append(data[..., 4:-4]) else: run_data.append(data[..., 4:]) concatenated_run_data = np.concatenate(run_data, axis=3) np.save(npy_file_name, concatenated_run_data) print('Saved {0}'.format(npy_file_name)) else: print('Using cached version of {0}'.format(npy_file_name))
def get_brain_mask(): """ Retrieves the path to the brain mask and loads that data Parameters ---------- None Returns ------- brain_mask : array """ brain_mask_path = dp.get_brain_mask_path() if not exists(brain_mask_path): data = np.load(dp.get_concatenated_path(1)) mean_vol = np.mean(data, axis=-1) in_brain_mask = mean_vol > 150 np.save(brain_mask_path, in_brain_mask) return np.load(brain_mask_path)
def plot_vol_mean_histogram(subj_num): """ Plots the mean volume for a particular subject and saves that histogram into the figures folder Parameters ---------- subj_num : int Returns ------- None """ data = np.load(dp.get_concatenated_path(subj_num))[..., NUM_OFFSET_VOLUMES:] mean_vol = np.mean(data, axis=-1) plt.hist(np.ravel(mean_vol), bins=100) plot_path = '{0}/figures/subj_{1}_vol_mean_histogram.png'.format( REPO_HOME_PATH, subj_num) plt.savefig(plot_path) print('Saved {0}'.format(plot_path))