Skip to content

Instantly share code, notes, and snippets.

View sergeyprokudin's full-sized avatar

Sergey Prokudin sergeyprokudin

  • ETH Zürich
  • Zürich
View GitHub Profile
@sergeyprokudin
sergeyprokudin / count_steps.py
Created January 12, 2019 18:56
Triple Step (Puzzle 8.1. from "Cracking the Coding Interview")
def count_steps(n):
"""Determine how many ways there are to walk n steps, if you can jump over 1, 2 or 3 steps at the time
f(-1) = 0
f(0) = 1 # just stand?
f(1) = 1
f(2) = 2
f(3) = 4
f(4) = f(n-1)+f(n-2)+f(n-3)
@sergeyprokudin
sergeyprokudin / robot_in_a_grid.py
Last active January 13, 2019 09:34
Robot in a Grid (dynamic programming, puzzle 8.2 from "Cracking the coding interview")
from collections import deque
def check_path(grid, i=0, j=0, failed_points=None, current_path=None):
""" Find a route between upper-left and lower right corner of the grid,
if you are only allowed to walk right and down.
Input:
1 1 1
0 0 1
0 0 1
@sergeyprokudin
sergeyprokudin / plot2d.py
Created January 15, 2019 14:42
Plot a set of 2d figures
import matplotlib.pyplot as plt
%matplotlib inline
def plot2d(fig_lst, labels=None, figsize=(10, 10), savepath=None):
"""Plots a set of 2d shapes represented as 2d point arrays
Parameters
----------
fig_lst: list
@sergeyprokudin
sergeyprokudin / save_h5.py
Created March 7, 2019 13:05
Small function to dave dictionary of numpy arrays to H5 file
import h5py
import os
def save_h5(savepath, arr_dict):
if os.path.exists(savepath):
os.remove(savepath)
h5ds = h5py.File(savepath)
@sergeyprokudin
sergeyprokudin / plot_gauss_pdf.txt
Created April 7, 2019 16:10
plots Guassian density together with some ground truth value gt
import matplotlib.pyplot as plt
import scipy.stats as stats
def plot_gauss_pdf(mu, sigma, gt):
''' plots Guassian density together with some ground truth value gt
'''
x = np.linspace(mu - 5*sigma, mu + 5*sigma, 100)
plt.plot(x, stats.norm.pdf(x, mu, sigma))
plt.axvline(mu, c='blue', label='prediction mean')
@sergeyprokudin
sergeyprokudin / vae.py
Created June 7, 2019 14:49
Basic variational autoencoder in Keras
import tensorflow as tf
from keras.layers import Input, Dense, Flatten, Reshape, Dropout
from keras.models import Model, Sequential
from keras.optimizers import Adam
from keras.objectives import binary_crossentropy
from keras.layers.merge import concatenate
from keras.losses import mean_squared_error
def variational_autoencoder(n_input_features, latent_space_size=64, hlayer_size=256,
lr=1.0e-3, kl_weight=0.1):
@sergeyprokudin
sergeyprokudin / ball_pivoting.py
Last active November 30, 2023 15:37
Given a 3D point cloud, get unstructured mesh using Ball Pivoting algorithm
import open3d as o3d
import trimesh
import numpy as np
def ball_pivoting_reconstruction(xyz, radii=None):
"""Given a 3D point cloud, get unstructured mesh using ball pivoting algorithm
Based on this stack overflow code snippet:
https://stackoverflow.com/questions/56965268/how-do-i-convert-a-3d-point-cloud-ply-into-a-mesh-with-faces-and-vertices
@sergeyprokudin
sergeyprokudin / point_cloud_viz.py
Last active April 17, 2020 08:28
Simple point cloud visualization with pyntcloud library
"""
Simple point cloud visualization with pyntcloud library
"""
import pyntcloud as pynt
import pandas as pd
import numpy as np
def show_cloud(xyz, rgb=None, initial_point_size=0.01):
@sergeyprokudin
sergeyprokudin / set_latex_font_matplotlib.py
Last active April 28, 2020 13:08
Setting latex font for matplotlib plots
# details:
# https://stackoverflow.com/questions/11367736/matplotlib-consistent-font-using-latex/27697390
from matplotlib import rc
import matplotlib
matplotlib.rcParams['mathtext.fontset'] = 'stix'
matplotlib.rcParams['font.family'] = 'STIXGeneral'
@sergeyprokudin
sergeyprokudin / chamfer_distance.py
Created April 30, 2020 14:04
Vanilla Chamfer distance computation in NumPy
import numpy as np
from sklearn.neighbors import NearestNeighbors
def chamfer_distance(x, y, metric='l2', direction='bi'):
"""Chamfer distance between two point clouds
Parameters
----------
x: numpy array [n_points_x, n_dims]