Skip to content

npy #

vtl.npy

Typed NumPy .npy array reader and writer. Its dtype descriptor policy lives in descriptor.v, next to the parser that uses it — there is no separate top-level dtype namespace. Tensor element types remain the root vtl.DType model; this module only classifies the descr strings found in .npy headers; see #747. VTL tensors stay homogeneous and numeric; heterogeneous table/record data belongs in a companion package.

Descriptor policy

parse_descriptor_policy recognizes these families so diagnostics can name them instead of reporting a generic parse failure:

  • Supported: boolean, integer, unsigned, float, complex, with widths b1, i1/i2/i4/i8, u1/u2/u4/u8, f4/f8, and c16.
  • Excluded: S fixed-width bytes, U fixed-width unicode strings, M datetime and m timedelta (width 8 with a unit such as ns/us/ms), O object, and V structured/void records.

Descriptor.ensure_supported() rejects excluded families with the family name and a pointer to the companion-table/array boundary; no value is silently coerced into a number. Datetime/timedelta descriptors must end at the closing unit bracket, and | byte order is only valid for single-byte numeric widths. The .npy reader applies the policy to every non-numeric descriptor and to list-form structured descriptors before reading values.

descriptor := npy.parse_descriptor_policy('<U3')!
descriptor.ensure_supported() or {
    println(err.msg()) // names the unicode string family and the boundary
}

Run the focused tests with:

v test npy/descriptor_test.v
v test npy/npy_test.v

Construction, indexing, comparison, selection, and serialization of supported non-numeric types remain out of scope and tracked in #747.

fn excluded_families #

fn excluded_families() []DescriptorFamily

excluded_families lists families that are intentionally out of scope.

fn open_mapped #

fn open_mapped[T](path string) !&MappedNpy[T]

open_mapped maps a supported .npy file without copying or decoding its payload. Call close when finished; the mapped file must not be modified or truncated while it is open.

fn parse_descriptor_policy #

fn parse_descriptor_policy(text string) !Descriptor

parse_descriptor_policy decodes a NumPy dtype descriptor string.

fn read #

fn read[T](path string) !&vtl.Tensor[T]

read reads a NumPy .npy v1.0, v2.0, or v3.0 array into T. The file dtype must have the same kind and width as T. Both byte orders and Fortran-order files are converted to VTL's row-major logical tensor layout.

fn read_bytes #

fn read_bytes[T](bytes []u8) !&vtl.Tensor[T]

read_bytes decodes a NumPy .npy v1.0, v2.0, or v3.0 payload into T. The payload's dtype must have the same kind and width as T.

fn supported_families #

fn supported_families() []DescriptorFamily

supported_families lists the families VTL tensors accept.

fn to_bytes #

fn to_bytes[T](tensor &vtl.Tensor[T]) ![]u8

to_bytes encodes a CPU tensor as an uncompressed NumPy .npy v1.0 payload.

fn write #

fn write[T](path string, tensor &vtl.Tensor[T]) !

write writes a CPU tensor as an uncompressed NumPy .npy v1.0 file. Numeric primitive types and row-major logical tensor order are supported.

fn DescriptorFamily.from #

fn DescriptorFamily.from[W](input W) !DescriptorFamily

from returns the DescriptorFamily value of an integer or of a field name, or an error if there is none.

fn (MappedNpy[T]) shape #

fn (mapped &MappedNpy[T]) shape() []int

shape returns a copy of the mapped array's shape.

fn (MappedNpy[T]) size #

fn (mapped &MappedNpy[T]) size() int

size returns the total number of logical elements.

fn (MappedNpy[T]) is_closed #

fn (mapped &MappedNpy[T]) is_closed() bool

is_closed reports whether close has released the mapped region.

fn (MappedNpy[T]) read_rows #

fn (mapped &MappedNpy[T]) read_rows(start int, end int) !&vtl.Tensor[T]

read_rows decodes rows [start, end) along the leading axis into owned storage.

fn (MappedNpy[T]) close #

fn (mut mapped MappedNpy[T]) close()

close releases the mapping. Decoded tensors returned by read_rows remain valid. Closing more than once is safe.

enum DescriptorFamily #

enum DescriptorFamily {
	boolean
	integer
	unsigned
	float
	complex
	bytes
	unicode
	datetime
	timedelta
	object
	void
	unknown
}

DescriptorFamily is the NumPy dtype family of a descriptor.

struct Descriptor #

struct Descriptor {
pub:
	raw        string
	byte_order u8
	kind       u8
	width      int
	unit       string
	family     DescriptorFamily
}

Descriptor is a parsed NumPy dtype descriptor such as <i4, |S5, or <M8[ns]. width is measured in bytes for numeric/bytes/void dtypes and in code points for unicode, matching NumPy's descriptor syntax.

fn (Descriptor) is_numeric #

fn (d &Descriptor) is_numeric() bool

is_numeric reports whether the descriptor belongs to a numeric family.

fn (Descriptor) label #

fn (d &Descriptor) label() string

label returns a human-readable family name for diagnostics.

fn (Descriptor) ensure_supported #

fn (d &Descriptor) ensure_supported() !

ensure_supported enforces the VTL boundary: tensors are homogeneous and numeric. Non-numeric families are rejected with an explicit reason rather than being coerced into numbers.

struct MappedNpy #

@[heap]
struct MappedNpy[T] {
mut:
	mapping       &os.Mmap = unsafe { nil }
	path          string
	mapped_length u64
	info          NpyFileInfo
	closed        bool
}

MappedNpy provides read-only, row-wise access to a mapped .npy file. Each returned tensor owns its decoded values and remains valid after close.