home / github / issues

Menu
  • Search all tables
  • GraphQL API

issues: 553672182

This data as json

id node_id number title user state locked assignee milestone comments created_at updated_at closed_at author_association active_lock_reason draft pull_request body reactions performed_via_github_app state_reason repo type
553672182 MDU6SXNzdWU1NTM2NzIxODI= 3715 ValueError: buffer source array is read-only with apply_ufunc 8419157 closed 0     5 2020-01-22T17:00:23Z 2022-04-18T02:18:47Z 2022-04-18T02:18:47Z NONE      

I am trying to use scipy.signal.find_peaks function within apply_ufunc but fails with ValueError: buffer source array is read-only.

MCVE Code Sample

```python import numpy as np import xarray as xr from scipy.signal import find_peaks

Generate waveform

x = (np.sin(2np.pi(2*np.linspace(2,10,1000))np.arange(1000)/48000) + np.random.normal(0, 1, 1000) * 0.15)

Find peaks non-xarray way

peaks, _ = find_peaks(x, prominence=1) print(peaks)

Cast waveform to xr.DataArray

x = xr.DataArray(x, dims='time')

Duplicate data along a new dimension

rep = xr.DataArray(range(11), dims='repeat') x = (x.broadcast_like(rep).assign_coords(repeat=rep))

def process_peaks(arr): # Apply find_peaks peaks, _ = find_peaks(arr, prominence=1) return peaks

Apply function to array

results = xr.apply_ufunc( process_peaks, x, input_core_dims=[['time']], output_core_dims=[['peaks']], vectorize=True )

Should show repeats of peak results

print(results) ```

Expected Output

In the MCvE above, there are two print statements, The first print out the results of the peak finding without involving xarray. The second print statement prints out the xr.DataArray of same peak results repeated along a dimension repeat.

Problem Description

The function scipy.signal.find_peaks threw an error ValueError: buffer source array is read-only. See the full traceback below ```


ValueError Traceback (most recent call last) <ipython-input-43-1a80f67560a6> in <module> 35 input_core_dims=[['time']], 36 output_core_dims=[['peaks']], ---> 37 vectorize=True 38 ) 39

c:\users\at17\repos\xarray\xarray\core\computation.py in apply_ufunc(func, input_core_dims, output_core_dims, exclude_dims, vectorize, join, dataset_join, dataset_fill_value, keep_attrs, kwargs, dask, output_dtypes, output_sizes, *args) 1040 join=join, 1041 exclude_dims=exclude_dims, -> 1042 keep_attrs=keep_attrs, 1043 ) 1044 elif any(isinstance(a, Variable) for a in args):

c:\users\at17\repos\xarray\xarray\core\computation.py in apply_dataarray_vfunc(func, signature, join, exclude_dims, keep_attrs, args) 230 231 data_vars = [getattr(a, "variable", a) for a in args] --> 232 result_var = func(data_vars) 233 234 if signature.num_outputs > 1:

c:\users\at17\repos\xarray\xarray\core\computation.py in apply_variable_ufunc(func, signature, exclude_dims, dask, output_dtypes, output_sizes, keep_attrs, args) 599 "apply_ufunc: {}".format(dask) 600 ) --> 601 result_data = func(input_data) 602 603 if signature.num_outputs == 1:

~.conda\envs\asi\lib\site-packages\numpy\lib\function_base.py in call(self, args, *kwargs) 2089 vargs.extend([kwargs[_n] for _n in names]) 2090 -> 2091 return self._vectorize_call(func=func, args=vargs) 2092 2093 def _get_ufunc_and_otypes(self, func, args):

~.conda\envs\asi\lib\site-packages\numpy\lib\function_base.py in _vectorize_call(self, func, args) 2155 """Vectorized call to func over positional args.""" 2156 if self.signature is not None: -> 2157 res = self._vectorize_call_with_signature(func, args) 2158 elif not args: 2159 res = func()

~.conda\envs\asi\lib\site-packages\numpy\lib\function_base.py in _vectorize_call_with_signature(self, func, args) 2196 2197 for index in np.ndindex(broadcast_shape): -> 2198 results = func((arg[index] for arg in args)) 2199 2200 n_results = len(results) if isinstance(results, tuple) else 1

<ipython-input-43-1a80f67560a6> in process_peaks(arr) 27 28 # Finally execute find_peaks ---> 29 peaks, _ = find_peaks(arr, prominence=1) 30 return peaks 31

~.conda\envs\asi\lib\site-packages\scipy\signal_peak_finding.py in find_peaks(x, height, threshold, distance, prominence, width, wlen, rel_height, plateau_size) 937 raise ValueError('distance must be greater or equal to 1') 938 --> 939 peaks, left_edges, right_edges = _local_maxima_1d(x) 940 properties = {} 941

_peak_finding_utils.pyx in scipy.signal._peak_finding_utils._local_maxima_1d()

~.conda\envs\asi\lib\site-packages\scipy\signal_peak_finding_utils.cp37-win_amd64.pyd in View.MemoryView.memoryview_cwrapper()

~.conda\envs\asi\lib\site-packages\scipy\signal_peak_finding_utils.cp37-win_amd64.pyd in View.MemoryView.memoryview.cinit()

ValueError: buffer source array is read-only ```

I have a conversation with some people on Gitter about it and a workaround was proposed, adding this line of code before find_peaks arr = arr.copy() I am not sure if a read-only array is a view or not. This appears to be a known problem to some people but there are no documentation on this limitation of apply_ufunc. It may be unlikely that a copy operation will be added to apply_ufunc to deal with this issue internally. So the way forwards is to add documentation to apply_ufunc about this issue and the workaround.

Output of xr.show_versions()

``` INSTALLED VERSIONS


commit: None python: 3.7.6 | packaged by conda-forge | (default, Jan 7 2020, 21:48:41) [MSC v.1916 64 bit (AMD64)] python-bits: 64 OS: Windows OS-release: 10 machine: AMD64 processor: Intel64 Family 6 Model 78 Stepping 3, GenuineIntel byteorder: little LC_ALL: None LANG: None LOCALE: None.None libhdf5: 1.10.5 libnetcdf: 4.7.3

xarray: 0.14.1+50.ge0fd4805 pandas: 0.25.3 numpy: 1.17.3 scipy: 1.3.1 netCDF4: 1.5.3 pydap: None h5netcdf: 0.7.4 h5py: 2.10.0 Nio: None zarr: None cftime: 1.0.4.2 nc_time_axis: None PseudoNetCDF: None rasterio: None cfgrib: None iris: None bottleneck: None dask: None distributed: None matplotlib: 3.1.2 cartopy: None seaborn: None numbagg: None setuptools: 45.0.0.post20200113 pip: 19.3.1 conda: None pytest: None IPython: 7.11.1 sphinx: None ​```

{
    "url": "https://api.github.com/repos/pydata/xarray/issues/3715/reactions",
    "total_count": 2,
    "+1": 2,
    "-1": 0,
    "laugh": 0,
    "hooray": 0,
    "confused": 0,
    "heart": 0,
    "rocket": 0,
    "eyes": 0
}
  completed 13221727 issue

Links from other tables

  • 1 row from issues_id in issues_labels
  • 5 rows from issue in issue_comments
Powered by Datasette · Queries took 80.172ms · About: xarray-datasette