Skip to content

Commit

Permalink
Merge pull request #115 from BenjaminHsieh/organize
Browse files Browse the repository at this point in the history
Major Organization
  • Loading branch information
briankleinqiu committed Dec 12, 2015
2 parents af5c9ba + c1e9320 commit 6e868f3
Show file tree
Hide file tree
Showing 54 changed files with 326 additions and 522 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
.PHONY: all clean coverage test
.PHONY: all clean coverage test verbose

all: clean

clean:
find . -name "*.so" -o -name "*.pyc" -o -name "*.pyx.md5" | xargs rm -f

coverage:
nosetests code/utils data --with-coverage --cover-package=data --cover-package=utils
nosetests code/utils/tests data/tests --with-coverage --cover-package=data/data.py --cover-package=code/utils/functions

test:
nosetests code/utils data
nosetests code/utils/tests data/tests

verbose:
nosetests -v code/utils data
nosetests -v code/utils/tests data/tests
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# project-template
# UC Berkeley Stat 159/259 Fall 2015
## Project-Theta

[![Build Status](https://travis-ci.org/berkeley-stat159/project-theta.svg?branch=master)](https://travis-ci.org/berkeley-stat159/project-theta?branch=master)
[![Coverage Status](https://coveralls.io/repos/berkeley-stat159/project-theta/badge.svg?branch=master)](https://coveralls.io/r/berkeley-stat159/project-theta?branch=master)

Fall 2015 group project
_**Group members:**_ Siyao Chang ([`changsiyao`](https://github.com/changsiyao)) , Boying Gong ([`boyinggong`](https://github.com/boyinggong)), Benjamin Hsieh ([`BenjaminHsieh`](https://github.com/BenjaminHsieh)), Brian Qiu ([`brianqiu`](https://github.com/brianqiu)), Jiang Zhu ([`pigriver123`](https://github.com/pigriver123))

_**Topic:**_ [The Neural Basis of Loss Aversion in Decision Making Under Risk] (https://openfmri.org/dataset/ds000005/)
[SOME SUMMARY OF PAPER]

## Instructions
[NEED TO ADD]
6 changes: 4 additions & 2 deletions code/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
.PHONY: test coverage

test:
nosetests code/utils -w ..
nosetests code/utils/tests -w ..

coverage:
nosetests code/utils -w .. --with-coverage --cover-package=utils
nosetests code/utils/tests -w .. --with-coverage --cover-package=utils/functions
2 changes: 1 addition & 1 deletion code/scripts/find_mask_threshold.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import matplotlib.pyplot as plt
import numpy as np
import sys
sys.path.append('../utils')
sys.path.append('../utils/functions')
import smooth_gaussian
import os

Expand Down
2 changes: 1 addition & 1 deletion code/scripts/findoutlier.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np
import json
import sys
pathtofunction = '../utils'
pathtofunction = '../utils/functions'
# Append the sys path
sys.path.append(pathtofunction)
from outlierfunction import outlier
Expand Down
2 changes: 1 addition & 1 deletion code/scripts/graph_lindiag_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sys

# Path to function
pathtofunction = '../utils'
pathtofunction = '../utils/functions'
# Append path to sys
sys.path.append(pathtofunction)

Expand Down
10 changes: 8 additions & 2 deletions code/scripts/graphoutlier.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@
# Paths
pathtodata = '../../data/'
pathtofig = '../../paper/figures/'
pathtofunction = '../utils'
pathtofunction = '../utils/functions'
pathtographing = '../utils/graphing'
# Append fuction path
sys.path.append(pathtofunction)
# Import function
from graphoutlier_functions import loadnib_dict, loadtxt_dict, plot_dvars, plot_fd, plot_meanSig
from basic_util import loadnib_dict, loadtxt_dict

# Append graphing path
sys.path.append(pathtographing)
# Import function
from graphoutlier_functions import plot_dvars, plot_fd, plot_meanSig


# load outlier files
Expand Down
4 changes: 2 additions & 2 deletions code/scripts/lme_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import sys

# Path to function
pathtofunction = '../utils'
pathtofunction = '../utils/functions'
# Append path to sys
sys.path.append(pathtofunction)

from behavtask_tr import events2neural_extend, merge_cond
from regression_functions import hrf, getRegressor, calcBeta, calcMRSS, deleteOutliers
from regression_functions import hrf, getRegressor, deleteOutliers
from lme_functions import calcBetaLme, calcSigProp, calcAnov, anovStat

n_vols=240
Expand Down
13 changes: 11 additions & 2 deletions code/scripts/logistic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import sys

# Path to function
pathtofunction = '../utils'
pathtofunction = '../utils/functions'
# Append path to sys
sys.path.append(pathtofunction)

Expand Down Expand Up @@ -49,7 +49,16 @@
logreg_proba = logreg.predict_proba(X)
confusion = create_confusion(logreg_proba, y)
addsub = fig.add_subplot(4, 4, i)
addsub, AUC = plot_roc(confusion, addsub, i)
addsub, ROC, AUC = plot_roc(confusion, addsub)
# Plot the ROC curve.
plt.plot(ROC[:,0], ROC[:,1], lw=2)
plt.xlim(-0.1,1.1)
plt.ylim(-0.1,1.1)
plt.xlabel('$FPR(t)$')
plt.ylabel('$TPR(t)$')
plt.grid()
plt.title('subject '+ str(i)+', AUC = %.4f'%AUC)
#------------------------------------------------------------------------#
Min_thrs = np.append(Min_thrs, getMin_thrs(confusion))
AUC_smr = np.append(AUC_smr, AUC)

Expand Down
2 changes: 1 addition & 1 deletion code/scripts/make_smooth_graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import matplotlib.pyplot as plt
import nibabel as nib
import sys
sys.path.append('../utils')
sys.path.append('../utils/functions')
import smooth_gaussian

#possibly put in os function to specify path later
Expand Down
2 changes: 1 addition & 1 deletion code/scripts/regression_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import sys

# Path to function
pathtofunction = '../utils'
pathtofunction = '../utils/functions'
# Append path to sys
sys.path.append(pathtofunction)

Expand Down
53 changes: 53 additions & 0 deletions code/utils/functions/basic_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""
A collection of basic utility functions for use in outlier detection/graphing
of fd, dvars, and meanSignal, see graphoutlier_functions.py
"""

import numpy as np
import nibabel as nib


def loadtxt_dict(file_name, fig_name):
"""
Input:
Txt file specified by file_name
Name of figure file wish to output
Output:
Dictonary that attached to file_name the np array after loading
file_name
"""
data = np.loadtxt(file_name)
dict_out = {fig_name: data}
return dict_out

def loadnib_dict(file_name, fig_name):
"""
Input:
Bold file (bold.nii, bold.nii.gz) specified by file_name
Name of figure file wish to output
Output:
Dictonary that attached to file_name the np array after loading file_name
"""
img = nib.load(file_name)
data = img.get_data()
dict_out = {fig_name: data}
return dict_out


# Calculate mean
def vol_mean(data):
""" Return mean across voxels for $D `data`
Input:
np array of data
Output: np array of dim (T,)
mean of data across all but the last dimension
"""
mean_list = []
# Loop over the each volume and outputs the mean of each dimension
for i in range(data.shape[-1]):
mean = np.mean(data[...,i])
mean_list.append(mean)
return np.asarray(mean_list)
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,3 @@ def events2neural_extend(behav_task, tr, n_trs):
time_course[on:on + dur,9] = RT
return time_course

def plot_time_course(time_course):
"""
Simple function to plot time_course, an array from return of
events2neural_extend
"""
plt.plot(time_course[:,0])
plt.show()
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
from scipy import stats
import pandas as pd
import numpy as np
from behavtask_tr import events2neural_extend, merge_cond
from regression_functions import hrf, getRegressor, calcBeta, calcMRSS, deleteOutliers

def calcBetaLme(data_full, gain_full, loss_full, linear_full, quad_full, run_group, thrshd=None):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ def create_confusion(logreg_proba, y, thrs_inc=0.01):
Parameters
----------
actual: Actual responses, 1-d array with values 0 or 1
fitted: Fitted probabilities, 1-d array with values between 0 and 1
y: Actual responses, 1-d array with values 0 or 1
logreg_proba: Fitted probabilities, 2-d array with values between 0 and 1 in second column
thrs_inc: increment of threshold probability (default 0.05)
Returns
Expand Down Expand Up @@ -52,15 +52,15 @@ def getMin_thrs(confusion):
false_pos: number of incorrect trues
false_neg: number of incorrect falses
"""
thrs_min = np.argmin(confusion[:,3]+ confusion[:,4])
thrs_min = np.min(confusion[:,3]+ confusion[:,4])
col_out = confusion[thrs_min, :]
thrs = col_out[0]
false_pos = col_out[3]
false_neg = col_out[4]
return thrs, false_pos, false_neg


def plot_roc(confusion, fig, sub_i):
def plot_roc(confusion, fig):
"""
function to plot the ROC (receiver operating characteristic) curve and
calculate the corresponding AUC (Area Under Curve).
Expand All @@ -83,19 +83,10 @@ def plot_roc(confusion, fig, sub_i):
# Compute true positive rate for current threshold.
TPR_t = confusion[i, 1] / float(confusion[i, 1] + confusion[i, 4])
ROC[i,1] = TPR_t

# Plot the ROC curve.
plt.plot(ROC[:,0], ROC[:,1], lw=2)
plt.xlim(-0.1,1.1)
plt.ylim(-0.1,1.1)
plt.xlabel('$FPR(t)$')
plt.ylabel('$TPR(t)$')
plt.grid()

AUC = 0.
for i in range(confusion.shape[0]-1):
AUC += (ROC[i+1,0]-ROC[i,0]) * (ROC[i+1,1]+ROC[i,1])
AUC *= -0.5

plt.title('subject '+ str(sub_i)+', AUC = %.4f'%AUC)
return fig, AUC
return fig, ROC, AUC
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ def calcBeta(data, gain, loss, linear_dr, quad_dr, threshold=None):
design[:, 3] = quad_dr
designp = npl.pinv(design)
if threshold!=None:
mean_data = np.mean(data, axis=-1)
mask = mean_data > threshold
mask = np.mean(data, axis=-1) > threshold
data[~mask]=0
T = data.shape[-1]
time_by_vox = np.reshape(data, (-1, T)).T
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,38 +1,28 @@
"""
A collection of utility functions for outlier detection/graphing
A collection of graphing functions for outlier detection/graphing
of fd, dvars, and meanSignal
"""
import numpy as np
import matplotlib.pyplot as plt
import nibabel as nib

def loadtxt_dict(file_name, fig_name):
"""
Input:
Txt file specified by file_name
Name of figure file wish to output
# Calculate mean this is same as the one in basic_util.py
# Test already written, included here because graphing functions not tested
# but need to use above:
def vol_mean(data):
""" Return mean across voxels for $D `data`
Output:
Dictonary that attached to file_name the np array after loading
file_name
Input:
np array of data
Output: np array of dim (T,)
mean of data across all but the last dimension
"""
data = np.loadtxt(file_name)
dict_out = {fig_name: data}
return dict_out
mean_list = []
# Loop over the each volume and outputs the mean of each dimension
for i in range(data.shape[-1]):
mean = np.mean(data[...,i])
mean_list.append(mean)
return np.asarray(mean_list)

def loadnib_dict(file_name, fig_name):
"""
Input:
Bold file (bold.nii, bold.nii.gz) specified by file_name
Name of figure file wish to output
Output:
Dictonary that attached to file_name the np array after loading file_name
"""
img = nib.load(file_name)
data = img.get_data()
dict_out = {fig_name: data}
return dict_out

# Graphing dvars: RMS signal derivative
def plot_dvars(dvars_dict, dvars_outliers, saveit=False):
Expand Down Expand Up @@ -64,6 +54,7 @@ def plot_dvars(dvars_dict, dvars_outliers, saveit=False):
plt.savefig(dvars_dict.keys()[0])
plt.close()


# Graphing fd: Framewise displacement
def plot_fd(fd_dict, fd_outliers, saveit=False):
"""
Expand Down Expand Up @@ -94,21 +85,6 @@ def plot_fd(fd_dict, fd_outliers, saveit=False):
plt.savefig(fd_dict.keys()[0])
plt.close()

# Calculate mean
def vol_mean(data):
""" Return mean across voxels for $D `data`
Input:
np array of data
Output: np array of dim (T,)
mean of data across all but the last dimension
"""
mean_list = []
# Loop over the each volume and outputs the mean of each dimension
for i in range(data.shape[-1]):
mean = np.mean(data[...,i])
mean_list.append(mean)
return np.asarray(mean_list)

# Graphing mean signal
def plot_meanSig(bdata_dict, saveit = False):
Expand Down Expand Up @@ -141,4 +117,3 @@ def plot_meanSig(bdata_dict, saveit = False):
plt.savefig(bdata_dict.keys()[0])
plt.close()


Loading

0 comments on commit 6e868f3

Please sign in to comment.