2017-09-03 108 views
1

我有动物位置(X,Y,Z)数据,我有树数据(X,Y,Z)。我需要拉动动物位置周围发生的所有XYZ树的输入 - 所以我需要将包含xyz动物位置的numpy数组与包含相同区域中树的x y z点位置的numpy数组进行比较。我想把所有的xyz树拉到点的4个单位半径内,并写了一个函数来完成它。但它实际上并不只是拉动动物位置周围的树木。它只是打印所有可能的树。我怎么能只拉动物点周围的树,然后把它们放到一个.txt文件中,我可以在另一个程序中使用它?我是新手编程和任何帮助,我可以得到非常感谢。Python:如何将numpy数组中的整数数据与另一个numpy数组中的整数数据进行比较,并将结果读入.txt文件?

以下是我的代码以#descriptions:

#using array instead of dataframe 
import numpy as np 
from laspy.file import File 

#load and consolidate Veg Point Coordinates into one array 
VegList = sorted(glob.glob('/Users/sophiathompson/Desktop/copys/Clips/*.las')) 
VegListCoords = [] 
for f in VegList: 
    print(f) 
    Veg= File(filename = f, mode = "r") # Open the file # Eventually, this  will need to be the actual .laz files 
    VegListCoords.append(np.vstack((Veg.x, Veg.y, Veg.z)).transpose()) 
    print (VegListCoords) 
    XYZVegComplete = np.concatenate((VegListCoords), axis = 0) 

#Load animal point locations (x, y, z .csv file) from clip 240967 into array 
Animal240967 = np.loadtxt(fname='/Users/ST/Desktop/copys/CatTXTfiles/240967_CatsFt.csv', delimiter =',') 

#Use to find all vegetation heights in a 4 unit diameter of each animal point (animalx, animaly). #'d out lines of code are my attempts to make something work 
def near_Animal(animalx, animaly): 
    for x, y, z in XYZVegComplete: 
     r=2 #xy coordinates in ft 
     PointsNearAnml = [] 
     if (x-animalx)**2 + (y-animaly)**2 >= r**2: 
      PracticeTxt=open("/Users/ST/Desktop/practicefilecreate.txt", "w") 
      print (x, y, z) 
      #print (x, y, z) >> PracticeTxt, x, y, z 
      #PracticeTxt.write('%d %d %d \n' % Points) 
      #PracticeTxt.write('%d %d %d \n' % x y z) 
      #Points= (x, y, z) 
      #with open("/Users/sophiathompson/Desktop/practicefilecreate.txt", "w") as PracticeTxt: 
      #print >> PracticeTxt, Points 
      #PracticeTxt.close 
#Use to call near_Animal: gather Veg Points based on proximity to animal  points (using arrays)- 
for animalx, animaly in Animal240967: 
    near_Animal(animalx, animaly) 
+0

使用您提供的代码并不容易。考虑创建一个[Minimum,Complete,and Verifiable Example](https://stackoverflow.com/help/mcve),其中包含具有代表性的示例数据和预期输出。那样你就更有可能得到更好的答案,速度更快。 –

回答

0

实际上要取决于不管你是一组内或组之间测量两个不同的功能。这里有一个函数,它都和一些说明它在做什么:

from scipy.spatial.distance import pdist, cdist 
def near_fn(*args, dist = 2., outfile = "practicefilecreate.txt"): 
    if len(args) == 1: #within a set, i.e. `Animal240967` 
     coords = args[0] 
     i, j = np.triu_indices(coords.size, 1) 
     mask = pdist(coords, 'euclidean') < dist 
     k = np.unique(np.r_[i[mask], j[mask]]) 
     np.savetxt(coords[k], outfile, delimiter = ',') #or whatever you want 
    elif len(args) == 2: # between sets i.e. `XYZVegComplete` and `Animal240967` 
     coords_ind, coords_dep = args 
     k = np.any(cdist(coords_ind, coords_dep, 'euclidean') < dist, axis = 1) 
     np.savetxt(coords_ind[k], outfile, delimiter = ',') #or whatever you want 
    else: 
     assert False, "Too many inputs" 

这里做的事情:

  1. pdist找到一组的元件之间的距离,但只找到上直角三角形的值(因为a->b之间的距离与b->a相同,并且距离a->总是0)。这是一个一维数组,对应于triu_indices给出的位置。发现小于您的阈值(pdist < dist)的距离,其映射到索引(k = i[...])和写入对应于这些索引到磁盘的坐标(np.savetxt(coords[k] . . .)

  2. cdist发现两组之间的距离,作为一个2-d矩阵。查找不到你的阈值(cdist(ind, dep, . . .) < dist)的元素,发现在它Truek = np.any(. . . , axis = 1)),并再次任何列,写的所有COORDS到磁盘(np.savetxt(ind[k] . . .)

如果你有很多的价值,您可能需要使用scipy.spatial.KDTree而不是

from scipy.spatial import KDTree 
def kd_near_fn(*args, dist = 2., outfile = "practicefilecreate.txt"): 
    trees = [KDTree(arg) for arg in args] 
    if len(trees) == 1: 
     i, j = trees[0].query_pairs(dist) 
     k = np.unique(np.r_[i, j]) 
    elif len(trees) == 2: 
     queries = trees[0].query_ball_tree(trees[1], dist) 
     k = np.array([len(query) > 0 for query in queries]) 
    else: 
     assert False, "too many inputs" 
    np.savetxt(args[0][k], outfile, delimiter = ',') 
+0

Daniel F非常感谢你的回应。我非常感谢您所包含的每一步操作的细目和描述。对我来说,这是令人惊讶的教育,作为一个新的计算机编程的人 – SSThompson

+0

随时upvote和检查答案,因为最有用的然后:) –

+0

我一直在尝试使用KDTree,但一直未能得到它的工作。我不确定我在哪里放入了我的动物点。我是否将它们当作KDTree参数中的一个参数?例如。在定义def_kd_near_fn(* args .....我会在这里定义animalx,生成动画吗?),然后在树中引用它们:[KDTree(arg-animalx,animaly),然后i,j对与X,Y相关对于植被文件,我试图从AnimalXY对中取出? – SSThompson

相关问题