examples/a121/post_process_sparse_iq.py

examples/a121/post_process_sparse_iq.py#

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

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