Example #1
0
    def processImage(self, fileBuffer):
        # tmp_name = uuid.uuid4().__str__() + ".png"
        try:

            image = Image.open(fileBuffer)
            image.thumbnail(self.size)
            converted = image.convert("L")
            # converted = ImageEnhance.Contrast(converted).enhance(1.1)
            # converted = ImageEnhance.Brightness(converted).enhance(1.1)
            converted = ImageEnhance.Sharpness(converted).enhance(1.4)

            # image = np.array(converted)
            image = matplotlib.image.pil_to_array(converted)
            binary_adaptive = threshold_adaptive(image, 40, offset=10)

            figsize = [
                x / float(self._dpi)
                for x in (binary_adaptive.shape[1], binary_adaptive.shape[0])
            ]
            fig = Figure(figsize=figsize, dpi=self._dpi, frameon=False)
            canvas = FigureCanvasAgg(fig)
            fig.figimage(binary_adaptive)

            output = StringIO()
            fig.savefig(output, format='png')
            output.seek(0)

            self.outfiles.append(output)
        except IOError:
            self.invalidFiles.append(fileBuffer)
Example #2
0
def visualize_array(src, cmap="gray", f_name=None):
    """

    :param src: image source array [c, h, w]
    :param cmap:
    :return: depth image visualized by selected color map (matplotlib.image object)
    """

    if isinstance(src, np.ndarray):
        src = src
    elif isinstance(src, torch.Tensor):
        src = src.data.numpy()
    else:
        raise NotImplementedError()

    from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
    from matplotlib.figure import Figure

    fig = Figure(dpi=100, frameon=False)
    canvas = FigureCanvas(fig)

    if src.shape[0] == 1:
        im = fig.figimage(src[0], cmap=cmap, resize=True)
    else:
        im = fig.figimage(src.transpose(1, 2, 0), resize=True)

    if f_name is not None:
        fig.savefig(f_name, dpi=100, transparent=True)
        return fig
    else:
        return fig
Example #3
0
    def processImage(self, fileBuffer):
        # tmp_name = uuid.uuid4().__str__() + ".png"
        try:

            image = Image.open(fileBuffer)
            image.thumbnail(self.size)
            converted = image.convert("L")
            # converted = ImageEnhance.Contrast(converted).enhance(1.1)
            # converted = ImageEnhance.Brightness(converted).enhance(1.1)
            converted = ImageEnhance.Sharpness(converted).enhance(1.4)

            # image = np.array(converted)
            image = matplotlib.image.pil_to_array(converted)
            binary_adaptive = threshold_adaptive(image, 40, offset=10)

            figsize = [x / float(self._dpi) for x in (binary_adaptive.shape[1], binary_adaptive.shape[0])]
            fig = Figure(figsize=figsize, dpi=self._dpi, frameon=False)
            canvas = FigureCanvasAgg(fig)
            fig.figimage(binary_adaptive)

            output = StringIO()
            fig.savefig(output, format='png')
            output.seek(0)

            self.outfiles.append(output)
        except IOError:
            self.invalidFiles.append(fileBuffer)
Example #4
0
class GraphFrame(wx.Frame):
    def __init__(self, maze_width=70, maze_height=70, magnification=8):
        wx.Frame.__init__(self, None, -1, 'Maze Generator')
        self.Bind(wx.EVT_CLOSE, self.OnClose, self)

        self.running = False

        self.maze_width = maze_width
        self.maze_height = maze_height
        self.magnification = magnification

        sizer = wx.BoxSizer(wx.VERTICAL)
        hSizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(hSizer)

        btnStart = wx.Button(self, -1, "Start")
        self.Bind(wx.EVT_BUTTON, self.OnStart, btnStart)
        hSizer.Add(btnStart)

        btnStop = wx.Button(self, -1, "Stop")
        self.Bind(wx.EVT_BUTTON, self.OnStop, btnStop)
        hSizer.Add(btnStop)

        self.fig = Figure()
        self.canvas = FigureCanvasWxAgg(self, -1, self.fig)
        sizer.Add(self.canvas, 1, wx.LEFT|wx.TOP|wx.GROW)

        self.SetSizer(sizer)
        self.SetSize((800, 500))

    def OnClose(self, evt):
        self.running = False
        self.plot_thread.join()
        evt.Skip()

    def OnStart(self, evt):
        self.Plot()

    def OnStop(self, evt):
        self.running = False

    def Plot(self):
        if self.running:
            return

        def thread():
            self.running = True
            for m in maze(self.maze_width, self.maze_height):
                if not self.running:
                    break
                wx.CallAfter(self.plot_data, magnify(m, magnification=self.magnification))
                time.sleep(0.05)
            
        self.plot_thread = threading.Thread(target=thread)
        self.plot_thread.start()

    def plot_data(self, snapshot):
        self.fig.clear()
        self.fig.figimage(snapshot, 0, 0, cmap=plt.cm.binary)
        self.canvas.draw()
Example #5
0
    def save_as_plt(self, fname, pixel_array=None, vmin=None, vmax=None,
        cmap=None, format=None, origin=None):
        """ This method saves the image from a numpy array using matplotlib

        :param fname: Location and name of the image file to be saved.
        :param pixel_array: Numpy pixel array, i.e. ``numpy()`` return value
        :param vmin: matplotlib vmin
        :param vmax: matplotlib vmax
        :param cmap: matplotlib color map
        :param format: matplotlib format
        :param origin: matplotlib origin

        This method will return True if successful
        """
        from matplotlib.backends.backend_agg \
        import FigureCanvasAgg as FigureCanvas
        from matplotlib.figure import Figure
        from pylab import cm

        if pixel_array is None:
            pixel_array = self.numpy()

        if cmap is None:
            cmap = cm.bone
        fig = Figure(figsize=pixel_array.shape[::-1], dpi=1, frameon=False)
        canvas = FigureCanvas(fig)
        fig.figimage(pixel_array, cmap=cmap, vmin=vmin,
            vmax=vmax, origin=origin)
        fig.savefig(fname, dpi=1, format=format)
        return True
Example #6
0
 def plot(self, fig: Figure):
     fig.figimage(
         self.logo_img,
         xo=(self.img_width * self.dpi - self.logo_img.shape[1]) / 2,
         yo=(self.img_height * self.dpi - self.logo_img.shape[0]) / 2,
         alpha=0.4,
     )
Example #7
0
    def save_as_plt(self, fname, pixel_array=None, vmin=None, vmax=None,
        cmap=None, format=None, origin=None):
        """ This method saves the image from a numpy array using matplotlib

        :param fname: Location and name of the image file to be saved.
        :param pixel_array: Numpy pixel array, i.e. ``numpy()`` return value
        :param vmin: matplotlib vmin
        :param vmax: matplotlib vmax
        :param cmap: matplotlib color map
        :param format: matplotlib format
        :param origin: matplotlib origin

        This method will return True if successful
        """
        from matplotlib.backends.backend_agg \
        import FigureCanvasAgg as FigureCanvas
        from matplotlib.figure import Figure
        from pylab import cm

        if pixel_array is None:
            pixel_array = self.numpy

        if cmap is None:
            cmap = cm.bone
        fig = Figure(figsize=pixel_array.shape[::-1], dpi=1, frameon=False)
        canvas = FigureCanvas(fig)
        fig.figimage(pixel_array, cmap=cmap, vmin=vmin,
            vmax=vmax, origin=origin)
        fig.savefig(fname, dpi=1, format=format)
        return True
def _save_figure(img, filename):
    from matplotlib.backends.backend_agg import FigureCanvasAgg
    from matplotlib.figure import Figure
    fig = Figure(frameon=False)
    FigureCanvasAgg(fig)
    fig.figimage(img, resize=True)
    fig.savefig(filename)
Example #9
0
def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None, origin=None):
    """
    Saves a 2D :class:`numpy.array` as an image with one pixel per element.
    The output formats available depend on the backend being used.

    Arguments:
      *fname*:
        A string containing a path to a filename, or a Python file-like object.
        If *format* is *None* and *fname* is a string, the output
        format is deduced from the extension of the filename.
      *arr*:
        A 2D array.
    Keyword arguments:
      *vmin*/*vmax*: [ None | scalar ]
        *vmin* and *vmax* set the color scaling for the image by fixing the
        values that map to the colormap color limits. If either *vmin* or *vmax*
        is None, that limit is determined from the *arr* min/max value.
      *cmap*:
        cmap is a colors.Colormap instance, eg cm.jet.
        If None, default to the rc image.cmap value.
      *format*:
        One of the file extensions supported by the active
        backend.  Most backends support png, pdf, ps, eps and svg.
      *origin*
        [ 'upper' | 'lower' ] Indicates where the [0,0] index of
        the array is in the upper left or lower left corner of
        the axes. Defaults to the rc image.origin value.
    """
    from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
    from matplotlib.figure import Figure

    fig = Figure(figsize=arr.shape[::-1], dpi=1, frameon=False)
    canvas = FigureCanvas(fig)
    fig.figimage(arr, cmap=cmap, vmin=vmin, vmax=vmax, origin=origin)
    fig.savefig(fname, dpi=1, format=format)
Example #10
0
def imsave(fname, arr, vmin=None, vmax=None, cmap='gray', format=None, origin=None):
    from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
    from matplotlib.figure import Figure

    fig = Figure(figsize=arr.shape[::-1], dpi=1, frameon=False)
    canvas = FigureCanvas(fig)
    fig.figimage(arr, cmap=cmap, vmin=vmin, vmax=vmax, origin=origin)
    fig.savefig(fname, dpi=1, format=format)
Example #11
0
def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None, origin=None):
    from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
    from matplotlib.figure import Figure

    fig = Figure(figsize=arr.shape[::-1], dpi=1, frameon=False)
    canvas = FigureCanvas(fig)
    fig.figimage(arr, cmap=cmap, vmin=vmin, vmax=vmax, origin=origin)
    fig.savefig(fname, dpi=1, format=format)
Example #12
0
class MyCanvas(FigureCanvas):
	
	def __init__(self, parent=None, width=5, height=4, dpi=100):
		
		plt.rcParams['font.family'] = ['SimHei']
		plt.rcParams['axes.unicode_minus'] = False
		
		self.fig = Figure(figsize=(width, height), dpi=dpi)
		
		super().__init__(self.fig)
		self.setParent(parent)
		
		self.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
		self.updateGeometry()
	
	def start_static_plot(self, picture, flag='gray'):
		
		if flag == 'gray':
			
			self.fig.suptitle('灰度图像')
			self.fig.figimage(picture, resize=True, cmap='gray')
		
		elif flag == 'D3D':
			
			self.fig.suptitle('3D图像')
			self.axes = self.fig.add_subplot(1, 1, 1, projection='3d')
			self.D3D = picture.copy()
			self.pictureheight3D, self.picturewidth3D = self.D3D.shape[:2]
			self.x3D = np.arange(0, self.picturewidth3D, 1)
			self.y3D = np.arange(0, self.pictureheight3D, 1)
			self.x3D, self.y3D = np.meshgrid(self.x3D, self.y3D)
			self.D3D = cv2.flip(self.D3D, 0)
			self.axes.plot_surface(self.x3D, self.y3D, self.D3D[self.y3D, self.x3D], rstride=2, cstride=2,
			                       cmap='rainbow')
		
		elif flag == 'contour':
			
			self.fig.suptitle('等温线图像')
			self.axes = self.fig.add_subplot(1, 1, 1)
			self.contour = picture.copy()
			self.pictureheight, self.picturewidth = self.contour.shape[:2]
			self.xcontour = np.arange(0, self.picturewidth, 1)
			self.ycontour = np.arange(0, self.pictureheight, 1)
			self.xcontour, self.ycontour = np.meshgrid(self.xcontour, self.ycontour)
			self.contour = cv2.flip(self.contour, 0)
			self.contourline = self.axes.contour(self.xcontour, self.ycontour,
			                                     self.contour[self.ycontour, self.xcontour], 10, colors='black',
			                                     linewidths=1)
			self.axes.clabel(self.contourline, inline=True, inline_spacing=5, fontsize=10, fmt='%.1f')
			self.axes.contourf(self.xcontour, self.ycontour, self.contour[self.ycontour, self.xcontour], 10,
			                   cmap='rainbow')
		
		elif flag == 'original':
			
			self.fig.suptitle('原始图像')
			self.fig.figimage(picture, resize=True)
Example #13
0
def saveImage(file_path, img, vmin=None, vmax=None, cmap=None, format=None, origin=None):
    """ Save numpy array to disk as image. """

    from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
    from matplotlib.figure import Figure

    fig = Figure(figsize=img.shape[::-1], dpi=1, frameon=False)
    FigureCanvas(fig)
    fig.figimage(img, cmap=cmap, vmin=vmin, vmax=vmax, origin=origin)
    fig.savefig(file_path, dpi=1, format=format)
def save_array_as_image(filename, array, colourmap=None, format=None, dpi=1, vmin=None, vmax=None, origin=None):
    from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
    from matplotlib.figure import Figure
    from matplotlib import cm

    figure = Figure(figsize=array.shape[::-1], dpi=1, frameon=False)
#    canvas = FigureCanvas(figure) # essential even though it isn't used
    FigureCanvas(figure) # essential even though it isn't used
    figure.figimage(array, cmap=cm.get_cmap(colourmap), vmin=vmin, vmax=vmax, origin=origin)
    figure.savefig(filename, dpi=dpi, format=format)
Example #15
0
def simulate(x, y, alpha, beta, gamma):
  img = Image.open('./public/population-density.png')
  width = img.size[0]//6
  height = img.size[1]//6
  img = img.resize((width, height)) 
  img = 255 - np.asarray(img)

  S_0 = img[:,:,1]
  I_0 = np.zeros_like(S_0)
  I_0[int(height * y), int(width * x)] = 1

  R_0 = np.zeros_like(S_0)

  T = 900                         # final time
  dt = 1                          # time increment
  N = int(T/dt) + 1               # number of time-steps
  t = np.linspace(0.0, T, N)      # time discretization

  # initialize the array containing the solution for each time-step
  u = np.empty((N, 3, S_0.shape[0], S_0.shape[1]))
  u[0][0] = S_0
  u[0][1] = I_0
  u[0][2] = R_0

  theCM = cm.get_cmap("Reds")
  theCM._init()
  alphas = np.abs(np.linspace(0, 1, theCM.N))
  theCM._lut[:-3,-1] = alphas


  # Compute data
  for n in range(N-1):
      u[n+1] = euler_step(u[n], f, dt, beta, gamma, alpha)

  keyFrames = []
  frames = 60.0 # upper limit on keyframes

  for i in range(0, N-1, int(N/frames)):
    fig = Figure()
    fig.figimage(img, resize=True)
    fig.figimage(u[i][1], vmin=0, cmap=theCM)

    canvas = FigureCanvas(fig)
    canvas.draw()

    rows, cols = canvas.get_width_height()

    keyFrames.append(np.fromstring(canvas.tostring_rgb(), dtype=np.uint8).reshape(cols, rows, 3))

  # Couldn't figure out a way to use BytesIO directly
  p = os.path.join(os.getenv('DATA_DIR'), "generated", uuid.uuid4().hex + ".mp4")

  imageio.mimsave(p, keyFrames, fps=5)

  return p
Example #16
0
def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None,
           origin=None, dpi=100):
    """
    Save an array as in image file.

    The output formats available depend on the backend being used.

    Arguments:
      *fname*:
        A string containing a path to a filename, or a Python file-like object.
        If *format* is *None* and *fname* is a string, the output
        format is deduced from the extension of the filename.
      *arr*:
        An MxN (luminance), MxNx3 (RGB) or MxNx4 (RGBA) array.
    Keyword arguments:
      *vmin*/*vmax*: [ None | scalar ]
        *vmin* and *vmax* set the color scaling for the image by fixing the
        values that map to the colormap color limits. If either *vmin*
        or *vmax* is None, that limit is determined from the *arr*
        min/max value.
      *cmap*:
        cmap is a colors.Colormap instance, e.g., cm.jet.
        If None, default to the rc image.cmap value.
      *format*:
        One of the file extensions supported by the active
        backend.  Most backends support png, pdf, ps, eps and svg.
      *origin*
        [ 'upper' | 'lower' ] Indicates where the [0,0] index of
        the array is in the upper left or lower left corner of
        the axes. Defaults to the rc image.origin value.
      *dpi*
        The DPI to store in the metadata of the file.  This does not affect the
        resolution of the output image.
    """
    from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
    from matplotlib.figure import Figure

    # Fast path for saving to PNG
    if (format == 'png' or format is None or
            isinstance(fname, six.string_types) and
            fname.lower().endswith('.png')):
        image = AxesImage(None, cmap=cmap, origin=origin)
        image.set_data(arr)
        image.set_clim(vmin, vmax)
        image.write_png(fname)
    else:
        fig = Figure(dpi=dpi, frameon=False)
        FigureCanvas(fig)
        fig.figimage(arr, cmap=cmap, vmin=vmin, vmax=vmax, origin=origin,
                     resize=True)
        fig.savefig(fname, dpi=dpi, format=format, transparent=True)
Example #17
0
def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None,
           origin=None, dpi=100):
    """
    Save an array as in image file.

    The output formats available depend on the backend being used.

    Arguments:
      *fname*:
        A string containing a path to a filename, or a Python file-like object.
        If *format* is *None* and *fname* is a string, the output
        format is deduced from the extension of the filename.
      *arr*:
        An MxN (luminance), MxNx3 (RGB) or MxNx4 (RGBA) array.
    Keyword arguments:
      *vmin*/*vmax*: [ None | scalar ]
        *vmin* and *vmax* set the color scaling for the image by fixing the
        values that map to the colormap color limits. If either *vmin*
        or *vmax* is None, that limit is determined from the *arr*
        min/max value.
      *cmap*:
        cmap is a colors.Colormap instance, e.g., cm.jet.
        If None, default to the rc image.cmap value.
      *format*:
        One of the file extensions supported by the active
        backend.  Most backends support png, pdf, ps, eps and svg.
      *origin*
        [ 'upper' | 'lower' ] Indicates where the [0,0] index of
        the array is in the upper left or lower left corner of
        the axes. Defaults to the rc image.origin value.
      *dpi*
        The DPI to store in the metadata of the file.  This does not affect the
        resolution of the output image.
    """
    from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
    from matplotlib.figure import Figure

    # Fast path for saving to PNG
    if (format == 'png' or format is None or
            isinstance(fname, six.string_types) and
            fname.lower().endswith('.png')):
        image = AxesImage(None, cmap=cmap, origin=origin)
        image.set_data(arr)
        image.set_clim(vmin, vmax)
        image.write_png(fname)
    else:
        fig = Figure(dpi=dpi, frameon=False)
        FigureCanvas(fig)
        fig.figimage(arr, cmap=cmap, vmin=vmin, vmax=vmax, origin=origin,
                     resize=True)
        fig.savefig(fname, dpi=dpi, format=format, transparent=True)
Example #18
0
def _build_image(data, cmap='gray'):
    """Build an image encoded in base64.
    """

    import matplotlib.pyplot as plt
    from matplotlib.figure import Figure
    from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas

    figsize = data.shape[::-1]
    if figsize[0] == 1:
        figsize = tuple(figsize[1:])
        data = data[:, :, 0]
    fig = Figure(figsize=figsize, dpi=1.0, frameon=False)
    FigureCanvas(fig)
    cmap = getattr(plt.cm, cmap, plt.cm.gray)
    fig.figimage(data, cmap=cmap)
    output = BytesIO()
    fig.savefig(output, dpi=1.0, format='png')
    return output.getvalue().encode('base64')
Example #19
0
    def __init__(self, parent=None, width=5.35984, height=2.37743, dpi=100):
        fig = Figure(figsize=(width, height), dpi=dpi)
        self.axes = fig.add_subplot(
            111
        )  #1x1x1 grid;see https://stackoverflow.com/questions/3584805/in-matplotlib-what-does-the-argument-mean-in-fig-add-subplot111
        im = Image.open("map.png")
        im = im.resize((416, 183))
        fig.figimage(im, xo=67,
                     yo=26)  #maybe i'll just resize image and reset origin lol
        self.axes.patch.set_alpha(0)

        #self.axes is of the class AxesSubplot.

        FigureCanvas.__init__(self, fig)
        self.setParent(parent)

        FigureCanvas.setSizePolicy(self, QtWidgets.QSizePolicy.Expanding,
                                   QtWidgets.QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)
Example #20
0
def imsave(fname,
           arr,
           vmin=None,
           vmax=None,
           cmap=None,
           format=None,
           origin=None):
    """
    Saves a 2D :class:`numpy.array` as an image with one pixel per element.
    The output formats available depend on the backend being used.

    Arguments:
      *fname*:
        A string containing a path to a filename, or a Python file-like object.
        If *format* is *None* and *fname* is a string, the output
        format is deduced from the extension of the filename.
      *arr*:
        A 2D array.
    Keyword arguments:
      *vmin*/*vmax*: [ None | scalar ]
        *vmin* and *vmax* set the color scaling for the image by fixing the
        values that map to the colormap color limits. If either *vmin* or *vmax*
        is None, that limit is determined from the *arr* min/max value.
      *cmap*:
        cmap is a colors.Colormap instance, eg cm.jet.
        If None, default to the rc image.cmap value.
      *format*:
        One of the file extensions supported by the active
        backend.  Most backends support png, pdf, ps, eps and svg.
      *origin*
        [ 'upper' | 'lower' ] Indicates where the [0,0] index of
        the array is in the upper left or lower left corner of
        the axes. Defaults to the rc image.origin value.
    """
    from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
    from matplotlib.figure import Figure

    fig = Figure(figsize=arr.shape[::-1], dpi=1, frameon=False)
    canvas = FigureCanvas(fig)
    fig.figimage(arr, cmap=cmap, vmin=vmin, vmax=vmax, origin=origin)
    fig.savefig(fname, dpi=1, format=format)
Example #21
0
def imsave(fname, arr, **kwargs):

    from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
    from matplotlib.figure import Figure
    from matplotlib.colors import ColorConverter as CC
    C = CC()
    
    import pylab
    if pylab.isinteractive():
        i_was_on = True
        pylab.ioff()
    else:
        i_was_on = False
        
    fig = Figure(figsize=arr.shape[::-1], dpi=1, frameon=False)
    canvas = FigureCanvas(fig)

    if 'background' in kwargs.keys():
        if kwargs['background'] != 'transparent':
            [r,g,b] = C.to_rgb(kwargs['background'])
            BG = numpy.ones(shape=(arr.shape[0],arr.shape[1],3))
            BG[:,:,0] = r
            BG[:,:,1] = g
            BG[:,:,2] = b
            fig.figimage(BG)

    fig.figimage(arr,
                 xo = kwargs.get('xo',0),
                 yo = kwargs.get('yo',0),
                 alpha = kwargs.get('alpha',None),
                 norm = kwargs.get('norm',None),
                 cmap = kwargs.get('cmap',None),
                 vmin = kwargs.get('vmin',None),
                 vmax = kwargs.get('vmax',None),
                 origin = kwargs.get('origin',None))
    
    fig.savefig(fname,
                dpi=1,
                format = kwargs.get('format',None))
    if i_was_on:
        pylab.ion()
Example #22
0
def save(filename, np_array, png=False):
    """ saves numpy array as bitmap and/or png image. A list of details
    may be included to save relevant info about the images in a CSV file
    of the same name. By default only a bitmap is produced.

    Parameters
    ----------

    filename : string
        Filename (without extension).

    np_array : numpy.array
        2D numpy array (dtype=uint8) representing grayscale image to save.

    png : bool
        By default, a bmp file is produced. If png=True, a png file will be
        retained along with the bitmap.
    """

    value_min = 0
    value_max = 255
    color_map = 'gray'

    fig = Figure(figsize=np_array.shape[::-1], dpi=1, frameon=False)
    fig.canvas = FigureCanvas(fig)
    fig.figimage(np_array, cmap=color_map, vmin=value_min,
                 vmax=value_max, origin=None)
    fig.savefig(filename, dpi=1, format=None)

    # save bitmap (convert from PNG)
    img = PIL.Image.open(filename + '.png')
    if len(img.split()) == 4:  # prevent IOError: cannot write mode RGBA as BMP
        r, g, b, a = img.split()
        img = PIL.Image.merge("RGB", (r, g, b))
    greyscale = img.convert("L")  # L indicates PIL's greyscale mode
    greyscale.save(filename + '.bmp')

    # delete PNG
    if not png:
        os.remove(filename + '.png')
Example #23
0
def imsave(fname,
           arr,
           vmin=None,
           vmax=None,
           cmap=None,
           format=None,
           origin=None,
           dpi=100):
    """
    Save an array as in image file.

    The output formats available depend on the backend being used.

    Arguments:
      *fname*:
        A string containing a path to a filename, or a Python file-like object.
        If *format* is *None* and *fname* is a string, the output
        format is deduced from the extension of the filename.
      *arr*:
        An MxN (luminance), MxNx3 (RGB) or MxNx4 (RGBA) array.
    Keyword arguments:
      *vmin*/*vmax*: [ None | scalar ]
        *vmin* and *vmax* set the color scaling for the image by fixing the
        values that map to the colormap color limits. If either *vmin* or *vmax*
        is None, that limit is determined from the *arr* min/max value.
      *cmap*:
        cmap is a colors.Colormap instance, eg cm.jet.
        If None, default to the rc image.cmap value.
      *format*:
        One of the file extensions supported by the active
        backend.  Most backends support png, pdf, ps, eps and svg.
      *origin*
        [ 'upper' | 'lower' ] Indicates where the [0,0] index of
        the array is in the upper left or lower left corner of
        the axes. Defaults to the rc image.origin value.
      *dpi*
        The DPI to store in the metadata of the file.  This does not affect the
        resolution of the output image.
    """
    from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
    from matplotlib.figure import Figure

    figsize = [x / float(dpi) for x in (arr.shape[1], arr.shape[0])]
    fig = Figure(figsize=figsize, dpi=dpi, frameon=False)
    canvas = FigureCanvas(fig)
    im = fig.figimage(arr, cmap=cmap, vmin=vmin, vmax=vmax, origin=origin)
    fig.savefig(fname, dpi=dpi, format=format)
Example #24
0
def plot(fileName, conversionType):
   try:
      fn = backendInputFits+fileName+".fits"
      sig_fract = 5.0
      percent_fract = 0.01
      hdulist = pyfits.open(fn)
      img_data_raw = hdulist[0].data
      width = float(img_data_raw.shape[1])/100
      height = float(img_data_raw.shape[0])/100
      hdulist.close()
      img_data_raw = numpy.array(img_data_raw, dtype=float)
      sky, num_iter = sky_mean_sig_clip(img_data_raw, sig_fract, percent_fract, max_iter=1)
      img_data = img_data_raw - sky
      min_val = 0.0

      #plotting
      if(conversionType == "Power"):
         new_img = power(img_data, power_index=3.0, scale_min = min_val)
         Rotated_Plot = ndimage.rotate(new_img, 180)
         Flipped_Plot = np.fliplr(Rotated_Plot)
         fig = Figure(figsize=(width, height))
         fig.figimage(Flipped_Plot, cmap='gray')
         resultFile = conversionType+"_"+fileName
         canvas = FigureCanvas(fig)
         canvas.print_figure(frontendInputFits+resultFile+".png")
      elif(conversionType == "Linear"):
         new_img = linear(img_data, scale_min = min_val)
         Rotated_Plot = ndimage.rotate(new_img, 180)
         Flipped_Plot = np.fliplr(Rotated_Plot)
         fig = Figure(figsize=(width, height))
         fig.figimage(Flipped_Plot, cmap='gray')
         resultFile = conversionType+"_"+fileName
         canvas = FigureCanvas(fig)
         canvas.print_figure(frontendInputFits+resultFile+".png")
      elif(conversionType == "Hist"):
         new_img = histeq(img_data_raw, num_bins=256)
         Rotated_Plot = ndimage.rotate(new_img, 180)
         Flipped_Plot = np.fliplr(Rotated_Plot)
         fig = Figure(figsize=(width, height))
         fig.figimage(Flipped_Plot, cmap='gray')
         resultFile = conversionType+"_"+fileName
         canvas = FigureCanvas(fig)
         canvas.print_figure(frontendInputFits+resultFile+".png")

      looper=1
      while(looper<100):
          time.sleep(1)
          if os.path.isfile(frontendInputFits+resultFile+".png"):
              break
          else:
              looper = looper + 1
              continue
   except(RuntimeError, TypeError, NameError):
       print 'errors in convertPlots function'
Example #25
0
def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None,
           origin=None, dpi=100):
    """
    Save an array as in image file.

    The output formats available depend on the backend being used.

    Arguments:
      *fname*:
        A string containing a path to a filename, or a Python file-like object.
        If *format* is *None* and *fname* is a string, the output
        format is deduced from the extension of the filename.
      *arr*:
        An MxN (luminance), MxNx3 (RGB) or MxNx4 (RGBA) array.
    Keyword arguments:
      *vmin*/*vmax*: [ None | scalar ]
        *vmin* and *vmax* set the color scaling for the image by fixing the
        values that map to the colormap color limits. If either *vmin* or *vmax*
        is None, that limit is determined from the *arr* min/max value.
      *cmap*:
        cmap is a colors.Colormap instance, eg cm.jet.
        If None, default to the rc image.cmap value.
      *format*:
        One of the file extensions supported by the active
        backend.  Most backends support png, pdf, ps, eps and svg.
      *origin*
        [ 'upper' | 'lower' ] Indicates where the [0,0] index of
        the array is in the upper left or lower left corner of
        the axes. Defaults to the rc image.origin value.
      *dpi*
        The DPI to store in the metadata of the file.  This does not affect the
        resolution of the output image.
    """
    from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
    from matplotlib.figure import Figure

    figsize = [x / float(dpi) for x in (arr.shape[1], arr.shape[0])]
    fig = Figure(figsize=figsize, dpi=dpi, frameon=False)
    canvas = FigureCanvas(fig)
    im = fig.figimage(arr, cmap=cmap, vmin=vmin, vmax=vmax, origin=origin)
    fig.savefig(fname, dpi=dpi, format=format)
Example #26
0
    def put_emotion_thumbnail_on_figure(self, image_plt_object: AxesImage,
                                        plt_figure: Figure):
        generated_image_left_start_in_fig_width = image_plt_object.clipbox.intervalx.min(
        )
        generated_image_right_end_in_fig_width = image_plt_object.clipbox.intervalx.max(
        )
        generated_image_width_pixel_size = generated_image_right_end_in_fig_width - generated_image_left_start_in_fig_width

        generated_image_top_in_fig_height = image_plt_object.clipbox.intervaly.max(
        )
        generated_image_bottom_in_fig_height = image_plt_object.clipbox.intervaly.min(
        )
        generated_image_height_pixel_size = generated_image_top_in_fig_height - generated_image_bottom_in_fig_height

        if self.emotion_thumbnail_image is not None:
            emotion_thumbnail_width = self.emotion_thumbnail_image.size[0]
            emotion_thumbnail_height = self.emotion_thumbnail_image.size[1]
            emotion_thumbnail_percentage_value_height_margin = 4
            emotion_thumbnail_percentage_value_width_margin = 4

            emotion_thumbnail_height_pixel_margin = generated_image_height_pixel_size * (
                emotion_thumbnail_percentage_value_height_margin / 100)
            emotion_thumbnail_height_pixel_position = (
                generated_image_top_in_fig_height - emotion_thumbnail_height
            ) - emotion_thumbnail_height_pixel_margin
            # We remove the thumbnail height and the margin from the fig height because the bottom position is 0 of height and we want the thumbnail to go down from the top

            emotion_thumbnail_width_pixel_margin = generated_image_width_pixel_size * (
                emotion_thumbnail_percentage_value_width_margin / 100)
            emotion_thumbnail_width_pixel_position = generated_image_left_start_in_fig_width + emotion_thumbnail_width_pixel_margin
            # We add the margin from the fig width because the left start position is 0 of width and we want to move the thumbnail to the right and we do not need to also add the thumbnail width

            plt_thumbnail_image_object = plt_figure.figimage(
                self.emotion_thumbnail_image,
                emotion_thumbnail_width_pixel_position,
                emotion_thumbnail_height_pixel_position)
            return plt_thumbnail_image_object
        return None
Example #27
0
class Chart(object):
    """ Convenience wrapper around a Matplotlib figure
    """

    file_types = MIME_TYPES.keys()

    def __init__(self, width: int, height: int, storage: Storage=LocalStorage(),
                 style: str='newsworthy', language: str='en-GB'):
        """
        :param width: width in pixels
        :param height: height in pixels
        :param storage: storage object that will handle file saving. Default
                        LocalStorage() class will save a file the working dir.
        :param style: a predefined style or the path to a custom style file
        :param language: a BCP 47 language tag (eg `en`, `sv-FI`)
        """

        # P U B L I C   P R O P E R T I E S
        # The user can alter these at any time
        self.data = DataList()  # A list of datasets
        self.annotate_trend = True  # Print out values at points on trendline?
        self.trendline = []  # List of x positions, or data points
        self.labels = []  # Optionally one label for each dataset
        self.annotations = []  # Manually added annotations
        self.interval = None  # yearly|quarterly|monthly|weekly|daily
        # We will try to guess interval based on the data,
        # but explicitly providing a value is safer. Used for finetuning.
        self.show_ticks = True  # toggle category names, dates, etc
        self.subtitle = None
        self.note = None
        self.xlabel = None
        self.ylabel = None
        self.caption = None
        self.highlight = None
        self.decimals = None
        # number of decimals to show in annotations, value ticks, etc
        # None means automatically chose the best number
        self.logo = None
        # Path to image that will be embedded in the caption area
        # Can also be set though a style property
        self.color_fn = None
        # Custom coloring function

        # P R I V A T E   P R O P E R T I E S
        # Properties managed through getters/setters
        self._title = None
        self._units = "count"

        # Calculated properties
        self._annotations = []  # Automatically added annotations
        self._storage = storage
        self._w, self._h = int(width), int(height)
        self._style = loadstyle(style)
        # Standardize and check if language tag is a valid BCP 47 tag
        self._language = standardize_tag(language)
        self._locale = Locale.parse(self._language.replace("-", "_"))

        # Dynamic typography
        self._title_font = FontProperties()
        self._title_font.set_family(self._style["title_font"])
        self._title_font.set_size(self._style["figure.titlesize"])
        self._title_font.set_weight(self._style["figure.titleweight"])

        self._fig = Figure()
        FigureCanvas(self._fig)
        self.ax = self._fig.add_subplot(111)
        # self._fig, self.ax = plt.subplots()
        self.value_axis = self.ax.yaxis
        self.category_axis = self.ax.xaxis

        # Calculate size in inches
        self._set_size(width, height)

        # Chart elements. Made available for fitting.
        self._title_elem = None
        self._subtitle_elem = None
        self._note_elem = None
        self._caption_elem = None
        self._logo_elem = None

    def _set_size(self, w, h=None):
        """ Set figure size, in pixels """
        dpi = self._fig.get_dpi()
        real_width = float(w) / dpi
        if h is None:
            real_height = self._fig.get_figheight()
        else:
            real_height = float(h) / dpi
        self._fig.set_size_inches(real_width, real_height)

    def _get_value_axis_formatter(self):
        return self._get_formatter(self.units)

    def _get_formatter(self, units):
        formatter = Formatter(self._language,
                              decimals=self.decimals,
                              scale="celsius")
        if units == "percent":
            return FuncFormatter(formatter.percent)
        elif units == "degrees":
            return FuncFormatter(formatter.temperature_short)
        else:
            return FuncFormatter(formatter.number)

    def _get_annotation_formatter(self):
        return self._get_formatter(self.units)

    def _text_rel_height(self, obj):
        """ Get the relative height of a text object to the whole canvas.
        Will try and guess even if wrap=True.
        """
        if not obj.get_wrap():
            # No autowrapping, use default bbox checking
            return self._rel_height(obj)

        self._fig.canvas.draw()  # Draw text to find out how big it is
        t = obj.get_text()
        r = self._fig.canvas.renderer
        w, h, d = r.get_text_width_height_descent(t, obj._fontproperties,
                                                  ismath=False)
        num_lines = len(obj._get_wrapped_text().split("\n"))
        return (h * num_lines) / float(self._h)

    def _rel_height(self, obj):
        """ Get the relative height of a chart object to the whole canvas.
        """
        self._fig.canvas.draw()  # We must draw the canvas to know all sizes
        bbox = obj.get_window_extent()
        return bbox.height / float(self._h)

    def _color_by(self, *args, **kwargs):
        """Color by some rule.
        Role of args and and kwargs are determined by the color rule.
        """
        color_name = None
        rule = self.color_fn
        if rule == "positive_negative":
            value = args[0]
            color_name = color_fn.positive_negative(value)
        elif rule == "warm_cold":
            value = args[0]
            color_name = color_fn.warm_cold(value)
        else:
            raise ValueError("Unknown color rule: {}".format(rule))

        if color_name in ["strong", "neutral", "positive", "negative", "warm", "cold"]:
            c = self._style[color_name + "_color"]
        else:
            c = color_name
        return c

    def _annotate_point(self, text, xy,
                        direction, offset=12,
                        **kwargs):
        """Add a label to a given point.

        :param text: text content of label
        :param xy: coordinates to annotate
        :param direction: placement of annotation.
            ("up", "down", "left", "right")
        :param kwags: any params accepted by plt.annotate
        """
        # TODO: Offset should maybe rather be a function of the font size,
        # but then we'd need to handle reltive fontsizes (ie "smaller") as well.
        bg_color = self._style.get("figure.facecolor", "white")
        opts = {
            "fontsize": self._style["annotation.fontsize"],
            "textcoords": "offset pixels",
            "path_effects": outline(bg_color),
        }
        if direction == "up":
            opts["verticalalignment"] = "bottom"
            opts["horizontalalignment"] = "center"
            opts["xytext"] = (0, offset)
        elif direction == "down":
            opts["verticalalignment"] = "top"
            opts["horizontalalignment"] = "center"
            opts["xytext"] = (0, -offset)
        elif direction == "left":
            opts["verticalalignment"] = "center"
            opts["horizontalalignment"] = "right"
            opts["xytext"] = (-offset, 0)
        elif direction == "right":
            opts["verticalalignment"] = "center"
            opts["horizontalalignment"] = "left"
            opts["xytext"] = (offset, 0)
        else:
            msg = f"'{direction}' is an unknown direction for an annotation"
            raise Exception(msg)

        # Override default opts if passed to the function
        opts.update(kwargs)

        ann = self.ax.annotate(text, xy=xy, **opts)
        # ann = self.ax.text(text, xy[0], xy[1])
        self._annotations.append(ann)

        return ann

    def _add_caption(self, caption, hextent=None):
        """Add a caption. Supports multiline input.

        Hextent is the left/right extent,  e.g. to avoid overlapping a logo
        """
        # Wrap=true is hardcoded to use the extent of the whole figure
        # Our workaround is to resize the figure, draw the text to find the
        # linebreaks, and then restore the original width!
        if hextent is None:
            hextent = (0, self._w)
        self._set_size(hextent[1] - hextent[0])
        x1 = hextent[0] / self._w
        text = self._fig.text(x1, 0.01, caption,
                              color=self._style["neutral_color"], wrap=True,
                              fontsize=self._style["caption.fontsize"])
        self._fig.canvas.draw()
        wrapped_text = text._get_wrapped_text()
        text.set_text(wrapped_text)
        self._set_size(self._w)

        self._caption_elem = text

    def _add_title(self, title_text):
        """Add a title."""
        # y=1 wraps title heavily, hence .9999
        text = self._fig.suptitle(title_text, wrap=True, x=0, y=.99999,
                                  horizontalalignment="left",
                                  multialignment="left",
                                  fontproperties=self._title_font)

        self._title_elem = text

    def _add_subtitle(self, subtitle_text):
        y_pos = 1 - self._title_rel_height
        text = self._fig.text(0, y_pos, subtitle_text, wrap=True,
                              verticalalignment="top", linespacing=1.4,
                              fontsize=self._style["subtitle.fontsize"])
        self._fig.canvas.draw()
        wrapped_text = text._get_wrapped_text()
        text.set_text(wrapped_text)
        self._set_size(self._w)
        self._subtitle_elem = text

    def _add_note(self, note_text):
        y_pos = self._footer_rel_height
        text = self._fig.text(0, y_pos, note_text, wrap=True,
                              fontsize=self._style["note.fontsize"])
        self._fig.canvas.draw()
        wrapped_text = text._get_wrapped_text()
        text.set_text(wrapped_text)
        self._set_size(self._w)
        self._note_elem = text

    def _add_xlabel(self, label):
        """Adds a label to the x axis."""
        self.ax.set_xlabel(label)

    def _add_ylabel(self, label):
        """Adds a label to the y axis."""
        self.ax.set_ylabel(label)

    def _add_data(self):
        """ Plot data to the chart.
        Typically defined by a more specific subclass
        """
        pass
        # raise NotImplementedError("This method should be overridden")

    def _apply_changes_before_rendering(self):
        """
         To ensure consistent rendering, we call this method just before
         rendering file(s). This is where all properties are applied.
        """
        # Apply all changes, in the correct order for consistent rendering
        self._fig.tight_layout()
        if len(self.data):
            self._add_data()
        if not self.show_ticks:
            self.category_axis.set_visible(False)
        else:
            # Remove dublicated labels (typically a side effect of using
            # few decimals while having a lot of values in a small range)
            pass
            """
            self._fig.canvas.draw()
            tl = [x.get_text() for x in self.value_axis.get_ticklabels()]
            print(tl)
            tl = [x if tl[i-1] != x else "" for (i, x) in enumerate(tl)]
            print(tl)
            self.value_axis.set_ticklabels(tl)
            """

        for a in self.annotations:
            self._annotate_point(a["text"], a["xy"], a["direction"])
        if self.ylabel is not None:
            self._add_ylabel(self.ylabel)
        if self.xlabel is not None:
            self._add_xlabel(self.xlabel)
        if self.title is not None:
            self._add_title(self.title)
        if self.subtitle is not None:
            self._add_subtitle(self.subtitle)

        # fit ticks etc.
        self._fig.tight_layout()

        logo = self._style.get("logo", self.logo)
        caption_hextent = None  # set this if adding a logo
        if logo:
            im = Image.open(logo)
            # scale down image if needed to fit
            new_width = min(self._w, im.size[0])
            new_height = new_width * (im.size[1] / im.size[0])
            im.thumbnail((new_width, new_height), Image.ANTIALIAS)

            # Position
            if self._locale.text_direction == "rtl":
                logo_im = self._fig.figimage(im, 0, 0)
                ext = logo_im.get_extent()
                caption_hextent = (ext[1], self._w)
            else:
                logo_im = self._fig.figimage(im, self._w - im.size[0], 0)
                ext = logo_im.get_extent()
                caption_hextent = (0, ext[0])
            self._logo_elem = logo_im

        if self.caption is not None:
            # Add caption without image
            self._add_caption(self.caption, hextent=caption_hextent)

        if self.note is not None:
            self._add_note(self.note)

        # Fit header
        header_height = 0
        if self._title_elem:
            header_height += self._title_rel_height
        if self._subtitle_elem:
            header_height += self._subtitle_rel_height

        self._fig.subplots_adjust(top=1 - header_height)

        # Fit area below chart
        try:
            tick_label_height = max([self._text_rel_height(lbl)
                                    for lbl in self.ax.get_xticklabels()])
        except ValueError:
            # handle charts without ticks
            tick_label_height = 0

        sub_canvas_height = (
            # ticks labels
            tick_label_height
            # some padding
            + 30 / self._h
            #  chart notes (if any)
            + self._note_rel_height
            #  chart caption and logo (if any)
            + self._footer_rel_height
        )
        self._fig.subplots_adjust(bottom=sub_canvas_height)

    @classmethod
    def init_from(cls, args: dict, storage=LocalStorage(),
                  style: str="newsworthy", language: str='en-GB'):
        """Create a chart from a Python object."""
        if not ("width" in args and "height" in args):
            raise Exception("The settings object must include an explicit width and height")
        chart = cls(args["width"], args["height"], storage=storage,
                    style=style, language=language)

        # Get everything from args that is a public attribute in Chart,
        # except data and labels.
        class_attrs = vars(chart)
        for k, v in args.items():
            if (not k.startswith("_")) and \
               (k in class_attrs) and \
               (k not in ["data", "labels", "type", "ymin", "ymax", "title",
                          "units"]):
                setattr(chart, k, v)
        if "data" in args:
            for data in args["data"].copy():
                chart.data.append(data)
        if "labels" in args:
            for label in args["labels"].copy():
                chart.labels.append(label)
        # Special handling for setters
        if "title" in args:
            chart.title = args["title"]
        if "units" in args:
            chart.units = args["units"]
        if "type" in args:
            chart.type = args["type"]
        if "ymin" in args:
            chart.ymin = args["ymin"]
        if "ymax" in args:
            chart.ymax = args["ymax"]
        if "ticks" in args:
            chart.ticks = args["ticks"]
        return chart

    def render(self, key: str, img_format: str):
        """Render file, and send to storage."""
        # Apply all changes, in the correct order for consistent rendering
        self._apply_changes_before_rendering()

        # Save plot in memory, to write it directly to storage
        buf = BytesIO()
        self._fig.savefig(buf, format=img_format)
        buf.seek(0)
        self._storage.save(key, buf, img_format)

    def render_all(self, key: str):
        """
        Render all available formats
        """
        # Apply all changes, in the correct order for consistent rendering
        self._apply_changes_before_rendering()

        for file_format in self.file_types:
            if file_format == "dw":
                continue

            # Save plot in memory, to write it directly to storage
            buf = BytesIO()
            self._fig.savefig(buf, format=file_format)
            buf.seek(0)
            self._storage.save(key, buf, file_format)

    @property
    def title(self):
        """ A user could have manipulated the fig property directly,
        so check for a title there as well.
        """
        if self._title is not None:
            return self._title
        elif self._fig._suptitle:
            return self._fig._suptitle.get_text()
        else:
            return None

    @title.setter
    def title(self, title: str):
        self._title = title

    @property
    def units(self):
        return self._units

    @units.setter
    def units(self, val: str):
        """ Units, used for number formatting. Note that 'degrees' is designed
        for temperature degrees.
        In some languages there are typographical differences between
        angles and short temperature notation (e.g. 45° vs 45 °).
        """
        allowed_units = ["count", "percent", "degrees"]
        if val in allowed_units:
            self._units = val
            # By default no decimals if unit is “count”
            if self.decimals is None and self._units == "count":
                self.decimals = 0
        else:
            raise ValueError("Supported units are: {}".format(allowed_units))

    @property
    def _title_rel_height(self):
        rel_height = 0
        if self._title_elem:
            rel_height += self._text_rel_height(self._title_elem)
            # Adds a fixes margin below
            rel_height += 30 / self._h
        return rel_height

    @property
    def _subtitle_rel_height(self):
        rel_height = 0
        if self._subtitle_elem:
            rel_height += self._text_rel_height(self._subtitle_elem)
            # Adds a fixes margin below
            rel_height += 30 / self._h
        return rel_height

    @property
    def _note_rel_height(self):
        rel_height = 0
        if self._note_elem:
            rel_height += self._text_rel_height(self._note_elem)
            # Adds a fixes margin below
            rel_height += 10 / self._h
        return rel_height

    @property
    def _footer_rel_height(self):
        footer_elem_heights = [0]
        if self._logo_elem:
            # Assuming the logo is place at fixed bottom
            logo_height = self._logo_elem.get_extent()[3]
            footer_elem_heights.append(logo_height / self._h)

        if self._caption_elem:
            # Increase the bottom padding by the height of the text bbox
            caption_height = self._text_rel_height(self._caption_elem)
            footer_elem_heights.append(caption_height)

        footer_height = max(footer_elem_heights)
        footer_height += 15 / self._h

        return footer_height

    def __str__(self):
        # Return main title or id
        if self.title is not None:
            return self.title
        else:
            return str(id(self))

    def __repr__(self):
        # Use type(self).__name__ to get the right class name for sub classes
        return "<{cls}: {name} ({h} x {w})>".format(cls=type(self).__name__,
                                                    name=str(self),
                                                    w=self._w, h=self._h)
    def train(self, sess, config):
        """ Training the GAN """
        print('initializing...opt')
        d_opt = self.d_opt
        g_opt = self.g_opt

        try:
            init = tf.global_variables_initializer()
            sess.run(init)
        except AttributeError:
            init = tf.intializer_all_varialble()
            sess.run(init)

        print('initializing...var')
        # g_summaries = [self.d_fake_summary,
        #                 self.d_fake_loss_summary,
        #                 self.g_loss_summary,
        #                 self.g_l2_loss_summary,
        #                 self.g_loss_adv_summary,
        #                 self.generated_wav_summary]
        # d_summaries = [self.d_loss_summary, self.d_real_summary, self.d_real_loss_summary, self.high_wav_summary]

        # if hasattr(self, 'alpha_summ'):
        #     g_summaries += self.alpha_summ
        # self.g_sum = tf.summary.merge(g_summaries)
        # self.d_sum = tf.summary.merge(d_summaries)

        if not os.path.exists(os.path.join(config.save_path, 'train')):
            os.makedirs(os.path.join(config.save_path, 'train'))

        self.writer = tf.summary.FileWriter(
            os.path.join(config.save_path, 'train'), self.sess.graph)

        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(coord=coord)

        sample_low, sample_high, sample_z = self.sess.run(
            [self.gt_low[0], self.gt_high[0], self.zz_A[0]],
            feed_dict={
                self.is_valid: False,
                self.is_train: True,
                self.is_mismatch: False
            })
        v_sample_low, v_sample_high, v_sample_z = self.sess.run(
            [self.gt_low[0], self.gt_high[0], self.zz_A[0]],
            feed_dict={
                self.is_valid: True,
                self.is_train: False,
                self.is_mismatch: False
            })

        print('sample low shape: ', sample_low.shape)
        print('sample high shape: ', sample_high.shape)
        print('sample z shape: ', sample_z.shape)

        save_path = config.save_path
        counter = 0
        # count of num of samples
        num_examples = 0
        for record in tf.python_io.tf_record_iterator(self.tfrecords):
            num_examples += 1
        print("total num of patches in tfrecords", self.tfrecords, ":  ",
              num_examples)

        # last samples
        # batch num
        num_batches = num_examples / self.batch_size
        print('batches per epoch: ', num_batches)

        if self.load(self.save_path):
            print('load success')
        else:
            print('load failed')
        batch_idx = 0
        current_epoch = 0
        batch_timings = []
        g_losses = []
        d_A_losses = []
        d_B_losses = []
        g_adv_losses = []
        g_l1_losses_BAB = []
        g_l1_losses_AB = []
        g_l1_losses_ABA = []
        g_l1_losses_BA = []

        try:
            while not coord.should_stop():
                start = timeit.default_timer()
                if counter % config.save_freq == 0:

                    for d_iter in range(self.disc_updates):
                        _d_opt, d_A_loss, d_B_loss = self.sess.run(
                            [d_opt, self.d_A_losses[0], self.d_B_losses[0]],
                            feed_dict={
                                self.is_valid: False,
                                self.is_train: True,
                                self.is_mismatch: True
                            })
                        _d_opt, d_A_loss, d_B_loss = self.sess.run(
                            [d_opt, self.d_A_losses[0], self.d_B_losses[0]],
                            feed_dict={
                                self.is_valid: False,
                                self.is_train: True,
                                self.is_mismatch: False
                            })

                        #_d_sum, d_fake_loss, d_real_loss = self.sess.run(
                        #   [self.d_sum, self.d_fake_losses[0], self.d_real_losses[0]], feed_dict={self.is_valid: False})

                        if self.d_clip_weights:
                            self.sess.run(self.d_clip,
                                          feed_dict={
                                              self.is_valid: False,
                                              self.is_train: True
                                          })

                    #_g_opt, _g_sum, g_adv_loss, g_l2_loss = self.sess.run([g_opt, self.g_sum, self.g_adv_losses[0], self.g_l2_losses[0]], feed_dict={self.is_valid:False})
                    _g_opt, g_adv_loss, g_AB_loss, g_BA_loss, g_ABA_loss, g_BAB_loss = self.sess.run(
                        [
                            g_opt, self.g_adv_losses[0], self.g_losses_AB[0],
                            self.g_losses_BA[0], self.g_l1_losses_ABA[0],
                            self.g_l1_losses_BAB[0]
                        ],
                        feed_dict={
                            self.is_valid: False,
                            self.is_train: True,
                            self.is_mismatch: True
                        })
                    _g_opt, g_adv_loss, g_AB_loss, g_BA_loss, g_ABA_loss, g_BAB_loss = self.sess.run(
                        [
                            g_opt, self.g_adv_losses[0], self.g_losses_AB[0],
                            self.g_losses_BA[0], self.g_l1_losses_ABA[0],
                            self.g_l1_losses_BAB[0]
                        ],
                        feed_dict={
                            self.is_valid: False,
                            self.is_train: True,
                            self.is_mismatch: False
                        })
                    # _phase_opt, phase_loss = self.sess.run([phase_opt, self.phase_losses[0]], feed_dict={self.is_valid:False,self.is_train: True})

                else:
                    for d_iter in range(self.disc_updates):
                        _d_opt, d_A_loss, d_B_loss = self.sess.run(
                            [d_opt, self.d_A_losses[0], self.d_B_losses[0]],
                            feed_dict={
                                self.is_valid: False,
                                self.is_train: True,
                                self.is_mismatch: True
                            })
                        _d_opt, d_A_loss, d_B_loss = self.sess.run(
                            [d_opt, self.d_A_losses[0], self.d_B_losses[0]],
                            feed_dict={
                                self.is_valid: False,
                                self.is_train: True,
                                self.is_mismatch: False
                            })
                        #d_fake_loss, d_real_loss = self.sess.run(
                        #    [self.d_fake_losses[0], self.d_real_losses[0]], feed_dict={self.is_valid: False})
                        if self.d_clip_weights:
                            self.sess.run(self.d_clip,
                                          feed_dict={
                                              self.is_valid: False,
                                              self.is_train: True
                                          })
                    _g_opt, g_adv_loss, g_AB_loss, g_BA_loss, g_ABA_loss, g_BAB_loss = self.sess.run(
                        [
                            g_opt, self.g_adv_losses[0], self.g_losses_AB[0],
                            self.g_losses_BA[0], self.g_l1_losses_ABA[0],
                            self.g_l1_losses_BAB[0]
                        ],
                        feed_dict={
                            self.is_valid: False,
                            self.is_train: True,
                            self.is_mismatch: True
                        })
                    _g_opt, g_adv_loss, g_AB_loss, g_BA_loss, g_ABA_loss, g_BAB_loss = self.sess.run(
                        [
                            g_opt, self.g_adv_losses[0], self.g_losses_AB[0],
                            self.g_losses_BA[0], self.g_l1_losses_ABA[0],
                            self.g_l1_losses_BAB[0]
                        ],
                        feed_dict={
                            self.is_valid: False,
                            self.is_train: True,
                            self.is_mismatch: False
                        })
                    # _phase_opt, phase_loss = self.sess.run([phase_opt, self.phase_losses[0]], feed_dict={self.is_valid:False,self.is_train: True})

                end = timeit.default_timer()
                batch_timings.append(end - start)
                d_A_losses.append(d_A_loss)
                d_B_losses.append(d_B_loss)
                g_adv_losses.append(g_adv_loss)
                g_l1_losses_BAB.append(g_BAB_loss)  # clean - reverb - clean
                g_l1_losses_AB.append(g_AB_loss)  # reverb - clean
                g_l1_losses_ABA.append(g_ABA_loss)  # reverb - clean  - reverb
                g_l1_losses_BA.append(g_BA_loss)  # clean - reverb

                print(
                    '{}/{} (epoch {}), d_A_loss = {:.5f}, '
                    'd_B_loss = {:.5f}, '  #d_nfk_loss = {:.5f}, '
                    'g_adv_loss = {:.5f}, g_AB_loss = {:.5f}, g_BAB_loss = {:.5f}, '
                    'g_BA_loss = {:.5f}, g_ABA_loss = {:.5f}, '
                    ' time/batch = {:.5f}, '
                    'mtime/batch = {:.5f}'.format(
                        counter, config.epoch * num_batches, current_epoch,
                        d_A_loss, d_B_loss, g_adv_loss, g_AB_loss, g_BAB_loss,
                        g_BA_loss, g_ABA_loss, end - start,
                        np.mean(batch_timings)))
                batch_idx += 1
                counter += 1

                if (counter) % 2000 == 0 and (counter) > 0:
                    self.save(config.save_path, counter)

                if (counter % config.save_freq == 0) or (counter == 1):
                    # self.writer.add_summary(_g_sum, counter)
                    # self.writer.add_summary(_d_sum, counter)
                    #feed_dict = {self.gt_high[0]:v_sample_high, self.gt_low[0]:v_sample_low, self.zz[0]:v_sample_z, self.is_valid:True}

                    s_A, s_B, s_reverb, s_gt, r_phase, f_phase = self.sess.run(
                        [
                            self.GG_A[0][0, :, :, :], self.GG_B[0][0, :, :, :],
                            self.gt_low[0][0, :, :, :],
                            self.gt_high[0][0, :, :, :],
                            self.ori_phase_[0][0, :, :, :],
                            self.rev_phase_[0][0, :, :, :]
                        ],
                        feed_dict={
                            self.is_valid: True,
                            self.is_train: False,
                            self.is_mismatch: False
                        })

                    if not os.path.exists(save_path + '/wav'):
                        os.makedirs(save_path + '/wav')
                    if not os.path.exists(save_path + '/txt'):
                        os.makedirs(save_path + '/txt')
                    if not os.path.exists(save_path + '/spec'):
                        os.makedirs(save_path + '/spec')

                    print(str(counter) + 'th finished')

                    x_AB = s_A
                    x_BA = s_B
                    x_reverb = s_reverb
                    x_gt = s_gt

                    Sre = self.get_spectrum(x_reverb).reshape(512, 128)
                    Sgt = self.get_spectrum(x_gt).reshape(512, 128)
                    SAB = self.get_spectrum(x_AB).reshape(512, 128)
                    SBA = self.get_spectrum(x_BA).reshape(512, 128)
                    S = np.concatenate((Sre, Sgt, SAB, SBA), axis=1)
                    fig = Figure(figsize=S.shape[::-1], dpi=1, frameon=False)
                    canvas = FigureCanvas(fig)
                    fig.figimage(S, cmap='jet')
                    fig.savefig(save_path + '/spec/' + 'valid_batch_index' +
                                str(counter) + '-th_pr.png')

                    x_pr = librosa.istft(self.inv_magphase(s_A, f_phase))
                    librosa.output.write_wav(
                        save_path + '/wav/' + str(counter) +
                        '_AB(dereverb).wav', x_pr, 16000)
                    x_pr = librosa.istft(self.inv_magphase(s_B, r_phase))
                    librosa.output.write_wav(
                        save_path + '/wav/' + str(counter) + '_BA(reverb).wav',
                        x_pr, 16000)
                    x_lr = librosa.istft(self.inv_magphase(s_reverb, f_phase))
                    librosa.output.write_wav(
                        save_path + '/wav/' + str(counter) + '_reverb.wav',
                        x_lr, 16000)
                    x_hr = librosa.istft(self.inv_magphase(s_gt, r_phase))
                    librosa.output.write_wav(
                        save_path + '/wav/' + str(counter) + '_orig.wav', x_hr,
                        16000)

                    s_AB, s_BA, s_reverb, s_gt = self.sess.run(
                        [
                            self.GG_A[0][0, :, :, :], self.GG_B[0][0, :, :, :],
                            self.gt_low[0][0, :, :, :],
                            self.gt_high[0][0, :, :, :]
                        ],
                        feed_dict={
                            self.is_valid: False,
                            self.is_train: True,
                            self.is_mismatch: False
                        })

                    x_AB = s_AB
                    x_BA = s_BA
                    x_reverb = s_reverb
                    x_gt = s_gt

                    Sre = self.get_spectrum(x_reverb).reshape(512, 128)
                    Sgt = self.get_spectrum(x_gt).reshape(512, 128)
                    SAB = self.get_spectrum(x_AB).reshape(512, 128)
                    SBA = self.get_spectrum(x_BA).reshape(512, 128)

                    S = np.concatenate((Sre, Sgt, SAB, SBA), axis=1)
                    fig = Figure(figsize=S.shape[::-1], dpi=1, frameon=False)
                    canvas = FigureCanvas(fig)
                    fig.figimage(S, cmap='jet')
                    fig.savefig(save_path + '/spec/' + 'train_batch_index' +
                                str(counter) + '-th_pr.png')

                    #np.savetxt(os.path.join(save_path, '/txt/d_real_losses.txt'), d_real_losses)
                    #np.savetxt(os.path.join(save_path, '/txt/d_fake_losses.txt'), d_fake_losses)
                    #np.savetxt(os.path.join(save_path, '/txt/g_adv_losses.txt'), g_adv_losses)
                    #np.savetxt(os.path.join(save_path, '/txt/g_l2_losses.txt'), g_l2_losses)

                if batch_idx >= num_batches:
                    current_epoch += 1
                    #reset batch idx
                    batch_idx = 0

                if current_epoch >= config.epoch:
                    print(str(self.epoch), ': epoch limit')
                    print('saving last model at iteration', str(counter))
                    self.save(config.save_path, counter)
                    # self.writer.add_summary(_g_sum, counter)
                    # self.writer.add_summary(_d_sum, counter)
                    break

        except tf.errors.InternalError:
            print('InternalError')
            pass

        except tf.errors.OutOfRangeError:
            print('done training')
            pass
        finally:
            coord.request_stop()
        coord.join(threads)
Example #29
0
def image_preview(request, id=0, size=0):
    print('image_preview', id, size)

    image = Images.objects.get(id=id)
    filename = image.filename
    filename = posixpath.join(settings.BASE_DIR, filename)

    data = fits.getdata(filename, -1).astype(np.double)
    header = fits.getheader(filename, -1)

    if request.GET.has_key('size'):
        size = int(request.GET.get('size', 0))

    if not request.GET.has_key('raw'):
        if image.type not in ['masterdark', 'masterflat']:
            dark = None

            if image.type not in ['dark', 'zero']:
                cdark = find_calibration_image(image, 'masterdark')
                if cdark is not None:
                    dark = fits.getdata(cdark.filename, -1)

            if dark is not None:
                data, header = calibrate.calibrate(
                    data, header, dark=dark)  # Subtract dark and linearize
                # data -= dark

                if image.type not in ['masterflat', 'flat']:
                    cflat = find_calibration_image(image, 'masterflat')
                    if cflat is not None:
                        flat = fits.getdata(cflat.filename, -1)
                        idx = flat > 0.5
                        data[idx] *= np.median(flat[idx]) / flat[idx]

        ldata = data
    else:
        ldata, lheader = data, header

    if size:
        data = rescale(data,
                       size / data.shape[1],
                       mode='reflect',
                       multichannel=False,
                       anti_aliasing=True,
                       preserve_range=True)

    figsize = (data.shape[1], data.shape[0])

    fig = Figure(facecolor='white',
                 dpi=72,
                 figsize=(figsize[0] / 72, figsize[1] / 72))

    limits = np.percentile(ldata[np.isfinite(ldata)],
                           [2.5, float(request.GET.get('qq', 99.75))])

    fig.figimage(data,
                 vmin=limits[0],
                 vmax=limits[1],
                 origin='lower',
                 cmap=request.GET.get('cmap', 'Blues_r'))

    canvas = FigureCanvas(fig)

    response = HttpResponse(content_type='image/jpeg')
    canvas.print_jpg(response)

    return response
Example #30
0
def reduce(getDarkFrames, getBiasFrames, getFlatFields, getRawFrames,
           sessionId):
    try:
        sig_fract = 5.0
        percent_fract = 0.01
        min_val = 0.0

        #Open Dark Frames data
        List = getDarkFrames
        reference_dark_image = 'None'
        dataList = []
        if List != []:
            for file in List:
                dataList.append(
                    openFile(backendInputFits + sessionId + "_Dark_" + file))
            reference_dark_image = medianImage(dataList)

        #Open Bias Frames data
        List = getBiasFrames
        reference_bias_image = 'None'
        if List != []:
            dataList = []
            for file in List:
                dataList.append(
                    openFile(backendInputFits + sessionId + "_Bias_" + file))
            reference_bias_image = medianImage(dataList)

        #Open Flat Fields data
        List = getFlatFields
        reference_flat_image = 'None'
        if List != []:
            dataList = []
            for file in List:
                dataList.append(
                    openFileAndNormalize(backendInputFits + sessionId +
                                         "_Flat_" + file))
            reference_flat_image = medianImage(dataList)

        #Open Raw Images Data and Reduce
        List = getRawFrames
        if List != []:
            for file in List:
                dataImage = openFile(backendInputFits + sessionId + "_Raw_" +
                                     file)
                #reference dark frame exists
                dark_corrected_image = 'None'
                if reference_dark_image != 'None':
                    dark_corrected_image = dataImage - reference_dark_image
                #reference dark frame does not exist but reference bias frame exists
                bias_corrected_image = 'None'
                if reference_dark_image == 'None' and reference_bias_image != 'None':
                    bias_corrected_image = dataImage - reference_bias_image
                #reference dark frame exists and reference bias frame exists
                if reference_dark_image != 'None' and reference_bias_image != 'None':
                    bias_corrected_image = dark_corrected_image - reference_bias_image

                #dark corrected image exists and flat exists
                if dark_corrected_image != 'None' and reference_flat_image != 'None' and bias_corrected_image == 'None':
                    final_image = dark_corrected_image / reference_flat_image
                #bias corrected image exists and flat exists
                if bias_corrected_image != 'None' and reference_flat_image != 'None':
                    final_image = bias_corrected_image / reference_flat_image
                #reference flat field does not exist but bias corrected image exists
                if bias_corrected_image != 'None' and reference_flat_image == 'None':
                    final_image = bias_corrected_image
                #reference flat field does not exist and only dark corrected image exists
                if dark_corrected_image != 'None' and reference_flat_image == 'None' and bias_corrected_image == 'None':
                    final_image = dark_corrected_image
                #reference flat field exists and raw image exists
                if bias_corrected_image == 'None' and reference_flat_image != 'None' and dark_corrected_image == 'None':
                    final_image = dataImage / reference_flat_image
                #reference flat field does not exist and only raw image exists
                if bias_corrected_image == 'None' and reference_flat_image == 'None' and dark_corrected_image == 'None':
                    final_image = dataImage

                width = float(final_image.shape[1]) / 100
                height = float(final_image.shape[0]) / 100
                if os.path.exists(backendOutputFits + "Processed_" + file):
                    os.remove(backendOutputFits + "Processed_" + file)
                pyfits.append(backendOutputFits + "Processed_" + file,
                              final_image)
                sky, num_iter = sky_mean_sig_clip(final_image,
                                                  sig_fract,
                                                  percent_fract,
                                                  max_iter=1)
                img_data = final_image - sky
                new_img = linear(img_data, scale_min=min_val)
                Rotated_Plot = ndimage.rotate(new_img, 180)
                Flipped_Plot = np.fliplr(Rotated_Plot)
                fig = Figure(figsize=(width, height))
                fig.figimage(Flipped_Plot, cmap='gray')
                canvas = FigureCanvas(fig)
                specialCharacterPosition = file.index('.')
                fileWithoutExtension = str(file[:specialCharacterPosition])
                canvas.print_figure(frontendInputFits + "Linear_" + sessionId +
                                    "_Processed_" + fileWithoutExtension +
                                    ".png")

    except:
        print 'error in reduce function'
Example #31
0
class App:
    '''Tkinter GUI application'''
    def __init__(self, root, files, img_width, img_height, islabeled):
        self.current_index = 0
        self.files_keys = files.keys()
        self.files = files
        self.img_width, self.img_height = img_width, img_height
        self.gt_labels = False
        self.manual_thresh = StringVar()
        self.just_set_thresh = False
        self.manual_thresh.trace("w", self.manual_thresh_change)
        self.make_widgets(root, img_width, img_height, islabeled)
        self.load_files()

    def make_widgets(self, root, img_width, img_height, islabeled):
        # enclosing frame
        frame = Frame(root)
        frame.grid(padx=10, pady=10)

        # canvas
        self.f = Figure(figsize=(img_width / 100.0, img_height / 100.0),
                        dpi=100)
        self.canvas = FigureCanvasTkAgg(self.f, master=frame)
        self.canvas.show()
        self.canvas.get_tk_widget().grid(column=0, row=1, rowspan=5)

        # other widgets
        self.filename_label = Label(frame, text="Filename")
        self.filename_label.grid(column=0, row=0)
        self.roi_slider = Scale(frame,
                                from_=100,
                                to=0,
                                orient=VERTICAL,
                                length=400,
                                command=self.roi_slider_change)
        self.roi_slider.grid(column=1, row=1, rowspan=4, padx=25)
        self.image_slider = Scale(frame,
                                  from_=0,
                                  to=100,
                                  orient=HORIZONTAL,
                                  length=400,
                                  command=self.image_slider_change)
        self.image_slider.grid(column=0, row=6)
        self.cutoff_label = Label(frame, text="set threshold value:")
        self.cutoff_label.grid(column=2, row=1)
        self.cutoff_entry = Entry(frame, textvariable=self.manual_thresh)
        self.cutoff_entry.grid(column=3, row=1)
        self.max_thresh_label = Label(frame, text="current threshold value:")
        self.max_thresh_label.grid(column=2, row=2)
        self.max_thresh_value_label = Label(frame, text="0")
        self.max_thresh_value_label.grid(column=3, row=2)
        self.save_button = Button(frame,
                                  text="Save Displayed ROIs",
                                  command=self.save_button_press)
        self.save_button.grid(column=2, columnspan=2, row=3)
        prev_next_frame = Frame(frame)
        prev_next_frame.grid(column=2, columnspan=2, row=4)
        self.next_image_button = Button(prev_next_frame,
                                        text="Next Image",
                                        command=self.next_image_button_press)
        self.next_image_button.grid(column=1, row=0, sticky=W)
        self.prev_image_button = Button(prev_next_frame,
                                        text="Previous Image",
                                        state=DISABLED,
                                        command=self.prev_image_button_press)
        self.prev_image_button.grid(column=0, row=0, sticky=E)
        self.index_label = Label(prev_next_frame, text=self.make_index_label())
        self.index_label.grid(column=0, columnspan=2, row=1)
        if islabeled:
            self.gt_labels_button = Button(
                frame,
                text="Show/Hide Ground Truth Labels",
                command=self.gt_labels_button_press)
        else:
            self.gt_labels_button = Button(
                frame,
                text="Show/Hide Ground Truth Labels",
                state=DISABLED,
                command=self.gt_labels_button_press)
        self.gt_labels_button.grid(column=2, columnspan=2, row=5)

    def load_files(self):
        '''load a new image and corresponding ROIs'''
        current_files = self.files[self.files_keys[self.current_index]]
        self.filename_label.config(text=self.files_keys[self.current_index])
        self.image = load_stack(current_files[0])
        self.image_slider.config(to=self.image.shape[0] - 1)
        self.image_slider.set(0)
        self.image_index = 0

        self.convnet_rois = np.load(current_files[1])['rois']
        self.convnet_roi_probs = np.load(current_files[1])['roi_probabilities']
        self.indexed_roi_probs = sorted(
            [(v, i) for i, v in enumerate(self.convnet_roi_probs)],
            reverse=True)
        assert (self.convnet_rois.shape[0] == self.convnet_roi_probs.shape[0])
        self.roi_slider.config(from_=self.convnet_rois.shape[0])

        if self.manual_thresh.get().strip() == "":
            self.roi_slider.set(self.convnet_rois.shape[0])
            self.roi_index = self.convnet_rois.shape[0]
        else:
            new_index = self.convnet_rois.shape[0]
            for j, (p, ind) in enumerate(self.indexed_roi_probs):
                if p < float(self.manual_thresh.get()):
                    new_index = j
                    break
            self.roi_index = new_index
            self.just_set_thresh = True
            self.roi_slider.set(new_index)

        if current_files[2] is not None:
            self.gt_rois = load_rois(current_files[2], self.img_width,
                                     self.img_height)
            self.gt_rois = self.gt_rois.max(axis=0)

        self.draw_canvas()

    def draw_canvas(self):
        '''draw current image and selected ROIs'''
        self.f.clf()
        overlay = np.zeros((self.image.shape[1], self.image.shape[2], 3))
        overlay[:, :, 0] = self.image[self.image_index, :, :]
        overlay[:, :, 1] = self.image[self.image_index, :, :]
        overlay[:, :, 2] = self.image[self.image_index, :, :]

        current_roi_indices = [
            i for (v, i) in self.indexed_roi_probs[0:self.roi_index]
        ]
        if len(current_roi_indices) > 0:
            cutoff = self.indexed_roi_probs[self.roi_index - 1][0]
            self.max_thresh_value_label.config(text=">= {:.3f}".format(cutoff))
            current_rois_mask = self.convnet_rois[current_roi_indices].max(
                axis=0)
            overlay[:, :, 2][current_rois_mask == 1] = 1
        else:
            cutoff = self.indexed_roi_probs[0][0]
            self.max_thresh_value_label.config(text="> {:.3f}".format(cutoff))

        if self.gt_labels:
            overlay[:, :, 0][self.gt_rois == 1] = 1

        self.f.figimage(overlay)
        self.canvas.draw()

    def roi_slider_change(self, value):
        '''callback for threshold value slider'''
        self.roi_index = int(value)
        if self.manual_thresh.get() != "" and not self.just_set_thresh:
            self.manual_thresh.set("")
        self.just_set_thresh = False
        self.draw_canvas()

    def image_slider_change(self, value):
        '''callback for image sequence slider'''
        self.image_index = int(value)
        self.draw_canvas()

    def save_button_press(self):
        '''callback to save currently selected ROIs'''
        current_roi_indices = [
            i for (v, i) in self.indexed_roi_probs[0:self.roi_index]
        ]
        current_rois = self.convnet_rois[current_roi_indices]
        current_files = self.files[self.files_keys[self.current_index]]
        new_file = current_files[1][0:-4] + "_MANUAL.npz"
        np.savez_compressed(new_file, rois=current_rois)
        print "saved as " + new_file

    def make_index_label(self):
        '''returns current image index as fraction string'''
        return str(self.current_index + 1) + "/" + str(len(self.files_keys))

    def next_image_button_press(self):
        '''callback for next image button'''
        if self.current_index < len(self.files_keys) - 1:
            self.current_index += 1
            self.index_label.config(text=self.make_index_label())
            self.load_files()
        if self.current_index == len(self.files_keys) - 1:
            self.next_image_button.config(state=DISABLED)
        if self.current_index > 0:
            self.prev_image_button.config(state=NORMAL)

    def prev_image_button_press(self):
        '''callback for previous image button'''
        if self.current_index > 0:
            self.current_index -= 1
            self.index_label.config(text=self.make_index_label())
            self.load_files()
        if self.current_index == 0:
            self.prev_image_button.config(state=DISABLED)
        if self.current_index < len(self.files_keys) - 1:
            self.next_image_button.config(state=NORMAL)

    def gt_labels_button_press(self):
        '''callback for show/hide ground truth ROIs button'''
        self.gt_labels = not self.gt_labels
        self.draw_canvas()

    def manual_thresh_change(self, *args):
        '''callback for manual thresholsd entry'''
        if self.manual_thresh.get().strip() != "":
            new_index = self.convnet_rois.shape[0]
            for j, (p, ind) in enumerate(self.indexed_roi_probs):
                if p < float(self.manual_thresh.get()):
                    new_index = j
                    break
            self.roi_index = new_index
            self.just_set_thresh = True
            self.roi_slider.set(new_index)
            self.draw_canvas()
Example #32
0
def image_cutout(request, id=0, size=0, mode='view'):
    image = Images.objects.get(id=id)
    filename = image.filename
    filename = posixpath.join(settings.BASE_DIR, filename)

    data = fits.getdata(filename, -1)
    header = fits.getheader(filename, -1)

    cdark = find_calibration_image(image, 'masterdark')
    if cdark is not None:
        dark = fits.getdata(cdark.filename, -1)
        if cdark is not None:
            dark = fits.getdata(cdark.filename, -1)
        else:
            cbias, cdc = find_calibration_image(
                image, 'bias'), find_calibration_image(image, 'dcurrent')
            if cbias is not None and cdc is not None:
                bias = fits.getdata(cbias.filename, -1)
                dc = fits.getdata(cdc.filename, -1)

                dark = bias + image.exposure * dc
            else:
                dark = None

        if dark is not None:
            data, header = calibrate.calibrate(
                data, header, dark=dark)  # Subtract dark and linearize

            cflat = find_calibration_image(image, 'masterflat')
            if cflat is not None:
                flat = fits.getdata(cflat.filename, -1)
                data *= np.median(flat) / flat

    ra, dec, sr = float(request.GET.get('ra')), float(
        request.GET.get('dec')), float(request.GET.get('sr'))

    wcs = WCS(header)
    x0, y0 = wcs.all_world2pix(ra, dec, sr)
    r0 = sr / np.hypot(wcs.pixel_scale_matrix[0, 0], wcs.pixel_scale_matrix[0,
                                                                            1])

    crop, cropheader = utils.crop_image(data, x0, y0, r0, header)

    if mode == 'download':
        s = StringIO()
        fits.writeto(s, crop, cropheader)

        response = HttpResponse(s.getvalue(),
                                content_type='application/octet-stream')
        response[
            'Content-Disposition'] = 'attachment; filename=crop_' + os.path.split(
                filename)[-1]
        response['Content-Length'] = len(s.getvalue())
        return response

    if size:
        if size > crop.shape[1]:
            crop = rescale(crop,
                           size / crop.shape[1],
                           mode='reflect',
                           multichannel=False,
                           anti_aliasing=False,
                           order=0)
        else:
            crop = rescale(crop,
                           size / crop.shape[1],
                           mode='reflect',
                           multichannel=False,
                           anti_aliasing=True)

    figsize = (crop.shape[1], crop.shape[0])

    fig = Figure(facecolor='white',
                 dpi=72,
                 figsize=(figsize[0] / 72, figsize[1] / 72))

    if np.any(np.isfinite(crop)):
        limits = np.percentile(crop[np.isfinite(crop)],
                               [0.5, float(request.GET.get('qq', 99.75))])
        fig.figimage(crop,
                     vmin=limits[0],
                     vmax=limits[1],
                     origin='lower',
                     cmap=request.GET.get('cmap', 'Blues_r'))

    canvas = FigureCanvas(fig)

    response = HttpResponse(content_type='image/jpeg')
    canvas.print_jpg(response)

    return response
class App:
    '''Tkinter GUI application'''
    def __init__(self, root, fname, img_width, img_height):
        self.fname = fname
        self.root = root
        self.rname = self.fname[0:-4] + 'dF.npz'
        self.img_width, self.img_height = img_width, img_height
        self.manual_thresh = StringVar()
        self.just_set_thresh = False
        self.manual_thresh.trace("w", self.manual_thresh_change)
        self.image = self.getIm()
        #self.image = load_stack(self.fname)
        self.convnet_rois = np.load(self.rname)['rois']
        self.convnet_roi_probs = np.load(self.rname)['roi_probabilities']
        self.roi_index = self.convnet_rois.shape[0]
        self.indexed_roi_probs = sorted(
            [(v, i) for i, v in enumerate(self.convnet_roi_probs)],
            reverse=True)
        self.image_index = 10
        self.make_widgets(root, img_width, img_height)
        self.draw_canvas()

    def getIm(self):
        im = io.imread(self.fname)
        im = np.divide(im, np.mean(im))
        return im

    def make_widgets(self, root, img_width, img_height):
        # enclosing frame
        frame = Frame(root)
        frame.grid(padx=10, pady=10)

        # canvas
        self.f = Figure(figsize=(img_width / 100.0, img_height / 100.0),
                        dpi=100)
        self.canvas = FigureCanvasTkAgg(self.f, master=frame)
        self.canvas.show()
        self.canvas.get_tk_widget().grid(column=0, row=1, rowspan=5)

        # other widgets
        self.filename_label = Label(frame, text="Filename")
        self.filename_label.grid(column=0, row=0)
        self.roi_slider = Scale(frame,
                                from_=self.convnet_rois.shape[0] - 1,
                                to=0,
                                orient=VERTICAL,
                                length=400,
                                command=self.roi_slider_change)
        self.roi_slider.grid(column=1, row=1, rowspan=4, padx=25)
        self.image_slider = Scale(frame,
                                  from_=0,
                                  to=self.image.shape[0] - 1,
                                  orient=HORIZONTAL,
                                  length=400,
                                  command=self.image_slider_change)
        self.image_slider.grid(column=0, row=6)
        self.save_button = Button(frame,
                                  text="Save Displayed ROIs",
                                  command=self.save_button_press)
        self.save_button.grid(column=2, columnspan=2, row=3)
        prev_next_frame = Frame(frame)
        prev_next_frame.grid(column=2, columnspan=2, row=4)

    def draw_canvas(self):
        '''draw current image and selected ROIs'''
        self.f.clf()
        frame = Frame(self.root)
        frame.grid(padx=10, pady=10)
        self.image_slider = Scale(frame,
                                  from_=0,
                                  to=self.image.shape[0] - 1,
                                  orient=HORIZONTAL,
                                  length=400,
                                  command=self.image_slider_change)
        overlay = np.zeros((self.image.shape[1], self.image.shape[2], 3))
        overlay[:, :, 0] = self.image[self.image_index, :, :]
        overlay[:, :, 1] = self.image[self.image_index, :, :]
        overlay[:, :, 2] = self.image[self.image_index, :, :]

        current_roi_indices = [
            i for (v, i) in self.indexed_roi_probs[0:self.roi_index]
        ]
        if len(current_roi_indices) > 0:
            print(self.roi_index - 1)
            cutoff = self.indexed_roi_probs[self.roi_index - 1][0]
            current_rois_mask = self.convnet_rois[current_roi_indices].max(
                axis=0)
            overlay[:, :, 2][current_rois_mask == 1] = 1
        else:
            cutoff = self.indexed_roi_probs[0][0]

        self.f.figimage(overlay)
        self.canvas.draw()

    def roi_slider_change(self, value):
        '''callback for threshold value slider'''
        self.roi_index = int(value)
        if self.manual_thresh.get() != "" and not self.just_set_thresh:
            self.manual_thresh.set("")
        self.just_set_thresh = False
        self.draw_canvas()

    def image_slider_change(self, value):
        '''callback for image sequence slider'''
        self.image_index = int(value)
        self.draw_canvas()

    def save_button_press(self):
        '''callback to save currently selected ROIs'''
        current_roi_indices = [
            i for (v, i) in self.indexed_roi_probs[0:self.roi_index]
        ]
        current_rois = self.convnet_rois[current_roi_indices]
        new_file = self.fname[0:-4] + "_ROIsManual.csv"
        self.saveROIsAsCSV(current_rois, new_file)
        #np.savez_compressed(new_file, rois=current_rois)
        print "saved as " + new_file

    def saveROIsAsCSV(self, rois, fname):
        dim = rois.shape
        outArr = np.zeros([dim[0], 4])  #minX, minY, maxX, maxY
        for x in range(dim[0]):
            #make a 1d array that sums elements across the first dim
            temp1 = np.sum(rois[x, :, :], axis=0)  #dim 1
            temp2 = np.sum(rois[x, :, :], axis=1)  #dim 2

            #now find the first and last nonzero elements in these 1ds
            temp1nz = np.nonzero(temp1)
            temp2nz = np.nonzero(temp2)

            outArr[x, :] = [
                temp1nz[0][0], temp2nz[0][0], temp1nz[-1][-1], temp2nz[-1][-1]
            ]
        np.savetxt(fname, outArr, delimiter=',', fmt='%i')

    def manual_thresh_change(self, *args):
        '''callback for manual thresholsd entry'''
        if self.manual_thresh.get().strip() != "":
            new_index = self.convnet_rois.shape[0]
            for j, (p, ind) in enumerate(self.indexed_roi_probs):
                if p < float(self.manual_thresh.get()):
                    new_index = j
                    break
            self.roi_index = new_index
            self.just_set_thresh = True
            self.roi_slider.set(new_index)
            self.draw_canvas(root)
Example #34
0
import utils
import numpy as np
import pytesseract
from PIL import Image
import pdb
import cv2

root = tkinter.Tk()
root.wm_title("Embedding in Tk")
img = utils.get_board('/Users/Alex/Desktop/Summer-2019/scrabble/labels.txt',
                      12)
im = Image.fromarray(img)
im = pytesseract.image_to_boxes(im)
fig = Figure(figsize=(5, 5), dpi=100)
t = np.arange(0, 3, .01)
fig.figimage(img, 0)
canvas = FigureCanvasTkAgg(fig, master=root)  # A tk.DrawingArea.
canvas.draw()
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)

toolbar = NavigationToolbar2Tk(canvas, root)
toolbar.update()
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)


def on_key_press(event):
    print("you pressed {}".format(event.key))
    key_press_handler(event, canvas, toolbar)


canvas.mpl_connect("key_press_event", on_key_press)
Example #35
0
    return S


## --- COULEURS ---
noir = cm.Greys
jaune = cm.gnuplot

## --- AFFICHAGE ---

matrice = Matrice(l)  #Initialisation de la matrice

fen = Tk.Tk()  # Fenêtre Tk principale
f = Figure(figsize=(l / resolution, l / resolution),
           dpi=resolution)  # Figure d'affichage Matplotlib

img = f.figimage(matrice.val, cmap=noir)  # Création de l'objet image

# Graphique du nombre de cellules vivantes
g = Figure(figsize=(4, 4), dpi=resolution)
graph = g.add_subplot(111)
graph.plot(np.arange(len(matrice.nb)), matrice.nb)

canvas1 = FigureCanvasTkAgg(f,
                            master=fen)  # Création d'un canvas Tk/Matplotlib
canvas1.show()
canvas1.get_tk_widget().pack(side=Tk.LEFT, fill=Tk.BOTH, expand=1)

canvas2 = FigureCanvasTkAgg(
    g, master=fen)  # Création d'un deuxième canvas Tk/Matplotlib
canvas2.show()
canvas2.get_tk_widget().pack(side=Tk.LEFT)
Example #36
0
    def train(self, sess, config):
        """ Training the GAN """
        print ('initializing...opt')
        d_opt = self.d_opt
        g_opt = self.g_opt

        try:
            init = tf.global_variables_initializer()
            sess.run(init)
        except AttributeError:
            init = tf.intializer_all_varialble()
            sess.run(init)

        print ('initializing...var')
        g_summaries = [self.d_fake_summary,
                        self.d_fake_loss_summary,
                        self.g_loss_summary,
                        self.g_l2_loss_summary,
                        self.g_loss_adv_summary,
                        self.generated_wav_summary,
                        self.generated_audio_summary]
        d_summaries = [self.d_loss_summary, self.d_real_summary, self.d_real_loss_summary, self.nonreverb_audio_summary, self.nonreverb_wav_summary]

        if hasattr(self, 'alpha_summ'):
            g_summaries += self.alpha_summ
        self.g_sum = tf.summary.merge(g_summaries)
        self.d_sum = tf.summary.merge(d_summaries)

        if not os.path.exists(os.path.join(config.save_path, 'train')):
            os.makedirs(os.path.join(config.save_path, 'train'))

        self.writer = tf.summary.FileWriter(os.path.join(config.save_path, 'train'), self.sess.graph)

        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(coord=coord)

        sample_reverb, sample_nonreverb, sample_z = self.sess.run([self.gt_reverb[0], self.gt_nonreverb[0], self.zz[0]], feed_dict={self.is_valid:False})
        v_sample_reverb, v_sample_nonreverb, v_sample_z = self.sess.run([self.gt_reverb[0], self.gt_nonreverb[0], self.zz[0]],
                                                          feed_dict={self.is_valid: True, self.is_train: False})

        print ('sample reverb shape: ', sample_reverb.shape)
        print ('sample nonreverb shape: ', sample_nonreverb.shape)
        print ('sample z shape: ', sample_z.shape)

        save_path = config.save_path
        counter = 0
        # count of num of samples
        num_examples = 0
        for record in tf.python_io.tf_record_iterator(self.tfrecords):
            num_examples += 1
        print ("total num of patches in tfrecords", self.tfrecords,":  ", num_examples)

        # last samples 
        # batch num
        num_batches = num_examples / self.batch_size
        print ('batches per epoch: ', num_batches)

        if self.load(self.save_path):
            print ('load success')
        else:
            print ('load failed')
        batch_idx = 0
        current_epoch = 0
        batch_timings = []
        g_losses = []
        d_fake_losses = []
        d_real_losses = []
        g_adv_losses = []
        g_l2_losses = []

        try:
            while not coord.should_stop():
                start = timeit.default_timer()
                if counter % config.save_freq == 0:

                    for d_iter in range(self.disc_updates):
                        _d_opt, _d_sum, d_fake_loss, d_real_loss = self.sess.run([d_opt, self.d_sum, self.d_fake_losses[0], self.d_real_losses[0]], feed_dict={self.is_valid:False, self.is_train: True})
                        #_d_sum, d_fake_loss, d_real_loss = self.sess.run(
                        #   [self.d_sum, self.d_fake_losses[0], self.d_real_losses[0]], feed_dict={self.is_valid: False})

                        if self.d_clip_weights:
                            self.sess.run(self.d_clip, feed_dict={self.is_valid:False,self.is_train: True})

                    #_g_opt, _g_sum, g_adv_loss, g_l2_loss = self.sess.run([g_opt, self.g_sum, self.g_adv_losses[0], self.g_l2_losses[0]], feed_dict={self.is_valid:False})
                    _g_opt, _g_sum, g_adv_loss, g_l2_loss = self.sess.run([g_opt, self.g_sum, self.g_adv_losses[0], self.g_l2_losses[0]], feed_dict={self.is_valid:False,self.is_train: True})

                else:
                    for d_iter in range(self.disc_updates):
                        _d_opt, d_fake_loss, d_real_loss = self.sess.run([d_opt, self.d_fake_losses[0], self.d_real_losses[0]], feed_dict={self.is_valid:False,self.is_train: True})
                        #d_fake_loss, d_real_loss = self.sess.run(
                        #    [self.d_fake_losses[0], self.d_real_losses[0]], feed_dict={self.is_valid: False})
                        if self.d_clip_weights:
                            self.sess.run(self.d_clip, feed_dict={self.is_valid:False,self.is_train: True})
                    #_g_opt, g_adv_loss, g_l2_loss = self.sess.run([g_opt, self.g_adv_losses[0], self.g_l2_losses[0]], feed_dict={self.is_valid:False})
                    _g_opt, g_adv_loss, g_l2_loss = self.sess.run([g_opt, self.g_adv_losses[0], self.g_l2_losses[0]], feed_dict={self.is_valid:False,self.is_train: True})

                end = timeit.default_timer()
                batch_timings.append(end - start)
                d_fake_losses.append(d_fake_loss)
                d_real_losses.append(d_real_loss)
                g_adv_losses.append(g_adv_loss)
                g_l2_losses.append(g_l2_loss)
                print('{}/{} (epoch {}), d_rl_loss = {:.5f}, '
                      'd_fk_loss = {:.5f}, '#d_nfk_loss = {:.5f}, '
                      'g_adv_loss = {:.5f}, g_l1_loss = {:.5f},'
                      ' time/batch = {:.5f}, '
                      'mtime/batch = {:.5f}'.format(counter,
                                                    config.epoch * num_batches,
                                                    current_epoch,
                                                    d_real_loss,
                                                    d_fake_loss,
                                                    g_adv_loss,
                                                    g_l2_loss,
                                                    end - start,
                                                    np.mean(batch_timings)))
                batch_idx += 1
                counter += 1

                if (counter) % 2000 == 0 and (counter) > 0:
                    self.save(config.save_path, counter)
                if (counter) % config.save_freq == 0:
                    self.writer.add_summary(_g_sum, counter)
                    self.writer.add_summary(_d_sum, counter)
                    #feed_dict = {self.gt_nonreverb[0]:v_sample_nonreverb, self.gt_reverb[0]:v_sample_reverb, self.zz[0]:v_sample_z, self.is_valid:True}

                    canvas_w, s_reverb, s_nonreverb = self.sess.run([self.GG[0], self.gt_reverb[0], self.gt_nonreverb[0]], feed_dict={self.is_valid:True,self.is_train: False})

                    if not os.path.exists(save_path+'/wav'):
                        os.makedirs(save_path + '/wav')
                    if not os.path.exists(save_path + '/txt'):
                        os.makedirs(save_path + '/txt')
                    if not os.path.exists(save_path + '/spec'):
                        os.makedirs(save_path + '/spec')

                    print ('max :', np.max(canvas_w[0]), 'min :', np.min(canvas_w[0]))

                    if self.pre_emphasis>0:
                        canvas_w = self.de_emphasis(canvas_w, self.pre_emphasis)
                        s_reverb = self.de_emphasis(s_reverb, self.pre_emphasis)
                        s_nonreverb = self.de_emphasis(s_nonreverb, self.pre_emphasis)


                    x_pr = canvas_w.flatten()
                    x_pr = x_pr[:int(len(x_pr)/8)]
                    x_lr = s_reverb.flatten()[:len(x_pr)]
                    x_hr = s_nonreverb.flatten()[:len(x_pr)]

                    Sl = self.get_spectrum(x_lr, n_fft=2048)
                    Sh = self.get_spectrum(x_hr, n_fft=2048)
                    Sp = self.get_spectrum(x_pr, n_fft=2048)

                    S = np.concatenate((Sl.reshape(Sh.shape[0], Sh.shape[1]), Sh, Sp), axis=1)
                    fig = Figure(figsize=S.shape[::-1], dpi=1, frameon=False)
                    canvas = FigureCanvas(fig)
                    fig.figimage(S, cmap='jet')
                    fig.savefig(save_path + '/spec/' + 'valid_batch_index' + str(counter) + '-th_pr.png')

                    librosa.output.write_wav(save_path + '/wav/'+str(counter)+'_dereverb.wav', x_pr, 16000)

                    librosa.output.write_wav(save_path + '/wav/'+str(counter)+'_reverb.wav', x_lr, 16000)

                    librosa.output.write_wav(save_path + '/wav/'+str(counter)+'_orig.wav', x_hr, 16000)

                    canvas_w, s_reverb, s_nonreverb = self.sess.run([self.GG[0],self.gt_reverb[0], self.gt_nonreverb[0]], feed_dict={self.is_valid:False, self.is_train: True})


                    print ('max :', np.max(canvas_w[0]), 'min :', np.min(canvas_w[0]))



                    x_pr = canvas_w.flatten()
                    x_pr = x_pr[:int(len(x_pr)/8)]
                    x_lr = s_reverb.flatten()[:len(x_pr)]
                    x_hr = s_nonreverb.flatten()[:len(x_pr)]

                    Sl = self.get_spectrum(x_lr, n_fft=2048)
                    Sh = self.get_spectrum(x_hr, n_fft=2048)
                    Sp = self.get_spectrum(x_pr, n_fft=2048)

                    S = np.concatenate((Sl.reshape(Sh.shape[0], Sh.shape[1]), Sh, Sp), axis=1)
                    fig = Figure(figsize=S.shape[::-1], dpi=1, frameon=False)
                    canvas = FigureCanvas(fig)
                    fig.figimage(S, cmap='jet')
                    fig.savefig(save_path + '/spec/' + 'train_batch_index' + str(counter) + '-th_pr.png')

                        #np.savetxt(os.path.join(save_path, '/txt/d_real_losses.txt'), d_real_losses)
                        #np.savetxt(os.path.join(save_path, '/txt/d_fake_losses.txt'), d_fake_losses)
                        #np.savetxt(os.path.join(save_path, '/txt/g_adv_losses.txt'), g_adv_losses)
                        #np.savetxt(os.path.join(save_path, '/txt/g_l2_losses.txt'), g_l2_losses)

                if batch_idx >= num_batches:
                    current_epoch += 1
                    #reset batch idx
                    batch_idx = 0

                if current_epoch >= config.epoch:
                    print (str(self.epoch),': epoch limit')
                    print ('saving last model at iteration',str(counter))
                    self.save(config.save_path, counter)
                    self.writer.add_summary(_g_sum, counter)
                    self.writer.add_summary(_d_sum, counter)
                    break

        except tf.errors.OutOfRangeError:
            print('done training')
            pass
        finally:
            coord.request_stop()
        coord.join(threads)
Example #37
0
def image_preview(request, id=0, size=0):
    image = Images.objects.get(id=id)
    filename = image.filename
    filename = posixpath.join(settings.BASE_DIR, filename)

    data = fits.getdata(filename, -1)
    header = fits.getheader(filename, -1)

    if 'size' in request.GET:
        size = int(request.GET.get('size', 0))

    if not 'raw' in request.GET:
        if image.type not in ['masterdark', 'masterflat', 'bias', 'dcurrent']:
            dark = None

            if image.type not in ['dark', 'zero']:
                cdark = find_calibration_image(image, 'masterdark')
                if cdark is not None:
                    dark = fits.getdata(cdark.filename, -1)
                else:
                    cbias, cdc = find_calibration_image(
                        image,
                        'bias'), find_calibration_image(image, 'dcurrent')
                    if cbias is not None and cdc is not None:
                        bias = fits.getdata(cbias.filename, -1)
                        dc = fits.getdata(cdc.filename, -1)

                        dark = bias + image.exposure * dc

            if dark is not None:
                data, header = calibrate.calibrate(
                    data, header, dark=dark)  # Subtract dark and linearize

                if image.type not in ['flat1']:
                    cflat = find_calibration_image(image, 'masterflat')
                    if cflat is not None:
                        flat = fits.getdata(cflat.filename, -1)
                        data *= np.median(flat) / flat
            else:
                data, header = calibrate.crop_overscans(data, header)

        ldata = data
    else:
        ldata, lheader = calibrate.crop_overscans(data, header, subtract=False)

    if size:
        data = rescale(data,
                       size / data.shape[1],
                       mode='reflect',
                       multichannel=False,
                       anti_aliasing=True,
                       preserve_range=True)

    figsize = (data.shape[1], data.shape[0])

    fig = Figure(facecolor='white',
                 dpi=72,
                 figsize=(figsize[0] / 72, figsize[1] / 72))

    limits = np.percentile(ldata[np.isfinite(ldata)],
                           [2.5, float(request.GET.get('qq', 99.75))])

    fig.figimage(data,
                 vmin=limits[0],
                 vmax=limits[1],
                 origin='lower',
                 cmap=request.GET.get('cmap', 'Blues_r'))

    canvas = FigureCanvas(fig)

    response = HttpResponse(content_type='image/jpeg')
    canvas.print_jpg(response)

    return response
Example #38
0
class Interface():
    def __init__(self, root, board, opp_type):

        self.board = board
        self.opponent = opp_type(self.board)

        self.root = root
        self.root.wm_title("ChessDisplay")

        self.f = Figure(figsize=(4, 4), dpi=100)
        self.ax1 = self.f.add_subplot(111)
        self.ax1.axis('off')

        self.dx = 56.5
        self.x_offset = 125.0
        self.dy = -56.0
        self.y_offset = 540.0

        img_dir = "../imgs/"

        img = image.imread(img_dir + 'board.gif')
        self.ax1.imshow(img)
        self.pieces_plot_list = []

        self.white_p = image.imread(img_dir + 'white_p.png')
        self.black_p = image.imread(img_dir + 'black_p.png')
        self.white_r = image.imread(img_dir + 'white_r.png')
        self.black_r = image.imread(img_dir + 'black_r.png')
        self.white_n = image.imread(img_dir + 'white_n.png')
        self.black_n = image.imread(img_dir + 'black_n.png')
        self.white_b = image.imread(img_dir + 'white_b.png')
        self.black_b = image.imread(img_dir + 'black_b.png')
        self.white_q = image.imread(img_dir + 'white_q.png')
        self.black_q = image.imread(img_dir + 'black_q.png')
        self.white_k = image.imread(img_dir + 'white_k.png')
        self.black_k = image.imread(img_dir + 'black_k.png')

        self.piece_dict = {
            'P': self.white_p,
            'p': self.black_p,
            'R': self.white_r,
            'r': self.black_r,
            'N': self.white_n,
            'n': self.black_n,
            'B': self.white_b,
            'b': self.black_b,
            'Q': self.white_q,
            'q': self.black_q,
            'K': self.white_k,
            'k': self.black_k
        }

        self.canvas = FigureCanvasTkAgg(self.f, master=root)
        self.canvas.draw()
        self.canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)

        self._update_plot()

        self.canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)

        self.q_button = Tk.Button(master=self.root,
                                  text='Quit',
                                  command=self._quit,
                                  width=10)
        self.q_button.pack(side=Tk.RIGHT, padx=10, pady=10)

        self.m_label = Tk.Label(master=self.root, text='Your move:')
        self.m_label.pack(side=Tk.LEFT, padx=5, pady=10)

        self.m_entry = Tk.Entry(master=self.root, width=25)
        self.m_entry.pack(side=Tk.LEFT, padx=5, pady=10)

        self.m_button = Tk.Button(master=self.root,
                                  text='Move',
                                  command=self._move,
                                  width=10)
        self.m_button.pack(side=Tk.LEFT, padx=5, pady=10)

        Tk.mainloop()

    def _update_plot(self):
        FEN_rep = self.board.fen()
        for piece in self.pieces_plot_list:
            piece.remove()
        ix = 0
        iy = 0
        self.pieces_plot_list = []
        for char in FEN_rep:
            # End of piece list
            if (char == ' '):
                break
            # Onto next rank
            if (char == '/'):
                iy += 1
                ix = 0
                continue
            # Empty squares
            if (char.isdigit()):
                ix += int(char)
            # There is a piece!
            else:
                ix += 1
                piece = self.f.figimage(self.piece_dict[char],
                                        self.dx * ix + self.x_offset,
                                        self.dy * iy + self.y_offset)
                self.pieces_plot_list.append(piece)
        self.canvas.draw()

    def _move(self):
        self.proposed_move = self.m_entry.get()
        try:
            self.board.push_san(self.proposed_move)
            legal_move = True
        except:
            legal_move = False
        if (legal_move):
            if (self.board.is_game_over()):
                self.ax1.text(0.5 * self.ax1.get_xlim()[-1],
                              0.5 * self.ax1.get_ylim()[-1],
                              "Game Over",
                              ha='center',
                              va='center')
            else:
                self.board.push(self.opponent.move())
            if (self.board.is_game_over()):
                self.ax1.text(0.5 * self.ax1.get_xlim()[-1],
                              0.5 * self.ax1.get_ylim()[-1],
                              "Game Over",
                              ha='center',
                              va='center')
        self.m_entry.delete(0, 'end')
        self._update_plot()

    def _quit(self):
        self.root.quit()  # stops mainloop
        self.root.destroy()  # this is necessary on Windows to prevent