def create_a_template(id): """ create template :param id: template id :return: a DataUnit instance """ if id == 0: # template 0, used in ISMRM2017 abstract # new and setting du = toolboxes.DataUnit() du._secured_data['if_random_neural_parameter'] = False du._secured_data['if_random_hemodynamic_parameter'] = False du._secured_data['if_random_x_state_initial'] = False du._secured_data['if_random_h_state_initial'] = False du._secured_data['if_random_stimuli'] = True du._secured_data['if_random_node_number'] = False du._secured_data['if_random_stimuli_number'] = False du._secured_data['if_random_delta_t'] = False du._secured_data['if_random_scan_time'] = False du._secured_data['t_delta'] = 1. / 16. du._secured_data['t_scan'] = 5 * 60 du._secured_data['n_node'] = 3 du._secured_data['n_stimuli'] = 1 du._secured_data['A'] = np.array([[-1, 0, 0], [0.8, -1, 0.4], [0.4, 0.8, -1]]) du._secured_data['B'] = [np.array([[0, 0, 0], [0, 0, 0], [0, 0, -0.4]])] du._secured_data['C'] = np.array([0.8, 0, 0]).reshape(3, 1) du._secured_data['learning_rate'] = 0.1 du._secured_data['n_backpro'] = 12 du.complete_data_unit(if_show_message=False, if_check_property=False) return du
def _run(): # print('_run() runs') # cores = dbo.load_database(RESULT_PATH_DCM_RNN) with open(data_path, 'rb') as f: cores = pickle.load(f) # for each DCM, recover fMRI signal with t_delta = 1/16 t_delta_list = [1.0 / 16] rMSEs = [] for i in range(len(cores)): # print('current processing: ' + str(i)) du = tb.DataUnit() du.load_parameter_core(cores[i]) y_list = du.map('t_delta', t_delta_list, 'y') signal_length = y_list[0].shape[0] # y_down_sampled = du.resample_arrays(y_list, (int(signal_length / 32), du.get('n_node'))) # y_up_sampled = du.resample_arrays(y_down_sampled, (signal_length, du.get('n_node')), order=3) y_down_sampled = [ du.resample(array, (int(signal_length / 32), du.get('n_node'))) for array in y_list ] y_up_sampled = [ du.resample(array, (signal_length, du.get('n_node'))) for array in y_down_sampled ] rMSE = du.compare(y_up_sampled, y_list[0]) rMSEs.append(rMSE) # save results output_dir = os.path.split(output_file)[0] print('output_dir: ' + output_dir) if not os.path.exists(output_dir): os.makedirs(output_dir) with open(output_file, 'wb') as f: pickle.dump(rMSEs, f)
def _run(): # print('_run() runs') # cores = dbo.load_database(RESULT_PATH_DCM_RNN) with open(data_path, 'rb') as f: cores = pickle.load(f) # for each DCM, recover fMRI signal with different t_delta t_delta_list = [ 1.0 / 64, 1.0 / 32, 1.0 / 16, 1.0 / 8, 1.0 / 4, 1.0 / 2 ] rMSEs = [] # for i in range(len(cores)): for i in range(len(cores)): # print('current processing: ' + str(i)) du = tb.DataUnit() du.load_parameter_core(cores[i]) y_list = du.map('t_delta', t_delta_list, 'y') y_resampled = [ du.resample(array, y_list[-1].shape) for array in y_list ] rMSE = du.compare(y_resampled, y_resampled[0]) rMSEs.append(rMSE) # save results output_dir = os.path.split(output_file)[0] print('output_dir: ' + output_dir) if not os.path.exists(output_dir): os.makedirs(output_dir) with open(output_file, 'wb') as f: pickle.dump(rMSEs, f)
interpolate_func = interp1d(t_axis_original, spm_data['y'][:, i], kind='cubic') spm_data['y_upsampled'][:, i] = interpolate_func(t_axis_new) spm_data['u_upsampled'] = np.zeros((len(t_axis_new), spm_data['u'].shape[1])) for i in range(spm_data['u'].shape[1]): interpolate_func = interp1d(t_axis_original, spm_data['u'][:, i]) spm_data['u_upsampled'][:, i] = interpolate_func(t_axis_new) temporal_mask = spm_data['u_upsampled'][:, 0] < 0.5 temp = copy.deepcopy(spm_data['y_upsampled']) mean_values = np.mean(temp[temporal_mask, :], axis=0) mean_values = np.repeat(mean_values.reshape([1, 3]), spm_data['y_upsampled'].shape[0], axis=0) spm_data['y_upsampled'] = spm_data['y_upsampled'] - mean_values N_SEGMENTS = mth.ceil(len(spm_data['y_upsampled']) / N_RECURRENT_STEP) * N_RECURRENT_STEP # build DataUnit du from spm_data du = tb.DataUnit() du._secured_data['n_node'] = spm_data['y_upsampled'].shape[1] du._secured_data['n_stimuli'] = spm_data['u_upsampled'].shape[1] du._secured_data['t_delta'] = TARGET_TEMPORAL_RESOLUTION du._secured_data['t_scan'] = du._secured_data['t_delta'] * spm_data['y_upsampled'].shape[0] du._secured_data['n_time_point'] = spm_data['y_upsampled'].shape[0] du._secured_data['u'] = spm_data['u_upsampled'] du._secured_data['y'] = spm_data['y_upsampled'] # initialize DataUnit du_hat, support masks should be specified here # Wxx = np.zeros((du.get('n_node'), du.get('n_node'))) Wxx = np.eye(du.get('n_node')) - np.eye(du.get('n_node')) * du.get('t_delta') Wxxu = [np.zeros((du.get('n_node'), du.get('n_node'))) for _ in range(du.get('n_stimuli'))] Wxu = np.zeros((du.get('n_node'), du.get('n_stimuli'))) Wxu[0, 0] = 1. * du.get('t_delta')
print("PROJECT_DIR is set as: " + PROJECT_DIR) import matplotlib import matplotlib.pyplot as plt import numpy as np import os import pickle import toolboxes #EXPERIMENT_DIR = os.path.join(PROJECT_DIR, 'experiments', 't_delta') #INPUT_DIR = os.path.join(PROJECT_DIR, 'data', 'DB0.pkl') # create new DCM # new and setting du = toolboxes.DataUnit() du._secured_data['if_random_node_number'] = True du._secured_data['if_random_stimuli'] = True du._secured_data['if_random_x_state_initial'] = False du._secured_data['if_random_h_state_initial'] = False du._secured_data['t_delta'] = 0.25 du._secured_data['t_scan'] = 5 * 60 du.complete_data_unit(if_show_message=False) x_axis = np.array(range(0, len(du.get('u')))) * du.get('t_delta') plt.figure(figsize=(4, 2), dpi=300) plt.subplot(2, 1, 1) for i in range(du.get('n_stimuli')): plt.plot(x_axis, du.get('u')[:, i], linewidth=0.75, alpha=0.75) plt.ylabel('inputs')
import importlib from scipy.fftpack import idct, dct DATA_DIR = os.path.join(PROJECT_DIR, 'dcm_rnn', 'resources', 'SPM_data_attention.pkl') MAX_EPOCHS = 32 * 3 CHECK_STEPS = 1 N_RECURRENT_STEP = 192 DATA_SHIFT = 2 MAX_BACK_TRACK = 16 MAX_CHANGE = 0.001 BATCH_RANDOM_DROP_RATE = 1. TARGET_T_DELTA = 1. / 16 N_CONFOUNDS = 19 du = tb.DataUnit() # load SPM attention spm_data from resources spm_data = pickle.load(open(DATA_DIR, 'rb')) # process spm_data, # up sample u and y to 16 frame/second shape = list(spm_data['u'].shape) shape[0] = int(spm_data['TR'] / TARGET_T_DELTA * shape[0]) spm_data['u_upsampled'] = du.resample(spm_data['u'], shape, order=3) spm_data['y_upsampled'] = du.resample(spm_data['y'], shape, order=3) # assume the observation model is # y = DCM_RNN(u) + Confounds * weights + noise # Confounds are the first cosine transfer basis (19) n_time_point = spm_data['u_upsampled'].shape[0] confounds = idct(np.eye(n_time_point)[:, :N_CONFOUNDS], axis=0, norm='ortho')
import copy import pandas as pd import math as mth from scipy.interpolate import interp1d import scipy as sp import scipy.io EXPERIMENT_PATH = os.path.join(PROJECT_DIR, 'experiments', 'compare_estimation_with_simulated_data') DATA_PATH = os.path.join(EXPERIMENT_PATH, 'data') CORE_PATH = os.path.join(DATA_PATH, 'core.pkl') SAVE_PATH_PKL = os.path.join(DATA_PATH, 'du_DCM_RNN.pkl') SAVE_PATH_MAT = os.path.join(DATA_PATH, 'DCM_initial.mat') SNR = 3 du = tb.DataUnit() du._secured_data['if_random_neural_parameter'] = False du._secured_data['if_random_hemodynamic_parameter'] = False du._secured_data['if_random_x_state_initial'] = False du._secured_data['if_random_h_state_initial'] = False du._secured_data['if_random_stimuli'] = True du._secured_data['if_random_node_number'] = False du._secured_data['if_random_stimuli_number'] = False du._secured_data['if_random_delta_t'] = False du._secured_data['if_random_scan_time'] = False du._secured_data['t_delta'] = 1. / 64. du._secured_data['t_scan'] = 5 * 60 du._secured_data['n_node'] = 3 du._secured_data['n_stimuli'] = 3 du._secured_data['A'] = np.array([[-0.8, 0., 0.], [0., -0.8, 0.],