Skip to content

Commit

Permalink
prepare for rgd,add python dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
SpatLyu committed Jun 7, 2024
1 parent ed50e78 commit ba0d57d
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 57 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Imports:
pander,
parallel,
purrr,
reticulate,
stats,
tibble,
tidyr,
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export(ecological_detector)
export(factor_detector)
export(gd)
export(gd_bestunidisc)
export(install_rpt)
export(interaction_detector)
export(opgd)
export(risk_detector)
Expand Down Expand Up @@ -39,6 +40,7 @@ importFrom(purrr,map2_dfr)
importFrom(purrr,map_dfr)
importFrom(purrr,pmap_dfc)
importFrom(purrr,set_names)
importFrom(reticulate,py_install)
importFrom(stats,as.formula)
importFrom(stats,pf)
importFrom(stats,t.test)
Expand Down
4 changes: 1 addition & 3 deletions R/discretization.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ st_unidisc = \(x,k,method = "quantile",factor = FALSE,...){
#' object.
#' @param return_disc (optional) Whether or not return discretized result used the optimal parameter.
#' Default is `TRUE`.
#' @param seed (optional) Random number seed, default is `12345678`
#' @param ... (optional) Other arguments passed to `st_unidisc()`.
#'
#' @return A list with the optimal parameter in the provided parameter combination with `k`,
Expand Down Expand Up @@ -71,8 +70,7 @@ st_unidisc = \(x,k,method = "quantile",factor = FALSE,...){
#' gd(fvc ~ .,data = new.fvc,type = 'interaction')
#' }
gd_bestunidisc = \(formula,data,discnum = NULL,discmethod = NULL,
cores = 1,return_disc = TRUE,seed = 12345678,...){
set.seed(seed)
cores = 1,return_disc = TRUE,...){
doclust = FALSE
if (inherits(cores, "cluster")) {
doclust = TRUE
Expand Down
23 changes: 23 additions & 0 deletions R/rpt_py.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#' @title set up `gdverse` python dependence
#' @author Wenbo Lv \email{[email protected]}
#' @description
#' Create an isolated Python virtual environment with all `gdverse` dependencies.
#'
#'
#' @param py_env The name, or full path, of the environment in which Python packages are to be installed.
#' @param py_version (optional) The requested Python version.Default is 3.9.
#' @param method (optional) Installation method. By default, "auto" automatically finds a method that will
#' work in the local environment.
#' @param ... Other parameers passed to `reticulate::py_install()`
#'
#' @importFrom reticulate py_install
#' @export
#'
install_rpt = \(py_env = "r-gdverse",
py_version = 3.9,
method = "auto", ...) {
reticulate::py_install(packages = c("numpy","pandas","ruptures"),
envname = py_env,
method = method,
python_version = py_version,...)
}
5 changes: 5 additions & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
rpt = NULL

.onLoad = function(libname, pkgname) {
rpt <<- reticulate::import("ruptures", delay_load = TRUE)
}
1 change: 1 addition & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ head(fvc)
```

```{r}
set.seed(12345678)
tictoc::tic()
fvc_gd = opgd(fvc ~ .,data = fvc,
discvar = names(select(fvc,-c(fvc,lulc))),
Expand Down
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ head(fvc)
```

``` r
set.seed(12345678)
tictoc::tic()
fvc_gd = opgd(fvc ~ .,data = fvc,
discvar = names(select(fvc,-c(fvc,lulc))),
cores = 6, type = 'factor')
tictoc::toc()
## 14.52 sec elapsed
## 14.33 sec elapsed
```

``` r
Expand All @@ -69,15 +70,15 @@ fvc_gd

| variable | Q-statistic | P-value |
|:--------:|:-----------:|:---------:|
| presum | 0.642 | 8.298e-10 |
| presum | 0.6402 | 6.669e-10 |
| lulc | 0.5533 | 9.106e-10 |
| premin | 0.443 | 8.559e-10 |
| tmpmin | 0.4058 | 4.982e-10 |
| premin | 0.4433 | 6.004e-10 |
| tmpmin | 0.4065 | 4.706e-10 |
| tmpmax | 0.2284 | 5.111e-10 |
| elev | 0.209 | 1.5e-10 |
| tmpavg | 0.2009 | 7.843e-10 |
| slope | 0.1942 | 5.872e-10 |
| tmpavg | 0.197 | 6.833e-10 |
| slope | 0.1937 | 8.865e-10 |
| pop | 0.1856 | 3.221e-10 |
| premax | 0.1324 | 2.448e-10 |
| ntl | 0.02171 | 6.162e-10 |
| ntl | 0.02125 | 6.277e-10 |
| aspect | 0.00741 | 5.448e-10 |
25 changes: 25 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,38 @@ jobs:

- name: Set up R
uses: r-lib/actions/setup-r@v2
with:
r-version: release

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: rcmdcheck reticulate

- uses: actions/setup-python@v4
with:
python-version: "3.x"

- name: setup r-gdverse venv
shell: Rscript {0}
run: |
path_to_python = reticulate::virtualenv_create(
envname = "r-gdverse",
python = Sys.which("python"),
packages = c("numpy","pandas","ruptures")
)
writeLines(sprintf("RETICULATE_PYTHON=%s", path_to_python),
Sys.getenv("GITHUB_ENV"))
- name: Install dependencies
run: |
install.packages('remotes')
remotes::install_deps(dependencies = TRUE)
remotes::install_cran('pkgdown')
- uses: r-lib/actions/check-r-package@v2

- name: Build site
run: |
Rscript -e 'pkgdown::build_site()'
Expand Down
3 changes: 0 additions & 3 deletions man/gd_bestunidisc.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions man/install_rpt.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

90 changes: 46 additions & 44 deletions vignettes/OPGD.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ We can use `gd_bestunidisc` to discretize them based on geodetector q-statistic.

``` r
tictoc::tic()
set.seed(12345678)
g = gd_bestunidisc(fvc ~ .,data = select(fvc,-lulc),discnum = 2:15,cores = 6)
tictoc::toc()
## 14.83 sec elapsed
## 15.33 sec elapsed
```


Expand All @@ -75,26 +76,26 @@ g
## [10] "tmpmax" "tmpmin"
##
## $k
## [1] 15 15 15 15 15 14 15 13 15 15 15
## [1] 15 15 15 15 13 13 14 13 15 15 15
##
## $method
## [1] "equal" "quantile" "fisher" "quantile" "fisher" "fisher" "fisher"
## [8] "fisher" "quantile" "quantile" "fisher"
## [8] "fisher" "fisher" "quantile" "fisher"
##
## $disv
## # A tibble: 136,243 × 11
## aspect elev ntl pop premax premin presum slope tmpavg tmpmax tmpmin
## <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
## 1 8 12 3 1 7 1 1 4 4 8 2
## 2 8 12 2 1 7 1 1 5 4 8 2
## 3 6 12 2 1 7 1 1 6 4 9 2
## 4 5 12 2 1 7 1 1 4 5 10 2
## 5 5 13 3 4 8 2 1 3 4 8 2
## 6 5 12 3 3 7 2 1 4 4 8 2
## 7 6 12 2 1 7 1 1 6 4 9 2
## 8 7 12 2 1 7 1 1 5 5 10 2
## 9 8 12 2 1 7 1 1 3 5 10 2
## 10 7 12 3 1 7 1 1 4 5 10 2
## 1 8 12 3 1 6 1 1 4 6 8 2
## 2 8 12 2 1 6 1 1 5 6 8 2
## 3 6 12 2 1 5 1 1 6 6 9 2
## 4 5 12 2 1 5 1 1 4 7 10 2
## 5 5 13 3 4 6 2 1 3 6 8 2
## 6 5 12 3 3 6 2 1 4 6 8 2
## 7 6 12 2 1 6 1 1 6 6 9 2
## 8 7 12 2 1 5 1 1 5 7 10 2
## 9 8 12 2 1 5 1 1 3 7 10 2
## 10 7 12 3 1 5 1 1 4 7 10 2
## # ℹ 136,233 more rows
```

Expand All @@ -104,16 +105,16 @@ new.fvc
## # A tibble: 136,243 × 11
## aspect elev ntl pop premax premin presum slope tmpavg tmpmax tmpmin
## <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
## 1 8 12 3 1 7 1 1 4 4 8 2
## 2 8 12 2 1 7 1 1 5 4 8 2
## 3 6 12 2 1 7 1 1 6 4 9 2
## 4 5 12 2 1 7 1 1 4 5 10 2
## 5 5 13 3 4 8 2 1 3 4 8 2
## 6 5 12 3 3 7 2 1 4 4 8 2
## 7 6 12 2 1 7 1 1 6 4 9 2
## 8 7 12 2 1 7 1 1 5 5 10 2
## 9 8 12 2 1 7 1 1 3 5 10 2
## 10 7 12 3 1 7 1 1 4 5 10 2
## 1 8 12 3 1 6 1 1 4 6 8 2
## 2 8 12 2 1 6 1 1 5 6 8 2
## 3 6 12 2 1 5 1 1 6 6 9 2
## 4 5 12 2 1 5 1 1 4 7 10 2
## 5 5 13 3 4 6 2 1 3 6 8 2
## 6 5 12 3 3 6 2 1 4 6 8 2
## 7 6 12 2 1 6 1 1 6 6 9 2
## 8 7 12 2 1 5 1 1 5 7 10 2
## 9 8 12 2 1 5 1 1 3 7 10 2
## 10 7 12 3 1 5 1 1 4 7 10 2
## # ℹ 136,233 more rows
```

Expand All @@ -126,16 +127,16 @@ new.fvc
## # A tibble: 136,243 × 13
## fvc lulc aspect elev ntl pop premax premin presum slope tmpavg tmpmax tmpmin
## <dbl> <dbl> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
## 1 0.198 10 8 12 3 1 7 1 1 4 4 8 2
## 2 0.193 10 8 12 2 1 7 1 1 5 4 8 2
## 3 0.192 10 6 12 2 1 7 1 1 6 4 9 2
## 4 0.189 10 5 12 2 1 7 1 1 4 5 10 2
## 5 0.208 10 5 13 3 4 8 2 1 3 4 8 2
## 6 0.196 10 5 12 3 3 7 2 1 4 4 8 2
## 7 0.191 10 6 12 2 1 7 1 1 6 4 9 2
## 8 0.185 10 7 12 2 1 7 1 1 5 5 10 2
## 9 0.174 10 8 12 2 1 7 1 1 3 5 10 2
## 10 0.166 10 7 12 3 1 7 1 1 4 5 10 2
## 1 0.198 10 8 12 3 1 6 1 1 4 6 8 2
## 2 0.193 10 8 12 2 1 6 1 1 5 6 8 2
## 3 0.192 10 6 12 2 1 5 1 1 6 6 9 2
## 4 0.189 10 5 12 2 1 5 1 1 4 7 10 2
## 5 0.208 10 5 13 3 4 6 2 1 3 6 8 2
## 6 0.196 10 5 12 3 3 6 2 1 4 6 8 2
## 7 0.191 10 6 12 2 1 6 1 1 6 6 9 2
## 8 0.185 10 7 12 2 1 5 1 1 5 7 10 2
## 9 0.174 10 8 12 2 1 5 1 1 3 7 10 2
## 10 0.166 10 7 12 3 1 5 1 1 4 7 10 2
## # ℹ 136,233 more rows
```

Expand All @@ -155,25 +156,25 @@ gd(fvc ~ .,data = new.fvc,type = 'factor')
------------------------------------
variable Q-statistic P-value
---------- ------------- -----------
presum 0.642 8.298e-10
presum 0.6402 6.669e-10

lulc 0.5533 9.106e-10

premin 0.4422 6.921e-10
premin 0.443 8.559e-10

tmpmin 0.4065 4.706e-10
tmpmin 0.4058 4.982e-10

tmpmax 0.2284 5.111e-10

elev 0.209 1.5e-10

tmpavg 0.197 6.833e-10
tmpavg 0.2009 7.843e-10

slope 0.1942 5.872e-10

pop 0.1856 3.221e-10

premax 0.1333 1.891e-10
premax 0.1328 1.735e-10

ntl 0.02171 6.162e-10

Expand Down Expand Up @@ -332,6 +333,7 @@ gd(fvc ~ .,data = new.fvc,type = 'interaction')


``` r
set.seed(12345678)
fvc_gd = opgd(fvc ~ .,data = fvc,
discvar = names(select(fvc,-c(fvc,lulc))),
cores = 6, type = c('factor','interaction'))
Expand All @@ -340,16 +342,16 @@ str(fvc_gd)
## $ :List of 1
## ..$ factor: tibble [12 × 3] (S3: tbl_df/tbl/data.frame)
## .. ..$ variable : chr [1:12] "presum" "lulc" "premin" "tmpmin" ...
## .. ..$ Q-statistic: num [1:12] 0.642 0.553 0.443 0.406 0.228 ...
## .. ..$ P-value : num [1:12] 8.30e-10 9.11e-10 8.56e-10 4.71e-10 5.11e-10 ...
## .. ..$ Q-statistic: num [1:12] 0.64 0.553 0.443 0.406 0.228 ...
## .. ..$ P-value : num [1:12] 6.67e-10 9.11e-10 8.56e-10 4.71e-10 5.11e-10 ...
## ..- attr(*, "class")= chr "factor_detector"
## $ :List of 1
## ..$ interaction: tibble [66 × 6] (S3: tbl_df/tbl/data.frame)
## .. ..$ variable1 : chr [1:66] "lulc" "lulc" "lulc" "lulc" ...
## .. ..$ variable2 : chr [1:66] "aspect" "elev" "ntl" "pop" ...
## .. ..$ Interaction : chr [1:66] "Enhance, nonlinear" "Enhance, bi-" "Enhance, nonlinear" "Enhance, bi-" ...
## .. ..$ Variable1 Q-statistics : num [1:66] 0.553 0.553 0.553 0.553 0.553 ...
## .. ..$ Variable2 Q-statistics : num [1:66] 0.00741 0.20903 0.02171 0.18561 0.13244 ...
## .. ..$ Variable2 Q-statistics : num [1:66] 0.00741 0.20903 0.02171 0.18561 0.1333 ...
## .. ..$ Variable1 and Variable2 interact Q-statistics: num [1:66] 0.563 0.739 0.611 0.636 0.677 ...
## ..- attr(*, "class")= chr "interaction_detector"
```
Expand All @@ -365,7 +367,7 @@ fvc_gd[[1]]
------------------------------------
variable Q-statistic P-value
---------- ------------- -----------
presum 0.642 8.298e-10
presum 0.6402 6.669e-10

lulc 0.5533 9.106e-10

Expand All @@ -379,11 +381,11 @@ fvc_gd[[1]]

tmpavg 0.197 6.833e-10

slope 0.1942 5.872e-10
slope 0.1938 5.584e-10

pop 0.1856 3.221e-10

premax 0.1324 2.448e-10
premax 0.1333 1.891e-10

ntl 0.02171 6.162e-10

Expand Down
Loading

0 comments on commit ba0d57d

Please sign in to comment.