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 / mnist_gpdnn_example.py
Created November 7, 2017 14:16
Shows how to combine NN with GP for end to end training. run with TF 1.4, GPflow git commit f618fe4d9aa096b32a3d24576d68f46a3f260116
import os
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data as mnist_input_data
import numpy as np
from sklearn import cluster
from scipy.spatial import distance
import pandas as pd
@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 / gpdnns.py
Last active February 5, 2024 21:04
GPDNN example on MNIST GPflow git commit f42fc3ea33ec3a8c37a45d3ccdd41e60bed5690e (unchecked accuracy results but seems to run okay)
import os
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data as mnist_input_data
import numpy as np
from sklearn import cluster
from scipy.spatial import distance
import pandas as pd
@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