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)
| 372 | |
| 373 | |
| 374 | def 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))]) |
no test coverage detected
searching dependent graphs…