| 
1 | 1 | import logging  | 
2 |  | -import math  | 
3 |  | -from itertools import product  | 
4 | 2 | 
 
  | 
5 | 3 | import numpy as np  | 
6 | 4 | 
 
  | 
7 | 5 | from ..geometry import Vec3Int  | 
 | 6 | +from .data_format import DataFormat  | 
8 | 7 | from .view import View  | 
9 | 8 | 
 
  | 
10 | 9 | 
 
  | 
@@ -32,55 +31,42 @@ def upsample_cube_job(  | 
32 | 31 |     assert all(1 >= f for f in mag_factors), (  | 
33 | 32 |         f"mag_factors ({mag_factors}) for upsampling must be smaller than 1"  | 
34 | 33 |     )  | 
 | 34 | +    if (  | 
 | 35 | +        target_view._data_format != DataFormat.WKW  | 
 | 36 | +        and target_view._is_compressed() == False  | 
 | 37 | +    ):  | 
 | 38 | +        assert buffer_shape % target_view.info.shard_shape == Vec3Int.zeros(), (  | 
 | 39 | +            f"buffer_shape ({buffer_shape}) must be divisible by shard_shape ({target_view.info.shard_shape})"  | 
 | 40 | +        )  | 
 | 41 | +    inverse_factors = [int(1 / f) for f in mag_factors]  | 
35 | 42 | 
 
  | 
36 | 43 |     try:  | 
37 | 44 |         num_channels = target_view.info.num_channels  | 
38 |  | -        target_bbox_in_mag = target_view.bounding_box.in_mag(target_view.mag)  | 
39 |  | -        shape = (num_channels,) + target_bbox_in_mag.size.to_tuple()  | 
40 |  | -        shape_xyz = target_bbox_in_mag.size_xyz  | 
41 |  | -        file_buffer = np.zeros(shape, target_view.get_dtype())  | 
42 |  | - | 
43 |  | -        tiles = product(  | 
44 |  | -            *list(  | 
45 |  | -                [  | 
46 |  | -                    list(range(0, math.ceil(length)))  | 
47 |  | -                    for length in shape_xyz.to_np() / buffer_shape.to_np()  | 
48 |  | -                ]  | 
49 |  | -            )  | 
50 |  | -        )  | 
51 | 45 | 
 
  | 
52 |  | -        for tile in tiles:  | 
53 |  | -            target_offset = Vec3Int(tile) * buffer_shape  | 
54 |  | -            source_offset = target_offset * source_view.mag  | 
55 |  | -            source_size = source_view.bounding_box.size_xyz  | 
56 |  | -            source_size = (buffer_shape * source_view.mag).pairmin(  | 
57 |  | -                source_size - source_offset  | 
58 |  | -            )  | 
59 |  | - | 
60 |  | -            bbox = source_view.bounding_box.offset(source_offset).with_size_xyz(  | 
61 |  | -                source_size  | 
62 |  | -            )  | 
 | 46 | +        for chunk in target_view.bounding_box.chunk(  | 
 | 47 | +            buffer_shape * target_view.mag, buffer_shape * target_view.mag  | 
 | 48 | +        ):  | 
 | 49 | +            shape = (num_channels,) + (chunk.in_mag(target_view.mag)).size.to_tuple()  | 
 | 50 | +            file_buffer = np.zeros(shape, dtype=target_view.get_dtype())  | 
63 | 51 |             cube_buffer_channels = source_view.read_xyz(  | 
64 |  | -                absolute_bounding_box=bbox,  | 
 | 52 | +                absolute_bounding_box=chunk,  | 
65 | 53 |             )  | 
66 | 54 | 
 
  | 
67 | 55 |             for channel_index in range(num_channels):  | 
68 | 56 |                 cube_buffer = cube_buffer_channels[channel_index]  | 
69 | 57 | 
 
  | 
70 | 58 |                 if not np.all(cube_buffer == 0):  | 
71 |  | -                    # Upsample the buffer  | 
72 |  | -                    inverse_factors = [int(1 / f) for f in mag_factors]  | 
73 | 59 |                     data_cube = upsample_cube(cube_buffer, inverse_factors)  | 
74 | 60 | 
 
  | 
75 |  | -                    buffer_bbox = target_view.bounding_box.with_topleft_xyz(  | 
76 |  | -                        target_offset * inverse_factors  | 
77 |  | -                    ).with_size_xyz(data_cube.shape)  | 
 | 61 | +                    buffer_bbox = chunk.with_topleft_xyz(Vec3Int.zeros()).with_size_xyz(  | 
 | 62 | +                        data_cube.shape  | 
 | 63 | +                    )  | 
78 | 64 |                     data_cube = buffer_bbox.xyz_array_to_bbox_shape(data_cube)  | 
79 | 65 |                     file_buffer[(channel_index,) + buffer_bbox.to_slices_xyz()] = (  | 
80 | 66 |                         data_cube  | 
81 | 67 |                     )  | 
82 | 68 | 
 
  | 
83 |  | -        target_view.write(file_buffer, absolute_bounding_box=target_view.bounding_box)  | 
 | 69 | +            target_view.write(file_buffer, absolute_bounding_box=chunk)  | 
84 | 70 | 
 
  | 
85 | 71 |     except Exception as exc:  | 
86 | 72 |         logging.error(  | 
 | 
0 commit comments