Skip to content

Instantly share code, notes, and snippets.

View wrongu's full-sized avatar

wrongu

View GitHub Profile
@wrongu
wrongu / Dockerfile
Created April 20, 2023 21:05
Minimal demo of using BayesKit + PosteriorDB + BridgeStan, plus a Dockerfile for a container to run it in
# Dockerfile for container with Python, Stan, Bridgestan, BayesKit, and PosteriorDB
FROM python:3.10.11-bullseye
# Install git
RUN apt-get install -y git
# Install BayesKit
RUN pip install --upgrade pip \
&& pip install git+https://github.com/flatironinstitute/bayes-kit.git
data {
// # of data points
int<lower=1> N;
// Observed x
vector[N] x_obs;
// Observed y
vector[N] y_obs;
// Estimate of measurement error on x_obs
vector<lower=0>[N] err_x_obs;
@wrongu
wrongu / conditional_argparse.py
Created November 25, 2022 22:23
Argparse action-handler trick to add new command line arguments at parsing time depending on value of some other argument
import argparse
from typing import Dict
class ActionHandler(argparse.Action):
def __init__(self, *args, **kwargs):
super(ActionHandler, self).__init__(*args, **kwargs)
# ran_once is used to flag if __call__ has been run yet. This avoids an infinite loop.
self.ran_once = False
@wrongu
wrongu / response_patterns_3x3.py
Last active April 25, 2017 15:01
Script to generate and count all unique 3x3 'response patterns' for RocAlphaGo.
# A pattern is a length-8 tuple of values in {-3, -2, -1, 0, 1, 2, 3} where
# the sign indicates color and the magnitude indicates liberties. The center
# of the 3x3 grid must be empty to consider the location. Indices of a
# pattern:
#
# 0 1 2
# 3 4 5
# 6 7 8
WHITE = -1
@wrongu
wrongu / compare_go_features.py
Last active May 10, 2016 12:59
Script to diagnose differences in hdf5 features between RocAlphaGo's game converter and Thouis' sgf_to_hdf5 script. Usage: `python compare_go_features file1.hdf5 file2.hdf5 [comma,separated,feature,names]
import sys
import h5py
import pdb
import numpy as np
feature_sizes = {
"board": 3,
"ones": 1,
"turns_since": 8,
"liberties": 8,
@wrongu
wrongu / run_gtp_engine.py
Last active April 21, 2016 00:27
script to execute GTP engine from https://github.com/wrongu/gtp
#!/usr/bin/env python
from gtp import Engine
from gtp import MinimalGame
import sys
import SocketServer
class GTPRequestForwarder(SocketServer.BaseRequestHandler):
def handle(self):
class Constraint(object):
def __init__(self, guess, lights):
self.n = guess
self.l = lights
self.g = sum(1 if c is 'g' else 0 for c in lights)
self.r = sum(1 if c is 'r' else 0 for c in lights)
def consistent(self, m):
# given m as 'true', return true iff this constraint would have come about from m
in_place = [v[0]==v[1] for v in zip(m, self.n)]
@wrongu
wrongu / graph.js
Last active January 7, 2016 16:05
A very tiny plotting module for use with the html5 canvas
/* graph.js
*
* written by Richard Lange, January 2016
*
* This file provids a small set of html5 canvas plotting routines.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
@wrongu
wrongu / braid.md
Last active August 29, 2015 14:01
Braid Algorithm

This is an explanation of my answer to the code golf question "Algorithmic Braiding - for mother's day". The problem asks this:

Your task is to create a program that, when given a number of strands and number of iterations of a braid, will tell where each strand goes.

We'll call the number of strands N and the number of iterations I. Let's start by looking at some simple outputs. The classic example is 3 strands:

1 2 3
 \     iteration 1
2 1 3

/ iteration 2

@wrongu
wrongu / text_to_image.py
Last active August 19, 2023 14:00
Convert Text to an Image with an 8x8 pixel font
#!/usr/bin/python
#
# First make sure you have the font saved: https://github.com/trillek-team/trillek-vcomputer-module/blob/master/assets/EC1271-8x8.png
from PIL import Image
font_file = "./font-8x8.png"
font_img = Image.open(font_file)
# map from character to zero-indexed (x,y) index.