-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy path2.GraphBasics.py
211 lines (177 loc) · 6.08 KB
/
2.GraphBasics.py
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# Databricks notebook source
# MAGIC %md # Module II. Graph Basics
# COMMAND ----------
# MAGIC %run "./TutorialClasses"
# COMMAND ----------
from pyspark.sql.functions import countDistinct, col
import matplotlib.pyplot as plt
import pandas as pd
# COMMAND ----------
MagAccount = 'kdd2019magstore'
MagContainer = 'mag-2019-06-07'
MagSAS = '?sv=2018-03-28&ss=bfqt&srt=sco&sp=rl&se=2019-08-10T02:22:11Z&st=2019-07-31T18:22:11Z&spr=https&sig=m8XIxbDhk3ZOBt5ceVYIodw3k0JhbXodUBZDxXOThcs%3D'
# COMMAND ----------
mag = MicrosoftAcademicGraph(container=MagContainer, account=MagAccount, sas=MagSAS)
# COMMAND ----------
# DBTITLE 1,Task 1. Degree Centrality
# Construct author collaboration graph
# Input: the author list of a paper
# Output: all possible combination of author pairs
# Note that (a1, a2) is the same as (a2, a1)
# paperAuthor
# ======================
# | PaperId | AuthorId |
# ======================
# | p1 | a1 |
# | p1 | a2 |
# | p1 | a3 |
# ======================
paperAuthor = mag.getDataframe('PaperAuthorAffiliations').select('PaperId', 'AuthorId')
paperAuthor1 = paperAuthor.select('PaperId', 'AuthorId').withColumnRenamed('AuthorId', 'Author1')
paperAuthor2 = paperAuthor.select('PaperId', 'AuthorId').withColumnRenamed('AuthorId', 'Author2')
# collaborationGraph
# ===============================
# | PaperId | Author1 | Author2 |
# ===============================
# | p1 | a2 | a1 |
# | p1 | a3 | a1 |
# | p1 | a3 | a2 |
# ===============================
collaborationGraph = paperAuthor1.join(paperAuthor2, paperAuthor1.PaperId == paperAuthor2.PaperId, 'inner').where(paperAuthor1.Author1 > paperAuthor2.Author2).drop(paperAuthor1.PaperId).drop(paperAuthor2.PaperId)
display(collaborationGraph)
# COMMAND ----------
# Compute node degree
# Input: author collaboration graph
# Output: number of distinct coauthors for each author
# coauthorCount1
# ====================
# | Author1 | Count1 |
# ====================
# | a2 | 1 |
# | a3 | 2 |
# ====================
coauthorCount1 = collaborationGraph.groupBy('Author1').agg(countDistinct('Author2').alias('Count1'))
# coauthorCount2
# ====================
# | Author2 | Count2 |
# ====================
# | a1 | 2 |
# | a2 | 1 |
# ====================
coauthorCount2 = collaborationGraph.groupBy('Author2').agg(countDistinct('Author1').alias('Count2'))
# coauthorCount
# =======================================
# | Author1 | Count1 | Author2 | Count2 |
# =======================================
# | null | 0 | a1 | 2 |
# | a2 | 1 | a2 | 1 |
# | a3 | 2 | null | 0 |
# =======================================
coauthorCount = coauthorCount1.join(coauthorCount2, coauthorCount1.Author1 == coauthorCount2.Author2, 'outer')
coauthorCount = coauthorCount.fillna(0, 'Count1').fillna(0, 'Count2')
# coauthorCount
# ================================================
# | Author1 | Count1 | Author2 | Count2 | Degree |
# ================================================
# | null | 0 | a1 | 2 | 2 |
# | a2 | 1 | a2 | 1 | 2 |
# | a3 | 2 | null | 0 | 2 |
# ================================================
coauthorCount = coauthorCount.withColumn('Degree', col('Count1') + col('Count2'))
display(coauthorCount)
# COMMAND ----------
# Plot node degree distribution
# x-axis: node degree
# y-axis: number of nodes
nodeDegreeDistribution = coauthorCount.groupBy('Degree').count()
df = nodeDegreeDistribution.orderBy('Degree').toPandas()
plt.clf()
x = df['Degree']
y = df['count']
plt.plot(x, y, '.')
plt.xlabel('Node Degree')
plt.ylabel('Number of Node')
plt.xscale('log')
plt.yscale('log')
display(plt.show())
# COMMAND ----------
# DBTITLE 1,Task 2. Temporal Dynamics
# Compute number of papers for each year
# paper
# ==================
# | PaperId | Year |
# ==================
# | p1 | y1 |
# | p2 | y1 |
# | p3 | y2 |
# ==================
paper = mag.getDataframe('Papers')
# numPaperByYear
# ================
# | Year | count |
# ================
# | y1 | 2 |
# | y2 | 1 |
# ================
numPaperByYear = paper.groupBy('Year').count()
display(numPaperByYear)
# COMMAND ----------
# Compute number of AI-related papers for each year
# paperFos
# ============================
# | PaperId | FieldOfStudyId |
# ============================
# | p1 | f1 |
# | p1 | f2 |
# | p2 | f3 |
# | p3 | f1 |
# ============================
paperFos = mag.getDataframe('PaperFieldsOfStudy')
# ai
# ============================================
# | FieldOfStudyId | NormalizedName |
# ============================================
# | f1 | artificial intelligence |
# ============================================
ai = mag.getDataframe('FieldsOfStudy').filter(col('NormalizedName') == 'artificial intelligence')
# aiPaperId
# ============================
# | PaperId | FieldOfStudyId |
# ============================
# | p1 | f1 |
# | p3 | f1 |
# ============================
aiPaperId = paperFos.join(ai, paperFos.FieldOfStudyId == ai.FieldOfStudyId, 'inner').select('PaperId')
# aiPaper
# ==================
# | PaperId | Year |
# ==================
# | p1 | y1 |
# | p3 | y2 |
# ==================
aiPaper = paper.join(aiPaperId, paper.PaperId == aiPaperId.PaperId, 'inner')
# numAiPaperByYear
# ================
# | Year | count |
# ================
# | y1 | 1 |
# | y2 | 1 |
# ================
numAiPaperByYear = aiPaper.groupBy('Year').count()
display(numAiPaperByYear)
# COMMAND ----------
# Plot the distribution of all papers and AI-related papers over the past 50 years
paperDf = numPaperByYear.orderBy('Year').toPandas()
aiPaperDf = numAiPaperByYear.orderBy('Year').toPandas()
plt.clf()
x = paperDf['Year']
y = paperDf['count']
plt.plot(x,y)
x = aiPaperDf['Year']
y = aiPaperDf['count']
plt.plot(x,y)
plt.xlim(1970, 2018)
plt.xlabel('Year')
plt.ylabel('Number of Paper')
plt.yscale('log')
display(plt.show())