1
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 ?

1 Answer 1

2

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')
Sign up to request clarification or add additional context in comments.

7 Comments

I tried that but I got this error TypeError: integer argument expected, got float
I am making an edit to the question to show what exactly I am doing!
Change the final argument from 0.0 to 0. The origin argument must be an integer.
I wanted to know why did you reshape the array ?
ndimage functions are usually applied to images, which are 2D.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.