I am currently writing some code which is supposed to perform FFT on a set of data. I have a python list of points and I can easily create a time list. When I run fft(datalist), I get the 'TypeError: 'numpy.ndarray' object is not callable' error. I think (but please correct me) the issue is that the list is one dimension and they have no attachment to time at all by using that one line of code above. My question is, do I have to input a two dimensional array with time and data points? or am I completely wrong and have to rethink?
Thanks, Mike
Edit - forgot to add some code. The t=time. Could it be because the number of entries in the array isnt equal to 2^n where N is an integer?
sample_rate=10.00
t=r_[0:191.6:1/sample_rate]
S = fft([mylist])
print S
The Numpy and SciPy fft functions are looking to have numpy arrays as input, not native python lists. Also they work just fine with lengths that are not powers of two. You probably just need to cast your list as an array before passing it to the fft.
From your example code above try:
from numpy.fftpack import fft
from numpy import array
""" However you generate your list goes here """
S = fft(array([mylist]))
Related
My task is to convert one big MATLAB file into python.
There is a line in MATLAB
weightsEI_slow = random('binom',1,0.2,[EneuronNum_slow,IneuronNum_slow]);
I am trying to convert this into python code, I am not quite finding the right documentation. I looked for numpy library too. Does any one have any suggestions?
It looks like you generate a random number that follows the Binomial distribution with probability p=0.2 and sample size n=1. In turn, you can leverage numpy
import numpy as np
np.random.binomial(n=1, p=0.2)
>0
If you require replicability, add np.random.seed(3408) before the number is sampled. Otherwise, the output might be 0 or 1 depending on the execution. Of course, you can switch in another integer value as the seed instead of 3408.
This question is different from this one: How to print the full NumPy array, without truncation?
In that question, the user wanted to know how to print the full array without truncation. I can print the array without truncation just fine. My problem is that only a small portion of the screen width is used. When trying to inspect large adjacency matrices, it's impossible to inspect them when the rows unnecessarily wrap.
I'm asking this question here because it always takes me hours to find the solution, and I want to disambiguate it from that answer post above.
For example:
import networkx as nx
import numpy as np
np.set_printoptions(threshold=np.inf)
graph = nx.gnm_random_graph(20, 20, 1)
nx.to_numpy_matrix(graph)
This output displays as:
Just gonna post NaN's comment as the answer:
Use np.set_printoptions(linewidth=n) where n has to do with the number of characters (not array elements) per line. So in your case n=100 should do the trick.
I have the following code snippet from SciPy:
resDat = data[scipy.random.randint(0,N,(N,))]
What I try to understand is how and why this line works. the randint function seems to return a list of N integer values in the range of the data indizes, so what I interpret this line of code to do is that resDat will become an array with N random values from data.
I tried to replicate this in the Python shell:
a=[1,2,3,4,5,6]
b=[1,2]
c=a[b]
However if I try this I get - on line 3 - the error
TypeError: list indices must be integers, not list
Which to my knowledge means, that I need to give it a number instead of a list. But why is the line at the top working then? I have the feeling I am missing some important distinction, but can't figure out which one.
Coming from a mainly .NET background the first line looks a bit like a LinQ statement, but is it comparable?
I believe data would be of type -
numpy.ndarray
You can do type(data) it should comes out as numpy.ndarray .
Also , scipy.random.randint() also returns a value of type numpy.ndarray .
You may not be able to do lst[[1,2]] , but you can use numpy.ndarray as a subscript to another numpy.ndarray .
A Simple example -
import numpy as np
data = np.array([10,15,20,25,30])
print(data[np.array([1,2,3])])
>> array([15,20,25])
I'm reading image files from a list with a variable number of objects.
How can I add the arrays from each file to each other?
Here's an example adding only two arrays:
imageArray= [sum(x,y) for x,y in zip(io.imread(list[1]),io.imread(list[2]))]
I want to extend this to be able to add a variable number of arrays.
I have tried the following, without avail:
for x in filelist:
imageArray = [sum(y) for y in itertools.izip(io.imread(x))]
Which yields the error:
TypeError: Invalid dimensions for image data
Any help would be much appreciated!
Edit
I have been able to read all the images into array using part of unutbu's answer below:
im = map(SNIO.imread,filelist)
From here, I wish to add all the resulting arrays together (element wise). The correct solution would reproduce the result from the following code:
imageArray = [x+y+z for x,y,z in zip(im[0],im[1],im[2])]
which works fine, but, as stated in the original question, I wish to do this with any number of arrays rather than specifying them as in the previous example.
Is io.imread the same as scipy.ndimage.io.imread?
If so, then to add the arrays element-wise, use np.add.reduce:
import numpy as np
import scipy.ndimage.io as SNIO
files = ...
arrs = map(SNIO.imread, files)
result = np.add.reduce(arrs)
This will be far faster than calling Python's sum function for each location in the arrays.
How would I translate the following into Python from Matlab? I'm still trying to wrap my head around lists/matrices and arrays in numpy, etc.
outframe(:,[4:4:nout-1]) = 0.25*inframe(:,[1:n-1]) + 0.75*inframe(:,[2:n])
pos=(beamnum>0)*(beamnum<=nbeams)*(binnum>0)*(binnum<=nbins)*((beamnum-1)*nbins+binnum)
for index =1:512:
outarray(index,:) =uint8(interp1([1:n],inarray64(index,:),[1:.25:n],method))
(There's other stuff, these are just the particular statements I'm not sure how to make sense of. I have numpy imported,
The main workhorse in numpy is the ndarray (or array). It will for the most part replace matlab matrices when you translate code. Like a matlab matrix, the ndarray stores homogeneous data (ie float64) and is optimized for numerical operations.
The numpy matrix is a subclass of the ndarray which can be convenient for some linear algebra intensive applications. Here is more info about the differences between the two.
The python list is more like a matlab cell array (though not exactly the same). It's one of the basic python data structures, but in scientific applications I find that it comes up most often when you need to hold heterogeneous data. (Or when you're doing something very simple and don't want to go to the trouble of creating a numpy array).
Your code above can be converted almost verbatim to python using the ndarray and replacing () with [] for indexing and taking into account that indexing starts at 1 in MATLAB and 0 in python
i.e. : the first element in MATLAB is element 1, and in python it is element 0.
Let's try this line by line:
outframe(:,[4:4:nout-1]) = 0.25*inframe(:,[1:n-1]) + 0.75*inframe(:,[2:n])
would translate in "English" to: all rows of outframe, but only every 4th column starting from 4 to nout-1 (i.e.4,8..). I assume you understand what inframe references mean.
pos=(beamnum>0)*(beamnum<=nbeams)*(binnum>0)*(binnum<=nbins)*((beamnum-1)*nbins+binnum)
Possibly beamnum is a vector and (beamnum >0) returns a vector of {0,1} such that the elements are '1' where the respective beamnum element is >0, else 0. The rest of it is clear, i hope.
The second last line is a for-loop and the last line should hopefully be clear.