Skip to content

Instantly share code, notes, and snippets.

@el-hult
el-hult / yuv4mpeg2_sample.py
Created August 20, 2021 12:20
This code generates a very short video in the format of Yuv4mpeg2. It should be easily exendible to accept a series om bitmap images and create a video out of them.
WIDTH = 100
HEIGHT = 100
DURATION = 1 # seconds
FRAMES = 25*DURATION
def rgb2ycrcb(r,g,b):
""" ITU-R BT.601 (formerly CCIR 601) transformation
input in range 0-255
output in range 16-235 (headroom/footroom) for Y'
output in range 16-240 (headroom/footroom) for Cr/Cb
@el-hult
el-hult / rpg.hs
Created July 6, 2021 12:25
A simple haskell program that is the base minimum for a text based dungeon crawler.
module RPG where
import Text.Read (readMaybe)
type UIState = String
data Domain = Domain (UIState,World) deriving Show
data World = World {loc:: Int} deriving Show
data Dir = L | R deriving (Read, Show)
{- | By using the "deriving (Read)" we get a low-code input mechanism, but you should probalbly write
your own input parser
-}
@el-hult
el-hult / while.hs
Created July 1, 2021 09:13
A simple While-loop in Haskell using the State Monad
-- Inspired by https://stackoverflow.com/questions/17719620/while-loop-in-haskell-with-a-condition
import Control.Monad.State
type IsZero = Bool
{-
This is in State monad. I.e. it is a function, that when given an initial state (Int,IsZero) will produce a tuple of ((Int,IsZero),())
We can see this as an action on our state space with no output value.
-}
@el-hult
el-hult / cals2tiff.py
Last active June 13, 2021 20:49
Code for converting a CALS raster file to TIFF. Using vanilla Python.
"""
Python script that takes a folder, and for each CALS Raster file, it converts to a TIFF file.
https://en.wikipedia.org/wiki/CALS_Raster_file_format
file ending ".cal"
It so happens that TIFF can be compressed with Group 4 compression (as in faxes), and that is the compression format of CALS Raster images Type 1.
class MemorizingNormalizer(nn.Module):
def __init__(self, d, eps, rho):
super().__init__()
self.means = nn.Parameter(torch.zeros(d), requires_grad=False)
self.vars = nn.Parameter(torch.ones(d), requires_grad=False)
self.eps = nn.Parameter(torch.tensor(eps, dtype=float), requires_grad=False)
self.rho = nn.Parameter(torch.tensor(rho, dtype=float), requires_grad=False)
def forward(self, x):
self.means.data = self.means * self.rho + (1 - self.rho) * x.mean(axis=0)
@el-hult
el-hult / random_variables_monkeypatch.py
Created September 23, 2019 10:27
A monkay-patching way to create random variables with overloaded plus operator. Not a pythonic way to do it, since monkey patching is generally frowned upon.
# standards
from itertools import islice
import types
# 3rd party
import numpy as np
class RandomVariable():
def __iter__(self):
@el-hult
el-hult / random_variables.py
Created September 23, 2019 10:25
Some classes for random variables, in a Pythonic way with inheritance. Fun way to make universal random number generation. Might be inefficient, but still very cool.
# standards
from abc import ABC
from itertools import islice
from collections import Counter
import types
# 3rd party
import numpy as np
import scipy.stats as stats
import matplotlib.pyplot as plt
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@el-hult
el-hult / typed_currencies_37.py
Last active August 22, 2019 19:22
An example on how one can use the typings module to make currency conversion work like a charm. The code uses only python 3.7 features.
import urllib.request
import json
import typing
import functools
SEK = typing.NewType('SEK',float)
EUR = typing.NewType('EUR', float)
ConversionRate = typing.NewType('ConversionRate',float)
ThreeLetterCurrencyCode = str # ISO4217
@el-hult
el-hult / comonadVectorDiff.hs
Created May 7, 2019 19:46
Finding right angles between 2D vectors using comonads
-- This is using a haskell comonad to find right angles between given geometrical vectors
-- The code is kind of overkill, but it is a cute showcase of comonads
-- a FocusSet. One element in Focus, and all other elements in a list.
data Fs a = Fs a [a] deriving (Show)
instance Functor Fs where
fmap fun (Fs k l) = Fs (fun k) (map fun l)