我的 python 脚本不工作,但没有显示消息错误

问题描述 投票:0回答:0

我正在尝试制作一个脚本来自动化一个过程,但是当我在终端中运行我的脚本时,没有任何反应,但没有显示错误消息。我正在尝试从适合格式的图像分析星系的形态。我在没有定义函数 obj_selection 的情况下尝试了这段代码并且它有效但我真的不明白为什么在我定义它时它不起作用。

import argparse
import os
from astropy.io import fits

import statmorph
from statmorph.utils.image_diagnostics import make_figure

if __name__== "__main__":
    parser = argparse.ArgumentParser(description='Study galaxy morphology with statmorph with segmap from gnuastro.')
    parser.add_argument("--infile", help="Name of input image(default: none)", nargs='?', type=str, default=None)
    parser.add_argument("--size", help="Size of the stamp (default:200)", nargs='?', type=int,const=200, default=200)
    parser.add_argument("--path", help="Directory in which figure is saved (default: current directory)", nargs='?', type=str, default=os.path.abspath(os.getcwd()))
    args = parser.parse_args()


def obj_selection():
    with fits.open(args.infile) as hdu:
        header0 = hdu[0].header
        sci_header    = hdu[1].header
        sci_data      = hdu[1].data
        err_header    = hdu[2].header
        err_data      = hdu[2].data
        clumps_header = hdu[4].header
        clumps_data   = hdu[4].data
        object_header = hdu[5].header
        object_data   = hdu[5].data
    
    path = str(args.path)
    clumps  = clumps_data.copy()
    objects = object_data.copy()
    center  = int(args.size/2)
    objects[objects != objects[center,center]] = - object_data.max()
    clumps[clumps == -1] = - object_data.max()
    clumps = clumps + objects
    clumps[clumps < 0] = 0 
    clumps[clumps > 1] = 1
    print(path + f'/{args.infile}_morph.png')
    
    try:
        source_morphs = statmorph.source_morphology(sci_data, clumps, weightmap=err_data)
        morph = source_morphs[0]
        make_figure(morph).savefig(f'{args.infile}_morph.png')
#       fig.savefig(f'{args.infile}_morph.png')
        print(path + f'/{args.infile}_morph.png')
        
    except:
        print('Error:check --help')
        pass

为了运行它,我使用了:

python morphology.py --infile=img_final.fits

python astropy
© www.soinside.com 2019 - 2024. All rights reserved.