def _load_local(self, path: str) -> Dataset: builder = DatasetBuilder() data, description = self._read(path) for mod, snrs in description.items(): for snr in snrs: for iq in data[(mod, snr)]: builder.add(iq=iq, Modulation=mod, SNR=snr) return builder.build()
def _create_dataset(cls, extras: dict = {}): keys = ["SNR", "Modulation"] + list(extras.keys()) db = DatasetBuilder(n=cls.NSAMPLES, keys=keys) iq = np.zeros((2, cls.NSAMPLES)) for mod in cls.MODS: for snr in cls.SNRS: for _ in range(cls.NEXAMPLES): db.add(iq, SNR=snr, Modulation=mod, **extras) return db.build()
def test_isbalanced(self): """Verify that the Dataset can correctly detect imbalance. """ db = DatasetBuilder(n=TestDataset.NSAMPLES, keys=["SNR", "Modulation"]) iq = np.zeros((2, TestDataset.NSAMPLES)) for i, mod in enumerate(TestDataset.MODS): for snr in TestDataset.SNRS: for _ in range(i * TestDataset.NEXAMPLES): db.add(iq, SNR=snr, Modulation=mod) baddata = db.build() self.assertTrue(TestDataset._data.is_balanced(label="Modulation")) self.assertTrue(TestDataset._data.is_balanced(label="SNR")) self.assertFalse(baddata.is_balanced(label="Modulation")) self.assertTrue(baddata.is_balanced(label="SNR"))
def test_equality(self): """Verify that the dataset.__eq__ method works """ db = DatasetBuilder(n=TestDataset.NSAMPLES, keys=["SNR", "Modulation"]) iq = np.zeros((2, TestDataset.NSAMPLES)) for i, mod in enumerate(TestDataset.MODS): for snr in TestDataset.SNRS: for _ in range(i * TestDataset.NEXAMPLES): db.add(iq, SNR=snr, Modulation=mod) baddata = db.build() copieddata = deepcopy(TestDataset._data) # By definition, a dataset must be equivalent to itself self.assertEqual(TestDataset._data, TestDataset._data) # Verify that equality works in both directions self.assertEqual(TestDataset._data, copieddata) self.assertEqual(copieddata, TestDataset._data) # Verify that inequality works in both directions self.assertNotEqual(TestDataset._data, baddata) self.assertNotEqual(baddata, TestDataset._data)
def test_IncorrectShape(self): """Verify that the assertion on IQ shape works """ # Verify that simply not providing anything close to IQ is failed iq = np.zeros(1024) db = DatasetBuilder() with self.assertRaises(ValueError): db.add(iq) # Verify that IQ length must match defaults iq = np.zeros((2, 1024)) db = DatasetBuilder(n=128) with self.assertRaises(ValueError): db.add(iq) # Verify that IQ length must match prior rows iq = np.zeros((2, 1024)) db = DatasetBuilder() db.add(iq) iq = np.zeros((2, 2048)) with self.assertRaises(ValueError): db.add(iq)
def test_MissingRequiredMeta(self): """Verify that the assertion on missing meta data works """ # Verify that the additions must match the constructor provided keys iq = np.zeros((2, 1024)) db = DatasetBuilder(keys=["Foo", "Bar"]) with self.assertRaises(ValueError): db.add(iq) # Verify that the defaults can make up for missing keys iq = np.zeros((2, 1024)) db = DatasetBuilder(keys=["Foo", "Bar"], defaults={"Foo": 1}) db.add(iq, Bar=3) db.add(iq, Foo=5, Bar=3) with self.assertRaises(ValueError): db.add(iq, Foo=2) # Verify that the required keys are picked up from prior rows iq = np.zeros((2, 1024)) db = DatasetBuilder() db.add(iq, Foo=1, Bar=3) with self.assertRaises(ValueError): db.add(iq, Foo=2)
def test_CorrectBuilding(self): """Verify that a Dataset is properly created when given valid args. """ db = DatasetBuilder() iq = np.zeros((2, 1024)) db.add(iq, label=1) db.add(iq, label=2) db.add(iq, label=2) db.add(iq, label=3) db.add(iq, label=3) db.add(iq, label=3) db.add(iq, label=4) db.add(iq, label=4) db.add(iq, label=4) db.add(iq, label=4) dataset = db.build() self.assertEqual(len(dataset), 10) self.assertEqual( dataset.get_examples_per_class(label="label"), {1: 1, 2: 2, 3: 3, 4: 4} )