-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathframetimesfrommat.py
72 lines (61 loc) · 2.21 KB
/
frametimesfrommat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 8 19:01:30 2018
@author: ycan
"""
import os
import numpy as np
import scipy.io
import iofuncs as iof
import analysis_scripts as asc
def frametimesfrommat(exp_name):
"""
Extract frame times from .mat files. Needed for analyzing data
from other people, binary files containing the frame time pulses
are not usually available.
The converted frametime files are corrected for monitor delay.
"""
exp_dir = iof.exp_dir_fixer(exp_name)
_, metadata = asc.read_spikesheet(exp_dir)
monitor_delay = metadata['monitor_delay(s)']
for i in range(1, 100):
try:
name = iof.getstimname(exp_dir, i)
except IndexError as e:
if str(e).startswith('Stimulus'):
continue
else:
raise
matfile = os.path.join(exp_dir, 'frametimes', name + '_frametimings.mat')
# Check for zero padded name
if not os.path.isfile(matfile):
name = '0' + name
matfile = os.path.join(exp_dir, 'frametimes', name + '_frametimings.mat')
try:
f = scipy.io.matlab.loadmat(matfile)
ftimes = f['ftimes'][0, :]
if 'ftimesoff' in f.keys():
ftimes_off = f['ftimesoff'][0, :]
else:
ftimes_off = None
except NotImplementedError:
import h5py
with h5py.File(matfile, mode='r') as f:
ftimes = f['ftimes'][:]
if 'ftimesoff' in f.keys():
ftimes_off = f['ftimesoff'][:]
else:
ftimes_off = None
if len(ftimes.shape) != 1:
ftimes = ftimes.flatten()
if ftimes_off is not None:
ftimes_off = ftimes_off.flatten()
ftimes = ftimes+monitor_delay
savedict = {'f_on' : ftimes}
if ftimes_off is not None:
ftimes_off = ftimes_off+monitor_delay
savedict.update({'f_off' : ftimes_off})
np.savez(os.path.join(exp_dir, 'frametimes', name + '_frametimes'),
**savedict)
print(f'Converted and saved frametimes for {name}')