Skip to content

Instantly share code, notes, and snippets.

View vaclavcadek's full-sized avatar

Václav Čadek vaclavcadek

  • Stealth
  • Prague, Czech Republic
View GitHub Profile
@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 = []
from scipy import stats
import matplotlib.pyplot as plt
import numpy as np
def PoissonPP( rt, Dx, Dy=None ):
'''
Determines the number of events `N` for a rectangular region,
given the rate `rt` and the dimensions, `Dx`, `Dy`.
Returns a <2xN> NumPy array.
'''
@vaclavcadek
vaclavcadek / Dockerfile
Created December 19, 2017 12:26
Jupyter notebook minimal configuration.
FROM ubuntu:latest
MAINTAINER Vaclav Cadek
RUN apt-get update
RUN apt-get install python-virtualenv -y
RUN virtualenv -p /usr/bin/python3 ~/venv
RUN . ~/venv/bin/activate
RUN ~/venv/bin/pip install numpy pandas matplotlib jupyter
@vaclavcadek
vaclavcadek / consumer.py
Last active December 6, 2017 12:06
Simple Kafka communication.
from kafka import KafkaConsumer
consumer = KafkaConsumer('fast-messages', bootstrap_servers='localhost:9092')
for message in consumer:
print(message)
def animate(i):
return plt.plot(x[:i], y[:i], c='r')
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)
fig = plt.figure(figsize=(12, 8))
plt.xlim(0, 2 * np.pi)
plt.ylim(-1, 1)
anim = animation.FuncAnimation(fig, animate, frames=100)
display(HTML(anim.to_html5_video()))
@vaclavcadek
vaclavcadek / shuffle_array
Created September 9, 2017 12:41
Elegant way to shuffle numpy array - e.g. before training
permutation = list(np.random.permutation(m))
shuffled_X = X[:, permutation]
shuffled_Y = Y[:, permutation].reshape((1,m))
@vaclavcadek
vaclavcadek / decorators.py
Created August 10, 2017 13:31
Example decorators in Python.
def logger(func):
def wrapped(*args, **kwargs):
print('Calling {} with args = {} and kwargs = {}'.format(func, args, kwargs))
return func(*args, **kwargs)
return wrapped
@logger
def factorial(n):
if n == 1:
return 1
@vaclavcadek
vaclavcadek / generate_gif.py
Created June 30, 2017 13:06
How to generate animation using numpy arrays.
from matplotlib.animation import FuncAnimation
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots(figsize=(5, 8))
def update(i):
im_normed = np.random.random((64, 64))
ax.imshow(im_normed)
@vaclavcadek
vaclavcadek / async.py
Last active June 18, 2017 11:20
Asyncio vs. sync approach
from concurrent import futures
import time
def long_running_task(result):
print('Task is running!')
time.sleep(1)
return result
start = time.perf_counter()