Closed mjskay closed 10 months ago
Attention: 1 lines
in your changes are missing coverage. Please review.
Comparison is base (
0322b46
) 95.14% compared to head (45b0643
) 95.31%. Report is 28 commits behind head on master.:exclamation: Current head 45b0643 differs from pull request most recent head fe6f23b. Consider uploading reports for the commit fe6f23b to get more accurate results
Files | Patch % | Lines |
---|---|---|
R/variables.R | 99.06% | 1 Missing :warning: |
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
This is how benchmark results would change (along with a 95% confidence interval in relative change) if 5f49cb39fd10080ab69076ecb112c3918590ba20 is merged into master:
This is how benchmark results would change (along with a 95% confidence interval in relative change) if 7336811ab059fa92ba47f941bcbba1d07a14fa9d is merged into master:
This is how benchmark results would change (along with a 95% confidence interval in relative change) if 45b0643af05e3144b87b8da54b1dcc97c8020890 is merged into master:
thank you! Please let me know once this PR is ready for review!
Should be ready now
closes https://github.com/stan-dev/posterior/issues/340 by adding an extract_variable_array() function to extract variables with indices into arrays of iterations x chains x any remaining dimensions. @avehtari does this do what you need?
Yes, it's helpful as it works the same way for any draws object type, and I can write my example in the issue as
p1$draws() |> subset_draws(draw=1) |> extract_variable_array(variable='r_id') |> drop()
or if I want to get all draws array
p1$draws() |> merge_chains() |> extract_variable_array(variable='r_id') |> drop()
(I can also drop drop()
and index the 4 dim array)
Great! @mjskay anything to change from your side? Otherwise, feel free to merge (or tell me such that I merge).
Thanks for the quick review @paul-buerkner!
Maybe let's wait a day or two and see if @jgabry @MansMeg @avehtari or others have objections/suggestions for the with_indices
parameter name, and if not then I can merge.
Agreed.
I'm fine with with_indices
and can't come up with better suggestions. I tested using variables()
, nvariables()
, and set_variables()
, and having with_indices
parameter is awesome!
Great, thanks @avehtari! I will take that as a strong endorsement and merge :)
Summary
This PR:
variables()
,variables<-()
,set_variables()
, andnvariables()
awith_indices
argument, which determines whether variable names are retrieved/set with ("x[1]"
,"x[2]"
...) or without ("x"
) indices.extract_variable_array()
function to extract variables with indices into arrays of iterations x chains x any remaining dimensions. @avehtari does this do what you need?It also makes a few other related changes:
For types that support
factor
variables (draws_df
,draws_list
, anddraws_rvars
),extract_variable()
andextract_variable_matrix()
now return afactor
/ordered
if the variable requested is afactor
orordered
.It factors out a bunch of internal code for manipulating variable indices into
R/variable-indices.R
. Most important are:split_variable_names()
andsplit_indices()
, which I suggest as the "canonical" functions for (1) splitting a variable into a string representing base name and indices and (2) parsing those indices if needed. The code forsplit_variable_names()
was repeated in several places in the code base (implemented slightly differently). I tried several implementations on large vectors and the one I put there should be fast.variable_names()
andvariable_names<-()
, which are functions with awith_indices
parameter that can manipulate character vectors of variable names. These usesplit_variable_names()
under the hood, and are the basis of the implementation ofwith_indices
invariables()
andvariables<-()
flatten_indices()
andflatten_array()
, which are used to turn arrays into vectors where indices are embedded into variable names (used when converting from draws_rvars to other formats that embed indices into variable names).Factoring these functions out means if we later add support for more specialized parsing of variable names, we only have to do that in one place. It also gets us a bit closer to implementations of index parsing we could consider exporting (e.g. for #61), which could perhaps be a
parse_variable_names()
function built onsplit_variable_names()
+split_indices()
.Moves the code for
variables()
,variables<-()
,set_variables()
, andnvariables()
fromR/draws-index.R
intoR/variables.R
and gives them their own documentation pages (R/draws-index.R
and the combined doc page for variables/iterations/chains/draws was getting bloated, especially with the extra args on variables functions).Minor refactor of
while_preserving_dims()
->copy_dims()
andwhile_preserving_levels()
->copy_levels()
Minor fix for a change in how the package doc page is specified in the latest version of roxygen2.
Copyright and Licensing
By submitting this pull request, the copyright holder is agreeing to license the submitted work under the following licenses: