Last active
December 1, 2020 22:47
-
-
Save sixy6e/6542225ca20f9b7828797147d7664085 to your computer and use it in GitHub Desktop.
segments stats toy example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
A toy script to demonstrate generating statistics across array segments/blobs/labels. | |
The context for the example is 2D imagery. | |
It'll compare two methods: | |
1. reverse indices provided by <https://github.com/sixy6e/idl-functions> and wrapped by <https://github.com/sixy6e/image-processing> to for ease of use in an image processing context | |
2. loop through id's and do a bool lookup |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from datetime import datetime | |
from typing import Tuple | |
import numpy | |
import structlog | |
from image_processing.segmentation import Segments | |
DIMS = (8000, 8000) | |
BANDS = 4 | |
LOG = structlog.get_logger() | |
def data() -> Tuple[numpy.ndarray, numpy.ndarray]: | |
"""Generate sample data.""" | |
# simulate a segmented image with random ID's between 0 and 60000 | |
seg_ids = numpy.random.randint(0, 60001, DIMS) | |
# simulate some landsat spectral bands | |
spectral = numpy.random.ranf((BANDS, *DIMS)) | |
return seg_ids, spectral | |
def reverse_indices(): | |
"""Test segmentwise stats using a reverse indices method.""" | |
seg_ids, spectral = data() | |
start = datetime.now() | |
segments = Segments(seg_ids) | |
results = {f"band_{i}": segments.mean(spectral[i]) for i in range(BANDS)} | |
finish = datetime.now() | |
LOG.info("reverse_indices", time_taken=finish-start) | |
def basic_bool(): | |
""" | |
Test segmentwise the mean using a basic numpy bool method. | |
Also, only the first 50 segments will calculated. | |
""" | |
seg_ids, spectral = data() | |
start = datetime.now() | |
results = {} | |
for i in range(BANDS): | |
for seg in range(50): | |
results[f"band_{i}"] = [numpy.mean(spectral[i][seg_ids == seg])] | |
finish = datetime.now() | |
LOG.info("bool method for only 50 segments", time_taken=finish-start) | |
def main(): | |
""" main """ | |
LOG.info("running reverse indices") | |
reverse_indices() | |
LOG.info("running bool method") | |
basic_bool() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment