<commit_before>#
#  DIALS viewer
#
#  Copyright (C) 2014 Diamond Light Source
#
#  Author: <NAME> (Luiso)
#
#  This code is distributed under the BSD license, a copy of which is
#  included in the root directory of this package."

import wx, os, numpy
import matplotlib.pyplot as plt
class TestFrame(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)

        self.MaxImageSize = 300

        btn_nxt_refl = wx.Button(self, -1, "Next Reflection ")
        b_a = wx.Button(self, -1, "Previous Reflection")
        btn_nxt_refl.Bind(wx.EVT_BUTTON, self.DisplayNext)
        b_a.Bind(wx.EVT_BUTTON, self.DisplayPrev)

        # starting with an EmptyBitmap
        self.Image = wx.StaticBitmap(self, bitmap=wx.EmptyBitmap(
                                     self.MaxImageSize, self.MaxImageSize))

        self.DisplayNext()

        # Using a Sizer to handle the layout: is not recommended to use absolute # positioning

        box = wx.BoxSizer(wx.VERTICAL)
        box.Add(btn_nxt_refl, 0, wx.CENTER | wx.ALL,10)

        # adding stretchable space before and after centers the image.
        box.Add((1,1),1)
        box.Add(self.Image
                , 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALL | wx.ADJUST_MINSIZE
                , 10)

        box.Add((1,1),1)
        box.Add(b_a, 0, wx.CENTER | wx.ALL,10)

        self.SetSizerAndFit(box)

        wx.EVT_CLOSE(self, self.OnCloseWindow)

    def DisplayNext(self, event = None):
        print "test 01"
        np_img = build_np_img(width = 64, height = 164)

        Img = GetBitmap_from_np_array(np_img)

        self.Image.SetBitmap(Img)
        self.Fit()
        self.Layout()
        self.Refresh()

    def DisplayPrev(self, event = None):
        print "test 01"
        np_img = build_np_img(width = 164, height = 64)

        Img = GetBitmap_from_np_array(np_img)

        self.Image.SetBitmap(Img)
        self.Fit()
        self.Layout()
        self.Refresh()

    def OnCloseWindow(self, event):
        self.Destroy()

def GetBitmap_from_np_array(np_img_2d):
  fig = plt.figure()
  # remember to make sure this is our convention in (x, y) vs (row, col)
  plt.imshow(numpy.transpose(np_img_2d), interpolation = "nearest")
  fig.canvas.draw()
  width, height = fig.canvas.get_width_height()
  np_buf = numpy.fromstring ( fig.canvas.tostring_rgb(), dtype=numpy.uint8 )
  np_buf.shape = (width, height, 3)
  np_buf = numpy.roll(np_buf, 3, axis = 2)
  image = wx.EmptyImage(width, height)
  image.SetData( np_buf )
  #image.SetData( np_buf.tostring()) # looks like there is no need to convert
  wxBitmap = image.ConvertToBitmap()

  return wxBitmap

def build_np_img(width = 64, height = 64):<commit_msg> adding wx.Button(s)<commit_after>#
#  DIALS viewer
#
#  Copyright (C) 2014 Diamond Light Source
#
#  Author: <NAME> (Luiso)
#
#  This code is distributed under the BSD license, a copy of which is
#  included in the root directory of this package."

import wx, os, numpy
import matplotlib.pyplot as plt
class TestFrame(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)

        self.MaxImageSize = 300

        btn_nxt_refl = wx.Button(self, -1, "Next Reflection ")
        btn_prv_refl = wx.Button(self, -1, "Previous Reflection")
        btn_nxt_refl.Bind(wx.EVT_BUTTON, self.DisplayNext_refl)
        btn_prv_refl.Bind(wx.EVT_BUTTON, self.DisplayPrev_refl)

        
        
        btn_nxt_slice = wx.Button(self, -1, "Next slice ")
        btn_prv_slice = wx.Button(self, -1, "Previous slice")
        btn_nxt_slice.Bind(wx.EVT_BUTTON, self.DisplayNext_slice)
        btn_prv_slice.Bind(wx.EVT_BUTTON, self.DisplayPrev_slice)
        
        
        
        # starting with an EmptyBitmap
        self.Image = wx.StaticBitmap(self, bitmap=wx.EmptyBitmap(
                                     self.MaxImageSize, self.MaxImageSize))

        self.DisplayNext_refl()

        # Using a Sizer to handle the layout: is not recommended to use absolute # positioning

        box = wx.BoxSizer(wx.VERTICAL)
        box.Add(btn_nxt_refl, 0, wx.CENTER | wx.ALL,10)

        # adding stretchable space before and after centers the image.
        box.Add((1,1),1)
        box.Add(self.Image
                , 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALL | wx.ADJUST_MINSIZE
                , 10)

        box.Add((1,1),1)
        box.Add(btn_prv_refl, 0, wx.CENTER | wx.ALL,10)


        box.Add((1,1),1)
        box.Add(btn_nxt_slice, 0, wx.CENTER | wx.ALL,10)

        box.Add((1,1),1)
        box.Add(btn_prv_slice, 0, wx.CENTER | wx.ALL,10)


        self.SetSizerAndFit(box)

        wx.EVT_CLOSE(self, self.OnCloseWindow)

    def DisplayNext_refl(self, event = None):

        np_img = build_np_img(width = 20, height = 30)
        Img = GetBitmap_from_np_array(np_img)

        self.Image.SetBitmap(Img)
        self.Fit()
        self.Layout()
        self.Refresh()

    def DisplayPrev_refl(self, event = None):

        np_img = build_np_img(width = 20, height = 10)
        Img = GetBitmap_from_np_array(np_img)

        self.Image.SetBitmap(Img)
        self.Fit()
        self.Layout()
        self.Refresh()

    def DisplayNext_slice(self, event = None):
        print "test 02"

    def DisplayPrev_slice(self, event = None):
        print "test 03"

    def OnCloseWindow(self, event):
        self.Destroy()

def GetBitmap_from_np_array(np_img_2d):
  fig = plt.figure()
  # remember to make sure this is our convention in (x, y) vs (row, col)
  plt.imshow(numpy.transpose(np_img_2d), interpolation = "nearest")
  fig.canvas.draw()
  width, height = fig.canvas.get_width_height()
  np_buf = numpy.fromstring ( fig.canvas.tostring_rgb(), dtype=numpy.uint8 )
  np_buf.shape = (width, height, 3)
  np_buf = numpy.roll(np_buf, 3, axis = 2)
  image = wx.EmptyImage(width, height)
  image.SetData( np_buf )
  #image.SetData( np_buf.tostring()) # looks like there is no need to convert
  wxBitmap = image.ConvertToBitmap()

  return wxBitmap

def build_np_img(width = 64, height = 64):
{'fraction_non_alphanumeric': 0.11334763240389374, 'fraction_numerical': 0.017818841775284606, 'mean_word_length': 2.5997624703087885, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '819563', 'n_tokens_mistral': 2196, 'n_tokens_neox': 2064, 'n_words': 477}