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

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for varmeta-0.1.3.tar.gz
Algorithm Hash digest
SHA256 2c07b28fd0aac1bf3291ae196af7280ba4afdf5dd6ff7397a3890f228ef9b88d
MD5 a1e85ffdcb0037d73fce1a9d2f10f2eb
BLAKE2b-256 d9750ffa7556cc40459d0e7f55e20c7ddc2d7e83b3a5278f1d943708ba1c19be

See more details on using hashes here.

File details

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

File metadata

  • Download URL: varmeta-0.1.3-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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 423f987839a740d4a0af547b084f5c67686c6a23a8484dfa4530a7309f2df6db
MD5 46db1108b14b7e61953f1d9ae4d38e60
BLAKE2b-256 774c668c1ff1cbd599f50996287733594a53f92a590663ec59a906f54356f5bc

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