Skip to content

Commit

Permalink
Add pyrep to primitive (and vice versa) transforms where possible.
Browse files Browse the repository at this point in the history
  • Loading branch information
gmierz committed Sep 8, 2018
1 parent 52ea64d commit 95b8912
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 7 deletions.
42 changes: 42 additions & 0 deletions pupillib/core/utilities/default_dataset_processors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'''
(*)~---------------------------------------------------------------------------
This file is part of Pupil-lib.
Pupil-lib is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Pupil-lib is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Pupil-lib. If not, see <https://www.gnu.org/licenses/>.
Copyright (C) 2018 Gregory W. Mierzwinski
---------------------------------------------------------------------------~(*)
'''

'''
This file holds processing function for `pupil_lib.py` to use
when defaulting to another dataset.
'''

def eye_pyrep_to_prim_default(pyrep_stream, all_data_field, all_data):
return {
'data': [eval(el[0])['diameter'] for el in pyrep_stream['time_series']],
'timestamps': all_data[all_data_field]['timestamps'],
'srate': all_data[all_data_field]['srate']
}

def gaze_pyrep_to_prim_default(pyrep_stream, all_data_field, all_data):
return {
'data': all_data[all_data_field]['data'],
'timestamps': all_data[all_data_field]['timestamps'],
'srate': all_data[all_data_field]['srate']
}

def gaze_prim_to_pyrep_default(pyrep_stream, all_data_field, all_data):
return gaze_pyrep_to_prim_default(pyrep_stream, all_data_field, all_data)
63 changes: 56 additions & 7 deletions pupillib/pupil_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
from pupillib.core.workers.processors.xdfloader_processor import XdfLoaderProcessor
from pupillib.dependencies.xdf.Python.xdf import load_xdf
from pupillib.core.utilities.config_store import ConfigStore
from pupillib.core.utilities.default_dataset_processors import (
eye_pyrep_to_prim_default,
gaze_pyrep_to_prim_default,
gaze_prim_to_pyrep_default
)
from pupillib.core.data_container import PupilDatasets

import threading
Expand Down Expand Up @@ -140,7 +145,7 @@ def xdf_pupil_load(dataset, xdf_file_and_name, data_num=0):
eye1_stream = i
elif i['info']['name'][0] == 'Pupil Primitive Data - Eye 0':
eye0_stream = i
elif i['info']['type'][0] == 'Markers':
elif i['info']['type'][0] == 'Markers' or i['info']['name'][0] == 'Markers':
markers_stream = i
elif i['info']['name'][0] == 'Pupil Python Representation - Eye 1':
eye1pyrep_stream = i
Expand Down Expand Up @@ -174,6 +179,29 @@ def xdf_pupil_load(dataset, xdf_file_and_name, data_num=0):
}
}

# Used to determine what data stream
# to default to when it's original dataset
# does not exist.
matchers = {
'eye0': 'eye0-pyrep',
'eye1': 'eye1-pyrep',
'gaze_x-pyrep': 'gaze_x',
'gaze_y-pyrep': 'gaze_y',
'gaze_x': 'gaze_x-pyrep',
'gaze_y': 'gaze_y-pyrep',
}

def check_matchers(n):
# We didn't find the datastream,
# and we have a default,
# and that default exists.
# So get the data from the default.
if data_entries[n] is None and \
n in matchers and \
data_entries[matchers[n]] is not None:
return True
return False

logger = MultiProcessingLog.get_logger()
failure = False
if not markers_stream:
Expand All @@ -186,10 +214,19 @@ def xdf_pupil_load(dataset, xdf_file_and_name, data_num=0):
logger.send('ERROR', 'Missing ' + i + ' from datastream',
os.getpid(), threading.get_ident())

filtered_names = []
for n in name_list:
if check_matchers(n):
filtered_names.append(matchers[n])
filtered_names.append(n)

xdf_processor = XdfLoaderProcessor()
xdf_transforms = xdf_processor.transform.all
all_data = {}
for a_data_name in name_list:
for a_data_name in filtered_names:
if data_entries[a_data_name] is None:
continue

funct_list = xdf_processor.data_name_to_function(a_data_name)
results = {}
for func in funct_list:
Expand Down Expand Up @@ -232,18 +269,30 @@ def recurse_new_config(old_config, res):
test_pass = xdf_transforms['test_results'](results, a_data_name)
if test_pass:
all_data[a_data_name] = results
else:
raise Exception("Tests conducted while loading data failed.")

# Always get the markers along with any data.
all_data['markers'] = {
'timestamps': xdf_transforms['get_marker_times'](markers_stream, {}),
'eventnames': xdf_transforms['get_marker_eventnames'](markers_stream, {})
}

# If a primitive wasn't obtained, give a 'primitive' like response
if not all_data['eye1'] and all_data['eye1-pyrep']:
all_data = [eval(el)['diameter'] for el in eye1pyrep_stream['time_series']]
if not all_data['eye0'] and all_data['eye0-pyrep']:
all_data = [eval(el)['diameter'] for el in eye0pyrep_stream['time_series']]
default_proc_functions = {
'eye0': eye_pyrep_to_prim_default,
'eye1': eye_pyrep_to_prim_default,
'gaze_x': gaze_pyrep_to_prim_default,
'gaze_y': gaze_pyrep_to_prim_default,
'gaze_x-pyrep': gaze_prim_to_pyrep_default,
'gaze_y-pyrep': gaze_prim_to_pyrep_default
}

for n in name_list:
if check_matchers(n):
func = default_proc_functions[n]
default = matchers[n] # This is the field that we should take data from
new_data = func(data_entries[default], default, all_data)
all_data[n] = new_data

dataset['custom_data'] = custom_data

Expand Down

0 comments on commit 95b8912

Please sign in to comment.