Пример #1
0
 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)
Пример #2
0
 def test_run_can_print(self):
     source = FixSource("ALABAMA")
     chain = Chain(
         source,
         Code("00 01 10 11", symbols=source.symbols),
         Hamming(4),
         Channel("bits=3,11,17,21,21,24,26"),
         verbosity=1,
     )
     chain.print_run()
     print()
     chain.print_run(with_elements=False)
     run = chain.runs[0]
     print(run.outputs, run.chain.elements, sep="\n")
Пример #3
0
 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)
Пример #4
0
 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>")
Пример #5
0
 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>")
Пример #6
0
#!/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()
Пример #7
0
#!/usr/bin/env python3

"""Example using the package.
"""

from __future__ import division
from __future__ import print_function

__author__ = 'Arpad Horvath'

from coding import (Chain, FixSource, Source, Code, Hamming, Channel)

source = FixSource("ALABAMA")

chain = Chain(
    source,
    Code("00 01 10 11", symbols=source.symbols),
    Hamming(4),
    Channel(1),
    #Channel([24,26]),  # with code 00 01 10 11 decode is broken
    #Channel(.1),
    verbosity = 1,
    )
result = chain.print_run()
Пример #8
0
from django.http import HttpRequest
from django.test import TestCase
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:
Пример #9
0
                           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>")