Skip to content

Instantly share code, notes, and snippets.

View john-bradshaw's full-sized avatar

John Bradshaw john-bradshaw

View GitHub Profile
@john-bradshaw
john-bradshaw / numpy_utils_intersect.py
Created April 20, 2016 12:57
intersect_two_numpy_arrays
import numpy as np
def intersect_indcs_two_arrays(x_array, y_array):
"""intersect_indcs_two_arrays(x, y) -> xm, ym
x, y: 1-d arrays of unique values
xm, ym: indices into x and y giving sorted intersection
code taken from http://stackoverflow.com/questions/33529057/indices-that-intersect-and-sort-two-numpy-arrays?lq=1
"""
assert np.all(np.logical_not(map(are_there_duplicates, [x_array, y_array]))), "Intersect assumes arrays are unique."
# basic idea taken from numpy.lib.arraysetops.intersect1d
@john-bradshaw
john-bradshaw / diff_privacy_functions_hall_demo.py
Created March 9, 2017 01:26
Simple demo for a section in Hall, R., Rinaldo, A. and Wasserman, L., 2013. Differential privacy for functions and functional data. Journal of Machine Learning Research, 14(Feb), pp.703-727.
"""
Code for section 4.1 of:
Hall, R., Rinaldo, A. and Wasserman, L., 2013. Differential privacy for
functions and functional data. Journal of Machine Learning Research, 14(Feb), pp.703-727.
Coded up in a bit of a rush so apologies if there are bugs.
"""
@john-bradshaw
john-bradshaw / gaussian_morphing.py
Last active October 15, 2017 16:13
Model Based Morphing for a Two dimensional Gaussian (reproduction of Saul, L.K. and Jordan, M.I., 1997 Fig 1a)
"""
Code for Figure 1a of
Saul, L.K. and Jordan, M.I., 1997. A variational principle for model-based morphing.
In Advances in Neural Information Processing Systems (pp. 267-273).
"""
import numpy as np
from scipy.integrate import odeint
from scipy.optimize import fsolve
from scipy import stats
@john-bradshaw
john-bradshaw / mixture_of_gaussians_morphing.py
Last active October 17, 2017 21:49
Mixture of Gaussians variational morphing similar to Fig 1c of Saul, L.K. and Jordan, M.I., 1997
"""
Code for Figure 1c of
Saul, L.K. and Jordan, M.I., 1997. A variational principle for model-based morphing.
In Advances in Neural Information Processing Systems (pp. 267-273).
Note not exactly the same setup and do not know settings of ell and variance to use
Also not sure how to deal with the change points
Written in a bit of a rush and not thoroughly tested so use with caution
"""
@john-bradshaw
john-bradshaw / bayesian_optimisation_simple_demo.py
Created November 15, 2017 18:49
Simple example to run Bayesian Optimisation GPs. Works with Python3, TF1.4, python-fire, GPflow git commit 4ff00cbbc83efff8cb537f16a7eb1c1e11de3a75
import enum
import numpy as np
from scipy import stats
import fire
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
from matplotlib.widgets import Button
@john-bradshaw
john-bradshaw / tf_control_dep_demo.py
Created April 18, 2018 14:20
Tensorflow Control Dependencies. Resouce Variables versus normal variables.
import numpy as np
import tensorflow as tf
def main1():
x = tf.get_variable("x", initializer=np.float64(0))
x_id = tf.identity(x)
x_rd = x.read_value()
x_p0 = x + 0
with tf.control_dependencies([x_id, x_rd, x_p0]):
@john-bradshaw
john-bradshaw / tf_control_dep_demo2.py
Created April 18, 2018 14:35
Demo on using control dependencies version 2.
import tensorflow as tf
def main1():
sess = tf.Session()
x = tf.Variable(1.)
sess.run(tf.initialize_variables([x]))
@john-bradshaw
john-bradshaw / split_reagents.py
Last active March 11, 2022 20:44
Code to split up reactants and reagents (currently untested, WIP)
"""
Module for identifying for RDKit functions working with reaction SMILES strings.
"""
import collections
import itertools
import functools
import typing
@john-bradshaw
john-bradshaw / different_smiles_same_fp.ipynb
Last active April 28, 2022 00:26
Notebook showing how different SMILES can sometimes have the same fingerprint
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@john-bradshaw
john-bradshaw / flax_mem_leak.py
Created August 12, 2022 17:11
Showing how flax's nn.compact if used incorrectly can cause memory leaks.
"""
Simple demonstration of memory leak.
XLA_PYTHON_CLIENT_PREALLOCATE=false CUDA_VISIBLE_DEVICES=0 python flax_mem_leak.py
Breaks on last iter through loop.
# Tested on version:
Name: flax
Version: 0.4.1
Summary: Flax: A neural network library for JAX designed for flexibility