Skip to content

Instantly share code, notes, and snippets.

@alexandru-dinu
alexandru-dinu / birthday_paradox.py
Last active January 21, 2019 09:15
Birthday paradox
import numpy as np
from tqdm import tqdm
import matplotlib.pyplot as plt
res = []
N = 1000
Bs = np.ceil(np.logspace(1, 6, 24)).astype(np.int32)
for B in tqdm(Bs):
from PIL import Image
import sys
from random import shuffle
import os
imgs = [x.strip() for x in open(sys.argv[1])]
r, c = int(sys.argv[2]), int(sys.argv[3])
w, h = Image.open(imgs[0]).size
#include <iostream>
#include <string>
class Cat {
public:
std::string s;
Cat (const std::string &s) : s(s) {}
Cat (const Cat &other) {
@alexandru-dinu
alexandru-dinu / HoG.py
Created September 13, 2019 14:04
quick-and-dirty hog
def hog(mag, angle, bin_dist=20):
h, w = mag.shape
bins = defaultdict(lambda: 0)
locs1 = (np.floor(angle / bin_dist) * bin_dist).astype(int)
locs2 = locs1 + bin_dist
ihs = (angle - locs1) / bin_dist
ils = 1 - ihs
@alexandru-dinu
alexandru-dinu / color_projection.py
Created October 16, 2019 20:44
3D color projection
import cv2
import seaborn as sns
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm, colors
# img = rgb ...
pixel_colors = img.reshape(-1, 3)
norm = colors.Normalize(vmin=-1.,vmax=1.)
norm.autoscale(pixel_colors)
@alexandru-dinu
alexandru-dinu / plots.py
Last active September 29, 2020 20:40
Nicer plots
import pylab as pl
import seaborn as sns
import matplotlib
from matplotlib import pyplot as plt
from IPython import display
%matplotlib inline
%config InlineBackend.figure_format = 'retina'
sns.set()
# or
@alexandru-dinu
alexandru-dinu / misc.py
Last active January 26, 2021 11:26
Misc
## IMPORTING
# add top-dirs to path
BASE_PATH = Path(__file__).resolve().parents[1]
sys.path[:0] = [str(BASE_PATH / 'src'), str(BASE_PATH)]
## LOGGING
def setup_logger(name, level, path=None):
if path is None:
import sys
import time
import logging
import multiprocessing as mp
def work(x):
ys = []
if x % 2 == 0:
@alexandru-dinu
alexandru-dinu / mmr.py
Last active October 14, 2021 09:16
Maximal Marginal Relevance (Carbonell and Goldstein, 1998)
import numpy as np
import torch as T
def maximal_marginal_relevance(
score_di_q: T.Tensor,
score_di_dj: T.Tensor,
*,
lam: float,
num_iters: int,
@alexandru-dinu
alexandru-dinu / fizzbuzz.cpp
Last active November 4, 2021 15:51
FizzBuzz
// adapted from
// https://wiki.haskell.org/Haskell_Quiz/FizzBuzz/Solution_Acontorer
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
const vector<pair<int, string>> tags = { { 3, "Fizz" },
{ 5, "Buzz" },