Skip to content

Commit

Permalink
rename to_decimal_num() to to_decimal() (#38)
Browse files Browse the repository at this point in the history
* rename `to_decimal_num()` to `to_decimal()`

* fix bug
  • Loading branch information
chaoming0625 authored Aug 10, 2024
1 parent 3a870f2 commit 4883c77
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion brainunit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# ==============================================================================
from __future__ import annotations

__version__ = "0.0.1.1"
__version__ = "0.0.2"

from . import _base
from . import _unit_common
Expand Down
8 changes: 4 additions & 4 deletions brainunit/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ def assert_quantity(
assert isinstance(unit, Unit), f"Expected a Unit, but got {unit}."
q = _to_quantity(q)
assert have_same_dim(get_dim(q), unit), f"Dimension mismatch: ({get_dim(q)}) ({get_dim(unit)})"
if not jnp.allclose(q.to_decimal_num(unit), mantissa, equal_nan=True):
raise AssertionError(f"Values do not match: {q.to_decimal_num(unit)} != {mantissa}")
if not jnp.allclose(q.to_decimal(unit), mantissa, equal_nan=True):
raise AssertionError(f"Values do not match: {q.to_decimal(unit)} != {mantissa}")


# SI dimensions (see table at the top of the file) and various descriptions,
Expand Down Expand Up @@ -1844,14 +1844,14 @@ def unit(self, *args):
"Please create a new Quantity object with the unit you want."
)

def to_decimal_num(self, unit: Unit) -> jax.typing.ArrayLike:
def to_decimal(self, unit: Unit) -> jax.typing.ArrayLike:
"""
Convert the given :py:class:`Quantity` into the decimal number.
Examples::
>>> a = jax.numpy.array([1, 2, 3]) * mV
>>> a.to_decimal_num(volt)
>>> a.to_decimal(volt)
array([0.001, 0.002, 0.003])
Args:
Expand Down
8 changes: 4 additions & 4 deletions brainunit/math/_fun_accept_unitless.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def _fun_accept_unitless_unary(
return func(x, *args, **kwargs)
else:
assert isinstance(unit_to_scale, Unit), f'unit_to_scale should be a Unit instance. Got {unit_to_scale}'
return func(x.to_decimal_num(unit_to_scale), *args, **kwargs)
return func(x.to_decimal(unit_to_scale), *args, **kwargs)
else:
assert unit_to_scale is None, f'Unit should be None for the function "{func}" when "x" is not a Quantity.'
return func(x, *args, **kwargs)
Expand Down Expand Up @@ -763,7 +763,7 @@ def _fun_accept_unitless_return_keep_unit(
return func(x, *args, **kwargs)
else:
assert isinstance(unit_to_scale, Unit), f'unit_to_scale should be a Unit instance. Got {unit_to_scale}'
r = func(x.to_decimal_num(unit_to_scale), *args, **kwargs)
r = func(x.to_decimal(unit_to_scale), *args, **kwargs)
return jax.tree.map(lambda a: a * unit_to_scale, r)
else:
# assert unit_to_scale is None, f'Unit should be None for the function "{func}" when "x" is not a Quantity.'
Expand Down Expand Up @@ -993,15 +993,15 @@ def _fun_accept_unitless_binary(
x = x.mantissa
else:
assert isinstance(unit_to_scale, Unit), f'unit_to_scale should be a Unit instance. Got {unit_to_scale}'
x = x.to_decimal_num(unit_to_scale)
x = x.to_decimal(unit_to_scale)
if isinstance(y, Quantity):
if unit_to_scale is None:
assert y.is_unitless, (f'Input should be unitless for the function "{func}" '
f'when scaling "unit_to_scale" is not provided.')
y = y.mantissa
else:
assert isinstance(unit_to_scale, Unit), f'unit_to_scale should be a Unit instance. Got {unit_to_scale}'
y = y.to_decimal_num(unit_to_scale)
y = y.to_decimal(unit_to_scale)
return func(x, y, *args, **kwargs)


Expand Down
6 changes: 3 additions & 3 deletions brainunit/math/_fun_accept_unitless_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def test_fun_accept_unitless_unary_1(self, value):
(bu.nA, bu.amp)]:
q = value * unit
result = fun(q, unit_to_scale=unit2scale)
expected = jnp_fun(q.to_decimal_num(unit2scale))
expected = jnp_fun(q.to_decimal(unit2scale))
assert_quantity(result, expected)

with pytest.raises(AssertionError):
Expand Down Expand Up @@ -119,7 +119,7 @@ def test_func_accept_unitless_binary(self, value):
q1 = value1 * meter
q2 = value2 * meter
result = bm_fun(q1, q2, unit_to_scale=bu.dametre)
expected = jnp_fun(q1.to_decimal_num(bu.dametre), q2.to_decimal_num(bu.dametre))
expected = jnp_fun(q1.to_decimal(bu.dametre), q2.to_decimal(bu.dametre))
assert_quantity(result, expected)

with pytest.raises(AssertionError):
Expand All @@ -146,7 +146,7 @@ def test_func_accept_unitless_binary_ldexp(self, value):

q1 = value1 * meter
q2 = value2 * meter
result = bm_fun(q1.to_decimal_num(meter), jnp.array(value2))
result = bm_fun(q1.to_decimal(meter), jnp.array(value2))
expected = jnp_fun(jnp.array(value1), jnp.array(value2))
assert_quantity(result, expected)

Expand Down
2 changes: 1 addition & 1 deletion docs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# from the environment for the first two.
SPHINXOPTS ?=
SPHINXBUILD ?= sphinx-build
SPHINXPROJ = brainpy
SPHINXPROJ = brainunit
SOURCEDIR = .
BUILDDIR = _build

Expand Down

0 comments on commit 4883c77

Please sign in to comment.