Reorganized 'utility' into own package

This commit is contained in:
2022-11-08 00:52:43 +01:00
parent bbd9d9037b
commit 781ae1442d
9 changed files with 64 additions and 98 deletions

View File

@@ -63,40 +63,6 @@ class GradientTestCase(unittest.TestCase):
self.assertEqual(np.array_equal(grad_h, expected_grad_h), True)
def test_gen_A_B(self):
"""Test the generation of the A and B sets used for the gradient calculation."""
# 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, R)
expected_A = [np.array([0, 2, 4, 6]),
np.array([1, 2, 5, 6]),
np.array([3, 4, 5, 6])]
expected_B = [np.array([0]),
np.array([1]),
np.array([0, 1]),
np.array([2]),
np.array([0, 2]),
np.array([1, 2]),
np.array([0, 1, 2])]
for A_i, expected_A_i in zip(decoder._A, expected_A):
self.assertEqual(np.array_equal(A_i, expected_A_i), True)
for B_k, expected_B_k in zip(decoder._B, expected_B):
self.assertEqual(np.array_equal(B_k, expected_B_k), True)
if __name__ == "__main__":
unittest.main()

View File

@@ -1,6 +1,8 @@
import unittest
import numpy as np
from decoders import utility
from utility import simulations
from utility import noise
class CountBitErrorsTestCase(unittest.TestCase):
@@ -15,9 +17,9 @@ class CountBitErrorsTestCase(unittest.TestCase):
d3 = np.array([0, 0, 0, 0])
y_hat3 = np.array([1, 1, 1, 1])
self.assertEqual(utility.count_bit_errors(d1, y_hat1), 2)
self.assertEqual(utility.count_bit_errors(d2, y_hat2), 0)
self.assertEqual(utility.count_bit_errors(d3, y_hat3), 4)
self.assertEqual(simulations.count_bit_errors(d1, y_hat1), 2)
self.assertEqual(simulations.count_bit_errors(d2, y_hat2), 0)
self.assertEqual(simulations.count_bit_errors(d3, y_hat3), 4)
# TODO: Is this correct?
@@ -30,11 +32,11 @@ class NoiseAmpFromSNRTestCase(unittest.TestCase):
SNR4 = -20
SNR5 = 60
self.assertEqual(utility._get_noise_amp_from_SNR(SNR1, signal_amp=1), 1)
self.assertAlmostEqual(utility._get_noise_amp_from_SNR(SNR2, signal_amp=1), 0.5, places=2)
self.assertEqual(utility._get_noise_amp_from_SNR(SNR3, signal_amp=1), 0.1)
self.assertEqual(utility._get_noise_amp_from_SNR(SNR4, signal_amp=1), 10)
self.assertEqual(utility._get_noise_amp_from_SNR(SNR5, signal_amp=2), 0.002)
self.assertEqual(noise.get_noise_amp_from_SNR(SNR1, signal_amp=1), 1)
self.assertAlmostEqual(noise.get_noise_amp_from_SNR(SNR2, signal_amp=1), 0.5, places=2)
self.assertEqual(noise.get_noise_amp_from_SNR(SNR3, signal_amp=1), 0.1)
self.assertEqual(noise.get_noise_amp_from_SNR(SNR4, signal_amp=1), 10)
self.assertEqual(noise.get_noise_amp_from_SNR(SNR5, signal_amp=2), 0.002)
if __name__ == '__main__':