MCPcopy Create free account
hub / github.com/google-deepmind/dm_control / flatten_observation

Function flatten_observation

dm_control/rl/control.py:374–399  ·  view source on GitHub ↗

Flattens multiple observation arrays into a single numpy array. Args: observation: A mutable mapping from observation names to numpy arrays. output_key: The key for the flattened observation array in the output. Returns: A mutable mapping of the same type as `observation`. This wil

(observation, output_key=FLAT_OBSERVATION_KEY)

Source from the content-addressed store, hash-verified

372
373
374def flatten_observation(observation, output_key=FLAT_OBSERVATION_KEY):
375 """Flattens multiple observation arrays into a single numpy array.
376
377 Args:
378 observation: A mutable mapping from observation names to numpy arrays.
379 output_key: The key for the flattened observation array in the output.
380
381 Returns:
382 A mutable mapping of the same type as `observation`. This will contain a
383 single key-value pair consisting of `output_key` and the flattened
384 and concatenated observation array.
385
386 Raises:
387 ValueError: If `observation` is not a `collections.abc.MutableMapping`.
388 """
389 if not isinstance(observation, collections.abc.MutableMapping):
390 raise ValueError('Can only flatten dict-like observations.')
391
392 if isinstance(observation, collections.OrderedDict):
393 keys = observation.keys()
394 else:
395 # Keep a consistent ordering for other mappings.
396 keys = sorted(observation.keys())
397
398 observation_arrays = [observation[key].ravel() for key in keys]
399 return type(observation)([(output_key, np.concatenate(observation_arrays))])

Callers 3

resetMethod · 0.85
stepMethod · 0.85
observation_specMethod · 0.85

Calls 1

keysMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…