Skip to content
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

Refactor rdiffnet #46

Merged
merged 35 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift click to select a range
7f3defd
Adding myself to the project
aoliveram Oct 29, 2024
18cd21f
Some discussion about the dimensions of ans (exposure calculation)
aoliveram Oct 30, 2024
34f2e73
just fixing a paragraph in Ego exposure
aoliveram Oct 31, 2024
f27f174
looking as.vector things
aoliveram Oct 31, 2024
8977e4f
out object from exposure_for()
aoliveram Oct 31, 2024
ef83eb2
More dimensional analysis. Changes to avoid ambiguous names
aoliveram Nov 1, 2024
8a8282c
stats.R fixed
aoliveram Nov 1, 2024
bd5c92f
out object (in exposure_for() function) now allows q diff processes
aoliveram Nov 1, 2024
8f3314e
Working forms of .exposure, exposure_for, and exposure.list
aoliveram Nov 2, 2024
b7f689a
updates for .exposure and exposure.list functions
aoliveram Nov 4, 2024
6ffdba5
correcting labels of variables
aoliveram Nov 4, 2024
d4720e0
Fixing tests of diffnet
aoliveram Nov 4, 2024
bcabadb
changes to exposure.list() to allow arrays of cumadopt. Add multidiff…
aoliveram Nov 5, 2024
587babb
aditional test -multidiffusion exposure calculations-
aoliveram Nov 6, 2024
9f9a25e
updating to Steps 1.1 (initial adopters) and 1.2 (finding seed nodes…
aoliveram Nov 6, 2024
a54800b
updating cumadopt, exposure simulation, and toa for multi-diff processes
aoliveram Nov 6, 2024
1d34b5d
adding a set of tests for rdiffnet_validate_args function
aoliveram Nov 8, 2024
83d1d66
rdiffnet function updated to allow multi-diff. An small error in rdif…
aoliveram Nov 8, 2024
c5990c4
generalization of rdiffnet_make_threshold function. Some others modif…
aoliveram Nov 11, 2024
3ef1d72
lot of work in new_diffnet and toa_mat functions. New tests for rdiff…
aoliveram Nov 12, 2024
39a3840
changes in new_diffnet and toa_mat. Now all the original tests for th…
aoliveram Nov 12, 2024
3b360b3
updating rdiffnet_validate_args to allow objects seed.nodes different…
aoliveram Nov 13, 2024
b183316
Merge branch 'master' into refactor-rdiffnet
aoliveram Nov 13, 2024
271047b
rdiffnet now allow multiple diff, showing the results. There is still…
aoliveram Nov 13, 2024
0615685
rdiffnet now allow multiple diff, showing the results. There is still…
aoliveram Nov 13, 2024
e871f33
Now rdiffnet allow multiple diff, and shows the name -Behavior-, -Num…
aoliveram Nov 13, 2024
3755959
some minor changes in summary.diffnet
aoliveram Nov 14, 2024
875b22f
advances in summary.diffnet() for multi-diff, but this will be change…
aoliveram Nov 15, 2024
e9a34cd
changes in exposure.list and exposure_for to allow personalized attrs…
aoliveram Nov 18, 2024
345df65
minor changes in toa_mat
aoliveram Nov 18, 2024
e01535e
now new_diffnet sets the num_of behavior internally
aoliveram Nov 19, 2024
b6e63ca
more changes to toa_mat to compute num_of_adoption on more classes
aoliveram Nov 19, 2024
516cf33
now toa_mat can compute adopt and cumadopt from diffnet (multiple) an…
aoliveram Nov 19, 2024
90f4af5
all comments were addressed, except -behavior- as a vector.
aoliveram Nov 19, 2024
93dc056
checking the status of "dynamic" and "static" graphs.
aoliveram Nov 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 45 additions & 15 deletions R/adjmat.r
gvegayon marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -464,29 464,59 @@ adjmat_to_edgelist.list <- function(graph, undirected, keep.isolates) {
#' @author George G. Vega Yon & Thomas W. Valente
toa_mat <- function(obj, labels=NULL, t0=NULL, t1=NULL) {

if (inherits(obj, "matrix")) {
num_of_behaviors <- dim(obj)[2]
} else if (inherits(obj, "diffnet")){
if (inherits(obj$toa, "matrix")) {
num_of_behaviors <- dim(obj$toa)[2]}
else {num_of_behaviors <- 1}
} else {num_of_behaviors <- 1}

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) {
ans[[q]] <- if ("matrix" %in% class(obj)) {
if ("integer" %in% class(obj[,q])){
toa_mat.integer(obj[,q], labels, t0, t1)
} else if ("numeric" %in% class(obj[,q])) { # Why included?
toa_mat.numeric(obj[,q], labels, t0, t1)
}
} else if ("diffnet" %in% class(obj)) { # Why included?
with(obj, list(adopt=adopt[[q]],cumadopt=cumadopt[[q]]))
} 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) {
Expand Down
126 changes: 95 additions & 31 deletions R/diffnet-class.r
Original file line number Diff line number Diff line change
Expand Up @@ -556,49 556,94 @@ new_diffnet <- function(graph, toa, t0=min(toa, na.rm = TRUE), t1=max(toa, na.rm
return(graph)
}

# Step 0.1: Setting num_of_behavior ------------------------------------------

if (inherits(toa, "matrix")) {
num_of_behaviors <- dim(toa)[2]
} else {num_of_behaviors <- 1}

# Step 1.1: Check graph ------------------------------------------------------
meta <- classify_graph(graph)
if (meta$type=="static")
warning("-graph- is static and will be recycled (see ?new_diffnet).")


# 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).")

# 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 (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 necessary -------------------
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)

# 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 {
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 ------------------------------------------

Expand Down Expand Up @@ -629,21 674,40 @@ new_diffnet <- function(graph, toa, t0=min(toa, na.rm = TRUE), t1=max(toa, na.rm
meta$multiple <- multiple
meta$name <- ifelse(!length(name), "", ifelse(is.na(name), "",
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
#dimnames(toa) <- NULL

if (num_of_behaviors==1) {
meta$behavior <- ifelse(!length(behavior), "", ifelse(is.na(behavior), "",
aoliveram marked this conversation as resolved.
Show resolved Hide resolved
as.character(behavior)))
dimnames(mat$adopt) <- NULL
dimnames(mat$cumadopt) <- NULL

adopt <- mat$adopt
cumadopt <- mat$cumadopt
} else {
meta$behavior <- paste(unlist(behavior), collapse = ", ")

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,
Expand Down
Loading
Loading