Commit 8059b684 authored by Philip ABBET's avatar Philip ABBET

Update putvein/4 to use the 'DatabaseTester' class of beat.backend.python v1.4.2

parent b54986f3
......@@ -175,23 +175,23 @@ class View:
class TemplateView:
"""Outputs:
- image: "{{ system_user.username }}/array_2d_uint8/1"
- client_id: "{{ system_user.username }}/uint64/1"
- model_id: "{{ system_user.username }}/text/1"
- client_id: "{{ system_user.username }}/uint64/1"
Several "image" are associated with a given "client_id".
Several "client_id" are associated with a given "model_id".
Several "image" are associated with a given "model_id".
Several "model_id" are associated with a given "client_id".
--------- --------- --------- --------- --------- ---------
| image | | image | | image | | image | | image | | image |
--------- --------- --------- --------- --------- ---------
----------------------------- -----------------------------
| client_id | | client_id |
----------------------------- -----------------------------
-----------------------------------------------------------
| model_id |
-----------------------------------------------------------
--------------- --------------- --------------- --------------- --------------- ---------------
| image | | image | | image | | image | | image | | image |
--------------- --------------- --------------- --------------- --------------- ---------------
----------------------------------------------- -----------------------------------------------
| model_id | | model_id |
----------------------------------------------- -----------------------------------------------
-----------------------------------------------------------------------------------------------
| client_id |
-----------------------------------------------------------------------------------------------
Note: for this particular database, there is only one "client_id"
Note: for this particular database, there is only one "image"
per "model_id".
"""
......@@ -206,23 +206,22 @@ class TemplateView:
# Open the database and load the objects to provide via the outputs
self.db = bob.db.putvein.Database()
model_ids = sorted(self.db.model_ids(protocol=self.parameters['protocol'],
groups=[self.parameters['group']],
kinds=[self.parameters['kind']]),
key=lambda x: int(x))
model_ids = self.db.model_ids(protocol=self.parameters['protocol'],
groups=[self.parameters['group']],
kinds=[self.parameters['kind']])
self.objs = []
for model_id in model_ids:
objs = sorted(self.db.objects(protocol=self.parameters['protocol'],
purposes=self.parameters.get('purpose', None),
groups=[self.parameters['group']],
kinds=[self.parameters['kind']],
model_ids=[model_id]),
key=lambda x: x.client_id)
objs = self.db.objects(protocol=self.parameters['protocol'],
purposes=self.parameters.get('purpose', None),
groups=[self.parameters['group']],
kinds=[self.parameters['kind']],
model_ids=[model_id])
self.objs.extend([ (model_id, obj) for obj in objs ])
self.objs = sorted(self.objs, key=lambda x: (x[1].client_id, x[0], x[1].id))
# Determine the range of indices that must be provided
self.start_index = force_start_index if force_start_index is not None else 0
......@@ -242,7 +241,6 @@ class TemplateView:
def next(self):
(model_id, obj) = self.objs[self.next_index - self.start_index]
# Output: model_id (only provide data when the model_id change)
if self.outputs['model_id'].isConnected() and \
self.outputs['model_id'].last_written_data_index < self.next_index:
......@@ -259,28 +257,24 @@ class TemplateView:
model_end_index
)
# Output: client_id (only provide data when the client_id change)
if self.outputs['client_id'].isConnected() and \
self.outputs['client_id'].last_written_data_index < self.next_index:
# Please pay attention - we can use the object CLIENT ID as long as we
# don't use both hands data of the same client in the same protocol.
client_id = obj.get_client_id()
client_end_index = get_client_end_index(self.objs, client_id,
client_end_index = get_client_end_index(self.objs, obj.client_id,
self.next_index,
self.start_index,
self.end_index)
self.outputs['client_id'].write(
{
'value': np.uint64(client_id)
'value': np.uint64(obj.client_id)
},
client_end_index
)
# Output: image (provide data at each iteration)
if self.outputs['image'].isConnected():
"""
......@@ -302,7 +296,6 @@ class TemplateView:
self.next_index
)
# Determine the next data index that must be provided
self.next_index = 1 + min([ x.last_written_data_index for x in self.outputs
if x.isConnected() ]
......@@ -314,125 +307,25 @@ class TemplateView:
#----------------------------------------------------------
# Test the behavior of the views (on fake data)
if __name__ == '__main__':
# Install a mock load method for the images
def mock_load(obj, root_folder):
return np.ndarray((3, 768, 1024), dtype=np.uint8)
def setup_tests():
# Install a mock methods
def mock_load(obj, directory):
return np.ndarray((3, 10, 20), dtype=np.uint8)
bob.db.putvein.models.File.load = mock_load
# 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
# Tester utility class
from beat.backend.python.outputs import OutputList
import itertools
class Tester:
def __init__(self, name, view_class, outputs_declaration, parameters):
self.name = name
self.view_class = view_class
self.outputs_declaration = {}
self.parameters = parameters
self.determine_increments(outputs_declaration)
for L in range(0, len(self.outputs_declaration) + 1):
for subset in itertools.combinations(self.outputs_declaration.keys(), L):
self.run(subset)
def determine_increments(self, outputs_declaration):
outputs = OutputList()
for name in outputs_declaration:
outputs.add(MockOutput(name, True))
view = self.view_class()
view.setup('', outputs, self.parameters)
view.next()
print "View '%s', increments found:" % self.name
for output in outputs:
self.outputs_declaration[output.name] = output.last_written_data_index + 1
print ' - %s: %d' % (output.name, output.last_written_data_index + 1)
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))
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(MockOutput(name, name in connected_outputs))
parameters = dict()
view = self.view_class()
view.setup('', outputs, self.parameters)
biggest_index_increment = max(connected_outputs.values())
biggest_index_output = [ x[0] for x in connected_outputs.items()
if x[1] == biggest_index_increment ][0]
lowest_index_increment = min(connected_outputs.values())
lowest_index_output = [ x[0] for x in connected_outputs.items()
if x[1] == lowest_index_increment ][0]
next_expected_indices = {}
for name, increment in connected_outputs.items():
next_expected_indices[name] = 0
current_index = 0
while not(view.done(outputs[lowest_index_output].last_written_data_index)):
view.next()
for name in connected_outputs.keys():
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)
for name in not_connected_outputs.keys():
assert(len(outputs[name].written_data) == 0)
#----------------------------------------------------------
current_index += lowest_index_increment
for name in connected_outputs.keys():
if current_index == next_expected_indices[name] + connected_outputs[name]:
next_expected_indices[name] += connected_outputs[name]
# Test the behavior of the views (on fake data)
if __name__ == '__main__':
for name in connected_outputs.keys():
assert(len(outputs[name].written_data) == current_index / connected_outputs[name])
setup_tests()
from beat.backend.python.database import DatabaseTester
# The actual tests
Tester('View', View,
DatabaseTester('View', View,
[
'client_id',
'image',
......@@ -445,7 +338,7 @@ if __name__ == '__main__':
)
)
Tester('TemplateView', TemplateView,
DatabaseTester('TemplateView', TemplateView,
[
'model_id',
'client_id',
......
......@@ -25,17 +25,17 @@ The PUT Vein Database
Changelog
=========
* **Version 1**, 10/Feb/2016:
* **Version 3**, 02/Nov/2017:
- Initial release
- Port to beat.backend.python v1.4.2
* **Version 2**, 10/Feb/2017:
- Major update to make database compatible with BIOWAVE database toolchains
* **Version 3**, 16/Oct/2017:
* **Version 1**, 10/Feb/2016:
- Bugfix: Incorrect usage of the BEAT synchronization mechanism
- Initial release
Description
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment