Coverage for tests/test_utils.py: 100%

26 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-10-09 10:24 +0000

1# This file is part of meas_photoz_base 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (http://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <http://www.gnu.org/licenses/>. 

21 

22import numpy as np 

23import pytest 

24 

25from lsst.meas.photoz.base.utils import chunk_iterator 

26 

27 

28@pytest.fixture(scope="module") 

29def size(): 

30 """Return the size of data arrays.""" 

31 return 12 

32 

33 

34@pytest.fixture(scope="module") 

35def data(size): 

36 """Return some arbitrary columns.""" 

37 return { 

38 "a": np.arange(size), 

39 "b": np.arange(size + 1, 1, -1) + 0.5, 

40 } 

41 

42 

43def test_chunk_iterator(size, data): 

44 """Test the chunk_iterator generator utility.""" 

45 keys = list(data.keys()) 

46 

47 for chunk_size in (3, 5): 

48 start = 0 

49 remaining = size 

50 iterator = chunk_iterator(chunk_size, data, size) 

51 while remaining > 0: 

52 chunk, n_chunk = next(iterator) 

53 assert n_chunk == min(remaining, chunk_size) 

54 assert list(chunk.keys()) == keys 

55 for key, values in chunk.items(): 

56 assert len(values) == n_chunk 

57 np.testing.assert_array_equal(data[key][start : start + n_chunk], values) 

58 remaining -= n_chunk 

59 start += n_chunk 

60 with pytest.raises(StopIteration): 

61 next(iterator)