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

head.greta_array returns wrong dimensions #384

Closed
njtierney opened this issue Apr 22, 2021 · 8 comments · Fixed by #409
Closed

head.greta_array returns wrong dimensions #384

njtierney opened this issue Apr 22, 2021 · 8 comments · Fixed by #409
Assignees
Milestone

Comments

@njtierney
Copy link
Collaborator

njtierney commented Apr 22, 2021

library(greta)
#> 
#> Attaching package: 'greta'
#> The following objects are masked from 'package:stats':
#> 
#>     binomial, cov2cor, poisson
#> The following objects are masked from 'package:base':
#> 
#>     %*%, apply, backsolve, beta, chol2inv, colMeans, colSums, diag,
#>     eigen, forwardsolve, gamma, identity, rowMeans, rowSums, sweep,
#>     tapply
# an array of random standard normals with the specificed dims
# e.g. randn(3, 2, 1)
randn <- function(...) {
  dim <- c(...)
  array(rnorm(prod(dim)), dim = dim)
}
rand_array <- randn(7, 2, 2)
rand_greta_array <- as_data(rand_array)
rand_array
#> , , 1
#> 
#>             [,1]       [,2]
#> [1,]  0.03177304  1.8284544
#> [2,]  0.81938056 -0.6474075
#> [3,] -0.64281612  0.3793273
#> [4,]  0.55142100 -0.1914557
#> [5,] -2.24318860  0.3394805
#> [6,] -1.64262209  2.0802298
#> [7,]  0.09018604  1.5560759
#> 
#> , , 2
#> 
#>              [,1]       [,2]
#> [1,]  2.388094172  1.5894255
#> [2,] -0.110582661  0.6256889
#> [3,] -1.134299181  0.9422529
#> [4,] -1.906239090 -0.3626022
#> [5,]  2.321062768  2.9050268
#> [6,]  0.008855675  0.6774489
#> [7,]  0.887511419  0.9159637
rand_greta_array
#> greta array (data)
#> 
#> , , 1
#> 
#>             [,1]       [,2]
#> [1,]  0.03177304  1.8284544
#> [2,]  0.81938056 -0.6474075
#> [3,] -0.64281612  0.3793273
#> [4,]  0.55142100 -0.1914557
#> [5,] -2.24318860  0.3394805
#> [6,] -1.64262209  2.0802298
#> [7,]  0.09018604  1.5560759
#> 
#> , , 2
#> 
#>              [,1]       [,2]
#> [1,]  2.388094172  1.5894255
#> [2,] -0.110582661  0.6256889
#> [3,] -1.134299181  0.9422529
#> [4,] -1.906239090 -0.3626022
#> [5,]  2.321062768  2.9050268
#> [6,]  0.008855675  0.6774489
#> [7,]  0.887511419  0.9159637

head(rand_array)
#> , , 1
#> 
#>             [,1]       [,2]
#> [1,]  0.03177304  1.8284544
#> [2,]  0.81938056 -0.6474075
#> [3,] -0.64281612  0.3793273
#> [4,]  0.55142100 -0.1914557
#> [5,] -2.24318860  0.3394805
#> [6,] -1.64262209  2.0802298
#> 
#> , , 2
#> 
#>              [,1]       [,2]
#> [1,]  2.388094172  1.5894255
#> [2,] -0.110582661  0.6256889
#> [3,] -1.134299181  0.9422529
#> [4,] -1.906239090 -0.3626022
#> [5,]  2.321062768  2.9050268
#> [6,]  0.008855675  0.6774489
head(rand_greta_array)
#> greta array (operation)
#> 
#>             [,1]
#> [1,]  0.03177304
#> [2,]  0.81938056
#> [3,] -0.64281612
#> [4,]  0.55142100
#> [5,] -2.24318860
#> [6,] -1.64262209

Created on 2021-05-04 by the reprex package (v2.0.0)

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 4.0.5 (2021-03-31)
#>  os       macOS Big Sur 10.16         
#>  system   x86_64, darwin17.0          
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_AU.UTF-8                 
#>  ctype    en_AU.UTF-8                 
#>  tz       Australia/Perth             
#>  date     2021-05-04                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version    date       lib source            
#>  backports     1.2.1      2020-12-09 [1] standard (@1.2.1) 
#>  base64enc     0.1-3      2015-07-28 [1] standard (@0.1-3) 
#>  cli           2.4.0      2021-04-05 [1] CRAN (R 4.0.2)    
#>  coda          0.19-4     2020-09-30 [1] CRAN (R 4.0.2)    
#>  codetools     0.2-18     2020-11-04 [1] CRAN (R 4.0.5)    
#>  crayon        1.4.1      2021-02-08 [1] CRAN (R 4.0.2)    
#>  digest        0.6.27     2020-10-24 [1] standard (@0.6.27)
#>  ellipsis      0.3.1      2020-05-15 [1] standard (@0.3.1) 
#>  evaluate      0.14       2019-05-28 [1] standard (@0.14)  
#>  fansi         0.4.2      2021-01-15 [1] CRAN (R 4.0.2)    
#>  fs            1.5.0      2020-07-31 [1] standard (@1.5.0) 
#>  future        1.21.0     2020-12-10 [1] CRAN (R 4.0.2)    
#>  globals       0.14.0     2020-11-22 [1] CRAN (R 4.0.2)    
#>  glue          1.4.2      2020-08-27 [1] standard (@1.4.2) 
#>  greta       * 0.3.1.9012 2021-04-27 [1] local             
#>  highr         0.8        2019-03-20 [1] standard (@0.8)   
#>  hms           1.0.0      2021-01-13 [1] CRAN (R 4.0.2)    
#>  htmltools     0.5.1.1    2021-01-22 [1] CRAN (R 4.0.2)    
#>  jsonlite      1.7.2      2020-12-09 [1] standard (@1.7.2) 
#>  knitr         1.31       2021-01-27 [1] CRAN (R 4.0.2)    
#>  lattice       0.20-41    2020-04-02 [1] CRAN (R 4.0.5)    
#>  lifecycle     1.0.0      2021-02-15 [1] CRAN (R 4.0.2)    
#>  listenv       0.8.0      2019-12-05 [1] CRAN (R 4.0.2)    
#>  magrittr      2.0.1      2020-11-17 [1] standard (@2.0.1) 
#>  Matrix        1.3-2      2021-01-06 [1] CRAN (R 4.0.5)    
#>  parallelly    1.24.0     2021-03-14 [1] CRAN (R 4.0.2)    
#>  pillar        1.6.0      2021-04-13 [1] CRAN (R 4.0.2)    
#>  pkgconfig     2.0.3      2019-09-22 [1] standard (@2.0.3) 
#>  prettyunits   1.1.1      2020-01-24 [1] standard (@1.1.1) 
#>  progress      1.2.2      2019-05-16 [1] CRAN (R 4.0.2)    
#>  purrr         0.3.4      2020-04-17 [1] standard (@0.3.4) 
#>  R6            2.5.0      2020-10-28 [1] standard (@2.5.0) 
#>  Rcpp          1.0.6      2021-01-15 [1] CRAN (R 4.0.2)    
#>  reprex        2.0.0      2021-04-02 [1] CRAN (R 4.0.2)    
#>  reticulate    1.19       2021-04-21 [1] CRAN (R 4.0.2)    
#>  rlang         0.4.10     2020-12-30 [1] CRAN (R 4.0.2)    
#>  rmarkdown     2.7        2021-02-19 [1] CRAN (R 4.0.2)    
#>  sessioninfo   1.1.1      2018-11-05 [1] standard (@1.1.1) 
#>  stringi       1.5.3      2020-09-09 [1] standard (@1.5.3) 
#>  stringr       1.4.0      2019-02-10 [1] standard (@1.4.0) 
#>  styler        1.4.1      2021-03-30 [1] CRAN (R 4.0.2)    
#>  tensorflow    2.4.0      2021-03-23 [1] CRAN (R 4.0.3)    
#>  tfruns        1.5.0      2021-02-26 [1] CRAN (R 4.0.2)    
#>  tibble        3.1.1      2021-04-18 [1] CRAN (R 4.0.3)    
#>  utf8          1.2.1      2021-03-12 [1] CRAN (R 4.0.2)    
#>  vctrs         0.3.7      2021-03-29 [1] CRAN (R 4.0.3)    
#>  whisker       0.4        2019-08-28 [1] standard (@0.4)   
#>  withr         2.4.2      2021-04-18 [1] CRAN (R 4.0.3)    
#>  xfun          0.22       2021-03-11 [1] CRAN (R 4.0.2)    
#>  yaml          2.2.1      2020-02-01 [1] standard (@2.2.1) 
#> 
#> [1] /Library/Frameworks/R.framework/Versions/4.0/Resources/library
related #383
@njtierney
Copy link
Collaborator Author

This seems like it might be due dispatch for matrix and arrays changing in R 4.0.0

SIGNIFICANT USER-VISIBLE CHANGES
...
matrix objects now also inherit from class "array", so e.g., class(diag(1)) is c("matrix", "array"). This invalidates code incorrectly assuming that class(matrix_obj)) has length one.

S3 methods for class "array" are now dispatched for matrix objects.

@njtierney

This comment has been minimized.

@njtierney
Copy link
Collaborator Author

njtierney commented May 4, 2021

and R 3.6.2

library(greta)
#> 
#> Attaching package: 'greta'
#> The following objects are masked from 'package:stats':
#> 
#>     binomial, cov2cor, poisson
#> The following objects are masked from 'package:base':
#> 
#>     %*%, apply, backsolve, beta, chol2inv, colMeans, colSums, diag,
#>     eigen, forwardsolve, gamma, identity, rowMeans, rowSums, sweep,
#>     tapply
# an array of random standard normals with the specificed dims
# e.g. randn(3, 2, 1)
randn <- function(...) {
  dim <- c(...)
  array(rnorm(prod(dim)), dim = dim)
}
rand_array <- randn(7, 2, 2)
rand_greta_array <- as_data(rand_array)
rand_array
#> , , 1
#> 
#>            [,1]       [,2]
#> [1,]  0.1114481 -0.3794806
#> [2,]  1.7881912  0.9542654
#> [3,] -0.7075102  0.3722603
#> [4,] -1.1301885 -0.8121186
#> [5,]  0.1774964  1.1779782
#> [6,] -2.1925452  0.4117449
#> [7,]  0.4064841 -0.6203245
#> 
#> , , 2
#> 
#>             [,1]        [,2]
#> [1,]  0.68897917 -0.08056499
#> [2,]  0.27028994 -0.02797147
#> [3,] -0.47672948 -2.55008918
#> [4,] -0.72843195 -0.45704610
#> [5,] -2.75690409 -1.58473127
#> [6,]  0.51755519  0.67108494
#> [7,] -0.03002811  0.78116162
rand_greta_array
#> greta array (data)
#> 
#> , , 1
#> 
#>            [,1]       [,2]
#> [1,]  0.1114481 -0.3794806
#> [2,]  1.7881912  0.9542654
#> [3,] -0.7075102  0.3722603
#> [4,] -1.1301885 -0.8121186
#> [5,]  0.1774964  1.1779782
#> [6,] -2.1925452  0.4117449
#> [7,]  0.4064841 -0.6203245
#> 
#> , , 2
#> 
#>             [,1]        [,2]
#> [1,]  0.68897917 -0.08056499
#> [2,]  0.27028994 -0.02797147
#> [3,] -0.47672948 -2.55008918
#> [4,] -0.72843195 -0.45704610
#> [5,] -2.75690409 -1.58473127
#> [6,]  0.51755519  0.67108494
#> [7,] -0.03002811  0.78116162

head(rand_array)
#> [1]  0.1114481  1.7881912 -0.7075102 -1.1301885  0.1774964 -2.1925452
head(rand_greta_array)
#> greta array (operation)
#> 
#>            [,1]
#> [1,]  0.1114481
#> [2,]  1.7881912
#> [3,] -0.7075102
#> [4,] -1.1301885
#> [5,]  0.1774964
#> [6,] -2.1925452

Created on 2021-05-04 by the reprex package (v0.3.0)

Session info
sessionInfo()
#> R version 3.6.2 (2019-12-12)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Debian GNU/Linux 10 (buster)
#> 
#> Matrix products: default
#> BLAS/LAPACK: /usr/lib/x86_64-linux-gnu/libopenblasp-r0.3.5.so
#> 
#> locale:
#>  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
#>  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
#>  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=C             
#>  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
#>  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
#> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] greta_0.3.1
#> 
#> loaded via a namespace (and not attached):
#>  [1] Rcpp_1.0.3        whisker_0.4       knitr_1.28        magrittr_1.5     
#>  [5] rappdirs_0.3.1    hms_0.5.3         progress_1.2.2    lattice_0.20-38  
#>  [9] R6_2.4.1          rlang_0.4.4       stringr_1.4.0     highr_0.8        
#> [13] globals_0.12.5    tools_3.6.2       parallel_3.6.2    grid_3.6.2       
#> [17] xfun_0.12         coda_0.19-3       tfruns_1.4        htmltools_0.4.0  
#> [21] yaml_2.2.1        digest_0.6.25     crayon_1.3.4      tensorflow_2.0.0 
#> [25] base64enc_0.1-3   vctrs_0.2.3       codetools_0.2-16  evaluate_0.14    
#> [29] rmarkdown_2.1     stringi_1.4.6     compiler_3.6.2    prettyunits_1.1.1
#> [33] jsonlite_1.6.1    reticulate_1.14   future_1.16.0     listenv_0.8.0    
#> [37] pkgconfig_2.0.3

@njtierney

This comment has been minimized.

@goldingn
Copy link
Member

goldingn commented May 4, 2021

Right, head is consistent between R and greta in 3.6.2 and 3.6.3 (probably everything <4.0). But the behaviour of head for arrays has changed in R after this point. Which is why your head(rand_array) in the top message is array-shaped, rather than pulling out a vector as it does in the subsequent versions.

greta's behaviour is consistent with the pre-R-4 way. So we need a function that will do either way, with a switch that decides which way to do it based on which version of R the user has.

@goldingn
Copy link
Member

goldingn commented May 4, 2021

oh, this is duplicated in / superseded by #388, right?

@njtierney
Copy link
Collaborator Author

Yeah it is duplicated, i'll migrate over the relevant material

@njtierney
Copy link
Collaborator Author

The getRVersion() function could be handy in this:

getRversion()
#> [1] '4.0.5'
class(getRversion())
#> [1] "R_system_version" "package_version"  "numeric_version"
getRversion() > "3.1.0"
#> [1] TRUE
getRversion() >= "3.1.0"
#> [1] TRUE
getRversion() <= "3.0.0"
#> [1] FALSE

Created on 2021-04-27 by the reprex package (v2.0.0)

njtierney added a commit to njtierney/greta that referenced this issue May 5, 2021
sets up initial logic for finding R version
@njtierney njtierney added this to the 0.3.2 milestone Jul 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants