dendrogram.lattice
module¶
An N-dimensional lattice class with an identify_cluster method.
-
class
dendrogram.lattice.
latticeND
(data, level)¶ Bases:
object
An N-dimensional lattice class.
-
property
shape
¶ Shape of the lattice.
- Type
numpy.ndarray of int
-
property
dim
¶ Dimension of the lattice.
- Type
int
-
property
len
¶ Total number of elements in the lattice.
- Type
int
-
property
lattice
¶ Area ocupied by the lattice.
- Type
numpy.ndarray of int
-
property
label
¶ Label of clusters.
- Type
numpy.ndarray of int
-
identify_cluster
()¶ Identify clusters in the lattice.
A cluster is a group of connected (neighboring) pixels.
- Returns
Label of clusters.
- Return type
numpy.ndarray of int
-
property
dendrogram.structureTree
module¶
A tree class and a makeTree function.
-
class
dendrogram.structureTree.
clusterTree
(label, mask, isleaf=True)¶ Bases:
object
A tree class.
Note
clusterTree can be either the tree itself or the branch of a tree.
-
property
label
¶ Label of this tree/branch.
- Type
int
-
property
mask
¶ Area ocupied by this tree/branch.
- Type
numpy.ndarray of bool
-
property
isleaf
¶ Whether this is a leaf or not.
- Type
bool
-
property
branches
¶ All branches of the tree. Empty if this is a branch.
- Type
dict of int
-
property
children
¶ Children of this branch/tree. Empty if this is a leaf.
- Type
dict of int
-
property
parent
¶ Parent of this branch. Empty if this is a tree.
- Type
dict of int
-
create_leaf
(label, mask)¶ Create a new leaf to the tree.
Note
This method does not connect the new leaf to any of the presenting branch!
- Parameters
label (int) – Label of the new leaf.
mask (numpy.ndarray of bool) – The area ocupied by the new leaf.
-
merge_branch
(label, mask, branch)¶ Merge many old branches to a new branch.
Note
Here, “merge” means setting the new branch to be the parent of old branches, not removing them!
- Parameters
label (int) – Label of the new branch.
mask (numpy.ndarray of bool) – The area ocupied by the new branch.
branch (set or list of int) – Labels of branches to be merged.
-
merge_final
(branch)¶ Merge final branches to the tree.
Note
Even if there is only one final branch, we still need to merge (or, more properly, “link”) it to the tree. Mind the difference between “tree” and “branch”.
- Parameters
branch (set or list of int) – Labels of branches to be merged.
-
topology
(stdout=True)¶ Print topology of the tree in pure text.
Note
Applying this method to large (with more than 100 branches) or deep (with more than 100 levels) trees is not recommend.
- Parameters
stdout (bool) – Whether to print to screen or not.
- Returns
Visualized topology of this tree.
- Return type
str
-
property
-
dendrogram.structureTree.
makeTree
(data, min_value, min_delta=0, min_npix=1, num_level=100, print_progress=False)¶ Make dendrogram tree from N-dimensional data.
Note
Applying this method to large (with more than 100 branches) or deep (with more than 100 levels) trees is not recommend.
- Parameters
data (numpy.ndarray of scalar) – Data to make dendrogram tree.
min_value (scalar) – Minimum value to consider.
min_delta (scalar, default to 0) – Lag to be ignored.
min_npix (int, default to 1) – Minimum number of pixels to form a cluster.
num_level (int, default to 100) – Number of levels.
print_progress (bool, default to False) – Whether to print progress or not.
- Returns
Tree for the dendrogram.
- Return type
clusterTree
Examples
Consider a simple two-dimensional bimodal data, let’s generate the tree with min_value=0:
>>> data = np.array([[2,1], [1,2]]) >>> tree = makeTree(data, min_value=0)
To check the result, we can print the topology of tree:
>>> tp = tree.topology() └──(-1) └──(2) ├──(0) └──(1)
As expected, two branches 0 and 1 illustrates the bimodality. Similarly, the script below generates dendrogram for a three-peak distribution:
>>> data = np.array([[3,1,1],[1,1,1],[2,1,3]]) >>> tree = makeTree(data, min_value=0) >>> tp = tree.topology() └──(-1) └──(3) ├──(0) ├──(1) └──(2)