[SciPy-User] scan array to extract min-max values (with if condition)

Thøger Emil Juul Thorsen thoeger at fys.ku.dk
Sat Sep 11 11:40:14 EDT 2010


I had a similar problem lately and never really got an elegant solution.
I suggested that polygon clipping be implemented in NumPy, since I
believe there are quite a lot of cases where it would be handy, and I
hereby renew that request.

Cheers; 
Emil

On Sat, 2010-09-11 at 14:45 +0200, Massimo Di Stefano wrote:
> Hello All,
> 
> i need to extract data from an array, that are inside a 
> rectangle area defined as :
> 
> N, S, E, W = 234560.94503118, 234482.56929822, 921336.53116178, 921185.3779625
> 
> the data are in a csv (comma delimited text file, with 3 columns X,Y,Z) 
> 
> #X,Y,Z
> 3020081.5500,769999.3100,0.0300
> 3020086.2000,769991.6500,0.4600
> 3020099.6600,769996.2700,0.9000
> ...
> ...
> 
> i read it using " numpy.loadtxt "
> 
> data :
> 
> http://www.geofemengineering.it/data/csv.txt     5,3 mb (158735 rows)
> 
> to extract data that are inside the boundy-box area (N, S, E, W) i'm using a loop
> inside a function like :
> 
> import numpy as np
> 
> def getMinMaxBB(data, N, S, E, W):
> 	mydata = data * 0.3048006096012
> 	for i in range(len(mydata)):
> 		if mydata[i,0] < E or mydata[i,0] > W or mydata[i,1] < N or mydata[i,1] > S :
> 			if i == 0:
> 				newdata = np.array((mydata[i,0],mydata[i,1],mydata[i,2]), float)
> 			else :
> 				newdata = np.vstack((newdata,(mydata[i,0], mydata[i,1], mydata[i,2])))
> 	results = {}
> 	results['Max_Z'] = newdata.max(0)[2]
> 	results['Min_Z'] = newdata.min(0)[2]
> 	results['Num_P'] = len(newdata)
> 	return results
>    	
> 
> N, S, E, W = 234560.94503118, 234482.56929822, 921336.53116178, 921185.3779625
> data = '/Users/sasha/csv.txt'
> mydata = np.loadtxt(data, comments='#', delimiter=',')
> out = getMinMaxBB(mydata, N, S, E, W)
> 
> print out
> 
> 
> This method works, but maybe is not soo fast, have you any hints on how to improve code with better performance ?
> 
> thanks!!!
> 
> Massimo.
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user






More information about the SciPy-User mailing list