Class PathObjectHierarchy
- All Implemented Interfaces:
Serializable
This essentially contains a single PathRootObject from which all other objects can be reached using the child object lists. However, by adding/removing objects via this hierarchy (rather than through the child lists directly), it is possible to maintain a more consistent structure (e.g. by automatically inserting objects as children of the objects whose ROI completely contains the object to be added), along with a spatial cache so that objects can be extracted if their ROIs overlap with a specified region.
Note: Be cautious when deserializing - it may not result in a hierarchy in a valid state. As a workaround, you can construct a new PathObjectHierarchy and call setHierarchy(deserializedHierarchy) to ensure that you have a properly-constructed hierarchy with the same data within it.
- Author:
- Pete Bankhead
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic final Comparator
<PathObject> Comparator to use when looking for a parent annotation in the hierarchy. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addListener
(PathObjectHierarchyListener listener) Add a hierarchy change listener.boolean
addObject
(PathObject pathObject) Add an object to the hierarchy, firing an event.boolean
addObject
(PathObject pathObject, boolean fireUpdate) Add an object to the hierarchy, optionally firing an event.boolean
addObjectBelowParent
(PathObject pathObjectParent, PathObject pathObject, boolean fireUpdate) Add path object as descendant of the requested parent.boolean
addObjects
(Collection<? extends PathObject> pathObjects) Add multiple objects to the hierarchy.boolean
addPathObject
(PathObject pathObject) Deprecated.boolean
addPathObjectBelowParent
(PathObject pathObjectParent, PathObject pathObject, boolean fireUpdate) Deprecated.since v0.4.0; useaddObjectBelowParent(PathObject, PathObject, boolean)
void
Deprecated.since v0.4.0 (the name was confusing because it wasn't intended primarily to listen to changes within individual PathObjects)boolean
addPathObjects
(Collection<? extends PathObject> pathObjects) Deprecated.since v0.4.0; useaddObjects(Collection)
insteadboolean
addPathObjectWithoutUpdate
(PathObject pathObject) Deprecated.since v0.4.0, useaddObject(PathObject, boolean)
instead (for naming consistency)void
clearAll()
Remove all objects from the hierarchy.void
fireHierarchyChangedEvent
(Object source) Fire a hierarchy update indicating the hierarchy structure has changed.void
fireHierarchyChangedEvent
(Object source, PathObject pathObject) Fire a hierarchy update indicating the hierarchy structure has changed, impacting descendants of a specified object.void
fireObjectClassificationsChangedEvent
(Object source, Collection<? extends PathObject> pathObjects) Fire a hierarchy update indicating object classifications have changed.void
fireObjectMeasurementsChangedEvent
(Object source, Collection<? extends PathObject> pathObjects) Fire a hierarchy update indicating object measurements have changed.void
fireObjectMeasurementsChangedEvent
(Object source, Collection<? extends PathObject> pathObjects, boolean isChanging) Fire a hierarchy update indicating object measurements have changed.void
fireObjectsChangedEvent
(Object source, Collection<? extends PathObject> pathObjects) Fire a hierarchy update indicating objects have changed.void
fireObjectsChangedEvent
(Object source, Collection<? extends PathObject> pathObjects, boolean isChanging) Fire a hierarchy update indicating objects have changed, and may still be changing.getAllObjects
(boolean includeRoot) Get all the objects in the hierarchy, optionally including the root object.Get all annotation objects in the hierarchy.Get all cell objects in the hierarchy.Get all detection objects in the hierarchy (including sub-classes of detections).getFlattenedObjectList
(List<PathObject> list) Get a flattened list containing all PathObjects in the hierarchy (including from the root object).getObjects
(Collection<PathObject> pathObjects, Class<? extends PathObject> cls) Get all objects in the hierarchy, optionally filtering to return only objects that are instances of a specific class.getObjectsForRegion
(Class<? extends PathObject> cls, ImageRegion region, Collection<PathObject> pathObjects) Get the objects overlapping or close to a specified region.getObjectsForROI
(Class<? extends PathObject> cls, ROI roi) Get the objects within a specified ROI, as defined by the general rules for resolving the hierarchy.getPointObjects
(Class<? extends PathObject> cls) Get objects that contain Point ROIs.Get the root object.Get the selection model, which handles the selection status of objects.Get all tile objects in the hierarchy.Get the TMAGrid, or null if there is no TMA grid associated with this hierarchy.boolean
hasObjectsForRegion
(Class<? extends PathObject> cls, ImageRegion region) Returns true if the hierarchy contains objects overlapping a specific region, optionally filtering to identify only objects of a specific class.boolean
insertPathObject
(PathObject pathObject, boolean fireChangeEvents) Insert an object into the hierarchy.boolean
insertPathObjects
(Collection<? extends PathObject> pathObjects) Insert a collection of objects into the hierarchy, firing a change event on completion.boolean
isEmpty()
Check if the hierarchy is empty (i.e.int
nObjects()
Number of objects in the hierarchy, excluding the root.void
removeListener
(PathObjectHierarchyListener listener) Remove a hierarchy change listener.boolean
removeObject
(PathObject pathObject, boolean keepChildren) Remove a single object from the hierarchy, firing a remove event.void
removeObjects
(Collection<? extends PathObject> pathObjects, boolean keepChildren) Remove a collection of objects, firing a single 'hierarchy changed' event afterwards to notify listeners if anything happened (i.e.boolean
removeObjectWithoutUpdate
(PathObject pathObject, boolean keepChildren) Remove a single object from the hierarchy, without firing a remove event.void
Deprecated.since v0.4.0 (the name was confusing because it wasn't intended primarily to listen to changes within individual PathObjects)void
Attempt to resolve the parent-child relationships between all objects within the hierarchy.void
setHierarchy
(PathObjectHierarchy hierarchy) Set the contents of this hierarchy to be the same as that of another hierarchy.void
setTMAGrid
(TMAGrid tmaGrid) Set the tma grid for this hierarchy.toString()
void
updateObject
(PathObject pathObject, boolean isChanging) Update an object that is already in the hierarchy (e.g.
-
Field Details
-
HIERARCHY_COMPARATOR
Comparator to use when looking for a parent annotation in the hierarchy. The logic is:- Sort by area (smallest first)
- Sort by hierarchy level (deepest first)
- Sort by
DefaultPathObjectComparator
-
-
Constructor Details
-
PathObjectHierarchy
public PathObjectHierarchy()Default constructor, creates an empty hierarchy.
-
-
Method Details
-
isEmpty
public boolean isEmpty()Check if the hierarchy is empty (i.e. no objects apart from the root object, no feature maps)- Returns:
-
addListener
Add a hierarchy change listener.- Parameters:
listener
-- Since:
- v0.4.0; replaced
addPathObjectListener(PathObjectHierarchyListener)
-
addPathObjectListener
Deprecated.since v0.4.0 (the name was confusing because it wasn't intended primarily to listen to changes within individual PathObjects)Legacy method to add a hierarchy change listener; useaddListener(PathObjectHierarchyListener)
instead.- Parameters:
listener
-
-
removeListener
Remove a hierarchy change listener.- Parameters:
listener
-- Since:
- v0.4.0; replaced
removePathObjectListener(PathObjectHierarchyListener)
-
removePathObjectListener
Deprecated.since v0.4.0 (the name was confusing because it wasn't intended primarily to listen to changes within individual PathObjects)Legacy method to remove a hierarchy change listener; useremoveListener(PathObjectHierarchyListener)
instead.- Parameters:
listener
-
-
getRootObject
Get the root object. All other objects in the hierarchy are descendants of the root.- Returns:
-
getTMAGrid
Get the TMAGrid, or null if there is no TMA grid associated with this hierarchy.- Returns:
-
getSelectionModel
Get the selection model, which handles the selection status of objects.- Returns:
-
setTMAGrid
Set the tma grid for this hierarchy. If identical to the current TMA grid, nothing will happen. Otherwise, if null then any existing TMA grid will be removed.- Parameters:
tmaGrid
-
-
insertPathObject
Insert an object into the hierarchy. This differs fromaddObject(PathObject, boolean)
in that it will seek to place the object in an appropriate location relative to existing objects, using the logic ofHIERARCHY_COMPARATOR
.- Parameters:
pathObject
- the object to addfireChangeEvents
- if true, an event will be added after adding the object. Choose false if a single event should be added after making multiple changes.- Returns:
- true if the hierarchy changed as a result of this call, false otherwise
-
insertPathObjects
Insert a collection of objects into the hierarchy, firing a change event on completion. This differs fromaddObjects(Collection)
in that it will seek to place the object in an appropriate location relative to existing objects, using the logic ofHIERARCHY_COMPARATOR
.- Parameters:
pathObjects
- the objects to add- Returns:
- true if the hierarchy changed as a result of this call, false otherwise
-
resolveHierarchy
public void resolveHierarchy()Attempt to resolve the parent-child relationships between all objects within the hierarchy. -
removeObject
Remove a single object from the hierarchy, firing a remove event.- Parameters:
pathObject
- the object to removekeepChildren
- if true, retain all children and descendants of the object being removed; if false, remove these also- Returns:
-
removeObjectWithoutUpdate
Remove a single object from the hierarchy, without firing a remove event.- Parameters:
pathObject
- the object to removekeepChildren
- if true, retain all children and descendants of the object being removed; if false, remove these also- Returns:
-
removeObjects
Remove a collection of objects, firing a single 'hierarchy changed' event afterwards to notify listeners if anything happened (i.e. if any of the objects really were found within the hierarchy) & removed.- Parameters:
pathObjects
- the objects to removekeepChildren
- if true, retain children and descendants of the objects being removed
-
addObject
Add an object to the hierarchy, firing an event.- Parameters:
pathObject
-- Returns:
-
addObject
Add an object to the hierarchy, optionally firing an event.- Parameters:
pathObject
-fireUpdate
-- Returns:
- Since:
- v0.4.0; replaces
addPathObjectWithoutUpdate(PathObject)
-
addPathObject
Deprecated.since v0.4.0; useaddObject(PathObject)
instead (for naming consistency)Legacy method to add an object to the hierarchy, firing an event.- Parameters:
pathObject
-- Returns:
-
addPathObjectWithoutUpdate
Deprecated.since v0.4.0, useaddObject(PathObject, boolean)
instead (for naming consistency)Legacy method to add an object to the hierarchy, without firing an event.- Parameters:
pathObject
-- Returns:
-
addPathObjectBelowParent
@Deprecated public boolean addPathObjectBelowParent(PathObject pathObjectParent, PathObject pathObject, boolean fireUpdate) Deprecated.since v0.4.0; useaddObjectBelowParent(PathObject, PathObject, boolean)
Legacy method to path object as descendant of the requested parent.- Parameters:
pathObjectParent
-pathObject
-fireUpdate
-- Returns:
-
addObjectBelowParent
public boolean addObjectBelowParent(PathObject pathObjectParent, PathObject pathObject, boolean fireUpdate) Add path object as descendant of the requested parent.- Parameters:
pathObjectParent
-pathObject
-fireUpdate
-- Returns:
- Since:
- v0.4.0 (replaces
addPathObjectBelowParent(PathObject, PathObject, boolean)
-
addObjects
Add multiple objects to the hierarchy.- Parameters:
pathObjects
-- Returns:
- Since:
- v0.4.0; replaces
addPathObjects(Collection)
-
addPathObjects
Deprecated.since v0.4.0; useaddObjects(Collection)
insteadLegacy method to add multiple objects to the hierarchy.- Parameters:
pathObjects
-- Returns:
-
clearAll
public void clearAll()Remove all objects from the hierarchy. -
getPointObjects
Get objects that contain Point ROIs.- Parameters:
cls
-- Returns:
-
getCellObjects
Get all cell objects in the hierarchy.- Returns:
-
getTileObjects
Get all tile objects in the hierarchy.- Returns:
-
getDetectionObjects
Get all detection objects in the hierarchy (including sub-classes of detections).- Returns:
-
getAnnotationObjects
Get all annotation objects in the hierarchy.- Returns:
-
getObjects
public Collection<PathObject> getObjects(Collection<PathObject> pathObjects, Class<? extends PathObject> cls) Get all objects in the hierarchy, optionally filtering to return only objects that are instances of a specific class. Note that this method returns the root object, unless it has been filtered out.- Parameters:
pathObjects
-cls
-- Returns:
-
updateObject
Update an object that is already in the hierarchy (e.g. because its ROI has changed).- Parameters:
pathObject
- the object to updateisChanging
- if true, indicate that the object is still being changed. Some listeners may delay processing in expectation of an update event where isChanging is false.
-
getFlattenedObjectList
Get a flattened list containing all PathObjects in the hierarchy (including from the root object).To get a flattened list containing all
PathObject
s without the root object, one can run the following:
getFlattenedObjectList(null).stream().filter(p -> !p.isRootObject()).toList()
Or, since v0.4.0, just use
getAllObjects(boolean)
instead.- Parameters:
list
-- Returns:
- Since:
getAllObjects(boolean)
-
getAllObjects
Get all the objects in the hierarchy, optionally including the root object.- Parameters:
includeRoot
-- Returns:
- Since:
- v0.4.0
-
nObjects
public int nObjects()Number of objects in the hierarchy, excluding the root.- Returns:
-
setHierarchy
Set the contents of this hierarchy to be the same as that of another hierarchy. In practice, this means copying the root and TMA grid of the second hierarchy.- Parameters:
hierarchy
-
-
getObjectsForROI
Get the objects within a specified ROI, as defined by the general rules for resolving the hierarchy. This relies on centroids for detections, and a 'covers' rule for others.- Parameters:
cls
- class of PathObjects (e.g. PathDetectionObject), or null to accept allroi
-- Returns:
-
getObjectsForRegion
public Collection<PathObject> getObjectsForRegion(Class<? extends PathObject> cls, ImageRegion region, Collection<PathObject> pathObjects) Get the objects overlapping or close to a specified region. Note that this performs a quick check; the results typically should be filtered if a more strict test for overlapping is applied.- Parameters:
cls
- class of object to return (subclasses are included)region
- requested region overlapping the objects ROIpathObjects
- optionally collection to which objects will be added- Returns:
- collection containing identified objects (same as the input collection, if provided)
-
hasObjectsForRegion
Returns true if the hierarchy contains objects overlapping a specific region, optionally filtering to identify only objects of a specific class.- Parameters:
cls
-region
-- Returns:
-
fireObjectMeasurementsChangedEvent
public void fireObjectMeasurementsChangedEvent(Object source, Collection<? extends PathObject> pathObjects) Fire a hierarchy update indicating object measurements have changed.- Parameters:
source
-pathObjects
-
-
fireObjectMeasurementsChangedEvent
public void fireObjectMeasurementsChangedEvent(Object source, Collection<? extends PathObject> pathObjects, boolean isChanging) Fire a hierarchy update indicating object measurements have changed.- Parameters:
source
-pathObjects
-isChanging
-
-
fireObjectClassificationsChangedEvent
public void fireObjectClassificationsChangedEvent(Object source, Collection<? extends PathObject> pathObjects) Fire a hierarchy update indicating object classifications have changed.- Parameters:
source
-pathObjects
-
-
fireObjectsChangedEvent
Fire a hierarchy update indicating objects have changed.- Parameters:
source
-pathObjects
-
-
fireObjectsChangedEvent
public void fireObjectsChangedEvent(Object source, Collection<? extends PathObject> pathObjects, boolean isChanging) Fire a hierarchy update indicating objects have changed, and may still be changing.- Parameters:
source
-pathObjects
-isChanging
- is true, listeners may choose not to respond until an event is fired with isChanging false
-
fireHierarchyChangedEvent
Fire a hierarchy update indicating the hierarchy structure has changed, impacting descendants of a specified object.- Parameters:
source
-pathObject
-
-
fireHierarchyChangedEvent
Fire a hierarchy update indicating the hierarchy structure has changed. This is often a good choice of event if multiple changes may have occurred, of if one is unsure what exactly has changed.- Parameters:
source
-
-
toString
-
addObject(PathObject)
instead (for naming consistency)