Skip to content

Commit

Permalink
Merge pull request #7 from steinwurf/upgrade-kodo-python
Browse files Browse the repository at this point in the history
Use kodo-python 13
  • Loading branch information
petya2164 committed Sep 6, 2018
2 parents 1dffb9b + 3f31e8a commit 6bd798d
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 15 deletions.
2 changes: 2 additions & 0 deletions NEWS.rst
Expand Up @@ -7,6 +7,8 @@ every change, see the Git log.
Latest
------
* tbd
* Major: Upgrade to kodo-python 13
* Minor: Added an option to specify the finite field for the simulation.

1.0.0
-----
Expand Down
21 changes: 18 additions & 3 deletions examples/relay_simulations.py
Expand Up @@ -10,6 +10,7 @@


def print_setup(runs,
field,
symbols,
symbol_size,
error_source_sink,
Expand All @@ -25,6 +26,7 @@ def print_setup(runs,
print("[ ] Runs: {} runs".format(runs))
print("[ SETUP ] configuration.coding")
print("[ ] Relay Recode: {}".format(relay_recode))
print("[ ] Finite Field: {}".format(field))
print("[ ] Size of Symbols: {} bytes".format(symbol_size))
print("[ ] Number of Symbols: {} symbols".format(symbols))
print("[ ] Source Systematic: {}".format(source_systematic))
Expand All @@ -37,7 +39,8 @@ def print_setup(runs,
print("[--------]")


def relay_simulation(symbols,
def relay_simulation(field,
symbols,
symbol_size,
error_source_sink,
error_source_relay,
Expand Down Expand Up @@ -67,8 +70,9 @@ def relay_simulation(symbols,
| relayN |
+--------+
"""
encoder_factory = kodo.FullVectorEncoderFactoryBinary(symbols, symbol_size)
decoder_factory = kodo.FullVectorDecoderFactoryBinary(symbols, symbol_size)
field = getattr(kodo.field, field)
encoder_factory = kodo.RLNCEncoderFactory(field, symbols, symbol_size)
decoder_factory = kodo.RLNCDecoderFactory(field, symbols, symbol_size)
s = simulator.Simulator(encoder_factory, decoder_factory)

source = s.create_source()
Expand Down Expand Up @@ -104,11 +108,20 @@ def main():
parser = argparse.ArgumentParser(
description=relay_simulation.__doc__,
formatter_class=argparse.RawTextHelpFormatter)

fields = ['binary', 'binary4', 'binary8', 'binary16']

parser.add_argument(
"--runs",
help="Set number of runs.",
type=int,
default=100)
parser.add_argument(
'--field',
type=str,
help='The finite field to use',
choices=fields,
default='binary')
parser.add_argument(
"--symbols",
help="Set symbols.",
Expand Down Expand Up @@ -159,6 +172,7 @@ def main():

print_setup(
runs=args.runs,
field=args.field,
symbols=args.symbols,
symbol_size=args.symbol_size,
error_source_sink=args.error_source_sink,
Expand All @@ -172,6 +186,7 @@ def main():
results = simulator.ResultSet()
for run in range(args.runs):
results.add(relay_simulation(
field=args.field,
symbols=args.symbols,
symbol_size=args.symbol_size,
error_source_sink=args.error_source_sink,
Expand Down
2 changes: 1 addition & 1 deletion resolve.json
Expand Up @@ -12,7 +12,7 @@
"name": "kodo-python",
"resolver": "git",
"method": "semver",
"major": 12,
"major": 13,
"sources": [
"github.com/steinwurf/kodo-python.git"
]
Expand Down
5 changes: 5 additions & 0 deletions simulator/receiver.py
Expand Up @@ -12,6 +12,11 @@ def __init__(self, id, stats, decoder):
self.decoder = decoder
self.stats = stats

# Define the data_out bytearray where the symbols should be decoded
# This bytearray must not go out of scope while the encoder exists!
self.data_out = bytearray(self.decoder.block_size())
self.decoder.set_mutable_symbols(self.data_out)

def receive(self, payload):
"""Receive payload."""
key = "{}_receive_from_{}".format(self.id, payload.sender.sender.id)
Expand Down
14 changes: 5 additions & 9 deletions simulator/source.py
Expand Up @@ -18,15 +18,11 @@ def __init__(self, id, stats, encoder):
self.encoder = encoder
self.stats = stats

# Create some data to encode. In this case we make a buffer
# with the same size as the encoder's block size (the max.
# amount a single encoder can encode)
# Just for fun - fill the input data with random data
data_in = os.urandom(encoder.block_size())

# Assign the data buffer to the encoder so that we can
# produce encoded symbols
encoder.set_const_symbols(data_in)
# Generate some random data to encode. We create a bytearray of the
# same size as the encoder's block size and assign it to the encoder.
# This bytearray must not go out of scope while the encoder exists!
self.data_in = bytearray(os.urandom(self.encoder.block_size()))
self.encoder.set_const_symbols(self.data_in)

def tick(self):
"""Increment time."""
Expand Down
5 changes: 4 additions & 1 deletion test/test_relay.py
Expand Up @@ -3,8 +3,10 @@

import sys
sys.path.append('..')
sys.path.append('mock')

import unittest
from mock import Mock
import simulator.relay


Expand All @@ -15,7 +17,8 @@ def test_instantiation(self):
"""Test instantiation."""
id = "test_id"
stats = {}
decoder = "decoder_object"
decoder = Mock(name="decoder_object")
decoder.block_size = Mock(return_value=100)
c = simulator.relay.Relay(id, stats, decoder)
self.assertEqual(c.sender.id, id)
self.assertEqual(c.receiver.id, id)
Expand Down
5 changes: 4 additions & 1 deletion test/test_sink.py
Expand Up @@ -3,8 +3,10 @@

import sys
sys.path.append('..')
sys.path.append('mock')

import unittest
from mock import Mock
import simulator.sink


Expand All @@ -15,7 +17,8 @@ def test_instantiation(self):
"""Test instantiation."""
id = "test_id"
stats = {}
decoder = "decoder_object"
decoder = Mock(name="decoder_object")
decoder.block_size = Mock(return_value=100)
c = simulator.sink.Sink(id, stats, decoder)
self.assertEqual(c.receiver.id, id)
self.assertEqual(c.receiver.decoder, decoder)
Expand Down

0 comments on commit 6bd798d

Please sign in to comment.