Skip to content

Instantly share code, notes, and snippets.

View thomasaarholt's full-sized avatar

Thomas Aarholt thomasaarholt

View GitHub Profile
@zonca
zonca / rebin.py
Created November 8, 2011 19:12
IDL rebin in python
import numpy as np
def rebin(a, new_shape):
"""
Resizes a 2d array by averaging or repeating elements,
new dimensions must be integral factors of original dimensions
Parameters
----------
a : array_like
@piscisaureus
piscisaureus / pr.md
Created August 13, 2012 16:12
Checkout github pull requests locally

Locate the section for your github remote in the .git/config file. It looks like this:

[remote "origin"]
	fetch = +refs/heads/*:refs/remotes/origin/*
	url = git@github.com:joyent/node.git

Now add the line fetch = +refs/pull/*/head:refs/remotes/origin/pr/* to this section. Obviously, change the github url to match your project's URL. It ends up looking like this:

@andrewjesaitis
andrewjesaitis / gist:7778867
Created December 3, 2013 22:37
Just an example of dividing a continuous matplotlib colormap into a distinct array of colors
import matplotlib as mpl
num_colors = len(values)
cm = mpl.cm.get_cmap(name='YlGnBu_r')
currentColors = [cm(1.*i/num_colors) for i in range(num_colors)]
@derricw
derricw / rebin_ndarray.py
Created April 9, 2015 15:46
Rebin an arbitrary numpy ndarray in N dimensions
import numpy as np
def bin_ndarray(ndarray, new_shape, operation='sum'):
"""
Bins an ndarray in all axes based on the target shape, by summing or
averaging.
Number of output dimensions must match number of input dimensions.
Example
@wassname
wassname / augumented_hdf5_matrix.py
Last active October 11, 2020 23:32
How to do data augmentation on a keras HDF5Matrix
"""Another way, note this one will load the whole array into memory ."""
from keras.preprocessing.image import ImageDataGenerator
import h5py
from keras.utils.io_utils import HDF5Matrix
seed=0
batch_size=32
# we create two instances with the same arguments
data_gen_args = dict(
rotation_range=90.,
@bencholmes
bencholmes / forceFFTSymmetry.m
Created August 30, 2018 13:30
A MATLAB function to force symmetry on an FFT, thus producing a real signal after an IFFT.
function X = forceFFTSymmetry(X)
% forceFFTSymmetry A function to force conjugate symmetry on an FFT such that when an
% IFFT is performed the result is a real signal.
% The function has been written to replace MATLAB's ifft(X,'symmetric'), as this function
% is not compatible with MATLAB Coder.
% Licensed under Creative Commons Zero (CC0) so use freely.
XStartFlipped = fliplr(X(2:floor(end/2)));
@thomasaarholt
thomasaarholt / savefig_no_whitespace.py
Last active March 17, 2022 03:07
Save matplotlib figures with no whitespace
import matplotlib.pyplot as plt
def save(filepath="image.png", fig=None):
'''Save the current image with no whitespace
Example filepath: "myfig.png" or r"C:\myfig.pdf"
Based on answers from https://stackoverflow.com/questions/11837979/
'''
import matplotlib.pyplot as plt
if not fig:
fig = plt.gcf()
@thomasaarholt
thomasaarholt / Remote_Notebook.md
Last active March 21, 2019 14:51
Remote Jupyter Notebook through ssh tunnel

Using Jupyter notebook remotely via a ssh tunnel.

Especially good for running on headless devices, such as supercomputers or clusters.

Remotely:

# remote_port should be value between 8888 and 9000
jupyter notebook --no-browser --port=remote_port

Locally, new terminal window

@thomasaarholt
thomasaarholt / colorbar.py
Last active December 3, 2019 14:34
Add colorbar next to image
from mpl_toolkits.axes_grid1 import make_axes_locatable
def colorbar(mappable):
"mappable is img = plt.imshow()"
ax = mappable.axes
fig = ax.figure
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
cax.aname = 'colorbar'
return fig.colorbar(mappable, cax=cax)
@thomasaarholt
thomasaarholt / .inputrc
Created February 27, 2019 21:21
Absolutely necessary .inputrc hotkeys for navigating in bash
"\e[A": history-search-backward
"\e[B": history-search-forward
"\e[C": forward-char
"\e[D": backward-char