Exemplo n.º 1
0
def atualizar_setor(setor: Wedge, abertura_min: float) -> [Wedge]:
    """Atualiza os valores de theta e opacidade do setor, movimentando-o"""

    global glob_multip_alpha

    # O valor de theta = valor atual + incremento (Se a soma não ultrapassar 360)
    # Se a soma ultrapassar 360, o valor será o resto da divisão (soma / 360)
    setor.set_theta1((setor.theta1 + glob_increm_ang) % 360)
    setor.set_theta2(setor.theta1 + abertura_min)

    # Se a opacidade do setor for maior que 0.4 ou for menor que o valor mínimo:
    # inverta de incremento para decremento ou vice-versa
    if setor.get_alpha() >= 0.4 or setor.get_alpha() < glob_incremento_alpha:
        glob_multip_alpha = (-1) * glob_multip_alpha

    setor.set_alpha(setor.get_alpha() +
                    glob_incremento_alpha * glob_multip_alpha)

    return [setor]
Exemplo n.º 2
0
class InteractivePlot:
    '''
	Produces 3 figures for manipulation of LRM images:
	
	image_fig, imaxes -- figure and axes objects associated with LRM output image (TIF, fit format).  The image plot within 
							imaxes is named image, while the array used for generating the plot is imdata.
							
	xsection -- figure containing cross section along a line through the diameter of the integration ring.  Points of the line are chosen by 
				shift + clicking (also resizes/repositions circle).  Shift + clicking within this figure will draw an additional
				line for indicating the background noise level.
				
	result_fig -- figure displaying cross section after subtraction of background noise.
	'''
    def __init__(self, fname, beampwr, wls, inttime, radii=None):

        self.image_fig = pl.figure(1)
        self.imaxes = self.image_fig.add_subplot(111)

        self.xsection = pl.figure(2)
        self.xsec_axes = self.xsection.add_subplot(111)
        self.xsec_line = self.xsec_axes.plot([], [])[0]
        self.xsec_scatrlineL = self.xsec_axes.plot([], [], color='g')[0]
        self.xsec_scatrlineR = self.xsec_axes.plot([], [], color='g')[0]

        self.result_fig = pl.figure(3)
        self.result_axes = self.result_fig.add_subplot(111)
        self.result_line = self.result_axes.plot([], [])[0]

        self.center = (0, 0)
        self.inrad = 0
        self.outrad = 0
        self.rad1 = 0
        self.rad2 = 0

        self.scatr_y0 = 0
        self.scatr_y1 = 0

        self.inttime = inttime
        self.beampwr = beampwr
        self.radii = radii
        self.wls = wls
        self.wli = 0
        self.path = os.path.dirname(fname)

        self.update_image()
        self.line = self.imaxes.plot([], [], lw='3')[0]

        #reset image bounds
        xmin = 0
        xmax = pl.shape(self.imdata)[1]
        ymin = 0
        ymax = pl.shape(self.imdata)[0]
        im_bounds = [xmin, xmax, ymin, ymax]
        self.imaxes.axis(im_bounds)

        self.pwr = []
        self.pce = []

        if radii != None:
            self.inrad = min(radii)
            self.outrad = max(radii)

        self.movering = False  # boolean indicating whether mouse clicks should redraw the ring shape
        self.clickevent = self.image_fig.canvas.mpl_connect(
            'button_press_event', self.click)
        self.keydownevent = self.image_fig.canvas.mpl_connect(
            'key_press_event', self.keydown)
        self.keyupevent = self.image_fig.canvas.mpl_connect(
            'key_release_event', self.keyup)

        self.xsec_clickevent = self.xsection.canvas.mpl_connect(
            'button_press_event', self.xsec_click)
        self.xsec_keydownevent = self.xsection.canvas.mpl_connect(
            'key_press_event', self.xsec_keydown)

    def update_image(self):

        if self.wli > len(self.wls) - 1:
            pl.close('all')
            return

        wl = int(self.wls[self.wli])
        print wl
        self.imdata = LoadPowerImage(self.path + str(wl) + filetype, wl,
                                     self.inttime[self.wli])[0]

        try:
            self.image.set_array(self.imdata)
            pl.figure(1)
            pl.clim(0, pl.amax(self.imdata))
            self.imaxes.figure.canvas.draw()
        except:
            self.image = self.imaxes.imshow(self.imdata, cmap='hot', vmax=None)

        self.wli += 1

    def update_xsec(self):

        # Generate coordinates at which to find image values
        n = 1000

        x0 = self.line.get_data()[0][0]
        x1 = self.line.get_data()[0][1]
        y0 = self.line.get_data()[1][0]
        y1 = self.line.get_data()[1][1]

        L = pl.sqrt((x1 - x0)**2 + (y1 - y0)**2)
        print "length: %0.2f" % L
        print "angle: %0.2f" % (L * bfp_scaling_factor / 2)

        xs, ys = GetLinePoints(n, x0, x1, y0, y1)

        L = L * bfp_scaling_factor

        xsec_xs = pl.linspace(-L / 2., L / 2, n)
        xsec_ys = map_coordinates(self.imdata, [ys, xs])

        # Set new cross section plot axes limits
        xmin = -L / 2.  #pl.amin(xsec_xs)
        xmax = L / 2.  #pl.amax(xsec_xs)
        ymin = 0
        ymax = pl.amax(self.imdata)
        xsec_bounds = [xmin, xmax, ymin, ymax]

        # Redraw cross section plot

        self.xsec_line.set_data(xsec_xs, xsec_ys)
        self.xsec_axes.axis(xsec_bounds)
        self.xsection.canvas.draw()

        self.DrawscatrLine(xmin=xmin)

    def update_result(self):

        # Generate coordinates at which to find image values
        n = 10000

        x0 = self.line.get_data()[0][0]
        x1 = self.line.get_data()[0][1]
        y0 = self.line.get_data()[1][0]
        y1 = self.line.get_data()[1][1]

        L = pl.sqrt((x1 - x0)**2 + (y1 - y0)**2)

        xs, ys = GetLinePoints(n, x0, x1, y0, y1)

        L = L * bfp_scaling_factor

        res_xs = pl.linspace(-L / 2., L / 2, n)
        res_ys = map_coordinates(self.unNoise(), [ys, xs], order=1)

        # Set new cross section plot axes limits
        xmin = -L / 2.  #pl.amin(xsec_xs)
        xmax = L / 2.  #pl.amax(xsec_xs)
        ymin = 0
        ymax = pl.amax(self.imdata)
        res_bounds = [xmin, xmax, ymin, ymax]

        # Redraw cross section plot

        self.result_line.set_data(res_xs, res_ys)
        self.result_axes.axis(res_bounds)
        self.result_fig.canvas.draw()

    ### Keyboard Event Handler ###
    def keydown(self, event):

        if event.key == 'shift': self.movering = True
        if event.key == 'control':
            self.center = (0, 0)
            self.rad1 = 0
            self.rad2 = 0
            self.DrawRing()

        if event.key == 'enter':
            self.pwr.append(self.getpwr())
            self.pce.append(self.getpwr() / self.beampwr[self.wli - 1])
            self.update_image()
            self.update_xsec()
            self.update_result()

        if event.key == 'escape':
            exit()

    def keyup(self, event):
        if event.key == 'shift': self.movering = False

    def xsec_keydown(self, event):
        if event.key == 'control':
            self.scatr_y0 = self.scatr_y1 = 0
            self.DrawscatrLine()
            self.update_result()

    ### Mouse Click Event Handler ###
    def click(self, event):

        #print "click: x=%f, y=%f" % (event.xdata,event.ydata)
        if not self.movering: return
        if event.inaxes != self.imaxes: return

        if event.button == 1:
            oldcenter = self.center
            self.center = (event.xdata, event.ydata)

        if event.button == 2:
            self.rad1 = pl.sqrt((event.xdata - self.center[0])**2 +
                                (event.ydata - self.center[1])**2)
        if event.button == 3:
            self.rad2 = pl.sqrt((event.xdata - self.center[0])**2 +
                                (event.ydata - self.center[1])**2)

        if self.radii == None:
            self.inrad = min(self.rad1, self.rad2)
            self.outrad = max(self.rad1, self.rad2)

        if self.outrad == self.rad1 and event.button == 2:
            self.DrawLine(event.xdata, event.ydata)
        elif self.outrad == self.rad2 and event.button == 3:
            self.DrawLine(event.xdata, event.ydata)
        elif event.button == 1:
            oldxs, oldys = self.line.get_data()

            delx = self.center[0] - oldcenter[0]
            newx = oldxs[0] + delx

            dely = self.center[1] - oldcenter[1]
            newy = oldys[0] + dely

            self.DrawLine(newx, newy)

        self.DrawRing()

        self.update_xsec()
        self.update_result()
        self.getpwr()

    def xsec_click(self, event):

        if event.button == 1: self.scatr_y0 = event.ydata
        if event.button == 3: self.scatr_y1 = event.ydata

        self.DrawscatrLine()
        self.update_result()
        self.getpwr()

    def DrawCircle(self):

        self.imaxes.patches = []  # Remove existing shapes from figure

        # Filled Region
        self.ring1 = Wedge(
            self.center,  # Ring center
            self.outrad,  # Outer Ring Diameter
            0,
            360,  # range of degrees to sweep through (leave 0-360 for full circle)
            width=0,  # Thickness of ring
            linewidth=1,
            linestyle='dotted',
            edgecolor='w',
            facecolor=None)

        #self.ring1.set_alpha(0.2)										# Set transparency (0-1)
        self.imaxes.add_patch(self.ring1)  # Add updated circle to figure

        self.imaxes.figure.canvas.draw()
        print 'center:', self.center
        print 'inrad:', self.inrad
        print 'outrad:', self.outrad
        print ''
        print self.getpwr()
        print ''

    def DrawLine(self, xdata, ydata):

        delx = xdata - self.center[0]
        dely = ydata - self.center[1]

        self.line.set_data([self.center[0] - delx, xdata],
                           [self.center[1] - dely, ydata])
        self.imaxes.figure.canvas.draw()

    def DrawscatrLine(self, xmin=None):

        # Left line

        if xmin != None:
            nx0 = xmin + (self.outrad - self.inrad) * bfp_scaling_factor
            nx1 = xmin
            ny0 = self.scatr_y0
            ny1 = self.scatr_y1
        else:
            nx0 = self.xsec_scatrlineL.get_data()[0][0]
            nx1 = self.xsec_scatrlineL.get_data()[0][1]
            ny0 = self.scatr_y0
            ny1 = self.scatr_y1

        self.xsec_scatrlineL.set_data([nx0, nx1], [ny0, ny1])

        # Right line

        nx0 *= -1
        nx1 *= -1

        self.xsec_scatrlineR.set_data([nx0, nx1], [ny0, ny1])

        self.xsection.canvas.draw()

    def DrawRing(self):
        self.imaxes.patches = []  # Remove existing shapes from figure

        # Filled Region
        self.ring1 = Wedge(
            self.center,  # Ring center
            self.outrad,  # Outer Ring Diameter
            0,
            360,  # range of degrees to sweep through (leave 0-360 for full circle)
            width=self.outrad - self.inrad,  # Thickness of ring
            linewidth=1,
            edgecolor='none',
            facecolor='g')
        # Edges
        self.ring2 = Wedge(
            self.center,  # Ring center
            self.outrad,  # Outer Ring Diameter
            0,
            360,  # range of degrees to sweep through (leave 0-360 for full circle)
            width=self.outrad - self.inrad,  # Thickness of ring
            linewidth=3,
            edgecolor='g',
            facecolor='none')

        self.ring1.set_alpha(0.2)  # Set transparency (0-1)
        self.imaxes.add_patch(self.ring1)  # Add updated circle to figure
        self.imaxes.add_patch(self.ring2)

        self.imaxes.figure.canvas.draw()
        print 'center:', self.center
        print 'inrad:', self.inrad
        print 'outrad:', self.outrad
        print self.getpwr()
        print ''

    def unNoise(self):
        '''
		applies filters to raw image data based on ring placement, scattering subtraction, and average background noise
		located near the (0,0) point.
		'''
        imrows, imcolumns = pl.shape(self.imdata)
        x0, y0 = self.center
        a = pl.arange(imrows * imcolumns)
        imflat = self.imdata.flatten()
        ringmask = (((divmod(a,imcolumns)[1]-x0)**2 + (divmod(a,imcolumns)[0]-y0)**2) < self.outrad**2) * \
          (((divmod(a,imcolumns)[1]-x0)**2 + (divmod(a,imcolumns)[0]-y0)**2) > self.inrad**2)

        #radial position with 0 at inner ring boundary
        ringpos = (pl.sqrt((divmod(a, imcolumns)[1] - x0)**2 +
                           (divmod(a, imcolumns)[0] - y0)**2) - self.inrad)

        #slope of scattering line in xsection plot
        slope = (self.scatr_y1 - self.scatr_y0) / ((self.outrad - self.inrad))

        sctrmask = slope * ringpos + self.scatr_y0

        SPPImFlat = (imflat - sctrmask) * ringmask

        # remove negative values
        SPPImFlat = SPPImFlat * (SPPImFlat >= 0)

        SPPimage = SPPImFlat.reshape(imrows, imcolumns)

        return SPPimage

    def iterate(self):
        ipwr = self.getpwr()
        self.pwr.append(ipwr)
        self.pce.append(ipwr / beampwr[i])

    def getpwr(self):
        pwr = pl.sum(self.unNoise())

        print 'std pwr:', pwr

        return pwr