1
- use crate :: pregel:: Column :: { Custom , Dst , Id , Src } ;
1
+ use crate :: pregel:: Column :: { Custom , Object , Subject , VertexId } ;
2
2
use polars:: prelude:: * ;
3
3
use std:: fmt:: { Debug , Display , Formatter } ;
4
4
use std:: { error, fmt} ;
@@ -76,24 +76,24 @@ impl error::Error for GraphFrameError {
76
76
#[ derive( Debug ) ]
77
77
pub enum MissingColumnError {
78
78
/// `Id` is a variant of `MissingColumnError` that represents the error that
79
- /// occurs when the `Id ` column is missing from a DataFrame.
80
- Id ,
79
+ /// occurs when the `VertexId ` column is missing from a DataFrame.
80
+ VertexId ,
81
81
/// `Src` is a variant of `MissingColumnError` that represents the error that
82
- /// occurs when the `Src ` column is missing from a DataFrame.
83
- Src ,
82
+ /// occurs when the `Subject ` column is missing from a DataFrame.
83
+ Subject ,
84
84
/// `Dst` is a variant of `MissingColumnError` that represents the error that
85
- /// occurs when the `Dst ` column is missing from a DataFrame.
86
- Dst ,
85
+ /// occurs when the `Object ` column is missing from a DataFrame.
86
+ Object ,
87
87
}
88
88
89
89
impl Display for MissingColumnError {
90
90
fn fmt ( & self , f : & mut Formatter ) -> fmt:: Result {
91
91
let message = |df, column : & str | format ! ( "Missing column {} in {}" , column, df) ;
92
92
93
93
match self {
94
- MissingColumnError :: Id => write ! ( f, "{}" , message( "vertices" , Id . as_ref( ) ) ) ,
95
- MissingColumnError :: Src => write ! ( f, "{}" , message( "edges" , Src . as_ref( ) ) ) ,
96
- MissingColumnError :: Dst => write ! ( f, "{}" , message( "edges" , Dst . as_ref( ) ) ) ,
94
+ MissingColumnError :: VertexId => write ! ( f, "{}" , message( "vertices" , VertexId . as_ref( ) ) ) ,
95
+ MissingColumnError :: Subject => write ! ( f, "{}" , message( "edges" , Subject . as_ref( ) ) ) ,
96
+ MissingColumnError :: Object => write ! ( f, "{}" , message( "edges" , Object . as_ref( ) ) ) ,
97
97
}
98
98
}
99
99
}
@@ -124,14 +124,14 @@ impl GraphFrame {
124
124
/// `vertices` and `edges` DataFrames. If any of the required columns (`Id`, `Src`,
125
125
/// `Dst`) are missing in the DataFrames, the function returns an `Error`.
126
126
pub fn new ( vertices : DataFrame , edges : DataFrame ) -> Result < Self > {
127
- if !vertices. get_column_names ( ) . contains ( & Id . as_ref ( ) ) {
128
- return Err ( GraphFrameError :: MissingColumn ( MissingColumnError :: Id ) ) ;
127
+ if !vertices. get_column_names ( ) . contains ( & VertexId . as_ref ( ) ) {
128
+ return Err ( GraphFrameError :: MissingColumn ( MissingColumnError :: VertexId ) ) ;
129
129
}
130
- if !edges. get_column_names ( ) . contains ( & Src . as_ref ( ) ) {
131
- return Err ( GraphFrameError :: MissingColumn ( MissingColumnError :: Src ) ) ;
130
+ if !edges. get_column_names ( ) . contains ( & Subject . as_ref ( ) ) {
131
+ return Err ( GraphFrameError :: MissingColumn ( MissingColumnError :: Subject ) ) ;
132
132
}
133
- if !edges. get_column_names ( ) . contains ( & Dst . as_ref ( ) ) {
134
- return Err ( GraphFrameError :: MissingColumn ( MissingColumnError :: Dst ) ) ;
133
+ if !edges. get_column_names ( ) . contains ( & Object . as_ref ( ) ) {
134
+ return Err ( GraphFrameError :: MissingColumn ( MissingColumnError :: Object ) ) ;
135
135
}
136
136
137
137
Ok ( GraphFrame { vertices, edges } )
@@ -152,17 +152,17 @@ impl GraphFrame {
152
152
/// The `from_edges` function returns a `Result<Self>` where `Self` is the
153
153
/// `GraphFrame` struct.
154
154
pub fn from_edges ( edges : DataFrame ) -> Result < Self > {
155
- let srcs = edges
155
+ let subjects = edges
156
156
. clone ( ) // this is because cloning a DataFrame is cheap
157
157
. lazy ( )
158
- . select ( [ col ( Src . as_ref ( ) ) . alias ( Id . as_ref ( ) ) ] ) ;
159
- let dsts = edges
158
+ . select ( [ col ( Subject . as_ref ( ) ) . alias ( VertexId . as_ref ( ) ) ] ) ;
159
+ let objects = edges
160
160
. clone ( ) // this is because cloning a DataFrame is cheap
161
161
. lazy ( )
162
- . select ( [ col ( Dst . as_ref ( ) ) . alias ( Id . as_ref ( ) ) ] ) ;
163
- let vertices = concat ( [ srcs , dsts ] , true , true ) ?
162
+ . select ( [ col ( Object . as_ref ( ) ) . alias ( VertexId . as_ref ( ) ) ] ) ;
163
+ let vertices = concat ( [ subjects , objects ] , true , true ) ?
164
164
. unique (
165
- Some ( vec ! [ Id . as_ref( ) . to_string( ) ] ) ,
165
+ Some ( vec ! [ VertexId . as_ref( ) . to_string( ) ] ) ,
166
166
UniqueKeepStrategy :: First ,
167
167
)
168
168
. collect ( ) ?;
@@ -184,7 +184,7 @@ impl GraphFrame {
184
184
pub fn out_degrees ( self ) -> PolarsResult < DataFrame > {
185
185
self . edges
186
186
. lazy ( )
187
- . groupby ( [ col ( Src . as_ref ( ) ) . alias ( Id . as_ref ( ) ) ] )
187
+ . groupby ( [ col ( Subject . as_ref ( ) ) . alias ( VertexId . as_ref ( ) ) ] )
188
188
. agg ( [ count ( ) . alias ( Custom ( "out_degree" ) . as_ref ( ) ) ] )
189
189
. collect ( )
190
190
}
@@ -203,7 +203,7 @@ impl GraphFrame {
203
203
pub fn in_degrees ( self ) -> PolarsResult < DataFrame > {
204
204
self . edges
205
205
. lazy ( )
206
- . groupby ( [ col ( Dst . as_ref ( ) ) ] )
206
+ . groupby ( [ col ( Object . as_ref ( ) ) ] )
207
207
. agg ( [ count ( ) . alias ( Custom ( "in_degree" ) . as_ref ( ) ) ] )
208
208
. collect ( )
209
209
}
@@ -226,26 +226,26 @@ impl Display for GraphFrame {
226
226
227
227
#[ cfg( test) ]
228
228
mod tests {
229
- use crate :: graph_frame:: GraphFrame ;
229
+ use crate :: graph_frame:: { GraphFrame , GraphFrameError } ;
230
+ use crate :: pregel:: Column ;
230
231
use polars:: prelude:: * ;
231
232
233
+ fn graph ( ) -> Result < GraphFrame , GraphFrameError > {
234
+ let subjects = Series :: new ( Column :: Subject . as_ref ( ) , [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] ) ;
235
+ let objects = Series :: new ( Column :: Object . as_ref ( ) , [ 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 1 ] ) ;
236
+ GraphFrame :: from_edges ( DataFrame :: new ( vec ! [ subjects, objects] ) . unwrap ( ) )
237
+ }
238
+
232
239
#[ test]
233
240
fn test_from_edges ( ) {
234
- let srcs = Series :: new ( "src" , [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] ) ;
235
- let dsts = Series :: new ( "dst" , [ 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 1 ] ) ;
236
- let edges = DataFrame :: new ( vec ! [ srcs, dsts] ) . unwrap ( ) ;
237
- let graphframe = GraphFrame :: from_edges ( edges) . unwrap ( ) ;
238
- assert_eq ! ( graphframe. vertices. height( ) , 10 ) ;
239
- assert_eq ! ( graphframe. edges. height( ) , 10 ) ;
241
+ let graph = graph ( ) . unwrap ( ) ;
242
+ assert_eq ! ( graph. vertices. height( ) , 10 ) ;
243
+ assert_eq ! ( graph. edges. height( ) , 10 ) ;
240
244
}
241
245
242
246
#[ test]
243
247
fn test_in_degree ( ) {
244
- let srcs = Series :: new ( "src" , [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] ) ;
245
- let dsts = Series :: new ( "dst" , [ 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 1 ] ) ;
246
- let edges = DataFrame :: new ( vec ! [ srcs, dsts] ) . unwrap ( ) ;
247
- let graphframe = GraphFrame :: from_edges ( edges) . unwrap ( ) ;
248
- let in_degree = graphframe. in_degrees ( ) . unwrap ( ) ;
248
+ let in_degree = graph ( ) . unwrap ( ) . in_degrees ( ) . unwrap ( ) ;
249
249
assert_eq ! ( in_degree. height( ) , 10 ) ;
250
250
assert_eq ! (
251
251
in_degree
@@ -261,11 +261,7 @@ mod tests {
261
261
262
262
#[ test]
263
263
fn test_out_degree ( ) {
264
- let srcs = Series :: new ( "src" , [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] ) ;
265
- let dsts = Series :: new ( "dst" , [ 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 1 ] ) ;
266
- let edges = DataFrame :: new ( vec ! [ srcs, dsts] ) . unwrap ( ) ;
267
- let graphframe = GraphFrame :: from_edges ( edges) . unwrap ( ) ;
268
- let out_degree = graphframe. out_degrees ( ) . unwrap ( ) ;
264
+ let out_degree = graph ( ) . unwrap ( ) . out_degrees ( ) . unwrap ( ) ;
269
265
assert_eq ! ( out_degree. height( ) , 10 ) ;
270
266
assert_eq ! (
271
267
out_degree
@@ -280,38 +276,40 @@ mod tests {
280
276
}
281
277
282
278
#[ test]
283
- fn test_new_missing_id_column ( ) {
284
- let vertices = DataFrame :: new ( vec ! [ Series :: new( "its_not_id " , [ 1 , 2 , 3 ] ) ] ) . unwrap ( ) ;
285
- let srcs = Series :: new ( "src" , [ 1 , 2 , 3 ] ) ;
286
- let dsts = Series :: new ( "dst" , [ 2 , 3 , 4 ] ) ;
287
- let edges = DataFrame :: new ( vec ! [ srcs , dsts ] ) . unwrap ( ) ;
279
+ fn test_new_missing_vertex_id_column ( ) {
280
+ let vertices = DataFrame :: new ( vec ! [ Series :: new( "not_vertex_id " , [ 1 , 2 , 3 ] ) ] ) . unwrap ( ) ;
281
+ let subjects = Series :: new ( Column :: Subject . as_ref ( ) , [ 1 , 2 , 3 ] ) ;
282
+ let objects = Series :: new ( Column :: Object . as_ref ( ) , [ 2 , 3 , 4 ] ) ;
283
+ let edges = DataFrame :: new ( vec ! [ subjects , objects ] ) . unwrap ( ) ;
288
284
match GraphFrame :: new ( vertices, edges) {
289
285
Ok ( _) => panic ! ( "Should have failed" ) ,
290
- Err ( e) => assert_eq ! ( e. to_string( ) , "Missing column id in vertices" ) ,
286
+ Err ( e) => assert_eq ! ( e. to_string( ) , "Missing column vertex_id in vertices" ) ,
291
287
}
292
288
}
293
289
294
290
#[ test]
295
- fn test_new_missing_src_column ( ) {
296
- let vertices = DataFrame :: new ( vec ! [ Series :: new( "id" , [ 1 , 2 , 3 ] ) ] ) . unwrap ( ) ;
297
- let srcs = Series :: new ( "its_not_src" , [ 1 , 2 , 3 ] ) ;
298
- let dsts = Series :: new ( "dst" , [ 2 , 3 , 4 ] ) ;
299
- let edges = DataFrame :: new ( vec ! [ srcs, dsts] ) . unwrap ( ) ;
291
+ fn test_new_missing_subject_column ( ) {
292
+ let vertices =
293
+ DataFrame :: new ( vec ! [ Series :: new( Column :: VertexId . as_ref( ) , [ 1 , 2 , 3 ] ) ] ) . unwrap ( ) ;
294
+ let subjects = Series :: new ( "not_src" , [ 1 , 2 , 3 ] ) ;
295
+ let objects = Series :: new ( Column :: Object . as_ref ( ) , [ 2 , 3 , 4 ] ) ;
296
+ let edges = DataFrame :: new ( vec ! [ subjects, objects] ) . unwrap ( ) ;
300
297
match GraphFrame :: new ( vertices, edges) {
301
298
Ok ( _) => panic ! ( "Should have failed" ) ,
302
- Err ( e) => assert_eq ! ( e. to_string( ) , "Missing column src in edges" ) ,
299
+ Err ( e) => assert_eq ! ( e. to_string( ) , "Missing column subject in edges" ) ,
303
300
}
304
301
}
305
302
306
303
#[ test]
307
- fn test_new_missing_dst_column ( ) {
308
- let vertices = DataFrame :: new ( vec ! [ Series :: new( "id" , [ 1 , 2 , 3 ] ) ] ) . unwrap ( ) ;
309
- let srcs = Series :: new ( "src" , [ 1 , 2 , 3 ] ) ;
310
- let dsts = Series :: new ( "its_not_dst" , [ 2 , 3 , 4 ] ) ;
311
- let edges = DataFrame :: new ( vec ! [ srcs, dsts] ) . unwrap ( ) ;
304
+ fn test_new_missing_object_column ( ) {
305
+ let vertices =
306
+ DataFrame :: new ( vec ! [ Series :: new( Column :: VertexId . as_ref( ) , [ 1 , 2 , 3 ] ) ] ) . unwrap ( ) ;
307
+ let subjects = Series :: new ( Column :: Subject . as_ref ( ) , [ 1 , 2 , 3 ] ) ;
308
+ let objects = Series :: new ( "not_dst" , [ 2 , 3 , 4 ] ) ;
309
+ let edges = DataFrame :: new ( vec ! [ subjects, objects] ) . unwrap ( ) ;
312
310
match GraphFrame :: new ( vertices, edges) {
313
311
Ok ( _) => panic ! ( "Should have failed" ) ,
314
- Err ( e) => assert_eq ! ( e. to_string( ) , "Missing column dst in edges" ) ,
312
+ Err ( e) => assert_eq ! ( e. to_string( ) , "Missing column object in edges" ) ,
315
313
}
316
314
}
317
315
}
0 commit comments