Skip to content

Commit

Permalink
Merge pull request #5 from niaid/FixPackageNamingIn2ngpc
Browse files Browse the repository at this point in the history
Fix package naming in2ngpc
  • Loading branch information
blowekamp authored Apr 2, 2021
2 parents 3a86a18 + c262840 commit 159489c
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 28 deletions.
2 changes: 1 addition & 1 deletion pytools/ng/mrc2ngpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def main(input_image, output_precompute, flat, gzip):
check_process = True

# Task 1: Convert mrc to NIFTI with SimpleITK script
py_code_main = "import sys; from ngtool.mrc2nifti import main; sys.exit(main())"
py_code_main = "import sys; from pytools.ng.mrc2nifti import main; sys.exit(main())"
cmd = [sys.executable, "-c", py_code_main, input_image, nifti_filename]

logger.info("Executing conversion of MRC to NIFTI..")
Expand Down
29 changes: 29 additions & 0 deletions test/fixtures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0.txt
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import pytest
import SimpleITK as sitk


@pytest.fixture(
scope="session",
params=[sitk.sitkUInt8, sitk.sitkInt16, sitk.sitkUInt16, sitk.sitkFloat32],
)
def image_mrc(request, tmp_path_factory):
pixel_type = request.param
print(f"Calling image_mrc with {sitk.GetPixelIDValueAsString(pixel_type)}")
fn = f"image_mrc_{sitk.GetPixelIDValueAsString(pixel_type).replace(' ', '_')}.mrc"
img = sitk.Image([10, 9, 8], pixel_type)
fn = tmp_path_factory.mktemp("data").joinpath(fn)
sitk.WriteImage(img, str(fn))
return str(fn)
33 changes: 6 additions & 27 deletions test/test_mrc2ngpc.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
Expand All @@ -13,34 +12,19 @@
#

from click.testing import CliRunner
from pytools.ng.mrc2nifti import main
import pytools.ng.mrc2ngpc
import pytest
import SimpleITK as sitk
import numpy as np


@pytest.fixture(
scope="session",
params=[sitk.sitkUInt8, sitk.sitkInt16, sitk.sitkUInt16, sitk.sitkFloat32],
)
def image_mrc(request, tmp_path_factory):

pixel_type = request.param
print(f"Calling image_mrc with {sitk.GetPixelIDValueAsString(pixel_type)}")
fn = f"image_mrc_{sitk.GetPixelIDValueAsString(pixel_type).replace(' ', '_')}.mrc"
img = sitk.Image([10, 9, 8], pixel_type)
fn = tmp_path_factory.mktemp("data").joinpath(fn)
sitk.WriteImage(img, str(fn))
return str(fn)

from fixtures import image_mrc

args = ["--help", "--version"]


@pytest.mark.parametrize("cli_args", args)
def test_mrc2nifti_main_help(cli_args):
def test_mrc2ngpc_main_help(cli_args):
runner = CliRunner()
result = runner.invoke(main, cli_args.split())
result = runner.invoke(pytools.ng.mrc2ngpc.main, cli_args.split())
assert not result.exception


Expand All @@ -54,13 +38,8 @@ def test_mrc2nifti_main_help(cli_args):
],
indirect=["image_mrc"],
)
def test_mrc2nifti(image_mrc, expected_pixel_type):
def test_mrc2ngpc(image_mrc, expected_pixel_type):
runner = CliRunner()
with runner.isolated_filesystem():
result = runner.invoke(main, [image_mrc, "out.nii"])
result = runner.invoke(pytools.ng.mrc2ngpc.main, [image_mrc, "out_ngpc"])
assert not result.exception
img = sitk.ReadImage("out.nii")

assert img.GetPixelID() == expected_pixel_type
assert img.GetSize() == (10, 9, 8)
np.testing.assert_allclose(img.GetSpacing(), (1e-7, 1e-7, 1e-7))
51 changes: 51 additions & 0 deletions test/test_mrc2nii.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0.txt
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from click.testing import CliRunner
import pytools.ng.mrc2nifti
import pytest
import SimpleITK as sitk
import numpy as np
from fixtures import image_mrc

args = ["--help", "--version"]


@pytest.mark.parametrize("cli_args", args)
def test_mrc2nifti_main_help(cli_args):
runner = CliRunner()
result = runner.invoke(pytools.ng.mrc2nifti.main, cli_args.split())
assert not result.exception


@pytest.mark.parametrize(
"image_mrc,expected_pixel_type",
[
(sitk.sitkUInt8, sitk.sitkUInt8),
(sitk.sitkInt16, sitk.sitkUInt16),
(sitk.sitkUInt16, sitk.sitkUInt16),
(sitk.sitkFloat32, sitk.sitkFloat32),
],
indirect=["image_mrc"],
)
def test_mrc2nifti(image_mrc, expected_pixel_type):
runner = CliRunner()
with runner.isolated_filesystem():
result = runner.invoke(pytools.ng.mrc2nifti.main, [image_mrc, "out.nii"])
assert not result.exception
img = sitk.ReadImage("out.nii")

assert img.GetPixelID() == expected_pixel_type
assert img.GetSize() == (10, 9, 8)
np.testing.assert_allclose(img.GetSpacing(), (1e-7, 1e-7, 1e-7))

0 comments on commit 159489c

Please sign in to comment.