-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFly.Rmd
311 lines (242 loc) · 8.46 KB
/
Fly.Rmd
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
---
title: 'Using R in Teaching from *Network Science*'
author: "Amir Barghi, Department of Mathematics and Statistics, Saint Michael's College"
output:
html_document:
df_print: paged
word_document: default
pdf_document: default
---
# Fly (*Drosophila Medulla*) Connectome
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Loading Packages
```{r, message = FALSE}
library(tidyverse)
library(igraph)
library(igraphdata)
library(ggraph)
library(latex2exp)
```
# Loading the Data Set
## Data from [NeuroData's Graph DataBase](https://neurodata.io/project/connectomes/)
Data Source: Takemura, Sy., Bharioke, A., Lu, Z. et al. A visual motion detection circuit suggested by Drosophila connectomics. *Nature* **500**, 175–181 (2013). https://doi.org/10.1038/nature12450
```{r}
g <- read_graph('https://s3.amazonaws.com/connectome-graphs/fly/drosophila_medulla_1.graphml',
format = 'graphml')
```
```{r}
V(g)
E(g)
components(g)$no
components(g)$csize
glimpse(vertex_attr(g))
glimpse(edge_attr(g))
vertex_attr(g, name = 'name')[1:10]
edge_attr(g, name = 'proofreading.details')[1:10]
```
# Visualizing the Fly Connectome
```{r}
set.seed(42)
ggraph(g, layout = 'lgl') +
geom_edge_fan(edge_linetype = 3, color = 'dark blue', alpha = 0.25) +
geom_node_point(color = 'dark red', size = 1, alpha = 0.75) +
theme_graph(base_family = 'Helvetica') +
labs(title = 'Fly Connectome',
subtitle = 'Displayed Using Layout Generator for Larger Graphs')
```
```{r}
set.seed(42)
ggraph(g, layout = 'drl') +
geom_edge_fan(edge_linetype = 3, color = 'dark blue', alpha = 0.25) +
geom_node_point(color = 'dark red', size = 1, alpha = 0.75) +
theme_graph(base_family = 'Helvetica') +
labs(title = 'Fly Connectome',
subtitle = 'Displayed Using Distributed Recursive Layout')
```
```{r}
set.seed(42)
ggraph(g, layout = 'mds') +
geom_edge_fan(edge_linetype = 3, color = 'dark blue', alpha = 0.25) +
geom_node_point(color = 'dark red', size = 1, alpha = 0.75) +
theme_graph(base_family = 'Helvetica') +
labs(title = 'Fly Connectome',
subtitle = 'Displayed Using Multidimensional Scaling Layout')
```
# Summary Statistics of the Fly Connectome
```{r}
suppressMessages(df <- bind_cols(enframe(eccentricity(g, mode = 'out')),
enframe(betweenness(g)),
enframe(degree(g, mode = 'out')),
enframe(transitivity(g, type = c('local')))))
df <- df %>% select(name...1, value...2, value...4, value...6, value...8)
names(df) <- c('name', 'eccentricity', 'betweenness', 'outdegree', 'clustering')
head(df)
tail(df)
glimpse(df)
```
```{r}
df %>%
summarize(avg_deg = mean(outdegree),
delta = max(outdegree),
prop = sum(outdegree <= avg_deg) / n(),
diam = max(eccentricity),
radius = min(eccentricity),
avg_cc = mean(clustering, na.rm = TRUE),
avg_distance = mean_distance(g, directed = TRUE, unconnected = TRUE))
```
```{r}
(d <- mean_distance(g, directed = TRUE, unconnected = TRUE))
mean(distances(g))
```
## Fig. 2.18(a) on p. 66
```{r}
distance_table(g)
D <- data.frame(1:length(distance_table(g)$res),
distance_table(g)$res / sum(distance_table(g)$res))
names(D) <- c('x', 'y')
D %>%
ggplot(aes(x = x, y = y)) +
geom_point() +
geom_line(aes(x = d), color = 'blue') +
labs(title = 'Distribution of Distance (Proportions) in the Fly Connectome') +
labs(x = 'distance', y = 'density')
```
## The Outdegree Distribution
```{r}
df %>%
ggplot(aes(x = outdegree, y = ..density..)) +
geom_density(fill = 'red') +
labs(title = 'KDE of Outdegrees in the Fly Connectome')
```
```{r}
df %>%
ggplot(aes(x = outdegree, y = ..density..)) +
geom_histogram(binwidth = 1, fill = 'blue') +
labs(title = 'Histogram of Outdegrees in the Fly Connectome')
```
```{r}
df %>%
filter(outdegree <= 20) %>%
ggplot(aes(x = outdegree, y = ..density..)) +
geom_density(fill = 'red') +
labs(title = 'KDE of Outdegrees in the Fly Connectome',
subtitle = TeX('for Nodes with Outdegree $\\leq 20$'))
```
```{r}
df %>%
filter(outdegree <= 20) %>%
ggplot(aes(x = outdegree, y = ..density..)) +
geom_histogram(binwidth = 1, fill = 'blue') +
labs(title = 'Histogram of Outdegrees in the Fly Connectome',
subtitle = TeX('for Nodes with Outdegree $\\leq 20$'))
```
## Fig. 2.18(b) on p. 66
```{r, message = FALSE}
df %>%
group_by(outdegree) %>%
summarise(cc_deg = mean(clustering, na.rm = TRUE)) %>%
ungroup() %>%
filter(outdegree > 0) %>%
ggplot(aes(x = outdegree, y = cc_deg)) +
geom_point(na.rm = TRUE, color = 'blue') +
scale_x_log10() +
scale_y_log10() +
labs(title = 'Relation Between Local Clustering Coefficient and Outdegree',
subtitle = 'in the Fly Connectome') +
labs(x = TeX('$p_k$'), y = TeX('$C_k$'))
```
## Local Clustering Coefficient Distribution
```{r}
df %>%
ggplot(aes(x = clustering, y = ..density..)) +
geom_density(fill = 'red', na.rm = TRUE) +
labs(title = 'KDE of Local Clustering Coefficients in the Fly Connectome')
```
```{r}
df %>%
ggplot(aes(x = clustering, y = ..density..)) +
geom_histogram(binwidth = .1, fill = 'blue', na.rm = TRUE) +
labs(title = 'Histogram of Local Clustering Coefficients in the Fly Connectome')
```
```{r}
log(gorder(g)) / log(mean(df$outdegree))
mean_distance(g, directed = TRUE, unconnected = TRUE)
diameter(g)
```
```{r, message = FALSE}
C <- mean(df$clustering, na.rm = TRUE)
M <- mean(df$outdegree)
df %>%
group_by(outdegree) %>%
summarise(cc_deg = mean(clustering)) %>%
ungroup()
```
## Fig. 3.13(d) on p. 96
```{r, message = FALSE}
df %>%
group_by(outdegree) %>%
summarise(cc_deg = mean(clustering)) %>%
filter(outdegree > 0) %>%
ggplot(aes(x = outdegree, y = cc_deg)) +
geom_point(na.rm = TRUE, color = 'blue') +
geom_line(aes(y = C), color = 'blue') +
geom_line(aes(y = M / gorder(g)), color = 'red') +
scale_x_log10() +
scale_y_log10() +
labs(title = 'Relation Between Local Clustering Coefficient and Outdegree',
subtitle = 'The blue line is the average local clustering coefficient; \nthe red one is the one predicted by the random model.') +
labs(x = 'k', y = TeX('$C(k)$'))
```
## Visualizing Other Relations with Outdegree
```{r}
df %>%
filter(outdegree > 0) %>%
ggplot(aes(x = outdegree, y = betweenness)) +
geom_point(na.rm = TRUE, size = 0.5, color = 'red') +
scale_x_log10() +
labs(title = 'Relationship Between Betweenness Centrality and Outdegree') +
labs(x = TeX('$\\log_{10}$(outdegree)'))
```
```{r}
df %>%
filter(outdegree > 0) %>%
ggplot(aes(x = outdegree, y = betweenness + 0.00000001)) +
geom_point(na.rm = TRUE, size = 0.5, color = 'red') +
scale_y_log10() +
labs(title = TeX('Relationship Between $\\log_{10}$ of Betweenness Centrality and Outdegree')) +
labs(y = TeX('$\\log_{10}$(betweenness)'))
```
```{r}
df %>%
filter(betweenness > 0, outdegree > 0) %>%
ggplot(aes(x = outdegree, y = betweenness)) +
geom_point(na.rm = TRUE, size = 0.5, color = 'red') +
scale_y_log10() +
scale_x_log10() +
labs(title = TeX('Relationship Between $\\log_{10}$ of Betweenness Centrality and $\\log_{10}$ of Outdegree')) +
labs(y = TeX('$\\log_{10}$(betweenness)'),
x = TeX('$\\log_{10}$(outdegree)'))
```
```{r}
df %>%
filter(outdegree > 0) %>%
ggplot(aes(x = outdegree, y = eccentricity)) +
geom_point(na.rm = TRUE, size = 0.5, color = 'orange') +
scale_x_log10() +
labs(title = TeX('Relationship Between Eccentricity and $\\log_{10}$ of Outdegree')) +
labs(x = TeX('$\\log_{10}$(outdegree)'))
```
```{r}
df %>%
filter(outdegree > 0) %>%
ggplot(aes(x = outdegree, y = clustering)) +
geom_point(na.rm = TRUE, size = 0.5, color = 'blue') +
scale_x_log10() +
labs(title = TeX('Relationship Between Local Clustering Coefficient and $\\log_{10}$ of Outdegree')) +
labs(x = TeX('$\\log_{10}$(outdegree)'))
```
# References
- Albert-Laszlo Barabasi, *Network Science*, Cambridge University Press, 2016. *Network Science* is available online at http://networksciencebook.com/ under the following license: "This book's text and illustrations are licensed under a [Creative Commons Attribution-NonCommercial 3.0 Unported License](https://creativecommons.org/licenses/by-nc/3.0/)."
- Takemura, Sy., Bharioke, A., Lu, Z. et al. A visual motion detection circuit suggested by Drosophila connectomics. *Nature* **500**, 175–181 (2013). https://doi.org/10.1038/nature12450