Coverage for tests/test_process_utils.py: 100%

1 from image_utils.process_utils import parallelize

2 from image_utils.file_utils import get_rand_hex

3 import pytest

4 from pathlib import Path

5 import numpy as np

6

7 img_path = Path('tests/flower.jpg')

8 save_path = Path(__file__).parent / 'output'

9

10

11# memory = Memory(location='./cachedir', verbose=0, mmap_mode='r')

12# @memory.cache

13# def costly_compute(data, column):

14# """Emulate a costly function by sleeping and returning a column."""

15# time.sleep(1)

16# return data[column] + 1

17

18 def do_func(lst, idx, arr):

19 print(len(lst), arr.shape)

20

21

22 def get_test_list(list_size):

23 return [get_rand_hex() for _ in range(list_size)]

24

25

26@pytest.mark.parametrize("num_processes", [1, 2, 10])

27@pytest.mark.parametrize("list_size", [1, 10, 1000])

28 def test_parallelize(num_processes, list_size):

29 test_list = get_test_list(list_size)

30 rng = np.random.RandomState(42)

31 test_arr = rng.randn(int(1e4), 4)

32 test_parallel = parallelize(do_func, num_processes=num_processes, shuffle=False, use_joblib=True)(test_list, arr=test_arr)