visutilsο
Module to visualise .tiff images, label masks and results from the fitting of neural networks for remote sensing.
- de_interlace(x: Sequence[Any], f: int) ndarray[tuple[int, ...], dtype[Any]]ο
Separates interlaced arrays,
xat a frequency offfrom each other.
- dec_extent_to_deg(shape: tuple[int, int], bounds: BoundingBox, src_crs: CRS, new_crs: CRS = CRS.from_epsg(4326), spacing: int = 32) tuple[tuple[int, int, int, int], ndarray[tuple[int, ...], dtype[float64]], ndarray[tuple[int, ...], dtype[float64]]]ο
Gets the extent of the image with
shapeand withboundsin latitude, longitude of systemnew_crs.- Parameters:
shape (tuple[int, int]) β 2D shape of image to be used to define the extents of the composite image.
bounds (BoundingBox) β Object describing a geospatial bounding box. Must contain
minx,maxx,minyandmaxyparameters.src_crs (CRS) β Source co-ordinate reference system (CRS).
new_crs (CRS) β Optional; The co-ordinate reference system (CRS) to transform to.
spacing (int) β Spacing of the lat - lon ticks.
- Returns:
The corners of the image in pixel co-ordinates e.g.
(0, 256, 0, 256).The latitude extent of the image with ticks at intervals defined by
spacing.The longitude extent of the image with ticks at intervals defined by
spacing.
- Return type:
tuple[tuple[int, int, int, int], ndarray[float], ndarray[float]]
- discrete_heatmap(data: ndarray[tuple[int, ...], dtype[int64]], classes: list[str] | tuple[str, ...], cmap_style: str | ListedColormap | None = None, block_size: int = 32) Noneο
Plots a heatmap with a discrete colour bar. Designed for Radiant Earth MLHub 256x256 SENTINEL images.
- Parameters:
- format_plot_names(model_name: str, timestamp: str, path: Sequence[str] | str | Path) dict[str, Path]ο
Creates unique filenames of plots in a standardised format.
- Parameters:
- Returns:
Formatted filenames for plots.
- Return type:
- get_mlp_cmap(cmap_style: Colormap | str | None = None, n_classes: int | None = None) Colormap | Noneο
Creates a cmap from query
- Parameters:
cmap_style (Colormap | str) β Optional;
matplotlibcolourmap style to get.n_classes (int) β Optional; Number of classes in data to assign colours to.
- Returns:
If
cmap_styleandn_classesprovided, returns aListedColormapinstance.If
cmap_styleprovided but non_classes, returns aColormapinstance.If neither arguments are provided,
Noneis returned.
- Return type:
Colormap | None
- labelled_rgb_image(image: ndarray[tuple[int, ...], dtype[float64]], mask: ndarray[tuple[int, ...], dtype[int64]], bounds: BoundingBox, src_crs: CRS, path: str | Path, name: str, classes: list[str] | tuple[str, ...], cmap_style: str | ListedColormap | None = None, new_crs: CRS | None = CRS.from_epsg(4326), block_size: int = 32, alpha: float = 0.5, show: bool = True, save: bool = True, figdim: tuple[int | float, int | float] = (8.02, 10.32)) Pathο
Produces a layered image of an RGB image, and itβs associated label mask heat map alpha blended on top.
- Parameters:
image (ndarray[int]) β Array representing the image of shape
(height x width x bands).mask (ndarray[int]) β Ground truth mask. Should be of shape (height x width) matching
image.bounds (BoundingBox) β Object describing a geospatial bounding box. Must contain
minx,maxx,minyandmaxyparameters.src_crs (CRS) β Source co-ordinate reference system (CRS).
path (str) β Path to where to save created figure.
name (str) β Name of figure. Will be used for title and in the filename.
classes (list[str]) β Optional; List of all possible class labels.
cmap_style (str | ListedColormap) β Optional; Name or object for colour map style.
new_crs (CRS) β Optional; The co-ordinate reference system (CRS) to transform to.
block_size (int) β Optional; Size of block image subdivision in pixels.
alpha (float) β Optional; Fraction determining alpha blending of label mask.
show (bool) β Optional; Show the figure when plotted.
save (bool) β Optional; Save the figure to
path.figdim (tuple[int | float, int | float]) β Optional; Figure (height, width) in inches.
- Returns:
Path to figure save location.
- Return type:
- make_confusion_matrix(pred: list[int] | ndarray[tuple[int, ...], dtype[int64]], labels: list[int] | ndarray[tuple[int, ...], dtype[int64]], classes: dict[int, str], filename: str | Path | None = None, cmap_style: str = 'Blues', figsize: tuple[int, int] = (2, 2), show: bool = True, save: bool = False) Noneο
Creates a heat-map of the confusion matrix of the given model.
- Parameters:
pred (list[int]) β Predictions made by model on test images.
labels (list[int]) β Accompanying ground truth labels for testing images.
classes (dict[int, str]) β Dictionary mapping class labels to class names.
filename (str) β Optional; Name of file to save plot to.
cmap_style (str) β Colourmap style to use in the confusion matrix.
show (bool) β Optional; Whether to show plot.
save (bool) β Optional; Whether to save plot to file.
- Returns:
None
- make_gif(dates: Sequence[str], images: ndarray[tuple[int, ...], dtype[Any]], masks: ndarray[tuple[int, ...], dtype[Any]], bounds: BoundingBox, src_crs: CRS, classes: list[str] | tuple[str, ...], gif_name: str, path: str | Path, cmap_style: str | ListedColormap | None = None, fps: float = 1.0, new_crs: CRS | None = CRS.from_epsg(4326), alpha: float = 0.5, figdim: tuple[int | float, int | float] = (8.02, 10.32)) Noneο
Wrapper to
labelled_rgb_image()to make a GIF for a patch out of scenes.- Parameters:
dates (Sequence[str]) β Dates of scenes to be used as the frames in the GIF.
images (ndarray[Any]) β All the frames of imagery to make the GIF from. Leading dimension must be the same length as
datesandmasks.masks (ndarray[Any]) β The masks for each frame of the GIF. Leading dimension must be the same length as
datesandimage.bounds (BoundingBox) β The bounding box (in the
src_crsCRS) of the patch theGIFwill be of.src_crs (CRS) β Source co-ordinate reference system (CRS).
gif_name (str) β Path to and name of GIF to be made.
path (Path | str]) β Path to where to save frames of the
GIF.cmap_style (str | ListedColormap) β Optional; Name or object for colour map style.
fps (float) β Optional; Frames per second of
GIF.new_crs (CRS) β Optional; The co-ordinate reference system (CRS) to transform to.
alpha (float) β Optional; Fraction determining alpha blending of label mask.
figdim (tuple[int | float, int | float]) β Optional; Figure (height, width) in inches.
- Returns:
None
- make_rgb_image(image: ndarray[tuple[int, ...], dtype[float64]], block_size: int = 32, max_pixel_value: int = 255) AxesImageο
Creates an RGB image from a composition of red, green and blue bands.
- Parameters:
- Returns:
Plotted RGB image object.
- Return type:
- make_roc_curves(probs: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes], labels: Sequence[int] | ndarray[tuple[int, ...], dtype[int64]], class_names: dict[int, str], colours: dict[int, str], micro: bool = True, macro: bool = True, filename: str | Path | None = None, show: bool = False, save: bool = True) Noneο
Plots ROC curves for each class, the micro and macro average ROC curves and accompanying AUCs.
Adapted from Scikit-learnβs example at: https://scikit-learn.org/stable/auto_examples/model_selection/plot_roc.html
- Parameters:
probs (list | ndarray[int]) β Array of probabilistic predicted classes from model where each sample should have a list of the predicted probability for each class.
labels (list | ndarray[int]) β List of corresponding ground truth labels.
class_names (dict[int, str]) β Dictionary mapping class labels to class names.
colours (dict[int, str]) β Dictionary mapping class labels to colours.
micro (bool) β Optional; Whether to compute and plot the micro average ROC curves.
macro (bool) β Optional; Whether to compute and plot the macro average ROC curves.
filename (str | Path) β Optional; Name of file to save plot to.
save (bool) β Optional; Whether to save the plots to file.
show (bool) β Optional; Whether to show the plots.
- Returns:
None
- plot_embedding(embeddings: Any, index: Sequence[BoundingBox] | Sequence[int], data_dir: Path | str, dataset_params: dict[str, Any], title: str | None = None, show: bool = False, save: bool = True, filename: str | Path | None = None, max_pixel_value: int = 255, cache_dir: str | Path | None = None) Noneο
Using TSNE Clustering, visualises the embeddings from a model.
- Parameters:
embeddings (Any) β Embeddings from a model.
bounds (Sequence[BoundingBox] | ndarray[BoundingBox]) β # noqa: E501 Array of objects describing a geospatial bounding box. Must contain
minx,maxx,minyandmaxyparameters.task_name (str) β Name of the task that the samples are from.
title (str) β Optional; Title of plot.
show (bool) β Optional; Whether to show plot.
save (bool) β Optional; Whether to save plot to file.
filename (str) β Optional; Name of file to save plot to.
cache_dir (str | Path) β Optional; Path to the directory to load the cached dataset from. Defaults to None (so will create dataset from scratch).
- Returns:
None
- plot_history(metrics: dict[str, Any], filename: str | Path | None = None, save: bool = True, show: bool = False) Noneο
Plots model history based on metrics supplied.
- Parameters:
- Returns:
None
- plot_results(plots: dict[str, bool], x: ndarray[tuple[int, ...], dtype[int64]] | None = None, y: list[int] | ndarray[tuple[int, ...], dtype[int64]] | None = None, z: list[int] | ndarray[tuple[int, ...], dtype[int64]] | None = None, metrics: dict[str, Any] | None = None, ids: list[str] | None = None, index: ndarray[tuple[int, ...], dtype[Any]] | None = None, probs: list[float] | ndarray[tuple[int, ...], dtype[float64]] | None = None, embeddings: ndarray[tuple[int, ...], dtype[Any]] | None = None, class_names: dict[int, str] | None = None, colours: dict[int, str] | None = None, save: bool = True, show: bool = False, model_name: str | None = None, timestamp: str | None = None, results_dir: Sequence[str] | str | Path | None = None, task_cfg: dict[str, Any] | None = None, global_cfg: dict[str, Any] | None = None) Noneο
Orchestrates the creation of various plots from the results of a model fitting.
- Parameters:
plots (dict[str, bool]) β Dictionary defining which plots to make.
x (list[list[int]] | ndarray[ndarray[int]]) β Optional; List of images supplied to the model.
y (list[list[int]] | ndarray[ndarray[int]]) β Optional; List of corresponding ground truth labels.
z (list[list[int]] | ndarray[ndarray[int]]) β Optional; List of predicted label masks.
metrics (dict[str, Any]) β Optional; Dictionary containing a log of various metrics used to assess the performance of a model.
ids (list[str]) β Optional; List of IDs defining the origin of samples to the model. Maybe either patch IDs or scene tags.
task_name (str) β Optional; Name of task that samples are from.
index (ndarray[int] | ndarray[BoundingBox]) β Optional; Array of objects describing a geospatial bounding box for each sample or a sequence of indexes.
probs (list[float] | ndarray[float]) β Optional; Array of probabilistic predicted classes from model where each sample should have a list of the predicted probability for each class.
embeddings (ndarray[Any]) β Embeddings from the model to visualise with TSNE clustering.
class_names (dict[int, str]) β Optional; Dictionary mapping class labels to class names.
colours (dict[int, str]) β Optional; Dictionary mapping class labels to colours.
save (bool) β Optional; Save the plots to file.
show (bool) β Optional; Show the plots.
model_name (str) β Optional; Name of model. e.g. MLP-MkVI.
timestamp (str) β Optional; Time and date to be used to identify experiment. If not specified, the current date-time is used.
results_dir (list[str] | str | Path) β Optional; Path to the directory for storing plots.
Notes
save==True,show==Falseregardless of input for plots made for each sample such as PvT or Mask plots.- Returns:
None
- plot_subpopulations(class_dist: list[tuple[int, int]], class_names: dict[int, str] | None = None, cmap_dict: dict[int, str] | None = None, filename: str | Path | None = None, save: bool = True, show: bool = False) Noneο
Creates a pie chart of the distribution of the classes within the data.
- Parameters:
class_dist (list[tuple[int, int]]) β Modal distribution of classes in the dataset provided.
class_names (dict[int, str]) β Optional; Dictionary mapping class labels to class names.
cmap_dict (dict[int, str]) β Optional; Dictionary mapping class labels to class colours.
filename (str) β Optional; Name of file to save plot to.
show (bool) β Optional; Whether to show plot.
save (bool) β Optional; Whether to save plot to file.
- Returns:
None
- prediction_plot(sample: dict[str, Any], sample_id: str, classes: dict[int, str], src_crs: CRS, new_crs: CRS = CRS.from_epsg(4326), path: str = '', cmap_style: str | ListedColormap | None = None, exp_id: str | None = None, fig_dim: tuple[int | float, int | float] | None = None, block_size: int = 32, show: bool = True, save: bool = True, fn_prefix: str | Path | None = None) Noneο
Produces a figure containing subplots of the predicted label mask, the ground truth label mask and a reference RGB image of the same patch.
- Parameters:
sample (dict[str, Any]) β Dictionary holding the
"image", ground truth ("mask") and predicted ("prediction") masks and the bounding box for this sample.sample_id (str) β ID for the sample.
classes (dict[int, str]) β Dictionary mapping class labels to class names.
src_crs (CRS) β Existing co-ordinate system of the image.
new_crs (CRS) β Optional; Co-ordinate system to convert image to and use for labelling.
exp_id (str) β Optional; Unique ID for the experiment run that predictions and labels come from.
block_size (int) β Optional; Size of block image sub-division in pixels.
cmap_style (str | ListedColormap) β Optional; Name or object for colour map style.
show (bool) β Optional; Show the figure when plotted.
save (bool) β Optional; Save the figure to file to
fn_prefix.fig_dim (tuple[float, float]) β Optional; Figure (height, width) in inches.
fn_prefix (str | Path) β Optional; Common filename prefix (including path to file) for all plots of this type from this experiment. Appended with the sample ID to give the filename to save the plot to.
- Returns:
None
- seg_plot(z: list[int] | ndarray[tuple[int, ...], dtype[Any]], y: list[int] | ndarray[tuple[int, ...], dtype[Any]], ids: list[str], index: Sequence[Any] | ndarray[tuple[int, ...], dtype[Any]], data_dir: Path | str, dataset_params: dict[str, Any], classes: dict[int, str], colours: dict[int, str], fn_prefix: str | Path | None, x: list[int] | ndarray[tuple[int, ...], dtype[Any]] | None = None, frac: float = 0.05, fig_dim: tuple[int | float, int | float] | None = (9.3, 10.5), model_name: str = '', path: str = '', max_pixel_value: int = 255, cache_dir: str | Path | None = None) Noneο
Custom function for pre-processing the outputs from image segmentation testing for data visualisation.
- Parameters:
z (list[float]) β Predicted segmentation masks by the network.
ids (list[str]) β Corresponding patch IDs for the test data supplied to the network.
bounds (list[BoundingBox] | ndarray[BoundingBox]) β Array of objects describing a geospatial bounding box. Must contain
minx,maxx,minyandmaxyparameters.task_name (str) β Name of the task that samples are from.
classes (dict[int, str]) β Dictionary mapping class labels to class names.
colours (dict[int, str]) β Dictionary mapping class labels to colours.
fn_prefix (str | Path) β Common filename prefix (including path to file) for all plots of this type from this experiment to use.
frac (float) β Optional; Fraction of patch samples to plot.
fig_dim (tuple[float, float]) β Optional; Figure (height, width) in inches.
cache_dir (str | Path) β Optional; Path to the directory to load the cached dataset from. Defaults to None (so will create dataset from scratch).
- Returns:
None