Skip to content

Commit

Permalink
Merge branch '312-Remove-rasterio-open' into 'master'
Browse files Browse the repository at this point in the history
Resolve "Evolution du check_conf : Suppression des appels rasterio_open"

Closes #312

See merge request 3d/PandoraBox/pandora!274
  • Loading branch information
lecontm committed Jul 28, 2023
2 parents 9f5c6ed + eb89f93 commit 2e40bcf
Show file tree
Hide file tree
Showing 11 changed files with 783 additions and 113 deletions.
490 changes: 490 additions & 0 deletions notebooks/advanced_examples/api_check_conf.ipynb

Large diffs are not rendered by default.

Binary file added notebooks/data/left_rgb.tif
Binary file not shown.
Binary file added notebooks/data/right_rgb.tif
Binary file not shown.
12 changes: 9 additions & 3 deletions notebooks/introduction_and_basic_usage.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@
"# Load pandora imports\n",
"import pandora\n",
"from pandora.img_tools import read_img\n",
"from pandora.check_json import check_pipeline_section, concat_conf, memory_consumption_estimation\n",
"from pandora.check_json import check_pipeline_section, concat_conf, memory_consumption_estimation, get_metadata, get_config_pipeline\n",
"from pandora.state_machine import PandoraMachine\n",
"from pandora import import_plugin, check_conf"
]
Expand Down Expand Up @@ -403,7 +403,10 @@
"metadata": {},
"outputs": [],
"source": [
"checked_cfg = check_pipeline_section(user_cfg, pandora_machine)"
"metadata_left = get_metadata(user_cfg[\"input\"][\"img_left\"], disp_min, disp_max)\n",
"metadata_right = get_metadata(user_cfg[\"input\"][\"img_right\"], None, None)\n",
"user_cfg_pipeline = get_config_pipeline(user_cfg)\n",
"checked_cfg = check_pipeline_section(user_cfg_pipeline, metadata_left, metadata_right, pandora_machine)"
]
},
{
Expand Down Expand Up @@ -616,7 +619,10 @@
"metadata": {},
"outputs": [],
"source": [
"checked_cfg = check_pipeline_section(user_cfg, pandora_machine)"
"metadata_left = get_metadata(user_cfg[\"input\"][\"img_left\"], disp_min, disp_max)\n",
"metadata_right = get_metadata(user_cfg[\"input\"][\"img_right\"], None, None)\n",
"user_cfg_pipeline = get_config_pipeline(user_cfg)\n",
"checked_cfg = check_pipeline_section(user_cfg_pipeline, metadata_left, metadata_right, pandora_machine)"
]
},
{
Expand Down
29 changes: 19 additions & 10 deletions notebooks/statistical_and_visual_analysis.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
"# Load pandora imports\n",
"import pandora\n",
"from pandora.img_tools import read_img\n",
"from pandora.check_json import check_pipeline_section, concat_conf\n",
"from pandora.check_json import check_pipeline_section, concat_conf, get_metadata, get_config_pipeline\n",
"from pandora.state_machine import PandoraMachine\n",
"from pandora import import_plugin, check_conf"
]
Expand Down Expand Up @@ -323,7 +323,8 @@
"metadata": {},
"outputs": [],
"source": [
"cfg_pipeline_zncc = check_pipeline_section(user_cfg_zncc, pandora_machine)['pipeline']"
"disp_min = -60\n",
"disp_max = 0"
]
},
{
Expand All @@ -332,8 +333,10 @@
"metadata": {},
"outputs": [],
"source": [
"disp_min = -60\n",
"disp_max = 0"
"metadata_left = get_metadata(user_cfg_zncc[\"input\"][\"img_left\"], disp_min, disp_max)\n",
"metadata_right = get_metadata(user_cfg_zncc[\"input\"][\"img_right\"], None, None)\n",
"user_cfg_pipeline = get_config_pipeline(user_cfg_zncc)\n",
"cfg_pipeline_zncc = check_pipeline_section(user_cfg_pipeline, metadata_left, metadata_right, pandora_machine)['pipeline']"
]
},
{
Expand Down Expand Up @@ -505,7 +508,8 @@
"metadata": {},
"outputs": [],
"source": [
"cfg_pipeline_census_sgm = check_pipeline_section(user_cfg_census, pandora_machine)['pipeline']"
"disp_min = -60\n",
"disp_max = 0"
]
},
{
Expand All @@ -514,8 +518,10 @@
"metadata": {},
"outputs": [],
"source": [
"disp_min = -60\n",
"disp_max = 0"
"metadata_left = get_metadata(user_cfg_census[\"input\"][\"img_left\"], disp_min, disp_max)\n",
"metadata_right = get_metadata(user_cfg_census[\"input\"][\"img_right\"], None, None)\n",
"user_cfg_pipeline = get_config_pipeline(user_cfg_census)\n",
"cfg_pipeline_census_sgm = check_pipeline_section(user_cfg_pipeline, metadata_left, metadata_right, pandora_machine)['pipeline']"
]
},
{
Expand Down Expand Up @@ -789,7 +795,8 @@
"metadata": {},
"outputs": [],
"source": [
"cfg_pipeline_census = check_pipeline_section(user_cfg_census, pandora_machine)['pipeline']"
"disp_min = -60\n",
"disp_max = 0"
]
},
{
Expand All @@ -798,8 +805,10 @@
"metadata": {},
"outputs": [],
"source": [
"disp_min = -60\n",
"disp_max = 0"
"metadata_left = get_metadata(user_cfg_census_sgm[\"input\"][\"img_left\"], disp_min, disp_max)\n",
"metadata_right = get_metadata(user_cfg_census_sgm[\"input\"][\"img_right\"], None, None)\n",
"user_cfg_pipeline = get_config_pipeline(user_cfg_census_sgm)\n",
"cfg_pipeline_census = check_pipeline_section(user_cfg_pipeline, metadata_left, metadata_right, pandora_machine)['pipeline']"
]
},
{
Expand Down
16 changes: 11 additions & 5 deletions notebooks/usage_with_multiscale.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@
"# Load pandora imports\n",
"import pandora\n",
"from pandora.img_tools import read_img\n",
"from pandora.check_json import check_pipeline_section, concat_conf\n",
"from pandora.check_json import check_pipeline_section, concat_conf, get_metadata, get_config_pipeline\n",
"from pandora.state_machine import PandoraMachine\n",
"from pandora import import_plugin, check_conf"
]
Expand Down Expand Up @@ -445,7 +445,10 @@
"metadata": {},
"outputs": [],
"source": [
"checked_cfg = check_pipeline_section(user_cfg, pandora_machine)"
"metadata_left = get_metadata(user_cfg[\"input\"][\"img_left\"], disp_min, disp_max)\n",
"metadata_right = get_metadata(user_cfg[\"input\"][\"img_right\"], None, None)\n",
"user_cfg_pipeline = get_config_pipeline(user_cfg)\n",
"checked_cfg = check_pipeline_section(user_cfg_pipeline, metadata_left, metadata_right, pandora_machine)"
]
},
{
Expand Down Expand Up @@ -669,7 +672,10 @@
"metadata": {},
"outputs": [],
"source": [
"checked_cfg = check_pipeline_section(user_cfg, pandora_machine)"
"metadata_left = get_metadata(user_cfg[\"input\"][\"img_left\"], disp_min, disp_max)\n",
"metadata_right = get_metadata(user_cfg[\"input\"][\"img_right\"], None, None)\n",
"user_cfg_pipeline = get_config_pipeline(user_cfg)\n",
"checked_cfg = check_pipeline_section(user_cfg_pipeline, metadata_left, metadata_right, pandora_machine)"
]
},
{
Expand Down Expand Up @@ -1012,9 +1018,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "notebooks_env",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "pandora-env"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
Expand Down
96 changes: 90 additions & 6 deletions pandora/check_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import sys
from collections.abc import Mapping
from typing import Dict, Union, List, Tuple
import xarray as xr

import numpy as np
from json_checker import Checker, Or, And
Expand Down Expand Up @@ -206,6 +207,64 @@ def get_config_input(user_cfg: Dict[str, dict]) -> Dict[str, dict]:
return cfg


def get_config_pipeline(user_cfg: Dict[str, dict]) -> Dict[str, dict]:
"""
Get the pipeline configuration
:param user_cfg: user configuration
:type user_cfg: dict
:return cfg: partial configuration
:rtype cfg: dict
"""

cfg = {}

if "pipeline" in user_cfg:
cfg["pipeline"] = user_cfg["pipeline"]

return cfg


def get_metadata(
img: str, disp_min: Union[int, str, None], disp_max: Union[int, str, None], classif: str = None, segm: str = None
) -> xr.Dataset:
"""
Read metadata from image, and return the corresponding xarray.DataSet
:param img: img_path
:type img: str
:param disp_min: minimal disparity
:type disp_min: int or str or None
:param disp_max: maximal disparity
:type disp_max: int or str or None
:param classif: path to the classif (optional)
:type classif: str
:param segm: path to the segm (optional)
:type segm: str
:return: partial xarray.DataSet (attributes and coordinates)
:rtype: xarray.DataSet
"""
img_ds = rasterio_open(img)
coords = {
"band": list(img_ds.descriptions),
"row": img_ds.height,
"col": img_ds.width,
}
attrs = {"disp_min": disp_min, "disp_max": disp_max}
dataset = xr.Dataset(data_vars={}, coords=coords, attrs=attrs)

if classif is not None:
classif_ds = rasterio_open(classif)
band_classif = list(classif_ds.descriptions)
dataset.coords["band_classif"] = band_classif
dataset.attrs["classif"] = True

if segm is not None:
dataset.attrs["segm"] = True

return dataset


def memory_consumption_estimation(
user_pipeline_cfg: Dict[str, dict],
user_input: Union[Dict[str, dict], Tuple[str, int, int]],
Expand Down Expand Up @@ -247,7 +306,9 @@ def memory_consumption_estimation(
else:
# First, check if the configuration is valid
cfg = {"pipeline": user_pipeline_cfg["pipeline"], "input": user_input["input"]}
checked_cfg = check_pipeline_section(cfg, pandora_machine)
img_left_metadata = get_metadata(cfg["input"]["img_left"], cfg["input"]["disp_min"], cfg["input"]["disp_max"])
img_right_metadata = get_metadata(cfg["input"]["img_right"], None, None)
checked_cfg = check_pipeline_section(cfg, img_left_metadata, img_right_metadata, pandora_machine)
# Obtain pipeline cfg
pipeline_cfg = checked_cfg["pipeline"]

Expand All @@ -271,22 +332,28 @@ def memory_consumption_estimation(
return None


def check_pipeline_section(user_cfg: Dict[str, dict], pandora_machine: PandoraMachine) -> Dict[str, dict]:
def check_pipeline_section(
user_cfg: Dict[str, dict], img_left: xr.Dataset, img_right: xr.Dataset, pandora_machine: PandoraMachine
) -> Dict[str, dict]:
"""
Check if the pipeline is correct by
- Checking the sequence of steps according to the machine transitions
- Checking parameters, define in dictionary, of each Pandora step
:param user_cfg: pipeline user configuration
:type user_cfg: dict
:param img_left: image left with metadata
:type img_left: xarray.Dataset
:param img_right: image right with metadata
:type img_right: xarray.Dataset
:param pandora_machine: instance of PandoraMachine
:type pandora_machine: PandoraMachine object
:return: cfg: pipeline configuration
:rtype: cfg: dict
"""
# Check if user configuration pipeline is compatible with transitions/states of pandora machine.
cfg = update_conf(default_short_configuration_pipeline, user_cfg)
pandora_machine.check_conf(cfg)
pandora_machine.check_conf(cfg, img_left, img_right)

cfg = update_conf(cfg, pandora_machine.pipeline_cfg)

Expand Down Expand Up @@ -341,9 +408,9 @@ def check_input_section(user_cfg: Dict[str, dict]) -> Dict[str, dict]:
check_disparities(
cfg["input"]["disp_min_right"],
cfg["input"]["disp_max_right"],
cfg["input"]["img_left"],
cfg["input"]["img_right"],
)

# check bands
check_band_names(cfg["input"]["img_left"])
check_band_names(cfg["input"]["img_right"])

Expand Down Expand Up @@ -373,8 +440,25 @@ def check_conf(user_cfg: Dict[str, dict], pandora_machine: PandoraMachine) -> di
user_cfg_input = get_config_input(user_cfg)
cfg_input = check_input_section(user_cfg_input)

# read metadata from left and right images
img_left_metadata = get_metadata(
cfg_input["input"]["img_left"],
cfg_input["input"]["disp_min"],
cfg_input["input"]["disp_max"],
cfg_input["input"]["left_classif"],
cfg_input["input"]["left_segm"],
)
img_right_metadata = get_metadata(
cfg_input["input"]["img_right"],
cfg_input["input"]["disp_min_right"],
cfg_input["input"]["disp_max_right"],
cfg_input["input"]["right_classif"],
cfg_input["input"]["right_segm"],
)

# check pipeline
cfg_pipeline = check_pipeline_section(user_cfg, pandora_machine)
user_cfg_pipeline = get_config_pipeline(user_cfg)
cfg_pipeline = check_pipeline_section(user_cfg_pipeline, img_left_metadata, img_right_metadata, pandora_machine)

# concatenate updated config
cfg = concat_conf([cfg_input, cfg_pipeline])
Expand Down
Loading

0 comments on commit 2e40bcf

Please sign in to comment.