Skip to content

Instantly share code, notes, and snippets.

@nicain
Last active August 30, 2018 18:03
Show Gist options
  • Save nicain/c710778d4f1263018ca11961284c260a to your computer and use it in GitHub Desktop.
Save nicain/c710778d4f1263018ca11961284c260a to your computer and use it in GitHub Desktop.
Minimal working example of a monkey-patch to save from display buffer
def add_frame_dir(sweep_stim, output_dir='.'):
image_dict = {}
timing_dict = collections.defaultdict(list)
for stimulus in sweep_stim.stimuli:
old_update = stimulus.update
def new_update(frame):
old_update(frame)
assert frame == stimulus.current_frame
sweep_stim.window.getMovieFrame()
t0 = time.time()
curr_image = sweep_stim.window.movieFrames.pop()
tmp = curr_image.resize((curr_image.size[0]/8, curr_image.size[1]/8))
curr_frame = np.array(tmp)
curr_frame_hash = xxhash.xxh64(curr_frame).digest()
key = os.path.join(output_dir, '%s.npy' % curr_frame_hash)
if key not in image_dict:
image_dict[key] = curr_image
timing_dict['timestamp'].append(time.time()-sweep_stim.start_time)
timing_dict['filename'].append(key)
stimulus.update = new_update
old_finalize = sweep_stim._finalize
def new_finalize(*args, **kwargs):
timing_df = pd.DataFrame(timing_dict)
timing_df.to_csv(os.path.join(output_dir, 'stimtable.csv'))
for key, val in image_dict.items():
print type(val)
np.save(key, np.array(val))
old_finalize(*args, **kwargs)
sweep_stim._finalize = new_finalize
@nicain
Copy link
Author

nicain commented Aug 30, 2018

time.time call should go before capture and hash, to more closely approximate the real time that the frame actually went up to the monitor, right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment