def test_hamming_code_with_0_error(self): for block_length in range(10, 11): source = FixSource("BCDAABDBDCDBDBDCACDD") chain = Chain( source, Code("00 01 10 11", symbols="ABCD"), Hamming(block_length), Channel(0), verbosity=0, ) chain.run() for level in [0, 1]: init, final = chain.runs[-1].outputs[level] self.assertEqual(init.message, final.message)
def test_hamming_code_can_correct_1_error(self): 'Hamming code could correct 1 error.' for block_length in range(4, 8): source = FixSource("ALABAMA") chain = Chain( source, Code("00 01 10 11", symbols=source.symbols), Hamming(block_length), Channel(1), verbosity=1, ) chain.run() for level in [0, 1]: init, final = chain.runs[-1].outputs[level] self.assertEqual(init.message, final.message)
def test_simple_chain_returns_correct_html(self): args_list = [ (3, 1, 0, 0), (2, 1, 0, 0), (2, 1, 0, 8), ] for args in args_list: source_number, code_number, channel_description,\ hamming_block_length = args response = simple_chain(HttpRequest(), *args) source_name, source, code_list = sources[source_number] code = code_list[code_number - 1] channel = Channel(channel_description) chain_ = Chain(source, code, channel) chain_.run() run = chain_.runs[0] expected_html = render_to_string( 'sources/chain.html', { "source": source, "source_description": urlizer.to_url(str(source)), "code": str(code), "channel": channel, "channel_description": channel.description, "hamming_block_length": hamming_block_length, "linearized_outputs": tools.colorize_and_linearize_outputs(run.outputs), "fix_source": get_fix_source(source.symbols, fix_sources) }) html = response.content.decode() self.assertEqual(remove_table_data(html), remove_table_data(expected_html)) self.assertContains(response, "hibament") self.assertContains(response, "<table") self.assertContains(response, "<tr><td>")
def test_simple_chain_returns_correct_html(self): args_list = [ (3, 1, 0, 0), (2, 1, 0, 0), (2, 1, 0, 8), ] for args in args_list: source_number, code_number, channel_description,\ hamming_block_length = args response = simple_chain(HttpRequest(), *args) source_name, source, code_list = sources[source_number] code = code_list[code_number-1] channel = Channel(channel_description) chain_ = Chain(source, code, channel) chain_.run() run = chain_.runs[0] expected_html = render_to_string( 'sources/chain.html', { "source": source, "source_description": urlizer.to_url(str(source)), "code": str(code), "channel": channel, "channel_description": channel.description, "hamming_block_length": hamming_block_length, "linearized_outputs": tools.colorize_and_linearize_outputs(run.outputs), "fix_source": get_fix_source(source.symbols, fix_sources) } ) html = response.content.decode() self.assertEqual(remove_table_data(html), remove_table_data(expected_html)) self.assertContains(response, "hibament") self.assertContains(response, "<table") self.assertContains(response, "<tr><td>")
#!/usr/bin/env python3 """Source coding example You can change the code words, the propabilities of the symbols, and the errors of the channel. """ from __future__ import division from __future__ import print_function __author__ = 'Arpad Horvath' from coding import (Chain, FixSource, Source, Code, Hamming, Channel) source = Source([1/4, 1/4, 1/4, 1/4], length=10) #source = Source([1/2, 1/4, 1/8, 1/8], length=10) #source = FixSource("ALABAMA") chain = Chain( source, Code("1000 0100 0010 0001", symbols=source.symbols), Channel(0), verbosity = 0, ) result = chain.run()
from sources.views import (home, source_detail, code_stat, sources, simple_chain, general_chain, get_fix_source, fix_sources, urlizer, change_communication_system) from sources.arithmetic import views from coding import FixSource, Source, Code, Channel, Chain import coding from sources import tools from collections import OrderedDict import re source = Source([.25] * 4) code = Code("00 01 10 11") fix_source_chain = Chain(FixSource('ALABAMA'), tools.get_code('A:0 B:10 L:110 M:111'), Channel([2])) fix_source_chain.run() fix_source_run = fix_source_chain.runs[0] outputs = fix_source_run.outputs # print( # outputs, # [tools.color_diff(*[o.message for o in output]) for output in outputs], # sep='\n' # ) def remove_table_data(text): lines = text.splitlines() span = re.compile("<td>.*?</td>") new_text = [] for line in lines: new_line = span.sub("<td></td>", line)
from sources.arithmetic import views from coding import FixSource, Source, Code, Channel, Chain import coding from sources import tools from collections import OrderedDict import re source = Source([.25]*4) code = Code("00 01 10 11") fix_source_chain = Chain( FixSource('ALABAMA'), tools.get_code('A:0 B:10 L:110 M:111'), Channel([2]) ) fix_source_chain.run() fix_source_run = fix_source_chain.runs[0] outputs = fix_source_run.outputs # print( # outputs, # [tools.color_diff(*[o.message for o in output]) for output in outputs], # sep='\n' # ) def remove_table_data(text): lines = text.splitlines() span = re.compile("<td>.*?</td>") new_text = [] for line in lines: new_line = span.sub("<td></td>", line)