forked from Intan-Technologies/IntanToNWB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReadIntanData.py
923 lines (801 loc) · 42.4 KB
/
ReadIntanData.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
import struct, numpy as np
from SetupResources import *
def read_into_1D(destination, offset, format_string, fid, bytes_per_sample, num_samples, repeat_factor=1):
""" Read from file into 1D destination array.
Parameters
----------
destination : numpy.ndarray
1D array to write to
offset : int
Index of the destination array to start writing to
format_string : str
String specifying the format of the read data (for example, '<iiii')
fid : _io.BufferedReader
Binary stream of a file to read from
bytes_per_sample : int
Number of bytes per sample (for example, 2 for uint16, 4 for int32)
num_samples : int
Number of samples to read
repeat_factor : int
How many times a unique sample has been repeated so that slower-sampled signals have the same length as faster-sampled signals
Returns
-------
None
"""
# While slower-sampled signals may be read with repeating samples, we want to store them at their native, slower rate.
# Calculate num_unique_samples to determine how many actual_samples were gathered, ignoring repeats.
num_unique_samples = int(num_samples / repeat_factor)
# Read data into temp array
end = offset + num_unique_samples
bytes_to_read = bytes_per_sample * num_samples
tmp = np.array(struct.unpack(format_string, fid.read(bytes_to_read)))
# Discard repeated samples, only saving unique samples to destination
destination[offset:end] = tmp[::repeat_factor]
def read_into_2D(destination, offset, fid, dtype, num_channels, num_samples, order, repeat_factor=1):
""" Read from file into 2D destination array.
Parameters
----------
destination : numpy.ndarray
2D array to write to
offset : int
Sample index of the destination array to start writing to
fid : _io.BufferedReader
Binary stream of a file to read from
dtype : str
Data type to read data as
num_channels : int
Number of channels (rows) to write to
num_samples : int
Number of samples per channel (columns) to write to
order : str
'F' for Fortran-like index ordering (last index changes slowest), 'C' for C-like index ordering (last index changes fastest)
repeat_factor : int
How many times a unique sample has been repeated so that slower-sampled signals have the same length as faster-sampled signals
Returns
-------
None
"""
# While slower-sampled signals may be read with repeating samples, we want to store them at their native, slower rate.
# Calculate num_unique_samples to determine how many actual samples were gathered, ignoring repeats.
num_unique_samples = int(num_samples / repeat_factor)
# Read data into temp array
end = offset + num_unique_samples
items_to_read = num_channels * num_samples
tmp = np.fromfile(fid, dtype=dtype, count=items_to_read)
# Discard repeated samples, only saving unique samples to destination
tmp = tmp[::repeat_factor]
destination[range(num_channels), offset:end] = tmp.reshape(num_channels, num_unique_samples, order=order)
def read_timestamp_block(header, data, indices, fids, file_format):
""" Populate data['t_amplifier'] with a block of timestamp data. For rhs files, populate it as data['t'] instead
Parameters
----------
header : dict
Dict containing previously read header information
data : dict
Dict containing fields to write data to - in this case, data['t_amplifier'] or data['t']
indices : dict
Dict containing indices keeping track of written data position
fids : dict
Dict containing binary streams of files to read from
file_format : str
Which file format this read is following - 'traditional', 'per_signal_type', or 'per_channel'
Returns
-------
None
"""
# Determine which binary stream to read timestamp data from
fid = fids['fid'] if file_format == 'traditional' else fids['time.dat']
if header['filetype'] == 'rhd':
# Determine suitable format string (pre-1.2 files had unsigned int timestamps, after that they are signed)
signing_character = 'i' if later_than_v1_2(header) else 'I'
format_string = '<' + signing_character * header['num_samples_per_data_block']
t_key = 't_amplifier'
else:
format_string = '<' + 'i' * header['num_samples_per_data_block']
t_key = 't'
# Read this block's timestamps into data['t_amplifier'] or data['t']
read_into_1D(destination=data[t_key],
offset=indices['amplifier'],
format_string=format_string,
fid=fid,
bytes_per_sample=4,
num_samples=header['num_samples_per_data_block']
)
def read_amplifier_block(header, data, indices, fids, file_format):
""" Populate data['amplifier_data'] with a block of amplifier data.
Parameters
----------
header : dict
Dict containing previously read header information
data : dict
Dict containing fields to write data to - in this case, data['amplifier_data']
indices : dict
Dict containing indices keeping track of written data position
fids : dict
Dict containing binary streams of files to read from
file_format : str
Which file format this read is following - 'traditional', 'per_signal_type', or 'per_channel'
Returns
-------
None
"""
# Only attempt reading if the header indicates there are amplifier channels to read
if header['num_amplifier_channels'] > 0:
# For traditional file format, read this block's amplifier data from .rhd file into data['amplifier_data']
if file_format == 'traditional':
read_into_2D(destination=data['amplifier_data'],
offset=indices['amplifier'],
fid=fids['fid'],
dtype='uint16',
num_channels=header['num_amplifier_channels'],
num_samples=header['num_samples_per_data_block'],
order='C',
repeat_factor=1
)
# For one file per signal type format, read this block's amplifier data from amplifier.dat into data['amplifier_data']
elif file_format == 'per_signal_type':
if header['filetype'] == 'rhd':
# In the case that auxiliary inputs were saved in the same file as amplifier data,
# read this block's amplifier data and then read this block's auxiliary data
if fids['aux_in_amplifier']:
# Read amplifier and auxiliary data together into temp_list
combined_channels = header['num_amplifier_channels'] + header['num_aux_input_channels']
temp_list = np.zeros([combined_channels, header['num_samples_per_data_block']], dtype='int16')
read_into_2D(destination=temp_list,
offset=0,
fid=fids['amplifier.dat'],
dtype='int16',
num_channels=combined_channels,
num_samples=header['num_samples_per_data_block'],
order='F',
repeat_factor=1
)
# Read amplifier data from the beginning of temp_list
data['amplifier_data'][range(header['num_amplifier_channels']), indices['amplifier']:indices['amplifier'] + header['num_samples_per_data_block']] = temp_list[0:header['num_amplifier_channels']]
# Read auxiliary data from temp_list, starting after the end of amplifier data
aux_list = temp_list[header['num_amplifier_channels']:header['num_amplifier_channels'] + header['num_aux_input_channels']]
# Convert auxiliary data from signed to unsigned
# This data is sampled 4x slower than amplifier data, so only read every 4th sample into data['aux_input_data']
unique_list = np.bitwise_xor(0x8000, aux_list[:, ::4]).astype(np.uint16)
unique_samples = int(header['num_samples_per_data_block'] / 4)
data['aux_input_data'][range(header['num_aux_input_channels']), indices['aux_input']:indices['aux_input'] + unique_samples] = unique_list
# In the case that amplifier.dat only contains amplifier data, just read directly to data['amplifier_data']
else:
read_into_2D(destination=data['amplifier_data'],
offset=indices['amplifier'],
fid=fids['amplifier.dat'],
dtype='int16',
num_channels=header['num_amplifier_channels'],
num_samples=header['num_samples_per_data_block'],
order='F',
repeat_factor=1
)
else:
read_into_2D(destination=data['amplifier_data'],
offset=indices['amplifier'],
fid=fids['amplifier.dat'],
dtype='int16',
num_channels=header['num_amplifier_channels'],
num_samples=header['num_samples_per_data_block'],
order='F',
repeat_factor=1
)
# For one file per channel format, read this block's amplifier data from various .dat files into data['amplifier_data']
elif file_format == 'per_channel':
for idx, channel in enumerate(header['amplifier_channels']):
read_into_1D(destination=data['amplifier_data'][idx],
offset=indices['amplifier'],
format_string='<' + 'h' * header['num_samples_per_data_block'],
fid=fids['amp-' + channel['native_channel_name'] + '.dat'],
bytes_per_sample=2,
num_samples=header['num_samples_per_data_block']
)
else:
raise Exception('Unrecognized file format: {}'.format(file_format))
def read_dc_amplifier_block(header, data, indices, fids, file_format):
""" Populate data['dc_amplifier_data'] with a block of amplifier data.
Parameters
----------
header : dict
Dict containing previously read header information
data : dict
Dict containing fields to write data to - in this case, data['amplifier_data']
indices : dict
Dict containing indices keeping track of written data position
fids : dict
Dict containing binary streams of files to read from
file_format : str
Which file format this read is following - 'traditional', 'per_signal_type', or 'per_channel'
Returns
-------
None
"""
if not header['dc_amplifier_data_saved']:
return
# For traditional file format, read this block's dc amplifier data from intan file into data['dc_amplifier_data']
if file_format == 'traditional':
read_into_2D(destination=data['dc_amplifier_data'],
offset=indices['amplifier'],
fid=fids['fid'],
dtype='uint16',
num_channels=header['num_amplifier_channels'],
num_samples=header['num_samples_per_data_block'],
order='C',
repeat_factor=1
)
# For one file per signal type format, read this block's dc amplifier data from dcamplifier.dat into data['dc_amplifier_data']
elif file_format == 'per_signal_type':
read_into_2D(destination=data['dc_amplifier_data'],
offset=indices['amplifier'],
fid=fids['dcamplifier.dat'],
dtype='int16',
num_channels=header['num_amplifier_channels'],
num_samples=header['num_samples_per_data_block'],
order='F',
repeat_factor=1
)
elif file_format == 'per_channel':
for idx, channel in enumerate(header['amplifier_channels']):
read_into_1D(destination=data['dc_amplifier_data'][idx],
offset=indices['amplifier'],
format_string='<' + 'h' * header['num_samples_per_data_block'],
fid=fids['dc-' + channel['native_channel_name'] + '.dat'],
bytes_per_sample=2,
num_samples=header['num_samples_per_data_block']
)
else:
raise Exception('Unrecognized file format: {}'.format(file_format))
def read_stim_block(header, data, indices, fids, file_format):
""" Populate data['stim_data_raw'] with a block of stim data.
Parameters
----------
header : dict
Dict containing previously read header information
data : dict
Dict containing fields to write data to - in this case, data['stim_data_raw']
indices : dict
Dict containing indices keeping track of written data position
fids : dict
Dict containing binary streams of files to read from
file_format : str
Which file format this read is following - 'traditional', 'per_signal_type', or 'per_channel'
Returns
-------
None
"""
# For traditional file format, read this block's stim data from intan file into data['stim_data_raw']
if file_format == 'traditional':
read_into_2D(destination=data['stim_data_raw'],
offset=indices['amplifier'],
fid=fids['fid'],
dtype='uint16',
num_channels=header['num_amplifier_channels'],
num_samples=header['num_samples_per_data_block'],
order='C',
repeat_factor=1
)
# For one file per signal type format, read this block's stim data from stim.dat into data['stim_data_raw']
elif file_format == 'per_signal_type':
read_into_2D(destination=data['stim_data_raw'],
offset=indices['amplifier'],
fid=fids['stim.dat'],
dtype='uint16',
num_channels=header['num_amplifier_channels'],
num_samples=header['num_samples_per_data_block'],
order='F',
repeat_factor=1
)
elif file_format == 'per_channel':
for idx, channel in enumerate(header['stim_channels']):
# It's possible for un-stimmed channels to not have a .dat file, so if this is the case, just move on to the next channel
if not (channel['native_channel_name'] + '.dat') in fids:
continue
read_into_1D(destination=data['stim_data_raw'][idx],
offset=indices['amplifier'],
format_string='<' + 'H' * header['num_samples_per_data_block'],
fid=fids[channel['native_channel_name'] + '.dat'],
bytes_per_sample=2,
num_samples=header['num_samples_per_data_block']
)
else:
raise Exception('Unrecognized file format: {}'.format(file_format))
def read_lowpass_block(header, data, indices, fids, file_format):
""" Populate data['lowpass_data'] with a block of lowpass amplifier data.
Parameters
----------
header : dict
Dict containing previously read header information
data : dict
Dict containing fields to write data to - in this case, data['supply_voltage_data']
indices : dict
Dict containing indices keeping track of written data position
fids : dict
Dict containing binary streams of files to read from
file_format : str
Which file format this read is following - 'traditional', 'per_signal_type', or 'per_channel'
Returns
-------
None
"""
# Only attempt reading if the header indicates there are amplifier channels to read, and lowpass data present
if header['num_amplifier_channels'] > 0 and header['lowpass_present']:
# For one file per signal type format, read this block's lowpass data from lowpass.dat into data['lowpass_data']
if file_format == 'per_signal_type':
num_lowpass_samples = int(header['num_samples_per_data_block'] / header['lowpass_downsample_factor'])
lowpass_offset = int(indices['amplifier'] / header['lowpass_downsample_factor'])
read_into_2D(destination=data['lowpass_data'],
offset=lowpass_offset,
fid=fids['lowpass.dat'],
dtype='int16',
num_channels=header['num_amplifier_channels'],
num_samples=num_lowpass_samples,
order='F',
repeat_factor=1
)
# For one file per channel format, read this block's lowpass data from various .dat files into data['lowpass_data']
elif file_format == 'per_channel':
num_lowpass_samples = int(header['num_samples_per_data_block'] / header['lowpass_downsample_factor'])
lowpass_offset = int(indices['amplifier'] / header['lowpass_downsample_factor'])
for idx, channel in enumerate(header['amplifier_channels']):
read_into_1D(destination=data['lowpass_data'][idx],
offset=lowpass_offset,
format_string='<' + 'h' * num_lowpass_samples,
fid=fids['low-' + channel['native_channel_name'] + '.dat'],
bytes_per_sample=2,
num_samples=num_lowpass_samples
)
else:
raise Exception('Unrecognized file format: {}'.format(file_format))
def read_highpass_block(header, data, indices, fids, file_format):
""" Populate data['highpass_data'] with a block of highpass amplifier data.
Parameters
----------
header : dict
Dict containing previously read header information
data : dict
Dict containing fields to write data to - in this case, data['supply_voltage_data']
indices : dict
Dict containing indices keeping track of written data position
fids : dict
Dict containing binary streams of files to read from
file_format : str
Which file format this read is following - 'traditional', 'per_signal_type', or 'per_channel'
Returns
-------
None
"""
# Only attempt reading if the header indicates there are amplifier channels to read, and highpass data present
if header['num_amplifier_channels'] > 0 and header['highpass_present']:
# For one file per signal type format, read this block's highpass data from highpass.dat into data['highpass_data']
if file_format == 'per_signal_type':
read_into_2D(destination=data['highpass_data'],
offset=indices['amplifier'],
fid=fids['highpass.dat'],
dtype='int16',
num_channels=header['num_amplifier_channels'],
num_samples=header['num_samples_per_data_block'],
order='F',
repeat_factor=1
)
# For one file per channel format, read this block's highpass data from various .dat files into data['highpass_data']
elif file_format == 'per_channel':
for idx, channel in enumerate(header['amplifier_channels']):
read_into_1D(destination=data['highpass_data'][idx],
offset=indices['amplifier'],
format_string='<' + 'h' * header['num_samples_per_data_block'],
fid=fids['high-' + channel['native_channel_name'] + '.dat'],
bytes_per_sample=2,
num_samples=header['num_samples_per_data_block']
)
else:
raise Exception('Unrecognized file format: {}'.format(file_format))
def read_aux_input_block(header, data, indices, fids, file_format):
""" Populate data['aux_input_data'] with a block of auxiliary input data.
Parameters
----------
header : dict
Dict containing previously read header information
data : dict
Dict containing fields to write data to - in this case, data['aux_input_data']
indices : dict
Dict containing indices keeping track of written data position
fids : dict
Dict containing binary streams of files to read from
file_format : str
Which file format this read is following - 'traditional', 'per_signal_type', or 'per_channel'
Returns
-------
None
"""
# Only attempt reading if the header indicates there are auxiliary input channels to read
if header['num_aux_input_channels'] > 0:
# For traditional file format, read this block's auxiliary data from .rhd file into data['aux_input_data']
if file_format == 'traditional':
read_into_2D(destination=data['aux_input_data'],
offset=indices['aux_input'],
fid=fids['fid'],
dtype='uint16',
num_channels=header['num_aux_input_channels'],
num_samples=int(header['num_samples_per_data_block'] / 4),
order='C',
repeat_factor=1
)
# For one file per signal type, read this block's auxiliary data from auxiliary.dat into data['aux_input_data']
elif file_format == 'per_signal_type':
if not fids['aux_in_amplifier']: # Handle aux in amplifier in the read_amplifier_block function
read_into_2D(destination=data['aux_input_data'],
offset=indices['aux_input'],
fid=fids['auxiliary.dat'],
dtype='uint16',
num_channels=header['num_aux_input_channels'],
num_samples=header['num_samples_per_data_block'],
order='F',
repeat_factor=4
)
# For one file per channel format, read this block's auxiliary data from various .dat files into data['aux_input_data']
elif file_format == 'per_channel':
for idx, channel in enumerate(header['aux_input_channels']):
read_into_1D(destination=data['aux_input_data'][idx],
offset=indices['aux_input'],
format_string='<' + 'H' * header['num_samples_per_data_block'],
fid=fids['aux-' + channel['native_channel_name'] + '.dat'],
bytes_per_sample=2,
num_samples=header['num_samples_per_data_block'],
repeat_factor=4
)
else:
raise Exception('Unrecognized file format: {}'.format(file_format))
def read_supply_voltage_block(header, data, indices, fids, file_format):
""" Populate data['supply_voltage_data'] with a block of supply voltage data.
Parameters
----------
header : dict
Dict containing previously read header information
data : dict
Dict containing fields to write data to - in this case, data['supply_voltage_data']
indices : dict
Dict containing indices keeping track of written data position
fids : dict
Dict containing binary streams of files to read from
file_format : str
Which file format this read is following - 'traditional', 'per_signal_type', or 'per_channel'
Returns
-------
None
"""
# Only attempt reading if the header indicates there are supply voltage channels to read
if header['num_supply_voltage_channels'] > 0:
# For traditional file format, read this block's supply voltage data from .rhd file into data['supply_voltage_data']
if file_format == 'traditional':
read_into_2D(destination=data['supply_voltage_data'],
offset=indices['supply_voltage'],
fid=fids['fid'],
dtype='uint16',
num_channels=header['num_supply_voltage_channels'],
num_samples=1,
order='C',
repeat_factor=1
)
# For one file per signal type, read this block's supply voltage data from supply.dat into data['supply_voltage_data']
elif file_format == 'per_signal_type':
read_into_2D(destination=data['supply_voltage_data'],
offset=indices['supply_voltage'],
fid = fids['supply.dat'],
dtype='uint16',
num_channels=header['num_supply_voltage_channels'],
num_samples=header['num_samples_per_data_block'],
order='F',
repeat_factor=header['num_samples_per_data_block']
)
# For one file per channel format, read this block's supply voltage data from various .dat files into data['supply_voltage_data']
elif file_format == 'per_channel':
for idx, channel in enumerate(header['supply_voltage_channels']):
read_into_1D(destination=data['supply_voltage_data'][idx],
offset=indices['supply_voltage'],
format_string='<' + 'H' * header['num_samples_per_data_block'],
fid=fids['vdd-' + channel['native_channel_name'] + '.dat'],
bytes_per_sample=2,
num_samples=header['num_samples_per_data_block'],
repeat_factor=header['num_samples_per_data_block']
)
else:
raise Exception('Unrecognized file format: {}'.format(file_format))
def read_temp_sensor_block(header, data, indices, fids, file_format):
""" Populate data['temp_sensor_data'] with a block of temperature sensor data.
Parameters
----------
header : dict
Dict containing previously read header information
data : dict
Dict containing fields to write data to - in this case, data['temp_sensor_data']
indices : dict
Dict containing indices keeping track of written data position
fids : dict
Dict containing binary streams of files to read from
file_format : str
Which file format this read is following - 'traditional', 'per_signal_type', or 'per_channel'
Returns
-------
None
"""
# Only attempt reading if the header indicates there are temp sensor channels to read
if header['num_temp_sensor_channels'] > 0:
# For traditional file format, read this block's temp sensor data from .rhd file into data['temp_sensor_data']
if file_format == 'traditional':
read_into_2D(destination=data['temp_sensor_data'],
offset=indices['supply_voltage'],
fid=fids['fid'],
dtype='uint16',
num_channels=header['num_temp_sensor_channels'],
num_samples=1,
order='C',
repeat_factor=1
)
# There's no way for temp sensor data to be saved outside of traditional file format, so just return
else:
return
def read_board_adc_block(header, data, indices, fids, file_format):
""" Populate data['board_adc_data'] with a block of analog input data.
Parameters
----------
header : dict
Dict containing previously read header information
data : dict
Dict containing fields to write data to - in this case, data['board_adc_data']
indices : dict
Dict containing indices keeping track of written data position
fids : dict
Dict containing binary streams of files to read from
file_format : str
Which file format this read is following - 'traditional', 'per_signal_type', or 'per_channel'
Returns
-------
None
"""
# Only attempt reading if the header indicates there are analog input channels to read
if header['num_board_adc_channels'] > 0:
# For traditional file format, read this block's analog input data from .rhd file into data['board_adc_data']
if file_format == 'traditional':
read_into_2D(destination=data['board_adc_data'],
offset=indices['board_adc'],
fid=fids['fid'],
dtype='uint16',
num_channels=header['num_board_adc_channels'],
num_samples=header['num_samples_per_data_block'],
order='C',
repeat_factor=1
)
# For one file per signal type, read this block's analog input data from analogin.dat into data['board_adc_data']
elif file_format == 'per_signal_type':
read_into_2D(destination=data['board_adc_data'],
offset=indices['board_adc'],
fid=fids['analogin.dat'],
dtype='uint16',
num_channels=header['num_board_adc_channels'],
num_samples=header['num_samples_per_data_block'],
order='F',
repeat_factor=1
)
# For one file per channel format, read this block's analog input data from various .dat files into data['board_adc_data']
elif file_format == 'per_channel':
for idx, channel in enumerate(header['board_adc_channels']):
read_into_1D(destination=data['board_adc_data'][idx],
offset=indices['board_adc'],
format_string='<' + 'H' * header['num_samples_per_data_block'],
fid=fids['board-' + channel['native_channel_name'] + '.dat'],
bytes_per_sample=2,
num_samples=header['num_samples_per_data_block']
)
else:
raise Exception('Unrecognized file format: {}'.format(file_format))
def read_board_dac_block(header, data, indices, fids, file_format):
""" Populate data['board_dac_data'] with a block of analog output data.
Parameters
----------
header : dict
Dict containing previously read header information
data : dict
Dict containing fields to write data to - in this case, data['board_dac_data']
indices : dict
Dict containing indices keeping track of written data position
fids : dict
Dict containing binary streams of files to read from
file_format : str
Which file format this read is following - 'traditional', 'per_signal_type', or 'per_channel'
Returns
-------
None
"""
if header['filetype'] != 'rhs':
return
# Only attempt reading if the header indicates there are analog output channels to read
if header['num_board_dac_channels'] > 0:
# For traditional file format, read this block's analog output data from intan file into data['board_dac_data']
if file_format == 'traditional':
read_into_2D(destination=data['board_dac_data'],
offset=indices['board_dac'],
fid=fids['fid'],
dtype='uint16',
num_channels=header['num_board_dac_channels'],
num_samples=header['num_samples_per_data_block'],
order='C',
repeat_factor=1
)
# For one file per signal type, read this block's analog output data from analogout.dat into data['board_dac_data']
elif file_format == 'per_signal_type':
read_into_2D(destination=data['board_dac_data'],
offset=indices['board_dac'],
fid=fids['analogout.dat'],
dtype='uint16',
num_channels=header['num_board_dac_channels'],
num_samples=header['num_samples_per_data_block'],
order='F',
repeat_factor=1
)
# For one file per channel format, read this block's analog output data from various .dat files into data['board_dac_data']
elif file_format == 'per_channel':
for idx, channel in enumerate(header['board_dac_channels']):
read_into_1D(destination=data['board_dac_data'][idx],
offset=indices['board_dac'],
format_string='<' + 'H' * header['num_samples_per_data_block'],
fid=fids['board-' + channel['native_channel_name'] + '.dat'],
bytes_per_sample=2,
num_samples=header['num_samples_per_data_block']
)
else:
raise Exception('Unrecognized file format: {}'.format(file_format))
def read_board_dig_in_block(header, data, indices, fids, file_format):
""" Populate data['board_dig_in_raw'] with a block of digital input data.
Parameters
----------
header : dict
Dict containing previously read header information
data : dict
Dict containing fields to write data to - in this case, data['board_dig_in_raw']
indices : dict
Dict containing indices keeping track of written data position
fids : dict
Dict containing binary streams of files to read from
file_format : str
Which file format this read is following - 'traditional', 'per_signal_type', or 'per_channel'
Returns
-------
None
"""
# Only attempt reading if the header indicates there are digital input channels to read
if header['num_board_dig_in_channels'] > 0:
# Determine which binary stream to read digital input from
if file_format == 'traditional':
fid = fids['fid']
use_raw = True
elif file_format == 'per_signal_type':
fid = fids['digitalin.dat']
use_raw = True
elif file_format == 'per_channel':
use_raw = False
else:
raise Exception('Unrecognized file format: {}'.format(file_format))
if use_raw:
# Read this block's digital input data from binary stream into data['board_dig_in_raw'].
# Later on, this raw data array will be separated to individual digital input channels.
read_into_1D(destination=data['board_dig_in_raw'],
offset=indices['board_dig_in'],
format_string='<' + 'H' * header['num_samples_per_data_block'],
fid=fid,
bytes_per_sample=2,
num_samples=header['num_samples_per_data_block']
)
else:
for idx, channel in enumerate(header['board_dig_in_channels']):
read_into_1D(destination=data['board_dig_in_data'][idx],
offset=indices['board_dig_in'],
format_string='<' + 'H' * header['num_samples_per_data_block'],
fid=fids['board-' + channel['native_channel_name'] + '.dat'],
bytes_per_sample=2,
num_samples=header['num_samples_per_data_block']
)
def read_board_dig_out_block(header, data, indices, fids, file_format):
""" Populate data['board_dig_out_raw'] with a block of digital output data.
Parameters
----------
header : dict
Dict containing previously read header information
data : dict
Dict containing fields to write data to - in this case, data['board_dig_out_raw']
indices : dict
Dict containing indices keeping track of written data position
fids : dict
Dict containing binary streams of files to read from
file_format : str
Which file format this read is following - 'traditional', 'per_signal_type', or 'per_channel'
Returns
-------
None
"""
# Only attempt reading if the header indicates there are digital output channels to read
if header['num_board_dig_out_channels'] > 0:
# Determine which binary stream to read digital output from
if file_format == 'traditional':
fid = fids['fid']
use_raw = True
elif file_format == 'per_signal_type':
fid = fids['digitalout.dat']
use_raw = True
elif file_format == 'per_channel':
use_raw = False
else:
raise Exception('Unrecognized file format: {}'.format(file_format))
if use_raw:
# Read this block's digital output data from binary stream into data['board_dig_out_raw'].
# Later on, this raw data array will be separated to individual digital output channels.
read_into_1D(destination=data['board_dig_out_raw'],
offset=indices['board_dig_out'],
format_string='<' + 'H' * header['num_samples_per_data_block'],
fid=fid,
bytes_per_sample=2,
num_samples=header['num_samples_per_data_block']
)
else:
for idx, channel in enumerate(header['board_dig_out_channels']):
read_into_1D(destination=data['board_dig_out_data'][idx],
offset=indices['board_dig_out'],
format_string='<' + 'H' * header['num_samples_per_data_block'],
fid=fids['board-' + channel['native_channel_name'] + '.dat'],
bytes_per_sample=2,
num_samples=header['num_samples_per_data_block']
)
def read_one_data_block(header, data, indices, fids, file_format):
""" Read one 60 or 128 sample data block into data, and increment all indices for that block.
Parameters
----------
header : dict
Dict containing previously read header information
data : dict
Dict containing fields to write data to - in this case, data['board_dig_out_raw']
indices : dict
Dict containing indices keeping track of written data position
fids : dict
Dict containing binary streams of files to read from
file_format : str
Which file format this read is following - 'traditional', 'per_signal_type', or 'per_channel'
Returns
-------
None
"""
if header['filetype'] == 'rhd':
# Read each possible signal type (each function checks if that signal type is present before reading)
read_timestamp_block(header, data, indices, fids, file_format)
read_amplifier_block(header, data, indices, fids, file_format)
read_lowpass_block(header, data, indices, fids, file_format)
read_highpass_block(header, data, indices, fids, file_format)
read_aux_input_block(header, data, indices, fids, file_format)
read_supply_voltage_block(header, data, indices, fids, file_format)
read_temp_sensor_block(header, data, indices, fids, file_format)
read_board_adc_block(header, data, indices, fids, file_format)
read_board_dig_in_block(header, data, indices, fids, file_format)
read_board_dig_out_block(header, data, indices, fids, file_format)
# Increment indices
indices['amplifier'] += header['num_samples_per_data_block']
indices['aux_input'] += int(header['num_samples_per_data_block'] / 4)
indices['supply_voltage'] += 1
indices['board_adc'] += header['num_samples_per_data_block']
indices['board_dig_in'] += header['num_samples_per_data_block']
indices['board_dig_out'] += header['num_samples_per_data_block']
else:
# Read each possible signal type (each function checks if that signal type is present before reading)
read_timestamp_block(header, data, indices, fids, file_format)
read_amplifier_block(header, data, indices, fids, file_format)
read_dc_amplifier_block(header, data, indices, fids, file_format)
read_stim_block(header, data, indices, fids, file_format)
read_lowpass_block(header, data, indices, fids, file_format)
read_highpass_block(header, data, indices, fids, file_format)
read_board_adc_block(header, data, indices, fids, file_format)
read_board_dac_block(header, data, indices, fids, file_format)
read_board_dig_in_block(header, data, indices, fids, file_format)
read_board_dig_out_block(header, data, indices, fids, file_format)
# Increment indices
indices['amplifier'] += header['num_samples_per_data_block']
indices['board_adc'] += header['num_samples_per_data_block']
indices['board_dac'] += header['num_samples_per_data_block']
indices['board_dig_in'] += header['num_samples_per_data_block']
indices['board_dig_out'] += header['num_samples_per_data_block']