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

Fix interpolation problem with ints #189

Merged
merged 4 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.swp
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did this end up here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because I edit these files using vim.
I know that I should have this in global gitingore, but I think we should have default configuration fits for most editors.

I could revert this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fine by me to leave it in, but I think I already have this in my global ignore :P

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
19 changes: 16 additions & 3 deletions napari_animation/_tests/test_interpolation.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import numbers
from dataclasses import asdict
from typing import NamedTuple

Expand All @@ -14,17 +15,29 @@
from napari_animation.interpolation.utils import nested_assert_close


def _expected_type(a, b):
if isinstance(a, numbers.Integral) and isinstance(b, numbers.Real):
return type(b)
return type(a)


# Actual tests
@pytest.mark.parametrize("a", [0.0, 0])
@pytest.mark.parametrize("b", [100.0, 100])
@pytest.mark.parametrize("a", [0.0, 0, np.float32(0)])
@pytest.mark.parametrize("b", [100.0, 100, np.float32(100)])
@pytest.mark.parametrize("fraction", [0, 0.0, 0.5, 1.0, 1])
def test_interpolate_num(a, b, fraction):
"""Check that interpolation of numbers produces valid output"""
result = interpolate_num(a, b, fraction)
assert isinstance(result, type(a))
assert isinstance(result, _expected_type(a, b))
assert result == fraction * b


@pytest.mark.parametrize("b", [1.0, np.float32(1)])
def test_interpolate_proper_type(b):
result = interpolate_num(0, b, 0.5)
assert np.isclose(result, 0.5)


@pytest.mark.parametrize("a,b", [([0.0, 0.0, 0.0], [1.0, 1.0, 1.0])])
@pytest.mark.parametrize(
"fraction,expected",
Expand Down
4 changes: 3 additions & 1 deletion napari_animation/interpolation/base_interpolation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from numbers import Number
from numbers import Number, Real, Integral
from typing import Sequence, Tuple, TypeVar
psobolewskiPhD marked this conversation as resolved.
Show resolved Hide resolved

import numpy as np
Expand Down Expand Up @@ -85,6 +85,8 @@ def interpolate_num(a: Number, b: Number, fraction: float) -> Number:
Interpolated value between a and b at fraction.
"""
number_cls = type(a)
if isinstance(b, Real) and isinstance(a, Integral):
number_cls = type(b)
return number_cls(a + (b - a) * fraction)


Expand Down
Loading