-
Notifications
You must be signed in to change notification settings - Fork 266
Expand file tree
/
Copy pathutils.py
More file actions
130 lines (111 loc) · 3.98 KB
/
utils.py
File metadata and controls
130 lines (111 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
"""Utilities for Network Moitif Finding Tutorial"""
from pyspark.sql import DataFrame
from pyspark.sql import functions as F
from graphframes import GraphFrame
def three_edge_count(paths: DataFrame) -> DataFrame:
"""three_edge_count View the counts of the different types of 3-node graphlets in the graph.
Parameters
----------
paths : pyspark.sql.DataFrame
A DataFrame of 3-paths in the graph.
Returns
-------
DataFrame
A DataFrame of the counts of the different types of 3-node graphlets in the graph.
""" # noqa: E501
graphlet_type_df = paths.select(
F.col("a.Type").alias("A_Type"),
F.col("e1.relationship").alias("E_relationship"),
F.col("b.Type").alias("B_Type"),
F.col("e2.relationship").alias("E2_relationship"),
F.col("c.Type").alias("C_Type"),
F.col("e3.relationship").alias("E3_relationship"),
F.when(F.col("d").isNotNull(), F.col("d.Type")).alias("D_Type"),
)
graphlet_count_df = (
graphlet_type_df.groupby(
"A_Type",
"E_relationship",
"B_Type",
"E2_relationship",
"C_Type",
"E3_relationship",
)
.count()
.orderBy(F.col("count").desc())
# Add a comma formatted column for display
.withColumn("count", F.format_number(F.col("count"), 0))
)
return graphlet_count_df
def four_edge_count(paths: DataFrame) -> DataFrame:
"""four_edge_count View the counts of the different types of 4-node graphlets in the graph.
Parameters
----------
paths : DataFrame
A DataFrame of 4-paths in the graph.
Returns
-------
DataFrame
A DataFrame of the counts of the different types of 4-node graphlets in the graph.
"""
graphlet_type_df = paths.select(
F.col("a.Type").alias("A_Type"),
F.col("e1.relationship").alias("E_relationship"),
F.col("b.Type").alias("B_Type"),
F.col("e2.relationship").alias("E2_relationship"),
F.col("c.Type").alias("C_Type"),
F.col("e3.relationship").alias("E3_relationship"),
F.col("d.Type").alias("D_Type"),
F.col("e4.relationship").alias("E4_relationship"),
F.when(F.col("e").isNotNull(), F.col("e.Type")).alias("E_Type"),
)
graphlet_count_df = (
graphlet_type_df.groupby(
"A_Type",
"E_relationship",
"B_Type",
"E2_relationship",
"C_Type",
"E3_relationship",
"D_Type",
"E4_relationship",
)
.count()
.orderBy(F.col("count").desc())
# Add a comma formatted column for display
.withColumn("count", F.format_number(F.col("count"), 0))
)
return graphlet_count_df
def add_degree(g: GraphFrame) -> GraphFrame:
"""add_degree compute the degree, adding it as a property of the nodes in the GraphFrame.
Parameters
----------
g : GraphFrame
Any valid GraphFrame
Returns
-------
GraphFrame
Same GraphFrame with a 'degree' property added
"""
degree_vertices: DataFrame = g.vertices.join(g.degrees, on="id")
return GraphFrame(degree_vertices, g.edges)
def add_type_degree(g: GraphFrame) -> DataFrame:
"""add_type_degree add a map property to the vertices with the degree by each type of relationship.
Parameters
----------
g : GraphFrame
Any valid GraphFrame
Returns
-------
DataFrame - I am broke, next line is wrong
A GraphFrame with a map[type:degree] 'type_degree' field added to the vertices
""" # noqa: E501
type_degree: DataFrame = (
g.edges.select(F.col("src").alias("id"), "relationship")
.filter(F.col("id").isNotNull())
.groupby("id", "relationship")
.count()
)
type_degree = type_degree.withColumn("type_degree", F.create_map(type_degree.columns))
type_degree = type_degree.select("src", "type_degree")
return g.vertices.join(type_degree, on="src")