import logging
from typing import Dict, Hashable, List, Mapping, Optional, Set, Union
from ts_lib_units.tetra_unit import DeprecatedTetraUnit, TetraUnit
from ts_lib_units.unit_types import UnitType
[docs]
class Unit:
"""TetraScience standard units"""
# When adding a units:
# Add a new class attribute with the type `TetraUnit`, including:
# - `ts_format`: The QUDT (or QUDT-like) unit string for the unit
# - `pint_format`: The `pint` unit string for the unit.
# You can reference other units in the library, or the
# [default pint registry](https://github.com/hgrecco/pint/blob/master/pint/default_en.txt)
# to build this string.
# - `qudt_link`: The QUDT link for the unit`, if available in:
# https://purl.allotrope.org/voc/qudt/REC/2022/09/qudt-merged.ttl.
# For the `ts_format` string:
# - If the unit is in QUDT or QUDT-ext, use the name in the URI.
# - Otherwise: Use camel case, use `Per` and do not capitalize after prefixes such as
# `Milli` or `Micro`
ABSORBANCE_UNIT = TetraUnit(
ts_format="AbsorbanceUnit",
pint_format=None,
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#AbsorbanceUnit",
unit_type=UnitType.UNITLESS,
aliases=["AU"],
)
AMPERE = TetraUnit(
ts_format="Ampere",
pint_format="ampere",
qudt_uri="http://qudt.org/vocab/unit/A",
unit_type=UnitType.SI,
aliases=["A", "amp"],
)
ANGSTROM = TetraUnit(
ts_format="Angstrom",
pint_format="angstrom",
qudt_uri="http://qudt.org/vocab/unit/ANGSTROM",
unit_type=UnitType.LENGTH,
aliases=["Å"],
)
ARBITRARY_UNIT = TetraUnit(
ts_format="ArbitraryUnit",
pint_format=None,
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#ArbitraryUnit",
unit_type=UnitType.ARBITRARY_UNIT,
aliases=["AU", "arb'U"],
)
BAR = TetraUnit(
ts_format="Bar",
pint_format="bar",
qudt_uri="http://qudt.org/vocab/unit/BAR",
unit_type=UnitType.ARBITRARY_UNIT,
aliases=["bar"],
)
BYTE = TetraUnit(
ts_format="Byte",
pint_format="byte",
qudt_uri="http://qudt.org/vocab/unit/BYTE",
unit_type=UnitType.ARBITRARY_UNIT,
aliases=["B"],
)
CENTIMETER = TetraUnit(
ts_format="Centimeter",
pint_format="centimeter",
qudt_uri="http://qudt.org/vocab/unit/CentiM",
unit_type=UnitType.LENGTH,
aliases=["cm"],
)
CENTIMETER_PER_HOUR = TetraUnit(
ts_format="CentimeterPerHour",
pint_format="centimeter / hour",
qudt_uri=None,
unit_type=UnitType.ARBITRARY_UNIT,
aliases=["cm/hr"],
)
CENTIPOISE = TetraUnit(
ts_format="Centipoise",
pint_format="centipoise",
qudt_uri="http://qudt.org/vocab/unit/CentiPOISE",
unit_type=UnitType.ARBITRARY_UNIT,
aliases=["cP"],
)
COUNT = TetraUnit(
ts_format="Count",
pint_format="count",
# Count is a quantity in QUDT but not a unit, so there is no QUDT unit URI
qudt_uri=None,
unit_type=UnitType.ARBITRARY_UNIT,
aliases=[],
)
COUNT_PER_LITER = TetraUnit(
ts_format="CountPerLiter",
pint_format="count / liter",
qudt_uri=None,
unit_type=UnitType.ARBITRARY_UNIT,
aliases=["Counts/L", "Counts/l"],
)
COUNT_PER_MICROLITER = TetraUnit(
ts_format="CountPerMicroliter",
pint_format="count / microliter",
qudt_uri=None,
unit_type=UnitType.ARBITRARY_UNIT,
aliases=["Counts/µL", "Counts/µl"],
)
COUNT_PER_MILLILITER = TetraUnit(
ts_format="CountPerMilliliter",
pint_format="count / milliliter",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#CountsPerMilliliter",
unit_type=UnitType.ARBITRARY_UNIT,
aliases=["Counts/mL", "Counts/ml"],
)
COUNT_PER_NANOLITER = TetraUnit(
ts_format="CountPerNanoliter",
pint_format="count / nanoliter",
qudt_uri=None,
unit_type=UnitType.ARBITRARY_UNIT,
aliases=["Counts/nL", "Counts/nl"],
)
CUBIC_CENTIMETER_PER_MOLE = TetraUnit(
ts_format="CubicCentimeterPerMole",
pint_format="(centimeter ** 3) / mole",
qudt_uri=None,
unit_type=UnitType.SI,
aliases=[],
)
CYCLE_NUMBER = TetraUnit(
ts_format="CycleNumber",
pint_format=None,
qudt_uri=None,
unit_type=UnitType.ARBITRARY_UNIT,
aliases=[],
)
DALTON = TetraUnit(
ts_format="Dalton",
pint_format="dalton",
qudt_uri="http://qudt.org/vocab/unit/Da",
unit_type=UnitType.MASS,
aliases=["Da"],
)
DAY = TetraUnit(
ts_format="Day",
pint_format="day",
qudt_uri="http://qudt.org/vocab/unit/DAY",
unit_type=UnitType.ARBITRARY_UNIT,
aliases=["d"],
)
DECIBEL = TetraUnit(
ts_format="Decibel",
pint_format="decibel",
qudt_uri="http://qudt.org/vocab/unit/DeciB",
unit_type=UnitType.ARBITRARY_UNIT,
aliases=["dB"],
)
DEGREE_ANGLE = TetraUnit(
ts_format="DegreeAngle",
pint_format="degree",
qudt_uri="http://qudt.org/vocab/unit/DEG",
unit_type=UnitType.ARBITRARY_UNIT,
aliases=["°", "deg"],
)
DEGREE_CELSIUS = TetraUnit(
ts_format="DegreeCelsius",
pint_format="degree_Celsius",
qudt_uri="http://qudt.org/vocab/unit/DEG_C",
unit_type=UnitType.TEMPERATURE,
aliases=["°C", "degC"],
)
DEGREE_CELSIUS_PER_MINUTE = TetraUnit(
ts_format="DegreeCelsiusPerMinute",
pint_format="degree_Celsius / minute",
qudt_uri="http://qudt.org/vocab/unit/DEG_C-PER-MIN",
unit_type=UnitType.ARBITRARY_UNIT,
aliases=["°C/min"],
)
DEGREE_FAHRENHEIT = TetraUnit(
ts_format="DegreeFahrenheit",
pint_format="degree_Fahrenheit",
qudt_uri="http://qudt.org/vocab/unit/DEG_F",
unit_type=UnitType.TEMPERATURE,
aliases=["°F", "degF"],
)
ELECTRON_VOLT = TetraUnit(
ts_format="ElectronVolt",
pint_format="electron_volt",
qudt_uri="http://qudt.org/vocab/unit/EV",
unit_type=UnitType.ARBITRARY_UNIT,
aliases=["eV"],
)
ENDOTOXIN_UNIT = TetraUnit(
ts_format="EndotoxinUnit",
pint_format="endotoxin_unit",
qudt_uri=None,
unit_type=UnitType.ENDOTOXIN,
aliases=["EU"],
)
ENDOTOXIN_UNIT_PER_NANOGRAM = TetraUnit(
ts_format="EndotoxinUnitPerNanogram",
pint_format="endotoxin_unit / nanogram",
qudt_uri=None,
unit_type=UnitType.ENDOTOXIN,
aliases=["EU/ng"],
)
ENDOTOXIN_UNIT_PER_MICROGRAM = TetraUnit(
ts_format="EndotoxinUnitPerMicrogram",
pint_format="endotoxin_unit / microgram",
qudt_uri=None,
unit_type=UnitType.ENDOTOXIN,
aliases=["EU/ug", "EU/μg"],
)
ENDOTOXIN_UNIT_PER_GRAM = TetraUnit(
ts_format="EndotoxinUnitPerGram",
pint_format="endotoxin_unit / gram",
qudt_uri=None,
unit_type=UnitType.ENDOTOXIN,
aliases=["EU/g"],
)
ENDOTOXIN_UNIT_PER_MICROLITER = TetraUnit(
ts_format="EndotoxinUnitPerMicroliter",
pint_format="endotoxin_unit / microliter",
qudt_uri=None,
unit_type=UnitType.ENDOTOXIN,
aliases=["EU/uL", "EU/μL"],
)
ENDOTOXIN_UNIT_PER_MILLILITER = TetraUnit(
ts_format="EndotoxinUnitPerMilliliter",
pint_format="endotoxin_unit / milliliter",
qudt_uri=None,
unit_type=UnitType.ENDOTOXIN,
aliases=["EU/mL"],
)
ENDOTOXIN_UNIT_PER_LITER = TetraUnit(
ts_format="EndotoxinUnitPerLiter",
pint_format="endotoxin_unit / liter",
qudt_uri=None,
unit_type=UnitType.ENDOTOXIN,
aliases=["EU/L"],
)
FEMTOGRAM = TetraUnit(
ts_format="Femtogram",
pint_format="femtogram",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#Femtogram",
unit_type=UnitType.SI,
aliases=["fg"],
)
FEMTOLITER = TetraUnit(
ts_format="Femtoliter",
pint_format="femtoliter",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#Femtoliter",
unit_type=UnitType.ARBITRARY_UNIT,
aliases=["fL"],
)
FRAME_PER_SECOND = TetraUnit(
ts_format="FramePerSecond",
pint_format=None,
qudt_uri="http://qudt.org/vocab/unit/FRAME-PER-SEC",
unit_type=UnitType.ARBITRARY_UNIT,
aliases=["frame/s"],
)
GRAM = TetraUnit(
ts_format="Gram",
pint_format="gram",
qudt_uri="http://qudt.org/vocab/unit/GM",
unit_type=UnitType.MASS,
aliases=["g"],
)
GRAM_PER_MILLILITER = TetraUnit(
ts_format="GramPerMilliliter",
pint_format="gram / milliliter",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#GramPerMilliliter",
unit_type=UnitType.CONCENTRATION,
aliases=["g/ml", "g/mL"],
)
GRAM_PER_LITER = TetraUnit(
ts_format="GramPerLiter",
pint_format="gram / liter",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#GramPerLiter",
unit_type=UnitType.CONCENTRATION,
aliases=["g/l", "g/L"],
)
GRAM_PER_LITER_SOLVENT = TetraUnit(
ts_format="GramPerLiterSolvent",
pint_format=None,
qudt_uri=None,
unit_type=UnitType.ARBITRARY_UNIT,
aliases=[],
)
GRAM_PER_LITER_SOLUTION = TetraUnit(
ts_format="GramPerLiterSolution",
pint_format=None,
qudt_uri=None,
unit_type=UnitType.ARBITRARY_UNIT,
aliases=[],
)
GRAM_PER_MOLE = TetraUnit(
ts_format="GramPerMole",
pint_format="gram / mole",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#GramPerMole",
unit_type=UnitType.MOLAR_MASS,
aliases=["g/mol", "g/mole"],
)
HERTZ = TetraUnit(
ts_format="Hertz",
pint_format="hertz",
qudt_uri="http://qudt.org/vocab/unit/HZ",
unit_type=UnitType.SI,
aliases=["Hz"],
)
HOUR = TetraUnit(
ts_format="Hour",
pint_format="hour",
qudt_uri="http://qudt.org/vocab/unit/HR",
unit_type=UnitType.TIME,
aliases=["h", "hr", "hrs"],
)
INCH = TetraUnit(
ts_format="Inch",
pint_format="inch",
qudt_uri="http://qudt.org/vocab/unit/IN",
unit_type=UnitType.LENGTH,
aliases=["inch"],
)
INTENSITY = TetraUnit(
ts_format="Intensity",
pint_format=None,
qudt_uri=None,
unit_type=UnitType.ARBITRARY_UNIT,
aliases=[],
)
JOULE = TetraUnit(
ts_format="Joule",
pint_format="joule",
qudt_uri="http://qudt.org/vocab/unit/J",
unit_type=UnitType.SI,
aliases=["J"],
)
JOULE_PER_MOLE = TetraUnit(
ts_format="JoulePerMole",
pint_format="joule / mole",
qudt_uri=None,
unit_type=UnitType.SI,
aliases=[],
)
KELVIN = TetraUnit(
ts_format="Kelvin",
pint_format="kelvin",
qudt_uri="http://qudt.org/vocab/unit/K",
unit_type=UnitType.TEMPERATURE,
aliases=["°K", "degK", "K"],
)
KILOCALORIE_PER_MOLE = TetraUnit(
ts_format="KilocaloriePerMole",
pint_format="kilocalorie / mole",
qudt_uri="http://qudt.org/vocab/unit/KiloCAL-PER-MOL",
unit_type=UnitType.ARBITRARY_UNIT,
aliases=["kcal/mol"],
)
KILOCOUNT_PER_SECOND = TetraUnit(
ts_format="KilocountPerSecond",
pint_format="kilocount / second",
qudt_uri=None,
unit_type=UnitType.ARBITRARY_UNIT,
aliases=[],
)
KILODALTON = TetraUnit(
ts_format="KiloDalton",
pint_format="kilodalton",
qudt_uri=None,
unit_type=UnitType.MASS,
aliases=["kDa"],
)
KILOGRAM = TetraUnit(
ts_format="Kilogram",
pint_format="kilogram",
qudt_uri="http://qudt.org/vocab/unit/KiloGM",
unit_type=UnitType.MASS,
aliases=["kg"],
)
KILOGRAM_PER_CUBIC_METER = TetraUnit(
ts_format="KilogramPerCubicMeter",
pint_format="kilogram / (meter ** 3)",
qudt_uri="http://qudt.org/vocab/unit/KiloGM-PER-M3",
unit_type=UnitType.SI,
aliases=["kg/m³", "kg/m3"],
)
KILOGRAM_PER_METER = TetraUnit(
ts_format="KilogramPerMeter",
pint_format="kilogram / meter",
qudt_uri="http://qudt.org/vocab/unit/KiloGM-PER-M",
unit_type=UnitType.SI,
aliases=["kg/m"],
)
KILOGRAM_PER_MOLE = TetraUnit(
ts_format="KilogramPerMole",
pint_format="kilogram / mole",
qudt_uri="http://qudt.org/vocab/unit/KiloGM-PER-MOL",
unit_type=UnitType.MOLAR_MASS,
aliases=["kg/mol", "kg/mole"],
)
KILOGRAM_PER_SQUARE_METER = TetraUnit(
ts_format="KilogramPerSquareMeter",
pint_format="kilogram / (meter ** 2)",
qudt_uri="http://qudt.org/vocab/unit/KiloGM-PER-M2",
unit_type=UnitType.SI,
aliases=["kg/m²", "kg/m2"],
)
KILOJOULE_PER_MOLE = TetraUnit(
ts_format="KiloJoulePerMole",
pint_format="kilojoule / mole",
qudt_uri=None,
unit_type=UnitType.SI,
aliases=[],
)
KILOPOND = TetraUnit(
ts_format="KiloPond",
pint_format="kilopond",
qudt_uri="http://qudt.org/vocab/unit/KiloPOND",
unit_type=UnitType.ARBITRARY_UNIT,
aliases=["kp"],
)
KILOVOLT = TetraUnit(
ts_format="KiloVolt",
pint_format="kilovolt",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#KiloVolt",
unit_type=UnitType.SI,
aliases=["kV"],
)
LIGHT_SCATTERING_UNIT = TetraUnit(
ts_format="LightScatteringUnit",
pint_format=None,
qudt_uri=None,
unit_type=UnitType.ARBITRARY_UNIT,
aliases=[],
)
LITER = TetraUnit(
ts_format="Liter",
pint_format="liter",
qudt_uri="http://qudt.org/vocab/unit/L",
unit_type=UnitType.VOLUME,
aliases=["l", "L"],
)
LITER_PER_HOUR = TetraUnit(
ts_format="LiterPerHour",
pint_format="liter / hour",
qudt_uri=None,
unit_type=UnitType.ARBITRARY_UNIT,
aliases=[],
)
LITER_PER_MINUTE = TetraUnit(
ts_format="LiterPerMinute",
pint_format="liter / minute",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#LiterPerMinute",
unit_type=UnitType.ARBITRARY_UNIT,
aliases=["L/min"],
)
LITER_PER_SQUARE_METER = TetraUnit(
ts_format="LiterPerSquareMeter",
pint_format="liter / (meter ** 2)",
qudt_uri=None,
unit_type=UnitType.ARBITRARY_UNIT,
aliases=[],
)
LMH_PER_PSI = TetraUnit(
ts_format="LiterPerSquareMeterPerHourPerPoundPerSquareInch",
pint_format="liter / ((meter**2) * hour * psi)",
qudt_uri=None,
unit_type=UnitType.ARBITRARY_UNIT,
aliases=[],
)
LITER_PER_SQUARE_METER_PER_MINUTE = TetraUnit(
ts_format="LiterPerSquareMeterPerMinute",
pint_format="liter / ((meter**2) * minute)",
qudt_uri=None,
unit_type=UnitType.ARBITRARY_UNIT,
aliases=[],
)
LITER_PER_SQUARE_METER_PER_HOUR = TetraUnit(
ts_format="LiterPerSquareMeterPerHour",
pint_format="liter / ((meter**2) * hour)",
qudt_uri=None,
unit_type=UnitType.ARBITRARY_UNIT,
aliases=[],
)
MEGAHERTZ = TetraUnit(
ts_format="MegaHertz",
pint_format="megahertz",
qudt_uri="http://qudt.org/vocab/unit/MegaHZ",
unit_type=UnitType.SI,
aliases=["MHz"],
)
MEGAPASCAL = TetraUnit(
ts_format="MegaPascal",
pint_format="megapascal",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#MegaPascal",
unit_type=UnitType.SI,
aliases=["MPa"],
)
METER = TetraUnit(
ts_format="Meter",
pint_format="meter",
qudt_uri="http://qudt.org/vocab/unit/M",
unit_type=UnitType.LENGTH,
aliases=["m"],
)
METER_PER_SECOND = TetraUnit(
ts_format="MeterPerSecond",
pint_format="meter / second",
qudt_uri="http://qudt.org/vocab/unit/M-PER-SEC",
)
METER_PER_SECOND_SQUARED = TetraUnit(
ts_format="MeterPerSecondSquared",
pint_format="meter / (second ** 2)",
qudt_uri="http://qudt.org/vocab/unit/M-PER-SEC2",
unit_type=UnitType.SI,
aliases=["m/s²", "m/s2"],
)
MICROGRAM = TetraUnit(
ts_format="Microgram",
pint_format="microgram",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#Microgram",
unit_type=UnitType.MASS,
aliases=["μg"],
)
MICROGRAM_PER_MILLILITER = TetraUnit(
ts_format="MicrogramPerMilliliter",
pint_format="microgram / milliliter",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#MicrogramPerMilliliter",
unit_type=UnitType.CONCENTRATION,
aliases=["μg/ml", "μg/mL"],
)
MICROGRAM_PER_LITER = TetraUnit(
ts_format="MicrogramPerLiter",
pint_format="microgram / liter",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#MicrogramPerLiter",
unit_type=UnitType.CONCENTRATION,
aliases=["μg/l", "μg/L"],
)
MICROLITER = TetraUnit(
ts_format="Microliter",
pint_format="microliter",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#Microliter",
unit_type=UnitType.VOLUME,
aliases=["ul", "μl", "μL"],
)
MICROLITER_PER_MINUTE = TetraUnit(
ts_format="MicroliterPerMinute",
pint_format="microliter / minute",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#MicroliterPerMinute",
unit_type=UnitType.ARBITRARY_UNIT,
aliases=["μL/min", "uL/min"],
)
MICROMETER = TetraUnit(
ts_format="Micrometer",
pint_format="micrometer",
qudt_uri="http://qudt.org/vocab/unit/MicroM",
unit_type=UnitType.LENGTH,
aliases=["μm"],
)
MICROMOLE = TetraUnit(
ts_format="Micromole",
pint_format="micromole",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#Micromole",
unit_type=UnitType.MOLE,
aliases=["μmol"],
)
MICROMOLAR = TetraUnit(
ts_format="Micromolar",
pint_format="micromolar",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#Micromolar",
unit_type=UnitType.CONCENTRATION,
aliases=["μM"],
)
MICRONEWTON_METER = TetraUnit(
ts_format="MicronewtonMeter",
pint_format="micronewton * meter",
qudt_uri="http://qudt.org/vocab/unit/MicroN-M",
unit_type=UnitType.TORQUE,
aliases=["μN·m", "μN.m"],
)
MICROSECOND = TetraUnit(
ts_format="MicroSecond",
pint_format="microsecond",
qudt_uri="http://qudt.org/vocab/unit/MicroSEC",
unit_type=UnitType.SI,
aliases=["μs", "us"],
)
MICROVOLT = TetraUnit(
ts_format="Microvolt",
pint_format="microvolt",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#Microvolt",
unit_type=UnitType.SI,
aliases=["μV", "uV", "microV"],
)
MILLI_ABSORBANCE_UNIT = TetraUnit(
ts_format="MilliAbsorbanceUnit",
pint_format=None,
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#MilliAbsorbanceUnit",
unit_type=UnitType.ARBITRARY_UNIT,
aliases=["mAU"],
)
MILLI_ABSORBANCE_UNIT_TIMES_MINUTE = TetraUnit(
ts_format="MilliAbsorbanceUnitTimesMinute",
pint_format=None,
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#MilliAbsorbanceUnitTimesMinute",
unit_type=UnitType.ARBITRARY_UNIT,
aliases=["mAU.min"],
)
MILLI_ABSORBANCE_UNIT_TIMES_MILLILITER = TetraUnit(
ts_format="MilliAbsorbanceUnitTimesMilliliter",
pint_format=None,
qudt_uri=None,
unit_type=UnitType.ARBITRARY_UNIT,
aliases=[],
)
MILLIAMPERE = TetraUnit(
ts_format="MilliAmpere",
pint_format="milliampere",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#MilliAmpere",
unit_type=UnitType.SI,
aliases=["mA"],
)
MILLIDEGREE = TetraUnit(
ts_format="MilliDegreeAngle",
pint_format="millidegree",
qudt_uri=None,
unit_type=UnitType.ARBITRARY_UNIT,
aliases=[],
)
MILLIGRAM = TetraUnit(
ts_format="Milligram",
pint_format="milligram",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#Milligram",
unit_type=UnitType.MASS,
aliases=["mg"],
)
MILLIGRAM_PER_KILOGRAM = TetraUnit(
ts_format="MilligramPerKilogram",
pint_format="milligram / kilogram",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#MilligramPerKilogram",
unit_type=UnitType.SI,
aliases=["mg/kg"],
)
MILLIGRAM_PER_LITER = TetraUnit(
ts_format="MilligramPerLiter",
pint_format="milligram / liter",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#MilligramPerLiter",
unit_type=UnitType.CONCENTRATION,
aliases=["mg/l", "mg/L"],
)
MILLIGRAM_PER_MILLILITER = TetraUnit(
ts_format="MilligramPerMilliliter",
pint_format="milligram / milliliter",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#MilligramPerMilliliter",
unit_type=UnitType.CONCENTRATION,
aliases=["mg/ml", "mg/mL"],
)
MILLILITER = TetraUnit(
ts_format="Milliliter",
pint_format="milliliter",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#Milliliter",
unit_type=UnitType.VOLUME,
aliases=["ml", "mL"],
)
MILLILITER_PER_GRAM = TetraUnit(
ts_format="MilliliterPerGram",
pint_format="milliliter / gram",
qudt_uri=None,
unit_type=UnitType.ARBITRARY_UNIT,
aliases=[],
)
MILLILITER_PER_MILLIGRAM_CENTIMETER = TetraUnit(
ts_format="MilliliterPerMilligramCentimeter",
pint_format="milliliter / (milligram * centimeter)",
qudt_uri=None,
unit_type=UnitType.ARBITRARY_UNIT,
aliases=[],
)
MILLILITER_PER_MINUTE = TetraUnit(
ts_format="MilliliterPerMinute",
pint_format="milliliter / minute",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#MilliliterPerMinute",
unit_type=UnitType.ARBITRARY_UNIT,
aliases=["mL/min"],
)
MILLILITER_PER_SECOND = TetraUnit(
ts_format="MilliliterPerSecond",
pint_format="milliliter / second",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#MilliliterPerSecond",
unit_type=UnitType.ARBITRARY_UNIT,
aliases=["mL/s"],
)
MILLIMETER = TetraUnit(
ts_format="Millimeter",
pint_format="millimeter",
qudt_uri="http://qudt.org/vocab/unit/MilliM",
unit_type=UnitType.LENGTH,
aliases=["mm"],
)
MILLIMETER_OF_MERCURY = TetraUnit(
ts_format="MillimeterOfMercury",
pint_format="millimeter_Hg",
qudt_uri="http://qudt.org/vocab/unit/MilliM_HG",
unit_type=UnitType.ARBITRARY_UNIT,
aliases=["mmHg"],
)
MILLIMETER_PER_SECOND_SQUARED = TetraUnit(
ts_format="MillimeterPerSecondSquared",
pint_format="millimeter / (second ** 2)",
qudt_uri=None,
unit_type=UnitType.SI,
aliases=[],
)
MILLIMOLAR = TetraUnit(
ts_format="Millimolar",
pint_format="millimolar",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#Millimolar",
unit_type=UnitType.CONCENTRATION,
aliases=["mM"],
)
MILLIMOLE = TetraUnit(
ts_format="Millimole",
pint_format="millimole",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#Millimole",
unit_type=UnitType.MOLE,
aliases=["mmol"],
)
MILLIMOLE_PER_LITER = TetraUnit(
ts_format="MillimolePerLiter",
pint_format="millimole / liter",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#MillimolePerLiter",
unit_type=UnitType.CONCENTRATION,
aliases=["mmol/L"],
)
MILLINORMAL = TetraUnit(
ts_format="MilliNormal",
pint_format=None,
qudt_uri=None,
unit_type=UnitType.ARBITRARY_UNIT,
aliases=[],
)
MILLION_CELLS_PER_MILLILITER = TetraUnit(
ts_format="MillionCellsPerMilliliter",
pint_format="megacount / milliliter",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#MillionCellsPerMilliliter",
unit_type=UnitType.ARBITRARY_UNIT,
aliases=["10^6 cells/mL"],
)
MILLIOSMOLE_PER_KILOGRAM = TetraUnit(
ts_format="MilliOsmolePerKilogram",
pint_format=None,
qudt_uri=None,
unit_type=UnitType.ARBITRARY_UNIT,
aliases=[],
)
MILLIPASCAL_SECOND = TetraUnit(
ts_format="MillipascalSecond",
pint_format="millipascal * second",
qudt_uri=None,
unit_type=UnitType.SI,
aliases=[],
)
MILLISECOND = TetraUnit(
ts_format="MilliSecond",
pint_format="millisecond",
qudt_uri="http://qudt.org/vocab/unit/MilliSEC",
unit_type=UnitType.TIME,
aliases=["msec"],
)
MILLISIEMEN = TetraUnit(
ts_format="Millisiemens",
pint_format="millisiemens",
qudt_uri=None,
unit_type=UnitType.SI,
aliases=[],
)
MILLISIEMENS_PER_CM = TetraUnit(
ts_format="MillisiemensPerCentimeter", # Note: 'Millisiemens' is used since the base unit is 'Siemens' (trailing 's' is not for plural)
pint_format="millisiemens / centimeter",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#MillisiemenPerCentimeter",
unit_type=UnitType.SI,
aliases=["mS/cm"],
)
MILLIVOLT = TetraUnit(
ts_format="MilliVolt",
pint_format="millivolt",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#Millivolt",
unit_type=UnitType.SI,
aliases=["mV"],
)
MILLIVOLT_PER_PH = TetraUnit(
ts_format="MillivoltPerPH",
pint_format=None,
qudt_uri=None,
unit_type=UnitType.ARBITRARY_UNIT,
aliases=[],
)
MILLIWATT = TetraUnit(
ts_format="MilliWatt",
pint_format="milliwatt",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#MilliWatt",
unit_type=UnitType.SI,
aliases=["mW"],
)
MINUTE_TIME = TetraUnit(
ts_format="MinuteTime",
pint_format="minute",
qudt_uri="http://qudt.org/vocab/unit/MIN",
unit_type=UnitType.TIME,
aliases=["min"],
)
MINUTE_TIME_SQUARED = TetraUnit(
ts_format="MinuteTimeSquared",
pint_format="minute ** 2",
qudt_uri=None,
unit_type=UnitType.ARBITRARY_UNIT,
aliases=[],
)
MOLE = TetraUnit(
ts_format="Mole",
pint_format="mole",
qudt_uri="http://qudt.org/vocab/unit/MOL",
unit_type=UnitType.MOLE,
aliases=["mol"],
)
MOLAR = TetraUnit(
ts_format="Molar",
pint_format="molar",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#Molar",
unit_type=UnitType.CONCENTRATION,
aliases=["M"],
)
MOLE_FRACTION = TetraUnit(
ts_format="MoleFraction",
pint_format=None,
qudt_uri=None,
unit_type=UnitType.ARBITRARY_UNIT,
aliases=[],
)
NANOAMPERE = TetraUnit(
ts_format="NanoAmpere",
pint_format="nanoampere",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#NanoAmpere",
unit_type=UnitType.SI,
aliases=["nA"],
)
NANOGRAM = TetraUnit(
ts_format="Nanogram",
pint_format="nanogram",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#Nanogram",
unit_type=UnitType.MASS,
aliases=["ng"],
)
NANOGRAM_PER_MICROLITER = TetraUnit(
ts_format="NanogramPerMicroliter",
pint_format="nanogram / microliter",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#NanogramPerMicroliter",
unit_type=UnitType.ARBITRARY_UNIT,
aliases=["ng/μL", "ng/uL"],
)
NANOGRAM_PER_MILLILITER = TetraUnit(
ts_format="NanogramPerMilliliter",
pint_format="nanogram / milliliter",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#NanogramPerMilliliter",
unit_type=UnitType.CONCENTRATION,
aliases=["ng/ml", "ng/mL"],
)
NANOGRAM_PER_LITER = TetraUnit(
ts_format="NanogramPerLiter",
pint_format="nanogram / liter",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#NanogramPerLiter",
unit_type=UnitType.CONCENTRATION,
aliases=["ng/l", "ng/L"],
)
NANOMETER = TetraUnit(
ts_format="Nanometer",
pint_format="nanometer",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#Nanometer",
unit_type=UnitType.LENGTH,
aliases=["nm", "NANOMETERS"],
)
NANOMETERPERMINUTE = TetraUnit(
ts_format="NanometerPerMinute",
pint_format="nanometer / minute",
qudt_uri=None,
unit_type=UnitType.ARBITRARY_UNIT,
aliases=[],
)
NANOMOLAR = TetraUnit(
ts_format="Nanomolar",
pint_format="nanomolar",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#Nanomolar",
unit_type=UnitType.CONCENTRATION,
aliases=["nM"],
)
NANOLITER = TetraUnit(
ts_format="Nanoliter",
pint_format="nanoliter",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#Nanoliter",
)
NANOMOLE = TetraUnit(
ts_format="Nanomole",
pint_format="nanomole",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#Nanomole",
unit_type=UnitType.MOLE,
aliases=["nmol"],
)
NANOSECOND = TetraUnit(
ts_format="NanoSecond",
pint_format="nanosecond",
qudt_uri=None,
unit_type=UnitType.SI,
aliases=[],
)
NEWTON = TetraUnit(
ts_format="Newton",
pint_format="newton",
qudt_uri="http://qudt.org/vocab/unit/N",
unit_type=UnitType.SI,
aliases=["N"],
)
NEWTON_METER = TetraUnit(
ts_format="NewtonMeter",
pint_format="newton * meter",
qudt_uri="http://qudt.org/vocab/unit/N-M",
unit_type=UnitType.TORQUE,
aliases=["N·m", "N.m"],
)
OHM = TetraUnit(
ts_format="Ohm",
pint_format="ohm",
qudt_uri="http://qudt.org/vocab/unit/OHM",
unit_type=UnitType.SI,
aliases=["Ω", "ohm"],
)
OSMOLE = TetraUnit(
ts_format="Osmole",
pint_format=None,
qudt_uri=None,
unit_type=UnitType.ARBITRARY_UNIT,
aliases=[],
)
OSMOLE_PER_KILOGRAM = TetraUnit(
ts_format="OsmolePerKilogram",
pint_format=None,
qudt_uri=None,
unit_type=UnitType.ARBITRARY_UNIT,
aliases=[],
)
OSMOLE_PER_LITER = TetraUnit(
ts_format="OsmolesPerLiter",
pint_format=None,
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#OsmolesPerLiter",
unit_type=UnitType.ARBITRARY_UNIT,
aliases=["osmol/L", "osm/L"],
)
PARTS_PER_MILLION = TetraUnit(
ts_format="PartsPerMillion",
pint_format="ppm",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#PartsPerMillion",
unit_type=UnitType.ARBITRARY_UNIT,
aliases=["ppm"],
)
PASCAL = TetraUnit(
ts_format="Pascal",
pint_format="pascal",
qudt_uri="http://qudt.org/vocab/unit/PA",
unit_type=UnitType.SI,
aliases=["Pa"],
)
PASCAL_SECOND = TetraUnit(
ts_format="PascalSecond",
pint_format="pascal * second",
qudt_uri="http://qudt.org/vocab/unit/PA-SEC",
unit_type=UnitType.SI,
aliases=["Pa·s", "Pa.s"],
)
PERCENT = TetraUnit(
ts_format="Percent",
pint_format="percent",
qudt_uri="http://qudt.org/vocab/unit/PERCENT",
unit_type=UnitType.ARBITRARY_UNIT,
aliases=["%"],
)
PERCENT_VOLUME = TetraUnit(
ts_format="PercentVolume",
pint_format=None,
qudt_uri=None,
unit_type=UnitType.ARBITRARY_UNIT,
aliases=[],
)
PERMITTIVITY = DeprecatedTetraUnit(
ts_format="Permittivity",
pint_format=None,
qudt_uri=None,
deprecated_message=(
"Permittivity is a quantity, not a unit. Find and use the relevant unit, "
"or request a unit to be added to this library."
),
)
PER_SECOND = TetraUnit(
ts_format="PerSecond",
pint_format="1 / second",
qudt_uri="http://qudt.org/vocab/unit/PER-SEC",
unit_type=UnitType.SI,
aliases=["/s"],
)
PICOGRAM = TetraUnit(
ts_format="Picogram",
pint_format="picogram",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#Picogram",
unit_type=UnitType.MASS,
aliases=["pg"],
)
PICOGRAM_PER_MILLILITER = TetraUnit(
ts_format="PicogramPerMilliliter",
pint_format="picogram / milliliter",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#PicogramPerMilliliter",
unit_type=UnitType.CONCENTRATION,
aliases=["pg/ml", "pg/mL"],
)
PICOGRAM_PER_LITER = TetraUnit(
ts_format="PicogramPerLiter",
pint_format="picogram / liter",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#PicogramPerLiter",
unit_type=UnitType.CONCENTRATION,
aliases=["pg/l", "pg/L"],
)
PICOMETER = TetraUnit(
ts_format="Picometer",
pint_format="picometer",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#Picometer",
unit_type=UnitType.LENGTH,
aliases=["pm"],
)
PICOMOLE = TetraUnit(
ts_format="Picomole",
pint_format="picomole",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#Picomole",
unit_type=UnitType.MOLE,
aliases=["pmol"],
)
PICOMOLAR = TetraUnit(
ts_format="Picomolar",
pint_format="picomolar",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#Picomolar",
unit_type=UnitType.CONCENTRATION,
aliases=["pM"],
)
POISE = TetraUnit(
ts_format="Poise",
pint_format="poise",
qudt_uri="http://qudt.org/vocab/unit/POISE",
unit_type=UnitType.ARBITRARY_UNIT,
aliases=["P"],
)
POUND_PER_SQUARE_FOOT = TetraUnit(
ts_format="PoundPerSquareFoot",
pint_format="pound / foot ** 2",
qudt_uri="http://qudt.org/vocab/unit/LB_F-PER-FT2",
unit_type=UnitType.ARBITRARY_UNIT,
aliases=["lbf/ft²", "lbf/ft2"],
)
POUND_PER_SQUARE_INCH = TetraUnit(
ts_format="PoundPerSquareInch", # Note: This unit string is widely used for psi in our task scripts, even though its different to QUDT
pint_format="psi",
qudt_uri="http://qudt.org/vocab/unit/LB_F-PER-IN2",
unit_type=UnitType.ARBITRARY_UNIT,
aliases=["lbf/in²", "lbf/in2", "psi"],
)
RADIAN = TetraUnit(
ts_format="Radian",
pint_format="radian",
qudt_uri="http://qudt.org/vocab/unit/RAD",
unit_type=UnitType.SI,
aliases=["rad"],
)
RECIPROCAL_CENTIMETER = TetraUnit(
ts_format="ReciprocalCentimeter",
pint_format="reciprocal_centimeter",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#ReciprocalCentimeter",
unit_type=UnitType.SI,
aliases=["1/cm"],
)
RECIPROCAL_MINUTE = DeprecatedTetraUnit(
# NOTE: This is the old unit format, use PER_MINUTE instead
ts_format="ReciprocalMinute",
pint_format="1 / minute",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#PerMinute",
deprecated_message="This unit is now deprecated, use PER_MINUTE instead",
)
PER_MINUTE = TetraUnit(
ts_format="PerMinute",
pint_format="1 / minute",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#PerMinute",
unit_type=UnitType.ARBITRARY_UNIT,
aliases=["min^-1"],
)
RELATIVE_FLUORESCENCE_UNIT = TetraUnit(
ts_format="RelativeFluorescenceUnit",
pint_format=None,
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#RelativeFluorescenceUnit",
unit_type=UnitType.UNITLESS,
aliases=["RFU"],
)
RELATIVE_PERMITTIVITY = TetraUnit(
ts_format="RelativePermittivity",
pint_format=None,
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#Unitless",
unit_type=UnitType.UNITLESS,
aliases=[],
)
REVOLUTION_PER_HOUR = TetraUnit(
ts_format="RevolutionPerHour",
pint_format="revolution / hour",
qudt_uri="http://qudt.org/vocab/unit/REV-PER-HR",
unit_type=UnitType.ARBITRARY_UNIT,
aliases=["rev/h"],
)
REVOLUTION_PER_MINUTE = TetraUnit(
ts_format="RevolutionPerMinute",
pint_format="rpm",
qudt_uri="http://qudt.org/vocab/unit/REV-PER-MIN",
unit_type=UnitType.ARBITRARY_UNIT,
aliases=["rev/min", "RPM", "rpm", "r/min"],
)
REVOLUTION_PER_SECOND = TetraUnit(
ts_format="RevolutionPerSecond",
pint_format="rps",
qudt_uri="http://qudt.org/vocab/unit/REV-PER-SEC",
unit_type=UnitType.ARBITRARY_UNIT,
aliases=["rev/s"],
)
SECOND_TIME = TetraUnit(
ts_format="SecondTime",
pint_format="second",
qudt_uri="http://qudt.org/vocab/unit/SEC",
unit_type=UnitType.TIME,
aliases=["s", "sec"],
)
SIEMENS_PER_CENTIMETER_SECOND = TetraUnit(
ts_format="SiemensPerCentimeterSecond",
pint_format="siemens / (centimeter * second)",
qudt_uri=None,
unit_type=UnitType.SI,
aliases=[],
)
SQUARE_CENTIMETER = TetraUnit(
ts_format="SquareCentimeter",
pint_format="centimeter ** 2",
qudt_uri="http://qudt.org/vocab/unit/CentiM2",
unit_type=UnitType.SI,
aliases=["cm²", "cm2"],
)
SQUARE_CENTIMETER_PER_SECOND = TetraUnit(
ts_format="SquareCentimeterPerSecond",
pint_format="centimeter ** 2 / second",
qudt_uri=None,
unit_type=UnitType.SI,
aliases=[],
)
SQUARE_METER = TetraUnit(
ts_format="SquareMeter",
pint_format="meter ** 2",
qudt_uri="http://qudt.org/vocab/unit/M2",
unit_type=UnitType.SI,
aliases=["m²", "m2"],
)
SQUARE_METER_PER_GRAM = TetraUnit(
ts_format="SquareMeterPerGram",
pint_format="meter ** 2 / gram",
qudt_uri=None,
unit_type=UnitType.SI,
aliases=[],
)
SQUARE_MICROMETER = TetraUnit(
ts_format="SquareMicrometer",
pint_format="micrometer ** 2",
qudt_uri="http://qudt.org/vocab/unit/MicroM2",
unit_type=UnitType.SI,
aliases=["μm²", "um2"],
)
SQUARE_ROOT_MEGAPASCAL = TetraUnit(
ts_format="SquareRootMegaPascal",
pint_format="megapascal ** 0.5",
qudt_uri=None,
unit_type=UnitType.ARBITRARY_UNIT,
aliases=[],
)
TON_OF_REFRIGERATION = TetraUnit(
ts_format="TonOfRefrigeration",
pint_format=None,
qudt_uri="http://qudt.org/vocab/unit/TON_FG",
unit_type=UnitType.ARBITRARY_UNIT,
aliases=["TOR"],
)
TORR = TetraUnit(
ts_format="Torr",
pint_format="torr",
qudt_uri="http://qudt.org/vocab/unit/TORR",
unit_type=UnitType.ARBITRARY_UNIT,
aliases=[],
)
UNITLESS = TetraUnit(
ts_format="Unitless",
pint_format="1",
qudt_uri="http://purl.allotrope.org/ontology/qudt-ext/unit#Unitless",
unit_type=UnitType.UNITLESS,
aliases=[],
)
ENZYME_UNIT_PER_LITER = TetraUnit(
ts_format="InternationalUnitPerLiter",
pint_format="enzyme_unit / liter",
qudt_uri="http://qudt.org/vocab/unit/ENZ-PER-L",
unit_type=UnitType.CATALYTIC_ACTIVITY,
aliases=["U/L"],
)
VOLT = TetraUnit(
ts_format="Volt",
pint_format="volt",
qudt_uri="http://qudt.org/vocab/unit/V",
unit_type=UnitType.SI,
aliases=["V"],
)
VOXEL = TetraUnit(
ts_format="Voxel",
pint_format=None,
qudt_uri=None,
unit_type=UnitType.ARBITRARY_UNIT,
aliases=[],
)
WATT = TetraUnit(
ts_format="Watt",
pint_format="watt",
qudt_uri="http://qudt.org/vocab/unit/W",
unit_type=UnitType.SI,
aliases=["W"],
)
[docs]
@classmethod
def get_all_units(cls) -> Dict[str, TetraUnit]:
"""Get all units in the library"""
return {
key: val
for key, val in vars(cls).items()
if not key.startswith("__") and not isinstance(val, classmethod)
}
[docs]
@classmethod
def contains(cls, ts_format: str) -> bool:
"""Check if a unit is in the library"""
return any(
attr_value.ts_format.lower() == ts_format.lower()
for attr_value in cls.get_all_units().values()
)
[docs]
@classmethod
def get(cls, ts_format: str) -> TetraUnit:
"""Get a unit by its ts_string"""
for attr_value in cls.get_all_units().values():
if attr_value.ts_format.lower() == ts_format.lower():
return attr_value
raise ValueError(f"Unit {ts_format} is not in our library")
@classmethod
def _get_aliases_by_type(cls, unit_type: UnitType) -> Dict[str, List[str]]:
"""Get all units in the library by unit type"""
available_aliases = {}
for val in vars(cls).values():
if isinstance(val, TetraUnit) and val.unit_type == unit_type:
available_aliases.update({val.ts_format: val.aliases})
return available_aliases
@classmethod
def _generate_default_mapping(cls, unit_type: UnitType) -> Dict[str, TetraUnit]:
"""Get all units in the library by unit type"""
mapped_aliases = {}
for val in vars(cls).values():
if isinstance(val, TetraUnit) and val.unit_type == unit_type:
mapped_aliases.update(
{alias: val.strip_aliases() for alias in val.aliases}
)
return mapped_aliases
[docs]
class UnitParser:
"""Class for parsing raw unit strings to standard unit strings"""
mapping: Mapping[Hashable, Optional[Union[str, TetraUnit]]] = {}
unmapped_units: Set[Hashable] = set()
[docs]
def parse(self, raw_unit: Optional[str]) -> Optional[Union[str, TetraUnit]]:
"""Parse a raw unit string to a standard unit string."""
if raw_unit is None or raw_unit == "":
return None
if raw_unit in self.mapping:
return self.mapping[raw_unit]
self.unmapped_units.add(raw_unit)
return raw_unit
[docs]
@classmethod
def from_unit_type(cls, unit_type: Union[UnitType, List[UnitType]]) -> "UnitParser":
"""
Create a UnitParser instance from a unit type
Args:
unit_type: UnitType or list of UnitType
(e.g. UnitType.CONCENTRATION, [UnitType.MOLE, UnitType.MOLAR_MASS])
"""
return cls(cls._generate_mapping(unit_type))
[docs]
@classmethod
def from_custom_mapping(cls, mapping: dict[str, TetraUnit]) -> "UnitParser":
"""
Create a UnitParser instance from a custom mapping
Args:
mapping: A dictionary mapping raw unit strings to TetraUnit instances
(e.g. {"mg/L": Unit.MILLIGRAM_PER_LITER, "g/L": Unit.GRAM_PER_LITER})
"""
return cls(mapping)
[docs]
@classmethod
def from_unit_type_and_custom_mapping(
cls,
mapping: list[dict[str, TetraUnit]] | dict[str, TetraUnit],
unit_type: Union[UnitType, List[UnitType]],
strict_mode: bool = True,
) -> "UnitParser":
"""
Create a UnitParser instance from a custom mapping and a unit type
Args:
mapping: A dictionary mapping raw unit strings to TetraUnit instances
unit_type: UnitType or list of UnitType
strict_mode: Whether to raise an error if the mapping provided overwrites any existing mappings for the unit type.
Defaults to True.
"""
default = Unit._generate_default_mapping(unit_type)
mappings = (
mapping + [default] if isinstance(mapping, list) else [mapping, default]
)
merged = cls._merge_mappings(mappings, strict_mode=strict_mode)
return cls(merged)
[docs]
@classmethod
def available_units(cls, unit_type: UnitType) -> str:
"""Checks the unit group for a given unit string"""
units_to_aliases = Unit._get_aliases_by_type(unit_type)
return f"""Existing aliased TetraUnits:\n {units_to_aliases}"""
def __init__(
self,
mappings: Union[
Mapping[Hashable, Optional[str]], List[Mapping[Hashable, Optional[str]]]
],
) -> None:
self.unmapped_units = set()
self.mapping = self._merge_mappings(mappings)
@staticmethod
def _merge_mappings(
mappings: Union[
Mapping[Hashable, Optional[str]], List[Mapping[Hashable, Optional[str]]]
],
strict_mode: bool = True,
) -> Dict[str, str]:
"""Merges mappings together checks for conflicting key with different values"""
if isinstance(mappings, Mapping):
return dict(mappings)
else:
merged_mappings = {}
for mapping in mappings:
intersection = merged_mappings.keys() & mapping.keys()
if not intersection:
merged_mappings.update(mapping)
continue
different_value_keys = [
key for key in intersection if merged_mappings[key] != mapping[key]
]
# Also catches alias assigned to different units
if different_value_keys and strict_mode:
raise ValueError(
(
"Found keys with multiple possible values:\n\n"
"Key | Value 1 | Value 2\n"
"-------------------------------\n"
)
+ "".join(
f"{key} | {merged_mappings[key]} | {mapping[key]}\n"
for key in different_value_keys
)
+ "\nIf a key is present in multiple mappings, please ensure they all map it to the same value."
)
elif different_value_keys:
logging.warning(
f"The following units will be overwritten in the merged mapping: {different_value_keys}"
)
return merged_mappings
@staticmethod
def _generate_mapping(
unit_type: Union[UnitType, List[UnitType]]
) -> Dict[str, TetraUnit]:
"""Generates mappings from the TetraUnit objects in Units, filtered by unit type if provided."""
if isinstance(unit_type, UnitType):
return Unit._generate_default_mapping(unit_type)
else:
mappings = []
for ut in unit_type:
mappings.append(Unit._generate_default_mapping(ut))
return UnitParser._merge_mappings(mappings)
CONCENTRATION_MAPPING = {
"pg/ml": Unit.PICOGRAM_PER_MILLILITER,
"pg/mL": Unit.PICOGRAM_PER_MILLILITER,
"ng/ml": Unit.NANOGRAM_PER_MILLILITER,
"ng/mL": Unit.NANOGRAM_PER_MILLILITER,
"μg/ml": Unit.MICROGRAM_PER_MILLILITER,
"μg/mL": Unit.MICROGRAM_PER_MILLILITER,
"mg/ml": Unit.MILLIGRAM_PER_MILLILITER,
"mg/mL": Unit.MILLIGRAM_PER_MILLILITER,
"g/ml": Unit.GRAM_PER_MILLILITER,
"g/mL": Unit.GRAM_PER_MILLILITER,
"pg/l": Unit.PICOGRAM_PER_LITER,
"pg/L": Unit.PICOGRAM_PER_LITER,
"ng/l": Unit.NANOGRAM_PER_LITER,
"ng/L": Unit.NANOGRAM_PER_LITER,
"μg/l": Unit.MICROGRAM_PER_LITER,
"μg/L": Unit.MICROGRAM_PER_LITER,
"mg/l": Unit.MILLIGRAM_PER_LITER,
"mg/L": Unit.MILLIGRAM_PER_LITER,
"g/l": Unit.GRAM_PER_LITER,
"g/L": Unit.GRAM_PER_LITER,
"M": Unit.MOLAR,
"mM": Unit.MILLIMOLAR,
"mmol/L": Unit.MILLIMOLE_PER_LITER,
"μM": Unit.MICROMOLAR,
"nM": Unit.NANOMOLAR,
"pM": Unit.PICOMOLAR,
}
MOLE_MAPPING = {
"mol": Unit.MOLE,
"mmol": Unit.MILLIMOLE,
"μmol": Unit.MICROMOLE,
"nmol": Unit.NANOMOLE,
"pmol": Unit.PICOMOLE,
}
MOLAR_MASS_MAPPING = {
"g/mol": Unit.GRAM_PER_MOLE,
"g/mole": Unit.GRAM_PER_MOLE,
"kg/mol": Unit.KILOGRAM_PER_MOLE,
"kg/mole": Unit.KILOGRAM_PER_MOLE,
}
TEMPERATURE_MAPPING = {
"°C": Unit.DEGREE_CELSIUS,
"degC": Unit.DEGREE_CELSIUS,
"°F": Unit.DEGREE_FAHRENHEIT,
"degF": Unit.DEGREE_FAHRENHEIT,
"°K": Unit.KELVIN,
"degK": Unit.KELVIN,
"K": Unit.KELVIN,
}
LENGTH_MAPPING = {
"m": Unit.METER,
"cm": Unit.CENTIMETER,
"mm": Unit.MILLIMETER,
"μm": Unit.MICROMETER,
"nm": Unit.NANOMETER,
"NANOMETERS": Unit.NANOMETER,
"pm": Unit.PICOMETER,
"Å": Unit.ANGSTROM,
"inch": Unit.INCH,
}
MASS_MAPPING = {
"pg": Unit.PICOGRAM,
"ng": Unit.NANOGRAM,
"μg": Unit.MICROGRAM,
"mg": Unit.MILLIGRAM,
"g": Unit.GRAM,
"kg": Unit.KILOGRAM,
"Da": Unit.DALTON,
"kDa": Unit.KILODALTON,
}
TIME_MAPPING = {
"msec": Unit.MILLISECOND,
"s": Unit.SECOND_TIME,
"sec": Unit.SECOND_TIME,
"min": Unit.MINUTE_TIME,
"h": Unit.HOUR,
"hr": Unit.HOUR,
"hrs": Unit.HOUR,
}
VOLUME_MAPPING = {
"ul": Unit.MICROLITER,
"μl": Unit.MICROLITER,
"μL": Unit.MICROLITER,
"ml": Unit.MILLILITER,
"mL": Unit.MILLILITER,
"l": Unit.LITER,
"L": Unit.LITER,
}
PERCENT_MAPPING = {
"%": Unit.PERCENT,
}
CATALYTIC_ACTIVITY_MAPPING = {
"U/L": Unit.ENZYME_UNIT_PER_LITER,
}
ENDOTOXIN_MAPPING = {
"EU": Unit.ENDOTOXIN_UNIT,
"EU/ng": Unit.ENDOTOXIN_UNIT_PER_NANOGRAM,
"EU/ug": Unit.ENDOTOXIN_UNIT_PER_MICROGRAM,
"EU/μg": Unit.ENDOTOXIN_UNIT_PER_MICROGRAM,
"EU/g": Unit.ENDOTOXIN_UNIT_PER_GRAM,
"EU/uL": Unit.ENDOTOXIN_UNIT_PER_MICROLITER,
"EU/μL": Unit.ENDOTOXIN_UNIT_PER_MICROLITER,
"EU/mL": Unit.ENDOTOXIN_UNIT_PER_MILLILITER,
"EU/L": Unit.ENDOTOXIN_UNIT_PER_LITER,
}