Skip to main content

Lightweight variable metadata management.

Project description

varmeta

varmeta is a small and lightweight Python package for managing and using variable metadata in scientific, engineering, and data analysis workflows in outputs. It's designed with type safety in mind for use with modern IDEs like VSCode + pylance.

Purpose

We often have variables with several components (e.g. x, y, and z components). We want to use them as lists or arrays, but tabulate them nicely for outputs. varmeta solves this problem in a simple way.

  • Set up a dictionary of Var instances, with string keys.
  • Set up matching dictionaries of data.
  • Create pandas dataframes with nice multi-index headings containing not just the variable keys, but also their names and units, OR:
  • Split variables into components automatically for other uses.

Quick-start tutorial

1. Define variables with metadata

Let's first set up some imports, constants, and Var instances.

import varmeta as vm

TEMP = "temperature"
FORCE = "force"

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

# 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. Use literal keys for Var and data dictionaries

Use shared literal string keys for dictionaries of Var instances and data dictionaries.

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

var_dct = vm.VarDict({
	TEMP: temperature,
	FORCE: force
})

3. Unpack data with components

vars, vals = vm.unpack(var_dct, data_dct)
print(vars)
# {'temp': Temperature [Celsius], 'force_x': Force - x [N], 'force_y': Force - y [N], 'force_z': Force - z [N]}
print(vals)
# {'temp': 25.0, 'force_x': 10.0, 'force_y': 20.0, 'force_z': 30.0}

5. Tabulate dict-based data into pandas DataFrames

It's easy to tabulate data into a DataFrame. Data is automatically unpacked, even numpy arrays!

data_dct = {FORCE: [[200, 250, -30], [300, 350, -100]], TEMP: [30, 40]}
df = vm.dict_to_df(var_dct, data_dct)
print(df)
# key     force_x   force_y   force_z        temp
# name  Force - x Force - y Force - z Temperature
# units         N         N         N     Celsius
# 0           200       250       -30          30
# 1           300       350      -100          40
data_dct = {TEMP: [30, 40], FORCE: [[200, 250, -30], [300, 350, -100]]}
df = vm.dict_to_df(var_dct, data_dct)
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

6. Tabulate records easily

Say we'd had many records instead. We can make tables with lists of data in dictionaries, unpacking each automatically, like this:

data_dict_lst = [
	{FORCE: [200, 250, -30], TEMP: 30},
	{FORCE: [300, 350, -100], TEMP: 40},
]
df = vm.records_to_df(var_dct, data_dict_lst)
print(df)
# key     force_x   force_y   force_z        temp
# name  Force - x Force - y Force - z Temperature
# units         N         N         N     Celsius
# 0           200       250       -30          30
# 1           300       350      -100          40

Serialisation

We can convert to dictionaries like this:

var_data = vm.vars_to_dict(var_dct)
print(var_data)
# Output
# {'temp': {'key': 'temp', 'name': 'Temperature', 'units': 'Celsius', 'description': 'Ambient temperature', 'components': None, 'component_axis': 0, 'data_type': 'object'}, 'force': {'key': 'force', 'name': 'Force', 'units': 'N', 'description': 'Force vector', 'components': ('x', 'y', 'z'), 'component_axis': 1, 'data_type': 'object'}}

Then we can convert back to Var instances like this:

var_dct_recreated = vm.vars_from_dict(var_data)
for k, v in var_dct_recreated:
	print(f"k: match is {v == var_dct[k]}")
# Output
# temp: match is True
# force: match is True

The data (var_data) can be easily saved and read from JSON using the standard json library.

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.1.1.tar.gz (19.9 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.1.1-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: varmeta-0.1.1.tar.gz
  • Upload date:
  • Size: 19.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.16

File hashes

Hashes for varmeta-0.1.1.tar.gz
Algorithm Hash digest
SHA256 b90c5d84a06ffa288f7f909a3868ae26a2e2b70c94cfce664283ceb8cb2be610
MD5 0cb048d0a3ce2b1a6047b0cccd9cd7b7
BLAKE2b-256 7d14c31a539eebf7db1693e974099448a70273859f7222bcd9a371ac9f622fc1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: varmeta-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.16

File hashes

Hashes for varmeta-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6eed654ab98e2564b8f0d99e3697bf6be6ee95f651fdff4f36c35face0b3c7ef
MD5 5bf23874d62f6e470f52479e0062fb85
BLAKE2b-256 63bf51dbec227d81cfdeef3ecb7160632c3bf1cc5c89676b4476e6ac15a10dfd

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