generated from neutrons/python_project_template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from ornlneutronimaging/bin
Multiscale filtering functionality
- Loading branch information
Showing
5 changed files
with
233 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import numpy as np\n", | ||
"import matplotlib.pyplot as plt\n", | ||
"from bm3dornl.bm3d import bm3d_ring_artifact_removal_ms\n", | ||
"from bm3dornl.denoiser_gpu import memory_cleanup\n", | ||
"\n", | ||
"with open(\"../../tests/bm3dornl-data/sino.npy\", \"rb\") as f:\n", | ||
" sino_noisy = np.load(f)\n", | ||
"\n", | ||
"memory_cleanup()\n", | ||
"\n", | ||
"block_matching_kwargs: dict = {\n", | ||
" \"patch_size\": (8, 8),\n", | ||
" \"stride\": 3,\n", | ||
" \"background_threshold\": 0.0,\n", | ||
" \"cut_off_distance\": (64, 64),\n", | ||
" \"num_patches_per_group\": 32,\n", | ||
" \"padding_mode\": \"circular\",\n", | ||
"}\n", | ||
"filter_kwargs: dict = {\n", | ||
" \"filter_function\": \"fft\",\n", | ||
" \"shrinkage_factor\": 3e-2,\n", | ||
"}\n", | ||
"kwargs = {\n", | ||
" \"mode\": \"simple\",\n", | ||
" \"k\": 4,\n", | ||
" \"block_matching_kwargs\": block_matching_kwargs,\n", | ||
" \"filter_kwargs\": filter_kwargs,\n", | ||
"}\n", | ||
"\n", | ||
"sino_bm3dornl = bm3d_ring_artifact_removal_ms(\n", | ||
" sinogram=sino_noisy,\n", | ||
" **kwargs,\n", | ||
")\n", | ||
"\n", | ||
"\n", | ||
"fig, axs = plt.subplots(1, 2, figsize=(12, 4))\n", | ||
"axs[0].imshow(sino_noisy, cmap=\"gray\")\n", | ||
"axs[0].set_title(\"Noisy sinogram\")\n", | ||
"axs[1].imshow(sino_bm3dornl, cmap=\"gray\")\n", | ||
"axs[1].set_title(\"BM3D denoised sinogram\")\n", | ||
"plt.show()\n" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"language_info": { | ||
"name": "python" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 16 additions & 33 deletions
49
tests/unit/bm3dornl/test_bm3d_ring_artifact_removal_ms.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,36 @@ | ||
import pytest | ||
from unittest.mock import patch | ||
import numpy as np | ||
from bm3dornl.bm3d import bm3d_ring_artifact_removal_ms | ||
|
||
|
||
size_x = 256 | ||
size_y = 256 | ||
|
||
|
||
@pytest.fixture | ||
def setup_sinogram(): | ||
return np.random.rand(256, 256) | ||
return np.random.rand(size_x, size_y) | ||
|
||
|
||
@patch("bm3dornl.bm3d.horizontal_binning") | ||
@patch("bm3dornl.bm3d.horizontal_debinning") | ||
@patch("bm3dornl.bm3d.bm3d_ring_artifact_removal") | ||
@pytest.mark.cuda_required | ||
def test_bm3d_ring_artifact_removal_ms( | ||
mock_bm3d_ring_artifact_removal, | ||
mock_horizontal_debinning, | ||
mock_horizontal_binning, | ||
setup_sinogram, | ||
): | ||
sinogram = setup_sinogram | ||
binned_sinos = [np.random.rand(64, 64) for _ in range(4)] | ||
mock_horizontal_binning.return_value = binned_sinos | ||
binned_sinos = [np.random.rand(64, 64) for _ in range(4)] | ||
mock_bm3d_ring_artifact_removal.return_value = binned_sinos | ||
binned_sinos = [np.random.rand(64, 64) for _ in range(4)] | ||
mock_horizontal_debinning.return_value = binned_sinos | ||
|
||
result = bm3d_ring_artifact_removal_ms(sinogram, k=4) | ||
|
||
assert result is not None | ||
mock_horizontal_binning.assert_called_once_with(sinogram, k=4) | ||
assert mock_bm3d_ring_artifact_removal.call_count == 3 | ||
assert mock_horizontal_debinning.call_count == 3 | ||
|
||
r, c = result.shape | ||
|
||
assert c == size_x | ||
assert r == size_y | ||
|
||
result_single_pass = bm3d_ring_artifact_removal_ms(sinogram, k=0) | ||
assert result_single_pass is not None | ||
mock_bm3d_ring_artifact_removal.assert_called_with( | ||
sinogram, | ||
mode="simple", | ||
block_matching_kwargs={ | ||
"patch_size": (8, 8), | ||
"stride": 3, | ||
"background_threshold": 0.0, | ||
"cut_off_distance": (64, 64), | ||
"num_patches_per_group": 32, | ||
"padding_mode": "circular", | ||
}, | ||
filter_kwargs={ | ||
"filter_function": "fft", | ||
"shrinkage_factor": 3e-2, | ||
}, | ||
) | ||
|
||
r, c = result_single_pass.shape | ||
|
||
assert c == size_x | ||
assert r == size_y |
Oops, something went wrong.