Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am loading a .csv file with this code:

import csv
import numpy as np
import scipy.spatial

points = np.array([((int(R), int(G), int(B)),float(X), float(Y), float(Z))
              for R, G, B, X, Y, Z in csv.reader(open('XYZcolorlist_D65.csv'))])
    # load R,G,B,X,Y,Z coordinates of 'points' in a np.array  

print points 

And that works fine.

However, if I add this further line, where I am trying to compute a Delaunay triangulation with scipy: http://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.Delaunay.html

tri = scipy.spatial.Delaunay(points[1, 2, 3])
# do the triangulation

I get this error message:

Traceback (most recent call last):
  File "C:\Users\gary\Documents\EPSON STUDIES\delaunay.py", line 15, in <module>
    tri = scipy.spatial.Delaunay(points[1, 2, 3])
IndexError: too many indices

Obviously the syntax scipy.spatial.Delaunay(points[1, 2, 3]) isn't correct.

What is it that I am doing wrong?

EDIT:

If it's easier to handle, I can also use this line to import (np arrays should be of same type of data?)

points = np.array([(float(R), float(G), float(B), float(X), float(Y), float(Z))
              for R, G, B, X, Y, Z in csv.reader(open('XYZcolorlist_D65.csv'))])
    # load R,G,B,X,Y,Z coordinates of 'points' in a np.array   

Then I would need to skip the first 3 values in each row...

share|improve this question
    
map(float, row) for row in reader is simpler syntax. –  cohoz Feb 7 at 3:39

2 Answers 2

The error is with the slicing of numpy array. To get the coordinates of the points, either version of you code is fine.

First version of your code, where the first column of points is a tuple of RGB values:

tri = scipy.spatial.Delaunay(points[:, 1:])

Second version, where the RGB values are flattened, taking up 3 columns, you need to skip three columns:

tri = scipy.spatial.Delaunay(points[:, 3:])

In fact, you can use np.loadtxt for reading in the data (yielding the second version):

points = np.loadtxt('XYZcolorlist_D65.csv')
share|improve this answer
    
I actually need the second, third, and fourth points –  adrienlucca.wordpress.com Feb 7 at 2:39
1  
Mind posting first few row of points? What is the dimension of the points that needs to be triangulated? –  YS-L Feb 7 at 2:46
    
print points[1:4] returns the first 3 rows: [[(255, 95, 127) 40.2074945517 26.5282949405 22.7094284437] [(255, 127, 127) 43.6647438365 32.3482625492 23.6181801523] [(255, 159, 127) 47.1225628354 39.1780944388 22.9366615044]], I want to get the values inside, not the entire rows –  adrienlucca.wordpress.com Feb 7 at 2:53
    
get the values inside - can you give an example? For the first point, you want [40.2, 26.5, 22.7]? –  YS-L Feb 7 at 2:59
1  
Do you actually want all the points in points, but skipping the first column which represents the color? –  YS-L Feb 7 at 3:02

I'm not sure exactly what you're trying to do, but maybe it's something like this...you can replace file_txt with your csv reader.

>>> from scipy.spatial import Delaunay
>>> file_txt = [[255, 95, 127, 40.2, 26.5, 22.7], [255, 127, 127, 43.6, 32.3, 23.6], [255, 159, 127, 47.1, 39.1, 22.9], [0,0,0,0,0,0]]
>>> a = [list(map(float, row[3:])) for row in file_txt]
>>> a
[[40.2, 26.5, 22.7], [43.6, 32.3, 23.6], [47.1, 39.1, 22.9], [0.0, 0.0, 0.0]]
>>> Delaunay(a)
<scipy.spatial.qhull.Delaunay object at 0x0383FA30>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.