Skip to content

Instantly share code, notes, and snippets.

View christopherlovell's full-sized avatar
🍊

Chris Lovell christopherlovell

🍊
View GitHub Profile
@christopherlovell
christopherlovell / filter_test.py
Created January 11, 2024 16:15
Synthesizer load filter test
import numpy as np
import matplotlib.pyplot as plt
from synthesizer.filters import Filter, FilterCollection
filter_codes = ["JWST/NIRCam.F444W", "JWST/NIRCam.F480M"]
filtcoll = FilterCollection(filter_codes=filter_codes)
fig, ax = filtcoll.plot_transmission_curves(show=False, linestyle='solid')
@christopherlovell
christopherlovell / pca.py
Created October 4, 2023 19:33
PCA example
"""
PCA visualisation based on the example here:
https://gist.github.com/anonymous/7d888663c6ec679ea65428715b99bfdd
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
plt.style.use('dark_background')
from swiftsimio import Writer
from swiftsimio.units import cosmo_units
import unyt
import numpy as np
import h5py
import glob
files = glob.glob('ics*.hdf5')
@christopherlovell
christopherlovell / train_rnn_test.py
Created March 14, 2022 23:48
train rnn example
import sys
import os
import numpy as np
import matplotlib
# matplotlib.use('agg')
from matplotlib import pyplot as plt
# from IPython.display import clear_output
import tensorflow as tf
# physical_devices = tf.config.experimental.list_physical_devices('GPU')
# assert len(physical_devices) > 0, "Not enough GPU hardware devices available"
@christopherlovell
christopherlovell / weighted_percentile.py
Last active January 17, 2020 13:26
Calculate the binned, weighted percentile
def weighted_quantile(values, quantiles, sample_weight=None,
values_sorted=False, old_style=False):
"""
Taken from From https://stackoverflow.com/a/29677616/1718096
Very close to numpy.percentile, but supports weights.
NOTE: quantiles should be in [0, 1]!
:param values: numpy.array with data
:param quantiles: array-like with many quantiles needed
"""
Script for creating subsets of particles from gizmo sims
Authors:
- Sydney Lower
- Chris Lovell
"""
import h5py
import caesar
@christopherlovell
christopherlovell / 0_schwimmbad_demo.py
Last active September 4, 2019 21:01
Example Schwimmbad usage
"""
A tutorial introduction to Schwimmbad
https://github.com/adrn/schwimmbad
To install the requirements:
> cat requirements.txt | xargs pip install
"""
@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)