Base#

Base Container Object.

class ivy.data_classes.container.base.ContainerBase(dict_in=None, queues=None, queue_load_sizes=None, container_combine_method='list_join', queue_timeout=None, print_limit=10, key_length_limit=None, print_indent=4, print_line_spacing=0, ivyh=None, default_key_color='green', keyword_color_dict=None, rebuild_child_containers=False, types_to_iteratively_nest=None, alphabetical_keys=True, dynamic_backend=None, build_callable=False, **kwargs)[source]#

Bases: dict, ABC

__getitem__(query)[source]#

Get slice, key or key chain of container object.

Parameters:

str (query slice or) – slice object, key or key chain to query all container elements.

Returns:

Container object at desired query.

__init__(dict_in=None, queues=None, queue_load_sizes=None, container_combine_method='list_join', queue_timeout=None, print_limit=10, key_length_limit=None, print_indent=4, print_line_spacing=0, ivyh=None, default_key_color='green', keyword_color_dict=None, rebuild_child_containers=False, types_to_iteratively_nest=None, alphabetical_keys=True, dynamic_backend=None, build_callable=False, **kwargs)[source]#

Initialize container object from input dict representation.

Parameters:
  • dict_in (default: None) – the dictionary the container should wrap around. Default is None.

  • queues (default: None) – Sequence of multiprocessing queues, each of which returns containers. This enables the current container to be passed around asynchronously while waiting for data. Default is None.

  • queue_load_sizes (default: None) – Size of leading dimension of the containers returned by each queue. Default is None.

  • container_combine_method (default: 'list_join') – The method to use for combining containers arriving from different queues. Default is ivy.Container.cont_list_join

  • queue_timeout (default: None) – The timeout when waiting for containers to arrive from the queues. Default is global.

  • print_limit (default: 10) – The total array size limit when printing the container. Default is 10.

  • key_length_limit (default: None) – The maximum key length when printing the container. Default is None.

  • print_indent (default: 4) – The number of whitespaces to use for indenting when printing the container. Default is 4.

  • print_line_spacing (default: 0) – The number of extra newlines to use between keys when printing the container. Default is 0.

  • ivyh (default: None) – Handle to ivy module to use for the calculations. Default is None, which results in the global ivy.

  • default_key_color (default: 'green') – The default key color for printing the container to the terminal. Default is ‘green’.

  • keyword_color_dict (default: None) – A dict mapping keywords to their termcolor color codes for printing the container. (Default value = None)

  • rebuild_child_containers (default: False) – Whether to rebuild container found in dict_in with these constructor params. Default is False, in which case the original container are kept as are.

  • build_callable (default: False) – Whether to treat functions encountered at leaf nodes as further instructions to build the container

  • types_to_iteratively_nest (default: None) – The data types to nest iteratively in the dict structure, each type must be iterable. Default is None.

  • alphabetical_keys (default: True) – Whether to sort the container keys alphabetically, or preserve the dict order. Default is True.

  • kwargs – keyword arguments for dict creation. Default is None.

__setitem__(query, val)[source]#

Set key or key chain of container object.

Parameters:
  • str (query slice or) – slice object, key or key chain at which to set all container elements.

  • ivy.Container (val) – The value to set at the desired query.

  • array – The value to set at the desired query.

  • other (or) – The value to set at the desired query.

Returns:

New container after updating.

_abc_impl = <_abc._abc_data object>#
_cont_at_key_chains_input_as_dict(key_chains, current_chain='', ignore_key_errors=False)[source]#
_cont_at_key_chains_input_as_seq(key_chains, ignore_key_errors=False)[source]#
_cont_call_static_method_with_flexible_args(static_method, *args, kw, required, defaults, self_idx=0, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=None, out=None)[source]#
Return type:

Container

static _cont_concat_unify(containers, device, axis=0)[source]#
_cont_get_dev(as_native=False)[source]#
_cont_get_dtype()[source]#
_cont_get_shape()[source]#
_cont_get_shapes()[source]#
property _cont_ivy#
static _cont_mean_unify(containers, device, _=None, _1=None)[source]#
_cont_prune_key_chains_input_as_dict(key_chains, return_cont=None)[source]#
_cont_prune_key_chains_input_as_seq(key_chains)[source]#
_cont_slice_keys(key_slice)[source]#
static _cont_sum_unify(containers, device, _=None, _1=None)[source]#
_get_queue_item(query)[source]#
cont_all_false(assert_is_bool=False, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False)[source]#

Determine whether all the entries in the container boolean evaluate to False.

Parameters:
  • assert_is_bool (default: False) – Whether or not to assert each entry is of type Boolean. (Default value = False)

  • key_chains (default: None) – The key-chains to apply or not apply the method to. Default is None.

  • to_apply (default: True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default is True.

  • prune_unapplied (default: False) – Whether to prune key_chains for which the function was not applied. Default is False.

  • map_sequences (default: False) – Whether to also map method to sequences (lists, tuples). Default is False.

Returns:

Boolean, whether all entries are boolean False.

cont_all_key_chains(include_empty=False)[source]#
Parameters:

include_empty (default: False) – Default value = False)

cont_all_true(assert_is_bool=False, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False)[source]#

Determine whether all the entries in the container boolean evaluate to True.

Parameters:
  • assert_is_bool (default: False) – Whether or not to assert each entry is of type Boolean. (Default value = False)

  • key_chains (default: None) – The key-chains to apply or not apply the method to. Default is None.

  • to_apply (default: True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default is True.

  • prune_unapplied (default: False) – Whether to prune key_chains for which the function was not applied. Default is False.

  • map_sequences (default: False) – Whether to also map method to sequences (lists, tuples). Default is False.

Returns:

Boolean, whether all entries are boolean True.

cont_as_bools(assert_is_bool=False, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False)[source]#

Return boolean evaluation for all nested items in the container.

Parameters:
  • assert_is_bool (default: False) – Whether or not to assert the entry is of type Boolean. (Default value = False)

  • key_chains (default: None) – The key-chains to apply or not apply the method to. Default is None.

  • to_apply (default: True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default is True.

  • prune_unapplied (default: False) – Whether to prune key_chains for which the function was not applied. Default is False.

  • map_sequences (default: False) – Whether to also map method to sequences (lists, tuples). Default is False.

Returns:

Container object with all entries boolean evaluated.

cont_assert_contains_sub_container(sub_cont, partial=False)[source]#

Assert that the current container contains the sub-container, otherwise exception raised with the diff printed to screen.

Parameters:
  • sub_cont – The sub-container to check.

  • partial (default: False) – Whether to also check for partially complete sub-containers. Default is False.

cont_assert_contains_sub_structure(sub_cont, check_shapes=True, partial=False)[source]#

Assert that the current container contains the sub-container structure, otherwise exception raised with the diff printed to screen.

Parameters:
  • sub_cont – The sub-container to check.

  • check_shapes (default: True) – Whether to check array shapes in the sub-structure. Default is True.

  • partial (default: False) – Whether to also check for partially complete sub-containers. Default is False.

static cont_assert_identical(containers, check_types=True, check_shapes=True, same_arrays=True, arrays_equal=True, key_chains=None, to_apply=True, partial=False)[source]#

Assert whether the input containers are identical. Otherwise, the diff is shown in an exception.

Parameters:
  • containers – containers to check.

  • check_types (default: True) – Whether to check if the datatypes of the leaf nodes are the same. Default is True.

  • check_shapes (default: True) – Whether to check if the shapes of the leaf nodes are the same. Default is True.

  • same_arrays (default: True) – Whether to check if the arrays are the exact same instances. Default is True.

  • arrays_equal (default: True) – Whether to check if the arrays have equal values. Default is True.

  • key_chains (default: None) – The key-chains to apply or not apply the method to. Default is None.

  • to_apply (default: True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default is True.

  • partial (default: False) – Whether to also check for partially complete sub-containers. Default is False.

static cont_assert_identical_structure(containers, check_types=True, check_shapes=True, key_chains=None, to_apply=True, partial=False, assert_and_assign=False)[source]#

Assert whether the input containers have identical structure. Otherwise, the diff is shown in an exception.

Parameters:
  • containers – containers to check.

  • check_types (default: True) – Whether to also check whether the datatypes of the leaf nodes are the same. Default is True.

  • check_shapes (default: True) – Whether to also check whether the shapes of the leaf nodes are the same. Default is True.

  • key_chains (default: None) – The key-chains to apply or not apply the method to. Default is None.

  • to_apply (default: True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default is True.

  • partial (default: False) – Whether to also check for partially complete sub-containers. Default is False.

  • assert_and_assign (default: False) – if true, then the container being compared with is updated with the value in the container being compared to given that the structures are congruent

cont_at_key_chain(key_chain, ignore_key_errors=False)[source]#

Query container object at a specified key-chain.

Parameters:
  • key_chain – param ignore_key_errors: (Default value = False)

  • ignore_key_errors (default: False) – (Default value = False)

Returns:

ret – sub-container or value at specified key chain

cont_at_key_chains(key_chains, ignore_none=True, ignore_key_errors=False)[source]#

Query container object at specified key-chains, either as list or nested dict.

Parameters:
  • key_chains – param ignore_none: (Default value = True)

  • ignore_key_errors (default: False) – Default value = False)

  • ignore_none (default: True) – (Default value = True)

Returns:

type – sub-container containing only the specified key chains

cont_at_keys(queries, ignore_none=True, containing=False, ignore_key_errors=False)[source]#

Query container object at specified keys, either as list or nested dict.

Parameters:
  • queries – The keys to query.

  • ignore_none (default: True) – Whether to ignore None input. Default is True.

  • containing (default: False) – Whether to include keys which only contain the query substrings. Default is False.

  • ignore_key_errors (default: False) – Whether to ignore Key-errors when trying to access the dict. Default is False.

Returns:

sub-container containing only key-chains containing the specified keys.

static cont_combine(*containers, config=None)[source]#

Combine keys and values in a sequence of containers, with priority given to the right-most container in the case of duplicates.

Parameters:
  • containers – containers to compare

  • config (default: None) – The configuration for the containers. Default is the same as container_rightmost.

Returns:

Combined containers

static cont_common_key_chains(containers)[source]#

Return the key-chains common across all containers.

Parameters:

containers – Containers to check.

Returns:

list of key-chains.

property cont_config#
cont_contains_sub_container(sub_cont, partial=False)[source]#

Determine whether the current container contains the sub-container, with matching structure and array values.

Parameters:
  • sub_cont – The sub-container to check.

  • partial (default: False) – Whether to also check for partially complete sub-containers. Default is False.

Returns:

Bool

cont_contains_sub_structure(sub_cont, check_shapes=True, partial=False)[source]#

Determine whether the current container contains the sub-container structure.

Parameters:
  • sub_cont – The sub-container to check.

  • check_shapes (default: True) – Whether to check array shapes in the sub-structure. Default is True.

  • partial (default: False) – Whether to also check for partially complete sub-containers. Default is False.

cont_copy()[source]#

Create a copy of this container.

Returns:

A copy of the container

cont_create_if_absent(key, value, inplace=True)[source]#

Add a key to the container with corresponding value, if it is not already present. otherwise, do nothing.

Parameters:
  • key – param value:

  • inplace (default: True) – Default value = True)

  • value

cont_cutoff_at_depth(depth_cutoff, inplace=False)[source]#

Summary.

Parameters:
  • depth_cutoff – param inplace: (Default value = False)

  • inplace (default: False) – (Default value = False)

cont_cutoff_at_height(height_cutoff, inplace=False)[source]#

Summary.

Parameters:
  • height_cutoff – param inplace: (Default value = False)

  • inplace (default: False) – (Default value = False)

cont_deep_copy()[source]#

Create a deep copy (copying all internal tensors) of this container.

return: A deep copy of the container

property cont_dev#

The device to which the arrays in the container belong.

None returned if the devices are not consistent.

property cont_dev_str#

The device to which the arrays in the container belong.

None returned if the devices are not consistent.

static cont_diff(*containers, mode='all', diff_keys='diff', detect_key_diffs=True, detect_value_diffs=True, detect_shape_diffs=True, config=None)[source]#

Compare keys and values in a sequence of containers, returning the single shared values where they are the same, and new nested sub-dicts with all values where they are different.

Parameters:
  • containers – containers to compare

  • mode (default: 'all') – The mode of the diff operation, returning either all keys and values, only those that are consist across the containers, or only the differences. Default is all.

  • diff_keys (default: 'diff') – The key/keys to add to the returned container when differences are found. Default is "diff".

  • detect_key_diffs (default: True) – Whether to treat different keys as detected differences. If not, the keys among the input containers are simply combined without flagging differences. Default is True.

  • detect_value_diffs (default: True) – Whether to treat different values as detected differences. Default is True.

  • detect_shape_diffs (default: True) – Whether to treat different array shapes as detected differences. Default is True.

  • config (default: None) – The configuration for the containers. Default is the same as container0.

  • *containers

Returns:

Compared containers

property cont_dtype#

The dtype of the arrays in the container.

None is returned if the dtypes are not consistent.

cont_duplicate_array_keychains()[source]#
cont_find_sub_container(sub_cont_to_find, partial=False)[source]#

Find the sub-container in the current container if it exists.

Parameters:
  • sub_cont_to_find – The sub-container to find.

  • partial (default: False) – Whether to also check for partially complete sub-containers. Default is False.

cont_find_sub_structure(sub_struc_to_find, check_shapes=True, partial=False)[source]#

Find the sub-container structure in the current container if it exists.

Parameters:
  • sub_struc_to_find – The sub-container to find.

  • check_shapes (default: True) – Whether to check array shapes in the sub-structure. Default is True.

  • partial (default: False) – Whether to also check for partially complete sub-containers. Default is False.

static cont_flatten_key_chain(key_chain, replacement='__', above_height=None, below_depth=None)[source]#

Summary.

Parameters:
  • key_chain – param replacement: (Default value = ‘__’)

  • above_height (default: None) – Default value = None)

  • below_depth (default: None) – Default value = None)

  • replacement (default: '__') – (Default value = ‘__’)

cont_flatten_key_chains(include_empty=False, above_height=None, below_depth=None)[source]#

Summary.

Parameters:
  • include_empty (default: False) – Default value = False)

  • above_height (default: None) – Default value = None)

  • below_depth (default: None) – Default value = None)

cont_format_key_chains(format_fn)[source]#

Format all key-chains, using the formatting function.

Parameters:

format_fn

Returns:

ret – Container with the same key-chain structure, but the key strings formatted.

static cont_from_disk_as_hdf5(h5_obj_or_filepath, slice_obj=slice(None, None, None), alphabetical_keys=True, ivyh=None)[source]#

Load container object from disk, as an h5py file, at the specified hdf5 filepath.

Parameters:
  • h5_obj_or_filepath – Filepath where the container object is saved to disk, or h5 object.

  • slice_obj (default: slice(None, None, None)) – slice object to slice all h5 elements. (Default value = slice(None))

  • alphabetical_keys (default: True) – Whether to sort the container keys alphabetically, or preserve the dict order. Default is True.

  • ivyh (default: None) – Handle to ivy module to use for the calculations. Default is None, which results in the global ivy.

Returns:

Container loaded from disk

static cont_from_disk_as_json(json_filepath, ivyh=None)[source]#

Load container object from disk at the specified json filepath. If some objects were not json-able during saving, then they will be loaded as strings.

Parameters:
  • json_filepath – Filepath where the container object is saved to disk.

  • ivyh (default: None) – Handle to ivy module to use for the calculations. Default is None, which results in the global ivy.

Returns:

Container loaded from disk

static cont_from_disk_as_pickled(pickle_filepath, ivyh=None)[source]#

Load container object from disk at the specified pickle filepath.

Parameters:
  • pickle_filepath – Filepath where the container object is saved to disk.

  • ivyh (default: None) – Handle to ivy module to use for the calculations. Default is None, which results in the global ivy.

Returns:

Container loaded from disk

cont_from_flat_list(flat_list)[source]#

Return new container object with the same hierarchy, but with values replaced from flat list.

Parameters:

flat_list – flat list of values to populate container with.

Returns:

Container.

static cont_handle_inplace(ret, out)[source]#

Return an inplace update of out, provided it is not None, by updating with the values in ret.

Parameters:
  • ret – The container with the return values

  • out – The optional out container, which is primed for being overwritten if it exists

Returns:

The out container, but filled with the values from the ret container

cont_has_key(query_key)[source]#

Determine whether container object has specified key somewhere in the nested structure.

Parameters:

query_key

Returns:

ret – Boolean

cont_has_key_chain(key_chain)[source]#

Determine whether container object has specified key-chain.

Parameters:

key_chain

Returns:

ret – Boolean

static cont_identical(containers, check_types=True, check_shapes=True, same_arrays=True, arrays_equal=True, key_chains=None, to_apply=True, partial=False, key_chain='', assert_and_assign=False)[source]#

Return a single boolean as to whether the input containers have identical key- chains and data types.

Parameters:
  • containers – containers to check.

  • check_types (default: True) – Whether to check if the datatypes of the leaf nodes are the same. Default is True.

  • check_shapes (default: True) – Whether to check if the shapes of the leaf nodes are the same. Default is True.

  • same_arrays (default: True) – Whether to check if the arrays are the exact same instances. Default is True.

  • arrays_equal (default: True) – Whether to check if the arrays have equal values. Default is True.

  • key_chains (default: None) – The key-chains to apply or not apply the method to. Default is None.

  • to_apply (default: True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default is True.

  • partial (default: False) – Whether to also check for partially complete sub-containers. Default is False.

  • key_chain (default: '') – Chain of keys for this dict entry (Default value = ‘’)

  • assert_and_assign (default: False) – if true, then the container being compared with is updated with the value in the container being compared to given that the structures are congruent

Returns:

Boolean

static cont_identical_array_shapes(containers, exclusive=False)[source]#

Determine whether all of the containers have identical number of arrays and identical array shapes, regardless of their key-chain structures.

Parameters:
  • containers – containers to check.

  • exclusive (default: False) – Whether to check if the data type is exclusively an array, rather than a variable or traced array. (Default value = False)

Returns:

Boolean

static cont_identical_configs(containers)[source]#

Return a single boolean as to whether the input containers all have identical configs.

Parameters:

containers – containers to check.

static cont_identical_structure(containers, check_types=True, check_shapes=True, key_chains=None, to_apply=True, partial=False, key_chain='', assert_and_assign=False)[source]#

Return a single boolean as to whether the input containers have identical structure.

Parameters:
  • containers – containers to check.

  • check_types (default: True) – Whether to also check whether the datatypes of the leaf nodes are the same. Default is True.

  • check_shapes (default: True) – Whether to also check whether the shapes of the leaf nodes are the same. Default is True.

  • key_chains (default: None) – The key-chains to apply or not apply the method to. Default is None.

  • to_apply (default: True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default is True.

  • partial (default: False) – Whether to also check for partially complete sub-containers. Default is False.

  • key_chain (default: '') – Chain of keys for this dict entry (Default value = ‘’)

  • assert_and_assign (default: False) – if true, then the container being compared with is updated with the value in the container being compared to given that the structures are congruent

Returns:

Boolean

cont_if_exists(key)[source]#

Return the sub-container at the following key if it exists, otherwise None.

Parameters:

key

cont_inplace_update(dict_in, **config)[source]#

Update the contents of this container inplace, using either a new dict or container.

Parameters:
  • dict_in (Union[Container, dict]) – New dict or container to update the current container inplace with.

  • **config

Return type:

Container

property cont_ivy#
cont_key_chains_containing(sub_str, include_empty=False)[source]#
Parameters:
  • sub_str – param include_empty: (Default value = False)

  • include_empty (default: False) – (Default value = False)

static cont_list_join(containers, config=None)[source]#

Join containers of lists together along the specified dimension.

Parameters:
  • containers – containers to list join

  • config (default: None) – The configuration for the containers. Default is the same as container0.

Returns:

List joined containers, with each entry being a list of arrays

static cont_list_stack(containers, dim, config=None)[source]#

List stack containers together along the specified dimension.

Parameters:
  • containers – containers to list stack

  • dim – dimension along which to list stack

  • config (default: None) – The configuration for the containers. Default is the same as container0.

Returns:

Stacked containers, with each entry being a list of arrays

static cont_load(filepath, format='h5py')[source]#
cont_map(func, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, inplace=False, key_chain='')[source]#

Apply function to all array values of container.

Parameters:
  • func – Function to apply to each container entry

  • key_chains (default: None) – The key-chains to apply or not apply the method to. Default is None.

  • to_apply (default: True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default is True.

  • prune_unapplied (default: False) – Whether to prune key_chains for which the function was not applied. Default is False.

  • map_sequences (default: False) – Whether to also map method to sequences (lists, tuples). Default is False.

  • inplace (default: False) – Whether to apply the mapping inplace, or return a new container. Default is False.

  • key_chain (default: '') – Chain of keys for this dict entry (Default value = ‘’)

Returns:

New container following the function mapped to each sub-array.

cont_map_sub_conts(func, key_chains=None, to_apply=True, prune_unapplied=False, inplace=False, key_chain='', include_self=True)[source]#

Apply function to all sub-contains in the container.

Parameters:
  • func – Function to apply to each sub-container

  • key_chains (default: None) – The key-chains to apply or not apply the method to. Default is None.

  • to_apply (default: True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default is True.

  • prune_unapplied (default: False) – Whether to prune key_chains for which the function was not applied. Default is False.

  • inplace (default: False) – Whether to apply the mapping inplace, or return a new container. Default is False.

  • key_chain (default: '') – Chain of keys for this dict entry (Default value = ‘’)

  • include_self (default: True) – Whether to also apply the (possibly in-place) function to this container. Default is True.

Returns:

New container following the function mapped to each sub-container.

property cont_max_depth#
static cont_multi_map(func, containers, key_chains=None, to_apply=True, prune_unapplied=False, key_chain='', config=None, map_nests=False, assert_identical=False)[source]#

Apply function to all array values from a collection of containers.

Parameters:
  • func – Function to apply to each container entry.

  • containers – containers to map.

  • key_chains (default: None) – The key-chains to apply or not apply the method to. Default is None.

  • to_apply (default: True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default is True.

  • prune_unapplied (default: False) – Whether to prune key_chains for which the function was not applied, otherwise the leftmost container value is used. Default is False.

  • key_chain (default: '') – Chain of keys for this dict entry (Default value = ‘’)

  • config (default: None) – The configuration for the containers. Default is the same as container0.

  • map_sequences – Whether to also map method to sequences (lists, tuples). Default is False.

  • assert_identical (default: False) – Whether to assert that the input containers are identical or not.

Returns:

Container

static cont_multi_map_in_function(fn, *args, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=None, out=None, **kwargs)[source]#
Return type:

Union[Tuple[Container, Container], Container]

cont_num_arrays(exclusive=False)[source]#

Compute the number of arrays present at the leaf nodes, including variables by default.

Parameters:

exclusive (default: False) – Whether to check if the data type is exclusively an array, rather than a variable or traced array. (Default value = False)

cont_overwrite_at_key_chain(key_chain, val, inplace=False)[source]#

Overwrite value of container object at a specified key-chain.

Parameters:
  • key_chain – param val:

  • inplace (default: False) – Default value = False)

  • val

Returns:

ret – new container with updated value at key chain, provided it existed before.

cont_overwrite_at_key_chains(target_dict, return_dict=None, inplace=False)[source]#

Overwrite values of container object at specified key-chains.

Parameters:
  • target_dict – param return_dict: (Default value = None)

  • inplace (default: False) – Default value = False)

  • return_dict (default: None) – (Default value = None)

Returns:

ret – new container with updated values at the key chains, provided they existed before.

cont_prune_empty(keep_nones=False, base=True)[source]#

Recursively prunes empty keys from the container dict structure. Returns None if the entire container is empty.

Parameters:
  • keep_nones (default: False) – Default value = False)

  • base (default: True) – Default value = True)

Returns:

ret – Container with empty keys pruned.

cont_prune_key_chain(key_chain)[source]#

Recursively prune chain of keys, specified as ‘key1/key2/key3/…’.

Parameters:

key_chain

Returns:

ret – Container with keys in key chain pruned.

cont_prune_key_chains(key_chains, ignore_none=True)[source]#

Recursively prune set of key chains.

Parameters:
  • key_chains – param ignore_none: (Default value = True)

  • ignore_none (default: True) – (Default value = True)

Returns:

ret – Container with keys in the set of key chains pruned.

cont_prune_key_from_key_chains(absolute=None, containing=None)[source]#

Recursively prune absolute key or key containing a certain substring from all key chains.

Parameters:
  • absolute (default: None) – The absolute key to detect in the key chains. (Default value = None)

  • containing (default: None) – A substring to check each key for, when deciding which keys to prune. (Default value = None)

Returns:

Container with specified key or substring-containing-key from all key chains removed from the chain.

cont_prune_keys(query_keys, ignore_none=True)[source]#

Recursively prune set of keys.

Parameters:
  • query_keys – param ignore_none: (Default value = True)

  • ignore_none (default: True) – (Default value = True)

Returns:

ret – Container with key-chains containing the specified keys pruned.

cont_prune_keys_from_key_chains(absolute=None, containing=None)[source]#

Recursively prune absolute keys or keys containing certain substrings from all key chains.

Parameters:
  • absolute (default: None) – The absolute key to detect in the key chains. (Default value = None)

  • containing (default: None) – A substring to check each key for, when deciding which keys to prune. (Default value = None)

Returns:

Container with specified keys or substring-containing-keys from all key chains removed from the chain.

static cont_reduce(containers, reduction, config=None)[source]#

Reduce containers.

Parameters:
  • containers – containers to reduce

  • reduction – the reduction function

  • config (default: None) – The configuration for the containers. Default is the same as container0.

Returns:

reduced containers

cont_remove_key_length_limit(inplace=False)[source]#

Summary.

Parameters:

inplace (default: False) – Default value = False)

cont_remove_print_limit(inplace=False)[source]#

Summary.

Parameters:

inplace (default: False) – Default value = False)

cont_reshape_like(target_dict, leading_shape=None, return_cont=None)[source]#

Set shapes of container entries to shapes specified by new container with the same key structure.

Parameters:
  • target_dict – param leading_shape: (Default value = None)

  • return_cont (default: None) – Default value = None)

  • leading_shape (default: None) – (Default value = None)

Returns:

ret – new container with values of updated shapes

cont_restructure(mapping, keep_orig=True, replace=True)[source]#

Create a new container with the same contents, but a new key-chain structure, and transposes and/or reshaped arrays. Given by the mapping with keys as old key-chains and values as new key-chains.

Parameters:
  • mapping – A dict with keys as old key-chains and values as new key-chains.

  • keep_orig (default: True) – Whether to keep the original keys, are start from a new container. Default is True.

  • replace (default: True) – Whether to replace the old key-chains by the new ones. Default is True.

cont_restructure_key_chains(keychain_mapping, keep_orig=True, replace=True)[source]#

Create a new container with the same contents, but a new key-chain structure. Given by the mapping with keys as old key-chains and values as new key-chains.

Parameters:
  • keychain_mapping – A dict with keys as old key-chains and values as new key-chains.

  • keep_orig (default: True) – Whether to keep the original keys, or start from a new empty container. Default is True.

  • replace (default: True) – Whether to replace the old key-chains by the new ones. Default is True.

cont_save(filepath, format='h5py')[source]#
cont_set_at_key_chain(key_chain, val, inplace=False)[source]#

Set value of container object at a specified key-chain.

Parameters:
  • key_chain – param val:

  • inplace (default: False) – Default value = False)

  • val

Returns:

ret – new container with updated value at key chain

cont_set_at_key_chains(target_dict, return_dict=None, inplace=False)[source]#

Set values of container object at specified key-chains.

Parameters:
  • target_dict – param return_dict: (Default value = None)

  • inplace (default: False) – Default value = False)

  • return_dict (default: None) – (Default value = None)

Returns:

ret – new container with updated values at the key chains

cont_set_at_keys(target_dict)[source]#

Set values of container object at specified keys.

Parameters:

target_dict

Returns:

type – new container with updated value at each key

property cont_shape#

The shape of the arrays in the container.

None is placed in indices which are not consistent across arrays.

property cont_shapes#

The shapes of each array in the container.

None is placed in leaf entries without a shape attribute.

cont_show()[source]#
cont_show_sub_container(sub_cont_or_keychain)[source]#

Summary.

Parameters:

sub_cont_or_keychain

cont_size_ordered_arrays(exclusive=False)[source]#

Return a container with keychains mapped to flat keys, and arrays given in order of smallest to largest.

Parameters:

exclusive (default: False) – Whether to check if the data type is exclusively an array, rather than a variable or traced array. (Default value = False)

cont_slice_keys(key_slice, all_depths=False)[source]#

Summary.

Parameters:
  • key_slice – param all_depths: (Default value = False)

  • all_depths (default: False) – (Default value = False)

cont_slice_via_key(slice_key)[source]#

Get slice of container, based on key.

Parameters:

slice_key – key to slice container at.

Returns:

Container object sliced at desired key.

cont_sort_by_key()[source]#
static cont_structural_diff(*containers, mode='all', diff_keys='diff', detect_key_diffs=True, detect_shape_diffs=True, config=None)[source]#

Compare keys and shapes in a sequence of containers, returning the single shared values where they are the same, and new nested sub-dicts with all values where they are different.

Parameters:
  • containers – containers to compare

  • mode (default: 'all') – The mode of the diff operation, returning either all keys and values, only those that are consist across the containers, or only the differences. Default is all.

  • diff_keys (default: 'diff') – The key/keys to add to the returned container when differences are found. Default is “diff”.

  • detect_key_diffs (default: True) – Whether to treat different keys as detected differences. If not, the keys among the input containers are simply combined without flagging differences. Default is True.

  • detect_shape_diffs (default: True) – Whether to treat different array shapes as detected differences. Default is True.

  • config (default: None) – The configuration for the containers. Default is the same as container0.

  • *containers

Returns:

Compared containers

cont_to_dict()[source]#

Summary.

Returns:

ret Container as nested dict.

cont_to_disk_as_hdf5(h5_obj_or_filepath, starting_index=0, mode='a', max_batch_size=None)[source]#

Save container object to disk, as an h5py file, at the specified filepath.

Parameters:
  • h5_obj_or_filepath – Filepath for where to save the container to disk, or h5 object.

  • starting_index (default: 0) – Batch index for which to start writing to file, if it already exists (Default value = 0)

  • mode (default: 'a') – H5 read/write mode for writing to disk, [‘r’, ‘r+’, ‘w’, ‘w-’, ‘a’], default is ‘a’.

  • max_batch_size (default: None) – Maximum batch size for the container on disk, this is useful if later appending to file. (Default value = None)

cont_to_disk_as_json(json_filepath)[source]#

Save container object to disk, as an json file, at the specified filepath.

Parameters:

json_filepath – Filepath for where to save the container to disk.

cont_to_disk_as_pickled(pickle_filepath)[source]#

Save container object to disk, as an pickled file, at the specified filepath.

Parameters:

pickle_filepath – Filepath for where to save the container to disk.

cont_to_flat_list()[source]#

Summary.

Returns:

ret – Container as flat list.

cont_to_iterator(key_chain='', leaf_keys_only=False, include_empty=False)[source]#
Parameters:
  • key_chain (default: '') – Default value = ‘’)

  • leaf_keys_only (default: False) – Default value = False)

  • include_empty (default: False) – Default value = False)

Returns:

Iterator for the container elements.

cont_to_iterator_keys(key_chain='', leaf_keys_only=False, include_empty=False)[source]#
Parameters:
  • key_chain (default: '') – Default value = ‘’)

  • leaf_keys_only (default: False) – Default value = False)

  • include_empty (default: False) – Default value = False)

Returns:

Iterator for the container elements.

cont_to_iterator_values(include_empty=False)[source]#
Parameters:

include_empty (default: False) – Default value = False)

Returns:

Iterator for the container values.

cont_to_jsonable(return_dict=None)[source]#
Parameters:

return_dict (default: None) – Default value = None)

cont_to_nested_list()[source]#
cont_to_raw()[source]#

Convert container to its original form.

Returns:

ret – Container data in its raw form.

static cont_trim_key(key, max_length)[source]#

Summary. Returns a trimmed key with a maximum length of max_length.

Parameters:
  • key – key to trim

  • max_length – maximum length of key

cont_try_kc(key)[source]#

Try the following key or key chain, returning self if not present.

Parameters:

key

static cont_unify(containers, device, mode, axis=0)[source]#

Unify a list of containers, on arbitrary devices, to a single container on the specified device.

Parameters:
  • containers – containers to unify

  • dev – The device to unify the containers to.

  • mode – The mode by which to unify, must be one of [ concat | mean | sum ]

  • axis (default: 0) – The axis along which to concattenate the container, if concat mode is set. Default is 0.

Returns:

Unified container

cont_unstack_conts(axis, keepdims=False, dim_size=None)[source]#

Unstack containers along specified dimension.

Parameters:
  • axis – Dimensions along which to unstack.

  • keepdims (default: False) – Whether to keep dimension 1 in the unstack dimensions. Default is False.

  • dim_size (default: None) – Size of the dimension to unstack. Determined from inputs by default.

Returns:

List of containers, unstacked along the specified dimension.

cont_update_config(**config)[source]#
cont_with_default_key_color(default_key_color, inplace=False)[source]#

Summary.

Parameters:
  • default_key_color – param inplace: (Default value = False)

  • inplace (default: False) – (Default value = False)

cont_with_entries_as_lists()[source]#
cont_with_ivy_backend(ivy_backend, inplace=False)[source]#

Summary.

Parameters:
  • self – input Container

  • ivy_backend (str) – backend to use

  • inplace (default: False) – whether to modify the container or return a copy

cont_with_key_length_limit(key_length_limit, inplace=False)[source]#

Summary.

Parameters:
  • key_length_limit – param inplace: (Default value = False)

  • inplace (default: False) – (Default value = False)

cont_with_print_indent(print_indent, inplace=False)[source]#

Summary.

Parameters:
  • print_indent – param inplace: (Default value = False)

  • inplace (default: False) – (Default value = False)

cont_with_print_limit(print_limit, inplace=False)[source]#

Summary.

Parameters:
  • print_limit – param inplace: (Default value = False)

  • inplace (default: False) – (Default value = False)

cont_with_print_line_spacing(print_line_spacing, inplace=False)[source]#

Summary.

Parameters:
  • print_line_spacing – param inplace: (Default value = False)

  • inplace (default: False) – (Default value = False)

property dynamic_backend#
static h5_file_size(h5_obj_or_filepath)[source]#

Get file size of h5 file contents.

Parameters:

h5_obj_or_filepath – Filepath where the container object is saved to disk, or h5 object.

Returns:

Size of h5 file contents, and batch size.

static shuffle_h5_file(h5_obj_or_filepath, seed_value=0)[source]#

Shuffle entries in all datasets of h5 file, such that they are still aligned along axis 0.

Parameters:
  • h5_obj_or_filepath – Filepath where the container object is saved to disk, or h5 object.

  • seed_value (default: 0) – random seed to use for array shuffling (Default value = 0)

split_conts(num_or_size_splits=None, axis=0, with_remainder=False, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False)[source]#

Split a container into multiple sub-containers.

The function does that by splitting their constituent arrays.

Parameters:
  • num_or_size_splits (default: None) – Number of equal arrays to divide the array into along the given axis if an integer. The size of each split element if a sequence of integers. Default is to divide into as many 1-dimensional arrays as the axis dimension.

  • axis (default: 0) – The axis along which to split, default is 0.

  • with_remainder (default: False) – If the tensor does not split evenly, then store the last remainder entry. Default is False.

  • key_chains (default: None) – The key-chains to apply or not apply the method to. Default is None.

  • to_apply (default: True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default is True.

  • prune_unapplied (default: False) – Whether to prune key_chains for which the function was not applied. Default is False.

  • map_sequences (default: False) – Whether to also map method to sequences (lists, tuples). Default is False.

Returns:

A list of sub-arrays.

ivy.data_classes.container.base._is_jsonable(x)[source]#
ivy.data_classes.container.base._repr(x)[source]#