Skip to content

Instantly share code, notes, and snippets.

View michaelosthege's full-sized avatar

Michael Osthege michaelosthege

View GitHub Profile
@michaelosthege
michaelosthege / weirdimport.py
Created March 7, 2017 22:04
import python stuff from any directory
def weirdimport(*paths):
import os, sys
fullpath = os.path.abspath(os.path.join(*paths))
global project
sys.path.append(os.path.dirname(fullpath))
try:
project = __import__(os.path.basename(fullpath))
sys.modules['project'] = project
finally:
del sys.path[-1]
@michaelosthege
michaelosthege / benchmark.py
Created July 6, 2017 12:05
Benchmarking helpers
import time
class Measurement(object):
def __init__(self, name):
self.name = name
return
def __enter__(self):
self.t_start = time.time()
return
@michaelosthege
michaelosthege / theano_numpy_ODE.py
Last active July 6, 2017 12:18 — forked from dean-shaff/theano_numpy_diffeq.py
Theano and Numpy speed comparison for Lorenz Attractor
import theano
import theano.tensor as T
import numpy
import h5py
import os
import time
def rungekuttastep(h, y, fprime, *theta):
k1 = h*fprime(y,*theta)
@michaelosthege
michaelosthege / pandas_snippets.py
Last active November 30, 2017 10:09
Some pandas code snippets
# saving a DataFrame to an Excel spreadsheet
writer = pandas.ExcelWriter(fp_xlsx)
df.to_excel(writer, 'Sheetname')
writer.save()
@michaelosthege
michaelosthege / integration_op.py
Created March 4, 2019 16:47
Custom Theano Op for wrapping around an ODE-integrator.
import base64
import hashlib
import theano
import theano.tensor as tt
def make_hashable(obj):
"""Makes tuples, lists, dicts, sets and frozensets hashable."""
if isinstance(obj, (tuple, list)):
return tuple((make_hashable(e) for e in obj))
@michaelosthege
michaelosthege / install-pymc3.bat
Last active August 10, 2020 10:21
pymc3-installation
conda create -n pm3 python=3.7 pandas jupyter matplotlib mkl-service libpython m2w64-toolchain coverage xlrd openpyxl scipy -y
activate pm3
# install last release from PyPI
pip install pymc3
# or latest version directly from master:
pip install git+https://github.com/pymc-devs/pymc3
# or from master in editable mode:
git clone https//github.com/pymc-devs/pymc3 pymc3
cd pymc3
@michaelosthege
michaelosthege / _R0_regions.ipynb
Last active August 12, 2020 19:21
Analysis of Rt.live model estimates of R0 - a comparison between US regions (alternative link: https://nbviewer.jupyter.org/gist/michaelosthege/6cd14970dd789247176c4d4a1dd28051/_R0_regions.ipynb)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@michaelosthege
michaelosthege / linear_interpolation.py
Created July 20, 2017 13:25
vectorized linear interpolation in numpy
import numpy
from matplotlib import pyplot
def interpolate(x_fix, y_fix, x_var):
x_repeat = numpy.tile(x_var[:,None], (len(x_fix),))
distances = numpy.abs(x_repeat - x_fix)
x_indices = numpy.searchsorted(x_fix, x_var)
@michaelosthege
michaelosthege / DatagramSocketReceiver.cs
Created June 24, 2017 21:06
Unity UdpClient (#if UNITY_EDITOR) and UWP DatagramSocket (#if !UNITY_EDITOR) for receiving UDP broadcasts.
public class BroadcastReceiver : IDisposable
{
//OnMessageReceived
public delegate void AddOnMessageReceivedDelegate(string message, IPEndPoint remoteEndpoint);
public event AddOnMessageReceivedDelegate MessageReceived;
private void OnMessageReceivedEvent(string message, IPEndPoint remoteEndpoint)
{
if (MessageReceived != null)
MessageReceived(message, remoteEndpoint);
}
@michaelosthege
michaelosthege / mypy_groupby.py
Created February 5, 2022 21:23
Reformats piped mypy output to group by the error code.
"""
Reformats piped mypy output to group by the error code.
Author: Michael Osthege
License: MIT
Usage
-----
mypy -p mypackage --show-error-codes | python mypy_groupby.py
"""