examples/a121/post_process_sparse_iq.py

examples/a121/post_process_sparse_iq.py#

  1# Copyright (c) Acconeer AB, 2023-2024
  2# All rights reserved
  3
  4"""
  5This example will demonstrate how one can post process already recorded SparseIQ data
  6with distance or presence (the other algorithms are similar).
  7
  8Note that some algorithms requires the data to have been recorded with specific settings.
  9This means that all recorded data cannot be processed by all algorithms.
 10
 11Usage:
 12
 13    python path/to/post_process_sparse_iq.py <path/to/h5 file> --processing distance
 14"""
 15
 16import argparse
 17import pathlib
 18
 19from acconeer.exptool import a121
 20from acconeer.exptool.a121.algo import distance, presence
 21
 22
 23def presence_processing(record: a121.H5Record) -> None:
 24    # User-defined post-processing parameters.
 25    # See these Presence resources for more information:
 26    #
 27    # Presence processor example:
 28    #   https://github.com/acconeer/acconeer-python-exploration/blob/master/examples/a121/algo/presence/processor.py
 29    # Presence doc page:
 30    #   https://docs.acconeer.com/en/latest/exploration_tool/algo/a121/detectors/presence_detection.html
 31    processor_config = presence.ProcessorConfig(intra_enable=True)
 32
 33    # Get needed metadata of recorded data from the H5Record
 34    # If your record contains "extended" data, an error will be raised here.
 35    record_sensor_config = record.session_config.sensor_config
 36    metadata = record.metadata
 37
 38    # Each algorithm has requirements on the data it processes. validate checks these requirements
 39    processor_config.validate(record_sensor_config)
 40
 41    # Create a processor with metadata from recording and the user-defined processing parameters
 42    processor = presence.Processor(
 43        sensor_config=record_sensor_config,
 44        metadata=metadata,
 45        processor_config=processor_config,
 46    )
 47
 48    # Post-process all recorded results and print the intra presence score
 49    for idx, result in enumerate(record.results):
 50        processor_result = processor.process(result)
 51        print(f"{idx: >5}: {processor_result.intra_presence_score}")
 52
 53
 54def distance_processing(record: a121.H5Record) -> None:
 55    # User-defined post-processing parameters.
 56    # See these Distance resources for more information:
 57    #
 58    # Distance processor example:
 59    #   https://github.com/acconeer/acconeer-python-exploration/blob/master/examples/a121/algo/distance/processor.py
 60    # Distance doc page:
 61    #   https://docs.acconeer.com/en/latest/exploration_tool/algo/a121/detectors/distance_detection.html
 62    processor_config = distance.ProcessorConfig(
 63        processor_mode=distance.ProcessorMode.DISTANCE_ESTIMATION,
 64        threshold_method=distance.ThresholdMethod.CFAR,
 65        measurement_type=distance.MeasurementType.FAR_RANGE,
 66        reflector_shape=distance.ReflectorShape.GENERIC,
 67        threshold_sensitivity=0.5,
 68        fixed_threshold_value=100.0,
 69        fixed_strength_threshold_value=0.0,
 70    )
 71
 72    # Get needed metadata of recorded data from the H5Record.
 73    # If your record contains "extended" data, an error will be raised here.
 74    record_sensor_config = record.session_config.sensor_config
 75    metadata = record.metadata
 76
 77    # Each algorithm has requirements on the data it processes. validate checks these requirements
 78    processor_config.validate(record_sensor_config)
 79
 80    # Create a processor with metadata from recording and the user-defined processing parameters
 81    all_subsweep_indexes = list(range(record_sensor_config.num_subsweeps))
 82    processor = distance.Processor(
 83        sensor_config=record_sensor_config,
 84        metadata=metadata,
 85        processor_config=processor_config,
 86        subsweep_indexes=all_subsweep_indexes,
 87    )
 88
 89    # Post-process all recorded results and print the estimated distances
 90    for idx, result in enumerate(record.results):
 91        processor_result = processor.process(result)
 92        print(f"{idx: >5}: {processor_result.estimated_distances}")
 93
 94
 95def main() -> None:
 96    parser = argparse.ArgumentParser()
 97    parser.add_argument("file", type=pathlib.Path, help="Path to the recorded .h5 file")
 98    parser.add_argument(
 99        "--processing",
100        type=str,
101        choices=["distance", "presence"],
102        help="The post-processing to use",
103    )
104
105    args = parser.parse_args()
106
107    with a121.open_record(args.file) as record:
108        if args.processing == "distance":
109            distance_processing(record)
110        elif args.processing == "presence":
111            presence_processing(record)
112        else:
113            print(f"No post-processing available for algorithm {args.processing!r}")
114
115
116if __name__ == "__main__":
117    main()

View this example on GitHub: acconeer/acconeer-python-exploration