Moved python files from sw to sw/python; Moved scritps into sw/python/scripts
This commit is contained in:
1
sw/python/utility/test/__init__.py
Normal file
1
sw/python/utility/test/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
"""This package contains unit tests."""
|
||||
72
sw/python/utility/test/test_proximal.py
Normal file
72
sw/python/utility/test/test_proximal.py
Normal file
@@ -0,0 +1,72 @@
|
||||
import unittest
|
||||
import numpy as np
|
||||
from decoders import proximal
|
||||
|
||||
|
||||
class CheckParityTestCase(unittest.TestCase):
|
||||
"""Test case for the check_parity function."""
|
||||
|
||||
def test_check_parity(self):
|
||||
# Hamming(7,4) code
|
||||
G = np.array([[1, 1, 1, 0, 0, 0, 0],
|
||||
[1, 0, 0, 1, 1, 0, 0],
|
||||
[0, 1, 0, 1, 0, 1, 0],
|
||||
[1, 1, 0, 1, 0, 0, 1]])
|
||||
H = np.array([[1, 0, 1, 0, 1, 0, 1],
|
||||
[0, 1, 1, 0, 0, 1, 1],
|
||||
[0, 0, 0, 1, 1, 1, 1]])
|
||||
R = np.array([[0, 0, 1, 0, 0, 0, 0],
|
||||
[0, 0, 0, 0, 1, 0, 0],
|
||||
[0, 0, 0, 0, 0, 1, 0],
|
||||
[0, 0, 0, 0, 0, 0, 1]])
|
||||
|
||||
decoder = proximal.ProximalDecoder(H)
|
||||
|
||||
d1 = np.array([0, 1, 0, 1])
|
||||
c1 = np.dot(np.transpose(G), d1) % 2
|
||||
|
||||
d2 = np.array([0, 0, 0, 0])
|
||||
c2 = np.dot(np.transpose(G), d2) % 2
|
||||
|
||||
d3 = np.array([1, 1, 1, 1])
|
||||
c3 = np.dot(np.transpose(G), d3) % 2
|
||||
|
||||
invalid_codeword = np.array([0, 1, 1, 0, 1, 1, 1])
|
||||
|
||||
self.assertEqual(decoder._check_parity(c1), True)
|
||||
self.assertEqual(decoder._check_parity(c2), True)
|
||||
self.assertEqual(decoder._check_parity(c3), True)
|
||||
self.assertEqual(decoder._check_parity(invalid_codeword), False)
|
||||
|
||||
|
||||
class GradientTestCase(unittest.TestCase):
|
||||
"""Test case for the calculation of the gradient of the
|
||||
code-constraint-polynomial."""
|
||||
|
||||
def test_grad_h(self):
|
||||
"""Test the gradient of the code-constraint polynomial."""
|
||||
# Hamming(7,4) code
|
||||
G = np.array([[1, 1, 1, 0, 0, 0, 0],
|
||||
[1, 0, 0, 1, 1, 0, 0],
|
||||
[0, 1, 0, 1, 0, 1, 0],
|
||||
[1, 1, 0, 1, 0, 0, 1]])
|
||||
H = np.array([[1, 0, 1, 0, 1, 0, 1],
|
||||
[0, 1, 1, 0, 0, 1, 1],
|
||||
[0, 0, 0, 1, 1, 1, 1]])
|
||||
R = np.array([[0, 0, 1, 0, 0, 0, 0],
|
||||
[0, 0, 0, 0, 1, 0, 0],
|
||||
[0, 0, 0, 0, 0, 1, 0],
|
||||
[0, 0, 0, 0, 0, 0, 1]])
|
||||
|
||||
x = np.array([1, 2, -1, -2, 2, 1, -1]) # Some randomly chosen vector
|
||||
expected_grad_h = np.array(
|
||||
[4, 26, -8, -36, 38, 28, -32]) # Manually calculated result
|
||||
|
||||
decoder = proximal.ProximalDecoder(H)
|
||||
grad_h = decoder._grad_h(x)
|
||||
|
||||
self.assertEqual(np.array_equal(grad_h, expected_grad_h), True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
55
sw/python/utility/test/test_soft_decision.py
Normal file
55
sw/python/utility/test/test_soft_decision.py
Normal file
@@ -0,0 +1,55 @@
|
||||
import unittest
|
||||
import numpy as np
|
||||
from decoders import maximum_likelihood
|
||||
|
||||
|
||||
class CodewordGenerationTestCase(unittest.TestCase):
|
||||
def test_codeword_generation(self):
|
||||
"""Test case for data word and code word generation."""
|
||||
# Hamming(7,4) code
|
||||
G = np.array([[1, 1, 1, 0, 0, 0, 0],
|
||||
[1, 0, 0, 1, 1, 0, 0],
|
||||
[0, 1, 0, 1, 0, 1, 0],
|
||||
[1, 1, 0, 1, 0, 0, 1]])
|
||||
H = np.array([[1, 0, 1, 0, 1, 0, 1],
|
||||
[0, 1, 1, 0, 0, 1, 1],
|
||||
[0, 0, 0, 1, 1, 1, 1]])
|
||||
|
||||
decoder = maximum_likelihood.MLDecoder(G, H)
|
||||
|
||||
expected_datawords = np.array([[0, 0, 0, 0],
|
||||
[0, 0, 0, 1],
|
||||
[0, 0, 1, 0],
|
||||
[0, 0, 1, 1],
|
||||
[0, 1, 0, 0],
|
||||
[0, 1, 0, 1],
|
||||
[0, 1, 1, 0],
|
||||
[0, 1, 1, 1],
|
||||
[1, 0, 0, 0],
|
||||
[1, 0, 0, 1],
|
||||
[1, 0, 1, 0],
|
||||
[1, 0, 1, 1],
|
||||
[1, 1, 0, 0],
|
||||
[1, 1, 0, 1],
|
||||
[1, 1, 1, 0],
|
||||
[1, 1, 1, 1]])
|
||||
|
||||
expected_codewords = np.array([[0, 0, 0, 0, 0, 0, 0],
|
||||
[1, 1, 0, 1, 0, 0, 1],
|
||||
[0, 1, 0, 1, 0, 1, 0],
|
||||
[1, 0, 0, 0, 0, 1, 1],
|
||||
[1, 0, 0, 1, 1, 0, 0],
|
||||
[0, 1, 0, 0, 1, 0, 1],
|
||||
[1, 1, 0, 0, 1, 1, 0],
|
||||
[0, 0, 0, 1, 1, 1, 1],
|
||||
[1, 1, 1, 0, 0, 0, 0],
|
||||
[0, 0, 1, 1, 0, 0, 1],
|
||||
[1, 0, 1, 1, 0, 1, 0],
|
||||
[0, 1, 1, 0, 0, 1, 1],
|
||||
[0, 1, 1, 1, 1, 0, 0],
|
||||
[1, 0, 1, 0, 1, 0, 1],
|
||||
[0, 0, 1, 0, 1, 1, 0],
|
||||
[1, 1, 1, 1, 1, 1, 1]])
|
||||
|
||||
self.assertEqual(np.array_equal(decoder._datawords, expected_datawords), True)
|
||||
self.assertEqual(np.array_equal(decoder._codewords, expected_codewords), True)
|
||||
48
sw/python/utility/test/test_utility.py
Normal file
48
sw/python/utility/test/test_utility.py
Normal file
@@ -0,0 +1,48 @@
|
||||
import unittest
|
||||
import numpy as np
|
||||
|
||||
from utility import noise, codes
|
||||
|
||||
|
||||
# TODO: Rewrite tests for new SNR calculation
|
||||
class NoiseAmpFromSNRTestCase(unittest.TestCase):
|
||||
"""Test case for noise amplitude calculation."""
|
||||
|
||||
def test_get_noise_amp_from_SNR(self):
|
||||
SNR1 = 0
|
||||
SNR2 = 3
|
||||
SNR3 = 20
|
||||
SNR4 = -20
|
||||
|
||||
var1 = noise.get_noise_variance_from_SNR(SNR1, n=8, k=8)
|
||||
var2 = noise.get_noise_variance_from_SNR(SNR2, n=8, k=8)
|
||||
var3 = noise.get_noise_variance_from_SNR(SNR3, n=8, k=8)
|
||||
var4 = noise.get_noise_variance_from_SNR(SNR4, n=8, k=8)
|
||||
|
||||
self.assertEqual(var1, 1 * 0.5)
|
||||
self.assertAlmostEqual(var2, 0.5 * 0.5, places=2)
|
||||
self.assertEqual(var3, 0.01 * 0.5)
|
||||
self.assertEqual(var4, 100 * 0.5)
|
||||
|
||||
|
||||
class CodesTestCase(unittest.TestCase):
|
||||
"""Tests relating to the 'codes' utilities."""
|
||||
|
||||
def test_get_systematic_H(self):
|
||||
# Hamming(7,4) code
|
||||
G = np.array([[1, 0, 0, 0, 0, 1, 1],
|
||||
[0, 1, 0, 0, 1, 0, 1],
|
||||
[0, 0, 1, 0, 1, 1, 0],
|
||||
[0, 0, 0, 1, 1, 1, 1]])
|
||||
|
||||
expected_H = np.array([[0, 1, 1, 1, 1, 0, 0],
|
||||
[1, 0, 1, 1, 0, 1, 0],
|
||||
[1, 1, 0, 1, 0, 0, 1]])
|
||||
|
||||
H = codes.get_systematic_H(G)
|
||||
|
||||
self.assertEqual(np.array_equal(expected_H, H), True)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
24
sw/python/utility/test/test_visualization.py
Normal file
24
sw/python/utility/test/test_visualization.py
Normal file
@@ -0,0 +1,24 @@
|
||||
import unittest
|
||||
from utility import visualization
|
||||
|
||||
|
||||
class NumRowsTestCase(unittest.TestCase):
|
||||
def test_get_num_rows(self):
|
||||
"""Test case for number of row calculation."""
|
||||
|
||||
num_rows1 = visualization._get_num_rows(num_graphs=4, num_cols=3)
|
||||
expected_rows1 = 2
|
||||
|
||||
num_rows2 = visualization._get_num_rows(num_graphs=5, num_cols=2)
|
||||
expected_rows2 = 3
|
||||
|
||||
num_rows3 = visualization._get_num_rows(num_graphs=4, num_cols=4)
|
||||
expected_rows3 = 1
|
||||
|
||||
num_rows4 = visualization._get_num_rows(num_graphs=4, num_cols=5)
|
||||
expected_rows4 = 1
|
||||
|
||||
self.assertEqual(num_rows1, expected_rows1)
|
||||
self.assertEqual(num_rows2, expected_rows2)
|
||||
self.assertEqual(num_rows3, expected_rows3)
|
||||
self.assertEqual(num_rows4, expected_rows4)
|
||||
Reference in New Issue
Block a user