Commit 6dfea59b authored by Baptiste Esteban's avatar Baptiste Esteban
Browse files

Starting working on tests

parent d3029fcb
Pipeline #25609 passed with stages
in 19 minutes and 17 seconds
......@@ -43,6 +43,6 @@ add_subdirectory(morpho)
add_subdirectory(labeling)
add_subdirectory(contrib)
add_subdirectory(bp)
if (pybind11_FOUND AND PYLENE_PYTHON)
if (PYLENE_BUILD_PYTHON)
add_subdirectory(python)
endif()
\ No newline at end of file
find_package(pybind11 REQUIRED)
pybind11_find_import(numpy REQUIRED)
# Python module for testing
set(PYTHON_PYLENE_SRC ${CMAKE_SOURCE_DIR}/pylene-python/src)
pybind11_add_module(pylena_test pylena_test.cpp
${PYTHON_PYLENE_SRC}/core/image_cast.cpp
${PYTHON_PYLENE_SRC}/core/numpy_format.cpp)
target_link_libraries(pylena_test PRIVATE Pylene ImagePath)
target_include_directories(pylena_test PRIVATE ${CMAKE_SOURCE_DIR}/pylene-python/include)
function(add_python_test filename)
file(COPY ${filename} DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
endfunction(add_python_test)
......@@ -7,4 +18,4 @@ add_python_test(test_pylena_numpy.py)
add_test(NAME test_python
COMMAND ${PYTHON_EXECUTABLE} -m unittest discover
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/tests/python)
\ No newline at end of file
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
\ No newline at end of file
#include <pln/core/image_cast.hpp>
#include <mln/core/image/ndimage.hpp>
#include <mln/core/range/foreach.hpp>
#include <fixtures/ImagePath/image_path.hpp>
#include <pybind11/pybind11.h>
#include <tuple>
namespace pln::test
{
/// \brief Check a specific array for the conversion NumPy -> ndbuffer_image
/// \param[in] img An image created by numpy with specific values and parameters
/// \return A boolean indicating if the convertion worked
auto check_from_numpy(const mln::ndbuffer_image& img)
{
bool res = true;
res = res && img.sample_type() == mln::sample_type_id::INT32;
res = res && img.pdim() == 2;
res = res && img.size(0) == 3;
res = res && img.size(1) == 4;
res = res && img.byte_stride(0) == 4;
res = res && img.byte_stride(1) == 12;
std::int32_t i = 0;
auto casted = img.__cast<std::int32_t, 2>();
mln_foreach (auto p, casted.domain())
res = res && casted(p) == (i++);
return res;
}
/// \brief Check a specific array for the conversion NumPy (fortran order) -> ndbuffer_image
/// \param[in] img An image created by numpy with specific values and parameters
/// \return A boolean indicating if the convertion worked
auto check_from_numpy_f(const mln::ndbuffer_image& img)
{
bool res = true;
res = res && img.sample_type() == mln::sample_type_id::INT32;
res = res && img.pdim() == 2;
res = res && img.size(0) == 3;
res = res && img.size(1) == 4;
res = res && img.byte_stride(0) == 16;
res = res && img.byte_stride(1) == 4;
//std::int32_t i = 0;
auto casted = img.__cast<std::int32_t, 2>();
mln_foreach (auto p, casted.domain())
//res = res && casted(p) == (i++);
pybind11::print(casted(p));
return res;
}
PYBIND11_MODULE(pylena_test, m)
{
pybind11::module_::import("pylena");
// Utility functions
m.def("id", [](mln::ndbuffer_image img) { return img; });
m.def("get_image_path", [](const std::string& name) { return fixtures::ImagePath::concat_with_filename(name); });
// Checking functions
m.def("check_from_numpy", &check_from_numpy);
m.def("check_from_numpy_f", &check_from_numpy_f);
}
} // namespace pln::test
\ No newline at end of file
import sys
sys.path.append("../../pylene-python")
import unittest
import numpy as np
from imageio import imread
import sys
sys.path.append("../../pylene")
import pylena as pln
import pylena_test as pln
class TestNumpyImage(unittest.TestCase):
def _test_equal(self, arr, arr2):
self.assertTrue(arr.ndim == arr2.ndim)
for i in range(arr.ndim):
self.assertTrue(arr.shape[i] == arr2.shape[i])
self.assertTrue(arr.strides[i] == arr2.strides[i])
self.assertTrue(arr.data == arr2.data)
self.assertTrue(np.all(arr == arr2))
def test_lena_grayscale(self):
img = pln.io.imread(pln.utils.get_image_filename("lena.pgm"))
ref = imread(pln.utils.get_image_filename("lena.pgm"))
self.assertTrue(np.all(img == ref))
def test_lena_color(self):
img = pln.io.imread(pln.utils.get_image_filename("lena.ppm"))
ref = imread(pln.utils.get_image_filename("lena.ppm"))
self.assertTrue(np.all(img == ref))
def test_id(self):
arr = np.arange(12).reshape((3, 4))
arr2 = pln.utils.id(arr)
self._test_equal(arr, arr2)
arr3 = pln.io.imread(pln.utils.get_image_filename("lena.pgm"))
arr4 = pln.utils.id(arr3)
self._test_equal(arr3, arr4)
def test_id_fortran_contiguous(self):
arr = np.arange(12).reshape((3, 4))
arr2 = arr.T
arr3 = pln.utils.id(arr2)
self._test_equal(arr2, arr3)
def test_from_numpy_uint8(self):
arr = np.array([[0, 0, 0, 1],
[0, 1, 0, 1],
[0, 0, 0, 1]]).astype(np.uint8)
res = pln.id(arr)
self.assertTrue(arr.ndim == res.ndim == 2)
self.assertTrue(arr.shape[0] == res.shape[0] == 3)
self.assertTrue(arr.shape[1] == res.shape[1] == 4)
self.assertTrue(arr.strides[0] == res.strides[0] == 4)
self.assertTrue(arr.strides[1] == res.strides[1] == 1)
self.assertTrue(arr.dtype == res.dtype == np.uint8)
self.assertTrue(arr.data == res.data)
def test_from_numpy_uint64(self):
import platform
if not platform.architecture()[0] == '64bit':
return
arr = np.array([[0, 0, 0, 1],
[0, 1, 0, 1],
[0, 0, 0, 1]]).astype(np.uint64)
res = pln.id(arr)
self.assertTrue(arr.ndim == res.ndim == 2)
self.assertTrue(arr.shape[0] == res.shape[0] == 3)
self.assertTrue(arr.shape[1] == res.shape[1] == 4)
self.assertTrue(arr.strides[0] == res.strides[0] == 32)
self.assertTrue(arr.strides[1] == res.strides[1] == 8)
self.assertTrue(arr.dtype == res.dtype == np.uint64)
self.assertTrue(arr.data == res.data)
def test_from_numpy_fortran_order(self):
arr = np.array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[9, 10, 11]], order="F").astype(np.uint8)
res = pln.id(arr)
self.assertTrue(arr.ndim == res.ndim == 2)
self.assertTrue(arr.shape[0] == res.shape[0] == 4)
self.assertTrue(arr.shape[1] == res.shape[1] == 3)
self.assertTrue(arr.strides[0] == res.strides[0] == 1)
self.assertTrue(arr.strides[1] == res.strides[1] == 4)
self.assertTrue(arr.dtype == res.dtype == np.uint8)
self.assertTrue(arr.data == res.data)
def test_from_numpy_checked(self):
arr = np.arange(12).reshape((4, 3)).astype(np.int32)
self.assertTrue(pln.check_from_numpy(arr))
# Fail because of ndimage point access optimization (ndbuffer_image_impl<T, pdim>::get_pointer)
#def test_from_numpy_f_checked(self):
# arr = np.asfortranarray(np.arange(12).reshape((4, 3)).astype(np.int32))
# self.assertTrue(pln.check_from_numpy_f(arr))
if __name__ == "__main__":
unittest.main()
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment