-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv.go
More file actions
192 lines (168 loc) · 4.46 KB
/
Copy pathcsv.go
File metadata and controls
192 lines (168 loc) · 4.46 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
package npy
import (
"encoding/csv"
"fmt"
"os"
"path/filepath"
)
// ToCsv exports an array to a CSV file
func ToCsv[T any](arr *Array[T], csvPath string) error {
// Create the file
f, err := os.Create(csvPath)
if err != nil {
return fmt.Errorf("failed to create CSV file: %w", err)
}
defer f.Close()
// Create a CSV writer
writer := csv.NewWriter(f)
defer writer.Flush()
// Handle the data based on dimensions
dimensions := len(arr.Shape)
if dimensions == 0 || (dimensions == 1 && arr.Shape[0] == 0) {
// Empty array
return nil
} else if dimensions == 1 {
// 1D array (vector) - write as a single row
record := make([]string, len(arr.Data))
for i, val := range arr.Data {
record[i] = fmt.Sprintf("%v", val)
}
if err := writer.Write(record); err != nil {
return fmt.Errorf("failed to write CSV row: %w", err)
}
} else if dimensions == 2 {
// 2D array (matrix)
rows := arr.Shape[0]
cols := arr.Shape[1]
for r := 0; r < rows; r++ {
record := make([]string, cols)
for c := 0; c < cols; c++ {
// Calculate index based on ordering
var idx int
if arr.Fortran {
// Column-major (Fortran) order
idx = c*rows + r
} else {
// Row-major (C) order
idx = r*cols + c
}
record[c] = fmt.Sprintf("%v", arr.Data[idx])
}
if err := writer.Write(record); err != nil {
return fmt.Errorf("failed to write CSV row: %w", err)
}
}
} else {
// Higher dimensions
return fmt.Errorf("arrays with more than 2 dimensions are not supported for Csv export")
}
return nil
}
// NPZToCsvDir exports all arrays in an NPZ file to CSV files in the specified directory
func NPZToCsvDir(npzPath string, outputDir string) error {
// Read the NPZ file
npz, err := ReadNPZFile(npzPath)
if err != nil {
return fmt.Errorf("failed to read NPZ file: %w", err)
}
// Make sure the output directory exists
if err := os.MkdirAll(outputDir, 0755); err != nil {
return fmt.Errorf("failed to create output directory: %w", err)
}
// Export each array based on its type
for _, key := range Keys(npz) {
outPath := filepath.Join(outputDir, key+".csv")
// Here we need to try each type due to Go's type system limitations
exported := false
if arr, ok := Get[bool](npz, key); ok {
if err := ToCsv(arr, outPath); err != nil {
return fmt.Errorf("failed to export %s: %w", key, err)
}
exported = true
}
if !exported {
if arr, ok := Get[int8](npz, key); ok {
if err := ToCsv(arr, outPath); err != nil {
return fmt.Errorf("failed to export %s: %w", key, err)
}
exported = true
}
}
if !exported {
if arr, ok := Get[int16](npz, key); ok {
if err := ToCsv(arr, outPath); err != nil {
return fmt.Errorf("failed to export %s: %w", key, err)
}
exported = true
}
}
if !exported {
if arr, ok := Get[int32](npz, key); ok {
if err := ToCsv(arr, outPath); err != nil {
return fmt.Errorf("failed to export %s: %w", key, err)
}
exported = true
}
}
if !exported {
if arr, ok := Get[int64](npz, key); ok {
if err := ToCsv(arr, outPath); err != nil {
return fmt.Errorf("failed to export %s: %w", key, err)
}
exported = true
}
}
if !exported {
if arr, ok := Get[uint8](npz, key); ok {
if err := ToCsv(arr, outPath); err != nil {
return fmt.Errorf("failed to export %s: %w", key, err)
}
exported = true
}
}
if !exported {
if arr, ok := Get[uint16](npz, key); ok {
if err := ToCsv(arr, outPath); err != nil {
return fmt.Errorf("failed to export %s: %w", key, err)
}
exported = true
}
}
if !exported {
if arr, ok := Get[uint32](npz, key); ok {
if err := ToCsv(arr, outPath); err != nil {
return fmt.Errorf("failed to export %s: %w", key, err)
}
exported = true
}
}
if !exported {
if arr, ok := Get[uint64](npz, key); ok {
if err := ToCsv(arr, outPath); err != nil {
return fmt.Errorf("failed to export %s: %w", key, err)
}
exported = true
}
}
if !exported {
if arr, ok := Get[float32](npz, key); ok {
if err := ToCsv(arr, outPath); err != nil {
return fmt.Errorf("failed to export %s: %w", key, err)
}
exported = true
}
}
if !exported {
if arr, ok := Get[float64](npz, key); ok {
if err := ToCsv(arr, outPath); err != nil {
return fmt.Errorf("failed to export %s: %w", key, err)
}
exported = true
}
}
if !exported {
return fmt.Errorf("unsupported data type for array %s", key)
}
}
return nil
}