tigrulya-exe / terraform

QGIS plugin with popular topographic correction algorithms and various methods of their evaluation
https://plugins.qgis.org/plugins/Terraform/
GNU General Public License v3.0
4 stars 2 forks source link

Rank TOC algorithms by multi-criteria score fails in metrics normalization #2

Closed twest820 closed 5 months ago

twest820 commented 10 months ago

Issue around how max() gets called. Looks like this code path is always hit, even if only one metric's selected.

QGIS version: 3.28.11-Firenze
QGIS code revision: b02458a196
Qt version: 5.15.3
Python version: 3.9.5
GDAL version: 3.7.2
GEOS version: 3.12.0-CAPI-1.18.0
PROJ version: Rel. 9.3.0, September 1st, 2023
PDAL version: 2.5.5 (git-version: 24f78d)
Algorithm started at: 2024-01-31T16:24:07
Algorithm 'Rank TOC algorithms by multi-criteria score' starting…
Input parameters:
{ 'CLASSIFICATION_MAP' : None, 'CORRECTIONS_OUTPUT_DIR' : 'TEMPORARY_OUTPUT', 'DEM' : '%DATAPATH%/DTM/s03960w06810.tif', 'INPUT' : '%DATAPATH%/orthoimage/s03960w06810.tif', 'METRICS' : ['Mean reflectance reduction',1,'Coefficient of variation reduction',1,'Inter quartile range reduction',1,'Relative median difference',1,'Number of outliers (static thresholds)',1,'Number of outliers - elements out of the [Q1 - IQR; Q3 + IQR] interval',1,'Determination coefficient of the band values and solar incidence angle',1], 'METRIC_MERGE_STRATEGY' : 'sum', 'OUTPUT_DIR' : 'TEMPORARY_OUTPUT', 'RUN_PARALLEL' : False, 'SOLAR_AZIMUTH' : 241, 'SZA' : 37, 'TASK_TIMEOUT' : 10000, 'TOPO_CORRECTION_ALGORITHMS' : [0,1,2,3,4,5,6,7,8,9,10], 'WORKER_COUNT' : 32 }

Starting topographic correction.
[COSINE-T]: initializing.
[COSINE-T]: starting per-band correction.
[QGIS processing]: calculating slope.
[QGIS processing]: calculating aspect.
[QGIS processing]: calculating luminance.
[COSINE-T]: band 0 was processed in 1187.5 ms
[COSINE-T]: band 1 was processed in 640.625 ms
[COSINE-T]: band 2 was processed in 625.0 ms
[COSINE-T]: band 3 was processed in 625.0 ms
[COSINE-T]: merging corrected bands.
[QGIS processing]: merging bands.
[COSINE-T]: finished.
[COSINE-C]: initializing.
[COSINE-C]: starting per-band correction.
[COSINE-C]: band 0 was processed in 671.875 ms
[COSINE-C]: band 1 was processed in 640.625 ms
[COSINE-C]: band 2 was processed in 640.625 ms
[COSINE-C]: band 3 was processed in 656.25 ms
[COSINE-C]: merging corrected bands.
[QGIS processing]: merging bands.
[COSINE-C]: finished.
[C-correction]: initializing.
[C-correction]: starting per-band correction.
[C-correction]: band 0 was processed in 1187.5 ms
[C-correction]: band 1 was processed in 1156.25 ms
[C-correction]: band 2 was processed in 1171.875 ms
[C-correction]: band 3 was processed in 1171.875 ms
[C-correction]: merging corrected bands.
[QGIS processing]: merging bands.
[C-correction]: finished.
[SCS]: initializing.
[SCS]: starting per-band correction.
[SCS]: band 0 was processed in 750.0 ms
[SCS]: band 1 was processed in 750.0 ms
[SCS]: band 2 was processed in 781.25 ms
[SCS]: band 3 was processed in 750.0 ms
[SCS]: merging corrected bands.
[QGIS processing]: merging bands.
[SCS]: finished.
[SCS+C]: initializing.
[SCS+C]: starting per-band correction.
[SCS+C]: band 0 was processed in 1328.125 ms
[SCS+C]: band 1 was processed in 1265.625 ms
[SCS+C]: band 2 was processed in 1296.875 ms
[SCS+C]: band 3 was processed in 1281.25 ms
[SCS+C]: merging corrected bands.
[QGIS processing]: merging bands.
[SCS+C]: finished.
[Minnaert]: initializing.
[Minnaert]: starting per-band correction.
[Minnaert]: band 0 was processed in 1593.75 ms
[Minnaert]: band 1 was processed in 1609.375 ms
[Minnaert]: band 2 was processed in 1531.25 ms
[Minnaert]: band 3 was processed in 1609.375 ms
[Minnaert]: merging corrected bands.
[QGIS processing]: merging bands.
[Minnaert]: finished.
[Minnaert-SCS]: initializing.
[Minnaert-SCS]: starting per-band correction.
[Minnaert-SCS]: band 0 was processed in 1703.125 ms
[Minnaert-SCS]: band 1 was processed in 1703.125 ms
[Minnaert-SCS]: band 2 was processed in 1671.875 ms
[Minnaert-SCS]: band 3 was processed in 1718.75 ms
[Minnaert-SCS]: merging corrected bands.
[QGIS processing]: merging bands.
[Minnaert-SCS]: finished.
[Pixel based Minnaert]: initializing.
[Pixel based Minnaert]: starting per-band correction.
[Pixel based Minnaert]: band 0 was processed in 1625.0 ms
[Pixel based Minnaert]: band 1 was processed in 1718.75 ms
[Pixel based Minnaert]: band 2 was processed in 1671.875 ms
[Pixel based Minnaert]: band 3 was processed in 1671.875 ms
[Pixel based Minnaert]: merging corrected bands.
[QGIS processing]: merging bands.
[Pixel based Minnaert]: finished.
[VECA]: initializing.
[VECA]: starting per-band correction.
[VECA]: band 0 was processed in 1187.5 ms
[VECA]: band 1 was processed in 1203.125 ms
[VECA]: band 2 was processed in 1203.125 ms
[VECA]: band 3 was processed in 1156.25 ms
[VECA]: merging corrected bands.
[QGIS processing]: merging bands.
[VECA]: finished.
[Teillet regression]: initializing.
[Teillet regression]: starting per-band correction.
[Teillet regression]: band 0 was processed in 1187.5 ms
[Teillet regression]: band 1 was processed in 1187.5 ms
[Teillet regression]: band 2 was processed in 1187.5 ms
[Teillet regression]: band 3 was processed in 1140.625 ms
[Teillet regression]: merging corrected bands.
[QGIS processing]: merging bands.
[Teillet regression]: finished.
[Pixel based C-correction]: initializing.
[Pixel based C-correction]: starting per-band correction.
[Pixel based C-correction]: band 0 was processed in 1640.625 ms
[Pixel based C-correction]: band 1 was processed in 1656.25 ms
[Pixel based C-correction]: band 2 was processed in 1671.875 ms
[Pixel based C-correction]: band 3 was processed in 1640.625 ms
[Pixel based C-correction]: merging corrected bands.
[QGIS processing]: merging bands.
[Pixel based C-correction]: finished.
Start evaluating group 1.
[QGIS processing]: calculating slope.
[QGIS processing]: calculating aspect.
[QGIS processing]: calculating luminance.
Computing metrics for original image.
Computing metrics for COSINE-T.
Computing metrics for COSINE-C.
Computing metrics for C-correction.
Computing metrics for SCS.
Computing metrics for SCS+C.
Computing metrics for Minnaert.
Computing metrics for Minnaert-SCS.
Computing metrics for Pixel based Minnaert.
Computing metrics for VECA.
Computing metrics for Teillet regression.
Computing metrics for Pixel based C-correction.
Normalizing metrics.
Error during evaluation: Traceback (most recent call last):
File "%APPDATA%/QGIS/QGIS3\profiles\default/python/plugins\Terraform\processing_alg\topocorrection_eval\eval.py", line 149, in evaluate
result += self._evaluate_group(group)
File "%APPDATA%/QGIS/QGIS3\profiles\default/python/plugins\Terraform\processing_alg\topocorrection_eval\multi_criteria_rank.py", line 108, in _evaluate_group
metrics_per_correction_band_df, normalized_metrics = self.merge_strategy.merge(group_df.copy(), group_idx)
File "%APPDATA%/QGIS/QGIS3\profiles\default/python/plugins\Terraform\processing_alg\topocorrection_eval\multi_criteria_rank.py", line 177, in merge
normalized_metrics: DataFrame = self._normalize(metrics, orig_metrics)
File "%APPDATA%/QGIS/QGIS3\profiles\default/python/plugins\Terraform\processing_alg\topocorrection_eval\multi_criteria_rank.py", line 182, in _normalize
norm_good_results = self._normalize_single(good_results, metrics_min=orig_metrics)
File "%APPDATA%/QGIS/QGIS3\profiles\default/python/plugins\Terraform\processing_alg\topocorrection_eval\multi_criteria_rank.py", line 196, in _normalize_single
metrics_max = metrics.max(level=1)
File "C:\PROGRA~1\QGIS32~1.11\apps\Python39\lib\site-packages\pandas\core\generic.py", line 11646, in max
return NDFrame.max(self, axis, skipna, numeric_only, **kwargs)
File "C:\PROGRA~1\QGIS32~1.11\apps\Python39\lib\site-packages\pandas\core\generic.py", line 11185, in max
return self._stat_function(
File "C:\PROGRA~1\QGIS32~1.11\apps\Python39\lib\site-packages\pandas\core\generic.py", line 11154, in _stat_function
nv.validate_stat_func((), kwargs, fname=name)
File "C:\PROGRA~1\QGIS32~1.11\apps\Python39\lib\site-packages\pandas\compat\numpy\function.py", line 80, in __call__
validate_kwargs(fname, kwargs, self.defaults)
File "C:\PROGRA~1\QGIS32~1.11\apps\Python39\lib\site-packages\pandas\util\_validators.py", line 162, in validate_kwargs
_check_for_invalid_keys(fname, kwargs, compat_args)
File "C:\PROGRA~1\QGIS32~1.11\apps\Python39\lib\site-packages\pandas\util\_validators.py", line 136, in _check_for_invalid_keys
raise TypeError(f"{fname}() got an unexpected keyword argument '{bad_arg}'")
TypeError: max() got an unexpected keyword argument 'level'

Traceback (most recent call last):
File "%APPDATA%/QGIS/QGIS3\profiles\default/python/plugins\Terraform\processing_alg\topocorrection_eval\qgis_algorithm.py", line 97, in processAlgorithm
result = self._process_internal(parameters, execution_ctx, feedback)
File "%APPDATA%/QGIS/QGIS3\profiles\default/python/plugins\Terraform\processing_alg\topocorrection_eval\multi_criteria_eval.py", line 175, in _process_internal
scores_per_group = self._get_scores_per_groups(ctx, group_ids_path)
File "%APPDATA%/QGIS/QGIS3\profiles\default/python/plugins\Terraform\processing_alg\topocorrection_eval\multi_criteria_rank.py", line 364, in _get_scores_per_groups
return algorithm.evaluate()
File "%APPDATA%/QGIS/QGIS3\profiles\default/python/plugins\Terraform\processing_alg\topocorrection_eval\multi_criteria_rank.py", line 102, in evaluate
return super().evaluate()
File "%APPDATA%/QGIS/QGIS3\profiles\default/python/plugins\Terraform\processing_alg\topocorrection_eval\eval.py", line 153, in evaluate
self.ctx.force_cancel(exc)
File "%APPDATA%/QGIS/QGIS3\profiles\default/python/plugins\Terraform\processing_alg\execution_context.py", line 165, in force_cancel
raise error or RuntimeError("Canceled")
File "%APPDATA%/QGIS/QGIS3\profiles\default/python/plugins\Terraform\processing_alg\topocorrection_eval\eval.py", line 149, in evaluate
result += self._evaluate_group(group)
File "%APPDATA%/QGIS/QGIS3\profiles\default/python/plugins\Terraform\processing_alg\topocorrection_eval\multi_criteria_rank.py", line 108, in _evaluate_group
metrics_per_correction_band_df, normalized_metrics = self.merge_strategy.merge(group_df.copy(), group_idx)
File "%APPDATA%/QGIS/QGIS3\profiles\default/python/plugins\Terraform\processing_alg\topocorrection_eval\multi_criteria_rank.py", line 177, in merge
normalized_metrics: DataFrame = self._normalize(metrics, orig_metrics)
File "%APPDATA%/QGIS/QGIS3\profiles\default/python/plugins\Terraform\processing_alg\topocorrection_eval\multi_criteria_rank.py", line 182, in _normalize
norm_good_results = self._normalize_single(good_results, metrics_min=orig_metrics)
File "%APPDATA%/QGIS/QGIS3\profiles\default/python/plugins\Terraform\processing_alg\topocorrection_eval\multi_criteria_rank.py", line 196, in _normalize_single
metrics_max = metrics.max(level=1)
File "C:\PROGRA~1\QGIS32~1.11\apps\Python39\lib\site-packages\pandas\core\generic.py", line 11646, in max
return NDFrame.max(self, axis, skipna, numeric_only, **kwargs)
File "C:\PROGRA~1\QGIS32~1.11\apps\Python39\lib\site-packages\pandas\core\generic.py", line 11185, in max
return self._stat_function(
File "C:\PROGRA~1\QGIS32~1.11\apps\Python39\lib\site-packages\pandas\core\generic.py", line 11154, in _stat_function
nv.validate_stat_func((), kwargs, fname=name)
File "C:\PROGRA~1\QGIS32~1.11\apps\Python39\lib\site-packages\pandas\compat\numpy\function.py", line 80, in __call__
validate_kwargs(fname, kwargs, self.defaults)
File "C:\PROGRA~1\QGIS32~1.11\apps\Python39\lib\site-packages\pandas\util\_validators.py", line 162, in validate_kwargs
_check_for_invalid_keys(fname, kwargs, compat_args)
File "C:\PROGRA~1\QGIS32~1.11\apps\Python39\lib\site-packages\pandas\util\_validators.py", line 136, in _check_for_invalid_keys
raise TypeError(f"{fname}() got an unexpected keyword argument '{bad_arg}'")
TypeError: max() got an unexpected keyword argument 'level'
wessnerd commented 6 months ago

I've had a similar issue; looks like the 'level' kwarg provided to the Pandas DataFrame .max() function was deprecated after version 1.3.0 according to the documentation. Pandas documentation recommends switching to groupby after v1.3.0, although it does not specify if that can be passed as a kwarg to the .max() function or as a separate function call, e.g. pandas.groupby(), to index into the MultiIndex DataFrame as intended. [EDIT: I just read the other issue, and noticed that tigrulya-exe had already pinpointed the Pandas versioning issue; feel free to ignore this!]

tigrulya-exe commented 5 months ago

@twest820 @wessnerd Hi! Sorry for the late response, the plugin was updated and tested with the latest version of QGIS, 3.38.0-Grenoble. You can download the new version from the plugin repository. Feel free to reopen the issue or create new ones if you encounter any problems.