2017-09-03 30 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

相關問題