SGD
¶
Build_SGD_graph
¶
Spatial locations are represented as nodes in an undirected graph.Normal spots are isolated, while anomalous spots are connected to their k-nearest anomalous neighbors. Note that in the anomaly detection results, incorrectly identified spots as anomalies (false positives) become connected, and false negatives become isolated, which leads to a deviation from the local structures of the ground truth graph. Spots are divided into two regions: one includes true positives plus false positives (TP+FP) anomalies, and the other includes true positives plus false negatives (TP+FN) anomalies. We perform a bootstrap sampling of m sets of spots from these two regions.
Examples:
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
Dict
|
Dictionary containing spatial data and labels. |
required |
n_neighbors |
int
|
Number of neighbors to consider for edge creation (default is 6). |
6
|
Source code in src\stands\evaluate\SGD.py
build_graph
¶
Build graphs for predicted and ground truth labels.
Returns:
Type | Description |
---|---|
List[DGLGraph]
|
List of DGLGraph objects representing the predicted and ground truth graphs. |
Source code in src\stands\evaluate\SGD.py
classify_cells
¶
Classify cells based on prediction and ground truth.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pred |
int
|
Predicted flag (1 for anomaly, 0 for normal). |
required |
truth |
int
|
Ground truth flag (1 for anomaly, 0 for normal). |
required |
Returns:
Type | Description |
---|---|
str
|
Classification result (TP, FP, FN, or TN). |
Source code in src\stands\evaluate\SGD.py
get_anomaly
¶
Extract anomaly information for nodes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
is_truth |
bool
|
If True, extract anomaly data from ground truth labels. If False, extract from predicted labels. |
True
|
typeid |
Optional[int]
|
Type ID for filtering data. |
None
|
binary_typeid |
Optional[int]
|
Binary Type ID for filtering data. |
None
|
Returns:
Type | Description |
---|---|
Tensor
|
Anomaly data as a tensor. |
Source code in src\stands\evaluate\SGD.py
get_classification
¶
Get classification labels for nodes in predicted and ground truth graphs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pred_graph |
DGLGraph
|
Predicted graph. |
required |
truth_graph |
DGLGraph
|
Ground truth graph. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Classification labels as a tensor. |
Source code in src\stands\evaluate\SGD.py
get_edge
¶
Generate edges based on spatial positions.
Returns:
Type | Description |
---|---|
Tuple[ndarray]
|
Two arrays representing source and destination nodes for edges. |
Source code in src\stands\evaluate\SGD.py
remove_edges
¶
Remove edges from the graph based on anomaly information.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
g |
DGLGraph
|
Input graph. |
required |
Source code in src\stands\evaluate\SGD.py
SGDEvaluator
¶
Evaluate SGD_degree or SGD_cc with the built predicted and ground truth SGD Graph.
Examples:
>>> evaluator = SGDEvaluator(bins = 10, num_bootstrap_samples = 50, sigma = 1)
>>> evaluator.evaluate_sgd(g_pred_list, g_truth_list, metric = 'degree')
1.01
>>> evaluator.evaluate_sgd(g_pred_list, g_truth_list, metric = 'cc')
0.34
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bins |
int
|
Number of equal-width bins in the given range when calculating SGD_cc. |
10
|
num_bootstrap_samples |
int
|
Number of bootstrap samples for distribution estimation. |
10
|
sigma |
int
|
Sigma parameter for Gaussian Earth Mover's Distance. |
1
|
Source code in src\stands\evaluate\SGD.py
evaluate_sgd
¶
evaluate_sgd(
g_pred_list: List[DGLGraph],
g_truth_list: List[DGLGraph],
metric: Literal["degree", "cc"],
)
Evaluate SGD based on predicted and ground truth graphs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
g_pred_list |
List[DGLGraph]
|
List of predicted DGLGraphs. |
required |
g_truth_list |
List[DGLGraph]
|
List of ground truth DGLGraphs. |
required |
metric |
Literal['degree', 'cc']
|
Metric to evaluate ('degree' or 'cc'). |
required |
Returns:
Type | Description |
---|---|
float
|
SGD score for the predicted and ground truth graphs. |
Source code in src\stands\evaluate\SGD.py
get_assigned_values
¶
Get assigned values from the SGD matrix and assignment matrix.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sgd_matrix |
List[List[float]]
|
Matrix of SGD values. |
required |
assignment_matrix |
List[List[int]]
|
Assignment matrix indicating optimal assignments. |
required |
Returns:
Type | Description |
---|---|
List[float]
|
List of dictionaries containing assigned values. |
Source code in src\stands\evaluate\SGD.py
solve_assignment_problems
¶
Solve the Assignment Problem using the CBC solver.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
distance_matrix |
List[List[float]]
|
Matrix representing the costs of assignments (distance). |
required |
Returns:
Type | Description |
---|---|
List[List[int]]
|
Assignment matrix indicating optimal assignments. |