def _generate_subject(sid, infmaps, bdists): import neuropythy as ny, six lh = VisualPerformanceFieldsDataset._generate_hemi( sid, 'lh', infmaps, bdists) rh = VisualPerformanceFieldsDataset._generate_hemi( sid, 'rh', infmaps, bdists) return ny.hcp_subject(sid).with_hemi(lh=lh, rh=rh)
def _generate_hemi(sid, h, infmaps, bdists): import neuropythy as ny, six hem = ny.hcp_subject(sid).hemis[h] bi = infmaps[sid][h] ds = bdists[sid][h] ps = {} for (k, v) in six.iteritems(ds): ps[k + '_distance'] = v for (k, v) in six.iteritems(bi): ps[k] = v return hem.with_prop(ps)
def _generate_boundary_distances(sid, h, infmaps): import neuropythy as ny import numpy as np import six sub = neuropythy.hcp_subject(sid) hemi = sub.hemis[h] # we use the inferred retinotopy data loaded above rdat = ny.retinotopy_data(infmaps[sid][h], 'inf_') # get x/y and labels (x0, y0) = ny.as_retinotopy(rdat, 'geographical') lbl = rdat['visual_area'] (ang, ecc) = ny.as_retinotopy(rdat, 'visual') # get V1/V2 lines: mask = (ecc <= 7) & (lbl > 0) v1 = np.where(mask & (lbl == 1))[0] v2 = np.where(mask & (lbl == 2))[0] v12 = np.union1d(v1, v2) # invert x for v2 x = np.array(x0) y = y0 x[v2] = -x[v2] # make a visual field mesh vmesh = hemi.surfaces['midgray'].submesh(v12) vmesh = vmesh.copy(coordinates=(x[vmesh.labels], y[vmesh.labels])) # draw a line from origin to the end of the stimulus; see what intersects it addrs = {} paths = {} for (ept, nm) in zip([(0, 7), (0, -7), (7, 0)], ['ventral', 'dorsal', 'horizontal']): seg = np.array([(0, 0), ept]) ipts = ny.geometry.segment_intersection_2D(seg, vmesh.edge_coordinates) ipts = np.transpose(ipts) ipts = ipts[np.isfinite(ipts[:, 0])] # these are the points to address dists = np.dot(ipts, seg[1] - seg[0]) ipts = ipts[np.argsort(dists)] # possibly the first/last few are not in the mesh; just ignore these addr = vmesh.address(ipts) ii = np.isfinite(addr['coordinates'][0]) addr = {k: v[:, ii] for (k, v) in six.iteritems(addr)} addrs[nm] = addr paths[nm] = ny.geometry.Path(hemi, addr) # use these paths to calculate distances eds = { k: p.estimated_distances['midgray'] for (k, p) in six.iteritems(paths) } return eds
def _generate_summary_table(infmaps, bdists, subject_list): import neuropythy as ny import six, numpy as np cols = [ 'sid', 'hemi', 'prf_polar_angle', 'prf_eccentricity', 'prf_variance_explained', 'prf_radius', 'visual_area', 'inf_polar_angle', 'inf_eccentricity', 'ventral', 'dorsal', 'horizontal_distance', 'ventral_distance', 'dorsal_distance' ] df = {k: [] for k in cols} for sid in subject_list: sub = ny.hcp_subject(sid) for h in ('lh', 'rh'): hemi = sub.hemis[h] dists = bdists[sid][h] if dists is None or any(v is None for v in six.itervalues(dists)): continue rmaps = ny.retinotopy_data(hemi, 'prf_') infmap = infmaps[sid][h] lbls = np.array(infmap['inf_visual_area']) iang = np.array(infmap['inf_polar_angle']) iecc = np.array(infmap['inf_eccentricity']) mask = np.where(np.isin(lbls, [1, 2]) & (iecc <= 7))[0] vnt = (np.abs(iang) < 90) drs = ~vnt n = len(mask) dat = { 'sid': np.full(n, sid), 'hemi': np.full(n, h), 'prf_polar_angle': rmaps['polar_angle'][mask], 'prf_eccentricity': rmaps['eccentricity'][mask], 'prf_radius': rmaps['radius'][mask], 'prf_variance_explained': rmaps['variance_explained'][mask], 'visual_area': lbls[mask], 'inf_polar_angle': np.abs(iang[mask]), 'inf_eccentricity': iecc[mask], 'ventral': vnt[mask], 'dorsal': drs[mask], 'horizontal_distance': dists['horizontal'][mask], 'ventral_distance': dists['ventral'][mask], 'dorsal_distance': dists['dorsal'][mask] } for (k, v) in six.iteritems(dat): df[k].append(v) df = {k: np.concatenate(v) for (k, v) in six.iteritems(df)} return ny.to_dataframe(df)
def load_hcp_data(credentials, slice_number): # Import dictionaries import neuropythy as ny import ipyvolume as ipv import nibabel as nib import numpy as np # Configure neuropythy ny.config['hcp_credentials'] = credentials fs = ny.data['hcp'].s3fs # Get full path to T1w NIFTIs fid = fs.ls('hcp-openaccess/HCP_1200/') fid.pop(0) fpath = [] for f in fid: fpath.append(f + '/MNINonLinear/T1w.nii.gz') # Get list of subject IDs sid = [] for f in fid: sid.append(f.split('/')[2]) # Save single slice for each subject into concatenated array im_array = [] sid_tmp = sid[0:3] for i in sid_tmp: print(i) sub = ny.hcp_subject(i) im = sub.load('MNINonLinear/T1w.nii.gz') data = im.get_fdata() im_array.append(data[:, :, slice_number]) arr = np.asarray(im_array) arr_padded = np.pad(img, [(0, 0), (25, 26), (0, 0)], mode='constant') # hard coded to pad this dimension! return arr, sid