stan-dev / posterior

The posterior R package
https://mc-stan.org/posterior/
Other
167 stars 24 forks source link

Add a `repair_variable_names` function #317

Open WardBrian opened 1 year ago

WardBrian commented 1 year ago

This allows you to translate from the names Stan uses (like theta.1) to those Posterior wants (theta[1]).

This function exists (privately) in cmdstanr, and is currently very simple:

repair_variable_names <- function(names) {
    names <- sub("\\.", "[", names)
    names <- gsub("\\.", ",", names)
    names[grep("\\[", names)] <- paste0(names[grep("\\[", names)], "]")
    names
}

This doesn't work for complex numbers or tuples, but since posterior doesn't seem to support those either I think this is fine

jgabry commented 1 year ago

If the other posterior authors are ok with adding this I can make a PR.

paul-buerkner commented 1 year ago

yes that would be nice!

Jonah Gabry @.***> schrieb am Fr., 17. Nov. 2023, 17:54:

If the other posterior authors are ok with adding this I can make a PR.

— Reply to this email directly, view it on GitHub https://github.com/stan-dev/posterior/issues/317#issuecomment-1816767698, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADCW2AGEQFTYLS5X4WBOYYLYE6JCTAVCNFSM6AAAAAA7P7MTDCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQMJWG43DONRZHA . You are receiving this because you are subscribed to this thread.Message ID: @.***>

jgabry commented 1 year ago

@paul-buerkner Any preference where this should go? I was thinking of documenting it on the same doc page as set_variables(), but I could also put it somewhere else. On slack @avehtari gave an example of

x <- set_variables(x, repair_variable_names(bad_names))

which seems like the most common use case for a function like repair_variable_names.

paul-buerkner commented 1 year ago

yeah I like that!

Jonah Gabry @.***> schrieb am Fr., 17. Nov. 2023, 18:30:

@paul-buerkner https://github.com/paul-buerkner Any preference where this should go? I was thinking of documenting it on the same doc page as set_variables(), but I could also put it somewhere else. On slack @avehtari https://github.com/avehtari gave an example of

x <- set_variables(x, repair_variable_names(bad_names))

which seems like the most common use case for a function like repair_variable_names.

— Reply to this email directly, view it on GitHub https://github.com/stan-dev/posterior/issues/317#issuecomment-1816819484, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADCW2ADQ6NLYK6J7LH75RZTYE6NK3AVCNFSM6AAAAAA7P7MTDCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQMJWHAYTSNBYGQ . You are receiving this because you were mentioned.Message ID: @.***>

avehtari commented 1 year ago

Started to think, whether repair is the best name? Would convert be better?

avehtari commented 1 year ago

This doesn't work for complex numbers or tuples, but since posterior doesn't seem to support those either I think this is fine

Can you add examples of how the names for these would look like? It would be good to recognize complex and tuples even if there would not be support at the moment

jgabry commented 1 year ago

Started to think, whether repair is the best name? Would convert be better?

I opened a PR for this (https://github.com/stan-dev/posterior/pull/318) using repair but I'm happy to change the name. Do you want to move the naming discussion to the PR review?

WardBrian commented 1 year ago

Can you add examples of how the names for these would look like? It would be good to recognize complex and tuples even if there would not be support at the moment

Complex variables use .real and .imag as their CSV names in Stan. So something like a complex_vector[2] foo has names foo.1.real,foo.1.imag,foo.2.real,foo.2.imag

Tuples use : to separate "slots" in a tuple. So a tuple(real, int) a is printed as a:1,a:2. This stacks naturally with the . for arrays of tuples/tuples of arrays.

If you want some really nasty edge cases, you can see some of the test cases of my stanio Python package