def package_generator(dtimes: List[dt.datetime], params_list: List[iisr_io.SeriesParameters]): for dtime in dtimes: series_list = [] cur_freq = None for params in params_list: if cur_freq is not None and cur_freq != params.frequency: yield iisr_io.TimeSeriesPackage(dtime, series_list) series_list = [] cur_freq = params.frequency series_list.append( get_test_signal_time_series(dtime, test_params=params)) yield iisr_io.TimeSeriesPackage(dtime, series_list)
def test_incorrect_series(self): test_time_series1 = get_test_signal_time_series() test_time_series2 = get_test_signal_time_series() test_time_series2.time_mark = datetime(2016, 3, 4, 6, 2, 55, 3123) with self.assertRaises(ValueError): iisr_io.TimeSeriesPackage(test_time_series1.time_mark, [test_time_series1, test_time_series2])
def _run_read(self, hours_shift, fix_time_bug): with tempfile.TemporaryDirectory() as dirname: test_file_path = Path(dirname) / DUMMY_FILE_NAME # Create test options n_samples = 64 test_parameters = [ get_test_parameters(channel=ch, n_samples=n_samples) for ch in self.channels ] minutes_shift = random.randint(-5, 5) # +-5 minutes maximum time_marks = [ DEFAULT_DATETIME + timedelta(seconds=10 * i) for i in range(self.n_marks) ] for i in range(self.shift_start_idx, self.shift_stop_idx): time_marks[i] += timedelta(hours=hours_shift, minutes=minutes_shift) test_quad_i = np.random.randint(-2**15 + 1, 2**15, n_samples) test_quad_q = np.random.randint(-2**15 + 1, 2**15, n_samples) test_quadratures = test_quad_i + 1j * test_quad_q test_series = [] test_packages = [] for time_mark in time_marks: tm_series = [] for params in test_parameters: series = iisr_io.TimeSeries(time_mark, params, test_quadratures) test_series.append(series) tm_series.append(series) test_packages.append( iisr_io.TimeSeriesPackage(time_mark, tm_series)) with iisr_io.open_data_file(test_file_path, 'w') as writer: for package in test_packages: writer.write(package) with open(str(test_file_path), 'rb') as file: reader = iisr_io.DataFileReader(file, file_info=get_file_info(), fix_time_lag_bug=fix_time_bug) read_series = list(reader.read_series()) self.assertEqual(self.n_marks * len(self.channels), len(read_series)) return test_series, read_series
def test_init(self): test_time_series1 = get_test_signal_time_series() test_time_series2 = get_test_signal_time_series() package = iisr_io.TimeSeriesPackage( test_time_series1.time_mark, [test_time_series1, test_time_series2]) for series, test_series in zip(package, [test_time_series1, test_time_series2]): self.assertEqual(series, test_series) self.assertEqual(package.time_series_list[0], test_time_series1) self.assertEqual(package.time_series_list[1], test_time_series2) self.assertEqual(package.time_mark, test_time_series2.time_mark)
def make_random_test_file(n_unique_series=2, n_time_marks=2, time_step_sec=1): """Create test_file with random series""" def gen_unique_parameters(): freqs = [154., 155.5, 156.8, 158., 159.5] random.shuffle(freqs) channels = [0, 1, 2, 3] random.shuffle(channels) pulse_len = [200, 500, 700, 900] random.shuffle(pulse_len) phase_code = [0, 5] random.shuffle(phase_code) # Channels must change first, as in original data for code, length, freq, ch in it.product(phase_code, pulse_len, freqs, channels): yield get_test_parameters(freq=freq, channel=ch, pulse_len=length, phase_code=code) generator = gen_unique_parameters() parameter_sets = [] for i in range(n_unique_series): parameter_sets.append(generator.__next__()) packages_list = [] all_series_list = [] for i in range(n_time_marks): time_mark = DEFAULT_DATETIME + timedelta(seconds=time_step_sec * i) series_list = [] for parameters in parameter_sets: n_samples = parameters.n_samples quad_i = np.random.randint(-2**15 + 1, 2**15, n_samples) quad_q = np.random.randint(-2**15 + 1, 2**15, n_samples) quadratures = quad_i + 1j * quad_q series = iisr_io.TimeSeries(time_mark, parameters, quadratures) series_list.append(series) all_series_list.append(series) packages_list.append(iisr_io.TimeSeriesPackage(time_mark, series_list)) with tempfile.TemporaryDirectory() as temp_dirname: file_path = Path(temp_dirname) / DUMMY_FILE_NAME with iisr_io.open_data_file( file_path, 'w') as writer: # type: iisr_io.DataFileWriter for package in packages_list: writer.write(package) yield file_path, all_series_list
def test_read_with_selector(self): valid_params = { 'frequencies': [Frequency(155.5, 'MHz')], 'channels': [Channel(0), Channel(2)], 'pulse_lengths': [TimeUnit(200, 'us')], 'pulse_types': ['long'], } filter_ = iisr_io.SeriesSelector(**valid_params) test_parameters = [] for fr, ch, len_ in it.product([155.5, 159.5], [0, 2], [200, 700]): test_parameters.append( get_test_parameters(freq=fr, pulse_len=len_, channel=ch)) with tempfile.TemporaryDirectory() as dirname: test_filepath = Path(dirname) / DUMMY_FILE_NAME with iisr_io.open_data_file(test_filepath, 'w') as data_file: for param in test_parameters: n_samples = param.n_samples test_quad_i = np.random.randint(-2**15 + 1, 2**15, n_samples) test_quad_q = np.random.randint(-2**15 + 1, 2**15, n_samples) test_quadratures = test_quad_i + 1j * test_quad_q series = iisr_io.TimeSeries(DEFAULT_DATETIME, param, test_quadratures) package = iisr_io.TimeSeriesPackage( DEFAULT_DATETIME, [series]) data_file.write(package) # Check if selector correct with iisr_io.open_data_file(test_filepath, series_selector=filter_) as reader: series_list = list(reader) self.assertEqual(len(series_list), 2) for series in series_list: self.assertEqual(series.parameters.frequency, valid_params['frequencies'][0]) self.assertIn(series.parameters.channel, valid_params['channels']) self.assertEqual(series.parameters.pulse_length, valid_params['pulse_lengths'][0]) self.assertEqual(series.parameters.pulse_type, valid_params['pulse_types'][0])
def test_write_read_reciprocity(self): with tempfile.TemporaryDirectory() as dirname: test_file_path = Path(dirname) / DUMMY_FILE_NAME # Create test options test_parameters = get_test_parameters() time_mark = DEFAULT_DATETIME n_samples = test_parameters.n_samples test_quad_i = np.random.randint(-2**15 + 1, 2**15, n_samples) test_quad_q = np.random.randint(-2**15 + 1, 2**15, n_samples) test_quadratures = test_quad_i + 1j * test_quad_q test_series = iisr_io.TimeSeries(time_mark, test_parameters, test_quadratures) test_package = iisr_io.TimeSeriesPackage(time_mark, [test_series]) with iisr_io.open_data_file(test_file_path, 'w') as writer: writer.write(test_package) with iisr_io.open_data_file(test_file_path, 'r') as reader: series = next(reader.read_series()) # Time self.assertEqual(time_mark, series.time_mark) # Quadratures self.assertEqual(len(test_quadratures), len(series.quadratures)) np.testing.assert_equal(test_quadratures, series.quadratures) # Check test options against read options self.assertEqual(test_parameters.n_samples, series.parameters.n_samples) self.assertEqual(test_parameters.frequency, series.parameters.frequency) self.assertEqual(test_parameters.pulse_type, series.parameters.pulse_type) self.assertEqual(test_parameters.pulse_length, series.parameters.pulse_length) self.assertEqual(test_parameters.sampling_frequency, series.parameters.sampling_frequency) self.assertEqual(test_parameters.channel, series.parameters.channel) self.assertEqual(test_parameters.phase_code, series.parameters.phase_code) self.assertEqual(test_parameters.total_delay, series.parameters.total_delay)
def test_empty(self): with self.assertRaises(ValueError): iisr_io.TimeSeriesPackage(datetime(2000, 1, 1), [])