Skip to content

Commit

Permalink
Merge pull request #37 from jodreen/master
Browse files Browse the repository at this point in the history
scene slicer  changes needed
  • Loading branch information
AlonDaks committed Nov 30, 2015
2 parents f79fc9f + 0229ac5 commit 5f20ff5
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 55 deletions.
71 changes: 27 additions & 44 deletions code/utils/scene_slicer.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
import nibabel as nib
import csv
import numpy as np
import json

IS_DAY = 0
IS_INT = 1
DAY_IND = 0
NIGHT_IND = 1
INT_IND = 2
EXT_IND = 3

DAY_NIGHT_IND = 0
INT_EXT_IND = 1

# 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'])
# path_to_subject_image = "../../" + data_paths['bold_dico_7Tad2grpbold7Tad']['sub1']['runs'][0]['path']


class SceneSlicer:
def __init__(
self,
path_to_images,
path_to_subject_image,
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.image = nib.load(path_to_subject_image)
self.scene_slices = []
self.segment_duration = [902, 882, 876, 976, 924, 878, 1086, 673.4]
self.scene_desc = {}
self.scene_keys = []
Expand All @@ -42,50 +39,36 @@ def generate_scene_desc_dict(self):
self.scene_keys = list(self.scene_desc.keys())
self.scene_keys.sort()

def get_image(self, run_num):
if self.images[run_num] == 0:
img = nib.load(self.path_to_images[run_num])
self.images[run_num] = img
return self.images[run_num]

def get_scene_slices(self, run_num):
if not self.images[run_num]:
self.get_image(run_num)
def get_scene_slices(self):
if not self.scene_keys:
self.generate_scene_desc_dict()
if not self.scene_slices[run_num]:

day_slices = []
night_slices = []
int_slices = []
ext_slices = []
img = self.images[run_num]

if not self.scene_slices:
day_night = []
int_ext = []
key_index = 0
scene_start = 0
for i in range(run_num):
scene_start += self.segment_duration[i]
for i in range(len(self.scene_keys)):
if self.scene_keys[i] > scene_start:
key_index = i
break
for i in range(img.shape[3]):
for i in range(self.image.shape[3]):
if key_index + 1 < len(self.scene_keys) and (
i * 2) + scene_start >= self.scene_keys[key_index + 1]:
key_index += 1
curr_time = self.scene_keys[key_index]
day_slices.append(i) if self.scene_desc[curr_time][
IS_DAY] else night_slices.append(i)
int_slices.append(i) if self.scene_desc[curr_time][
IS_INT] else ext_slices.append(i)
self.scene_slices[run_num] = (day_slices, night_slices, int_slices,
ext_slices)
return self.scene_slices[run_num]
day_night.append(0) if self.scene_desc[curr_time][
IS_DAY] else day_night.append(1)
int_ext.append(0) if self.scene_desc[curr_time][
IS_INT] else int_ext.append(1)
self.scene_slices = (day_night, int_ext)
return self.scene_slices

def get_day_night(self, run_num, slice):
if not self.scene_slices[run_num]:
self.get_scene_slices(run_num)
scene_slices = self.scene_slices[run_num]
is_day_slice = slice in scene_slices[DAY_IND]
is_int_slice = slice in scene_slices[INT_IND]
def get_day_night(self, slice):
if not self.scene_slices:
self.get_scene_slices()
is_day_slice = self.scene_slices[DAY_NIGHT_IND][slice] == 0
is_int_slice = self.scene_slices[INT_EXT_IND][slice] == 0
return (is_day_slice, is_int_slice)

# ss = SceneSlicer(path_to_subject_image)
# print ss.get_scene_slices()
37 changes: 37 additions & 0 deletions code/utils/sentiment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import csv
import textblob as tb
from math import ceil, floor
import unicodedata

path_to_scene_csv = "../../ds113_study_description/stimulus/task001/annotations/german_audio_description.csv"


def get_polarity_dict(filename):
with open(filename, 'rt') as csvfile:
reader = csv.DictReader(
csvfile,
fieldnames=['start', 'end', 'german_desc'])
for row in reader:
start = ceil(float(row['start']))
end = floor(float(row['end']))
desc = row['german_desc']
desc = unicode(desc, "utf-8")
desc = unicodedata.normalize('NFKD', desc).encode('ascii', 'ignore')
blob = tb.TextBlob(desc)
try:
translated_blob = blob.translate(from_lang="de", to="en")
except tb.exceptions.NotTranslated:
pass
sentiment = get_sentiment(translated_blob)

def get_sentiment(blob):
sentences = blob.sentences
sentence_total = len(sentences)
sentiment_total = 0
for sentence in sentences:
sentiment_total += sentence.sentiment.polarity
return float(sentiment_total) / float(sentence_total)



get_polarity_dict(path_to_scene_csv)
15 changes: 4 additions & 11 deletions code/utils/tests/test_scene_slicer.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_prepare():
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')
ss = scene_slicer.SceneSlicer('test_data.nii', 'scene.csv')
return ss


Expand All @@ -47,22 +47,15 @@ def test_scene_slicer_dict():
delete_files()


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


def test_scene_slicer_slices():
ss = test_prepare()
ss.get_scene_slices(0)
assert ss.scene_slices[0] != 0
ss.get_scene_slices()
assert len(ss.scene_slices) != 0
delete_files()


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

0 comments on commit 5f20ff5

Please sign in to comment.