Posts Tagged tool

APEMoST

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

Recently I have been busy updating APEMoST, which is a MCMC sampler for Bayesian inference. This can be used as a statistical procedure to estimate parameters of a model. That sounds pretty generic, and indeed it is. One specific example would be to determine the orbit parameters of exoplanets. You can also specify multiple models (1 planet, 2 planets, no planets) and calculate (with the tool) which model is more likely.

Everything is at http://apemost.sourceforge.net/. Papers pending ;-)

No Comments

Merging and sorting sorted sources

This neat little algorithm takes a number of sorted files and merges them together to a sorted file. It uses fixed block sizes (predefined 16).

It basically has a “slot” for each file, you may think a queue of the blocks in this file, and tries to work its way through to the end of all queues. So it takes from the slot with the smallest block, writes it to the output file, and refills the slot with the next queue element from that file.
It is a little more sophisticated, because instead of looking through all slots for the next smallest block, it actually juggles the slots (files) in a sorted manner.
I will definitely reuse this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
 
#define outputfile "mergedblocks"
#ifndef DEBUG
#define DEBUG 0
#endif
 
#define ENTRYSIZE 16
 
FILE * f;
 
int comp(const char * a, const char * b) {
	int i;
	for (i = 0; i < ENTRYSIZE; i++) {
		if (a[i] != b[i])
			return a[i] - b[i];
	}
	return 0;
}
void switchentries(char * a, char * b) {
	int i; 
	char t;
	for(i=0; i<ENTRYSIZE; i++) {
		t = a[i];
		a[i] = b[i];
		b[i] = t;
	}
}
 
 
int main(int argc, char ** argv)
{
	int i;
	int j;
	int nfiles;
	int v;
 
	char ** filenames;
	FILE ** files;
	FILE * outfile;
	char ** data;
	unsigned long * nentries;
	int * slot2file;
 
	assert(argc > 1);
	filenames = argv + 1;
	nfiles = argc - 1;
 
	files = (FILE **)calloc(nfiles, sizeof(FILE*));
	assert(files != NULL);
	data = (char **) calloc(nfiles, sizeof(char*));
	assert(data != NULL);
	nentries = (unsigned long *) calloc(nfiles, sizeof(unsigned long));
	assert(nentries != NULL);
	slot2file = (int *) calloc(nfiles, sizeof(int));
	assert(slot2file != NULL);
	for (i = 0; i < nfiles; i++) {
		files[i] = fopen(filenames[i], "r");
		assert(files[i] != NULL);
		nentries[i] = 0;
		slot2file[i] = i;
		data[i] = (char*) malloc(sizeof(char) * ENTRYSIZE);
		assert(data[i] != NULL);
		memset((void*)data[i], 0, ENTRYSIZE);
 
		/* load first entry */
		for (j = 0; j < ENTRYSIZE; j++) {
			v = getc(files[i]);
			assert(v != EOF);
			data[i][j] = v;
		}
		nentries[i]++;
		/* find next fitting */
		for(j = i - 1; j >= 0 && comp(data[j], data[j + 1]) > 0; j--) {
			if(DEBUG)
			printf("switching %d <-> %d\n", j, j+1);
			switchentries(data[j+1], data[j]);
			v = slot2file[j+1];
			slot2file[j+1] = slot2file[j];
			slot2file[j] = v;
		}
	}
	outfile = fopen(outputfile, "w");
	assert(outfile != NULL);
 
	while(1) {
		if(DEBUG)
		for (i = 0; i < nfiles; i++) {
			printf("slot %3d: ", i);
			for(j = 0; j < ENTRYSIZE; j++) {
				printf("%02x", (unsigned char)data[i][j]);
			}
			j = slot2file[i];
			if (j < 0) {
				printf(" (ended)");
				j = (j + 1)*-1;
			}
			printf("\t%d->%d: '%s'", i, j, filenames[j]);
			printf("\n");
		}
		if(slot2file[0] < 0) {
			break;
		}
		/* smallest is at 0 */
		/* write out smallest */
		if (slot2file[1] >= 0 && comp(data[0], data[1]) == 0) {
			printf("duplicate found\n");
			printf("file %s, item %lu\n", filenames[slot2file[0]], nentries[slot2file[0]]);
			printf("file %s, item %lu\n", filenames[slot2file[1]], nentries[slot2file[1]]);
			for(j = 0; j < ENTRYSIZE; j++) {
				printf("%02x", (unsigned char)data[0][j]);
			}
			printf("\n");
		} else {
			if(DEBUG)
			printf("smallest writeout\n");
			for (j = 0; j < ENTRYSIZE; j++) {
				putc(data[0][j], outfile);
			}
		}
 
		if(DEBUG)
		printf("refill %s\n", filenames[slot2file[0]]);
		/* refill slot if possible */
		for (j = 0; j < ENTRYSIZE; j++) {
			v = getc(files[slot2file[0]]);
			if (v == EOF) {
				if(DEBUG)
				printf("end of file\n");
				slot2file[0] = -slot2file[0] - 1;
				break;
			}
			data[0][j] = v;
		}
		if (slot2file[0] >= 0) {
			nentries[slot2file[0]]++;
		}else{ /* push to the end */
			if(DEBUG)
			printf("moving to the end\n");
			for(j = 1; j < nfiles && slot2file[j] >= 0; j++) {
				i = j - 1;
				if(DEBUG)
				printf("switching %d <-> %d\n", i, j);
				switchentries(data[i], data[j]);
				v = slot2file[i];
				slot2file[i] = slot2file[j];
				slot2file[j] = v;
			}
 
		}
		/* put in right place (bubblesort for 1 unsorted item at 0) */
		if(DEBUG)
		printf("sorting\n");
		for(j = 1; j < nfiles && slot2file[j] >= 0;j++){
			i = j - 1;
			if(comp(data[i], data[j]) > 0) {
				if(DEBUG)
				printf("switching %d <-> %d\n", i, j);
				switchentries(data[i], data[j]);
				v = slot2file[i];
				slot2file[i] = slot2file[j];
				slot2file[j] = v;
			}else{
				break;
			}
		}
	}
 
 
	for (i = 0; i < nfiles; i++) {
		slot2file[i] = -(slot2file[i] + 1);
		fclose(files[i]);
		if (i>0)
			nentries[0] += nentries[i];
	}
	fclose(outfile);
	printf("mergesorted %d files (%lu entries), wrote to %s.\n", 
		nfiles, nentries[0], outputfile);
	/*free(data);*/
 
	return 0;
}

2 Comments

PDF merge/join/split

You always wanted it to be this simple. Now it is.

Screenshot-PDF join.

Download:
=> 8KB pdfjoin-nosrc.tar.bz2

14712KB pdfjoin.tar.bz2 (with Ghostscript sources)

I programmed it in Python+Glade+GTK. It uses ghostscript as backend.
Unpack and run “python pdfjoin.py”.

I’ll make a Windows package sometime.

,

1 Comment

Moving dokuwiki pages to another namespace

I adapted the perl code here: http://blog.doomicile.de/2008/08/11/moving-dokuwiki-pages-with-perl/

It originally moves pages according to a month/day/year scheme.

I just wanted to move all the pages to a subnamespace. Here is my script:

#!/usr/bin/perl
use strict;
use WWW::Mechanize;
use WWW::Mechanize::FormFiller;
use URI::URL;
# list with page names
my @urls = `cat liste.txt`;
my $agent = WWW::Mechanize->new( autocheck => 1 );
my $formfiller = WWW::Mechanize::FormFiller->new();
$agent->env_proxy();
print "Logging in ... \n";
$agent->get('http://myurl/wiki/doku.php?do=login');
$agent->form_number(7) if $agent->forms and scalar @{ $agent->forms };
$formfiller->add_filler( 'u' => Fixed => 'myadmin' );
$formfiller->add_filler( 'p' => Fixed => 'mypassword' );
$formfiller->fill_form( $agent->current_form );
$agent->submit();
print "Logging in done.\n";
my $n, my $o, my $p;
foreach (@urls) {
print $_;
( $n, $o ) = m/(.*):([^:]*)/;
$p = 'myprefix:';
if (length($n) == 0) {
$p = 'myprefix';
( $o ) = m/(.*)/;
}
print '=> ' . $p . '|' . $n . '|' . $o . "\n";
$agent->get( "http://myurl/wiki/doku.php?id=" . $_ . "&do=admin&page=pagemove" );
$agent->form_number(6) if $agent->forms and scalar @{ $agent->forms };
$formfiller->fill_form( $agent->current_form );
$formfiller->fill_form( $agent->current_form );
if (length($n) == 0) {
$formfiller->add_filler( 'nsr' => Fixed => '' );
$formfiller->add_filler( 'newns' => Fixed => $p );
$formfiller->add_filler( 'ns' => Fixed => $p );
} else {
$formfiller->add_filler( 'nsr' => Fixed => '<new>' );
$formfiller->add_filler( 'newns' => Fixed => $p . $n );
$formfiller->add_filler( 'ns' => Fixed => ':' );
}
$formfiller->add_filler( 'pagename' => Fixed => $o );
$formfiller->fill_form( $agent->current_form );
$agent->save_content("/tmp/mydump.txt");
$agent->submit();
$agent->save_content("/tmp/mydump-response.txt");
}

PS: Apparently, I can write perl. I did not know.

No Comments

NoScript

NoScript opens its own page on every update, which I find very annoying. You can turn that off by the noscript.firstRunRedirection config option (go to about:config).

No Comments

Deskbar Extension Converter with Calculator functionality

Ever wanted to get answers to questions like the following?

3 kilometers in meters
6000 gallons in litres
1E+06 au in pc
7*4 EUR in dollars
0x5621*sin(3)+16 euro in dollar
$ 0.50*100/3 in euro
-L- 0.50 in euro
15 kg/m^3 in g/cm^3
3E+09*sin(3)+pow(3,10) kg/m^3 in g/cm^3
3 1000 in 1

And that quickly, from your desktop?

There existed a Converter and a Calculator extension, I made them work together.

Converter-with-Calculator

Download: converterplus-1.9.tar (bz2, 9 KB) converterplus-1.8.tar (bz2, 6 KB)
Howto install:
You have to copy both files from the archive to ~/.gnome2/deskbar-applet/modules-2.20-compatible/, then activate them in the deskbar-applet Preferences.

Old versions of Converter and Calculator will not work together!
You can easily check if you got the right versions: They have my name as author in them and contain the line “This version of calculator can be used with converter” and vice versa.

You need to have the program units installed to benefit from Calculator. It is in your distribution’s repository under the name ‘units’.

Bugs: Please send bug reports by email or leave a comment here.

Technical details:
(users don’t need to read this)

The question was to either copy over the code from Calculator to Converter or to reuse the Module. Code duplication (DRY) was a definite argument against that. If Calculators code gets updated, Converter benefits too. Also, you can enable each functionality seperately.

Problem was to access the other module when loaded in deskbar, I made a hack for loading a file in the same directory, no matter of the context (see source).

7 Comments

Latex: Table converter

If you copy-paste a table out of open-office, it is usually one line per cell.

This Python script converts it into a LaTeX-table (without \begin and \end):

table2latex.py:

#!/usr/bin/env python

import sys

f = sys.stdin

if(len(sys.argv) < 2):

        print "First Argument is number of columns"

        sys.exit(1)

numcolumns = int(sys.argv[1])

borders = False

if len(sys.argv) > 2 and sys.argv[2] == "--with-borders":

        borders = True

i = 0

while True:

        for j in range(numcolumns):

                l = f.readline()

                if l == "":

                        sys.exit(0)

                if i == 0:

                        print "\\tablehdr{%s}" % l.strip(),

                else:

                        print "%s" % l.strip(),

                if j == numcolumns - 1:

                        print '\\\\'

                        if borders:

                                print '\hline'

                else:

                        print '&',

        i = i + 1

Use it like this (pipe in from stdin, first arg is column count):

[user@thiscomputer ~]$ seq 30 | python table2latex.py 3

\tablehdr{1} & \tablehdr{2} & \tablehdr{3} \\

4 & 5 & 6 \\

7 & 8 & 9 \\

10 & 11 & 12 \\

13 & 14 & 15 \\

16 & 17 & 18 \\

19 & 20 & 21 \\

22 & 23 & 24 \\

25 & 26 & 27 \\

28 & 29 & 30 \\

[user@thiscomputer ~]$ 

The second argument can be if borders should be used, i.e. \hline after each row.

No Comments

TU: TIL Tutors helper

This is a script only of interest to tutors in TIL, who use sbt.logic.at for entering students data. It sets the edited selection when one enters gradings sbt-logic-at.user (js, 2 KB).
Install Greasemonkey first, then click the link or download and add manually.

1 Comment

Python: HTML database descriptions from create statements

This code produces HTML database descriptions from create statements.

Feed into stdin, await from stdout.
For example: python dbdesc.py < DATASCHEME > dbdesc.html

It isn’t perfect nor meant to be, but gives a good starting point for a documentation.

#!/usr/bin/python
import sys
import re
a = ''
for i in sys.stdin.readlines():
a = a + " " + re.sub('--.*', '', i).strip()
a = a.strip().lower()
a = re.sub('\/\*[^*]*\*\/', '', a)
print """
<html>
<head>
<link rel="stylesheet" href="dbdesc.css" />
</head>
<body>
"""
for m in re.findall('create table ([^ (]*)[ (]*([^;]*)[ )]*;', a):
tablename = m[0]
content = m[1]
print "<h2>%s</h2>"  % tablename
print """<table class="dbdesc"><thead><tr>
<th class="name">Feldname</th>
<th class="type">Typ</th>
<th class="option">Option</th>
<th class="comment">Bemerkung</th>
</tr></thead>"""
atts = {}
for l in content.split(','):
l = l.strip()
if l == '':
continue
if l.startswith('constraint') or l.startswith('foreign key'):
continue
l = l.split(None, 2)
if len(l)<2:
continue
name = l[0]
if name == 'constraint' or name.__contains__('(') or name.__contains__(')'):
continue
type = l[1]
if len(l)<3:
l.append('')
op = ""
if 'not null' in l[2]:       op = op + '!'
if 'primary key' in l[2]:    op = op + '1+P'
elif 'unique' in l[2]:       op = op + '1'
print """ <tr>
<td class="name">%s</td>
<td class="type">%s</td>
<td class="option">%s</td>
<td class="comment"><!-- TODO: Bemerkungen --></td>
</tr>""" % (name, type, op)
print """ </table> """
print """</html> """

CSS file dbdesc.css

table.dbdesc{	width: 40em;	border: 1px solid #080;	border-width: 1px 0;}
table.dbdesc th{	border-bottom: 1px solid #080;}
table.dbdesc td{	border: none;}
table.dbdesc td, table.dbdesc th{	width: 15em;}
table.dbdesc th{	font-style: italic;	font-weight: normal; }
table.dbdesc td.option{	text-align: center;}
table.dbdesc td.type{	text-transform: uppercase;}

,

No Comments

Skype: Chat Messages/History log file format

To be able to search and use the Skype chat log, message history, and call history I reverse-engineered the Skype file format of chatmsg256.dbb, chatmsg512.dbb, callmember256.dbb, … files.

I made a script able to export the history to html, as well as Python classes able to random-access the logs.

As far as I can see, I’m the first one on the net that did this, everyone else is sane and uses the API.

You can view and edit all information here: http://johbuc6.coconia.net/doku.php/skype/start

No Comments