spyndex.spyndex.computeIndex

spyndex.spyndex.computeIndex(index: str | List[str], params: dict | None = None, online: bool = False, returnOrigin: bool = True, coordinate: str = 'index', **kwargs) Any[source]

Computes one or more Spectral Indices from the Awesome Spectral Indices list.

Parameters:
  • index (str | list[str]) – Index or list of indices to compute. Check all available indices from the Awesome Spectral Indices Repository.

  • params (dict, default = None) – Parameters used as inputs for the computation. The input data must be compatible with Overloaded Operators. Some inputs’ types supported are pandas series, numpy arrays, xarray objects and numeric objects. Earth Engine objects are also compatible when using eemont.

  • online (bool, default = False) – Whether to retrieve the most recent list of indices directly from the GitHub repository and not from the local copy.

  • returnOrigin (bool, default = True) –

    Whether to return multiple indices as an object with the same type as the inputs.
    • pandas.Series: Returns a pandas.DataFrame.

    • numpy.ndarray: Returns a numpy.ndarray.

    • xarray.DataArray: Returns a xarray.DataArray.

    • ee.Image: Returns a ee.Image.

    • ee.Number: Returns a ee.List.

    • dask.Array: Returns a dask.Array.

    • dask.Series: Returns a dask.DataFrame.

    When numeric objects are used in combination with other objects, the type of the other object is returned. If false, a list is returned.

  • coordinate (str, default = "index") – Name of the coordinate used to concatenate xarray.DataArray objects when returnOrigin = True.

  • kwargs

    Parameters used as inputs for the computation as keyword pairs. Ignored when params is defined.

    New in version 0.0.5.

Returns:

Computed Spectral Indices according to the inputs’ type.

Return type:

Any

See also

computeKernel

Computes a kernel k(a,b).

Examples

Compute a Spectral Index by passing the required params dictionary:

>>> import spyndex
>>> spyndex.computeIndex(
...     index = "NDVI",
...     params = {
...         "N": 0.643,
...         "R": 0.175
...     }
... )
0.5721271393643031

Compute a Spectral Index by passing the required params as keyword pairs:

>>> spyndex.computeIndex("NDVI",N = 0.643,R = 0.175)
0.5721271393643031

Two or more Spectral Indices can also be computed:

>>> spyndex.computeIndex(
...     index = ["NDVI","SAVI"],
...     params = {
...         "N": 0.643,
...         "R": 0.175,
...         "L": 0.5
...     }
... )
[0.5721271393643031, 0.5326251896813354]

Spyndex is versatile! Let’s compute Spectral Indices from a numpy.ndarray:

>>> import numpy as np
>>> R = np.random.normal(0.12,0.05,10000)
>>> G = np.random.normal(0.34,0.07,10000)
>>> N = np.random.normal(0.67,0.12,10000)
>>> spyndex.computeIndex(
...     index = ["NDVI","SAVI","GNDVI"],
...     params = {
...         "N": N,
...         "R": R,
...         "G": G,
...         "L": 0.5
...     }
... )
array([[0.57190873, 0.63776266, 0.52554653, ..., 0.692647  , 0.72013087,
        0.57576994],
       [0.5494994 , 0.60604837, 0.47157809, ..., 0.60647869, 0.65887439,
        0.52585032],
       [0.33304486, 0.46408771, 0.28007567, ..., 0.35734698, 0.28536337,
        0.50212151]])

Now, let’s try a pandas.DataFrame:

>>> import pandas as pd
>>> df = pd.DataFrame({"Red":R,"Green":G,"NIR":N})
>>> spyndex.computeIndex(
...     index = ["NDVI","SAVI","GNDVI"],
...     params = {
...         "N": df["NIR"],
...         "R": df["Red"],
...         "G": df["Green"],
...         "L": 0.5
...     }
... )
        NDVI      SAVI     GNDVI
0     0.571909  0.549499  0.333045
1     0.637763  0.606048  0.464088
2     0.525547  0.471578  0.280076
3     0.498328  0.443842  0.514775
4     0.625445  0.512757  0.227829
...        ...       ...       ...
9995  0.706123  0.604131  0.233519
9996  0.731205  0.630090  0.389462
9997  0.692647  0.606479  0.357347
9998  0.720131  0.658874  0.285363
9999  0.575770  0.525850  0.502122

What about a xarray.DataArray?

>>> import xarray as xr
>>> da = xr.DataArray(np.array([G,R,N]).reshape(3,100,100),
...     dims = ("band","x","y"),
...     coords = {"band": ["Green","Red","NIR"]})
>>> spyndex.computeIndex(
...     index = ["NDVI","SAVI","GNDVI"],
...     params = {
...         "N": da.sel(band = "NIR"),
...         "R": da.sel(band = "Red"),
...         "G": da.sel(band = "Green"),
...         "L": 0.5
...     }
... )
<xarray.DataArray (index: 3, x: 100, y: 100)>
Coordinates:
    * index    (index) <U5 'NDVI' 'SAVI' 'GNDVI'
Dimensions without coordinates: x, y

Now let’s try dask!

>>> import dask.array as da
>>> array_shape = (10000,10000)
>>> chunk_size = (1000,1000)
>>> dask_array = da.array([
...     da.random.normal(0.6,0.10,array_shape,chunks = chunk_size),
...     da.random.normal(0.1,0.05,array_shape,chunks = chunk_size),
...     da.random.normal(0.3,0.02,array_shape,chunks = chunk_size)
... ])
>>> spyndex.computeIndex(
...     index = ["NDVI","SAVI","GNDVI"],
...     params = {
...         "N": dask_array[0],
...         "R": dask_array[1],
...         "G": dask_array[2],
...         "L": 0.5
...     }
... ).compute()

And a dask.DataFrame?

>>> import dask.dataframe as dd
>>> df = pd.DataFrame({
...     "NIR": np.random.normal(0.6,0.10,1000),
...     "RED": np.random.normal(0.1,0.05,1000),
...     "GREEN": np.random.normal(0.3,0.02,1000),
... })
>>> df = dd.from_pandas(df,npartitions = 10)
>>> spyndex.computeIndex(
...     index = ["NDVI","SAVI","GNDVI"],
...     params = {
...         "N": df["NIR"],
...         "R": df["RED"],
...         "G": df["GREEN"],
...         "L": 0.5
...     }
... ).compute()