Skip to content

Commit

Permalink
Fix length computation sympy (#770)
Browse files Browse the repository at this point in the history
* use scipy for advanced integration methods inside MathematicalExpression. Fixes #725
* catch UnitStrippedWarning as it is expected during this kind of computation.
* try to use both modules (get rid of deprecationwarnings)

* Silence DeepSource
Co-authored-by: vhirtham <20574817+vhirtham@users.noreply.github.com>
  • Loading branch information
marscher authored Jun 13, 2022
1 parent eac3ca0 commit bea885f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
Release Notes
###############

********************
0.6.2 (tba)
********************

fixes
=====

- `MathematicalExpression` now uses SciPy and NumPy in numerical function evaluation. This enables it to use
advanced integration methods and fixes lengths computation of `DynamicShapeSegment` [:pull:`770`].

********************
0.6.1 (19.05.2022)
********************
Expand Down
2 changes: 1 addition & 1 deletion weldx/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def __init__(
self._expression: sympy.Expr = expression

self.function = sympy.lambdify(
tuple(self._expression.free_symbols), self._expression, "numpy"
tuple(self._expression.free_symbols), self._expression, ("numpy", "scipy")
)

self._parameters: dict[str, Union[pint.Quantity, xr.DataArray]] = {}
Expand Down
10 changes: 9 additions & 1 deletion weldx/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import annotations

import copy
import warnings
from dataclasses import InitVar, dataclass
from pathlib import Path
from typing import TYPE_CHECKING, Union
Expand Down Expand Up @@ -211,7 +212,14 @@ def get_section_length(self, position: float) -> pint.Quantity:
"""
if self._series.is_expression:
length = self._length_expr.evaluate(max_coord=position).data
# ignore unit stripped warning, as it is expected.
# Note, that the result still has the unit,
# it is stripped during computation (scipy.integrate)
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore", category=pint.errors.UnitStrippedWarning
)
length = self._length_expr.evaluate(max_coord=position).data
else:
length = self._len_section_disc(position=position)
if length <= 0:
Expand Down
16 changes: 15 additions & 1 deletion weldx/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,20 @@ def test_evaluate_exceptions(ma_def, variables, exception_type, test_name):
with pytest.raises(exception_type):
ma_def.evaluate(**variables)

@staticmethod
def test_integrate_length_computation():
"""Ensure we can integrate with Sympy during length computation."""
from weldx import DynamicShapeSegment

class MySegment(DynamicShapeSegment):
def __init__(self):
f = "x * sin(s) + y * s"
p = dict(x=Q_([1, 0, 0], "mm"), y=Q_([0, 1, 0], "mm"))
super().__init__(f, parameters=p)

s = MySegment()
assert s.get_section_length(1).u == Q_("mm")


# --------------------------------------------------------------------------------------
# TimeSeries
Expand Down Expand Up @@ -468,7 +482,7 @@ def test_interp_time_warning():
@pytest.mark.parametrize(
"time, exception_type, test_name",
[
# (DTI(["2010-10-10"]), ValueError, "# wrong type #1"),
# (DTI(["2010-10-10"]), ValueError, "# wrong type #1"), # skipcq: PY-W0069
("a string", TypeError, "# wrong type #2"),
([1, 2, 3], TypeError, "# wrong type #3"),
(1, TypeError, "# wrong type #4"),
Expand Down

0 comments on commit bea885f

Please sign in to comment.