strax icon indicating copy to clipboard operation
strax copied to clipboard

Array view instead of copy

Open WenzDaniel opened this issue 4 years ago • 0 comments

Users should be aware that lines like the following:

for d in data_type:
    data= d['data'][:d['length']]
    modifies_data(data)

will create only a "view" onto data in memory. Hence when data is modified by any function, e.g. modifies_data, data_type['data']will be modified as well. This behavior is in many cases not desired and leads to bugs which are really hard to trace. The correct way looks like:

for d in data_type:
    data = np.copy(d['data'][:d['length']])
    modifies_data(data)

See also https://stackoverflow.com/questions/11524664/how-can-i-tell-if-numpy-creates-a-view-or-a-copy and https://stackoverflow.com/questions/4370745/view-onto-a-numpy-array for some additional information.

WenzDaniel avatar Oct 13 '20 07:10 WenzDaniel