|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from abc import ABC, abstractmethod |
| 4 | +from typing import Any |
| 5 | + |
| 6 | +from pandas import DataFrame |
| 7 | + |
| 8 | +from graphdatascience.procedure_surface.api.catalog.graph_api import GraphV2 |
| 9 | +from graphdatascience.procedure_surface.api.estimation_result import EstimationResult |
| 10 | +from graphdatascience.procedure_surface.api.similarity.knn_endpoints import ( |
| 11 | + KnnMutateResult, |
| 12 | + KnnStatsResult, |
| 13 | + KnnWriteResult, |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +class KnnFilteredEndpoints(ABC): |
| 18 | + """Base class for Filtered K-Nearest Neighbors endpoints.""" |
| 19 | + |
| 20 | + @abstractmethod |
| 21 | + def mutate( |
| 22 | + self, |
| 23 | + G: GraphV2, |
| 24 | + mutate_relationship_type: str, |
| 25 | + mutate_property: str, |
| 26 | + node_properties: str | list[str] | dict[str, str], |
| 27 | + source_node_filter: str, |
| 28 | + target_node_filter: str, |
| 29 | + seed_target_nodes: bool | None = None, |
| 30 | + top_k: int | None = None, |
| 31 | + similarity_cutoff: float | None = None, |
| 32 | + delta_threshold: float | None = None, |
| 33 | + max_iterations: int | None = None, |
| 34 | + sample_rate: float | None = None, |
| 35 | + perturbation_rate: float | None = None, |
| 36 | + random_joins: int | None = None, |
| 37 | + random_seed: int | None = None, |
| 38 | + initial_sampler: Any | None = None, |
| 39 | + relationship_types: list[str] | None = None, |
| 40 | + node_labels: list[str] | None = None, |
| 41 | + sudo: bool | None = None, |
| 42 | + log_progress: bool = True, |
| 43 | + username: str | None = None, |
| 44 | + concurrency: Any | None = None, |
| 45 | + job_id: Any | None = None, |
| 46 | + ) -> KnnMutateResult: |
| 47 | + """Run filtered K-Nearest Neighbors in mutate mode.""" |
| 48 | + ... |
| 49 | + |
| 50 | + @abstractmethod |
| 51 | + def stats( |
| 52 | + self, |
| 53 | + G: GraphV2, |
| 54 | + node_properties: str | list[str] | dict[str, str], |
| 55 | + source_node_filter: str, |
| 56 | + target_node_filter: str, |
| 57 | + seed_target_nodes: bool | None = None, |
| 58 | + top_k: int | None = None, |
| 59 | + similarity_cutoff: float | None = None, |
| 60 | + delta_threshold: float | None = None, |
| 61 | + max_iterations: int | None = None, |
| 62 | + sample_rate: float | None = None, |
| 63 | + perturbation_rate: float | None = None, |
| 64 | + random_joins: int | None = None, |
| 65 | + random_seed: int | None = None, |
| 66 | + initial_sampler: Any | None = None, |
| 67 | + relationship_types: list[str] | None = None, |
| 68 | + node_labels: list[str] | None = None, |
| 69 | + sudo: bool | None = None, |
| 70 | + log_progress: bool = True, |
| 71 | + username: str | None = None, |
| 72 | + concurrency: Any | None = None, |
| 73 | + job_id: Any | None = None, |
| 74 | + ) -> KnnStatsResult: |
| 75 | + """Run filtered K-Nearest Neighbors in stats mode.""" |
| 76 | + ... |
| 77 | + |
| 78 | + @abstractmethod |
| 79 | + def stream( |
| 80 | + self, |
| 81 | + G: GraphV2, |
| 82 | + node_properties: str | list[str] | dict[str, str], |
| 83 | + source_node_filter: str, |
| 84 | + target_node_filter: str, |
| 85 | + seed_target_nodes: bool | None = None, |
| 86 | + top_k: int | None = None, |
| 87 | + similarity_cutoff: float | None = None, |
| 88 | + delta_threshold: float | None = None, |
| 89 | + max_iterations: int | None = None, |
| 90 | + sample_rate: float | None = None, |
| 91 | + perturbation_rate: float | None = None, |
| 92 | + random_joins: int | None = None, |
| 93 | + random_seed: int | None = None, |
| 94 | + initial_sampler: Any | None = None, |
| 95 | + relationship_types: list[str] | None = None, |
| 96 | + node_labels: list[str] | None = None, |
| 97 | + sudo: bool | None = None, |
| 98 | + log_progress: bool = True, |
| 99 | + username: str | None = None, |
| 100 | + concurrency: Any | None = None, |
| 101 | + job_id: Any | None = None, |
| 102 | + ) -> DataFrame: |
| 103 | + """Run filtered K-Nearest Neighbors in stream mode.""" |
| 104 | + ... |
| 105 | + |
| 106 | + @abstractmethod |
| 107 | + def write( |
| 108 | + self, |
| 109 | + G: GraphV2, |
| 110 | + write_relationship_type: str, |
| 111 | + write_property: str, |
| 112 | + node_properties: str | list[str] | dict[str, str], |
| 113 | + source_node_filter: str, |
| 114 | + target_node_filter: str, |
| 115 | + seed_target_nodes: bool | None = None, |
| 116 | + top_k: int | None = None, |
| 117 | + similarity_cutoff: float | None = None, |
| 118 | + delta_threshold: float | None = None, |
| 119 | + max_iterations: int | None = None, |
| 120 | + sample_rate: float | None = None, |
| 121 | + perturbation_rate: float | None = None, |
| 122 | + random_joins: int | None = None, |
| 123 | + random_seed: int | None = None, |
| 124 | + initial_sampler: Any | None = None, |
| 125 | + relationship_types: list[str] | None = None, |
| 126 | + node_labels: list[str] | None = None, |
| 127 | + write_concurrency: int | None = None, |
| 128 | + write_to_result_store: bool | None = None, |
| 129 | + sudo: bool | None = None, |
| 130 | + log_progress: bool = True, |
| 131 | + username: str | None = None, |
| 132 | + concurrency: Any | None = None, |
| 133 | + job_id: Any | None = None, |
| 134 | + ) -> KnnWriteResult: |
| 135 | + """Run filtered K-Nearest Neighbors in write mode.""" |
| 136 | + ... |
| 137 | + |
| 138 | + @abstractmethod |
| 139 | + def estimate( |
| 140 | + self, |
| 141 | + G: GraphV2 | dict[str, Any], |
| 142 | + node_properties: str | list[str] | dict[str, str], |
| 143 | + source_node_filter: str, |
| 144 | + target_node_filter: str, |
| 145 | + seed_target_nodes: bool | None = None, |
| 146 | + top_k: int | None = None, |
| 147 | + similarity_cutoff: float | None = None, |
| 148 | + delta_threshold: float | None = None, |
| 149 | + max_iterations: int | None = None, |
| 150 | + sample_rate: float | None = None, |
| 151 | + perturbation_rate: float | None = None, |
| 152 | + random_joins: int | None = None, |
| 153 | + random_seed: int | None = None, |
| 154 | + initial_sampler: Any | None = None, |
| 155 | + relationship_types: list[str] | None = None, |
| 156 | + node_labels: list[str] | None = None, |
| 157 | + sudo: bool | None = None, |
| 158 | + username: str | None = None, |
| 159 | + concurrency: Any | None = None, |
| 160 | + ) -> EstimationResult: |
| 161 | + """Estimate filtered K-Nearest Neighbors execution requirements. |
| 162 | +
|
| 163 | + Parameters |
| 164 | + ---------- |
| 165 | + G : GraphV2 | dict[str, Any] |
| 166 | + The graph to run the algorithm on. |
| 167 | + node_properties : str | list[str] |
| 168 | + The node properties to use for similarity computation. |
| 169 | + mutate_property : str |
| 170 | + The relationship property to store the similarity score in. |
| 171 | + mutate_relationship_type : str |
| 172 | + The relationship type to use for the new relationships. |
| 173 | + source_node_filter : str | None, default=None |
| 174 | + A Cypher expression to filter which nodes can be sources in the similarity computation. |
| 175 | + target_node_filter : str | None, default=None |
| 176 | + A Cypher expression to filter which nodes can be targets in the similarity computation. |
| 177 | + seed_target_nodes : bool | None, default=None |
| 178 | + Whether to use a seeded approach for target node selection. |
| 179 | + similarity_cutoff : float | None, default=None |
| 180 | + The threshold for similarity scores. |
| 181 | + perturbation_rate : float | None, default=None |
| 182 | + The rate at which to perturb the similarity graph. |
| 183 | + delta_threshold : float | None, default=None |
| 184 | + The threshold for convergence assessment. |
| 185 | + sample_rate : float | None, default=None |
| 186 | + The sampling rate for the algorithm. |
| 187 | + random_joins : int | None, default=None |
| 188 | + The number of random joins to perform. |
| 189 | + initial_sampler : str | None, default=None |
| 190 | + The initial sampling strategy. |
| 191 | + max_iterations : int | None, default=None |
| 192 | + The maximum number of iterations to run. |
| 193 | + top_k : int | None, default=None |
| 194 | + The number of nearest neighbors to find for each node. |
| 195 | + random_seed : int | None, default=None |
| 196 | + The seed for the random number generator. |
| 197 | + concurrency : int | None, default=None |
| 198 | + Concurrency configuration. |
| 199 | + job_id : str | None, default=None |
| 200 | + Job ID for the operation. |
| 201 | + log_progress : bool | None, default=None |
| 202 | + Whether to log progress. |
| 203 | + sudo : bool | None, default=None |
| 204 | + Run the algorithm with elevated privileges. |
| 205 | + username : str | None, default=None |
| 206 | + Username for the operation. |
| 207 | + **kwargs : Any |
| 208 | + Additional parameters. |
| 209 | +
|
| 210 | + Returns |
| 211 | + ------- |
| 212 | + KnnMutateResult |
| 213 | + Object containing metadata from the execution. |
| 214 | + """ |
| 215 | + ... |
0 commit comments