Keep the arrow extra available for existing requirements. Update wheel checks and install instructions, and verify that integer packing leaves PyArrow unloaded.
101 lines
3.6 KiB
Python
101 lines
3.6 KiB
Python
"""GDSII readers returning a one-row Arrow StructArray for the library.
|
|
|
|
The returned arrays own their buffers independently of the input.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from collections.abc import Sequence
|
|
import gzip
|
|
import os
|
|
from typing import Any, Callable
|
|
|
|
import numpy
|
|
import pyarrow
|
|
|
|
from ._native import PheonError, ffi, lib
|
|
|
|
|
|
def _export(call: Callable[[Any, Any], int]) -> pyarrow.StructArray:
|
|
array = ffi.new("struct ArrowArray *")
|
|
schema = ffi.new("struct ArrowSchema *")
|
|
try:
|
|
if call(array, schema) != 0:
|
|
error = lib.last_error_message()
|
|
message = (
|
|
ffi.string(error).decode("utf-8", errors="replace")
|
|
if error != ffi.NULL
|
|
else "GDSII read failed"
|
|
)
|
|
raise PheonError(message)
|
|
# PyArrow consumes the structures, setting their release callbacks to
|
|
# null. The finally block also covers failure during import.
|
|
return pyarrow.Array._import_from_c(
|
|
int(ffi.cast("uintptr_t", array)), int(ffi.cast("uintptr_t", schema))
|
|
)
|
|
finally:
|
|
if array.release != ffi.NULL:
|
|
array.release(array)
|
|
if schema.release != ffi.NULL:
|
|
schema.release(schema)
|
|
|
|
|
|
def read_path(path: str | os.PathLike[str]) -> pyarrow.StructArray:
|
|
"""Read a GDSII file, recognizing gzip by its magic bytes."""
|
|
with open(os.path.expanduser(path), "rb") as stream:
|
|
compressed = stream.read(2) == b"\x1f\x8b"
|
|
stream.seek(0)
|
|
if compressed:
|
|
with gzip.GzipFile(fileobj=stream) as source:
|
|
return read_bytes(source.read())
|
|
return read_bytes(stream.read())
|
|
|
|
|
|
def _read_buffer(
|
|
buffer: Any, call: Callable[[Any, int, Any, Any], int]
|
|
) -> pyarrow.StructArray:
|
|
with memoryview(buffer) as view:
|
|
if not view.c_contiguous:
|
|
raise ValueError("GDSII input must be a contiguous buffer")
|
|
with ffi.from_buffer("const uint8_t[]", view) as data:
|
|
return _export(lambda array, schema: call(data, view.nbytes, array, schema))
|
|
|
|
|
|
def read_bytes(buffer: Any) -> pyarrow.StructArray:
|
|
"""Read a complete GDSII library from a contiguous borrowed byte buffer."""
|
|
return _read_buffer(buffer, lib.read_bytes)
|
|
|
|
|
|
def scan_bytes(buffer: Any) -> pyarrow.StructArray:
|
|
"""Scan metadata, cell byte ranges and references without decoding geometry."""
|
|
return _read_buffer(buffer, lib.scan_bytes)
|
|
|
|
|
|
def read_cells_bytes(
|
|
buffer: Any, ranges: Sequence[Sequence[int]] | numpy.ndarray
|
|
) -> pyarrow.StructArray:
|
|
"""Read complete cells in the requested order from half-open byte ranges.
|
|
|
|
``ranges`` has shape (N, 2) and unsigned 64-bit integer offsets. Signed
|
|
integer inputs are accepted after checking that every offset is nonnegative.
|
|
An empty sequence requests metadata with no cells.
|
|
"""
|
|
values = numpy.asarray(ranges)
|
|
if values.shape == (0,):
|
|
values = numpy.empty((0, 2), dtype=numpy.uint64)
|
|
if values.ndim != 2 or values.shape[1] != 2:
|
|
raise ValueError("Cell ranges must have shape (N, 2)")
|
|
if values.dtype.kind not in "iu":
|
|
raise TypeError("Cell offsets must be integers")
|
|
if values.dtype.kind == "i" and numpy.any(values < 0):
|
|
raise ValueError("Cell offsets must be nonnegative")
|
|
values = numpy.require(
|
|
values, dtype=numpy.uint64, requirements=("C_CONTIGUOUS", "ALIGNED")
|
|
)
|
|
with ffi.from_buffer("const uint64_t[]", values) as offsets:
|
|
return _read_buffer(
|
|
buffer,
|
|
lambda data, size, array, schema: lib.read_cells_bytes(
|
|
data, size, offsets, len(values), array, schema
|
|
),
|
|
)
|