s-expressions using snuggs as Custom Algorithm - how to create an ImageData object ? #639
-
snuggs could be a nice addition to the Custom Algorithm, I'm wondering how I can create an ImageData object for development/testing purposes using a STAC Item as input to generate the ImageData and then implement to Custom Algorithm |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
@fabricebrito I'm not quite sure to fully understand what you want exactly. I also wonder what snuggs does that numexpr (expression option) can't do. as for creating a simple custom algorithm using snuggs I'm sure you could do it on your own application from titiler.core.algorithm import BaseAlgorithm
from rio_tiler.models import ImageData
class Snugg(BaseAlgorithm):
# Parameters
expression: str # There is no default, which means calls to this algorithm without any parameter will fail
# We don't set any metadata for this Algorithm
def __call__(self, img: ImageData) -> ImageData:
data = snuggs.eval(self.expression, data=data)
# Create output ImageData
return ImageData(
data,
img.mask,
assets=img.assets,
crs=img.crs,
bounds=img.bounds,
) then you'll need to from typing import Callable
from titiler.core.algorithm import algorithms as default_algorithms
from titiler.core.algorithm import Algorithms
from titiler.core.factory import TilerFactory
# Add the `Multiply` algorithm to the default ones
algorithms: Algorithms = default_algorithms.register({"snugg": Snugg})
# Create a PostProcessParams dependency
PostProcessParams: Callable = algorithms.dependency
endpoints = TilerFactory(process_dependency=PostProcessParams) and use with something like: |
Beta Was this translation helpful? Give feedback.
-
"""titiler.core.algorithm s-expressions using snuggs."""
import numpy
import snuggs
from rio_tiler.models import ImageData
from titiler.core.algorithm.base import BaseAlgorithm
class SExpressions(BaseAlgorithm):
"""s-expressions using snuggs."""
# parameters
sexpression: str
# metadata
output_dtype: str = "float32"
output_nbands: int = 1
def __call__(self, img: ImageData) -> ImageData:
"""s-expressions using snuggs."""
ctx = {asset: img.data[img.band_names.index(asset)] for asset in img.band_names}
arr = snuggs.eval(self.sexpression, **ctx)
# ImageData only accept image in form of (count, height, width)
arr = numpy.expand_dims(arr, axis=0).astype(self.output_dtype)
return ImageData(
arr,
img.mask,
assets=img.assets,
crs=img.crs,
bounds=img.bounds,
) invoke with: import json
import httpx
stac_url = "https://earth-search.aws.element84.com/v0/collections/sentinel-s2-l2a-cogs/items/S2B_10TFK_20210713_0_L2A"
x, y, z = 166, 385, 10
httpx.get(
f"http://127.0.0.1:8000/stac/tiles/{z}/{x}/{y}",
params={
"url": stac_url,
"assets": ["B08", "B04"],
"algorithm": "snuggs",
"asset_as_band": True,
"algorithm_params":json.dumps( {"sexpression": "(/ (- B08 B04) (+ B08 B04))"})
}, timeout=60
) |
Beta Was this translation helpful? Give feedback.
yes. but if you don't use
asset_as_band
the asset list will be in form ofred_b1, blue_b1