Class PathClass
- All Implemented Interfaces:
Serializable
,Comparable<PathClass>
The constructors in this class should never be called directly, because there should only ever be one instance of each classification - shared among all objects with that classification. This is important for checking if classifications are identical, and also assigning colors to them for display.
To achieve this, be sure to use one of the getInstance()
or fromXXX()
methods each time
you want to access or create a new PathClass
instance.
This class has been with QuPath since the beginning, but was thoroughly revised for v0.4.0 to simplify the code, improve the validation, and make it easier to use.
- Author:
- Pete Bankhead
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Enum representing standard classifications. -
Field Summary
Modifier and TypeFieldDescriptionstatic final String
Get the delimiter to use between names of the PathClass when converting to a string.static final String
Default name for a class representing "Negative" staining intensitystatic final String
Default name for a class representing "1+" staining intensity (i.e.static final String
Default name for a class representing "Positive" staining intensitystatic final String
Default name for a class representing "3+" staining intensity (i.e.static final String
Default name for a class representing "2+" staining intensity (i.e.static final PathClass
Default PathClass that represents no classification. -
Method Summary
Modifier and TypeMethodDescriptionint
This is now equivalent tothis.toString().compareTo(o.toString())
.static PathClass
Get a PathClass from an array of individual names.static PathClass
fromCollection
(Collection<String> names) Get a PathClass using all the name elements specified in the collection, without providing a default color.static PathClass
fromCollection
(Collection<String> names, Integer color) Get a PathClass instance using all the name elements specified in the collection, with optional default color if a new instance is created.static PathClass
fromString
(String string) Get a PathClass instance from a string representation, without specifying a default color.static PathClass
fromString
(String string, Integer color) Get a PathClass instance from a string representation, optionally providing a default color if a new instance needs to be created.Get the 'base' class, i.e.getColor()
Get the color that should be used to display objects with this classification.static PathClass
getInstance
(String name) static PathClass
getInstance
(String name, Integer color) Get a base PathClass instance, without any parent PathClass.static PathClass
getInstance
(PathClass parent, String name, Integer color) Get a derived PathClass instance with the specified parent.getName()
Get the name of this classification.static PathClass
getNegative
(PathClass parentClass) Get a standalone or derived Negative classificationstatic PathClass
Get the value ofNULL_CLASS
, used to represent no classification.static PathClass
getOnePlus
(PathClass parentClass) Get a standalone or derived 1+ classification, indicating weak positivityGet the parent classification, or null if this classification has no parent.static PathClass
getPositive
(PathClass parentClass) Get a standalone or derived Positive classificationstatic PathClass
getSingleton
(PathClass pathClass) Get the singleton PathClass that is equivalent to the PathClass provided.static PathClass
getThreePlus
(PathClass parentClass) Get a standalone or derived 3+ classification, indicating strong positivitystatic PathClass
getTwoPlus
(PathClass parentClass) Get a standalone or derived 2+ classification, indicating moderate positivityboolean
isAncestorOf
(PathClass childClass) Returnstrue
if this class is equal to the specified child class, or an ancestor of that class.boolean
Returnstrue
if#getParentClass() != null
.boolean
isDerivedFrom
(PathClass parentClass) Returnstrue
if this class, or any ancestor class, is equal to the specified parent class.boolean
isValid()
A PathClass is valid if its name is not null.protected Object
Called during deserialization to ensure that only a single instance of each PathClass exists.void
setColor
(int red, int green, int blue) Set the color as 8-bit RGB valuesvoid
Set the color that should be used to display objects with this classification.toSet()
Return a view of this path class as an unmodifiable set, with each element representing the name of a path class component names.toString()
Create a string representation, using the specified delimiter between elements of derived PathClasses.
-
Field Details
-
NAME_POSITIVE
Default name for a class representing "Positive" staining intensity- See Also:
-
NAME_NEGATIVE
Default name for a class representing "Negative" staining intensity- See Also:
-
NAME_ONE_PLUS
Default name for a class representing "1+" staining intensity (i.e. weakly positive)- See Also:
-
NAME_TWO_PLUS
Default name for a class representing "2+" staining intensity (i.e. moderately positive)- See Also:
-
NAME_THREE_PLUS
Default name for a class representing "3+" staining intensity (i.e. strongly positive)- See Also:
-
DELIMITER
Get the delimiter to use between names of the PathClass when converting to a string. -
NULL_CLASS
Default PathClass that represents no classification. Usually no classification is represented bynull
, so this is not normally needed; however, sometimes it is required in contexts where a null is not permitted (e.g. some collections).
-
-
Method Details
-
getParentClass
Get the parent classification, or null if this classification has no parent.- Returns:
-
isDerivedClass
public boolean isDerivedClass()Returnstrue
if#getParentClass() != null
.- Returns:
-
isDerivedFrom
Returnstrue
if this class, or any ancestor class, is equal to the specified parent class.- Parameters:
parentClass
-- Returns:
-
isAncestorOf
Returnstrue
if this class is equal to the specified child class, or an ancestor of that class.- Parameters:
childClass
-- Returns:
-
getBaseClass
Get the 'base' class, i.e. trace back throughgetParentClass()
until no parent is available.For a PathClass with no parent, this just returns itself.
- Returns:
-
setColor
Set the color that should be used to display objects with this classification.- Parameters:
colorRGB
- color, as a packed (A)RGB value
-
setColor
public void setColor(int red, int green, int blue) Set the color as 8-bit RGB values- Parameters:
red
-green
-blue
-- Since:
- v0.4.0
-
getColor
Get the color that should be used to display objects with this classification.- Returns:
- packed (A)RGB value representing the classification color.
-
getName
Get the name of this classification. Note that this does not incorporate information from any parent classifications; to access this, usetoString()
instead.- Returns:
-
toString
-
toString
Create a string representation, using the specified delimiter between elements of derived PathClasses.- Parameters:
delimiter
-- Returns:
-
isValid
public boolean isValid()A PathClass is valid if its name is not null.This should generally the case, but a single (invalid) PathClass with a null name can be used to indicate the absence of a classification; however, it should not be assigned to any object. Rather, objects should be assigned either a valid PathClass or null to indicate that they have no classification.
- Returns:
-
toSet
Return a view of this path class as an unmodifiable set, with each element representing the name of a path class component names.Important! If any path class component names are duplicates, these will (necessarily) be removed from the set. Therefore it is not guaranteed that calling
fromCollection(Collection)
on the output will return the samePathClass
object.var pathClass = ...; var pathClass2 = PathClass.getInstance(pathClass.toSet()); assert pathClass == pathClass2; // This may or may not be true!
However the
PathClass
objects should be the same if the name components are all valid and there are no duplicates (which should normally be the case).- Returns:
-
compareTo
This is now equivalent tothis.toString().compareTo(o.toString())
.Note that in previous versions (< 0.1.2), the comparison was made based on the name only.
This could result in unexpected behavior whenever comparing with equality and using derived
PathClass
objects, because only the (final) name part was being compared and this could potentially result in classifications (wrongly) being considered equal (e.g. "Tumor: Positive" and "Stroma: Positive").This was most significant when working with Groovy, where
==
is replaced bycompareTo
.- Specified by:
compareTo
in interfaceComparable<PathClass>
-
getNullClass
Get the value ofNULL_CLASS
, used to represent no classification. In most cases,null
should be used instead; this exists only as a representation in cases wherenull
is not permitted (e.g. in some collection implementations).- Returns:
-
fromString
Get a PathClass instance from a string representation, without specifying a default color.This calls
fromString(String, Integer)
with the second argument asnull
.- Parameters:
string
-- Returns:
-
fromString
Get a PathClass instance from a string representation, optionally providing a default color if a new instance needs to be created.This ultimately calls
getInstance(PathClass, String, Integer)
but differs in that it accepts a string representation that may include theDELIMITER
. If so, this is used to split the string into different name components that are passed tofromCollection(Collection, Integer)
.- Parameters:
string
- a string representation containing one or more name elements, separated byDELIMITER
color
- a default color (optional, may be null)- Returns:
-
fromCollection
Get a PathClass using all the name elements specified in the collection, without providing a default color.- Parameters:
names
-- Returns:
- See Also:
-
fromCollection
Get a PathClass instance using all the name elements specified in the collection, with optional default color if a new instance is created. The rules are:- If the collection is empty,
NULL_CLASS
is returned - If the collection has one element, this is equivalent to calling
getInstance(String, Integer)
- If the collection has multiple element, this is equivalent to creating a base class from
the first element and deriving subclassifications by calling
getInstance(PathClass, String, Integer)
for each element, in the order returned by the collection's iterator
- Parameters:
names
-color
-- Returns:
- See Also:
- If the collection is empty,
-
getInstance
-
getInstance
Get a base PathClass instance, without any parent PathClass.This is equivalent to calling
getInstance(PathClass, String, Integer)
with the first argument asnull
.- Parameters:
name
-color
-- Returns:
- See Also:
-
getInstance
Get a derived PathClass instance with the specified parent.This will be derived from the parent PathClass (if provided) and have the specified name, stripped to remove any leading or training whitespace.
Note that the name should generally be an alphanumeric string, optionally including punctuation symbols but not including
DELIMITER
.The delimiter is currently a colon
":"
but it is advised not to rely upon this and to avoid punctuation where possible, because the delimiter may possibly change in a future release - primarily because the choice of colon can be problematic in some cases, e.g. when using ontologies.- Parameters:
parent
- parent class (optional, may be null)name
- name of the PathClasscolor
- color to use if a new instance is created (may be null to use the default)- Returns:
- a PathClass instance; the same instance will be returned given the same parent and name
- See Also:
-
getOnePlus
Get a standalone or derived 1+ classification, indicating weak positivity- Parameters:
parentClass
- parent classification (may be null)- Returns:
-
getTwoPlus
Get a standalone or derived 2+ classification, indicating moderate positivity- Parameters:
parentClass
- parent classification (may be null)- Returns:
-
getThreePlus
Get a standalone or derived 3+ classification, indicating strong positivity- Parameters:
parentClass
- parent classification (may be null)- Returns:
-
getNegative
Get a standalone or derived Negative classification- Parameters:
parentClass
- parent classification (may be null)- Returns:
-
getPositive
Get a standalone or derived Positive classification- Parameters:
parentClass
- parent classification (may be null)- Returns:
-
getSingleton
Get the singleton PathClass that is equivalent to the PathClass provided.This is important because there should only ever be one PathClass instance for any classification - and accessing PathClasses only via the
getInstance()
methods here should ensure that.However, if receiving a PathClass from some other source then it is possible that the PathClass was created some other way and duplicates could emerge. Calling this method returns resolves that problem by returning the single instance that should be used.
This is significant if the PathClass has been created via Java deserialization, which skips the required use of
getInstance()
by default.- Parameters:
pathClass
-- Returns:
- either the input PathClass, or an equivalent that should be used
-
fromArray
Get a PathClass from an array of individual names.- Parameters:
names
-- Returns:
- See Also:
-
readResolve
Called during deserialization to ensure that only a single instance of each PathClass exists. Partly addresses https://github.com/qupath/qupath/issues/1306 (during standard deserialation, but not from JSON)- Returns:
- Throws:
ObjectStreamException
-