find length and centralize auto assign labels to label assigner

This commit is contained in:
jopohl
2016-07-01 15:14:19 +02:00
parent 59bb21ac96
commit 6ca222dbcd
4 changed files with 49 additions and 26 deletions

View File

@@ -2,6 +2,7 @@ from collections import defaultdict
from urh import constants
from urh.signalprocessing.Interval import Interval
from urh.signalprocessing.LabelSet import LabelSet
from urh.signalprocessing.ProtocoLabel import ProtocolLabel
@@ -142,12 +143,46 @@ class LabelAssigner(object):
val_type_index=0, color_index=None) for i, interval in enumerate(common_constant_intervals)]
def find_byte_length(self):
"""
Find the byte length using a scoring algorithm.
:return:
"""
if self.sync_end is None:
self.find_sync()
for block in self.__blocks:
byte_len = block.get_byte_length(bit_start=self.sync_end+1, decoded=True)
for byte in block.get_bytes(bit_start=self.sync_end+1, decoded=True):
pass
#print(byte)
scores = defaultdict(int)
weights = {-5: 1, -4: 2, -3: 3, -2: 4, -1: 5, 0: 6}
for block in self.__blocks:
byte_start = int(block.convert_index(self.sync_end + 1, 0, 2, decoded=True)[0])
byte_len = block.get_byte_length(decoded=True) - byte_start
for i, byte in enumerate(block.get_bytes(start=byte_start, decoded=True)):
diff = byte - byte_len
bit_range = block.convert_index(byte_start + i, 2, 0, decoded=True)
if diff in weights:
scores[bit_range] += weights[diff]
try:
length_range = max(scores, key=scores.__getitem__)
except ValueError:
return None
return ProtocolLabel(start=int(length_range[0]), end=int(length_range[1]) - 1,
name="Length", color_index=None, val_type_index=0)
def auto_assign_to_labelset(self, labelset: LabelSet):
preamble = self.find_preamble()
if preamble is not None:
labelset.add_label(preamble)
sync = self.find_sync()
if sync is not None:
labelset.add_label(sync)
const_labels = self.find_constants()
for lbl in const_labels:
labelset.add_label(lbl)
length_label = self.find_byte_length()
if length_label is not None:
labelset.add_label(length_label)

View File

@@ -863,17 +863,4 @@ class ProtocolAnalyzer(object):
def auto_assign_labels(self):
label_assigner = LabelAssigner(self.blocks)
preamble = label_assigner.find_preamble()
if preamble is not None:
self.default_labelset.add_label(preamble)
sync = label_assigner.find_sync()
if sync is not None:
self.default_labelset.add_label(sync)
const_labels = label_assigner.find_constants()
for lbl in const_labels:
self.default_labelset.add_label(lbl)
length_label = label_assigner.find_byte_length()
label_assigner.auto_assign_to_labelset(self.default_labelset)

View File

@@ -151,20 +151,16 @@ class ProtocolBlock(object):
def __str__(self):
return self.bits2string(self.plain_bits)
def get_byte_length(self, bit_start=0, decoded=True) -> int:
def get_byte_length(self, decoded=True) -> int:
"""
Return the length of this block in byte.
:param bit_start: Give the start from where to calculate the byte length. Useful if you want to exclude preamble/sync
:return: length of block in byte from bit_start on
"""
start = self.convert_index(bit_start, 0, 2, decoded=decoded)[0]
end = len(self.decoded_bits) if decoded else len(self.__plain_bits)
end = self.convert_index(end, 0, 2, decoded=decoded)[0]
return int(end - start)
return int(end)
def get_bytes(self, bit_start=0, decoded=True) -> list:
start = int(self.convert_index(bit_start, 0, 2, decoded=decoded)[0])
def get_bytes(self, start=0, decoded=True) -> list:
data = self.decoded_ascii_str[start:] if decoded else self.plain_ascii_str[start:]
return list(map(ord, data))

View File

@@ -136,6 +136,8 @@ class TestAutoAssignments(unittest.TestCase):
preamble_end = 31
sync_start = 32
sync_end = 63
length_start = 64
length_end = 71
t = time.time()
self.protocol.auto_assign_labels()
@@ -143,13 +145,16 @@ class TestAutoAssignments(unittest.TestCase):
preamble_label = ProtocolLabel(name="Preamble", start=preamble_start, end=preamble_end, val_type_index=0, color_index=0)
sync_label = ProtocolLabel(name="Sync", start=sync_start, end=sync_end, val_type_index=0, color_index=1)
length_label = ProtocolLabel(name="Length", start=length_start, end=length_end, val_type_index=0, color_index=2)
self.assertEqual(1, len([lbl for lbl in self.protocol.default_labelset if lbl.name == "Preamble"]))
self.assertEqual(1, len([lbl for lbl in self.protocol.default_labelset if lbl.name == "Sync"]))
self.assertEqual(1, len([lbl for lbl in self.protocol.default_labelset if lbl.name == "Length"]))
for block in self.protocol.blocks:
self.assertIn(preamble_label, block.labelset)
self.assertIn(sync_label, block.labelset)
self.assertIn(length_label, block.labelset)
def test_assign_constants(self):