Skip to content

Commit

Permalink
New coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
vpicouet committed Jun 10, 2021
1 parent 42d8d40 commit 8516b25
Show file tree
Hide file tree
Showing 4 changed files with 677 additions and 507 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ __pycache__

pyds9plugin/images/*.png
pyds9plugin/images/*.xml
pyds9plugin/test*
pyds9plugin/test/*
pyds9plugin/doc/ref/old_high_quality/

pyds9plugin.egg-info/
Expand Down
15 changes: 8 additions & 7 deletions pyds9plugin/DS9Utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2925,9 +2925,9 @@ def DS9plot_rp_convolved(
d.append("plot legend position top ")
d.append("plot title legend ''")
d.append("plot name 'Data: Flux = %i, FWHM_fit = %0.1f' " % (d_["Flux"], abs(popt[1])))
d.append("plot line shape circle ")
# d.append("plot line shape circle ")
d.append("plot line dash yes ")
d.append("plot line shape color black")
# d.append("plot line shape color black")
d.append("plot error color black ")
d.append("plot load /tmp/3.dat xy ")
d.append("plot name 'Moffat: Amp=%0.1f, alpha=%0.1f, beta=%0.1f' " % (popt_m[0], popt_m[1], popt_m[2]))
Expand Down Expand Up @@ -4516,7 +4516,7 @@ def center_region(xpapoint=None, Plot=True, argv=[]):

parser = CreateParser(get_name_doc(), path=False)
parser.add_argument(
"-m", "--method", default="1", help="", type=str, choices=["Maximum", "Center-of-mass", "2x1D-Gaussian-fitting", "2D-Gaussian-fitting"]
"-m", "--method", default="Maximum", help="", type=str, choices=["Maximum", "Center-of-mass", "2x1D-Gaussian-fitting", "2D-Gaussian-fitting"]
) # metavar='',
parser.add_argument("-b", "--background_removal", default="0", help="", type=str, choices=["1", "0"]) # metavar='',
args = parser.parse_args_modif(argv, required=False)
Expand Down Expand Up @@ -8152,7 +8152,7 @@ def RunSextractor(xpapoint=None, detector=None, path=None, argv=[]):
parser.add_argument(
"-PN", "--PARAMETERS_NAME", default="sex.param", help="File name in ds9_package/Calibration/Sextractor/", type=str, metavar="",
) # metavar='',
parser.add_argument("-DT", "--DETECT_TYPE", help="CCD (linear) or PHOTO (with gamma correction)", type=str, choices=["CCD", "PHOTO"])
parser.add_argument("-DT", "--DETECT_TYPE", default="CCD", help="CCD (linear) or PHOTO (with gamma correction)", type=str, choices=["CCD", "PHOTO"])
parser.add_argument("-DM", "--DETECT_MINAREA", default="10", help="min number of pixels above threshold", type=str, metavar="") # metavar='',
parser.add_argument(
"-Dm", "--DETECT_MAXAREA", default="0", help="max number of pixels above threshold (0=unlimited)}", type=str, metavar=""
Expand Down Expand Up @@ -8191,6 +8191,7 @@ def RunSextractor(xpapoint=None, detector=None, path=None, argv=[]):
"-WT",
"--WEIGHT_TYPE",
type=str,
default='NONE',
choices=["NONE", "NONE,MAP_VAR", "MAP_VAR", "MAP_VAR,MAP_VAR"],
help="First one for detection image, second one for photometric image",
metavar="",
Expand All @@ -8201,7 +8202,7 @@ def RunSextractor(xpapoint=None, detector=None, path=None, argv=[]):
parser.add_argument(
"-FI", "--FLAG_IMAGE", default="NONE", help="filename for an input FLAG-image", type=str, metavar="",
) # metavar='',
parser.add_argument("-FT", "--FLAG_TYPE", type=str, choices=["OR", "AND", "MAX", "MIN", "MOST"], help="flag pixel combination", metavar="")
parser.add_argument("-FT", "--FLAG_TYPE", type=str, default='OR',choices=["OR", "AND", "MAX", "MIN", "MOST"], help="flag pixel combination", metavar="")
parser.add_argument(
"-PA", "--PHOT_APERTURES", default="6,12,18", help="MAG_APER aperture diameter(s) in pixels", type=str, metavar="",
) # metavar='',
Expand Down Expand Up @@ -8250,7 +8251,7 @@ def RunSextractor(xpapoint=None, detector=None, path=None, argv=[]):
],
)
# parser.add_argument('-SB', '--SUBTRACT_BACK',default='1', help='Subtraction sky background ?', type=str,choices=['1','0'])
parser.add_argument("-BT", "--BACK_TYPE", default="1", type=str, choices=["AUTO", "MANUAL"], metavar="")
parser.add_argument("-BT", "--BACK_TYPE", default="AUTO", type=str, choices=["AUTO", "MANUAL"], metavar="")
parser.add_argument(
"-BV", "--BACK_VALUE", default="0.0", help="Default background value in MANUAL mode", type=str, metavar="",
) # metavar='',
Expand All @@ -8263,7 +8264,7 @@ def RunSextractor(xpapoint=None, detector=None, path=None, argv=[]):
parser.add_argument(
"-BFT", "--BACK_FILTTHRESH", default="0.0", help="Threshold above which the background map filter operates", type=str, metavar="",
) # metavar='',
parser.add_argument("-BPT", "--BACKPHOTO_TYPE", default="1", type=str, choices=["LOCAL", "GLOBAL"], metavar="")
parser.add_argument("-BPT", "--BACKPHOTO_TYPE", default="LOCAL", type=str, choices=["LOCAL", "GLOBAL"], metavar="")
parser.add_argument(
"-bpt", "--BACKPHOTO_THICK", default="24", help="thickness of the background LOCAL annulus", type=str, metavar="",
) # metavar='',
Expand Down
189 changes: 189 additions & 0 deletions pyds9plugin/testing/test_profile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Dec 7 20:30:33 2020
@author: Vincent
"""
import numpy as np
import os, glob, sys
from pyds9plugin.DS9Utils import *
from shutil import copyfile, rmtree
from pyds9 import DS9
from pkg_resources import resource_filename
from IPython import get_ipython
# ipython = get_ipython()
# get_ipython().magic("%load_ext line_profiler")

from line_profiler import LineProfiler
import pstats
from io import StringIO






DS9_BackUp_path = os.environ['HOME'] + '/DS9QuickLookPlugIn'
test_folder = '/Users/Vincent/Github/pyds9plugin/pyds9plugin/testing'#resource_filename('pyds9plugin', 'testing')
im = '/Users/Vincent/Github/pyds9plugin/pyds9plugin/images/stack.fits'#resource_filename('pyds9plugin', 'images/stack.fits')
if os.path.exists(test_folder) is False:
os.mkdir(test_folder)
files_folder = test_folder + '/files'
profiles_folder = test_folder + '/profiles'
if os.path.exists(files_folder) is False:
os.mkdir(files_folder)

def Profile_old(file=None,command="DS9open('%s','%s/stack.fits')",argv=None,profiles_folder=profiles_folder,functions=''):
if file is None:
file = command.split('(')[0]
if argv is not None:
sys.argv = ['DS9Utils',command.split('(')[0]] + argv
ipython.magic("lprun -u 1e-1 -T %s/Prun_%s.py -s -r -f %s %s %s "%(profiles_folder,file,command.split('(')[0],functions,command))
return


def Profile(command="lock(xpapoint=name, argv='-x %s -f image -c image -l 1 -l 1 -m 1')",profiles_folder=profiles_folder,functions=[lock,]):
result=StringIO()
file = command.split('(')[0]

LineProfiler(*functions).run(command).print_stats(result)
result=result.getvalue()
print(result)
# chop the string into a csv-like buffer
# result='ncalls'+result.split('ncalls')[-1]
# result='\n'.join([','.join(line.rstrip().split(None,6)) for line in result.split('\n')])
# save it to disk
f=open('%s/Prun_%s.py'%(profiles_folder, file),'w')
f.write(result)
f.close()
return

#Profile(file='prun_open.py',command="DS9open(None,'%s/stack.fits')"%(files_folder))

def main():
d=DS9()
xpapoint = d.get('xpa').split('\t')[-1]
name = xpapoint
print('\n Setup \n' )
#os.system('DS9Utils %s open " %s/stack.fits" Slice 0 '%(name,files_folder))
# Profile(command="DS9open(None,'%s/stack.fits')"%(files_folder))



Profile(command="open_file('%s',argv=' -p %s -t Slice -c 0 ')"%(name,im),functions=[open_file,])
# ipython.magic("lprun -u 1e-1 -T /tmp/prun_open.py -s -r -f DS9open -f get DS9open('%s','%s/stack.fits') "%(name,files_folder))
# ipython.magic("lprun -u 1e-1 -T /tmp/prun_get.py -s -r -f d.get d.get('file')" )

Profile(command="lock('%s',argv='-f image -c image -l 1 -l 1 -m 1')"%(name),functions=[lock,])
Profile(command="lock('%s',argv='-f wcs -c wcs -l 1 -l 1 -m 1')"%(name),functions=[lock,])
Profile(command="lock('%s',argv='-f none -c none -l 1 -l 0 -m 1')"%(name),functions=[lock,])

Profile(command="setup(argv='-x %s')"%(name),functions=[setup,])
d=DS9n()
d.set('regions command "circle 100 100 20"')
d.set('regions select all')
Profile(command="setup(argv='-x %s')"%(name),functions=[setup,])

Profile(command="original_settings(argv='-x %s')"%(name),functions=[original_settings,])
d.set('regions delete all')
d.set('regions command "circle 100 100 200"')
d.set('regions select all')
Profile(command="setup(argv='-x %s')"%(name),functions=[setup,])


print('\n Header \n' )

# Profile(command="DS9CreateHeaderCatalog('%s')"%(xpapoint),argv=['?',"%s"%(resource_filename('pyds9plugin', 'Images/*.fits')),'0','-'])
os.system('DS9Utils create_header_catalog -x %s -p "%s" '%(name, '/Users/Vincent/Github/pyds9plugin/pyds9plugin/Images/*.fits'))#,functions='-f CreateCatalog_new')
# create_header_catalog(arv="-x %s -p '%s' -n 0 "%(name,))

Profile(command="organize_files(argv='-x %s -p %s')"%(name,glob.glob('/Users/Vincent/DS9QuickLookPlugIn/HeaderDataBase/*.csv')[0]),functions=[organize_files,])

# os.system('DS9Utils %s AddHeaderField test 1 no_comment "%s/Desktop/*.fits" '%( os.environ['HOME'], name))

# print('\n Regions \n' )

# d.set('regions command "circle 100 100 200"')
# Profile(command="DS9Region2Catalog('%s')"%(xpapoint),argv=[files_folder+'/test.csv'])
# d.set('regions delete all')
# Profile(command="DS9Catalog2Region('%s')"%(xpapoint),argv=['0', files_folder+'/test.csv','x,y','-','circle','10','0','-'])
# d.set('regions select all')
# Profile(command="ReplaceWithNans('%s')"%(xpapoint),argv=['nan','0'],functions='-f InterpolateNaNs -f fitswrite')


print('\n Image Processing \n' )



Profile(command="python_command(argv = '-x %s -e ds9+=np.random.normal(0,0.5*np.nanstd(ds9),size=ds9.shape)')"%(name),functions=[python_command,])
DS9Plot(d,path='%s/test.dat'%(test_folder))

#Profile(command="BackgroundFit1D('%s')"%(xpapoint),argv='x none none 1 1 1 0 none'.split(' '))
d.set('regions delete all')
d.set('regions command "circle 477 472 20"')
d.set('regions select all')
Profile(command="fit_gaussian_2d(argv = '-x %s -p 1')"%(name),functions=[fit_gaussian_2d,])


# Profile(command="DS9InterpolateNaNs('%s')"%(xpapoint),argv=[d.get('file')],functions='-f InterpolateNaNs -f fitswrite')

d.set('regions delete all')
d.set('regions command "box 477 472 100 99"')
d.set('regions select all')
Profile(command="trim(argv = '-x %s')"%(name),functions=[trim,])
d.set('frame delete')

print('\n Other \n' )

print('\n INSTRUMENTATION AIT \n' )
d.set('regions command "circle 500 500 50"');d.set('regions select all')
Profile(command="radial_profile(argv = '-x %s')"%(name),functions=[radial_profile,])
d.set('regions delete all')
d.set('regions command "circle 500 500 50"');d.set('regions select all')

Profile(command="center_region(argv = '-x %s')"%(name),functions=[center_region,])


Profile(command="open_file('%s',argv=' -p %s -t Slice -c 0 ')"%(name,im),functions=[open_file,])

Profile(command="RunSextractor(argv='-x %s')"%(name),functions=[RunSextractor,])



d.set('regions delete all')
d.set('regions command "circle 50 50 20"')
d.set('regions select all')
Profile(command="plot_3d(argv='-x %s')"%(name),functions=[plot_3d,])


print('\n TEST COMPLETED 100% \n' )




return

# d.set('plot current add ')#line|bar|scatter
# d.set('plot load %s %s'%('/Users/Vincent/Github/DS9functions/pyds9plugin/testing/test.dat', 'xy'))
# d.set('plot current graph 1')
# d.set('plot load %s %s'%('/Users/Vincent/Github/DS9functions/pyds9plugin/testing/test.dat', 'xy'))
# d.set("plot title {%s}"%(title))
# d.set("plot title x {%s}"%(xlabel))
# d.set("plot title y {%s}"%(ylabel))

if __name__ == '__main__':
try:
os.system('echo 0 > %s'%(DS9_BackUp_path + '/.verbose.txt'))
os.system('echo 0 > %s'%(DS9_BackUp_path + '/.message.txt'))
copyfile(im, test_folder + '/files/' + os.path.basename(im))
a = main()
[os.remove(file) for file in glob.glob(os.environ['HOME'] + '/Github/DS9functions/pyds9plugin/testing/files/*')]

finally:
os.system('echo 0 > %s'%(DS9_BackUp_path + '/.verbose.txt'))
os.system('echo 1 > %s'%(DS9_BackUp_path + '/.message.txt'))


# os.system('DS9Utils %s '%(name))
# os.system('DS9Utils %s '%(name))
Loading

0 comments on commit 8516b25

Please sign in to comment.