Skip to content

Commit

Permalink
Small nitpicks
Browse files Browse the repository at this point in the history
  • Loading branch information
jesse-wei committed Jun 11, 2023
1 parent d791338 commit 0fa32e3
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 22 deletions.
16 changes: 9 additions & 7 deletions NeuroRuler/utils/global_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@

import SimpleITK as sitk
from pathlib import Path
from typing import Any
from NeuroRuler.utils.constants import View

IMAGE_DICT: dict[Path, sitk.Image] = dict()
"""The group of images that have been loaded.
"""The group of images that has been loaded.
Since Python 3.7+, dicts maintain insertion order. Therefore, we can use CURR_IMAGE_INDEX for retrieval and deletion.
Since Python 3.7+, dicts maintain insertion order. Therefore, we can use
CURR_IMAGE_INDEX for retrieval and deletion.
Use list(IMAGE_DICT.keys())[i] to return the i'th key in the dict, which can also index into the dict.
Not sure about this operation's speed, but it's used only
in the GUI for insertion and deletion operations, should be fine.
in the GUI for insertion and deletion operations, which is fine.
All images in the dictionary have matching properties, as defined by mri_image.get_properties."""
All images in the dictionary have matching properties, as defined by img_helpers.ImageProperties
(there is a threshold for pixel spacing)."""

GROUP_MAX_SPACING_DIFF: float = 0.0001
"""The maximum difference in pixel spacing (in mm) between two images of the global group, such that they may be considered to be the same spacing."""
"""The maximum difference in pixel spacing (in mm) between two images of the global group,
such that they are considered to have the same spacing."""

CURR_IMAGE_INDEX: int = 0
"""Image of the current image in the loaded batch of images, which is a dict[Path, sitk.Image].
Expand All @@ -29,7 +31,7 @@
"""Global ``sitk.ImageFileReader``."""

ORIENT_FILTER: sitk.DICOMOrientImageFilter = sitk.DICOMOrientImageFilter()
"""Global sitk.DICOMOrientImageFilter for orienting images.
"""Global ``sitk.DICOMOrientImageFilter`` for orienting images.
See https://simpleitk.org/doxygen/latest/html/classitk_1_1simple_1_1DICOMOrientImageFilter.html#details
and the orientation strings in constants.py. Use ITK-SNAP for the orientations that we copy."""
Expand Down
4 changes: 2 additions & 2 deletions NeuroRuler/utils/gui_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
then we need an actual working value here."""

from pathlib import Path
import NeuroRuler.utils.constants as constants

DEBUG: bool = False
"""Whether or not to print debugging information throughout execution."""
Expand Down Expand Up @@ -40,4 +39,5 @@
"""Whether the advanced menu messages display in terminal or new window"""

GROUP_MAX_SPACING_DIFF: float = 0.0001
"""The maximum difference in pixel spacing (in mm) between two images of the global group, such that they may be considered to be the same spacing. See global_vars.GROUP_MAX_SPACING_DIFF."""
"""The maximum difference in pixel spacing (in mm) between two images of the global group,
such that they are considered to have the same spacing. See ``global_vars.GROUP_MAX_SPACING_DIFF``."""
24 changes: 14 additions & 10 deletions NeuroRuler/utils/img_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
import NeuroRuler.utils.constants as constants


class ImageProperties(NamedTuple):
center: tuple
size: tuple
spacing: tuple


def update_images(path_list: list[Path]) -> list[Path]:
"""Initialize IMAGE_DICT. See the docstring for IMAGE_DICT in global_vars.py for more info.
Expand Down Expand Up @@ -147,20 +153,14 @@ def get_curr_image() -> sitk.Image:
return global_vars.IMAGE_DICT[get_curr_path()]


class ImageProperties(NamedTuple):
center: tuple
size: tuple
spacing: tuple


# TODO: Add more properties?
def get_properties_from_sitk_image(img: sitk.Image) -> ImageProperties:
"""Tuple of properties of a sitk.Image.
TODO: Add more properties
If modifying this, NeuroRuler/GUI/main.py print_properties also has to be modified slightly,
as well are_properties_eq below.
If modifying this, NeuroRuler/GUI/main.py ``print_properties`` also has to be modified slightly,
as well as ``are_properties_eq`` below.
:param img:
:type img: sitk.Image
Expand All @@ -178,13 +178,17 @@ def are_properties_eq(props1: ImageProperties, props2: ImageProperties) -> bool:
:type props1: ImageProperties
:param props2:
:type props2: ImageProperties
:return: true if the two image properties are considered equal.
:return: True if the two image properties are considered equal.
:rtype: bool"""
is_center_eq = props1.center == props2.center
is_size_eq = props1.size == props2.size
spacing1 = props1.spacing
spacing2 = props2.spacing
is_spacing_eq = abs(spacing1[0] - spacing2[0]) <= global_vars.GROUP_MAX_SPACING_DIFF and abs(spacing1[1] - spacing2[1]) <= global_vars.GROUP_MAX_SPACING_DIFF and abs(spacing1[2] - spacing2[2]) <= global_vars.GROUP_MAX_SPACING_DIFF
is_spacing_eq = (
abs(spacing1[0] - spacing2[0]) <= global_vars.GROUP_MAX_SPACING_DIFF
and abs(spacing1[1] - spacing2[1]) <= global_vars.GROUP_MAX_SPACING_DIFF
and abs(spacing1[2] - spacing2[2]) <= global_vars.GROUP_MAX_SPACING_DIFF
)

return is_center_eq and is_size_eq and is_spacing_eq

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ gui()
python cli.py <file>
```

See [test_cli.py](https://github.com/NIRALUser/NeuroRuler/blob/main/tests/test_cli.py) for example usages.
See [test_cli.py](https://github.com/NIRALUser/NeuroRuler/blob/main/tests/test_cli.py) for example usages. Note that the CLI script accepts only one file at a time because it's expected that you'll use a shell script to loop over multiple files.

```text
usage: cli.py [-h] [-d] [-r] [-x X] [-y Y] [-z Z] [-s SLICE] [-c CONDUCTANCE] [-i ITERATIONS] [-t STEP] [-f FILTER] [-l LOWER]
Expand Down
7 changes: 5 additions & 2 deletions gui_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
"STARTUP_WIDTH_RATIO": 0.75,
"STARTUP_HEIGHT_RATIO": 0.7,
"DISPLAY_ADVANCED_MENU_MESSAGES_IN_TERMINAL": "False",
// The maximum difference in pixel spacing (in mm) between two images of the global group, such that they may be considered to be the same spacing.
// If for some reason you don't want any tolerance, set this to 0.0.
// The maximum difference in pixel spacing (in mm) between any two images of the global group,
// such that they are considered to have the same spacing.
// For example, if pixel spacing is 0.0001, then the x spacing values of the two images have to be
// within 0.0001 of each other, and same for the y and z spacing values.
// If, for some reason, you don't want any tolerance, set this to 0.0.
"GROUP_MAX_SPACING_DIFF": 0.0001
}

0 comments on commit 0fa32e3

Please sign in to comment.