This file contains hidden or 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
import tempfile | |
import imageio | |
from IPython.display import Image | |
def ipygif(z, fps=16, width=200, height=200): | |
f = tempfile.NamedTemporaryFile(prefix="ipygif", suffix=".gif") | |
z = z - z.min() | |
z *= 255.0 / z.max() | |
imageio.mimwrite(f.name, z.astype(np.uint8), fps=fps) | |
return Image(filename=f.name, embed=True, width=width, height=height) |
This file contains hidden or 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
""" | |
Implements the nonparametric simultaneous confidence interval described | |
in Gao et al. [1], which modifies the Mandel-Betensky simultaneous CI [2] | |
such that it is admissible. Notation in the code below comes from [1]. | |
Takes as input an array of bootstrap samples / replicates, returns lower | |
and upper bounds at 1-alpha confidence. | |
[1]: https://www.mdpi.com/2073-8994/13/7/1212 | |
[2]: https://pubmed.ncbi.nlm.nih.gov/26452746/ |
This file contains hidden or 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
import numpy as np | |
from .triginterp_inner import triginterp_inner | |
def triginterp(x, y, xi): | |
"""Trigonometric interpolation with zero-padding (lol, not periodic). Easy to change.""" | |
P = np.zeros_like(xi, dtype=np.float64) | |
triginterp_inner( | |
x.astype(np.float64), y.astype(np.float64), xi.astype(np.float64), P | |
) |
This file contains hidden or 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
import torch | |
import torch.nn.functional as F | |
def translate(input, shifts, mode='bilinear', padding_mode='zeros'): | |
"""Translate a batch of images by a batch of xy shifts | |
Arguments | |
--------- | |
input : torch.Tensor NCHW |
This file contains hidden or 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
# adapted from https://matplotlib.org/3.1.1/gallery/statistics/confidence_ellipse.html | |
import numpy as np | |
import matplotlib.pyplot as plt | |
import colorcet | |
from matplotlib.patches import Ellipse | |
import matplotlib.transforms as transforms | |
def cluster_scatter(xs, ys, ids, ax=None, n_std=2.0): | |
ax = ax or plt.gca() |
This file contains hidden or 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
from pathlib import Path | |
import cmdstanpy | |
import ujson | |
def stanc(name, code, workdir=".stan"): | |
Path(workdir).mkdir(exist_ok=True) | |
path = Path(workdir) / f"{name}.stan" | |
with open(path, "w") as f: | |
f.write(code) | |
model = cmdstanpy.CmdStanModel(stan_file=path, stanc_options={'warn-pedantic': True}) |
This file contains hidden or 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
def tukey_scatter(x, y, iqrs=1.5, ax=None, **kwargs): | |
ax = ax or plt.gca() | |
x_25, x_75 = np.percentile(x, [25, 75]) | |
x_iqr = x_75 - x_25 | |
y_25, y_75 = np.percentile(x, [25, 75]) | |
y_iqr = y_75 - y_25 | |
inliers = np.flatnonzero( | |
(x_25 - iqrs * x_iqr < x) | |
& (x < x_75 + iqrs * x_iqr) | |
& (y_25 - iqrs * y_iqr < y) |
This file contains hidden or 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
def regline(x, y, ax=None, **kwargs): | |
b = ((x - x.mean()) * (y - y.mean())).sum() / np.square(x - x.mean()).sum() | |
a = y.mean() - b * x.mean() | |
x0, x1 = ax.get_xlim() | |
r = np.corrcoef(x, y)[0, 1] | |
ax.plot([x0, x1], [a + b * x0, a + b * x1], lw=1) | |
ax.text( | |
0.1, | |
0.9, | |
f"$\\rho={r:.2f}$", |
This file contains hidden or 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
try: | |
import torch | |
import torch.nn.functional as F | |
HAVE_TORCH = True | |
except ImportError: | |
HAVE_TORCH = False | |
def normxcorr1d( | |
template, | |
x, |
This file contains hidden or 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
# https://stackoverflow.com/a/74706214/3979938 | |
from matplotlib.transforms import offset_copy | |
def inline_xlabel(ax, label): | |
t = offset_copy( | |
ax.transAxes, | |
y=-(ax.xaxis.get_tick_padding() + ax.xaxis.get_tick_space()), | |
fig=fig, | |
units='points' | |
) |