Skip to content

Instantly share code, notes, and snippets.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@grey-area
grey-area / dict_expand.py
Last active January 21, 2019 13:56
Dictionary list product expansion
from functools import reduce
from copy import deepcopy
from itertools import product
def get_key_paths(d, key_paths=[], param_lists=[], acc=[]):
for k, v in d.items():
if isinstance(v, dict):
get_key_paths(v, key_paths, param_lists, acc=acc + [k])
elif isinstance(v, list):
@grey-area
grey-area / custom_cmaps.py
Created January 23, 2019 12:27
Making custom greyscale colormaps to emphasise parts of the scale
import numpy as np
import matplotlib.pyplot as plt
# make_cmap function is from: http://schubert.atmos.colostate.edu/~cslocum/custom_cmap.html
def make_cmap(colors, position=None, bit=False):
'''
make_cmap takes a list of tuples which contain RGB values. The RGB
values may either be in 8-bit [0 to 255] (in which bit must be set to
True when called) or arithmetic [0 to 1] (default). make_cmap returns
@grey-area
grey-area / class_methods.py
Last active March 6, 2019 12:45
Different methods of creating instances of class with common values for some data attribute
import numpy as np
# Base class
class Base:
# 'Constructor' class methods to make instances with common values for data attribute
@classmethod
def create_common_instance_1(cls):
value = np.eye(3)
return cls(value)
@grey-area
grey-area / main.tex
Created March 19, 2019 11:58
Using matplotlib2tikz to generate tikz figures, and using them in latex
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{figure}[ht]
\centering
import numpy as np
import matplotlib.pyplot as plt
from scipy.special import gamma
trials = 200
steps = 1000
xs = np.arange(steps)
ds = [1, 10, 100]
fig, axes = plt.subplots(1, 3, sharey=True)
@grey-area
grey-area / power_tower_complex_divergence.py
Created August 17, 2019 23:02
Produce a fractal by colouring points of the complex plane according to whether the power tower x^(x^(x^... converges or not
import numpy as np
from PIL import Image
from tqdm import tqdm
# Image resolution will be this divided by 2
# due to downsampling
resolution = 4000
# Points in the complex plane
import numpy as np
from PIL import Image
from matplotlib.colors import hsv_to_rgb
# Parameters
lim = 50
sub_pixel_resolution = 1
xs_min, xs_max = (-lim, lim)
ys_min, ys_max = (-lim * 0.5625, lim * 0.5625)
import torch
import torch.nn as nn
class MirroredConv1d(nn.Module):
def __init__(self, **kwargs):
super(MirroredConv1d, self).__init__()
if kwargs['out_channels'] % 2 == 1:
raise ValueError('Number of output channels must be even')
import torch
from pathlib import Path
import platform
from datetime import datetime
import git
from pip._internal.operations import freeze
def save_checkpoint(checkpoint_path, model, optimizer, config_dict):
run_info = {