-
Notifications
You must be signed in to change notification settings - Fork 177
Add LabelVariable #832
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
Open
guyer
wants to merge
12
commits into
usnistgov:master
Choose a base branch
from
guyer:add_LabelVariable
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add LabelVariable #832
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6374e47
Add LabelVariable
guyer 8418d69
Merge branch 'master' into add_LabelVariable
guyer 5b86e0b
Fix initialization so that it actually works
guyer b677b42
Label Variable of any type and any dimension
guyer 126dfe2
Add doctests to LabelVariable
guyer dd5822a
Add LabelVariable to imports
guyer afbd79e
Factor out transformation of gridded CellVariable to ndimage
guyer 1c19a17
Reformat doctest
guyer e7cca1e
Fix spelling
guyer 6a01a53
Fix doctest whitespace
guyer 021578a
Fix typos
guyer 8b97fe2
Merge branch 'master' into add_LabelVariable
guyer 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
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
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
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,238 @@ | ||
| from __future__ import unicode_literals | ||
| __docformat__ = 'restructuredtext' | ||
|
|
||
| from fipy.variables.cellVariable import CellVariable | ||
| from fipy.tools import numerix | ||
|
|
||
| __all__ = ["LabelVariable"] | ||
| from future.utils import text_to_native_str | ||
| __all__ = [text_to_native_str(n) for n in __all__] | ||
|
|
||
| class LabelVariable(CellVariable): | ||
| """Label features in `var` using `scipy.ndimage.label` | ||
|
|
||
| >>> import fipy as fp | ||
|
|
||
| Create a 1D domain with two distinct non-zero regions. | ||
|
|
||
| >>> mesh = fp.Grid1D(nx=5) | ||
|
|
||
| >>> features = fp.CellVariable(mesh=mesh, name="features") | ||
| >>> features.setValue(1., where=(mesh.x > 1) & (mesh.x < 3)) | ||
| >>> features.setValue(0.5, where=(mesh.x > 4) & (mesh.x < 5)) | ||
| >>> print(features.ndimage) | ||
| [ 0. 1. 1. 0. 0.5] | ||
|
|
||
| Label the non-zero regions. | ||
|
|
||
| >>> labels = fp.LabelVariable(features, name="labels") | ||
| >>> print(labels.num_features) | ||
| 2 | ||
| >>> print(labels.ndimage) | ||
| [0 1 1 0 2] | ||
|
|
||
| To have no connectivity between cells, we can assign a structure with | ||
| no neighbors. | ||
|
|
||
| >>> labels = fp.LabelVariable(features, | ||
| ... structure=[0,1,0], | ||
| ... name="labels") | ||
| >>> print(labels.num_features) | ||
| 3 | ||
| >>> print(labels.ndimage) | ||
| [0 1 2 0 3] | ||
|
|
||
| Similarly, create a 2D domain with two distinct non-zero regions. | ||
|
|
||
| >>> mesh = fp.Grid2D(nx=5, ny=5) | ||
|
|
||
| >>> features = fp.CellVariable(mesh=mesh, name="features") | ||
| >>> features.setValue(1., where=((mesh.x > 1) & (mesh.x < 3) | ||
| ... & (mesh.y > 1) & (mesh.y < 3))) | ||
| >>> features.setValue(0.5, where=((mesh.x > 3) & (mesh.x < 4) | ||
| ... & (mesh.y > 3) & (mesh.y < 4))) | ||
|
|
||
| >>> print(features.ndimage) | ||
| [[ 0. 0. 0. 0. 0. ] | ||
| [ 0. 1. 1. 0. 0. ] | ||
| [ 0. 1. 1. 0. 0. ] | ||
| [ 0. 0. 0. 0.5 0. ] | ||
| [ 0. 0. 0. 0. 0. ]] | ||
|
|
||
| >>> labels = fp.LabelVariable(features, name="labels") | ||
| >>> print(labels.num_features) | ||
| 2 | ||
| >>> print(labels.ndimage) | ||
| [[0 0 0 0 0] | ||
| [0 1 1 0 0] | ||
| [0 1 1 0 0] | ||
| [0 0 0 2 0] | ||
| [0 0 0 0 0]] | ||
|
|
||
| By default, the two domains are seen as unconnected because there is no | ||
| overlap of cells along either the x or y axis. The following structure | ||
| creates connectivity along one diagonal, but not the other. Note that | ||
| the structure array follows NumPy (y, x) ordering, rather than FiPy (x, | ||
| y) ordering. | ||
|
|
||
| >>> labels = fp.LabelVariable(features, | ||
| ... structure=[[1,1,0], | ||
| ... [1,1,1], | ||
| ... [0,1,1]], | ||
| ... name="labels") | ||
| >>> print(labels.num_features) | ||
| 1 | ||
| >>> print(labels.ndimage) | ||
| [[0 0 0 0 0] | ||
| [0 1 1 0 0] | ||
| [0 1 1 0 0] | ||
| [0 0 0 1 0] | ||
| [0 0 0 0 0]] | ||
|
|
||
| Similarly, create a 3D domain with three distinct non-zero regions. | ||
|
|
||
| >>> mesh = fp.Grid3D(nx=3, ny=3, nz=3) | ||
|
|
||
| >>> features = fp.CellVariable(mesh=mesh, name="features") | ||
| >>> features.setValue(1., where=((mesh.x > 0) & (mesh.x < 2) | ||
| ... & (mesh.y > 0) & (mesh.y < 2) | ||
| ... & (mesh.z > 0) & (mesh.z < 2))) | ||
| >>> features.setValue(0.7, where=((mesh.x > 2) & (mesh.x < 3) | ||
| ... & (mesh.y > 2) & (mesh.y < 3) | ||
| ... & (mesh.z > 0) & (mesh.z < 1))) | ||
| >>> features.setValue(0.5, where=((mesh.x > 2) & (mesh.x < 3) | ||
| ... & (mesh.y > 2) & (mesh.y < 3) | ||
| ... & (mesh.z > 2) & (mesh.z < 3))) | ||
|
|
||
| >>> print(features.ndimage) | ||
| [[[ 1. 1. 0. ] | ||
| [ 1. 1. 0. ] | ||
| [ 0. 0. 0.7]] | ||
| <BLANKLINE> | ||
| [[ 1. 1. 0. ] | ||
| [ 1. 1. 0. ] | ||
| [ 0. 0. 0. ]] | ||
| <BLANKLINE> | ||
| [[ 0. 0. 0. ] | ||
| [ 0. 0. 0. ] | ||
| [ 0. 0. 0.5]]] | ||
|
|
||
| >>> labels = fp.LabelVariable(features, name="labels") | ||
| >>> print(labels.num_features) | ||
| 3 | ||
| >>> print(labels.ndimage) | ||
| [[[1 1 0] | ||
| [1 1 0] | ||
| [0 0 2]] | ||
| <BLANKLINE> | ||
| [[1 1 0] | ||
| [1 1 0] | ||
| [0 0 0]] | ||
| <BLANKLINE> | ||
| [[0 0 0] | ||
| [0 0 0] | ||
| [0 0 3]]] | ||
|
|
||
| By default, the three domains are seen as unconnected because there is | ||
| no overlap of cells along any of the x, y, or z axes. The following | ||
| structure creates connectivity along one major diagonal, but not the | ||
| other two. Note that the structure array follows NumPy (z, y, x) | ||
| ordering, rather than FiPy (x, y, z) ordering. | ||
|
|
||
| >>> labels = fp.LabelVariable(features, | ||
| ... structure=[[[1,0,0], | ||
| ... [0,1,0], | ||
| ... [0,0,0]], | ||
| ... [[0,1,0], | ||
| ... [1,1,1], | ||
| ... [0,1,0]], | ||
| ... [[0,0,0], | ||
| ... [0,1,0], | ||
| ... [0,0,1]]], | ||
| ... name="labels") | ||
| >>> print(labels.num_features) | ||
| 2 | ||
| >>> print(labels.ndimage) | ||
| [[[1 1 0] | ||
| [1 1 0] | ||
| [0 0 2]] | ||
| <BLANKLINE> | ||
| [[1 1 0] | ||
| [1 1 0] | ||
| [0 0 0]] | ||
| <BLANKLINE> | ||
| [[0 0 0] | ||
| [0 0 0] | ||
| [0 0 1]]] | ||
|
|
||
| Parameters | ||
| ---------- | ||
| var : ~fipy.variables.cellVariable.CellVariable | ||
| Field to be labeled. Any non-zero values in input are counted as | ||
| features and zero values are considered the background. | ||
|
|
||
| .. important: | ||
| Only sensible if `var` is defined on a `...Grid...` Mesh. | ||
| structure : array_like, optional | ||
| A structuring element that defines feature connections. | ||
| `structure` must be centrosymmetric | ||
| (see ```scipy.ndimage.label`` Notes | ||
| <https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.label.html#scipy.ndimage.label>`_). | ||
| If no structuring element is provided, | ||
| one is automatically generated with a squared connectivity equal to | ||
| one. That is, for a 2-D `input` array, the default structuring element | ||
| is:: | ||
| [[0,1,0], | ||
| [1,1,1], | ||
| [0,1,0]] | ||
guyer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| dtype : date-type, optional | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "data-type" |
||
| The desired data-type for the labels. Note that the type must be able | ||
| to store the largest label, or this Variable will raise an Exception. | ||
| Default: int. | ||
| """ | ||
| def __init__(self, var, name="", structure=None, dtype=int): | ||
| # We want our value to hold dtype, | ||
| # but if we pass an array, the CellVariable | ||
| # will probably be wonky | ||
| value = numerix.array(0.).astype(dtype).item() | ||
| CellVariable.__init__(self, | ||
| mesh=var.mesh, | ||
| name=name, | ||
| value=value, | ||
| elementshape=var.shape[:-1]) | ||
| self.var = self._requires(var) | ||
| self.structure = structure | ||
| self.dtype = dtype | ||
| self._num_features = None | ||
|
|
||
| def _calcValue(self): | ||
| """Label features of `var` | ||
|
|
||
| Side-effect: sets :attr:`self._num_features`. | ||
| """ | ||
| from scipy import ndimage | ||
|
|
||
| feat = self.var.globalValue | ||
| feat = feat.reshape(self.var.mesh.shape[::-1]) | ||
|
|
||
| arr = numerix.empty(self.var.mesh.shape[::-1], dtype=self.dtype) | ||
| self._num_features = ndimage.label(input=feat, | ||
| structure=self.structure, | ||
| output=arr) | ||
| return arr.flatten() | ||
|
|
||
| @property | ||
| def num_features(self): | ||
| """How many objects were found | ||
| """ | ||
| if self.stale or not self._isCached() or self._num_features is None: | ||
| self._getValue() | ||
|
|
||
| return self._num_features | ||
|
|
||
| def _test(): | ||
| import fipy.tests.doctestPlus | ||
| return fipy.tests.doctestPlus.testmod() | ||
|
|
||
| if __name__ == "__main__": | ||
| _test() | ||
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
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.