-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_legacy.go
More file actions
556 lines (528 loc) · 16.8 KB
/
Copy pathbinary_legacy.go
File metadata and controls
556 lines (528 loc) · 16.8 KB
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
package main
import (
"encoding/binary"
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"io"
"math"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
func convertLegacyRootToBinary(input, output string, overwrite bool) error {
nodes, err := findRecordNodeDirs(input)
if err != nil {
return err
}
if len(nodes) == 0 {
return fmt.Errorf("no legacy Open Ephys Record Node was found under %s", input)
}
if err := os.MkdirAll(output, 0755); err != nil {
return err
}
var totalWork int64
work := make(map[string]int64, len(nodes))
for _, node := range nodes {
w, estimateErr := estimateNodeWork(node)
if estimateErr != nil {
fmt.Printf("warning: could not estimate %s exactly: %v\n", filepath.Base(node), estimateErr)
}
if w <= 0 {
w = 1
}
work[node] = w
totalWork += w
}
progress := NewProgressDisplay(totalWork)
defer progress.Close()
converted, skipped, failures := 0, 0, 0
for i, node := range nodes {
rel, relErr := filepath.Rel(input, node)
if relErr != nil || rel == "." {
rel = filepath.Base(node)
}
dst := filepath.Join(output, rel)
progress.SetSession(fmt.Sprintf("[%d/%d] %s", i+1, len(nodes), rel), work[node])
c, s, convertErr := convertRecordNode(node, dst, overwrite, progress)
converted += c
skipped += s
if convertErr != nil {
failures++
progress.Messagef(" [ERROR] %v", convertErr)
}
progress.FinishSession()
}
progress.FinishAll()
fmt.Printf("Summary: %d converted, %d skipped, %d failed\n", converted, skipped, failures)
if failures > 0 {
return fmt.Errorf("%d Record Node conversion(s) failed", failures)
}
return nil
}
type npyArrayFile struct {
file *os.File
dataOffset int64
count int64
elemSize int64
}
func openNpyArray(path, expectedDescr string, elemSize int64) (*npyArrayFile, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
pre := make([]byte, 10)
if _, err := io.ReadFull(f, pre); err != nil {
f.Close()
return nil, err
}
if string(pre[0:6]) != "\x93NUMPY" || pre[6] != 1 || pre[7] != 0 {
f.Close()
return nil, fmt.Errorf("unsupported or invalid NPY header: %s", path)
}
headerLen := int64(binary.LittleEndian.Uint16(pre[8:10]))
header := make([]byte, headerLen)
if _, err := io.ReadFull(f, header); err != nil {
f.Close()
return nil, err
}
headerText := string(header)
if expectedDescr != "" && !strings.Contains(headerText, "'descr': '"+expectedDescr+"'") && !strings.Contains(headerText, "\"descr\": \""+expectedDescr+"\"") {
f.Close()
return nil, fmt.Errorf("NPY dtype mismatch in %s; expected %s", path, expectedDescr)
}
st, err := f.Stat()
if err != nil {
f.Close()
return nil, err
}
dataOffset := 10 + headerLen
dataBytes := st.Size() - dataOffset
if dataBytes < 0 || elemSize <= 0 || dataBytes%elemSize != 0 {
f.Close()
return nil, fmt.Errorf("invalid NPY payload length in %s", path)
}
return &npyArrayFile{file: f, dataOffset: dataOffset, count: dataBytes / elemSize, elemSize: elemSize}, nil
}
func (a *npyArrayFile) Close() error {
if a == nil || a.file == nil {
return nil
}
return a.file.Close()
}
func (a *npyArrayFile) ReadAt(index int64, dst []byte) error {
if index < 0 || index >= a.count || int64(len(dst)) != a.elemSize {
return fmt.Errorf("NPY scalar read out of range")
}
_, err := a.file.ReadAt(dst, a.dataOffset+index*a.elemSize)
return err
}
func (a *npyArrayFile) ReadAllBytes() ([]byte, error) {
if a.count > int64(int(^uint(0)>>1))/a.elemSize {
return nil, errors.New("NPY array is too large for this build")
}
data := make([]byte, int(a.count*a.elemSize))
_, err := a.file.ReadAt(data, a.dataOffset)
return data, err
}
func readNpyInt64All(path string) ([]int64, error) {
a, err := openNpyArray(path, "<i8", 8)
if err != nil {
return nil, err
}
defer a.Close()
raw, err := a.ReadAllBytes()
if err != nil && err != io.EOF {
return nil, err
}
out := make([]int64, a.count)
for i := range out {
out[i] = int64(binary.LittleEndian.Uint64(raw[i*8 : i*8+8]))
}
return out, nil
}
func readNpyInt16All(path string) ([]int16, error) {
a, err := openNpyArray(path, "<i2", 2)
if err != nil {
return nil, err
}
defer a.Close()
raw, err := a.ReadAllBytes()
if err != nil && err != io.EOF {
return nil, err
}
out := make([]int16, a.count)
for i := range out {
out[i] = int16(binary.LittleEndian.Uint16(raw[i*2 : i*2+2]))
}
return out, nil
}
func readNpyFloat64All(path string) ([]float64, error) {
a, err := openNpyArray(path, "<f8", 8)
if err != nil {
return nil, err
}
defer a.Close()
raw, err := a.ReadAllBytes()
if err != nil && err != io.EOF {
return nil, err
}
out := make([]float64, a.count)
for i := range out {
out[i] = math.Float64frombits(binary.LittleEndian.Uint64(raw[i*8 : i*8+8]))
}
return out, nil
}
func findSingleBinaryRecording(path string) (string, OEBin, error) {
st, err := os.Stat(path)
if err != nil {
return "", OEBin{}, err
}
if !st.IsDir() {
if strings.EqualFold(filepath.Base(path), "structure.oebin") {
return readBinaryRecording(filepath.Dir(path))
}
return "", OEBin{}, fmt.Errorf("Binary input must be a recording directory or structure.oebin")
}
var structures []string
err = filepath.WalkDir(path, func(candidate string, entry os.DirEntry, walkErr error) error {
if walkErr != nil {
return walkErr
}
if !entry.IsDir() && strings.EqualFold(entry.Name(), "structure.oebin") {
structures = append(structures, candidate)
}
return nil
})
if err != nil {
return "", OEBin{}, err
}
if len(structures) != 1 {
return "", OEBin{}, fmt.Errorf("expected exactly one Binary recording (structure.oebin) under %s; found %d", path, len(structures))
}
return readBinaryRecording(filepath.Dir(structures[0]))
}
func readBinaryRecording(dir string) (string, OEBin, error) {
data, err := os.ReadFile(filepath.Join(dir, "structure.oebin"))
if err != nil {
return "", OEBin{}, err
}
var info OEBin
if err := json.Unmarshal(data, &info); err != nil {
return "", OEBin{}, fmt.Errorf("cannot parse structure.oebin: %w", err)
}
if len(info.Continuous) == 0 {
return "", OEBin{}, errors.New("structure.oebin contains no continuous streams")
}
return dir, info, nil
}
func convertBinaryToLegacy(input, output string, overwrite bool) error {
dir, info, err := findSingleBinaryRecording(input)
if err != nil {
return err
}
if err := prepareDirectoryOutput(output, overwrite); err != nil {
return err
}
var totalWork int64
streamWork := make([]int64, len(info.Continuous))
for i, stream := range info.Continuous {
continuousDir := filepath.Join(dir, "continuous", filepath.FromSlash(strings.TrimSuffix(stream.FolderName, "/")))
if stat, statErr := os.Stat(filepath.Join(continuousDir, "continuous.dat")); statErr == nil {
streamWork[i] = stat.Size()
totalWork += stat.Size()
}
}
if totalWork <= 0 {
totalWork = 1
}
progress := NewProgressDisplay(totalWork)
defer progress.Close()
progress.SetSession("Binary recording -> legacy Open Ephys", totalWork)
root := XMLNode{XMLName: xmlName("EXPERIMENT"), Attr: []xml.Attr{
xmlAttr("format_version", "0.4"), xmlAttr("number", "1"),
}}
recording := XMLNode{XMLName: xmlName("RECORDING"), Attr: []xml.Attr{xmlAttr("number", "1")}}
for streamIndex, stream := range info.Continuous {
if stream.NumChannels <= 0 || len(stream.Channels) != stream.NumChannels {
return fmt.Errorf("stream %q has inconsistent channel metadata", stream.StreamName)
}
continuousDir := filepath.Join(dir, "continuous", filepath.FromSlash(strings.TrimSuffix(stream.FolderName, "/")))
work := streamWork[streamIndex]
if work <= 0 {
work = 1
}
progress.SetTask(stream.StreamName, work)
if err := exportBinaryStreamToLegacy(continuousDir, output, stream, streamIndex, info.Events, &recording, progress); err != nil {
return fmt.Errorf("stream %q: %w", stream.StreamName, err)
}
progress.FinishTask()
}
progress.FinishSession()
progress.FinishAll()
root.Children = append(root.Children, recording)
if err := writeXMLFile(filepath.Join(output, "structure.openephys"), root); err != nil {
return err
}
copyIfExists(filepath.Join(dir, "settings.xml"), filepath.Join(output, "settings.xml"))
return nil
}
func exportBinaryStreamToLegacy(continuousDir, output string, stream OEBinContinuous, streamIndex int, allEvents []OEBinEvent, recording *XMLNode, progress *ProgressDisplay) error {
dat, err := os.Open(filepath.Join(continuousDir, "continuous.dat"))
if err != nil {
return err
}
defer dat.Close()
stat, err := dat.Stat()
if err != nil {
return err
}
rowBytes := int64(stream.NumChannels) * 2
if rowBytes <= 0 || stat.Size()%rowBytes != 0 {
return fmt.Errorf("continuous.dat size is not divisible by %d", rowBytes)
}
totalSamples := stat.Size() / rowBytes
recordCount := (totalSamples + samplesPerRecord - 1) / samplesPerRecord
sampleNumbers, err := openNpyArray(filepath.Join(continuousDir, "sample_numbers.npy"), "<i8", 8)
if err != nil {
return fmt.Errorf("sample_numbers.npy: %w", err)
}
defer sampleNumbers.Close()
timestamps, err := openNpyArray(filepath.Join(continuousDir, "timestamps.npy"), "<f8", 8)
if err != nil {
return fmt.Errorf("timestamps.npy: %w", err)
}
defer timestamps.Close()
if sampleNumbers.count < totalSamples || timestamps.count < totalSamples {
return fmt.Errorf("sample_numbers.npy/timestamps.npy contain fewer values than continuous.dat")
}
safeStream := sanitizeComponent(stream.StreamName)
channelFiles := make([]*os.File, stream.NumChannels)
channelNames := make([]string, stream.NumChannels)
for i, channel := range stream.Channels {
name := fmt.Sprintf("100_%s_%s.continuous", safeStream, sanitizeComponent(channel.ChannelName))
channelNames[i] = name
f, createErr := os.Create(filepath.Join(output, name))
if createErr != nil {
return createErr
}
channelFiles[i] = f
if createErr := writeLegacyContinuousHeader(f, channel, stream); createErr != nil {
return createErr
}
}
closeChannels := func() {
for _, f := range channelFiles {
if f != nil {
_ = f.Close()
}
}
}
defer closeChannels()
timestampName := fmt.Sprintf("100_%s.timestamps", safeStream)
tsFile, err := os.Create(filepath.Join(output, timestampName))
if err != nil {
return err
}
defer tsFile.Close()
var scalar [8]byte
row := make([]byte, int(rowBytes)*samplesPerRecord)
record := make([]byte, recordSize)
for rec := int64(0); rec < recordCount; rec++ {
start := rec * samplesPerRecord
remaining := totalSamples - start
count := int64(samplesPerRecord)
if remaining < count {
count = remaining
}
n, err := dat.ReadAt(row[:int(count*rowBytes)], start*rowBytes)
if err != nil && err != io.EOF {
return err
}
if err := sampleNumbers.ReadAt(start, scalar[:]); err != nil {
return err
}
sampleStart := int64(binary.LittleEndian.Uint64(scalar[:]))
if err := timestamps.ReadAt(start, scalar[:]); err != nil {
return err
}
timestamp := math.Float64frombits(binary.LittleEndian.Uint64(scalar[:]))
binary.LittleEndian.PutUint64(scalar[:], math.Float64bits(timestamp))
if _, err := tsFile.Write(scalar[:]); err != nil {
return err
}
for channel := 0; channel < stream.NumChannels; channel++ {
for i := range record {
record[i] = 0
}
binary.LittleEndian.PutUint64(record[0:8], uint64(sampleStart))
binary.LittleEndian.PutUint16(record[8:10], uint16(samplesPerRecord))
binary.BigEndian.PutUint16(record[10:12], 1)
for sample := 0; sample < samplesPerRecord; sample++ {
value := int16(0)
if int64(sample) < count {
off := (sample*stream.NumChannels + channel) * 2
value = int16(binary.LittleEndian.Uint16(row[off : off+2]))
}
binary.BigEndian.PutUint16(record[12+sample*2:14+sample*2], uint16(value))
}
copy(record[recordSize-10:], recordMarker[:])
if _, err := channelFiles[channel].Write(record); err != nil {
return err
}
}
if progress != nil {
progress.Add(int64(n))
}
}
eventEntries := eventMetadataForStream(stream, streamIndex, allEvents)
eventFilename := ""
if len(eventEntries) > 0 {
eventFilename = fmt.Sprintf("100_%s.events", safeStream)
if err := exportBinaryEventsToLegacy(continuousDir, output, eventEntries, eventFilename); err != nil {
return err
}
}
streamNode := XMLNode{XMLName: xmlName("STREAM"), Attr: []xml.Attr{
xmlAttr("name", stream.StreamName),
xmlAttr("source_node_id", strconv.Itoa(stream.SourceProcessorID)),
xmlAttr("source_node_name", stream.SourceProcessorName),
xmlAttr("sample_rate", strconv.FormatFloat(stream.SampleRate, 'g', -1, 64)),
}}
for i, channel := range stream.Channels {
streamNode.Children = append(streamNode.Children, XMLNode{XMLName: xmlName("CHANNEL"), Attr: []xml.Attr{
xmlAttr("name", channel.ChannelName),
xmlAttr("bitVolts", strconv.FormatFloat(channel.BitVolts, 'g', -1, 64)),
xmlAttr("type", "continuous"),
xmlAttr("filename", channelNames[i]),
}})
}
streamNode.Children = append(streamNode.Children, XMLNode{XMLName: xmlName("TIMESTAMPS"), Attr: []xml.Attr{xmlAttr("filename", timestampName)}})
if eventFilename != "" {
streamNode.Children = append(streamNode.Children, XMLNode{XMLName: xmlName("EVENTS"), Attr: []xml.Attr{xmlAttr("filename", eventFilename)}})
}
recording.Children = append(recording.Children, streamNode)
return nil
}
func writeLegacyContinuousHeader(w io.Writer, channel OEBinChannel, stream OEBinContinuous) error {
header := fmt.Sprintf("header.version = 0.4\nformat = 'Open Ephys Data Format'\nversion = 0.4\ndescription = 'Open Ephys Data Format'\ndate_created = '%s'\nchannel = '%s'\nchannelType = 'continuous'\nsampleRate = %s\nblockLength = %d\nbitVolts = %s\n",
time.Now().Format("2006-01-02 15:04:05"), channel.ChannelName, strconv.FormatFloat(stream.SampleRate, 'g', -1, 64), samplesPerRecord, strconv.FormatFloat(channel.BitVolts, 'g', -1, 64))
buf := make([]byte, headerBytes)
copy(buf, header)
_, err := w.Write(buf)
return err
}
func eventMetadataForStream(stream OEBinContinuous, streamIndex int, events []OEBinEvent) []OEBinEvent {
_ = streamIndex
var out []OEBinEvent
for _, event := range events {
if event.StreamName == stream.StreamName || event.StreamName == "" {
out = append(out, event)
}
}
return out
}
func exportBinaryEventsToLegacy(continuousDir, output string, entries []OEBinEvent, filename string) error {
binaryRoot := filepath.Dir(filepath.Dir(continuousDir))
path := filepath.Join(output, filename)
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
header := make([]byte, headerBytes)
copy(header, fmt.Sprintf("header.version = 0.4\nformat = 'Open Ephys Data Format'\nversion = 0.4\ndescription = 'Open Ephys event data'\n"))
if _, err := f.Write(header); err != nil {
return err
}
for _, entry := range entries {
folder := filepath.FromSlash(strings.TrimSuffix(entry.FolderName, "/"))
eventDir := filepath.Join(binaryRoot, "events", folder)
samples, err := readNpyInt64All(filepath.Join(eventDir, "sample_numbers.npy"))
if err != nil {
return err
}
states, err := readNpyInt16All(filepath.Join(eventDir, "states.npy"))
if err != nil {
return err
}
if len(samples) != len(states) {
return fmt.Errorf("event sample/state array lengths differ in %s", eventDir)
}
processorID := uint8(0)
name := filepath.Base(filepath.Dir(eventDir))
if strings.HasPrefix(name, "Legacy_Event-") {
id := strings.TrimPrefix(name, "Legacy_Event-")
id = strings.SplitN(id, ".", 2)[0]
if n, parseErr := strconv.Atoi(id); parseErr == nil && n >= 0 && n <= 255 {
processorID = uint8(n)
}
}
for i, sample := range samples {
line := int(states[i])
state := uint8(0)
if line > 0 {
state = 1
} else {
line = -line
}
if line < 1 {
line = 1
}
if line > 255 {
line = 255
}
record := make([]byte, eventRecordSize)
binary.LittleEndian.PutUint64(record[0:8], uint64(sample))
binary.LittleEndian.PutUint16(record[8:10], 0)
record[11] = processorID
record[12] = state
record[13] = byte(line - 1)
binary.LittleEndian.PutUint16(record[14:16], 1)
if _, err := f.Write(record); err != nil {
return err
}
}
}
return nil
}
func prepareDirectoryOutput(path string, overwrite bool) error {
if st, err := os.Stat(path); err == nil {
if !overwrite {
return fmt.Errorf("output already exists: %s (use --overwrite to replace it)", path)
}
if !st.IsDir() {
return fmt.Errorf("output must be a directory: %s", path)
}
if err := os.RemoveAll(path); err != nil {
return err
}
} else if !os.IsNotExist(err) {
return err
}
return os.MkdirAll(path, 0755)
}
func xmlName(local string) xml.Name {
return xml.Name{Local: local}
}
func xmlAttr(name, value string) xml.Attr {
return xml.Attr{Name: xml.Name{Local: name}, Value: value}
}
func writeXMLFile(path string, node XMLNode) error {
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
enc := xml.NewEncoder(f)
if _, err := f.WriteString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); err != nil {
return err
}
if err := enc.Encode(node); err != nil {
return err
}
return enc.Flush()
}