A transform that's instantiated at the first call to `apply_image`.
| 316 | |
| 317 | |
| 318 | class LazyTransform(BaseTransform): |
| 319 | """ |
| 320 | A transform that's instantiated at the first call to `apply_image`. |
| 321 | """ |
| 322 | def __init__(self, get_transform): |
| 323 | """ |
| 324 | Args: |
| 325 | get_transform (img -> Transform): a function which will be used to instantiate a Transform. |
| 326 | """ |
| 327 | self.get_transform = get_transform |
| 328 | self._transform = None |
| 329 | |
| 330 | def apply_image(self, img): |
| 331 | if not self._transform: |
| 332 | self._transform = self.get_transform(img) |
| 333 | return self._transform.apply_image(img) |
| 334 | |
| 335 | def _apply(self, x, meth): |
| 336 | assert self._transform is not None, \ |
| 337 | "LazyTransform.{} can only be called after the transform has been applied on an image!" |
| 338 | return getattr(self._transform, meth)(x) |
| 339 | |
| 340 | def __getattr__(self, name): |
| 341 | if name.startswith("apply_"): |
| 342 | return lambda x: self._apply(x, name) |
| 343 | raise AttributeError("TransformList object has no attribute {}".format(name)) |
| 344 | |
| 345 | def __repr__(self): |
| 346 | if self._transform is None: |
| 347 | return "LazyTransform(get_transform={})".format(str(self.get_transform)) |
| 348 | else: |
| 349 | return repr(self._transform) |
| 350 | |
| 351 | __str__ = __repr__ |
| 352 | |
| 353 | def apply_coords(self, coords): |
| 354 | return self._apply(coords, "apply_coords") |
| 355 | |
| 356 | |
| 357 | if __name__ == '__main__': |
no outgoing calls
no test coverage detected
searching dependent graphs…