Skip to content

Commit 27c4d9d

Browse files
committed
reworked mentioned parts
1 parent cf2898d commit 27c4d9d

File tree

4 files changed

+23
-39
lines changed

4 files changed

+23
-39
lines changed

synaptic_reconstruction/tools/distance_measure_widget.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
from .base_widget import BaseWidget
1212
from .. import distance_measurements
13-
from .util import compute_scale_from_voxel_size
1413

1514
try:
1615
from napari_skimage_regionprops import add_table
@@ -109,7 +108,7 @@ def on_measure_seg_to_object(self):
109108
resolution = [v for v in resolution.values()]
110109
# if user input is present override metadata
111110
if self.voxel_size_param.value() != 0.0: # changed from default
112-
resolution = [self.voxel_size_param.value() for _ in range(len(segmentation.shape))]
111+
resolution = segmentation.ndim * [self.voxel_size_param.value()]
113112

114113
(distances,
115114
endpoints1,
@@ -139,7 +138,7 @@ def on_measure_pairwise(self):
139138
resolution = [v for v in resolution.values()]
140139
# if user input is present override metadata
141140
if self.voxel_size_param.value() != 0.0: # changed from default
142-
resolution = [self.voxel_size_param.value() for _ in range(len(segmentation.shape))]
141+
resolution = segmentation.ndim * [self.voxel_size_param.value()]
143142

144143
(distances,
145144
endpoints1,
@@ -162,7 +161,7 @@ def _create_settings_widget(self):
162161

163162
self.save_path, layout = self._add_path_param(name="Save Table", select_type="file", value="")
164163
setting_values.layout().addLayout(layout)
165-
164+
166165
self.voxel_size_param, layout = self._add_float_param(
167166
"voxel_size", 0.0, min_val=0.0, max_val=100.0,
168167
)

synaptic_reconstruction/tools/segmentation_widget.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from qtpy.QtWidgets import QWidget, QVBoxLayout, QPushButton, QLabel, QComboBox
44

55
from .base_widget import BaseWidget
6-
from .util import (run_segmentation, get_model, get_model_registry, _available_devices, get_device,
6+
from .util import (run_segmentation, get_model, get_model_registry, _available_devices, get_device,
77
get_current_tiling, compute_scale_from_voxel_size)
88
from synaptic_reconstruction.inference.util import get_default_tiling
99
import copy
@@ -69,8 +69,8 @@ def on_predict(self):
6969
return
7070

7171
# load current tiling
72-
self.tiling = get_current_tiling(self.tiling, self.default_tiling, image.shape)
73-
72+
self.tiling = get_current_tiling(self.tiling, self.default_tiling, model_type)
73+
7474
# TODO: Use scale derived from the image resolution.
7575
# get avg image shape from training of the selected model
7676
# wichmann data avg voxel size = 17.53
@@ -92,8 +92,8 @@ def on_predict(self):
9292
if voxel_size:
9393
# calculate scale so voxel_size is the same as in training
9494
scale = compute_scale_from_voxel_size(voxel_size, model_type)
95-
print(f"Rescaled the image by {scale} to optimize for the selected model.")
96-
95+
show_info(f"Rescaled the image by {scale} to optimize for the selected model.")
96+
9797
segmentation = run_segmentation(
9898
image, model=model, model_type=model_type, tiling=self.tiling, scale=scale
9999
)

synaptic_reconstruction/tools/util.py

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def _available_devices():
176176
return available_devices
177177

178178

179-
def get_current_tiling(tiling: dict, default_tiling: dict, image_shape):
179+
def get_current_tiling(tiling: dict, default_tiling: dict, model_type: str):
180180
# get tiling values from qt objects
181181
for k, v in tiling.items():
182182
for k2, v2 in v.items():
@@ -185,8 +185,8 @@ def get_current_tiling(tiling: dict, default_tiling: dict, image_shape):
185185
tiling[k][k2] = v2.value()
186186
# check if user inputs tiling/halo or not
187187
if default_tiling == tiling:
188-
if len(image_shape) == 2:
189-
# if its 2d image expand x,y and set z to 1
188+
if "2d" in model_type:
189+
# if its a 2d model expand x,y and set z to 1
190190
tiling = {
191191
"tile": {
192192
"x": 512,
@@ -199,31 +199,14 @@ def get_current_tiling(tiling: dict, default_tiling: dict, image_shape):
199199
"z": 1
200200
}
201201
}
202-
elif len(image_shape) == 2:
203-
# if its a 2d image set z to 1
202+
elif "2d" in model_type:
203+
# if its a 2d model set z to 1
204204
tiling["tile"]["z"] = 1
205205
tiling["halo"]["z"] = 1
206206

207207
return tiling
208208

209209

210-
def compute_average_voxel_size(voxel_size: dict) -> float:
211-
"""
212-
Computes the average voxel size dynamically based on available dimensions.
213-
214-
Args:
215-
voxel_size (dict): Dictionary containing voxel dimensions (e.g., x, y, z).
216-
217-
Returns:
218-
float: Average voxel size.
219-
"""
220-
# Extract all dimension values
221-
dimensions = [voxel_size[key] for key in voxel_size if key in ["x", "y", "z"]]
222-
223-
# Compute the average
224-
return sum(dimensions) / len(dimensions)
225-
226-
227210
def compute_scale_from_voxel_size(
228211
voxel_size: dict,
229212
model_type: str
@@ -233,7 +216,7 @@ def compute_scale_from_voxel_size(
233216
voxel_size["x"] / training_voxel_size["x"],
234217
voxel_size["y"] / training_voxel_size["y"],
235218
]
236-
if len(voxel_size) == 3:
219+
if len(voxel_size) == 3 and len(training_voxel_size) == 3:
237220
scale.append(
238221
voxel_size["z"] / training_voxel_size["z"]
239222
)

synaptic_reconstruction/tools/volume_reader.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ def get_reader(path: PathOrPaths) -> Optional[ReaderFunction]:
2323
def _read_mrc(path, fname):
2424
with open_file(path, mode="r") as f:
2525
data = f["data"][:]
26-
26+
voxel_size = read_voxel_size(path)
2727
metadata = {
28-
"file_path": path
28+
"file_path": path,
29+
"voxel_size": voxel_size
2930
}
30-
read_voxel_size(path, metadata)
3131
layer_attributes = {
3232
"name": fname,
3333
"colormap": "gray",
@@ -74,22 +74,24 @@ def read_image_volume(path: PathOrPaths) -> List[LayerData]:
7474
return
7575

7676

77-
def read_voxel_size(input_path: str, layer_attributes: dict) -> None:
77+
def read_voxel_size(input_path: str) -> dict | None:
7878
"""Read voxel size from mrc/rec file and store it in layer_attributes.
79-
The original unit of voxel size is Angstrom and we convert it to nanometers
80-
by dividing it by ten.
79+
The original unit of voxel size is Angstrom and we convert it to nanometers
80+
by dividing it by ten.
8181
8282
Args:
8383
input_path (str): path to mrc/rec file
8484
layer_attributes (dict): napari layer attributes to store voxel size to
8585
"""
86+
new_voxel_size = None
8687
with mrcfile.open(input_path, permissive=True) as mrc:
8788
try:
8889
voxel_size = mrc.voxel_size
89-
layer_attributes["voxel_size"] = {
90+
new_voxel_size = {
9091
"x": voxel_size.x / 10,
9192
"y": voxel_size.y / 10,
9293
"z": voxel_size.z / 10,
9394
}
9495
except Exception as e:
9596
print(f"Failed to read voxel size: {e}")
97+
return new_voxel_size

0 commit comments

Comments
 (0)