Skip to content

Commit

Permalink
Merge pull request #52 from AlonDaks/alon-dev
Browse files Browse the repository at this point in the history
Packaged our code as a python module called stat159lambda
  • Loading branch information
lisaannyu committed Dec 9, 2015
2 parents 1298523 + 2ad3680 commit 9942a70
Show file tree
Hide file tree
Showing 31 changed files with 73 additions and 52 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
.DS_Store

# Ignore Data files
.nii

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ 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/stat159lambda/utils data --with-coverage --cover-package=data --cover-package=utils

test:
nosetests code/utils data
nosetests code/stat159lambda/utils data

verbose:
nosetests -v code/utils data
nosetests -v code/stat159lambda/utils data
File renamed without changes.
File renamed without changes.
File renamed without changes.
12 changes: 12 additions & 0 deletions code/stat159lambda/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import os
import sys

REPO_HOME_PATH = os.path.realpath(__file__).replace('/code/stat159lambda/config.py', '')

USE_CACHED_DATA = os.environ.get('STAT159_CACHED_DATA', 'True') == 'True'

NUM_PROCESSES = int(os.environ.get('STAT159_NUM_PROCESSES', 5))

NUM_VOXELS = 1108800

NUM_TOTAL_VOLUMES = 3543
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import sys
import matplotlib.pyplot as plt
import gc

REPO_HOME_RELATIVE_PATH = '../../'
sys.path.append(REPO_HOME_RELATIVE_PATH)
from stat159lambda.config import REPO_HOME_PATH


def calc_vol_rms_diff(data_file_path):
Expand All @@ -20,14 +18,14 @@ def calc_vol_rms_diff(data_file_path):

def save_plot(vol_rms_diff, subj_num):
plt.plot(vol_rms_diff)
plt.savefig('{0}figures/subj{1}_vol_rms_diff.png'.format(
REPO_HOME_RELATIVE_PATH, subj_num))
plt.savefig('{0}/figures/subj{1}_vol_rms_diff.png'.format(
REPO_HOME_PATH, subj_num))


if __name__ == '__main__':
subj_num = sys.argv[1]
data_file_path = '{0}data/processed/sub{1}_rcds_2d.npy'.format(
REPO_HOME_RELATIVE_PATH, subj_num)
data_file_path = '{0}/data/processed/sub{1}_rcds_2d.npy'.format(
REPO_HOME_PATH, subj_num)
vol_rms_diff = calc_vol_rms_diff(data_file_path)
save_plot(vol_rms_diff, subj_num)
del vol_rms_diff
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,23 @@
from glob import glob
from os.path import exists
from multiprocessing import Pool
from stat159lambda.config import REPO_HOME_PATH

REPO_HOME_RELATIVE_PATH = '../../'
sys.path.append(REPO_HOME_RELATIVE_PATH)
import config as cf

DATA_PATHS = json.load(open(REPO_HOME_RELATIVE_PATH + 'data/data_path.json'))
DATA_PATHS = json.load(open('{0}/data/data_path.json'.format(REPO_HOME_PATH)))

INCORRECT_NUM_ARGS_MESSAGE = 'Invalid number of arguments: specify alignment type'
ILLEGAL_ARG_MESSAGE = 'preprocess.py must be provided with alignment argument'


def concatenate_runs(alignment):
for i, subject in enumerate(DATA_PATHS['subjects']):
nii_file_name = '{0}data/processed/sub{1}_{2}'.format(
REPO_HOME_RELATIVE_PATH, i + 1, alignment)
nii_file_name = '{0}/data/processed/sub{1}_{2}'.format(
REPO_HOME_PATH, i + 1, alignment)
if not exists(nii_file_name + '.nii') or not cf.USE_CACHED_DATA:
run_data = []
for j, run in enumerate(subject['runs']):
task_path = run[alignment]['path']
img = nib.load(REPO_HOME_RELATIVE_PATH + task_path)
img = nib.load(REPO_HOME_PATH + '/' + task_path)
data = img.get_data()
if j == 0:
run_data.append(data[..., :-4])
Expand All @@ -44,8 +41,8 @@ def concatenate_runs(alignment):


def reshape_data_to_2d(alignment):
files_to_reshape = np.sort(glob('{0}data/processed/sub*_{1}.nii'.format(
REPO_HOME_RELATIVE_PATH, alignment)))
files_to_reshape = np.sort(glob('{0}/data/processed/sub*_{1}.nii'.format(
REPO_HOME_PATH, alignment)))
for f in files_to_reshape:
file_name_2d = f.replace('.nii', '_2d')
if not exists(file_name_2d + '.npy') or not cf.USE_CACHED_DATA:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
from os import path
import sharedmem as sm
import gc

REPO_HOME_RELATIVE_PATH = '../../'
sys.path.append(REPO_HOME_RELATIVE_PATH)
import config as cf
from stat159lambda.config import REPO_HOME_PATH

INCORRECT_NUM_ARGS_MESSAGE = 'invalid number of arguments: specify alignment type, subject1 and subject2'
ILLEGAL_ALIGNMENT_ARG_MESSAGE = 'alignment argument must be either <rcds>, <linear>, <non_linear>'
Expand Down Expand Up @@ -54,10 +51,10 @@ def check_command_line_arguments(arguments):
subject2 = int(arguments[3])
except:
raise ValueError(ILLEGAL_SUBJECTS_ARG_MESSAGE)
subject1_file_path = '{0}data/processed/sub{1}_{2}_2d.npy'.format(
REPO_HOME_RELATIVE_PATH, subject1, alignment)
subject2_file_path = '{0}data/processed/sub{1}_{2}_2d.npy'.format(
REPO_HOME_RELATIVE_PATH, subject2, alignment)
subject1_file_path = '{0}/data/processed/sub{1}_{2}_2d.npy'.format(
REPO_HOME_PATH, subject1, alignment)
subject2_file_path = '{0}/data/processed/sub{1}_{2}_2d.npy'.format(
REPO_HOME_PATH, subject2, alignment)
if not path.exists(subject1_file_path):
raise ValueError(
'Filing missing: {0}, run preprocess.py to generate necessary file'.format(
Expand All @@ -72,8 +69,8 @@ def check_command_line_arguments(arguments):
if __name__ == '__main__':
subject1_file_path, subject2_file_path = check_command_line_arguments(
sys.argv)
correlation_file_name = '{0}data/processed/r_sub{1}_sub{2}_{3}'.format(
REPO_HOME_RELATIVE_PATH, sys.argv[2], sys.argv[3], sys.argv[1])
correlation_file_name = '{0}/data/processed/r_sub{1}_sub{2}_{3}'.format(
REPO_HOME_PATH, sys.argv[2], sys.argv[3], sys.argv[1])
if not path.exists(correlation_file_name +
'.npy') or not cf.USE_CACHED_DATA:
shared_subject1 = sm.copy(np.load(subject1_file_path))
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import numpy.linalg as npl
from scipy.stats import t as t_dist
from scipy.ndimage import gaussian_filter
from . import scene_slicer as ssm
import stat159lambda.utils.scene_slicer as ssm
import nibabel as nib


Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import numpy.linalg as npl
from scipy.stats import t as t_dist

from .. import linear_modeling
from stat159lambda.utils import linear_modeling
from numpy.testing import assert_equal, assert_almost_equal

# Make an X, y
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import absolute_import
from .. import parse_demographics
from stat159lambda.utils import parse_demographics

import os
import csv
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

import numpy as np

from .. import pearson
from stat159lambda.utils import pearson

from numpy.testing import assert_almost_equal

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import numpy as np

from .. import pearson
from stat159lambda.utils import pearson

from numpy.testing import assert_almost_equal

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import absolute_import
from .. import plot
from stat159lambda.utils import plot

import numpy as np
import matplotlib
Expand Down
28 changes: 28 additions & 0 deletions code/stat159lambda/utils/tests/test_rf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from stat159lambda.utils import rf
import numpy as np
from sklearn.ensemble import RandomForestClassifier
try:
from mock import patch
except:
from unittest.mock import patch


def test_prepare():
X = np.array([[10, 8, 8], [1, 11, 8], [4, 1, 1], [5, 0, 3], [7, 9, 1],
[1, 5, 8], [6, 3, 0], [7, 10, 3], [5, 4, 3], [7, 0, 8]])
y = np.array([1, 0, 1, 1, 0, 2, 1, 1, 0, 0])
return X, y


def test_cv_rf_accuracy():
X, y = test_prepare()
assert rf.cv_rf_accuracy(X, y, 2, 2, 2, num_folds=5) > 0


# @patch.object(RandomForestClassifier, '__init__')
# def test_rf_accuracy(mock_rf_init):
# X, y = test_prepare()
# rf.rf_accuracy(X, y, 2, 2, 2, num_folds=5)
# assert_array_equal(mock_rf_init.call_args[0][0],
# np.array([[10, 8, 8], [1, 11, 8], [4, 1, 1], [5, 0, 3], [7, 9, 1],
# [1, 5, 8], [6, 3, 0], [7, 10, 3], [5, 4, 3], [7, 0, 8]]))
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import absolute_import, division, print_function
from .. import scene_slicer
from stat159lambda.utils import scene_slicer
import os
import nibabel as nib
import csv
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import absolute_import
from .. import variance
from stat159lambda.utils import variance

import numpy as np
import nibabel as nib
Expand Down
File renamed without changes.
14 changes: 0 additions & 14 deletions code/utils/tests/test_rf.py

This file was deleted.

1 change: 1 addition & 0 deletions tools/travis_tools.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Tools for working with travis-ci
export WHEELHOUSE="http://travis-wheels.scikit-image.org/"
export PYTHONPATH=$(pwd)/code

retry () {
# https://gist.github.com/fungusakafungus/1026804
Expand Down

0 comments on commit 9942a70

Please sign in to comment.