Utilities (deepdish.util)

deepdish.util.pad(data, padwidth, value=0.0)[source]

Pad an array with a specific value.

Parameters:
  • data (ndarray) – Numpy array of any dimension and type.
  • padwidth (int or tuple) – If int, it will pad using this amount at the beginning and end of all dimensions. If it is a tuple (of same length as ndim), then the padding amount will be specified per axis.
  • value (data.dtype) – The value with which to pad. Default is 0.0.

Examples

>>> import deepdish as dd
>>> import numpy as np

Pad an array with zeros.

>>> x = np.ones((3, 3))
>>> dd.util.pad(x, (1, 2), value=0.0)
array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  1.,  1.,  1.,  0.,  0.],
       [ 0.,  0.,  1.,  1.,  1.,  0.,  0.],
       [ 0.,  0.,  1.,  1.,  1.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.]])
deepdish.util.pad_to_size(data, shape, value=0.0)[source]

This is similar to pad, except you specify the final shape of the array.

Parameters:
  • data (ndarray) – Numpy array of any dimension and type.
  • shape (tuple) – Final shape of padded array. Should be tuple of length data.ndim. If it has to pad unevenly, it will pad one more at the end of the axis than at the beginning. If a dimension is specified as -1, then it will remain its current size along that dimension.
  • value (data.dtype) – The value with which to pad. Default is 0.0. This can even be an array, as long as pdata[:] = value is valid, where pdata is the size of the padded array.

Examples

>>> import deepdish as dd
>>> import numpy as np

Pad an array with zeros.

>>> x = np.ones((4, 2))
>>> dd.util.pad_to_size(x, (5, 5))
array([[ 0.,  1.,  1.,  0.,  0.],
       [ 0.,  1.,  1.,  0.,  0.],
       [ 0.,  1.,  1.,  0.,  0.],
       [ 0.,  1.,  1.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.]])
deepdish.util.pad_repeat_border(data, padwidth)[source]

Similar to pad, except the border value from data is used to pad.

Parameters:
  • data (ndarray) – Numpy array of any dimension and type.
  • padwidth (int or tuple) – If int, it will pad using this amount at the beginning and end of all dimensions. If it is a tuple (of same length as ndim), then the padding amount will be specified per axis.

Examples

>>> import deepdish as dd
>>> import numpy as np

Pad an array by repeating its borders:

>>> shape = (3, 4)
>>> x = np.arange(np.prod(shape)).reshape(shape)
>>> dd.util.pad_repeat_border(x, 2)
array([[ 0,  0,  0,  1,  2,  3,  3,  3],
       [ 0,  0,  0,  1,  2,  3,  3,  3],
       [ 0,  0,  0,  1,  2,  3,  3,  3],
       [ 4,  4,  4,  5,  6,  7,  7,  7],
       [ 8,  8,  8,  9, 10, 11, 11, 11],
       [ 8,  8,  8,  9, 10, 11, 11, 11],
       [ 8,  8,  8,  9, 10, 11, 11, 11]])
deepdish.util.pad_repeat_border_corner(data, shape)[source]

Similar to pad_repeat_border, except the padding is always done on the upper end of each axis and the target size is specified.

Parameters:
  • data (ndarray) – Numpy array of any dimension and type.
  • shape (tuple) – Final shape of padded array. Should be tuple of length data.ndim. If it has to pad unevenly, it will pad one more at the end of the axis than at the beginning.

Examples

>>> import deepdish as dd
>>> import numpy as np

Pad an array by repeating its upper borders.

>>> shape = (3, 4)
>>> x = np.arange(np.prod(shape)).reshape(shape)
>>> dd.util.pad_repeat_border_corner(x, (5, 5))
array([[  0.,   1.,   2.,   3.,   3.],
       [  4.,   5.,   6.,   7.,   7.],
       [  8.,   9.,  10.,  11.,  11.],
       [  8.,   9.,  10.,  11.,  11.],
       [  8.,   9.,  10.,  11.,  11.]])
class deepdish.util.Saveable[source]

Key-value coding interface for classes. Generally, this is an interface that make it possible to access instance members through keys (strings), instead of through named variables. What this interface enables, is to save and load an instance of the class to file. This is done by encoding it into a dictionary, or decoding it from a dictionary. The dictionary is then saved/loaded using deepdish.io.save.

classmethod load(path)[source]

Loads an instance of the class from a file.

Parameters:path (str) – Path to an HDF5 file.

Examples

This is an abstract data type, but let us say that Foo inherits from Saveable. To construct an object of this class from a file, we do:

>>> foo = Foo.load('foo.h5') 
classmethod load_from_dict(d)[source]

Overload this function in your subclass. It takes a dictionary and should return a constructed object.

When overloading, you have to decorate this function with @classmethod.

Parameters:d (dict) – Dictionary representation of an instance of your class.
Returns:obj – Returns an object that has been constructed based on the dictionary.
Return type:object
save(path)[source]

Saves an instance of the class using deepdish.io.save.

Parameters:path (str) – Output path to HDF5 file.
save_to_dict()[source]

Overload this function in your subclass. It should return a dictionary representation of the current instance.

If you member variables that are objects, it is best to convert them to dictionaries before they are entered into your dictionary hierarchy.

Returns:d – Returns a dictionary representation of the current instance.
Return type:dict
class deepdish.util.NamedRegistry[source]

This class provides a named hierarchy of classes, where each class is associated with a string name.

classmethod construct(name, *args, **kwargs)[source]

Constructs an instance of an object given its name.

classmethod getclass(name)[source]

Returns the class object given its name.

name

Returns the name of the registry entry.

classmethod register(name)[source]

Decorator to register a class.

classmethod root(reg_cls)[source]

Decorate your base class with this, to create a new registry for it

class deepdish.util.SaveableRegistry[source]

This combines the features of deepdish.util.Saveable and deepdish.util.NamedRegistry.