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",
	desciption="Ambient temperature",
	components=None
)

# Vector variable (e.g., 3D force)
force = vm.Var(
	key=FORCE,
	name="Force",
	units="N",
	desciption="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', 'desciption': 'Ambient temperature', 'components': None, 'component_axis': 0, 'data_type': 'object'}, 'force': {'key': 'force', 'name': 'Force', 'units': 'N', 'desciption': '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.0.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.0-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: varmeta-0.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 6154855f6b572301a77f9de80a085c682c3fad976b03db5e7bf346966b642398
MD5 4356b671d765ed3c42685d4eb24fe914
BLAKE2b-256 884093f7f21bdb3792bae9ceb68df743f1cd31bc570e307379c532d1b95f43cd

See more details on using hashes here.

File details

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

File metadata

  • Download URL: varmeta-0.1.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4f28244688247d9f36e62ee89188355590f17f254080809356927da3bc08dc55
MD5 21bfb62618d86f8ee4bb9486b3e4ba72
BLAKE2b-256 a9c4ce9ebcd0384e3d37c954e0fdfba746bb3f99a9c2c38828e54a212e7a0ac3

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