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