Skip to content

Instantly share code, notes, and snippets.

View FlukeAndFeather's full-sized avatar

Max Czapanskiy FlukeAndFeather

View GitHub Profile
@FlukeAndFeather
FlukeAndFeather / isolated.md
Last active July 30, 2019 03:29
removing isolated cells from raster
library(raster)
#> Loading required package: sp
# Create a random raster
set.seed(10)
foo <- raster(matrix(round(runif(100)), 10, 10))
# Add up neighbors
# NOTE: won't do anything about cells on the raster edge
foo_focal <- focal(foo, w = matrix(1, 3, 3), fun = sum)
# Set an isolation threshold
@FlukeAndFeather
FlukeAndFeather / find_0cross
Last active October 3, 2019 22:13
R function for finding zero-crossing values in a vector. In the case of exact zeros, carries the previous sign forward. I.e. [+ + 0 -] has a zero-cross at position 4, but [+ + 0 +] has no zero-crosses.
find0cross <- function(x) {
# x must be numeric
stopifnot(is.numeric(x))
# Length 0 and 1 can't have zero crossings
if (length(x) < 2) {
return(rep(FALSE, length(x)))
}
# Recursively find zero-crossing values
# Takes a grayscale tiff (with alpha) and converts all dark points (by threshold) to the new color
colorize_tiff <- function(input, threshold, newcolor) {
# Read original tiff
orig_tiff <- tiff::readTIFF(input)
# Convert grayscale to black and white
orig_tiff[, , 1] <- ifelse(orig_tiff[, , 1] > threshold, 1, 0)
# Create a colorized tiff
new_tiff <- array(0, dim = c(dim(orig_tiff)[1:2], 4))
# Set RGB
@FlukeAndFeather
FlukeAndFeather / sonar_supplement.Rmd
Last active February 22, 2021 23:24
Supporting Information for "Large baleen and small toothed whales face greatest energetic consequences from sonar disturbance"
---
title: "Supporting Information"
subtitle: "Modeling short-term energetic costs of sonar disturbance to cetaceans using high resolution foraging data"
author:
- "Max F. Czapanskiy"
- "Matthew S. Savoca"
- "William T. Gough"
- "Paolo S. Segre"
- "Danuta M. Wisniewska"
- "David E. Cade"
@FlukeAndFeather
FlukeAndFeather / col_summ.md
Created March 21, 2020 17:34
Add a row at the end of a data frame with summaries for numeric colums
library(tidyverse)
set.seed(1024)
d <- tibble(
  a = letters[1:5],
  b = LETTERS[6:10],
  x = 1:5,
  y = (1:5)^2,
  z = runif(5)
)
@FlukeAndFeather
FlukeAndFeather / prey_field.R
Created April 2, 2020 21:27
Simulate prey fields with a spectral power law of spatial frequency to the –1.5
library(raster)
library(scales)
library(tidyverse)
powpow <- function(n, a, b) {
# based on answers at: https://dsp.stackexchange.com/questions/47640/generating-a-timeseries-with-an-arbitrary-power-spectrum
# Thank you Sam!
if (n %% 2 != 0)
@FlukeAndFeather
FlukeAndFeather / partial_italic.R
Created February 22, 2021 03:49
Partially italicize ggplot tick labels
library(tidyverse)
# Start with some dummy data
whale_data <- tribble(
~species, ~prey, ~order,
"B. musculus", "krill", 5,
"B. physalus", "fish", 3,
"B. physalus", "krill", 4,
"M. novaeangliae", "fish", 1,
"M. novaeangliae", "krill", 2
@FlukeAndFeather
FlukeAndFeather / pointsinmussels.R
Created June 30, 2021 18:04
How unlikely was it to randomly generate 24/25 points in mussels in a quadrat photo?
library(ggrepel)
library(tidyverse)
# These were my measurements for mussel area and quadrat area respectively
musselfrac <- 0.043 / 0.097
# dbinom() is the probability of n successes out of x trials with probability p
# So dbinom(0:25, 25, musselfrac) is the chance of: [0 points in mussels, 1
# point in mussels, 2 points in mussels, ..., 25 points in mussels]
musselprobs <- tibble(n = 0:25,
p = dbinom(0:25, 25, musselfrac))
# This is the probability of 24/25 points in mussels
@FlukeAndFeather
FlukeAndFeather / pred_mr.R
Last active February 24, 2025 23:27
Predict mammalian metabolic rates from various scaling equations.
pred_mr <- function(
mass_kg,
mr_method = c("kleiber", "kolokotronesetal", "nagy", "savageetal", "whiteseymour"),
unit = c("kcal_day", "kJ_day", "mlO2_hr", "W"),
multiplier = 1
) {
#' Predicted metabolic rate
#'
#' Predict mammalian metabolic rates from various scaling equations.
#'
@FlukeAndFeather
FlukeAndFeather / relative_wind.R
Created August 3, 2021 02:18
Given a flight trajectory [(t, x, y)] and co-located wind vectors [(u, v)], calculate the relative angle between flight and wind along the trajectory
library(dplyr)
library(geosphere)
library(glue)
library(ggplot2)
library(mapproj)
# Example data frame
set.seed(1)
tracks <- tibble(
time = 1:5,