Skip to content

Instantly share code, notes, and snippets.

@xgrg
Last active February 24, 2016 15:24
Show Gist options
  • Save xgrg/dd221cdea75f13b94651 to your computer and use it in GitHub Desktop.
Save xgrg/dd221cdea75f13b94651 to your computer and use it in GitHub Desktop.
Usage : ./map_csv_to_corticalROI.py -i lh.r.aparc.a2009s.annot.gii --csv stats.csv -o test.tex
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def map_values(parc, csv):
from soma import aims
import numpy as np
labels2009 = ['G_and_S_frontomargin', 'G_and_S_occipital_inf', 'G_and_S_paracentral', 'G_and_S_subcentral', 'G_and_S_transv_frontopol', 'G_and_S_cingul-Ant', 'G_and_S_cingul-Mid-Ant', 'G_and_S_cingul-Mid-Post', 'G_cingul-Post-dorsal', 'G_cingul-Post-ventral', 'G_cuneus', 'G_front_inf-Opercular', 'G_front_inf-Orbital', 'G_front_inf-Triangul', 'G_front_middle', 'G_front_sup', 'G_Ins_lg_and_S_cent_ins', 'G_insular_short', 'G_occipital_middle', 'G_occipital_sup', 'G_oc-temp_lat-fusifor', 'G_oc-temp_med-Lingual', 'G_oc-temp_med-Parahip', 'G_orbital', 'G_pariet_inf-Angular', 'G_pariet_inf-Supramar', 'G_parietal_sup', 'G_postcentral', 'G_precentral', 'G_precuneus', 'G_rectus', 'G_subcallosal', 'G_temp_sup-G_T_transv', 'G_temp_sup-Lateral', 'G_temp_sup-Plan_polar', 'G_temp_sup-Plan_tempo', 'G_temporal_inf', 'G_temporal_middle', 'Lat_Fis-ant-Horizont', 'Lat_Fis-ant-Vertical', 'Lat_Fis-post', 'Pole_occipital', 'Pole_temporal', 'S_calcarine', 'S_central', 'S_cingul-Marginalis', 'S_circular_insula_ant', 'S_circular_insula_inf', 'S_circular_insula_sup', 'S_collat_transv_ant', 'S_collat_transv_post', 'S_front_inf', 'S_front_middle', 'S_front_sup', 'S_interm_prim-Jensen', 'S_intrapariet_and_P_trans', 'S_oc_middle_and_Lunatus', 'S_oc_sup_and_transversal', 'S_occipital_ant', 'S_oc-temp_lat', 'S_oc-temp_med_and_Lingual', 'S_orbital_lateral', 'S_orbital_med-olfact', 'S_orbital-H_Shaped', 'S_parieto_occipital', 'S_pericallosal', 'S_postcentral', 'S_precentral-inf-part', 'S_precentral-sup-part', 'S_suborbital', 'S_subparietal', 'S_temporal_inf', 'S_temporal_sup', 'S_temporal_transverse']
df = {}
f = [e.rstrip('\n') for e in open('/tmp/stats.csv').readlines()]
f.pop(0)
for each in f:
k,v = each.split(',')
df[k] = v
p = aims.read(parc)
t = aims.TimeTexture_FLOAT(1, len(p))
d = []
for i, each in enumerate(p[0]):
label = int(round(each))
if label != 0:
lab = labels2009[label-2]
v = float(df[lab])
d.append(v)
else:
d.append(0.0)
t[0] = aims.Texture_FLOAT(np.array(d, dtype=np.float32, order='F'))
return t
if __name__ == "__main__":
import argparse, os, os.path as osp
parser = argparse.ArgumentParser(description='map values to cortical surface')
parser.add_argument("-i", dest="tex", help="gyral parcellation texture", type=str, required=True)
parser.add_argument("--csv", dest='csvfile', type=str, help="csv", required=True)
parser.add_argument("-o", dest='output', type=str, help="output texture", required=True)
args = parser.parse_args()
if not osp.isfile(args.tex):
raise Exception('%s should be a valid file'%args.tex)
if not osp.isfile(args.csvfile):
raise Exception('%s should be a valid file'%args.csvfile)
args.tex = osp.abspath(args.tex)
args.csvfile = osp.abspath(args.csvfile)
tex = map_values(args.tex, args.csvfile)
from soma import aims
aims.write(tex, args.output)
name value
G_and_S_frontomargin 0.0289763240869
G_and_S_occipital_inf 0.999782640064
G_and_S_paracentral 0.744812346539
G_and_S_subcentral 0.35751889465
G_and_S_transv_frontopol 0.938343391082
G_and_S_cingul-Ant 0.882785221832
G_and_S_cingul-Mid-Ant 0.572690177111
G_and_S_cingul-Mid-Post 0.495329926174
G_cingul-Post-dorsal 0.33172106481
G_cingul-Post-ventral 0.467316534144
G_cuneus 0.952254959972
G_front_inf-Opercular 0.849856256759
G_front_inf-Orbital 0.09910006389
G_front_inf-Triangul 0.945845415422
G_front_middle 0.364093347606
G_front_sup 0.895487568799
G_Ins_lg_and_S_cent_ins 0.0950805784073
G_insular_short 0.783628698196
G_occipital_middle 0.392149448534
G_occipital_sup 0.986517183269
G_oc-temp_lat-fusifor 0.586764198346
G_oc-temp_med-Lingual 0.160746627118
G_oc-temp_med-Parahip 0.214753543531
G_orbital 0.22100150499
G_pariet_inf-Angular 0.911443343414
G_pariet_inf-Supramar 0.0289763240869
G_parietal_sup 0.639812790341
G_postcentral 0.850072710549
G_precentral 0.433032490176
G_precuneus 0.587352692516
G_rectus 0.662637599343
G_subcallosal 0.19809186756
G_temp_sup-G_T_transv 0.555173185537
G_temp_sup-Lateral 0.340236162662
G_temp_sup-Plan_polar 0.619876895035
G_temp_sup-Plan_tempo 0.779217679366
G_temporal_inf 0.17171214425
G_temporal_middle 0.829631202744
Lat_Fis-ant-Horizont 0.975420598832
Lat_Fis-ant-Vertical 0.126221117017
Lat_Fis-post 0.794625277468
Pole_occipital 0.206592744263
Pole_temporal 0.107933692954
S_calcarine 0.597846514039
S_central 0.746056847635
S_cingul-Marginalis 0.741805364709
S_circular_insula_ant 0.148787578814
S_circular_insula_inf 0.911074837678
S_circular_insula_sup 0.131694923462
S_collat_transv_ant 0.0782497510469
S_collat_transv_post 0.733615437886
S_front_inf 0.60402578165
S_front_middle 0.738086920448
S_front_sup 0.894492338472
S_interm_prim-Jensen 0.871107277146
S_intrapariet_and_P_trans 0.307110929893
S_oc_middle_and_Lunatus 0.607579495831
S_oc_sup_and_transversal 0.66025105252
S_occipital_ant 0.0657394280287
S_oc-temp_lat 0.202807526405
S_oc-temp_med_and_Lingual 0.446036115936
S_orbital_lateral 0.192283902263
S_orbital_med-olfact 0.810572896586
S_orbital-H_Shaped 0.151325455904
S_parieto_occipital 0.388180134283
S_pericallosal 0.440618825309
S_postcentral 0.231716739611
S_precentral-inf-part 0.921634802409
S_precentral-sup-part 0.980025614273
S_suborbital 0.939997946109
S_subparietal 0.887504098085
S_temporal_inf 0.154411568073
S_temporal_sup 0.340427984184
S_temporal_transverse 0.981867974303
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment