MCPcopy Create free account
hub / github.com/PyTables/PyTables / convert_to_np_atom

Function convert_to_np_atom

tables/utils.py:106–131  ·  view source on GitHub ↗

Convert a generic object into a NumPy object compliant with atom.

(
    arr: npt.ArrayLike, atom: Atom, copy: bool | None = copy_if_needed
)

Source from the content-addressed store, hash-verified

104# is assured that it will return a copy of the object and never the same
105# object or a new one sharing the same memory.
106def convert_to_np_atom(
107 arr: npt.ArrayLike, atom: Atom, copy: bool | None = copy_if_needed
108) -> np.ndarray:
109 """Convert a generic object into a NumPy object compliant with atom."""
110 # First, convert the object into a NumPy array
111 nparr = array_of_flavor(arr, "numpy")
112 # Copy of data if necessary for getting a contiguous buffer, or if
113 # dtype is not the correct one.
114 if atom.shape == ():
115 # Scalar atom case
116 nparr = np.array(nparr, dtype=atom.dtype, copy=copy)
117 else:
118 # Multidimensional atom case. Addresses #133.
119 # We need to use this strange way to obtain a dtype compliant
120 # array because NumPy doesn't honor the shape of the dtype when
121 # it is multidimensional. See:
122 # http://scipy.org/scipy/numpy/ticket/926
123 # for details.
124 # All of this is done just to taking advantage of the NumPy
125 # broadcasting rules.
126 newshape = nparr.shape[: -len(atom.dtype.shape)]
127 nparr2 = np.empty(newshape, dtype=[("", atom.dtype)])
128 nparr2["f0"][:] = nparr
129 # Return a view (i.e. get rid of the record type)
130 nparr = nparr2.view(atom.dtype)
131 return nparr
132
133
134# The next is used in Array, EArray and VLArray, and it is a bit more

Callers 2

_assign_valuesMethod · 0.85
convert_to_np_atom2Function · 0.85

Calls 1

array_of_flavorFunction · 0.85

Tested by

no test coverage detected