Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add celsius to kelvin transform function #50

Merged
merged 2 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions brainunit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
from __future__ import annotations

__version__ = "0.0.2"

Expand All @@ -23,12 +22,14 @@
from . import math
from ._base import *
from ._base import __all__ as _base_all
from ._celsius import *
from ._celsius import __all__ as _celsius_all
from ._unit_common import *
from ._unit_common import __all__ as _common_all
from ._unit_constants import *
from ._unit_constants import __all__ as _constants_all
from ._unit_shortcuts import *
from ._unit_shortcuts import __all__ as _std_units_all

__all__ = ['math'] + _common_all + _std_units_all + _constants_all + _base_all
del _common_all, _std_units_all, _constants_all, _base_all
__all__ = ['math'] + _common_all + _std_units_all + _constants_all + _base_all + _celsius_all
del _common_all, _std_units_all, _constants_all, _base_all, _celsius_all
64 changes: 64 additions & 0 deletions brainunit/_celsius.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Copyright 2024 BDP Ecosystem Limited. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================


import jax.typing

from ._base import Quantity
from ._unit_common import kelvin

__all__ = [
"celsius2kelvin",
"kelvin2celsius",
]


def celsius2kelvin(celsius: jax.typing.ArrayLike) -> Quantity:
"""
Convert Celsius value to kelvin value.

Parameters
----------
celsius : jax.typing.ArrayLike
The celsius value to convert.

Returns
-------
Quantity: The converted value.

"""
if isinstance(celsius, Quantity):
raise TypeError("The input value should be not be a Quantity.")
return (celsius + 273.15) * kelvin


def kelvin2celsius(value: Quantity) -> jax.typing.ArrayLike:
"""
Convert kelvin value to Celsius value.

Parameters
----------
value : Quantity
The kelvin value to convert.

Returns
-------
Quantity: The converted value.

"""
if not isinstance(value, Quantity) and value.unit != kelvin:
raise TypeError("The input value should be a Quantity with kelvin.")
return value.mantissa - 273.15

33 changes: 33 additions & 0 deletions brainunit/_celsius_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright 2024 BDP Ecosystem Limited. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================


import brainunit as u


def test1():
a = u.celsius2kelvin(0)
assert a == 273.15 * u.kelvin

b = u.celsius2kelvin(-100)
assert b == 173.15 * u.kelvin


def test2():
a = u.kelvin2celsius(273.15 * u.kelvin)
assert a == 0

b = u.kelvin2celsius(173.15 * u.kelvin)
assert b == -100
6 changes: 5 additions & 1 deletion brainunit/math/_fun_accept_unitless.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def exp2(
unit_to_scale: Optional[Unit] = None,
) -> jax.Array:
"""
Calculate 2**p for all p in the input quantity or array.
Calculate ``2**p`` for all p in the input quantity or array.

Parameters
----------
Expand All @@ -192,6 +192,8 @@ def expm1(
"""
Calculate the exponential of the input elements minus 1.

Calculate ``exp(x) - 1`` for all elements in the array.

Parameters
----------
x : array_like, Quantity
Expand Down Expand Up @@ -261,6 +263,8 @@ def log1p(
"""
Natural logarithm of 1 + the input elements.

Calculates ``log(1 + x)``.

Parameters
----------
x : array_like, Quantity
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ See also the BDP ecosystem
^^^^^^^^^^^^^^^^^^^^^^^^^^


We are building the `BDP ecosystem <https://ecosystem-for-brain-dynamics.readthedocs.io/>`_:
We are building the `BDP ecosystem <https://ecosystem-for-brain-dynamics.readthedocs.io/>`_.



Expand Down