diff --git a/.clang-format b/.clang-format index 411b009..92c5002 100644 --- a/.clang-format +++ b/.clang-format @@ -21,7 +21,7 @@ AccessModifierOffset: -2 AlignAfterOpenBracket: Align AlignConsecutiveAssignments: false AlignConsecutiveDeclarations: true -AlignEscapedNewlines: Right +AlignEscapedNewlines: Left AlignOperands: true AlignTrailingComments: true # clang 9.0 AllowAllArgumentsOnNextLine: true diff --git a/.editorconfig b/.editorconfig index 7b95893..648b969 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,4 +1,4 @@ -# http://EditorConfig.org +# https://EditorConfig.org # top-most EditorConfig file root = true diff --git a/.github/workflows/build-test-package.yml b/.github/workflows/build-test-package.yml index 41a3821..010ef05 100644 --- a/.github/workflows/build-test-package.yml +++ b/.github/workflows/build-test-package.yml @@ -4,14 +4,13 @@ on: [push,pull_request] jobs: cxx-build-workflow: - uses: InsightSoftwareConsortium/ITKRemoteModuleBuildTestPackageAction/.github/workflows/build-test-cxx.yml@246883ef3828fc00219145aeec9efa67b9889d0b + uses: InsightSoftwareConsortium/ITKRemoteModuleBuildTestPackageAction/.github/workflows/build-test-cxx.yml@b223658f3ab10680ab2ec3286d070b9601ea6ef1 with: cmake-options: "Module_Montage_BUILD_EXAMPLES:BOOL=ON" python-build-workflow: - uses: InsightSoftwareConsortium/ITKRemoteModuleBuildTestPackageAction/.github/workflows/build-test-package-python.yml@246883ef3828fc00219145aeec9efa67b9889d0b + uses: InsightSoftwareConsortium/ITKRemoteModuleBuildTestPackageAction/.github/workflows/build-test-package-python.yml@b223658f3ab10680ab2ec3286d070b9601ea6ef1 with: - manylinux-platforms: '["_2_28-x64","2014-x64"]' test-notebooks: true secrets: pypi_password: ${{ secrets.pypi_password }} diff --git a/.github/workflows/clang-format-linter.yml b/.github/workflows/clang-format-linter.yml index a4582b7..899d579 100644 --- a/.github/workflows/clang-format-linter.yml +++ b/.github/workflows/clang-format-linter.yml @@ -7,5 +7,6 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v1 + - uses: actions/checkout@v3 + - uses: InsightSoftwareConsortium/ITKClangFormatLinterAction@master diff --git a/include/itkTileMontage.h b/include/itkTileMontage.h index eea43ca..9948c7f 100644 --- a/include/itkTileMontage.h +++ b/include/itkTileMontage.h @@ -203,6 +203,24 @@ class ITK_TEMPLATE_EXPORT TileMontage : public ProcessObject return static_cast(this->GetOutput(this->nDIndexToLinearIndex(position)))->Get(); } + /** Reliability of each tile, highest normalized to 1.0. */ + using TileReliabilities = std::vector; + + /** Tile reliability, from 0.0 (lowest) to 1.0 (highest). + * This can be used to judge successfulness of registration to adjacent tiles. */ + itkGetConstReferenceMacro(TileReliabilities, TileReliabilities); + float + GetTileReliability(DataObjectPointerArraySizeType linearIndex) const + { + return m_TileReliabilities[linearIndex]; + } + float + GetTileReliability(TileIndexType nDIndex) const + { + DataObjectPointerArraySizeType linearIndex = nDIndexToLinearIndex(nDIndex); + return GetTileReliability(linearIndex); + } + protected: TileMontage(); ~TileMontage() override = default; @@ -241,6 +259,8 @@ class ITK_TEMPLATE_EXPORT TileMontage : public ProcessObject nDIndexToLinearIndex(TileIndexType nDIndex) const; TileIndexType LinearIndexTonDIndex(DataObjectPointerArraySizeType linearIndex) const; + DataObjectPointerArraySizeType + ReferenceLinearIndex(DataObjectPointerArraySizeType candidateIndex) const; /** Register a pair of images with given indices. Handles FFTcaching. */ void @@ -301,6 +321,7 @@ class ITK_TEMPLATE_EXPORT TileMontage : public ProcessObject std::vector m_TransformCandidates; // to adjacent tiles std::vector m_CandidateConfidences; std::vector m_CurrentAdjustments; + std::vector m_TileReliabilities; typename PCMOptimizerType::PeakInterpolationMethodEnum m_PeakInterpolationMethod = PCMOptimizerType::PeakInterpolationMethodEnum::Parabolic; diff --git a/include/itkTileMontage.hxx b/include/itkTileMontage.hxx index cdf00ff..1a075ca 100644 --- a/include/itkTileMontage.hxx +++ b/include/itkTileMontage.hxx @@ -114,6 +114,7 @@ TileMontage::SetMontageSize(SizeType montageSize) m_FFTCache.resize(m_LinearMontageSize); m_Tiles.resize(m_LinearMontageSize); m_CurrentAdjustments.resize(m_LinearMontageSize); + m_TileReliabilities.resize(m_LinearMontageSize); m_TransformCandidates.resize(ImageDimension * m_LinearMontageSize); // adjacency along each dimension m_CandidateConfidences.resize(ImageDimension * m_LinearMontageSize); this->Modified(); @@ -230,6 +231,19 @@ TileMontage::LinearIndexTonDIndex(DataObject::DataObjec return ind; } +template +auto +TileMontage::ReferenceLinearIndex(DataObjectPointerArraySizeType candidateIndex) const + -> DataObjectPointerArraySizeType +{ + SizeValueType linIndex = candidateIndex % m_LinearMontageSize; + TileIndexType currentIndex = this->LinearIndexTonDIndex(linIndex); + TileIndexType referenceIndex = currentIndex; + unsigned dim = candidateIndex / m_LinearMontageSize; + referenceIndex[dim] = currentIndex[dim] - 1; + return this->nDIndexToLinearIndex(referenceIndex); +} + template void TileMontage::RegisterPair(TileIndexType fixed, TileIndexType moving) @@ -421,11 +435,7 @@ TileMontage::OptimizeTiles() if (!m_TransformCandidates[i].empty()) { SizeValueType linIndex = i % m_LinearMontageSize; - TileIndexType currentIndex = this->LinearIndexTonDIndex(linIndex); - TileIndexType referenceIndex = currentIndex; - unsigned dim = i / m_LinearMontageSize; - referenceIndex[dim] = currentIndex[dim] - 1; - SizeValueType refLinearIndex = this->nDIndexToLinearIndex(referenceIndex); + SizeValueType refLinearIndex = this->ReferenceLinearIndex(i); // construct equation: -c*refLinearIndex + c*linIndex = c*candidateOffset, c=confidence const float & confidence = m_CandidateConfidences[i][0]; @@ -526,6 +536,8 @@ TileMontage::OptimizeTiles() std::cout << "\nresiduals:\n"; } + m_TileReliabilities.clear(); + m_TileReliabilities.resize(m_LinearMontageSize, 0.0); // reset all to zero for (SizeValueType i = 0; i < m_NumberOfPairs; i++) { TCoordinate residual = 0; @@ -569,6 +581,67 @@ TileMontage::OptimizeTiles() maxCost = cost; maxIndex = i; } + + // accumulate costs into tile reliabilities + SizeValueType linIndex = candidateIndex % m_LinearMontageSize; + SizeValueType refLinearIndex = this->ReferenceLinearIndex(candidateIndex); + m_TileReliabilities[linIndex] += cost; + m_TileReliabilities[refLinearIndex] += cost; + } + if (this->GetDebug()) + { + std::cout << std::endl; + } + + // now convert costs into reliabilities + float minReliabilityCost = std::numeric_limits::max(); + float maxReliabilityCost = 0.0; + // first divide by number of registrations + for (SizeValueType i = 0; i < m_LinearMontageSize; ++i) + { + TileIndexType nDIndex = LinearIndexTonDIndex(i); + unsigned regCount = 2 * Dimension; // for tiles in the middle + // now reduce this by one for each dimension's edge this finds itself on + for (unsigned d = 0; d < Dimension; ++d) + { + if (nDIndex[d] == 0 || nDIndex[d] == m_MontageSize[d] - 1) + { + --regCount; + } + } + assert(regCount >= 1); // every tile must have participated in at least on registration + m_TileReliabilities[i] /= regCount; + if (m_TileReliabilities[i] < minReliabilityCost) + { + minReliabilityCost = m_TileReliabilities[i]; + } + if (m_TileReliabilities[i] > maxReliabilityCost) + { + maxReliabilityCost = m_TileReliabilities[i]; + } + } + if (this->GetDebug()) + { + std::cout << "Reliabilities:"; + } + // now transform costs into (0.0, 1.0] reliability range + for (SizeValueType i = 0; i < m_LinearMontageSize; ++i) + { + // map minReliabilityCost to 1.0 reliability; map maxReliabilityCost to near zero (min/max) + float sourceRange = maxReliabilityCost - minReliabilityCost; + float newMin = minReliabilityCost / maxReliabilityCost; + float targetRange = 1.0 - newMin; + float fraction = (maxReliabilityCost - m_TileReliabilities[i]) / sourceRange; + m_TileReliabilities[i] = newMin + fraction * (targetRange); + if (this->GetDebug()) + { + if (i % m_MontageSize[0] == 0) + { + std::cout << '\n'; + } + std::cout << std::fixed << std::setprecision(4); + std::cout << " " << std::setw(6) << m_TileReliabilities[i]; + } } if (this->GetDebug()) { diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..611be3c --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,112 @@ +[build-system] +requires = ["scikit-build-core"] +build-backend = "scikit_build_core.build" + +[project] +name = "itk-montage" +version = "0.8.2" +description = "Montaging for microscopy imaging files." +readme = "README.md" +license = {file = "LICENSE"} +authors = [ + { name = "Dženan Zukić", email = "dzenan.zukic@kitware.com" }, + { name = "Matt McCormick", email = "matt.mccormick@kitware.com" }, +] +keywords = [ + "itk", +] +classifiers = [ + "Development Status :: 4 - Beta", + "Intended Audience :: Developers", + "Intended Audience :: Education", + "Intended Audience :: Healthcare Industry", + "Intended Audience :: Science/Research", + "License :: OSI Approved :: Apache Software License", + "Operating System :: Android", + "Operating System :: MacOS", + "Operating System :: Microsoft :: Windows", + "Operating System :: POSIX", + "Operating System :: Unix", + "Programming Language :: C++", + "Programming Language :: Python", + "Topic :: Scientific/Engineering", + "Topic :: Scientific/Engineering :: Information Analysis", + "Topic :: Scientific/Engineering :: Medical Science Apps.", + "Topic :: Software Development :: Libraries", +] +requires-python = ">=3.8" +dependencies = [ + "itk-core>=5.4rc4", + "itk-filtering>=5.4rc4", + "itk-io>=5.4rc4", + "numpy", +] + +[project.urls] +Download = "https://github.com/InsightSoftwareConsortium/ITKMontage" +Homepage = "https://github.com/InsightSoftwareConsortium/ITKMontage" + +[tool.scikit-build] +# The versions of CMake to allow. If CMake is not present on the system or does +# not pass this specifier, it will be downloaded via PyPI if possible. An empty +# string will disable this check. +cmake.version = ">=3.16.3" + +# A list of args to pass to CMake when configuring the project. Setting this in +# config or envvar will override toml. See also ``cmake.define``. +cmake.args = [] + +# A table of defines to pass to CMake when configuring the project. Additive. +cmake.define = {} + +# Verbose printout when building. +cmake.verbose = true + +# The build type to use when building the project. Valid options are: "Debug", +# "Release", "RelWithDebInfo", "MinSizeRel", "", etc. +cmake.build-type = "Release" + +# The source directory to use when building the project. Currently only affects +# the native builder (not the setuptools plugin). +cmake.source-dir = "." + +# The versions of Ninja to allow. If Ninja is not present on the system or does +# not pass this specifier, it will be downloaded via PyPI if possible. An empty +# string will disable this check. +ninja.version = ">=1.11" + +# The logging level to display, "DEBUG", "INFO", "WARNING", and "ERROR" are +# possible options. +logging.level = "INFO" + +# Files to include in the SDist even if they are skipped by default. Supports +# gitignore syntax. +sdist.include = [] + +# Files to exclude from the SDist even if they are included by default. Supports +# gitignore syntax. +sdist.exclude = [] + +# A list of license files to include in the wheel. Supports glob patterns. +wheel.license-files = ["LICEN[CS]E*",] + +# Target the platlib or the purelib. If not set, the default is to target the +# platlib if wheel.cmake is true, and the purelib otherwise. +wheel.platlib = "false" + +# If CMake is less than this value, backport a copy of FindPython. Set to 0 +# disable this, or the empty string. +backport.find-python = "3.26.1" + +# Select the editable mode to use. Can be "redirect" (default) or "inplace". +editable.mode = "redirect" + +# Rebuild the project when the package is imported. The build-directory must be +# set. +editable.rebuild = false + +# If set, this will provide a method for scikit-build-core backward compatibility. +minimum-version = "0.8.2" + +# The build directory. Defaults to a temporary directory, but can be set. +build-dir = "build/{wheel_tag}" diff --git a/setup.py b/setup.py deleted file mode 100644 index b446d18..0000000 --- a/setup.py +++ /dev/null @@ -1,56 +0,0 @@ -# -*- coding: utf-8 -*- -from __future__ import print_function -from os import sys - -try: - from skbuild import setup -except ImportError: - print('scikit-build is required to build from source.', file=sys.stderr) - print('Please run:', file=sys.stderr) - print('', file=sys.stderr) - print(' python -m pip install scikit-build') - sys.exit(1) - -setup( - name='itk-montage', - version='0.8.1', - author='Dženan Zukić, Matt McCormick', - author_email='itk+community@discourse.itk.org', - packages=['itk'], - package_dir={'itk': 'itk'}, - download_url=r'https://github.com/InsightSoftwareConsortium/ITKMontage', - description=r'Montaging for microscopy imaging files.', - long_description='itk-montage provides classes for montaging of ' - 'microscopy imaging files.\n' - 'Please refer to:\n' - 'Bican, J. "Phase Correlation Method for ITK" ' - 'Insight Journal, July-December 2006, https://hdl.handle.net/1926/396.', - classifiers=[ - "License :: OSI Approved :: Apache Software License", - "Programming Language :: Python", - "Programming Language :: C++", - "Development Status :: 4 - Beta", - "Intended Audience :: Developers", - "Intended Audience :: Education", - "Intended Audience :: Healthcare Industry", - "Intended Audience :: Science/Research", - "Topic :: Scientific/Engineering", - "Topic :: Scientific/Engineering :: Medical Science Apps.", - "Topic :: Scientific/Engineering :: Information Analysis", - "Topic :: Software Development :: Libraries", - "Operating System :: Android", - "Operating System :: Microsoft :: Windows", - "Operating System :: POSIX", - "Operating System :: Unix", - "Operating System :: MacOS" - ], - license='Apache', - keywords='ITK InsightToolkit Montage Image-stitching Image-montage', - url=r'https://github.com/InsightSoftwareConsortium/ITKMontage', - install_requires=[ - r'itk-core>=v5.3.0', - r'itk-filtering>=v5.3.0', - r'itk-io>=v5.3.0', - r'numpy' - ] - )