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

Make typing-extensions and sympy required dependencies to fix PyBaMM import #3848

Merged
merged 13 commits into from
Mar 5, 2024
Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

- Renamed "testing" argument for plots to "show_plot" and flipped its meaning (show_plot=True is now the default and shows the plot) ([#3842](https://github.com/pybamm-team/PyBaMM/pull/3842))
- Dropped support for BPX version 0.3.0 and below ([#3414](https://github.com/pybamm-team/PyBaMM/pull/3414))
- Integrated the `[latexify]` extra into the core PyBaMM package, deprecating the `pybamm[latexify]` set of optional dependencies. SymPy is now a required dependency and will be installed upon installing PyBaMM ([#3848](https://github.com/pybamm-team/PyBaMM/pull/3848))
agriyakhetarpal marked this conversation as resolved.
Show resolved Hide resolved

# [v24.1](https://github.com/pybamm-team/PyBaMM/tree/v24.1) - 2024-01-31

Expand Down
33 changes: 11 additions & 22 deletions docs/source/user_guide/installation/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,17 @@ Required dependencies

PyBaMM requires the following dependencies.

================================================================ ==========================
Package Minimum supported version
================================================================ ==========================
`NumPy <https://numpy.org>`__ 1.23.5
`SciPy <https://docs.scipy.org/doc/scipy/>`__ 1.9.3
`CasADi <https://web.casadi.org/docs/>`__ 3.6.3
`Xarray <https://docs.xarray.dev/en/stable/>`__ 2022.6.0
`Anytree <https://anytree.readthedocs.io/en/stable/>`__ 2.8.0
================================================================ ==========================
=================================================================== ==========================
Package Minimum supported version
=================================================================== ==========================
`NumPy <https://numpy.org>`__ 1.23.5
`SciPy <https://docs.scipy.org/doc/scipy/>`__ 1.9.3
`CasADi <https://web.casadi.org/docs/>`__ 3.6.3
`Xarray <https://docs.xarray.dev/en/stable/>`__ 2022.6.0
`Anytree <https://anytree.readthedocs.io/en/stable/>`__ 2.8.0
`SymPy <https://docs.sympy.org/latest/index.html>`__ 1.9.3
`typing-extensions <https://pypi.org/project/typing-extensions/>`__ 4.10.0
=================================================================== ==========================

.. _install.optional_dependencies:

Expand Down Expand Up @@ -174,19 +176,6 @@ Dependency Minimum Version p
`pybtex <https://docs.pybtex.org/>`__ 0.24.0 cite BibTeX-compatible bibliography processor.
=========================================================== ================== ================== =========================================

.. _install.latexify_dependencies:

Latexify dependencies
^^^^^^^^^^^^^^^^^^^^^

Installable with ``pip install "pybamm[latexify]"``

=========================================================== ================== ================== =========================
Dependency Minimum Version pip extra Notes
=========================================================== ================== ================== =========================
`sympy <https://docs.sympy.org/latest/index.html>`__ 1.9.3 latexify For symbolic mathematics.
=========================================================== ================== ================== =========================

.. _install.bpx_dependencies:

bpx dependencies
Expand Down
7 changes: 1 addition & 6 deletions pybamm/expression_tree/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@
from __future__ import annotations
import numpy as np
from scipy.sparse import csr_matrix, issparse
from typing import TYPE_CHECKING

import pybamm
from pybamm.util import have_optional_dependency
from pybamm.type_definitions import DomainType, AuxiliaryDomainType, DomainsType

if TYPE_CHECKING: # pragma: no cover
import sympy
import sympy


class Array(pybamm.Symbol):
Expand Down Expand Up @@ -157,7 +153,6 @@ def is_constant(self):

def to_equation(self) -> sympy.Array:
"""Returns the value returned by the node when evaluated."""
sympy = have_optional_dependency("sympy")
entries_list = self.entries.tolist()
return sympy.Array(entries_list)

Expand Down
6 changes: 1 addition & 5 deletions pybamm/expression_tree/binary_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import numbers

import numpy as np
import sympy
from scipy.sparse import csr_matrix, issparse
import functools

import pybamm
from pybamm.util import have_optional_dependency

from typing import Callable, cast

Expand Down Expand Up @@ -180,7 +180,6 @@ def _sympy_operator(self, left, right):

def to_equation(self):
"""Convert the node and its subtree into a SymPy equation."""
sympy = have_optional_dependency("sympy")
if self.print_name is not None:
return sympy.Symbol(self.print_name)
else:
Expand Down Expand Up @@ -388,7 +387,6 @@ def _binary_evaluate(self, left, right):

def _sympy_operator(self, left, right):
"""Override :meth:`pybamm.BinaryOperator._sympy_operator`"""
sympy = have_optional_dependency("sympy")
left = sympy.Matrix(left)
right = sympy.Matrix(right)
return left * right
Expand Down Expand Up @@ -737,7 +735,6 @@ def _binary_new_copy(

def _sympy_operator(self, left, right):
"""Override :meth:`pybamm.BinaryOperator._sympy_operator`"""
sympy = have_optional_dependency("sympy")
return sympy.Min(left, right)


Expand Down Expand Up @@ -782,7 +779,6 @@ def _binary_new_copy(

def _sympy_operator(self, left, right):
"""Override :meth:`pybamm.BinaryOperator._sympy_operator`"""
sympy = have_optional_dependency("sympy")
return sympy.Max(left, right)


Expand Down
3 changes: 1 addition & 2 deletions pybamm/expression_tree/concatenations.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
from collections import defaultdict

import numpy as np
import sympy
from scipy.sparse import issparse, vstack
from typing import Sequence

import pybamm
from pybamm.util import have_optional_dependency


class Concatenation(pybamm.Symbol):
Expand Down Expand Up @@ -159,7 +159,6 @@ def is_constant(self):

def _sympy_operator(self, *children):
"""Apply appropriate SymPy operators."""
sympy = have_optional_dependency("sympy")
self.concat_latex = tuple(map(sympy.latex, children))

if self.print_name is not None:
Expand Down
5 changes: 1 addition & 4 deletions pybamm/expression_tree/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import numpy as np
from scipy import special
import sympy
from typing import Sequence, Callable
from typing_extensions import TypeVar

Expand Down Expand Up @@ -210,7 +211,6 @@ def _sympy_operator(self, child):

def to_equation(self):
"""Convert the node and its subtree into a SymPy equation."""
sympy = have_optional_dependency("sympy")
if self.print_name is not None:
return sympy.Symbol(self.print_name)
else:
Expand Down Expand Up @@ -275,7 +275,6 @@ def _function_new_copy(self, children):

def _sympy_operator(self, child):
"""Apply appropriate SymPy operators."""
sympy = have_optional_dependency("sympy")
class_name = self.__class__.__name__.lower()
sympy_function = getattr(sympy, class_name)
return sympy_function(child)
Expand Down Expand Up @@ -332,7 +331,6 @@ def _function_diff(self, children, idx):

def _sympy_operator(self, child):
"""Override :meth:`pybamm.Function._sympy_operator`"""
sympy = have_optional_dependency("sympy")
return sympy.asinh(child)


Expand Down Expand Up @@ -360,7 +358,6 @@ def _function_diff(self, children, idx):

def _sympy_operator(self, child):
"""Override :meth:`pybamm.Function._sympy_operator`"""
sympy = have_optional_dependency("sympy")
return sympy.atan(child)


Expand Down
3 changes: 0 additions & 3 deletions pybamm/expression_tree/independent_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import numpy as np

import pybamm
from pybamm.util import have_optional_dependency
from pybamm.type_definitions import DomainType, AuxiliaryDomainType, DomainsType

KNOWN_COORD_SYS = ["cartesian", "cylindrical polar", "spherical polar"]
Expand Down Expand Up @@ -58,7 +57,6 @@ def _jac(self, variable) -> pybamm.Scalar:

def to_equation(self) -> sympy.Symbol:
"""Convert the node and its subtree into a SymPy equation."""
sympy = have_optional_dependency("sympy")
if self.print_name is not None:
return sympy.Symbol(self.print_name)
else:
Expand Down Expand Up @@ -102,7 +100,6 @@ def _evaluate_for_shape(self):

def to_equation(self):
"""Convert the node and its subtree into a SymPy equation."""
sympy = have_optional_dependency("sympy")
return sympy.Symbol("t")


Expand Down
5 changes: 1 addition & 4 deletions pybamm/expression_tree/operations/latexify.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import pybamm
from pybamm.expression_tree.printing.sympy_overrides import custom_print_func
from pybamm.util import have_optional_dependency
import sympy


def get_rng_min_max_name(rng, min_or_max):
Expand Down Expand Up @@ -89,7 +89,6 @@ def _get_bcs_displays(self, var):
Returns a list of boundary condition equations with ranges in front of
the equations.
"""
sympy = have_optional_dependency("sympy")
bcs_eqn_list = []
bcs = self.model.boundary_conditions.get(var, None)

Expand Down Expand Up @@ -120,7 +119,6 @@ def _get_bcs_displays(self, var):

def _get_param_var(self, node):
"""Returns a list of parameters and a list of variables."""
sympy = have_optional_dependency("sympy")
param_list = []
var_list = []
dfs_nodes = [node]
Expand Down Expand Up @@ -163,7 +161,6 @@ def _get_param_var(self, node):
return param_list, var_list

def latexify(self, output_variables=None):
sympy = have_optional_dependency("sympy")
# Voltage is the default output variable if it exists
if output_variables is None:
if "Voltage [V]" in self.model.variables:
Expand Down
8 changes: 2 additions & 6 deletions pybamm/expression_tree/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
import sys

import numpy as np
from typing import TYPE_CHECKING, Literal
from typing import Literal

if TYPE_CHECKING: # pragma: no cover
import sympy
import sympy

import pybamm
from pybamm.util import have_optional_dependency


class Parameter(pybamm.Symbol):
Expand Down Expand Up @@ -48,7 +46,6 @@ def is_constant(self) -> Literal[False]:

def to_equation(self) -> sympy.Symbol:
"""Convert the node and its subtree into a SymPy equation."""
sympy = have_optional_dependency("sympy")
if self.print_name is not None:
return sympy.Symbol(self.print_name)
else:
Expand Down Expand Up @@ -244,7 +241,6 @@ def _evaluate_for_shape(self):

def to_equation(self) -> sympy.Symbol:
"""Convert the node and its subtree into a SymPy equation."""
sympy = have_optional_dependency("sympy")
if self.print_name is not None:
return sympy.Symbol(self.print_name)
else:
Expand Down
3 changes: 1 addition & 2 deletions pybamm/expression_tree/scalar.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
#
from __future__ import annotations
import numpy as np
import sympy
from typing import Literal

import pybamm
from pybamm.util import have_optional_dependency
from pybamm.type_definitions import Numeric


Expand Down Expand Up @@ -87,7 +87,6 @@ def is_constant(self) -> Literal[True]:

def to_equation(self):
"""Returns the value returned by the node when evaluated."""
sympy = have_optional_dependency("sympy")
if self.print_name is not None:
return sympy.Symbol(self.print_name)
else:
Expand Down
2 changes: 1 addition & 1 deletion pybamm/expression_tree/symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import numbers

import numpy as np
import sympy
from scipy.sparse import csr_matrix, issparse
from functools import lru_cache, cached_property
from typing import TYPE_CHECKING, Sequence, cast
Expand Down Expand Up @@ -1056,7 +1057,6 @@ def print_name(self, name):
self._print_name = prettify_print_name(name)

def to_equation(self):
sympy = have_optional_dependency("sympy")
return sympy.Symbol(str(self.name))

def to_json(self):
Expand Down
4 changes: 1 addition & 3 deletions pybamm/expression_tree/unary_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import numpy as np
from scipy.sparse import csr_matrix, issparse
import sympy
import pybamm
from pybamm.util import have_optional_dependency
from pybamm.type_definitions import DomainsType
Expand Down Expand Up @@ -108,7 +109,6 @@ def _sympy_operator(self, child):

def to_equation(self):
"""Convert the node and its subtree into a SymPy equation."""
sympy = have_optional_dependency("sympy")
if self.print_name is not None:
return sympy.Symbol(self.print_name)
else:
Expand Down Expand Up @@ -672,7 +672,6 @@ def _evaluates_on_edges(self, dimension: str) -> bool:

def _sympy_operator(self, child):
"""Override :meth:`pybamm.UnaryOperator._sympy_operator`"""
sympy = have_optional_dependency("sympy")
return sympy.Integral(child, sympy.Symbol("xn"))


Expand Down Expand Up @@ -996,7 +995,6 @@ def _unary_new_copy(self, child):

def _sympy_operator(self, child):
"""Override :meth:`pybamm.UnaryOperator._sympy_operator`"""
sympy = have_optional_dependency("sympy")
if (
self.child.domain[0] in ["negative particle", "positive particle"]
and self.side == "right"
Expand Down
3 changes: 1 addition & 2 deletions pybamm/expression_tree/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import numpy as np
import numbers
import pybamm
from pybamm.util import have_optional_dependency
import sympy
from pybamm.type_definitions import (
DomainType,
AuxiliaryDomainType,
Expand Down Expand Up @@ -135,7 +135,6 @@ def _evaluate_for_shape(self):

def to_equation(self):
"""Convert the node and its subtree into a SymPy equation."""
sympy = have_optional_dependency("sympy")
if self.print_name is not None:
return sympy.Symbol(self.print_name)
else:
Expand Down
3 changes: 1 addition & 2 deletions pybamm/models/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import pybamm
from pybamm.expression_tree.operations.serialise import Serialise
from pybamm.util import have_optional_dependency
import sympy


class BaseModel:
Expand Down Expand Up @@ -1185,7 +1185,6 @@ def latexify(self, filename=None, newline=True, output_variables=None):
This will return first five model equations
>>> model.latexify(newline=False)[1:5]
"""
sympy = have_optional_dependency("sympy")
if sympy:
from pybamm.expression_tree.operations.latexify import Latexify

Expand Down
6 changes: 2 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ dependencies = [
"casadi>=3.6.3",
"xarray>=2022.6.0",
"anytree>=2.8.0",
"sympy>=1.12",
"typing-extensions>=4.10.0",
]

[project.urls]
Expand Down Expand Up @@ -86,10 +88,6 @@ plot = [
cite = [
"pybtex>=0.24.0",
]
# To generate LaTeX strings
latexify = [
"sympy>=1.12",
]
# Battery Parameter eXchange format
bpx = [
"bpx>=0.4.0",
Expand Down
Loading
Loading