Skip to content

Instantly share code, notes, and snippets.

@ksheedlo
Last active June 8, 2016 19:14
Show Gist options
  • Save ksheedlo/5147037 to your computer and use it in GitHub Desktop.
Save ksheedlo/5147037 to your computer and use it in GitHub Desktop.
Simple library set up for calling TISEAN programs as Python functions. I'm going to use this on the next chaos pset.
###############################################################################
#
# File: tizz/__init__.py
# Author: Ken Sheedlo
#
# TISEAN interface library for Python
#
###############################################################################
import numpy
import StringIO
import subprocess
import sys
PROGRAMS = [
'ar-model',
'arima-model',
'av-d2',
'boxcount',
'causality',
'changes',
'corr',
'd2',
'delay',
'extrema',
'false_nearest',
'fsle',
'ghkss',
'histogram',
'lfo-ar',
'lfo-run',
'lfo-test',
'low121',
'lyap_k',
'lyap_r',
'lyap_spec',
'lzo-gm',
'lzo-run',
'lzo-test',
'makenoise',
'mem_spec',
'mutual',
'nrlazy',
'nstat_z',
'pca',
'poincare',
'polyback',
'polynom',
'polynomp',
'polypar',
'rand',
'rbf',
'recurr',
'resample',
'rescale',
'routines',
'sav_gol',
'xcor',
'xzero',
'zeroth',
'addnoise',
'ar-run',
'autocor',
'c1',
'c2d',
'c2g',
'c2naive',
'c2t',
'changes',
'choose',
'cluster',
'compare',
'delay',
'endtoend',
'events',
'henon',
'ikeda',
'intervals',
'lazy',
'lorenz',
'notch',
'pc',
'predict',
'project',
'randomize',
'randomize_auto',
'randomize_cool',
'randomize_cost',
'randomize_extend',
'randomize_perm',
'randomize_spike',
'randomize_uneven',
'rms',
'spectrum',
'spikeauto',
'spikespec',
'stp',
'surrogates',
'timerev',
'upo',
'upoembed',
'wiener',
'xc2',
'xrecur'
]
def _activate(program):
'''
Constructs a callback interface to a TISEAN program.
Params:
program The name of the TISEAN program to call.
Returns: a tuple (name, callback) where
name A suitable Python name for the program. This will be equal
to the TISEAN program's name unless it uses Python special
characters (e.g., -). In this case an appropriate substitute
will be used (- => _).
callback The function to run, conforming to the standard tizz
interface. It takes an optional input keyword-arg that will
be converted to a string and passed to TISEAN as standard
input. It returns the result from TISEAN as a Numpy ndarray.
'''
pyname = program.replace('-', '_')
def _straightline(dargs):
'''
Converts a keyword-arg dictionary into a shell arguments.
'''
def _combine(lst, kv):
return lst + ['-{0}'.format(kv[0]), str(kv[1])]
return reduce(_combine, dargs.items(), [])
def _callback(*args, **kwargs):
'''
Standard tizz library interface callback.
'''
iarray = kwargs.get('input')
idata = None
if iarray is not None:
ibuf = StringIO.StringIO()
numpy.savetxt(ibuf, iarray)
idata = ibuf.getvalue()
ibuf.close()
pargs = [program, '-V', '0'] + list(args) + _straightline(dict({
kv for kv in kwargs.items()
if kv[0] != 'input'
}))
child = subprocess.Popen(
pargs,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
)
(outdata, _) = child.communicate(input=idata)
buf = StringIO.StringIO(outdata)
try:
return numpy.loadtxt(buf)
finally:
buf.close()
return (pyname, _callback)
selfmod = sys.modules[__name__]
selfmod.__dict__.update([_activate(program) for program in PROGRAMS])
###############################################################################
#
# File: test.py
# Author: Ken Sheedlo
#
# TISEAN interface library for Python, unit tests.
#
###############################################################################
import numpy
import tizz
import unittest
class TestTizz(unittest.TestCase):
'''
Test suite for tizz library.
'''
def test_mutual_should_construct_mutual_information(self):
'''
Tests the mutual program to make sure it is working.
'''
data = numpy.loadtxt('tizz/test.dat')
muts = tizz.mutual(input=data)
expected = numpy.array([
[ 0., 2.766932],
[ 1., 2.626527],
[ 2., 2.523629],
[ 3., 2.43525 ],
[ 4., 2.356612],
[ 5., 2.28539 ],
[ 6., 2.220294],
[ 7., 2.160475],
[ 8., 2.105323],
[ 9., 2.054395],
[10., 2.007321],
[11., 1.963788],
[12., 1.923651],
[13., 1.886742],
[14., 1.852874],
[15., 1.821637],
[16., 1.792148],
[17., 1.763563],
[18., 1.734117],
[19., 1.704597],
[20., 1.673778]
], dtype=numpy.float64)
self.assertEquals(muts.shape, expected.shape)
for i in xrange(21):
self.assertAlmostEqual(expected[i, 0], muts[i, 0])
self.assertAlmostEqual(expected[i, 1], muts[i, 1])
if __name__ == "__main__":
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment