Skip to main content

A thin wrapper over np.ndarray to provide an intuitive interface for attaching pydantic models as metadata to arrays.

Project description

dantArrays: Enhanced NumPy Arrays with Rich, Computed Metadata


Overview

dantArrays extends standard NumPy arrays by embedding rich, computed metadata directly into array slices, rows, or columns. This integration allows you to keep data tightly coupled with its descriptive context, computed metrics, or dynamic annotations—without losing NumPy's efficiency and versatility.


Key Features

  • Rich Metadata Integration: Attach structured metadata (using Pydantic models) to slices of NumPy arrays.
  • Automatic Computations: Define metadata fields computed directly from array data.
  • Data Safety: Safe mechanisms to access and modify underlying array data without corrupting computed metadata.
  • Flexibility: Easily update array slices or metadata, automatically recalculating dependent computed fields.
  • Usability: Clear, Pythonic API for both simple and complex data manipulation tasks.

Installation

pip install dantarrays

Quickstart Example

Here's how easily you can add metadata to your arrays:

import numpy as np
from pydantic import BaseModel
from dantarrays import DantArray, mean_value_field

class StockMetadata(BaseModel):
    symbol: str
    mean_price: float = mean_value_field()

price_data = np.array([
    [100, 101, 102],
    [50, 49, 51],
])

stocks = DantArray(data=price_data, metadata_class=StockMetadata, major_axis=0)

stocks.update_metadata(0, symbol="AAPL")
stocks.update_metadata(1, symbol="MSFT")

print(stocks.meta(0).mean_price)  # Automatically computed mean price
# Output: 101.0

Usage

Defining Metadata Models

Use Pydantic models to define structured metadata:

from pydantic import BaseModel
from dantarrays import max_value_field, mean_value_field

class ExperimentMetadata(BaseModel):
    experiment_id: str
    max_measurement: float = max_value_field()
    avg_measurement: float = mean_value_field()

Creating a DantArray

import numpy as np
from dantarrays import DantArray

data = np.random.rand(5, 10)
experiments = DantArray(data=data, metadata_class=ExperimentMetadata, major_axis=0)

Updating Metadata

experiments.update_metadata(0, experiment_id="EXP001")

Accessing Metadata

metadata = experiments.get_metadata(0)
print(metadata.max_measurement)

Using the .meta() API for Convenient Field Access

The .meta() method provides ergonomic, attribute-style access to metadata fields:

experiments.meta(0).experiment_id = "EXP002"
print(experiments.meta(0).experiment_id)
# Output: EXP002

You can also update multiple fields conveniently:

experiments.meta(0).update(experiment_id="EXP003", avg_measurement=0.75)

Editing Data Safely

with experiments.edit_data() as data:
    data[0, :] = np.random.rand(10)

Immutable Updates

updated_experiments = experiments.with_updated_slice(0, np.zeros(10))

Advanced Examples

Batch Metadata Creation

indices = [0, 1, 2]
experiments.batch_create(indices, experiment_id="default")

Computed Fields Based on Data

Create a metadata field dynamically computed from the data slice:

from dantarrays import data_based_field

class ImageMetadata(BaseModel):
    brightness: float = data_based_field(lambda slice: float(np.mean(slice)))

Accessing Full Metadata

for idx, meta in experiments.get_existing_metadata():
    print(f"Experiment {meta.experiment_id} avg: {meta.avg_measurement}")

Data Validation and Safety

Direct access to the internal data array triggers warnings, ensuring safe data manipulation:

experiments._data_unsafe  # Warns about unsafe access

Contextual Computations

Computed fields have access to context about their array slice:

from dantarrays import ComputedFieldContext

class CustomComputation:
    def __call__(self, ctx: ComputedFieldContext):
        return ctx.size * np.mean(ctx.get_slice())

API Reference

DantArray

  • .update_metadata(idx, **kwargs): Update metadata fields.
  • .get_metadata(idx): Retrieve metadata.
  • .meta(idx): Convenient, attribute-style metadata accessor.
  • .batch_create(indices, **kwargs): Batch create metadata entries.
  • .with_updated_data(new_data): Immutable data update.
  • .apply_with_metadata(func): Apply function to array slices and metadata.
  • .refresh_computed_fields(): Refresh computed fields after data changes.

Use Cases

  • Financial time-series analysis
  • Image datasets with annotations
  • Scientific experiment tracking
  • Feature metadata management in machine learning

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dantarrays-0.0.4.tar.gz (36.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dantarrays-0.0.4-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file dantarrays-0.0.4.tar.gz.

File metadata

  • Download URL: dantarrays-0.0.4.tar.gz
  • Upload date:
  • Size: 36.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.26

File hashes

Hashes for dantarrays-0.0.4.tar.gz
Algorithm Hash digest
SHA256 401d895236a31de14b6643c8541e0972090fb8931dfa7abd762d7d24da697985
MD5 017ad9926fcc2e562aa7a6d4ff6af3d2
BLAKE2b-256 b7de733b275ff0c6b95c053de8e860d9d5b4635b3c15e23831b94f70834759d7

See more details on using hashes here.

File details

Details for the file dantarrays-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: dantarrays-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 11.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.26

File hashes

Hashes for dantarrays-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 f18220e01cad46c926ce80cff1a8470e5d10d8c2b8ddcba9fb46c5d34677b5d7
MD5 3b6256edd47004d0b0e98590117d0c18
BLAKE2b-256 fcfe3cc73d59c946071ec11d564d0d5df9c218239a9229a6ff93c158a8160d65

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page