def test_output_filename_correct(self, filename): """ Tests that the output filename is correct. """ create_random_data(filename, seconds=1., sample_rate=10.) self.assertTrue(os.path.exists(filename))
def test_noise(self, filename): """ Tests that the noise is added correctly(its mean and std_dev are correct). """ seconds = 1. sample_rate = 1.e6 baseline = 1. noise_loc = 1. noise_scale = 0.6 noise = stats.norm(loc=noise_loc, scale=noise_scale) create_random_data(filename, seconds=seconds, sample_rate=sample_rate, baseline=baseline, noise=noise) reader = get_reader_from_filename(filename) data_all = reader.get_all_data() data = data_all[0] mean = np.mean(data) mean_should_be = baseline + noise_loc self.assertAlmostEqual( mean, mean_should_be, 1, "Unexpected mean. Wanted {0}, got {1}.".format( mean_should_be, mean)) std_dev = np.std(data) self.assertAlmostEqual( noise_scale, std_dev, 1, "Unexpected standard deviation. " "Wanted {0}, got {1}".format(noise_scale, std_dev)) reader.close()
def test_noise(self, filename): """ Tests that the noise is added correctly(its mean and std_dev are correct). """ seconds = 1. sample_rate = 1.e6 baseline = 1. noise_loc = 1. noise_scale = 0.6 noise = stats.norm(loc=noise_loc, scale=noise_scale) create_random_data(filename, seconds=seconds, sample_rate=sample_rate, baseline=baseline, noise=noise) reader = get_reader_from_filename(filename) data_all = reader.get_all_data() data = data_all[0] mean = np.mean(data) mean_should_be = baseline + noise_loc self.assertAlmostEqual(mean, mean_should_be, 1, "Unexpected mean. Wanted {0}, got {1}.".format(mean_should_be, mean)) std_dev = np.std(data) self.assertAlmostEqual(noise_scale, std_dev, 1, "Unexpected standard deviation. " "Wanted {0}, got {1}".format(noise_scale, std_dev)) reader.close()
def test_opening_existing_with_overwrite(self, filename): """ Tests that we can overwrite an existing file with overwrite=True """ # create a blank file f = open(filename, mode='w') f.close() seconds = 1. sample_rate = 1. baseline = 1. create_random_data(filename, seconds=seconds, sample_rate=sample_rate, baseline=baseline, overwrite=True) self.assertTrue(os.path.exists(filename))
def _test_number_of_events_help(name): # Need to seed, otherwise each process will generate the same random data! np.random.seed() # Check that setting the event rate gives correct number of events. seconds = 5. sample_rate = 1.e6 baseline = 1. event_rate = 50. event_duration = stats.norm(loc=100.e-6, scale=5.e-6) event_depth = stats.norm(loc=-1., scale=.05) noise = stats.norm(scale=0.02) # create a list of file_names so we can average the number of events. if os.path.exists(name): os.remove(name) n_e_r = create_random_data(filename=name, seconds=seconds, sample_rate=sample_rate, baseline=baseline, noise=noise, event_rate=event_rate, event_durations=event_duration, event_depths=event_depth) save_file_name = name[:-len('.h5')] + '_Events.h5' if os.path.exists(save_file_name): os.remove(save_file_name) event_database = find_events([name], save_file_names=[save_file_name])[0] ed = EventDatabase(event_database) n_events_found = ed.get_event_count() os.remove(name) ed.close() os.remove(event_database) return n_e_r, n_events_found, name
def test_baseline_no_noise_no_events(self, filename): """ Tests that with only the baseline, we create the right data. """ seconds = 1. sample_rate = 1.e6 baseline = 1. event_rate = 0 event_duration = stats.norm(loc=100.e-6, scale=10.e-6) event_depth = stats.norm(loc=.5, scale=.05) data_should_be = np.zeros(int(seconds * sample_rate)) + baseline create_random_data(filename=filename, seconds=seconds, sample_rate=sample_rate, baseline=baseline, event_rate=event_rate, event_durations=event_duration, event_depths=event_depth) _test_params_equality(self, filename, data_should_be, sample_rate)
def test_event_params(self, filename): """ Tests that setting the event depth and duration give correct values. """ seconds = 5. sample_rate = 1.e6 baseline = 10. event_rate = 100. event_duration_loc = 1.e-4 event_duration_scale = 5.e-6 event_duration = stats.norm(loc=event_duration_loc, scale=event_duration_scale) event_depth_loc = -1. event_depth_scale = .05 event_depth = stats.norm(loc=-1., scale=.05) noise = stats.norm(scale=0.01) n_events_returned = create_random_data(filename, seconds=seconds, sample_rate=sample_rate, baseline=baseline, event_rate=event_rate, event_durations=event_duration, event_depths=event_depth, noise=noise) save_file_name = filename[:-len('.h5')] + 'Events.h5' if os.path.exists(save_file_name): os.remove(save_file_name) event_database = find_events([filename], save_file_names=[save_file_name])[0] ed = EventDatabase(event_database) count = ed.get_event_count() count_should_be = event_rate * seconds diff = abs(count - count_should_be) self.assertLessEqual(diff, 100, "Unexpected number of events. " "Expected {0}, was {1}.".format(count_should_be, count)) table_sample_rate = ed.get_sample_rate() durations = [x['event_length'] / table_sample_rate for x in ed.get_event_table().iterrows()] depths = [x['current_blockage'] for x in ed.get_event_table().iterrows()] mean_duration = np.mean(durations) self.assertAlmostEqual(event_duration_loc, mean_duration, 5, "Unexpected mean event duration. " "Wanted {0}, got {1}.".format(event_duration_loc, mean_duration)) mean_depth = np.mean(depths) self.assertAlmostEqual(event_depth_loc, mean_depth, 1, "Unexpected mean event depth. " "Wanted {0}, got {1}.".format(event_depth_loc, mean_depth)) ed.close() os.remove(event_database)
def test_event_params(self, filename): """ Tests that setting the event depth and duration give correct values. """ seconds = 5. sample_rate = 1.e6 baseline = 10. event_rate = 100. event_duration_loc = 1.e-4 event_duration_scale = 5.e-6 event_duration = stats.norm(loc=event_duration_loc, scale=event_duration_scale) event_depth_loc = -1. event_depth_scale = .05 event_depth = stats.norm(loc=-1., scale=.05) noise = stats.norm(scale=0.01) n_events_returned = create_random_data(filename, seconds=seconds, sample_rate=sample_rate, baseline=baseline, event_rate=event_rate, event_durations=event_duration, event_depths=event_depth, noise=noise) save_file_name = filename[:-len('.h5')] + 'Events.h5' if os.path.exists(save_file_name): os.remove(save_file_name) event_database = find_events([filename], save_file_names=[save_file_name])[0] ed = EventDatabase(event_database) count = ed.get_event_count() count_should_be = event_rate * seconds diff = abs(count - count_should_be) self.assertLessEqual( diff, 100, "Unexpected number of events. " "Expected {0}, was {1}.".format(count_should_be, count)) table_sample_rate = ed.get_sample_rate() durations = [ x['event_length'] / table_sample_rate for x in ed.get_event_table().iterrows() ] depths = [ x['current_blockage'] for x in ed.get_event_table().iterrows() ] mean_duration = np.mean(durations) self.assertAlmostEqual( event_duration_loc, mean_duration, 5, "Unexpected mean event duration. " "Wanted {0}, got {1}.".format(event_duration_loc, mean_duration)) mean_depth = np.mean(depths) self.assertAlmostEqual( event_depth_loc, mean_depth, 1, "Unexpected mean event depth. " "Wanted {0}, got {1}.".format(event_depth_loc, mean_depth)) ed.close() os.remove(event_database)