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

switch bins to closed on left #253

Merged
merged 3 commits into from
Mar 18, 2024
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
4 changes: 2 additions & 2 deletions R/utils-bin.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ bin.numeric <- function(x, binWidth = NULL, viewport, stringsAsFactors = c(FALSE
bins <- xVP
if (stringsAsFactors) bins <- as.factor(bins)
} else {
bins <- veupathUtils::cut_width(xVP, binWidth, boundary = min(xVP))
bins <- veupathUtils::cut_width(xVP, binWidth, boundary = min(xVP), closed = "left")
}
} else {
numBins <- findNumBins(xVP)
bins <- veupathUtils::cut_interval(xVP, numBins)
bins <- veupathUtils::cut_interval(xVP, numBins, right=FALSE)
}

bins <- pruneViewportAdjustmentFromBins(bins, xVP, x, viewport)
Expand Down
35 changes: 33 additions & 2 deletions tests/testthat/test-histogram.R
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ test_that("histogram.dt() returns requested numBins/ binWidth", {
expect_equal(as.numeric(binSpec(dt)$value), 5) # ensure we get the correct number of bins
expect_true(max(as.numeric(unlist(dt$binEnd)) - as.numeric(unlist(dt$binStart)) - binWidth) < 0.1) # Tolerance 0.1
numericLabelsStart <- unlist(lapply(unlist(dt$binLabel), function(x) as.numeric(stringi::stri_split_regex(x, ",|]|\\(|\\[")[[1]][2])))
numericLabelsEnd <- unlist(lapply(unlist(dt$binLabel), function(x) as.numeric(stringi::stri_split_regex(x, ",|]|\\(|\\[")[[1]][3])))
numericLabelsEnd <- unlist(lapply(unlist(dt$binLabel), function(x) as.numeric(stringi::stri_split_regex(x, ",|]|\\(|\\[|\\)")[[1]][3])))
expect_true(max(numericLabelsEnd - numericLabelsStart) - binWidth < 0.1) # Label tolerance

binReportValue <- 'numBins'
Expand All @@ -153,7 +153,7 @@ test_that("histogram.dt() returns requested numBins/ binWidth", {
expect_equal(as.numeric(binSpec(dt)$value), 15) # ensure we get the correct number of bins
expect_true(max(as.numeric(unlist(dt$binEnd)) - as.numeric(unlist(dt$binStart)) - binWidth) < 0.1) # Tolerance 0.1
numericLabelsStart <- unlist(lapply(unlist(dt$binLabel), function(x) as.numeric(stringi::stri_split_regex(x, ",|]|\\(|\\[")[[1]][2])))
numericLabelsEnd <- unlist(lapply(unlist(dt$binLabel), function(x) as.numeric(stringi::stri_split_regex(x, ",|]|\\(|\\[")[[1]][3])))
numericLabelsEnd <- unlist(lapply(unlist(dt$binLabel), function(x) as.numeric(stringi::stri_split_regex(x, ",|]|\\(|\\[|\\)")[[1]][3])))
expect_true(max(numericLabelsEnd - numericLabelsStart) - binWidth < 0.1) # Label tolerance
})

Expand Down Expand Up @@ -1175,3 +1175,34 @@ test_that("histogram.dt does not produce corrupted bins when given TAC data from
expect_equal(sum(is.na(as.numeric(unlist(dt$binStart)))), 0)
expect_equal(sum(is.na(as.numeric(unlist(dt$binEnd)))), 0)
})

test_that("our bins are always left closed/ inclusive", {
variables <- new("VariableMetadataList", SimpleList(
new("VariableMetadata",
variableClass = new("VariableClass", value = 'native'),
variableSpec = new("VariableSpec", variableId = 'cat3', entityId = 'entity'),
plotReference = new("PlotReference", value = 'facet2'),
dataType = new("DataType", value = 'STRING'),
dataShape = new("DataShape", value = 'CATEGORICAL')),
new("VariableMetadata",
variableClass = new("VariableClass", value = 'native'),
variableSpec = new("VariableSpec", variableId = 'cat4', entityId = 'entity'),
plotReference = new("PlotReference", value = 'facet1'),
dataType = new("DataType", value = 'STRING'),
dataShape = new("DataShape", value = 'CATEGORICAL')),
new("VariableMetadata",
variableClass = new("VariableClass", value = 'native'),
variableSpec = new("VariableSpec", variableId = 'contA', entityId = 'entity'),
plotReference = new("PlotReference", value = 'xAxis'),
dataType = new("DataType", value = 'NUMBER'),
dataShape = new("DataShape", value = 'CONTINUOUS'))
))

df <- as.data.frame(testDF)
viewport <- list('xMin'=min(df$entity.contA), 'xMax'=max(df$entity.contA))
binReportValue <- 'binWidth'

dt <- histogram.dt(df, variables, binWidth = .3, value='count', barmode = 'overlay', binReportValue, viewport)

expect_equal(all(unlist(lapply(dt$binLabel,substr,1,1)) == '['), TRUE)
})
Loading