Skip to content

Commit

Permalink
remove hardcoding from std_ndvi_vals()
Browse files Browse the repository at this point in the history
  • Loading branch information
ericgitonga committed Oct 16, 2023
1 parent ebd1dd5 commit a265e47
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
31 changes: 20 additions & 11 deletions ecoscope/analysis/seasons.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,32 @@ def _min_max_scaler(x):
return x_std


def std_ndvi_vals(aoi=None, start=None, end=None):
coll = ee.ImageCollection("MODIS/MCD43A4_006_NDVI").select("NDVI").filterDate(start, end)

ndvi_vals = (
coll.toBands()
.reduceRegion("mean", ee.Feature(shapely.geometry.mapping(aoi)).geometry(), bestEffort=True)
.values()
.getInfo()
def std_ndvi_vals(aoi=None, img_coll=None, band=None, img_scale=1, start=None, end=None):

coll = (
ee.ImageCollection(img_coll)
.select(band)
.filterDate(start, end)
.map(lambda x: x.multiply(ee.Image(img_scale)).set("system:time_start", x.get("system:time_start")))
)
ndvi_vals_std = _min_max_scaler(ndvi_vals)
return pd.DataFrame(

if aoi:
geo = ee.Feature(shapely.geometry.mapping(aoi)).geometry()
else:
geo = None

ndvi_vals = coll.toBands().reduceRegion("mean", geo, bestEffort=True).values().getInfo()

df = pd.DataFrame(
{
"img_date": pd.to_datetime(coll.aggregate_array("system:time_start").getInfo(), unit="ms", utc=True),
"NDVI": ndvi_vals_std,
"NDVI": ndvi_vals,
}
).dropna(axis=0)

df["NDVI"] = _min_max_scaler(df["NDVI"])
return df


def val_cuts(vals, num_seasons=2):
distr = sklearn.mixture.GaussianMixture(n_components=num_seasons, max_iter=500)
Expand Down
2 changes: 1 addition & 1 deletion ecoscope/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.4.4"
__version__ = "1.4.5"

0 comments on commit a265e47

Please sign in to comment.