Skip to content

Instantly share code, notes, and snippets.

View vaclavcadek's full-sized avatar

Václav Čadek vaclavcadek

  • AmpX
  • Prague, Czech Republic
View GitHub Profile
@vaclavcadek
vaclavcadek / memory_profiler.py
Created January 11, 2022 16:19
Simple snippet of how to profile memory in python.
import time
import gc
import psutil
import humanize
gc.collect()
time.sleep(2)
mem_before = psutil.virtual_memory()[3]
@vaclavcadek
vaclavcadek / single_channel_ica.py
Created October 22, 2021 17:29
A not so successful attempt to perform single channel ICA using Wavelet Transform.
import itertools
from scipy import signal
import pywt
import numpy as np
import matplotlib.pyplot as plt
from sklearn.decomposition import FastICA
# #############################################################################
@vaclavcadek
vaclavcadek / dockerize.sh
Created May 14, 2021 11:38
Simple TF serving example with normalization layer being part of TF graph wrapped in TF serving REST API.
# Download the TensorFlow Serving Docker image and repo
docker pull tensorflow/serving
# Start TensorFlow Serving container and open the REST API port
docker run -t --rm -p 8501:8501 -v "/home/vaclav/PycharmProjects/the-algorithm-lab/tensorflow2/practice/deployment/my_model:/models/my_model" -e MODEL_NAME=my_model tensorflow/serving &
# Query the model using the predict API
curl -d '{"instances": [17.99000,20.57000,19.69000,11.42000,20.29000,12.45000,18.25000,13.71000,13.00000,12.46000,16.02000,15.78000,19.17000,15.85000,13.73000,14.54000,14.68000,16.13000,19.81000,13.54000,13.08000,9.50400,15.34000,21.16000,16.65000,17.14000,14.58000,18.61000,15.30000,17.57000]}' \
-X POST http://localhost:8501/v1/models/my_model:predict
@vaclavcadek
vaclavcadek / svg_write_streamlit.py
Created April 7, 2021 13:30
How to write SVG file instead of broken (blurry) matplotlib.
def svg_write(fig, center=True):
"""
Renders a matplotlib figure object to SVG and embedd it as base64.
Disable center to left-margin align like other objects.
Shamelessly taken from:
https://discuss.streamlit.io/t/display-svg/172
"""
# Save to stringIO instead of file
imgdata = StringIO()
@vaclavcadek
vaclavcadek / backward_compatibility.py
Created March 25, 2021 13:00
Forward and backward compatibility illustration.
import json
import pickle
class Foo:
""" An old Foo."""
def __init__(self, a, b):
self.a = a
self.b = b
@vaclavcadek
vaclavcadek / Dockerfile
Last active April 2, 2024 17:37
Simple example of AWS Lambda + custom image, usage: 1) docker build . -t test/hello-world 2) docker run --rm -p 9000:8080 test/hello-world:latest 3) curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"foo": "test"}'
FROM python:3.8-alpine
# Define global args
ARG FUNCTION_DIR="/home/app/"
RUN apk add --no-cache \
libstdc++
# Install aws-lambda-cpp build dependencies
RUN apk add --no-cache \
@vaclavcadek
vaclavcadek / fourier_transform.py
Created March 4, 2021 16:24
Code for amazing 3B1B lecture (https://youtu.be/spUNpyF58BY) about Fourier Transform intuition.
from scipy.integrate import simpson
import matplotlib.pyplot as plt
import numpy as np
def almost_fourier_transform(g: np.ndarray, t: np.ndarray, f: float):
# approximate center of mass as a mean
t1 = np.min(t)
t2 = np.max(t)
scaling_factor = (1 / (t2 - t1))
@vaclavcadek
vaclavcadek / 51_1.tsp
Created September 13, 2018 15:46
Simple implementation (I don't remember where I found it, but it was in C/C++) of Lin-Kernighan heuristic for TSP.
51
27 68
30 48
43 67
58 48
58 27
37 69
38 46
46 10
61 33
@vaclavcadek
vaclavcadek / rgb2hex.py
Created August 8, 2018 13:58
Transform RGB color to hex.
def rgb2hex(rgb):
return ''.join([format(val, '02X') for val in rgb])
@vaclavcadek
vaclavcadek / ortools_cp.py
Created July 30, 2018 16:05
Simple Ortools solver for CP
from ortools.sat.python import cp_model
class SolutionPrinter(cp_model.CpSolverSolutionCallback):
def __init__(self, variables):
super().__init__()
self.variables = variables
self.solutions = []