예제 #1
0
class FixersClassTests(unittest.TestCase):
    def setUp(self):
        self.data = Data()
        self.dmgNA = DMGNA()
        self.x = np.arange(25, dtype=float).reshape((5, 5))
        self.data.setX(self.x).damage(self.dmgNA, 0.5)

    def test_meanZeroInit(self):
        z = FXZero()
        self.assertIsInstance(z, FXZero, "instantiation failed")

    def test_meanFixerInit(self):
        m = FXMean()
        self.assertIsInstance(m, FXMean, "instantiation failed")

    def test_zeroFix(self):
        d = copy.deepcopy(self.data)
        d.info()  # before fix
        z = FXZero()
        d.fix(z)
        d.info()  # after fix

    def test_meanFix(self):
        d = copy.deepcopy(self.data)
        d.info()  # before fix
        m = FXMean()
        d.fix(m)
        d.info()  # after fix
예제 #2
0
class DamagersClassTests(unittest.TestCase):
    def setUp(self):
        self.data = Data()
        x = np.arange(25, dtype=float).reshape((5, 5))
        self.data.setX(x)

    def test_prevention_of_the_abstract_damager_instatination(self):
        with self.assertRaises(Exception, msg="abstract class instantiated"):
            dmg = Damager()

    def test_instatination_of_the_DMGNA_damager(self):
        dmg = DMGNA()
        self.assertIsInstance(dmg, DMGNA, "it has wrong type")

    def test_instatination_of_the_DMGNoiseFeatures_damager(self):
        dmg = DMGNoiseFeatures()
        self.assertIsInstance(dmg, DMGNoiseFeatures, "it has wrong type")

    def test_instatination_of_the_DMGNoiseValues_damager(self):
        dmg = DMGNoiseValues()
        self.assertIsInstance(dmg, DMGNoiseValues, "it has wrong type")

    def test_data_damaged_by_DMGNA_damager(self):
        d = deepcopy(self.data)
        dmg = DMGNA()
        d.damage(dmg, 0.5)
        self.assertNotEqual(
            hash("{}".format(d.x)),
            hash("{}".format(self.data.x)),
            "damager did not change the data",
        )

    def test_data_damaged_by_DMGNoiseFeatures_damager(self):
        d = deepcopy(self.data)
        dmg = DMGNoiseFeatures()
        noiseFeaturesCount = 10  # testing constant
        d.damage(dmg, noiseFeaturesCount)
        self.assertEqual(
            d.x.shape[1] - noiseFeaturesCount,
            self.data.x.shape[1],
            "damager created wrong number of features",
        )

    @unittest.skip  # not implemented
    def test_data_damaged_by_DMGNoiseValues_damager(self):
        d = deepcopy(self.data)
        dmg = DMGNoiseValues()
        d.damage(dmg, 0.5)
        self.assertNotEqual(
            hash("{}".format(d.x)),
            hash("{}".format(self.data.x)),
            "damager did not change the data",
        )
예제 #3
0
class DataClassTests(unittest.TestCase):
    def setUp(self):
        self.data = Data()
        self.dataStoragePath = "data"
        # return super().setUp()

    def test_instantiation(self):
        self.assertIsInstance(self.data, Data, "it has wrong type")

    def test_setArrayOfIntegersAsX(self):
        self.data.setX([[1, 2, 3, 4, 5], [1, 2, 3, 4, 5]])
        self.assertIsInstance(self.data, Data,
                              "does not accept array of integers")

    def test_typeConvertionForX(self):
        self.data.setX([[1, 2, 3], [1, 2, 3]])
        self.assertIsInstance(
            self.data.x[1, 1],
            float,
            "the type of the data element is different than `float`",
        )

    def test_stringInArrayAsX(self):
        with self.assertRaises(ValueError):
            self.data.setX([[1, "a", 3], [1, 2, 3]])

    def test_oneDimentionalArrayAsX(self):
        with self.assertRaises(Exception):
            self.data.setX([1, 2, 3, 4, 5, 6])

    def test_storeInFile(self):
        url = self.data.save(self.dataStoragePath)
        self.assertGreater(len(url), 1, "url to file empty")
        size = os.stat(url).st_size
        self.assertGreater(size, 1, "file empty")

    def test_readFromFile(self):
        x = [[100.0, 200.0, 300.0], [1.0, 2.0, 3.0]]
        self.data.setX(x)
        url = self.data.save(self.dataStoragePath)

        d = Data()
        d.read(url)
        self.assertListEqual(
            list(self.data.x.flatten()),
            list(d.x.flatten()),
            "the data is damaged during store/read operation",
        )

    def test_makeTarget(self):
        self.data.setX([[1, 2, 3], [2, 2, 4], [3, 4, 2]])
        tg = TGAlpha()
        self.data.makeTarget(tg)
        self.assertIsInstance(self.data.y, ndarray, "target is not an array")

    def test_targetValues(self):
        self.data.setX([[1, 2, 3], [2, 2, 4], [3, 4, 2]])
        tg = TGAlpha()
        self.data.makeTarget(tg)
        self.assertListEqual(
            list(set(self.data.y.flatten())),
            [0.0, 1.0],
            "target class not in set {0., 1.}",
        )