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

can't use the aggregate function #731

Open
khalilT opened this issue Jan 22, 2025 · 13 comments
Open

can't use the aggregate function #731

khalilT opened this issue Jan 22, 2025 · 13 comments

Comments

@khalilT
Copy link

khalilT commented Jan 22, 2025

Hello,
I can't call the aggregate function from the stars package, although I can see it in the documentation and methods.

> methods(class = "stars")

 [1] [                 [[<-              [<-               %in%              $<-               adrop             aggregate         aperm            
 [9] as.data.frame     as.POSIXct        c                 coerce            contour           cut               dim               dimnames         
[17] dimnames<-        droplevels        expand_dimensions hist              image             initialize        is.na             Math             
[25] merge             Ops               plot              prcomp            predict           print             show              slotsFromS3      
[33] split             st_apply          st_area           st_as_sf          st_as_sfc         st_as_stars       st_bbox           st_coordinates   
[41] st_crop           st_crs            st_crs<-          st_dimensions     st_dimensions<-   st_downsample     st_extract        st_geometry      
[49] st_geotransform   st_geotransform<- st_interpolate_aw st_intersects     st_join           st_mosaic         st_normalize      st_redimension   
[57] st_rotate         st_sample         st_set_bbox       st_transform      st_write          time              write_stars      
see '?methods' for accessing help and source code

I use R version 4.4.1, and I have both dependencies sf and terra

here is my sessioninfo:

Platform: x86_64-pc-linux-gnu
Running under: Ubuntu 24.04.1 LTS
@edzer
Copy link
Member

edzer commented Jan 22, 2025

That's not a sessionInfo().

> library(stars)
Loading required package: abind
Loading required package: sf
Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.4.0; sf_use_s2() is TRUE
> methods(class = "stars")
 [1] [                 [[<-              [<-               %in%             
 [5] $<-               adrop             aggregate         aperm            
 [9] as.data.frame     as.POSIXct        c                 coerce           
[13] contour           cut               dim               dimnames         
[17] dimnames<-        droplevels        expand_dimensions hist             
[21] image             initialize        is.na             Math             
[25] merge             Ops               plot              prcomp           
[29] predict           print             show              slotsFromS3      
[33] split             st_apply          st_area           st_as_sf         
[37] st_as_sfc         st_as_stars       st_bbox           st_coordinates   
[41] st_crop           st_crs            st_crs<-          st_dimensions    
[45] st_dimensions<-   st_downsample     st_extract        st_geometry      
[49] st_geotransform   st_geotransform<- st_interpolate_aw st_intersects    
[53] st_join           st_mosaic         st_normalize      st_redimension   
[57] st_rotate         st_sample         st_set_bbox       st_transform     
[61] st_write          time              write_stars      
see '?methods' for accessing help and source code
> sessionInfo()
R version 4.4.2 (2024-10-31)
Platform: x86_64-pc-linux-gnu
Running under: Ubuntu 24.04.1 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.26.so;  LAPACK version 3.12.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_GB.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_GB.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C       

time zone: Europe/Berlin
tzcode source: system (glibc)

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] stars_0.6-8 sf_1.0-20   abind_1.4-8

loaded via a namespace (and not attached):
 [1] e1071_1.7-16       magrittr_2.0.3     KernSmooth_2.23-26 parallel_4.4.2    
 [5] classInt_0.4-11    cli_3.6.3          grid_4.4.2         DBI_1.2.3         
 [9] proxy_0.4-27       class_7.3-23       compiler_4.4.2     tools_4.4.2       
[13] Rcpp_1.0.13-1      rlang_1.1.4        units_0.8-6       

@khalilT
Copy link
Author

khalilT commented Jan 22, 2025

I am really sorry I didn't notice the last part was truncated.
Here is everything again (also the message when loading stars)

Loading required package: abind
Loading required package: sf
Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.4.0; sf_use_s2() is TRUE
R version 4.4.1 (2024-06-14)
Platform: x86_64-pc-linux-gnu
Running under: Ubuntu 24.04.1 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.12.0 
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.12.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8   
 [6] LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

time zone: Europe/Berlin
tzcode source: system (glibc)

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] stars_0.6-8  sf_1.0-19    abind_1.4-8  terra_1.8-12

loaded via a namespace (and not attached):
 [1] codetools_0.2-20   e1071_1.7-16       magrittr_2.0.3     KernSmooth_2.23-24 parallel_4.4.1     classInt_0.4-11    cli_3.6.3         
 [8] grid_4.4.1         DBI_1.2.3          proxy_0.4-27       class_7.3-22       compiler_4.4.1     rstudioapi_0.16.0  tools_4.4.1       
[15] Rcpp_1.0.14        rlang_1.1.5        units_0.8-5  

@edzer
Copy link
Member

edzer commented Jan 22, 2025

Did you load library(stars)?

@khalilT
Copy link
Author

khalilT commented Jan 22, 2025

of course.
I can't call the function even this way stars::aggregate()

Image

@edzer
Copy link
Member

edzer commented Jan 22, 2025

Please give the full output, starting from a clean R session (no other packages loaded), when you run

library(stars)
methods(class = "stars")
sessionInfo()

@khalilT
Copy link
Author

khalilT commented Jan 22, 2025

sure. Here it is:


> library(stars)
Loading required package: abind
Loading required package: sf
Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.4.0; sf_use_s2() is TRUE
> methods(class = "stars")
 [1] [                 [[<-              [<-               %in%              $<-               adrop             aggregate         aperm            
 [9] as.data.frame     as.POSIXct        c                 coerce            contour           cut               dim               dimnames         
[17] dimnames<-        droplevels        expand_dimensions filter            hist              image             initialize        is.na            
[25] Math              merge             Ops               plot              prcomp            predict           print             show             
[33] slotsFromS3       split             st_apply          st_area           st_as_sf          st_as_sfc         st_as_stars       st_bbox          
[41] st_coordinates    st_crop           st_crs            st_crs<-          st_dimensions     st_dimensions<-   st_downsample     st_extract       
[49] st_geometry       st_geotransform   st_geotransform<- st_interpolate_aw st_intersects     st_join           st_mosaic         st_normalize     
[57] st_redimension    st_rotate         st_sample         st_set_bbox       st_transform      st_write          time              write_stars      
see '?methods' for accessing help and source code
> sessionInfo()
R version 4.4.1 (2024-06-14)
Platform: x86_64-pc-linux-gnu
Running under: Ubuntu 24.04.1 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.12.0 
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.12.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8   
 [6] LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

time zone: Europe/Berlin
tzcode source: system (glibc)

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] stars_0.6-8 sf_1.0-19   abind_1.4-8

loaded via a namespace (and not attached):
 [1] gtable_0.3.5       dplyr_1.1.4        compiler_4.4.1     tidyselect_1.2.1   Rcpp_1.0.14        parallel_4.4.1     gridExtra_2.3     
 [8] scales_1.3.0       ggplot2_3.5.1      R6_2.5.1           generics_0.1.3     classInt_0.4-11    MASS_7.3-61        polyclip_1.10-7   
[15] tibble_3.2.1       units_0.8-5        munsell_0.5.1      DBI_1.2.3          pillar_1.9.0       rlang_1.1.5        utf8_1.2.4        
[22] terra_1.8-12       cli_3.6.3          withr_3.0.1        magrittr_2.0.3     tweenr_2.0.3       class_7.3-22       grid_4.4.1        
[29] rstudioapi_0.16.0  ggforce_0.4.2      lifecycle_1.0.4    vctrs_0.6.5        KernSmooth_2.23-24 proxy_0.4-27       glue_1.7.0        
[36] farver_2.1.2       codetools_0.2-20   fansi_1.0.6        e1071_1.7-16       colorspace_2.1-1   tools_4.4.1        pkgconfig_2.0.3   ```

@edzer
Copy link
Member

edzer commented Jan 22, 2025

You have 41 packages loaded via namespace, I have 17, it's possible that the problem lies in that difference. I asked you to do this in a clean R session.

@khalilT
Copy link
Author

khalilT commented Jan 22, 2025

I restarted the session (ctl + shift + F10) but it was still loading when I copied the output.
I closed the project, and this is from a clean R session (also only 17 base packages are loaded, the problem is still there though)

Loading required package: abind
Loading required package: sf
Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.4.0; sf_use_s2() is TRUE
> methods(class = "stars")
 [1] [                 [[<-              [<-               %in%              $<-               adrop             aggregate         aperm            
 [9] as.data.frame     as.POSIXct        c                 coerce            contour           cut               dim               dimnames         
[17] dimnames<-        droplevels        expand_dimensions hist              image             initialize        is.na             Math             
[25] merge             Ops               plot              prcomp            predict           print             show              slotsFromS3      
[33] split             st_apply          st_area           st_as_sf          st_as_sfc         st_as_stars       st_bbox           st_coordinates   
[41] st_crop           st_crs            st_crs<-          st_dimensions     st_dimensions<-   st_downsample     st_extract        st_geometry      
[49] st_geotransform   st_geotransform<- st_interpolate_aw st_intersects     st_join           st_mosaic         st_normalize      st_redimension   
[57] st_rotate         st_sample         st_set_bbox       st_transform      st_write          time              write_stars      
see '?methods' for accessing help and source code
> sessionInfo()
R version 4.4.1 (2024-06-14)
Platform: x86_64-pc-linux-gnu
Running under: Ubuntu 24.04.1 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.12.0 
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.12.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8   
 [6] LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

time zone: Europe/Berlin
tzcode source: system (glibc)

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] stars_0.6-8 sf_1.0-19   abind_1.4-8

loaded via a namespace (and not attached):
 [1] e1071_1.7-16       magrittr_2.0.3     KernSmooth_2.23-24 parallel_4.4.1     classInt_0.4-11    cli_3.6.3          grid_4.4.1        
 [8] DBI_1.2.3          proxy_0.4-27       class_7.3-22       compiler_4.4.1     rstudioapi_0.16.0  tools_4.4.1        Rcpp_1.0.14       
[15] rlang_1.1.5        units_0.8-5  ```

@edzer
Copy link
Member

edzer commented Jan 22, 2025

the problem is still there though

it looks like it's gone.

@edzer
Copy link
Member

edzer commented Jan 22, 2025

It was already gone in #731 (comment) - we forgot to scroll right.

@khalilT
Copy link
Author

khalilT commented Jan 22, 2025

As I said, I see it when I call methods(class = "stars") and it is in the documentation, but I can't call it even with stars::aggregate().

@edzer
Copy link
Member

edzer commented Jan 22, 2025

It's normal that you can't call stars::aggregate() because

> stars::aggregate
Error: 'aggregate' is not an exported object from 'namespace:stars'
  • it's an exported method, the generic is in stats.

@khalilT
Copy link
Author

khalilT commented Jan 22, 2025

maybe I am missing something very obvious, but this function for example used to work 6 months ago or so, and it is based on the aggregate method for stars objects.

agg_by_refregion <- function(x,y,f) {
  #'@title agg_by_refregion
  #'
  #'@description applies a function f on a raster x cropped by a multi-polygon object y
  #'@description and returns the aggregated result for every subset x[y]
  #'
  #'@param x: multi-dim raster object of type stars
  #'@param y: multi-polygon vector object of type sf
  #'@param f: function to apply
  st_crs(x) <- st_crs(y)
  result_sf <- aggregate(x, y, f)
  result_df <- result_sf %>% as.data.frame()
  result_df_all <- left_join(result_df, y, by="geometry")
  names(result_df_all)[3] <- "fvalue"
  return(result_df_all)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants