Skip to content

Instantly share code, notes, and snippets.

View mfansler's full-sized avatar
⚙️

Mervin Fansler mfansler

⚙️
View GitHub Profile
## see https://stackoverflow.com/q/76039888/570918
diffmat <- function(drow_margin, dcol_margin, freeze=TRUE) {
nrow <- length(drow_margin)
ncol <- length(dcol_margin)
rmat <- matrix(drow_margin, nrow, ncol)
cmat <- matrix(dcol_margin, nrow, ncol, byrow=TRUE)
dmat <- 0.5*(rmat + cmat)
if (freeze) {
## keep satisfactory rows and columns fixed
@mfansler
mfansler / new-cran-pkg.sh
Last active April 20, 2024 22:20
Conda Forge `staged-recipes` utility scripts
#!/usr/bin/env bash -l
#' Usage: `bash -l new-cran-pkg.sh {r-package}`
#' Purpose: Generates a Conda Forge recipe using `conda_r_skeleton_helper` and
#' pushes this to a personal fork of `staged-recipes`.
#' Notes: Should be located in and run from a `staged-recipes` clone prefix.
#' `git remote -v` should show `origin` set to a personal fork
#' and `upstream` set to 'git@github.com:conda-forge/staged-recipes.git'.
#' CRAN package name should be prefixed with 'r-' and given in all lowercase.
set -xe -o pipefail
@mfansler
mfansler / list_r_pkg_linked_libs.sh
Created December 22, 2022 23:43
List all libraries a Conda Forge R library links against
#!/usr/bin/env bash -l
## PARAMS
## file should have one "[name]=[version]=[build]" per line
FILE_PKG_BUILDS="r-tiff.cf_pkgs.txt"
##
DYLIB_LOC="lib/R/library/tiff/libs"
tmp=$(mktemp -d)
@mfansler
mfansler / read_ad_df.R
Created August 26, 2022 17:02
Read anndata dataframes with pure R
library(rhdf5)
library(tidyverse)
read_ad_df <- function (file, name) {
x_attrs <- h5readAttributes(file, name)
## check requested entry is a dataframe
## TODO: do we need to check encoding-version?
stopifnot(x_attrs[['encoding-type']] == "dataframe")
@mfansler
mfansler / xargs_parallel_syntax.sh
Created July 22, 2022 16:38
Notes on xargs syntax for parallel execution
#!/bin/bash
## space-separated input
## -n1: run each separately
## -P6: run up to 6 in parallel
## -I '{}': replace {} with argument
## \$ delays evaluation to bash execution
## waits randomly for up to five seconds, then prints
echo {1..12} |\
xargs -n1 -P6 -I '{}' \
bash -c "sleep \$[ \$RANDOM % 5 ]; echo {}"
@mfansler
mfansler / conda.txt
Last active April 24, 2024 00:44 — forked from dataprofessor/conda.txt
Installing mamba on Google Colab
#################################################################################
# INSTALL MAMBA ON GOOGLE COLAB
#################################################################################
! wget -O miniconda.sh https://repo.anaconda.com/miniconda/Miniconda3-py37_4.10.3-Linux-x86_64.sh
! chmod +x miniconda.sh
! bash ./miniconda.sh -b -f -p /usr/local
! rm miniconda.sh
! conda config --add channels conda-forge
! conda install -y mamba
! mamba update -qy --all
@mfansler
mfansler / reset_audio.sh
Created March 4, 2021 03:09
Restart Macbook coreaudiod
#!/bin/sh
sudo launchctl kickstart -k system/com.apple.audio.coreaudiod
@mfansler
mfansler / conda-download.sh
Created February 15, 2021 17:09
Download Conda package tarballs needed to create an environment
#!/bin/bash -l
# use tmp dir to avoid name conflicts
tmp=$(mktemp -d)
# solve environment, ignoring existing cache
CONDA_PKGS_DIRS=$tmp conda create -dp $tmp/0 --json "$@" |\
# filter to tarball URLs
grep '"url"' | grep -oE 'https[^"]+' |\
# download locally
xargs wget -c
@mfansler
mfansler / conda-archive.sh
Last active February 16, 2021 03:36
Script to archive Conda environments to YAML
#!/bin/bash -le
# Usage: ./conda-archive.sh foo
# Result: creates "foo.full.yaml", "foo.min.yaml", and removes "foo" environment
echo "Archiving environment '$1' to YAML"
conda env export -vn $1 > $1.full.yaml
conda env export -vn $1 --from-history > $1.min.yaml
@mfansler
mfansler / conda-list-any.py
Last active April 17, 2021 22:51
Script to check all Conda environments for a given package
#!/usr/bin/env conda run -n base --no-capture-output python
## Usage: conda-list-any.py [PACKAGE ...]
## Example: conda-list-any.py numpy pandas
import conda.gateways.logging
from conda.core.envs_manager import list_all_known_prefixes
from conda.cli.main_list import list_packages
from conda.common.compat import text_type
import sys