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

keep dtype when pushing #206

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion pyclesperanto_prototype/_tier0/_array_operators.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np

cl_buffer_datatype_dict = {
np.bool: "bool",
bool: "bool",
np.uint8: "uchar",
np.uint16: "ushort",
np.uint32: "uint",
Expand Down
16 changes: 14 additions & 2 deletions pyclesperanto_prototype/_tier0/_push.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import warnings

import numpy as np
from ._backends import Backend

Expand Down Expand Up @@ -34,11 +36,21 @@ def push(any_array):
if hasattr(any_array, 'shape') and hasattr(any_array, 'dtype') and hasattr(any_array, 'get'):
any_array = np.asarray(any_array.get())

return Backend.get_instance().get().from_array(np.float32(any_array))
not_supported_types = {
np.float64: np.float32,
np.int64: np.int32,
np.uint64: np.int64
}
if any_array.dtype in not_supported_types.keys():
replacement_type = not_supported_types[any_array.dtype]
warnings.warn(f"dtype {any_array.dtype} not supported, converting to {replacement_type} instead")
any_array = any_array.astype(replacement_type)

return Backend.get_instance().get().from_array(any_array)


def push_zyx(any_array):
import warnings

warnings.warn(
"Deprecated: `push_zyx()` is now deprecated as it does the same as `push()`.",
DeprecationWarning
Expand Down
14 changes: 6 additions & 8 deletions tests/test_apply_vector_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ def test_apply_vector_field_3d():
assert (np.array_equal(a, b))

import pytest
import pyopencl as cl

from . import LINUX, CI

@pytest.mark.xfail('LINUX and CI', reason='clImages not supported on CI', raises=ValueError)
Expand Down Expand Up @@ -93,9 +91,9 @@ def test_apply_vector_field_3d_linear_interpolation():

reference = cle.push(np.asarray([[
[0, 0, 0, 0, 0],
[0, 1, 1, 0.5, 0],
[0, 1.5, 1, 0.5, 0],
[0, 1, 1, 0.5, 0],
[0, 1, 1, 0, 0],
[0, 2, 1, 0, 0],
[0, 1, 1, 0, 0],
[0, 0, 0, 0, 0],
]]))

Expand Down Expand Up @@ -183,9 +181,9 @@ def test_apply_vector_field_2d_linear_interpolation():

reference = cle.push(np.asarray([
[0, 0, 0, 0, 0],
[0, 1, 1, 0.5, 0],
[0, 1.5, 1, 0.5, 0],
[0, 1, 1, 0.5, 0],
[0, 1, 1, 0, 0],
[0, 2, 1, 0, 0],
[0, 1, 1, 0, 0],
[0, 0, 0, 0, 0],
]))

Expand Down
6 changes: 3 additions & 3 deletions tests/test_convolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ def test_convolve():

a = cle.pull(test1)
b = cle.pull(test2)
assert (np.min(a) == np.min(b))
assert (np.max(a) == np.max(b))
assert (np.mean(a) == np.mean(b))
assert abs(np.min(a) - np.min(b)) < 0.0001
assert abs(np.max(a) - np.max(b)) < 0.0001
assert abs(np.mean(a) - np.mean(b)) < 0.0001
15 changes: 15 additions & 0 deletions tests/test_push.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np
import pyclesperanto_prototype as cle
import pytest

def test_push_np():
reference = np.asarray([
Expand All @@ -13,6 +14,20 @@ def test_push_np():

assert np.allclose(result, reference)

@pytest.mark.parametrize("dtype", [np.float32, np.uint8, np.int8, np.uint16, np.int16, np.uint32, np.int32, np.uint64, np.int64])
def test_push_np_dtypes(dtype):
reference = np.asarray([
[1, 2],
[-3, 4]
]).astype(dtype)

image = cle.push(reference)

result = cle.pull(image)

assert np.allclose(result, reference)
assert result.dtype == reference.dtype


def test_push_list():
reference = [
Expand Down