Python:如何将Numpy数组中的整数数据与另一个Numpy数组中的整数数据进行比较,然后将结果读取到.txt文件中



我有动物位置(x,y,z)数据,我有树数据(x,y,z)。我需要拉动动物位置周围发生的所有XYZ树输入 - 因此,我需要将包含XYZ动物位置的Numpy阵列与包含同一区域中树的X Y Z点位置的Numpy阵列进行比较。我想在点的4个单位半径中拉动所有XYZ树,并写了一个功能来完成。但实际上,它不仅仅将树木拉在动物位置周围。它只是打印所有可能的树木。如何仅将树木在动物点周围拉动,然后将它们放入我可以在另一个程序中使用的.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)

您实际上需要两个不同的功能,具体取决于您在集合中还是在集合之间进行测量。这是一个兼具的功能,以及对其正在做什么的解释:

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)。这是一个1D数组,对应于triu_indices给出的位置。查找距离少于阈值(pdist < dist)的距离,将其映射到索引(k = i[...]),然后写入与磁盘相对应的坐标(np.savetxt(coords[k] . . . )

  2. cdist找到两组之间的距离,作为2-D矩阵。找到小于阈值的元素(cdist(ind, dep, . . . ) < dist),在其中找到任何带有True的列(k = np.any( . . . , axis = 1)),然后再次将所有坐标写入磁盘(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 = ',')

相关内容

  • 没有找到相关文章

最新更新