Skip to content

Instantly share code, notes, and snippets.

View dansondergaard's full-sized avatar

Dan Søndergaard dansondergaard

View GitHub Profile
int *translate_observations_to_indexes(struct hmm_t *hmm, const char *observations) {
int *observations_idx = malloc(strlen(observations) * sizeof(int));
for (int i = 0; i < strlen(observations); i++) {
for (int j = 0; j < hmm->observables_size; j++) {
if (observations[i] == hmm->observables[j]) {
observations_idx[i] = j;
}
}
}
return observations_idx;
class Node(object):
def __init__(self, name, checkpoint=False):
self.name = name
self.explored = False
self.checkpoint = checkpoint
self.edges = []
def add_edge(self, other):
self.edges.append(other)
@dansondergaard
dansondergaard / print_stream_redirect.py
Created August 26, 2013 17:35
I recently discovered this awesome redirection syntax for the print statement in Python. Also, it reveals quite nicely that print writes the newline separately to the stream.
class Logger(object):
def __init__(self):
self.records = []
def write(self, record):
self.records.append(record)
logger = Logger()
print >> logger, "hello"
print >> logger, "world"
@dansondergaard
dansondergaard / naivebayes.r
Created October 19, 2013 23:43
R implementation of the Naive Bayes classifier example from [Wikipedia](http://en.wikipedia.org/wiki/Naive_Bayes_classifier#Examples).
# naivebayes.txt
#
# sex height weight footsize
# male 6 180 12
# male 5.92 190 11
# male 5.58 170 12
# male 5.92 165 10
# female 5 100 6
# female 5.5 150 8
# female 5.42 130 7
@dansondergaard
dansondergaard / decisiontree.py
Created October 30, 2013 00:11
A simple decision tree for classification.
"""Decision Tree"""
__all__ = ['grow', 'classify']
class Node(object):
def __init__(self, error, dimension, value, left, right):
self.error = error
self.dimension = dimension
self.value = value
def compute_error(partition):
"""Find the majority category of the partition. Count how big
a part of the partition that the majority category is.
Return the ratio of things that are *not* of the majority category."""
categories = map(get_category, partition)
majority = max(set(categories), key=categories.count)
return 1 - (categories.count(majority) / float(len(partition)))
# This is what a typical partition looks like. The inner tuple may
# be very large (in the thousands) and the list of the length may
@dansondergaard
dansondergaard / symbol.py
Created December 20, 2013 11:57
Symbols in Python.
class symbol(object):
_symbols = {}
def __new__(cls, name):
if name not in symbol._symbols:
symbol._symbols[name] = object.__new__(cls, name)
return symbol._symbols[name]
def __init__(self, name):
self.name = name
import xml.etree.ElementTree as et
import base64
import array
def parse_file(filename_or_obj):
tree = et.parse(filename_or_obj)
for spectrum in tree.findall('.//spectrum'):
mz_elm = spectrum.find('mzArrayBinary').find('data')
intensity_elm = spectrum.find('intenArrayBinary').find('data')
#!/usr/bin/env python3
import sys
import os
import os.path
import argparse
import subprocess
DEVNULL = open(os.devnull, 'wb')
@dansondergaard
dansondergaard / notifications.fish
Created May 23, 2015 13:21
Notification when long-running process has completed (only for OS X and Fish 2.2b1-10 or newer).
#################
# notifications #
#################
function __record_cmd_begin -d "Record time when command started." -e fish_preexec
set -g cmd_begin_time (eval date +"%s")
end
function __record_cmd_end -d "Record time when command ended." -e fish_postexec
set -g cmd_end_time (eval date +"%s")