Skip to content

Instantly share code, notes, and snippets.

@letmaik
letmaik / models_committed.py
Created May 22, 2014 14:54
delete, insert, update events after a commit for SQLAlchemy
from sqlalchemy import create_engine, event, orm
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm.session import Session as SessionBase, object_session
from sqlalchemy.event.api import listen
# The following adds delete, insert, and update events after successful commits.
# SQLAlchemy provides only events after flushes, but not after commits.
# The classes are adapted from Flask-SQLAlchemy.
# see also https://stackoverflow.com/a/12026787/60982
@letmaik
letmaik / voronoi_polygons.py
Last active June 9, 2022 22:16
Creates a Voronoi diagram with cell polygons using scipy's Delaunay triangulation (scipy >= 0.9)
from __future__ import division
import collections
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from scipy.spatial import Delaunay, KDTree
# an adaptation of https://stackoverflow.com/a/15783581/60982
# using ideas from https://stackoverflow.com/a/9471601/60982
@letmaik
letmaik / index.html
Created October 7, 2016 10:07
Leaflet GridLayer example with Canvas
<!DOCTYPE html>
<html>
<head>
<title>GridLayer Test</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.1/dist/leaflet.css" />
<style>
body {
padding: 0;
margin: 0;
@letmaik
letmaik / .travis.yml
Last active December 15, 2021 23:10
Deploy snapshots to Sonatype after Travis CI build
language: java
env:
global:
- SONATYPE_USERNAME=yourusername
- secure: "your encrypted SONATYPE_PASSWORD=pass"
after_success:
- python addServer.py
- mvn clean deploy --settings ~/.m2/mySettings.xml
@letmaik
letmaik / sample_nb.py
Created February 7, 2021 15:18
Embed matplotlib svg plots as regular image in jupyter notebooks (with "Save as..." and automatic resize)
# Jupyter Notebook content
import numpy as np
import matplotlib.pyplot as plt
import util
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)
@letmaik
letmaik / masked_adaptive_threshold.py
Last active January 22, 2021 20:51
Adaptive threshold with masking (two versions)
from __future__ import division
import numpy as np
from scipy.ndimage.filters import correlate1d
def masked_adaptive_threshold(image,mask,max_value,size,C):
'''
image must already be masked (unmasked areas=0)
mask is a boolean array
see http://stackoverflow.com/a/10015315/60982
@letmaik
letmaik / README.md
Last active November 23, 2020 04:37
Using travis_retry inside shell scripts together with set -e

If you want to use travis_retry from within your own shell script files then you first have to make the travis_retry shell function available by sourcing the travis_retry.sh file, otherwise you just get a "command not found" error. See example.sh for a full example.

Note that the original function as found in the travis-ci/travis-build repository was slightly modified to allow it to be used in a shell context where set -e is enabled.

For reference, a tweet by Travis CI saying that you should copy the travis_retry code as I've done here: https://twitter.com/plexus/status/499194992632811520

@letmaik
letmaik / histogram.py
Created June 29, 2014 11:39
Fast version of numpy histogram functions
'''
This module contains a fast replacement for numpy's histogramdd and histogram2d.
Two changes were made. The first was replacing
np.digitize(a, b)
with
np.searchsorted(b, a, "right")
@letmaik
letmaik / CartesianProduct.ps1
Created April 27, 2017 09:23
Cartesian product with keys (cross join) in PowerShell
function CartesianProduct {
param
(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[Hashtable]
$values = @{ Foo = 1..5; Bar = 1..10}
)
$keys = @($values.GetEnumerator() | ForEach-Object { $_.Name })
$result = @($values[$keys[0]] | ForEach-Object { @{ $keys[0] = $_ } })
if ($keys.Length -gt 1) {
@letmaik
letmaik / symlinks.psm1
Created March 8, 2017 19:02
Create symlinks to folders and files on Windows without admin privileges in Powershell