Skip to content

Commit

Permalink
use naive math
Browse files Browse the repository at this point in the history
  • Loading branch information
Emma Ai committed Aug 22, 2024
1 parent fe6baa6 commit 85fe055
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 69 deletions.
2 changes: 1 addition & 1 deletion docker/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ odc-dscache>=0.2.3
odc-stac @ git+https://github.com/opendatacube/odc-stac@69bdf64

# odc-stac is in PyPI
odc-stats[ows] @ git+https://github.com/opendatacube/odc-stats@84f0413
odc-stats[ows] @ git+https://github.com/opendatacube/odc-stats@fe6baa6

# For ML
tflite-runtime
Expand Down
160 changes: 92 additions & 68 deletions odc/stats/plugins/lc_treelite_cultivated.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,43 @@

# pylint: disable=invalid-name
def feature_MNDWI(nbart_green, nbart_swir_1):
return ne.evaluate(
"(a-b)/(a+b)",
local_dict={
"a": nbart_green,
"b": nbart_swir_1,
},
).astype("float32")
return (nbart_green - nbart_swir_1) / (nbart_green + nbart_swir_1)
# return ne.evaluate(
# "(a-b)/(a+b)",
# local_dict={
# "a": nbart_green,
# "b": nbart_swir_1,
# },
# ).astype("float32")


def feature_BUI(nbart_swir_1, nbart_nir, nbart_red):
return ne.evaluate(
"((a-b)/(a+b))-((b-c)/(b+c))",
local_dict={
"a": nbart_swir_1,
"b": nbart_nir,
"c": nbart_red,
},
).astype("float32")
return ((nbart_swir_1 - nbart_nir) / (nbart_swir_1 + nbart_nir)) - (
(nbart_nir - nbart_red) / (nbart_nir + nbart_red)
)
# return ne.evaluate(
# "((a-b)/(a+b))-((b-c)/(b+c))",
# local_dict={
# "a": nbart_swir_1,
# "b": nbart_nir,
# "c": nbart_red,
# },
# ).astype("float32")


def feature_BSI(nbart_swir_1, nbart_red, nbart_nir, nbart_blue):
return ne.evaluate(
"((a+b)-(c+d))/((a+b)+(c+d))",
local_dict={
"a": nbart_swir_1,
"b": nbart_red,
"c": nbart_nir,
"d": nbart_blue,
},
).astype("float32")
return ((nbart_swir_1 + nbart_red) - (nbart_nir + nbart_blue)) / (
(nbart_swir_1 + nbart_red) + (nbart_nir + nbart_blue)
)
# return ne.evaluate(
# "((a+b)-(c+d))/((a+b)+(c+d))",
# local_dict={
# "a": nbart_swir_1,
# "b": nbart_red,
# "c": nbart_nir,
# "d": nbart_blue,
# },
# ).astype("float32")


def feature_TCW(
Expand All @@ -61,61 +68,78 @@ def feature_TCW(
nbart_swir_1,
nbart_swir_2,
):
return ne.evaluate(
"(0.0315*a+0.2021*b+0.3102*c+0.1594*d+-0.6806*e+-0.6109*f)",
local_dict={
"a": nbart_blue,
"b": nbart_green,
"c": nbart_red,
"d": nbart_nir,
"e": nbart_swir_1,
"f": nbart_swir_2,
},
).astype("float32")
return (
0.0315 * nbart_blue
+ 0.2021 * nbart_green
+ 0.3102 * nbart_red
+ 0.1594 * nbart_nir
+ -0.6806 * nbart_swir_1
+ -0.6109 * nbart_swir_2
)
# return ne.evaluate(
# "(0.0315*a+0.2021*b+0.3102*c+0.1594*d+-0.6806*e+-0.6109*f)",
# local_dict={
# "a": nbart_blue,
# "b": nbart_green,
# "c": nbart_red,
# "d": nbart_nir,
# "e": nbart_swir_1,
# "f": nbart_swir_2,
# },
# ).astype("float32")


def feature_NDMI(nbart_nir, nbart_swir_1):
return ne.evaluate(
"(a-b)/(a+b)",
local_dict={
"a": nbart_nir,
"b": nbart_swir_1,
},
).astype("float32")
return (nbart_nir - nbart_swir_1) / (nbart_nir + nbart_swir_1)
# return ne.evaluate(
# "(a-b)/(a+b)",
# local_dict={
# "a": nbart_nir,
# "b": nbart_swir_1,
# },
# ).astype("float32")


def feature_AWEI_sh(nbart_blue, nbart_green, nbart_nir, nbart_swir_1, nbart_swir_2):
return ne.evaluate(
"(a+2.5*b-1.5*(c+d)-0.25*e)",
local_dict={
"a": nbart_blue,
"b": nbart_green,
"c": nbart_nir,
"d": nbart_swir_1,
"e": nbart_swir_2,
},
).astype("float32")
return (
nbart_blue
+ 2.5 * nbart_green
- 1.5 * (nbart_nir + nbart_swir_1)
- 0.25 * nbart_swir_2
)
# return ne.evaluate(
# "(a+2.5*b-1.5*(c+d)-0.25*e)",
# local_dict={
# "a": nbart_blue,
# "b": nbart_green,
# "c": nbart_nir,
# "d": nbart_swir_1,
# "e": nbart_swir_2,
# },
# ).astype("float32")


def feature_BAEI(nbart_red, nbart_green, nbart_swir_1):
return ne.evaluate(
"(a+0.3)/(b+c)",
local_dict={
"a": nbart_red,
"b": nbart_green,
"c": nbart_swir_1,
},
).astype("float32")
return (nbart_red + 0.3) / (nbart_green + nbart_swir_1)
# return ne.evaluate(
# "(a+0.3)/(b+c)",
# local_dict={
# "a": nbart_red,
# "b": nbart_green,
# "c": nbart_swir_1,
# },
# ).astype("float32")


def feature_NDSI(nbart_green, nbart_swir_1):
return ne.evaluate(
"(a-b)/(a+b)",
local_dict={
"a": nbart_green,
"b": nbart_swir_1,
},
).astype("float32")
return (nbart_green - nbart_swir_1) / (nbart_green + nbart_swir_1)
# return ne.evaluate(
# "(a-b)/(a+b)",
# local_dict={
# "a": nbart_green,
# "b": nbart_swir_1,
# },
# ).astype("float32")


def generate_features(input_block, bands_indices):
Expand Down Expand Up @@ -241,7 +265,7 @@ def aggregate_results(input_block, cultivated_value, natural_value):
local_dict={"m": input_block, "nodata": NODATA},
).astype("float32")

m_size = input_block.shape[-1]
m_size = output_block.shape[-1]

if m_size > 1:
output_block = output_block[..., -1]
Expand Down

0 comments on commit 85fe055

Please sign in to comment.