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

Fixes #423 export population to csv #480

Merged
merged 2 commits into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export(createPopulationCharacteristics)
export(createSimulationBatch)
export(exportIndividualSimulations)
export(exportPKAnalysesToCSV)
export(exportPopulationToCSV)
export(exportResultsToCSV)
export(exportSensitivityAnalysisResultsToCSV)
export(getAllContainerPathsIn)
Expand Down
8 changes: 5 additions & 3 deletions R/create-individual.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,10 @@ createIndividualCharacteristics <- function(
ageUnit = "year(s)",
gestationalAge = 40,
gestationalAgeUnit = "week(s)",
moleculeOntogenies = NULL) {
moleculeOntogenies = NULL,
seed = NULL) {

# Assuming that if this function is called directly, PKSim was either initialized already
# Assuming that if this function is called directly, PK-Sim was either initialized already
# or should be initialized automatically
initPKSim()

Expand All @@ -99,6 +100,7 @@ createIndividualCharacteristics <- function(
validateIsNumeric(gestationalAge)
validateIsString(gestationalAgeUnit)
validateIsOfType(moleculeOntogenies, MoleculeOntogeny, nullAllowed = TRUE)
validateIsNumeric(seed, nullAllowed = TRUE)

moleculeOntogenies <- c(moleculeOntogenies)
individualCharacteristics <- IndividualCharacteristics$new()
Expand All @@ -109,7 +111,7 @@ createIndividualCharacteristics <- function(
individualCharacteristics$weight <- createSnapshotParameter(weight, weightUnit)
individualCharacteristics$height <- createSnapshotParameter(height, heightUnit)
individualCharacteristics$gestationalAge <- createSnapshotParameter(gestationalAge, gestationalAgeUnit)

individualCharacteristics$seed <- seed
for (moleculeOntogeny in moleculeOntogenies) {
individualCharacteristics$addMoleculeOntogeny(moleculeOntogeny)
}
Expand Down
15 changes: 9 additions & 6 deletions R/create-population.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ createPopulation <- function(populationCharacteristics) {

# NOTE THIS IS A WORKAROUND UNTIL THE CODE IN PKSIM IS UPDATED
if (populationCharacteristics$species == Species$Human) {
# create an individual with similar properites Species and population. WEIGHT AND AGE DO NOT MATTER as long as we can create an invidiual
# create an individual with similar properties Species and population. WEIGHT AND AGE DO NOT MATTER as long as we can create an invidiual
individualCharacteristics <- ospsuite::createIndividualCharacteristics(
species = populationCharacteristics$species,
population = populationCharacteristics$population,
age = 30
)
}
else {
# create an individual with similar properites Species and population. WEIGHT AND AGE DO NOT MATTER as long as we can create an invidiual
# create an individual with similar properties Species and population. WEIGHT AND AGE DO NOT MATTER as long as we can create an invidiual
individualCharacteristics <- ospsuite::createIndividualCharacteristics(
species = populationCharacteristics$species,
population = populationCharacteristics$population,
Expand All @@ -38,7 +38,7 @@ createPopulation <- function(populationCharacteristics) {

derivedParameters <- list()

# Even though those parameters are derived parmaeters, we keep them in the population for consistency purpose with the PKSim export.
# Even though those parameters are derived parameters, we keep them in the population for consistency purpose with the PKSim export.
standardDerivedParametersToKeep <- c(StandardPath$Weight, StandardPath$BMI, StandardPath$BSA)

for (derivedParameterPath in individual$derivedParameters$paths) {
Expand All @@ -52,7 +52,7 @@ createPopulation <- function(populationCharacteristics) {
}
}

# other parameters to remvove that should not have been exported in the first place
# other parameters to remove that should not have been exported in the first place
standardDistributedParametersToRemove <- c(toPathString(StandardContainer$Organism, "MeanBW"), toPathString(StandardContainer$Organism, "MeanHeight"))
for (parameterToRemove in standardDistributedParametersToRemove) {
if (population$has(parameterToRemove)) {
Expand Down Expand Up @@ -85,6 +85,7 @@ createPopulation <- function(populationCharacteristics) {
#' @param gestationalAgeMax max gestational age for the population (optional, for human species only)
#' @param gestationalAgeUnit Unit in which the gestational age value is defined. Default is kg/m2
#' @param moleculeOntogenies Optional list of \code{MoleculeOntogeny} that will be used to retrieve ontogeny information for molecules.
#' @param seed Optional Seed parameter used to generate random values. This is only useful in order to reproduce the same population
#'
#' @return An instance of \code{PopulationCharacteristics} to be used in conjunction with \code{createPopulation}
#'
Expand All @@ -109,8 +110,8 @@ createPopulationCharacteristics <- function(
gestationalAgeMin = NULL,
gestationalAgeMax = NULL,
gestationalAgeUnit = "week(s)",

moleculeOntogenies = NULL) {
moleculeOntogenies = NULL,
seed = NULL) {

# Assuming that if this function is called directly, PKSim was either initialized already
# or should be initialized automatically
Expand Down Expand Up @@ -142,6 +143,7 @@ createPopulationCharacteristics <- function(
validateIsString(gestationalAgeUnit)

validateIsOfType(moleculeOntogenies, MoleculeOntogeny, nullAllowed = TRUE)
validateIsInteger(seed, nullAllowed = TRUE)

moleculeOntogenies <- c(moleculeOntogenies)
populationCharacteristics <- PopulationCharacteristics$new()
Expand All @@ -154,6 +156,7 @@ createPopulationCharacteristics <- function(
populationCharacteristics$height <- createParameterRange(heightMin, heightMax, heightUnit)
populationCharacteristics$gestationalAge <- createParameterRange(gestationalAgeMin, gestationalAgeMax, gestationalAgeUnit)
populationCharacteristics$BMI <- createParameterRange(BMIMin, BMIMax, BMIUnit)
populationCharacteristics$seed <-seed

for (moleculeOntogeny in moleculeOntogenies) {
populationCharacteristics$addMoleculeOntogeny(moleculeOntogeny)
Expand Down
11 changes: 10 additions & 1 deletion R/dot-net-wrapper.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,16 @@ DotNetWrapper <- R6::R6Class(
rClr::clrSet(self$ref, propertyName, as.integer(value))
}
},

wrapNullableIntegerProperty = function(propertyName, value) {
if(missing(value)){
return(rClr::clrGet(self$ref, propertyName))
}
if(is.null(value)){
return()
} else {
rClr::clrSet(self$ref, propertyName, as.integer(value))
}
},
wrapIndexProperty = function(propertyName, value) {
# Special method needed because Index are 0-based in .NET but 1-based in R
if (missing(value)) {
Expand Down
7 changes: 7 additions & 0 deletions R/individual-characteristics.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ IndividualCharacteristics <- R6::R6Class(
#' @field allMoleculeOntogenies All molecule ontogenies defined for this individual characteristics.
allMoleculeOntogenies = function(value) {
private$readOnlyProperty("allMoleculeOntogenies", value, private$.moleculeOntogenies)
},
#' @field seed Seed used to generate the population
seed = function(value) {
private$wrapNullableIntegerProperty("Seed", value)
}
),
private = list(
Expand Down Expand Up @@ -85,6 +89,9 @@ IndividualCharacteristics <- R6::R6Class(
for (moleculeOntogeny in self$allMoleculeOntogenies) {
moleculeOntogeny$printMoleculeOntogeny()
}
if(!is.null(self$seed)){
private$printLine("Seed", self$seed)
}
invisible(self)
},

Expand Down
8 changes: 8 additions & 0 deletions R/population-characteristics.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ PopulationCharacteristics <- R6::R6Class(
#' @field allMoleculeOntogenies All molecule ontogenies defined for this individual characteristics.
allMoleculeOntogenies = function(value) {
private$readOnlyProperty("allMoleculeOntogenies", value, private$.moleculeOntogenies)
},
#' @field seed Seed used to generate the population
seed = function(value) {
private$wrapNullableIntegerProperty("Seed", value)
}
),
private = list(
Expand Down Expand Up @@ -92,9 +96,13 @@ PopulationCharacteristics <- R6::R6Class(
private$printRange("Weight", self$weight)
private$printRange("Height", self$height)
private$printRange("BMI", self$BMI)

for (moleculeOntogeny in self$allMoleculeOntogenies) {
moleculeOntogeny$printMoleculeOntogeny()
}
if(!is.null(self$seed)){
private$printLine("Seed", self$seed)
}
invisible(self)
},

Expand Down
2 changes: 1 addition & 1 deletion R/printable.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' @title Printable
#' @docType class
#' @description Base class that implements some basic functionalities for printing to console
#' @description Base class that implements some basic properties for printing to console
#'
Printable <- R6::R6Class(
"Printable",
Expand Down
24 changes: 24 additions & 0 deletions R/utilities-population.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,27 @@ populationAsDataFrame <- function(population) {

data.frame(columns, stringsAsFactors = FALSE, check.names = FALSE)
}

#' Saves the population to csv file
#'
#' @param population Population to export to csv (typically imported from file using \code{loadPopulation})
#' @param filePath Full path where the population will be saved.
#'
#' @examples
#' csvPath <- system.file("extdata", "pop.csv", package = "ospsuite")
#'
#' # Load the population
#' population <- loadPopulation(csvPath)
#'
#' # Exports the population
#' exportPopulationToCSV(population, tempfile())
#' @export
exportPopulationToCSV <- function(population, filePath) {
validateIsOfType(population, Population)
validateIsString(filePath)
filePath <- expandPath(filePath)
df <- populationAsDataFrame(population)
write.csv(df, file = filePath, row.names = FALSE)
invisible()
}

Binary file modified inst/lib/OSPSuite.Assets.dll
Binary file not shown.
Binary file modified inst/lib/OSPSuite.Core.dll
Binary file not shown.
Binary file modified inst/lib/OSPSuite.Infrastructure.Autofac.dll
Binary file not shown.
Binary file modified inst/lib/OSPSuite.Infrastructure.Import.dll
Binary file not shown.
Binary file modified inst/lib/OSPSuite.Infrastructure.dll
Binary file not shown.
Binary file modified inst/lib/OSPSuite.R.dll
Binary file not shown.
2 changes: 2 additions & 0 deletions man/IndividualCharacteristics.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions man/PopulationCharacteristics.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/Printable.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/createIndividualCharacteristics.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion man/createPopulationCharacteristics.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions man/exportPopulationToCSV.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions packages.config
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="OSPSuite.Assets" version="10.0.80"/>
<package id="OSPSuite.Core" version="10.0.80"/>
<package id="OSPSuite.Infrastructure" version="10.0.80"/>
<package id="OSPSuite.Infrastructure.Import" version="10.0.80"/>
<package id="OSPSuite.Infrastructure.Autofac" version="10.0.80"/>
<package id="OSPSuite.R" version="10.0.80 "/>
<package id="OSPSuite.Assets" version="10.0.84"/>
<package id="OSPSuite.Core" version="10.0.84"/>
<package id="OSPSuite.Infrastructure" version="10.0.84"/>
<package id="OSPSuite.Infrastructure.Import" version="10.0.84"/>
<package id="OSPSuite.Infrastructure.Autofac" version="10.0.84"/>
<package id="OSPSuite.R" version="10.0.84 "/>
<package id="OSPSuite.Serializer" version="3.0.0.1"/>
<package id="OSPSuite.Utility" version="4.0.0.4"/>
<package id="LumenWorksCsvReader" version="4.0.0"/>
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-population.R
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,13 @@ test_that("It can retrieve all ids define ina population", {
population <- loadPopulation(populationFileName)
expect_length(population$allIndividualIds, 10)
})

context("exportPopulationToCSV")

test_that("It can export valid population to CSV", {
executeWithTestFile(function(csvFile) {
population <- loadPopulation(populationFileName)
exportPopulationToCSV(population, csvFile)
expect_true(file.exists(csvFile))
})
})