<commit_before>import wx
import numpy as np
import re
import os
import gpfile

import matplotlib
matplotlib.use('WXAgg')
import matplotlib.pyplot as plt

from matplotlib.pyplot import cm
from floatspin import FloatSpin,EVT_FLOATSPIN
from floatslider import FloatSlider

from wx.lib.masked import NumCtrl,EVT_NUM

#import view

from plotframe import PlotFrame
from lineoutframe import LineoutFrame
from linecutframe import LinecutFrame
from distanceframe import DistanceFrame
# from shellframe import ShellFrame

from slopeexframe import SlopeExFrame
# Experimental feature
# from BinaryFitFrame import BinaryFitFrame
from labelticksframe import LabelticksFrame

from pydispatch import dispatcher
import signal

import utils
import yaml
import logging


"""
The mainframe module hostst the MainFrame class and the MainPanel class.
In the MainFrame.__init__ the view object is intialized and all other frames 
are created.
The MainPanel hosts the colorbar controls and the mod plugin widgets.
"""

class MainFrame(wx.Frame):
    """
    Central class of the application.
    
    The MainFrame class hosts all subframes, the datafile object,
    the colormap and information on ticks and labels.
    """

    title = 'colorplot utility: '

    def __init__(self, parent, cvfig):
        """
        Initialize the frame, create subframes and load a default 
        datafile object.
        
        Create Menu, MainPanel, PlotFrame, LineoutFrame, LinecutFrame and
        SlopeExFrame. All utility frames are created beforehand and viewed
        on demand.
        """
        # find a way to autosize the frame!
        # this is annoying, especially with respect to the plugins
        wx.Frame.__init__(self, None, wx.ID_ANY, size=(630,590),style=wx.DEFAULT_FRAME_STYLE)
        self.parent = parent
        self.cvfig = cvfig        

        # Set title for the frame and align
        self.SetTitle(self.title+self.cvfig.datafile.filename)
        self.alignToBottomRight()

        # Create menu and status bar
        self.create_menu()
        self.create_status_bar()

        # The plot frame is created first with a dummy plot
        self.PlotFrame = PlotFrame(self, self.cvfig)

        # The MainPanel contains all the colorbar controls
        # The PlotPanel listens to the MainPanel for changes (e.g. to the colorbar)
        self.MainPanel = MainPanel(self, self.cvfig)
        # self.MainPanel.attach(self.PlotFrame.PlotPanel)

        # The frame with the settings (font, ticks, labels, etc)
        self.LabelticksFrame = LabelticksFrame(self, self.cvfig)

        # We have to draw the plot first before we can apply the modlist
        # dispatcher.send(signal.PLOT_DRAW_ANEW,self)

        # The other tools are intialized
        self.LineoutFrame = LineoutFrame(self, self.cvfig)
        self.SlopeExFrame = SlopeExFrame(self, self.cvfig)
        self.DistanceFrame = DistanceFrame(self, self.cvfig)
        # self.ShellFrame = ShellFrame(self, self.cvfig)

        self.PlotFrame.Show()
        self.PlotFrame.Layout()


    def alignToBottomRight(self):
        """
        Used to align the Main Window to the bottom right of the screen.
        """
        dw, dh = wx.DisplaySize()
        w, h = self.GetSize()
        x = dw -w
        y = dh-1.2*h
        self.SetPosition((x, y))


    def create_menu(self):
        """
        Creates the Menu in the toolbar.
        """
        self.menubar = wx.MenuBar()
        menu_file = wx.Menu()
        menu_tools = wx.Menu()
        menu_axes = wx.Menu()

        m_load = menu_file.Append(wx.ID_ANY, "&Load Data\tCtrl-L", "Load data from file")
        self.Bind(wx.EVT_MENU, self.on_load_plot, m_load)
        m_loadcv2d = menu_file.Append(wx.ID_ANY, "Load &Config\tCtrl-C", "Load cv2d config file")
        self.Bind(wx.EVT_MENU, self.on_load_cv2d, m_loadcv2d)
        m_svdt = menu_file.Append(wx.ID_ANY, "&Save Data\tCtrl-S", "Save data to file")
        self.Bind(wx.EVT_MENU, self.on_save_datafile, m_svdt)
        menu_file.AppendSeparator()
        m_svpdf = menu_file.Append(wx.ID_ANY, "Save &Pdf\tCtrl-P", "Save to pdf file")
        self.Bind(wx.EVT_MENU, self.on_save_pdf, m_svpdf)
        m_svcfg = menu_file.Append(wx.ID_ANY, "Save Config", "Save cv2d config file")
        self.Bind(wx.EVT_MENU, self.on_save_cv2d, m_svcfg)
        menu_file.AppendSeparator()
        m_exit = menu_file.Append(wx.ID_ANY, "E&xit\tCtrl-X", "Exit")
        self.Bind(wx.EVT_MENU, self.on_exit, m_exit)

        m_labelticks = menu_axes.Append(wx.ID_ANY, "&Configure Plot\tCtrl-C", "Set axes labels and tick label format")
        self.Bind(wx.EVT_MENU,self.on_labelticks,m_labelticks)


        m_linecut = menu_tools.Append(wx.ID_ANY,  "Linecut &Extraction\tCtrl-E", "Extract linecut series")
        self.Bind(wx.EVT_MENU, self.on_linecut, m_linecut)

        m_slopex = menu_tools.Append(wx.ID_ANY,  "Sl&ope Extraction\tCtrl-O", "Extract linear slope")
        self.Bind(wx.EVT_MENU, self.on_slopex, m_slopex)

        m_lineout = menu_tools.Append(wx.ID_ANY,  "Linecut &Viewer\tCtrl-V", "Plot data along line")
        self.Bind(wx.EVT_MENU, self.on_lineout, m_lineout)

        m_distance = menu_tools.Append(wx.ID_ANY,  "&Distance Viewer\tCtrl-D", "Measure distances")
        self.Bind(wx.EVT_MENU, self.on_distance, m_distance)

        #m_shell = menu_tools.Append(wx.ID_ANY,  "S&hell\tCtrl-h", "Open a python shell")
        #self.Bind(wx.EVT_MENU, self.on_shell, m_shell)

        # m_binaryfit = menu_tools.Append(wx.ID_ANY, "Segment and Fit (beta!)", "Fit to prominent data features")
        # self.Bind(wx.EVT_MENU,self.on_binaryfit,m_binaryfit)


        menu_help = wx.Menu()
        m_about = menu_help.Append(wx.ID_ANY, "&About\tF1", "About colorview2d.")
        self.Bind(wx.EVT_MENU, self.on_about, m_about)

        self.menubar.Append(menu_file, "&File")
        self.menubar.Append(menu_axes, "&Axes")
        self.menubar.Append(menu_tools, "&Tools")
        self.menubar.Append(menu_help, "&Help")
        self.SetMenuBar(self.menubar)


    def create_status_bar(self):
        self.statusbar = self.CreateStatusBar()

    def on_labelticks(self,event):
        """
        Show the previously created labelticks frame.
        """
        self.LabelticksFrame.Show()

    def on_lineout(self,event):
        """
        Shows the Frame to extract linecuts.
        
        """
        self.LineoutFrame.Show()

    def on_shell(self,event):
        """
        Shows the Frame to extract linecuts.
        
        """
        # self.ShellFrame.Show()


        
    def on_binaryfit(self,event):
        """
        Shows the fitting Frame.

        The frame is modal. A previously created fit is redrawn.
        """
        #self.BinaryFitFrame = BinaryFitFrame(self)
        #self.BinaryFitFrame.Show()

    def on_linecut(self,event):
        """
        Shows the frame to extract linecut series.

        """
        #self.PlotFrame.PlotPanel.axes.autoscale(False)
        self.LinecutFrame = LinecutFrame(self)
        dispatcher.send(signal.PLOT_AUTOSCALE_OFF,self)
        self.LinecutFrame.Show()

    def on_slopex(self,event):
        """
        Shows the Frame to extract slopes.

        """
        self.SlopeExFrame.Show()

    def on_distance(self,event):
        """
        Shows the Frame to measure distances.

        """
        self.DistanceFrame.Show()


    def on_save_datafile(self, event):
        """
        Saves the datafile in gnuplot format.

        The datafile is saved with information
        on the currently applied modifications and their parameters.
        """
        file_choices = "DAT (*.dat)|*.dat"


        comment = """\
# Original filename: {}
#
# data modifications: {}
#
# axes: {} | {} | {}
""".format(self.cvfig.datafile.filename,view.pipeline,self.Xlabel,self.Ylabel,self.Cblabel)

        dlg = wx.FileDialog(
            self,
            message="Save plot data as...",
            defaultDir=os.getcwd(),
            defaultFile=self.cvfig.datafile.filename,
            wildcard=file_choices,
            style=wx.SAVE)

        if dlg.ShowModal() == wx.ID_OK:
            path = dlg.GetPath()
            self.cvfig.datafile.save(path,comment)


    def on_save_pdf(self, event):
        """
        Saves the plot in pdf format.

        """
        file_choices = "PDF (*.pdf)|*.pdf"
        defaultfilename = os.path.splitext(self.cvfig.datafile.filename)[0]+'.pdf'

        dlg = wx.FileDialog(
            self,
            message="Save plot as...",
            defaultDir=os.getcwd(),
            defaultFile=defaultfilename,
            wildcard=file_choices,
            style=wx.SAVE)

        if dlg.ShowModal() == wx.ID_OK:
            path = dlg.GetPath()
            self.PlotFrame.PlotPanel.fig.set_size_inches(self.cvfig.config['Width'], self.cvfig.config['Height'])
            self.PlotFrame.PlotPanel.fig.savefig(path, dpi = self.cvfig.config['Dpi'])
            self.PlotFrame.PlotPanel.Layout()

    def on_save_cv2d(self, event):
        """
        Saves a cv2d config file.

        """
        file_choices = "CV2D (*.cv2d)|*.cv2d"
        defaultfilename = os.path.splitext(self.cvfig.datafile.filename)[0]+'.cv2d'
<commit_msg>Added tight layout call to correct the broken labels in the pdf export.<commit_after>import wx
import numpy as np
import re
import os
import gpfile

import matplotlib
matplotlib.use('WXAgg')
import matplotlib.pyplot as plt

from matplotlib.pyplot import cm
from floatspin import FloatSpin,EVT_FLOATSPIN
from floatslider import FloatSlider

from wx.lib.masked import NumCtrl,EVT_NUM

#import view

from plotframe import PlotFrame
from lineoutframe import LineoutFrame
from linecutframe import LinecutFrame
from distanceframe import DistanceFrame
# from shellframe import ShellFrame

from slopeexframe import SlopeExFrame
# Experimental feature
# from BinaryFitFrame import BinaryFitFrame
from labelticksframe import LabelticksFrame

from pydispatch import dispatcher
import signal

import utils
import yaml
import logging


"""
The mainframe module hostst the MainFrame class and the MainPanel class.
In the MainFrame.__init__ the view object is intialized and all other frames 
are created.
The MainPanel hosts the colorbar controls and the mod plugin widgets.
"""

class MainFrame(wx.Frame):
    """
    Central class of the application.
    
    The MainFrame class hosts all subframes, the datafile object,
    the colormap and information on ticks and labels.
    """

    title = 'colorplot utility: '

    def __init__(self, parent, cvfig):
        """
        Initialize the frame, create subframes and load a default 
        datafile object.
        
        Create Menu, MainPanel, PlotFrame, LineoutFrame, LinecutFrame and
        SlopeExFrame. All utility frames are created beforehand and viewed
        on demand.
        """
        # find a way to autosize the frame!
        # this is annoying, especially with respect to the plugins
        wx.Frame.__init__(self, None, wx.ID_ANY, size=(630,590),style=wx.DEFAULT_FRAME_STYLE)
        self.parent = parent
        self.cvfig = cvfig        

        # Set title for the frame and align
        self.SetTitle(self.title+self.cvfig.datafile.filename)
        self.alignToBottomRight()

        # Create menu and status bar
        self.create_menu()
        self.create_status_bar()

        # The plot frame is created first with a dummy plot
        self.PlotFrame = PlotFrame(self, self.cvfig)

        # The MainPanel contains all the colorbar controls
        # The PlotPanel listens to the MainPanel for changes (e.g. to the colorbar)
        self.MainPanel = MainPanel(self, self.cvfig)
        # self.MainPanel.attach(self.PlotFrame.PlotPanel)

        # The frame with the settings (font, ticks, labels, etc)
        self.LabelticksFrame = LabelticksFrame(self, self.cvfig)

        # We have to draw the plot first before we can apply the modlist
        # dispatcher.send(signal.PLOT_DRAW_ANEW,self)

        # The other tools are intialized
        self.LineoutFrame = LineoutFrame(self, self.cvfig)
        self.SlopeExFrame = SlopeExFrame(self, self.cvfig)
        self.DistanceFrame = DistanceFrame(self, self.cvfig)
        # self.ShellFrame = ShellFrame(self, self.cvfig)

        self.PlotFrame.Show()
        self.PlotFrame.Layout()


    def alignToBottomRight(self):
        """
        Used to align the Main Window to the bottom right of the screen.
        """
        dw, dh = wx.DisplaySize()
        w, h = self.GetSize()
        x = dw -w
        y = dh-1.2*h
        self.SetPosition((x, y))


    def create_menu(self):
        """
        Creates the Menu in the toolbar.
        """
        self.menubar = wx.MenuBar()
        menu_file = wx.Menu()
        menu_tools = wx.Menu()
        menu_axes = wx.Menu()

        m_load = menu_file.Append(wx.ID_ANY, "&Load Data\tCtrl-L", "Load data from file")
        self.Bind(wx.EVT_MENU, self.on_load_plot, m_load)
        m_loadcv2d = menu_file.Append(wx.ID_ANY, "Load &Config\tCtrl-C", "Load cv2d config file")
        self.Bind(wx.EVT_MENU, self.on_load_cv2d, m_loadcv2d)
        m_svdt = menu_file.Append(wx.ID_ANY, "&Save Data\tCtrl-S", "Save data to file")
        self.Bind(wx.EVT_MENU, self.on_save_datafile, m_svdt)
        menu_file.AppendSeparator()
        m_svpdf = menu_file.Append(wx.ID_ANY, "Save &Pdf\tCtrl-P", "Save to pdf file")
        self.Bind(wx.EVT_MENU, self.on_save_pdf, m_svpdf)
        m_svcfg = menu_file.Append(wx.ID_ANY, "Save Config", "Save cv2d config file")
        self.Bind(wx.EVT_MENU, self.on_save_cv2d, m_svcfg)
        menu_file.AppendSeparator()
        m_exit = menu_file.Append(wx.ID_ANY, "E&xit\tCtrl-X", "Exit")
        self.Bind(wx.EVT_MENU, self.on_exit, m_exit)

        m_labelticks = menu_axes.Append(wx.ID_ANY, "&Configure Plot\tCtrl-C", "Set axes labels and tick label format")
        self.Bind(wx.EVT_MENU,self.on_labelticks,m_labelticks)


        m_linecut = menu_tools.Append(wx.ID_ANY,  "Linecut &Extraction\tCtrl-E", "Extract linecut series")
        self.Bind(wx.EVT_MENU, self.on_linecut, m_linecut)

        m_slopex = menu_tools.Append(wx.ID_ANY,  "Sl&ope Extraction\tCtrl-O", "Extract linear slope")
        self.Bind(wx.EVT_MENU, self.on_slopex, m_slopex)

        m_lineout = menu_tools.Append(wx.ID_ANY,  "Linecut &Viewer\tCtrl-V", "Plot data along line")
        self.Bind(wx.EVT_MENU, self.on_lineout, m_lineout)

        m_distance = menu_tools.Append(wx.ID_ANY,  "&Distance Viewer\tCtrl-D", "Measure distances")
        self.Bind(wx.EVT_MENU, self.on_distance, m_distance)

        #m_shell = menu_tools.Append(wx.ID_ANY,  "S&hell\tCtrl-h", "Open a python shell")
        #self.Bind(wx.EVT_MENU, self.on_shell, m_shell)

        # m_binaryfit = menu_tools.Append(wx.ID_ANY, "Segment and Fit (beta!)", "Fit to prominent data features")
        # self.Bind(wx.EVT_MENU,self.on_binaryfit,m_binaryfit)


        menu_help = wx.Menu()
        m_about = menu_help.Append(wx.ID_ANY, "&About\tF1", "About colorview2d.")
        self.Bind(wx.EVT_MENU, self.on_about, m_about)

        self.menubar.Append(menu_file, "&File")
        self.menubar.Append(menu_axes, "&Axes")
        self.menubar.Append(menu_tools, "&Tools")
        self.menubar.Append(menu_help, "&Help")
        self.SetMenuBar(self.menubar)


    def create_status_bar(self):
        self.statusbar = self.CreateStatusBar()

    def on_labelticks(self,event):
        """
        Show the previously created labelticks frame.
        """
        self.LabelticksFrame.Show()

    def on_lineout(self,event):
        """
        Shows the Frame to extract linecuts.
        
        """
        self.LineoutFrame.Show()

    def on_shell(self,event):
        """
        Shows the Frame to extract linecuts.
        
        """
        # self.ShellFrame.Show()


        
    def on_binaryfit(self,event):
        """
        Shows the fitting Frame.

        The frame is modal. A previously created fit is redrawn.
        """
        #self.BinaryFitFrame = BinaryFitFrame(self)
        #self.BinaryFitFrame.Show()

    def on_linecut(self,event):
        """
        Shows the frame to extract linecut series.

        """
        #self.PlotFrame.PlotPanel.axes.autoscale(False)
        self.LinecutFrame = LinecutFrame(self)
        dispatcher.send(signal.PLOT_AUTOSCALE_OFF,self)
        self.LinecutFrame.Show()

    def on_slopex(self,event):
        """
        Shows the Frame to extract slopes.

        """
        self.SlopeExFrame.Show()

    def on_distance(self,event):
        """
        Shows the Frame to measure distances.

        """
        self.DistanceFrame.Show()


    def on_save_datafile(self, event):
        """
        Saves the datafile in gnuplot format.

        The datafile is saved with information
        on the currently applied modifications and their parameters.
        """
        file_choices = "DAT (*.dat)|*.dat"


        comment = """\
# Original filename: {}
#
# data modifications: {}
#
# axes: {} | {} | {}
""".format(self.cvfig.datafile.filename,view.pipeline,self.Xlabel,self.Ylabel,self.Cblabel)

        dlg = wx.FileDialog(
            self,
            message="Save plot data as...",
            defaultDir=os.getcwd(),
            defaultFile=self.cvfig.datafile.filename,
            wildcard=file_choices,
            style=wx.SAVE)

        if dlg.ShowModal() == wx.ID_OK:
            path = dlg.GetPath()
            self.cvfig.datafile.save(path,comment)


    def on_save_pdf(self, event):
        """
        Saves the plot in pdf format.

        """
        file_choices = "PDF (*.pdf)|*.pdf"
        defaultfilename = os.path.splitext(self.cvfig.datafile.filename)[0]+'.pdf'

        dlg = wx.FileDialog(
            self,
            message="Save plot as...",
            defaultDir=os.getcwd(),
            defaultFile=defaultfilename,
            wildcard=file_choices,
            style=wx.SAVE)

        if dlg.ShowModal() == wx.ID_OK:
            path = dlg.GetPath()
            self.PlotFrame.PlotPanel.fig.set_size_inches(self.cvfig.config['Width'], self.cvfig.config['Height'])
            self.PlotFrame.PlotPanel.fig.tight_layout()
            self.PlotFrame.PlotPanel.fig.savefig(path, dpi = self.cvfig.config['Dpi'])
            self.PlotFrame.PlotPanel.Layout()

    def on_save_cv2d(self, event):
        """
        Saves a cv2d config file.

        """
        file_choices = "CV2D (*.cv2d)|*.cv2d"
        defaultfilename = os.path.splitext(self.cvfig.datafile.filename)[0]+'.cv2d'

{'fraction_non_alphanumeric': 0.10513565891472869, 'fraction_numerical': 0.002583979328165375, 'mean_word_length': 2.5270552496677428, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '481111', 'n_tokens_mistral': 6130, 'n_tokens_neox': 5902, 'n_words': 1375}