forked from open-ephys/analysis-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinary.py
161 lines (113 loc) · 5.22 KB
/
Binary.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: malfatti
@date: 2019-07-27
Loads data recorded by Open Ephys in Binary format as numpy memmap.
DatLoad(Folder, Processor=None, Experiment=None, Recording=None, Unit='uV', ChannelMap=[])
Parameters
Folder: str
Folder containing at least the subfolder 'experiment1'.
Processor: str or None, optional
Processor number to load, according to subsubsubfolders under
Folder>experimentX/recordingY/continuous . The number used is the one
after the processor name. For example, to load data from the folder
'Channel_Map-109_100.0' the value used should be '109'.
If not set, load all processors.
Experiment: int or None, optional
Experiment number to load, according to subfolders under Folder.
If not set, load all experiments.
Recording: int or None, optional
Recording number to load, according to subsubfolders under Folder>experimentX .
If not set, load all recordings.
Unit: str or None, optional
Unit to return the data, either 'uV' or 'mV' (case insensitive). In
both cases, return data in float32. Defaults to 'uV'.
If anything else, return data in int16.
ChannelMap: list, optional
If empty (default), load all channels.
If not empty, return only channels in ChannelMap, in the provided order.
CHANNELS ARE COUNTED STARTING AT 0.
Returns:
Data: dict
Dictionary with data in the structure Data[Processor][Experiment][Recording].
Rate: dict
Dictionary with sampling rates in the structure Rate[Processor][Experiment].
Example:
import Binary
Folder = '/home/user/PathToData/2019-07-27_00-00-00'
Data, Rate = Binary.Load(Folder)
ChannelMap = [0,15,1,14]
Recording = 3
Data2, Rate2 = Binary.Load(Folder, Recording=Recording, ChannelMap=ChannelMap, Unit='Bits')
Warning:
Data placed inside dictionaries is affected when passed to functions.
Therefore, if you do, for example:
FilteredData = MyFilterFunction(Data['100']['0']['0'], Rate['100']['0'], Frequencies)
the data in Data['100']['0']['0'] will be filtered! To avoid this behaviour, pass the
data with the .copy() method:
FilteredData = MyFilterFunction(Data['100']['0']['0'].copy(), Rate['100']['0'], Frequencies)
then the data in Data['100']['0']['0'] will remain unaltered.
"""
#%%
import numpy as np
from ast import literal_eval
from glob import glob
def ApplyChannelMap(Data, ChannelMap):
print('Retrieving channels according to ChannelMap... ', end='')
for R, Rec in Data.items():
if Rec.shape[1] < len(ChannelMap) or max(ChannelMap) > Rec.shape[1]-1:
print('')
print('Not enough channels in data to apply channel map. Skipping...')
continue
Data[R] = Data[R][:,ChannelMap]
return(Data)
def BitsToVolts(Data, ChInfo, Unit):
print('Converting to uV... ', end='')
Data = {R: Rec.astype('float32') for R, Rec in Data.items()}
if Unit.lower() == 'uv': U = 1
elif Unit.lower() == 'mv': U = 10**-3
for R in Data.keys():
for C in range(len(ChInfo)):
Data[R][:,C] = Data[R][:,C] * ChInfo[C]['bit_volts'] * U
if 'ADC' in ChInfo[C]['channel_name']: Data[R][:,C] *= 10**6
return(Data)
def Load(Folder, Processor=None, Experiment=None, Recording=None, Unit='uV', ChannelMap=[]):
Files = sorted(glob(Folder+'/**/*.dat', recursive=True))
InfoFiles = sorted(glob(Folder+'/*/*/structure.oebin'))
Data, Rate = {}, {}
for F,File in enumerate(Files):
Exp, Rec, _, Proc = File.split('/')[-5:-1]
Exp = str(int(Exp[10:])-1)
Rec = str(int(Rec[9:])-1)
Proc = Proc.split('.')[0].split('-')[-1]
if '_' in Proc: Proc = Proc.split('_')[0]
if Proc not in Data.keys(): Data[Proc], Rate[Proc] = {}, {}
if Experiment:
if int(Exp) != Experiment-1: continue
if Recording:
if int(Rec) != Recording-1: continue
if Processor:
if Proc != Processor: continue
print('Loading recording', int(Rec)+1, '...')
if Exp not in Data[Proc]: Data[Proc][Exp] = {}
Data[Proc][Exp][Rec] = np.memmap(File, dtype='int16')
Info = literal_eval(open(InfoFiles[F]).read())
ProcIndex = [Info['continuous'].index(_) for _ in Info['continuous']
if str(_['recorded_processor_id']) == Proc][0]
ChNo = Info['continuous'][ProcIndex]['num_channels']
if Data[Proc][Exp][Rec].shape[0]%ChNo:
print('Rec', Rec, 'is broken')
del(Data[Proc][Exp][Rec])
continue
SamplesPerCh = Data[Proc][Exp][Rec].shape[0]//ChNo
Data[Proc][Exp][Rec] = Data[Proc][Exp][Rec].reshape((SamplesPerCh, ChNo))
Rate[Proc][Exp] = Info['continuous'][ProcIndex]['sample_rate']
for Proc in Data.keys():
for Exp in Data[Proc].keys():
if Unit.lower() in ['uv', 'mv']:
ChInfo = Info['continuous'][ProcIndex]['channels']
Data[Proc][Exp] = BitsToVolts(Data[Proc][Exp], ChInfo, Unit)
if ChannelMap: Data[Proc][Exp] = ApplyChannelMap(Data[Proc][Exp], ChannelMap)
print('Done.')
return(Data, Rate)