-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnpy.go
More file actions
684 lines (609 loc) · 17.3 KB
/
Copy pathnpy.go
File metadata and controls
684 lines (609 loc) · 17.3 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
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
package npy
import (
"archive/zip"
"bytes"
"encoding/binary"
"fmt"
"io"
"os"
"regexp"
"strconv"
"strings"
)
// DType represents NumPy data types
type DType string
// NumPy data types
const (
Bool DType = "bool"
Int8 DType = "int8"
Int16 DType = "int16"
Int32 DType = "int32"
Int64 DType = "int64"
Uint8 DType = "uint8"
Uint16 DType = "uint16"
Uint32 DType = "uint32"
Uint64 DType = "uint64"
Float32 DType = "float32"
Float64 DType = "float64"
)
// Array represents a NumPy array with type parameter for data
type Array[T any] struct {
Data []T
Shape []int
DType DType
Fortran bool // True if array is in Fortran order (column-major)
}
// header represents the metadata in a NumPy file
type header struct {
Shape []int
DType DType
Fortran bool
}
// ReadFile reads a NumPy array from a .npy file with the specified type
func ReadFile[T any](path string) (*Array[T], error) {
// Check file extension to ensure we're reading a .npy file
if !strings.HasSuffix(path, ".npy") {
return nil, fmt.Errorf("expected .npy file extension, got %s", path)
}
f, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("failed to open file: %w", err)
}
defer f.Close()
return Read[T](f)
}
// WriteFile writes a NumPy array to a .npy file
func WriteFile[T any](path string, arr *Array[T]) error {
// Ensure correct file extension
if !strings.HasSuffix(path, ".npy") {
path += ".npy" // Automatically add extension if missing
}
f, err := os.Create(path)
if err != nil {
return fmt.Errorf("failed to create file: %w", err)
}
defer f.Close()
return Write(f, arr)
}
// NPZFile represents a NumPy .npz file containing multiple arrays
type NPZFile struct {
arrays map[string]interface{}
}
// NewNPZFile creates a new empty NPZ file
func NewNPZFile() *NPZFile {
return &NPZFile{
arrays: make(map[string]interface{}),
}
}
// Add adds an array to the NPZ file
func Add[T any](npz *NPZFile, name string, arr *Array[T]) {
npz.arrays[name] = arr
}
// Get retrieves an array from the NPZ file
func Get[T any](npz *NPZFile, name string) (*Array[T], bool) {
val, ok := npz.arrays[name]
if !ok {
return nil, false
}
arr, ok := val.(*Array[T])
return arr, ok
}
// Keys returns the names of all arrays in the NPZ file
func Keys(npz *NPZFile) []string {
keys := make([]string, 0, len(npz.arrays))
for k := range npz.arrays {
keys = append(keys, k)
}
return keys
}
// ReadNPZFile reads multiple NumPy arrays from a .npz file
func ReadNPZFile(path string) (*NPZFile, error) {
// Check file extension
if !strings.HasSuffix(path, ".npz") {
return nil, fmt.Errorf("expected .npz file extension, got %s", path)
}
// Open the zip file
zipReader, err := zip.OpenReader(path)
if err != nil {
return nil, fmt.Errorf("failed to open NPZ file: %w", err)
}
defer zipReader.Close()
// Create NPZ file
npz := NewNPZFile()
// Process each file in the zip
for _, f := range zipReader.File {
// Skip directories
if f.FileInfo().IsDir() {
continue
}
// Extract name
name := f.Name
name = strings.TrimSuffix(name, ".npy")
// Open the file
rc, err := f.Open()
if err != nil {
return nil, fmt.Errorf("failed to open file %s in NPZ: %w", f.Name, err)
}
// We need to determine the type of the array before we can read it
// Since we can't know the type in advance, we'll read the header first to peek at the dtype
// This is a bit hacky, but we don't have a better option with Go's type system
// Read magic string and version
magic := make([]byte, 6)
if _, err := io.ReadFull(rc, magic); err != nil {
rc.Close()
return nil, fmt.Errorf("failed to read magic string from %s: %w", f.Name, err)
}
// Read version
var major, minor uint8
if err := binary.Read(rc, binary.LittleEndian, &major); err != nil {
rc.Close()
return nil, fmt.Errorf("failed to read major version from %s: %w", f.Name, err)
}
if err := binary.Read(rc, binary.LittleEndian, &minor); err != nil {
rc.Close()
return nil, fmt.Errorf("failed to read minor version from %s: %w", f.Name, err)
}
// Read header length
var headerLen uint16
if major == 1 {
if err := binary.Read(rc, binary.LittleEndian, &headerLen); err != nil {
rc.Close()
return nil, fmt.Errorf("failed to read header length from %s: %w", f.Name, err)
}
} else if major == 2 {
var headerLen32 uint32
if err := binary.Read(rc, binary.LittleEndian, &headerLen32); err != nil {
rc.Close()
return nil, fmt.Errorf("failed to read header length from %s: %w", f.Name, err)
}
headerLen = uint16(headerLen32)
} else {
rc.Close()
return nil, fmt.Errorf("unsupported version in %s: %d.%d", f.Name, major, minor)
}
// Read header
headerBytes := make([]byte, headerLen)
if _, err := io.ReadFull(rc, headerBytes); err != nil {
rc.Close()
return nil, fmt.Errorf("failed to read header from %s: %w", f.Name, err)
}
// Parse header
hdr, err := parseHeader(string(headerBytes))
if err != nil {
rc.Close()
return nil, fmt.Errorf("failed to parse header from %s: %w", f.Name, err)
}
// Close the reader - we'll reopen the file to read the full array with proper typing
rc.Close()
// Reopen the file
rc, err = f.Open()
if err != nil {
return nil, fmt.Errorf("failed to reopen file %s in NPZ: %w", f.Name, err)
}
// Read array based on dtype
var array interface{}
switch hdr.DType {
case Bool:
arr, err := Read[bool](rc)
if err != nil {
rc.Close()
return nil, fmt.Errorf("failed to read bool array from %s: %w", f.Name, err)
}
array = arr
case Int8:
arr, err := Read[int8](rc)
if err != nil {
rc.Close()
return nil, fmt.Errorf("failed to read int8 array from %s: %w", f.Name, err)
}
array = arr
case Int16:
arr, err := Read[int16](rc)
if err != nil {
rc.Close()
return nil, fmt.Errorf("failed to read int16 array from %s: %w", f.Name, err)
}
array = arr
case Int32:
arr, err := Read[int32](rc)
if err != nil {
rc.Close()
return nil, fmt.Errorf("failed to read int32 array from %s: %w", f.Name, err)
}
array = arr
case Int64:
arr, err := Read[int64](rc)
if err != nil {
rc.Close()
return nil, fmt.Errorf("failed to read int64 array from %s: %w", f.Name, err)
}
array = arr
case Uint8:
arr, err := Read[uint8](rc)
if err != nil {
rc.Close()
return nil, fmt.Errorf("failed to read uint8 array from %s: %w", f.Name, err)
}
array = arr
case Uint16:
arr, err := Read[uint16](rc)
if err != nil {
rc.Close()
return nil, fmt.Errorf("failed to read uint16 array from %s: %w", f.Name, err)
}
array = arr
case Uint32:
arr, err := Read[uint32](rc)
if err != nil {
rc.Close()
return nil, fmt.Errorf("failed to read uint32 array from %s: %w", f.Name, err)
}
array = arr
case Uint64:
arr, err := Read[uint64](rc)
if err != nil {
rc.Close()
return nil, fmt.Errorf("failed to read uint64 array from %s: %w", f.Name, err)
}
array = arr
case Float32:
arr, err := Read[float32](rc)
if err != nil {
rc.Close()
return nil, fmt.Errorf("failed to read float32 array from %s: %w", f.Name, err)
}
array = arr
case Float64:
arr, err := Read[float64](rc)
if err != nil {
rc.Close()
return nil, fmt.Errorf("failed to read float64 array from %s: %w", f.Name, err)
}
array = arr
default:
rc.Close()
return nil, fmt.Errorf("unsupported dtype in %s: %s", f.Name, hdr.DType)
}
rc.Close()
npz.arrays[name] = array
}
return npz, nil
}
// WriteNPZFile writes multiple NumPy arrays to a .npz file
func WriteNPZFile(path string, npz *NPZFile) error {
// Ensure correct file extension
if !strings.HasSuffix(path, ".npz") {
path += ".npz" // Automatically add extension if missing
}
// Create the zip file
zipFile, err := os.Create(path)
if err != nil {
return fmt.Errorf("failed to create NPZ file: %w", err)
}
defer zipFile.Close()
// Create zip writer
zipWriter := zip.NewWriter(zipFile)
defer zipWriter.Close()
// Write each array to the zip
for name, array := range npz.arrays {
// Ensure name has .npy extension
if !strings.HasSuffix(name, ".npy") {
name += ".npy"
}
// Create file in zip
w, err := zipWriter.Create(name)
if err != nil {
return fmt.Errorf("failed to create file %s in NPZ: %w", name, err)
}
// Write array based on type
switch arr := array.(type) {
case *Array[bool]:
if err := Write(w, arr); err != nil {
return fmt.Errorf("failed to write bool array to %s: %w", name, err)
}
case *Array[int8]:
if err := Write(w, arr); err != nil {
return fmt.Errorf("failed to write int8 array to %s: %w", name, err)
}
case *Array[int16]:
if err := Write(w, arr); err != nil {
return fmt.Errorf("failed to write int16 array to %s: %w", name, err)
}
case *Array[int32]:
if err := Write(w, arr); err != nil {
return fmt.Errorf("failed to write int32 array to %s: %w", name, err)
}
case *Array[int64]:
if err := Write(w, arr); err != nil {
return fmt.Errorf("failed to write int64 array to %s: %w", name, err)
}
case *Array[uint8]:
if err := Write(w, arr); err != nil {
return fmt.Errorf("failed to write uint8 array to %s: %w", name, err)
}
case *Array[uint16]:
if err := Write(w, arr); err != nil {
return fmt.Errorf("failed to write uint16 array to %s: %w", name, err)
}
case *Array[uint32]:
if err := Write(w, arr); err != nil {
return fmt.Errorf("failed to write uint32 array to %s: %w", name, err)
}
case *Array[uint64]:
if err := Write(w, arr); err != nil {
return fmt.Errorf("failed to write uint64 array to %s: %w", name, err)
}
case *Array[float32]:
if err := Write(w, arr); err != nil {
return fmt.Errorf("failed to write float32 array to %s: %w", name, err)
}
case *Array[float64]:
if err := Write(w, arr); err != nil {
return fmt.Errorf("failed to write float64 array to %s: %w", name, err)
}
default:
return fmt.Errorf("unsupported array type in %s", name)
}
}
return nil
}
// readData reads the actual data from the file based on the header information
func readData[T any](r io.Reader, hdr *header) ([]T, error) {
// Calculate total number of elements
totalElements := 1
for _, dim := range hdr.Shape {
totalElements *= dim
}
// Allocate slice for data
data := make([]T, totalElements)
// Read data
if err := binary.Read(r, binary.LittleEndian, &data); err != nil {
return nil, fmt.Errorf("failed to read data: %w", err)
}
return data, nil
}
// generateHeader creates a header string for a NumPy array
func generateHeader[T any](arr *Array[T]) string {
// Map Go dtype to NumPy dtype
var dtypeStr string
switch arr.DType {
case Bool:
dtypeStr = "|b1"
case Int8:
dtypeStr = "|i1"
case Int16:
dtypeStr = "<i2"
case Int32:
dtypeStr = "<i4"
case Int64:
dtypeStr = "<i8"
case Uint8:
dtypeStr = "|u1"
case Uint16:
dtypeStr = "<u2"
case Uint32:
dtypeStr = "<u4"
case Uint64:
dtypeStr = "<u8"
case Float32:
dtypeStr = "<f4"
case Float64:
dtypeStr = "<f8"
default:
dtypeStr = "<f8" // Default to float64
}
// Format shape
shapeStr := "("
for i, dim := range arr.Shape {
if i > 0 {
shapeStr += ", "
}
shapeStr += strconv.Itoa(dim)
}
// Handle empty or single-dimensional arrays
if len(arr.Shape) == 0 {
shapeStr += ","
} else if len(arr.Shape) == 1 {
shapeStr += ","
}
shapeStr += ")"
// Create header string
fortranStr := "False"
if arr.Fortran {
fortranStr = "True"
}
return fmt.Sprintf("{'descr': '%s', 'fortran_order': %s, 'shape': %s, }", dtypeStr, fortranStr, shapeStr)
}
// Read reads a NumPy array from an io.Reader
func Read[T any](r io.Reader) (*Array[T], error) {
// Read magic string and version
magic := make([]byte, 6)
if _, err := io.ReadFull(r, magic); err != nil {
return nil, fmt.Errorf("failed to read magic string: %w", err)
}
if !bytes.Equal(magic, []byte("\x93NUMPY")) {
return nil, fmt.Errorf("invalid magic string: %q", magic)
}
// Read version
var major, minor uint8
if err := binary.Read(r, binary.LittleEndian, &major); err != nil {
return nil, fmt.Errorf("failed to read major version: %w", err)
}
if err := binary.Read(r, binary.LittleEndian, &minor); err != nil {
return nil, fmt.Errorf("failed to read minor version: %w", err)
}
// Read header length
var headerLen uint16
if major == 1 {
if err := binary.Read(r, binary.LittleEndian, &headerLen); err != nil {
return nil, fmt.Errorf("failed to read header length: %w", err)
}
} else if major == 2 {
var headerLen32 uint32
if err := binary.Read(r, binary.LittleEndian, &headerLen32); err != nil {
return nil, fmt.Errorf("failed to read header length: %w", err)
}
headerLen = uint16(headerLen32)
} else {
return nil, fmt.Errorf("unsupported version: %d.%d", major, minor)
}
// Read header
headerBytes := make([]byte, headerLen)
if _, err := io.ReadFull(r, headerBytes); err != nil {
return nil, fmt.Errorf("failed to read header: %w", err)
}
// Parse header
hdr, err := parseHeader(string(headerBytes))
if err != nil {
return nil, fmt.Errorf("failed to parse header: %w", err)
}
// Read data
data, err := readData[T](r, hdr)
if err != nil {
return nil, fmt.Errorf("failed to read data: %w", err)
}
return &Array[T]{
Data: data,
Shape: hdr.Shape,
DType: hdr.DType,
Fortran: hdr.Fortran,
}, nil
}
// Write writes a NumPy array to an io.Writer
func Write[T any](w io.Writer, arr *Array[T]) error {
// Validate array
if arr.Data == nil {
return fmt.Errorf("array data is nil")
}
if arr.Shape == nil {
return fmt.Errorf("array shape is nil")
}
if arr.DType == "" {
return fmt.Errorf("array dtype is empty")
}
// Calculate total number of elements from shape
totalElements := 1
for _, dim := range arr.Shape {
totalElements *= dim
}
// Validate data length
if len(arr.Data) != totalElements {
return fmt.Errorf("data length (%d) does not match shape dimensions (%d)", len(arr.Data), totalElements)
}
// Write magic string and version
if _, err := w.Write([]byte("\x93NUMPY")); err != nil {
return fmt.Errorf("failed to write magic string: %w", err)
}
// Write version (using v1.0)
if err := binary.Write(w, binary.LittleEndian, uint8(1)); err != nil {
return fmt.Errorf("failed to write major version: %w", err)
}
if err := binary.Write(w, binary.LittleEndian, uint8(0)); err != nil {
return fmt.Errorf("failed to write minor version: %w", err)
}
// Generate header
headerStr := generateHeader(arr)
// Header needs to be padded to be a multiple of 16 bytes (including the 10 byte file header)
// for alignment purposes
paddingLen := 16 - ((10 + len(headerStr)) % 16)
if paddingLen < 1 {
paddingLen += 16 // Ensure at least one padding char
}
headerStr += strings.Repeat(" ", paddingLen-1) + "\n"
// Write header length
if err := binary.Write(w, binary.LittleEndian, uint16(len(headerStr))); err != nil {
return fmt.Errorf("failed to write header length: %w", err)
}
// Write header
if _, err := w.Write([]byte(headerStr)); err != nil {
return fmt.Errorf("failed to write header: %w", err)
}
// Write data
if err := binary.Write(w, binary.LittleEndian, arr.Data); err != nil {
return fmt.Errorf("failed to write data: %w", err)
}
return nil
}
// parseHeader parses a NumPy header string into a header struct
func parseHeader(headerStr string) (*header, error) {
// Extract dictionary content from the header string
re := regexp.MustCompile(`{.*}`)
dictStr := re.FindString(headerStr)
if dictStr == "" {
return nil, fmt.Errorf("invalid header format")
}
// Extract shape
shapeRe := regexp.MustCompile(`'shape':\s*\(([\d,\s]*)\)`)
shapeMatch := shapeRe.FindStringSubmatch(dictStr)
if len(shapeMatch) < 2 {
return nil, fmt.Errorf("shape not found in header")
}
shapeStr := shapeMatch[1]
shapeParts := strings.Split(shapeStr, ",")
shape := make([]int, 0, len(shapeParts))
for _, part := range shapeParts {
part = strings.TrimSpace(part)
if part == "" {
continue
}
dim, err := strconv.Atoi(part)
if err != nil {
return nil, fmt.Errorf("invalid shape dimension: %s", part)
}
shape = append(shape, dim)
}
// Extract dtype
dtypeRe := regexp.MustCompile(`'descr':\s*'([^']*)'`)
dtypeMatch := dtypeRe.FindStringSubmatch(dictStr)
if len(dtypeMatch) < 2 {
return nil, fmt.Errorf("dtype not found in header")
}
dtypeStr := dtypeMatch[1]
// Extract endianness and map to Go data type
var dtype DType
if len(dtypeStr) >= 2 {
typeChar := dtypeStr[1:]
// Endianness doesn't matter for our Go representation
// We'll use the native Go types and handle endianness during read/write
switch typeChar {
case "b1":
dtype = Bool
case "i1":
dtype = Int8
case "i2":
dtype = Int16
case "i4":
dtype = Int32
case "i8":
dtype = Int64
case "u1":
dtype = Uint8
case "u2":
dtype = Uint16
case "u4":
dtype = Uint32
case "u8":
dtype = Uint64
case "f4":
dtype = Float32
case "f8":
dtype = Float64
default:
return nil, fmt.Errorf("unsupported dtype: %s", dtypeStr)
}
} else {
return nil, fmt.Errorf("invalid dtype format: %s", dtypeStr)
}
// Extract fortran_order (column-major vs row-major)
fortranRe := regexp.MustCompile(`'fortran_order':\s*(True|False)`)
fortranMatch := fortranRe.FindStringSubmatch(dictStr)
if len(fortranMatch) < 2 {
return nil, fmt.Errorf("fortran_order not found in header")
}
fortran := fortranMatch[1] == "True"
return &header{
Shape: shape,
DType: dtype,
Fortran: fortran,
}, nil
}