Archive for the ‘Usefull’ Tag

livescore.py v0.1c – Live Results in your Terminal

Remember livescore.py ? Of course not, but now it is hosted in Google Code!
A minor update which show the date is available also..
Check it out if you may..

link
livescore.py
PS. I know, summer is a dead period (unless Euro or World Cup is in the near future). Damn!

Advertisements

Pure Python implementation of Runge Kutta

I was asked (or forced, if you like) to implement the Hodgkin-Huxley model (a model that describes how action potentials in neurons are initiated and propagated) using Runge Kutta for a system of differential equations. The following snippet is the Runge Kutta 4th order, for an arbitrary number of differential equation.


class RungeKutta(object):
	"""Runge Kutta 4th order for a system of differential equations."""
	"""https://testparticle.wordpress.com"""
	__author__ = 'woci'
	__version__ = 0.1b
	__url__ = """https://testparticle.wordpress.com"""
	def __init__(self, functions, initConditions, dh, save=True):
		self.Trajectory, self.save   = [initConditions], save
		self.functions    = [lambda *args: 1.0]  + functions
		[self.N, self.dh] = [len(initConditions), dh]
		self.coeff        = [1.0/6.0, 2.0/6.0, 2.0/6.0, 1.0/6.0]
		self.InArgCoeff   = [0.0, 0.5, 0.5, 1.0]
	def iterate(self):
		step   = self.Trajectory[-1][:]
		istep, iac  = step[:], self.InArgCoeff
		k, ktmp = self.N * [0.0], self.N * [0.0]
		for ic, c in enumerate(self.coeff):
			for if_, f in enumerate(self.functions):
				arguments  = [ (x + k[i]*iac[ic]) \
						for i, x in enumerate(istep)] 
				ktmp[if_]  = self.dh * f(*arguments)
			k = ktmp[:]
			step = [s + c*k[ik] for ik,s in enumerate(step)]
		if self.save: self.Trajectory += [step]
		else: self.Trajectory = [step]
	def solve(self, finishtime):
		while (self.Trajectory[-1][0] < finishtime ):
			self.iterate()
		return None
	def series(self):
			return zip(*self.Trajectory)
&#91;/sourcecode&#93;
You define the functions, the initial conditions, the timestep and you have yourself a solver.
The functions can be defined using the standard <strong>def</strong> or by just using lambda.

def dxdt(t):
	return cos(t)
dxdt(t) = lambda t: cos(x)

The following source solves the «Lorenz Oscillator» equations and plots the timeseries if you have matplotlib at hand.

if __name__ == '__main__':
	print "Solving Lorenz Equation"
	dxdt = lambda t, x, y, z: 10.0*(y-x)
	dydt = lambda t, x, y, z: 28.0*x - x*z - y
	dzdt = lambda t, x, y, z: x*y - (8.0/3.0)*z
	funs = [dxdt, dydt, dzdt]
	init_cond = [0., 1, 2, 3]
	dt = 0.01
	RK4 = RungeKutta(funs, init_cond, dt)
	RK4.solve(10.0)
	t, x, y, z = RK4.series()
	pylab.plot(t, x)
	pylab.plot(t, y)
	pylab.plot(t, z)
	pylab.show()

Lorenz Solution
Please refer if you use this script.

Ubuntu Pre-order

masthead-shipit
I have just pre-ordered my free «Ubuntu 9.04 (Jaunty Jackalope) CD«.
Last time (8.04) was a success. This will be too…

GPLv2 vs GPLv3 Word Clouds

I really enjoy using wordle, in order to get an idea where the weight in a text is. For example GPLv2 and GPLv3, where in the new license, the word «Program» is dominated by the word «Work». This ought to give an idea where the GPL is heading.

GPLv2
Wordle: GPLv2

GPLv3
Wordle: GPLv3

The «Zen of Python» Word Cloud

>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren’t special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one– and preferably only one –obvious way to do it.
Although that way may not be obvious at first unless you’re Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it’s a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea — let’s do more of those!

The Zen Of Python
by http://www.wordle.net/

Βάλε χρώμα…

Ποτέ δεν ήμουν καλός στα σχόλια στον κώδικα. Αυτό έχω πάψει από καιρό να το θεωρώ προτέρημα του στυλ «Θυμάμαι τι κάνω εδώ, μα πόσο καλός μπορεί να είμαι!», και έχω αποδεχτεί το γεγονός ότι είναι αδυναμία. Παρόλα αυτά, δεν γίνεται να εξηγηθούν τα πάντα μέσα στο source, (ειδικά όταν σε εξετάζει και κάποιος, και πρέπει να είναι κατανοητό). Αποφάσισα μαζί με τα cpp αρχεία, τα αποτελέσματα, τα διαγράμματα και τον σχολιασμό αυτών, να παραθέσω και αναλυτική (λέμε τώρα) ανάλυση του κώδικα. Αυτό που έψαχνα ήταν κάτι να μου «χρωματίζει» τα κομμάτια που θα παρέθετα, για να είναι πιο ευανάγνωστο το κείμενο και βρήκα το Colorer, ένα online εργαλείο, που χρωματίζει αρκετές «γλώσσες» και δίνει τα αποτελέσματά του σε HTML και η αντιγραφή σε Word ή και σε όποιον άλλο κειμενογράφο είναι παιχνιδάκι..