fsleyes.editor.selection¶
This module provides the Selection class, which represents a
selection of voxels in a 3D Image.
-
class
fsleyes.editor.selection.Selection(image, display, selection=None)[source]¶ Bases:
__main__.MockClassThe
Selectionclass represents a selection of voxels in a 3DImage. The selection is stored as anumpymask array, the same shape as the image. Methods are available to query and update the selection.Changes to a
Selectioncan be made through blocks, which are 3D cuboid regions. The following methods allow a block to be selected/deselected, where the block is specified by a voxel coordinate, and a block size:Selects the block (sets all voxels to 1) specified by the given voxel and block size.
De-selects the block (sets all voxels to 0) specified by the given voxel and box size.
The following methods offer more fine grained control over selection blocks - with these methods, you pass in a block that you have created yourself, and an offset into the selection, specifying its location:
Copies the given
blockinto the selection, starting atoffset.Copies the given
blockinto the selection, starting atoffset.Adds the selection (via a boolean OR operation) in the given
blockto the current selection, starting atoffset.Clears all voxels in the selection where the values in
blockare non-zero.A third approach to making a selection is provided by the
selectByValue()method, which allows a selection to be made in a manner similar to a bucket fill technique found in any image editor.The related
invertRegion()method, given a seed location, will invert the selected state of all voxels adjacent to that location. This approach allows a fill holes type approach, where a region outline is delineated, and then the interior inverted to select it.A
Selectionobject keeps track of the most recent change made through any of the above methods. The most recent change can be retrieved through thegetLastChange()method. TheSelectionclass inherits from theNotifierclass - you can be notified whenever the selection changes by registering as a listener.Finally, the
Selectionclass offers a few other methods for convenience:Returns the selection array.
Returns the number of voxels that are currently selected.
Clears (sets to 0) the entire selection, or the selection specified by the
restrictparameter, if it is given.Extracts the smallest region from the
selectionwhich contains all selected voxels.Returns a \(N \times 3\) array which contains the coordinates of all voxels that are currently selected.
-
__init__(image, display, selection=None)[source]¶ Create a
Selectioninstance.- Parameters
image – The
Imageinstance associated with thisSelection.display – The
Displayinstance for theimage.selection – Selection array. If not provided, one is created. Must be a
numpy.uint8array with the same shape asimage. This array is not copied.
-
property
shape¶ Returns the selection shape.
-
getSelection()[source]¶ Returns the selection array.
Warning
Do not modify the selection array directly - use the
Selectioninstance methods (e.g.setSelection()) instead. If you modify the selection directly through this attribute, thegetLastChange()method, and selection notification, will break.
-
selectBlock(voxel, boxSize, axes=(0, 1, 2), bias=None, combine=False)[source]¶ Selects the block (sets all voxels to 1) specified by the given voxel and block size. See the
routines.voxelBlock()function for details on the arguments.- Parameters
combine – Combine this change with the previous stored change (see
__storeChange()).
-
deselectBlock(voxel, boxSize, axes=(0, 1, 2), bias=None, combine=False)[source]¶ De-selects the block (sets all voxels to 0) specified by the given voxel and box size. See the
routines.voxelBlock()function for details on the arguments.- Parameters
combine – Combine this change with the previous stored change (see
__storeChange()).
-
setSelection(block, offset, combine=False)[source]¶ Copies the given
blockinto the selection, starting atoffset.- Parameters
block – A
numpy.uint8array containing a selection.offset – Voxel coordinates specifying the block location.
combine – Combine this change with the previous stored change (see
__storeChange()).
-
replaceSelection(block, offset, combine=False)[source]¶ Copies the given
blockinto the selection, starting atoffset.- Parameters
block – A
numpy.uint8array containing a selection.offset – Voxel coordinates specifying the block location.
combine – Combine this change with the previous stored change (see
__storeChange()).
-
addToSelection(block, offset, combine=False)[source]¶ Adds the selection (via a boolean OR operation) in the given
blockto the current selection, starting atoffset.- Parameters
block – A
numpy.uint8array containing a selection.offset – Voxel coordinates specifying the block location.
combine – Combine this change with the previous stored change (see
__storeChange()).
-
removeFromSelection(block, offset, combine=False)[source]¶ Clears all voxels in the selection where the values in
blockare non-zero.- Parameters
block – A
numpy.uint8array containing a selection.offset – Voxel coordinates specifying the block location.
combine – Combine this change with the previous stored change (see
__storeChange()).
-
getBoundedSelection()[source]¶ Extracts the smallest region from the
selectionwhich contains all selected voxels.Returns a tuple containing the region, as a
numpy.uint8array, and the coordinates specifying its location in the fullselectionarray.Warning
This method is slow, and in many cases it may be faster simply to access the full selection array.
-
clearSelection(restrict=None, combine=False)[source]¶ Clears (sets to 0) the entire selection, or the selection specified by the
restrictparameter, if it is given.Note
Calling this method when the selection is already empty will clear the most recently stored change - see
getLastChange().- Parameters
restrict – An optional sequence of three
sliceobjects, specifying the portion of the selection to clear.combine – Combine this change with the previous stored change (see
__storeChange()).
-
getLastChange()[source]¶ Returns the most recent change made to this
Selection.A tuple is returned, containing the following:
A
numpy.uint8array containing the old block valueA
numpy.uint8array containing the new block valueVoxel coordinates denoting the block location in the full
selectionarray.
If there is no stored change this method will return
(None, None, None)(see also the note inclearSelection()).
-
setChange(block, offset, oldBlock=None)[source]¶ Sets/overwrites the most recently saved change made to this
Selection.
-
getIndices(restrict=None)[source]¶ Returns a \(N \times 3\) array which contains the coordinates of all voxels that are currently selected.
If the
restrictargument is not provided, the entire selection image is searched.- Parameters
restrict – A
sliceobject specifying a sub-set of the full selection to consider.
-
selectByValue(seedLoc, precision=None, searchRadius=None, local=False, restrict=None, combine=False)[source]¶ A bucket fill style selection routine.
- Parameters
combine – Combine with the previous stored change (see
__storeChange()).
See the
selectByValue()function for details on the other arguments.- Returns
The generated selection array (a
numpyboolean array), and offset of this array into the full selection image.
-
invertRegion(seedLoc, restrict=None)[source]¶ Inverts the selected state of the region adjacent to
seedLoc.See the
selectByValue()function for details on the other arguments.
-
selectLine(from_, to, boxSize, axes=(0, 1, 2), bias=None, combine=False)[source]¶ Selects a line from
from_toto.- Parameters
combine – Combine with the previous stored change (see
__storeChange()).
See the
selectLine()function for details on the other arguments.
-
deselectLine(from_, to, boxSize, axes=(0, 1, 2), bias=None, combine=False)[source]¶ Deselects a line from
from_toto.- Parameters
combine – Combine with the previous stored change (see
__storeChange()).
See the
selectLine()function for details on the other arguments.
-
transferSelection(destImg, destDisplay)[source]¶ Re-samples the current selection into the destination image space.
Each
Selectioninstance is in terms of a specificImageinstance, which has a specific dimensionality. In order to apply aSelectionwhich is in terms of oneImage, the selection array needs to be re-sampled.- Parameters
destImg – The
Imagethat the selection is to be transferred to.destDisplay – The
Displayinstance associated withdestImg.
- Returns
a new
numpy.uint8array, suitable for creating a newSelectionobject for use with the givendestImg.
-
_Selection__getSelectionBlock(size, offset)¶ Extracts a block from the selection image starting from the specified
offset, and of the specifiedsize.
-
_Selection__storeChange(old, new, offset, combine=False)¶ Stores the given selection change.
- Parameters
old – A copy of the portion of the
selectionthat has changed,new – The new selection values.
offset – Offset into the full
selectionarraycombine – If
False(the default), the previously stored change will be replaced by the current change. Otherwise the previous and current changes will be combined.
-
_Selection__updateSelectionBlock(block, offset, combine=False)¶ Replaces the current selection at the specified
offsetwith the givenblock.The old values for the block are stored, and can be retrieved via the
getLastChange()method.- Parameters
block – A
numpy.uint8array containing the new selection values.offset – Voxel coordinates specifying the location of
block.combine – Combine with the previous stored change (see
__storeChange()).
-
__module__= 'fsleyes.editor.selection'¶
-
-
fsleyes.editor.selection.fixSlices(slices)[source]¶ A convenience function used by
selectByValue(),clearSelection()andgetIndices(), to sanitise theirrestrictparameter.
-
fsleyes.editor.selection.selectByValue(data, seedLoc, precision=None, searchRadius=None, local=False, restrict=None)[source]¶ A bucket fill style selection routine. Given a seed location, finds all voxels which have a value similar to that of that location. The current selection is replaced with all voxels that were found.
- Parameters
seedLoc – Voxel coordinates specifying the seed location
precision – Voxels which have a value that is less than
precisionfrom the seed location value will be selected.searchRadius – May be either a single value, or a sequence of three values - one for each axis. If provided, the search is limited to a sphere (in the voxel coordinate system), centred on the seed location, with the specified
searchRadius(in voxels). If not provided, the search will cover the entire image space.local – If
True, a voxel will only be selected if it is adjacent to an already selected voxel (using 8-neighbour connectivity).restrict – An optional sequence of three
sliceobject, specifying a sub-set of the image to search.
- Returns
The generated selection array (a
numpyboolean array), and offset of this array into the data.
-
fsleyes.editor.selection.selectLine(shape, dims, from_, to, boxSize, axes=(0, 1, 2), bias=None)[source]¶ Selects a continuous “line” in an array of the given
shape, between the pointsfrom_andto.- Parameters
shape – Shape of the image in which the selection is taking place.
dims – Size of one voxel along each axis (the pixdims).
from – Start point of the line
to – End point of the line
See the
routines.voxelBlock()function for details on the other arguments.- Returns
A tuple containing:
A 3D boolean
numpyarray containing the selected line.An offset of this array according to the
shapeof the full image. If the