Skip to content

Instantly share code, notes, and snippets.

@nicholsn
Created December 17, 2014 23:23
Show Gist options
  • Select an option

  • Save nicholsn/4f6ca767e6a59dc1dbd9 to your computer and use it in GitHub Desktop.

Select an option

Save nicholsn/4f6ca767e6a59dc1dbd9 to your computer and use it in GitHub Desktop.
{
"metadata": {
"name": "",
"signature": "sha256:b52d91c59219130c59f60a3b4d38f44bf63c0bb281cf4e10c43b025291a31537"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## T1 and T2 Usability Report - Baseline\n",
"\n",
"- select the earliest scanning session based on date\n",
"- lookup all the unique scan types in the system\n",
"- identify T1 and T1 scans\n",
"- lookup and compare usibiity of T1 and T2 scans"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import os\n",
"import sys\n",
"import datetime\n",
"import ConfigParser\n",
"import xml.etree.ElementTree as ET \n",
"\n",
"import pyxnat"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"cfg = ConfigParser.SafeConfigParser()"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"cfg.read(os.path.expanduser('~/.ncanda.cfg'))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 3,
"text": [
"['/Users/nolan/.ncanda.cfg']"
]
}
],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"user = cfg.get('xnat', 'user')\n",
"passwd = cfg.get('xnat', 'pass')"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"xnat = pyxnat.Interface(server='https://ncanda.sri.com/xnat',\n",
" user=user,\n",
" password=passwd,\n",
" cachedir='/tmp')"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"proj = xnat.select.project('accepted')"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"subject = proj.subject('NCANDA_S00457')\n",
"list(subject.experiments())"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 7,
"text": [
"[<Experiment Object> NCANDA_E00916, <Experiment Object> NCANDA_E00949]"
]
}
],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def get_baseline_mrsession(subject):\n",
" dt_list = []\n",
" base_idx = int\n",
" exps = subject.experiments()\n",
" for exp in exps:\n",
" # grab the experiment date\n",
" dt = exp.xpath('//xnat:date')[0].text\n",
" year, month, day = [int(d) for d in dt.split('-')]\n",
" dt_list.append(datetime.date(year, month, day))\n",
" # find the lowest date\n",
" if dt_list:\n",
" base_idx = dt_list.index(min(dt_list))\n",
" return exps[base_idx]\n",
" else:\n",
" print ValueError(\"Subjet: {0} is missing a scan date\".format(subject.id()))"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 56
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"exp = get_baseline_mrsession(subject)\n",
"print exp"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"<Experiment Object> NCANDA_E00916\n"
]
}
],
"prompt_number": 9
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# get a unique set of all scan types\n",
"unique = set()\n",
"for e in proj.experiments():\n",
" [unique.add(t) for t in e.xpath('//xnat:scan/@type')]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 10
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"unique"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 11,
"text": [
"{'Ax DTI 60',\n",
" 'MoCoSeries',\n",
" 'PhoenixZIPReport',\n",
" 'SPGR_GRAPPA Iron',\n",
" 'Screen Save',\n",
" 'iron scan',\n",
" 'localizer',\n",
" 'ncanda-calibration-v1',\n",
" 'ncanda-dti60b1000-v1',\n",
" 'ncanda-dti60b1000-v1_ADC',\n",
" 'ncanda-dti60b1000-v1_ColFA',\n",
" 'ncanda-dti60b1000-v1_FA',\n",
" 'ncanda-dti60b1000-v1_TENSOR',\n",
" 'ncanda-dti60b1000-v1_TRACEW',\n",
" 'ncanda-dti6b500pepolar-v1',\n",
" 'ncanda-dti6b500pepolar-v1_TRACEW',\n",
" 'ncanda-fmri-antisaccade-v1',\n",
" 'ncanda-fmri-ringrewards-v1',\n",
" 'ncanda-fmri-ringrewards-v1/repeat of 2',\n",
" 'ncanda-fmri-ringrewards-v2',\n",
" 'ncanda-fmri-ringrewards-v3',\n",
" 'ncanda-fmri-ringrewards-v4',\n",
" 'ncanda-grefieldmap-v1',\n",
" 'ncanda-localizer-v1',\n",
" 'ncanda-mprage-v1',\n",
" 'ncanda-rsfmri-v1',\n",
" 'ncanda-t1spgr-v1',\n",
" 'ncanda-t2fse-v1'}"
]
}
],
"prompt_number": 11
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"t1 = ['ncanda-t1spgr-v1', 'ncanda-mprage-v1']\n",
"t2 = ['ncanda-t2fse-v1']"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 12
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def get_scan_quality(experiment, scan_type):\n",
" quality = dict()\n",
" try:\n",
" for scan in experiment.scans():\n",
" if scan.xpath('//@type')[0] in scan_type:\n",
" quality.update({scan: scan.xpath('//xnat:quality')[0].text})\n",
" except AttributeError, e:\n",
" print str(e)\n",
" # for more than scan_type return usable\n",
" if len(quality.keys()) > 1:\n",
" print quality\n",
" for k,v in quality.iteritems():\n",
" if v == 'usable':\n",
" return dict(k=v)\n",
" else:\n",
" quality = dict(k=v)\n",
" return quality"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 61
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print get_scan_quality(exp, t1)\n",
"print get_scan_quality(exp, t2)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"{<Scan Object> 4: 'unusable'}\n",
"{<Scan Object> 3: 'usable'}\n"
]
}
],
"prompt_number": 37
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def check_t1_t2_usable(experiment):\n",
" quality = False\n",
" t1 = ['ncanda-t1spgr-v1', 'ncanda-mprage-v1']\n",
" t2 = ['ncanda-t2fse-v1']\n",
" t1_quality = get_scan_quality(experiment, t1)\n",
" t2_quality = get_scan_quality(experiment, t2)\n",
" try:\n",
" if t1_quality.values()[0] == t2_quality.values()[0]:\n",
" quality = True\n",
" except IndexError, e:\n",
" print str(e)\n",
" return quality"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 63
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"check_t1_t2_usable(exp)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 40,
"text": [
"False"
]
}
],
"prompt_number": 40
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def check_accepted(project):\n",
" result = []\n",
" for sub in project.subjects():\n",
" base_exp = get_baseline_mrsession(sub)\n",
" try:\n",
" base_dt = base_exp.xpath('//xnat:date')[0].text\n",
" except AttributeError, e:\n",
" print str(e)\n",
" quality = check_t1_t2_usable(base_exp)\n",
" try:\n",
" result.append((sub.id(), base_exp.id(), base_dt, quality))\n",
" except AttributeError, e:\n",
" print str(e)\n",
" return result\n",
" "
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 65
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"res = check_accepted(proj)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"{<Scan Object> 2: 'unusable', <Scan Object> 16: 'usable'}\n",
"{<Scan Object> 5: 'usable', <Scan Object> 3: 'unusable'}"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"{<Scan Object> 3: 'usable-extra', <Scan Object> 11: 'usable'}"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"{<Scan Object> 4: 'usable-extra', <Scan Object> 5: 'usable'}"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"{<Scan Object> 2: 'usable-extra', <Scan Object> 16: 'usable'}"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Subjet: NCANDA_S00268 is missing a scan date"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"'NoneType' object has no attribute 'xpath'\n",
"'NoneType' object has no attribute 'scans'\n",
"'NoneType' object has no attribute 'scans'\n",
"list index out of range\n",
"'NoneType' object has no attribute 'id'"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"{<Scan Object> 4: 'usable', <Scan Object> 3: 'unusable'}"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"{<Scan Object> 4: 'usable', <Scan Object> 3: 'unusable'}"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"{<Scan Object> 3: 'unusable', <Scan Object> 4: 'usable'}"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"{<Scan Object> 4: 'usable', <Scan Object> 11: 'unusable'}"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"{<Scan Object> 4: 'usable', <Scan Object> 44: 'usable-extra'}"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"list index out of range"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"{<Scan Object> 2: 'usable', <Scan Object> 3: 'usable-extra'}"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"{<Scan Object> 3: 'unusable', <Scan Object> 24: 'usable'}"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"{<Scan Object> 3: 'unusable', <Scan Object> 12: 'usable'}"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 66
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print res[0]\n",
"print len(res)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"('NCANDA_S00057', 'NCANDA_E00059', '2013-01-31', True)\n",
"526\n"
]
}
],
"prompt_number": 87
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"with open('usable_t1_t2_baseline.csv', 'w') as fi:\n",
" fi.flush()\n",
" hdr = ','.join(['participant', 'study', 'study_date', 't1_t2_usable'])\n",
" fi.writelines(hdr)\n",
" fi.write('\\n')\n",
" for row in res:\n",
" csv = ','.join(map(str, row))\n",
" fi.writelines(csv)\n",
" fi.write('\\n')\n",
" fi.close()\n",
" "
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 90
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment