bands
darts_utils.bands
¶
Hardcoded band information for encoding/decoding and normalization.
_supported_dtypes
module-attribute
¶
_supported_dtypes = [
"bool",
"int8",
"int16",
"int32",
"int64",
"uint8",
"uint16",
"uint32",
"uint64",
"float32",
"float64",
]
manager
module-attribute
¶
manager = darts_utils.bands.BandManager(
{
"blue": darts_utils.bands.BandCodec.optical(),
"red": darts_utils.bands.BandCodec.optical(),
"green": darts_utils.bands.BandCodec.optical(),
"nir": darts_utils.bands.BandCodec.optical(),
"s2_scl": darts_utils.bands.BandCodec.mask(11),
"planet_udm": darts_utils.bands.BandCodec.mask(8),
"quality_data_mask": darts_utils.bands.BandCodec.mask(
2
),
"dem": darts_utils.bands.BandCodec(
disk_dtype="float32",
memory_dtype="float32",
valid_range=(-100, 3000),
scale_factor=0.1,
offset=-100.0,
fill_value=-1,
),
"arcticdem_data_mask": darts_utils.bands.BandCodec(
disk_dtype="bool",
memory_dtype="uint8",
valid_range=(0, 1),
),
"tc_brightness": darts_utils.bands.BandCodec.tc(),
"tc_greenness": darts_utils.bands.BandCodec.tc(),
"tc_wetness": darts_utils.bands.BandCodec.tc(),
"ndvi": darts_utils.bands.BandCodec.ndi(),
"relative_elevation": darts_utils.bands.BandCodec(
disk_dtype="int16",
memory_dtype="float32",
valid_range=(-50, 50),
scale_factor=100 / 30000,
offset=-50.0,
fill_value=-1,
),
"slope": darts_utils.bands.BandCodec(
disk_dtype="int16",
memory_dtype="float32",
valid_range=(0, 90),
scale_factor=1 / 100,
offset=0.0,
fill_value=-1,
),
"aspect": darts_utils.bands.BandCodec(
disk_dtype="int16",
memory_dtype="float32",
valid_range=(0, 360),
scale_factor=1 / 10,
offset=0.0,
fill_value=-1,
),
"hillshade": darts_utils.bands.BandCodec(
disk_dtype="int16",
memory_dtype="float32",
valid_range=(0, 1),
scale_factor=1 / 10000,
offset=0.0,
fill_value=-1,
),
"curvature": darts_utils.bands.BandCodec.ndi(),
"probabilities": darts_utils.bands.BandCodec.percentage(),
"probabilities-*": darts_utils.bands.BandCodec.percentage(),
"binarized_segmentation": darts_utils.bands.BandCodec.bool(),
"binarized_segmentation-*": darts_utils.bands.BandCodec.bool(),
"extent": darts_utils.bands.BandCodec.bool(),
}
)
BandCodec
dataclass
¶
BandCodec(
disk_dtype: str,
memory_dtype: str,
valid_range: tuple[float | int, float | int],
scale_factor: float | None = None,
offset: float | None = None,
fill_value: float | int | None = None,
)
Encoding / Decoding information for a single band (channel).
Stores information about how to convert data between the three different representations:
- memory: the in-memory representation of the data (native)
- disk: the on-disk representation of the data (best compression)
- model: the representation used for model training & inference (normalized between 0 and 1)
In general the "default" representation is the memory representation and is further referred to as "decoded". The disk and model representations are referred to as "encoded", both with their own scale factors and offsets. The scale and offset of the disk representation must be chosen such that the encoded values fit into the disk dtype. The model representation is normalized to the range [0, 1] using the valid range of the memory representation. The formulas used are based on the NetCDF conventions for encoding and decoding data used by Xarray: https://docs.xarray.dev/en/stable/user-guide/io.html#scaling-and-type-conversions
bool
classmethod
¶
bool() -> darts_utils.bands.BandCodec
Create a BandCodec for boolean bands.
Boolean bands are represented as bool
in memory and on disk, with a valid range of (False, True).
They do not have a scale factor or offset, and the fill value is always None.
Returns:
-
BandCodec
(darts_utils.bands.BandCodec
) –A BandCodec instance for boolean bands.
Source code in darts-utils/src/darts_utils/bands.py
mask
classmethod
¶
mask(vmax: int) -> darts_utils.bands.BandCodec
Create a BandCodec for non-binary masks.
Assumes the mask always start with 0
Parameters:
-
vmax
(int
) –Maximum value of the mask
Returns:
-
BandCodec
(darts_utils.bands.BandCodec
) –A BandCodec instance for non-binary masks.
Source code in darts-utils/src/darts_utils/bands.py
ndi
classmethod
¶
ndi() -> darts_utils.bands.BandCodec
Create a BandCodec for Normalized Difference Index (NDI) bands.
NDI bands are represented as float32
in memory and as int16
on disk,
with a valid range of (-1.0, 1.0) in memory and (0, 20000) on disk with -1 as NoData.
Returns:
-
BandCodec
(darts_utils.bands.BandCodec
) –A BandCodec instance for NDI bands.
Source code in darts-utils/src/darts_utils/bands.py
optical
classmethod
¶
optical() -> darts_utils.bands.BandCodec
Create a BandCodec for optical satellite imagery.
Optical imagery bands are represented as float32
in memory and as uint16
on disk,
with a valid range of (-0.1, 0.5), in memory and (0, 6000) on disk with 0 as NoData.
Theoretically, the valid range of optical bands is between 0 and 1, but in practice,
values above 0.5 are very rare in cloud and snowless regions.
Further, the new Sentinel 2 L2A products allow for negative values due to atmospheric correction.
Please see the documentation about bands for caveats with optical data.
Returns:
-
BandCodec
(darts_utils.bands.BandCodec
) –A BandCodec instance for optical bands.
Source code in darts-utils/src/darts_utils/bands.py
percentage
classmethod
¶
percentage() -> darts_utils.bands.BandCodec
Create a BandCodec for percentage bands.
Percentage bands are represented as float32
in memory and as uint8
on disk,
with a valid range of (0.0, 1.0) in memory and (0, 100) on disk with 255 as NoData.
Returns:
-
BandCodec
(darts_utils.bands.BandCodec
) –A BandCodec instance for percentage bands.
Source code in darts-utils/src/darts_utils/bands.py
tc
classmethod
¶
tc() -> darts_utils.bands.BandCodec
Create a BandCodec for tcvis data.
TCVis bands are represented as uint8
in memory and on dask, utilizing the complete 0-255 range.
There are no NoData values.
Returns:
-
BandCodec
(darts_utils.bands.BandCodec
) –A BandCodec instance for TCVis bands.
Source code in darts-utils/src/darts_utils/bands.py
validate
¶
validate() -> str | None
Validate the codec configuration.
Checks if the disk representation's valid range fits within the limits of the disk dtype, and if the model representation's valid range is normalized between 0 and 1.
Further checks the validity of the data types for scale factor, offset, and fill value.
Returns:
-
str | None
–str | None: Reason for invalid configuration if any, otherwise None.
Source code in darts-utils/src/darts_utils/bands.py
BandManager
dataclass
¶
BandManager(codecs: dict[str, darts_utils.bands.BandCodec])
Meta class for loading, storing and encoding xarray datasets based on band codecs.
Supports wildcard patterns for band names, e.g. "probabilities_*" to match all dataset variables starting with "probabilities_".
__contains__
¶
__getitem__
¶
__getitem__(
selector: list[str] | str,
) -> (
dict[str, darts_utils.bands.BandCodec]
| darts_utils.bands.BandCodec
)
Get a subset of codecs by band names or a single codec by band name.
Parameters:
Returns:
-
dict[str, darts_utils.bands.BandCodec] | darts_utils.bands.BandCodec
–dict[str, BandCodec]: A dictionary of selected codecs.
Raises:
-
KeyError
–If the band name is not found in the codecs.
Source code in darts-utils/src/darts_utils/bands.py
__iter__
¶
crop
¶
Crop the dataset to the valid range of each band.
Clips each band in the dataset to its valid range defined in the codec. This is useful for ensuring that the data fits into the encoding.
Inplace operation
This operation happens inplace - hence the data of the input dataset is changed.
Parameters:
Returns:
Source code in darts-utils/src/darts_utils/bands.py
get
¶
get(selector: str) -> darts_utils.bands.BandCodec | None
Get a codec by band name.
Parameters:
-
selector
(str
) –The band name to select.
Returns:
-
darts_utils.bands.BandCodec | None
–BandCodec | None: The codec for the band, or None if not found.
Source code in darts-utils/src/darts_utils/bands.py
normalize
¶
Normalize the dataset to the model representation.
Applies the normalization formula to each band in the dataset based on the codec configuration.
Leaves boolean bands as they are, just converting them to float32. Also fills NaN values with 0.0 after normalization. All other bands are normalized to the range [0, 1] using the valid range of the memory representation and converted to float32.
Parameters:
Returns:
Raises:
-
ValueError
–If the dataset has unknown bands / channels.
Source code in darts-utils/src/darts_utils/bands.py
open
¶
Load a dataset from a NetCDF file.
Parameters:
Returns:
Source code in darts-utils/src/darts_utils/bands.py
to_netcdf
¶
Store the dataset to a NetCDF file.
Parameters:
-
dataset
(xarray.Dataset
) –The dataset to store.
-
path
(pathlib.Path | str
) –The path to the NetCDF file.
-
crop
(bool
, default:True
) –Whether to crop the dataset to the valid range. This happens inplace! Defaults to True.
Source code in darts-utils/src/darts_utils/bands.py
validate
¶
Validate all codecs in the manager.
Iterates through all codecs and checks if they are valid.
Raises:
-
ValueError
–If any codec is invalid, with a message indicating the band and reason for