Skip to content

Instantly share code, notes, and snippets.

@xgrg
Last active January 24, 2020 22:46
Show Gist options
  • Save xgrg/70dc94e5f410d99c63f543b83ccd65ee to your computer and use it in GitHub Desktop.
Save xgrg/70dc94e5f410d99c63f543b83ccd65ee to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Define functions"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [],
"source": [
"import pyxnat\n",
"from tqdm import tqdm\n",
"from bx import lists\n",
"import numpy as np\n",
"import nibabel as nib\n",
"from glob import glob\n",
"import pandas as pd\n",
"import logging as log\n",
"import json\n",
"\n",
"\n",
"def roistats_from_map(map_fp, atlas_fp, subject, \n",
" func=np.mean):\n",
" '''Extract ROI values from given map'''\n",
" \n",
" def _roistats_from_map(map_fp, atlas, func=np.mean):\n",
" m = np.array(nib.load(map_fp).dataobj)\n",
" n_labels = list(np.unique(atlas))\n",
" label_values = dict([(label, func(m[atlas==label])) \\\n",
" for label in n_labels])\n",
" return label_values\n",
"\n",
" # Load atlas and count ROIs\n",
" atlas_im = nib.load(atlas_fp)\n",
" atlas = np.array(atlas_im.dataobj)\n",
"\n",
" roi_labels = list(np.unique(atlas))\n",
" df = _roistats_from_map(map_fp, atlas, func)\n",
" \n",
" # Name rows and columns and return the DataFrame\n",
" columns = [int(e) for e in roi_labels]\n",
" res = pd.DataFrame(df, columns=columns, index=[subject]) \n",
"\n",
" return res\n",
"\n",
"lut = dict([(int(e),v) for e,v in\\\n",
" json.load(open('/tmp/freesurfer_lut.json')).items()])\n",
"\n",
"\n",
"def normMean(self):\n",
" '''Extract mean intensities from labels from a given \n",
" XNAT resource'''\n",
" \n",
" f1 = list(self.files('*mri/aparc.a2009s+aseg.mgz'))[0]\n",
" f2 = list(self.files('*mri/norm.mgz'))[0]\n",
" \n",
" s_label = f2._uri.split('/')[-3]\n",
" e_id = f2._uri.split('/')[-7]\n",
" \n",
" f1.get('/tmp/aparc.mgz')\n",
" f2.get('/tmp/norm.mgz')\n",
" \n",
" roi_values = roistats_from_map('/tmp/norm.mgz', \n",
" '/tmp/aparc.mgz',\n",
" subject=s_label) \n",
" \n",
" df = pd.DataFrame(roi_values, columns=lut)\n",
" return df.rename(columns=lut).dropna(axis=1)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Do the job over the collection of images and build the Excel"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {
"scrolled": false
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 430/430 [1:55:33<00:00, 12.87s/it]\n\n\n"
]
}
],
"source": [
"x = pyxnat.Interface(config='/home/grg/.xnat_bsc.cfg')\n",
"data = []\n",
"\n",
"vbm = lists.VBM_ALFA_PLUS_AAIC2020_20200117\n",
"\n",
"for each in tqdm(vbm):\n",
" e = x.select.experiment(each)\n",
" r = e.resource('FREESURFER6_HIRES')\n",
" s_label = r._uri.split('/')[-3]\n",
" \n",
" if r.exists():\n",
" df = normMean(r)\n",
" df['id'] = s_label\n",
" data.append(df)\n",
"\n",
"res = pd.concat(data).dropna(axis=1)\n",
"res.to_excel('/tmp/ALFA_PLUS_FreeSurfer6_normMean_20200124.xlsx')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Compare the obtained numbers to the ones provided in aseg"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Unknown</th>\n",
" <th>Left-Cerebral-White-Matter</th>\n",
" <th>Left-Cerebral-Cortex</th>\n",
" <th>Left-Lateral-Ventricle</th>\n",
" <th>Left-Inf-Lat-Vent</th>\n",
" <th>Left-Cerebellum-White-Matter</th>\n",
" <th>Left-Cerebellum-Cortex</th>\n",
" <th>Left-Thalamus-Proper</th>\n",
" <th>Left-Caudate</th>\n",
" <th>Left-Putamen</th>\n",
" <th>...</th>\n",
" <th>ctx_rh_S_pericallosal</th>\n",
" <th>ctx_rh_S_postcentral</th>\n",
" <th>ctx_rh_S_precentral-inf-part</th>\n",
" <th>ctx_rh_S_precentral-sup-part</th>\n",
" <th>ctx_rh_S_suborbital</th>\n",
" <th>ctx_rh_S_subparietal</th>\n",
" <th>ctx_rh_S_temporal_inf</th>\n",
" <th>ctx_rh_S_temporal_sup</th>\n",
" <th>ctx_rh_S_temporal_transverse</th>\n",
" <th>id</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>10019</th>\n",
" <td>1.016431</td>\n",
" <td>97.309149</td>\n",
" <td>33.833333</td>\n",
" <td>17.132928</td>\n",
" <td>35.111708</td>\n",
" <td>80.651961</td>\n",
" <td>51.22696</td>\n",
" <td>79.790825</td>\n",
" <td>71.61588</td>\n",
" <td>76.644843</td>\n",
" <td>...</td>\n",
" <td>62.998485</td>\n",
" <td>54.057011</td>\n",
" <td>53.788426</td>\n",
" <td>53.425984</td>\n",
" <td>50.710706</td>\n",
" <td>55.237634</td>\n",
" <td>46.745794</td>\n",
" <td>52.414558</td>\n",
" <td>58.212389</td>\n",
" <td>BBRC_E00046</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>1 rows × 195 columns</p>\n",
"</div>"
],
"text/plain": [
" Unknown Left-Cerebral-White-Matter Left-Cerebral-Cortex \\\n",
"10019 1.016431 97.309149 33.833333 \n",
"\n",
" Left-Lateral-Ventricle Left-Inf-Lat-Vent \\\n",
"10019 17.132928 35.111708 \n",
"\n",
" Left-Cerebellum-White-Matter Left-Cerebellum-Cortex \\\n",
"10019 80.651961 51.22696 \n",
"\n",
" Left-Thalamus-Proper Left-Caudate Left-Putamen ... \\\n",
"10019 79.790825 71.61588 76.644843 ... \n",
"\n",
" ctx_rh_S_pericallosal ctx_rh_S_postcentral \\\n",
"10019 62.998485 54.057011 \n",
"\n",
" ctx_rh_S_precentral-inf-part ctx_rh_S_precentral-sup-part \\\n",
"10019 53.788426 53.425984 \n",
"\n",
" ctx_rh_S_suborbital ctx_rh_S_subparietal ctx_rh_S_temporal_inf \\\n",
"10019 50.710706 55.237634 46.745794 \n",
"\n",
" ctx_rh_S_temporal_sup ctx_rh_S_temporal_transverse id \n",
"10019 52.414558 58.212389 BBRC_E00046 \n",
"\n",
"[1 rows x 195 columns]"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# first subject of the list\n",
"data[0] "
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>measurement</th>\n",
" <th>region</th>\n",
" <th>value</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>201</th>\n",
" <td>normMean</td>\n",
" <td>Left-Lateral-Ventricle</td>\n",
" <td>17.1329</td>\n",
" </tr>\n",
" <tr>\n",
" <th>202</th>\n",
" <td>normMean</td>\n",
" <td>Left-Inf-Lat-Vent</td>\n",
" <td>35.1117</td>\n",
" </tr>\n",
" <tr>\n",
" <th>203</th>\n",
" <td>normMean</td>\n",
" <td>Left-Cerebellum-White-Matter</td>\n",
" <td>80.6520</td>\n",
" </tr>\n",
" <tr>\n",
" <th>204</th>\n",
" <td>normMean</td>\n",
" <td>Left-Cerebellum-Cortex</td>\n",
" <td>51.2270</td>\n",
" </tr>\n",
" <tr>\n",
" <th>205</th>\n",
" <td>normMean</td>\n",
" <td>Left-Thalamus-Proper</td>\n",
" <td>79.7908</td>\n",
" </tr>\n",
" <tr>\n",
" <th>206</th>\n",
" <td>normMean</td>\n",
" <td>Left-Caudate</td>\n",
" <td>71.6159</td>\n",
" </tr>\n",
" <tr>\n",
" <th>207</th>\n",
" <td>normMean</td>\n",
" <td>Left-Putamen</td>\n",
" <td>76.6448</td>\n",
" </tr>\n",
" <tr>\n",
" <th>208</th>\n",
" <td>normMean</td>\n",
" <td>Left-Pallidum</td>\n",
" <td>98.6589</td>\n",
" </tr>\n",
" <tr>\n",
" <th>209</th>\n",
" <td>normMean</td>\n",
" <td>3rd-Ventricle</td>\n",
" <td>19.0765</td>\n",
" </tr>\n",
" <tr>\n",
" <th>210</th>\n",
" <td>normMean</td>\n",
" <td>4th-Ventricle</td>\n",
" <td>17.8495</td>\n",
" </tr>\n",
" <tr>\n",
" <th>211</th>\n",
" <td>normMean</td>\n",
" <td>Brain-Stem</td>\n",
" <td>75.3624</td>\n",
" </tr>\n",
" <tr>\n",
" <th>212</th>\n",
" <td>normMean</td>\n",
" <td>Left-Hippocampus</td>\n",
" <td>53.5058</td>\n",
" </tr>\n",
" <tr>\n",
" <th>213</th>\n",
" <td>normMean</td>\n",
" <td>Left-Amygdala</td>\n",
" <td>52.3038</td>\n",
" </tr>\n",
" <tr>\n",
" <th>214</th>\n",
" <td>normMean</td>\n",
" <td>CSF</td>\n",
" <td>25.9545</td>\n",
" </tr>\n",
" <tr>\n",
" <th>215</th>\n",
" <td>normMean</td>\n",
" <td>Left-Accumbens-area</td>\n",
" <td>66.9074</td>\n",
" </tr>\n",
" <tr>\n",
" <th>216</th>\n",
" <td>normMean</td>\n",
" <td>Left-VentralDC</td>\n",
" <td>88.6360</td>\n",
" </tr>\n",
" <tr>\n",
" <th>217</th>\n",
" <td>normMean</td>\n",
" <td>Left-vessel</td>\n",
" <td>50.8390</td>\n",
" </tr>\n",
" <tr>\n",
" <th>218</th>\n",
" <td>normMean</td>\n",
" <td>Left-choroid-plexus</td>\n",
" <td>47.8562</td>\n",
" </tr>\n",
" <tr>\n",
" <th>219</th>\n",
" <td>normMean</td>\n",
" <td>Right-Lateral-Ventricle</td>\n",
" <td>16.8327</td>\n",
" </tr>\n",
" <tr>\n",
" <th>220</th>\n",
" <td>normMean</td>\n",
" <td>Right-Inf-Lat-Vent</td>\n",
" <td>36.6024</td>\n",
" </tr>\n",
" <tr>\n",
" <th>221</th>\n",
" <td>normMean</td>\n",
" <td>Right-Cerebellum-White-Matter</td>\n",
" <td>76.6231</td>\n",
" </tr>\n",
" <tr>\n",
" <th>222</th>\n",
" <td>normMean</td>\n",
" <td>Right-Cerebellum-Cortex</td>\n",
" <td>48.0209</td>\n",
" </tr>\n",
" <tr>\n",
" <th>223</th>\n",
" <td>normMean</td>\n",
" <td>Right-Thalamus-Proper</td>\n",
" <td>77.1640</td>\n",
" </tr>\n",
" <tr>\n",
" <th>224</th>\n",
" <td>normMean</td>\n",
" <td>Right-Caudate</td>\n",
" <td>66.7911</td>\n",
" </tr>\n",
" <tr>\n",
" <th>225</th>\n",
" <td>normMean</td>\n",
" <td>Right-Putamen</td>\n",
" <td>72.4695</td>\n",
" </tr>\n",
" <tr>\n",
" <th>226</th>\n",
" <td>normMean</td>\n",
" <td>Right-Pallidum</td>\n",
" <td>95.4322</td>\n",
" </tr>\n",
" <tr>\n",
" <th>227</th>\n",
" <td>normMean</td>\n",
" <td>Right-Hippocampus</td>\n",
" <td>54.0092</td>\n",
" </tr>\n",
" <tr>\n",
" <th>228</th>\n",
" <td>normMean</td>\n",
" <td>Right-Amygdala</td>\n",
" <td>48.6094</td>\n",
" </tr>\n",
" <tr>\n",
" <th>229</th>\n",
" <td>normMean</td>\n",
" <td>Right-Accumbens-area</td>\n",
" <td>66.0330</td>\n",
" </tr>\n",
" <tr>\n",
" <th>230</th>\n",
" <td>normMean</td>\n",
" <td>Right-VentralDC</td>\n",
" <td>85.5522</td>\n",
" </tr>\n",
" <tr>\n",
" <th>231</th>\n",
" <td>normMean</td>\n",
" <td>Right-vessel</td>\n",
" <td>46.8429</td>\n",
" </tr>\n",
" <tr>\n",
" <th>232</th>\n",
" <td>normMean</td>\n",
" <td>Right-choroid-plexus</td>\n",
" <td>48.2169</td>\n",
" </tr>\n",
" <tr>\n",
" <th>233</th>\n",
" <td>normMean</td>\n",
" <td>5th-Ventricle</td>\n",
" <td>0.0000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>234</th>\n",
" <td>normMean</td>\n",
" <td>WM-hypointensities</td>\n",
" <td>67.7161</td>\n",
" </tr>\n",
" <tr>\n",
" <th>235</th>\n",
" <td>normMean</td>\n",
" <td>Left-WM-hypointensities</td>\n",
" <td>0.0000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>236</th>\n",
" <td>normMean</td>\n",
" <td>Right-WM-hypointensities</td>\n",
" <td>0.0000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>237</th>\n",
" <td>normMean</td>\n",
" <td>non-WM-hypointensities</td>\n",
" <td>0.0000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>238</th>\n",
" <td>normMean</td>\n",
" <td>Left-non-WM-hypointensities</td>\n",
" <td>0.0000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>239</th>\n",
" <td>normMean</td>\n",
" <td>Right-non-WM-hypointensities</td>\n",
" <td>0.0000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>240</th>\n",
" <td>normMean</td>\n",
" <td>Optic-Chiasm</td>\n",
" <td>73.9953</td>\n",
" </tr>\n",
" <tr>\n",
" <th>241</th>\n",
" <td>normMean</td>\n",
" <td>CC_Posterior</td>\n",
" <td>104.9961</td>\n",
" </tr>\n",
" <tr>\n",
" <th>242</th>\n",
" <td>normMean</td>\n",
" <td>CC_Mid_Posterior</td>\n",
" <td>91.9224</td>\n",
" </tr>\n",
" <tr>\n",
" <th>243</th>\n",
" <td>normMean</td>\n",
" <td>CC_Central</td>\n",
" <td>98.5755</td>\n",
" </tr>\n",
" <tr>\n",
" <th>244</th>\n",
" <td>normMean</td>\n",
" <td>CC_Mid_Anterior</td>\n",
" <td>102.8931</td>\n",
" </tr>\n",
" <tr>\n",
" <th>245</th>\n",
" <td>normMean</td>\n",
" <td>CC_Anterior</td>\n",
" <td>99.8819</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" measurement region value\n",
"201 normMean Left-Lateral-Ventricle 17.1329\n",
"202 normMean Left-Inf-Lat-Vent 35.1117\n",
"203 normMean Left-Cerebellum-White-Matter 80.6520\n",
"204 normMean Left-Cerebellum-Cortex 51.2270\n",
"205 normMean Left-Thalamus-Proper 79.7908\n",
"206 normMean Left-Caudate 71.6159\n",
"207 normMean Left-Putamen 76.6448\n",
"208 normMean Left-Pallidum 98.6589\n",
"209 normMean 3rd-Ventricle 19.0765\n",
"210 normMean 4th-Ventricle 17.8495\n",
"211 normMean Brain-Stem 75.3624\n",
"212 normMean Left-Hippocampus 53.5058\n",
"213 normMean Left-Amygdala 52.3038\n",
"214 normMean CSF 25.9545\n",
"215 normMean Left-Accumbens-area 66.9074\n",
"216 normMean Left-VentralDC 88.6360\n",
"217 normMean Left-vessel 50.8390\n",
"218 normMean Left-choroid-plexus 47.8562\n",
"219 normMean Right-Lateral-Ventricle 16.8327\n",
"220 normMean Right-Inf-Lat-Vent 36.6024\n",
"221 normMean Right-Cerebellum-White-Matter 76.6231\n",
"222 normMean Right-Cerebellum-Cortex 48.0209\n",
"223 normMean Right-Thalamus-Proper 77.1640\n",
"224 normMean Right-Caudate 66.7911\n",
"225 normMean Right-Putamen 72.4695\n",
"226 normMean Right-Pallidum 95.4322\n",
"227 normMean Right-Hippocampus 54.0092\n",
"228 normMean Right-Amygdala 48.6094\n",
"229 normMean Right-Accumbens-area 66.0330\n",
"230 normMean Right-VentralDC 85.5522\n",
"231 normMean Right-vessel 46.8429\n",
"232 normMean Right-choroid-plexus 48.2169\n",
"233 normMean 5th-Ventricle 0.0000\n",
"234 normMean WM-hypointensities 67.7161\n",
"235 normMean Left-WM-hypointensities 0.0000\n",
"236 normMean Right-WM-hypointensities 0.0000\n",
"237 normMean non-WM-hypointensities 0.0000\n",
"238 normMean Left-non-WM-hypointensities 0.0000\n",
"239 normMean Right-non-WM-hypointensities 0.0000\n",
"240 normMean Optic-Chiasm 73.9953\n",
"241 normMean CC_Posterior 104.9961\n",
"242 normMean CC_Mid_Posterior 91.9224\n",
"243 normMean CC_Central 98.5755\n",
"244 normMean CC_Mid_Anterior 102.8931\n",
"245 normMean CC_Anterior 99.8819"
]
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# same subject's aseg stats\n",
"e = x.select.experiment('BBRC_E00046')\n",
"r = e.resource('FREESURFER6_HIRES')\n",
"r.aseg().query('measurement == \"normMean\"')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment