len() of unsized object - ks test

J

Jamie Mitchell

Hello all,

I am trying to perform a Kolmogorov-Smirnov test in Python but I'm having a few difficulties.

# My files are netCDF so I import them as follows:

control=netCDF4.Dataset('/data/cr1/jmitchel/Q0/swh/controlperiod/south_west/swhcontrol_swest_concatannavg_1D.nc','r')

# The string is simply a 1D array

# Then performing the ks test:

kstest(control,'norm')

# I then get the following error:

File "<stdin>", line 1, in <module>
File "/usr/local/sci/lib/python2.7/site-packages/scipy/stats/stats.py", line 3413, in kstest
N = len(vals)
TypeError: len() of unsized object

Any ideas on why this isn't working would be great.

Thanks,

Jamie
 
M

MRAB

Hello all,

I am trying to perform a Kolmogorov-Smirnov test in Python but I'm having a few difficulties.

# My files are netCDF so I import them as follows:

control=netCDF4.Dataset('/data/cr1/jmitchel/Q0/swh/controlperiod/south_west/swhcontrol_swest_concatannavg_1D.nc','r')
A brief look at the documentation online says that 'control' is an
instance that _describes_ the dataset:

"""A netCDF Dataset is a collection of dimensions, groups, variables and
attributes. Together they describe the meaning of data and relations
among data fields stored in a netCDF file."""

So I don't think that it's the data itself.
 
S

Steven D'Aprano

Hello all,

I am trying to perform a Kolmogorov-Smirnov test in Python but I'm
having a few difficulties.

# My files are netCDF so I import them as follows:

control=netCDF4.Dataset('/data/cr1/jmitchel/Q0/swh/controlperiod/ south_west/swhcontrol_swest_concatannavg_1D.nc','r')

# The string is simply a 1D array

# Then performing the ks test:

kstest(control,'norm')

# I then get the following error:

File "<stdin>", line 1, in <module>
File
"/usr/local/sci/lib/python2.7/site-packages/scipy/stats/stats.py",
line 3413, in kstest
N = len(vals)
TypeError: len() of unsized object

Any ideas on why this isn't working would be great.

My guess is that you are calling len() on something that doesn't have a
length :)

But seriously, first of, what does "vals" contain? Look at the
documentation for the kstest function. Does it have a parameter called
"vals"? If so, what argument are you providing for that? Make sure that
it is what you expect it is.

What does the documentation for netCDF4.Dataset say? Perhaps you have to
*read* from the file, not just open it?

control = netCDF4.Dataset(filename,'r')
data = control.read() # Perhaps this?
kstest(data,'norm')

But I'm just guessing. You'll need to read the documentation to see what
arguments are expected, then you'll need to check what arguments you're
actually providing. Doing:

print(repr(control))

may help.
 
J

Jamie Mitchell

Hello all,



I am trying to perform a Kolmogorov-Smirnov test in Python but I'm having a few difficulties.



# My files are netCDF so I import them as follows:



control=netCDF4.Dataset('/data/cr1/jmitchel/Q0/swh/controlperiod/south_west/swhcontrol_swest_concatannavg_1D.nc','r')



# The string is simply a 1D array



# Then performing the ks test:



kstest(control,'norm')



# I then get the following error:



File "<stdin>", line 1, in <module>

File "/usr/local/sci/lib/python2.7/site-packages/scipy/stats/stats.py", line 3413, in kstest

N = len(vals)

TypeError: len() of unsized object



Any ideas on why this isn't working would be great.



Thanks,



Jamie

Thanks for your help.

Steven your right I wasn't reading in the file on netCDF4.Dataset, I was just opening it. I have rectified it now - a silly mistake!

Thanks again,

Jamie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top