Re: [Pytables-users] Numpy data and Pytable array (error: IndexError: tuple index out of range)

2012-10-29 Thread Anthony Scopatz
Hello Jack,

I am not really sure what is going wrong because you did not post the
full code where the exception is happening.  However, this error seems
to be because the pnts array is one dimensional. (Which is why pnts.shape
has a length of 1.)  You could verify this by printing out pnts right
before the
line that fails.

Also, why are you using ctypes?  This seems wrong...

Be Well
Anthony

On Sun, Oct 28, 2012 at 9:25 PM, JACK  wrote:

>
>
> Hi all,
>
> I am new to python and pytables. Currently I am writing a project about
> clustering and KNN algorithm. That is what I have got.
>
> **   code  ***
>
> import numpy.random as npr
> import numpy as np
>
> #step0: obtain the cluster
> dtype = np.dtype('f4')
>
> pnts_inds = np.arange(100)
> npr.shuffle(pnts_inds)
> pnts_inds = pnts_inds[:10]
> pnts_inds = np.sort(pnts_inds)
> for i,ind in enumerate(pnts_inds):
> clusters[i] = pnts_obj[ind]
>
> #step1: save the result to a HDF5 file called clst_fn.h5
>
> filters = tables.Filters(complevel = 1, complib = 'zlib')
> clst_fobj = tables.openFile('clst_fn.h5', 'w')
> clst_obj = clst_fobj.createCArray(clst_fobj.root, 'clusters',
>tables.Atom.from_dtype(dtype), clusters.shape,
>filters = filters)
> clst_obj[:] = clusters
> clst_fobj.close()
>
> #step2: other function
> blabla
>
> #step3: load the cluster from clst_fn
>
> pnts_fobj= tables.openFile('clst_fn.h5','r')
> for pnts in pnts_fobj.walkNodes('/', classname = 'Array'):
> break
>
> #
> #step4: evoke another function (called knn). The function input argument
> is the
> #data from pnts. I have checked the knn function individually. This
> function
> #works well if the input is pnts  = npr.rand(100,128)
>
> def knn(pnts):
> pnts = numpy.ascontiguousarray(pnts)
> N = ctypes.c_uint(pnts.shape[0])
> D = ctypes.c_uint(pnts.shape[1])
> #
>
> # evoke knn using the cluster from clst_fn (see step 3)
> knn(pnts)
>
>
> **   end of code   ***
>
> My problem now is that python is giving me a hard time by showing:
> error: IndexError: tuple index out of range
> This error comes from
> "D = ctypes.c_uint(pnts.shape[1])"   this line.
>
> Obviously, there must be something wrong with the input argument. Any
> thought
> about fixing the problem? Thank you in advance.
>
>
>
>
>
>
> --
> The Windows 8 Center - In partnership with Sourceforge
> Your idea - your app - 30 days.
> Get started!
> http://windows8center.sourceforge.net/
> what-html-developers-need-to-know-about-coding-windows-8-metro-style-apps/
> ___
> Pytables-users mailing list
> Pytables-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/pytables-users
>
--
The Windows 8 Center - In partnership with Sourceforge
Your idea - your app - 30 days.
Get started!
http://windows8center.sourceforge.net/
what-html-developers-need-to-know-about-coding-windows-8-metro-style-apps/___
Pytables-users mailing list
Pytables-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/pytables-users


[Pytables-users] Numpy data and Pytable array (error: IndexError: tuple index out of range)

2012-10-28 Thread JACK


Hi all,

I am new to python and pytables. Currently I am writing a project about
clustering and KNN algorithm. That is what I have got.

**   code  ***

import numpy.random as npr
import numpy as np

#step0: obtain the cluster
dtype = np.dtype('f4')

pnts_inds = np.arange(100)
npr.shuffle(pnts_inds)
pnts_inds = pnts_inds[:10]
pnts_inds = np.sort(pnts_inds)
for i,ind in enumerate(pnts_inds):
clusters[i] = pnts_obj[ind]

#step1: save the result to a HDF5 file called clst_fn.h5

filters = tables.Filters(complevel = 1, complib = 'zlib')
clst_fobj = tables.openFile('clst_fn.h5', 'w')  
clst_obj = clst_fobj.createCArray(clst_fobj.root, 'clusters',
   tables.Atom.from_dtype(dtype), clusters.shape,
   filters = filters)
clst_obj[:] = clusters
clst_fobj.close()

#step2: other function
blabla

#step3: load the cluster from clst_fn

pnts_fobj= tables.openFile('clst_fn.h5','r')
for pnts in pnts_fobj.walkNodes('/', classname = 'Array'):
break

#
#step4: evoke another function (called knn). The function input argument is the 
#data from pnts. I have checked the knn function individually. This function 
#works well if the input is pnts  = npr.rand(100,128)

def knn(pnts):
pnts = numpy.ascontiguousarray(pnts)
N = ctypes.c_uint(pnts.shape[0])
D = ctypes.c_uint(pnts.shape[1])
#

# evoke knn using the cluster from clst_fn (see step 3)
knn(pnts)


**   end of code   ***

My problem now is that python is giving me a hard time by showing:
error: IndexError: tuple index out of range
This error comes from 
"D = ctypes.c_uint(pnts.shape[1])"   this line.

Obviously, there must be something wrong with the input argument. Any thought
about fixing the problem? Thank you in advance.





--
The Windows 8 Center - In partnership with Sourceforge
Your idea - your app - 30 days.
Get started!
http://windows8center.sourceforge.net/
what-html-developers-need-to-know-about-coding-windows-8-metro-style-apps/
___
Pytables-users mailing list
Pytables-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/pytables-users