exampleAPI: Example of using the API inference

Description Usage Details References Examples

View source: R/exampleAPI.R

Description

This is just an example on how to use the REST API of this package to generate the distribution of the population count.

Usage

1

Details

This is a script that shows how to use the REST API of this package to compute the distribution of the initial target population count, the distribution of the population count at successive time instants and the origin-destination matrix.

References

https://github.com/MobilePhoneESSnetBigData

Examples

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
########################################
# First, in a separate R console run the following instructions that start the http API
# on your local computer
# One can replace 127.0.0.1 with the API address of another Web server
library(plumber)
pathPL <-'plumber'
initPop_api <- plumber::plumb(system.file(pathPL, 'plumb.R', package = 'inference'))
initPop_api$run(host = '127.0.0.1', port = 8000)
########################################

library(httr)
library(jsonlite)
library(data.table)

# set the folder where the necessary input files are stored and the prefix of the input file names.
path      <- 'extdata'
prefix <- 'postLocDevice'
postLocPath <- system.file(path, package = 'inference')

# compute the deduplication factors
dpFileName <- system.file(path, 'duplicity.csv', package = 'inference')
rgFileName <- system.file(path, 'regions.csv', package = 'inference')

# prepare the body of the http request
body <- list(
  .dupFileName = dpFileName,
  .regsFileName = rgFileName,
  .postLocPrefix = prefix,
  .postLocPath = postLocPath
)

# set API path
pathDedup <- 'computeDeduplicationFactors'

# send POST Request to API
url <- "http://127.0.0.1:8000"
raw.result <- POST(url = url, path = pathDedup, body = body, encode = 'json')

# check status code
raw.result$status_code

# transform back the results from json format
omega_r <- as.data.table(fromJSON(rawToChar(raw.result$content)))

# Compute the parameters of the distribution
# First reads the number of individuals detected by network
nFileName <- system.file(path, 'nnet.csv', package = 'inference')
nnet <- readNnetInitial(nFileName)

pRFileName <- system.file(path, 'pop_reg.csv', package = 'inference')
pRateFileName <- system.file(path, 'pnt_rate.csv', package = 'inference')
grFileName <- system.file(path, 'grid.csv', package = 'inference')

# prepare the body of the http request
body <- list(
   .omega = omega_r,
  .popRegFileName = pRFileName,
  .pntRateFileName  = pRateFileName,
  .regsFileName = rgFileName,
  .gridFileName = grFileName,
  .rel_bias = 0,
  .cv = 1e-5
)

# set API path
pathDistr <- 'computeDistrParams'

 # send POST Request to API
raw.result <- POST(url = url, path = pathDistr, body = body, encode = 'json')

# check status code
raw.result$status_code

# transform back the results from json format
params <- as.data.table(fromJSON(rawToChar(raw.result$content)))

# Compute the population count distribution at t0 using the Beta Negative Binomial distribution

# prepare the body of the http request
body <- list(
  .nnet = nnet,
  .params = params,
  .popDistr = 'BetaNegBin',
  .rndVal = TRUE,
  .ciprob = 0.95,
  .method = 'ETI'
)

# set API path
pathInit <- 'computeInitialPopulation'

 # send POST Request to API
raw.result <- POST(url = url, path = pathInit, body = body, encode = 'json')

# check status code
raw.result$status_code

# transform back the results from json format
n_bnb <- fromJSON(rawToChar(raw.result$content))

# display results
n_bnb$stats
head(n_bnb$rnd_values)

# Compute the population count distribution at time instants t > t0 using the
# Beta Negative Binomial distribution
# first set the name of the file with the population moving from one region
# to another (output of the aggregation package)
nnetODFile <- system.file(path, 'nnetOD.zip', package = 'inference')

# prepare the body of the http request
body <- list(
  .nt0 = as.data.table(n_bnb$rnd_values),
  .nnetODFileName = nnetODFile,
  .zip = TRUE,
  .rndVal = TRUE,
  .ciprob = 0.95,
  .method = 'ETI'
)

# set API path
pathT <- 'computePopulationT'

 # send POST Request to API
raw.result <- POST(url = url, path = pathT, body = body, encode = 'json')

# check status code
raw.result$status_code

# transform back the results from json format
nt_bnb <- fromJSON(rawToChar(raw.result$content))

# display results
# first, select a random time instant
times <- names(nt_bnb)
t <- sample(1:length(times), size = 1)
t
nt_bnb[[t]]$stats
head(nt_bnb[[t]]$rnd_values)


# Compute the Origin-Destination matrices for all pairs of time instants
# time_from-time_to using the Beta Negative Binomial distribution

# prepare the body of the http request
body <- list(
  .nt0 = as.data.table(n_bnb$rnd_values),
  .nnetODFileName = nnetODFile,
  .zip = TRUE,
  .rndVal = TRUE,
  .ciprob = 0.95,
  .method = 'ETI'
)

# set API path
pathOD <- 'computePopulationOD'

 # send POST Request to API
raw.result <- POST(url = url, path = pathOD, body = body, encode = 'json')

# check status code
raw.result$status_code

# transform back the results from json format
OD_bnb <- fromJSON(rawToChar(raw.result$content))

# display results
time_pairs <- names(OD_bnb)
# first, select a random time instants pair
i <- sample(1:length(time_pairs), size = 1)
time_pairs[i]
OD_bnb[[i]]$stats
head(OD_bnb[[i]]$rnd_values)

bogdanoancea/inference documentation built on Nov. 28, 2020, 9:20 p.m.