From 730a37b7b35800d837a779def37436068b208a82 Mon Sep 17 00:00:00 2001 From: Marcelo Ponce Date: Fri, 14 Aug 2020 13:10:28 -0400 Subject: [PATCH] adding tests --- NAMESPACE | 2 + R/covid19_checks.R | 4 +- tests/testthat.R | 4 + tests/testthat/.test_examples.R.swp | Bin 0 -> 16384 bytes tests/testthat/covid19_checks_tests.R | 12 ++ tests/testthat/test_examples.R | 200 ++++++++++++++++++++++++++ 6 files changed, 220 insertions(+), 2 deletions(-) create mode 100644 tests/testthat.R create mode 100644 tests/testthat/.test_examples.R.swp create mode 100644 tests/testthat/covid19_checks_tests.R create mode 100644 tests/testthat/test_examples.R diff --git a/NAMESPACE b/NAMESPACE index fdbf0a882..415728bbb 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -21,6 +21,7 @@ export(integrity.check) export(itrends) export(live.map) export(mtrends) +export(nullify.data) export(plt.SIR.model) export(preProcessingData) export(report.summary) @@ -70,6 +71,7 @@ importFrom(rentrez,entrez_search) importFrom(rentrez,entrez_summary) importFrom(rentrez,extract_from_esummary) importFrom(stats,Gamma) +importFrom(stats,complete.cases) importFrom(stats,glm) importFrom(stats,lm) importFrom(stats,na.omit) diff --git a/R/covid19_checks.R b/R/covid19_checks.R index 238065d9a..57a2d5ed3 100644 --- a/R/covid19_checks.R +++ b/R/covid19_checks.R @@ -64,7 +64,7 @@ integrity.check <- function(data, datasetName="", disclose=FALSE, recommend=TRUE if ( sum(condn, na.rm=TRUE) !=0 ) { warning("number of 'active+recovered+deaths' cases does NOT match the number of 'confirmed' cases!",'\n', - '\t',"on ", length(which(condn))," entries -- ", which(condn), '\n', + '\t',"on ", length(which(condn))," entries -- ", paste(which(condn),collapse=' '), '\n', '\t || ',paste(colnames(data),collapse=' '),'\n', " ", paste(which(condn),collapse=' '), ' || ', paste(data[which(condn),],collapse=' ')) disclose.entries <- c(disclose.entries, which(condn)) @@ -197,7 +197,7 @@ nullify.data <- function(data,stringent=FALSE) { orig.recs <- nrow(data) # aggregated data-type - susp.entries <- integrity.check(data, datasetName, disclose=TRUE, recommend=FALSE) + susp.entries <- integrity.check(data, disclose=TRUE, recommend=FALSE) susp.recs <- length(susp.entries) if (susp.recs > 0) { diff --git a/tests/testthat.R b/tests/testthat.R new file mode 100644 index 000000000..f3bd3c8a5 --- /dev/null +++ b/tests/testthat.R @@ -0,0 +1,4 @@ +library(testthat) +library(covid19.analytics) + +test_check("covid19.analytics") diff --git a/tests/testthat/.test_examples.R.swp b/tests/testthat/.test_examples.R.swp new file mode 100644 index 0000000000000000000000000000000000000000..3b59b2ff052dccba67d8dda0a60510143c582b21 GIT binary patch literal 16384 zcmeI2U2Ggz6~}KwOS274(*zzU0^N?OJeJmDyH1)o>LQtV)4K9U*xe**)6|S-@9s`B zJ2RQNW5;bndFUHO@PMlJr3EBFMS}7rUJw$WO`u3n9*PRBc;Jnaib@6XK!N|c^RctL zwqOvpZIv{`-sU?hi87H3LgH z9+hnSSGVjXA;a-S)B3pQog1IE1H= z9MNv&2lk)b{+^<=&_JPqLIZ^c3Jnw*C^S%LpwK{}f&VKFNPic56eHS|jOsJV?==Iz z?@7)p$?xL><;iLBQ)r;jK%s#`1BC_(4HOzEG*D=u&_JPqLIZ^c3JtsqHDERwyAE{s9$;OpR7@Dz9id<;~;2>8eQ82bgd41Nfn0T)07BnZJcD1#4yTfo2eF!ma_1fBs; zgD-%`fC(1Bwcr}Sz?B;qdjVVqm%#Jj)8J8X7TgaGgAaf=N*E)!0=@>m3cdn93myj@ za1_jdTflYTmEDZ}0Xz#XfMu`*4uQSkrRy<9a2fm@`~>_M{0Mv#d>&ZfC^!K2gFn3& zb-{PQx51aemw*M1fL-8kFy@=!4e&a+0{#Mi0KN!95P*Z=r`IyJ0v-UrM}p^N@JVnM z+z-xxec&dr7rX`^`7`)6coIAT$Zr;a`YVYGzOdwq7fC;odLVo+kVY7F@!`jY-VRK^ zW8Dt}Vs<5@PHZSop41VjrX}kD6S;xO)_(Y~@;$Yd8W?*|b`a~pFg+&#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0h53lfNx95#-K8N6S> zcHOp4I5HNJBvG9?Y1g5z5=&%Nl4QN(L8nR*x*2=3yohn?GEN)EV}ToFs>p_=Bw%by z;nVE)fXLH_56w*CkAx&yT9FGCBhMCRt&^QkyiFu|O{mAqUaVvpRP6NZbL1j%+Ib_% z2&cIZWX9uE^p@gfZx;sSn>u*ta4g@Jx2A6)hRiX2i0;JDw!AgV#t!s)WP0l0%=EUm zCNE8R&mBmGl%zB{oH@3*+7Cfx@L6|r#W6;{HN5E@$jYPT9cOeYASn?Rm*=VrG{GG7 zZ{S@-mMX~h&%HDv(gZoTGMaYiJLX2ugL8!(O>w+&Ab%N3j3(E!hT}AI><^g#ArBGU zY}3JjLeGiF*U+;tcyANUS2t2W%E_J+U|2dY$kkM{^}ilXuKzgEIzFCzuQ4q3R+j6v zqhe4H@8B^vvNS@HKw@w@ycRML{<4h!_EN`zMFoVMHdL77*!jk3s!e@~LYMN-RGLA% hsbI|Kmm1aD?6JoDsVYCQfVA;dO2Ls!st4y7`w#Ax0o?!q literal 0 HcmV?d00001 diff --git a/tests/testthat/covid19_checks_tests.R b/tests/testthat/covid19_checks_tests.R new file mode 100644 index 000000000..7318e56c9 --- /dev/null +++ b/tests/testthat/covid19_checks_tests.R @@ -0,0 +1,12 @@ +test.data.checks <- function() { +datasets <- c("TS-all","TS-confirmed","TS-confirmed-US","TS-Toronto","aggregated") + +for (i in datasets) { + print(i) + data <- covid19.data(i) + nullify.data(data) + nullify.data(data, stringent=TRUE) + print(paste(rep("#",80),collapse='')) +} + +} diff --git a/tests/testthat/test_examples.R b/tests/testthat/test_examples.R new file mode 100644 index 000000000..9691bf519 --- /dev/null +++ b/tests/testthat/test_examples.R @@ -0,0 +1,200 @@ +# TESTS SET for the covid19.analytics package +# M.Ponce + + +############################################# +############ DATA RETRIEVAL ############## +############################################# +test_that("Data Retrieval", { + + loc.data <- c(F,T) + + for (repo in loc.data) { + # WORLD DATA + # aggregated + agg.data <- covid19.data("aggregated", local.data=repo) + expect_s3_class(agg.data,"data.frame") + + # TS + ts.data <- covid19.data("ts-ALL", local.data=repo) + expect_s3_class(ts.data,"data.frame") + + # ALL + all.data <- covid19.data("ALL", local.data=repo) + expect_type(all.data,"list") + } + + # Toronto Data + # original data type + expect_type(covid19.Toronto.data("original"), "list") + + # TS Toronto data + tor.data <- covid19.Toronto.data() + expect_s3_class(tor.data,"data.frame") + + # US data + us.data <- covid19.US.data() + expect_s3_class(us.data,"data.frame") +}) + + +##################################################################### + +### Toronto Data + +test_that("Toronto Data", { + # TS Toronto data + tor.data <- covid19.Toronto.data() + expect_s3_class(tor.data,"data.frame") + + # TRENDS + # single trend + st.Tor <- unique(tor.data$status) + for (i in st.Tor) { + expect_warning(single.trend(tor.data[tor.data$status == i,], info=i)) + } + + # Multiple trends + expect_output(mtrends(tor.data)) +}) + + +##################################################################### + +### Integrity/Consistencies tests + +test_that("integrity/consistency", { + source("covid19_checks_tests.R") + expect_warning(test.data.checks()) +}) + +##################################################################### + + +### TOTS.PER.LOCATION() + +test_that("TOTS.PER.LOCATION fn", { + cases <- c("confirmed","deaths","recovered") + + for (i in seq_along(cases)) { + i.case <- cases[i] + header("",paste('processing ',i.case)) + + # read the time series data for confirmed cases + all.data <- covid19.data(paste0('ts-',i.case)) + expect_s3_class(all.data,"data.frame") + + # run on all the cases + #pdf(paste0("Japan_",i.case,".pdf")) + expect_invisible(tots.per.location(all.data,"Japan")) + #dev.off() + + } +}) + + +### GROWTH.RATE() +test_that("GROWTH.RATE fn", { + # read time series data for confirmed cases + TS.data <- covid19.data("ts-confirmed") + expect_s3_class(TS.data,"data.frame") + + locs <- c("Hubei","Italy","Germany","Canada") + for (i in seq_along(locs)) { + i.loc <- locs[i] + + print(i.loc) + + # compute changes and growth rates per location for 'Italy' + #pdf(paste0("gr-changes_",i.loc,".pdf")) + expect_output( growth.rate(TS.data,geo.loc=i.loc) ) + #dev.off() + + } +}) + + +test_that("GROWTH.RATE fn with more cases...", { + # obtain Time Series data + TSconfirmed <- covid19.data("ts-confirmed") + + # explore different combinations of regions/cities/countries + # when combining different locations heatmaps will also be generated comparing the trends among these locations + expect_output( growth.rate(TSconfirmed,geo.loc=c("Italy","Canada","Ontario","Quebec","Uruguay")) ) + + expect_output( growth.rate(TSconfirmed,geo.loc=c("Hubei","Italy","Spain","United States","Canada","Ontario","Quebec","Uruguay")) ) + + expect_output( growth.rate(TSconfirmed,geo.loc=c("Hubei","Italy","Spain","US","Canada","Ontario","Quebec","Uruguay")) ) +}) + + +########################################################################## + + +########### REPORTS ############## + +test_that("REPORTS ...", { + expect_output(report.summary(graphical.output=FALSE)) + + expect_output(report.summary(graphical.output=FALSE, saveReport=TRUE) ) +}) + +########################################################################### + +#### INTERACTIVE PLOTS +# retrieve time series data +TS.data <- covid19.data("ts-ALL") + +# static and interactive plot +totals.plt(TS.data, interactive.display=FALSE) + + +# retrieve aggregated data +data <- covid19.data("aggregated") + +# interactive map of aggregated cases -- with more spatial resolution +live.map(data, interactive.display=FALSE) + +# or +live.map(interactive.display=FALSE) + +# interactive map of the time series data of the confirmed cases with less spatial resolution, ie. aggregated by country +live.map(covid19.data("ts-confirmed"), interactive.display=FALSE) + + + +#### SIR MODEL +# read time series data for confirmed cases +data <- covid19.data("ts-confirmed") + +# run a SIR model for a given geographical location +generate.SIR.model(data,"Hubei", t0=1,t1=15) +generate.SIR.model(data,"Germany",tot.population=83149300) +generate.SIR.model(data,"Uruguay", tot.population=3500000) +generate.SIR.model(data,"Ontario",tot.population=14570000) + +# the function will agregate data for a geographical location, like a country with multiple entries +generate.SIR.model(data,"Canada",tot.population=37590000) + +# modelling the spread for the whole world, storing the model and generating an interactive visualization +world.SIR.model <- generate.SIR.model(data,"ALL", t0=1,t1=15, tot.population=7.8e9, staticPlt=FALSE) +# plotting and visualizing the model +plt.SIR.model(world.SIR.model,"World",interactiveFig=FALSE,fileName="world.SIR.model") + + + +#### +# clean up +# use a variable "covid19analytics.test.DONT.cleanup" to don't delete files + +#if(!("covid19analytics.test.DONT.cleanup" %in% ls())) { +if (FALSE) { + rm.file.tps <- c("txt","pdf") + for (tp in rm.file.tps) + for (fl in dir(pattern=tp)) { + message("clean-up: removing",fl) + file.remove(fl) + } +} + +####