存储值并继续运行功能
我正在使用Astropy检测图像中的来源。我正在尝试编写一个可以检测我的来源的函数,并以将其坐标存储在数组中的选项以及绘制源的另一种选择。这是我的代码:
def SourceDetect(file, SIG, FWHM, THRESH, store = False, PlotStars = False):
image = pf.open(file)
im = image[0].data
mean, median, std = sigma_clipped_stats(im, sigma=SIG)
daofind = DAOStarFinder(fwhm = FWHM, threshold = THRESH * std)
sources = daofind(im - median)
positions = np.transpose((sources['xcentroid'], sources['ycentroid']))
if(store):
return positions
if(PlotStars):
apertures = CircularAperture(positions, r = 6.)
norm = ImageNormalize(stretch = SqrtStretch())
plt.imshow(im, cmap = 'Greys', origin = 'lower', norm = norm,
interpolation = 'nearest')
apertures.plot(color = 'blue', lw = 1.5, alpha = 1)
for i in range(0, len(sources)):
plt.text(sources[i]['xcentroid'], sources[i]['ycentroid'], i, color='black');
但是,当我使用存储和绘图设置为true时,只有函数的存储部分运行时,除非我将其制作false,否则我将无法绘制它。有没有办法编写此代码,我将能够存储和绘制我的坐标?
I am using astropy to detect sources in an image. I am trying to write a function that will detect my sources, with an option of storing their coordinates in an array, and another option to plot the sources. This is my code:
def SourceDetect(file, SIG, FWHM, THRESH, store = False, PlotStars = False):
image = pf.open(file)
im = image[0].data
mean, median, std = sigma_clipped_stats(im, sigma=SIG)
daofind = DAOStarFinder(fwhm = FWHM, threshold = THRESH * std)
sources = daofind(im - median)
positions = np.transpose((sources['xcentroid'], sources['ycentroid']))
if(store):
return positions
if(PlotStars):
apertures = CircularAperture(positions, r = 6.)
norm = ImageNormalize(stretch = SqrtStretch())
plt.imshow(im, cmap = 'Greys', origin = 'lower', norm = norm,
interpolation = 'nearest')
apertures.plot(color = 'blue', lw = 1.5, alpha = 1)
for i in range(0, len(sources)):
plt.text(sources[i]['xcentroid'], sources[i]['ycentroid'], i, color='black');
However when I run this code with both store and plot set to True only the store part of the function runs and I can't get it to plot unless I make store False. Is there a way to write this code where I will be able to have my coordinates stored and plotted?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简单更改顺序 - 第一个
plotstars
,下一个存储
,这将首先显示绘图,然后以值退出功能。
但是,如果您要首先获得值,则应将其运行两次 - 首先仅使用
store = true
,然后仅使用plotstars = true
Simple change order - first
PlotStars
, nextstore
and this will first display plot and later it exits function with value.
But if you want first get value and later plot then you should run it two times - first only with
store=True
and later only withPlotStars=True