This file contains 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 sliding_mean_and_stddev_interval( | |
axis, xs, ys, n_neighbors=30, linewidth=1, color="k", ci_alpha=0.25 | |
): | |
"""Plot moving means and standard deviations""" | |
_1 = np.ones(2 * n_neighbors + 1) | |
_ys = np.nan_to_num(ys) | |
sliding_sum = correlate1d(_ys, _1, mode="constant") | |
sliding_N = correlate1d(np.isfinite(ys).astype(float), _1, mode="constant") | |
sliding_mean = sliding_sum / sliding_N | |
sliding_sumsq = correlate1d(_ys**2, _1, mode="constant") |
This file contains 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 | |
from torch import nn | |
from tqdm.auto import trange | |
class PPCA(nn.Module): | |
def __init__(self, d, c): | |
super().__init__() | |
self.d = d | |
self.c = c |
This file contains 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 matplotlib.pyplot as plt | |
import contextlib | |
@contextlib.contextmanager | |
def subplots(*args, **kwargs): | |
fig, axes = plt.subplots(*args, **kwargs) | |
try: | |
yield fig, axes | |
finally: | |
plt.show() |
This file contains 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 runs_to_ranges(x, one_more=False): | |
ranges = [] | |
cur = x[0] | |
b = x[0] | |
for a, b in zip(x, x[1:]): | |
assert b > a | |
if b - a == 1: | |
continue | |
else: | |
ranges.append(range(cur, a + 1 + one_more)) |
This file contains 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
%config InlineBackend.figure_format = 'retina' | |
import matplotlib.pyplot as plt | |
from matplotlib.markers import MarkerStyle | |
from matplotlib.transforms import offset_copy | |
from matplotlib.patches import Ellipse, Rectangle, ConnectionPatch | |
from matplotlib.lines import Line2D | |
from matplotlib.legend_handler import HandlerTuple | |
import contextlib | |
plt.rc("figure", dpi=300) |
This file contains 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' | |
) |
This file contains 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 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 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 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}) |
NewerOlder