Skip to content

Commit

Permalink
Remove empty blocks' block_name from frame (#179)
Browse files Browse the repository at this point in the history
* Remove empty blocks' block_name from frame

If a previously published block wasn't published during a frame, the block's
name would end up in the frame's "block_names" list anyway. We now remove it if
the block is empty, i.e. if there are no timestamps.

* Build block_names correctly the first time

Instead of removing names that shouldn't be in the list, build it right the
first time.

* Extend block_names if already in frame
  • Loading branch information
BrianJKoopman committed Oct 9, 2020
1 parent 488bc7c commit ae3594f
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 4 deletions.
12 changes: 8 additions & 4 deletions ocs/agent/aggregator.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,11 +428,8 @@ def to_frame(self, hksess=None, clear=False):

frame['address'] = self.address
frame['provider_session_id'] = self.sessid
if 'block_names' in frame:
frame['block_names'].extend(list(self.blocks.keys()))
else:
frame['block_names'] = core.G3VectorString(list(self.blocks.keys()))

block_names = []
for block_name, block in self.blocks.items():
if not block.timestamps:
continue
Expand All @@ -446,6 +443,13 @@ def to_frame(self, hksess=None, clear=False):
e=e)
continue
frame['blocks'].append(m)
block_names.append(block_name)

if 'block_names' in frame:
frame['block_names'].extend(block_names)
else:
frame['block_names'] = core.G3VectorString(block_names)

if clear:
self.clear()
return frame
Expand Down
70 changes: 70 additions & 0 deletions tests/test_aggregator.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,76 @@ def test_passing_non_float_like_str_in_provider_to_frame():

provider.to_frame(hksess=sess)

def test_sparsely_sampled_block():
"""If a block is sparsely sampled and published, the aggregator was
including its block_name anyway, even when missing. This test publishes two
blocks, writes to_frame, then publishes only one block then checks to makes
sure the blocks and block_names arrays are of the same length. Lastly, we
check that the block_name returns when we again save the sparse block.
"""
# Dummy Provider for testing
provider = Provider('test_provider', 'test_sessid', 3, 1)
provider.frame_start_time = time.time()
data = {'test': {'block_name': 'test',
'timestamps': [time.time()],
'data': {'key3': [0],
'key4': ['z']},
'prefix': ''}
}
provider.save_to_block(data)
data = {'test2': {'block_name': 'test2',
'timestamps': [time.time()],
'data': {'key1': [1],
'key2': ['a']},
'prefix': ''}
}
provider.save_to_block(data)

# Dummy HKSessionHelper
sess = so3g.hk.HKSessionHelper(description="testing")
sess.start_time = time.time()
sess.session_id = 'test_sessid'

a = provider.to_frame(hksess=sess, clear=True)

# Now omit the 'test' block.
provider.frame_start_time = time.time()
data = {'test2': {'block_name': 'test2',
'timestamps': [time.time()],
'data': {'key1': [1],
'key2': ['a']},
'prefix': ''}
}
provider.save_to_block(data)

b = provider.to_frame(hksess=sess, clear=True)

assert len(b['block_names']) == len(b['blocks'])

# Check the name is present if we again publish 'test'
provider.frame_start_time = time.time()
data = {'test': {'block_name': 'test',
'timestamps': [time.time()],
'data': {'key3': [0],
'key4': ['z']},
'prefix': ''}
}
provider.save_to_block(data)
data = {'test2': {'block_name': 'test2',
'timestamps': [time.time()],
'data': {'key1': [1],
'key2': ['a']},
'prefix': ''}
}
provider.save_to_block(data)

c = provider.to_frame(hksess=sess, clear=True)

assert len(c['block_names']) == len(c['blocks'])
assert 'test' in c['block_names']
assert 'test2' in c['block_names']

# This is perhaps another problem, I'm passing irregular length data sets and
# it's not raising any sort of alarm. How does this get handled?
def test_data_type_in_provider_save_to_block():
Expand Down

0 comments on commit ae3594f

Please sign in to comment.