Skip to content

Instantly share code, notes, and snippets.

View cheind's full-sized avatar

Christoph Heindl cheind

View GitHub Profile
@cheind
cheind / caprecap.py
Last active April 23, 2024 06:35
Capture-recapture method to estimate population size
# Estimate the population size using capture-recapture method
# Assumes: experiment twice, closed population
import numpy as np
import matplotlib.pyplot as plt
T = 1000 # true pop size
N1 = 50 # size of sample 1
N2 = 30 # size of sample 2
@cheind
cheind / cv2ndc.py
Created September 28, 2023 11:10
OpenCV camera coordinates to OpenGL NDC matrix
import numpy as np
def cv_to_ndc(fx, fy, cx, cy, near, far, w, h):
"""Returns the 4x4 projection matrix that converts from OpenCV camera
coordinates to OpenGL NDC coordinates.
This takes into account that cameras in
- OpenCV has +z into scene, +y down the image
- OpenGL has -z into scene, +y up the image
@cheind
cheind / fractals.py
Last active July 24, 2023 13:01
Fractals from #Numberphile/Chaos Theory
import numpy as np
import matplotlib.pyplot as plt
def sierpinski_triangle():
"""Generates a Sierpinski triangle.
Given 3 anchor points and a trace point, the
next trace point is half-way between its current
location and a randomly chosen anchor.
@cheind
cheind / platform_generate.py
Created May 5, 2022 07:53
cross platform pip-compile helper
import argparse
import platform
import sys
import subprocess
from pathlib import Path
def describe() -> dict:
meta = {
"python": "py" + ".".join(platform.python_version_tuple()[:2]),
@cheind
cheind / blend.py
Last active March 21, 2022 16:26
Blending of motion parabolas
import numpy as np
import matplotlib.pyplot as plt
import dataclasses
@dataclasses.dataclass
class MotionEstimate:
coeffs: np.ndarray # 3x1
t0: float
degree: int = dataclasses.field(init=False)
@cheind
cheind / complexity.py
Created November 10, 2021 08:41
(Batched) Sample Entropy in PyTorch for measuring complexities of time-series (see https://en.wikipedia.org/wiki/Sample_entropy)
import torch
def sample_entropy(
x: torch.Tensor, m: int = 2, r: float = None, stride: int = 1, subsample: int = 1
):
"""Returns the (batched) sample entropy of the given time series.
Sample entropy is a measure of complexity of sequences that can be related
to predictability. Sample entropy (SE) is defined as the negative logarithm of
the following ratio:
@cheind
cheind / optcost.py
Last active April 12, 2021 10:01
Linear Programming for Optimizing Funding Costs. See https://doi.org/10.5281/zenodo.4607219 for documentation.
from scipy.optimize import linprog
import numpy as np
import pandas as pd
def print_metrics(df):
print('Total staff costs', df.to_numpy().sum())
print('Management cost ratio')
print(df.MgtStaffCosts / df.to_numpy().sum())
print('Partner cost ratio')
@cheind
cheind / matrix_transactions.py
Last active January 11, 2021 07:44
Transactional-like (undoable) matrix operations (row delete, column permute) implemented based on permutation matrices
import numpy as np
from itertools import count
def perm_matrix(perm_indices):
'''Returns the permutation matrix corresponding to given permutation indices
Here `perm_indices` defines the permutation order in the following sense:
value `j` at index `i` will move row/column `j` of the original matrix to
row/column `i`in the permuated matrix P*M/M*P^T.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@cheind
cheind / show_annotations.py
Created February 25, 2018 11:37
BeaverDam dense annotation viewer
import cv2
import json
import pandas as pd
import numpy as np
def convert_to_pandas(content):
events = []
for obj in content:
for f in obj['frames']:
events.append({