|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from abc import ABC, abstractmethod |
| 4 | +from typing import List, Optional |
| 5 | + |
| 6 | +from graphdatascience import Graph |
| 7 | +from graphdatascience.procedure_surface.api.base_result import BaseResult |
| 8 | + |
| 9 | + |
| 10 | +class GraphSamplingEndpoints(ABC): |
| 11 | + """ |
| 12 | + Abstract base class defining the API for graph sampling algorithms algorithm. |
| 13 | + """ |
| 14 | + |
| 15 | + @abstractmethod |
| 16 | + def rwr( |
| 17 | + self, |
| 18 | + G: Graph, |
| 19 | + graph_name: str, |
| 20 | + start_nodes: Optional[List[int]] = None, |
| 21 | + restart_probability: Optional[float] = None, |
| 22 | + sampling_ratio: Optional[float] = None, |
| 23 | + node_label_stratification: Optional[bool] = None, |
| 24 | + relationship_weight_property: Optional[str] = None, |
| 25 | + relationship_types: Optional[List[str]] = None, |
| 26 | + node_labels: Optional[List[str]] = None, |
| 27 | + sudo: Optional[bool] = None, |
| 28 | + log_progress: Optional[bool] = None, |
| 29 | + username: Optional[str] = None, |
| 30 | + concurrency: Optional[int] = None, |
| 31 | + job_id: Optional[str] = None, |
| 32 | + ) -> GraphSamplingResult: |
| 33 | + """ |
| 34 | + Computes a set of Random Walks with Restart (RWR) for the given graph and stores the result as a new graph in the catalog. |
| 35 | +
|
| 36 | + This method performs a random walk, beginning from a set of nodes (if provided), |
| 37 | + where at each step there is a probability to restart back at the original nodes. |
| 38 | + The result is turned into a new graph induced by the random walks and stored in the catalog. |
| 39 | +
|
| 40 | + Parameters |
| 41 | + ---------- |
| 42 | + G : Graph |
| 43 | + The input graph on which the Random Walk with Restart (RWR) will be |
| 44 | + performed. |
| 45 | + graph_name : str |
| 46 | + The name of the new graph in the catalog. |
| 47 | + start_nodes : list of int, optional |
| 48 | + A list of node IDs to start the random walk from. If not provided, all |
| 49 | + nodes are used as potential starting points. |
| 50 | + restart_probability : float, optional |
| 51 | + The probability of restarting back to the original node at each step. |
| 52 | + Should be a value between 0 and 1. If not specified, a default value is used. |
| 53 | + sampling_ratio : float, optional |
| 54 | + The ratio of nodes to sample during the computation. This value should |
| 55 | + be between 0 and 1. If not specified, no sampling is performed. |
| 56 | + node_label_stratification : bool, optional |
| 57 | + If True, the algorithm tries to preserve the label distribution of the original graph in the sampled graph. |
| 58 | + relationship_weight_property : str, optional |
| 59 | + The name of the property on relationships to use as weights during |
| 60 | + the random walk. If not specified, the relationships are treated as |
| 61 | + unweighted. |
| 62 | + relationship_types : list of str, optional |
| 63 | + The relationship types used to select relationships for this algorithm run. |
| 64 | + node_labels : list of str, optional |
| 65 | + The node labels used to select nodes for this algorithm run. |
| 66 | + sudo : bool, optional |
| 67 | + Override memory estimation limits. Use with caution as this can lead to |
| 68 | + memory issues if the estimation is significantly wrong. |
| 69 | + log_progress : bool, optional |
| 70 | + If True, logs the progress of the computation. |
| 71 | + username : str, optional |
| 72 | + The username to attribute the procedure run to |
| 73 | + concurrency : int, optional |
| 74 | + The number of concurrent threads used for the algorithm execution. |
| 75 | + job_id : str, optional |
| 76 | + An identifier for the job that can be used for monitoring and cancellation |
| 77 | +
|
| 78 | + Returns |
| 79 | + ------- |
| 80 | + GraphSamplingResult |
| 81 | + The result of the Random Walk with Restart (RWR), including the sampled |
| 82 | + nodes and their scores. |
| 83 | + """ |
| 84 | + pass |
| 85 | + |
| 86 | + @abstractmethod |
| 87 | + def cnarw( |
| 88 | + self, |
| 89 | + G: Graph, |
| 90 | + graph_name: str, |
| 91 | + start_nodes: Optional[List[int]] = None, |
| 92 | + restart_probability: Optional[float] = None, |
| 93 | + sampling_ratio: Optional[float] = None, |
| 94 | + node_label_stratification: Optional[bool] = None, |
| 95 | + relationship_weight_property: Optional[str] = None, |
| 96 | + relationship_types: Optional[List[str]] = None, |
| 97 | + node_labels: Optional[List[str]] = None, |
| 98 | + sudo: Optional[bool] = None, |
| 99 | + log_progress: Optional[bool] = None, |
| 100 | + username: Optional[str] = None, |
| 101 | + concurrency: Optional[int] = None, |
| 102 | + job_id: Optional[str] = None, |
| 103 | + ) -> GraphSamplingResult: |
| 104 | + """ |
| 105 | + Computes a set of Random Walks with Restart (RWR) for the given graph and stores the result as a new graph in the catalog. |
| 106 | +
|
| 107 | + This method performs a random walk, beginning from a set of nodes (if provided), |
| 108 | + where at each step there is a probability to restart back at the original nodes. |
| 109 | + The result is turned into a new graph induced by the random walks and stored in the catalog. |
| 110 | +
|
| 111 | + Parameters |
| 112 | + ---------- |
| 113 | + G : Graph |
| 114 | + The input graph on which the Random Walk with Restart (RWR) will be |
| 115 | + performed. |
| 116 | + graph_name : str |
| 117 | + The name of the new graph in the catalog. |
| 118 | + start_nodes : list of int, optional |
| 119 | + A list of node IDs to start the random walk from. If not provided, all |
| 120 | + nodes are used as potential starting points. |
| 121 | + restart_probability : float, optional |
| 122 | + The probability of restarting back to the original node at each step. |
| 123 | + Should be a value between 0 and 1. If not specified, a default value is used. |
| 124 | + sampling_ratio : float, optional |
| 125 | + The ratio of nodes to sample during the computation. This value should |
| 126 | + be between 0 and 1. If not specified, no sampling is performed. |
| 127 | + node_label_stratification : bool, optional |
| 128 | + If True, the algorithm tries to preserve the label distribution of the original graph in the sampled graph. |
| 129 | + relationship_weight_property : str, optional |
| 130 | + The name of the property on relationships to use as weights during |
| 131 | + the random walk. If not specified, the relationships are treated as |
| 132 | + unweighted. |
| 133 | + relationship_types : list of str, optional |
| 134 | + The relationship types used to select relationships for this algorithm run. |
| 135 | + node_labels : list of str, optional |
| 136 | + The node labels used to select nodes for this algorithm run. |
| 137 | + sudo : bool, optional |
| 138 | + Override memory estimation limits. Use with caution as this can lead to |
| 139 | + memory issues if the estimation is significantly wrong. |
| 140 | + log_progress : bool, optional |
| 141 | + If True, logs the progress of the computation. |
| 142 | + username : str, optional |
| 143 | + The username to attribute the procedure run to |
| 144 | + concurrency : int, optional |
| 145 | + The number of concurrent threads used for the algorithm execution. |
| 146 | + job_id : str, optional |
| 147 | + An identifier for the job that can be used for monitoring and cancellation |
| 148 | +
|
| 149 | + Returns |
| 150 | + ------- |
| 151 | + GraphSamplingResult |
| 152 | + The result of the Random Walk with Restart (RWR), including the sampled |
| 153 | + nodes and their scores. |
| 154 | + """ |
| 155 | + pass |
| 156 | + |
| 157 | + |
| 158 | +class GraphSamplingResult(BaseResult): |
| 159 | + graph_name: str |
| 160 | + from_graph_name: str |
| 161 | + node_count: int |
| 162 | + relationship_count: int |
| 163 | + start_node_count: int |
| 164 | + project_millis: int |
0 commit comments