-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
R Package Structure Add calc_birdflow_mc()
- Loading branch information
0 parents
commit 8b37a3a
Showing
14 changed files
with
347 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
^.*\.Rproj$ | ||
^\.Rproj\.user$ | ||
^README\.Rmd$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Basic .gitattributes for a R repo. | ||
# From: https://github.com/alexkaratarakis/gitattributes/blob/master/R.gitattributes | ||
# Modified slightly | ||
|
||
# Source files | ||
# ============ | ||
*.Rdata binary | ||
*.RData binary | ||
*.rda binary | ||
*.rdb binary | ||
*.rds binary | ||
*.Rd text | ||
*.Rdx binary | ||
*.Rmd text | ||
*.md text | ||
*.R text | ||
*.Rproj text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# History files | ||
.Rhistory | ||
.Rapp.history | ||
|
||
# Session Data files | ||
.RData | ||
|
||
# User-specific files | ||
.Ruserdata | ||
|
||
# Example code in package build process | ||
*-Ex.R | ||
|
||
# Output files from R CMD build | ||
/*.tar.gz | ||
|
||
# Output files from R CMD check | ||
/*.Rcheck/ | ||
|
||
# RStudio files | ||
.Rproj.user/ | ||
|
||
# produced vignettes | ||
vignettes/*.html | ||
vignettes/*.pdf | ||
|
||
# OAuth2 token, see https://github.com/hadley/httr/releases/tag/v0.3 | ||
.httr-oauth | ||
|
||
# knitr and R markdown default cache directories | ||
*_cache/ | ||
/cache/ | ||
|
||
# Temporary files created by R markdown | ||
*.utf8.md | ||
*.knit.md | ||
|
||
# R Environment Variables | ||
.Renviron | ||
.Rproj.user | ||
inst/doc | ||
/doc/ | ||
/Meta/ | ||
docs | ||
|
||
# Annoying Rplot.pdf file that gets created while testing | ||
/tests/testthat/Rplots.pdf | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
Version: 1.0 | ||
|
||
RestoreWorkspace: Default | ||
SaveWorkspace: Default | ||
AlwaysSaveHistory: Default | ||
|
||
EnableCodeIndexing: Yes | ||
UseSpacesForTab: Yes | ||
NumSpacesForTab: 2 | ||
Encoding: UTF-8 | ||
|
||
RnwWeave: Sweave | ||
LaTeX: pdfLaTeX | ||
|
||
AutoAppendNewline: Yes | ||
StripTrailingWhitespace: Yes | ||
|
||
BuildType: Package | ||
PackageUseDevtools: Yes | ||
PackageInstallArgs: --no-multiarch --with-keep.source | ||
PackageRoxygenize: rd,collate,namespace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
Package: BirdFlowExtras | ||
Type: Package | ||
Title: Extend BirdFlowR migration model functionality | ||
Version: 0.0.0.9001 | ||
Authors@R: | ||
person("Ethan", "Plunkett", email = "[email protected]", role = c("aut", "cre"), | ||
comment = c(ORCID = "0000-0003-4405-2251")) | ||
Description: This includes functions that rely on BirdFlowR and a BirdFlow | ||
model. They are in this second repository either because they are only | ||
likely to be useful to a subset of BirdFlowR users, or they depend on | ||
packages that are not on CRAN - in which case including them in BirdFlowR | ||
would make it CRAN incompatible. | ||
The initial motivation for a separate package was the calc_birdflow_mc() | ||
function, but others will likely be added over time. | ||
License: MIT + file | ||
Encoding: UTF-8 | ||
Roxygen: list(markdown = TRUE) | ||
RoxygenNote: 7.2.3 | ||
Suggests: | ||
BirdFlowModels, | ||
spelling, | ||
testthat (>= 3.0.0) | ||
Imports: | ||
BirdFlowR, | ||
MigConnectivity | ||
Remotes: | ||
birdflow-science/BirdFlowModels, | ||
birdflow-science/BirdFlowR, | ||
SMBC-NZP/MigConnectivity | ||
URL: https://birdflow-science.github.io/BirdFlowExtras/, https://github.com/birdflow-science/BirdFlowExtras | ||
BugReports: https://github.com/birdflow-science/BirdFlowExtras/issue | ||
Language: en-US | ||
Depends: | ||
R (>= 3.5) | ||
Config/testthat/edition: 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# MIT License | ||
|
||
Copyright (c) 2022 BirdFlowR authors | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Generated by roxygen2: do not edit by hand | ||
|
||
export(calc_birdflow_mc) | ||
import(BirdFlowR) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#' Function to calculate migratory connectivity (MC) on a BirdFlow object | ||
#' | ||
#' This function calculates migratory connectivity based on the transitions | ||
#' in a BirdFlowR model using the raster cells as regions. | ||
#' It relies on `MigConnectivity::calcMC()` to do the bulk of the work. | ||
#' | ||
#' @param bf A BirdFlow model | ||
#' @inheritDotParams BirdFlowR::lookup_timestep_sequence -x | ||
#' @return The migratory connectivity of the BirdFlow model over the time period | ||
#' indicated by `...` | ||
#' @export | ||
#' @import BirdFlowR | ||
#' @examples | ||
#' bf <- BirdFlowModels::amewoo | ||
#' mc <- calc_birdflow_mc(bf, season = "prebreeding") | ||
#' print(mc) | ||
#' | ||
calc_birdflow_mc <- function(bf, ...) { | ||
|
||
# Figure out time | ||
ts <- lookup_timestep_sequence(bf, ...) | ||
origin_t <- ts[1] | ||
target_t <- ts[length(ts)] | ||
|
||
# Dynamic masks | ||
origin_dm <- get_dynamic_mask(bf, origin_t) # origin dynamic cells | ||
target_dm <- get_dynamic_mask(bf, target_t) # target dynamic cells | ||
|
||
# distance matricies | ||
dist <- great_circle_distances(bf) # all active cells | ||
origin_dist <- dist[origin_dm, origin_dm] # origin dynamic | ||
target_dist <- dist[target_dm, target_dm] # target dynamic | ||
|
||
# Origin relative abundance | ||
origin_abun <- get_distr(bf, origin_t)[origin_dm] | ||
|
||
# Transition probabilities | ||
psi <- t(combine_transitions(bf, ...)) | ||
|
||
# Double check dimensions | ||
stopifnot(isTRUE(all.equal(nrow(psi), sum(origin_dm)))) | ||
stopifnot(isTRUE(all.equal(ncol(psi), sum(target_dm)))) | ||
|
||
# Calculate MC | ||
mc <- MigConnectivity::calcMC(originDist = origin_dist, | ||
targetDist = target_dist, | ||
originRelAbund = origin_abun, | ||
psi = psi) | ||
|
||
return(mc) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
--- | ||
output: github_document | ||
--- | ||
|
||
<!-- README.md is generated from README.Rmd. Please edit that file --> | ||
|
||
```{r, include = FALSE} | ||
knitr::opts_chunk$set( | ||
collapse = TRUE, | ||
comment = "#>", | ||
fig.path = "man/figures/README-", | ||
out.width = "100%" | ||
) | ||
``` | ||
|
||
# BirdFlowExtras | ||
|
||
<!-- badges: start --> | ||
<!-- badges: end --> | ||
|
||
**BirdFlowExtras** extends **BirdFlowR** with functions that are anticipated to | ||
be used by fewer users than the core functions; or that rely on packages that | ||
are not on CRAN or Bioconductor and thus cannot be used by packages on CRAN. | ||
|
||
## Installation | ||
|
||
You can install the development version of BirdFlowExtras like so: | ||
|
||
```r | ||
if(!require("remotes")) | ||
install.packages("remotes") | ||
remotes::install_github("birdflow-science/BirdFlowR") | ||
``` | ||
|
||
## Example | ||
|
||
Calculates Migratory Connectivity (MC) for a BirdFlowR model. This relies | ||
on the **MigConnectivity** package to do most of the work. | ||
|
||
```{r example} | ||
library(BirdFlowExtras) | ||
bf <- BirdFlowModels::amewoo | ||
calc_birdflow_mc(bf, season = "postbreeding") | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
|
||
<!-- README.md is generated from README.Rmd. Please edit that file --> | ||
|
||
# BirdFlowExtras | ||
|
||
<!-- badges: start --> | ||
<!-- badges: end --> | ||
|
||
**BirdFlowExtras** extends **BirdFlowR** with functions that are | ||
anticipated to be used by fewer users than the core functions; or that | ||
rely on packages that are not on CRAN or Bioconductor and thus cannot be | ||
used by packages on CRAN. | ||
|
||
## Installation | ||
|
||
You can install the development version of BirdFlowExtras like so: | ||
|
||
``` r | ||
if(!require("remotes")) | ||
install.packages("remotes") | ||
remotes::install_github("birdflow-science/BirdFlowR") | ||
``` | ||
|
||
## Example | ||
|
||
Calculates Migratory Connectivity (MC) for a BirdFlowR model. This | ||
relies on the **MigConnectivity** package to do most of the work. | ||
|
||
``` r | ||
library(BirdFlowExtras) | ||
bf <- BirdFlowModels::amewoo | ||
calc_birdflow_mc(bf, season = "postbreeding") | ||
#> [1] 0.2690275 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
BirdFlow | ||
BirdFlowR | ||
MigConnectivity | ||
timestep | ||
calc | ||
birdflow | ||
mc |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
if (requireNamespace("spelling", quietly = TRUE)) | ||
spelling::spell_check_test(vignettes = TRUE, error = FALSE, | ||
skip_on_cran = TRUE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# This file is part of the standard setup for testthat. | ||
# It is recommended that you do not modify it. | ||
# | ||
# Where should you do additional test configuration? | ||
# Learn more about the roles of various files in: | ||
# * https://r-pkgs.org/testing-design.html#sec-tests-files-overview | ||
# * https://testthat.r-lib.org/articles/special-files.html | ||
|
||
library(testthat) | ||
library(BirdFlowExtras) | ||
|
||
test_check("BirdFlowExtras") |