Skip to content

Commit b5884e5

Browse files
authored
Merge pull request #18 from den-sq/main
Revise Volume Writing Addresses outstanding border artifact transposition issues, reduces memory usage somewhat.
2 parents ffd84b5 + 23df17a commit b5884e5

File tree

7 files changed

+173
-263
lines changed

7 files changed

+173
-263
lines changed

.coverage

128 KB
Binary file not shown.

.coveragerc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[run]
2+
omit = *test*
3+
omit = *temp*

python/coverage.svg

Lines changed: 3 additions & 3 deletions
Loading

python/ouroboros/helpers/files.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import os
22
from pathlib import Path
3-
from tifffile import imread, TiffWriter
3+
from tifffile import imread, TiffWriter, memmap
44
from .memory_usage import calculate_gigabytes_from_dimensions
55
import shutil
66

7+
import numpy as np
8+
79

810
def load_and_save_tiff_from_slices(
911
folder_name: str,
@@ -180,3 +182,36 @@ def parse_tiff_name(tiff_name: str) -> int:
180182

181183
def num_digits_for_n_files(n: int) -> int:
182184
return len(str(n - 1))
185+
186+
187+
def write_memmap_with_create(file_path: os.PathLike, indicies: tuple[np.ndarray], data: np.ndarray,
188+
shape: tuple, dtype: type):
189+
if file_path.exists():
190+
try:
191+
target_file = memmap(file_path)
192+
except BaseException as be:
193+
print(f"MM: {be} - {file_path} ")
194+
import time
195+
time.sleep(0.5)
196+
target_file = memmap(file_path)
197+
else:
198+
if shape is None or dtype is None:
199+
raise ValueError(f"Must have shape ({shape} given) and dtype ({dtype} given) when creating a memmap.")
200+
target_file = memmap(file_path, shape=shape, dtype=dtype)
201+
target_file[:] = 0
202+
203+
def ab(flags: np.ndarray[bool], index):
204+
return tuple(dim[flags] for dim in index) if isinstance(index, tuple) else index[flags]
205+
206+
if indicies is not None and data is not None:
207+
exist = target_file[indicies] != 0
208+
if np.any(exist):
209+
target_file[ab(exist, indicies)] = np.mean([target_file[indicies][exist], data[exist]],
210+
axis=0, dtype=target_file.dtype)
211+
target_file[ab(np.invert(exist), indicies)] = data[np.invert(exist)]
212+
else:
213+
target_file[indicies] = data
214+
elif indicies is not None or data is not None:
215+
raise ValueError(f"Could not write data as indicies (None? {indicies is None})"
216+
f"or data (None? {data is None}) were missing.")
217+
del target_file

python/ouroboros/helpers/slice.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,7 @@ def slice_volume_from_grids(
164164
return slice_points.reshape(len(grids), height, width)
165165

166166

167-
def backproject_slices(bounding_box: BoundingBox, slice_rects: np.ndarray, slices: np.ndarray,
168-
volume: np.ndarray = None) -> tuple[np.ndarray]:
167+
def backproject_slices(bounding_box: BoundingBox, slice_rects: np.ndarray, slices: np.ndarray) -> tuple[np.ndarray]:
169168
"""
170169
Write a slice to volume based on a grid of coordinates. Returns coordinates.
171170
@@ -185,7 +184,7 @@ def backproject_slices(bounding_box: BoundingBox, slice_rects: np.ndarray, slice
185184
"""
186185
if slices.shape[0] == 0:
187186
# No slices, just return
188-
return np.empty(0, dtype=np.uint16), np.empty(0, dtype=np.float32), np.empty(0, dtype=np.float32)
187+
return np.empty((3, 0), dtype=np.uint16), np.empty(0, dtype=np.float32), np.empty(0, dtype=np.float32)
189188

190189
# Normalize grid coordinates based on bounding box (since volume coordinates are truncated to bounding box)
191190
grid_call = partial(coordinate_grid, shape=slices[0].shape, floor=bounding_box.get_min())
@@ -224,10 +223,6 @@ def corner_weights(w, c):
224223
bordered_values = np.logical_and.reduce(np.less(net_lookup, bounding_box.get_max().reshape(3, 1)))
225224
set_values = np.logical_and((net_point_weights != 0), bordered_values)
226225

227-
if volume is not None:
228-
volume[(net_lookup[0, set_values], net_lookup[1, set_values], net_lookup[2, set_values])] = \
229-
(net_point_totals[set_values] / net_point_weights[set_values])[:, np.newaxis]
230-
231226
return net_lookup[:, set_values], net_point_totals[set_values], net_point_weights[set_values]
232227

233228

0 commit comments

Comments
 (0)