<commit_before>from __future__ import print_function

import os
import sys
import wx
import numpy as np
import matplotlib
matplotlib.interactive(False)
#Use the WxAgg back end. The Wx one takes too long to render
matplotlib.use('WXAgg')
from sas.sasgui.guiframe.local_perspectives.plotting.SimplePlot import PlotFrame
#import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import matplotlib.colors as colors
from sas.sasgui.guiframe.events import StatusEvent
from sas.sasgui.perspectives.calculator.calculator_widgets import InputTextCtrl
from sas.sascalc.dataloader.data_info import Data2D
from sas.sascalc.dataloader.data_info import Detector
from sas.sascalc.dataloader.manipulations import reader2D_converter
from sas.sasgui.guiframe.documentation_window import DocumentationWindow

_BOX_WIDTH = 60
IS_WIN = True
if sys.platform.count("win32") > 0:
    _DIALOG_WIDTH = 400
else:
    _DIALOG_WIDTH = 480
    IS_WIN = False

class ImageView:
    """
    Open a file dialog to allow the user to select a given file.
    Display the loaded data if available.
    """
    def __init__(self, parent=None):
        """
        Init
        """
        self.parent = parent

    def load(self):
        """
        load image files
        """
        parent = self.parent
        if parent is None:
            location = os.getcwd()
        else:
            location = parent._default_save_location
        path_list = self.choose_data_file(location=location)
        if path_list is None:
            return
        if len(path_list) >= 0 and path_list[0] is not None:
            if parent is not None:
                parent._default_save_location = os.path.dirname(path_list[0])
        err_msg = ''
        for file_path in path_list:
            basename = os.path.basename(file_path)
            _, extension = os.path.splitext(basename)
            try:
                img = mpimg.imread(file_path)
                is_png = extension.lower() == '.png'
                plot_frame = ImageFrame(parent, -1, basename, img)
                plot_frame.Show(False)
                ax = plot_frame.plotpanel
                if not is_png:
                    ax.subplot.set_ylim(ax.subplot.get_ylim()[::-1])
                ax.subplot.set_xlabel('x [pixel]')
                ax.subplot.set_ylabel('y [pixel]')
                ax.figure.subplots_adjust(left=0.15, bottom=0.1,
                                          right=0.95, top=0.95)
                plot_frame.SetTitle('Picture -- %s --' % basename)
                plot_frame.Show(True)
                if parent is not None:
                    parent.put_icon(plot_frame)
            except:
                err_msg += "Failed to load '%s'.\n" % basename
        if err_msg:
            if parent is not None:
                wx.PostEvent(parent, StatusEvent(status=err_msg, info="error"))
            else:
                print(err_msg)

    def choose_data_file(self, location=None):
        """
        Open a file dialog to allow loading a file
        """
        path = None
        if location is None:
            location = os.getcwd()
        dlg = wx.FileDialog(self.parent, "Image Viewer: Choose a image file",
                            location, "", "", style=wx.FD_OPEN | wx.FD_MULTIPLE)
        if dlg.ShowModal() == wx.ID_OK:
            path = dlg.GetPaths()
        else:
            return None
        dlg.Destroy()
        return path<commit_msg>Added image file selector for the Image Viewer Tool. Fixes #898<commit_after>from __future__ import print_function

import os
import sys
import wx
import numpy as np
import matplotlib
matplotlib.interactive(False)
#Use the WxAgg back end. The Wx one takes too long to render
matplotlib.use('WXAgg')
from sas.sasgui.guiframe.local_perspectives.plotting.SimplePlot import PlotFrame
#import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import matplotlib.colors as colors
from sas.sasgui.guiframe.events import StatusEvent
from sas.sasgui.perspectives.calculator.calculator_widgets import InputTextCtrl
from sas.sascalc.dataloader.data_info import Data2D
from sas.sascalc.dataloader.data_info import Detector
from sas.sascalc.dataloader.manipulations import reader2D_converter
from sas.sasgui.guiframe.documentation_window import DocumentationWindow

_BOX_WIDTH = 60
IS_WIN = True
if sys.platform.count("win32") > 0:
    _DIALOG_WIDTH = 400
else:
    _DIALOG_WIDTH = 480
    IS_WIN = False

class ImageView:
    """
    Open a file dialog to allow the user to select a given file.
    Display the loaded data if available.
    """
    def __init__(self, parent=None):
        """
        Init
        """
        self.parent = parent

    def load(self):
        """
        load image files
        """
        parent = self.parent
        if parent is None:
            location = os.getcwd()
        else:
            location = parent._default_save_location
        path_list = self.choose_data_file(location=location)
        if path_list is None:
            return
        if len(path_list) >= 0 and path_list[0] is not None:
            if parent is not None:
                parent._default_save_location = os.path.dirname(path_list[0])
        err_msg = ''
        for file_path in path_list:
            basename = os.path.basename(file_path)
            _, extension = os.path.splitext(basename)
            try:
                img = mpimg.imread(file_path)
                is_png = extension.lower() == '.png'
                plot_frame = ImageFrame(parent, -1, basename, img)
                plot_frame.Show(False)
                ax = plot_frame.plotpanel
                if not is_png:
                    ax.subplot.set_ylim(ax.subplot.get_ylim()[::-1])
                ax.subplot.set_xlabel('x [pixel]')
                ax.subplot.set_ylabel('y [pixel]')
                ax.figure.subplots_adjust(left=0.15, bottom=0.1,
                                          right=0.95, top=0.95)
                plot_frame.SetTitle('Picture -- %s --' % basename)
                plot_frame.Show(True)
                if parent is not None:
                    parent.put_icon(plot_frame)
            except:
                err_msg += "Failed to load '%s'.\n" % basename
        if err_msg:
            if parent is not None:
                wx.PostEvent(parent, StatusEvent(status=err_msg, info="error"))
            else:
                print(err_msg)

    def choose_data_file(self, location=None):
        """
        Open a file dialog to allow loading a file
        """
        path = None
        if location is None:
            location = os.getcwd()
        wildcard="Bitmap (*.bmp)|*.bmp|"\
            "GIF (*.gif)|*.gif|"\
            "JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|"\
            "PNG (*.png)|*.png|"\
            "TIFF (*.tif;*.tiff)|*.tif;*tiff|"\
            "All Files (*.*)|*.*|"

        dlg = wx.FileDialog(self.parent, "Image Viewer: Choose an image file",
                            location, "", wildcard, style=wx.FD_OPEN | wx.FD_MULTIPLE)
        if dlg.ShowModal() == wx.ID_OK:
            path = dlg.GetPaths()
        else:
            return None
        dlg.Destroy()
        return path
{'fraction_non_alphanumeric': 0.09512534818941504, 'fraction_numerical': 0.008495821727019499, 'mean_word_length': 1.9502875924404273, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2977089', 'n_tokens_mistral': 2229, 'n_tokens_neox': 2128, 'n_words': 534}