# Now we're going to handle a covariates file that's been generated by a preprocessing routine. # First we'll read in the text file using pandas and convert it to a design matrix. # To be explicit with the meta-data we're going to change some default attributes during conversion. import pandas as pd covariatesFile = os.path.join(get_resource_path(),'covariates_example.csv') cov = pd.read_csv(covariatesFile) cov = Design_Matrix(cov,hasIntercept=False,hrf=[]) cov.heatmap() ######################################################################### # The class has several methods features for basic data scaling and manipulation. Others can likely be found in pandas core functionality. # Here we fill NaN values with 0 and zscore all columns except the last. Because the class has all of pandas functionality, method-chaining is built-in. cov = cov.fillna(0).zscore(cov.columns[:-1]) cov.heatmap() ######################################################################### # Concatenate Multiple Design Matrices # ---------------------------------- # # A really nice feature of this class is simplified, but intelligent matrix concatentation. Here it's trivially to horizontally concatenate our convolved onsets and covariates, while keeping our column names and order. full = dm.append(cov,axis=1) full.heatmap() ######################################################################### # But we can also intelligently vertically concatenate design matrices to handle say, different experimental runs, or subjects. The method enables the user to indicate which columns to keep separated during concatenation or which to treat as extensions along the first dimension. By default the class will keep constant terms separated. dm2 = dm.append(dm,axis=0,separate=True)
dm = onsets_to_dm(onsetsFile, sampling_freq=sampling_freq, run_length=160, sort=True) # 2) Convolve them with the hrf dm = dm.convolve() # 2) Load in covariates for this run covariatesFile = os.path.join(get_resource_path(), 'covariates_example.csv') cov = pd.read_csv(covariatesFile) cov = Design_Matrix(cov, sampling_freq=sampling_freq) # 3) In the covariates, fill any NaNs with 0, add intercept and linear trends and dct basis functions cov = cov.fillna(0) # Retain a list of nuisance covariates (e.g. motion and spikes) which we'll also want to also keep separate for each run cov_columns = cov.columns cov = cov.add_poly(1).add_dct_basis() # 4) Join the onsets and covariates together full = dm.append(cov, axis=1) # 5) Append it to the master Design Matrix keeping things separated by run all_runs = all_runs.append(full, axis=0, unique_cols=cov.columns) all_runs.heatmap(vmin=-1, vmax=1) ######################################################################### # We can see the left most columns of our multi-run design matrix contain our conditions of interest (stacked across all runs), the middle columns includes separate run-wise nuisiance covariates (motion, spikes) and the right most columns contain run specific polynomials (intercept, trends, etc).