Skip to content

Instantly share code, notes, and snippets.

@benman1
benman1 / bokeh_server.py
Created April 23, 2018 20:27
bokeh server with data update and plot endpoints
from bokeh.embed import components
from bokeh.core.templates import FILE
from bokeh.models import ColumnDataSource, DataRange1d, Plot
from bokeh.models.glyphs import Line
from bokeh.resources import INLINE
from bokeh.util.string import encode_utf8
from flask import Flask, request, jsonify
import numpy as np
@benman1
benman1 / nim templating for bokeh html
Created April 23, 2018 20:33
Simple templating solution with key-value substitutions in a template - nim templating for bokeh html
import streams, nre
#[ This script reads a template file and substitutes according to
key-value pairs. All variables in the template start with $. For
example $elementid.
]#
type
Substitutions = seq[tuple[name: string, value: string]]
@benman1
benman1 / estimate_distribution.py
Created May 3, 2018 16:34
estimate distribution with scipy
# found at https://stackoverflow.com/questions/6620471/fitting-empirical-distribution-to-theoretical-ones-with-scipy-python
%matplotlib inline
import warnings
import numpy as np
import pandas as pd
import scipy.stats as st
import statsmodels as sm
import matplotlib
@benman1
benman1 / install_atlas_lapack.md
Created May 6, 2018 02:47
ATLAS LAPACK installation
@benman1
benman1 / bins_from_random_forest.py
Last active June 10, 2018 19:21
binning as preprocessing
import sys
import pandas as pd
from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor
from sklearn.datasets import load_iris
def extract_bins(tree):
'''Extract bins from tree
'''
edges = {}
@benman1
benman1 / fix_python_repo.sh
Last active July 8, 2018 19:09
Fix a python repo that's completely out of date and badly formatted
#!/bin/sh
find . -name '*.py' -exec dos2unix {} \;
find . -name '*.py' -exec 2to3 -w {} \;
find . -name '*.py' -exec autopep8 --in-place --aggressive --aggressive {} \;
find . -name '*.py' -exec docformatter --in-place {} \;
isort -rc --atomic .
find . -name '*.py' -exec vim -c '%s/except \(.*\), e:/except \1 as e:/' {} -c 'wq' \;
find . -name '*.py' -exec autoflake --in-place --remove-unused-variables {} \;
find . -name '*.py' -exec vim -c '%s/xrange/range/' {} -c 'wq' \;
@benman1
benman1 / commands.sh
Created August 1, 2018 13:38
Bash one liners
# display the biggest files within a directory
find mypath -type f -printf '%s %p\n'| sort -nr | head -10
@benman1
benman1 / parallel.sh
Last active September 14, 2018 10:38
Parallel bash loop over files
# USAGE:
# ./parallel.sh <command> <input directory> [<threads> [<log file>]]
#
# EXAMPLE:
# ./parallel.sh longrunningcommand.java /myfiles/input/ 10 process.log
shell_command=$1
input_dir=$2
threads=$3
threads=${threads:=10}
output_log=$4
[alias]
unadd = reset HEAD
st = status
co = checkout
uncommit = reset --soft HEAD^
changed = diff-tree -no-commit-id --name-only -r
updatesubmodules = pull --recurse-submodules && git submodule update
hist = log --pretty=format:\"%h %ad | %s%d [%an]\" --graph --date=short
[pull]
rebase = true
@benman1
benman1 / hdf5_to_pb.py
Created December 12, 2018 15:21
keras model conversion/save; convert keras format (hdf5) into tensorflow SavedModelBuilder (protobuf)
import tensorflow as tf
from tensorflow.python.saved_model import tag_constants
from tensorflow.python.saved_model.signature_def_utils_impl import predict_signature_def
KERAS_HDF5 = 'weights.hdf5'
EXPORT_PATH = '/tmp/tf_model/' # where to save the tensorflow model
model = create_model(params) # build and compile the architecture
model.load_weights(KERAS_HDF5)