Utilities API¶
This section documents the utility components of OKAPI.
Fitness Functions¶
Functions for evaluating the fitness of trees.
accuracy_fitness(tree, gt, task='binary')
¶
Calculate the Accuracy score as a fitness measure using torchmetrics.
Accuracy is the proportion of correct predictions among the total number of cases processed. This implementation supports binary, multiclass, and multilabel classification.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tree
|
Tree
|
The tree whose evaluation will be compared against ground truth |
required |
gt
|
Tensor
|
Ground truth tensor containing labels |
required |
task
|
Literal['binary', 'multiclass', 'multilabel']
|
Classification task type: - 'binary': Binary classification (default) - 'multiclass': Multiclass classification - 'multilabel': Multilabel classification |
'binary'
|
Returns:
| Type | Description |
|---|---|
float
|
Accuracy score as a float between 0 and 1 (higher is better) |
Source code in okapi/fitness.py
average_precision_fitness(tree, gt, task='binary')
¶
Calculate the Average Precision (AP) score as a fitness measure using torchmetrics.
Average Precision summarizes a precision-recall curve as the weighted mean of precisions achieved at each threshold, with the increase in recall from the previous threshold used as the weight. This implementation supports binary, multiclass, and multilabel classification.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tree
|
Tree
|
The tree whose evaluation will be compared against ground truth |
required |
gt
|
Tensor
|
Ground truth tensor containing labels |
required |
task
|
Literal['binary', 'multiclass', 'multilabel']
|
Classification task type: - 'binary': Binary classification (default) - 'multiclass': Multiclass classification - 'multilabel': Multilabel classification |
'binary'
|
Returns:
| Type | Description |
|---|---|
float
|
Average Precision score as a float between 0 and 1 (higher is better) |
Source code in okapi/fitness.py
roc_auc_score_fitness(tree, gt, task='binary')
¶
Calculate the Area Under the ROC Curve (AUC-ROC) score as a fitness measure using torchmetrics.
The AUC-ROC score represents the probability that a randomly chosen positive instance is ranked higher than a randomly chosen negative instance. This implementation supports binary, multiclass, and multilabel classification.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tree
|
Tree
|
The tree whose evaluation will be compared against ground truth |
required |
gt
|
Tensor
|
Ground truth tensor containing labels |
required |
task
|
Literal['binary', 'multiclass', 'multilabel']
|
Classification task type: - 'binary': Binary classification (default) - 'multiclass': Multiclass classification - 'multilabel': Multilabel classification |
'binary'
|
Returns:
| Type | Description |
|---|---|
float
|
ROC AUC score as a float between 0 and 1 (higher is better) |
Source code in okapi/fitness.py
Callbacks¶
The callback system allows customizing the evolutionary process.
Base class for callbacks that can be triggered during the evolutionary process.
Callbacks allow monitoring and potentially modifying the evolution process at specific points: before/after each generation, and at the start/end of the entire evolution. Custom callbacks should inherit from this class and override the methods corresponding to the desired intervention points.
Source code in okapi/callback.py
on_evolution_end(okapi)
¶
Called at the end of the entire evolution process.
This hook is triggered when all generations have been completed or when the evolution process is manually stopped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
okapi
|
Okapi
|
The Okapi instance running the evolution |
required |
Source code in okapi/callback.py
on_evolution_start(okapi)
¶
Called at the start of the evolution process.
This hook is triggered before any generations are run, after the initial population has been created.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
okapi
|
Okapi
|
The Okapi instance running the evolution |
required |
Source code in okapi/callback.py
on_generation_end(okapi)
¶
Called at the end of each generation.
This hook is triggered after a generation (iteration) of evolution has completed, including selection, crossover, and mutation operations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
okapi
|
Okapi
|
The Okapi instance running the evolution |
required |
Source code in okapi/callback.py
on_generation_start(okapi)
¶
Called at the start of each generation.
This hook is triggered before a generation (iteration) of evolution begins, before any selection, crossover, or mutation operations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
okapi
|
Okapi
|
The Okapi instance running the evolution |
required |
Source code in okapi/callback.py
Visualization¶
Functions for visualizing trees.
Create a visual representation of a tree structure using Graphviz.
This function generates a directed graph visualization of a tree structure, showing nodes and their hierarchical relationships. For ValueNodes, it can optionally display the tensor values and evaluations if they're small enough.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
to_draw
|
Node | OperatorNode | Tree | ValueNode
|
The object to visualize (can be a Tree, Node, OperatorNode, or ValueNode) |
required |
dot
|
Optional existing Digraph object to add to. If None, a new one is created. |
None
|
|
add_val_eval
|
If True, include value and evaluation information for ValueNodes |
True
|
Returns:
| Type | Description |
|---|---|
|
A Graphviz Digraph object representing the tree structure |
Source code in okapi/draw.py
Postprocessing Functions¶
Functions for postprocessing tree outputs.
scale_vector_to_sum_1(tensor)
¶
Normalize a tensor so that each vector sums to 1.
This function scales each vector in the tensor along the last dimension such that its elements sum to 1, making it suitable for representing probability distributions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tensor
|
Tensor
|
Input tensor to be normalized |
required |
Returns:
| Type | Description |
|---|---|
|
Normalized tensor where each vector sums to 1 |
Source code in okapi/functions.py
set_multiclass_postprocessing()
¶
Configure the global postprocessing function for multiclass classification.
This function sets the global postprocessing to normalize tensor outputs so that they can be interpreted as probability distributions, which is required for multiclass classification tasks. This is important if probas and not logits are an output.
Source code in okapi/functions.py
Other Utilities¶
Additional utility functions.
Pickle
¶
A utility class for serializing and deserializing Python objects using pickle.
This class provides static methods for saving objects to files and loading them back, which is particularly useful for persisting tree architectures.
Source code in okapi/utils.py
load(path)
staticmethod
¶
Load a Python object from a pickle file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
File path to load the object from |
required |
Returns:
| Type | Description |
|---|---|
|
The deserialized Python object |
Source code in okapi/utils.py
save(path, obj)
staticmethod
¶
Save a Python object to a pickle file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
File path where the object will be saved |
required | |
obj
|
The Python object to serialize and save |
required |
Source code in okapi/utils.py
first_uniques_mask(arr)
¶
Create a boolean mask that identifies the first occurrence of each unique item in an array.
This function is useful for filtering duplicates from an array while preserving the order of first appearances.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arr
|
An array-like object to analyze |
required |
Returns:
| Type | Description |
|---|---|
|
A list of booleans where True indicates the first occurrence of a value and |
|
|
False indicates a duplicate of a previously seen value |
Source code in okapi/utils.py
mark_paths(list_of_paths)
¶
Mark each path in the list with its type (directory or file) or None if it doesn't exist.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
list_of_paths
|
A list of paths to be marked. |
required |
Returns:
| Type | Description |
|---|---|
tuple[list[str | None], bool]
|
A tuple containing: - A list of strings or None values representing the type of each path. - A boolean indicating whether all paths are of the same type. |