-
-
Notifications
You must be signed in to change notification settings - Fork 726
Wrap LabelSetMeasures in LabelOverlapMeasuresImageFilter #4221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
hjmjohnson
merged 4 commits into
InsightSoftwareConsortium:main
from
dzenanz:lsmLabelOverlap
May 4, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
79fc85a
ENH: Wrap LabelSetMeasures in LabelOverlapMeasuresImageFilter
dzenanz f704028
ENH: Add a Python test
dzenanz fff2d0d
ENH: Add Python-friendly LabelSetMeasures accessors
hjmjohnson e04e97e
COMP: Drop unused std::unordered_map SWIG infrastructure
hjmjohnson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
Modules/Filtering/ImageStatistics/include/itkLabelOverlapLabelSetMeasures.h
This file contains hidden or 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,76 @@ | ||
| /*========================================================================= | ||
| * | ||
| * Copyright NumFOCUS | ||
| * | ||
| * 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 | ||
| * | ||
| * https://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. | ||
| * | ||
| *=========================================================================*/ | ||
| #ifndef itkLabelOverlapLabelSetMeasures_h | ||
| #define itkLabelOverlapLabelSetMeasures_h | ||
|
|
||
| #include "itkIntTypes.h" | ||
|
|
||
| namespace itk | ||
| { | ||
| /** \class LabelOverlapLabelSetMeasures | ||
| * \brief Metrics stored per label | ||
| * \ingroup ITKImageStatistics | ||
| */ | ||
| struct LabelOverlapLabelSetMeasures | ||
| { | ||
| SizeValueType m_Source{ 0 }; | ||
| SizeValueType m_Target{ 0 }; | ||
| SizeValueType m_Union{ 0 }; | ||
| SizeValueType m_Intersection{ 0 }; | ||
| SizeValueType m_SourceComplement{ 0 }; | ||
| SizeValueType m_TargetComplement{ 0 }; | ||
|
|
||
| // ITK's igenerator wrapping pipeline does not expose public data members of | ||
| // a struct to Python. Provide explicit getters so wrapped consumers (Python | ||
| // tests, downstream bindings) can read these fields. Also provide | ||
| // descriptively-named aliases for the m_-prefixed forms; the m_Foo names | ||
| // remain accessible from C++ for backward compatibility with consumers that | ||
| // construct or mutate these values directly. | ||
| SizeValueType | ||
| GetSource() const | ||
| { | ||
| return m_Source; | ||
| } | ||
| SizeValueType | ||
| GetTarget() const | ||
| { | ||
| return m_Target; | ||
| } | ||
| SizeValueType | ||
| GetUnion() const | ||
| { | ||
| return m_Union; | ||
| } | ||
| SizeValueType | ||
| GetIntersection() const | ||
| { | ||
| return m_Intersection; | ||
| } | ||
| SizeValueType | ||
| GetSourceComplement() const | ||
| { | ||
| return m_SourceComplement; | ||
| } | ||
| SizeValueType | ||
| GetTargetComplement() const | ||
| { | ||
| return m_TargetComplement; | ||
| } | ||
| }; | ||
| } // namespace itk | ||
| #endif // itkLabelOverlapLabelSetMeasures_h |
This file contains hidden or 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
1 change: 1 addition & 0 deletions
1
Modules/Filtering/ImageStatistics/test/Input/DzZ_Seeds.seg.nrrd.sha512
This file contains hidden or 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 @@ | ||
| 4106f7a97659761a7fd42594a4f19aa9dad312445a1c9ff63f397f0c32cb952ee5d6ba6c6582951d883e6f40587091437a872c5fce966670d57f4984125c7a5d |
1 change: 1 addition & 0 deletions
1
Modules/Filtering/ImageStatistics/test/Input/DzZ_T1.seg.nrrd.sha512
This file contains hidden or 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 @@ | ||
| eb93426d1ee5f00d722979fd9dbfd8701f27bbb4256522e3d25c4db0f88f33b6a2db1ce572613e38c1a91787ca911e2399d206a137f7e987421b357bd5925b5d |
This file contains hidden or 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,2 +1,3 @@ | ||
| itk_wrap_module(ITKImageStatistics) | ||
| set(WRAPPER_SUBMODULE_ORDER itkLabelOverlapLabelSetMeasures) | ||
| itk_auto_load_and_end_wrap_submodules() |
1 change: 1 addition & 0 deletions
1
Modules/Filtering/ImageStatistics/wrapping/itkLabelOverlapLabelSetMeasures.wrap
This file contains hidden or 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 @@ | ||
| itk_wrap_simple_class("itk::LabelOverlapLabelSetMeasures") |
14 changes: 14 additions & 0 deletions
14
Modules/Filtering/ImageStatistics/wrapping/test/CMakeLists.txt
This file contains hidden or 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,14 @@ | ||
| set(test_input_dir ${itk-module_SOURCE_DIR}/test/Input) | ||
|
|
||
| # let's make sure 3D uchar images are wrapped | ||
| list(FIND ITK_WRAP_IMAGE_DIMS 3 wrap_3_index) | ||
| if(ITK_WRAP_PYTHON AND ITK_WRAP_unsigned_char AND wrap_3_index GREATER -1) | ||
| itk_python_add_test( | ||
| NAME LabelOverlapMeasuresImageFilterTest | ||
| TEST_DRIVER_ARGS | ||
| COMMAND | ||
| ${CMAKE_CURRENT_SOURCE_DIR}/itkLabelOverlapMeasuresImageFilterTest.py | ||
| DATA{${test_input_dir}/DzZ_T1.seg.nrrd} | ||
| DATA{${test_input_dir}/DzZ_Seeds.seg.nrrd} | ||
| ) | ||
| endif() |
51 changes: 51 additions & 0 deletions
51
Modules/Filtering/ImageStatistics/wrapping/test/itkLabelOverlapMeasuresImageFilterTest.py
This file contains hidden or 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,51 @@ | ||
| # ========================================================================== | ||
| # | ||
| # Copyright NumFOCUS | ||
| # | ||
| # 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 | ||
| # | ||
| # https://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 itk | ||
| from sys import argv | ||
|
|
||
| itk.auto_progress(2) | ||
|
|
||
| ref = itk.imread(argv[1], itk.UC) | ||
| seg = itk.imread(argv[2], itk.UC) | ||
|
|
||
| lom_filter = itk.LabelOverlapMeasuresImageFilter[itk.Image[itk.UC, 3]].New() | ||
| lom_filter.SetTargetImage(seg) | ||
| lom_filter.SetSourceImage(ref) | ||
| lom_filter.UpdateLargestPossibleRegion() | ||
|
|
||
| # GetLabelSetMeasures() returns std::unordered_map<LabelType, ...>, which SWIG | ||
| # cannot wrap as a Python dict across submodule boundaries. Use the paired | ||
| # accessors GetLabels() + GetMeasureForLabel() for Python iteration. | ||
| labels = list(lom_filter.GetLabels()) | ||
| print(f"Found {len(labels)} labels") | ||
| assert len(labels) > 0, "GetLabels() returned no labels" | ||
| for label in sorted(labels): | ||
| measure = lom_filter.GetMeasureForLabel(label) | ||
| # Use the explicit Get* accessors (igenerator does not expose public data | ||
| # members of a struct to Python; the m_Foo fields are not visible). | ||
| intersection = measure.GetIntersection() | ||
| union = measure.GetUnion() | ||
| print(f"Label: {label}, i: {intersection}, u: {union}") | ||
| assert ( | ||
| intersection >= 0 | ||
| ), f"Label {label}: intersection ({intersection}) must be non-negative" | ||
| assert ( | ||
| union >= intersection | ||
| ), f"Label {label}: union ({union}) must be >= intersection ({intersection})" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.