ConversionTable#
- class deeptrack.backend.units.ConversionTable(**conversions: dict[str, tuple[Unit, Unit]])#
Bases:
objectConvert a dictionary of values to desired units.
The conversions are specified in the constructor with a dictionary, which is saved as self.conversions. Each key in the dictionary corresponds to the name of a property. The corresponding value is a tuple of two Unit objects: 1) the default unit; 2) the desired unit.
To convert a dictionary of values to the desired units, the convert() method is called with the dictionary as an argument. The dictionary is converted to a dictionary of quantities in the desired units. If any value is not a quantity, it is assumed to be in the default unit. If a key is not in self.conversions, the corresponding value is left unchanged.
Parameters#
- **conversions: dict[str, tuple[Unit, Unit]]
A mapping from key names to unit pairs. Each pair defines the default and target units for that quantity.
Attributes#
- conversions: dict[str, tuple[Unit, Unit]]
A mapping from key names to unit pairs. Each pair defines the default and target units for that quantity.
Examples#
>>> from deeptrack.backend.units import ConversionTable
Load the unit registry: >>> from deeptrack import units_registry as u
Create a conversion table: >>> conversion_table = ConversionTable( … length=(u.meter, u.micrometer), … time=(u.second, u.millisecond) … )
Convert dictionary values: >>> conversion_table.convert(length=1.0, time=0.5) {‘length’: 1000000.0 <Unit(‘micrometer’)>, ‘time’: 500.0 <Unit(‘millisecond’)>}
Methods Summary
convert(**kwargs)Convert keyword arguments to their target units.
Methods Documentation
- convert(**kwargs: Any) dict[str, Quantity | Any]#
Convert keyword arguments to their target units.
Each keyword argument corresponding to a key in the conversion table is converted to its desired unit using Pint. Values are assumed to be in the default unit unless already specified as Pint Quantities.
Values can be scalars, NumPy arrays, lists, tuples, or torch tensors. All are interpreted as being in the default unit unless already a Pint Quantity.
Parameters#
- **kwargs: object
Keyword arguments containing values to be converted.
Returns#
- dict[str, Quantity or Any]
A dictionary with converted values. Keys not in the conversion table or with unsupported types are returned unchanged.