Skip to content

Instantly share code, notes, and snippets.

View phargogh's full-sized avatar

James Douglass phargogh

View GitHub Profile
@phargogh
phargogh / convert-repos.sh
Last active June 30, 2020 20:33
Convert bitbucket user repos and snippets to mercurial bundles
REPO_DIR=$(pwd)/repos
OUT_DIR=$(pwd)/bundles
mkdir -p $OUT_DIR $REPO_DIR
for repo in `cat repos.txt`
do
if echo $repo | grep ^ssh
then
PRIVATE='-private-'
else
@phargogh
phargogh / README.md
Last active September 16, 2020 00:42
The current version of InVEST, for testing

The three files here are for developing the InVEST download link auto-updating functionality.

The file invest-current_version.json is there in case it's easier to only update the download links.

The file invest-all-links.json is there in case it's easier to update all of the hyperlinks in one go.

The file invest-all-links-nid.json is the same as invest-all-links.json, just with the drupal Node ID included for easier reference.

@phargogh
phargogh / standard_factored_form.py
Created October 17, 2020 19:22
Factor an integer into standard factored form.
import time
import math
import collections
PRIMES = set((2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199))
def standard_factored_form(n):
print(f"Factoring {n}")
remainder = n
factors = collections.defaultdict(int)
@phargogh
phargogh / cbc_emissions_math_Test.py
Created October 28, 2020 01:38
InVEST Coastal Blue Carbon's emissions math expermient
# Related to the discussion at https://community.naturalcapitalproject.org/t/coastal-blue-carbon-zero-net-c-sequestration-in-disturbed-blue-carbon-habitats/1398/9
# Note that this is intended to simply recreate the math used in the CBC model,
# which is just a Riemann Sum of the emissions equation. It would be far
# faster (and should be more numerically correct) to simply use the fundamental
# theorem of calculus and evaluate the function directly for each transition period.
# d = 0.0001997
d = 0.00020075
# found_emissions = 0.0000133733
@phargogh
phargogh / conclusions.txt
Last active January 21, 2021 22:55
Timing pour points implementations
Conclusions:
* On the Colombia DEM (15030 x 20631 pixels) and the Gura DEM (1939 x 603 pixels) the
delineateit implementation reliably runs in 1/3 the time of the proposed
`_ManagedRaster`-based pour points implementation.
@phargogh
phargogh / README.md
Last active February 5, 2021 21:04
Use cases for numpy array indexing with slice(None)

Example of indexing with slice(None)

Setup

Just a standard python install should be fine, so long as you have numpy available.

Alternatively, a local conda environment would work well for this:

@phargogh
phargogh / evaluate-contrived.py
Last active February 13, 2021 07:56
Experiment: extract and evaluate latex from rst
import sympy.parsing.latex
CARBON_EQ_21 = r"V\frac{sequest_x}{yr\_fut-yr\_cur}\sum^{yr\_fut-yr\_cur-1}_{t=0}\frac{1}{\left(1+\frac{r}{100}\right)^t\left(1+\frac{c}{100}\right)^t}"
CARBON_EQ_21_SIMPLE = r"V\frac{s}{fut-cur}\sum^{fut-cur-1}_{t=0}\frac{1}{\left(1+\frac{r}{100}\right)^t\left(1+\frac{c}{100}\right)^t}"
def replace(string):
return string.replace(r'\_', r'\textunderscore')
@phargogh
phargogh / requirements.txt
Created February 20, 2021 20:40
Experiment: lint RST with sphinx.ext.napoleon parsing
sphinx
rstcheck
taskgraph
@phargogh
phargogh / detect-impervious.py
Last active March 9, 2021 01:02
Tracer code to do a neighborhood search within a search radius using a convolution and array masking.
import numpy
import scipy.signal
# 1's represent pixels that are an impervious landcover.
# Derived from the landcover classification.
impervious_mask = numpy.array([
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
@phargogh
phargogh / formati18n.vim
Last active March 11, 2021 04:13
Simple vim script for fixing up ARGS_SPEC for gettext translation.
" Example usage:
" Launch a new vim session by (bash) $ find src -name "*py" | xargs grep -l ARGS_SPEC\ = | xargs vim
" argdo formati18n.vim
execute '%s/"about": (/"about": _(/g'
execute '%s/"name": "\(\p\+\)"/"name": _("\1")/g'
execute '%s/"model_name": "\(\p\+\)"/"model_name": _("\1")/g'
execute "normal! 3Giimport gettext\r\<Esc>"
" The & looks to add the original matched pattern.