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

add error message for missing strata #227

Open
wants to merge 25 commits into
base: main
Choose a base branch
from

Conversation

chrisorwa
Copy link
Collaborator

No description provided.

@chrisorwa chrisorwa linked an issue Aug 1, 2024 that may be closed by this pull request
Copy link

codecov bot commented Aug 1, 2024

Codecov Report

Attention: Patch coverage is 0% with 17 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
R/est.incidence.by.R 0.00% 17 Missing ⚠️
Files with missing lines Coverage Δ
R/est.incidence.by.R 0.00% <0.00%> (ø)

Copy link
Member

@d-morrison d-morrison left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this PR; please address comments and add tests for the code lines in this PR.

cli::cli_abort(
class = "missing_var",
message = c(
"Can't find the column {.var {strata}} in {.envvar pop_data}.",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please revise this inline markup; pop_data is a function argument, not an environment variable (see https://cli.r-lib.org/reference/inline-markup.html)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is well-noted.

Comment on lines 118 to 120
if (length(strata_var) > 0) {
"i" <- "Did you mean {.var {strata_var}}."
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a test that covers this case

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been implemented.

@@ -101,6 +101,27 @@ est.incidence.by <- function(
return(to_return)
}

if (!any(is.element(strata,pop_data %>% names()))) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please always format according to the style guide (add a space after the comma in strata,pop_data, etc)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change has been made.

Copy link
Member

@d-morrison d-morrison left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getting closer; please see inline comments.

@@ -101,6 +101,27 @@ est.incidence.by <- function(
return(to_return)
}

if (!any(is.element(strata, pop_data %>% names()))) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should any() be all()?

Copy link
Collaborator Author

@chrisorwa chrisorwa Aug 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be all(). The change has been made.

grep(
x = pop_data %>% names(),
value = TRUE,
pattern = strata,
Copy link
Member

@d-morrison d-morrison Aug 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens here if strata has multiple entries in it? for example, strata = c("Country", "catch")? Please check this scenario and adjust this code if needed (probably, try to match each missing stratum variable separately).

Copy link
Collaborator Author

@chrisorwa chrisorwa Aug 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is in order and adds complexity to Issue #233. For example, if strata = c("age", "Country"), I had opted to use regex paste0("^", strata) to capture only columns that begin with age to avoid and avoid words such as village.

Addendum:
Never mind, I found a solution to it.

message = c(
"Can't find the column {.arg {strata}} in {.arg pop_data}.",
if (length(strata_var) > 0) {
"i" <- "Did you mean {.var {strata_var}}."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please end questions with a question mark instead of a period.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been implemented.

cli::cli_abort(
class = "missing_var",
message = c(
"Can't find the column {.arg {strata}} in {.arg pop_data}.",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please inform the user that this error relates to their input for the strata argument, so they know which input to fix.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been implemented.

Copy link
Member

@d-morrison d-morrison left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the hard work on this PR; please see inline comments.

@@ -101,6 +101,36 @@ est.incidence.by <- function(
return(to_return)
}

if (!all(is.element(strata, pop_data %>% names()))) {
# accommodate multiple strata
combined_pattern <- paste(strata, collapse = "|")
Copy link
Member

@d-morrison d-morrison Aug 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach seems sub-optimal, for two reasons:

  1. we won't be able to tell which columns partially match each specific missing element of strata.
  2. we didn't remove the elements of strata that already exactly match columns in pop_data

More generally, complicated regular expressions are a recipe for bugs; if you think you need to use | in a regex, please carefully consider alternative approaches. As always, we want to keep the code as simple as possible; for example, in this case, instead of pasting strata together, we could instead individually grep each element of strata that isn't exactly in pop_data, using a for loop, or lapply(), or purrr::map(), etc.

Then, we can report the possible partial matches for each missing element of strata; the markup capabilities of cli_abort() enable us to provide this information in an orderly, structured format. Please give all this a try, but if you get stuck, just let me know and I'll provide more detailed instructions.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have made changes to this approach to capture full match, partial match and no match.

curve <- load_curve_params("https://osf.io/download/rtw5k/")

expect_error(
object = est.incidence.by(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we testing est.incidence.by() in the test file for est.incidence()? Please be careful to keep these tests properly organized.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a test in which strata contains multiple elements that don't exactly match the columns of pop_data

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have separated est.incidence and est.incidence.by into different scripts. In addition, I have added a test for multiple strata in est.incidence.by.

@d-morrison d-morrison added bug an unexpected problem or unintended behavior enhancement API application programming interface (exported functions, etc) unit testing labels Aug 14, 2024
@d-morrison d-morrison added this to the v2.0 release milestone Aug 14, 2024
Copy link
Member

@d-morrison d-morrison left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this PR is still in-progress?

  • Please address the lint errors and restore the files that seem to have been accidentally deleted.

  • Before re-requesting review, please look through Files changed and add at least one comment per changed file: briefly explain what's been changed and why.

R/est.incidence.by.R Show resolved Hide resolved
@@ -39,7 +50,7 @@
#'
#' curve <- load_curve_params("https://osf.io/download/rtw5k/") %>%
#' filter(antigen_iso %in% c("HlyE_IgA", "HlyE_IgG")) %>%
#' slice(1:100, .by = antigen_iso) # Reduce dataset for the purposes of this example
#' slice(1:100, .by = antigen_iso)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove comment to make the line less than 80 characters.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of removing the comment, why not move it to a new line?

DESCRIPTION Outdated Show resolved Hide resolved
R/est.incidence.by.R Outdated Show resolved Hide resolved
R/est.incidence.by.R Show resolved Hide resolved
R/est.incidence.by.R Show resolved Hide resolved
R/est.incidence.by.R Show resolved Hide resolved
R/est.incidence.by.R Show resolved Hide resolved
Comment on lines 178 to 179
require(serocalculator) # note - this gets out of sync" %>%
"when using load_all() in development"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reduce sentences to 80 characters (linting requirement).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it looks like the second half of that comment got turned into code; why?

tests/testthat/_snaps/est.incidence.by.md Outdated Show resolved Hide resolved
Copy link
Collaborator Author

@chrisorwa chrisorwa Sep 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file has been moved to PR #228 . This file was originally part of the as_noise_params PR and got to this PR by mistake. I have moved it back to the noise PR.

Copy link
Member

@d-morrison d-morrison left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this! Please see comments. Not sure why codecov didn't throw an error; please increase the patch coverage.

DESCRIPTION Outdated
@@ -1,7 +1,7 @@
Package: serocalculator
Type: Package
Title: Estimating Infection Rates from Serological Data
Version: 1.2.0.9001
Version: 1.3.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please increment dev version instead of release version

@@ -1,4 +1,7 @@
# serocalculator (development version)
# serocalculator 1.3.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use dev version

@@ -26,7 +35,9 @@
#'
#' @return
#' * if `strata` has meaningful inputs:
#' An object of class `"seroincidence.by"`; i.e., a list of `"seroincidence"` objects from [est.incidence()], one for each stratum, with some meta-data attributes.
#' An object of class `"seroincidence.by"`; i.e., a list of `
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please move linebreak to before the backtick

NEWS.md Outdated
@@ -1,4 +1,7 @@
# serocalculator (development version)
# serocalculator 1.3.0
* Add test for `est.incidence`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

est.incidence.by?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and what behavior is being tested?

NEWS.md Outdated
# serocalculator 1.3.0
* Add test for `est.incidence`

* Add option to test `strata` in `pop_data`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this a user-facing option? not sure what this means.

slice_head(n = 100)


est_true <- est.incidence(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

slice_head(n = 100)


est_false <- est.incidence(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see above

start = start
)

expect_snapshot(x = typhoid_results)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add an additional test using expect_snapshot_value() or expect_snapshot_file() (please think through which one seems best to use).

Comment on lines +89 to +93
"consider using the `est.incidence()` function to
simplify your code and avoid this warning.",
"\n\n Since the `strata` argument is empty, `est.incidence.by()`
will return a `seroincidence` object, instead of a
`seroincidence.by` object.\n"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add tests everywhere indicated by Codecov comments

@d-morrison d-morrison added feature a feature request or enhancement and removed enhancement labels Sep 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API application programming interface (exported functions, etc) bug an unexpected problem or unintended behavior feature a feature request or enhancement unit testing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add user error message for missing strata
2 participants