Skip to content

Commit 1a6d09b

Browse files
committed
Add topological sort max distance from source for strongly connected components as anomaly detection feature
1 parent 2205747 commit 1a6d09b

5 files changed

Lines changed: 43 additions & 0 deletions

domains/anomaly-detection/anomalyDetectionCsv.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ anomaly_detection_features() {
8080
execute_cypher_queries_until_results "${ANOMALY_DETECTION_FEATURE_CYPHER_DIR}/AnomalyDetectionFeature-WeaklyConnectedComponents-Exists.cypher" \
8181
"${ANOMALY_DETECTION_FEATURE_CYPHER_DIR}/AnomalyDetectionFeature-WeaklyConnectedComponents-Write.cypher" "${@}"
8282
execute_cypher "${ANOMALY_DETECTION_FEATURE_CYPHER_DIR}/AnomalyDetectionFeature-WeaklyConnectedComponents-CreateNode.cypher" "${@}"
83+
# Determines topological sort max distance from source for strongly connected components if not already done
84+
execute_cypher_queries_until_results "${ANOMALY_DETECTION_FEATURE_CYPHER_DIR}/AnomalyDetectionFeature-TopologicalSortComponents-Exists.cypher" \
85+
"${ANOMALY_DETECTION_FEATURE_CYPHER_DIR}/AnomalyDetectionFeature-TopologicalSortComponents-Projection.cypher" "${@}"
86+
execute_cypher_queries_until_results "${ANOMALY_DETECTION_FEATURE_CYPHER_DIR}/AnomalyDetectionFeature-TopologicalSortComponents-Exists.cypher" \
87+
"${ANOMALY_DETECTION_FEATURE_CYPHER_DIR}/AnomalyDetectionFeature-TopologicalSortComponents-Write.cypher" "${@}"
8388
}
8489

8590
# Run queries to find anomalies in the graph.

domains/anomaly-detection/anomalyDetectionPython.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ anomaly_detection_features() {
127127
execute_cypher_queries_until_results "${ANOMALY_DETECTION_FEATURE_CYPHER_DIR}/AnomalyDetectionFeature-WeaklyConnectedComponents-Exists.cypher" \
128128
"${ANOMALY_DETECTION_FEATURE_CYPHER_DIR}/AnomalyDetectionFeature-WeaklyConnectedComponents-Write.cypher" "${@}"
129129
execute_cypher "${ANOMALY_DETECTION_FEATURE_CYPHER_DIR}/AnomalyDetectionFeature-WeaklyConnectedComponents-CreateNode.cypher" "${@}"
130+
# Determines topological sort max distance from source for strongly connected components if not already done
131+
execute_cypher_queries_until_results "${ANOMALY_DETECTION_FEATURE_CYPHER_DIR}/AnomalyDetectionFeature-TopologicalSortComponents-Exists.cypher" \
132+
"${ANOMALY_DETECTION_FEATURE_CYPHER_DIR}/AnomalyDetectionFeature-TopologicalSortComponents-Projection.cypher" "${@}"
133+
execute_cypher_queries_until_results "${ANOMALY_DETECTION_FEATURE_CYPHER_DIR}/AnomalyDetectionFeature-TopologicalSortComponents-Exists.cypher" \
134+
"${ANOMALY_DETECTION_FEATURE_CYPHER_DIR}/AnomalyDetectionFeature-TopologicalSortComponents-Write.cypher" "${@}"
130135
}
131136

132137
# Execute the Python scripts for anomaly detection.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Return the first node with a "topologicalSortMaxDistanceFromSource" if it exists
2+
3+
MATCH (component:StronglyConnectedComponent)
4+
WHERE $projection_node_label + 'Members' IN labels(component)
5+
AND component.topologicalSortMaxDistanceFromSource IS NOT NULL
6+
RETURN component.name AS shortCodeUnitName
7+
,elementId(component) AS nodeElementId
8+
,component.topologicalSortMaxDistanceFromSource AS topologicalSortMaxDistanceFromSource
9+
LIMIT 1
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Creates a projection of the strongly connected components graph for the given member type. Requires: "AnomalyDetectionFeature-StronglyConnectedComponents-CreateDependency.cypher"
2+
3+
MATCH (sourceComponent:StronglyConnectedComponent)
4+
OPTIONAL MATCH (sourceComponent)-[:DEPENDS_ON]->(targetComponent:StronglyConnectedComponent)
5+
WHERE $projection_node_label + 'Members' IN labels(sourceComponent)
6+
AND $projection_node_label + 'Members' IN labels(targetComponent)
7+
WITH gds.graph.project($projection_name + '-components', sourceComponent, targetComponent) AS graph
8+
RETURN graph.graphName AS graphName
9+
,graph.nodeCount AS nodeCount
10+
,graph.relationshipCount AS relationshipCount
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Topological Sort to write the property "topologicalSortMaxDistanceFromSource" (e.g. build order) for strongly connected components into the graph. Requires "AnomalyDetectionFeature-TopologicalSortComponents-Projection".
2+
// Needs graph-data-science plugin version >= 2.5.0
3+
4+
CALL gds.dag.topologicalSort.stream(
5+
$projection_name + '-components', {
6+
computeMaxDistanceFromSource: true
7+
}) YIELD nodeId, maxDistanceFromSource
8+
WITH nodeId
9+
,gds.util.asNode(nodeId) AS component
10+
,toInteger(maxDistanceFromSource) AS maxDistanceFromSource
11+
SET component.topologicalSortMaxDistanceFromSource = maxDistanceFromSource
12+
WITH maxDistanceFromSource, count(*) AS occurrences
13+
RETURN maxDistanceFromSource, occurrences
14+
ORDER BY maxDistanceFromSource

0 commit comments

Comments
 (0)