mt_metadata.common.units

This is a placeholder module. See github issue #30

In the mt_metadata packaage, the standard is that units are described by all lower case strings.

The dictionaries UNITS is keyed by these lower case strings.

Attributes

prefixes

base_units

derived_units

all_units

unit_name

UNITS_DF

Classes

Unit

!!! abstract "Usage Documentation"

Functions

find_separator(unit_string)

Find the first separator in a unit string.

parse_unit_string(unit_string)

Parse a unit string into a list of units and separators, including nested units and brackets.

get_unit_object(unit[, allow_none])

From the unit name or symbol return a Unit object.

get_unit_from_df(value[, allow_none])

Retrieve a row from the UNITS_DF DataFrame based on the unit's name or symbol.

Module Contents

mt_metadata.common.units.prefixes
mt_metadata.common.units.base_units
mt_metadata.common.units.derived_units
mt_metadata.common.units.all_units
mt_metadata.common.units.unit_name = ''
mt_metadata.common.units.UNITS_DF
class mt_metadata.common.units.Unit(/, **data)

Bases: pydantic.BaseModel

!!! abstract “Usage Documentation”

[Models](../concepts/models.md)

A base class for creating Pydantic models.

__class_vars__

The names of the class variables defined on the model.

__private_attributes__

Metadata about the private attributes of the model.

__signature__

The synthesized __init__ [Signature][inspect.Signature] of the model.

__pydantic_complete__

Whether model building is completed, or if there are still undefined fields.

__pydantic_core_schema__

The core schema of the model.

__pydantic_custom_init__

Whether the model has a custom __init__ function.

__pydantic_decorators__

Metadata containing the decorators defined on the model. This replaces Model.__validators__ and Model.__root_validators__ from Pydantic V1.

__pydantic_generic_metadata__

A dictionary containing metadata about generic Pydantic models. The origin and args items map to the [__origin__][genericalias.__origin__] and [__args__][genericalias.__args__] attributes of [generic aliases][types-genericalias], and the parameter item maps to the __parameter__ attribute of generic classes.

__pydantic_parent_namespace__

Parent namespace of the model, used for automatic rebuilding of models.

__pydantic_post_init__

The name of the post-init method for the model, if defined.

__pydantic_root_model__

Whether the model is a [RootModel][pydantic.root_model.RootModel].

__pydantic_serializer__

The pydantic-core SchemaSerializer used to dump instances of the model.

__pydantic_validator__

The pydantic-core SchemaValidator used to validate instances of the model.

__pydantic_fields__

A dictionary of field names and their corresponding [FieldInfo][pydantic.fields.FieldInfo] objects.

__pydantic_computed_fields__

A dictionary of computed field names and their corresponding [ComputedFieldInfo][pydantic.fields.ComputedFieldInfo] objects.

__pydantic_extra__

A dictionary containing extra values, if [extra][pydantic.config.ConfigDict.extra] is set to ‘allow’.

__pydantic_fields_set__

The names of fields explicitly set during instantiation.

__pydantic_private__

Values of private attributes set on the model instance.

model_config

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

name: Annotated[str, Field(default=None, description='Common name of the unit.')]
description: Annotated[str, Field(default=None, description='Description of the unit.')]
symbol: Annotated[str, Field(default=None, description='Symbol like representation of the unit', validation_alias=AliasChoices('symbol', 'abbrviation'))]
plot_label: Annotated[str, Field(default=None, description='Plot label of the unit.')]
combine(other, separator='/')

Combine two unit objects into a single string representation.

Parameters:
  • other (Unit) – The other unit object to combine with.

  • separator (str, optional) – The separator to use between the two units, by default “/”

Returns:

Combined string representation of the two units.

Return type:

str

to_dict()
from_dict(value)
mt_metadata.common.units.find_separator(unit_string)

Find the first separator in a unit string.

Parameters:

unit_string (str) – The unit string to search for separators.

Returns:

The first separator found in the unit string.

Return type:

str

mt_metadata.common.units.parse_unit_string(unit_string)

Parse a unit string into a list of units and separators, including nested units and brackets.

Parameters:

unit_string (str) – The unit string to parse (e.g., “mV nT/[km ohm]”).

Returns:

A list of dictionaries, each containing a unit name and its separator. Example: [{“name”: “mV”, “sep”: “ “}, {“name”: “nT”, “sep”: “/”}, {“name”: “[”, “sep”: None},

{“name”: “km”, “sep”: “ “}, {“name”: “ohm”, “sep”: None}, {“name”: “]”, “sep”: None}]

Return type:

list[dict]

mt_metadata.common.units.get_unit_object(unit, allow_none=True)

From the unit name or symbol return a Unit object. This function will search the unit name, symbol and plot_label for a match. If the unit is not found, a KeyError will be raised. If allow_none is True, None will be returned if the unit is not found.

Parameters:
  • unit (str) – name or symbol of the unit to search for.

  • allow_none (bool, optional) – If the unit isn’t found return an empty unit of unknons, by default True

Returns:

Unit object with the unit name, symbol, description and plot_label.

Return type:

Unit

Raises:

KeyError – If the unit is not found in the DataFrame.

mt_metadata.common.units.get_unit_from_df(value, allow_none=True)

Retrieve a row from the UNITS_DF DataFrame based on the unit’s name or symbol.

Parameters:

value (str) – The name or symbol of the unit to search for.

Returns:

A row from the UNITS_DF DataFrame corresponding to the given name or symbol.

Return type:

pd.Series

Raises:

KeyError – If the unit is not found in the DataFrame.