├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── __init__.py ├── setup.py ├── surface_distance ├── __init__.py ├── lookup_tables.py └── metrics.py └── surface_distance_test.py /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # How to Contribute 2 | 3 | We'd love to accept your patches and contributions to this project. There are just a few small guidelines you need to follow. 4 | 5 | ## Contributor License Agreement 6 | 7 | Contributions to this project must be accompanied by a Contributor License Agreement. You (or your employer) retain the copyright to your contribution; this simply gives us permission to use and redistribute your contributions as part of the project. Head over to to see your current agreements on file or to sign a new one. 8 | 9 | You generally only need to submit a CLA once, so if you've already submitted one (even if it was for a different project), you probably don't need to do it again. 10 | 11 | ## Code reviews 12 | 13 | All submissions, including submissions by project members, require review. We use GitHub pull requests for this purpose. Consult [GitHub Help](https://help.github.com/articles/about-pull-requests/) for more information on using pull requests. 14 | 15 | ## Community Guidelines 16 | 17 | This project follows [Google's Open Source Community Guidelines](https://opensource.google.com/conduct/). -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Surface distance metrics 2 | 3 | ## Summary 4 | When comparing multiple image segmentations, performance metrics that assess how closely the surfaces align can be a useful difference measure. This group of surface distance based measures computes the closest distances from all surface points on one segmentation to the points on another surface, and returns performance metrics between the two. This distance can be used alongside other metrics to compare segmented regions against a ground truth. 5 | 6 | Surfaces are represented using surface elements with corresponding area, allowing for more consistent approximation of surface measures. 7 | 8 | ## Metrics included 9 | This library computes the following performance metrics for segmentation: 10 | 11 | - Average surface distance (see `compute_average_surface_distance`) 12 | - Hausdorff distance (see `compute_robust_hausdorff`) 13 | - Surface overlap (see `compute_surface_overlap_at_tolerance`) 14 | - Surface dice (see `compute_surface_dice_at_tolerance`) 15 | - Volumetric dice (see `compute_dice_coefficient`) 16 | 17 | ## Installation 18 | First clone the repo, then install the dependencies and `surface-distance` 19 | package via pip: 20 | 21 | ```shell 22 | $ git clone https://github.com/deepmind/surface-distance.git 23 | $ pip install surface-distance/ 24 | ``` 25 | 26 | ## Usage 27 | For simple usage examples, see `surface_distance_test.py`. 28 | -------------------------------------------------------------------------------- /__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2018 Google Inc. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS-IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | """Surface distance module: https://github.com/deepmind/surface-distance .""" 15 | 16 | from .surface_distance import * # pylint: disable=wildcard-import 17 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | # Copyright 2018 Google Inc. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS-IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | """PyPI package definition.""" 15 | 16 | from setuptools import setup 17 | 18 | setup(name="surface-distance", 19 | version="0.1", 20 | description=( 21 | "Library containing utilities to compute performance metrics for " 22 | "segmentation"), 23 | long_description=open('README.md').read(), 24 | long_description_content_type='text/markdown', 25 | url="https://github.com/deepmind/surface-distance", 26 | author="DeepMind", 27 | license="Apache License, Version 2.0", 28 | packages=["surface_distance"], 29 | install_requires=["numpy", "scipy", "absl-py"]) 30 | -------------------------------------------------------------------------------- /surface_distance/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2018 Google Inc. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS-IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | """Surface distance module: https://github.com/deepmind/surface-distance .""" 15 | 16 | from .metrics import * # pylint: disable=wildcard-import 17 | __version__ = "0.1" 18 | -------------------------------------------------------------------------------- /surface_distance/lookup_tables.py: -------------------------------------------------------------------------------- 1 | # Copyright 2018 Google Inc. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS-IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | """Lookup tables used by surface distance metrics.""" 15 | 16 | from __future__ import absolute_import 17 | from __future__ import division 18 | from __future__ import print_function 19 | 20 | import math 21 | import numpy as np 22 | 23 | ENCODE_NEIGHBOURHOOD_3D_KERNEL = np.array([[[128, 64], [32, 16]], [[8, 4], 24 | [2, 1]]]) 25 | 26 | # _NEIGHBOUR_CODE_TO_NORMALS is a lookup table. 27 | # For every binary neighbour code 28 | # (2x2x2 neighbourhood = 8 neighbours = 8 bits = 256 codes) 29 | # it contains the surface normals of the triangles (called "surfel" for 30 | # "surface element" in the following). The length of the normal 31 | # vector encodes the surfel area. 32 | # 33 | # created using the marching_cube algorithm 34 | # see e.g. https://en.wikipedia.org/wiki/Marching_cubes 35 | # pylint: disable=line-too-long 36 | _NEIGHBOUR_CODE_TO_NORMALS = [ 37 | [[0, 0, 0]], 38 | [[0.125, 0.125, 0.125]], 39 | [[-0.125, -0.125, 0.125]], 40 | [[-0.25, -0.25, 0.0], [0.25, 0.25, -0.0]], 41 | [[0.125, -0.125, 0.125]], 42 | [[-0.25, -0.0, -0.25], [0.25, 0.0, 0.25]], 43 | [[0.125, -0.125, 0.125], [-0.125, -0.125, 0.125]], 44 | [[0.5, 0.0, -0.0], [0.25, 0.25, 0.25], [0.125, 0.125, 0.125]], 45 | [[-0.125, 0.125, 0.125]], 46 | [[0.125, 0.125, 0.125], [-0.125, 0.125, 0.125]], 47 | [[-0.25, 0.0, 0.25], [-0.25, 0.0, 0.25]], 48 | [[0.5, 0.0, 0.0], [-0.25, -0.25, 0.25], [-0.125, -0.125, 0.125]], 49 | [[0.25, -0.25, 0.0], [0.25, -0.25, 0.0]], 50 | [[0.5, 0.0, 0.0], [0.25, -0.25, 0.25], [-0.125, 0.125, -0.125]], 51 | [[-0.5, 0.0, 0.0], [-0.25, 0.25, 0.25], [-0.125, 0.125, 0.125]], 52 | [[0.5, 0.0, 0.0], [0.5, 0.0, 0.0]], 53 | [[0.125, -0.125, -0.125]], 54 | [[0.0, -0.25, -0.25], [0.0, 0.25, 0.25]], 55 | [[-0.125, -0.125, 0.125], [0.125, -0.125, -0.125]], 56 | [[0.0, -0.5, 0.0], [0.25, 0.25, 0.25], [0.125, 0.125, 0.125]], 57 | [[0.125, -0.125, 0.125], [0.125, -0.125, -0.125]], 58 | [[0.0, 0.0, -0.5], [0.25, 0.25, 0.25], [-0.125, -0.125, -0.125]], 59 | [[-0.125, -0.125, 0.125], [0.125, -0.125, 0.125], [0.125, -0.125, -0.125]], 60 | [[-0.125, -0.125, -0.125], [-0.25, -0.25, -0.25], [0.25, 0.25, 0.25], [0.125, 0.125, 0.125]], 61 | [[-0.125, 0.125, 0.125], [0.125, -0.125, -0.125]], 62 | [[0.0, -0.25, -0.25], [0.0, 0.25, 0.25], [-0.125, 0.125, 0.125]], 63 | [[-0.25, 0.0, 0.25], [-0.25, 0.0, 0.25], [0.125, -0.125, -0.125]], 64 | [[0.125, 0.125, 0.125], [0.375, 0.375, 0.375], [0.0, -0.25, 0.25], [-0.25, 0.0, 0.25]], 65 | [[0.125, -0.125, -0.125], [0.25, -0.25, 0.0], [0.25, -0.25, 0.0]], 66 | [[0.375, 0.375, 0.375], [0.0, 0.25, -0.25], [-0.125, -0.125, -0.125], [-0.25, 0.25, 0.0]], 67 | [[-0.5, 0.0, 0.0], [-0.125, -0.125, -0.125], [-0.25, -0.25, -0.25], [0.125, 0.125, 0.125]], 68 | [[-0.5, 0.0, 0.0], [-0.125, -0.125, -0.125], [-0.25, -0.25, -0.25]], 69 | [[0.125, -0.125, 0.125]], 70 | [[0.125, 0.125, 0.125], [0.125, -0.125, 0.125]], 71 | [[0.0, -0.25, 0.25], [0.0, 0.25, -0.25]], 72 | [[0.0, -0.5, 0.0], [0.125, 0.125, -0.125], [0.25, 0.25, -0.25]], 73 | [[0.125, -0.125, 0.125], [0.125, -0.125, 0.125]], 74 | [[0.125, -0.125, 0.125], [-0.25, -0.0, -0.25], [0.25, 0.0, 0.25]], 75 | [[0.0, -0.25, 0.25], [0.0, 0.25, -0.25], [0.125, -0.125, 0.125]], 76 | [[-0.375, -0.375, 0.375], [-0.0, 0.25, 0.25], [0.125, 0.125, -0.125], [-0.25, -0.0, -0.25]], 77 | [[-0.125, 0.125, 0.125], [0.125, -0.125, 0.125]], 78 | [[0.125, 0.125, 0.125], [0.125, -0.125, 0.125], [-0.125, 0.125, 0.125]], 79 | [[-0.0, 0.0, 0.5], [-0.25, -0.25, 0.25], [-0.125, -0.125, 0.125]], 80 | [[0.25, 0.25, -0.25], [0.25, 0.25, -0.25], [0.125, 0.125, -0.125], [-0.125, -0.125, 0.125]], 81 | [[0.125, -0.125, 0.125], [0.25, -0.25, 0.0], [0.25, -0.25, 0.0]], 82 | [[0.5, 0.0, 0.0], [0.25, -0.25, 0.25], [-0.125, 0.125, -0.125], [0.125, -0.125, 0.125]], 83 | [[0.0, 0.25, -0.25], [0.375, -0.375, -0.375], [-0.125, 0.125, 0.125], [0.25, 0.25, 0.0]], 84 | [[-0.5, 0.0, 0.0], [-0.25, -0.25, 0.25], [-0.125, -0.125, 0.125]], 85 | [[0.25, -0.25, 0.0], [-0.25, 0.25, 0.0]], 86 | [[0.0, 0.5, 0.0], [-0.25, 0.25, 0.25], [0.125, -0.125, -0.125]], 87 | [[0.0, 0.5, 0.0], [0.125, -0.125, 0.125], [-0.25, 0.25, -0.25]], 88 | [[0.0, 0.5, 0.0], [0.0, -0.5, 0.0]], 89 | [[0.25, -0.25, 0.0], [-0.25, 0.25, 0.0], [0.125, -0.125, 0.125]], 90 | [[-0.375, -0.375, -0.375], [-0.25, 0.0, 0.25], [-0.125, -0.125, -0.125], [-0.25, 0.25, 0.0]], 91 | [[0.125, 0.125, 0.125], [0.0, -0.5, 0.0], [-0.25, -0.25, -0.25], [-0.125, -0.125, -0.125]], 92 | [[0.0, -0.5, 0.0], [-0.25, -0.25, -0.25], [-0.125, -0.125, -0.125]], 93 | [[-0.125, 0.125, 0.125], [0.25, -0.25, 0.0], [-0.25, 0.25, 0.0]], 94 | [[0.0, 0.5, 0.0], [0.25, 0.25, -0.25], [-0.125, -0.125, 0.125], [-0.125, -0.125, 0.125]], 95 | [[-0.375, 0.375, -0.375], [-0.25, -0.25, 0.0], [-0.125, 0.125, -0.125], [-0.25, 0.0, 0.25]], 96 | [[0.0, 0.5, 0.0], [0.25, 0.25, -0.25], [-0.125, -0.125, 0.125]], 97 | [[0.25, -0.25, 0.0], [-0.25, 0.25, 0.0], [0.25, -0.25, 0.0], [0.25, -0.25, 0.0]], 98 | [[-0.25, -0.25, 0.0], [-0.25, -0.25, 0.0], [-0.125, -0.125, 0.125]], 99 | [[0.125, 0.125, 0.125], [-0.25, -0.25, 0.0], [-0.25, -0.25, 0.0]], 100 | [[-0.25, -0.25, 0.0], [-0.25, -0.25, 0.0]], 101 | [[-0.125, -0.125, 0.125]], 102 | [[0.125, 0.125, 0.125], [-0.125, -0.125, 0.125]], 103 | [[-0.125, -0.125, 0.125], [-0.125, -0.125, 0.125]], 104 | [[-0.125, -0.125, 0.125], [-0.25, -0.25, 0.0], [0.25, 0.25, -0.0]], 105 | [[0.0, -0.25, 0.25], [0.0, -0.25, 0.25]], 106 | [[0.0, 0.0, 0.5], [0.25, -0.25, 0.25], [0.125, -0.125, 0.125]], 107 | [[0.0, -0.25, 0.25], [0.0, -0.25, 0.25], [-0.125, -0.125, 0.125]], 108 | [[0.375, -0.375, 0.375], [0.0, -0.25, -0.25], [-0.125, 0.125, -0.125], [0.25, 0.25, 0.0]], 109 | [[-0.125, -0.125, 0.125], [-0.125, 0.125, 0.125]], 110 | [[0.125, 0.125, 0.125], [-0.125, -0.125, 0.125], [-0.125, 0.125, 0.125]], 111 | [[-0.125, -0.125, 0.125], [-0.25, 0.0, 0.25], [-0.25, 0.0, 0.25]], 112 | [[0.5, 0.0, 0.0], [-0.25, -0.25, 0.25], [-0.125, -0.125, 0.125], [-0.125, -0.125, 0.125]], 113 | [[-0.0, 0.5, 0.0], [-0.25, 0.25, -0.25], [0.125, -0.125, 0.125]], 114 | [[-0.25, 0.25, -0.25], [-0.25, 0.25, -0.25], [-0.125, 0.125, -0.125], [-0.125, 0.125, -0.125]], 115 | [[-0.25, 0.0, -0.25], [0.375, -0.375, -0.375], [0.0, 0.25, -0.25], [-0.125, 0.125, 0.125]], 116 | [[0.5, 0.0, 0.0], [-0.25, 0.25, -0.25], [0.125, -0.125, 0.125]], 117 | [[-0.25, 0.0, 0.25], [0.25, 0.0, -0.25]], 118 | [[-0.0, 0.0, 0.5], [-0.25, 0.25, 0.25], [-0.125, 0.125, 0.125]], 119 | [[-0.125, -0.125, 0.125], [-0.25, 0.0, 0.25], [0.25, 0.0, -0.25]], 120 | [[-0.25, -0.0, -0.25], [-0.375, 0.375, 0.375], [-0.25, -0.25, 0.0], [-0.125, 0.125, 0.125]], 121 | [[0.0, 0.0, -0.5], [0.25, 0.25, -0.25], [-0.125, -0.125, 0.125]], 122 | [[-0.0, 0.0, 0.5], [0.0, 0.0, 0.5]], 123 | [[0.125, 0.125, 0.125], [0.125, 0.125, 0.125], [0.25, 0.25, 0.25], [0.0, 0.0, 0.5]], 124 | [[0.125, 0.125, 0.125], [0.25, 0.25, 0.25], [0.0, 0.0, 0.5]], 125 | [[-0.25, 0.0, 0.25], [0.25, 0.0, -0.25], [-0.125, 0.125, 0.125]], 126 | [[-0.0, 0.0, 0.5], [0.25, -0.25, 0.25], [0.125, -0.125, 0.125], [0.125, -0.125, 0.125]], 127 | [[-0.25, 0.0, 0.25], [-0.25, 0.0, 0.25], [-0.25, 0.0, 0.25], [0.25, 0.0, -0.25]], 128 | [[0.125, -0.125, 0.125], [0.25, 0.0, 0.25], [0.25, 0.0, 0.25]], 129 | [[0.25, 0.0, 0.25], [-0.375, -0.375, 0.375], [-0.25, 0.25, 0.0], [-0.125, -0.125, 0.125]], 130 | [[-0.0, 0.0, 0.5], [0.25, -0.25, 0.25], [0.125, -0.125, 0.125]], 131 | [[0.125, 0.125, 0.125], [0.25, 0.0, 0.25], [0.25, 0.0, 0.25]], 132 | [[0.25, 0.0, 0.25], [0.25, 0.0, 0.25]], 133 | [[-0.125, -0.125, 0.125], [0.125, -0.125, 0.125]], 134 | [[0.125, 0.125, 0.125], [-0.125, -0.125, 0.125], [0.125, -0.125, 0.125]], 135 | [[-0.125, -0.125, 0.125], [0.0, -0.25, 0.25], [0.0, 0.25, -0.25]], 136 | [[0.0, -0.5, 0.0], [0.125, 0.125, -0.125], [0.25, 0.25, -0.25], [-0.125, -0.125, 0.125]], 137 | [[0.0, -0.25, 0.25], [0.0, -0.25, 0.25], [0.125, -0.125, 0.125]], 138 | [[0.0, 0.0, 0.5], [0.25, -0.25, 0.25], [0.125, -0.125, 0.125], [0.125, -0.125, 0.125]], 139 | [[0.0, -0.25, 0.25], [0.0, -0.25, 0.25], [0.0, -0.25, 0.25], [0.0, 0.25, -0.25]], 140 | [[0.0, 0.25, 0.25], [0.0, 0.25, 0.25], [0.125, -0.125, -0.125]], 141 | [[-0.125, 0.125, 0.125], [0.125, -0.125, 0.125], [-0.125, -0.125, 0.125]], 142 | [[-0.125, 0.125, 0.125], [0.125, -0.125, 0.125], [-0.125, -0.125, 0.125], [0.125, 0.125, 0.125]], 143 | [[-0.0, 0.0, 0.5], [-0.25, -0.25, 0.25], [-0.125, -0.125, 0.125], [-0.125, -0.125, 0.125]], 144 | [[0.125, 0.125, 0.125], [0.125, -0.125, 0.125], [0.125, -0.125, -0.125]], 145 | [[-0.0, 0.5, 0.0], [-0.25, 0.25, -0.25], [0.125, -0.125, 0.125], [0.125, -0.125, 0.125]], 146 | [[0.125, 0.125, 0.125], [-0.125, -0.125, 0.125], [0.125, -0.125, -0.125]], 147 | [[0.0, -0.25, -0.25], [0.0, 0.25, 0.25], [0.125, 0.125, 0.125]], 148 | [[0.125, 0.125, 0.125], [0.125, -0.125, -0.125]], 149 | [[0.5, 0.0, -0.0], [0.25, -0.25, -0.25], [0.125, -0.125, -0.125]], 150 | [[-0.25, 0.25, 0.25], [-0.125, 0.125, 0.125], [-0.25, 0.25, 0.25], [0.125, -0.125, -0.125]], 151 | [[0.375, -0.375, 0.375], [0.0, 0.25, 0.25], [-0.125, 0.125, -0.125], [-0.25, 0.0, 0.25]], 152 | [[0.0, -0.5, 0.0], [-0.25, 0.25, 0.25], [-0.125, 0.125, 0.125]], 153 | [[-0.375, -0.375, 0.375], [0.25, -0.25, 0.0], [0.0, 0.25, 0.25], [-0.125, -0.125, 0.125]], 154 | [[-0.125, 0.125, 0.125], [-0.25, 0.25, 0.25], [0.0, 0.0, 0.5]], 155 | [[0.125, 0.125, 0.125], [0.0, 0.25, 0.25], [0.0, 0.25, 0.25]], 156 | [[0.0, 0.25, 0.25], [0.0, 0.25, 0.25]], 157 | [[0.5, 0.0, -0.0], [0.25, 0.25, 0.25], [0.125, 0.125, 0.125], [0.125, 0.125, 0.125]], 158 | [[0.125, -0.125, 0.125], [-0.125, -0.125, 0.125], [0.125, 0.125, 0.125]], 159 | [[-0.25, -0.0, -0.25], [0.25, 0.0, 0.25], [0.125, 0.125, 0.125]], 160 | [[0.125, 0.125, 0.125], [0.125, -0.125, 0.125]], 161 | [[-0.25, -0.25, 0.0], [0.25, 0.25, -0.0], [0.125, 0.125, 0.125]], 162 | [[0.125, 0.125, 0.125], [-0.125, -0.125, 0.125]], 163 | [[0.125, 0.125, 0.125], [0.125, 0.125, 0.125]], 164 | [[0.125, 0.125, 0.125]], 165 | [[0.125, 0.125, 0.125]], 166 | [[0.125, 0.125, 0.125], [0.125, 0.125, 0.125]], 167 | [[0.125, 0.125, 0.125], [-0.125, -0.125, 0.125]], 168 | [[-0.25, -0.25, 0.0], [0.25, 0.25, -0.0], [0.125, 0.125, 0.125]], 169 | [[0.125, 0.125, 0.125], [0.125, -0.125, 0.125]], 170 | [[-0.25, -0.0, -0.25], [0.25, 0.0, 0.25], [0.125, 0.125, 0.125]], 171 | [[0.125, -0.125, 0.125], [-0.125, -0.125, 0.125], [0.125, 0.125, 0.125]], 172 | [[0.5, 0.0, -0.0], [0.25, 0.25, 0.25], [0.125, 0.125, 0.125], [0.125, 0.125, 0.125]], 173 | [[0.0, 0.25, 0.25], [0.0, 0.25, 0.25]], 174 | [[0.125, 0.125, 0.125], [0.0, 0.25, 0.25], [0.0, 0.25, 0.25]], 175 | [[-0.125, 0.125, 0.125], [-0.25, 0.25, 0.25], [0.0, 0.0, 0.5]], 176 | [[-0.375, -0.375, 0.375], [0.25, -0.25, 0.0], [0.0, 0.25, 0.25], [-0.125, -0.125, 0.125]], 177 | [[0.0, -0.5, 0.0], [-0.25, 0.25, 0.25], [-0.125, 0.125, 0.125]], 178 | [[0.375, -0.375, 0.375], [0.0, 0.25, 0.25], [-0.125, 0.125, -0.125], [-0.25, 0.0, 0.25]], 179 | [[-0.25, 0.25, 0.25], [-0.125, 0.125, 0.125], [-0.25, 0.25, 0.25], [0.125, -0.125, -0.125]], 180 | [[0.5, 0.0, -0.0], [0.25, -0.25, -0.25], [0.125, -0.125, -0.125]], 181 | [[0.125, 0.125, 0.125], [0.125, -0.125, -0.125]], 182 | [[0.0, -0.25, -0.25], [0.0, 0.25, 0.25], [0.125, 0.125, 0.125]], 183 | [[0.125, 0.125, 0.125], [-0.125, -0.125, 0.125], [0.125, -0.125, -0.125]], 184 | [[-0.0, 0.5, 0.0], [-0.25, 0.25, -0.25], [0.125, -0.125, 0.125], [0.125, -0.125, 0.125]], 185 | [[0.125, 0.125, 0.125], [0.125, -0.125, 0.125], [0.125, -0.125, -0.125]], 186 | [[-0.0, 0.0, 0.5], [-0.25, -0.25, 0.25], [-0.125, -0.125, 0.125], [-0.125, -0.125, 0.125]], 187 | [[-0.125, 0.125, 0.125], [0.125, -0.125, 0.125], [-0.125, -0.125, 0.125], [0.125, 0.125, 0.125]], 188 | [[-0.125, 0.125, 0.125], [0.125, -0.125, 0.125], [-0.125, -0.125, 0.125]], 189 | [[0.0, 0.25, 0.25], [0.0, 0.25, 0.25], [0.125, -0.125, -0.125]], 190 | [[0.0, -0.25, -0.25], [0.0, 0.25, 0.25], [0.0, 0.25, 0.25], [0.0, 0.25, 0.25]], 191 | [[0.0, 0.0, 0.5], [0.25, -0.25, 0.25], [0.125, -0.125, 0.125], [0.125, -0.125, 0.125]], 192 | [[0.0, -0.25, 0.25], [0.0, -0.25, 0.25], [0.125, -0.125, 0.125]], 193 | [[0.0, -0.5, 0.0], [0.125, 0.125, -0.125], [0.25, 0.25, -0.25], [-0.125, -0.125, 0.125]], 194 | [[-0.125, -0.125, 0.125], [0.0, -0.25, 0.25], [0.0, 0.25, -0.25]], 195 | [[0.125, 0.125, 0.125], [-0.125, -0.125, 0.125], [0.125, -0.125, 0.125]], 196 | [[-0.125, -0.125, 0.125], [0.125, -0.125, 0.125]], 197 | [[0.25, 0.0, 0.25], [0.25, 0.0, 0.25]], 198 | [[0.125, 0.125, 0.125], [0.25, 0.0, 0.25], [0.25, 0.0, 0.25]], 199 | [[-0.0, 0.0, 0.5], [0.25, -0.25, 0.25], [0.125, -0.125, 0.125]], 200 | [[0.25, 0.0, 0.25], [-0.375, -0.375, 0.375], [-0.25, 0.25, 0.0], [-0.125, -0.125, 0.125]], 201 | [[0.125, -0.125, 0.125], [0.25, 0.0, 0.25], [0.25, 0.0, 0.25]], 202 | [[-0.25, -0.0, -0.25], [0.25, 0.0, 0.25], [0.25, 0.0, 0.25], [0.25, 0.0, 0.25]], 203 | [[-0.0, 0.0, 0.5], [0.25, -0.25, 0.25], [0.125, -0.125, 0.125], [0.125, -0.125, 0.125]], 204 | [[-0.25, 0.0, 0.25], [0.25, 0.0, -0.25], [-0.125, 0.125, 0.125]], 205 | [[0.125, 0.125, 0.125], [0.25, 0.25, 0.25], [0.0, 0.0, 0.5]], 206 | [[0.125, 0.125, 0.125], [0.125, 0.125, 0.125], [0.25, 0.25, 0.25], [0.0, 0.0, 0.5]], 207 | [[-0.0, 0.0, 0.5], [0.0, 0.0, 0.5]], 208 | [[0.0, 0.0, -0.5], [0.25, 0.25, -0.25], [-0.125, -0.125, 0.125]], 209 | [[-0.25, -0.0, -0.25], [-0.375, 0.375, 0.375], [-0.25, -0.25, 0.0], [-0.125, 0.125, 0.125]], 210 | [[-0.125, -0.125, 0.125], [-0.25, 0.0, 0.25], [0.25, 0.0, -0.25]], 211 | [[-0.0, 0.0, 0.5], [-0.25, 0.25, 0.25], [-0.125, 0.125, 0.125]], 212 | [[-0.25, 0.0, 0.25], [0.25, 0.0, -0.25]], 213 | [[0.5, 0.0, 0.0], [-0.25, 0.25, -0.25], [0.125, -0.125, 0.125]], 214 | [[-0.25, 0.0, -0.25], [0.375, -0.375, -0.375], [0.0, 0.25, -0.25], [-0.125, 0.125, 0.125]], 215 | [[-0.25, 0.25, -0.25], [-0.25, 0.25, -0.25], [-0.125, 0.125, -0.125], [-0.125, 0.125, -0.125]], 216 | [[-0.0, 0.5, 0.0], [-0.25, 0.25, -0.25], [0.125, -0.125, 0.125]], 217 | [[0.5, 0.0, 0.0], [-0.25, -0.25, 0.25], [-0.125, -0.125, 0.125], [-0.125, -0.125, 0.125]], 218 | [[-0.125, -0.125, 0.125], [-0.25, 0.0, 0.25], [-0.25, 0.0, 0.25]], 219 | [[0.125, 0.125, 0.125], [-0.125, -0.125, 0.125], [-0.125, 0.125, 0.125]], 220 | [[-0.125, -0.125, 0.125], [-0.125, 0.125, 0.125]], 221 | [[0.375, -0.375, 0.375], [0.0, -0.25, -0.25], [-0.125, 0.125, -0.125], [0.25, 0.25, 0.0]], 222 | [[0.0, -0.25, 0.25], [0.0, -0.25, 0.25], [-0.125, -0.125, 0.125]], 223 | [[0.0, 0.0, 0.5], [0.25, -0.25, 0.25], [0.125, -0.125, 0.125]], 224 | [[0.0, -0.25, 0.25], [0.0, -0.25, 0.25]], 225 | [[-0.125, -0.125, 0.125], [-0.25, -0.25, 0.0], [0.25, 0.25, -0.0]], 226 | [[-0.125, -0.125, 0.125], [-0.125, -0.125, 0.125]], 227 | [[0.125, 0.125, 0.125], [-0.125, -0.125, 0.125]], 228 | [[-0.125, -0.125, 0.125]], 229 | [[-0.25, -0.25, 0.0], [-0.25, -0.25, 0.0]], 230 | [[0.125, 0.125, 0.125], [-0.25, -0.25, 0.0], [-0.25, -0.25, 0.0]], 231 | [[-0.25, -0.25, 0.0], [-0.25, -0.25, 0.0], [-0.125, -0.125, 0.125]], 232 | [[-0.25, -0.25, 0.0], [-0.25, -0.25, 0.0], [-0.25, -0.25, 0.0], [0.25, 0.25, -0.0]], 233 | [[0.0, 0.5, 0.0], [0.25, 0.25, -0.25], [-0.125, -0.125, 0.125]], 234 | [[-0.375, 0.375, -0.375], [-0.25, -0.25, 0.0], [-0.125, 0.125, -0.125], [-0.25, 0.0, 0.25]], 235 | [[0.0, 0.5, 0.0], [0.25, 0.25, -0.25], [-0.125, -0.125, 0.125], [-0.125, -0.125, 0.125]], 236 | [[-0.125, 0.125, 0.125], [0.25, -0.25, 0.0], [-0.25, 0.25, 0.0]], 237 | [[0.0, -0.5, 0.0], [-0.25, -0.25, -0.25], [-0.125, -0.125, -0.125]], 238 | [[0.125, 0.125, 0.125], [0.0, -0.5, 0.0], [-0.25, -0.25, -0.25], [-0.125, -0.125, -0.125]], 239 | [[-0.375, -0.375, -0.375], [-0.25, 0.0, 0.25], [-0.125, -0.125, -0.125], [-0.25, 0.25, 0.0]], 240 | [[0.25, -0.25, 0.0], [-0.25, 0.25, 0.0], [0.125, -0.125, 0.125]], 241 | [[0.0, 0.5, 0.0], [0.0, -0.5, 0.0]], 242 | [[0.0, 0.5, 0.0], [0.125, -0.125, 0.125], [-0.25, 0.25, -0.25]], 243 | [[0.0, 0.5, 0.0], [-0.25, 0.25, 0.25], [0.125, -0.125, -0.125]], 244 | [[0.25, -0.25, 0.0], [-0.25, 0.25, 0.0]], 245 | [[-0.5, 0.0, 0.0], [-0.25, -0.25, 0.25], [-0.125, -0.125, 0.125]], 246 | [[0.0, 0.25, -0.25], [0.375, -0.375, -0.375], [-0.125, 0.125, 0.125], [0.25, 0.25, 0.0]], 247 | [[0.5, 0.0, 0.0], [0.25, -0.25, 0.25], [-0.125, 0.125, -0.125], [0.125, -0.125, 0.125]], 248 | [[0.125, -0.125, 0.125], [0.25, -0.25, 0.0], [0.25, -0.25, 0.0]], 249 | [[0.25, 0.25, -0.25], [0.25, 0.25, -0.25], [0.125, 0.125, -0.125], [-0.125, -0.125, 0.125]], 250 | [[-0.0, 0.0, 0.5], [-0.25, -0.25, 0.25], [-0.125, -0.125, 0.125]], 251 | [[0.125, 0.125, 0.125], [0.125, -0.125, 0.125], [-0.125, 0.125, 0.125]], 252 | [[-0.125, 0.125, 0.125], [0.125, -0.125, 0.125]], 253 | [[-0.375, -0.375, 0.375], [-0.0, 0.25, 0.25], [0.125, 0.125, -0.125], [-0.25, -0.0, -0.25]], 254 | [[0.0, -0.25, 0.25], [0.0, 0.25, -0.25], [0.125, -0.125, 0.125]], 255 | [[0.125, -0.125, 0.125], [-0.25, -0.0, -0.25], [0.25, 0.0, 0.25]], 256 | [[0.125, -0.125, 0.125], [0.125, -0.125, 0.125]], 257 | [[0.0, -0.5, 0.0], [0.125, 0.125, -0.125], [0.25, 0.25, -0.25]], 258 | [[0.0, -0.25, 0.25], [0.0, 0.25, -0.25]], 259 | [[0.125, 0.125, 0.125], [0.125, -0.125, 0.125]], 260 | [[0.125, -0.125, 0.125]], 261 | [[-0.5, 0.0, 0.0], [-0.125, -0.125, -0.125], [-0.25, -0.25, -0.25]], 262 | [[-0.5, 0.0, 0.0], [-0.125, -0.125, -0.125], [-0.25, -0.25, -0.25], [0.125, 0.125, 0.125]], 263 | [[0.375, 0.375, 0.375], [0.0, 0.25, -0.25], [-0.125, -0.125, -0.125], [-0.25, 0.25, 0.0]], 264 | [[0.125, -0.125, -0.125], [0.25, -0.25, 0.0], [0.25, -0.25, 0.0]], 265 | [[0.125, 0.125, 0.125], [0.375, 0.375, 0.375], [0.0, -0.25, 0.25], [-0.25, 0.0, 0.25]], 266 | [[-0.25, 0.0, 0.25], [-0.25, 0.0, 0.25], [0.125, -0.125, -0.125]], 267 | [[0.0, -0.25, -0.25], [0.0, 0.25, 0.25], [-0.125, 0.125, 0.125]], 268 | [[-0.125, 0.125, 0.125], [0.125, -0.125, -0.125]], 269 | [[-0.125, -0.125, -0.125], [-0.25, -0.25, -0.25], [0.25, 0.25, 0.25], [0.125, 0.125, 0.125]], 270 | [[-0.125, -0.125, 0.125], [0.125, -0.125, 0.125], [0.125, -0.125, -0.125]], 271 | [[0.0, 0.0, -0.5], [0.25, 0.25, 0.25], [-0.125, -0.125, -0.125]], 272 | [[0.125, -0.125, 0.125], [0.125, -0.125, -0.125]], 273 | [[0.0, -0.5, 0.0], [0.25, 0.25, 0.25], [0.125, 0.125, 0.125]], 274 | [[-0.125, -0.125, 0.125], [0.125, -0.125, -0.125]], 275 | [[0.0, -0.25, -0.25], [0.0, 0.25, 0.25]], 276 | [[0.125, -0.125, -0.125]], 277 | [[0.5, 0.0, 0.0], [0.5, 0.0, 0.0]], 278 | [[-0.5, 0.0, 0.0], [-0.25, 0.25, 0.25], [-0.125, 0.125, 0.125]], 279 | [[0.5, 0.0, 0.0], [0.25, -0.25, 0.25], [-0.125, 0.125, -0.125]], 280 | [[0.25, -0.25, 0.0], [0.25, -0.25, 0.0]], 281 | [[0.5, 0.0, 0.0], [-0.25, -0.25, 0.25], [-0.125, -0.125, 0.125]], 282 | [[-0.25, 0.0, 0.25], [-0.25, 0.0, 0.25]], 283 | [[0.125, 0.125, 0.125], [-0.125, 0.125, 0.125]], 284 | [[-0.125, 0.125, 0.125]], 285 | [[0.5, 0.0, -0.0], [0.25, 0.25, 0.25], [0.125, 0.125, 0.125]], 286 | [[0.125, -0.125, 0.125], [-0.125, -0.125, 0.125]], 287 | [[-0.25, -0.0, -0.25], [0.25, 0.0, 0.25]], 288 | [[0.125, -0.125, 0.125]], 289 | [[-0.25, -0.25, 0.0], [0.25, 0.25, -0.0]], 290 | [[-0.125, -0.125, 0.125]], 291 | [[0.125, 0.125, 0.125]], 292 | [[0, 0, 0]]] 293 | # pylint: enable=line-too-long 294 | 295 | 296 | def create_table_neighbour_code_to_surface_area(spacing_mm): 297 | """Returns an array mapping neighbourhood code to the surface elements area. 298 | 299 | Note that the normals encode the initial surface area. This function computes 300 | the area corresponding to the given `spacing_mm`. 301 | 302 | Args: 303 | spacing_mm: 3-element list-like structure. Voxel spacing in x0, x1 and x2 304 | direction. 305 | """ 306 | # compute the area for all 256 possible surface elements 307 | # (given a 2x2x2 neighbourhood) according to the spacing_mm 308 | neighbour_code_to_surface_area = np.zeros([256]) 309 | for code in range(256): 310 | normals = np.array(_NEIGHBOUR_CODE_TO_NORMALS[code]) 311 | sum_area = 0 312 | for normal_idx in range(normals.shape[0]): 313 | # normal vector 314 | n = np.zeros([3]) 315 | n[0] = normals[normal_idx, 0] * spacing_mm[1] * spacing_mm[2] 316 | n[1] = normals[normal_idx, 1] * spacing_mm[0] * spacing_mm[2] 317 | n[2] = normals[normal_idx, 2] * spacing_mm[0] * spacing_mm[1] 318 | area = np.linalg.norm(n) 319 | sum_area += area 320 | neighbour_code_to_surface_area[code] = sum_area 321 | 322 | return neighbour_code_to_surface_area 323 | 324 | 325 | # In the neighbourhood, points are ordered: top left, top right, bottom left, 326 | # bottom right. 327 | ENCODE_NEIGHBOURHOOD_2D_KERNEL = np.array([[8, 4], [2, 1]]) 328 | 329 | 330 | def create_table_neighbour_code_to_contour_length(spacing_mm): 331 | """Returns an array mapping neighbourhood code to the contour length. 332 | 333 | For the list of possible cases and their figures, see page 38 from: 334 | https://nccastaff.bournemouth.ac.uk/jmacey/MastersProjects/MSc14/06/thesis.pdf 335 | 336 | In 2D, each point has 4 neighbors. Thus, are 16 configurations. A 337 | configuration is encoded with '1' meaning "inside the object" and '0' "outside 338 | the object". The points are ordered: top left, top right, bottom left, bottom 339 | right. 340 | 341 | The x0 axis is assumed vertical downward, and the x1 axis is horizontal to the 342 | right: 343 | (0, 0) --> (0, 1) 344 | | 345 | (1, 0) 346 | 347 | Args: 348 | spacing_mm: 2-element list-like structure. Voxel spacing in x0 and x1 349 | directions. 350 | """ 351 | neighbour_code_to_contour_length = np.zeros([16]) 352 | 353 | vertical = spacing_mm[0] 354 | horizontal = spacing_mm[1] 355 | diag = 0.5 * math.sqrt(spacing_mm[0]**2 + spacing_mm[1]**2) 356 | # pyformat: disable 357 | neighbour_code_to_contour_length[int("00" 358 | "01", 2)] = diag 359 | 360 | neighbour_code_to_contour_length[int("00" 361 | "10", 2)] = diag 362 | 363 | neighbour_code_to_contour_length[int("00" 364 | "11", 2)] = horizontal 365 | 366 | neighbour_code_to_contour_length[int("01" 367 | "00", 2)] = diag 368 | 369 | neighbour_code_to_contour_length[int("01" 370 | "01", 2)] = vertical 371 | 372 | neighbour_code_to_contour_length[int("01" 373 | "10", 2)] = 2*diag 374 | 375 | neighbour_code_to_contour_length[int("01" 376 | "11", 2)] = diag 377 | 378 | neighbour_code_to_contour_length[int("10" 379 | "00", 2)] = diag 380 | 381 | neighbour_code_to_contour_length[int("10" 382 | "01", 2)] = 2*diag 383 | 384 | neighbour_code_to_contour_length[int("10" 385 | "10", 2)] = vertical 386 | 387 | neighbour_code_to_contour_length[int("10" 388 | "11", 2)] = diag 389 | 390 | neighbour_code_to_contour_length[int("11" 391 | "00", 2)] = horizontal 392 | 393 | neighbour_code_to_contour_length[int("11" 394 | "01", 2)] = diag 395 | 396 | neighbour_code_to_contour_length[int("11" 397 | "10", 2)] = diag 398 | # pyformat: enable 399 | 400 | return neighbour_code_to_contour_length 401 | -------------------------------------------------------------------------------- /surface_distance/metrics.py: -------------------------------------------------------------------------------- 1 | # Copyright 2018 Google Inc. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS-IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | """Module exposing surface distance based measures.""" 15 | 16 | from __future__ import absolute_import 17 | from __future__ import division 18 | from __future__ import print_function 19 | 20 | from . import lookup_tables # pylint: disable=relative-beyond-top-level 21 | import numpy as np 22 | from scipy import ndimage 23 | 24 | 25 | def _assert_is_numpy_array(name, array): 26 | """Raises an exception if `array` is not a numpy array.""" 27 | if not isinstance(array, np.ndarray): 28 | raise ValueError("The argument {!r} should be a numpy array, not a " 29 | "{}".format(name, type(array))) 30 | 31 | 32 | def _check_nd_numpy_array(name, array, num_dims): 33 | """Raises an exception if `array` is not a `num_dims`-D numpy array.""" 34 | if len(array.shape) != num_dims: 35 | raise ValueError("The argument {!r} should be a {}D array, not of " 36 | "shape {}".format(name, num_dims, array.shape)) 37 | 38 | 39 | def _check_2d_numpy_array(name, array): 40 | _check_nd_numpy_array(name, array, num_dims=2) 41 | 42 | 43 | def _check_3d_numpy_array(name, array): 44 | _check_nd_numpy_array(name, array, num_dims=3) 45 | 46 | 47 | def _assert_is_bool_numpy_array(name, array): 48 | _assert_is_numpy_array(name, array) 49 | if array.dtype != bool: 50 | raise ValueError("The argument {!r} should be a numpy array of type bool, " 51 | "not {}".format(name, array.dtype)) 52 | 53 | 54 | def _compute_bounding_box(mask): 55 | """Computes the bounding box of the masks. 56 | 57 | This function generalizes to arbitrary number of dimensions great or equal 58 | to 1. 59 | 60 | Args: 61 | mask: The 2D or 3D numpy mask, where '0' means background and non-zero means 62 | foreground. 63 | 64 | Returns: 65 | A tuple: 66 | - The coordinates of the first point of the bounding box (smallest on all 67 | axes), or `None` if the mask contains only zeros. 68 | - The coordinates of the second point of the bounding box (greatest on all 69 | axes), or `None` if the mask contains only zeros. 70 | """ 71 | num_dims = len(mask.shape) 72 | bbox_min = np.zeros(num_dims, np.int64) 73 | bbox_max = np.zeros(num_dims, np.int64) 74 | 75 | # max projection to the x0-axis 76 | proj_0 = np.amax(mask, axis=tuple(range(num_dims))[1:]) 77 | idx_nonzero_0 = np.nonzero(proj_0)[0] 78 | if len(idx_nonzero_0) == 0: # pylint: disable=g-explicit-length-test 79 | return None, None 80 | 81 | bbox_min[0] = np.min(idx_nonzero_0) 82 | bbox_max[0] = np.max(idx_nonzero_0) 83 | 84 | # max projection to the i-th-axis for i in {1, ..., num_dims - 1} 85 | for axis in range(1, num_dims): 86 | max_over_axes = list(range(num_dims)) # Python 3 compatible 87 | max_over_axes.pop(axis) # Remove the i-th dimension from the max 88 | max_over_axes = tuple(max_over_axes) # numpy expects a tuple of ints 89 | proj = np.amax(mask, axis=max_over_axes) 90 | idx_nonzero = np.nonzero(proj)[0] 91 | bbox_min[axis] = np.min(idx_nonzero) 92 | bbox_max[axis] = np.max(idx_nonzero) 93 | 94 | return bbox_min, bbox_max 95 | 96 | 97 | def _crop_to_bounding_box(mask, bbox_min, bbox_max): 98 | """Crops a 2D or 3D mask to the bounding box specified by `bbox_{min,max}`.""" 99 | # we need to zeropad the cropped region with 1 voxel at the lower, 100 | # the right (and the back on 3D) sides. This is required to obtain the 101 | # "full" convolution result with the 2x2 (or 2x2x2 in 3D) kernel. 102 | # TODO: This is correct only if the object is interior to the 103 | # bounding box. 104 | cropmask = np.zeros((bbox_max - bbox_min) + 2, np.uint8) 105 | 106 | num_dims = len(mask.shape) 107 | # pyformat: disable 108 | if num_dims == 2: 109 | cropmask[0:-1, 0:-1] = mask[bbox_min[0]:bbox_max[0] + 1, 110 | bbox_min[1]:bbox_max[1] + 1] 111 | elif num_dims == 3: 112 | cropmask[0:-1, 0:-1, 0:-1] = mask[bbox_min[0]:bbox_max[0] + 1, 113 | bbox_min[1]:bbox_max[1] + 1, 114 | bbox_min[2]:bbox_max[2] + 1] 115 | # pyformat: enable 116 | else: 117 | assert False 118 | 119 | return cropmask 120 | 121 | 122 | def _sort_distances_surfels(distances, surfel_areas): 123 | """Sorts the two list with respect to the tuple of (distance, surfel_area). 124 | 125 | Args: 126 | distances: The distances from A to B (e.g. `distances_gt_to_pred`). 127 | surfel_areas: The surfel areas for A (e.g. `surfel_areas_gt`). 128 | 129 | Returns: 130 | A tuple of the sorted (distances, surfel_areas). 131 | """ 132 | sorted_surfels = np.array(sorted(zip(distances, surfel_areas))) 133 | return sorted_surfels[:, 0], sorted_surfels[:, 1] 134 | 135 | 136 | def compute_surface_distances(mask_gt, 137 | mask_pred, 138 | spacing_mm): 139 | """Computes closest distances from all surface points to the other surface. 140 | 141 | This function can be applied to 2D or 3D tensors. For 2D, both masks must be 142 | 2D and `spacing_mm` must be a 2-element list. For 3D, both masks must be 3D 143 | and `spacing_mm` must be a 3-element list. The description is done for the 2D 144 | case, and the formulation for the 3D case is present is parenthesis, 145 | introduced by "resp.". 146 | 147 | Finds all contour elements (resp surface elements "surfels" in 3D) in the 148 | ground truth mask `mask_gt` and the predicted mask `mask_pred`, computes their 149 | length in mm (resp. area in mm^2) and the distance to the closest point on the 150 | other contour (resp. surface). It returns two sorted lists of distances 151 | together with the corresponding contour lengths (resp. surfel areas). If one 152 | of the masks is empty, the corresponding lists are empty and all distances in 153 | the other list are `inf`. 154 | 155 | Args: 156 | mask_gt: 2-dim (resp. 3-dim) bool Numpy array. The ground truth mask. 157 | mask_pred: 2-dim (resp. 3-dim) bool Numpy array. The predicted mask. 158 | spacing_mm: 2-element (resp. 3-element) list-like structure. Voxel spacing 159 | in x0 anx x1 (resp. x0, x1 and x2) directions. 160 | 161 | Returns: 162 | A dict with: 163 | "distances_gt_to_pred": 1-dim numpy array of type float. The distances in mm 164 | from all ground truth surface elements to the predicted surface, 165 | sorted from smallest to largest. 166 | "distances_pred_to_gt": 1-dim numpy array of type float. The distances in mm 167 | from all predicted surface elements to the ground truth surface, 168 | sorted from smallest to largest. 169 | "surfel_areas_gt": 1-dim numpy array of type float. The length of the 170 | of the ground truth contours in mm (resp. the surface elements area in 171 | mm^2) in the same order as distances_gt_to_pred. 172 | "surfel_areas_pred": 1-dim numpy array of type float. The length of the 173 | of the predicted contours in mm (resp. the surface elements area in 174 | mm^2) in the same order as distances_gt_to_pred. 175 | 176 | Raises: 177 | ValueError: If the masks and the `spacing_mm` arguments are of incompatible 178 | shape or type. Or if the masks are not 2D or 3D. 179 | """ 180 | # The terms used in this function are for the 3D case. In particular, surface 181 | # in 2D stands for contours in 3D. The surface elements in 3D correspond to 182 | # the line elements in 2D. 183 | 184 | _assert_is_bool_numpy_array("mask_gt", mask_gt) 185 | _assert_is_bool_numpy_array("mask_pred", mask_pred) 186 | 187 | if not len(mask_gt.shape) == len(mask_pred.shape) == len(spacing_mm): 188 | raise ValueError("The arguments must be of compatible shape. Got mask_gt " 189 | "with {} dimensions ({}) and mask_pred with {} dimensions " 190 | "({}), while the spacing_mm was {} elements.".format( 191 | len(mask_gt.shape), 192 | mask_gt.shape, len(mask_pred.shape), mask_pred.shape, 193 | len(spacing_mm))) 194 | 195 | num_dims = len(spacing_mm) 196 | if num_dims == 2: 197 | _check_2d_numpy_array("mask_gt", mask_gt) 198 | _check_2d_numpy_array("mask_pred", mask_pred) 199 | 200 | # compute the area for all 16 possible surface elements 201 | # (given a 2x2 neighbourhood) according to the spacing_mm 202 | neighbour_code_to_surface_area = ( 203 | lookup_tables.create_table_neighbour_code_to_contour_length(spacing_mm)) 204 | kernel = lookup_tables.ENCODE_NEIGHBOURHOOD_2D_KERNEL 205 | full_true_neighbours = 0b1111 206 | elif num_dims == 3: 207 | _check_3d_numpy_array("mask_gt", mask_gt) 208 | _check_3d_numpy_array("mask_pred", mask_pred) 209 | 210 | # compute the area for all 256 possible surface elements 211 | # (given a 2x2x2 neighbourhood) according to the spacing_mm 212 | neighbour_code_to_surface_area = ( 213 | lookup_tables.create_table_neighbour_code_to_surface_area(spacing_mm)) 214 | kernel = lookup_tables.ENCODE_NEIGHBOURHOOD_3D_KERNEL 215 | full_true_neighbours = 0b11111111 216 | else: 217 | raise ValueError("Only 2D and 3D masks are supported, not " 218 | "{}D.".format(num_dims)) 219 | 220 | # compute the bounding box of the masks to trim the volume to the smallest 221 | # possible processing subvolume 222 | bbox_min, bbox_max = _compute_bounding_box(mask_gt | mask_pred) 223 | # Both the min/max bbox are None at the same time, so we only check one. 224 | if bbox_min is None: 225 | return { 226 | "distances_gt_to_pred": np.array([]), 227 | "distances_pred_to_gt": np.array([]), 228 | "surfel_areas_gt": np.array([]), 229 | "surfel_areas_pred": np.array([]), 230 | } 231 | 232 | # crop the processing subvolume. 233 | cropmask_gt = _crop_to_bounding_box(mask_gt, bbox_min, bbox_max) 234 | cropmask_pred = _crop_to_bounding_box(mask_pred, bbox_min, bbox_max) 235 | 236 | # compute the neighbour code (local binary pattern) for each voxel 237 | # the resulting arrays are spacially shifted by minus half a voxel in each 238 | # axis. 239 | # i.e. the points are located at the corners of the original voxels 240 | neighbour_code_map_gt = ndimage.filters.correlate( 241 | cropmask_gt.astype(np.uint8), kernel, mode="constant", cval=0) 242 | neighbour_code_map_pred = ndimage.filters.correlate( 243 | cropmask_pred.astype(np.uint8), kernel, mode="constant", cval=0) 244 | 245 | # create masks with the surface voxels 246 | borders_gt = ((neighbour_code_map_gt != 0) & 247 | (neighbour_code_map_gt != full_true_neighbours)) 248 | borders_pred = ((neighbour_code_map_pred != 0) & 249 | (neighbour_code_map_pred != full_true_neighbours)) 250 | 251 | # compute the distance transform (closest distance of each voxel to the 252 | # surface voxels) 253 | if borders_gt.any(): 254 | distmap_gt = ndimage.morphology.distance_transform_edt( 255 | ~borders_gt, sampling=spacing_mm) 256 | else: 257 | distmap_gt = np.inf * np.ones(borders_gt.shape) 258 | 259 | if borders_pred.any(): 260 | distmap_pred = ndimage.morphology.distance_transform_edt( 261 | ~borders_pred, sampling=spacing_mm) 262 | else: 263 | distmap_pred = np.inf * np.ones(borders_pred.shape) 264 | 265 | # compute the area of each surface element 266 | surface_area_map_gt = neighbour_code_to_surface_area[neighbour_code_map_gt] 267 | surface_area_map_pred = neighbour_code_to_surface_area[ 268 | neighbour_code_map_pred] 269 | 270 | # create a list of all surface elements with distance and area 271 | distances_gt_to_pred = distmap_pred[borders_gt] 272 | distances_pred_to_gt = distmap_gt[borders_pred] 273 | surfel_areas_gt = surface_area_map_gt[borders_gt] 274 | surfel_areas_pred = surface_area_map_pred[borders_pred] 275 | 276 | # sort them by distance 277 | if distances_gt_to_pred.shape != (0,): 278 | distances_gt_to_pred, surfel_areas_gt = _sort_distances_surfels( 279 | distances_gt_to_pred, surfel_areas_gt) 280 | 281 | if distances_pred_to_gt.shape != (0,): 282 | distances_pred_to_gt, surfel_areas_pred = _sort_distances_surfels( 283 | distances_pred_to_gt, surfel_areas_pred) 284 | 285 | return { 286 | "distances_gt_to_pred": distances_gt_to_pred, 287 | "distances_pred_to_gt": distances_pred_to_gt, 288 | "surfel_areas_gt": surfel_areas_gt, 289 | "surfel_areas_pred": surfel_areas_pred, 290 | } 291 | 292 | 293 | def compute_average_surface_distance(surface_distances): 294 | """Returns the average surface distance. 295 | 296 | Computes the average surface distances by correctly taking the area of each 297 | surface element into account. Call compute_surface_distances(...) before, to 298 | obtain the `surface_distances` dict. 299 | 300 | Args: 301 | surface_distances: dict with "distances_gt_to_pred", "distances_pred_to_gt" 302 | "surfel_areas_gt", "surfel_areas_pred" created by 303 | compute_surface_distances() 304 | 305 | Returns: 306 | A tuple with two float values: 307 | - the average distance (in mm) from the ground truth surface to the 308 | predicted surface 309 | - the average distance from the predicted surface to the ground truth 310 | surface. 311 | """ 312 | distances_gt_to_pred = surface_distances["distances_gt_to_pred"] 313 | distances_pred_to_gt = surface_distances["distances_pred_to_gt"] 314 | surfel_areas_gt = surface_distances["surfel_areas_gt"] 315 | surfel_areas_pred = surface_distances["surfel_areas_pred"] 316 | average_distance_gt_to_pred = ( 317 | np.sum(distances_gt_to_pred * surfel_areas_gt) / np.sum(surfel_areas_gt)) 318 | average_distance_pred_to_gt = ( 319 | np.sum(distances_pred_to_gt * surfel_areas_pred) / 320 | np.sum(surfel_areas_pred)) 321 | return (average_distance_gt_to_pred, average_distance_pred_to_gt) 322 | 323 | 324 | def compute_robust_hausdorff(surface_distances, percent): 325 | """Computes the robust Hausdorff distance. 326 | 327 | Computes the robust Hausdorff distance. "Robust", because it uses the 328 | `percent` percentile of the distances instead of the maximum distance. The 329 | percentage is computed by correctly taking the area of each surface element 330 | into account. 331 | 332 | Args: 333 | surface_distances: dict with "distances_gt_to_pred", "distances_pred_to_gt" 334 | "surfel_areas_gt", "surfel_areas_pred" created by 335 | compute_surface_distances() 336 | percent: a float value between 0 and 100. 337 | 338 | Returns: 339 | a float value. The robust Hausdorff distance in mm. 340 | """ 341 | distances_gt_to_pred = surface_distances["distances_gt_to_pred"] 342 | distances_pred_to_gt = surface_distances["distances_pred_to_gt"] 343 | surfel_areas_gt = surface_distances["surfel_areas_gt"] 344 | surfel_areas_pred = surface_distances["surfel_areas_pred"] 345 | if len(distances_gt_to_pred) > 0: # pylint: disable=g-explicit-length-test 346 | surfel_areas_cum_gt = np.cumsum(surfel_areas_gt) / np.sum(surfel_areas_gt) 347 | idx = np.searchsorted(surfel_areas_cum_gt, percent/100.0) 348 | perc_distance_gt_to_pred = distances_gt_to_pred[ 349 | min(idx, len(distances_gt_to_pred)-1)] 350 | else: 351 | perc_distance_gt_to_pred = np.inf 352 | 353 | if len(distances_pred_to_gt) > 0: # pylint: disable=g-explicit-length-test 354 | surfel_areas_cum_pred = (np.cumsum(surfel_areas_pred) / 355 | np.sum(surfel_areas_pred)) 356 | idx = np.searchsorted(surfel_areas_cum_pred, percent/100.0) 357 | perc_distance_pred_to_gt = distances_pred_to_gt[ 358 | min(idx, len(distances_pred_to_gt)-1)] 359 | else: 360 | perc_distance_pred_to_gt = np.inf 361 | 362 | return max(perc_distance_gt_to_pred, perc_distance_pred_to_gt) 363 | 364 | 365 | def compute_surface_overlap_at_tolerance(surface_distances, tolerance_mm): 366 | """Computes the overlap of the surfaces at a specified tolerance. 367 | 368 | Computes the overlap of the ground truth surface with the predicted surface 369 | and vice versa allowing a specified tolerance (maximum surface-to-surface 370 | distance that is regarded as overlapping). The overlapping fraction is 371 | computed by correctly taking the area of each surface element into account. 372 | 373 | Args: 374 | surface_distances: dict with "distances_gt_to_pred", "distances_pred_to_gt" 375 | "surfel_areas_gt", "surfel_areas_pred" created by 376 | compute_surface_distances() 377 | tolerance_mm: a float value. The tolerance in mm 378 | 379 | Returns: 380 | A tuple of two float values. The overlap fraction in [0.0, 1.0] of the 381 | ground truth surface with the predicted surface and vice versa. 382 | """ 383 | distances_gt_to_pred = surface_distances["distances_gt_to_pred"] 384 | distances_pred_to_gt = surface_distances["distances_pred_to_gt"] 385 | surfel_areas_gt = surface_distances["surfel_areas_gt"] 386 | surfel_areas_pred = surface_distances["surfel_areas_pred"] 387 | rel_overlap_gt = ( 388 | np.sum(surfel_areas_gt[distances_gt_to_pred <= tolerance_mm]) / 389 | np.sum(surfel_areas_gt)) 390 | rel_overlap_pred = ( 391 | np.sum(surfel_areas_pred[distances_pred_to_gt <= tolerance_mm]) / 392 | np.sum(surfel_areas_pred)) 393 | return (rel_overlap_gt, rel_overlap_pred) 394 | 395 | 396 | def compute_surface_dice_at_tolerance(surface_distances, tolerance_mm): 397 | """Computes the _surface_ DICE coefficient at a specified tolerance. 398 | 399 | Computes the _surface_ DICE coefficient at a specified tolerance. Not to be 400 | confused with the standard _volumetric_ DICE coefficient. The surface DICE 401 | measures the overlap of two surfaces instead of two volumes. A surface 402 | element is counted as overlapping (or touching), when the closest distance to 403 | the other surface is less or equal to the specified tolerance. The DICE 404 | coefficient is in the range between 0.0 (no overlap) to 1.0 (perfect overlap). 405 | 406 | Args: 407 | surface_distances: dict with "distances_gt_to_pred", "distances_pred_to_gt" 408 | "surfel_areas_gt", "surfel_areas_pred" created by 409 | compute_surface_distances() 410 | tolerance_mm: a float value. The tolerance in mm 411 | 412 | Returns: 413 | A float value. The surface DICE coefficient in [0.0, 1.0]. 414 | """ 415 | distances_gt_to_pred = surface_distances["distances_gt_to_pred"] 416 | distances_pred_to_gt = surface_distances["distances_pred_to_gt"] 417 | surfel_areas_gt = surface_distances["surfel_areas_gt"] 418 | surfel_areas_pred = surface_distances["surfel_areas_pred"] 419 | overlap_gt = np.sum(surfel_areas_gt[distances_gt_to_pred <= tolerance_mm]) 420 | overlap_pred = np.sum(surfel_areas_pred[distances_pred_to_gt <= tolerance_mm]) 421 | surface_dice = (overlap_gt + overlap_pred) / ( 422 | np.sum(surfel_areas_gt) + np.sum(surfel_areas_pred)) 423 | return surface_dice 424 | 425 | 426 | def compute_dice_coefficient(mask_gt, mask_pred): 427 | """Computes soerensen-dice coefficient. 428 | 429 | compute the soerensen-dice coefficient between the ground truth mask `mask_gt` 430 | and the predicted mask `mask_pred`. 431 | 432 | Args: 433 | mask_gt: 3-dim Numpy array of type bool. The ground truth mask. 434 | mask_pred: 3-dim Numpy array of type bool. The predicted mask. 435 | 436 | Returns: 437 | the dice coeffcient as float. If both masks are empty, the result is NaN. 438 | """ 439 | volume_sum = mask_gt.sum() + mask_pred.sum() 440 | if volume_sum == 0: 441 | return np.nan 442 | volume_intersect = (mask_gt & mask_pred).sum() 443 | return 2*volume_intersect / volume_sum 444 | -------------------------------------------------------------------------------- /surface_distance_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2018 Google Inc. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS-IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | """Simple tests for surface metric computations.""" 15 | 16 | from __future__ import absolute_import 17 | from __future__ import division 18 | from __future__ import print_function 19 | 20 | import math 21 | from absl.testing import absltest 22 | from absl.testing import parameterized 23 | import numpy as np 24 | import surface_distance 25 | from surface_distance.surface_distance import metrics 26 | 27 | 28 | class SurfaceDistanceTest(parameterized.TestCase, absltest.TestCase): 29 | 30 | def _assert_almost_equal(self, expected, actual, places): 31 | """Assertion wrapper correctly handling NaN equality.""" 32 | if np.isnan(expected) and np.isnan(actual): 33 | return 34 | self.assertAlmostEqual(expected, actual, places) 35 | 36 | def _assert_metrics(self, 37 | surface_distances, mask_gt, mask_pred, 38 | expected_average_surface_distance, 39 | expected_hausdorff_100, 40 | expected_hausdorff_95, 41 | expected_surface_overlap_at_1mm, 42 | expected_surface_dice_at_1mm, 43 | expected_volumetric_dice, 44 | places=3): 45 | actual_average_surface_distance = ( 46 | surface_distance.compute_average_surface_distance(surface_distances)) 47 | for i in range(2): 48 | self._assert_almost_equal( 49 | expected_average_surface_distance[i], 50 | actual_average_surface_distance[i], 51 | places=places) 52 | 53 | self._assert_almost_equal( 54 | expected_hausdorff_100, 55 | surface_distance.compute_robust_hausdorff(surface_distances, 100), 56 | places=places) 57 | 58 | self._assert_almost_equal( 59 | expected_hausdorff_95, 60 | surface_distance.compute_robust_hausdorff(surface_distances, 95), 61 | places=places) 62 | 63 | actual_surface_overlap_at_1mm = ( 64 | surface_distance.compute_surface_overlap_at_tolerance( 65 | surface_distances, tolerance_mm=1)) 66 | for i in range(2): 67 | self._assert_almost_equal( 68 | expected_surface_overlap_at_1mm[i], 69 | actual_surface_overlap_at_1mm[i], 70 | places=places) 71 | 72 | self._assert_almost_equal( 73 | expected_surface_dice_at_1mm, 74 | surface_distance.compute_surface_dice_at_tolerance( 75 | surface_distances, tolerance_mm=1), 76 | places=places) 77 | 78 | self._assert_almost_equal( 79 | expected_volumetric_dice, 80 | surface_distance.compute_dice_coefficient(mask_gt, mask_pred), 81 | places=places) 82 | 83 | @parameterized.parameters(( 84 | np.zeros([2, 2, 2], dtype=bool), 85 | np.zeros([2, 2], dtype=bool), 86 | [1, 1], 87 | ), ( 88 | np.zeros([2, 2], dtype=bool), 89 | np.zeros([2, 2, 2], dtype=bool), 90 | [1, 1], 91 | ), ( 92 | np.zeros([2, 2], dtype=bool), 93 | np.zeros([2, 2], dtype=bool), 94 | [1, 1, 1], 95 | )) 96 | def test_compute_surface_distances_raises_on_incompatible_shapes( 97 | self, mask_gt, mask_pred, spacing_mm): 98 | with self.assertRaisesRegex(ValueError, 99 | 'The arguments must be of compatible shape'): 100 | surface_distance.compute_surface_distances(mask_gt, mask_pred, spacing_mm) 101 | 102 | @parameterized.parameters(( 103 | np.zeros([2], dtype=bool), 104 | np.zeros([2], dtype=bool), 105 | [1], 106 | ), ( 107 | np.zeros([2, 2, 2, 2], dtype=bool), 108 | np.zeros([2, 2, 2, 2], dtype=bool), 109 | [1, 1, 1, 1], 110 | )) 111 | def test_compute_surface_distances_raises_on_invalid_shapes( 112 | self, mask_gt, mask_pred, spacing_mm): 113 | with self.assertRaisesRegex(ValueError, 114 | 'Only 2D and 3D masks are supported'): 115 | surface_distance.compute_surface_distances(mask_gt, mask_pred, spacing_mm) 116 | 117 | 118 | class SurfaceDistance2DTest(SurfaceDistanceTest, parameterized.TestCase): 119 | 120 | def test_on_2_pixels_2mm_away(self): 121 | mask_gt = np.zeros((128, 128), bool) 122 | mask_pred = np.zeros((128, 128), bool) 123 | mask_gt[50, 70] = 1 124 | mask_pred[50, 72] = 1 125 | surface_distances = surface_distance.compute_surface_distances( 126 | mask_gt, mask_pred, spacing_mm=(2, 1)) 127 | 128 | diag = 0.5 * math.sqrt(2**2 + 1**2) 129 | expected_distances = { 130 | 'surfel_areas_gt': np.asarray([diag, diag, diag, diag]), 131 | 'surfel_areas_pred': np.asarray([diag, diag, diag, diag]), 132 | 'distances_gt_to_pred': np.asarray([1., 1., 2., 2.]), 133 | 'distances_pred_to_gt': np.asarray([1., 1., 2., 2.]), 134 | } 135 | self.assertEqual(len(expected_distances), len(surface_distances)) 136 | for key, expected_value in expected_distances.items(): 137 | np.testing.assert_array_equal(expected_value, surface_distances[key]) 138 | 139 | self._assert_metrics( 140 | surface_distances, 141 | mask_gt, 142 | mask_pred, 143 | expected_average_surface_distance=(1.5, 1.5), 144 | expected_hausdorff_100=2.0, 145 | expected_hausdorff_95=2.0, 146 | expected_surface_overlap_at_1mm=(0.5, 0.5), 147 | expected_surface_dice_at_1mm=0.5, 148 | expected_volumetric_dice=0.0) 149 | 150 | def test_two_squares_shifted_by_one_pixel(self): 151 | # We make sure we do not have active pixels on the border of the image, 152 | # because this will add additional 2D surfaces on the border of the image 153 | # because the image is padded with background. 154 | mask_gt = np.asarray( 155 | [ 156 | [0, 0, 0, 0, 0, 0], 157 | [0, 1, 1, 0, 0, 0], 158 | [0, 1, 1, 0, 0, 0], 159 | [0, 0, 0, 0, 0, 0], 160 | [0, 0, 0, 0, 0, 0], 161 | [0, 0, 0, 0, 0, 0], 162 | ], 163 | dtype=bool) 164 | 165 | mask_pred = np.asarray( 166 | [ 167 | [0, 0, 0, 0, 0, 0], 168 | [0, 1, 1, 0, 0, 0], 169 | [0, 1, 1, 0, 0, 0], 170 | [0, 1, 1, 0, 0, 0], 171 | [0, 0, 0, 0, 0, 0], 172 | [0, 0, 0, 0, 0, 0], 173 | ], 174 | dtype=bool) 175 | 176 | vertical = 2 177 | horizontal = 1 178 | diag = 0.5 * math.sqrt(horizontal**2 + vertical**2) 179 | surface_distances = surface_distance.compute_surface_distances( 180 | mask_gt, mask_pred, spacing_mm=(vertical, horizontal)) 181 | 182 | # We go from top left corner, clockwise to describe the surfaces and 183 | # distances. The 2 surfaces are: 184 | # 185 | # /-\ /-\ 186 | # | | | | 187 | # \-/ | | 188 | # \-/ 189 | expected_surfel_areas_gt = np.asarray( 190 | [diag, horizontal, diag, vertical, diag, horizontal, diag, vertical]) 191 | expected_surfel_areas_pred = np.asarray([ 192 | diag, horizontal, diag, vertical, vertical, diag, horizontal, diag, 193 | vertical, vertical 194 | ]) 195 | expected_distances_gt_to_pred = np.asarray([0] * 5 + [horizontal] + [0] * 2) 196 | expected_distances_pred_to_gt = np.asarray([0] * 5 + [vertical] * 3 + 197 | [0] * 2) 198 | 199 | # We sort these using the same sorting algorithm 200 | (expected_distances_gt_to_pred, expected_surfel_areas_gt) = ( 201 | metrics._sort_distances_surfels(expected_distances_gt_to_pred, 202 | expected_surfel_areas_gt)) 203 | (expected_distances_pred_to_gt, expected_surfel_areas_pred) = ( 204 | metrics._sort_distances_surfels(expected_distances_pred_to_gt, 205 | expected_surfel_areas_pred)) 206 | 207 | expected_distances = { 208 | 'surfel_areas_gt': expected_surfel_areas_gt, 209 | 'surfel_areas_pred': expected_surfel_areas_pred, 210 | 'distances_gt_to_pred': expected_distances_gt_to_pred, 211 | 'distances_pred_to_gt': expected_distances_pred_to_gt, 212 | } 213 | 214 | self.assertEqual(len(expected_distances), len(surface_distances)) 215 | for key, expected_value in expected_distances.items(): 216 | np.testing.assert_array_equal(expected_value, surface_distances[key]) 217 | 218 | self._assert_metrics( 219 | surface_distances, 220 | mask_gt, 221 | mask_pred, 222 | expected_average_surface_distance=( 223 | surface_distance.compute_average_surface_distance( 224 | expected_distances)), 225 | expected_hausdorff_100=(surface_distance.compute_robust_hausdorff( 226 | expected_distances, 100)), 227 | expected_hausdorff_95=surface_distance.compute_robust_hausdorff( 228 | expected_distances, 95), 229 | expected_surface_overlap_at_1mm=( 230 | surface_distance.compute_surface_overlap_at_tolerance( 231 | expected_distances, tolerance_mm=1)), 232 | expected_surface_dice_at_1mm=( 233 | surface_distance.compute_surface_dice_at_tolerance( 234 | surface_distances, tolerance_mm=1)), 235 | expected_volumetric_dice=(surface_distance.compute_dice_coefficient( 236 | mask_gt, mask_pred))) 237 | 238 | def test_empty_prediction_mask(self): 239 | mask_gt = np.zeros((128, 128), bool) 240 | mask_pred = np.zeros((128, 128), bool) 241 | mask_gt[50, 60] = 1 242 | surface_distances = surface_distance.compute_surface_distances( 243 | mask_gt, mask_pred, spacing_mm=(3, 2)) 244 | self._assert_metrics( 245 | surface_distances, 246 | mask_gt, 247 | mask_pred, 248 | expected_average_surface_distance=(np.inf, np.nan), 249 | expected_hausdorff_100=np.inf, 250 | expected_hausdorff_95=np.inf, 251 | expected_surface_overlap_at_1mm=(0.0, np.nan), 252 | expected_surface_dice_at_1mm=0.0, 253 | expected_volumetric_dice=0.0) 254 | 255 | def test_empty_ground_truth_mask(self): 256 | mask_gt = np.zeros((128, 128), bool) 257 | mask_pred = np.zeros((128, 128), bool) 258 | mask_pred[50, 60] = 1 259 | surface_distances = surface_distance.compute_surface_distances( 260 | mask_gt, mask_pred, spacing_mm=(3, 2)) 261 | self._assert_metrics( 262 | surface_distances, 263 | mask_gt, 264 | mask_pred, 265 | expected_average_surface_distance=(np.nan, np.inf), 266 | expected_hausdorff_100=np.inf, 267 | expected_hausdorff_95=np.inf, 268 | expected_surface_overlap_at_1mm=(np.nan, 0.0), 269 | expected_surface_dice_at_1mm=0.0, 270 | expected_volumetric_dice=0.0) 271 | 272 | def test_both_empty_masks(self): 273 | mask_gt = np.zeros((128, 128), bool) 274 | mask_pred = np.zeros((128, 128), bool) 275 | surface_distances = surface_distance.compute_surface_distances( 276 | mask_gt, mask_pred, spacing_mm=(3, 2)) 277 | self._assert_metrics( 278 | surface_distances, 279 | mask_gt, 280 | mask_pred, 281 | expected_average_surface_distance=(np.nan, np.nan), 282 | expected_hausdorff_100=np.inf, 283 | expected_hausdorff_95=np.inf, 284 | expected_surface_overlap_at_1mm=(np.nan, np.nan), 285 | expected_surface_dice_at_1mm=np.nan, 286 | expected_volumetric_dice=np.nan) 287 | 288 | 289 | class SurfaceDistance3DTest(SurfaceDistanceTest): 290 | 291 | def test_on_2_pixels_2mm_away(self): 292 | mask_gt = np.zeros((128, 128, 128), bool) 293 | mask_pred = np.zeros((128, 128, 128), bool) 294 | mask_gt[50, 60, 70] = 1 295 | mask_pred[50, 60, 72] = 1 296 | surface_distances = surface_distance.compute_surface_distances( 297 | mask_gt, mask_pred, spacing_mm=(3, 2, 1)) 298 | self._assert_metrics(surface_distances, mask_gt, mask_pred, 299 | expected_average_surface_distance=(1.5, 1.5), 300 | expected_hausdorff_100=2.0, 301 | expected_hausdorff_95=2.0, 302 | expected_surface_overlap_at_1mm=(0.5, 0.5), 303 | expected_surface_dice_at_1mm=0.5, 304 | expected_volumetric_dice=0.0) 305 | 306 | def test_two_cubes_shifted_by_one_pixel(self): 307 | mask_gt = np.zeros((100, 100, 100), bool) 308 | mask_pred = np.zeros((100, 100, 100), bool) 309 | mask_gt[0:50, :, :] = 1 310 | mask_pred[0:51, :, :] = 1 311 | surface_distances = surface_distance.compute_surface_distances( 312 | mask_gt, mask_pred, spacing_mm=(2, 1, 1)) 313 | self._assert_metrics( 314 | surface_distances, mask_gt, mask_pred, 315 | expected_average_surface_distance=(0.322, 0.339), 316 | expected_hausdorff_100=2.0, 317 | expected_hausdorff_95=2.0, 318 | expected_surface_overlap_at_1mm=(0.842, 0.830), 319 | expected_surface_dice_at_1mm=0.836, 320 | expected_volumetric_dice=0.990) 321 | 322 | def test_empty_prediction_mask(self): 323 | mask_gt = np.zeros((128, 128, 128), bool) 324 | mask_pred = np.zeros((128, 128, 128), bool) 325 | mask_gt[50, 60, 70] = 1 326 | surface_distances = surface_distance.compute_surface_distances( 327 | mask_gt, mask_pred, spacing_mm=(3, 2, 1)) 328 | self._assert_metrics( 329 | surface_distances, mask_gt, mask_pred, 330 | expected_average_surface_distance=(np.inf, np.nan), 331 | expected_hausdorff_100=np.inf, 332 | expected_hausdorff_95=np.inf, 333 | expected_surface_overlap_at_1mm=(0.0, np.nan), 334 | expected_surface_dice_at_1mm=0.0, 335 | expected_volumetric_dice=0.0) 336 | 337 | def test_empty_ground_truth_mask(self): 338 | mask_gt = np.zeros((128, 128, 128), bool) 339 | mask_pred = np.zeros((128, 128, 128), bool) 340 | mask_pred[50, 60, 72] = 1 341 | surface_distances = surface_distance.compute_surface_distances( 342 | mask_gt, mask_pred, spacing_mm=(3, 2, 1)) 343 | self._assert_metrics( 344 | surface_distances, mask_gt, mask_pred, 345 | expected_average_surface_distance=(np.nan, np.inf), 346 | expected_hausdorff_100=np.inf, 347 | expected_hausdorff_95=np.inf, 348 | expected_surface_overlap_at_1mm=(np.nan, 0.0), 349 | expected_surface_dice_at_1mm=0.0, 350 | expected_volumetric_dice=0.0) 351 | 352 | def test_both_empty_masks(self): 353 | mask_gt = np.zeros((128, 128, 128), bool) 354 | mask_pred = np.zeros((128, 128, 128), bool) 355 | surface_distances = surface_distance.compute_surface_distances( 356 | mask_gt, mask_pred, spacing_mm=(3, 2, 1)) 357 | self._assert_metrics( 358 | surface_distances, mask_gt, mask_pred, 359 | expected_average_surface_distance=(np.nan, np.nan), 360 | expected_hausdorff_100=np.inf, 361 | expected_hausdorff_95=np.inf, 362 | expected_surface_overlap_at_1mm=(np.nan, np.nan), 363 | expected_surface_dice_at_1mm=np.nan, 364 | expected_volumetric_dice=np.nan) 365 | 366 | 367 | if __name__ == '__main__': 368 | absltest.main() 369 | --------------------------------------------------------------------------------