Skip to content

Commit

Permalink
refactoring for file names, adjusting tests appropriately
Browse files Browse the repository at this point in the history
  • Loading branch information
jodreen committed Nov 19, 2015
1 parent 3222260 commit e351ce9
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 29 deletions.
24 changes: 13 additions & 11 deletions code/utils/scene_slicer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,24 @@
INT_IND = 2
EXT_IND = 3

# with open('../../data/data_path.json', 'r') as fh:
# data_paths = json.load(fh)
# path_to_images = []
# for i in range(8):
# path_to_images.append("../../" + data_paths['bold_dico_7Tad2grpbold7Tad']['sub1']['runs'][0]['path'])

class SceneSlicer:
def __init__(self, sub_num, path_to_root):
self.path_to_root = path_to_root
with open(self.path_to_root + 'data/data_path.json', 'r') as fh:
self.data_paths = json.load(fh)
self.images = [0] * 8
self.scene_slices = [0] * 8
self.sub_num = sub_num
def __init__(self, path_to_images, path_to_scene_csv="../../ds113_study_description/stimulus/task001/annotations/scenes.csv"):
self.path_to_images = path_to_images
self.path_to_scene_csv = path_to_scene_csv
self.images = [0] * len(path_to_images)
self.scene_slices = [0] * len(path_to_images)
self.segment_duration = [902, 882, 876, 976, 924, 878, 1086, 673.4]
self.scene_desc = {}
self.scene_keys = []

def generate_scene_desc_dict(self):
with open(self.path_to_root + 'ds113_study_description/stimulus/task001/annotations/scenes.csv', 'rb') as csvfile:
with open(self.path_to_scene_csv, 'rb') as csvfile:
reader = csv.DictReader(csvfile, fieldnames=['seconds', 'scene', 'day-night', 'int-ext'])
for row in reader:
scene_time = int(float(row['seconds']))
Expand All @@ -33,9 +36,7 @@ def generate_scene_desc_dict(self):

def get_image(self, run_num):
if self.images[run_num] == 0:
sub_str = 'sub' + str(self.sub_num)
img_path = self.path_to_root + self.data_paths['bold_dico_7Tad2grpbold7Tad'][sub_str]['runs'][run_num]["path"]
img = nib.load(img_path)
img = nib.load(self.path_to_images[run_num])
self.images[run_num] = img
return self.images[run_num]

Expand Down Expand Up @@ -76,3 +77,4 @@ def get_day_night(self, run_num, slice):
is_day_slice = slice in scene_slices[DAY_IND]
is_int_slice = slice in scene_slices[INT_IND]
return (is_day_slice, is_int_slice)

57 changes: 39 additions & 18 deletions code/utils/tests/test_scene_slicer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,64 @@
from __future__ import absolute_import, division, print_function

from .. import scene_slicer
import os
import nibabel as nib
import csv
import numpy as np

IS_DAY = 0
IS_INT = 1


def test_prepare():
data = np.array([[[[7, 9], [7, 8]], [[1, 2], [1, 8]]],
[[[2, 3], [2, 1]], [[5, 4], [4, 3]]]])
img = nib.Nifti1Image(data, affine=np.diag([1, 1, 1, 1]))
nib.save(img, 'test_data.nii')
with open('scene.csv', 'w') as csvfile:
scenewriter = csv.writer(csvfile, delimiter=',', quotechar='"')
scenewriter.writerow([17.0, "SAVANNAH", "DAY", "EXT"])
scenewriter.writerow([272.0, "DOCTORS OFFICE", "DAY", "INT"])
ss = scene_slicer.SceneSlicer(['test_data.nii'], 'scene.csv')
return ss


def delete_files():
os.remove('test_data.nii')
os.remove('scene.csv')


def test_scene_slicer_init():
ss = scene_slicer.SceneSlicer(1, "")
assert ss.sub_num == 1
assert len(ss.images) == 8
assert len(ss.scene_slices) == 8
ss = test_prepare()
assert ss.scene_desc is not None
assert ss.scene_keys is not None
assert ss.segment_duration == [902, 882, 876, 976, 924, 878, 1086, 673.4]

delete_files()

def test_scene_slicer_dict():
ss = scene_slicer.SceneSlicer(1, "")
ss = test_prepare()
ss.generate_scene_desc_dict()
for i in ss.scene_desc:
len(ss.scene_desc[i]) == 2
assert len(ss.scene_desc[i]) == 2
assert ss.scene_desc[i][IS_DAY] == 0 or ss.scene_desc[i][IS_DAY] == 1
assert ss.scene_desc[i][IS_INT] == 0 or ss.scene_desc[i][IS_INT] == 1

delete_files()

def test_scene_slicer_image():
ss = scene_slicer.SceneSlicer(1, "")
ss.get_image(2)
assert ss.images[2] != 0

ss = test_prepare()
ss.get_image(0)
assert ss.images[0] != 0
delete_files()

def test_scene_slicer_slices():
ss = scene_slicer.SceneSlicer(1, "")
ss.get_scene_slices(4)
assert ss.scene_slices[4] != 0

ss = test_prepare()
ss.get_scene_slices(0)
assert ss.scene_slices[0] != 0
delete_files()

def test_scene_slicer_day_night():
ss = scene_slicer.SceneSlicer(1, "")
scene_tup = ss.get_day_night(1, 0)
ss = test_prepare()
scene_tup = ss.get_day_night(0, 0)
assert scene_tup == (True, False)
delete_files()

0 comments on commit e351ce9

Please sign in to comment.