def test_addition(self): prototype1 = Reporter(attributes=[DictValue('noisy', faults=GaussianNoise(sigma=0.1)), DictValue('noisier', faults=GaussianNoise(sigma=0.2))]) prototype2 = Reporter(attributes=[DictValue('noised', faults=GaussianNoise(sigma=0.1)), DictValue('noisier', faults=InterruptionFault(likelihood=0.1))]) new_prototype = prototype1 + prototype2 self.assertIs(len(new_prototype.attributes), 3) self.assertIs(len(new_prototype.get_attribute_by_id('noisier').faults), 2) pass
def test_fault_collection(self): faults = evaluate_faults( [GaussianNoise(sigma=0.1), GaussianNoise(sigma=0.2)]) self.assertEqual(len(faults), 2) for f in faults: self.assertIsInstance(f, GaussianNoise) self.assertEqual(literal_eval(faults[0].init_statement), {'sigma': 0.1}) self.assertEqual(literal_eval(faults[1].init_statement), {'sigma': 0.2}) pass
def test_single_fault(self): faults = evaluate_faults(GaussianNoise(sigma=0.1)) self.assertEqual(len(faults), 1) self.assertIsInstance(faults[0], GaussianNoise) self.assertEqual(literal_eval(faults[0].init_statement), {'sigma': 0.1}) pass
def test_gaussian(self): test_frame = pd.DataFrame({'col1': range(5), 'col2': range(5)}) noise = GaussianNoise(1) noisy_frame = noise.impact(test_frame) with self.assertRaises(AssertionError): pd_testing.assert_frame_equal(test_frame, noisy_frame) pass
def test_introspected_attribute_fault_mapping(self): new_reporter = Reporter(faults=GaussianNoise(sigma=1)) data = {'att1': 1, 'att2': 2} output_data = new_reporter(data).observed self.assertNotEqual(data, output_data) self.assertEqual(data.keys(), output_data.keys()) pass
def test_fault_application(self): constitutively_fallible_object = Fallible(GaussianNoise(sigma=0.1)) faults, result = constitutively_fallible_object.apply_all_faults(100) self.assertEqual(len(faults), 1) self.assertIsInstance(faults[0], GaussianNoise) self.assertEqual(literal_eval(faults[0].init_statement), {'sigma': 0.1}) self.assertIsNot(100, result) pass
def test_attribute_fault_mapping(self): new_reporter = Reporter( attributes=[DictValue('att1'), DictValue('att2')], faults=GaussianNoise(sigma=1)) data = {'att1': 1, 'att2': 2} output_data = new_reporter(data).observed self.assertNotEqual(data, output_data) pass
def machine_error(scale): """ Simple example Noisifier recipe, applies gaussian noise and occasional interruptions to the input depending on the scale given, recommended scale range from 1-10 """ return Noisifier(reporter=Reporter(faults=[ GaussianNoise(sigma=0.1 * scale), InterruptionFault(likelihood=min(1, 0.01 * scale)) ]), faults=None)
def test_addition(self): test_attribute1 = AttributeReader('test', faults=GaussianNoise(sigma=1)) test_attribute2 = AttributeReader( 'test', faults=InterruptionFault(likelihood=1)) new_attribute = test_attribute1 + test_attribute2 self.assertIs(len(new_attribute.faults), 2) self.assertIsInstance(new_attribute.faults[0], GaussianNoise) self.assertIsInstance(new_attribute.faults[1], InterruptionFault) pass
def test_attribute_faults(self): new_prototype = Reporter(attributes=[DictValue('noisy', faults=GaussianNoise(sigma=0.1)), DictValue('noiseless')]) report0 = new_prototype({'noisy': 100, 'noiseless': 100}) self.assertEqual(report0.truth, {'noisy': 100, 'noiseless': 100}) self.assertEqual(report0.observed['noiseless'], 100) self.assertNotEqual(report0.observed['noisy'], 100) self.assertEqual(len(report0.triggered_faults['reporter']), 0) self.assertEqual(len(report0.triggered_faults['noiseless']), 0) self.assertEqual(len(report0.triggered_faults['noisy']), 1) self.assertIsInstance(report0.triggered_faults['noisy'][0], GaussianNoise) pass
def test_gaussian(self): noise = GaussianNoise(30) input_image = Image.open( str(pkg_resources.resource_filename(__name__, 'test_image.jpeg'))) initial_hash = hashlib.sha512() initial_hash.update(np.array(input_image)) messy_image = noise.impact(input_image) messy_hash = hashlib.sha512() messy_hash.update(np.array(messy_image)) self.assertNotEqual(initial_hash.digest(), messy_hash.digest()) self.assertEqual(input_image.size, messy_image.size) self.assertEqual(input_image.mode, messy_image.mode) pass
def test_object_series_call(self): class Foo: def __init__(self, noisy, noiseless): self.noisy = noisy self.noiseless = noiseless new_prototype = Reporter(attributes=[ ObjectAttribute('noisy', faults=GaussianNoise(sigma=0.1)), ObjectAttribute('noiseless') ]) series_builder = Noisifier(reporter=new_prototype) data = [Foo(100, 100), Foo(10, 100), Foo(100, 10)] result = series_builder(data) self.assertEqual(len([i for i in result]), 3) for truth, new in zip(data, result): self.assertEqual(truth.noiseless, new.noiseless) self.assertNotEqual(truth.noisy, new.noisy) pass
def test_looping(self): new_prototype = Reporter(attributes=[ DictValue('noisy', faults=GaussianNoise(sigma=0.1)), DictValue('noiseless') ]) series_builder = Noisifier(reporter=new_prototype) data = [{ 'noisy': 100, 'noiseless': 100 }, { 'noisy': 10, 'noiseless': 50 }, { 'noisy': 100, 'noiseless': 10 }] result = series_builder(data, loop=True) for index, value in enumerate(result): if index == 4: break self.assertEqual(value['noiseless'], 50) pass
def test_dict_series_call(self): new_prototype = Reporter(attributes=[ DictValue('noisy', faults=GaussianNoise(sigma=0.1)), DictValue('noiseless') ]) series_builder = Noisifier(reporter=new_prototype) data = [{ 'noisy': 100, 'noiseless': 100 }, { 'noisy': 10, 'noiseless': 100 }, { 'noisy': 100, 'noiseless': 10 }] result = series_builder(data) self.assertEqual(len([i for i in result]), 3) for truth, new in zip(data, result): self.assertEqual(truth['noiseless'], new['noiseless']) self.assertNotEqual(truth['noisy'], new['noisy']) pass
def fault_strategy(): return [GaussianNoise(sigma=0.1)]
def test_method_delegation(self): new_prototype = Reporter(attributes=[DictValue('noisy', faults=GaussianNoise(sigma=0.1)), DictValue('noiseless')]) report = new_prototype({'noisy': 100, 'noiseless': 100}) self.assertEqual(set(report.keys()), {'noisy', 'noiseless'}) pass
def test_slicing(self): new_prototype = Reporter(attributes=[DictValue('noisy', faults=GaussianNoise(sigma=0.1)), DictValue('noiseless')]) report = new_prototype({'noisy': 100, 'noiseless': 100}) self.assertEqual(report['noiseless'], 100) pass