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 1 commit
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
Prev Previous commit
Next Next commit
generalization of rdiffnet_make_threshold function. Some others modif…
…ication following the merge of the 41... branch
  • Loading branch information
aoliveram committed Nov 11, 2024
commit c5990c49db0c65fae2967b0d8c55f58ae7316775
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 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")),
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")
)
Expand Down
2 changes: 1 addition & 1 deletion R/diffnet-class.r
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 631,7 @@ 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 <- 5
meta$version <- utils::packageVersion("netdiffuseR")

# Removing dimnames
graph <- Map(function(x) Matrix::unname(x), x=graph)
Expand Down
48 changes: 18 additions & 30 deletions R/rdiffnet.r
Original file line number Diff line number Diff line change
Expand Up @@ -111,42 111,30 @@
#' @name rdiffnet
NULL

rdiffnet_make_threshold <- function(x, n) {
rdiffnet_make_threshold <- function(x, n, q) {

# Using sapply to compute the threshold
if (inherits(x, "function")) {

thr <- sapply(1:n, x)
thr <- matrix(sapply(1:(n*q), function(i) x()), nrow = n, ncol = q)

} else if ((length(x)==1) && is.numeric(x)) {
} else if (is.numeric(x) && length(x) == 1) {

thr <- rep(x, n)
thr <- matrix(rep(x, n * q), nrow = n, ncol = q)

} else {
# Setting depending on class
if (any(class(x) %in% c("data.frame", "matrix"))) {

thr <- as.vector(as.matrix(x))

# Must match the length of n
if (length(thr) != n)
stop("Incorrect length for -threshold.dist- (",length(x),")",
". It should be a vector of length ",n,".")

if (any(class(x) %in% c("data.frame", "matrix"))) {
thr <- as.matrix(x)
if (!all(dim(thr) == c(n, q))) stop("Incorrect dimensions for threshold.dist.",
"It should be a matrix of size ", n, "x", q, ".")
} else if (is.vector(x)) {

thr <- x

# Must match the length of n
if (length(thr) != n)
stop("Incorrect length for -threshold.dist- (",length(x),")",
". It should be a vector of length ",n,".")

} else {

stop("-threshold.dist- must be either a numeric vector of length -n-, a numeric scalar, or a function.")

}
if (length(x) == n * q && q>1) {
stop("Incorrect input: A vector of length ", n*q, " is not allowed.",
"Please provide a vector of length ", n, ".")
} else if (length(x) == n) {
thr <- matrix(rep(x, q), nrow = n, ncol = q)
} else stop("Incorrect length for threshold.dist.")
} else stop("threshold.dist must be a numeric vector or matrix of appropriate size or a function.")
}

thr
Expand Down Expand Up @@ -316,7 304,7 @@ rdiffnet <- function(
seed.p.adopt = 0.05,
seed.graph = "scale-free",
rgraph.args = list(),
rewire = TRUE, #set TRUE originally
rewire = TRUE,
rewire.args = list(),
threshold.dist = runif(n),
exposure.args = list(),
Expand Down Expand Up @@ -440,10 428,10 @@ rdiffnet <- function(
toa <- matrix(NA, nrow = dim(cumadopt)[1], ncol = dim(cumadopt)[3])

# Step 2.0: Thresholds -------------------------------------------------------
thr <- rdiffnet_make_threshold(threshold.dist, n) # REMINDER TO CHANGE rdiffnet_make_threshold
thr <- rdiffnet_make_threshold(threshold.dist, n, num_of_behaviors) # REMINDER TO CHANGE rdiffnet_make_threshold

# ONLY MEANWHILE
thr <- array(c(thr,rev(thr)), dim=c(length(thr), dim(cumadopt)[3]))
#thr <- array(c(thr,rev(thr)), dim=c(length(thr), dim(cumadopt)[3]))

# Step 3.0: Running the simulation -------------------------------------------

Expand Down
25 changes: 0 additions & 25 deletions R/stats.R
Original file line number Diff line number Diff line change
Expand Up @@ -476,15 476,6 @@ NULL
# Checking self
if (!self) graph <- sp_diag(graph, rep(0, nnodes(graph)))

#ans <- ( graph %*% (attrs * cumadopt) )
#
#if (normalized) {
# as.vector(ans/( graph %*% attrs 1e-20 ))
#} else {
# as.vector(ans)
#}
#

norm <- graph %*% attrs 1e-20

if (!is.na(dim(cumadopt)[3])) {
Expand All @@ -505,13 496,9 @@ NULL
}
}

#as.vector(ans)
return(as.vector(ans))
}

# library(microbenchmark)
# microbenchmark(.exposure, netdiffuseR:::exposure_cpp)

check_lags <- function(npers, lags) {

# Checking length
Expand Down Expand Up @@ -673,18 660,6 @@ exposure_for <- function(
lags
) {

#out <- matrix(nrow = nrow(cumadopt), ncol = ncol(cumadopt))

#if (lags >= 0L) {
# for (i in 1:(nslices(graph) - lags))
# out[,i lags]<- .exposure(graph[[i]], cumadopt[,i,drop=FALSE], attrs[,i,drop=FALSE],
# outgoing, valued, normalized, self)
#} else {
# for (i in (1-lags):nslices(graph))
# out[,i lags]<- .exposure(graph[[i]], cumadopt[,i,drop=FALSE], attrs[,i,drop=FALSE],
# outgoing, valued, normalized, self)
#}

if (!is.na(dim(cumadopt)[3])) {
out <- array(NA, dim = c(dim(cumadopt)[1], dim(cumadopt)[2], dim(cumadopt)[3]))

Expand Down
73 changes: 72 additions & 1 deletion tests/testthat/test-rdiffnet-parameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 3,9 @@
# Must work
test_that(
"Checking single diffusion rdiffnet args", {

# Must work

seed.p.adopt <- c(0.14)
seed.nodes <- c('random')
behavior <- c("random behavior")
Expand Down Expand Up @@ -49,11 52,38 @@ test_that(
)
})

test_that("Checking threshold for single diffusion", {

# Must work

thr <- rdiffnet_make_threshold(1.5, n = 50, q = 1)
expect_equal(dim(thr), c(50, 1))

x <- runif(50)
thr <- rdiffnet_make_threshold(x, n = 50, q = 1)
expect_equal(dim(thr), c(50, 1))

thr <- rdiffnet_make_threshold(function() 0.5, n = 50, q = 1)
expect_equal(dim(thr), c(50, 1))

thr <- rdiffnet_make_threshold(function() rexp(1), n = 50, q = 1)
expect_equal(dim(thr), c(50, 1))

# Must show ERROR

x <- runif(100) # Length n*q
expect_error(
rdiffnet_make_threshold(x, n = 50, q = 1)
)

})

# Multiple --------------------------------------------------------------------

# Must work
test_that("Multi diff models rdiff args work", {

# Must work

seed.p.adopt <- list(0.14,0.05)
seed.nodes <- list('random', "central")
behavior <- list("random behavior_1", "random behavior_2")
Expand Down Expand Up @@ -110,3 140,44 @@ test_that("Multi diff models rdiff args work", {
)
})


# NOT working now !!!

# test_that("Checking threshold for multiple diffusion", {
#
# # Must work
#
# x <- matrix(runif(100), nrow = 50, ncol = 2)
# thr <- rdiffnet_make_threshold(x, n = 50, q = 2)
# expect_equal(dim(thr), c(50, 2))
#
# x <- runif(100) # Length n*q
# expect_error(
# rdiffnet_make_threshold(x, n = 50, q = 2)
# )
#
# seed.p.adopt <- list(function() runif(1), function() rexp(1))
# thr <- rdiffnet_make_threshold(seed.p.adopt, n = 50, q = 2)
# expect_equal(dim(thr), c(50,1))
#
#
# seed.p.adopt <- list(0.14,0.05)
# thr <- rdiffnet_make_threshold(seed.p.adopt[[1]], n = 50, q = 2)
# expect_equal(dim(thr), c(50,2))
#
#
# seed.p.adopt <- list(runif(50), runif(50))
#
# # Test first element of list
# thr <- rdiffnet_make_threshold(seed.p.adopt[[1]], n = 50, q =1 )
#
# expect_equal(dim(thr), c(50,1))
#
#
# # Must show ERROR
#
# x <- runif(100) # Length n*q
# expect_error(
# rdiffnet_make_threshold(x, n=100,q=3),
# "incorrect input
# }
Loading