Apply a mapper/filter on the datapoints of a DataFlow. Note: 1. 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(MapData(ds))`` will be incorrect. Example: .. code
| 285 | |
| 286 | |
| 287 | class MapData(ProxyDataFlow): |
| 288 | """ |
| 289 | Apply a mapper/filter on the datapoints of a DataFlow. |
| 290 | |
| 291 | Note: |
| 292 | 1. Please make sure func doesn't modify its arguments in place, |
| 293 | unless you're certain it's safe. |
| 294 | 2. If you discard some datapoints, ``len(MapData(ds))`` will be incorrect. |
| 295 | |
| 296 | Example: |
| 297 | |
| 298 | .. code-block:: none |
| 299 | |
| 300 | ds = Mnist('train') # each datapoint is [img, label] |
| 301 | ds = MapData(ds, lambda dp: [dp[0] * 255, dp[1]]) |
| 302 | """ |
| 303 | |
| 304 | def __init__(self, ds, func): |
| 305 | """ |
| 306 | Args: |
| 307 | ds (DataFlow): input DataFlow |
| 308 | func (datapoint -> datapoint | None): takes a datapoint and returns a new |
| 309 | datapoint. Return None to discard/skip this datapoint. |
| 310 | """ |
| 311 | super(MapData, self).__init__(ds) |
| 312 | self.func = func |
| 313 | |
| 314 | def __iter__(self): |
| 315 | for dp in self.ds: |
| 316 | ret = self.func(copy(dp)) # shallow copy the list |
| 317 | if ret is not None: |
| 318 | yield ret |
| 319 | |
| 320 | |
| 321 | class MapDataComponent(MapData): |
no outgoing calls
no test coverage detected