|
| 1 | +from typing import Any, List, Optional, Union |
| 2 | + |
| 3 | +from pandas import DataFrame |
| 4 | + |
| 5 | +from ...call_parameters import CallParameters |
| 6 | +from ...graph.graph_object import Graph |
| 7 | +from ...query_runner.query_runner import QueryRunner |
| 8 | +from ..api.estimation_result import EstimationResult |
| 9 | +from ..api.wcc_endpoints import WccEndpoints, WccMutateResult, WccStatsResult, WccWriteResult |
| 10 | +from ..utils.config_converter import ConfigConverter |
| 11 | + |
| 12 | + |
| 13 | +class WccCypherEndpoints(WccEndpoints): |
| 14 | + """ |
| 15 | + Implementation of the WCC algorithm endpoints. |
| 16 | + This class handles the actual execution by forwarding calls to the query runner. |
| 17 | + """ |
| 18 | + |
| 19 | + def __init__(self, query_runner: QueryRunner): |
| 20 | + self._query_runner = query_runner |
| 21 | + |
| 22 | + def mutate( |
| 23 | + self, |
| 24 | + G: Graph, |
| 25 | + mutate_property: str, |
| 26 | + threshold: Optional[float] = None, |
| 27 | + relationship_types: Optional[List[str]] = None, |
| 28 | + node_labels: Optional[List[str]] = None, |
| 29 | + sudo: Optional[bool] = None, |
| 30 | + log_progress: Optional[bool] = None, |
| 31 | + username: Optional[str] = None, |
| 32 | + concurrency: Optional[int] = None, |
| 33 | + job_id: Optional[str] = None, |
| 34 | + seed_property: Optional[str] = None, |
| 35 | + consecutive_ids: Optional[bool] = None, |
| 36 | + relationship_weight_property: Optional[str] = None, |
| 37 | + ) -> WccMutateResult: |
| 38 | + config = ConfigConverter.convert_to_gds_config( |
| 39 | + mutate_property=mutate_property, |
| 40 | + concurrency=concurrency, |
| 41 | + consecutive_ids=consecutive_ids, |
| 42 | + job_id=job_id, |
| 43 | + log_progress=log_progress, |
| 44 | + node_labels=node_labels, |
| 45 | + relationship_types=relationship_types, |
| 46 | + relationship_weight_property=relationship_weight_property, |
| 47 | + seed_property=seed_property, |
| 48 | + sudo=sudo, |
| 49 | + threshold=threshold, |
| 50 | + username=username, |
| 51 | + ) |
| 52 | + |
| 53 | + # Run procedure and return results |
| 54 | + params = CallParameters(graph_name=G.name(), config=config) |
| 55 | + params.ensure_job_id_in_config() |
| 56 | + |
| 57 | + cypher_result = self._query_runner.call_procedure(endpoint="gds.wcc.mutate", params=params).squeeze() |
| 58 | + |
| 59 | + return WccMutateResult(**cypher_result.to_dict()) |
| 60 | + |
| 61 | + def stats( |
| 62 | + self, |
| 63 | + G: Graph, |
| 64 | + threshold: Optional[float] = None, |
| 65 | + relationship_types: Optional[List[str]] = None, |
| 66 | + node_labels: Optional[List[str]] = None, |
| 67 | + sudo: Optional[bool] = None, |
| 68 | + log_progress: Optional[bool] = None, |
| 69 | + username: Optional[str] = None, |
| 70 | + concurrency: Optional[int] = None, |
| 71 | + job_id: Optional[str] = None, |
| 72 | + seed_property: Optional[str] = None, |
| 73 | + consecutive_ids: Optional[bool] = None, |
| 74 | + relationship_weight_property: Optional[str] = None, |
| 75 | + ) -> WccStatsResult: |
| 76 | + config = ConfigConverter.convert_to_gds_config( |
| 77 | + concurrency=concurrency, |
| 78 | + consecutive_ids=consecutive_ids, |
| 79 | + job_id=job_id, |
| 80 | + log_progress=log_progress, |
| 81 | + node_labels=node_labels, |
| 82 | + relationship_types=relationship_types, |
| 83 | + relationship_weight_property=relationship_weight_property, |
| 84 | + seed_property=seed_property, |
| 85 | + sudo=sudo, |
| 86 | + threshold=threshold, |
| 87 | + username=username, |
| 88 | + ) |
| 89 | + |
| 90 | + # Run procedure and return results |
| 91 | + params = CallParameters(graph_name=G.name(), config=config) |
| 92 | + params.ensure_job_id_in_config() |
| 93 | + |
| 94 | + cypher_result = self._query_runner.call_procedure(endpoint="gds.wcc.stats", params=params).squeeze() # type: ignore |
| 95 | + |
| 96 | + return WccStatsResult(**cypher_result.to_dict()) |
| 97 | + |
| 98 | + def stream( |
| 99 | + self, |
| 100 | + G: Graph, |
| 101 | + min_component_size: Optional[int] = None, |
| 102 | + threshold: Optional[float] = None, |
| 103 | + relationship_types: Optional[List[str]] = None, |
| 104 | + node_labels: Optional[List[str]] = None, |
| 105 | + sudo: Optional[bool] = None, |
| 106 | + log_progress: Optional[bool] = None, |
| 107 | + username: Optional[str] = None, |
| 108 | + concurrency: Optional[int] = None, |
| 109 | + job_id: Optional[str] = None, |
| 110 | + seed_property: Optional[str] = None, |
| 111 | + consecutive_ids: Optional[bool] = None, |
| 112 | + relationship_weight_property: Optional[str] = None, |
| 113 | + ) -> DataFrame: |
| 114 | + config = ConfigConverter.convert_to_gds_config( |
| 115 | + concurrency=concurrency, |
| 116 | + consecutive_ids=consecutive_ids, |
| 117 | + job_id=job_id, |
| 118 | + log_progress=log_progress, |
| 119 | + min_component_size=min_component_size, |
| 120 | + node_labels=node_labels, |
| 121 | + relationship_types=relationship_types, |
| 122 | + relationship_weight_property=relationship_weight_property, |
| 123 | + seed_property=seed_property, |
| 124 | + sudo=sudo, |
| 125 | + threshold=threshold, |
| 126 | + username=username, |
| 127 | + ) |
| 128 | + |
| 129 | + # Run procedure and return results |
| 130 | + params = CallParameters(graph_name=G.name(), config=config) |
| 131 | + params.ensure_job_id_in_config() |
| 132 | + |
| 133 | + return self._query_runner.call_procedure(endpoint="gds.wcc.stream", params=params) |
| 134 | + |
| 135 | + def write( |
| 136 | + self, |
| 137 | + G: Graph, |
| 138 | + write_property: str, |
| 139 | + min_component_size: Optional[int] = None, |
| 140 | + threshold: Optional[float] = None, |
| 141 | + relationship_types: Optional[List[str]] = None, |
| 142 | + node_labels: Optional[List[str]] = None, |
| 143 | + sudo: Optional[bool] = None, |
| 144 | + log_progress: Optional[bool] = None, |
| 145 | + username: Optional[str] = None, |
| 146 | + concurrency: Optional[int] = None, |
| 147 | + job_id: Optional[str] = None, |
| 148 | + seed_property: Optional[str] = None, |
| 149 | + consecutive_ids: Optional[bool] = None, |
| 150 | + relationship_weight_property: Optional[str] = None, |
| 151 | + write_concurrency: Optional[int] = None, |
| 152 | + ) -> WccWriteResult: |
| 153 | + config = ConfigConverter.convert_to_gds_config( |
| 154 | + write_property=write_property, |
| 155 | + concurrency=concurrency, |
| 156 | + consecutive_ids=consecutive_ids, |
| 157 | + job_id=job_id, |
| 158 | + log_progress=log_progress, |
| 159 | + min_component_size=min_component_size, |
| 160 | + node_labels=node_labels, |
| 161 | + relationship_types=relationship_types, |
| 162 | + relationship_weight_property=relationship_weight_property, |
| 163 | + seed_property=seed_property, |
| 164 | + sudo=sudo, |
| 165 | + threshold=threshold, |
| 166 | + username=username, |
| 167 | + ) |
| 168 | + |
| 169 | + if write_concurrency is not None: |
| 170 | + config["writeConcurrency"] = write_concurrency |
| 171 | + |
| 172 | + params = CallParameters(graph_name=G.name(), config=config) |
| 173 | + params.ensure_job_id_in_config() |
| 174 | + |
| 175 | + result = self._query_runner.call_procedure(endpoint="gds.wcc.write", params=params).squeeze() # type: ignore |
| 176 | + |
| 177 | + return WccWriteResult(**result.to_dict()) |
| 178 | + |
| 179 | + def estimate( |
| 180 | + self, graph_name: Optional[str] = None, projection_config: Optional[dict[str, Any]] = None |
| 181 | + ) -> EstimationResult: |
| 182 | + config: Union[str, dict[str, Any]] = {} |
| 183 | + |
| 184 | + if graph_name is not None: |
| 185 | + config = graph_name |
| 186 | + elif projection_config is not None: |
| 187 | + config = projection_config |
| 188 | + else: |
| 189 | + raise ValueError("Either graph_name or projection_config must be provided.") |
| 190 | + |
| 191 | + params = CallParameters(config=config) |
| 192 | + |
| 193 | + result = self._query_runner.call_procedure(endpoint="gds.wcc.stats.estimate", params=params).squeeze() |
| 194 | + |
| 195 | + return EstimationResult(**result.to_dict()) |
0 commit comments