Skip to content

Commit

Permalink
Refactor Python unit/perf test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
gigony committed Jul 4, 2021
1 parent 3257d96 commit b91d30f
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 103 deletions.
8 changes: 8 additions & 0 deletions python/cucim/requirements-test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
GPUtil==1.4.0
imagecodecs==2021.6.8
openslide-python==1.1.2
psutil==5.8.0
pytest==6.2.4
pytest-cov==2.12.1
pytest-lazy-fixture==0.6.3
tifffile==2021.7.2
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@
#

import pytest


def open_image_cucim(file_path):
from cucim import CuImage
img = CuImage(file_path)
return img
from ...util.io import open_image_cucim


def test_read_region_cuda_memleak(testimg_tiff_stripe_4096x4096_256):
Expand Down Expand Up @@ -69,9 +64,9 @@ def test_read_region_cpu_memleak(testimg_tiff_stripe_4096x4096_256):


def test_read_random_region_cpu_memleak(testimg_tiff_stripe_4096x4096_256):
import random
import os
import psutil
import random
process = psutil.Process(os.getpid())

img = open_image_cucim(testimg_tiff_stripe_4096x4096_256)
Expand Down
7 changes: 1 addition & 6 deletions python/cucim/tests/unit/clara/test_load_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@
#

import pytest


def open_image_cucim(file_path):
from cucim import CuImage
img = CuImage(file_path)
return img
from ...util.io import open_image_cucim


def test_load_non_existing_image():
Expand Down
6 changes: 1 addition & 5 deletions python/cucim/tests/unit/clara/test_load_image_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@
# limitations under the License.
#


def open_image_cucim(file_path):
from cucim import CuImage
img = CuImage(file_path)
return img
from ...util.io import open_image_cucim


def test_load_image_metadata(testimg_tiff_stripe_32x24_16):
Expand Down
65 changes: 28 additions & 37 deletions python/cucim/tests/unit/clara/test_tiff_read_region.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@

import numpy as np
import pytest


def open_image_cucim(file_path):
from cucim import CuImage
img = CuImage(file_path)
return img
from ...util.io import open_image_cucim


def test_tiff_stripe_inner(testimg_tiff_stripe_32x24_16):
Expand All @@ -39,11 +34,9 @@ def test_tiff_stripe_inner(testimg_tiff_stripe_32x24_16):
for (start_pos, size) in region_list:
cucim_arr = np.asarray(cucim_img.read_region(start_pos, size))

# Not channel values are zero, so we need to check that.
channel_value_count = np.apply_along_axis(np.count_nonzero, 2,
cucim_arr)
count_all_zero = np.count_nonzero(channel_value_count == 0)
assert count_all_zero == 0
# Not all channel values are zero, so we need to check that.
channel_value_count = np.count_nonzero(cucim_arr, axis=2)
assert np.all(channel_value_count > 0)


def test_tiff_stripe_boundary(testimg_tiff_stripe_32x24_16):
Expand All @@ -64,9 +57,8 @@ def test_tiff_stripe_boundary(testimg_tiff_stripe_32x24_16):

for (start_pos, size) in region_list:
cucim_arr = np.asarray(cucim_img.read_region(start_pos, size))
# Not channel values are zero, so we need to check that.
channel_value_count = np.apply_along_axis(np.count_nonzero, 2,
cucim_arr)
# Not all channel values are zero, so we need to check that.
channel_value_count = np.count_nonzero(cucim_arr, axis=2)
count_all_zero = np.count_nonzero(channel_value_count == 0)
# 75% of the pixels would be all zero
assert count_all_zero - (tile_width * tile_height * 0.75) < 5
Expand All @@ -87,8 +79,7 @@ def test_tiff_stripe_outside(testimg_tiff_stripe_32x24_16):
for (start_pos, size) in region_list:
cucim_arr = np.asarray(cucim_img.read_region(start_pos, size))
# All channel values should be zero, so we need to check that.
channel_value_count = np.apply_along_axis(np.count_nonzero, 2,
cucim_arr)
channel_value_count = np.count_nonzero(cucim_arr, axis=2)
count_all_zero = np.count_nonzero(channel_value_count == 0)
# All pixels would be zero.
assert count_all_zero == (tile_width * tile_height)
Expand All @@ -112,9 +103,8 @@ def test_tiff_stripe_multiresolution(testimg_tiff_stripe_4096x4096_256):
start_pos, size = ((0, 0), (256, 256))
for level in range(level_count):
cucim_arr = np.asarray(cucim_img.read_region(start_pos, size, level))
# Not channel values are zero, so we need to check that.
channel_value_count = np.apply_along_axis(np.count_nonzero, 2,
cucim_arr)
# Not all channel values are zero, so we need to check that.
channel_value_count = np.count_nonzero(cucim_arr, axis=2)
count_all_zero = np.count_nonzero(channel_value_count == 0)
img_size = cucim_img.resolutions['level_dimensions'][level]
# Only outside of the box is zero.
Expand All @@ -137,21 +127,22 @@ def test_array_interface_support(testimg_tiff_stripe_32x24_16_jpeg):
assert array_interface['shape'] == tuple(whole_img.shape)
assert array_interface['version'] == 3

def test_cuda_array_interface_support(testimg_tiff_stripe_32x24_16_jpeg):
img = open_image_cucim(testimg_tiff_stripe_32x24_16_jpeg)
whole_img = img.read_region(device='cuda')
array_interface = whole_img.__cuda_array_interface__
print(array_interface)

# {'data': (81888083968, False), 'strides': None,
# 'descr': [('', '|u1')], 'typestr': '|u1', 'shape': (24, 32, 3),
# 'version': 3, 'mask': None, 'stream': 1}
assert array_interface['data'][0] is not None
assert not array_interface['data'][1]
assert array_interface['strides'] is None
assert array_interface['descr']
assert array_interface['typestr']
assert array_interface['shape'] == tuple(whole_img.shape)
assert array_interface['version'] == 3
assert array_interface['mask'] is None
assert array_interface['stream'] == 1

def test_cuda_array_interface_support(testimg_tiff_stripe_32x24_16_jpeg):
img = open_image_cucim(testimg_tiff_stripe_32x24_16_jpeg)
whole_img = img.read_region(device='cuda')
array_interface = whole_img.__cuda_array_interface__
print(array_interface)

# {'data': (81888083968, False), 'strides': None,
# 'descr': [('', '|u1')], 'typestr': '|u1', 'shape': (24, 32, 3),
# 'version': 3, 'mask': None, 'stream': 1}
assert array_interface['data'][0] is not None
assert not array_interface['data'][1]
assert array_interface['strides'] is None
assert array_interface['descr']
assert array_interface['typestr']
assert array_interface['shape'] == tuple(whole_img.shape)
assert array_interface['version'] == 3
assert array_interface['mask'] is None
assert array_interface['stream'] == 1
19 changes: 19 additions & 0 deletions python/cucim/tests/util/io.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#
# Copyright (c) 2021, NVIDIA CORPORATION.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

def open_image_cucim(file_path):
from cucim import CuImage
img = CuImage(file_path)
return img
59 changes: 11 additions & 48 deletions run
Original file line number Diff line number Diff line change
Expand Up @@ -679,60 +679,23 @@ test() {
fi
}

check_install_python_dep_() {
local package_names="$@"

[ -z "$package_names" ] && c_echo_err R "Please specify package names to check!" && return 1

local packages_to_install=""
local has_all_packages=true

for package_name in $package_names; do
# `package_name` has '<package name>[:<conda package name>]' format.
if [ -n "${CONDA_PREFIX}" ]; then
# abc:def => def
# abc => abc
package_name="${package_name##*:}"
if ! conda list | grep -i -c "${package_name} " > /dev/null; then
c_echo G "${package_name}" W " doesn't exists. Installing " G "${package_name}" W "..."
has_all_packages=false

# Workaround for `openslide-python`
# (See https://github.com/conda-forge/openslide-python-feedstock/issues/4)
[ "${package_name}" = "openslide-python" ] && package_name="conda-forge/linux-64::openslide-python"

packages_to_install="${packages_to_install} ${package_name}"
fi
else
# abc:def => abc
# abc => abc
package_name="${package_name%%:*}"
if ! pip3 list | grep -i -c "${package_name} " > /dev/null; then
c_echo G "${package_name}" W " doesn't exists. Installing " G "${package_name}" W "..."
has_all_packages=false
packages_to_install="${packages_to_install} ${package_name}"
fi
fi
done
if [ "$has_all_packages" != "true" ]; then
if [ -n "${CONDA_PREFIX}" ]; then
run_command conda install -c conda-forge -y ${packages_to_install}
install_python_test_deps_() {
if [ -n "${CONDA_PREFIX}" ]; then
# Cannot use 'conda install' until
# https://github.com/conda-forge/openslide-python-feedstock/issues/4
# is fixed.
# (run_command conda install -c conda-forge -y --file ${TOP}/python/cucim/requirements-test.txt)
run_command pip3 install -r ${TOP}/python/cucim/requirements-test.txt
else
if [ -n "${VIRTUAL_ENV}" ]; then
run_command pip3 install -r ${TOP}/python/cucim/requirements-test.txt
else
if [ -n "${VIRTUAL_ENV}" ]; then
run_command pip3 install ${packages_to_install}
else
run_command pip3 install --user ${packages_to_install}
fi
run_command pip3 install --user -r ${TOP}/python/cucim/requirements-test.txt
fi
fi
hash -r
}

install_python_test_deps_() {
# Package name has '<package name>[:<conda package name>]' format.
check_install_python_dep_ pytest pytest-cov pytest-lazy-fixture psutil GPUtil tifffile imagecodecs openslide-python
}

test_python_desc() { echo 'Execute Python test cases
Arguments:
Expand Down

0 comments on commit b91d30f

Please sign in to comment.