-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathas_egor.R
More file actions
276 lines (237 loc) · 7.84 KB
/
as_egor.R
File metadata and controls
276 lines (237 loc) · 7.84 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
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
if (getRversion() >= "2.15.1")
utils::globalVariables(c("from", "to", "name", "ego_id", "vertex.names"))
#' @rdname egor
#' @param x an object to be coerced to [`egor`].
#' @export
as.egor <- function(x, ...)
UseMethod("as.egor")
#' @export
#' @noRd
#' @method as.egor egor
as.egor.egor <- function(x, ...)
x
#' @export
#' @describeIn egor Can convert (legacy) `nested_egor` object to `egor` object.
#' @method as.egor nested_egor
as.egor.nested_egor <- function(x,
ID.vars = list(
ego = ".egoID",
alter = ".alterID",
source = ".Source",
target = ".Target"
),
...) {
if (has_ego_design(x))
x <- x$variables
IDv <- modifyList(eval(formals()$ID.vars), ID.vars)
if (IDv$ego %in% names(x$.alts[[1]]))
alts <- bind_rows(x$.alts, .id = "egoID")
else {
alts <- select(x, IDv$ego, .alts)
alts <- tidyr::unnest(alts, .alts)
}
if (".aaties" %in% names(x)) {
if (IDv$ego %in% names(x$.aaties[[1]]))
aaties <- bind_rows(x$.aaties)
else {
aaties <- select(x, IDv$ego, .aaties)
aaties <- tidyr::unnest(aaties, .aaties)
}
egos <- select(x, -.alts, -.aaties)
egor(
alts,
egos,
aaties,
ID.vars = list(
ego = ".egoID",
alter = ".altID",
source = ".srcID",
target = ".tgtID"
)
)
} else {
egos <- select(x, -.alts)
egor(
alts,
egos,
ID.vars = list(
ego = ".egoID",
alter = ".altID",
source = ".srcID",
target = ".tgtID"
)
)
}
}
#' @rdname egor
#' @param x `list` of `igraph`/`network` objects representing ego networks.
#' @param ego_name `character` or `numeric` of length one or same length as there are networks. If the `igraph`/`network` objects don't include egos as a node, set to `NULL` (default).
#' @export
as.egor.list <-
function(x, ego_name = NULL, ...) {
if (length(ego_name) != length(x) & length(ego_name) > 1)
stop("Length of `ego_names` does not match up with number of ego networks.")
if (inherits(x[[1]], "igraph"))
as_egor_igraph(x, ego_name)
else if (inherits(x[[1]], "network"))
as_egor_network(x, ego_name)
}
as_egor_igraph <-
function(x, ego_name = NULL) {
# Check if all objects are igraph
if (!all(purrr::map_lgl(x, function(y) inherits(y, "igraph")))) {
stop(
"One or more elements are not of type `igraph`. Conversion only works if all elements in `x` are `igraph` objects."
)
}
graph_attrs <-
purrr::map_dfr(x, igraph::graph_attr,
.id = "ego_id") %>%
distinct(ego_id, .keep_all = TRUE)
names(graph_attrs) <- gsub("\\.", "", names(graph_attrs))
edges <-
purrr::map_dfr(x,
igraph::as_data_frame, .id = "ego_id")
alters <-
purrr::map_dfr(x,
igraph::as_data_frame,
what = "vertices",
.id = "ego_id")
extract_egos_and_return(graph_attrs = graph_attrs,
alters, edges, ego_name)
}
as_egor_network <-
function(x, ego_name = NULL) {
# Check if all objects are network
if (!all(purrr::map_chr(x, class) == "network")) {
stop(
"One or more elements are not of type `network`. Conversion only works if all elements in `x` are `network` objects."
)
}
# Network Attributes
extract_network_attributes <- function(network) {
network_attr_names <- network::list.network.attributes(network)
network_attr_vals <-
purrr::map(network_attr_names, network::get.network.attribute, x = network)
network_attr_df <- data.frame(network_attr_vals)
network_attr_df <-
setNames(network_attr_df, network_attr_names)
}
# Node Attributes
extract_node_attributes <- function(network) {
vertex_attr_names <- network::list.vertex.attributes(network)
vertex_attr_vals <-
purrr::map(vertex_attr_names, network::get.vertex.attribute, x = network)
vertex_attr_df <- data.frame(vertex_attr_vals)
vertex_attr_df <- setNames(vertex_attr_df, vertex_attr_names)
}
# Edge Attributes
extract_edge_attributes <- function(network) {
el <- network::as.edgelist(network)
el <-
el %>%
as.data.frame() %>%
mutate(from = as.character(factor(
V1,
levels = 1:attr(el, "n"),
labels = attr(el, "vnames")
)),
to = as.character(factor(
V2,
levels = 1:attr(el, "n"),
labels = attr(el, "vnames")
))) %>%
select(-V1, -V2)
edge_attr_names <- network::list.edge.attributes(network)
edge_attr_names <- edge_attr_names[edge_attr_names != "na"]
edge_attr_vals <-
purrr::map(edge_attr_names, network::get.edge.value, x = network$mel)
edge_attr_df <-
bind_cols(
el,
edge_attr_vals,
.name_repair = function(...)
tidy_names(..., quiet = TRUE)
)
setNames(edge_attr_df, c("from", "to", edge_attr_names))
}
net_attrs <-
purrr::map_dfr(x, extract_network_attributes, .id = "ego_id")
node_attrs <-
purrr::map_dfr(x, extract_node_attributes, .id = "ego_id") |>
rename(name = vertex.names)
edge_attrs <-
purrr::map_dfr(x, extract_edge_attributes, .id = "ego_id")
extract_egos_and_return(graph_attrs = net_attrs, alters = node_attrs,
edges = edge_attrs, ego_name)
}
#' This extracts egos from igraph/network data if they are named in `ego_name`
#' and returns an egor object
#' @param graph_attrs List of graph attributes
#' @param alters alters
#' @param edges edges
#' @param ego_name ego_name
extract_egos_and_return <-
function(graph_attrs, alters, edges, ego_name = NULL) {
names(graph_attrs) <- gsub("\\.", "", names(graph_attrs))
names(alters) <- gsub("\\.", "", names(alters))
names(edges) <- gsub("\\.", "", names(edges))
# alters <-
# alters %>%
# rename(name = 2)
if (is.null(ego_name)) {
egos <-
graph_attrs
} else if (length(ego_name) == 1) {
edges <-
edges %>%
filter(from != .env$ego_name,
to != .env$ego_name) %>%
mutate(across(c(from, to), as.character))
egos <-
alters %>%
filter(name == .env$ego_name) %>%
bind_cols(
select(graph_attrs, -ego_id),
.name_repair = function(...)
tidy_names(..., quiet = TRUE)
)
alters <-
alters %>%
filter(name != .env$ego_name)
} else {
edges <-
purrr::map2_dfr(split(edges, factor(edges$ego_id, unique(edges$ego_id))),
ego_name,
~ filter(.x,
from != .y,
to != .y)) %>%
mutate(across(c(from, to), as.character))
split_alters <-
split(alters, factor(alters$ego_id, unique(alters$ego_id)))
egos <-
purrr::map2_dfr(split_alters,
ego_name,
~ filter(.x, name == .y)) %>%
bind_cols(
select(graph_attrs, -ego_id),
.name_repair = function(...)
tidy_names(..., quiet = TRUE)
)
alters <-
purrr::map2_dfr(split_alters,
ego_name,
~ filter(.x, name != .y))
}
egor(
alters,
egos,
edges,
ID.vars = list(
ego = "ego_id",
alter = "name",
source = "from",
target = "to"
)
)
}