Skip to content

Instantly share code, notes, and snippets.

View nat-n's full-sized avatar

Nat Noordanus nat-n

View GitHub Profile
@nat-n
nat-n / bitwiseAddition.js
Last active August 29, 2015 14:10
Implementing naive integer addition from bitwise operators in javascript.
function add(a,b){
var x, o;
do {
x = a & b;
o = a | b;
a = o ^ x;
b = x << 1;
} while (x)
return o;
@nat-n
nat-n / three-value-toggle.html
Created June 16, 2014 15:21
A javascript and styles to implement a new input type which resembles a checkbox but with three arbitrary values instead of just two.
<html>
<head>
<meta charset="UTF-8">
<title>Three Value Toggle</title>
</style>
</head>
<body>
<input name="foo" type="tvg" value="Maybe">
<input name="bar" type="tvg" value="True">
@nat-n
nat-n / dserver.rb
Created May 3, 2014 21:17
An experimental class wrapping druby to make it easy to spawn and control Distributed Ruby servers from within a ruby context. Thus allowing a ruby program to relatively painlessly spawn and control "worker" subprocesses.
#!/usr/bin/env ruby
require 'tempfile'
require 'drb/drb'
class DServer
attr_reader :active_server
attr_reader :service
@nat-n
nat-n / work_queue.rb
Last active August 29, 2015 14:00
A simple Ruby class for managing work queues to be run in series or in parallel.
class WorkQueue
attr_reader :result
def initialize
@q = []
end
def push &block
@q << block
@nat-n
nat-n / analyze_pi.rb
Last active December 31, 2015 10:38
Analyses the first 100 billion digits of pi to determine the greatest number of decimal places separating consecutive occurrences of the same digit. The purpose is to discover the first gap of more than 255 decimal places between consecutive occurrences of a base 10 digit in pi. Downloads segments of 100 000 000 digits at a time as zipped text f…
#!/usr/bin/env ruby
# encoding: utf-8
#
# Created by Nat Noordanus on 2013-12-13.
# Description:
# Analyses the first 100 billion digits of pi to determine the greatest number
# of decimal places separating consecutive occurrences of the same digit.
# The purpose is to discover the first gap of more than 255 decimal places
# between consecutive occurrences of a base 10 digit in pi.
@nat-n
nat-n / read_variable_length_integer_file.js
Last active December 30, 2015 15:19
A proof of concept script for streaming a binary file of variable length integers through a nodejs context.
var fs, vi, sys, rs, bufferSize, remainder, startTime;
startTime = Date.now();
fs = require('fs');
vi = require('varint');
sys = require('sys');
rs = fs.createReadStream('/path/to.file');
total = 0;
@nat-n
nat-n / volume_stamper.py
Last active December 17, 2015 11:49
Takes a *stamp* volume file (such as a single label image of an oil capsule on the left side of the temple) and adds it to each image in the input directory and writes the resulting images to the output directory. The stamp file must match the dimensions of all the image files.
#!/usr/bin/python
# created on 18/5/13 by Nat Noordanus n@natn.me
import os, argparse
parser = argparse.ArgumentParser()
parser.add_argument("input_dir")
parser.add_argument("stamp_file")
# Provides an efficiently searchable tree index of a given array of stringafiable objects.
# Is specifically designed to be much faster than using Array's _index_ or _include?_ methods for locating a numerical value within an Array.
# However it should work just as well with Arrays of strings or other objects that respond appropriately to _to_s_.
class QuickIndex
# @param ary [Array] of items to be indexed.
# @param stop_char (String) which should not occur as a substring in any of the stringified objects in ary.
#
@nat-n
nat-n / convert3d.rb
Last active December 16, 2015 10:29
A very light wrapper for the InsightToolkit Convert3D tool which must be downloaded seperately. See http://www.itksnap.org/pmwiki/pmwiki.php?n=Convert3D.Convert3D . It is assumed that the Convert3D tool resides in the shall path as c3d. Otherwise Convert3D.path= must be used to set the location of the executable.
# A very light wrapper for the InsightToolkit Convert3D tool which must be downloaded seperately.
# See http://www.itksnap.org/pmwiki/pmwiki.php?n=Convert3D.Convert3D
# It is assumed that the Convert3D tool resides in the shall path as c3d.
# Otherwise Convert3D.path= must be used to set the location of the executable.
module Convert3D
@@c3d_path = "c3d" # assume c3d is in the environment path by default
@@Formats = [".nrrd", ".hdr", ".img", ".img.gz", ".dcm", ".cub", ".mha", ".df3", ".nii.gz"]
def self.path= path
@nat-n
nat-n / mlv_merge.py
Last active December 14, 2015 19:49
These python modules provide functions for dealing with multi label volumetric images (such as anatomical atlases) in .nii format or any similar format compatible with nibabel. Both can also be used as command line tools. mlv_merge.py takes a directory of single-label or multilabel volumetric image files and creates a new nifiti multi-label imag…
#!/usr/bin/env python
import os
import sys
import argparse
import collections
import numpy
import nibabel as nib