存储值并继续运行功能

发布于 2025-01-18 12:26:16 字数 1029 浏览 3 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

余生一个溪 2025-01-25 12:26:16

简单更改顺序 - 第一个plotstars,下一个存储

    if PlotStars:
        # ... code ...
    if store:
        return positions

,这将首先显示绘图,然后以值退出功能。

但是,如果您要首先获得值,则应将其运行两次 - 首先仅使用store = true,然后仅使用plotstars = true

Simple change order - first PlotStars, next store

    if PlotStars:
        # ... code ...
    if store:
        return positions

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 with PlotStars=True

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文