Hi
I am writing a small script which will create a mask file interactively. The script is working fine but when I use this in another script which imports pyraf, it seems hanging. I am attaching the script here. could anybody help me?
thanks
Vinu V
The main program:
#!/usr/bin/env python
from pyraf import iraf
from manualmaskfunc1 import *
ManualMaskManager('I_EDCSNJ1216453-1201176.fits')
It calls manualmaskfunc1.py and it is as follows
from matplotlib.widgets import Lasso
from matplotlib.nxutils import points_inside_poly
import pyfits
from pylab import figure, show, nx, close, draw
import numpy as n
from matplotlib.patches import Polygon
import config as c
import os
#class ManualMaskManager:
class ManualMaskManager:
"""This class generate mask interactevely"""
def __init__(self, cutimage):
self.cutimage = cutimage
f = pyfits.open(self.cutimage)
self.z = f[0].data
f.close()
self.size = self.z.shape[0]
self.x = n.reshape(n.arange(self.size * self.size), (self.size, self.size)) % self.size
self.x =self.x.astype(n.float32)
self.y = n.reshape(n.arange(self.size * self.size), (self.size, self.size)) / self.size
self.y = self.y.astype(n.float32)
fig = figure()
ax = fig.add_subplot(111, autoscale_on=False)
ax.contourf(self.x, self.y, self.z, 50)
# self.lman = ManualMaskManager(self.cutimage, ax, x, y, z, size)
# show()
self.axes = ax
self.canvas = ax.figure.canvas
self.mask = n.zeros((self.size, self.size), int)
# self.axes.contourf(x,y,z, 50)
x1 = n.reshape(self.x, (self.size * self.size, 1))
y1 = n.reshape(self.y, (self.size * self.size, 1))
self.xy = n.concatenate((x1,y1),axis=1)
self.Nxy = self.size * self.size
self.cid = self.canvas.mpl_connect('button_press_event', self.onpress)
self.cid2 = self.canvas.mpl_connect('key_press_event', self.onpress1) show()
# return
def bad_pixel(self, ind):
for element in ind:
self.mask[element / self.size, element % self.size] = 1
def bad_pixel_remove(self, ind):
for element in ind:
self.mask[element / self.size, element % self.size] = 0
def write_fits(self):
try:
os.remove('M_' + str(self.cutimage)[:-5] + '.fits')
except:
pass
hdu = pyfits.PrimaryHDU(self.mask.astype(n.float32))
hdu.writeto('M_' + str(self.cutimage)[:-5] + '.fits')
close()
def onpress1(self, event):
if event.key=='a':
self.bad_pixel(self.ind)
accept = 'yes'
print accept
if event.key=='c':
self.bad_pixel_remove(self.ind)
rect = Polygon(self.ver, facecolor='red', alpha=0.5)
self.axes.add_patch(rect)
self.canvas.draw_idle()
accept = 'no'
print accept
elif event.key=='q':
self.canvas.mpl_disconnect(self.cid)
self.canvas.mpl_disconnect(self.cid2)
self.write_fits()
def callback(self, verts):
self.ind = nx.nonzero(points_inside_poly(self.xy, verts))
self.ver = verts
rect = Polygon(self.ver, facecolor='green', alpha=0.5)
self.axes.add_patch(rect)
self.canvas.draw_idle()
def onpress(self, event):
self.lasso = Lasso(event.inaxes, (event.xdata, event.ydata), self.callback)