Question

from scipy import ndimage
import numpy as np

with open("Data.dat", "r+") as f:
    content = f.readlines()
    content = [s.strip() for s in content]
    content = np.asarray(content)
    weights = np.array([1, 2, 4, 2, 1])
    final = np.empty(content)
    ndimage.filters.convolve1d(content, weights, -1, final, 'reflect', 0.0, 0.0)

Here np is the numpy package. The length of content is 750, so tried to initialize the shape of the output array with np.empty() as I don't want any value in that.

But when I run I get this error:

Traceback (most recent call last):
File "C:\Users\Animesh\Desktop\Smoothing.py", line 18, in <module>
final = np.empty(content)
ValueError: sequence too large; must be smaller than 32

What should be done ?

Was it helpful?

Solution

To make final an empty array of the same shape and dtype as content, use:

final = np.empty_like(content)

Regarding the TypeError: integer argument expected, got float:

Although the docstring for convolve1d says

origin : scalar, optional
    The `origin` parameter controls the placement of the filter.
    Default 0.0.

the origin argument must be an integer, not a float.

Here is an example which runs without error:

import scipy.ndimage as ndimage
import numpy as np

# content = np.genfromtxt('Data.dat')
content = np.asarray(np.arange(750))
weights = np.array([1, 2, 4, 2, 1])
final = np.empty_like(content)
ndimage.convolve1d(content, weights, axis=-1, output=final, mode='reflect', cval=0.0,
                   origin=0
                   # origin=0.0  # This raises TypeError
                   )
print(final)

Uncommenting origin=0.0 raises the TypeError.


Regarding

with open("Data.dat", "r+") as f:
    content = f.readlines()
    content = [s.strip() for s in content]
    content = np.asarray(content)

This makes content an array of strings. Since you are taking a convolution, you must want an array of numbers. So instead replace the above with

content = np.genfromtxt('Data.dat')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top