def test_prefix_mul(): volt = define_unit("V") mV = milli * volt assert mV.name == "mV" assert mV.data_unit == volt assert mV.scale == 1e-3
import matplotlib.pyplot as plt import numpy as np from matplotlib.axes import Axes from matplotlib.lines import Line2D from unitlib import define_unit from unitlib.prefixes import milli second = define_unit("s") ms = milli * second volt = define_unit("V") mV = milli * volt x = np.linspace(0, 10) * ms y = np.random.randn(len(x)) * mV fig, ax = plt.subplots() ax: Axes ax.plot(x, y) def test_plotted_data_is_in_display_units(): line: Line2D = ax.lines[0] assert all(line.get_xdata() == x) assert all(line.get_xdata() != x.data) assert all(line.get_ydata() == y) assert all(line.get_ydata() != y.data) if __name__ == "__main__": plt.show()
import numpy as np import pytest from numpy import allclose as numeric_equals from unitlib import Array, Quantity, Unit, define_unit from unitlib.core_objects.unit_internals import PoweredUnitAtom from unitlib.prefixes import milli, nano volt = define_unit("V") mV = milli * volt nV = nano * volt second = define_unit("s") ms = milli * second minute = define_unit("min", 60 * second) siemens = define_unit("S") nS = nano * siemens def test_array_times_unit(): amumu = 3 * mV * mV assert numeric_equals(amumu.data, 3e-6) def test_array_div_unit(): amudu = 3 * nS / mV assert numeric_equals(amudu.data, 3e-6) def test_1_over_vs_x_over():
from unitlib import define_unit # No unit equivalence functionality yet. rad = radian = define_unit("rad") sr = steradian = define_unit("sr") volt = define_unit("V") coulomb = define_unit("C") farad = define_unit("F") siemens = define_unit("S") Hz = define_unit("Hz") ohm = define_unit("Ω") pascal = Pa = define_unit("Pa") joule = define_unit("J") watt = define_unit("W") weber = Wb = define_unit("Wb") tesla = define_unit("T") henry = define_unit("H") lumen = lm = define_unit("lm") lux = lx = define_unit("lx") becquerel = Bq = define_unit("Bq") gray = Gy = define_unit("Gy") sievert = Sv = define_unit("Sv") katal = kat = define_unit("kat")
E TypeError: __init__() got an unexpected keyword argument 'unit_atom' Problem was that `PoweredUnitAtom.__new__` made a new `UnitAtom` and wrongly called **UnitAtom's** `__init__` with PoweredUnitAtom's kwargs. Fixed by not using a custom `PoweredUnitAtom.__new__`, but rather placing its logic in `PoweredUnitAtom._raised_to` (which is inherited unmodified by `UnitAtom`). """ import numpy as np from unitlib import define_unit from unitlib.core_objects.unit_internals import PoweredUnitAtom from unitlib.prefixes import milli volt = define_unit("V") mV = milli * volt def check(mV2: PoweredUnitAtom): assert mV2.name == "mV²" assert mV2.unit_atom == mV assert mV2.power == 2 assert mV2.data_unit == volt**2 assert mV2.scale == mV2.data == np.array(1e-6) assert mV2.data_in_display_units == mV2.dd == np.array(1) assert mV2.value == 1 assert mV2.components == (mV2, ) def test_new_and_init():
from unitlib import define_unit from unitlib.prefixes import kilo, milli, micro from .SI_base import meter, second, kilogram minute = define_unit("min", 60 * second) hour = define_unit("h", 60 * minute) day = define_unit("day", 24 * hour) year = define_unit("year", 365 * day) liter = define_unit("L") km = kilometre = kilometer = kilo * meter ha = hectare = define_unit("ha") ton = tonne = define_unit("t") gram = gramme = define_unit("g", kilogram / kilo.factor) mg = milli * gram μg = ug = micro * gram
from unitlib import define_unit second = define_unit("s") meter = metre = define_unit("m") gram = define_unit("g") kilogram = kg = define_unit("kg") amp = ampere = define_unit("A") kelvin = define_unit("K") mole = mol = define_unit("mol") candela = cd = define_unit("cd")
def test_new(): second = define_unit("s") minute = define_unit("min", 60 * second) assert minute.data_unit == second assert minute.scale == 60
def test_quantity_format(): volt = define_unit("volt") q = Quantity(8, volt) assert str(q) == repr(q) == f"{q}" == "8 volt"
def test_unit_str_repr(): volt = define_unit("volt") assert repr(volt) == '<DataUnitAtom "volt">' assert str(volt) == f"{volt}" == "volt"
import pytest from unitlib import define_unit from unitlib.core_objects import IncompatibleUnitsError from unitlib.prefixes import milli volt = define_unit("V") mV = milli * volt siemens = define_unit("S") array_1D = [5, 3, 5.2] * mV def test_len(): assert len(array_1D) == 3 def test_getitem_1D(): assert array_1D[1] == 3 * mV assert all(array_1D[:2] == [5, 3] * mV) def test_setitem_1D(): array = array_1D # copy not yet implemented array[2] = 1 * volt assert all(array == [5, 3, 1000] * mV) array[:] = 3 * mV assert all(array == [3, 3, 3] * mV)