Skip to content

Commit

Permalink
Merge pull request #819 from papr/fixes
Browse files Browse the repository at this point in the history
Final fixes before v0.9.14 release
  • Loading branch information
mkassner authored Aug 30, 2017
2 parents d6ee735 + 46c08e8 commit 0a01a22
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 36 deletions.
1 change: 1 addition & 0 deletions pupil_src/launchables/world.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ def reset_restart():
getter=lambda: eyes_are_alive[1].value
))
selector_label = "Select to load"
user_launchable_plugins.sort(key=lambda p: p.__name__)
labels = [p.__name__.replace('_', ' ') for p in user_launchable_plugins]
user_launchable_plugins.insert(0, selector_label)
labels.insert(0, selector_label)
Expand Down
37 changes: 19 additions & 18 deletions pupil_src/shared_modules/csv_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,41 +45,42 @@ def write_key_value_file(csvfile,dictionary,append=False):
"""
writer = csv.writer(csvfile, delimiter=',')
if not append:
writer.writerow(['key','value'])
for key,val in dictionary.items():
writer.writerow([key,val])
writer.writerow(['key', 'value'])
for key, val in dictionary.items():
writer.writerow([key, val])


if __name__ == '__main__':
test = {'foo':'bar','oh':'rl","y','it was':'not me'}
test_append = {'jo':'ho'}
test = {'foo': 'bar', 'oh': 'rl"abc"y', 'it was': 'not 🚨'}
test_append = {'jo': 'ho'}
test_updated = test.copy()
test_updated.update(test_append)

testfile = '.test.csv'

# Test write+read
with open(testfile, 'w') as csvfile:
write_key_value_file(csvfile,test)
with open(testfile, 'r') as csvfile:
with open(testfile, 'w', encoding='utf-8') as csvfile:
write_key_value_file(csvfile, test)
with open(testfile, 'r', encoding='utf-8') as csvfile:
result = read_key_value_file(csvfile)
assert test == result
assert test == result, (test, result)

# Test write+append (same keys)+read
with open(testfile, 'w') as csvfile:
write_key_value_file(csvfile,test)
write_key_value_file(csvfile,test,append=True)
with open(testfile, 'r') as csvfile:
with open(testfile, 'w', encoding='utf-8') as csvfile:
write_key_value_file(csvfile, test)
write_key_value_file(csvfile, test, append=True)
with open(testfile, 'r', encoding='utf-8') as csvfile:
result = read_key_value_file(csvfile)
assert test == result

# Test write+append (different keys)+read
with open(testfile, 'w') as csvfile:
write_key_value_file(csvfile,test)
write_key_value_file(csvfile,test_append,append=True)
with open(testfile, 'r') as csvfile:
with open(testfile, 'w', encoding='utf-8') as csvfile:
write_key_value_file(csvfile, test)
write_key_value_file(csvfile, test_append, append=True)
with open(testfile, 'r', encoding='utf-8') as csvfile:
result = read_key_value_file(csvfile)
assert test_updated == result

import os
os.remove(testfile)
print('CSV Test: successful')
print('CSV Test: successful')
2 changes: 1 addition & 1 deletion pupil_src/shared_modules/fixation_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ def init_gui(self):
def close():
self.alive = False

self.menu = ui.Growing_Menu('Online Fixation Detector')
self.menu = ui.Growing_Menu('Fixation Detector')
self.menu.collapsed = True
self.menu.append(ui.Button('Close', close))

Expand Down
2 changes: 1 addition & 1 deletion pupil_src/shared_modules/player_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def load_meta_info(rec_dir):
def update_meta_info(rec_dir, meta_info):
logger.info('Updating meta info')
meta_info_path = os.path.join(rec_dir,"info.csv")
with open(meta_info_path,'w',newline='') as csvfile:
with open(meta_info_path,'w',newline='',encoding='utf-8') as csvfile:
csv_utils.write_key_value_file(csvfile,meta_info)


Expand Down
3 changes: 1 addition & 2 deletions pupil_src/shared_modules/square_marker_detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,7 @@ def detect_markers_robust(gray_img,grid_size,prev_markers,min_marker_perimeter=4
new_markers = []
tick -=1


if prev_img is not None and prev_markers:
if prev_img is not None and prev_img.shape == gray_img.shape and prev_markers:

new_ids = [m['id'] for m in new_markers]

Expand Down
42 changes: 30 additions & 12 deletions pupil_src/shared_modules/video_capture/file_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class File_Source(Base_Source):
timestamps (str): Path to timestamps file
"""

def __init__(self, g_pool, source_path=None, timed_playback=False):
def __init__(self, g_pool, source_path=None, timed_playback=False, loop=False):
super().__init__(g_pool)

# minimal attribute set
Expand All @@ -99,6 +99,7 @@ def __init__(self, g_pool, source_path=None, timed_playback=False):
self.source_path = source_path
self.timestamps = None
self.timed_playback = timed_playback
self.loop = loop

if not source_path or not os.path.isfile(source_path):
logger.error('Init failed. Source file could not be found at `%s`'%source_path)
Expand Down Expand Up @@ -161,16 +162,19 @@ def __init__(self, g_pool, source_path=None, timed_playback=False):

loc, name = os.path.split(os.path.splitext(source_path)[0])
self._intrinsics = load_intrinsics(loc, name, self.frame_size)
self.play = True

def ensure_initialisation(fallback_func=None):
def ensure_initialisation(fallback_func=None, requires_playback=False):
from functools import wraps

def decorator(func):
@wraps(func)
def run_func(self, *args, **kwargs):
if self._initialised and self.video_stream:
return func(self, *args, **kwargs)
elif fallback_func:
# test self.play only if requires_playback is True
if not requires_playback or self.play:
return func(self, *args, **kwargs)
if fallback_func:
return fallback_func(*args, **kwargs)
else:
logger.debug('Initialisation required.')
Expand All @@ -186,6 +190,7 @@ def intrinsics(self):
return self._intrinsics

@property
@ensure_initialisation(fallback_func=lambda: (640, 480))
def frame_size(self):
return int(self.video_stream.format.width), int(self.video_stream.format.height)

Expand All @@ -198,6 +203,7 @@ def get_init_dict(self):
settings = super().get_init_dict()
settings['source_path'] = self.source_path
settings['timed_playback'] = self.timed_playback
settings['loop'] = self.loop
return settings

@property
Expand Down Expand Up @@ -246,9 +252,14 @@ def get_frame(self):
logger.debug('Frame index not consistent.')
break
if not frame:
logger.info("End of videofile %s %s"%(self.current_frame_idx,len(self.timestamps)))
raise EndofVideoFileError('Reached end of videofile')

if self.loop:
logger.info('Looping enabled. Seeking to beginning.')
self.seek_to_frame(0)
self.target_frame_idx = 0
return self.get_frame()
else:
logger.info("End of videofile %s %s"%(self.current_frame_idx,len(self.timestamps)))
raise EndofVideoFileError('Reached end of videofile')
try:
timestamp = self.timestamps[index]
except IndexError:
Expand All @@ -268,14 +279,14 @@ def wait(self,frame):
self.display_time = frame.timestamp - time()
sleep(self.slowdown)

@ensure_initialisation(fallback_func=lambda evt: sleep(0.05))
def recent_events(self,events):
@ensure_initialisation(fallback_func=lambda evt: sleep(0.05), requires_playback=True)
def recent_events(self, events):
try:
frame = self.get_frame()
except EndofVideoFileError:
logger.info('Video has ended.')
self.notify_all({"subject":'file_source.video_finished', 'source_path':self.source_path})
self._initialised = False
self.notify_all({"subject":'file_source.video_finished', 'source_path': self.source_path})
self.play = False
else:
self._recent_frame = frame
events['frame'] = frame
Expand Down Expand Up @@ -314,13 +325,20 @@ def init_gui(self):
from pyglui import ui
ui_elements = []
ui_elements.append(ui.Info_Text("Running Capture with '%s' as src"%self.source_path))
ui_elements.append(ui.Slider('slowdown',self,min=0,max=1.0))
ui_elements.append(ui.Slider('slowdown', self, min=0, max=1.0))

def toggle_looping(val):
self.loop = val
if val:
self.play = True
ui_elements.append(ui.Switch('loop', self, setter=toggle_looping))
self.g_pool.capture_source_menu.extend(ui_elements)

@property
def jpeg_support(self):
return False


class File_Manager(Base_Manager):
"""Summary
Expand Down
9 changes: 7 additions & 2 deletions pupil_src/shared_modules/zmq_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,13 @@ def __init__(self, ctx, ipc_pub_url):
self.socket = Msg_Dispatcher(ctx, ipc_pub_url)

def emit(self, record):
self.socket.send('logging.{0}'.format(record.levelname.lower()),
record.__dict__)
try:
self.socket.send('logging.{0}'.format(record.levelname.lower()), record.__dict__)
except TypeError:
record_dict = record.__dict__
# stringify `exc_info` since it includes unserializable objects
record_dict['exc_info'] = str(record_dict['exc_info'])
self.socket.send('logging.{0}'.format(record.levelname.lower()), record_dict)


class ZMQ_Socket(object):
Expand Down

0 comments on commit 0a01a22

Please sign in to comment.