-
Notifications
You must be signed in to change notification settings - Fork 22
Refactor rdiffnet #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 21 commits
7f3defd
18cd21f
34f2e73
f27f174
8977e4f
ef83eb2
8a8282c
bd5c92f
8f3314e
b7f689a
6ffdba5
d4720e0
bcabadb
587babb
9f9a25e
a54800b
1d34b5d
83d1d66
c5990c4
3ef1d72
39a3840
3b360b3
b183316
271047b
0615685
e871f33
3755959
875b22f
e9a34cd
345df65
e01535e
b6e63ca
516cf33
90f4af5
93dc056
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,8 @@ Authors@R: c( | |
), | ||
person("Thomas", "Valente", email="[email protected]", role=c("aut", "cph"), | ||
comment=c(ORCID="0000-0002-8824-5816", what="R original code")), | ||
person("Anibal", "Olivera Morales", role = c("aut", "ctb"), | ||
comment=c(ORCID="0009-0000-3736-7939", what="Multi-diffusion version")), | ||
person("Stephanie", "Dyal", email="[email protected]", role=c("ctb"), comment="Package's first version"), | ||
person("Timothy", "Hayes", email="[email protected]", role=c("ctb"), comment="Package's first version") | ||
) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -462,31 +462,52 @@ adjmat_to_edgelist.list <- function(graph, undirected, keep.isolates) { | |
#' @keywords manip | ||
#' @include graph_data.r | ||
#' @author George G. Vega Yon & Thomas W. Valente | ||
toa_mat <- function(obj, labels=NULL, t0=NULL, t1=NULL) { | ||
toa_mat <- function(obj, num_of_behaviors=1, labels=NULL, t0=NULL, t1=NULL) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The number of behaviors should be implicit (so you can figure that out from diffnet object). So don't ask the user of it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just deleted the 'num_of_behaviors' dependency in toa_mat and other functions. |
||
|
||
if (!inherits(obj, "diffnet")) { | ||
if (!length(t0)) t0 <- min(obj, na.rm = TRUE) | ||
if (!length(t1)) t1 <- max(obj, na.rm = TRUE) | ||
} | ||
|
||
cls <- class(obj) | ||
ans <- if ("numeric" %in% cls) { | ||
toa_mat.numeric(obj, labels, t0, t1) | ||
} else if ("integer" %in% cls) { | ||
toa_mat.integer(obj, labels, t0, t1) | ||
} else if ("diffnet" %in% cls) { | ||
with(obj, list(adopt=adopt,cumadopt=cumadopt)) | ||
} else | ||
stopifnot_graph(obj) | ||
|
||
|
||
if (inherits(obj, "diffnet")) { | ||
dimnames(ans$adopt) <- with(obj$meta, list(ids,pers)) | ||
dimnames(ans$cumadopt) <- with(obj$meta, list(ids,pers)) | ||
ans <- list() | ||
if (num_of_behaviors == 1) { | ||
cls <- class(obj) | ||
ans[[1]] <- if ("numeric" %in% cls) { | ||
toa_mat.numeric(obj, labels, t0, t1) | ||
} else if ("integer" %in% cls) { | ||
toa_mat.integer(obj, labels, t0, t1) | ||
} else if ("diffnet" %in% cls) { | ||
with(obj, list(adopt=adopt,cumadopt=cumadopt)) | ||
} else { | ||
stopifnot_graph(obj) | ||
} | ||
} else { | ||
for (q in 1:num_of_behaviors) { | ||
cls <- class(obj[,q]) | ||
ans[[q]] <- if ("numeric" %in% cls) { # Why included? | ||
toa_mat.numeric(obj[,q], labels, t0, t1) | ||
} else if ("integer" %in% cls) { | ||
toa_mat.integer(obj[,q], labels, t0, t1) | ||
} else if ("diffnet" %in% cls) { # Why included? | ||
with(obj[,q], list(adopt=adopt,cumadopt=cumadopt)) | ||
} else { | ||
stopifnot_graph(obj[,q]) | ||
} | ||
} | ||
} | ||
|
||
for (q in 1:num_of_behaviors) { | ||
if (inherits(obj, "diffnet")) { | ||
dimnames(ans[[q]]$adopt) <- with(obj$meta, list(ids,pers)) | ||
dimnames(ans[[q]]$cumadopt) <- with(obj$meta, list(ids,pers)) | ||
} | ||
} | ||
|
||
return(ans) | ||
if (num_of_behaviors==1) { | ||
return(ans[[1]]) | ||
} else { | ||
return(ans) | ||
} | ||
} | ||
|
||
toa_mat.default <- function(per, t0, t1) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -547,7 +547,8 @@ new_diffnet <- function(graph, toa, t0=min(toa, na.rm = TRUE), t1=max(toa, na.rm | |
self = getOption("diffnet.self"), | ||
multiple = getOption("diffnet.multiple"), | ||
name = "Diffusion Network", | ||
behavior = "Unspecified" | ||
behavior = "Unspecified", | ||
num_of_behaviors = 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The num of behaviors should be guessed from the data. |
||
) { | ||
|
||
# Step 0.0: Check if its diffnet! -------------------------------------------- | ||
|
@@ -563,42 +564,84 @@ new_diffnet <- function(graph, toa, t0=min(toa, na.rm = TRUE), t1=max(toa, na.rm | |
|
||
|
||
# Step 1.2: Checking that lengths fit | ||
if (length(toa)!=meta$n) stop("-graph- and -toa- have different lengths (", | ||
meta$n, " and ", length(toa), " respectively). ", | ||
"-toa- should be of length n (number of vertices).") | ||
if (num_of_behaviors == 1) { | ||
if (length(toa)!=meta$n){ stop("-graph- and -toa- have different lengths (", meta$n, " and ", length(toa), | ||
" respectively). ", "-toa- should be of length n (number of vertices).") } | ||
} else { | ||
if (length(toa[,1])!=meta$n) {stop("-graph- and -toa[,1]- have different lengths (", meta$n, " and ", length(toa[,1]), | ||
" respectively). ", "-toa- should be of length n (number of vertices).") } | ||
} | ||
|
||
# Step 2.1: Checking class of TOA and coercing if necesary ------------------- | ||
if (!inherits(toa, "integer")) { | ||
warning("Coercing -toa- into integer.") | ||
toa <- as.integer(toa) | ||
if (num_of_behaviors==1) { | ||
if (!inherits(toa, "integer")) { | ||
warning("Coercing -toa- into integer.") | ||
toa <- as.integer(toa) | ||
} | ||
} else { | ||
for (q in 1:num_of_behaviors) { | ||
if (!inherits(toa[,q], "integer")) { | ||
warning("Coercing -toa- into integer.") | ||
toa[,q] <- as.integer(toa[,q]) | ||
} | ||
} | ||
} | ||
|
||
# Step 2.2: Checking names of toa | ||
if (!length(names(toa))) | ||
names(toa) <- meta$ids | ||
if (num_of_behaviors==1) { | ||
if (!length(names(toa))) {names(toa) <- meta$ids} | ||
} else { | ||
if (!length(rownames(toa))) { # Not necessary? toa_mat(toa, labels = meta$ids, t0=t0, t1=t1) already has labels | ||
rownames(toa) <- meta$ids | ||
} | ||
} | ||
|
||
# Step 3.1: Creating Time of adoption matrix --------------------------------- | ||
mat <- toa_mat(toa, labels = meta$ids, t0=t0, t1=t1) | ||
mat <- toa_mat(toa, num_of_behaviors, labels = meta$ids, t0=t0, t1=t1) | ||
|
||
# Step 3.2: Verifying dimensions and fixing meta$pers | ||
|
||
if (meta$type != "static") { | ||
tdiff <- meta$nper - ncol(mat[[1]]) | ||
if (tdiff < 0) | ||
stop("Range of -toa- is bigger than the number of slices in -graph- (", | ||
ncol(mat[[1]]), " and ", length(graph) ," respectively). ", | ||
"There must be at least as many slices as range of toa.") | ||
else if (tdiff > 0) | ||
stop("Range of -toa- is smaller than the number of slices in -graph- (", | ||
ncol(mat[[1]]), " and ", length(graph) ," respectively). ", | ||
"Please provide lower and upper boundaries for the values in -toa- ", | ||
"using -t0- and -t- (see ?toa_mat).") | ||
if (num_of_behaviors==1) { | ||
if (meta$type != "static") { | ||
tdiff <- meta$nper - ncol(mat$adopt) | ||
if (tdiff < 0) | ||
stop("Range of -toa- is bigger than the number of slices in -graph- (", | ||
ncol(mat$adopt), " and ", length(graph) ," respectively). ", | ||
"There must be at least as many slices as range of toa.") | ||
else if (tdiff > 0) | ||
stop("Range of -toa- is smaller than the number of slices in -graph- (", | ||
ncol(mat$adopt), " and ", length(graph) ," respectively). ", | ||
"Please provide lower and upper boundaries for the values in -toa- ", | ||
"using -t0- and -t- (see ?toa_mat).") | ||
} else { | ||
graph <- lapply(1:ncol(mat$adopt), function(x) methods::as(graph, "dgCMatrix")) | ||
meta <- classify_graph(graph) | ||
} | ||
} else { | ||
graph <- lapply(1:ncol(mat[[1]]), function(x) methods::as(graph, "dgCMatrix")) | ||
meta <- classify_graph(graph) | ||
if (meta$type != "static") { | ||
tdiff <- meta$nper - ncol(mat[[1]]$adopt) | ||
if (tdiff < 0) | ||
stop("Range of -toa- is bigger than the number of slices in -graph- (", | ||
ncol(mat[[1]]$adopt), " and ", length(graph) ," respectively). ", | ||
"There must be at least as many slices as range of toa.") | ||
else if (tdiff > 0) | ||
stop("Range of -toa- is smaller than the number of slices in -graph- (", | ||
ncol(mat[[1]]$adopt), " and ", length(graph) ," respectively). ", | ||
"Please provide lower and upper boundaries for the values in -toa- ", | ||
"using -t0- and -t- (see ?toa_mat).") | ||
} else { | ||
|
||
# This should be reviewed !! (here the graph becomes 'dynamic') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It took me a while realize that, at Step 1.1 in new_diffnet, a graph will never be 'static'. This is because, if I give as an input a single 'slice' of graph ('static'), that will be converted to a list of graph before this step anyway, so it become a 'dynamic' graph whatever the circumstance. This implies that the type of graph is shown as 'dynamic':
Since the code never really uses those lines (neither single diff nor multi diff), it's "working." |
||
|
||
graph <- lapply(1:ncol(mat[[1]]$adopt), function(x) methods::as(graph, "dgCMatrix")) | ||
meta <- classify_graph(graph) | ||
} | ||
} | ||
|
||
meta$pers <- as.integer(colnames(mat$adopt)) | ||
# labels of the time periods | ||
if (num_of_behaviors==1) { | ||
meta$pers <- as.integer(colnames(mat$adopt)) | ||
} else {meta$pers <- as.integer(colnames(mat[[1]]$adopt))} # same for all behaviors | ||
|
||
# Step 4.0: Checking the attributes ------------------------------------------ | ||
|
||
|
@@ -631,18 +674,36 @@ new_diffnet <- function(graph, toa, t0=min(toa, na.rm = TRUE), t1=max(toa, na.rm | |
as.character(name))) | ||
meta$behavior <- ifelse(!length(behavior), "", ifelse(is.na(behavior), "", | ||
as.character(behavior))) | ||
meta$version <- utils::packageVersion("netdiffuseR") | ||
|
||
# Removing dimnames | ||
graph <- Map(function(x) Matrix::unname(x), x=graph) | ||
dimnames(toa) <- NULL | ||
dimnames(mat$adopt) <- NULL | ||
dimnames(mat$cumadopt) <- NULL | ||
|
||
if (num_of_behaviors==1) { | ||
dimnames(mat$adopt) <- NULL | ||
dimnames(mat$cumadopt) <- NULL | ||
|
||
adopt <- mat$adopt | ||
cumadopt <- mat$cumadopt | ||
} else { | ||
for (q in 1:num_of_behaviors) { | ||
dimnames(mat[[q]]$adopt) <- NULL | ||
dimnames(mat[[q]]$cumadopt) <- NULL | ||
} | ||
adopt <- list() | ||
cumadopt <- list() | ||
for (q in 1:num_of_behaviors) { | ||
adopt[[q]] <- mat[[q]]$adopt | ||
cumadopt[[q]] <- mat[[q]]$cumadopt | ||
} | ||
} | ||
|
||
return(structure(list( | ||
graph = graph, | ||
toa = toa, | ||
adopt = mat$adopt, | ||
cumadopt = mat$cumadopt, | ||
adopt = adopt, | ||
cumadopt = cumadopt, | ||
# Attributes | ||
vertex.static.attrs = vertex.static.attrs, | ||
vertex.dyn.attrs = vertex.dyn.attrs, | ||
|
Uh oh!
There was an error while loading. Please reload this page.