pandora2d.common

This module contains functions allowing to save the results and the configuration of Pandora pipeline.

Attributes

T

Classes

Registry

Registry of classes.

AllPrimitiveEncoder

JSON Encoder to serialize all elements

Functions

convert_disp_to_grid(→ xarray.Dataset)

Convert disparity maps to deformation grids

convert_grid_to_disp(→ xarray.Dataset)

Convert deformation grids to disparity maps

save_disparity_maps(→ None)

Save disparity maps into directory defined by cfg's output/path key,

save_confidence_maps(→ None)

Save confidence maps into directory defined by cfg's output/path key,

_save_disparity_maps_report(→ None)

Generate a report about disparities statistics and save it to json file.

_save_dataset(→ None)

Save data_vars in the output directory.

save_attributes(→ None)

Save dataset attributes in a json file

adjust_georeferencement(→ None)

Change origin in case a ROI is present and set pixel size to the matching cost step.

get_step(→ tuple[int, int])

Get step from matching cost or return default value.

set_pixel_size(→ None)

Set the pixel size according to the step used in calculating the matching cost.

dataset_disp_maps(→ xarray.Dataset)

Create the dataset containing disparity maps and score maps

complete_dataset_disp_maps(→ None)

Complete the dataset with computed disparity maps and score maps.

save_config(→ None)

Save config to json file in directory given by the key output/path.

string_to_path(→ pathlib.Path)

Get the absolute path of a given path string. If the path is not absolute,

resolve_path_in_config(→ dict)

Create a copy of config with all path strings replaced by an absolute path string relative to

all_same(→ bool)

Return True if all items in sequence are equals.

build_usable_data_mask(→ numpy.typing.NDArray[numpy.bool_])

Build a boolean mask indicating which elements of the input array are usable.

get_cost_volume_without_margins(→ xarray.Dataset)

Getting cost_volume without the margins on the disparities

Module Contents

pandora2d.common.T[source]
class pandora2d.common.Registry(default: type[T] | None = None)[source]

Bases: Generic[T]

Registry of classes.

A class to decorate classes in order to register them with a string name.

registered: dict[str, type[T]][source]
default = None[source]
add(name: str) collections.abc.Callable[[type[T]], type[T]][source]

Register a class with name.

Parameters:

name – Name to register the decorated class with.

Returns:

The decorated class.

get(name: str) type[T][source]

Get the class registered as name.

Parameters:

name – The name of the registered class to retrieve.

Returns:

The class registered under name or the default class if not found.

Raises:

KeyError – If no class is registered under name and no default is set.

class pandora2d.common.AllPrimitiveEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]

Bases: json.JSONEncoder

JSON Encoder to serialize all elements

default(o)[source]

Implement this method in a subclass such that it returns a serializable object for o, or calls the base implementation (to raise a TypeError).

For example, to support arbitrary iterators, you could implement default like this:

def default(self, o):
    try:
        iterable = iter(o)
    except TypeError:
        pass
    else:
        return list(iterable)
    # Let the base class default method raise the TypeError
    return JSONEncoder.default(self, o)
pandora2d.common.convert_disp_to_grid(dataset: xarray.Dataset, pixel_convention: list[int]) xarray.Dataset[source]

Convert disparity maps to deformation grids

Parameters:
  • dataset – disparity maps dataset

  • pixel_convention – initial pixel convention for grid

pandora2d.common.convert_grid_to_disp(dataset: xarray.Dataset, pixel_convention: list[int]) xarray.Dataset[source]

Convert deformation grids to disparity maps

Parameters:
  • dataset – deformation maps dataset

  • pixel_convention – initial pixel convention for grid

pandora2d.common.save_disparity_maps(dataset: xarray.Dataset, cfg: dict) None[source]

Save disparity maps into directory defined by cfg’s output/path key, create it with its parents if necessary.

Parameters:
  • dataset

    Dataset which contains:

    • lines : the disparity map for the lines 2D DataArray (row, col)

    • columns : the disparity map for the columns 2D DataArray (row, col)

  • cfg – user configuration

Returns:

None

pandora2d.common.save_confidence_maps(dataset: xarray.Dataset, cfg: dict) None[source]

Save confidence maps into directory defined by cfg’s output/path key, create it with its parents if necessary.

Parameters:
  • dataset

    Dataset which contains:

    • lines : the confidence map for the lines 2D DataArray (row, col)

    • columns : the confidence map for the columns 2D DataArray (row, col)

  • cfg – user configuration

Returns:

None

pandora2d.common._save_disparity_maps_report(dataset: xarray.Dataset, output: pathlib.Path) None[source]

Generate a report about disparities statistics and save it to json file. :param dataset: disparity maps :param output: path where to save report

pandora2d.common._save_dataset(dataset: xarray.Dataset, output: pathlib.Path) None[source]

Save data_vars in the output directory.

Parameters:
  • dataset – Dataset

  • output – output directory

Returns:

None

pandora2d.common.save_attributes(dataset: xarray.Dataset, output: str | os.PathLike) None[source]

Save dataset attributes in a json file

Parameters:
  • dataset

    Dataset which contains:

    • row_map : the disparity map for the lines 2D DataArray (row, col)

    • col_map : the disparity map for the columns 2D DataArray (row, col)

  • output – output directory

Returns:

None

pandora2d.common.adjust_georeferencement(dataset: xarray.Dataset, cfg: dict) None[source]

Change origin in case a ROI is present and set pixel size to the matching cost step.

Parameters:
  • dataset – dataset to configure.

  • cfg – configuration

pandora2d.common.get_step(cfg: dict) tuple[int, int][source]

Get step from matching cost or return default value. :param cfg: configuration :return: row_step, col_step

pandora2d.common.set_pixel_size(dataset: xarray.Dataset, row_step: int = 1, col_step: int = 1) None[source]

Set the pixel size according to the step used in calculating the matching cost.

This ensures that all pixels are well geo-referenced in case a step is applied.

Parameters:
  • dataset – Data to save

  • row_step – step used in row

  • col_step – step used in column

pandora2d.common.dataset_disp_maps(coords: xarray.Coordinates, dataset_validity: xarray.Dataset, attributes: dict = None, dtype: numpy.typing.DTypeLike = np.float32, cost_volume_confidence_step: bool = False) xarray.Dataset[source]

Create the dataset containing disparity maps and score maps :param coords: disparity maps coordinates :param dataset_validity: xr.Dataset containing validity information :param attributes: disparity map for col :param dtype: dtype of the dataset :param cost_volume_confidence_step: whether there is a cost volume confidence step in the pipeline or not :return: dataset: Dataset with the empty disparity maps and score with the data variables :

  • row_map 2D xarray.DataArray (row, col)

  • col_map 2D xarray.DataArray (row, col)

  • score 2D xarray.DataArray (row, col)

pandora2d.common.complete_dataset_disp_maps(disparity_dataset: xarray.Dataset, delta_row: numpy.ndarray, delta_col: numpy.ndarray, correlation_score: numpy.ndarray) None[source]

Complete the dataset with computed disparity maps and score maps.

If a cost volume confidence step is present in the pipeline, the confidence measure is filled during the corresponding step.

Parameters:
  • disparity_dataset – initialized disparity maps dataset

  • delta_row – disparity map for row

  • delta_col – disparity map for col

  • correlation_score – score map

pandora2d.common.save_config(config: dict) None[source]

Save config to json file in directory given by the key output/path.

Create file tree if it does not exist, :param config: configuration to save

pandora2d.common.string_to_path(path: str, relative_to: pathlib.Path | str) pathlib.Path[source]

Get the absolute path of a given path string. If the path is not absolute, it resolves it relative to the provided relative_to path.

Parameters:
  • path – The path string to convert to an absolute path.

  • relative_to – The base path to resolve the relative path.

Returns:

The absolute path of the given path string.

Example:
>>> string_to_path('/absolute/path', Path('/home/user'))
PosixPath('/absolute/path')
>>> string_to_path('relative/path', Path('/home/user'))
PosixPath('/home/user/relative/path')
>>> string_to_path('~/mydir', Path('/home/user'))
PosixPath('/home/user/mydir')
pandora2d.common.resolve_path_in_config(config: dict, config_path: pathlib.Path) dict[source]

Create a copy of config with all path strings replaced by an absolute path string relative to config_path.

Parameters:
  • config – config to modify

  • config_path – path to the config file.

Returns:

The configuration with changed paths.

pandora2d.common.all_same(iterable: collections.abc.Iterable) bool[source]

Return True if all items in sequence are equals.

pandora2d.common.build_usable_data_mask(disp_data: numpy.typing.NDArray, nodata: float | None) numpy.typing.NDArray[numpy.bool_][source]

Build a boolean mask indicating which elements of the input array are usable.

An element is considered usable if it is finite (not NaN or infinite) and, when a nodata value is provided, different from that value.

Parameters:
  • disp_data – Input array containing the data to be tested.

  • nodata – Value representing missing or invalid data. If None, only finiteness is checked.

Returns:

A boolean array with the same shape as disp_data, where True indicates usable data.

pandora2d.common.get_cost_volume_without_margins(cost_volumes: xarray.Dataset) xarray.Dataset[source]

Getting cost_volume without the margins on the disparities

Parameters:

cost_volumes – the cost volumes dataset with the data variables: - cost_volume 4D xarray.DataArray (row, col, disp_row, disp_col)

Returns:

cost_volumes without margins