Skip to content

Instantly share code, notes, and snippets.

@jminas
Created July 17, 2016 15:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jminas/0147437f6ca018a61b64db6e1a02f7ef to your computer and use it in GitHub Desktop.
Save jminas/0147437f6ca018a61b64db6e1a02f7ef to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%reset -f\n",
"import os\n",
"import numpy as np\n",
"from numpy import inf\n",
"from numpy import nan\n",
"\n",
"from glob import glob\n",
"\n",
"import scipy\n",
"\n",
"import nibabel as nib\n",
"\n",
"import nilearn\n",
"import nilearn.plotting\n",
"\n",
"\n",
"# Create thumbnail images of subject by task and save out to png\n",
"# Perform correlation of baseline speech network with neurosynth network\n",
"# Annotate with whether null trials were presented (in which case a baseline speech network should be present)\n",
"\n",
"## Known issues:\n",
"# Annotation of null/not null assumes first file returned describes null events\n",
"# For multi session subjects, not clear what the l1 output corresponds to (session1, 2, ??)\n",
"\n",
"## About\n",
"# Greg Ciccarelli\n",
"# Nov 2, 2015"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import Image\n",
"import ImageFont, ImageDraw"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Dictionary of task and contrast number corresponding to baseline speech network\n",
"tasks = sorted([\"task001\",\"task002\",\"task003\",\"task005\",\"task006\",\"task007\"])\n",
"#tasks = sorted(tasks)\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"img_path = '/om/project/voice/processedData/plots/speech_baseline/cool'\n",
"l1dir = '/om/project/voice/processedData/l1analysis/l1output_20151202'\n",
"model = 'model200'"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Neurosynth speech network\n",
"speech_img = nib.load('/om/user/mathiasg/projects/voice/speechproduction_pAgF_z_FDR_0.01.nii.gz')\n",
"speech_mat = np.array(speech_img.get_data())\n",
"speech_vec = np.copy(speech_mat)\n",
"speech_vec = np.reshape(speech_vec, (np.size(speech_vec),1))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"imgs = os.listdir(img_path)\n",
"imgs = sorted(imgs)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [],
"source": [
"subjs = [] \n",
"for sub in imgs:\n",
" if sub[:8] not in subjs:\n",
" subjs.append(sub[:8])"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"outliers = np.genfromtxt(\"outliers_voice.txt\",dtype=str)\n",
"\n",
"def countMotion(s,t):\n",
" count = 0\n",
" for x in outliers:\n",
" if s in x and t in x:\n",
" count += 1\n",
" return count"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def getRho(subj,task):\n",
" \n",
" zpath = os.path.join(l1dir, model, task, model, task, \n",
" subj, 'zstats', 'mni', 'zstat' + '01.nii.gz')\n",
" # Check if image\n",
" if os.path.exists(zpath):\n",
" zstat = nib.load(zpath)\n",
" zstat_mat = np.array(zstat.get_data())\n",
" zstat_vec = np.copy(zstat_mat)\n",
" zstat_vec = np.reshape(zstat_vec, (np.size(zstat_vec),1)) \n",
" \n",
" [rho, p] = scipy.stats.pearsonr(speech_vec, zstat_vec)\n",
" format(float(rho),'.3f')\n",
" \n",
" else:\n",
" \n",
" rho = -1\n",
" return rho"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Assume first file is representative for the subject- \n",
"# not true under unusual circumstance of multi session subject\n",
"def checkNulls(subj,task):\n",
" hasNull = False\n",
" file_path_psylog = glob(os.path.join('/om/project/voice/processedData/audio/psycholog/',\n",
" task, subj, 'session00*/R00*/*.tsv'))\n",
" \n",
" if file_path_psylog:\n",
" file_path_psylog= file_path_psylog[0]\n",
" data = np.genfromtxt(file_path_psylog, delimiter='\\t', dtype=str)\n",
" if 'NULL' in data[:,2] or 'null' in data[:,2]:\n",
" hasNull = True\n",
" return hasNull"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def plot_subj_by_task(subjs, tasks, img_path, file_name_save): #, speech_vec):\n",
" \n",
" ## Input\n",
" \n",
" ## Output\n",
" # *.png\n",
" \n",
" # NOTE: Image module transposes row and column dimensions\n",
" \n",
" N_px_row = 400\n",
" N_px_col = 600\n",
" shiftx = 90\n",
" shifty = 100\n",
" \n",
" N_row = len(subjs)\n",
" N_col = len(tasks) #*2 #Times 2 for lh and rh\n",
" #creates a new empty image, RGB mode, and size 400 by 400.\n",
" new_im = Image.new('RGB', (N_col*N_px_col + shiftx, N_row*N_px_row + shifty))\n",
" \n",
" for idx_subj, subj in enumerate(subjs):\n",
" #\n",
" print \"==\"\n",
" print subj\n",
"\n",
" for idx_task, task in enumerate(tasks):\n",
" print idx_task\n",
" print task\n",
" \n",
" plot_img = os.path.join(img_path, '%s_%s.png' % (subj,task))\n",
" \n",
" #if task == tasks[-1] and subj == subjs[-1]: ##add color bar for last image\n",
" #plot_img = '/om/project/voice/processedData/plots/speech_baseline/Archive' + \\\n",
" # '/std_displaythresh/%s_%s.png' % (subj,task)\n",
" \n",
" # Check for image\n",
" if os.path.exists(plot_img):\n",
" \n",
" #opens an image of brain:\n",
" im = Image.open(plot_img)\n",
" #resize so it is no bigger than 100,100\n",
" im.thumbnail((N_px_col,N_px_row))\n",
" #paste the image at location i,j:\n",
" new_im.paste(im, (idx_task*N_px_col + shiftx,idx_subj*N_px_row + shifty))\n",
" draw = ImageDraw.Draw(new_im)\n",
" #font = ImageFont.truetype(\"arial.ttf\", 15)\n",
" #font = ImageFont.load(\"arial.pil\")\n",
" #draw.text((idx_task*N_px_col+20,idx_subj*N_px_row+70), \n",
" # os.path.split(plot_img)[1] + \" \" + message.upper())\n",
" \n",
" \n",
" #check motion outliers ====================================================\n",
" motion = countMotion(subj,task)\n",
" if motion == 0:\n",
" color = Image.open(\"green.png\")\n",
" message = \"No motion outliers\"\n",
" elif motion == 1:\n",
" color = Image.open(\"yellow.png\")\n",
" message = \"1 outlier - run either >0.10 or missing\"\n",
" elif motion >= 2:\n",
" color = Image.open(\"red.png\")\n",
" message = \"2 or more outliers\"\n",
" else:\n",
" continue\n",
" color.thumbnail((N_px_col/11,N_px_row))\n",
" new_im.paste(color, (idx_task*N_px_col+40, idx_subj*N_px_row+shifty))\n",
" draw = ImageDraw.Draw(new_im)\n",
" draw.text((idx_task*N_px_col+50, idx_subj*N_px_row+shifty+20), \n",
" \"motion\", fill=(0,0,0,0))\n",
" \n",
" # label participant and print message\n",
" draw.text((idx_task*N_px_col+20,idx_subj*N_px_row+70), \n",
" os.path.split(plot_img)[1] + \" \" + message.upper())\n",
"\n",
" \n",
" \n",
" \n",
" #neurosynth corr ====================================================\n",
" rho = getRho(subj,task)\n",
" \n",
" if rho >= 0.15:\n",
" color = Image.open(\"green.png\")\n",
" elif rho == -1:\n",
" color = Image.open(\"red.png\")\n",
" else:\n",
" color = Image.open(\"yellow.png\")\n",
" color.thumbnail((N_px_col/11,N_px_row))\n",
" new_im.paste(color, (idx_task*N_px_col+40, idx_subj*N_px_row+shifty+80))\n",
" draw = ImageDraw.Draw(new_im)\n",
" #put rho in square\n",
" draw.text((idx_task*N_px_col+50, idx_subj*N_px_row+shifty+100), \n",
" str(round(rho,3)), fill=(0,0,0,0))\n",
" \n",
" #null trials ====================================================\n",
" hasNull = checkNulls(subj,task)\n",
" if hasNull:\n",
" color = Image.open(\"green.png\")\n",
" else:\n",
" color = Image.open(\"red.png\")\n",
" color.thumbnail((N_px_col/11,N_px_row))\n",
" new_im.paste(color, (idx_task*N_px_col+40, idx_subj*N_px_row+shifty+160))\n",
" draw = ImageDraw.Draw(new_im)\n",
" draw.text((idx_task*N_px_col+55, idx_subj*N_px_row+shifty+180), \n",
" \"null\", fill=(0,0,0,0))\n",
" \n",
" \n",
" # if img not found - why?\n",
" else:\n",
" draw = ImageDraw.Draw(new_im)\n",
" draw.text((idx_task*N_px_col+200,idx_subj*N_px_row+300), \n",
" \"missing %s %s\" % (subj,task), fill=(255,0,0,255))\n",
" \n",
"\n",
" \n",
" #new_im.show()\n",
" new_im.save(file_name_save) \n",
"\n",
" #f.savefig(file_name_save) \n",
" #plt.close(f) "
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"'0.155'"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Tests\n",
"rho = 0.1546\n",
"#str(round(rho,3))\n",
"str(format(rho,'.3f'))\n",
"#str(format(round((rho,3)),'.3f'))"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"subs8 = []\n",
"for sub in subjs:\n",
" if sub[:6] == 'voice8':\n",
" subs8.append(sub)\n",
"\n",
"subs9 = []\n",
"for sub in subjs:\n",
" if sub[:6] == 'voice9':\n",
" subs9.append(sub)"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"==\n",
"voice854\n",
"0\n",
"task001\n",
"1\n",
"task002\n",
"2\n",
"task003\n",
"3\n",
"task005\n",
"4\n",
"task006\n",
"5\n",
"task007\n",
"==\n",
"voice862\n",
"0\n",
"task001\n",
"1\n",
"task002\n",
"2\n",
"task003\n",
"3\n",
"task005\n",
"4\n",
"task006\n",
"5\n",
"task007\n",
"==\n",
"voice864\n",
"0\n",
"task001\n",
"1\n",
"task002\n",
"2\n",
"task003\n",
"3\n",
"task005\n",
"4\n",
"task006\n",
"5\n",
"task007\n",
"==\n",
"voice867\n",
"0\n",
"task001\n",
"1\n",
"task002\n",
"2\n",
"task003\n",
"3\n",
"task005\n",
"4\n",
"task006\n",
"5\n",
"task007\n",
"==\n",
"voice872\n",
"0\n",
"task001\n",
"1\n",
"task002\n",
"2\n",
"task003\n",
"3\n",
"task005\n",
"4\n",
"task006\n",
"5\n",
"task007\n",
"==\n",
"voice873\n",
"0\n",
"task001\n",
"1\n",
"task002\n",
"2\n",
"task003\n",
"3\n",
"task005\n",
"4\n",
"task006\n",
"5\n",
"task007\n",
"==\n",
"voice875\n",
"0\n",
"task001\n",
"1\n",
"task002\n",
"2\n",
"task003\n",
"3\n",
"task005\n",
"4\n",
"task006\n",
"5\n",
"task007\n",
"==\n",
"voice877\n",
"0\n",
"task001\n",
"1\n",
"task002\n",
"2\n",
"task003\n",
"3\n",
"task005\n",
"4\n",
"task006\n",
"5\n",
"task007\n",
"==\n",
"voice880\n",
"0\n",
"task001\n",
"1\n",
"task002\n",
"2\n",
"task003\n",
"3\n",
"task005\n",
"4\n",
"task006\n",
"5\n",
"task007\n",
"==\n",
"voice884\n",
"0\n",
"task001\n",
"1\n",
"task002\n",
"2\n",
"task003\n",
"3\n",
"task005\n",
"4\n",
"task006\n",
"5\n",
"task007\n",
"==\n",
"voice886\n",
"0\n",
"task001\n",
"1\n",
"task002\n",
"2\n",
"task003\n",
"3\n",
"task005\n",
"4\n",
"task006\n",
"5\n",
"task007\n",
"==\n",
"voice889\n",
"0\n",
"task001\n",
"1\n",
"task002\n",
"2\n",
"task003\n",
"3\n",
"task005\n",
"4\n",
"task006\n",
"5\n",
"task007\n",
"==\n",
"voice891\n",
"0\n",
"task001\n",
"1\n",
"task002\n",
"2\n",
"task003\n",
"3\n",
"task005\n",
"4\n",
"task006\n",
"5\n",
"task007\n",
"==\n",
"voice893\n",
"0\n",
"task001\n",
"1\n",
"task002\n",
"2\n",
"task003\n",
"3\n",
"task005\n",
"4\n",
"task006\n",
"5\n",
"task007\n",
"==\n",
"voice894\n",
"0\n",
"task001\n",
"1\n",
"task002\n",
"2\n",
"task003\n",
"3\n",
"task005\n",
"4\n",
"task006\n",
"5\n",
"task007\n",
"==\n",
"voice895\n",
"0\n",
"task001\n",
"1\n",
"task002\n",
"2\n",
"task003\n",
"3\n",
"task005\n",
"4\n",
"task006\n",
"5\n",
"task007\n",
"==\n",
"voice896\n",
"0\n",
"task001\n",
"1\n",
"task002\n",
"2\n",
"task003\n",
"3\n",
"task005\n",
"4\n",
"task006\n",
"5\n",
"task007\n",
"==\n",
"voice897\n",
"0\n",
"task001\n",
"1\n",
"task002\n",
"2\n",
"task003\n",
"3\n",
"task005\n",
"4\n",
"task006\n",
"5\n",
"task007\n",
"==\n",
"voice898\n",
"0\n",
"task001\n",
"1\n",
"task002\n",
"2\n",
"task003\n",
"3\n",
"task005\n",
"4\n",
"task006\n",
"5\n",
"task007\n"
]
}
],
"source": [
"# save 800s\n",
"file_name_save = '20160108_voice8xx.png'\n",
"plot_subj_by_task(subs8, tasks, img_path, file_name_save)"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"==\n",
"voice973\n",
"0\n",
"task001\n",
"1\n",
"task002\n",
"2\n",
"task003\n",
"3\n",
"task005\n",
"4\n",
"task006\n",
"5\n",
"task007\n",
"==\n",
"voice974\n",
"0\n",
"task001\n",
"1\n",
"task002\n",
"2\n",
"task003\n",
"3\n",
"task005\n",
"4\n",
"task006\n",
"5\n",
"task007\n",
"==\n",
"voice975\n",
"0\n",
"task001\n",
"1\n",
"task002\n",
"2\n",
"task003\n",
"3\n",
"task005\n",
"4\n",
"task006\n",
"5\n",
"task007\n",
"==\n",
"voice976\n",
"0\n",
"task001\n",
"1\n",
"task002\n",
"2\n",
"task003\n",
"3\n",
"task005\n",
"4\n",
"task006\n",
"5\n",
"task007\n",
"==\n",
"voice978\n",
"0\n",
"task001\n",
"1\n",
"task002\n",
"2\n",
"task003\n",
"3\n",
"task005\n",
"4\n",
"task006\n",
"5\n",
"task007\n",
"==\n",
"voice979\n",
"0\n",
"task001\n",
"1\n",
"task002\n",
"2\n",
"task003\n",
"3\n",
"task005\n",
"4\n",
"task006\n",
"5\n",
"task007\n",
"==\n",
"voice980\n",
"0\n",
"task001\n",
"1\n",
"task002\n",
"2\n",
"task003\n",
"3\n",
"task005\n",
"4\n",
"task006\n",
"5\n",
"task007\n",
"==\n",
"voice981\n",
"0\n",
"task001\n",
"1\n",
"task002\n",
"2\n",
"task003\n",
"3\n",
"task005\n",
"4\n",
"task006\n",
"5\n",
"task007\n",
"==\n",
"voice982\n",
"0\n",
"task001\n",
"1\n",
"task002\n",
"2\n",
"task003\n",
"3\n",
"task005\n",
"4\n",
"task006\n",
"5\n",
"task007\n",
"==\n",
"voice983\n",
"0\n",
"task001\n",
"1\n",
"task002\n",
"2\n",
"task003\n",
"3\n",
"task005\n",
"4\n",
"task006\n",
"5\n",
"task007\n",
"==\n",
"voice984\n",
"0\n",
"task001\n",
"1\n",
"task002\n",
"2\n",
"task003\n",
"3\n",
"task005\n",
"4\n",
"task006\n",
"5\n",
"task007\n",
"==\n",
"voice986\n",
"0\n",
"task001\n",
"1\n",
"task002\n",
"2\n",
"task003\n",
"3\n",
"task005\n",
"4\n",
"task006\n",
"5\n",
"task007\n",
"==\n",
"voice987\n",
"0\n",
"task001\n",
"1\n",
"task002\n",
"2\n",
"task003\n",
"3\n",
"task005\n",
"4\n",
"task006\n",
"5\n",
"task007\n",
"==\n",
"voice988\n",
"0\n",
"task001\n",
"1\n",
"task002\n",
"2\n",
"task003\n",
"3\n",
"task005\n",
"4\n",
"task006\n",
"5\n",
"task007\n",
"==\n",
"voice989\n",
"0\n",
"task001\n",
"1\n",
"task002\n",
"2\n",
"task003\n",
"3\n",
"task005\n",
"4\n",
"task006\n",
"5\n",
"task007\n",
"==\n",
"voice990\n",
"0\n",
"task001\n",
"1\n",
"task002\n",
"2\n",
"task003\n",
"3\n",
"task005\n",
"4\n",
"task006\n",
"5\n",
"task007\n",
"==\n",
"voice991\n",
"0\n",
"task001\n",
"1\n",
"task002\n",
"2\n",
"task003\n",
"3\n",
"task005\n",
"4\n",
"task006\n",
"5\n",
"task007\n",
"==\n",
"voice992\n",
"0\n",
"task001\n",
"1\n",
"task002\n",
"2\n",
"task003\n",
"3\n",
"task005\n",
"4\n",
"task006\n",
"5\n",
"task007\n",
"==\n",
"voice993\n",
"0\n",
"task001\n",
"1\n",
"task002\n",
"2\n",
"task003\n",
"3\n",
"task005\n",
"4\n",
"task006\n",
"5\n",
"task007\n",
"==\n",
"voice994\n",
"0\n",
"task001\n",
"1\n",
"task002\n",
"2\n",
"task003\n",
"3\n",
"task005\n",
"4\n",
"task006\n",
"5\n",
"task007\n",
"==\n",
"voice995\n",
"0\n",
"task001\n",
"1\n",
"task002\n",
"2\n",
"task003\n",
"3\n",
"task005\n",
"4\n",
"task006\n",
"5\n",
"task007\n",
"==\n",
"voice996\n",
"0\n",
"task001\n",
"1\n",
"task002\n",
"2\n",
"task003\n",
"3\n",
"task005\n",
"4\n",
"task006\n",
"5\n",
"task007\n",
"==\n",
"voice998\n",
"0\n",
"task001\n",
"1\n",
"task002\n",
"2\n",
"task003\n",
"3\n",
"task005\n",
"4\n",
"task006\n",
"5\n",
"task007\n",
"==\n",
"voice999\n",
"0\n",
"task001\n",
"1\n",
"task002\n",
"2\n",
"task003\n",
"3\n",
"task005\n",
"4\n",
"task006\n",
"5\n",
"task007\n"
]
}
],
"source": [
"# save 900s\n",
"file_name_save = '20160108_voice9xx.png'\n",
"plot_subj_by_task(subs9, tasks, img_path, file_name_save)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"----------\n",
"-\n",
"\n",
"-\n",
"-\n",
"-\n",
"--\n",
"-\n",
"-\n",
"--\n",
"\n",
"-\n",
"--\n",
"-\n",
"-\n",
"--\n",
"\n",
"----\n",
"\n",
"--"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def plot_subj_by_task(subjs, tasks, img_path, file_name_save): #, speech_vec):\n",
" \n",
" ## Input\n",
" \n",
" ## Output\n",
" # *.png\n",
" \n",
" # NOTE: Image module transposes row and column dimensions\n",
" \n",
" N_px_row = 400\n",
" N_px_col = 200\n",
" shift = 25\n",
" \n",
" N_row = len(subjs)\n",
" N_col = len(tasks)*2 # Times 2 for lh and rh\n",
" #creates a new empty image, RGB mode, and size 400 by 400.\n",
" new_im = Image.new('RGB', (N_col*N_px_col, N_row*N_px_row + shift))\n",
" \n",
" for idx_subj, subj in enumerate(subjs):\n",
" #\n",
" print \"==\"\n",
" print subj\n",
"\n",
" for idx_task, task in enumerate(tasks):\n",
" print idx_task\n",
" print task\n",
" print task_contrast[task]\n",
" \n",
" #==========================================================================\n",
" file_path_data = os.path.join(file_path_l1.replace('_png', ''), task, subj, \n",
" 'zstats', 'mni', 'zstat' + task_contrast[task] + '.nii.gz')\n",
" print file_path_data\n",
" # Check if image\n",
" if os.path.exists(file_path_data):\n",
" zstat = nib.load(file_path_data)\n",
" zstat_mat = np.array(zstat.get_data())\n",
" zstat_vec = np.copy(zstat_mat)\n",
" zstat_vec = np.reshape(zstat_vec, (np.size(zstat_vec),1)) \n",
" \n",
" [rho, p] = scipy.stats.pearsonr(speech_vec, zstat_vec)\n",
" else:\n",
" rho = 2\n",
" \n",
" \n",
" #==========================================================================\n",
" # Check if null trial\n",
" # Assume first file is representative for the subject- \n",
" # not true under unusual circumstance of multi session subject\n",
" file_path_psylog = glob(os.path.join('/om/project/voice/processedData/audio/psycholog/',\n",
" task, subj, 'session00*/R00*/*.tsv'))\n",
" if file_path_psylog:\n",
" file_path_psylog= file_path_psylog[0]\n",
" data = np.genfromtxt(file_path_psylog, delimiter='\\t', dtype=str)\n",
" if 'NULL' in data[:,2]:\n",
" null_str = 'Null'\n",
" else:\n",
" null_str = 'No Null'\n",
" else:\n",
" null_str = 'Unk' \n",
" \n",
" #---------------------------------------------------------------------------\n",
" # Check if dicom converted data\n",
" nifti_present = glob(os.path.join(\n",
" '/om/project/voice/processedData/openfmri', subj, \n",
" 'session*', 'functional', subj + '_' + task + '*.nii.gz'))\n",
"\n",
" #---------------------------------------------------------------------------\n",
" hemi_str = 'rh'\n",
" file_path_data = os.path.join(img_path, '%s_%s.png' % (subj,task))\n",
" \n",
" # Check if image\n",
" if os.path.exists(file_path_data):\n",
" #opens an image:\n",
" im = Image.open(file_path_data)\n",
" #resize so it is no bigger than 100,100\n",
" im.thumbnail((N_px_col,N_px_row))\n",
" #paste the image at location i,j:\n",
" new_im.paste(im, (idx_task*N_px_col*2,idx_subj*N_px_row + shift))\n",
" draw = ImageDraw.Draw(new_im)\n",
" #font = ImageFont.truetype(\"arial.ttf\", 15)\n",
" #font = ImageFont.load(\"arial.pil\")\n",
" draw.text((idx_task*N_px_col*2,idx_subj*N_px_row), \n",
" os.path.split(file_path_data)[1])\n",
" elif nifti_present:\n",
" #opens an image:\n",
" im = Image.open('/om/user/gr21783/git/voice/mri/scripts/analysis_openfmri/dataQuality/yellow.png')\n",
" #resize so it is no bigger than 100,100\n",
" im.thumbnail((N_px_col,N_px_row))\n",
" #paste the image at location i,j:\n",
" new_im.paste(im, (idx_task*N_px_col*2,idx_subj*N_px_row + shift))\n",
" draw = ImageDraw.Draw(new_im) \n",
" draw.text((idx_task*N_px_col*2,idx_subj*N_px_row), \n",
" os.path.split(file_path_data)[1])\n",
" \n",
" file_path_data = file_path_data.replace('rh', 'lh')\n",
" if os.path.exists(file_path_data):\n",
" im = Image.open(file_path_data)\n",
" im.thumbnail((N_px_col,N_px_row))\n",
" new_im.paste(im, (idx_task*N_px_col*2 + N_px_col,idx_subj*N_px_row + shift))\n",
" #im.show()\n",
" draw = ImageDraw.Draw(new_im)\n",
" #font = ImageFont.truetype(\"arial.ttf\", 15) #doesn't work\n",
" #font = ImageFont.load(\"arial.pil\") #doesn't work\n",
" #draw.text((idx_task*N_px_col*2 + N_px_col,idx_subj*N_px_row), \n",
" # os.path.split(file_path_data)[1]) \n",
" draw.text((idx_task*N_px_col*2 + N_px_col,idx_subj*N_px_row), \n",
" null_str + \" rho: %2.2f\" % (rho))\n",
" elif nifti_present:\n",
" #opens an image:\n",
" im = Image.open('/om/user/gr21783/git/voice/mri/scripts/analysis_openfmri/dataQuality/yellow.png')\n",
" #resize so it is no bigger than 100,100\n",
" im.thumbnail((N_px_col,N_px_row))\n",
" #paste the image at location i,j:\n",
" new_im.paste(im, (idx_task*N_px_col*2 + N_px_col,idx_subj*N_px_row + shift))\n",
" draw = ImageDraw.Draw(new_im) \n",
" draw.text((idx_task*N_px_col*2 + N_px_col,idx_subj*N_px_row), \n",
" os.path.split(file_path_data)[1]) \n",
" \n",
" else:\n",
" print \"missing \" + file_path_data\n",
"\n",
" \n",
" #new_im.show()\n",
" new_im.save(file_name_save) \n",
"\n",
" #f.savefig(file_name_save) \n",
" #plt.close(f) "
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment