2017-09-03 165 views
3

我正在嘗試編寫一個方法,該方法允許我計算落在另一個具有三維座標的對象中的三維中的對象數量。你可以說這個對象的值也有一個半徑,所以我試圖計算一個球體內部的對象數量。計算落在x,y,z座標之間的一組值的數量

我不會發布我目前的腳本,但我會嘗試一個例子:我有一個三維座標爲gal_pos的半徑爲gal_rad的星系。

import numpy as np 
gal_pos = np.array(
    [[ 528.1373291 , 432.18615723, 443.8348999 ], 
    [ 540.12231445, 450.08154297, 442.07891846], 
    [ 590.73675537, 234.6769104 , 296.02798462], 
    [ 529.98809814, 161.75544739, 567.58203125], 
    [ 552.45446777, 312.1973877 , 375.42492676], 
    [ 700.94335938, 65.46828461, 172.71842957], 
    [ 463.43258667, 73.57706451, 285.4147644 ], 
    [ 547.74414062, 330.9855957 , 401.49771118], 
    [ 591.89801025, 196.19670105, 274.60073853], 
    [ 581.28320312, 376.70013428, 359.81851196], 
    [ 520.09820557, 302.17849731, 371.68771362], 
    [ 812.84539795, 97.41672516, 150.87428284], 
    [ 541.6552124 , 17.40070724, 373.07562256], 
    [ 523.34509277, 302.18151855, 503.6333313 ]]) 

gal_rad = np.array(
    [ 1.14752779, 1.02471195, 0.79648002, 0.6085083 , 0.78725676, 
    1.07809084, 0.57744866, 0.93733404, 0.76053329, 0.68979678, 
    0.61188519, 1.07989271, 0.83872035, 0.59899661]) 

然後,我也有明星與3D立場star_pos

star_pos = np.array(
    [[ 517.0300293 , 264.54165649, 547.87835693], 
    [ 530.37280273, 358.40835571, 455.68734741], 
    [ 530.42211914, 358.20803833, 455.80908203], 
    [ 530.86737061, 324.91717529, 407.96405029], 
    [ 547.05175781, 333.9262085 , 403.82403564], 
    [ 530.61053467, 325.91259766, 407.04153442], 
    [ 533.9979248 , 331.18804932, 451.3795166 ], 
    [ 531.20678711, 326.75308228, 406.44711304], 
    [ 550.81237793, 340.88101196, 408.75830078], 
    [ 519.52880859, 299.91259766, 516.25140381], 
    [ 525.82739258, 301.46209717, 501.66738892], 
    [ 524.87988281, 268.88357544, 510.], 
    [ 524.43371582, 299.99725342, 512.36077881], 
    [ 524.40429688, 299.8979187 , 512.57452393], 
    [ 524.40765381, 299.89120483, 512.5032959 ], 
    [ 545.57440186, 331.59066772, 401.20291138], 
    [ 532.29016113, 306.27557373, 491.26434326], 
    [ 530.77410889, 326.18057251, 407.06216431], 
    [ 524.14819336, 306.60586548, 509.55993652]]) 

以上僅僅是我所擁有的數值範例。

xmax_rad = gal_pos[:,0]+gal_rad 
xmin_rad = gal_pos[:,0]-gal_rad 

ymax_rad = gal_pos[:,1]+gal_rad 
ymin_rad = gal_pos[:,1]-gal_rad 

zmax_rad = gal_pos[:,2]+gal_rad 
zmin_rad = gal_pos[:,2]-gal_rad 

tot_pop = [] # Total population found each galaxy 

Nind = [(x,y,z) for x,y,z in enumerate(star_pos) 
     if any(xmin_rad <=x<= xmax_rad) and 
     any(ymin_rad<=y<=ymax_rad) 
     and any(zmin_rad<=x<=zmax_rad)] 
tot_pop.append(Nind) 

print tot_pop 

這種方法,我嘗試了很有道理的我被打破了一切,但是這個正被用於尺寸〜300的陣列,但對於Nind回報ValueError: need more than 2 values to unpack。可能由於這個事實,我的迭代不能解壓3個對象?

我已經嘗試了其他方法,我把每個位置的大小,但它返回不正確的結果,以及通過直方圖計數值,但又一次,返回不正確的結果(我檢查通過投影在二維直方圖)。這種方法在哪裏指數爲每個星系返回null陣列的每個星系:

tot_pop = [] 
for k in np.arange(len(gal_pos)): 
    Nind = [(x,y) for x,y in enumerate(star_pos) 
     if xmin_rad[k] <=x<= xmax_rad[k]) and 
     ymin_rad[k]<=y<=ymax_rad[k]] 

    tot_pop.append(Nind) 
+0

相關輸入的形狀是什麼? – Divakar

+0

@ MSeifert @Divakar嗨,對不起,我應該包括那些。在我的分析(128,3)中,'star_pos'的形狀實際上是一個NumPy數組。同樣'gal_pos'是形狀(14,3),也是一個NumPy數組。我會在我的文章中包含一些值! – DarthLazar

回答

3

你可以使用zip遍歷星系+半徑然後用廣播和布爾索引找到匹配:

result = [] 
for galaxy, galaxy_radius in zip(gal_pos, gal_rad): 
    # With broadcasting you can simply subtract the positions from the galaxy center 
    # and using abs avoids checking lower and upper bound. 
    rel_star_pos = abs(star_pos - galaxy) 
    # Check which distances are below the radius and keep these which are 
    # within the radius for x, y and z 
    matches = (rel_star_pos <= galaxy_radius).all(axis=1) 
    # use boolean indexing to append the stars which satisfy the above condition 
    result.append(star_pos[matches]) 
print(result) 

如果你想添加索引(不是實際星座標)可以在append行更改爲:

result.append(np.where(matches)[0]) 

或者,如果你只是要匹配的數量:

result.append(np.sum(matches)) 

但是我無法找到與給定數據的任何比賽:

[array([], shape=(0, 3), dtype=float64), 
array([], shape=(0, 3), dtype=float64), 
array([], shape=(0, 3), dtype=float64), 
array([], shape=(0, 3), dtype=float64), 
array([], shape=(0, 3), dtype=float64), 
array([], shape=(0, 3), dtype=float64), 
array([], shape=(0, 3), dtype=float64), 
array([], shape=(0, 3), dtype=float64), 
array([], shape=(0, 3), dtype=float64), 
array([], shape=(0, 3), dtype=float64), 
array([], shape=(0, 3), dtype=float64), 
array([], shape=(0, 3), dtype=float64), 
array([], shape=(0, 3), dtype=float64), 
array([], shape=(0, 3), dtype=float64)] 
+0

雖然我給出的數值沒有給出結果,但我已經顯示了其他100個數值!非常感謝你。 – DarthLazar

3

這裏有一個幾乎量化方法利用的效率NumPy broadcastingslicing助陣,以及 -

# Define low and high limits 
l = gal_pos - gal_rad[:,None] 
h = gal_pos + gal_rad[:,None] 

# Get mask of valid ones for each row of star_pos 
mask = np.ones(star_pos.shape[0], dtype=bool) 
for i in range(star_pos.shape[1]): 
    mask &= ((l[:,i,None] <= star_pos[:,i]) & (h[:,i,None] >= star_pos[:,i])).any(0) 

# Finally use the mask to select valid rows off star_pos 
out = star_pos[mask] 

調用它,因爲幾乎矢量化,因爲我們仍然通過列的數量star_pos迭代。但是,由於我們正在處理X,Y,Z數據,那將是3。因此,可以很安全地將它稱爲向量化的原因。

對於給定的樣本,這裏就是我得到的 -

In [302]: out 
Out[302]: array([], shape=(0, 3), dtype=float64) 

所以,無論從star_pos點被滿足的限制。

+0

嗯,有趣。我用'any'和'|'掙扎了一下。我不認爲這是正確的。我認爲它應該在所有3個座標的「範圍」內。 – MSeifert

+0

@MSeifert我想'any(xmin_rad <= x <= xmax_rad)'讓我感到困惑。固定。 – Divakar

+0

現在好多了:)我傾向於**不完全矢量化解決方案(通過擴展尺寸)解決這些問題,因爲它太容易耗盡內存 - 因爲最終會產生2 n * m個陣列,這些陣列甚至會耗盡如果數據集的大小很大,那麼會有大量RAM的計算機。在這種情況下,它只是關於長度300陣列,所以這裏沒有問題,所以+1 :) – MSeifert

相關問題