Skip to content

Instantly share code, notes, and snippets.

View gilliss's full-sized avatar

Tom Gilliss gilliss

View GitHub Profile
@gilliss
gilliss / truncnorm.py
Created April 20, 2019 16:39
Exploring parameterization of scipy.stats.truncnorm
"""
Exploring parameterization of scipy.stats.truncnorm
Ref:
https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.truncnorm.html
https://github.com/scipy/scipy/blob/v1.2.1/scipy/stats/_continuous_distns.py
truncnorm_gen
"""
import numpy as np
import scipy.stats as stats
@gilliss
gilliss / Dockerfile
Created April 2, 2019 14:13
debugging this Dockerfile for use with Shifter
# Pull in image to stack on top of
# ... https://hub.docker.com/r/wisecg/rootmj
# ... Installs CLHEP-2.3.2.2, ROOT-6.12.04, and a few Python packages, bash entry
# ... atop python:3.6.4-jessie (Debian 8 "jessie" OS)
FROM wisecg/rootmj:v2
# Install Anaconda
# ... '-b' accepts license
RUN wget https://repo.continuum.io/archive/Anaconda3-5.3.1-Linux-x86_64.sh
RUN bash Anaconda3-5.3.1-Linux-x86_64.sh -b
@gilliss
gilliss / find_bin.py
Created March 5, 2019 20:37
Test a few functions for finding bin index corresponding to a value along the binned axis
"""
Profile several 'FindBin' methods
The "(np.abs(binCenters - val)).argmin()" method is the best, so far
"""
import sys
import numpy as np
from datetime import datetime
@gilliss
gilliss / mc_parallel.py
Created March 2, 2019 04:21
Simple monte carlo approximation done in parallel with MPI
"""
Calculate expectation values for quantities of a distribution
via parallelized Monte Carlo approximation.
Approximate with t samples distributed across k CPU cores.
Let t = m * k, such that each core handles m samples.
Let P(x) be some distribution as a function of x.
E[x] ~= (1/t) * Sum_{i=0}^{t}[ x_i ]
= (1/t) * Sum_{j=0}^{k}[ Sum_{i=0}^{m}[ x_i ] ]
@gilliss
gilliss / save_dict_numpy_h5py.py
Last active September 13, 2022 10:07
Save a dictionary of NumPy arrays in a HDF5 file
"""
Save NumPy arrays in a dictionary structure into a HDF5 file.
Ref:
http://docs.h5py.org
"""
import numpy as np
import h5py
import os
@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:
@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 / 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 / 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 / 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