Skip to content

Commit

Permalink
test(consistent): add Rmd and test that checks if data produced by mo…
Browse files Browse the repository at this point in the history
…del is consistent to previous runs (also renamed test files)
  • Loading branch information
amyheather committed Jan 15, 2025
1 parent f9a9374 commit 59f82a7
Show file tree
Hide file tree
Showing 6 changed files with 689 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
.RData
.Ruserdata
inst/doc
inst/*html
72 changes: 72 additions & 0 deletions inst/generate_exp_results.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
title: "Generate expected results"
output: html_document
---

This notebook is used to run a specific version of the model and save each results dataframe as a csv. These are used in `test-backtest.R` to verify that the model produces consistent results.

The `.Rmd` file is provided as it is possible that results may change due to alterations to the model structure and operations. Once it has been confirmed that changes are intentional and not any introduced errors, this script can be run to regenerate the `.csv` files used in the test.

The run time is provided at the end of this notebook.

## Set-up

Load required packages.

```{r setup}
library(simmer)
library(simulation)
```

Start timer.

```{r start_timer}
start_time <- Sys.time()
```

Define path to expected results.

```{r path}
tests <- "../tests/testthat/testdata/"
```

## Run model and save results

```{r}
# Define model parameters
param <- defaults()
param["patient_inter"] <- 4.0
param["mean_n_consult_time"] <- 10.0
param["number_of_nurses"] <- 5.0
param["data_collection_period"] <- 80.0
param["number_of_runs"] <- 10.0
param["cores"] <- 1.0
# Run the trial
envs <- trial(param = param)
```

```{r}
# Monitored data about arrivals
result_arrivals <- do.call(rbind, lapply(envs, get_mon_arrivals))
write.csv(result_arrivals, file.path(tests, "arrivals.csv"), row.names = FALSE)
```

```{r}
# Monitored data about resources
result_resources <- do.call(rbind, lapply(envs, get_mon_resources))
write.csv(result_resources, file.path(tests, "resources.csv"), row.names = FALSE)
```

## Calculate run time

```{r end_timer}
# Get run time in seconds
end_time <- Sys.time()
runtime <- as.numeric(end_time - start_time, units="secs")
# Display converted to minutes and seconds
minutes <- as.integer(runtime / 60)
seconds <- as.integer(runtime %% 60)
print(sprintf("Notebook run time: %dm %ds", minutes, seconds))
```
32 changes: 32 additions & 0 deletions tests/testthat/test-backtest.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Back testing for the Discrete-Event Simulation (DES) Model.
#
# These check that the model code produces results consistent with prior code.

test_that("results from a new run match those previously generated", {
# Choose a specific set of parameters (ensuring test remains on the same
# set, regardless of any changes to defaults())
param <- defaults()
param["patient_inter"] <- 4.0
param["mean_n_consult_time"] <- 10.0
param["number_of_nurses"] <- 5.0
param["data_collection_period"] <- 80.0
param["number_of_runs"] <- 10.0
param["cores"] <- 1.0

# Run the trial then get the monitored arrivals and resources
envs <- trial(param = param)
arrivals <- do.call(rbind, lapply(envs, simmer::get_mon_arrivals))
resources <- do.call(rbind, lapply(envs, simmer::get_mon_resources))

# Import the expected results
exp_arrivals <- read.csv(test_path("testdata", "arrivals.csv"))
exp_resources <- read.csv(test_path("testdata", "resources.csv"))

# Remove the na.action attribute before comparing
attr(arrivals, "na.action") <- NULL
attr(resources, "na.action") <- NULL

# Compare results
expect_equal(arrivals, exp_arrivals)
expect_equal(resources, exp_resources)
})
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Unit testing for the Discrete-Event Simulation (DES) Model.
#
# These check specific parts of the simulation and code, ensuring they work
# correctly and as expected.

test_that("the same seed returns the same result", {

# Run model twice using same run number (which will set the seed)
Expand Down
184 changes: 184 additions & 0 deletions tests/testthat/testdata/arrivals.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
"name","start_time","end_time","activity_time","finished","replication"
"patient0",3.02072733251338,4.47779459955131,1.45706726703793,TRUE,1
"patient1",7.74729844894181,12.1079847067336,4.36068625779175,TRUE,1
"patient2",8.3064794964158,20.6021000305647,12.2956205341489,TRUE,1
"patient3",19.8863536462721,29.4520285832566,9.56567493698454,TRUE,1
"patient5",22.6332689682764,35.0093044756247,12.3760355073483,TRUE,1
"patient4",22.0450850062605,35.9524362943648,13.9073512881043,TRUE,1
"patient6",25.6813883901566,36.2268200632633,10.5454316731067,TRUE,1
"patient8",47.518101045011,50.8874358088582,3.36933476384729,TRUE,1
"patient10",52.491006479679,55.4322103560775,2.9412038763985,TRUE,1
"patient11",55.0585768326059,56.1193030654326,1.06072623282671,TRUE,1
"patient7",43.377125260888,62.1374769849705,18.7603517240825,TRUE,1
"patient12",57.3220389309212,63.1091635647103,5.78712463378906,TRUE,1
"patient13",57.5597955725029,69.2929166307106,11.7331210582077,TRUE,1
"patient9",50.1370875938669,73.7822401241033,23.6451525302365,TRUE,1
"patient16",77.5318529076741,79.5669564079897,2.03510350031566,TRUE,1
"patient0",7.46140976216564,8.9279364586241,1.46652669645846,TRUE,1
"patient1",9.08040204983125,9.97566385541048,0.895261805579234,TRUE,1
"patient2",16.0032409460082,26.746909550847,10.7436686048389,TRUE,1
"patient3",18.6708314710094,31.8135905254886,13.1427590544791,TRUE,1
"patient5",25.3434698140881,32.0812533513344,6.73778353724629,TRUE,1
"patient4",24.7173486874661,32.1685382614392,7.4511895739731,TRUE,1
"patient6",30.3168512177537,41.1392129809003,10.8223617631466,TRUE,1
"patient7",36.6780747660683,51.1307786397864,14.452703873718,TRUE,1
"patient8",39.8644636570942,56.8987866295948,17.0343229725006,TRUE,1
"patient9",57.8322329957666,61.3969625361122,3.56472954034553,TRUE,1
"patient13",64.7260881673908,64.7654857851928,0.0393976178020239,TRUE,1
"patient10",60.3137701515736,68.6400649272058,8.32629477563214,TRUE,1
"patient12",64.4070073579135,69.9165835402692,5.50957618235568,TRUE,1
"patient16",72.3308438180934,74.1351061741573,1.80426235606397,TRUE,1
"patient15",71.5672695609355,77.284847816507,5.71757825557142,TRUE,1
"patient11",63.0692021761718,78.9697259496574,15.9005237734857,TRUE,1
"patient1",9.38263747993777,11.424650975167,2.04201349522918,TRUE,1
"patient3",14.2341277091379,14.9148347782518,0.680707069113851,TRUE,1
"patient4",14.2743190223898,15.0468256869077,0.772506664517933,TRUE,1
"patient5",14.7323145079042,16.3140894140083,1.58177490610407,TRUE,1
"patient0",6.9225062873579,19.2516721479484,12.3291658605905,TRUE,1
"patient6",16.3618213790084,22.1847695684607,5.82294818945229,TRUE,1
"patient2",13.3989752768154,36.2354086939157,22.8364334171002,TRUE,1
"patient7",33.2562633943028,37.1408303890266,3.8845669947238,TRUE,1
"patient8",34.0541159197903,45.9168268013485,11.8627108815582,TRUE,1
"patient9",38.8934549002374,47.6161157869297,8.7226608866923,TRUE,1
"patient12",49.7239363474973,51.1388867117666,1.41495036426932,TRUE,1
"patient11",46.4478469476123,52.173471029291,5.72562408167869,TRUE,1
"patient13",56.8057329709277,64.4420913824902,7.63635841156256,TRUE,1
"patient10",45.8879329169931,66.3614389197712,20.4735060027781,TRUE,1
"patient0",0.686402440071106,9.36745872492802,8.68105628485692,TRUE,1
"patient1",17.902182175701,24.1736664743325,6.27148429863155,TRUE,1
"patient2",21.1127702246765,25.600888077856,4.48811785317957,TRUE,1
"patient3",24.0522032888202,29.1457038284806,5.09350053966045,TRUE,1
"patient7",30.4719773673426,31.0675280767467,0.595550709404051,TRUE,1
"patient5",27.6036014749463,31.6514976909351,4.04789621598881,TRUE,1
"patient6",28.7559090846192,32.1519368266025,3.39602774198334,TRUE,1
"patient9",35.2057474270969,37.6468771674245,2.4411297403276,TRUE,1
"patient12",44.9125169171398,48.8274807403451,3.91496382320528,TRUE,1
"patient4",24.2550028198011,51.0579816880684,26.8029788682673,TRUE,1
"patient8",34.6688497268169,53.0685942457588,18.3997445189419,TRUE,1
"patient13",45.1462220392097,53.7460577501571,4.918577009812,TRUE,1
"patient11",44.241076077431,62.5759479423998,18.3348718649688,TRUE,1
"patient14",50.136570732025,64.837452235682,13.7794705476136,TRUE,1
"patient18",61.7594847173904,67.214359587182,2.37690735150004,TRUE,1
"patient15",52.3798078043698,70.8344625697051,17.7658683239462,TRUE,1
"patient19",62.7879657817547,73.7118166379004,6.49745705071837,TRUE,1
"patient21",65.3501358979855,77.6768008988655,3.96498426096513,TRUE,1
"patient22",69.6538295872234,79.0236104923519,1.3468095934864,TRUE,1
"patient0",7.95203992690811,8.67741933813703,0.72537941122892,TRUE,1
"patient1",9.4337886922426,9.99298837747345,0.559199685230851,TRUE,1
"patient2",11.0422483660645,11.8078503509285,0.765601984863992,TRUE,1
"patient4",17.0063814702007,20.8528115071511,3.8464300369504,TRUE,1
"patient6",22.5653418049726,22.9768111174908,0.411469312518238,TRUE,1
"patient3",13.5057299729801,33.5186904806057,20.0129605076256,TRUE,1
"patient5",21.9793821454725,36.8126188063351,14.8332366608626,TRUE,1
"patient11",38.0948152871056,40.750883842185,2.6560685550794,TRUE,1
"patient8",28.9063751147997,41.7110627987815,12.8046876839818,TRUE,1
"patient12",40.4436899994685,42.0763373433797,1.32545350119472,TRUE,1
"patient9",34.0418520763636,51.6025690657277,17.5607169893641,TRUE,1
"patient14",47.9564288058772,55.1872486324028,7.23081982652557,TRUE,1
"patient13",45.3695097054722,57.5474879282418,12.1779782227696,TRUE,1
"patient10",36.5704606313378,58.32643803345,21.7559774021123,TRUE,1
"patient7",25.731399436643,60.4891424223844,34.7577429857414,TRUE,1
"patient17",59.6553163211862,60.7459384573262,1.09062213614,TRUE,1
"patient15",50.8180426604751,66.3775850746951,14.7750160089674,TRUE,1
"patient18",62.3908515073146,66.7376574160318,4.34680590871722,TRUE,1
"patient19",64.6835889836201,71.3928171276473,6.70922814402729,TRUE,1
"patient20",71.2322742806539,71.5027330273402,0.270458746686288,TRUE,1
"patient16",58.6635843012551,76.4261505651337,17.7625662638786,TRUE,1
"patient2",2.99613172279779,3.65623367150729,0.660101948709499,TRUE,1
"patient0",0.850146381184459,6.99981416203082,6.14966778084636,TRUE,1
"patient4",6.30416743548695,9.86116604505215,3.5569986095652,TRUE,1
"patient5",6.44733324365799,10.457854142395,4.01052089873701,TRUE,1
"patient3",4.14471384415512,11.3119266857673,7.16721284161222,TRUE,1
"patient7",13.0345119700794,14.9365185156328,1.90200654555334,TRUE,1
"patient9",17.6808955962979,18.0913984719229,0.410502875624983,TRUE,1
"patient1",1.58308594253107,22.6937886012056,21.1107026586745,TRUE,1
"patient12",22.2879195585154,25.7650134770527,3.07122487584716,TRUE,1
"patient6",12.7038009060736,28.7556868277867,16.0518859217132,TRUE,1
"patient14",25.2132617780266,31.1115921520074,2.35590532422066,TRUE,1
"patient8",17.5067076510959,31.463182237577,13.9564745864811,TRUE,1
"patient15",28.5878726303087,31.6834466179581,0.57185446595067,TRUE,1
"patient16",30.0982351958262,32.3363586465086,0.873176408931613,TRUE,1
"patient18",40.7041623532959,41.0780486635431,0.37388631024714,TRUE,1
"patient13",22.6235515014527,41.3675067033521,15.6024932262994,TRUE,1
"patient10",18.0604510944728,41.6585202879168,23.598069193444,TRUE,1
"patient11",19.9958070439595,43.7181378414787,23.7223307975192,TRUE,1
"patient17",38.8176503140667,52.5274113762836,13.7097610622169,TRUE,1
"patient20",48.4803230363427,54.1697652266844,5.68944219034165,TRUE,1
"patient23",58.6484274741178,58.7619923761031,0.113564901985228,TRUE,1
"patient21",52.5768993803965,62.5049212917854,9.9280219113889,TRUE,1
"patient24",62.621643011711,64.1132116869757,1.49156867526472,TRUE,1
"patient19",40.9180555991015,67.5053831906992,26.4273345271561,TRUE,1
"patient22",56.7358848236417,71.4334972942644,14.6976124706227,TRUE,1
"patient25",63.9520833895275,75.5818460348066,11.6297626452791,TRUE,1
"patient26",69.9458105218695,77.8904972415971,7.94468671972764,TRUE,1
"patient27",77.8126628348135,78.6862808572369,0.873618022423333,TRUE,1
"patient28",78.3116583046966,78.7492615726011,0.437603267904522,TRUE,1
"patient1",6.68141488877403,12.1376538039865,5.45623891521245,TRUE,1
"patient0",0.193384399983336,17.3247667750987,17.1313823751154,TRUE,1
"patient2",9.93019039181141,17.448762801028,7.51857240921659,TRUE,1
"patient3",20.411255225008,22.4575867574403,2.04633153243239,TRUE,1
"patient4",37.503542719732,46.9457381172718,9.4421953975398,TRUE,1
"patient6",40.677658963812,52.7810006322362,12.1033416684241,TRUE,1
"patient8",49.5283797709883,54.7224044008792,5.19402462989092,TRUE,1
"patient7",48.1474195006235,54.9166969400762,6.76927743945271,TRUE,1
"patient5",37.6855513565558,56.4056358776351,18.7200845210793,TRUE,1
"patient10",57.0184671381821,60.9906779436208,3.97221080543869,TRUE,1
"patient11",59.5487797731867,65.0299522566011,5.48117248341441,TRUE,1
"patient12",61.3094157452097,65.7692025747483,4.45978682953864,TRUE,1
"patient9",53.3599395173857,77.3654097733779,24.0054702559922,TRUE,1
"patient17",78.0200163758491,78.9011001418475,0.881083765998483,TRUE,1
"patient13",62.3315021494736,79.2177079894893,16.8862058400157,TRUE,1
"patient14",67.293391334317,79.8748421054373,12.5814507711203,TRUE,1
"patient1",4.56376784937997,13.1301751308653,8.56640728148532,TRUE,1
"patient0",3.34879730726202,13.1406362712893,9.79183896402729,TRUE,1
"patient2",6.31518788161297,13.8656471243547,7.55045924274172,TRUE,1
"patient7",15.3104441445857,15.8628171375558,0.552372992970049,TRUE,1
"patient4",8.4617936636985,16.45455534786,7.99276168416155,TRUE,1
"patient9",18.7448303103983,20.0127988776311,1.26796856723277,TRUE,1
"patient10",19.7300227135956,22.828312044025,2.81551316639388,TRUE,1
"patient3",8.10209673901206,23.0229880380157,14.9208912990036,TRUE,1
"patient11",23.9906049108851,25.9918325351391,2.00122762425392,TRUE,1
"patient12",25.9405169038821,26.1159227703485,0.175405866466463,TRUE,1
"patient13",26.6712937972809,27.3953209092167,0.724027111935745,TRUE,1
"patient5",8.46540280187395,31.5905420198408,18.4603668889755,TRUE,1
"patient8",16.205905417034,36.7166887793275,20.5107833622935,TRUE,1
"patient6",8.63425913581517,39.5747750411756,26.4341387698863,TRUE,1
"patient15",42.4788180677501,44.4253911596783,1.94657309192818,TRUE,1
"patient14",33.8646769392969,49.2844993482058,15.4198224089088,TRUE,1
"patient16",46.7747331422285,53.2490956935337,6.47436255130511,TRUE,1
"patient17",62.1826629418392,65.1537224588428,2.9710595170036,TRUE,1
"patient18",64.84189974549,79.2594541268846,14.4175543813946,TRUE,1
"patient1",11.5292835763662,14.9031427099375,3.37385913357139,TRUE,1
"patient0",5.61236810945763,18.1701515059521,12.5577833964945,TRUE,1
"patient2",16.210924648233,25.1845847722787,8.97366012404573,TRUE,1
"patient3",16.2344117024303,27.4989239428969,11.2645122404666,TRUE,1
"patient8",30.8557707337136,32.3019222296248,1.44615149591118,TRUE,1
"patient6",28.0462873339633,35.008438914653,6.96215158068975,TRUE,1
"patient5",22.9520218396357,35.1578565632246,12.2058347235889,TRUE,1
"patient4",20.1216766714946,44.57814563056,24.4564689590654,TRUE,1
"patient9",32.6265519136036,45.3491203040745,12.7225683904709,TRUE,1
"patient10",39.8955314477744,46.3532623473393,6.45773089956492,TRUE,1
"patient7",28.6024922323464,47.6987349736164,19.09624274127,TRUE,1
"patient12",49.297259401364,50.9600157626919,1.66275636132788,TRUE,1
"patient13",50.0016368793526,54.9727527572045,4.97111587785184,TRUE,1
"patient15",52.4486960655281,62.1820293174783,9.73333325195027,TRUE,1
"patient11",47.8695339800992,65.4498565438669,17.5803225637677,TRUE,1
"patient14",51.2955471107939,66.6380878136676,15.3425407028737,TRUE,1
"patient18",65.2617488409633,71.1529451309944,5.89119629003108,TRUE,1
"patient17",64.5503794320677,74.6158714600181,10.0654920279504,TRUE,1
"patient20",71.9046437494131,78.6033728006901,6.69872905127704,TRUE,1
"patient1",3.74071044458036,6.05729660095607,2.31658615637571,TRUE,1
"patient0",0.0598256252706051,7.58141499871884,7.52158937344823,TRUE,1
"patient4",17.3040651446707,21.569363072759,4.26529792808835,TRUE,1
"patient7",26.1080013875599,26.8199421426962,0.711940755136311,TRUE,1
"patient3",14.3875698569124,27.2706708969897,12.8831010400773,TRUE,1
"patient6",24.4548260763968,31.2205793819139,6.76575330551714,TRUE,1
"patient5",19.9931382919229,33.1586089791753,13.1654706872524,TRUE,1
"patient2",10.0408778447025,33.3171065682758,23.2762287235732,TRUE,1
"patient9",42.9153433614659,49.3683949611399,6.45305159967393,TRUE,1
"patient8",32.6399369044863,50.0871238356138,17.4471869311276,TRUE,1
"patient10",44.087143624148,52.037969929624,7.95082630547595,TRUE,1
"patient12",51.0600299635205,52.7642459295084,1.70421596598791,TRUE,1
"patient13",55.5018677249036,57.1663071227677,1.66443939786404,TRUE,1
"patient11",45.4748332341918,59.4559074943474,13.9810742601556,TRUE,1
"patient14",63.1769930478035,63.282704972647,0.10571192484349,TRUE,1
Loading

0 comments on commit 59f82a7

Please sign in to comment.