Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
R Package Structure
Add calc_birdflow_mc()
  • Loading branch information
ethanplunkett committed Dec 19, 2023
0 parents commit 8b37a3a
Show file tree
Hide file tree
Showing 14 changed files with 347 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
^.*\.Rproj$
^\.Rproj\.user$
^README\.Rmd$
17 changes: 17 additions & 0 deletions .gitattributes
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
48 changes: 48 additions & 0 deletions .gitignore
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

21 changes: 21 additions & 0 deletions BirdFlowExtras.Rproj
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
35 changes: 35 additions & 0 deletions DESCRIPTION
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
21 changes: 21 additions & 0 deletions LICENSE.md
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.
4 changes: 4 additions & 0 deletions NAMESPACE
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)
51 changes: 51 additions & 0 deletions R/calc_birdflow_mc.R
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)
}
46 changes: 46 additions & 0 deletions README.Rmd
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")
```

34 changes: 34 additions & 0 deletions README.md
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
```
7 changes: 7 additions & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
BirdFlow
BirdFlowR
MigConnectivity
timestep
calc
birdflow
mc
45 changes: 45 additions & 0 deletions man/calc_birdflow_mc.Rd

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

3 changes: 3 additions & 0 deletions tests/spelling.R
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)
12 changes: 12 additions & 0 deletions tests/testthat.R
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")

0 comments on commit 8b37a3a

Please sign in to comment.