Skip to content

Instantly share code, notes, and snippets.

Avatar

Andreas Stöckel astoeckel

View GitHub Profile
@astoeckel
astoeckel / env_guard.py
Created Aug 11, 2021
Environment Guard
View env_guard.py
import os
class EnvGuard:
"""
Class used to temporarily set some environment variables to a certain value.
In particular, this is used to set the variable OMP_NUM_THREADS to "1" for
each of the subprocesses using cvxopt.
"""
def __init__(self, env={}):
self.env = env
@astoeckel
astoeckel / low_firing_rate_dynamics_sweep.py
Created Jul 23, 2021
Compute neuron transfer function
View low_firing_rate_dynamics_sweep.py
def compute_transfer_function(us, xs, dt=dt, smoothing=100.0):
Us = np.fft.fftshift(np.fft.fft(us))
Xs = np.fft.fftshift(np.fft.fft(xs))
fs = np.fft.fftshift(np.fft.fftfreq(len(us), dt))
wnd = np.exp(-np.square(fs) / np.square(smoothing))
wnd /= np.sum(wnd) * dt
UXs = np.fft.fftshift(np.fft.fft(Us * np.conj(Xs)))
@astoeckel
astoeckel / fuzzy.py
Created Feb 27, 2021
Fuzzy Jaccard similarity based search
View fuzzy.py
def fuzzy_find(term, lst, threshold=0.75, shingle_len=3):
# Create a set of n-grams of length shingle_len
def shingle(s):
shingles = []
for t in re.split(' ', s):
for i in range(max(1, len(t) - shingle_len)):
shingles.append(t[i:i+shingle_len])
return set(shingles)
# Computes the Jaccard similarity between two sets
@astoeckel
astoeckel / zorder.py
Created Jan 29, 2021
Python 2D z-order curve
View zorder.py
def zorder(N):
if N == 0:
return np.zeros((0, 0), dtype=np.int)
elif N == 1:
return np.zeros((1, 2), dtype=np.int)
else:
pnts = zorder(N - 1)
l = pnts.shape[0]
offs = 2 ** (N - 2)
res = np.zeros((4 * l, 2), dtype=np.int)
@astoeckel
astoeckel / nbdkit.service
Last active Nov 16, 2020
nbdkit systemd socket and service file
View nbdkit.service
# nbdkit.service
[Unit]
Description=nbdkit Service
After=nbdkit.socket
Requires=nbdkit.socket
[Service]
Environment="LD_LIBRARY_PATH=/opt/nbdkit/lib64"
Environment="TMPDIR=/var/lib/nbdkit/cache/"
@astoeckel
astoeckel / compile_and_install_nbdkit_centos_7.sh
Created Nov 15, 2020
Compiling and installing nbdkit on CentOS 7
View compile_and_install_nbdkit_centos_7.sh
#!/usr/bin/bash
# Downloads NBDKit and builds a minimal version. This minimal version only
# installs basic plugins and filters, and the SSH plugin.
# For compilation to succeed, you'll need to install
# yum install gcc gcc-c++ libssh-devel openssl-devel autoconf automake libtool
# Also, best create a user nbdkit
# adduser --system --user-group --shell /sbin/nologin --home-dir /opt/nbdkit --create-home nbdkit
@astoeckel
astoeckel / gist:fae7fd37a0f5c5f48bce03bee301fbcd
Created Apr 29, 2020
Libre Office Export All Sheets As CSV
View gist:fae7fd37a0f5c5f48bce03bee301fbcd
Sub ExportAllToCsv
document = ThisComponent
' Use the global string tools library to generate a base filename for each CSV
' based on the current prefixless filename
GlobalScope.BasicLibraries.loadLibrary("Tools")
DocumentName = Left(document.Namespace, Len(document.Namespace) - 1)
BasePath = Tools.Strings.DirectoryNameoutofPath(DocumentName, "/")
BaseFilename = Tools.Strings.GetFileNameWithoutExtension(DocumentName, "/")
@astoeckel
astoeckel / csc_matrix.cpp
Last active Jan 22, 2020
CSC Matrix Operations -- Code I wrote before I realised that Eigen can be made to directly interface with CSC matrices via SparseMatrix and makeCompressed()
View csc_matrix.cpp
/*
* libbioneuronqp -- Library solving for synaptic weights
* Copyright (C) 2020 Andreas Stöckel
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
@astoeckel
astoeckel / mount.qcow2
Last active Dec 25, 2019
Mounts a LUKS encrypted QCOW2 image
View mount.qcow2
#!/bin/bash
QCOW2_ACTION="$1"
QCOW2_FILENAME="$2"
QCOW2_MOUNTPOINT="$3"
if test -z "$QCOW2_ACTION" || test -z "$QCOW2_FILENAME" || test -z "$QCOW2_MOUNTPOINT"; then
echo "Usage: mount.qcow2 <mount|unmount> <FILENAME> <MOUNTPOINT>";
exit 1
fi
@astoeckel
astoeckel / transfer_rhythmdb_ratings.py
Created Aug 11, 2019
Python script that transfers the ratings from multiple Rhythmbox databases into a single Rhythmbox database. If the same song exists multiple times in the target database, only the one with the highest quality is assigned a rating.
View transfer_rhythmdb_ratings.py
#!/usr/bin/env python3
import os
import sys
import urllib.parse
import urllib.request
import lxml.etree as ET
import icu
import collections
import math