megnet.utils.general module

Operation utilities on lists and arrays

check_shape(array: Optional[numpy.ndarray], shape: Sequence) bool[source]

Check if array complies with shape. Shape is a sequence of integer that may end with None. If None is at the end of shape, then any shapes in array after that dimension will match with shape.

Example: array with shape [10, 20, 30, 40] matches with [10, 20, None], but

does not match with shape [10, 20, 30, 20]

Parameters
  • array (np.ndarray or None) – array to be checked

  • shape (Sequence) – integer array shape, it may ends with None

Returns: bool

expand_1st(x: numpy.ndarray) numpy.ndarray[source]

Adding an extra first dimension

Parameters

x – (np.array)

Returns

(np.array)

fast_label_binarize(value: List, labels: List) List[int][source]

Faster version of label binarize

label_binarize from scikit-learn is slow when run 1 label at a time. label_binarize also is efficient for large numbers of classes, which is not common in megnet

Parameters
  • value – Value to encode

  • labels (list) – Possible class values

Returns

List of integers

Return type

([int])

reshape(array: numpy.ndarray, shape: Sequence) numpy.ndarray[source]

Take an array and reshape it according to shape. Here shape may contain None field at the end.

if array shape is [3, 4] and shape is [3, 4, None], then array is shaped to [3, 4, 1]. If the two shapes do not match then report an error

Parameters
  • array (np.ndarray) – array to be reshaped

  • shape (Sequence) – shape dimensions

Returns: np.ndarray, reshaped array

to_list(x: Union[collections.abc.Iterable, numpy.ndarray]) List[source]

If x is not a list, convert it to list