Skip to content

Instantly share code, notes, and snippets.

View christopherlovell's full-sized avatar
🍊

Chris Lovell christopherlovell

🍊
View GitHub Profile
@christopherlovell
christopherlovell / read_snaps.py
Created March 25, 2019 17:39
Read hdf5 snaps and concatenate
import numpy as np
import h5py
import glob
files = glob.glob('snap*')
coods = [None] * len(files)
ages = [None] * len(files)
metals = [None] * len(files)
@christopherlovell
christopherlovell / einsum_demo.py
Created March 6, 2019 17:37
Demo of numpy einsum matrix multiplication / summation functionality
import numpy as np
# dimensions (age,metallicity,wavelength)
a = 2
Z = 3
wl = 4
weights = np.random.rand(a,Z,1)
grid = np.random.randint(1,10,(a,Z,wl))
@christopherlovell
christopherlovell / bytes_to_unicode.py
Created January 22, 2019 15:47
Convert pickle dict formatted as a byte string in to string format
"""
Taken from https://stackoverflow.com/questions/22840092/unpickling-data-from-python-2-with-unicode-strings-in-python-3
Updated for numpy bytes_ object
"""
import numpy as np
def bytes_to_unicode(ob):
t = type(ob)
@christopherlovell
christopherlovell / example.md
Last active July 27, 2018 11:51
t-SNE example

t-SNE spectra example

Apply t-SNE to galaxy spectra from two different simulations.

First import some modules

import numpy as np

%matplotlib inline
@christopherlovell
christopherlovell / sdss-dr7-wget.sh
Created June 13, 2018 16:47
Create list of URLs for wget from SDSS DR7 spec info
#!/bin/bash
# Clear output file
> out.csv
while read f; do
# read in line to array, comma delimited
arrIn=(${f//,/ })
@christopherlovell
christopherlovell / resample_distribution.py
Created May 11, 2018 11:07
Resample a distribution to match another
from scipy.stats import binned_statistic
import random
def resample_distribution(x, y, k=300, bin_limits=None):
"""
Resample y to fit distribution of x
Args:
x, y (array) distributions
k (int) number of samples
@christopherlovell
christopherlovell / mondrian.html
Last active May 9, 2018 23:01
D3 Mondrian Generator
<!DOCTYPE html>
<html lang="en">
<head>
<title>Mondrian Generator</title>
<script src="https://d3js.org/d3.v4.js" charset="utf-8"></script>
<style>
</style>
</head>
<body>
$ git filter-branch --tag-name-filter cat --index-filter 'git rm -r --cached --ignore-unmatch filename' --prune-empty -f -- --all
$ rm -rf .git/refs/original/
$ git reflog expire --expire=now --all
$ git gc --prune=now
$ git gc --aggressive --prune=now
$ git push origin --force --all
$ git push origin --force --tags
@christopherlovell
christopherlovell / _supermongo_matplotlib_stylesheet.md
Last active July 5, 2016 14:13
Matplotlib Stylesheet for creating SM style plots

To use this stylesheet, you need to put it inside your mplconfig_dir. By default, mpl_configdir should be ~/.config/matplotlib, but you can check where yours is with matplotlib.get_configdir(). You can then import it with styleuse('sm').

Based on work by Monica L. Turner, http://space.mit.edu/~turnerm/python.html (accessed 5/7/2016)

@christopherlovell
christopherlovell / lognormal.R
Last active September 5, 2019 13:29
Fitting a lognormal in R to a large data set and plotting the Q-Q distribution
library(MASS)
# generate a million lognormal samples
n <- 1000000
dat <- rlnorm(n, meanlog = 0, sdlog = 1)
# add some noise (optional)
dat <- dat + runif(n, 0, 1)
# create a vector of histogram breaks