Skip to content

Instantly share code, notes, and snippets.

View jonashaag's full-sized avatar

Jonas Haag jonashaag

View GitHub Profile
@jonashaag
jonashaag / cirm.py
Last active November 18, 2021 02:09
Python cIRM (Complex Ideal Ratio Mask), ORM (Optimal Ratio Mask), IRM (Ideal Ratio Mask), PSM (Phase Sensitive Mask)
def cirm(y, s, K=10, C=0.1, flat=True):
y = librosa.core.stft(y.astype('float64'), 256, 64).astype('complex128')
s = librosa.core.stft(s.astype('float64'), 256, 64).astype('complex128')
mr = (np.real(y) * np.real(s) + np.imag(y) * np.imag(s))/(np.real(y)**2 + np.imag(y)**2)
mi = (np.real(y) * np.imag(s) - np.imag(y) * np.real(s))/(np.real(y)**2 + np.imag(y)**2)
m = mr + 1j * mi
if flat:
return m
else:
return K * ((1 - np.exp(-C * m))/(1 + np.exp(-C * m)))
@jonashaag
jonashaag / gist:7ca5b19abf537a447d0de1b923043f45
Last active March 28, 2024 12:20
T60/RT60, C50/C80, D in Python
import librosa
from scipy.signal import butter, lfilter
SR = 8000
NFFT = 256 # change ~proportionally to SR
def butter_bandpass(lowcut, highcut, fs, order=5):
nyq = 0.5 * fs
low = lowcut / nyq
import requests
import os
url = 'http://' + os.environ['COLAB_TPU_ADDR'].split(':')[0] + ':8475/requestversion/2.2.0-dev20200311'
resp = requests.post(url)
print(resp)
%pip install tf-nightly==2.2.0-dev20200311
if __name__ == '__main__':
import dask.distributed
client = dask.distributed.Client()
queue = dask.distributed.Queue('queue1')
def f():
return 42
class PickleableCtypesFuncPtr:
"""A version of a ctypes._FuncPtr that can be pickled. The shared library
must be available in the depickling environment.
"""
def __init__(self, dll_type, lib, name):
self.__setstate__((dll_type, lib, name))
def __setstate__(self, state):
self.state = state
dll_type, lib, func = self.state
@jonashaag
jonashaag / lockgil.py
Last active May 5, 2020 15:44
Pure Python solution to locking the GIL
# Use libc in ctypes "PyDLL" mode, which prevents CPython from
# releasing the GIL during procedure calls.
_libc_name = ctypes.util.find_library("c")
if _libc_name is None:
raise RuntimeError("Cannot find libc")
libc_py = ctypes.PyDLL(_libc_name)
...
libc_py.usleep(...)
<!doctype html>
<script type="application/javascript">
const s = `
application/mp4
application/mp4; codecs=bogus
application/octet-stream
application/octet-stream; codecs='avc1.42E01E, mp4a.40.2'
application/octet-stream; codecs='mp4a.40.2'
application/octet-stream; codecs='theora, vorbis'
application/octet-stream; codecs='vorbis'
@jonashaag
jonashaag / django_change_translation_fallback_language.py
Created September 24, 2018 11:18
Django change translation fallback language
# This is a hack so that French translation falls back to English translation,
# not German translation (which is the default locale and the original
# strings).
from django.utils.translation import trans_real
class MyDjangoTranslation(trans_real.DjangoTranslation):
def _add_fallback(self, localedirs=None):
if self._DjangoTranslation__language[:2] in {'de', 'en'}:
return super()._add_fallback(localedirs)
@jonashaag
jonashaag / celery_model_serializer.py
Last active March 28, 2022 23:14
Celery Django model serializer – pass Django model instances as arguments to Celery tasks
"""Our convenience Celery Task wrapper that allows us to conveniently pass
model instances as Task arguments.
It "serializes" model instances to IDs and "deserializes" these IDs to model
instances upon task execution.
Serialized representation of a model instance is (sentinel, app_name, model_name, pk).
"""
import celery
from django.apps import apps
@jonashaag
jonashaag / aws_fargate_docker_application_load_balancer_without_public_ip.md
Last active March 14, 2024 23:34
AWS Fargate Docker Application Load Balancer Howto (without public IP)

AWS Fargate Docker Simple Deployment Setup with SSL termination

How to:

  • create a Docker-based AWS Fargate/ECS deployment
  • without the Docker containers having a public IP
  • with an Application Load Balancer as reverse proxy / SSL termination proxy sitting in front of the containers

For Fargate/ECS to be able to access your Docker images hosted on ECR (or somewhere else) you'll have to allow outbound internet access to the Fargate subnets. Here's how you do it.