Created
May 17, 2020 01:08
-
-
Save braised-babbage/1ab51f0e306e06d461f80e221a2e76f5 to your computer and use it in GitHub Desktop.
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
from collections import defaultdict | |
import numpy as np | |
from typing import Dict, Any, List, Union, Callable, Set, Iterable, Iterator, Optional, Tuple | |
from dataclasses import dataclass | |
@dataclass | |
class Image: | |
""" Representation of an image, along with metadata. """ | |
data: np.ndarray | |
header: Dict[str, Any] | |
@property | |
def shape(self) -> Tuple[int]: | |
return self.data.shape | |
def metadata(self, *keys: str) -> Tuple[Any]: | |
return tuple(self.header.get(key, None) for key in keys) | |
class ImageSet: | |
""" A queryable set of images. """ | |
def __init__(self, *imagesets: Union[Image, Iterable[Image]]): | |
self.images = [] | |
for ims in imagesets: | |
if isinstance(ims, Image): | |
self.images.append(ims) | |
else: | |
for im in ims: | |
self.images.append(im) | |
def __iter__(self) -> Iterator[Image]: | |
return iter(self.images) | |
def __len__(self) -> int: | |
return len(self.images) | |
def query(self, **kwargs) -> 'ImageSet': | |
""" Get a subset of images with metadata satisfying certain conditions. """ | |
def matches(image: Image) -> bool: | |
return image.metadata(*kwargs.keys()) == tuple(kwargs.values()) | |
return ImageSet([img for img in self if matches(img)]) | |
def __repr__(self): | |
return f"<ImageSet of size {len(self)}>" | |
@dataclass | |
class Transform: | |
""" A transformation mapping an ImageSet to an ImageSet. """ | |
transform_op: Callable[..., Union[Image, Iterable[Image]]] | |
partition_keys: Optional[List[str]] = None | |
def __call__(self, images: ImageSet) -> ImageSet: | |
def partition_value(image: Image): | |
if self.partition_keys is not None: | |
return image.metadata(*self.partition_keys) | |
else: | |
return id(image) | |
partition = defaultdict(list) | |
for image in images: | |
partition[partition_value(image)].append(image) | |
result = ImageSet([self.transform_op(*images) for images in partition.values()]) | |
return result | |
# Examples | |
a = Image(data=np.random.rand(10), header={'type': 'raw', 'segment': 10}) | |
b = Image(data=np.random.rand(10), header={'type': 'hr', 'segment': 10}) | |
c = Image(data=np.random.rand(10), header={'type': 'raw', 'segment': 12}) | |
d = Image(data=np.random.rand(10), header={'type': 'hr', 'segment': 12}) | |
images = ImageSet(a,b,c,d) | |
raw = images.query(type='raw') | |
doubled = ImageSet(images, images) | |
def increment_data(img: Image) -> Image: | |
return Image(data=img.data + 1.0, header=img.header) | |
def subtract_raw(*imgs: Image) -> List[Image]: | |
raws = [img for img in imgs if img.metadata('type') == ('raw',)] | |
assert len(raws) == 1 | |
raw, = raws | |
return [Image(data=img.data-raw.data, header=img.header) | |
for img in imgs if img.metadata('type') == ('hr',)] | |
IncrementTransform = Transform(transform_op=increment_data) | |
SubtractRaw = Transform(transform_op=subtract_raw, partition_keys=['segment']) | |
reduced = SubtractRaw(images) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment