|
| 1 | +""" |
| 2 | +Processes SOFAST data with debug mode enabled. |
| 3 | +
|
| 4 | +This module provides a function to process optical measurements using the SOFAST |
| 5 | +framework while enabling debug mode. The function performs the following tasks: |
| 6 | +loading necessary data files, calibrating fringe images, and processing the |
| 7 | +measurements. If an error occurs during processing, debug figures are saved for |
| 8 | +further analysis. |
| 9 | +
|
| 10 | +Usage: |
| 11 | + To execute the processing with debug mode, run the module directly. The output |
| 12 | + figures and log files will be saved in a specified directory structure. |
| 13 | +""" |
| 14 | + |
| 15 | +# ChatGPT 4o-mini assisted with docstring creation |
| 16 | + |
| 17 | +from os.path import join, dirname |
| 18 | + |
| 19 | +from opencsp.app.sofast.lib.DisplayShape import DisplayShape as Display |
| 20 | +from opencsp.app.sofast.lib.DefinitionFacet import DefinitionFacet |
| 21 | +from opencsp.app.sofast.lib.ImageCalibrationScaling import ImageCalibrationScaling |
| 22 | +from opencsp.app.sofast.lib.MeasurementSofastFringe import MeasurementSofastFringe |
| 23 | +from opencsp.app.sofast.lib.ProcessSofastFringe import ProcessSofastFringe as Sofast |
| 24 | +from opencsp.app.sofast.lib.SpatialOrientation import SpatialOrientation |
| 25 | +from opencsp.common.lib.camera.Camera import Camera |
| 26 | +from opencsp.common.lib.deflectometry.Surface2DParabolic import Surface2DParabolic |
| 27 | +from opencsp.common.lib.opencsp_path.opencsp_root_path import opencsp_code_dir |
| 28 | +import opencsp.common.lib.tool.file_tools as ft |
| 29 | +import opencsp.common.lib.tool.log_tools as lt |
| 30 | + |
| 31 | + |
| 32 | +def example_process_in_debug_mode(): |
| 33 | + """ |
| 34 | + Processes SOFAST data with debug mode enabled. This example intentionally runs SOFAST |
| 35 | + on a single facet with erroneous facet corner location information. When SOFAST is in |
| 36 | + debug mode, the cause of the most common errors can easily be diagnosed by looking at |
| 37 | + the produced figures from debug mode. Run this script as-is, the look through the output |
| 38 | + figures produced (location is printed in terminal) to see the cause of the error (the |
| 39 | + erroneous facet corner definition file). |
| 40 | +
|
| 41 | + This function performs the following steps: |
| 42 | +
|
| 43 | + 1. Sets up the necessary directories and logging for output. |
| 44 | + 2. Loads required data files, including camera, display, spatial orientation, |
| 45 | + measurement, calibration, and facet definition data. |
| 46 | + 3. Calibrates the fringe images from the measurement data. |
| 47 | + 4. Instantiates a SOFAST processing object and enables debug mode. |
| 48 | + 5. Processes the optical data for a single facet. If an error occurs during |
| 49 | + processing, all debug figures are saved to the specified output directory. |
| 50 | +
|
| 51 | + Notes |
| 52 | + ----- |
| 53 | + The function needs the following files to run: |
| 54 | +
|
| 55 | + - Measurement data (HDF5 format) |
| 56 | + - Camera calibration data (HDF5 format) |
| 57 | + - Display shape data (HDF5 format) |
| 58 | + - Spatial orientation data (HDF5 format) |
| 59 | + - Image calibration data (HDF5 format) |
| 60 | + - Facet definition data (JSON format) |
| 61 | +
|
| 62 | + Example |
| 63 | + ------- |
| 64 | + To process the SOFAST data with debug mode enabled, simply call the function: |
| 65 | +
|
| 66 | + >>> example_process_in_debug_mode() |
| 67 | +
|
| 68 | + The resulting debug figures will be saved in the output directory if an error |
| 69 | + occurs during processing. |
| 70 | + """ |
| 71 | + # 1. General setup |
| 72 | + # ================ |
| 73 | + |
| 74 | + # Define save dir |
| 75 | + dir_save = join(dirname(__file__), "data/output/sofast_with_debug_mode_on") |
| 76 | + ft.create_directories_if_necessary(dir_save) |
| 77 | + |
| 78 | + # Set up logger |
| 79 | + lt.logger(join(dir_save, "log.txt"), lt.log.INFO) |
| 80 | + |
| 81 | + # Define sample data directory |
| 82 | + dir_data_sofast = join(opencsp_code_dir(), "test/data/sofast_fringe") |
| 83 | + dir_data_common = join(opencsp_code_dir(), "test/data/sofast_common") |
| 84 | + |
| 85 | + # Directory Setup |
| 86 | + file_measurement = join(dir_data_sofast, "data_measurement/measurement_facet.h5") |
| 87 | + file_camera = join(dir_data_common, "camera_sofast_downsampled.h5") |
| 88 | + file_display = join(dir_data_common, "display_distorted_2d.h5") |
| 89 | + file_orientation = join(dir_data_common, "spatial_orientation.h5") |
| 90 | + file_calibration = join(dir_data_sofast, "data_measurement/image_calibration.h5") |
| 91 | + file_facet = join(dirname(__file__), "data/input/incorrect_facet_definition.json") |
| 92 | + |
| 93 | + # 2. Load saved single facet Sofast collection data |
| 94 | + # ================================================= |
| 95 | + camera = Camera.load_from_hdf(file_camera) |
| 96 | + display = Display.load_from_hdf(file_display) |
| 97 | + orientation = SpatialOrientation.load_from_hdf(file_orientation) |
| 98 | + measurement = MeasurementSofastFringe.load_from_hdf(file_measurement) |
| 99 | + calibration = ImageCalibrationScaling.load_from_hdf(file_calibration) |
| 100 | + facet_data = DefinitionFacet.load_from_json(file_facet) |
| 101 | + |
| 102 | + # 3. Processes data with SOFAST and handle errors |
| 103 | + # =============================================== |
| 104 | + # Define surface definition (parabolic surface), this is the mirror |
| 105 | + surface = Surface2DParabolic(initial_focal_lengths_xy=(300.0, 300.0), robust_least_squares=True, downsample=10) |
| 106 | + |
| 107 | + # Calibrate fringes - (aka sinosoidal image) |
| 108 | + measurement.calibrate_fringe_images(calibration) |
| 109 | + |
| 110 | + # Instantiate sofast object |
| 111 | + sofast = Sofast(measurement, orientation, camera, display) |
| 112 | + |
| 113 | + # Turn on debug mode |
| 114 | + sofast.params.debug_geometry.debug_active = True |
| 115 | + |
| 116 | + # Process |
| 117 | + try: |
| 118 | + sofast.process_optic_singlefacet(facet_data, surface) |
| 119 | + except ValueError: |
| 120 | + # Save all debug figures |
| 121 | + lt.info(f'An error occured when processing SOFAST data. Saving all debug figures to {dir_save}.') |
| 122 | + for idx, fig in enumerate(sofast.params.debug_geometry.figures): |
| 123 | + fig.savefig(join(dir_save, f'{idx:02d}.png')) |
| 124 | + |
| 125 | + |
| 126 | +if __name__ == "__main__": |
| 127 | + example_process_in_debug_mode() |
0 commit comments