def innermost(self, *args, **kwargs): from datamatrix import convert as cnv df_in = cnv.to_pandas(self) fnc = getattr(df_in, function_name) df_out = fnc(*args, **kwargs) return df_out
def test_convert(): refdm = DataMatrix(length=3) refdm[u'tést'] = 1, 2, u'' refdm.B = u'mathôt', u'b', u'x' refdm.C = u'a,\\b"\'c', 8, u'' testdm = io.readtxt('testcases/data/data.csv') check_dm(refdm, testdm) testdm = cnv.from_json(cnv.to_json(testdm)) check_dm(refdm, testdm) testdf = cnv.to_pandas(testdm) testdm = cnv.from_pandas(testdf) check_dm(refdm, testdm) print(np.array(testdf)) print(np.array(testdm)) assert (np.array(testdf, dtype=str) == np.array(testdm, dtype=str)).all()
def corr_img(img, trace, mcparams, xyz, deconv_bold): """Get a per-voxel correlation image between a nifti image and a trace""" pcor = np.empty(img.shape[:-1]) pcor[:] = np.nan imgdat = img.get_data() dm = mcparams[SKIP_FIRST:len(trace)] dm.bold = FloatColumn dm.trace = FloatColumn dm.trace = trace[SKIP_FIRST:] df = cnv.to_pandas(dm) for i, (x, y, z) in enumerate(zip(*xyz)): if DOWNSAMPLE and (x % 2 or y % 2 or z % 2): continue df.bold = flt(imgdat[x, y, z])[SKIP_FIRST:len(trace)] if deconv_bold: df.bold = deconv_prf(df.bold) results = smf.ols(FORMULA_VOXEL_TRACE, df).fit() pcor[x, y, z] = results.tvalues[1] if DOWNSAMPLE: pcor[x + 1, y + 1, z + 1] = results.tvalues[1] return nib.Nifti2Image(pcor, img.affine)
def __dataframe__(self): from datamatrix import convert as cnv return cnv.to_pandas(self)
def do_run(sub, run, row, xyz): print( 'Starting sub: {}, run: {}, voxels: {}'.format( sub, run, len(xyz[0]) ) ) img = get_avmovie_data(sub, run) traces = { 'pupil': get_pupil_data(sub, run), 'arousal': get_arousal_data(run), # doesn't depend on subject 'luminance': get_luminance_data(sub, run), 'change': get_change_data(sub, run) } mcparams = get_mcparams(sub, run) # Per-voxel correlationsx, y, z for tracename, trace in traces.items(): row['t_bold_{}'.format(tracename)] = corr_img( img, trace, mcparams, xyz, deconv_bold=tracename == 'pupil' # PRF deconvolution ) # Per-ROI correlations voxels = img.get_data()[xyz[0], xyz[1], xyz[2], :] avg = np.nanmean(voxels, axis=0) # Create a DataFrame with equally long traces, also including the # average bold value trace_len = min(len(trace) for trace in traces.values()) print('trace length: {}'.format(trace_len)) dm = mcparams[SKIP_FIRST:trace_len] dm.bold = FloatColumn dm.bold = avg[SKIP_FIRST:trace_len] for tracename, trace in traces.items(): dm[tracename] = FloatColumn dm[tracename] = trace[SKIP_FIRST:trace_len] df = cnv.to_pandas(dm) # Correlate each trace with the average bold signal for tracename in traces: results = smf.ols( FORMULA_BOLD_TRACE.format(tracename), data=df ).fit() t = results.tvalues[1] print('t(boldavg ~ {}) = {:.4f}'.format(tracename, t)) row['t_boldavg_{}'.format(tracename)] = t # Correlate each trace with each other trace for tracename1, tracename2 in itertools.product(TRACES, TRACES): if tracename1 <= tracename2: continue results = smf.ols( FORMULA_TRACE_TRACE.format(tracename1, tracename2), data=df ).fit() t = results.tvalues[1] print('t({} ~ {}) = {:.4f}'.format(tracename1, tracename2, t)) row['t_{}_{}'.format(tracename1, tracename2)] = t row.run = run