Browse Source

Add test script and unit tests

Matthias Vogelgesang 10 years ago
parent
commit
7a7c52085a
2 changed files with 136 additions and 0 deletions
  1. 52 0
      tests/runtests.sh
  2. 84 0
      tests/tests.py

+ 52 - 0
tests/runtests.sh

@@ -0,0 +1,52 @@
+#!/bin/bash
+
+CUR=$(pwd)
+DATASET=bug.tar.gz
+DATASET_URL=http://www.ipe.fzk.de/~vogelgesang/$DATASET
+
+if [ ! -d "$CUR/data" ]; then
+    echo "* Downloading data set ..."
+    curl -o $CUR/$DATASET $DATASET_URL
+
+    echo "* Extracting data set into data/ ..."
+    mkdir -p $CUR/data/
+    tar xfz $CUR/$DATASET -C $CUR/data
+
+    echo "* Clean up ..."
+    rm $CUR/$DATASET
+fi
+
+if [ ! -d "$CUR/venv" ]; then
+    echo "* Creating virtualenv ..."
+    virtualenv --system-site-packages -q $CUR/venv
+fi
+
+echo "* Activate virtualenv ..."
+source $CUR/venv/bin/activate
+
+# Install nose if not available
+pip freeze | grep nose > /dev/null
+RC=$?
+
+if [ "$RC" -ne "0" ]; then
+    echo "* Installing nose ..."
+    pip install nose
+fi
+
+# Install unittest2 if Python less than 2.7
+python -c 'import sys; v=sys.version_info; sys.exit((v[0], v[1]) < (2,7))'
+RC=$?
+
+if [ "$RC" -ne "0" ]; then
+    pip freeze | grep unittest2 > /dev/null
+    RC=$?
+
+    if [ "$RC" -ne "0" ]; then
+        echo "* Installing unittest2 ..."
+        pip install unittest2
+    fi
+fi
+
+echo "* Running tests ..."
+export CUDA_VISIBLE_DEVICES=0
+nosetests -s tests.py

+ 84 - 0
tests/tests.py

@@ -0,0 +1,84 @@
+import os
+import shutil
+import tempfile
+
+try:
+    import unittest2 as unittest
+except ImportError:
+    import unittest
+
+import numpy as np
+from gi.repository import Ufo
+from libtiff import TIFF
+
+
+def data_path(suffix=''):
+    return os.path.join(os.path.abspath('./data'), suffix)
+
+
+class BasicTests(unittest.TestCase):
+    def setUp(self):
+        config = Ufo.Config(paths=['../build/src'])
+        self.pm = Ufo.PluginManager(config=config)
+        self.graph = Ufo.TaskGraph()
+        self.sched = Ufo.Scheduler(config=config)
+        self.tmpdir = tempfile.mkdtemp()
+
+    def tearDown(self):
+        shutil.rmtree(self.tmpdir)
+
+    def tmp_path(self, suffix=''):
+        return os.path.join(self.tmpdir, suffix)
+
+    def get_task(self, name, **kwargs):
+        plugin = self.pm.get_task(name)
+        plugin.set_properties(**kwargs)
+        return plugin
+
+    def test_ramp(self):
+        outname = self.tmp_path('rmp-%05i.tif')
+        ramp = self.pm.get_task('lamino-ramp')
+        writer = self.pm.get_task('writer')
+
+        ramp.set_properties(width=4096, fwidth=1458, height=2048,
+                            theta=1.0921048, tau=0.3)
+        writer.set_properties(filename=outname)
+
+        self.graph.connect_nodes(ramp, writer)
+        self.sched.run(self.graph)
+
+        files = os.listdir(self.tmpdir)
+        self.assertIn('rmp-00000.tif', files)
+        self.assertEqual(len(files), 1)
+
+    def test_bug_lamino_reco(self):
+        reader = self.get_task('reader', path=data_path('bug/projections/*.tif'))
+        writer = self.get_task('writer', filename=self.tmp_path('vol-%05i.tif'))
+        reco = self.get_task('lamino-bp')
+        padding = self.get_task('padding-2d')
+        fft_input = self.get_task('fft', dimensions=2)
+        ramp = self.get_task('lamino-ramp')
+        fft_ramp = self.get_task('fft', dimensions=2)
+        conv = self.get_task('lamino-conv')
+        ifft = self.get_task('ifft', dimensions=2)
+
+        THETA = -1.570796326
+        reco.set_properties(theta=THETA, psi=0, angle_step=0.01256637,
+		                    vol_sx=832, vol_sy=832, vol_sz=400,
+		                    vol_ox=416, vol_oy=416, vol_oz=200,
+		                    proj_ox=592, proj_oy=205)
+
+        ramp.set_properties(width=4096, fwidth=1186, height=512,
+                            theta=THETA, tau=1)
+
+        self.graph.connect_nodes(reader, padding)
+        self.graph.connect_nodes(padding, fft_input)
+        self.graph.connect_nodes(ramp, fft_ramp)
+
+        self.graph.connect_nodes_full(fft_ramp, conv, 0)
+        self.graph.connect_nodes_full(fft_input, conv, 1)
+
+        self.graph.connect_nodes(conv, ifft)
+        self.graph.connect_nodes(ifft, reco)
+        self.graph.connect_nodes(reco, writer)
+        self.sched.run(self.graph)