Apply a mapper/filter on a datapoint component. Note: 1. This dataflow itself doesn't modify the datapoints. But please make sure func doesn't modify its arguments in place, unless you're certain it's safe. 2. If you discard some datapoints, ``len(MapD
| 319 | |
| 320 | |
| 321 | class MapDataComponent(MapData): |
| 322 | """ |
| 323 | Apply a mapper/filter on a datapoint component. |
| 324 | |
| 325 | Note: |
| 326 | 1. This dataflow itself doesn't modify the datapoints. |
| 327 | But please make sure func doesn't modify its arguments in place, |
| 328 | unless you're certain it's safe. |
| 329 | 2. If you discard some datapoints, ``len(MapDataComponent(ds, ..))`` will be incorrect. |
| 330 | |
| 331 | Example: |
| 332 | |
| 333 | .. code-block:: none |
| 334 | |
| 335 | ds = Mnist('train') # each datapoint is [img, label] |
| 336 | ds = MapDataComponent(ds, lambda img: img * 255, 0) # map the 0th component |
| 337 | """ |
| 338 | def __init__(self, ds, func, index=0): |
| 339 | """ |
| 340 | Args: |
| 341 | ds (DataFlow): input DataFlow which produces either list or dict. |
| 342 | func (TYPE -> TYPE|None): takes ``dp[index]``, returns a new value for ``dp[index]``. |
| 343 | Return None to discard/skip this datapoint. |
| 344 | index (int or str): index or key of the component. |
| 345 | """ |
| 346 | self._index = index |
| 347 | self._func = func |
| 348 | super(MapDataComponent, self).__init__(ds, self._mapper) |
| 349 | |
| 350 | def _mapper(self, dp): |
| 351 | r = self._func(dp[self._index]) |
| 352 | if r is None: |
| 353 | return None |
| 354 | dp = copy(dp) # shallow copy to avoid modifying the datapoint |
| 355 | if isinstance(dp, tuple): |
| 356 | dp = list(dp) # to be able to modify it in the next line |
| 357 | dp[self._index] = r |
| 358 | return dp |
| 359 | |
| 360 | |
| 361 | class RepeatedData(ProxyDataFlow): |
no outgoing calls
no test coverage detected