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 set_variables alias of variables<- #296

Merged
merged 6 commits into from
Aug 7, 2023

Conversation

n-kall
Copy link
Collaborator

@n-kall n-kall commented Aug 1, 2023

Summary

Adds set_variables function which is an alias of variables<-. Fixes #281

Example functionality:

x <- as_draws(matrix(rnorm(100), ncol = 2))
variables(x)
#> [1] "...1" "...2"

x |> set_variables(c("theta[1]", "theta[2]")) |> variables()
#> [1] "theta[1]" "theta[2]"

# this is equivalent to
variables(x) <- c("theta[1]", "theta[2]")
variables(x)
#> [1] "theta[1]" "theta[2]"

Copyright and Licensing

By submitting this pull request, the copyright holder is agreeing to
license the submitted work under the following licenses:

@n-kall n-kall changed the title add set_variables alias of variables<- with documentation and tests add set_variables alias of variables<- Aug 1, 2023
@paul-buerkner
Copy link
Collaborator

Thank you! Would you mind moving the code to the R file where variables() <- is defined to keep them together? Same with the tests.

@n-kall
Copy link
Collaborator Author

n-kall commented Aug 1, 2023

``

Would you mind moving the code to the R file where variables() <- is defined to keep them together? Same with the tests.

Done!

Copy link
Collaborator

@paul-buerkner paul-buerkner left a comment

Choose a reason for hiding this comment

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

Thank you! I have a few minor comments before merging this.

R/draws-index.R Outdated
#' x <- as_draws(matrix(rnorm(100), ncol = 2))
#' variables(x)
#'
#' x |> set_variables(c("theta[1]", "theta[2]")) |> variables()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please avoid the use of the pipe in the docs because this may lead to a failur in old R versions not having the pipe.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed

R/draws-index.R Outdated
#' variables(x)
#'
#' @export
set_variables <- function(.x, ...) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

why use .x as the first argument here? Wouldn't x be okay too?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed by changing variable to x

R/draws-index.R Outdated Show resolved Hide resolved
R/draws-index.R Outdated

#' @rdname set_variables
#' @export
set_variables.draws <- function(x, variables, ...) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why make set_variables generic? If it is a non-generic function that just forwards to variables<- then implementers of other classes need only override the generic there.

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 didn't think too much about this either way, as I just used rename_variables as a template, which is a generic with only a rename_variables.draws method defined. If it's better to change it to a non-generic function, I can do that

Copy link
Collaborator

Choose a reason for hiding this comment

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

I defer to @paul-buerkner, but IMO it would be simpler :).

Copy link
Collaborator

Choose a reason for hiding this comment

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

Good point. didn't think of this, but I agree. We should make it a non-generic function. Same with rename_variables I think but I will look at the latter separately.

Copy link
Member

Choose a reason for hiding this comment

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

non-generic sounds good to me too

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 changed it to non-generic now

@codecov-commenter
Copy link

codecov-commenter commented Aug 1, 2023

Codecov Report

Merging #296 (e4af9b4) into master (59f4e6c) will increase coverage by 0.11%.
Report is 27 commits behind head on master.
The diff coverage is 100.00%.

❗ Current head e4af9b4 differs from pull request most recent head c74e765. Consider uploading reports for the commit c74e765 to get more accurate results

@@            Coverage Diff             @@
##           master     #296      +/-   ##
==========================================
+ Coverage   95.84%   95.96%   +0.11%     
==========================================
  Files          44       44              
  Lines        3347     3417      +70     
==========================================
+ Hits         3208     3279      +71     
+ Misses        139      138       -1     
Files Changed Coverage Δ
R/as_draws_df.R 99.01% <100.00%> (ø)
R/draws-index.R 93.93% <100.00%> (+0.06%) ⬆️
R/repair_draws.R 98.14% <100.00%> (ø)
R/rvar-.R 97.50% <100.00%> (+0.36%) ⬆️
R/rvar-apply.R 100.00% <100.00%> (ø)
R/rvar-cast.R 98.96% <100.00%> (+0.09%) ⬆️
R/rvar-dim.R 100.00% <100.00%> (ø)
R/rvar-rfun.R 100.00% <100.00%> (ø)
R/rvar-slice.R 100.00% <100.00%> (ø)
R/summarise_draws.R 82.14% <100.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@github-actions
Copy link

github-actions bot commented Aug 2, 2023

This is how benchmark results would change (along with a 95% confidence interval in relative change) if 72b4048 is merged into master:

  •   :ballot_box_with_check:as_draws_array: 158ms -> 156ms [-1.89%, +0.43%]
  •   :rocket:as_draws_df: 124ms -> 54.1ms [-57.99%, -54.91%]
  •   :ballot_box_with_check:as_draws_list: 278ms -> 272ms [-4.38%, +0.3%]
  •   :rocket:as_draws_matrix: 48.8ms -> 47.9ms [-3.8%, -0.21%]
  •   :ballot_box_with_check:as_draws_rvars: 258ms -> 256ms [-2.12%, +0.34%]
  •   :ballot_box_with_check:summarise_draws_100_variables: 1.11s -> 1.11s [-1.3%, +0.49%]
  •   :ballot_box_with_check:summarise_draws_10_variables: 122ms -> 122ms [-0.54%, +1.3%]
    Further explanation regarding interpretation and methodology can be found in the documentation.

Copy link
Collaborator

@paul-buerkner paul-buerkner left a comment

Choose a reason for hiding this comment

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

looks good. One minor thing before I will merge

R/draws-index.R Outdated
#' x <- as_draws(matrix(rnorm(100), ncol = 2))
#' variables(x)
#'
#' set_variables(x, c("theta[1]", "theta[2]"))
Copy link
Collaborator

Choose a reason for hiding this comment

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

you are not assigning to x here. Is that intentional?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good catch! Fixed this now.

@github-actions
Copy link

github-actions bot commented Aug 6, 2023

This is how benchmark results would change (along with a 95% confidence interval in relative change) if e4af9b4 is merged into master:

  •   :ballot_box_with_check:as_draws_array: 146ms -> 151ms [-1.58%, +8.98%]
  •   :rocket:as_draws_df: 138ms -> 51.2ms [-67.33%, -58.67%]
  •   :ballot_box_with_check:as_draws_list: 264ms -> 272ms [-1.06%, +7.03%]
  •   :ballot_box_with_check:as_draws_matrix: 46.7ms -> 45.8ms [-9.44%, +5.49%]
  •   :ballot_box_with_check:as_draws_rvars: 255ms -> 250ms [-6.52%, +2.99%]
  •   :ballot_box_with_check:summarise_draws_100_variables: 1.1s -> 1.1s [-2.49%, +1.4%]
  •   :ballot_box_with_check:summarise_draws_10_variables: 122ms -> 121ms [-5.62%, +4.04%]
    Further explanation regarding interpretation and methodology can be found in the documentation.

@paul-buerkner paul-buerkner merged commit 55e9233 into stan-dev:master Aug 7, 2023
10 checks passed
@paul-buerkner
Copy link
Collaborator

Thank you for adding this!

@n-kall
Copy link
Collaborator Author

n-kall commented Aug 7, 2023

Thank you for adding this!

Thanks for the review!

@n-kall n-kall deleted the set_variables branch August 7, 2023 08:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[feature] set_variable_names (alias of variables<-) for piping
5 participants