Class of edge data which is used for DGLGraph construction. Internal use only.
| 192 | |
| 193 | |
| 194 | class EdgeData(BaseData): |
| 195 | """Class of edge data which is used for DGLGraph construction. Internal use only.""" |
| 196 | |
| 197 | def __init__(self, src_id, dst_id, data, type=None, graph_id=None): |
| 198 | self.src = np.array(src_id) |
| 199 | self.dst = np.array(dst_id) |
| 200 | self.data = data |
| 201 | self.type = type if type is not None else ("_V", "_E", "_V") |
| 202 | self.graph_id = ( |
| 203 | np.array(graph_id) |
| 204 | if graph_id is not None |
| 205 | else np.full(len(src_id), 0) |
| 206 | ) |
| 207 | _validate_data_length( |
| 208 | { |
| 209 | **{"src": self.src, "dst": self.dst, "graph_id": self.graph_id}, |
| 210 | **self.data, |
| 211 | } |
| 212 | ) |
| 213 | |
| 214 | @staticmethod |
| 215 | def load_from_csv( |
| 216 | meta: MetaEdge, data_parser: Callable, base_dir=None, separator="," |
| 217 | ): |
| 218 | df = BaseData.read_csv(meta.file_name, base_dir, separator) |
| 219 | src_ids = BaseData.pop_from_dataframe(df, meta.src_id_field) |
| 220 | if src_ids is None: |
| 221 | raise DGLError( |
| 222 | "Missing src id field [{}] in file [{}].".format( |
| 223 | meta.src_id_field, meta.file_name |
| 224 | ) |
| 225 | ) |
| 226 | dst_ids = BaseData.pop_from_dataframe(df, meta.dst_id_field) |
| 227 | if dst_ids is None: |
| 228 | raise DGLError( |
| 229 | "Missing dst id field [{}] in file [{}].".format( |
| 230 | meta.dst_id_field, meta.file_name |
| 231 | ) |
| 232 | ) |
| 233 | graph_ids = BaseData.pop_from_dataframe(df, meta.graph_id_field) |
| 234 | etype = tuple(meta.etype) |
| 235 | edata = data_parser(df) |
| 236 | return EdgeData(src_ids, dst_ids, edata, type=etype, graph_id=graph_ids) |
| 237 | |
| 238 | @staticmethod |
| 239 | def to_dict(edge_data: List["EdgeData"], node_dict: dict) -> dict: |
| 240 | edge_dict = {} |
| 241 | for e_data in edge_data: |
| 242 | (src_type, e_type, dst_type) = e_data.type |
| 243 | graph_ids = np.unique(e_data.graph_id) |
| 244 | for graph_id in graph_ids: |
| 245 | if graph_id in edge_dict and e_data.type in edge_dict[graph_id]: |
| 246 | raise DGLError( |
| 247 | f"Duplicate edge type[{e_data.type}] for same graph[{graph_id}], please place the same edge_type for same graph into single EdgeData." |
| 248 | ) |
| 249 | idx = e_data.graph_id == graph_id |
| 250 | src_mapping = node_dict[graph_id][src_type]["mapping"] |
| 251 | dst_mapping = node_dict[graph_id][dst_type]["mapping"] |
no outgoing calls