Skip to content

Spectral Indices

siapy.features.spectral_indices

get_spectral_indices

get_spectral_indices(bands_acronym: str | Iterable[str]) -> dict[str, SpectralIndex]
PARAMETER DESCRIPTION
bands_acronym

TYPE: str | Iterable[str]

Source code in siapy/features/spectral_indices.py
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def get_spectral_indices(
    bands_acronym: str | Iterable[str],
) -> dict[str, spyndex.axioms.SpectralIndex]:
    bands_acronym = _convert_str_to_list(bands_acronym)
    bands_acronym_set = set(bands_acronym)

    if not bands_acronym_set.issubset(list(spyndex.bands)):
        raise InvalidInputError(
            {
                "received_bands_acronym": bands_acronym_set,
                "valid_bands_acronym": list(spyndex.bands),
            },
            "Invalid input argument for 'bands_acronym'. Please ensure that all elements in 'bands_acronym' are valid band acronyms.",
        )

    spectral_indexes = {}
    for name in spyndex.indices.to_dict():
        index = spyndex.indices[name]
        if set(index.bands).issubset(bands_acronym_set):
            spectral_indexes[name] = index

    return spectral_indexes

compute_spectral_indices

compute_spectral_indices(data: DataFrame, spectral_indices: str | Iterable[str], bands_map: dict[str, str] | None = None, remove_nan_and_constants: bool = True) -> DataFrame
PARAMETER DESCRIPTION
data

TYPE: DataFrame

spectral_indices

TYPE: str | Iterable[str]

bands_map

TYPE: dict[str, str] | None DEFAULT: None

remove_nan_and_constants

TYPE: bool DEFAULT: True

Source code in siapy/features/spectral_indices.py
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
def compute_spectral_indices(
    data: pd.DataFrame,
    spectral_indices: str | Iterable[str],
    bands_map: dict[str, str] | None = None,
    remove_nan_and_constants: bool = True,
) -> pd.DataFrame:
    spectral_indices = _convert_str_to_list(spectral_indices)

    params = {}
    for band in data.columns:
        if bands_map is not None and band in bands_map.keys():
            if bands_map[band] not in list(spyndex.bands):
                raise InvalidInputError(
                    {
                        "received_band_mapping": bands_map[band],
                        "valid_bands_acronym": list(spyndex.bands),
                    },
                    f"Invalid band mapping is not a recognized band acronym. \n"
                    f"Received mapping: {band} -> {bands_map[band]}. \n"
                    "Please ensure that all values in 'bands_map' are valid band acronyms.",
                )
            params[bands_map[band]] = data[band]
        else:
            if band not in list(spyndex.bands):
                raise InvalidInputError(
                    {
                        "received_band": band,
                        "valid_bands_acronym": list(spyndex.bands),
                    },
                    f"Invalid band: '{band}' is not a recognized band acronym. \n"
                    "Please ensure that all columns in 'data' are valid band acronyms.",
                )
            params[band] = data[band]

    df = spyndex.computeIndex(index=list(spectral_indices), params=params)
    if remove_nan_and_constants:
        # Drop columns with inf or NaN values
        df = df.drop(df.columns[df.isin([np.inf, -np.inf, np.nan]).any()], axis=1)
        # Drop columns with constant values
        df = df.drop(df.columns[df.nunique() == 1], axis=1)
    return df