Skip to content

Instantly share code, notes, and snippets.

@jminas
Created August 25, 2015 19:12
Show Gist options
  • Save jminas/8cd93e933bd4cd623d85 to your computer and use it in GitHub Desktop.
Save jminas/8cd93e933bd4cd623d85 to your computer and use it in GitHub Desktop.
onsets_sart
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Generating Onsets SART"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from scipy.io import loadmat\n",
"import pandas as pd\n",
"import numpy as np\n",
"from glob import glob\n",
"import csv\n",
"from __future__ import division\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import os\n",
"from scipy import stats as st\n",
"from scipy.stats import norm\n",
"import statsmodels.formula.api as sm\n",
"import datetime\n",
"import re\n",
"import shutil\n",
"import string\n",
"import seaborn as sns\n",
"from __future__ import division, print_function\n",
"\n",
"now = datetime.datetime.now()\n",
"date = now.strftime(\"%Y-%m-%d_%H:%M\")\n",
"month = now.strftime(\"%B\")\n",
"\n",
"pd.set_option('display.max_rows', 200)\n",
"pd.set_option('display.max_columns', 200)\n",
"import sys\n",
"\n",
"from scipy.io import loadmat"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"--------------------------------------------------------------------------------------------------\n",
"One set of regressors for pre-probe intervals (each including 5 nontarget trials) (4 types)\n",
"- 1) on\n",
"- 2) off\n",
"\n",
"One set for the occurrence of the probes themselves (we are collapsing over both probe1 and probe2 (since we are not interested in aware unaware.\n",
"- 3) probe (Probe1 + Probe2) \n",
"\n",
"--------------------------------------------------------------------------------------------------\n",
"\n",
"Contrasts: \n",
"\n",
" a | ON | OFF | PROBE\n",
" :------|:----:|:----:|:-----:\n",
" on | 1 | 0 | 0 \n",
" off | 0 | 1 | 0 \n",
" on>off | 1 | -1 | 0 \n",
" off>on | -1 | 1 | 0 "
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"#this is the dictionary for naming onset files by number based off the openfmri project organization\n",
"#https://openfmri.org/content/data-organization\n",
"cond_num = {'on':1, 'off':2,'probe':3, 'corr':4, 'error':5, 'target':6} "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"--------------------------------------------------------------------------------------------------\n",
"One set of regressors for pre-probe intervals (each including 5 nontarget trials) (4 types)\n",
"\n",
"- 1) Off-task \n",
"- 2) On-task\n",
"--------------------------------------------------------------------------------------------------"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/jminas/anaconda/lib/python2.7/site-packages/IPython/kernel/__main__.py:67: SettingWithCopyWarning: \n",
"A value is trying to be set on a copy of a slice from a DataFrame.\n",
"Try using .loc[row_indexer,col_indexer] = value instead\n",
"\n",
"See the the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n"
]
}
],
"source": [
"runs = [1,2,3,4]\n",
"subjects = ['p10', 'p11'] \n",
"names = ['on', 'off'] \n",
"\n",
"for run in runs: \n",
" for sub in subjects:\n",
" \n",
" df = pd.DataFrame(dtype=float)\n",
" df = df.append(pd.read_csv('../data/%s_schedule%d.txt' % (sub, run), sep = '\\t'), ignore_index=True)\n",
" \n",
" #read the log data to find out the real trigger time\n",
" log_data = pd.read_csv('../data/%s_schedule%d.log' % (sub, run) ,sep='\\t', header = None ) #delim_whitespace=True)\n",
" trigg_time = log_data[log_data[2] == 'Keypress: plus']\n",
" ttime = trigg_time.iloc[0,0]\n",
" onset_times = log_data[log_data[2].str.contains(\"New trial\")]\n",
" onset_times = onset_times.reset_index()\n",
" onset_times['onsets'] = onset_times[0] - ttime\n",
" df['onsets'] = onset_times['onsets']\n",
" \n",
" #add condition names\n",
" df['condition'] = df.apply(lambda row:( 'probe' if row['Stim']=='probe' else 'probe2' if row['Stim']=='probe2' else \n",
" 'target' if row['Stim'] == '3' else 'nontarget'), axis=1)\n",
" \n",
" #df['RT'].replace('[]', None, inplace=True)\n",
" #df['RT'] = df['RT'].astype(float)\n",
" df['regressor'] = df.apply(lambda row:('On' if (row['condition']=='probe' and row['Response']== '1') else \n",
" 'Off' if (row['condition']=='probe' and row['Response']== '2') else\n",
" 'Aware' if (row['condition']=='probe2' and row['Response']== '1') else\n",
" 'Unaware' if (row['condition']=='probe2' and row['Response']== '2') else None), axis=1)\n",
" \n",
" df['targ'] = df.apply(lambda row:('corr_targ' if (row['condition']=='target' and row['CorrRej']== 1) else \n",
" 'incorr_targ' if (row['condition']=='target' and row['Commission']== 1) else\n",
" 'corr_nontarg' if (row['condition']=='nontarget' and row['Hit']== 1) else None), axis=1)\n",
" \n",
" #Make a new dataframe\n",
" df2 = pd.DataFrame(columns=('name', 'onset', 'duration', 'run'))\n",
" idx=0\n",
" for ii in range(df.shape[0]-1):\n",
" \n",
" ###these are the 5 trials before ON\n",
" if df['regressor'][ii] == 'On':\n",
" name = 'on' \n",
" onset = df['onsets'][ii-5] \n",
" dur = df['onsets'][ii] - df['onsets'][ii-5]\n",
" run = run\n",
" df2.loc[idx] = [name, onset, dur, run]\n",
" idx = idx+1\n",
" \n",
" ###these are the 5 trials before OFF\n",
" elif df['regressor'][ii] == 'Off':\n",
" name = 'off'\n",
" onset = df['onsets'][ii-5] \n",
" dur = df['onsets'][ii] - df['onsets'][ii-5]\n",
" run = run\n",
" df2.loc[idx] = [name, onset, dur, run]\n",
" idx = idx+1\n",
" \n",
" \n",
" \n",
" for namezzz in names:\n",
" #onset_dir='/home/jminas/openmind/sart/openfmri/sart_%s/model/model001/onsets/task001_run%03d'%(sub,run)\n",
" onset_dir = '/home/jminas/Dropbox/SART/fMRI/onsets/sart_%s/task001_run%03d'%(sub,run)\n",
" if not os.path.exists(onset_dir):\n",
" os.makedirs(onset_dir)\n",
" cols = df2[df2['name'].isin([namezzz])]\n",
" #print(cols)\n",
" cols['x'] = 1\n",
" cols = cols[['onset', 'duration', 'x']]\n",
" \n",
" #cols.to_csv(os.path.join(onset_dir, 'cond%03d.txt' % (cond_num[namezzz])), sep= '\\t', index=False, header=False)\n",
" cols.to_csv(os.path.join(onset_dir, '%s_cond%03d.txt' % (namezzz,cond_num[namezzz])), sep= '\\t', index=False, header=False)\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"--------------------------------------------------------------------------------------------------\n",
"One set of regressors for ALL probes\n",
"\n",
" - 1) Probes\n",
" \n",
"--------------------------------------------------------------------------------------------------"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"#PROBES\n",
"runs = [1,2,3,4]\n",
"subjects = ['p10', 'p11'] #'sart_p5', 'sart_p6', 'sart_p7', 'sart_p8', 'sart_p9'] #'p2','p3']\n",
"names = ['probe'] \n",
"for run in runs:\n",
" for sub in subjects:\n",
" idx=0\n",
" df5 = pd.DataFrame(columns=('name', 'onset', 'duration', 'run'))\n",
" for ii in range(df.shape[0]-1):\n",
" \n",
" ### These are the probe regressors\n",
" if df['condition'][ii] == 'probe':\n",
" name = 'probe' \n",
" onset = df['onsets'][ii]#-trigger_time\n",
" dur = df['onsets'][ii+2] - df['onsets'][ii]\n",
" run = run\n",
" df5.loc[idx] = [name, onset, dur, run]\n",
" idx = idx+1\n",
" for namezzz in names:\n",
" #onset_dir='/home/jminas/openmind/sart/openfmri/sart_%s/model/model001/onsets/task001_run%03d'%(sub,run)\n",
" onset_dir = '/home/jminas/Dropbox/SART/fMRI/onsets/sart_%s/task001_run%03d'%(sub,run)\n",
" if not os.path.exists(onset_dir):\n",
" os.makedirs(onset_dir)\n",
" cols = df5[df5['name'].isin([namezzz])]\n",
" #print(cols)\n",
" cols['x'] = 1\n",
" cols = cols[['onset', 'duration', 'x']]\n",
" #cols.to_csv(os.path.join(onset_dir, 'cond%03d.txt' % (cond_num[namezzz])), sep= '\\t', index=False, header=False)\n",
" cols.to_csv(os.path.join(onset_dir, '%s_cond%03d.txt' % (namezzz,cond_num[namezzz])), sep= '\\t', index=False, header=False)\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# accuacy\n",
"--------------------------------------------------------------------------------------------------\n",
"One set of regressors for pre-target intervals (each including 5 nontarget trials) \n",
"- 1) correct\n",
"- 2) error\n",
"\n",
"One set for the occurrence of the targets themselves\n",
"- 3) target\n",
"\n",
"--------------------------------------------------------------------------------------------------\n",
"\n",
"Contrasts: \n",
"\n",
" a | CORR | ERR | TARG\n",
" :------|:----:|:----:|:-----:\n",
" corr | 1 | 0 | 0 \n",
" err | 0 | 1 | 0 \n",
" cor>err| 1 | -1 | 0 \n",
" off>on | -1 | 1 | 0 "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"--------------------------------------------------------------------------------------------------\n",
"One set of regressors for pre-probe intervals (each including 5 nontarget trials) (4 types)\n",
"\n",
"- 1) Correct (CR) \n",
"- 2) Errors (Commissions)\n",
"--------------------------------------------------------------------------------------------------"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/jminas/anaconda/lib/python2.7/site-packages/IPython/kernel/__main__.py:33: SettingWithCopyWarning: \n",
"A value is trying to be set on a copy of a slice from a DataFrame.\n",
"Try using .loc[row_indexer,col_indexer] = value instead\n",
"\n",
"See the the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n"
]
}
],
"source": [
"runs = [1,2,3,4]\n",
"subjects = ['p10', 'p11'] \n",
"names = ['corr', 'error'] \n",
"for run in runs:\n",
" for sub in subjects:\n",
" idx=0\n",
" df4 = pd.DataFrame(columns=('name', 'onset', 'duration', 'run'))\n",
" for ii in range(df.shape[0]-1):\n",
" \n",
" ### These are the 5 pretrial correct and incorrect blocks\n",
" if df['targ'][ii] == 'corr_targ':\n",
" name = 'corr'\n",
" onset = df['onsets'][ii-5] \n",
" dur = df['onsets'][ii] - df['onsets'][ii-5]\n",
" run = run\n",
" df4.loc[idx] = [name, onset, dur, run]\n",
" idx = idx+1\n",
" elif df['targ'][ii] == 'incorr_targ':\n",
" name = 'error'\n",
" onset = df['onsets'][ii-5] \n",
" dur = df['onsets'][ii] - df['onsets'][ii-5]\n",
" run = run\n",
" df4.loc[idx] = [name, onset, dur, run]\n",
" idx = idx+1\n",
" #print(df4)\n",
" for namezzz in names:\n",
" #onset_dir='/home/jenni/Dropbox/SART/fMRI/onsets/sart_%s/model/model001/onsets/task001_run%03d'%(sub,run)\n",
" onset_dir = '/home/jminas/Dropbox/SART/fMRI/onsets/sart_%s/task001_run%03d'%(sub,run)\n",
" if not os.path.exists(onset_dir):\n",
" os.makedirs(onset_dir)\n",
" cols = df4[df4['name'].isin([namezzz])]\n",
" #print(cols)\n",
" cols['x'] = 1\n",
" cols = cols[['onset', 'duration', 'x']]\n",
" \n",
" #cols.to_csv(os.path.join(onset_dir, 'cond%03d.txt' % (cond_num[namezzz])), sep= '\\t', index=False, header=False)\n",
" cols.to_csv(os.path.join(onset_dir, '%s_cond%03d.txt' % (namezzz,cond_num[namezzz])), sep= '\\t', index=False, header=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"--------------------------------------------------------------------------------------------------\n",
"One set of regressors for ALL Targets\n",
"\n",
"- 1) Targets\n",
"\n",
"--------------------------------------------------------------------------------------------------"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"#targets\n",
"runs = [1,2,3,4]\n",
"subjects = ['p10', 'p11'] \n",
"names = ['target'] \n",
"for run in runs:\n",
" for sub in subjects:\n",
" idx=0\n",
" df6 = pd.DataFrame(columns=('name', 'onset', 'duration', 'run'))\n",
" for ii in range(df.shape[0]-1):\n",
" \n",
" ### These are the probe regressors\n",
" if df['condition'][ii] == 'target':\n",
" name = 'target' \n",
" onset = df['onsets'][ii]#-trigger_time\n",
" dur = df['onsets'][ii]\n",
" run = run\n",
" df6.loc[idx] = [name, onset, dur, run]\n",
" idx = idx+1\n",
" for namezzz in names:\n",
" #onset_dir='/home/jminas/openmind/sart/openfmri/sart_%s/model/model001/onsets/task001_run%03d'%(sub,run)\n",
" onset_dir = '/home/jminas/Dropbox/SART/fMRI/onsets/sart_%s/task001_run%03d'%(sub,run)\n",
" if not os.path.exists(onset_dir):\n",
" os.makedirs(onset_dir)\n",
" cols = df6[df6['name'].isin([namezzz])]\n",
" #print(cols)\n",
" cols['x'] = 1\n",
" cols = cols[['onset', 'duration', 'x']]\n",
" #cols.to_csv(os.path.join(onset_dir, 'cond%03d.txt' % (cond_num[namezzz])), sep= '\\t', index=False, header=False)\n",
" cols.to_csv(os.path.join(onset_dir, '%s_cond%03d.txt' % (namezzz,cond_num[namezzz])), sep= '\\t', index=False, header=False)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": []
}
],
"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.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment