亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

像素化位置和通量的算法

像素化位置和通量的算法

MM們 2022-11-29 17:15:27
如果你在一個平面上有 n 個粒子(位置為 (x_n,y_n)),具有一定的通量 flux_n,你會怎么做,你必須對這些粒子進行像素化,所以你必須從 (x,y) 到 (pixel_i) , pixel_j) 空間,你必須總結落入每個像素的 m 個粒子的通量?有什么建議么?謝謝!
查看完整描述

1 回答

?
慕尼黑5688855

TA貢獻1848條經驗 獲得超2個贊

有幾種方法可以解決您的問題。


假設:你的位置已經被存儲成兩個numpy array形狀(N,),即位置x_n(或y_n)為n在[0, N),讓我們稱它們為x和y。通量存儲在numpy array具有相同形狀的 a 中fluxes。


1 - 密集案例 創建看起來像網格的東西:


#get minimums and maximums position

mins = int(x.min()), int(y.min())

maxs = int(x.max()), int(y.max())

#actually you can also add and subtract 1 or more unit 

#in order to have a grid larger than the x, y extremes

#something like mins-=epsilon and maxs += epsilon


#create the grid

xx = np.arange(mins[0], maxs[0])

yy = np.arange(mins[1], maxs[1])

現在你可以執行一個 double for loop, tacking ,每次,兩個連續的元素xx和yy,要做到這一點,你可以簡單地采?。?/p>


x1 = xx[:-1] #excluding the last element

x2 = xx[1:]  #excluding the first element


#the same for y:

y1 = yy[:-1] #excluding the last element

y2 = yy[1:]  #excluding the first element


fluxes_grid = np.zeros((xx.shape[0], yy.shape[0]))

for i, (x1_i, x2_i) in enumerate(zip(x1, x2)):

    for j, (y1_j, y2_j) in enumerate(zip(y1, y2)):

        idx = np.where((x>=x1_i) & (x<x2_i) & (y>=y1_j) & (y<y2_j))[0]

        fluxes_grid[i,j] = np.sum(fluxes[idx])

在此循環結束時,您有一個網格,其元素是代表通量總和的像素。


2 - 使用像 K-NN 這樣的量化算法


如果你有很多 o 點,會發生什么,以至于循環需要幾個小時?更快的解決方案是使用量化方法,例如 K 最近鄰,剛性網格上的 KNN。有很多方法可以運行 KNN(包括已經實現的版本,例如sklearn KNN)。但如果您可以利用 GPU,則效率會有所不同。例如,這是我的 tensorflow (vs 2.1) 實現。定義方形網格后:


_min, maxs = min(mins), max(maxs)

xx = np.arange(_min, _max)

yy = np.arange(_min, _max)

您可以構建矩陣grid和位置矩陣X:grid = np.column_stack([xx, yy]) X = np.column_stack([x, y])


那么你必須定義一個矩陣歐幾里德成對距離函數:


@tf.function

def pairwise_dist(A, B):  

    # squared norms of each row in A and B

    na = tf.reduce_sum(tf.square(A), 1)

    nb = tf.reduce_sum(tf.square(B), 1)

    # na as a row and nb as a co"lumn vectors

    na = tf.reshape(na, [-1, 1])

    nb = tf.reshape(nb, [1, -1])

    # return pairwise euclidead difference matrix

    D = tf.sqrt(tf.maximum(na - 2*tf.matmul(A, B, False, True) + nb, 0.0))

    return D

因此:


#compute the pairwise distances:

D = pairwise_dist(grid, X)

D = D.numpy() #get a numpy matrix from a tf tensor

#D has shape M, N, where M is the number of points in the grid and N the number of positions.

#now take a rank and from this the best K (e.g. 10)

ranks = np.argsort(D, axis=1)[:, :10]

#for each point in the grid you have the nearest ten.

現在您必須獲取對應于這 10 個位置的通量并對它們求和。


我避免進一步指定第二種方法,我不知道你的目錄的維度,如果你有或沒有 GPU,或者你是否想使用這種優化。如果你愿意,我可以改進這個解釋,前提是你有興趣。


查看完整回答
反對 回復 2022-11-29
  • 1 回答
  • 0 關注
  • 107 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號