def test_unknown_mode(): config = configs.EnvelopeServiceConfig() mocker = clients.MockClient() mocker.squeeze = False session_info = mocker.start_session(config) recorder = recording.Recorder(sensor_config=config, session_info=session_info) data_info, data = mocker.get_next() recorder.sample(data_info, data) recorder.close() record = recorder.record packed = recording.pack(record) assert "mode" in packed with pytest.warns(None) as captured_warnings: recording.unpack(packed) assert len(captured_warnings) == 0 with pytest.warns(UserWarning): packed["mode"] = "some_unknown_mode" unpacked = recording.unpack(packed) assert (unpacked.mode is None)
def main(): args = utils.ExampleArgumentParser().parse_args() utils.config_logging(args) filename = "data.h5" if os.path.exists(filename): print("File '{}' already exists, won't overwrite".format(filename)) sys.exit(1) if args.socket_addr: client = SocketClient(args.socket_addr) elif args.spi: client = SPIClient() else: port = args.serial_port or utils.autodetect_serial_port() client = UARTClient(port) config = configs.EnvelopeServiceConfig() config.sensor = args.sensors config.update_rate = 30 session_info = client.setup_session(config) recorder = recording.Recorder(sensor_config=config, session_info=session_info) client.start_session() n = 100 for i in range(n): data_info, data = client.get_next() recorder.sample(data_info, data) print("Sampled {:>4}/{}".format(i + 1, n), end="\r", flush=True) print() client.disconnect() record = recorder.close() os.makedirs(os.path.dirname(filename), exist_ok=True) recording.save(filename, record) print("Saved to '{}'".format(filename))
def test_recording(tmp_path, mode): config = configs.MODE_TO_CONFIG_CLASS_MAP[mode]() config.downsampling_factor = 2 mocker = clients.MockClient() mocker.squeeze = False session_info = mocker.start_session(config) recorder = recording.Recorder( sensor_config=config, session_info=session_info, ) for _ in range(10): data_info, data = mocker.get_next() recorder.sample(data_info, data) recorder.close() record = recorder.record assert record.mode == mode assert record.sensor_config_dump == config._dumps() assert len(record.data) == 10 assert len(record.data_info) == 10 assert isinstance(record.data, np.ndarray) for ext in ["h5", "npz"]: filename = os.path.join(tmp_path, "record." + ext) recording.save(filename, record) loaded_record = recording.load(filename) for a in attr.fields(recording.Record): if a.name == "data": continue assert getattr(record, a.name) == getattr(loaded_record, a.name) assert np.all(record.data == loaded_record.data) assert record.sensor_config.downsampling_factor == config.downsampling_factor
def test_recording(tmp_path, mode, ext, give_pathlib_path): config = configs.MODE_TO_CONFIG_CLASS_MAP[mode]() config.downsampling_factor = 2 mocker = clients.MockClient() mocker.squeeze = False session_info = mocker.start_session(config) recorder = recording.Recorder( sensor_config=config, session_info=session_info, ) for _ in range(10): data_info, data = mocker.get_next() recorder.sample(data_info, data) recorder.close() record = recorder.record assert record.mode == mode assert record.sensor_config_dump == config._dumps() assert len(record.data) == 10 assert len(record.data_info) == 10 assert isinstance(record.data, np.ndarray) filename = Path(tmp_path).joinpath("record." + ext) if not give_pathlib_path: filename = str(filename) recording.save(filename, record) loaded_record = recording.load(filename) for a in attr.fields(recording.Record): assert np.all( getattr(record, a.name) == getattr(loaded_record, a.name)) assert record.sensor_config.downsampling_factor == config.downsampling_factor
def main(): parser = utils.ExampleArgumentParser() parser.add_argument("-o", "--output-dir", type=str, required=True) parser.add_argument("--file-format", type=str, default="h5") parser.add_argument("--frames-per-file", type=int, default=10000) args = parser.parse_args() utils.config_logging(args) if os.path.exists(args.output_dir): print("Directory '{}' already exists, won't overwrite".format(args.output_dir)) sys.exit(1) file_format = args.file_format.lower() if file_format == "np": file_format = "npz" if file_format not in ["h5", "npz"]: print("Unknown format '{}'".format(args.file_format)) sys.exit(1) if args.frames_per_file < 10: print("Frames per file must be at least 10") sys.exit(1) if args.socket_addr: client = SocketClient(args.socket_addr) elif args.spi: client = SPIClient() else: port = args.serial_port or utils.autodetect_serial_port() client = UARTClient(port) config = configs.EnvelopeServiceConfig() config.sensor = args.sensors config.update_rate = 30 session_info = client.start_session(config) os.makedirs(args.output_dir) interrupt_handler = utils.ExampleInterruptHandler() print("Press Ctrl-C to end session") total_num_frames = 0 while not interrupt_handler.got_signal: record_count, num_frames_in_record = divmod(total_num_frames, args.frames_per_file) if num_frames_in_record == 0: recorder = recording.Recorder(sensor_config=config, session_info=session_info) data_info, data = client.get_next() recorder.sample(data_info, data) if num_frames_in_record + 1 == args.frames_per_file: record = recorder.close() filename = os.path.join( args.output_dir, "{:04}.{}".format(record_count + 1, file_format)) print("Saved", filename) recording.save(filename, record) total_num_frames += 1 print("Sampled {:>5}".format(total_num_frames), end="\r", flush=True) try: client.disconnect() except Exception: pass record_count, num_frames_in_record = divmod(total_num_frames, args.frames_per_file) if num_frames_in_record > 0: record = recorder.close() filename = os.path.join( args.output_dir, "{:04}.{}".format(record_count + 1, file_format)) print("Saved", filename) recording.save(filename, record)
def main(): parser = utils.ExampleArgumentParser() parser.add_argument("-o", "--output-file", type=str, required=True) parser.add_argument("-l", "--limit-frames", type=int) args = parser.parse_args() utils.config_logging(args) if os.path.exists(args.output_file): print("File '{}' already exists, won't overwrite".format( args.output_file)) sys.exit(1) _, ext = os.path.splitext(args.output_file) if ext.lower() not in [".h5", ".npz"]: print("Unknown format '{}'".format(ext)) sys.exit(1) if args.limit_frames is not None and args.limit_frames < 1: print("Frame limit must be at least 1") sys.exit(1) if args.socket_addr: client = SocketClient(args.socket_addr) elif args.spi: client = SPIClient() else: port = args.serial_port or utils.autodetect_serial_port() client = UARTClient(port) config = configs.EnvelopeServiceConfig() config.sensor = args.sensors config.update_rate = 30 session_info = client.setup_session(config) recorder = recording.Recorder(sensor_config=config, session_info=session_info) client.start_session() interrupt_handler = utils.ExampleInterruptHandler() print("Press Ctrl-C to end session") i = 0 while not interrupt_handler.got_signal: data_info, data = client.get_next() recorder.sample(data_info, data) i += 1 if args.limit_frames: print("Sampled {:>4}/{}".format(i, args.limit_frames), end="\r", flush=True) if i >= args.limit_frames: break else: print("Sampled {:>4}".format(i), end="\r", flush=True) print() client.disconnect() record = recorder.close() os.makedirs(os.path.dirname(os.path.abspath(args.output_file)), exist_ok=True) recording.save(args.output_file, record) print("Saved to '{}'".format(args.output_file))
def main(): parser = utils.ExampleArgumentParser() parser.add_argument("-t", "--temp", type=str, required=True) parser.add_argument("-l", "--label", type=str, required=False) # Sätt till true sedan parser.add_argument("-o", "--output-file", type=str, required=True) parser.add_argument("-lim", "--limit-frames", type=int) #parser.add_argument("-a", "--angel", type=str, required=False) #parser.add_argument("-d", "--distance", type=str, required=True) args = parser.parse_args() utils.config_logging(args) try: float(args.temp) except: print("Temp value not a float. Make sure you use . instead of , !") sys.exit(1) valid_arguments = ["snow", "wet", "ice", "dry", "metal"] if args.label.lower() not in valid_arguments: print("Not a valid label. Only", *valid_arguments, sep=", "), print("are accepted labels!") sys.exit(1) if os.path.exists(args.output_file): print("File '{}' already exists, won't overwrite".format( args.output_file)) sys.exit(1) _, ext = os.path.splitext(args.output_file) if ext.lower() not in [".h5", ".npz"]: print("Unknown format '{}'".format(ext)) sys.exit(1) if args.limit_frames is not None and args.limit_frames < 1: print("Frame limit must be at least 1") sys.exit(1) if args.socket_addr: client = SocketClient(args.socket_addr) elif args.spi: client = SPIClient() else: port = args.serial_port or utils.autodetect_serial_port() client = UARTClient(port) config = configs.IQServiceConfig() config.sensor = args.sensors config.update_rate = 650 #Ändra samplingsfrekvens här config.range_interval = [0.06, 0.5] #Avståndsintervall i meter config.profile = config.Profile.PROFILE_1 config.repetition_mode = config.RepetitionMode.SENSOR_DRIVEN session_info = client.setup_session(config) recorder = recording.Recorder(sensor_config=config, session_info=session_info, temp=args.temp, label=args.label.lower()) #angel=args.angel, #distance=args.distance client.start_session() interrupt_handler = utils.ExampleInterruptHandler() print("Press Ctrl-C to end session") i = 0 while not interrupt_handler.got_signal: data_info, data = client.get_next() recorder.sample(data_info, data) i += 1 if args.limit_frames: print("Sampled {:>4}/{}".format(i, args.limit_frames), end="\r", flush=True) if i >= args.limit_frames: break else: print("Sampled {:>4}".format(i), end="\r", flush=True) client.disconnect() record = recorder.close() recording.save(args.output_file, record) print("Saved to '{}'".format(args.output_file))