MCPcopy Index your code
hub / github.com/tensorpack/tensorpack / MapDataComponent

Class MapDataComponent

tensorpack/dataflow/common.py:321–358  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

319
320
321class 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
361class RepeatedData(ProxyDataFlow):

Callers 3

get_eval_dataflowFunction · 0.90
get_dataFunction · 0.85
get_dataFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected