def test_sample_inputs(self): """This checks the shape and type of the returned multidimensional array.""" n = 8 N = 2**n all_arr = sample_inputs(n, N) self.check_multi_dimensional_array(all_arr, N, n, BIT_TYPE) N = 2**int(n / 2) rand_arr = sample_inputs(n, N) self.check_multi_dimensional_array(rand_arr, N, n, BIT_TYPE)
def test_uniqueness_set(self): """This method tests the uniqueness set generation function.""" n = 8 k = 1 N = 2**n instance_count = 25 measurements = 2 transformation = LTFArray.transform_id combiner = LTFArray.combiner_xor instances = [ LTFArray(weight_array=LTFArray.normal_weights( n=n, k=k, random_instance=RandomState(0xA1A1 + i)), transform=transformation, combiner=combiner) for i in range(instance_count) ] challenges = sample_inputs(n, N, random_instance=RandomState(0xFAB10)) uniqueness_set = PropertyTest.uniqueness_set(instances, challenges, measurements=measurements) # Check uniqueness_set to have the expected number of elements self.assertEqual(len(uniqueness_set), N * measurements) # For normal distributed weights is the expected uniqueness near 0.5 self.assertEqual(round(mean(uniqueness_set), 1), 0.5)
def test_reliability(self): """This method tests the test_reliability calculation.""" n = 8 k = 8 N = 2**n transformation = LTFArray.transform_id combiner = LTFArray.combiner_xor instance = LTFArray( weight_array=LTFArray.normal_weights( n=n, k=k, random_instance=RandomState(0xA1A1)), transform=transformation, combiner=combiner, ) challenges = sample_inputs(n, N, random_instance=RandomState(0xFAB1A)) reliabilities = [] for challenge in challenges: reliabilities.append( PropertyTest.reliability(instance, reshape(challenge, (1, n)))) # For noiseless simulations the responses are always the same hence the reliability is 0% assert_array_equal(reliabilities, repeat(0.0, N)) noisy_instance = NoisyLTFArray( weight_array=NoisyLTFArray.normal_weights( n=n, k=k, random_instance=RandomState(0xA1A1)), transform=transformation, combiner=combiner, sigma_noise=15.0, random_instance=RandomState(0x5015E), ) for challenge in challenges: reliability = PropertyTest.reliability(noisy_instance, reshape(challenge, (1, n))) # For noisy simulations the responses should vary self.assertNotEqual(reliability, 0.0)
def prepare(self): self.set = sample_inputs(self.parameters.n, self.parameters.N, RandomState(seed=self.parameters.seed_input)) self.ltf_array = LTFArray( weight_array=LTFArray.normal_weights(self.parameters.n, self.parameters.k), transform=self.parameters.transform, combiner=self.parameters.combiner, )
def run(self): """Runs a property test.""" ins_gen_function = getattr(self, self.parameters.ins_gen_function) instances = ins_gen_function(**self.parameters.param_ins_gen) n = self.parameters.param_ins_gen['n'] challenge_prng = RandomState(self.parameters.challenge_seed) challenges = array(list(sample_inputs(n, self.parameters.challenge_count, random_instance=challenge_prng))) property_test = PropertyTest(instances, logger=self.progress_logger) test_function = getattr(PropertyTest, self.parameters.test_function) self.result = test_function(property_test, challenges, measurements=self.parameters.measurements)
def test_reliability_statistic(self): """This method tests the reliability statistic of an instance set.""" n = 8 k = 1 N = 2**n instance_count = 2 measurements = 100 transformation = LTFArray.transform_id combiner = LTFArray.combiner_xor instances = [ LTFArray(weight_array=LTFArray.normal_weights( n=n, k=k, random_instance=RandomState(0xA1A1 + i)), transform=transformation, combiner=combiner) for i in range(instance_count) ] challenges = sample_inputs(n, N, random_instance=RandomState(0xFAB10)) property_test = PropertyTest(instances) reliability_statistic = property_test.reliability_statistic( challenges, measurements=measurements) # For an noiseless set of simulations the reliability must be 0% for key, value in reliability_statistic.items(): if key == 'sv': self.assertEqual(value, 0.0, '{}'.format(key)) elif key == 'samples': self.assertEqual(len(value), instance_count * N, '{}'.format(key)) else: self.assertEqual(value, 0.0, '{}'.format(key)) noisy_instances = [ NoisyLTFArray( weight_array=LTFArray.normal_weights( n=n, k=k, random_instance=RandomState(0xA1A1 + i)), transform=transformation, combiner=combiner, sigma_noise=0.5, random_instance=RandomState(0xCABE), ) for i in range(instance_count) ] noisy_property_test = PropertyTest(noisy_instances) noisy_reliability_statistic = noisy_property_test.reliability_statistic( challenges, measurements=measurements) self.assertNotEqual(noisy_reliability_statistic['mean'], 0.0)
def test_reliability_set(self): """This method tests the reliability_statistic calculation.""" n = 8 k = 3 N = 2**n measurements = 10 transformation = LTFArray.transform_id combiner = LTFArray.combiner_xor instances = [] instance_count = 3 for i in range(instance_count): instance = LTFArray( weight_array=LTFArray.normal_weights( n=n, k=k, random_instance=RandomState(0xA1A1 + i)), transform=transformation, combiner=combiner, ) instances.append(instance) challenges = sample_inputs(n, N, random_instance=RandomState(0xFAB0)) reliability_set = PropertyTest.reliability_set( instances, challenges, measurements=measurements) # The result is an array like with N * k entries. self.assertEqual(len(reliability_set), N * instance_count) # For noiseless simulations the all reliabilities must be 0% assert_array_equal(reliability_set, repeat(0.0, N * instance_count)) noisy_instances = [] for i in range(instance_count): noisy_instance = NoisyLTFArray( weight_array=NoisyLTFArray.normal_weights( n=n, k=k, random_instance=RandomState(0xA1A1 + i)), transform=transformation, combiner=combiner, sigma_noise=0.5, random_instance=RandomState(0x5015C + i), ) noisy_instances.append(noisy_instance) noisy_reliability_set = PropertyTest.reliability_set( noisy_instances, challenges, measurements=measurements) # For a noisy simulation the mean reliability must differ from zero self.assertNotEqual(mean(noisy_reliability_set), 0.0)
def test_uniqueness(self): """ This method tests the function which can be used to calculate the uniqueness of a set of simulation instances. """ n = 8 k = 1 N = 2**n instance_count = 50 transformation = LTFArray.transform_id combiner = LTFArray.combiner_xor instances = [ LTFArray(weight_array=LTFArray.normal_weights( n=n, k=k, random_instance=RandomState(0xA1A1 + i)), transform=transformation, combiner=combiner) for i in range(instance_count) ] challenges = sample_inputs(n, N, random_instance=RandomState(0xFAB10)) uniqueness = [] for challenge in challenges: uniqueness.append( PropertyTest.uniqueness(instances, reshape(challenge, (1, n)))) # For normal distributed weights is the expected uniqueness near 0.5 self.assertEqual(round(mean(uniqueness), 1), 0.5)
def example_reliability_statistic(): """This method shows hot to use the PropertyTest.reliability_statistic.""" n = 8 k = 1 N = 2**n instance_count = 3 measurements = 100 transformation = NoisyLTFArray.transform_id combiner = NoisyLTFArray.combiner_xor weights = NoisyLTFArray.normal_weights(n=n, k=k) instances = [ NoisyLTFArray( weight_array=weights, transform=transformation, combiner=combiner, sigma_noise=NoisyLTFArray.sigma_noise_from_random_weights(n, 0.5)) for _ in range(instance_count) ] challenges = array(list(sample_inputs(n, N))) property_test = PropertyTest(instances) reliability_statistic = property_test.reliability_statistic( challenges, measurements=measurements) print('The reliability statistic is {}.'.format(reliability_statistic))
def test_uniqueness_statistic(self): """This method tests the uniqueness statistic function.""" n = 8 k = 1 N = 2**n instance_count = 11 measurements = 1 transformation = LTFArray.transform_id combiner = LTFArray.combiner_xor instances = [ LTFArray(weight_array=LTFArray.normal_weights( n=n, k=k, random_instance=RandomState(0xA1A1 + i)), transform=transformation, combiner=combiner) for i in range(instance_count) ] challenges = sample_inputs(n, N, random_instance=RandomState(0xFAB10)) property_test = PropertyTest(instances) uniqueness_statistic = property_test.uniqueness_statistic( challenges, measurements=measurements) # For normal distributed weights is the expected uniqueness near 0.5 self.assertEqual(round(uniqueness_statistic['mean'], 1), 0.5)
def example_uniqueness_statistic(): """This method shows the uniqueness statistic function.""" n = 8 k = 1 N = 2**n instance_count = 11 measurements = 1 transformation = NoisyLTFArray.transform_id combiner = NoisyLTFArray.combiner_xor weights = NoisyLTFArray.normal_weights(n=n, k=k) instances = [ NoisyLTFArray( weight_array=weights, transform=transformation, combiner=combiner, sigma_noise=NoisyLTFArray.sigma_noise_from_random_weights( n, weights)) for _ in range(instance_count) ] challenges = array(list(sample_inputs(n, N))) property_test = PropertyTest(instances) uniqueness_statistic = property_test.uniqueness_statistic( challenges, measurements=measurements) print('The uniqueness statistic is {}.'.format(uniqueness_statistic))