2016-11-14 6 views
0

私は、一連の観測された星からmodel PSFを生成しようとしています。オフ中心の重み付き数値ヒストグラム2d?

enter image description here

センター(ピーク強度)はビンである:私はthis answer(以下MCVE)にali_mが提供する素晴らしい例このような

5つ星私が使用しているを見て、次のよ[9, 9]。ビン[8, 8]のピーク密度を示す

enter image description here

numpyhitsogram2dを経由してその組み合わせの結果がこれです。代わりに

cx, cy = np.array([1.] * len(stars)), np.array([1.] * len(stars)) 

[9, 9]でそれを中央に、私は重心を(下記参照)を取得する必要があります。どうしてこれなの?


import numpy as np 
from matplotlib import pyplot as plt 

stars = # Uploaded here: http://pastebin.com/tjLqM9gQ 

fig, ax = plt.subplots(2, 3, figsize=(5, 5)) 
for i in range(5): 
    ax.flat[i].imshow(
     stars[i], cmap=plt.cm.viridis, interpolation='nearest', 
     origin='lower', vmin=0.) 
    ax.flat[i].axhline(9., ls='--', lw=2, c='w') 
    ax.flat[i].axvline(9., ls='--', lw=2, c='w') 
fig.tight_layout() 

# (nstars, ny, nx) pixel coordinates relative to each centroid 
# pixel coordinates (integer) 
x, y = np.mgrid[:20, :20] 
# centroids (float) 
cx, cy = np.array([0.] * len(stars)), np.array([0.] * len(stars)) 
dx = cx[:, None, None] + x[None, ...] 
dy = cy[:, None, None] + y[None, ...] 

# 2D weighted histogram 
bins = np.linspace(0., 20., 20) 
h, xe, ye = np.histogram2d(dx.ravel(), dy.ravel(), bins=bins, 
          weights=stars.ravel()) 
fig, ax = plt.subplots(1, 1, subplot_kw={'aspect': 'equal'}) 
ax.hold(True) 
ax.imshow(h, cmap=plt.cm.viridis, interpolation='nearest', 
      origin='lower', vmin=0.) 
ax.axhline(8., ls='--', lw=2, c='w') 
ax.axvline(8., ls='--', lw=2, c='w') 

plt.show() 
+1

正確に何が問題なのかは少し不明です。もしあなたが問題の銀河構成要素を省き、あなたが期待するものを正確に述べ、それがあなたが得たものとどのように比較してより小さな例を作り出すかは、おそらく助けになるだろうか? – ImportanceOfBeingErnest

+0

これは本当に簡単です: 'np.histogram2d'は、生成する配列と同じように、ビン[9,9]に中心が置かれていると思います。なぜ[8、8]に中心があるのか​​分からない。申し訳ありませんが、例を短くすることはできません。 – Gabriel

答えて

4

理由、ヒストグラムは単一星強度分布を中心とする点(9,9)を中心とされていない、それを生成するためのコードは、ヒストグラムのビンの周りにシフトすることです。

私は既にコメントに示唆したように、物事を単純にしておきます。例えば。問題を見るためにプロットする必要はありません。また、私はそれらが何であるか理解していないので、それらを避けましょう。dxdyです。

私たちは、その後、元のコードでの唯一の問題は、本当に0と20の間で20ポイントを作成する行bins = np.linspace(0., 20., 20)

import numpy as np 

stars = # Uploaded here: http://pastebin.com/tjLqM9gQ 

# The argmax of a single star results in (9,9) 
single_star_argmax = np.unravel_index(np.argmax(stars[0]), stars[0].shape) 

# Create a meshgrid of coordinates (0,1,...,19) times (0,1,...,19) 
y,x = np.mgrid[:len(stars[0,:,0]), :len(stars[0,0,:])] 
# duplicating the grids 
xcoord, ycoord = np.array([x]*len(stars)), np.array([y]*len(stars)) 
# compute histogram with coordinates as x,y 
# and [20,20] bins  
h, xe, ye = np.histogram2d(xcoord.ravel(), ycoord.ravel(), 
          bins=[len(stars[0,0,:]), len(stars[0,:,0])], 
          weights=stars.ravel()) 

# The argmax of the combined stars results in (9,9) 
combined_star_argmax = np.unravel_index(np.argmax(h), h.shape) 

print single_star_argmax 
print combined_star_argmax 
print single_star_argmax == combined_star_argmax 
# prints: 
# (9, 9) 
# (9, 9) 
# True 

によってヒストグラムを計算することができ、
0. 1.05263158 2.10526316 ... 18.94736842 20.
これはにビンサイズを拡大縮小~1.05とあなたのargmaxがすでに "早く"起こってから期待されるようにします。あなたが本当に欲しい
は、このようなミスを避けるために0と19、np.linspace(0,19,20)または np.arange(0,20)
の間で20ポイントである、一つは単純に引数、bins=20として元の配列の長さを与えることができます。

+0

説明とコードについては、IOBEいただきありがとうございます。 – Gabriel

関連する問題