Numpy.trim_zeros for structured array without creating new array -
is possible trim 0 'records' of structured numpy array without copying it; i.e. free allocated memory 'unused' 0 entries @ beginning or end; actually, interested in trimming zeros @ end.
there builtin function numpy.trim_zeros()
1d arrays. return value:
returns:
trimmed : 1-d array or sequence
the result of trimming input. input data type preserved.
however, can't whether not create copy , frees memory. not proficient enough tell source code behaviour.
more specifically, have following code:
import numpy edges = numpy.zeros(3, dtype=[('i', 'i4'), ('j', 'i4'), ('length', 'f4')]) # fill first 2 records sensible data: edges[0]['i'] = 0 edges[0]['j'] = 1 edges[0]['length'] = 2.0 edges[1]['i'] = 1 edges[1]['j'] = 2 edges[1]['length'] = 2.0 # list memory adress , size edges.__array_interface__ edges = numpy.trim_zeros(edges) # not work structured array edges.__array_interface__
update
my question 'twofold':
1) builtin function frees memory or copy array?
answer:
copiescreates slice (=view);[ipython console] import numpy; numpy??
(see resize numpy array smaller size without copy , view onto numpy array?)
2) solution have similar functionality structured arrays?
answer:
begin=(edges!=numpy.zeros(1,edges.dtype)).argmax() end=len(edges)-(edges!=numpy.zeros(1,edges.dtype))[::-1].argmax() # 1) create slice without copy no memory free goodedges=edges[begin:end] # 2) or copy , free memory (temporary both arrays exist) goodedges=edges[begin:end].copy() del edges
imho, there 2 problem.
- first,
trim_zeros
function doesn't recognize zeroes on composite dtype.
you can locate them begin=(edges!=zeros(1,edges.dtype)).argmax()
, end=len(edges)-(edges!=zeros(1,edges.dtype))[::-1].argmax()
. goodedges=edges[begin:end]
interresting data.
- second,
trim_zeros
function doesn't free memory:
returns ------- trimmed : 1-d array or sequence. result of trimming input. input data type preserved.
so think must manually : goodedges=edges[begin:end].copy();del edges
.
Comments
Post a Comment