morphoclass.model_utils module¶
Utilities for GNN models. Currently, only the hierarchical labels.
-
class
morphoclass.model_utils.HierarchicalLabels(labels, adj, flat_to_hierarchical_oh)¶ Bases:
objectClass representing a set of hierarchical labels.
It holds the structural information of a set of hierarchical labels and is meant to be used conjointly with an appropriate hierarchical model.
The constructor constructs class instance from a given label adjacency matrix.
It takes labels for all hierarchical nodes and their hierarchical relationship in terms of the adjacency matrix. Additionally a dictionary mapping flat labels (=leaf nodes) to the hierarchical one-hot labels must be provided.
- Parameters
labels – List of labels for all hierarchical nodes.
adj – Adjacency matrix representing the label hierarchy.
flat_to_hierarchical_oh – dictionary mapping flat labels to hierarchical one-hot labels.
- Returns
An instance of the HierarchicalLabels class.
- Return type
instance
-
property
adj¶ Get the adjacency matrix for hierarchical labels.
- Returns
- Return type
The adjacency matrix representing hierarchical labels.
-
flat_labels()¶ Flat labels in numerical form.
- Returns
The numerical values of the flat labels.
- Return type
list
-
flat_to_hierarchical_oh(n)¶ Map flat numerical labels to hierarchical one-hot labels.
- Parameters
n – Flat numerical label.
- Returns
One-hot hierarchical label.
- Return type
oh_label
-
classmethod
from_adjacency_matrix(labels, adj, flat_to_hierarchical_oh)¶ Construct class instance from a given label adjacency matrix.
This factory takes labels for all hierarchical nodes and their hierarchical relationship in terms of the adjacency matrix. Additionally a dictionary mapping flat labels (=leaf nodes) to the hierarchical one-hot labels must be provided.
- Parameters
labels – List of labels for all hierarchical nodes.
adj – Adjacency matrix representing the label hierarchy.
flat_to_hierarchical_oh – dictionary mapping flat labels to hierarchical one-hot labels.
- Returns
An instance of the HierarchicalLabels class.
- Return type
instance
-
classmethod
from_flat_labels(label_dict)¶ Construct a class instance from a label dictionary.
This factory expects a dictionary mapping numerical flat labels, i.e. leaf nodes of the hierarchical tree, to the list of hierarchical nodes representing the path from the root of the hierarchy to the given leaf node.
For example, a valid representation for the following hierarchy
A B / | / | a b c d /| x y zwould be given by
label_dict = { 0: ['A', 'a'], 1: ['A', 'b'], 2: ['B, 'c', 'x'], 3: ['B, 'c', 'y'], 4: ['B, 'c', 'z'], 5: ['B', 'd'], }
- Parameters
label_dict (dict) – Dictionary mapping dense numerical flat labels to a list of hierarchical string labels.
-
gen_class_masks(parent_mask=None)¶ Generate masks for labels probabilities of which should sum to 1.
- Parameters
parent_mask – The labels to start with, defaults to root nodes.
- Yields
mask – All possible masks for nodes with sums of probabilities equal to 1.
-
get_class_segmentation_mask()¶ Create a segmentation mask for labels.
The mast is such that each softmax block is marked by a different integer.
- Returns
The class segmentation mask.
- Return type
segmentation_mask
-
property
labels¶ Get the labels.
- Returns
- Return type
The labels.
-
n_leafs()¶ Get the number of leaf nodes.
This is the same as the number of flat labels.
- Returns
- Return type
The number of leaf nodes.
-
property
roots¶ Get the label roots.
- Returns
- Return type
The label roots.
-
class
morphoclass.model_utils.HierarchicalLabelsDeprecated(label_dict)¶ Bases:
objectClass representing a set of hierarchical labels.
The attributes are initialized from given label_dict
The argument label_dict is a dictionary mapping numerical labels to a list of strings representing the hierarchical labels. For example [‘animal’, ‘mammal’, ‘dog’, ‘Corgi’] would represent the hierarchical label for ‘Corgi’, which is a subclass of ‘dog’, which is a subclass of ‘mammal’, which is a subclass of ‘animal’.
- Parameters
label_dict (dict) – Dictionary mapping dense numerical labels to a list of hierarchical string labels.
-
get_mask(order=0)¶ Get a mask for the nodes corresponding to a given hierarchy order.
The order 0 in the tree hierarchy is the root.
Actually all of the non-trivial masks are already computed in is_tree so if performance starts to matter one could extend is_tree to cache the child_mask and return.
-
static
is_tree(tree_idx)¶ Check if the given parents tensor represents a valid tree.
Start with the parents and iteratively visit the children, and mark the visited nodes. Continue until all nodes are visited. If at one iteration step the number of visited nodes does not increase (so there’s a disconnected loop) or a child is found to have been seen before, then parents does not represent a tree.
- Parameters
tree_idx – The tree index.
- Returns
Whether or not the given tree index represents a valid tree.
- Return type
bool
-
to_one_hot(dense_label)¶ Convert dense labels to one-hot labels.
- Parameters
dense_label – Given dense labels.
- Returns
- Return type
One-hot labels.