def extract_subrois(timeseries_file, label_file, indices):
 img = nb.load(timeseries_file)
 data = img.get_data()
 roiimg = nb.load(label_file)
 rois = roiimg.get_data()
 prefix = split_filename(timeseries_file)[1]
 out_ts_file = os.path.join(os.getcwd(), (u'%s_subcortical_ts.txt' % prefix))
 with open(out_ts_file, u'wt') as fp:
  for fsindex in indices:
   ijk = np.nonzero((rois == fsindex))
   ts = data[ijk]
   for (i0, row) in enumerate(ts):
    fp.write((((u'%d,%d,%d,%d,' % (fsindex, ijk[0][i0], ijk[1][i0], ijk[2][i0])) + u','.join([(u'%.10f' % val) for val in row])) + u'\n'))
 return out_ts_file

def extract_subrois ( timeseries_file , label_file , indices ) : img = nb . load ( timeseries_file ) data = img . get_data ( ) roiimg = nb . load ( label_file ) rois = roiimg . get_data ( ) prefix = split_filename ( timeseries_file ) [ 1 ] out_ts_file = os . path . join ( os . getcwd ( ) , ( u'%s_subcortical_ts.txt' % prefix ) ) with open ( out_ts_file , u'wt' ) as fp : for fsindex in indices : ijk = np . nonzero ( ( rois == fsindex ) ) ts = data [ ijk ] for ( i0 , row ) in enumerate ( ts ) : fp . write ( ( ( ( u'%d,%d,%d,%d,' % ( fsindex , ijk [ 0 ] [ i0 ] , ijk [ 1 ] [ i0 ] , ijk [ 2 ] [ i0 ] ) ) + u',' . join ( [ ( u'%.10f' % val ) for val in row ] ) ) + u'\n' ) ) return out_ts_file
extract voxel time courses for each subcortical roi index parameters timeseries_file: a 4d nifti file label_file: a 3d file containing rois in the same space/size of the 4d file indices: a list of indices for rois to extract .