Skip to content

Commit

Permalink
Harmonise napari plugin and CLI entry points (#193)
Browse files Browse the repository at this point in the history
* Harmonise napari plugin and CLI entry points

* Set minimum for negative default parameters

* Set specific type
  • Loading branch information
adamltyson committed Apr 30, 2024
1 parent 7010a0b commit 8012442
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 20 deletions.
33 changes: 20 additions & 13 deletions brainreg/napari/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ def brainreg_register():
freeform_n_steps=6,
freeform_use_n_steps=4,
bending_energy_weight=0.95,
grid_spacing=10,
smoothing_sigma_reference=1,
smoothing_sigma_floating=1,
grid_spacing=-10,
smoothing_sigma_reference=-1.0,
smoothing_sigma_floating=-1.0,
histogram_n_bins_floating=128,
histogram_n_bins_reference=128,
debug=False,
Expand Down Expand Up @@ -194,15 +194,19 @@ def brainreg_register():
label="bending_energy_weight",
),
grid_spacing=dict(
value=DEFAULT_PARAMETERS["grid_spacing"], label="grid_spacing"
value=DEFAULT_PARAMETERS["grid_spacing"],
label="grid_spacing",
min=-100,
),
smoothing_sigma_reference=dict(
value=DEFAULT_PARAMETERS["smoothing_sigma_reference"],
label="smoothing_sigma_reference",
min=-99.0,
),
smoothing_sigma_floating=dict(
value=DEFAULT_PARAMETERS["smoothing_sigma_floating"],
label="smoothing_sigma_floating",
min=-99.0,
),
histogram_n_bins_floating=dict(
value=DEFAULT_PARAMETERS["histogram_n_bins_floating"],
Expand Down Expand Up @@ -239,10 +243,10 @@ def widget(
freeform_use_n_steps: int,
bending_energy_weight: float,
grid_spacing: int,
smoothing_sigma_reference: int,
smoothing_sigma_reference: float,
smoothing_sigma_floating: float,
histogram_n_bins_floating: float,
histogram_n_bins_reference: float,
histogram_n_bins_floating: int,
histogram_n_bins_reference: int,
debug: bool,
reset_button,
check_orientation_button,
Expand Down Expand Up @@ -314,7 +318,9 @@ def widget(
grid_spacing: int
Sets the control point grid spacing in x, y & z.
Smaller grid spacing allows for more local deformations
but increases the risk of over-fitting.
but increases the risk of over-fitting. Positive
values are interpreted as real values in mm, negative values
are interpreted as distance in voxels.
smoothing_sigma_reference: int
Adds a Gaussian smoothing to the reference image (the one being
registered), with the sigma defined by the number. Positive
Expand All @@ -325,11 +331,11 @@ def widget(
registered), with the sigma defined by the number. Positive
values are interpreted as real values in mm, negative values
are interpreted as distance in voxels.
histogram_n_bins_floating: float
histogram_n_bins_floating: int
Number of bins used for the generation of the histograms used
for the calculation of Normalized Mutual Information on the
floating image
histogram_n_bins_reference: float
histogram_n_bins_reference: int
Number of bins used for the generation of the histograms used
for the calculation of Normalized Mutual Information on the
reference image
Expand Down Expand Up @@ -404,9 +410,9 @@ def run():
freeform_n_steps,
freeform_use_n_steps,
bending_energy_weight,
-grid_spacing,
-smoothing_sigma_reference,
-smoothing_sigma_floating,
grid_spacing,
smoothing_sigma_reference,
smoothing_sigma_floating,
histogram_n_bins_floating,
histogram_n_bins_reference,
debug=False,
Expand Down Expand Up @@ -463,6 +469,7 @@ def run():
n_free_cpus,
save_original_orientation=save_original_orientation,
brain_geometry=brain_geometry.value,
debug=debug,
)

logging.info("Calculating volumes of each brain area")
Expand Down
36 changes: 29 additions & 7 deletions brainreg/napari/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,52 @@ def initialise_brainreg(atlas_key, data_orientation_key, voxel_sizes):
)


def downsample_and_save_brain(img_layer, scaling):
def downsample_and_save_brain(
img_layer,
scaling,
anti_aliasing=True,
preserve_range=True,
mode="constant",
):
first_frame_shape = skimage.transform.rescale(
img_layer.data[0], scaling[1:2], anti_aliasing=True
img_layer.data[0],
scaling[1:2],
anti_aliasing=anti_aliasing,
preserve_range=preserve_range,
mode=mode,
).shape
preallocated_array = np.empty(
(img_layer.data.shape[0], first_frame_shape[0], first_frame_shape[1])
)
print("downsampling data in x, y")
print("Downsampling data in x, y")
for i, img in tqdm(enumerate(img_layer.data)):
down_xy = skimage.transform.rescale(
img, scaling[1:2], anti_aliasing=True
img,
scaling[1:2],
anti_aliasing=anti_aliasing,
preserve_range=preserve_range,
mode=mode,
)
preallocated_array[i] = down_xy

first_ds_frame_shape = skimage.transform.rescale(
preallocated_array[:, :, 0], [scaling[0], 1], anti_aliasing=True
preallocated_array[:, :, 0],
[scaling[0], 1],
anti_aliasing=anti_aliasing,
preserve_range=preserve_range,
mode=mode,
).shape
downsampled_array = np.empty(
(first_ds_frame_shape[0], first_frame_shape[0], first_frame_shape[1])
)
print("downsampling data in z")
print("Downsampling data in z")
for i, img in tqdm(enumerate(preallocated_array.T)):
down_xyz = skimage.transform.rescale(
img, [1, scaling[0]], anti_aliasing=True
img,
[1, scaling[0]],
anti_aliasing=anti_aliasing,
preserve_range=preserve_range,
mode=mode,
)
downsampled_array[:, :, i] = down_xyz.T
return downsampled_array
Expand Down

0 comments on commit 8012442

Please sign in to comment.