Coverage for src/abm_colony_collection/make_voxels_array.py: 100%

18 statements  

« prev     ^ index     » next       coverage.py v7.1.0, created at 2025-09-15 20:34 +0000

1import numpy as np 

2 

3 

4def make_voxels_array(locations: list) -> np.ndarray: 

5 """ 

6 Convert list of locations to a segmentated image array. 

7 

8 Parameters 

9 ---------- 

10 locations 

11 List of location dictionaries containing id and voxels. 

12 

13 Returns 

14 ------- 

15 : 

16 Segmentation array. 

17 """ 

18 

19 # Extract all voxel positions with id. 

20 all_ids: list[int] = [] 

21 all_xyz: list[tuple[int, int, int]] = [] 

22 for location in locations: 

23 cell_id = location["id"] 

24 xyz = [(x, y, z) for region in location["location"] for x, y, z in region["voxels"]] 

25 all_xyz = all_xyz + xyz 

26 all_ids = all_ids + [cell_id] * len(xyz) 

27 

28 # Return if no voxels. 

29 if len(all_ids) == 0: 

30 return np.zeros((1, 1, 1)) 

31 

32 # Create empty array. 

33 mins = np.min(all_xyz, axis=0) 

34 maxs = np.max(all_xyz, axis=0) 

35 length, width, height = np.subtract(maxs, mins) + 3 

36 array = np.zeros((height, width, length), dtype=np.uint16) 

37 

38 # Fill voxel array. 

39 all_xyz_offset = [(z - mins[2] + 1, y - mins[1] + 1, x - mins[0] + 1) for x, y, z in all_xyz] 

40 array[tuple(np.transpose(all_xyz_offset))] = all_ids 

41 

42 return array