database.py 28.3 KB
Newer Older
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
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :

###############################################################################
#                                                                             #
# Copyright (c) 2016 Idiap Research Institute, http://www.idiap.ch/           #
# Contact: beat.support@idiap.ch                                              #
#                                                                             #
# This file is part of the beat.backend.python module of the BEAT platform.   #
#                                                                             #
# Commercial License Usage                                                    #
# Licensees holding valid commercial BEAT licenses may use this file in       #
# accordance with the terms contained in a written agreement between you      #
# and Idiap. For further information contact tto@idiap.ch                     #
#                                                                             #
# Alternatively, this file may be used under the terms of the GNU Affero      #
# Public License version 3 as published by the Free Software and appearing    #
# in the file LICENSE.AGPL included in the packaging of this file.            #
# The BEAT platform is distributed in the hope that it will be useful, but    #
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY  #
# or FITNESS FOR A PARTICULAR PURPOSE.                                        #
#                                                                             #
# You should have received a copy of the GNU Affero Public License along      #
# with the BEAT platform. If not, see http://www.gnu.org/licenses/.           #
#                                                                             #
###############################################################################


"""Validation of databases"""

import os
import sys

import six
import simplejson
36 37
import itertools
import numpy as np
38
from collections import namedtuple
39 40

from . import loader
Philip ABBET's avatar
Philip ABBET committed
41 42 43
from . import utils

from .dataformat import DataFormat
44
from .outputs import OutputList
Philip ABBET's avatar
Philip ABBET committed
45 46


47 48
#----------------------------------------------------------

Philip ABBET's avatar
Philip ABBET committed
49 50

class Storage(utils.CodeStorage):
Philip ABBET's avatar
Philip ABBET committed
51
    """Resolves paths for databases
Philip ABBET's avatar
Philip ABBET committed
52

Philip ABBET's avatar
Philip ABBET committed
53
    Parameters:
Philip ABBET's avatar
Philip ABBET committed
54

55 56
      prefix (beat.backend.python.utils.Prefix): Establishes the prefix of
        your installation.
Philip ABBET's avatar
Philip ABBET committed
57

Philip ABBET's avatar
Philip ABBET committed
58 59
      name (str): The name of the database object in the format
        ``<name>/<version>``.
Philip ABBET's avatar
Philip ABBET committed
60

Philip ABBET's avatar
Philip ABBET committed
61
    """
Philip ABBET's avatar
Philip ABBET committed
62

Philip ABBET's avatar
Philip ABBET committed
63
    def __init__(self, prefix, name):
Philip ABBET's avatar
Philip ABBET committed
64

Philip ABBET's avatar
Philip ABBET committed
65 66
        if name.count('/') != 1:
            raise RuntimeError("invalid database name: `%s'" % name)
Philip ABBET's avatar
Philip ABBET committed
67

Philip ABBET's avatar
Philip ABBET committed
68 69
        self.name, self.version = name.split('/')
        self.fullname = name
Philip ABBET's avatar
Philip ABBET committed
70

71 72 73 74 75 76
        if isinstance(prefix, utils.Prefix):
            self.prefix = prefix
        else:
            self.prefix = utils.Prefix(prefix)

        path = self.prefix.path(os.path.join('databases', name + '.json'))
77
        path = path[:-5]
Philip ABBET's avatar
Philip ABBET committed
78
        super(Storage, self).__init__(path, 'python') #views are coded in Python
79 80


81 82
#----------------------------------------------------------

83

84
class Runner(object):
Philip ABBET's avatar
Philip ABBET committed
85
    '''A special loader class for database views, with specialized methods
86

Philip ABBET's avatar
Philip ABBET committed
87
    Parameters:
88

Philip ABBET's avatar
Philip ABBET committed
89
      db_name (str): The full name of the database object for this view
90

Philip ABBET's avatar
Philip ABBET committed
91 92
      module (module): The preloaded module containing the database views as
        returned by :py:func:`beat.core.loader.load_module`.
93

94 95
      prefix (beat.backend.python.utils.Prefix): Establishes the prefix of
        your installation.
96

Philip ABBET's avatar
Philip ABBET committed
97 98
      root_folder (str, path): The path pointing to the root folder of this
        database
99

Philip ABBET's avatar
Philip ABBET committed
100 101 102
      exc (class): The class to use as base exception when translating the
        exception from the user code. Read the documention of :py:func:`run`
        for more details.
103

Philip ABBET's avatar
Philip ABBET committed
104
      *args: Constructor parameters for the database view. Normally, none.
105

Philip ABBET's avatar
Philip ABBET committed
106
      **kwargs: Constructor parameters for the database view. Normally, none.
107

Philip ABBET's avatar
Philip ABBET committed
108
    '''
109

110
    def __init__(self, module, definition, prefix, root_folder, exc=None):
111

Philip ABBET's avatar
Philip ABBET committed
112 113 114 115 116 117 118
        try:
            class_ = getattr(module, definition['view'])
        except Exception as e:
            if exc is not None:
                type, value, traceback = sys.exc_info()
                six.reraise(exc, exc(value), traceback)
            else:
119
                raise # just re-raise the user exception
120

121 122 123 124 125 126 127
        self.obj          = loader.run(class_, '__new__', exc)
        self.ready        = False
        self.prefix       = prefix
        self.root_folder  = root_folder
        self.definition   = definition
        self.exc          = exc or RuntimeError
        self.data_sources = None
128 129


130 131
    def index(self, filename):
        '''Index the content of the view'''
132

133
        parameters = self.definition.get('parameters', {})
134

135
        objs = loader.run(self.obj, 'index', self.exc, self.root_folder, parameters)
136

137 138
        if not isinstance(objs, list):
            raise self.exc("index() didn't return a list")
139

140 141
        if not os.path.exists(os.path.dirname(filename)):
            os.makedirs(os.path.dirname(filename))
142

143 144
        with open(filename, 'wb') as f:
            simplejson.dump(objs, f)
145 146


147 148
    def setup(self, filename, start_index=None, end_index=None, pack=True):
        '''Sets up the view'''
149

150 151
        if self.ready:
            return
152

153 154
        with open(filename, 'rb') as f:
            objs = simplejson.load(f)
155

156 157
        Entry = namedtuple('Entry', sorted(objs[0].keys()))
        objs = [ Entry(**x) for x in objs ]
158

159
        parameters = self.definition.get('parameters', {})
160

161 162
        loader.run(self.obj, 'setup', self.exc, self.root_folder, parameters,
                   objs, start_index=start_index, end_index=end_index)
163 164


165 166 167
        # Create data sources for the outputs
        from .data import DatabaseOutputDataSource
        from .dataformat import DataFormat
168

169 170 171 172 173 174
        self.data_sources = {}
        for output_name, output_format in self.definition.get('outputs', {}).items():
            data_source = DatabaseOutputDataSource()
            data_source.setup(self, output_name, output_format, self.prefix,
                              start_index=start_index, end_index=end_index, pack=pack)
            self.data_sources[output_name] = data_source
175

176
        self.ready = True
177 178


179 180
    def get(self, output, index):
        '''Returns the data of the provided output at the provided index'''
181

Philip ABBET's avatar
Philip ABBET committed
182
        if not self.ready:
183
            raise self.exc("Database view not yet setup")
184

185
        return loader.run(self.obj, 'get', self.exc, output, index)
186 187


188 189 190 191
    def objects(self):
        return self.obj.objs


192 193
#----------------------------------------------------------

194 195

class Database(object):
Philip ABBET's avatar
Philip ABBET committed
196
    """Databases define the start point of the dataflow in an experiment.
197 198


Philip ABBET's avatar
Philip ABBET committed
199
    Parameters:
200

201 202
      prefix (beat.backend.python.utils.Prefix): Establishes the prefix of
        your installation.
203

Philip ABBET's avatar
Philip ABBET committed
204
      name (str): The fully qualified database name (e.g. ``db/1``)
205

Philip ABBET's avatar
Philip ABBET committed
206 207 208 209 210 211
      dataformat_cache (dict, optional): A dictionary mapping dataformat names
        to loaded dataformats. This parameter is optional and, if passed, may
        greatly speed-up database loading times as dataformats that are already
        loaded may be re-used. If you use this parameter, you must guarantee
        that the cache is refreshed as appropriate in case the underlying
        dataformats change.
212 213


Philip ABBET's avatar
Philip ABBET committed
214
    Attributes:
215

Philip ABBET's avatar
Philip ABBET committed
216
      name (str): The full, valid name of this database
217

Philip ABBET's avatar
Philip ABBET committed
218 219
      data (dict): The original data for this database, as loaded by our JSON
        decoder.
220

Philip ABBET's avatar
Philip ABBET committed
221
    """
222

Philip ABBET's avatar
Philip ABBET committed
223
    def __init__(self, prefix, name, dataformat_cache=None):
224

Philip ABBET's avatar
Philip ABBET committed
225 226 227 228
        self._name = None
        self.prefix = prefix
        self.dataformats = {} # preloaded dataformats
        self.storage = None
229

Philip ABBET's avatar
Philip ABBET committed
230 231
        self.errors = []
        self.data = None
232

Philip ABBET's avatar
Philip ABBET committed
233 234
        # if the user has not provided a cache, still use one for performance
        dataformat_cache = dataformat_cache if dataformat_cache is not None else {}
235

Philip ABBET's avatar
Philip ABBET committed
236
        self._load(name, dataformat_cache)
237 238


Philip ABBET's avatar
Philip ABBET committed
239 240
    def _load(self, data, dataformat_cache):
        """Loads the database"""
241

Philip ABBET's avatar
Philip ABBET committed
242
        self._name = data
Philip ABBET's avatar
Philip ABBET committed
243

Philip ABBET's avatar
Philip ABBET committed
244 245 246 247 248
        self.storage = Storage(self.prefix, self._name)
        json_path = self.storage.json.path
        if not self.storage.json.exists():
            self.errors.append('Database declaration file not found: %s' % json_path)
            return
Philip ABBET's avatar
Philip ABBET committed
249

Philip ABBET's avatar
Philip ABBET committed
250 251
        with open(json_path, 'rb') as f:
            self.data = simplejson.load(f)
Philip ABBET's avatar
Philip ABBET committed
252

253 254 255
        self.code_path = self.storage.code.path
        self.code = self.storage.code.load()

Philip ABBET's avatar
Philip ABBET committed
256 257
        for protocol in self.data['protocols']:
            for _set in protocol['sets']:
Philip ABBET's avatar
Philip ABBET committed
258

Philip ABBET's avatar
Philip ABBET committed
259
                for key, value in _set['outputs'].items():
Philip ABBET's avatar
Philip ABBET committed
260

Philip ABBET's avatar
Philip ABBET committed
261 262
                    if value in self.dataformats:
                        continue
Philip ABBET's avatar
Philip ABBET committed
263

Philip ABBET's avatar
Philip ABBET committed
264 265 266 267 268
                    if value in dataformat_cache:
                        dataformat = dataformat_cache[value]
                    else:
                        dataformat = DataFormat(self.prefix, value)
                        dataformat_cache[value] = dataformat
Philip ABBET's avatar
Philip ABBET committed
269

Philip ABBET's avatar
Philip ABBET committed
270
                    self.dataformats[value] = dataformat
271 272


Philip ABBET's avatar
Philip ABBET committed
273 274 275 276 277
    @property
    def name(self):
        """Returns the name of this object
        """
        return self._name or '__unnamed_database__'
278 279


280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
    @name.setter
    def name(self, value):
        self._name = value
        self.storage = Storage(self.prefix, value)


    @property
    def description(self):
        """The short description for this object"""
        return self.data.get('description', None)


    @description.setter
    def description(self, value):
        """Sets the short description for this object"""
        self.data['description'] = value


    @property
    def documentation(self):
        """The full-length description for this object"""

        if not self._name:
            raise RuntimeError("database has no name")

        if self.storage.doc.exists():
            return self.storage.doc.load()
        return None


    @documentation.setter
    def documentation(self, value):
        """Sets the full-length description for this object"""

        if not self._name:
            raise RuntimeError("database has no name")

        if hasattr(value, 'read'):
            self.storage.doc.save(value.read())
        else:
            self.storage.doc.save(value)


    def hash(self):
        """Returns the hexadecimal hash for its declaration"""

        if not self._name:
            raise RuntimeError("database has no name")

        return self.storage.hash()


Philip ABBET's avatar
Philip ABBET committed
332 333 334 335
    @property
    def schema_version(self):
        """Returns the schema version"""
        return self.data.get('schema_version', 1)
336 337


Philip ABBET's avatar
Philip ABBET committed
338 339 340
    @property
    def valid(self):
        return not bool(self.errors)
Philip ABBET's avatar
Philip ABBET committed
341 342


Philip ABBET's avatar
Philip ABBET committed
343 344 345
    @property
    def protocols(self):
        """The declaration of all the protocols of the database"""
346

Philip ABBET's avatar
Philip ABBET committed
347 348
        data = self.data['protocols']
        return dict(zip([k['name'] for k in data], data))
349 350


Philip ABBET's avatar
Philip ABBET committed
351 352
    def protocol(self, name):
        """The declaration of a specific protocol in the database"""
353

Philip ABBET's avatar
Philip ABBET committed
354
        return self.protocols[name]
355 356


Philip ABBET's avatar
Philip ABBET committed
357 358 359
    @property
    def protocol_names(self):
        """Names of protocols declared for this database"""
360

Philip ABBET's avatar
Philip ABBET committed
361 362
        data = self.data['protocols']
        return [k['name'] for k in data]
363 364


Philip ABBET's avatar
Philip ABBET committed
365 366
    def sets(self, protocol):
        """The declaration of a specific set in the database protocol"""
367

Philip ABBET's avatar
Philip ABBET committed
368 369
        data = self.protocol(protocol)['sets']
        return dict(zip([k['name'] for k in data], data))
370 371


Philip ABBET's avatar
Philip ABBET committed
372 373
    def set(self, protocol, name):
        """The declaration of all the protocols of the database"""
374

Philip ABBET's avatar
Philip ABBET committed
375
        return self.sets(protocol)[name]
376 377


Philip ABBET's avatar
Philip ABBET committed
378 379
    def set_names(self, protocol):
        """The names of sets in a given protocol for this database"""
380

Philip ABBET's avatar
Philip ABBET committed
381 382
        data = self.protocol(protocol)['sets']
        return [k['name'] for k in data]
383 384


385
    def view(self, protocol, name, exc=None, root_folder=None):
Philip ABBET's avatar
Philip ABBET committed
386
        """Returns the database view, given the protocol and the set name
387

Philip ABBET's avatar
Philip ABBET committed
388
        Parameters:
389

Philip ABBET's avatar
Philip ABBET committed
390
          protocol (str): The name of the protocol where to retrieve the view from
391

Philip ABBET's avatar
Philip ABBET committed
392 393
          name (str): The name of the set in the protocol where to retrieve the
            view from
394

Philip ABBET's avatar
Philip ABBET committed
395 396
          exc (class): If passed, must be a valid exception class that will be
            used to report errors in the read-out of this database's view.
397

Philip ABBET's avatar
Philip ABBET committed
398
        Returns:
399

Philip ABBET's avatar
Philip ABBET committed
400 401
          The database view, which will be constructed, but not setup. You
          **must** set it up before using methods ``done`` or ``next``.
402

Philip ABBET's avatar
Philip ABBET committed
403 404 405 406 407 408 409 410 411
        """

        if not self._name:
            exc = exc or RuntimeError
            raise exc("database has no name")

        if not self.valid:
            message = "cannot load view for set `%s' of protocol `%s' " \
                    "from invalid database (%s)" % (protocol, name, self.name)
412 413 414
            if exc:
                raise exc(message)

Philip ABBET's avatar
Philip ABBET committed
415 416 417 418 419 420 421 422 423 424 425 426 427
            raise RuntimeError(message)

        # loads the module only once through the lifetime of the database object
        try:
            if not hasattr(self, '_module'):
                self._module = loader.load_module(self.name.replace(os.sep, '_'),
                          self.storage.code.path, {})
        except Exception as e:
            if exc is not None:
                type, value, traceback = sys.exc_info()
                six.reraise(exc, exc(value), traceback)
            else:
                raise #just re-raise the user exception
428

429 430 431 432 433
        if root_folder is None:
            root_folder = self.data['root_folder']

        return Runner(self._module, self.set(protocol, name),
                      self.prefix, root_folder, exc)
434 435


436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
    def json_dumps(self, indent=4):
        """Dumps the JSON declaration of this object in a string


        Parameters:

          indent (int): The number of indentation spaces at every indentation level


        Returns:

          str: The JSON representation for this object

        """

        return simplejson.dumps(self.data, indent=indent,
                                cls=utils.NumpyJSONEncoder)


    def __str__(self):
        return self.json_dumps()


    def write(self, storage=None):
        """Writes contents to prefix location

        Parameters:

          storage (Storage, optional): If you pass a new storage, then this object
            will be written to that storage point rather than its default.

        """

        if storage is None:
            if not self._name:
                raise RuntimeError("database has no name")
            storage = self.storage #overwrite

        storage.save(str(self), self.code, self.description)


    def export(self, prefix):
        """Recursively exports itself into another prefix

        Dataformats associated are also exported recursively


        Parameters:

          prefix (str): A path to a prefix that must different then my own.


        Returns:

          None


        Raises:

          RuntimeError: If prefix and self.prefix point to the same directory.

        """

        if not self._name:
            raise RuntimeError("database has no name")

        if not self.valid:
            raise RuntimeError("database is not valid")

        if isinstance(prefix, six.string_types):
            prefix = utils.Prefix(prefix)

        if prefix.paths[0] in self.prefix.paths:
            raise RuntimeError("Cannot export database to the same prefix (%s in " \
                    "%s)" % (prefix.paths[0], self.prefix.paths))

        for k in self.dataformats.values():
            k.export(prefix)

        self.write(Storage(prefix, self.name))


518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
#----------------------------------------------------------


class View(object):

    def index(self, root_folder, parameters):
        """Returns a list of (named) tuples describing the data provided by the view.

        The ordering of values inside the tuples is free, but it is expected
        that the list is ordered in a consistent manner (ie. all train images of
        person A, then all train images of person B, ...).

        For instance, assuming a view providing that kind of data:

        ----------- ----------- ----------- ----------- ----------- -----------
        |  image  | |  image  | |  image  | |  image  | |  image  | |  image  |
        ----------- ----------- ----------- ----------- ----------- -----------
        ----------- ----------- ----------- ----------- ----------- -----------
        | file_id | | file_id | | file_id | | file_id | | file_id | | file_id |
        ----------- ----------- ----------- ----------- ----------- -----------
        ----------------------------------- -----------------------------------
        |             client_id           | |             client_id           |
        ----------------------------------- -----------------------------------

        a list like the following should be generated:

        [
            (client_id=1, file_id=1, image=filename1),
            (client_id=1, file_id=2, image=filename2),
            (client_id=1, file_id=3, image=filename3),
            (client_id=2, file_id=4, image=filename4),
            (client_id=2, file_id=5, image=filename5),
            (client_id=2, file_id=6, image=filename6),
            ...
        ]

        DO NOT store images, sound files or data loadable from a file in the list!
        Store the path of the file to load instead.
        """

        raise NotImplementedError


    def setup(self, root_folder, parameters, objs, start_index=None, end_index=None):

        # Initialisations
        self.root_folder = root_folder
        self.parameters = parameters
        self.objs = objs

        # Determine the range of indices that must be provided
        self.start_index = start_index if start_index is not None else 0
        self.end_index = end_index if end_index is not None else len(self.objs) - 1

        self.objs = self.objs[self.start_index : self.end_index + 1]


    def get(self, output, index):
        """Returns the data of the provided output at the provided index in the list
        of (named) tuples describing the data provided by the view (accessible at
        self.objs)"""

        raise NotImplementedError
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855


#----------------------------------------------------------


class DatabaseTester:
    """Used while developing a new database view, to test its behavior

    This class tests that, for each combination of connected/not connected
    outputs:

      - Data indices seems consistent
      - All the connected outputs produce data
      - All the not connected outputs don't produce data

    It also report some stats, and can generate a text file detailing the
    data generated by each output.

    By default, outputs are assumed to produce data at constant intervals.
    Those that don't follow this pattern, must be declared as 'irregular'.

    Note that no particular check is done about the database declaration or
    the correctness of the generated data with their data formats. This class
    is mainly used to check that the outputs are correctly synchronized.
    """

    # Mock output class
    class MockOutput:

        def __init__(self, name, connected):
            self.name = name
            self.connected = connected
            self.last_written_data_index = -1
            self.written_data = []

        def write(self, data, end_data_index):
            self.written_data.append(( self.last_written_data_index + 1, end_data_index, data ))
            self.last_written_data_index = end_data_index

        def isConnected(self):
            return self.connected


    class SynchronizedUnit:

        def __init__(self, start, end):
            self.start = start
            self.end = end
            self.data = {}
            self.children = []

        def addData(self, output, start, end, data):
            if (start == self.start) and (end == self.end):
                self.data[output] = self._dataToString(data)
            elif (len(self.children) == 0) or (self.children[-1].end < start):
                unit = DatabaseTester.SynchronizedUnit(start, end)
                unit.addData(output, start, end, data)
                self.children.append(unit)
            else:
                for index, unit in enumerate(self.children):
                    if (unit.start <= start) and (unit.end >= end):
                        unit.addData(output, start, end, data)
                        break
                    elif (unit.start == start) and (unit.end < end):
                        new_unit = DatabaseTester.SynchronizedUnit(start, end)
                        new_unit.addData(output, start, end, data)
                        new_unit.children.append(unit)

                        for i in range(index + 1, len(self.children)):
                            unit = self.children[i]
                            if (unit.end <= end):
                                new_unit.children.append(unit)
                            else:
                                break

                        self.children = self.children[:index] + [new_unit] + self.children[i:]
                        break

        def toString(self):
            texts = {}

            for child in self.children:
                child_texts = child.toString()
                for output, text in child_texts.items():
                    if texts.has_key(output):
                        texts[output] += ' ' + text
                    else:
                        texts[output] = text

            if len(self.data) > 0:
                length = max([ len(x) + 6 for x in self.data.values() ])

                if len(texts) > 0:
                    children_length = len(texts.values()[0])
                    if children_length >= length:
                        length = children_length
                    else:
                        diff = length - children_length
                        if diff % 2 == 0:
                            diff1 = diff / 2
                            diff2 = diff1
                        else:
                            diff1 = diff // 2
                            diff2 = diff - diff1

                        for k, v in texts.items():
                            texts[k] = '|%s%s%s|' % ('-' * diff1, v[1:-1], '-' * diff2)

                for output, value in self.data.items():
                    output_length = len(value) + 6
                    diff = length - output_length
                    if diff % 2 == 0:
                        diff1 = diff / 2
                        diff2 = diff1
                    else:
                        diff1 = diff // 2
                        diff2 = diff - diff1
                    texts[output] = '|-%s %s %s-|' % ('-' * diff1, value, '-' * diff2)

            length = max(len(x) for x in texts.values())
            for k, v in texts.items():
                if len(v) < length:
                    texts[k] += ' ' * (length - len(v))

            return texts

        def _dataToString(self, data):
            if (len(data) > 1) or (len(data) == 0):
                return 'X'

            value = data[data.keys()[0]]

            if isinstance(value, np.ndarray) or isinstance(value, dict):
                return 'X'

            return str(value)


    def __init__(self, name, view_class, outputs_declaration, parameters,
                 irregular_outputs=[], all_combinations=True):
        self.name = name
        self.view_class = view_class
        self.outputs_declaration = {}
        self.parameters = parameters
        self.irregular_outputs = irregular_outputs

        self.determine_regular_intervals(outputs_declaration)

        if all_combinations:
            for L in range(0, len(self.outputs_declaration) + 1):
                for subset in itertools.combinations(self.outputs_declaration.keys(), L):
                    self.run(subset)
        else:
            self.run(self.outputs_declaration.keys())

        print

    def determine_regular_intervals(self, outputs_declaration):
        outputs = OutputList()
        for name in outputs_declaration:
            outputs.add(DatabaseTester.MockOutput(name, True))

        view = self.view_class()
        view.setup('', outputs, self.parameters)

        view.next()

        for output in outputs:
            if output.name not in self.irregular_outputs:
                self.outputs_declaration[output.name] = output.last_written_data_index + 1
            else:
                self.outputs_declaration[output.name] = None


    def run(self, connected_outputs):
        if len(connected_outputs) == 0:
            return

        print "Testing '%s', with %d output(s): %s" % (self.name, len(connected_outputs),
                                                       ', '.join(connected_outputs))

        # Create the mock outputs
        connected_outputs = dict([ x for x in self.outputs_declaration.items()
                                     if x[0] in connected_outputs ])

        not_connected_outputs = dict([ x for x in self.outputs_declaration.items()
                                         if x[0] not in connected_outputs ])

        outputs = OutputList()
        for name in self.outputs_declaration.keys():
            outputs.add(DatabaseTester.MockOutput(name, name in connected_outputs))


        # Create the view
        view = self.view_class()
        view.setup('', outputs, self.parameters)


        # Initialisations
        next_expected_indices = {}
        for name, interval in connected_outputs.items():
            next_expected_indices[name] = 0

        next_index = 0

        def _done():
            for output in outputs:
                if output.isConnected() and not view.done(output.last_written_data_index):
                    return False
            return True


        # Ask for all the data
        while not(_done()):
            view.next()

            # Check the indices for the connected outputs
            for name in connected_outputs.keys():
                if name not in self.irregular_outputs:
                    assert(outputs[name].written_data[-1][0] == next_expected_indices[name])
                    assert(outputs[name].written_data[-1][1] == next_expected_indices[name] + connected_outputs[name] - 1)
                else:
                    assert(outputs[name].written_data[-1][0] == next_expected_indices[name])
                    assert(outputs[name].written_data[-1][1] >= next_expected_indices[name])

            # Check that the not connected outputs didn't produce data
            for name in not_connected_outputs.keys():
                assert(len(outputs[name].written_data) == 0)

            # Compute the next data index that should be produced
            next_index = 1 + min([ x.written_data[-1][1] for x in outputs if x.isConnected() ])

            # Compute the next data index that should be produced by each connected output
            for name in connected_outputs.keys():
                if name not in self.irregular_outputs:
                    if next_index == next_expected_indices[name] + connected_outputs[name]:
                        next_expected_indices[name] += connected_outputs[name]
                else:
                    if next_index > outputs[name].written_data[-1][1]:
                        next_expected_indices[name] = outputs[name].written_data[-1][1] + 1

        # Check the number of data produced on the regular outputs
        for name in connected_outputs.keys():
            print '  - %s: %d data' % (name, len(outputs[name].written_data))
            if name not in self.irregular_outputs:
                assert(len(outputs[name].written_data) == next_index / connected_outputs[name])

        # Check that all outputs ends on the same index
        for name in connected_outputs.keys():
            assert(outputs[name].written_data[-1][1] == next_index - 1)


        # Generate a text file with lots of details (only if all outputs are connected)
        if len(connected_outputs) == len(self.outputs_declaration):
            sorted_outputs = sorted(outputs, key=lambda x: len(x.written_data))

            unit = DatabaseTester.SynchronizedUnit(0, sorted_outputs[0].written_data[-1][1])

            for output in sorted_outputs:
                for data in output.written_data:
                    unit.addData(output.name, data[0], data[1], data[2])

            texts = unit.toString()

            outputs_max_length = max([ len(x) for x in self.outputs_declaration.keys() ])

            with open(self.name.replace(' ', '_') + '.txt', 'w') as f:
                for i in range(1, len(sorted_outputs) + 1):
                    output_name = sorted_outputs[-i].name
                    f.write(output_name + ': ')

                    if len(output_name) < outputs_max_length:
                        f.write(' ' * (outputs_max_length - len(output_name)))

                    f.write(texts[output_name] + '\n')