Skip to content

Instantly share code, notes, and snippets.

View wmayner's full-sized avatar

William Mayner wmayner

View GitHub Profile
@wmayner
wmayner / ray_map_shortcircuit.py
Last active May 11, 2022 19:05
Map a Ray remote function, returning early if a particular value is found
import functools
import time
import ray
ray.init()
def as_completed(object_refs, num_returns=1):
@wmayner
wmayner / color_swatch_notebook_cell.py
Last active July 17, 2023 01:00
Display a color swatch from hex color strings with IPython in a Jupyter Notebook
from IPython.display import Markdown, display
colors = ['#018700', '#00acc6', '#e6a500']
display(Markdown('<br>'.join(
f'<span style="font-family: monospace">{color} <span style="color: {color}">████████</span></span>'
for color in colors
)))
@wmayner
wmayner / setup-macos.md
Last active April 29, 2020 20:18
Brief guide on how to set up a new Mac

Set up a new Mac

zsh

zsh is a more useful shell than bash, in my opinion, so we'll change your default shell from bash to zsh.

Follow instructions here: https://github.com/ohmyzsh/ohmyzsh/wiki/Installing-ZSH#macos

Note: zsh uses the ~/.zshrc file instead of ~/.bash_profile, so you should move anything that you need in your ~.bash_profile to the ~.zshrc.

@wmayner
wmayner / conda_auto_env.zsh
Last active April 28, 2020 21:03
Automatically activate conda environments in zsh
# Auto activate conda environments
# - If an `environment.yml` or `.venv` file is in the current directory,
# the environment specified by that file is activated
# - `environment.yml` files are parsed
# - `.venv` files must contain just the name of the environment
# - If no such files are present and no environment is already activated, then
# we try to activate an environment with the same name as the current directory
# - Assumes that the conda installation is at $HOME/miniconda3
# Modified from:
# https://github.com/chdoig/conda-auto-env
@wmayner
wmayner / state_pairs_to_tpm.py
Last active February 26, 2020 02:44
Convert pairs of states to a transition probability matrix.
# -*- coding: utf-8 -*-
# state_pairs_to_tpm.py
import numpy as np
import pandas as pd
def state_pairs_to_tpm(previous_states, next_states):
"""Return a TPM from observed state transitions.
@wmayner
wmayner / sliding_window.py
Created September 5, 2019 16:15
Sliding window function for NumPy arrays
# from
# https://gist.githubusercontent.com/nils-werner/9d321441006b112a4b116a8387c2280c/raw/489137f314c022ca08f0c5089319ded2eb8c5ae1/sliding_window.py
def sliding_window(size, data, step=1, padded=False, axis=-1, copy=True):
"""Calculate a sliding window over an array.
Note:
If the array cannot be windowed evenly, it is truncated at the end.
Args:
data (np.ndarray): The array to be windowed.
@wmayner
wmayner / style.md
Created July 12, 2018 16:51
MATLAB coding style

Code style

Functions are preferred to scripts.

Nested structs are used to hold data, so that the data is labeled explicitly rather than relying on an indexing convention.

Fields in structs can be dynamically accessed with the someStruct.(fieldName) syntax; i.e.,

@wmayner
wmayner / random_cut_timer.py
Created March 15, 2017 01:33
Evaluating a random cut vs. other PyPhi functions
import json
import os
import pickle
import random
from time import time
import numpy as np
import pyphi
from pyphi import Network, Subsystem
@wmayner
wmayner / slate-bindings.md
Last active December 5, 2018 19:31
Short guide to using my Slate keybindings

Slate bindings

Window focus

  • Pressing Ctrl + Esc (or Ctrl + Caps Lock if you have Caps Lock mapped to Esc) will put a “window hint” at the center of every currently visible window.
  • These will each have a unique letter in them.
  • Pressing a letter will focus that window.
  • Pressing Esc will cancel the window hints.

Window movement/resizing