Skip to content

Instantly share code, notes, and snippets.

@martenlienen
martenlienen / sampling.py
Created July 7, 2023 13:01
Implementation of the [alias method](https://en.wikipedia.org/wiki/Alias_method) for sampling from a categorical distribution in constant time
import heapq
import matplotlib.pyplot as pp
import numpy as np
rng = np.random.default_rng()
k = 20 # Number of classes
n = 10_000_000 # Number of samples
@martenlienen
martenlienen / backward_tree.py
Created October 17, 2021 09:56
Utilities to inspect the backwards tree in pytorch
import gc
import sys
from collections.abc import Iterable
from dataclasses import dataclass
import torch
from calmsize import size
def get_tensors():
@martenlienen
martenlienen / TUM.gpl
Created September 21, 2021 15:24
TUM Corporate Design Color Palette for GIMP and Inkscape
GIMP Palette
Name: TUM Corporate
Columns: 0
# Color palette with the official colors from TUM's corporate design [1].
#
# [1] https://portal.mytum.de/corporatedesign/index_html/vorlagen/index_farben
0 101 189 Pantone 300 C
255 255 255 White
0 0 0 Black
0 82 147 Pantone 301
@martenlienen
martenlienen / format_cfm_schedule.py
Last active June 24, 2021 21:20
Extract CF Munich's schedule from their website into an excel sheet. The userscript extracts the data from their eversports widget into JSON and the python script formats that data into an excel sheet.
#!/usr/bin/env python
import argparse
import json
from pathlib import Path
from openpyxl import Workbook
from openpyxl.styles import Alignment, Font
WEEKDAYS = [
@martenlienen
martenlienen / colorblind.gpl
Created June 22, 2021 11:06
Generate inkscape / gimp palettes from seaborn and matplotlib color maps
GIMP Palette
Name: colorblind (seaborn)
Columns: 0
#
1 115 178
35 176 253
145 215 254
222 143 5
250 185 70
252 220 162
@martenlienen
martenlienen / compile_sysimage.jl
Created October 6, 2020 23:08
Iteratively compile all project dependencies into a custom sysimage
#!/usr/bin/env julia
import Pkg
using UUIDs
using PackageCompiler
function flatten_dependencies(uuid::UUID, package_infos::Dict{UUID, Pkg.Types.PackageInfo})
deps = Set{UUID}()
new = [uuid]
while !isempty(new)
@martenlienen
martenlienen / main.cpp
Created March 18, 2018 18:49
rvalue method and move assignment shenanigans
#include <iostream>
#include <string>
class Message {
public:
Message &operator=(const std::string &&msg) && {
std::cout << msg << std::endl;
return *this;
}
@martenlienen
martenlienen / main.cpp
Created December 2, 2015 18:16
vector casting trickery
#include <array>
#include <iostream>
#include <vector>
int main() {
std::vector<std::array<int, 2>> as{{1, 2}, {3, 4}, {7, -1}};
int* buffer = (int*)as.data();
for (int i = 0; i < as.size() * 2; i++) {
std::cout << buffer[i] << std::endl;
x = X.find(params[:id])
patch = Hana::Patch.new(JSON.parse(request.body.read))
json = JSON.parse(HAL::Writers::XWriter.new(x).to_json)
patched = patch.apply(json)
HAL::Writers::XWriter.new(x).from_json(patched.to_json)
x.save
// Loading
var resource = $.get("https://url/...");
// Manipulating
var updated = doSomething(resource);
// Updating
$.patch(resource._links.self.href, diff(resource, updated))