|
| 1 | +from typing import Any |
| 2 | + |
| 3 | +from pandas import DataFrame |
| 4 | + |
| 5 | +from graphdatascience.procedure_surface.api.catalog.graph_api import GraphV2 |
| 6 | +from graphdatascience.procedure_surface.api.estimation_result import EstimationResult |
| 7 | +from graphdatascience.procedure_surface.api.model.graphsage_model import GraphSageModelV2 |
| 8 | +from graphdatascience.procedure_surface.api.node_embedding.graphsage_predict_endpoints import ( |
| 9 | + GraphSageMutateResult, |
| 10 | + GraphSagePredictEndpoints, |
| 11 | + GraphSageWriteResult, |
| 12 | +) |
| 13 | +from graphdatascience.procedure_surface.api.node_embedding.graphsage_train_endpoints import ( |
| 14 | + GraphSageTrainEndpoints, |
| 15 | + GraphSageTrainResult, |
| 16 | +) |
| 17 | + |
| 18 | + |
| 19 | +class GraphSageEndpoints(GraphSageTrainEndpoints, GraphSagePredictEndpoints): |
| 20 | + """ |
| 21 | + API for the GraphSage algorithm, combining both training and prediction functionalities. |
| 22 | + """ |
| 23 | + |
| 24 | + def __init__( |
| 25 | + self, |
| 26 | + train_endpoints: GraphSageTrainEndpoints, |
| 27 | + predict_endpoints: GraphSagePredictEndpoints, |
| 28 | + ) -> None: |
| 29 | + self._train_endpoints = train_endpoints |
| 30 | + self._predict_endpoints = predict_endpoints |
| 31 | + |
| 32 | + def train( |
| 33 | + self, |
| 34 | + G: GraphV2, |
| 35 | + model_name: str, |
| 36 | + feature_properties: list[str], |
| 37 | + *, |
| 38 | + activation_function: Any | None = None, |
| 39 | + negative_sample_weight: int | None = None, |
| 40 | + embedding_dimension: int | None = None, |
| 41 | + tolerance: float | None = None, |
| 42 | + learning_rate: float | None = None, |
| 43 | + max_iterations: int | None = None, |
| 44 | + sample_sizes: list[int] | None = None, |
| 45 | + aggregator: Any | None = None, |
| 46 | + penalty_l2: float | None = None, |
| 47 | + search_depth: int | None = None, |
| 48 | + epochs: int | None = None, |
| 49 | + projected_feature_dimension: int | None = None, |
| 50 | + batch_sampling_ratio: float | None = None, |
| 51 | + store_model_to_disk: bool | None = None, |
| 52 | + relationship_types: list[str] | None = None, |
| 53 | + node_labels: list[str] | None = None, |
| 54 | + username: str | None = None, |
| 55 | + log_progress: bool = True, |
| 56 | + sudo: bool | None = None, |
| 57 | + concurrency: Any | None = None, |
| 58 | + job_id: Any | None = None, |
| 59 | + batch_size: int | None = None, |
| 60 | + relationship_weight_property: str | None = None, |
| 61 | + random_seed: Any | None = None, |
| 62 | + ) -> tuple[GraphSageModelV2, GraphSageTrainResult]: |
| 63 | + return self._train_endpoints.train( |
| 64 | + G, |
| 65 | + model_name, |
| 66 | + feature_properties, |
| 67 | + activation_function=activation_function, |
| 68 | + negative_sample_weight=negative_sample_weight, |
| 69 | + embedding_dimension=embedding_dimension, |
| 70 | + tolerance=tolerance, |
| 71 | + learning_rate=learning_rate, |
| 72 | + max_iterations=max_iterations, |
| 73 | + sample_sizes=sample_sizes, |
| 74 | + aggregator=aggregator, |
| 75 | + penalty_l2=penalty_l2, |
| 76 | + search_depth=search_depth, |
| 77 | + epochs=epochs, |
| 78 | + projected_feature_dimension=projected_feature_dimension, |
| 79 | + batch_sampling_ratio=batch_sampling_ratio, |
| 80 | + store_model_to_disk=store_model_to_disk, |
| 81 | + relationship_types=relationship_types, |
| 82 | + node_labels=node_labels, |
| 83 | + username=username, |
| 84 | + log_progress=log_progress, |
| 85 | + sudo=sudo, |
| 86 | + concurrency=concurrency, |
| 87 | + job_id=job_id, |
| 88 | + batch_size=batch_size, |
| 89 | + relationship_weight_property=relationship_weight_property, |
| 90 | + random_seed=random_seed, |
| 91 | + ) |
| 92 | + |
| 93 | + def stream( |
| 94 | + self, |
| 95 | + G: GraphV2, |
| 96 | + model_name: str, |
| 97 | + *, |
| 98 | + relationship_types: list[str] | None = None, |
| 99 | + node_labels: list[str] | None = None, |
| 100 | + username: str | None = None, |
| 101 | + log_progress: bool = True, |
| 102 | + sudo: bool | None = None, |
| 103 | + concurrency: Any | None = None, |
| 104 | + job_id: Any | None = None, |
| 105 | + batch_size: int | None = None, |
| 106 | + ) -> DataFrame: |
| 107 | + return self._predict_endpoints.stream( |
| 108 | + G, |
| 109 | + model_name, |
| 110 | + relationship_types=relationship_types, |
| 111 | + node_labels=node_labels, |
| 112 | + username=username, |
| 113 | + log_progress=log_progress, |
| 114 | + sudo=sudo, |
| 115 | + concurrency=concurrency, |
| 116 | + job_id=job_id, |
| 117 | + batch_size=batch_size, |
| 118 | + ) |
| 119 | + |
| 120 | + def write( |
| 121 | + self, |
| 122 | + G: GraphV2, |
| 123 | + model_name: str, |
| 124 | + write_property: str, |
| 125 | + *, |
| 126 | + relationship_types: list[str] | None = None, |
| 127 | + node_labels: list[str] | None = None, |
| 128 | + username: str | None = None, |
| 129 | + log_progress: bool = True, |
| 130 | + sudo: bool | None = None, |
| 131 | + concurrency: Any | None = None, |
| 132 | + write_concurrency: int | None = None, |
| 133 | + job_id: Any | None = None, |
| 134 | + batch_size: int | None = None, |
| 135 | + ) -> GraphSageWriteResult: |
| 136 | + return self._predict_endpoints.write( |
| 137 | + G, |
| 138 | + model_name, |
| 139 | + write_property, |
| 140 | + relationship_types=relationship_types, |
| 141 | + node_labels=node_labels, |
| 142 | + username=username, |
| 143 | + log_progress=log_progress, |
| 144 | + sudo=sudo, |
| 145 | + concurrency=concurrency, |
| 146 | + write_concurrency=write_concurrency, |
| 147 | + job_id=job_id, |
| 148 | + batch_size=batch_size, |
| 149 | + ) |
| 150 | + |
| 151 | + def mutate( |
| 152 | + self, |
| 153 | + G: GraphV2, |
| 154 | + model_name: str, |
| 155 | + mutate_property: str, |
| 156 | + relationship_types: list[str] | None = None, |
| 157 | + node_labels: list[str] | None = None, |
| 158 | + username: str | None = None, |
| 159 | + log_progress: bool = True, |
| 160 | + sudo: bool | None = None, |
| 161 | + concurrency: Any | None = None, |
| 162 | + job_id: Any | None = None, |
| 163 | + batch_size: int | None = None, |
| 164 | + ) -> GraphSageMutateResult: |
| 165 | + return self._predict_endpoints.mutate( |
| 166 | + G, |
| 167 | + model_name, |
| 168 | + mutate_property, |
| 169 | + relationship_types=relationship_types, |
| 170 | + node_labels=node_labels, |
| 171 | + username=username, |
| 172 | + log_progress=log_progress, |
| 173 | + sudo=sudo, |
| 174 | + concurrency=concurrency, |
| 175 | + job_id=job_id, |
| 176 | + batch_size=batch_size, |
| 177 | + ) |
| 178 | + |
| 179 | + def estimate( |
| 180 | + self, |
| 181 | + G: GraphV2 | dict[str, Any], |
| 182 | + model_name: str, |
| 183 | + relationship_types: list[str] | None = None, |
| 184 | + node_labels: list[str] | None = None, |
| 185 | + batch_size: int | None = None, |
| 186 | + concurrency: int | None = None, |
| 187 | + log_progress: bool = True, |
| 188 | + username: str | None = None, |
| 189 | + sudo: bool | None = None, |
| 190 | + job_id: str | None = None, |
| 191 | + ) -> EstimationResult: |
| 192 | + return self._predict_endpoints.estimate( |
| 193 | + G, |
| 194 | + model_name, |
| 195 | + relationship_types=relationship_types, |
| 196 | + node_labels=node_labels, |
| 197 | + batch_size=batch_size, |
| 198 | + concurrency=concurrency, |
| 199 | + log_progress=log_progress, |
| 200 | + username=username, |
| 201 | + sudo=sudo, |
| 202 | + job_id=job_id, |
| 203 | + ) |
0 commit comments