Skip to main content

Lightweight variable metadata management.

Project description

varmeta

varmeta is a lightweight Python package for managing variable metadata in scientific, engineering, and data analysis workflows. It automatically unpacks multi-component variables and generates pandas DataFrames with rich multi-level column headers.

Purpose

Scientific and engineering data often has variables with multiple components (e.g., x, y, and z components of a force vector). varmeta makes it easy to:

  • Define variables once with metadata (name, units, description, components)
  • Automatically unpack component variables (e.g., forceforce__x, force__y, force__z)
  • Generate pandas DataFrames with multi-index headers showing keys, names, and units
  • Serialize and deserialize variable metadata to/from JSON

Quick-start tutorial

1. Define variables with metadata

import varmeta as vm

TEMP = "temp"
FORCE = "force"

# Scalar variable
temperature = vm.Var(
    key=TEMP,
    name="Temperature",
    units="Celsius",
    description="Ambient temperature",
)

# Vector variable (e.g., 3D force)
force = vm.Var(
    key=FORCE,
    name="Force",
    units="N",
    description="Force vector",
    components=("x", "y", "z"),
    component_axis=1,
)

2. Create a VarSet collection

# Create a VarSet from a list of Var instances
varset = vm.VarSet([temperature, force])

# Dict-like access by key
print(varset[TEMP])  # Temperature [Celsius]
print(varset[FORCE])  # Force [N]

3. Unpack and pack data with components

Component variables can be unpacked (expanded) and packed (reconstructed):

data = {TEMP: 25.0, FORCE: [10.0, 20.0, 30.0]}

# Unpack: expand components
unpacked_varset, unpacked_data = varset.unpack(data)
print(unpacked_data)
# {'temp': 25.0, 'force__x': 10.0, 'force__y': 20.0, 'force__z': 30.0}

# Pack: reconstruct original structure
packed_data = varset.pack(unpacked_data)
print(packed_data)
# {'temp': 25.0, 'force': [10.0, 20.0, 30.0]}

Note: Component keys use double underscore __ separator (e.g., force__x) to minimize collision risk with regular variable names.

4. Create pandas DataFrames

It's easy to tabulate data into a DataFrame. Components are automatically unpacked, even from numpy arrays!

# Single dict → DataFrame
data = {TEMP: [30, 40], FORCE: [[200, 250, -30], [300, 350, -100]]}
df = varset.to_dataframe(data)
print(df)
# key          temp  force__x  force__y  force__z
# name  Temperature Force - x Force - y Force - z
# units     Celsius         N         N         N
# 0              30       200       250       -30
# 1              40       300       350      -100

5. Create DataFrames from records

You can also pass a list of data dictionaries (records):

records = [
    {TEMP: 30, FORCE: [200, 250, -30]},
    {TEMP: 40, FORCE: [300, 350, -100]},
]
df = varset.to_dataframe(records)
print(df)
# key          temp  force__x  force__y  force__z
# name  Temperature Force - x Force - y Force - z
# units     Celsius         N         N         N
# 0              30       200       250       -30
# 1              40       300       350      -100

Serialization

You can serialize VarSet metadata to JSON and reconstruct it later:

import json

# Serialize to JSON-compatible dict
var_data = varset.to_dict()
json_str = json.dumps(var_data)

# Deserialize from dict
varset_recreated = vm.VarSet.from_dict(json.loads(json_str))

# Verify equality
for key in varset:
    print(f"{key}: {'matches!' if varset[key] == varset_recreated[key] else 'differs!'}")
# Output:
# temp: matches!
# force: matches!

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

varmeta-0.2.1.tar.gz (35.0 kB view details)

Uploaded Source

Built Distribution

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

varmeta-0.2.1-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file varmeta-0.2.1.tar.gz.

File metadata

  • Download URL: varmeta-0.2.1.tar.gz
  • Upload date:
  • Size: 35.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.0 {"installer":{"name":"uv","version":"0.10.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for varmeta-0.2.1.tar.gz
Algorithm Hash digest
SHA256 a3d3b4eacd57b4e0257ba32bfeed658e26c7e7ae7d6bb6a0840ca46652980f86
MD5 db71f3aee7167f5387df092846eacd0c
BLAKE2b-256 6514da9c78e8a5ac19b18e7f3e947a6da07baa5abc0747c8b0eb00462d938d1c

See more details on using hashes here.

File details

Details for the file varmeta-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: varmeta-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 10.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.0 {"installer":{"name":"uv","version":"0.10.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for varmeta-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 05b3965b559c969d1a6c2d61f8b65b58c336ee456f752282f09a2f768c8c3bab
MD5 a14c41fb0c18ebd20c89c6cda3dbb23b
BLAKE2b-256 a184581a71560bad34b84e30c21da755dc5a628f59672adc7f876b6f31e77223

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