Skip to content

Instantly share code, notes, and snippets.

View gilliss's full-sized avatar

Tom Gilliss gilliss

View GitHub Profile
@gilliss
gilliss / MarginalizeMesh.py
Last active May 4, 2018 14:51
Marginalize a 2D distribution calculated over a np.meshgrid
"""
Marginalize a 2-dimensional distribution evaluated over a numpy meshgrid.
-The Distribution class holds the distribution, the marginalization method,
and the grid parameters to be used for the integration.
-The Marginalize() method allows specification of the axis you would like to marginalize.
The marginalization is done via simple grid integration, using rectangular Riemann
sum approximation of integral.
There is similar code in scipy's scipy/stats/tests/test_multivariate.py::test_marginalization()
@gilliss
gilliss / chstwo.py
Last active April 15, 2019 14:17
Chi-square test for the difference between two data sets
"""
A chi-square test for the difference between two data sets.
_count() method is from SciPy's https://github.com/scipy/scipy/blob/v0.14.0/scipy/stats/stats.py#L3467
A version of this function is in NRC, and related functions are in SciPy:
NRC: chstwo(bins1, bins2, nbins, knstrn, df, chsq, prob)
SciPy: power_divergence(f_obs, f_exp=None, ddof=0, axis=0, lambda_=None)
"""
import numpy as np
@gilliss
gilliss / barh_left_right.py
Last active May 22, 2018 16:37
Plotting a vertical orientation histogram, from the right or left side of an axes object, using matplotlib.pyplot.barh
"""
Plotting a matplotlib.pyplot.barh from the left or right of axes
Ref:
https://matplotlib.org/api/_as_gen/matplotlib.pyplot.barh.html
https://matplotlib.org/examples/lines_bars_and_markers/barh_demo.html
"""
import numpy as np
import matplotlib.pyplot as plt
@gilliss
gilliss / AxisBinLabelsROOT.cc
Last active June 12, 2018 13:44
Change axis bin labels in ROOT
/*
Code to change the strings used for TAxis bin labels in ROOT.
Ref:
https://root.cern.ch/doc/master/classTAxis.html
*/
#include <map>
#include <string>
#include <iostream>
@gilliss
gilliss / CustomColorList.py
Last active August 22, 2018 01:11
A custom list of colors in matplotlib.pyplot
"""
Create a custom list of colors for use in data visualization
"""
import matplotlib.pyplot as plt
import numpy as np
# create n data points
n = 20
xData = np.linspace(0,n,n)
@gilliss
gilliss / SaveNumpy.py
Created June 19, 2018 16:13
Saving numpy arrays as .npy and .npz
"""
Save and access numpy arrays, as single arrays in .npy files, and as multiple arrays in .npz files
Ref:
https://docs.scipy.org/doc/numpy-1.14.0/reference/generated/numpy.save.html
https://docs.scipy.org/doc/numpy-1.14.0/reference/generated/numpy.savez.html
"""
import numpy as np
import glob
import os
@gilliss
gilliss / MixtureModelScipyOptimize.py
Last active August 22, 2018 13:50
Using scipy.optimize.fmin (simplex) to fit a simple mixture model
"""
Use scipy.optimize.fmin (simplex algorithm) to find optimal weights for components of a simple mixture model.
"""
import numpy as np
import scipy.stats as stats
from scipy.optimize import fmin
#############
# Generate data
@gilliss
gilliss / json_module.py
Created October 26, 2018 22:47
Write, read, and print using the json module.
"""
Write, read, and print using the json module.
"""
import json
import os
# Create dictionary
d = {'first': 1, 'second': 2, 'third': 3}
file_path = './tmp_file.json'
@gilliss
gilliss / ip_problem.py
Last active January 21, 2019 22:01
a potential solution
def isSectionValid(s):
"""
Inputs:
s = string of chars with 1<=len(s)<=3
Output:
True or False for valid or invalid
Description:
Leading digit of section must not be 0 if length of section > 1. Value
of section must be within [0,255]. If section fails either of these
requirements, section is invalid.
@gilliss
gilliss / argparse_module.py
Last active February 20, 2019 15:50
Simple example using the argparse module
"""
A simple example using argparse to sum or find max of a list of numbers,
and optionally multiply result by some factor.
Examples:
$ python basic_argparse.py 1 2 3 4
$ python basic_argparse.py --mult 10 --max 1 2 3 4
$ python basic_argparse.py --max --mult 10 1 2 3 4
Ref: