Skip to content

Commit d7a7900

Browse files
committed
release
1 parent d5d3ebf commit d7a7900

File tree

6 files changed

+52
-61
lines changed

6 files changed

+52
-61
lines changed

explorecapability/buildcodetest.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import unittest
22

33
from linearcode import ErrorCorrectingCode
4-
from test.testutils import remove_files
4+
from fileutils import remove_files
55

66

77
class BuildCodeTest(unittest.TestCase):
@@ -10,4 +10,4 @@ def setUpClass(cls) -> None:
1010
remove_files()
1111

1212
def test_capability(self):
13-
ErrorCorrectingCode(r=23, n=28, t=5, channel_error_probability=0.01)
13+
ErrorCorrectingCode(r=19, n=23, t=4, channel_error_probability=0.01)

test/testutils.py fileutils.py

File renamed without changes.

linearcode.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ def read_file_to_dict(name):
685685

686686

687687
@timeout.timeout(yaml.safe_load(open("config.yml"))['timeout'])
688-
def code(coder_file, message, m_length, error=0):
688+
def encode(coder_file, message, m_length, error=0):
689689
"""
690690
Codes a message and distorts the code
691691
by the specified error vector if any

readme.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,12 @@ The 1st file is used in II, the 2nd and the 3rd are used in III.
2020
II. The generator matrix is used to code a message.
2121
Then an arbitrary or a particular error vector is added to the codeword.
2222

23-
III. The parity check matrix and the standard array are used to decode a distorted message.
23+
III. The parity check matrix and the standard array are used to decode a distorted message.
24+
25+
To run the program use Python 3:
26+
1. Clone the repo using "git clone 'repo url'".
27+
2. Open terminal and "cd 'directory where you just have cloned the repo'".
28+
3. Type "python3" in the prompt.
29+
4. Type "from simulation import simulate".
30+
5. Type "simulate()", it starts the program.
31+
6. Follow instructions on the screen.

simulation.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import yaml
2+
3+
from fileutils import remove_files
4+
from linearcode import ErrorCorrectingCode, encode, decode, get_random_number_of_hamming_weight
5+
6+
7+
def simulate():
8+
remove_files()
9+
r = int(input("Enter number of check bits r: "))
10+
n = int(input("Enter length of a code word n: "))
11+
t = int(input("Enter number of error to correct t: "))
12+
k = n - r
13+
ErrorCorrectingCode(r=r, n=n, t=t)
14+
message = int(input("Enter a message: "), 2)
15+
error = int(input("Enter an error in binary, 0 if you want to skip: "), 2)
16+
if error == 0:
17+
error = get_random_number_of_hamming_weight(n, t)
18+
config = yaml.safe_load(open('config.yml'))
19+
codeword, distorted_codeword, error = encode(
20+
coder_file=config['coder-generator'],
21+
message=message,
22+
m_length=k,
23+
error=error)
24+
print("Code word: {0:>0{a}b}".format(codeword, a=n))
25+
print("Distorted code word: {0:>0{a}b}".format(distorted_codeword, a=n))
26+
print("Error vector: {0:>0{a}b}".format(error, a=n))
27+
distorted_codeword = int(input("Enter a distorted codeword: "), 2)
28+
decoded_message = decode(
29+
parity_check_file=config['decoder-parity-check'],
30+
n=n,
31+
syndrome_file=config['decoder-syndrome-decoding'],
32+
distorted_code=distorted_codeword)
33+
print("Decoded message: {0:>0{a}b}".format(decoded_message, a=n - r))

test/linearcodetest.py

+7-57
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import random
22
import unittest
3+
from itertools import combinations
34

45
import yaml
5-
from itertools import combinations
66

77
import linearcode
8+
from fileutils import remove_files
89
from linearcode import LinearCode, \
910
ErrorCorrectingCode, \
1011
add_matrix_in_front, \
@@ -29,7 +30,6 @@
2930
sum_modulo_2, \
3031
transpose_matrix, \
3132
get_random_number_of_hamming_weight
32-
from test.testutils import remove_files
3333

3434

3535
class LinearCodeTest(unittest.TestCase):
@@ -89,10 +89,8 @@ def test_f_l_i_v_inner_loop(self):
8989
def test_sum_modulo_2(self):
9090
test_list = []
9191
n = 10
92-
print()
9392
for i in range(n):
9493
test_list.append(i + 1)
95-
print('{0:0>{width}b}'.format(i + 1, width=n))
9694
assert sum_modulo_2(iterable=test_list) == 11
9795

9896
def test_allocate_bits_in_column(self):
@@ -107,10 +105,8 @@ def test_fill_parity_check_matrix_and_a_matrix_transposed(self):
107105
n=self.n,
108106
r=self.r,
109107
d=self.d)
110-
print()
111108
for line in parity_check_matrix:
112109
assert line != 0
113-
print(format(line, '#0{0}b'.format(self.n + 2)))
114110
for line in a_matrix_transposed:
115111
assert get_hamming_weight(line) >= self.d - 1
116112

@@ -133,7 +129,6 @@ def test_add_matrix_to_front(self):
133129
k = 7
134130
r = n - k
135131
matrix1 = []
136-
print()
137132
for i in range(k):
138133
matrix1.append(i + 1)
139134
matrix2 = fill_i_matrix(size=k)
@@ -153,34 +148,21 @@ def test_generator_matrix_from_a_matrix_transposed(self):
153148
r = n - k
154149
d = 1
155150
parity_check_matrix, a_matrix_transposed = fill_parity_check_matrix_and_a_matrix_transposed(n=n, r=r, d=d)
156-
print()
157-
for i in parity_check_matrix:
158-
print(format(i, '#0{0}b'.format(n + 2)))
159-
print()
160151
for i in fill_generator_matrix_from_a_matrix_transposed(
161152
a_matrix_transposed=a_matrix_transposed,
162153
k=k,
163154
r=r):
164155
assert get_hamming_weight(i) >= d
165-
print(format(i, '#0{0}b'.format(n + 2)))
166156

167157
def test_transpose_matrix(self):
168158
n = 10
169159
r = 7
170160
d = 1
171161
matrix, a_matrix_transposed = fill_parity_check_matrix_and_a_matrix_transposed(n=n, r=r, d=d)
172-
print()
173-
for i in matrix:
174-
print(format(i, '#0{0}b'.format(n + 2)))
175-
print()
176162
transposed_matrix = transpose_matrix(matrix=matrix, number_of_columns=n)
177-
for i in transposed_matrix:
178-
print(format(i, '#0{0}b'.format(r + 2)))
179-
print()
180163
doubly_transposed_matrix = transpose_matrix(matrix=transposed_matrix, number_of_columns=r)
181164
for i in range(len(doubly_transposed_matrix)):
182165
assert doubly_transposed_matrix[i] == matrix[i]
183-
print(format(doubly_transposed_matrix[i], '#0{0}b'.format(n + 2)))
184166

185167
def test_compute_parity(self):
186168
number_parity_0 = 0b101
@@ -259,28 +241,12 @@ def test_generate_syndrome_decoding_table(self):
259241
a_matrix_transposed=a_matrix_transposed,
260242
k=k,
261243
r=r)
262-
print()
263-
print('A parity check matrix:')
264-
print()
265-
for num in parity_check_matrix:
266-
print(format(num, '#0{0}b'.format(n + 2)))
267-
print()
268-
print('A generator matrix:')
269-
print()
270-
for num in generator_matrix:
271-
print(format(num, '#0{0}b'.format(n + 2)))
272244
syndrome_decoding_table = generate_syndrome_decoding_table(
273245
generator_matrix=generator_matrix,
274246
parity_check_matrix=parity_check_matrix,
275247
n=n,
276248
k=k,
277249
d=d)
278-
for key in syndrome_decoding_table.keys():
279-
print()
280-
print(format(key, '#0{0}b'.format(k + 2)))
281-
print('---------')
282-
for word in syndrome_decoding_table[key]:
283-
print(format(word, '#0{0}b'.format(n + 2)))
284250
for key in syndrome_decoding_table.keys():
285251
if key == 0:
286252
continue
@@ -309,21 +275,17 @@ def test_read_file_to_dict(self):
309275
zero_word_counter = 0
310276
word_counter = 0
311277
for key in dictionary.keys():
312-
print()
313-
print('{0:0>{width}b}'.format(key, width=r))
314-
print('---------')
315278
for word in dictionary[key]:
316279
if word == 0:
317280
zero_word_counter += 1
318-
print('{0:0>{width}b}'.format(word, width=n))
319281
word_counter += 1
320282
assert zero_word_counter <= 1
321283
assert word_counter > 2 ** r
322284

323285
def test_code(self):
324286
config = yaml.safe_load(open('config-test.yml'))
325287
for i in range(2 ** self.k):
326-
code, distorted_code, error = linearcode.code(
288+
code, distorted_code, error = linearcode.encode(
327289
coder_file=config['coder-generator-test'],
328290
message=i,
329291
m_length=self.k,
@@ -336,13 +298,8 @@ def test_code(self):
336298
num_col_m2=1
337299
),
338300
number_of_columns=1)[0] == 0
339-
print()
340-
print('Message: {0}\n'.format(i))
341-
print('Code: {0:0>{width}b}\n'.format(code, width=self.n))
342-
print('Distorted code: {0:0>{width}b}\n'.format(distorted_code, width=self.n))
343-
print('Error: {0:0>{width}b}\n'.format(error, width=self.n))
344301
for i in range(2 ** self.k):
345-
code, distorted_code, error = linearcode.code(
302+
code, distorted_code, error = linearcode.encode(
346303
coder_file=config['coder-generator-test'],
347304
message=i,
348305
m_length=self.k)
@@ -359,7 +316,7 @@ def test_code(self):
359316
def test_code_exception(self):
360317
config = yaml.safe_load(open('config-test.yml'))
361318
with self.assertRaises(ValueError):
362-
linearcode.code(
319+
linearcode.encode(
363320
coder_file=config['coder-generator-test'],
364321
message=1,
365322
m_length=2,
@@ -375,18 +332,15 @@ def test_decode(self):
375332
distorted_code=int('110010000110111000100', 2)
376333
)
377334
assert decoded_message == message
378-
print()
379-
print(decoded_message)
380335

381336
def test_code_decode(self):
382337
config = yaml.safe_load(open('config-test.yml'))
383338
number_of_unfixed_errors = 0
384-
print()
385339
number_of_repetitions = 100
386340
for i in range(number_of_repetitions):
387341
message = random.randrange(2 ** self.k)
388342
rand_error = get_random_number_of_hamming_weight(self.n, int((self.d - 1) / 2))
389-
code, distorted_code, error = linearcode.code(
343+
code, distorted_code, error = linearcode.encode(
390344
coder_file=config['coder-generator-test'],
391345
message=message,
392346
m_length=self.k,
@@ -400,12 +354,7 @@ def test_code_decode(self):
400354
)
401355
if decoded != message:
402356
number_of_unfixed_errors += 1
403-
print('Message is {0}, distorted code is {1:0>{width}b}, error is {2:0>{width}b}, deciphered is {3}'
404-
.format(message, distorted_code, rand_error, decoded, width=self.n))
405-
print()
406357
assert number_of_unfixed_errors == 0
407-
print('Number of errors which weren\'t fixed is {0}, the rate is {1}'
408-
.format(number_of_unfixed_errors, number_of_unfixed_errors / number_of_repetitions))
409358

410359
def test_get_random_number_of_hamming_weight(self):
411360
for i in range(100):
@@ -424,5 +373,6 @@ def test_get_random_number_of_hamming_weight_exception(self):
424373
def tearDownClass(cls) -> None:
425374
remove_files()
426375

376+
427377
if __name__ == '__main__':
428378
unittest.main()

0 commit comments

Comments
 (0)