|
| 1 | +// Copyright (c) 2022, NVIDIA CORPORATION. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import {Float32Buffer, MemoryView} from '@rapidsai/cuda'; |
| 16 | +import {DataFrame, DataType, Int32, scope, Series} from '@rapidsai/cudf'; |
| 17 | +import {DeviceBuffer} from '@rapidsai/rmm'; |
| 18 | + |
| 19 | +import {GraphCOO} from './addon'; |
| 20 | +import {CUGraph, ForceAtlas2Options} from './node_cugraph'; |
| 21 | +import {renumberEdges, renumberNodes} from './renumber'; |
| 22 | + |
| 23 | +export interface GraphOptions { |
| 24 | + directedEdges?: boolean; |
| 25 | +} |
| 26 | + |
| 27 | +export class Graph<T extends DataType = any> { |
| 28 | + public static fromEdgeList<T extends DataType>(src: Series<T>, |
| 29 | + dst: Series<T>, |
| 30 | + options: GraphOptions = {directedEdges: true}) { |
| 31 | + const nodes = renumberNodes(src, dst); |
| 32 | + const edges = renumberEdges(src, dst, nodes); |
| 33 | + return new Graph(nodes, edges, options); |
| 34 | + } |
| 35 | + |
| 36 | + protected constructor(nodes: DataFrame<{id: Int32, node: T}>, |
| 37 | + edges: DataFrame<{id: Int32, src: Int32, dst: Int32}>, |
| 38 | + options: GraphOptions = {directedEdges: true}) { |
| 39 | + this._edges = edges; |
| 40 | + this._nodes = nodes; |
| 41 | + this._directed = options?.directedEdges ?? true; |
| 42 | + } |
| 43 | + |
| 44 | + declare protected _nodes: DataFrame<{id: Int32, node: T}>; |
| 45 | + declare protected _edges: DataFrame<{id: Int32, src: Int32, dst: Int32}>; |
| 46 | + declare protected _directed: boolean; |
| 47 | + |
| 48 | + declare protected _graph: CUGraph; |
| 49 | + |
| 50 | + protected get graph(): CUGraph { |
| 51 | + return this._graph || (this._graph = new GraphCOO(this._edges.get('src')._col, |
| 52 | + this._edges.get('dst')._col, |
| 53 | + {directedEdges: this._directed})); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @summary The number of edges in this Graph |
| 58 | + */ |
| 59 | + public get numEdges() { return this.graph.numEdges(); } |
| 60 | + |
| 61 | + /** |
| 62 | + * @summary The number of nodes in this Graph |
| 63 | + */ |
| 64 | + public get numNodes() { return this.graph.numNodes(); } |
| 65 | + |
| 66 | + public get nodes() { return this._nodes.drop(['id']); } |
| 67 | + |
| 68 | + public get edges() { |
| 69 | + const unnumber = (typ: 'src'|'dst') => { |
| 70 | + const id = this._edges.get(typ); |
| 71 | + const eid = this._edges.get('id'); |
| 72 | + const lhs = new DataFrame({id, eid}); |
| 73 | + const rhs = this._nodes.rename({node: typ}); |
| 74 | + return lhs.join({on: ['id'], other: rhs}); |
| 75 | + }; |
| 76 | + |
| 77 | + return scope(() => unnumber('src') // |
| 78 | + .join({on: ['eid'], other: unnumber('dst')}) |
| 79 | + .sortValues({eid: {ascending: true}}), |
| 80 | + [this]) |
| 81 | + .rename({eid: 'id'}) |
| 82 | + .select(['id', 'src', 'dst']); |
| 83 | + } |
| 84 | + |
| 85 | + public get nodeIds() { return this._nodes.select(['id']); } |
| 86 | + |
| 87 | + public get edgeIds() { return this._edges.select(['id', 'src', 'dst']); } |
| 88 | + |
| 89 | + /** |
| 90 | + * @summary Compute the total number of edges incident to a vertex (both in and out edges). |
| 91 | + */ |
| 92 | + public degree() { |
| 93 | + return new DataFrame({id: this._nodes.get('id')._col, degree: this.graph.degree()}); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @summary ForceAtlas2 is a continuous graph layout algorithm for handy network visualization. |
| 98 | + * |
| 99 | + * @note Peak memory allocation occurs at 30*V. |
| 100 | + * |
| 101 | + * @param {ForceAtlas2Options} options |
| 102 | + * |
| 103 | + * @returns {Float32Buffer} The new positions. |
| 104 | + */ |
| 105 | + public forceAtlas2(options: ForceAtlas2Options = {}) { |
| 106 | + const {numNodes} = this; |
| 107 | + let positions: Float32Buffer|undefined; |
| 108 | + if (options.positions) { |
| 109 | + positions = options.positions ? new Float32Buffer(options.positions instanceof MemoryView |
| 110 | + ? options.positions?.buffer |
| 111 | + : options.positions) |
| 112 | + : undefined; |
| 113 | + if (positions && positions.length !== numNodes * 2) { |
| 114 | + // reallocate new positions and copy over old X/Y positions |
| 115 | + const p = |
| 116 | + new Float32Buffer(new DeviceBuffer(numNodes * 2 * Float32Buffer.BYTES_PER_ELEMENT)); |
| 117 | + if (positions.length > 0) { |
| 118 | + const pn = positions.length / 2; |
| 119 | + const sx = positions.subarray(0, Math.min(numNodes, pn)); |
| 120 | + const sy = positions.subarray(pn, pn + Math.min(numNodes, pn)); |
| 121 | + p.copyFrom(sx, 0, 0).copyFrom(sy, 0, numNodes); |
| 122 | + } |
| 123 | + positions = p; |
| 124 | + } |
| 125 | + } |
| 126 | + return new Float32Buffer(this.graph.forceAtlas2({...options, positions})); |
| 127 | + } |
| 128 | +} |
0 commit comments