|
| 1 | +// Copyright 2023 The npyio Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package npy |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "strings" |
| 10 | + |
| 11 | + py "github.com/nlpodyssey/gopickle/types" |
| 12 | +) |
| 13 | + |
| 14 | +// Array is a multidimensional, homogeneous array of fixed-size items. |
| 15 | +type Array struct { |
| 16 | + descr ArrayDescr |
| 17 | + shape []int |
| 18 | + strides []int |
| 19 | + fortran bool |
| 20 | + |
| 21 | + data any |
| 22 | +} |
| 23 | + |
| 24 | +var ( |
| 25 | + _ py.PyNewable = (*Array)(nil) |
| 26 | + _ py.PyStateSettable = (*Array)(nil) |
| 27 | +) |
| 28 | + |
| 29 | +func (*Array) PyNew(args ...any) (any, error) { |
| 30 | + var ( |
| 31 | + subtype = args[0] |
| 32 | + descr = args[1].(*ArrayDescr) |
| 33 | + shape = args[2].([]int) |
| 34 | + strides = args[3].([]int) |
| 35 | + data = args[4].([]byte) |
| 36 | + flags = args[5].(int) |
| 37 | + ) |
| 38 | + |
| 39 | + return newArray(subtype, *descr, shape, strides, data, flags) |
| 40 | +} |
| 41 | + |
| 42 | +func newArray(subtype any, descr ArrayDescr, shape, strides []int, data []byte, flags int) (*Array, error) { |
| 43 | + switch subtype := subtype.(type) { |
| 44 | + case *Array: |
| 45 | + // ok. |
| 46 | + default: |
| 47 | + return nil, fmt.Errorf("subtyping ndarray with %T is not (yet?) supported", subtype) |
| 48 | + } |
| 49 | + |
| 50 | + arr := &Array{ |
| 51 | + descr: descr, |
| 52 | + shape: shape, |
| 53 | + strides: strides, |
| 54 | + data: data, |
| 55 | + } |
| 56 | + return arr, nil |
| 57 | +} |
| 58 | + |
| 59 | +func (arr *Array) PySetState(arg any) error { |
| 60 | + tuple, ok := arg.(*py.Tuple) |
| 61 | + if !ok { |
| 62 | + return fmt.Errorf("invalid argument type %T", arg) |
| 63 | + } |
| 64 | + |
| 65 | + var ( |
| 66 | + vers = 0 |
| 67 | + shape py.Tuple |
| 68 | + raw any |
| 69 | + ) |
| 70 | + switch tuple.Len() { |
| 71 | + case 5: |
| 72 | + err := parseTuple(tuple, &vers, &shape, &arr.descr, &arr.fortran, nil) |
| 73 | + if err != nil { |
| 74 | + return fmt.Errorf("could not parse ndarray.__setstate__ tuple: %w", err) |
| 75 | + } |
| 76 | + raw = tuple.Get(4) |
| 77 | + case 4: |
| 78 | + err := parseTuple(tuple, &shape, &arr.descr, &arr.fortran, nil) |
| 79 | + if err != nil { |
| 80 | + return fmt.Errorf("could not parse ndarray.__setstate__ tuple: %w", err) |
| 81 | + } |
| 82 | + raw = tuple.Get(3) |
| 83 | + default: |
| 84 | + return fmt.Errorf("invalid length (%d) for ndarray.__setstate__ tuple", tuple.Len()) |
| 85 | + } |
| 86 | + |
| 87 | + arr.shape = nil |
| 88 | + for i := range shape { |
| 89 | + v, ok := shape.Get(i).(int) |
| 90 | + if !ok { |
| 91 | + return fmt.Errorf("invalid shape[%d]: got=%T, want=int", i, shape.Get(i)) |
| 92 | + } |
| 93 | + arr.shape = append(arr.shape, v) |
| 94 | + } |
| 95 | + |
| 96 | + err := arr.setupStrides() |
| 97 | + if err != nil { |
| 98 | + return fmt.Errorf("ndarray.__setstate__ could not infer strides: %w", err) |
| 99 | + } |
| 100 | + |
| 101 | + switch raw := raw.(type) { |
| 102 | + case *py.List: |
| 103 | + arr.data = raw |
| 104 | + |
| 105 | + case []byte: |
| 106 | + data, err := arr.descr.unmarshal(raw, arr.shape) |
| 107 | + if err != nil { |
| 108 | + return fmt.Errorf("ndarray.__setstate__ could not unmarshal raw data: %w", err) |
| 109 | + } |
| 110 | + arr.data = data |
| 111 | + } |
| 112 | + |
| 113 | + return nil |
| 114 | +} |
| 115 | + |
| 116 | +func (arr *Array) setupStrides() error { |
| 117 | + // TODO(sbinet): complete implementation. |
| 118 | + // see: _array_fill_strides in numpy/_core/multiarray/ctors.c |
| 119 | + |
| 120 | + if arr.shape == nil { |
| 121 | + arr.strides = nil |
| 122 | + return nil |
| 123 | + } |
| 124 | + |
| 125 | + strides := make([]int, len(arr.shape)) |
| 126 | + // FIXME(sbinet): handle non-contiguous arrays |
| 127 | + // FIXME(sbinet): handle FORTRAN arrays |
| 128 | + |
| 129 | + var ( |
| 130 | + // notCFContig bool |
| 131 | + noDim bool // a dimension != 1 was found |
| 132 | + ) |
| 133 | + |
| 134 | + // check if array is both FORTRAN- and C-contiguous |
| 135 | + for _, dim := range arr.shape { |
| 136 | + if dim != 1 { |
| 137 | + if noDim { |
| 138 | + // notCFContig = true |
| 139 | + break |
| 140 | + } |
| 141 | + noDim = true |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + itemsize := arr.descr.itemsize() |
| 146 | + switch { |
| 147 | + case arr.fortran: |
| 148 | + for i, dim := range arr.shape { |
| 149 | + strides[i] = itemsize |
| 150 | + switch { |
| 151 | + case dim != 0: |
| 152 | + itemsize *= dim |
| 153 | + default: |
| 154 | + // notCFContig = false |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + default: |
| 159 | + for i := len(arr.shape) - 1; i >= 0; i-- { |
| 160 | + dim := arr.shape[i] |
| 161 | + strides[i] = itemsize |
| 162 | + switch { |
| 163 | + case dim != 0: |
| 164 | + itemsize *= dim |
| 165 | + default: |
| 166 | + // notCFContig = false |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + arr.strides = strides |
| 172 | + return nil |
| 173 | +} |
| 174 | + |
| 175 | +// Descr returns the array's data type descriptor. |
| 176 | +func (arr Array) Descr() ArrayDescr { |
| 177 | + return arr.descr |
| 178 | +} |
| 179 | + |
| 180 | +// Shape returns the array's shape. |
| 181 | +func (arr Array) Shape() []int { |
| 182 | + return arr.shape |
| 183 | +} |
| 184 | + |
| 185 | +// Strides returns the array's strides in bytes. |
| 186 | +func (arr Array) Strides() []int { |
| 187 | + return arr.strides |
| 188 | +} |
| 189 | + |
| 190 | +// Fortran returns whether the array's data is stored in FORTRAN-order |
| 191 | +// (ie: column-major) instead of C-order (ie: row-major.) |
| 192 | +func (arr Array) Fortran() bool { |
| 193 | + return arr.fortran |
| 194 | +} |
| 195 | + |
| 196 | +// Data returns the array's underlying data. |
| 197 | +func (arr Array) Data() any { |
| 198 | + return arr.data |
| 199 | +} |
| 200 | + |
| 201 | +func (arr Array) String() string { |
| 202 | + o := new(strings.Builder) |
| 203 | + fmt.Fprintf(o, "Array{descr: %v, ", arr.descr) |
| 204 | + switch arr.shape { |
| 205 | + case nil: |
| 206 | + fmt.Fprintf(o, "shape: nil, ") |
| 207 | + default: |
| 208 | + fmt.Fprintf(o, "shape: %v, ", arr.shape) |
| 209 | + } |
| 210 | + switch arr.strides { |
| 211 | + case nil: |
| 212 | + fmt.Fprintf(o, "strides: nil, ") |
| 213 | + default: |
| 214 | + fmt.Fprintf(o, "strides: %v, ", arr.strides) |
| 215 | + } |
| 216 | + fmt.Fprintf(o, "fortran: %v, data: %+v}", |
| 217 | + arr.fortran, |
| 218 | + arr.data, |
| 219 | + ) |
| 220 | + return o.String() |
| 221 | +} |
0 commit comments