I am trying to slice a variable from a netcdf file and plot it but I am running into problems.
This is from my code:
import numpy as np
from netCDF4 import Dataset
Raw= "filename.nc"
data = Dataset(Raw)
u=data.variables['u'][:,:,:,:]
print u.shape
U=u([0,0,[200:500],[1:300]])
#The print statement yields (2, 17, 900, 2600) as u's dimensions.
#U Is the slice of the dataset I am interested inn. A small subset of the 4-dimensional vector. This last line of code gives me a syntax error and I cannot figure out why.
Trying to pick out a single value from the array ( u(0,0,0,1)) gives me an Type error: TypeError: 'MaskedArray' The program's aim is to perform simple algebra on a subset of this subset and to plot this data. Any help is appreciated.
I think the comment by Spencer Hill is correct. Without seeing the full error message, I can't be sure, but I'm pretty sure that the TypeError results from you (through the use of parenthesis) trying to call the array as a function. Try:
U=u[0,0,200:500,1:300]
Related
I am stuck with an issue when it comes to taking slices of my data in python (I come from using Matlab).
So here is the code I'm using,
import scipy.io as sc
import math as m
import numpy as np
from scipy.linalg import expm, sinm, cosm
import matplotlib.pyplot as plt
import pandas as pd
import sys
data = pd.read_excel('DataDMD.xlsx')
print(data.shape)
print(data)
The out put looks like so,
Output
So I wish to take certain rows only (or from my understand in Python slices) of this data matrix. The other problem I have is that the top row of my matrix becomes almost like the titles of the columns instead of actually data points. So I have two problems,
1) I don't need the top of the matrix to have any 'titles' or anything of that sort because it's all numeric and all symbolizes data.
2) I only need to take the 6th row of the whole matrix as a new data matrix.
3) I plan on using matrix multiplication later so is panda allowed or do I need numpy?
So this is what I've tried,
data.iloc[0::6,:]
this gives me something like this,
Output2
which is wrong because I don't need the values of 24.8 to be the 'title' but be the first row of the new matrix.
I've also tried using np.array for this but my problem is when I try to using iloc, it says (which makes sense)
'numpy.ndarray' object has no attribute 'iloc'
If anyone has any ideas, please let me know! Thanks!
To avoid loading the first record as the header, try using the following:
pd.read_excel('DataDMD.xlsx', header=None)
The read_excel function has an header argument; the value for the header argument indicates which row of the data should be used as header. It gets a default value of 0. Use None as a value for the header argument if none of the rows in your data functions as the header.
There are many useful arguments, all described in the documentation of the function.
This should also help with number 2.
Hope this helps.
Good luck!
I need to use CubicSpline to interpolated between points. This is my function
cs = CubicSpline(aTime, aControl)
u = cs(t) # u is a ndarray of one element.
I cannot convert u to a float. uu = float(u) or uu = float(u[0]) doesn't work in the function.
I can convert u to a float in the shell by float(u). This shouldn't work because I have not provided an index but I get an error if I use u[0].
I have read something about np.squeeze. I tried it but it didn't help.
I added a print ("u=",u) statement after the u=cs(t). The result was
u= [ 1.88006889e+09 5.39398193e-01 5.39398193e-01]
How can this be? I expect 1 value. The second and third numbers look about right.
I found the problem. Programming error, of course but the error messages I got were very misleading. I was calling the interpolate function with 3 values so it returned three vales. Why I couldn't get just the one afterwards is still a mystery but now that I call the interpolate with just one value I get one float as expected. Overall this still didn't help as the interpolate1d function is too slow. I wrote my own cubic interpolate function that is MUCH faster.
Again, programming error and poor error messages were the problem.
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.
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]))