<commit_before>import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import numpy as np
#import threading
#from threading import Thread
from multiprocessing import Process
#from statistics import median
import sys
sys.path.append('../genetic_algorithm')
sys.path.append('../particle_swarm_optimization')
sys.path.append('../functions')
sys.path.append('../utils')

from oa_utils import optimize_settings, write_xy_data
import regression_utils

from genetic_algorithm import GA
import ga_settings
from particle_swarm_optimization import PSO
import pso_settings
import ackley_function
import easom_function
import rosenbrock_function
import griewank_function

def gen_filename(x1_name, x2_name):
    return x1_name + ',' + x2_name + '.dat'

def num_parts_vs_time(o_algorithm, num_parts):
    tests = {}

    for test in num_parts:
        settings['population_size'] = test

        algorithm = o_algorithm(settings, objective_function)

        algorithm.start_timer()
        algorithm.run()
        algorithm.stop_timer()

        x = algorithm.get_best_x()

        print("%5d: %7.3f" % (test, algorithm.get_time()))

        tests[test] = {'time': algorithm.get_time(), 'accuracy': abs(x.get_fitness())}

    return tests

def func_val_vs_iterations(o_algorithm, num_parts):
    fig, ax = plt.subplots(nrows=1, ncols=1)

    for test in num_parts:
        f = []

        settings.settings['population_size'] = test

        algorithm = o_algorithm(settings, objective_function)

        while settings['num_iterations'] > algorithm.num_generations:
            f.append(algorithm.get_best_f())
            algorithm.do_loop()

        ax.plot(range(1,len(f)+1), f)

    plt.title('Number of particles vs iterations')
    plt.xlabel('Number of Iterations')
    plt.ylabel('Objective Function Value')
    plt.legend(num_parts)
    fig_name = 'func_value_vs_iterations'
    fig.savefig(fig_name + '.png')
    plt.close(fig)

# saves all test data to file
# x1_name: name of setting in settings file
# x2_name: name of setting in settings file
def get_two_d_accuracy(o_algorithm, o_settings, o_function, \
                           x1_start, x1_step, x1_end, \
                           x2_start, x2_step, x2_end, \
                           x1_name, x2_name, \
                           population_size=50, num_tests_per_point=10, plot=True, \
                           save_histograms=True, response_surface=True, debug=False):
    if response_surface:
        plot = True

    # turn off settings that slow us down
    o_settings = optimize_settings(o_settings)

    tests = {}
    hist_num_bins = 150

    o_settings['population_size'] = population_size
    num_tests_per = num_tests_per_point
    x1_srt = x1_start
    x1_e   = x1_end
    x1_stp = x1_step
    x2_srt = x2_start
    x2_e   = x2_end
    x2_stp = x2_step
    num_tests = int(( (int(100*x1_e)-int(100*x1_srt))/int(100*x1_stp) + 1 )* \
                    ( (int(100*x2_e)-int(100*x2_srt))/int(100*x2_stp) + 1 ))
    X = np.ones(shape=(num_tests,6))
    y = np.zeros(shape=(num_tests,1))

    n = 0

    if plot:
        fig = plt.figure()
        ax1 = fig.add_subplot(111, projection='3d')
        if o_function != griewank_function.objective_function:
            ax1.set_zlim(0, 1)

    for i in np.arange(x1_srt, x1_e+x1_stp, x1_stp):
        for j in np.arange(x2_srt, x2_e+x2_stp, x2_stp):
            # set settings for this test
            o_settings[x1_name] = i
            o_settings[x2_name] = j

            # initial variables
            values = []
            euclid_distance = []
            test_name = x1_name + '(' + str(i) + ')'+ ',' + x2_name + '(' + str(j) + ')'

            print("Running test %s" % test_name)

            # create histogram plot if true
            if save_histograms:
                hist_fig = plt.figure()
                hist_ax = hist_fig.add_subplot(111)

            # run optimization algorithm
            for k in range(0,num_tests_per):
                algorithm = o_algorithm(o_settings, o_function)
                algorithm.run()
                # save enf values
                values.append(algorithm.get_best_x().get_fval())
                # euclidean distance
                squares = 0
                for square in algorithm.get_best_x().pos:
                    if o_function == rosenbrock_function.objective_function:
                        squares += (square-1)**2
                    elif o_function == easom_function.objective_function:
                        squares += (square-np.pi)**2
                    else:
                        squares += square**2
                euclid_distance.append(np.sqrt(squares))

            # save histogram if true
            if save_histograms:
                hist_ax.hist(values, hist_num_bins, range=(0, 1.5), normed=True)
                hist_fig.savefig(test_name + '.png')
                plt.close(hist_fig)

            # find average and save data
            #avg = sum(values)/len(values)
            #avg = median(values)
            avg = sum(euclid_distance)/len(euclid_distance)
            #avg = median(euclid_distance)
            tests[test_name] = avg
            if plot:
                ax1.scatter(i,j,avg)

            X[n][1] = i
            X[n][2] = j
            X[n][3] = i*i
            X[n][4] = i*j
            X[n][5] = j*j
            y[n] = avg

            # increment test number
            n += 1

    fname = gen_filename(x1_name, x2_name)
    write_xy_data(X, y, fname)

    if debug:
        print("\n*** DATA ***")
        print("X")
        print(X)
        print("\ny")
        print(y)
        print("\ntests")
        print(tests)

    if response_surface:
        # get regression coefficients
        b = regression_utils.get_regression_coef(X, y)

        pltx = np.arange(x1_start, x1_end+x1_step, x1_step)
        plty = np.arange(x2_start, x2_end+x2_step, x2_step)
        pltX, pltY = np.meshgrid(pltx, plty)
        F = b[0] + b[1]*pltX + b[2]*pltY + b[3]*pltX*pltX + b[4]*pltX*pltY + b[5]*pltY*pltY
        ax1.plot_wireframe(pltX, pltY, F)

    if plot:
        print("\nPlotting ...")
        x1_name = x1_name[0].upper() + x1_name[1:]
        x2_name = x2_name[0].upper() + x2_name[1:]
        ax1.set_xlabel(x1_name)
        ax1.set_ylabel(x2_name)
        ax1.set_zlabel('Average Euclidean Distance from Global Minimum')
        plt.show()

    return (X, y)

def ga_data_points(o_algorithm, settings, o_function):
    x1_start = 0.1
    x1_step = 0.1
    x1_end = 1.0
    x2_start = 0.1
    x2_step = 0.1
    x2_end = 1.0
    # x1_start = 0.5
    # x1_step = 0.1
    # x1_end = 0.9
    # x2_start = 0.6
    # x2_step = 0.1
    # x2_end = 0.9
    x1_name = "selection_cutoff"
    x2_name = "mutation_rate"
    # x1_start = 0.5
    # x1_step = 0.5
    # x1_end = 1.0
    # x2_start = 0.1
    # x2_step = 0.1
    # x2_end = 0.4
    # x1_name = "max_mutation_amount"
    # x2_name = "mutation_rate"

    get_two_d_accuracy(o_algorithm, settings, o_function, \
                              x1_start, x1_step, x1_end, \
                              x2_start, x2_step, x2_end, \
                              x1_name, x2_name, \
                              population_size=20, num_tests_per_point=10, plot=True, \
                              save_histograms=False, response_surface=False \
                             )

def pso_data_points(o_algorithm, settings, o_function):
    x1_start = 0.0
    x1_step = 0.1
    x1_end = 0.4
    x2_start = 0.6
    x2_step = 0.1
    x2_end = 0.9
    x1_name = "cp"
    x2_name = "cg"

    return get_two_d_accuracy(o_algorithm, settings, o_function, \
                              x1_start, x1_step, x1_end, \
                              x2_start, x2_step, x2_end, \
                              x1_name, x2_name, \
                              population_size=50, num_tests_per_point=50, plot=True, \
                              save_histograms=False, response_surface=True \
                             )

if __name__ == "__main__":

    #num_particles = [50, 100, 500]# + range(1000, 10000, 1000)
    #tests = num_parts_vs_time(OptimizationAlgorithm, num_particles)

    #func_val_vs_iterations(OptimizationAlgorithm, num_particles)

    #pso_data_points(PSO, pso_settings.settings, rosenbrock_function.objective_function)
    (X, y) = ga_data_points(GA, ga_settings.settings, griewank_function.objective_function)
    write_xy_data(X, y, 'ga_rate_vs_cutoff_griewank'):
    sys.exit()

    Process( target = ga_data_points, \
            args = (GA, ga_settings.settings, rosenbrock_function.objective_function) \
          ).start()

    Process( target = pso_data_points, \
            args = (PSO, pso_settings.settings, rosenbrock_function.objective_function) \
          ).start()
<commit_msg>fixed saving data line<commit_after>import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import numpy as np
#import threading
#from threading import Thread
from multiprocessing import Process
#from statistics import median
import sys
sys.path.append('../genetic_algorithm')
sys.path.append('../particle_swarm_optimization')
sys.path.append('../functions')
sys.path.append('../utils')

from oa_utils import optimize_settings, write_xy_data
import regression_utils

from genetic_algorithm import GA
import ga_settings
from particle_swarm_optimization import PSO
import pso_settings
import ackley_function
import easom_function
import rosenbrock_function
import griewank_function

def gen_filename(x1_name, x2_name):
    return x1_name + ',' + x2_name + '.dat'

def num_parts_vs_time(o_algorithm, num_parts):
    tests = {}

    for test in num_parts:
        settings['population_size'] = test

        algorithm = o_algorithm(settings, objective_function)

        algorithm.start_timer()
        algorithm.run()
        algorithm.stop_timer()

        x = algorithm.get_best_x()

        print("%5d: %7.3f" % (test, algorithm.get_time()))

        tests[test] = {'time': algorithm.get_time(), 'accuracy': abs(x.get_fitness())}

    return tests

def func_val_vs_iterations(o_algorithm, num_parts):
    fig, ax = plt.subplots(nrows=1, ncols=1)

    for test in num_parts:
        f = []

        settings.settings['population_size'] = test

        algorithm = o_algorithm(settings, objective_function)

        while settings['num_iterations'] > algorithm.num_generations:
            f.append(algorithm.get_best_f())
            algorithm.do_loop()

        ax.plot(range(1,len(f)+1), f)

    plt.title('Number of particles vs iterations')
    plt.xlabel('Number of Iterations')
    plt.ylabel('Objective Function Value')
    plt.legend(num_parts)
    fig_name = 'func_value_vs_iterations'
    fig.savefig(fig_name + '.png')
    plt.close(fig)

# saves all test data to file
# x1_name: name of setting in settings file
# x2_name: name of setting in settings file
def get_two_d_accuracy(o_algorithm, o_settings, o_function, \
                           x1_start, x1_step, x1_end, \
                           x2_start, x2_step, x2_end, \
                           x1_name, x2_name, \
                           population_size=50, num_tests_per_point=10, plot=True, \
                           save_histograms=True, response_surface=True, debug=False):
    if response_surface:
        plot = True

    # turn off settings that slow us down
    o_settings = optimize_settings(o_settings)

    tests = {}
    hist_num_bins = 150

    o_settings['population_size'] = population_size
    num_tests_per = num_tests_per_point
    x1_srt = x1_start
    x1_e   = x1_end
    x1_stp = x1_step
    x2_srt = x2_start
    x2_e   = x2_end
    x2_stp = x2_step
    num_tests = int(( (int(100*x1_e)-int(100*x1_srt))/int(100*x1_stp) + 1 )* \
                    ( (int(100*x2_e)-int(100*x2_srt))/int(100*x2_stp) + 1 ))
    X = np.ones(shape=(num_tests,6))
    y = np.zeros(shape=(num_tests,1))

    n = 0

    if plot:
        fig = plt.figure()
        ax1 = fig.add_subplot(111, projection='3d')
        if o_function != griewank_function.objective_function:
            ax1.set_zlim(0, 1)

    for i in np.arange(x1_srt, x1_e+x1_stp, x1_stp):
        for j in np.arange(x2_srt, x2_e+x2_stp, x2_stp):
            # set settings for this test
            o_settings[x1_name] = i
            o_settings[x2_name] = j

            # initial variables
            values = []
            euclid_distance = []
            test_name = x1_name + '(' + str(i) + ')'+ ',' + x2_name + '(' + str(j) + ')'

            print("Running test %s" % test_name)

            # create histogram plot if true
            if save_histograms:
                hist_fig = plt.figure()
                hist_ax = hist_fig.add_subplot(111)

            # run optimization algorithm
            for k in range(0,num_tests_per):
                algorithm = o_algorithm(o_settings, o_function)
                algorithm.run()
                # save enf values
                values.append(algorithm.get_best_x().get_fval())
                # euclidean distance
                squares = 0
                for square in algorithm.get_best_x().pos:
                    if o_function == rosenbrock_function.objective_function:
                        squares += (square-1)**2
                    elif o_function == easom_function.objective_function:
                        squares += (square-np.pi)**2
                    else:
                        squares += square**2
                euclid_distance.append(np.sqrt(squares))

            # save histogram if true
            if save_histograms:
                hist_ax.hist(values, hist_num_bins, range=(0, 1.5), normed=True)
                hist_fig.savefig(test_name + '.png')
                plt.close(hist_fig)

            # find average and save data
            #avg = sum(values)/len(values)
            #avg = median(values)
            avg = sum(euclid_distance)/len(euclid_distance)
            #avg = median(euclid_distance)
            tests[test_name] = avg
            if plot:
                ax1.scatter(i,j,avg)

            X[n][1] = i
            X[n][2] = j
            X[n][3] = i*i
            X[n][4] = i*j
            X[n][5] = j*j
            y[n] = avg

            # increment test number
            n += 1

    fname = gen_filename(x1_name, x2_name)
    write_xy_data(X, y, fname)

    if debug:
        print("\n*** DATA ***")
        print("X")
        print(X)
        print("\ny")
        print(y)
        print("\ntests")
        print(tests)

    if response_surface:
        # get regression coefficients
        b = regression_utils.get_regression_coef(X, y)

        pltx = np.arange(x1_start, x1_end+x1_step, x1_step)
        plty = np.arange(x2_start, x2_end+x2_step, x2_step)
        pltX, pltY = np.meshgrid(pltx, plty)
        F = b[0] + b[1]*pltX + b[2]*pltY + b[3]*pltX*pltX + b[4]*pltX*pltY + b[5]*pltY*pltY
        ax1.plot_wireframe(pltX, pltY, F)

    if plot:
        print("\nPlotting ...")
        x1_name = x1_name[0].upper() + x1_name[1:]
        x2_name = x2_name[0].upper() + x2_name[1:]
        ax1.set_xlabel(x1_name)
        ax1.set_ylabel(x2_name)
        ax1.set_zlabel('Average Euclidean Distance from Global Minimum')
        plt.show()

    return (X, y)

def ga_data_points(o_algorithm, settings, o_function):
    x1_start = 0.1
    x1_step = 0.1
    x1_end = 1.0
    x2_start = 0.1
    x2_step = 0.1
    x2_end = 1.0
    # x1_start = 0.5
    # x1_step = 0.1
    # x1_end = 0.9
    # x2_start = 0.6
    # x2_step = 0.1
    # x2_end = 0.9
    x1_name = "selection_cutoff"
    x2_name = "mutation_rate"
    # x1_start = 0.5
    # x1_step = 0.5
    # x1_end = 1.0
    # x2_start = 0.1
    # x2_step = 0.1
    # x2_end = 0.4
    # x1_name = "max_mutation_amount"
    # x2_name = "mutation_rate"

    get_two_d_accuracy(o_algorithm, settings, o_function, \
                              x1_start, x1_step, x1_end, \
                              x2_start, x2_step, x2_end, \
                              x1_name, x2_name, \
                              population_size=20, num_tests_per_point=10, plot=True, \
                              save_histograms=False, response_surface=False \
                             )

def pso_data_points(o_algorithm, settings, o_function):
    x1_start = 0.0
    x1_step = 0.1
    x1_end = 0.4
    x2_start = 0.6
    x2_step = 0.1
    x2_end = 0.9
    x1_name = "cp"
    x2_name = "cg"

    return get_two_d_accuracy(o_algorithm, settings, o_function, \
                              x1_start, x1_step, x1_end, \
                              x2_start, x2_step, x2_end, \
                              x1_name, x2_name, \
                              population_size=50, num_tests_per_point=50, plot=True, \
                              save_histograms=False, response_surface=True \
                             )

if __name__ == "__main__":

    #num_particles = [50, 100, 500]# + range(1000, 10000, 1000)
    #tests = num_parts_vs_time(OptimizationAlgorithm, num_particles)

    #func_val_vs_iterations(OptimizationAlgorithm, num_particles)

    #pso_data_points(PSO, pso_settings.settings, rosenbrock_function.objective_function)
    (X, y) = ga_data_points(GA, ga_settings.settings, griewank_function.objective_function)
    write_xy_data(X, y, 'ga_rate_vs_cutoff_griewank')
    sys.exit()

    Process( target = ga_data_points, \
            args = (GA, ga_settings.settings, rosenbrock_function.objective_function) \
          ).start()

    Process( target = pso_data_points, \
            args = (PSO, pso_settings.settings, rosenbrock_function.objective_function) \
          ).start()
<|endoftext|>


<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-
# vim: ai ts=4 sts=4 et sw=4
#
# Copyright © 2015 Collabora Ltd.
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

"""
TODO
"""

import os
import sys

# PyPy support
# pylint: disable=interface-not-implemented
try:
    from xml.etree import cElementTree as ElementTree
except ImportError:
    from xml.etree import ElementTree

from dbusapi import ast

def _ignore_node(node):
    """
    Decide whether to ignore the given node when parsing.

    We definitely want to ignore:
     * {http://telepathy.freedesktop.org/wiki/DbusSpec#extensions-v0}docstring
     * {http://www.freedesktop.org/dbus/1.0/doc.dtd}doc
    """
    return node.tag[0] == '{'  # in a namespace

# pylint: disable=interface-not-implemented
class InterfaceParser(object):

    """
    Parse a D-Bus introspection XML file.

    This validates the file, but not exceedingly strictly. It is a pragmatic
    parser, designed for use by the InterfaceComparator rather than more
    general code. It ignores certain common extensions found in introspection
    XML files, such as documentation elements, but will fail on other
    unrecognised elements.
    """

    TP_DTD = 'http://telepathy.freedesktop.org/wiki/DbusSpec#extensions-v0'

    def __init__(self, filename):
        """
        Construct a new InterfaceParser.

        Args:
            filename: path to the XML introspection file to parse
        """
        self._filename = os.path.abspath(filename)
        self._output = []

    def _issue_output(self, message):
        """Append a message to the parser output."""
        self._output.append(message)

    def print_output(self):
        """Print all logged parser messages."""
        for message in self._output:
            sys.stderr.write('ERROR: %s\n' % message)

    def get_output(self):
        """Return a list of all logged parser messages."""
        return self._output

    def parse(self):
        """
        Parse the introspection XML file and build an AST.

        Returns:
            A non-empty dict of interfaces in the file, mapping each interface
            name to an ast.Interface instance.

            If parsing fails, None is returned.
        """
        self._output = []
        tree = ElementTree.parse(self._filename)
        out = self._parse_root(tree.getroot())

        # Squash output on error.
        if len(self._output) != 0:
            return None

        return out

    def _parse_root(self, root):
        """Parse the root node in the XML tree; return a dict of interfaces."""
        # Handle specifications wrapped in tp:spec.
        if root.tag == '{%s}spec' % self.TP_DTD:
            for node in root.getchildren():
                if node.tag == 'node':
                    root = node
                    break

        # Continue parsing as per the D-Bus introspection format
        interfaces = {}

        if root.tag != 'node':
            self._issue_output('Unrecognised root node ‘%s’.' % root.tag)
            return interfaces

        for node in root.getchildren():
            if node.tag == 'interface':
                interface = self._parse_interface(node)
                if interface is None:
                    continue

                if interface.name in interfaces:
                    self._issue_output('Duplicate interface definition '
                                       '‘%s’.' % interface.format_name())
                    continue

                interfaces[interface.name] = interface
            elif _ignore_node(node):
                pass
            else:
                self._issue_output('Unrecognised node ‘%s’ in root.' %
                                   node.tag)

        return interfaces

    # pylint: disable=too-many-branches
    def _parse_interface(self, interface_node):  # noqa
        """Parse an <interface> element; return an ast.Interface or None."""
        assert interface_node.tag == 'interface'

        if 'name' not in interface_node.attrib:
            self._issue_output('Missing required attribute ‘%s’ in '
                               'interface.' % 'name')
            return None

        name = interface_node.attrib['name']
        methods = {}
        signals = {}
        properties = {}
        annotations = {}

        for node in interface_node.getchildren():
            if node.tag == 'method':
                method = self._parse_method(node)
                if method is None:
                    continue

                if method.name in methods:
                    self._issue_output('Duplicate method definition ‘%s’.' %
                                       method.format_name())
                    continue

                methods[method.name] = method
            elif node.tag == 'signal':
                signal = self._parse_signal(node)
                if signal is None:
                    continue

                if signal.name in signals:
                    self._issue_output('Duplicate signal definition ‘%s’.' %
                                       signal.format_name())
                    continue

                signals[signal.name] = signal
            elif node.tag == 'property':
                prop = self._parse_property(node)
                if prop is None:
                    continue

                if prop.name in properties:
                    self._issue_output('Duplicate property definition ‘%s’.' %
                                       prop.format_name())
                    continue

                properties[prop.name] = prop
            elif node.tag == 'annotation':
                annotation = self._parse_annotation(node)
                if annotation is None:
                    continue

                annotations[annotation.name] = annotation
            elif _ignore_node(node):
                pass
            else:
                self._issue_output('Unrecognised node ‘%s’ in interface '
                                   '‘%s’.' % (node.tag, name))

        return ast.Interface(name, methods, properties, signals,
                             annotations)

    def _parse_method(self, method_node):
        """Parse a <method> element; return an ast.Method or None."""
        assert method_node.tag == 'method'

        if 'name' not in method_node.attrib:
            self._issue_output('Missing required attribute ‘%s’ in method.' %
                               'name')
            return None

        name = method_node.attrib['name']
        args = []
        annotations = {}

        for node in method_node.getchildren():
            if node.tag == 'arg':
                arg = self._parse_arg(node)
                if arg is None:
                    continue

                args.append(arg)
            elif node.tag == 'annotation':
                annotation = self._parse_annotation(node)
                if annotation is None:
                    continue

                annotations[annotation.name] = annotation
            elif _ignore_node(node):
                pass
            else:
                self._issue_output('Unrecognised node ‘%s’ in method ‘%s’.' %
                                   (node.tag, name))

        return ast.Method(name, args, annotations)

    def _parse_signal(self, signal_node):
        """Parse a <signal> element; return an ast.Signal or None."""
        assert signal_node.tag == 'signal'

        if 'name' not in signal_node.attrib:
            self._issue_output('Missing required attribute ‘%s’ in signal.' %
                               'name')
            return None

        name = signal_node.attrib['name']
        args = []
        annotations = {}

        for node in signal_node.getchildren():
            if node.tag == 'arg':
                arg = self._parse_arg(node)
                if arg is None:
                    continue

                args.append(arg)
            elif node.tag == 'annotation':
                annotation = self._parse_annotation(node)
                if annotation is None:
                    continue

                annotations[annotation.name] = annotation
            elif _ignore_node(node):
                pass
            else:
                self._issue_output('Unrecognised node ‘%s’ in signal ‘%s’.' %
                                   (node.tag, name))

        return ast.Signal(name, args, annotations)

    def _parse_property(self, property_node):
        """Parse a <property> element; return an ast.Property or None."""
        assert property_node.tag == 'property'

        if 'name' not in property_node.attrib:
            self._issue_output('Missing required attribute ‘%s’ in property.' %
                               'name')
            return None
        elif 'type' not in property_node.attrib:
            self._issue_output('Missing required attribute ‘%s’ in property.' %
                               'type')
            return None
        elif 'access' not in property_node.attrib:
            self._issue_output('Missing required attribute ‘%s’ in property.' %
                               'access')
            return None

        name = property_node.attrib['name']
        prop_type = property_node.attrib['type']
        access = property_node.attrib['access']
        annotations = {}

        for node in property_node.getchildren():
            if node.tag == 'annotation':
                annotation = self._parse_annotation(node)
                if annotation is None:
                    continue

                annotations[annotation.name] = annotation
            elif _ignore_node(node):
                pass
            else:
                self._issue_output('Unrecognised node ‘%s’ in property ‘%s’.' %
                                   (node.tag, name))

        return ast.Property(name, prop_type, access, annotations)

    def _parse_arg(self, arg_node):
        """Parse an <arg> element; return an ast.Argument or None."""
        assert arg_node.tag == 'arg'

        if 'type' not in arg_node.attrib:
            self._issue_output('Missing required attribute ‘%s’ in arg.' %
                               'type')
            return None

        name = arg_node.attrib.get('name', None)
        direction = arg_node.attrib.get('direction',
                                        ast.Argument.DIRECTION_IN)
        arg_type = arg_node.attrib['type']
        annotations = {}

        for node in arg_node.getchildren():
            if node.tag == 'annotation':
                annotation = self._parse_annotation(node)
                if annotation is None:
                    continue

                annotations[annotation.name] = annotation
            elif _ignore_node(node):
                pass
            else:
                self._issue_output('Unrecognised node ‘%s’ in arg ‘%s’.' %
                                   (node.tag, name))

        return ast.Argument(name, direction, arg_type, annotations)

    def _parse_annotation(self, annotation_node):
        """Parse an <annotation> element; return an ast.Annotation or None."""
        assert annotation_node.tag == 'annotation'

        if 'name' not in annotation_node.attrib:
            self._issue_output('Missing required attribute ‘%s’ in '
                               'annotation.' % 'name')
            return None
        if 'value' not in annotation_node.attrib:
            self._issue_output('Missing required attribute ‘%s’ in '
                               'annotation.' % 'value')
            return None

        name = annotation_node.attrib.get('name')
        value = annotation_node.attrib.get('value')

        for node in annotation_node.getchildren():
            if _ignore_node(node):
                pass
            else:
                self._issue_output('Unrecognised node ‘%s’ in annotation '
                                   '‘%s’.' % (node.tag, name))

        return ast.Annotation(name, value)
<commit_msg>dbusapi: Reformat an error message to include the filename<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-
# vim: ai ts=4 sts=4 et sw=4
#
# Copyright © 2015 Collabora Ltd.
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

"""
TODO
"""

import os
import sys

# PyPy support
# pylint: disable=interface-not-implemented
try:
    from xml.etree import cElementTree as ElementTree
except ImportError:
    from xml.etree import ElementTree

from dbusapi import ast

def _ignore_node(node):
    """
    Decide whether to ignore the given node when parsing.

    We definitely want to ignore:
     * {http://telepathy.freedesktop.org/wiki/DbusSpec#extensions-v0}docstring
     * {http://www.freedesktop.org/dbus/1.0/doc.dtd}doc
    """
    return node.tag[0] == '{'  # in a namespace

# pylint: disable=interface-not-implemented
class InterfaceParser(object):

    """
    Parse a D-Bus introspection XML file.

    This validates the file, but not exceedingly strictly. It is a pragmatic
    parser, designed for use by the InterfaceComparator rather than more
    general code. It ignores certain common extensions found in introspection
    XML files, such as documentation elements, but will fail on other
    unrecognised elements.
    """

    TP_DTD = 'http://telepathy.freedesktop.org/wiki/DbusSpec#extensions-v0'

    def __init__(self, filename):
        """
        Construct a new InterfaceParser.

        Args:
            filename: path to the XML introspection file to parse
        """
        self._filename = filename
        self._output = []

    def _issue_output(self, message):
        """Append a message to the parser output."""
        self._output.append(message)

    def print_output(self):
        """Print all logged parser messages."""
        for message in self._output:
            sys.stderr.write('%s: error: %s\n' % (self._filename, message))

    def get_output(self):
        """Return a list of all logged parser messages."""
        return self._output

    def parse(self):
        """
        Parse the introspection XML file and build an AST.

        Returns:
            A non-empty dict of interfaces in the file, mapping each interface
            name to an ast.Interface instance.

            If parsing fails, None is returned.
        """
        self._output = []
        tree = ElementTree.parse(os.path.abspath(self._filename))
        out = self._parse_root(tree.getroot())

        # Squash output on error.
        if len(self._output) != 0:
            return None

        return out

    def _parse_root(self, root):
        """Parse the root node in the XML tree; return a dict of interfaces."""
        # Handle specifications wrapped in tp:spec.
        if root.tag == '{%s}spec' % self.TP_DTD:
            for node in root.getchildren():
                if node.tag == 'node':
                    root = node
                    break

        # Continue parsing as per the D-Bus introspection format
        interfaces = {}

        if root.tag != 'node':
            self._issue_output('Unrecognised root node ‘%s’.' % root.tag)
            return interfaces

        for node in root.getchildren():
            if node.tag == 'interface':
                interface = self._parse_interface(node)
                if interface is None:
                    continue

                if interface.name in interfaces:
                    self._issue_output('Duplicate interface definition '
                                       '‘%s’.' % interface.format_name())
                    continue

                interfaces[interface.name] = interface
            elif _ignore_node(node):
                pass
            else:
                self._issue_output('Unrecognised node ‘%s’ in root.' %
                                   node.tag)

        return interfaces

    # pylint: disable=too-many-branches
    def _parse_interface(self, interface_node):  # noqa
        """Parse an <interface> element; return an ast.Interface or None."""
        assert interface_node.tag == 'interface'

        if 'name' not in interface_node.attrib:
            self._issue_output('Missing required attribute ‘%s’ in '
                               'interface.' % 'name')
            return None

        name = interface_node.attrib['name']
        methods = {}
        signals = {}
        properties = {}
        annotations = {}

        for node in interface_node.getchildren():
            if node.tag == 'method':
                method = self._parse_method(node)
                if method is None:
                    continue

                if method.name in methods:
                    self._issue_output('Duplicate method definition ‘%s’.' %
                                       method.format_name())
                    continue

                methods[method.name] = method
            elif node.tag == 'signal':
                signal = self._parse_signal(node)
                if signal is None:
                    continue

                if signal.name in signals:
                    self._issue_output('Duplicate signal definition ‘%s’.' %
                                       signal.format_name())
                    continue

                signals[signal.name] = signal
            elif node.tag == 'property':
                prop = self._parse_property(node)
                if prop is None:
                    continue

                if prop.name in properties:
                    self._issue_output('Duplicate property definition ‘%s’.' %
                                       prop.format_name())
                    continue

                properties[prop.name] = prop
            elif node.tag == 'annotation':
                annotation = self._parse_annotation(node)
                if annotation is None:
                    continue

                annotations[annotation.name] = annotation
            elif _ignore_node(node):
                pass
            else:
                self._issue_output('Unrecognised node ‘%s’ in interface '
                                   '‘%s’.' % (node.tag, name))

        return ast.Interface(name, methods, properties, signals,
                             annotations)

    def _parse_method(self, method_node):
        """Parse a <method> element; return an ast.Method or None."""
        assert method_node.tag == 'method'

        if 'name' not in method_node.attrib:
            self._issue_output('Missing required attribute ‘%s’ in method.' %
                               'name')
            return None

        name = method_node.attrib['name']
        args = []
        annotations = {}

        for node in method_node.getchildren():
            if node.tag == 'arg':
                arg = self._parse_arg(node)
                if arg is None:
                    continue

                args.append(arg)
            elif node.tag == 'annotation':
                annotation = self._parse_annotation(node)
                if annotation is None:
                    continue

                annotations[annotation.name] = annotation
            elif _ignore_node(node):
                pass
            else:
                self._issue_output('Unrecognised node ‘%s’ in method ‘%s’.' %
                                   (node.tag, name))

        return ast.Method(name, args, annotations)

    def _parse_signal(self, signal_node):
        """Parse a <signal> element; return an ast.Signal or None."""
        assert signal_node.tag == 'signal'

        if 'name' not in signal_node.attrib:
            self._issue_output('Missing required attribute ‘%s’ in signal.' %
                               'name')
            return None

        name = signal_node.attrib['name']
        args = []
        annotations = {}

        for node in signal_node.getchildren():
            if node.tag == 'arg':
                arg = self._parse_arg(node)
                if arg is None:
                    continue

                args.append(arg)
            elif node.tag == 'annotation':
                annotation = self._parse_annotation(node)
                if annotation is None:
                    continue

                annotations[annotation.name] = annotation
            elif _ignore_node(node):
                pass
            else:
                self._issue_output('Unrecognised node ‘%s’ in signal ‘%s’.' %
                                   (node.tag, name))

        return ast.Signal(name, args, annotations)

    def _parse_property(self, property_node):
        """Parse a <property> element; return an ast.Property or None."""
        assert property_node.tag == 'property'

        if 'name' not in property_node.attrib:
            self._issue_output('Missing required attribute ‘%s’ in property.' %
                               'name')
            return None
        elif 'type' not in property_node.attrib:
            self._issue_output('Missing required attribute ‘%s’ in property.' %
                               'type')
            return None
        elif 'access' not in property_node.attrib:
            self._issue_output('Missing required attribute ‘%s’ in property.' %
                               'access')
            return None

        name = property_node.attrib['name']
        prop_type = property_node.attrib['type']
        access = property_node.attrib['access']
        annotations = {}

        for node in property_node.getchildren():
            if node.tag == 'annotation':
                annotation = self._parse_annotation(node)
                if annotation is None:
                    continue

                annotations[annotation.name] = annotation
            elif _ignore_node(node):
                pass
            else:
                self._issue_output('Unrecognised node ‘%s’ in property ‘%s’.' %
                                   (node.tag, name))

        return ast.Property(name, prop_type, access, annotations)

    def _parse_arg(self, arg_node):
        """Parse an <arg> element; return an ast.Argument or None."""
        assert arg_node.tag == 'arg'

        if 'type' not in arg_node.attrib:
            self._issue_output('Missing required attribute ‘%s’ in arg.' %
                               'type')
            return None

        name = arg_node.attrib.get('name', None)
        direction = arg_node.attrib.get('direction',
                                        ast.Argument.DIRECTION_IN)
        arg_type = arg_node.attrib['type']
        annotations = {}

        for node in arg_node.getchildren():
            if node.tag == 'annotation':
                annotation = self._parse_annotation(node)
                if annotation is None:
                    continue

                annotations[annotation.name] = annotation
            elif _ignore_node(node):
                pass
            else:
                self._issue_output('Unrecognised node ‘%s’ in arg ‘%s’.' %
                                   (node.tag, name))

        return ast.Argument(name, direction, arg_type, annotations)

    def _parse_annotation(self, annotation_node):
        """Parse an <annotation> element; return an ast.Annotation or None."""
        assert annotation_node.tag == 'annotation'

        if 'name' not in annotation_node.attrib:
            self._issue_output('Missing required attribute ‘%s’ in '
                               'annotation.' % 'name')
            return None
        if 'value' not in annotation_node.attrib:
            self._issue_output('Missing required attribute ‘%s’ in '
                               'annotation.' % 'value')
            return None

        name = annotation_node.attrib.get('name')
        value = annotation_node.attrib.get('value')

        for node in annotation_node.getchildren():
            if _ignore_node(node):
                pass
            else:
                self._issue_output('Unrecognised node ‘%s’ in annotation '
                                   '‘%s’.' % (node.tag, name))

        return ast.Annotation(name, value)
<|endoftext|>


<commit_before><commit_msg>applications: opening columns links to opening details<commit_after><|endoftext|>


<commit_before>#!/usr/bin/env python
import sys
sys.path.append("..")
import pdb
from PyQt4 import QtCore, QtGui, uic
from core import version, dataMgr, projectMgr
from gui import ctrlRibbon, imgLabel
from PIL import Image, ImageQt

class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self)
        self.setGeometry(50,50,768,512)
        self.iconPath = '../../icons/32x32/'
        self.setWindowTitle("Ilastik rev: " + version.getIlastikVersion())

        self.createRibbons()
        self.initImageWindows()
        self.createImageWindows()

    def createRibbons(self):

        self.ribbonToolbar = self.addToolBar("ToolBarForRibbons")

        self.ribbon = ctrlRibbon.Ribbon(self.ribbonToolbar)
        for ribbon_group in ctrlRibbon.createRibbons():
            tabs = ribbon_group.makeTab()
            self.ribbon.addTab(tabs, ribbon_group.name)
        self.ribbonToolbar.addWidget(self.ribbon)

        # Wee, this is really ugly... anybody have better ideas for connecting
        # the signals. This way has no future and is just a workarround
        self.connect(self.ribbon.tabList[0][0].itemList[0], QtCore.SIGNAL('clicked()'), self.newProjectDlg)

    def newProjectDlg(self):
        self.projectDlg = ProjectDlg(self)

    def initImageWindows(self):
        self.labelDocks = []

    def createImageWindows(self):
        label_w = imgLabel.labelWidget(self, ["test.tif", "test2.tif"])

        dock = QtGui.QDockWidget("ImageDock_main", self)
        dock.setAllowedAreas(QtCore.Qt.BottomDockWidgetArea | QtCore.Qt.RightDockWidgetArea| QtCore.Qt.TopDockWidgetArea| QtCore.Qt.LeftDockWidgetArea)
        dock.setWidget(label_w)

        area = QtCore.Qt.BottomDockWidgetArea

        self.addDockWidget(area, dock)
        self.labelDocks.append(dock)

class ProjectDlg(QtGui.QDialog):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self)
        self.parent = parent
        self.fileList = []
        self.thumbList = []
        self.initDlg()

        # this enables   self.columnPos['File']:
        self.columnPos = {}
        for i in xrange( self.tableWidget.columnCount() ):
            self.columnPos[ str(self.tableWidget.horizontalHeaderItem(i).text()) ] = i

    def initDlg(self):
        uic.loadUi('dlgProject.ui', self)
        self.tableWidget.resizeRowsToContents()
        self.tableWidget.resizeColumnsToContents()
        self.tableWidget.setColumnWidth(0,350)
        self.tableWidget.setAlternatingRowColors(True)
        self.tableWidget.setShowGrid(False)

#        self.connect(self.addFile, QtCore.SIGNAL("clicked()"), self.addFile)
#        self.connect(self.confirmButtons, QtCore.SIGNAL("accepted()"), self.accept)
#        self.connect(self.confirmButtons, QtCore.SIGNAL("rejected()"), self.reject)
        self.connect(self.tableWidget, QtCore.SIGNAL("cellPressed(int, int)"), self.updateThumbnail)

        # Still have to beautify a bit with sth like that
#        self.filesTable.setHorizontalHeaderLabels(labels)
#        self.filesTable.horizontalHeader().setResizeMode(0, QtGui.QHeaderView.Stretch)
#        self.filesTable.verticalHeader().hide()
#        self.filesTable.setShowGrid(False)
        self.show()

    @QtCore.pyqtSignature("")
    def on_addFile_clicked(self):

        fileNames = QtGui.QFileDialog.getOpenFileNames(self, "Open Image", ".", "Image Files (*.png *.jpg *.bmp *.tif)")
        if fileNames:
            for file_name in fileNames:
                self.fileList.append(file_name)
                rowCount = self.tableWidget.rowCount()
                self.tableWidget.insertRow(0)

                theFlag = QtCore.Qt.ItemIsEnabled
                flagON = ~theFlag | theFlag
                flagOFF = ~theFlag

                # file name
                r = QtGui.QTableWidgetItem(file_name)
                self.tableWidget.setItem(0, self.columnPos['File'], r)

                # group
                r = QtGui.QComboBox()
                r.setEditable(True)
                self.tableWidget.setCellWidget(0, self.columnPos['Groups'], r)

                # labels
                r = QtGui.QTableWidgetItem()
                r.data(QtCore.Qt.CheckStateRole)
                r.setCheckState(QtCore.Qt.Checked)
                r.setFlags(r.flags() & flagOFF);
                self.tableWidget.setItem(0, self.columnPos['Labels'], r)

                # train
                r = QtGui.QTableWidgetItem()
                r.data(QtCore.Qt.CheckStateRole)
                r.setCheckState(QtCore.Qt.Checked)
                r.setFlags(r.flags() & flagON);
                self.tableWidget.setItem(0, self.columnPos['Train'], r)

                # test
                r = QtGui.QTableWidgetItem()
                r.data(QtCore.Qt.CheckStateRole)
                r.setCheckState(QtCore.Qt.Checked)
                r.setFlags(r.flags() & flagON);
                self.tableWidget.setItem(0, self.columnPos['Test'], r)

                self.initThumbnail(file_name)

    def initThumbnail(self, file_name):
        picture = Image.open(file_name.__str__())
        picture.thumbnail((68, 68), Image.ANTIALIAS)
        w,h = picture.size
        print "Thumbnail size = ", w, " width and ", h ," height"
        icon = QtGui.QPixmap.fromImage(ImageQt.ImageQt(picture))
        self.thumbList.append(icon)
        #In Windows I get strange seg faults from time to time, sometimes the image is not displayed properly, why?
        #self.thumbnailImage.setPixmap(self.thumbList[-1])

    def updateThumbnail(self, row=0, col=0):
        #In Windows I get strange seg faults from time to time, sometimes the image is not displayed properly, why?
        #self.thumbnailImage.clear()
        #self.thumbnailImage.setPixmap(self.thumbList[-row-1])
        pass

    @QtCore.pyqtSignature("")
    def on_confirmButtons_accepted(self):
        projectName = self.projectName
        labeler = self.labeler
        description = self.description
        self.parent.project = projectMgr.Project(projectName, labeler, description,[])

        rowCount = self.tableWidget.rowCount()
        dataItemList = []
        for k in range(0, rowCount):
            fileName = self.tableWidget.item(k, self.columnPos['File']).text()
            theDataItem = dataMgr.DataItemImage(fileName)
            dataItemList.append( theDataItem )

            groups = []
            for i in xrange( self.tableWidget.cellWidget(k, self.columnPos['Groups']).count() ):
                groups.append( str(self.tableWidget.cellWidget(k, self.columnPos['Groups']).itemText(i)) )
            theDataItem.groupMembership = groups

            theDataItem.hasLabels = self.tableWidget.item(k, self.columnPos['Labels']) == QtCore.Qt.Checked
            theDataItem.isTraining = self.tableWidget.item(k, self.columnPos['Train']) == QtCore.Qt.Checked
            theDataItem.isTesting = self.tableWidget.item(k, self.columnPos['Test']) == QtCore.Qt.Checked

            contained = false
            for pr in theDataItem.projects:
                if pr==self.parent.project:
                    contained = true
            if not contained:
                theDataItem.project.append(self.parent.project)

        self.parent.project.setDataList(dataItemList)
        self.close()

    @QtCore.pyqtSignature("")
    def on_confirmButtons_rejected(self):
        self.close()

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    mainwindow = MainWindow()
    mainwindow.show()
    sys.exit(app.exec_())<commit_msg>typos<commit_after>#!/usr/bin/env python
import sys
sys.path.append("..")
import pdb
from PyQt4 import QtCore, QtGui, uic
from core import version, dataMgr, projectMgr
from gui import ctrlRibbon, imgLabel
from PIL import Image, ImageQt

class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self)
        self.setGeometry(50,50,768,512)
        self.iconPath = '../../icons/32x32/'
        self.setWindowTitle("Ilastik rev: " + version.getIlastikVersion())

        self.createRibbons()
        self.initImageWindows()
        self.createImageWindows()

    def createRibbons(self):

        self.ribbonToolbar = self.addToolBar("ToolBarForRibbons")

        self.ribbon = ctrlRibbon.Ribbon(self.ribbonToolbar)
        for ribbon_group in ctrlRibbon.createRibbons():
            tabs = ribbon_group.makeTab()
            self.ribbon.addTab(tabs, ribbon_group.name)
        self.ribbonToolbar.addWidget(self.ribbon)

        # Wee, this is really ugly... anybody have better ideas for connecting
        # the signals. This way has no future and is just a workarround
        self.connect(self.ribbon.tabList[0][0].itemList[0], QtCore.SIGNAL('clicked()'), self.newProjectDlg)

    def newProjectDlg(self):
        self.projectDlg = ProjectDlg(self)

    def initImageWindows(self):
        self.labelDocks = []

    def createImageWindows(self):
        label_w = imgLabel.labelWidget(self, ["test.tif", "test2.tif"])

        dock = QtGui.QDockWidget("ImageDock_main", self)
        dock.setAllowedAreas(QtCore.Qt.BottomDockWidgetArea | QtCore.Qt.RightDockWidgetArea| QtCore.Qt.TopDockWidgetArea| QtCore.Qt.LeftDockWidgetArea)
        dock.setWidget(label_w)

        area = QtCore.Qt.BottomDockWidgetArea

        self.addDockWidget(area, dock)
        self.labelDocks.append(dock)

class ProjectDlg(QtGui.QDialog):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self)
        self.parent = parent
        self.fileList = []
        self.thumbList = []
        self.initDlg()

        # this enables   self.columnPos['File']:
        self.columnPos = {}
        for i in xrange( self.tableWidget.columnCount() ):
            self.columnPos[ str(self.tableWidget.horizontalHeaderItem(i).text()) ] = i

    def initDlg(self):
        uic.loadUi('dlgProject.ui', self)
        self.tableWidget.resizeRowsToContents()
        self.tableWidget.resizeColumnsToContents()
        self.tableWidget.setColumnWidth(0,350)
        self.tableWidget.setAlternatingRowColors(True)
        self.tableWidget.setShowGrid(False)

#        self.connect(self.addFile, QtCore.SIGNAL("clicked()"), self.addFile)
#        self.connect(self.confirmButtons, QtCore.SIGNAL("accepted()"), self.accept)
#        self.connect(self.confirmButtons, QtCore.SIGNAL("rejected()"), self.reject)
        self.connect(self.tableWidget, QtCore.SIGNAL("cellPressed(int, int)"), self.updateThumbnail)

        # Still have to beautify a bit with sth like that
#        self.filesTable.setHorizontalHeaderLabels(labels)
#        self.filesTable.horizontalHeader().setResizeMode(0, QtGui.QHeaderView.Stretch)
#        self.filesTable.verticalHeader().hide()
#        self.filesTable.setShowGrid(False)
        self.show()

    @QtCore.pyqtSignature("")
    def on_addFile_clicked(self):

        fileNames = QtGui.QFileDialog.getOpenFileNames(self, "Open Image", ".", "Image Files (*.png *.jpg *.bmp *.tif)")
        if fileNames:
            for file_name in fileNames:
                self.fileList.append(file_name)
                rowCount = self.tableWidget.rowCount()
                self.tableWidget.insertRow(0)

                theFlag = QtCore.Qt.ItemIsEnabled
                flagON = ~theFlag | theFlag
                flagOFF = ~theFlag

                # file name
                r = QtGui.QTableWidgetItem(file_name)
                self.tableWidget.setItem(0, self.columnPos['File'], r)

                # group
                r = QtGui.QComboBox()
                r.setEditable(True)
                self.tableWidget.setCellWidget(0, self.columnPos['Groups'], r)

                # labels
                r = QtGui.QTableWidgetItem()
                r.data(QtCore.Qt.CheckStateRole)
                r.setCheckState(QtCore.Qt.Checked)
                r.setFlags(r.flags() & flagOFF);
                self.tableWidget.setItem(0, self.columnPos['Labels'], r)

                # train
                r = QtGui.QTableWidgetItem()
                r.data(QtCore.Qt.CheckStateRole)
                r.setCheckState(QtCore.Qt.Checked)
                r.setFlags(r.flags() & flagON);
                self.tableWidget.setItem(0, self.columnPos['Train'], r)

                # test
                r = QtGui.QTableWidgetItem()
                r.data(QtCore.Qt.CheckStateRole)
                r.setCheckState(QtCore.Qt.Checked)
                r.setFlags(r.flags() & flagON);
                self.tableWidget.setItem(0, self.columnPos['Test'], r)

                self.initThumbnail(file_name)

    def initThumbnail(self, file_name):
        picture = Image.open(file_name.__str__())
        picture.thumbnail((68, 68), Image.ANTIALIAS)
        w,h = picture.size
        print "Thumbnail size = ", w, " width and ", h ," height"
        icon = QtGui.QPixmap.fromImage(ImageQt.ImageQt(picture))
        self.thumbList.append(icon)
        #In Windows I get strange seg faults from time to time, sometimes the image is not displayed properly, why?
        #self.thumbnailImage.setPixmap(self.thumbList[-1])

    def updateThumbnail(self, row=0, col=0):
        #In Windows I get strange seg faults from time to time, sometimes the image is not displayed properly, why?
        #self.thumbnailImage.clear()
        #self.thumbnailImage.setPixmap(self.thumbList[-row-1])
        pass

    @QtCore.pyqtSignature("")
    def on_confirmButtons_accepted(self):
        projectName = self.projectName
        labeler = self.labeler
        description = self.description
        self.parent.project = projectMgr.Project(projectName, labeler, description,[])

        rowCount = self.tableWidget.rowCount()
        dataItemList = []
        for k in range(0, rowCount):
            fileName = self.tableWidget.item(k, self.columnPos['File']).text()
            theDataItem = dataMgr.DataItemImage(fileName)
            dataItemList.append( theDataItem )

            groups = []
            for i in xrange( self.tableWidget.cellWidget(k, self.columnPos['Groups']).count() ):
                groups.append( str(self.tableWidget.cellWidget(k, self.columnPos['Groups']).itemText(i)) )
            theDataItem.groupMembership = groups

            theDataItem.hasLabels = self.tableWidget.item(k, self.columnPos['Labels']) == QtCore.Qt.Checked
            theDataItem.isTraining = self.tableWidget.item(k, self.columnPos['Train']) == QtCore.Qt.Checked
            theDataItem.isTesting = self.tableWidget.item(k, self.columnPos['Test']) == QtCore.Qt.Checked

            contained = False
            for pr in theDataItem.projects:
                if pr==self.parent.project:
                    contained = true
            if not contained:
                theDataItem.projects.append(self.parent.project)

        self.parent.project.setDataList(dataItemList)
        self.close()

    @QtCore.pyqtSignature("")
    def on_confirmButtons_rejected(self):
        self.close()

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    mainwindow = MainWindow()
    mainwindow.show()
    sys.exit(app.exec_())<|endoftext|>


<commit_before><commit_msg>try sorting queueing tasks by task_id, to get full jobs to complete faster<commit_after><|endoftext|>


<commit_before>#!/usr/bin/env python
#
# Electrum - lightweight Bitcoin client
# Copyright (C) 2012 thomasv@gitorious
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import sys
import time
import datetime
import re
import threading
import os.path, json, ast, traceback
import shutil
import signal

try:
    import PyQt4
except Exception:
    sys.exit("Error: Could not import PyQt4 on Linux systems, you may try 'sudo apt-get install python-qt4'")

from PyQt4.QtGui import *
from PyQt4.QtCore import *
import PyQt4.QtCore as QtCore

from electrum.i18n import _, set_language
from electrum.util import print_error, print_msg
from electrum.plugins import run_hook, always_hook
from electrum import WalletStorage, Wallet
from electrum.bitcoin import MIN_RELAY_TX_FEE

try:
    import icons_rc
except Exception:
    sys.exit("Error: Could not import icons_rc.py, please generate it with: 'pyrcc4 icons.qrc -o gui/qt/icons_rc.py'")

from util import *
from main_window import ElectrumWindow

class OpenFileEventFilter(QObject):
    def __init__(self, windows):
        self.windows = windows
        super(OpenFileEventFilter, self).__init__()

    def eventFilter(self, obj, event):
        if event.type() == QtCore.QEvent.FileOpen:
            if len(self.windows) >= 1:
                self.windows[0].pay_to_URI(event.url().toEncoded())
                return True
        return False

class ElectrumGui:

    def __init__(self, config, network):
        set_language(config.get('language'))
        self.network = network
        self.config = config
        self.windows = []
        self.efilter = OpenFileEventFilter(self.windows)
        self.app = QApplication(sys.argv)
        self.app.installEventFilter(self.efilter)
        self.timer = Timer()

        # init tray
        self.dark_icon = self.config.get("dark_icon", False)
        self.tray = QSystemTrayIcon(self.tray_icon(), None)
        self.tray.setToolTip('Electrum')
        self.tray.activated.connect(self.tray_activated)
        self.build_tray_menu()
        self.tray.show()
        self.app.connect(self.app, QtCore.SIGNAL('new_window'), self.start_new_window)

    def build_tray_menu(self):
        # Avoid immediate GC of old menu when window closed via its action
        self.old_menu = self.tray.contextMenu()
        m = QMenu()
        for window in self.windows:
            submenu = m.addMenu(window.wallet.basename())
            submenu.addAction(_("Show/Hide"), window.show_or_hide)
            submenu.addAction(_("Close"), window.close)
        m.addAction(_("Dark/Light"), self.toggle_tray_icon)
        m.addSeparator()
        m.addAction(_("Exit Electrum"), self.close)
        self.tray.setContextMenu(m)

    def tray_icon(self):
        if self.dark_icon:
            return QIcon(':icons/electrum_dark_icon.png')
        else:
            return QIcon(':icons/electrum_light_icon.png')

    def toggle_tray_icon(self):
        self.dark_icon = not self.dark_icon
        self.config.set_key("dark_icon", self.dark_icon, True)
        self.tray.setIcon(self.tray_icon())

    def tray_activated(self, reason):
        if reason == QSystemTrayIcon.DoubleClick:
            if all([w.is_hidden() for w in self.windows]):
                for w in self.windows:
                    w.bring_to_top()
            else:
                for w in self.windows:
                    w.hide()

    def close(self):
        for window in self.windows:
            window.close()

    def new_window(self, config):
        self.app.emit(SIGNAL('new_window'), config)

    def load_wallet_file(self, path):
        self.app.emit(SIGNAL('new_window'), self.config, path)

    def start_new_window(self, config, path=None):
        if path is None:
            path = config.get_wallet_path()
        for w in self.windows:
            if w.config.get_wallet_path() == path:
                w.bring_to_top()
                break
        else:
            w = ElectrumWindow(config, self.network, self)
            w.connect_slots(self.timer)
            w.load_wallet_file(path)
            w.show()
            self.windows.append(w)
            self.build_tray_menu()

        url = config.get('url')
        if url:
            w.pay_to_URI(url)
        return w

    def close_window(self, window):
        self.windows.remove(window)
        self.build_tray_menu()

    def main(self):
        self.timer.start()

        last_wallet = self.config.get('gui_last_wallet')
        if last_wallet is not None and self.config.get('wallet_path') is None:
            if os.path.exists(last_wallet):
                self.config.cmdline_options['default_wallet_path'] = last_wallet

        # main window
        self.current_window = self.main_window = self.start_new_window(self.config)

        # plugins interact with main window
        run_hook('init_qt', self)

        # initial configuration
        if self.config.get('hide_gui') is True and self.tray.isVisible():
            self.main_window.hide()

        signal.signal(signal.SIGINT, lambda *args: self.app.quit())

        # main loop
        self.app.exec_()

        # clipboard persistence. see http://www.mail-archive.com/pyqt@riverbankcomputing.com/msg17328.html
        event = QtCore.QEvent(QtCore.QEvent.Clipboard)
        self.app.sendEvent(self.app.clipboard(), event)

        if self.tray:
            self.tray.hide()
<commit_msg>Clean up imports, 2nd attempt<commit_after>#!/usr/bin/env python
#
# Electrum - lightweight Bitcoin client
# Copyright (C) 2012 thomasv@gitorious
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import sys
import os.path
import signal

try:
    import PyQt4
except Exception:
    sys.exit("Error: Could not import PyQt4 on Linux systems, you may try 'sudo apt-get install python-qt4'")

from PyQt4.QtGui import *
from PyQt4.QtCore import *
import PyQt4.QtCore as QtCore

from electrum.i18n import _, set_language
from electrum.plugins import run_hook

try:
    import icons_rc
except Exception:
    sys.exit("Error: Could not import icons_rc.py, please generate it with: 'pyrcc4 icons.qrc -o gui/qt/icons_rc.py'")

from util import *   # * needed for plugins
from main_window import ElectrumWindow

class OpenFileEventFilter(QObject):
    def __init__(self, windows):
        self.windows = windows
        super(OpenFileEventFilter, self).__init__()

    def eventFilter(self, obj, event):
        if event.type() == QtCore.QEvent.FileOpen:
            if len(self.windows) >= 1:
                self.windows[0].pay_to_URI(event.url().toEncoded())
                return True
        return False

class ElectrumGui:

    def __init__(self, config, network):
        set_language(config.get('language'))
        self.network = network
        self.config = config
        self.windows = []
        self.efilter = OpenFileEventFilter(self.windows)
        self.app = QApplication(sys.argv)
        self.app.installEventFilter(self.efilter)
        self.timer = Timer()

        # init tray
        self.dark_icon = self.config.get("dark_icon", False)
        self.tray = QSystemTrayIcon(self.tray_icon(), None)
        self.tray.setToolTip('Electrum')
        self.tray.activated.connect(self.tray_activated)
        self.build_tray_menu()
        self.tray.show()
        self.app.connect(self.app, QtCore.SIGNAL('new_window'), self.start_new_window)

    def build_tray_menu(self):
        # Avoid immediate GC of old menu when window closed via its action
        self.old_menu = self.tray.contextMenu()
        m = QMenu()
        for window in self.windows:
            submenu = m.addMenu(window.wallet.basename())
            submenu.addAction(_("Show/Hide"), window.show_or_hide)
            submenu.addAction(_("Close"), window.close)
        m.addAction(_("Dark/Light"), self.toggle_tray_icon)
        m.addSeparator()
        m.addAction(_("Exit Electrum"), self.close)
        self.tray.setContextMenu(m)

    def tray_icon(self):
        if self.dark_icon:
            return QIcon(':icons/electrum_dark_icon.png')
        else:
            return QIcon(':icons/electrum_light_icon.png')

    def toggle_tray_icon(self):
        self.dark_icon = not self.dark_icon
        self.config.set_key("dark_icon", self.dark_icon, True)
        self.tray.setIcon(self.tray_icon())

    def tray_activated(self, reason):
        if reason == QSystemTrayIcon.DoubleClick:
            if all([w.is_hidden() for w in self.windows]):
                for w in self.windows:
                    w.bring_to_top()
            else:
                for w in self.windows:
                    w.hide()

    def close(self):
        for window in self.windows:
            window.close()

    def new_window(self, config):
        self.app.emit(SIGNAL('new_window'), config)

    def load_wallet_file(self, path):
        self.app.emit(SIGNAL('new_window'), self.config, path)

    def start_new_window(self, config, path=None):
        if path is None:
            path = config.get_wallet_path()
        for w in self.windows:
            if w.config.get_wallet_path() == path:
                w.bring_to_top()
                break
        else:
            w = ElectrumWindow(config, self.network, self)
            w.connect_slots(self.timer)
            w.load_wallet_file(path)
            w.show()
            self.windows.append(w)
            self.build_tray_menu()

        url = config.get('url')
        if url:
            w.pay_to_URI(url)
        return w

    def close_window(self, window):
        self.windows.remove(window)
        self.build_tray_menu()

    def main(self):
        self.timer.start()

        last_wallet = self.config.get('gui_last_wallet')
        if last_wallet is not None and self.config.get('wallet_path') is None:
            if os.path.exists(last_wallet):
                self.config.cmdline_options['default_wallet_path'] = last_wallet

        # main window
        self.current_window = self.main_window = self.start_new_window(self.config)

        # plugins interact with main window
        run_hook('init_qt', self)

        # initial configuration
        if self.config.get('hide_gui') is True and self.tray.isVisible():
            self.main_window.hide()

        signal.signal(signal.SIGINT, lambda *args: self.app.quit())

        # main loop
        self.app.exec_()

        # clipboard persistence. see http://www.mail-archive.com/pyqt@riverbankcomputing.com/msg17328.html
        event = QtCore.QEvent(QtCore.QEvent.Clipboard)
        self.app.sendEvent(self.app.clipboard(), event)

        if self.tray:
            self.tray.hide()
<|endoftext|>


<commit_before>import compileall
import os
import shutil
import sysconfig
import textwrap
import venv as _venv
from pathlib import Path
from typing import TYPE_CHECKING, Optional, Union

import virtualenv as _virtualenv

if TYPE_CHECKING:
    # Literal was introduced in Python 3.8.
    from typing import Literal

    VirtualEnvironmentType = Literal["virtualenv", "venv"]
else:
    VirtualEnvironmentType = str

class VirtualEnvironment:
    """
    An abstraction around virtual environments, currently it only uses
    virtualenv but in the future it could use pyvenv.
    """

    def __init__(
        self,
        location: Path,
        template: Optional["VirtualEnvironment"] = None,
        venv_type: Optional[VirtualEnvironmentType] = None,
    ):
        self.location = location
        assert template is None or venv_type is None
        self._venv_type: VirtualEnvironmentType
        if template is not None:
            self._venv_type = template._venv_type
        elif venv_type is not None:
            self._venv_type = venv_type
        else:
            self._venv_type = "virtualenv"
        self._user_site_packages = False
        self._template = template
        self._sitecustomize: Optional[str] = None
        self._update_paths()
        self._create()

    def _update_paths(self) -> None:
        bases = {
            "installed_base": self.location,
            "installed_platbase": self.location,
            "base": self.location,
            "platbase": self.location,
        }
        paths = sysconfig.get_paths(vars=bases)
        self.bin = Path(paths["scripts"])
        self.site = Path(paths["purelib"])
        self.lib = Path(paths["stdlib"])

    def __repr__(self) -> str:
        return f"<VirtualEnvironment {self.location}>"

    def _create(self, clear: bool = False) -> None:
        if clear:
            shutil.rmtree(self.location)
        if self._template:
            # Clone virtual environment from template.
            shutil.copytree(self._template.location, self.location, symlinks=True)
            self._sitecustomize = self._template.sitecustomize
            self._user_site_packages = self._template.user_site_packages
        else:
            # Create a new virtual environment.
            if self._venv_type == "virtualenv":
                _virtualenv.cli_run(
                    [
                        "--no-pip",
                        "--no-wheel",
                        "--no-setuptools",
                        os.fspath(self.location),
                    ],
                )
            elif self._venv_type == "venv":
                builder = _venv.EnvBuilder()
                context = builder.ensure_directories(self.location)
                builder.create_configuration(context)
                builder.setup_python(context)
                self.site.mkdir(parents=True, exist_ok=True)
            self.sitecustomize = self._sitecustomize
            self.user_site_packages = self._user_site_packages

    def _customize_site(self) -> None:
        # Enable user site (before system).
        contents = textwrap.dedent(
            """
            import os, site, sys
            if not os.environ.get('PYTHONNOUSERSITE', False):
                site.ENABLE_USER_SITE = True
                # First, drop system-sites related paths.
                original_sys_path = sys.path[:]
                known_paths = set()
                for path in site.getsitepackages():
                    site.addsitedir(path, known_paths=known_paths)
                system_paths = sys.path[len(original_sys_path):]
                for path in system_paths:
                    if path in original_sys_path:
                        original_sys_path.remove(path)
                sys.path = original_sys_path
                # Second, add user-site.
                site.addsitedir(site.getusersitepackages())
                # Third, add back system-sites related paths.
                for path in site.getsitepackages():
                    site.addsitedir(path)
            """
        ).strip()
        if self._sitecustomize is not None:
            contents += "\n" + self._sitecustomize
        sitecustomize = self.site / "sitecustomize.py"
        sitecustomize.write_text(contents)
        # Make sure bytecode is up-to-date too.
        assert compileall.compile_file(str(sitecustomize), quiet=1, force=True)

    def clear(self) -> None:
        self._create(clear=True)

    def move(self, location: Union[Path, str]) -> None:
        shutil.move(os.fspath(self.location), location)
        self.location = Path(location)
        self._update_paths()

    @property
    def sitecustomize(self) -> Optional[str]:
        return self._sitecustomize

    @sitecustomize.setter
    def sitecustomize(self, value: str) -> None:
        self._sitecustomize = value
        self._customize_site()

    @property
    def user_site_packages(self) -> bool:
        return self._user_site_packages

    @user_site_packages.setter
    def user_site_packages(self, value: bool) -> None:
        self._customize_site()
        pyvenv_cfg = self.location.joinpath("pyvenv.cfg")
        modified_lines = []
        for line in pyvenv_cfg.read_text().splitlines():
            k, v = line.split("=", 1)
            if k.strip() == "include-system-site-packages":
                line = f"include-system-site-packages = {str(bool(value)).lower()}"
            modified_lines.append(line)
        pyvenv_cfg.write_text("\n".join(modified_lines))
<commit_msg>Don't enable global site when enabling user site<commit_after>import compileall
import os
import shutil
import sysconfig
import textwrap
import venv as _venv
from pathlib import Path
from typing import TYPE_CHECKING, Optional, Union

import virtualenv as _virtualenv

if TYPE_CHECKING:
    # Literal was introduced in Python 3.8.
    from typing import Literal

    VirtualEnvironmentType = Literal["virtualenv", "venv"]
else:
    VirtualEnvironmentType = str

class VirtualEnvironment:
    """
    An abstraction around virtual environments, currently it only uses
    virtualenv but in the future it could use pyvenv.
    """

    def __init__(
        self,
        location: Path,
        template: Optional["VirtualEnvironment"] = None,
        venv_type: Optional[VirtualEnvironmentType] = None,
    ):
        self.location = location
        assert template is None or venv_type is None
        self._venv_type: VirtualEnvironmentType
        if template is not None:
            self._venv_type = template._venv_type
        elif venv_type is not None:
            self._venv_type = venv_type
        else:
            self._venv_type = "virtualenv"
        self._user_site_packages = False
        self._template = template
        self._sitecustomize: Optional[str] = None
        self._update_paths()
        self._create()

    def _update_paths(self) -> None:
        bases = {
            "installed_base": self.location,
            "installed_platbase": self.location,
            "base": self.location,
            "platbase": self.location,
        }
        paths = sysconfig.get_paths(vars=bases)
        self.bin = Path(paths["scripts"])
        self.site = Path(paths["purelib"])
        self.lib = Path(paths["stdlib"])

    def __repr__(self) -> str:
        return f"<VirtualEnvironment {self.location}>"

    def _create(self, clear: bool = False) -> None:
        if clear:
            shutil.rmtree(self.location)
        if self._template:
            # Clone virtual environment from template.
            shutil.copytree(self._template.location, self.location, symlinks=True)
            self._sitecustomize = self._template.sitecustomize
            self._user_site_packages = self._template.user_site_packages
        else:
            # Create a new virtual environment.
            if self._venv_type == "virtualenv":
                _virtualenv.cli_run(
                    [
                        "--no-pip",
                        "--no-wheel",
                        "--no-setuptools",
                        os.fspath(self.location),
                    ],
                )
            elif self._venv_type == "venv":
                builder = _venv.EnvBuilder()
                context = builder.ensure_directories(self.location)
                builder.create_configuration(context)
                builder.setup_python(context)
                self.site.mkdir(parents=True, exist_ok=True)
            self.sitecustomize = self._sitecustomize
            self.user_site_packages = self._user_site_packages

    def _customize_site(self) -> None:
        # Enable user site (before system).
        contents = textwrap.dedent(
            f"""
            import os, site, sys
            if not os.environ.get('PYTHONNOUSERSITE', False):
                site.ENABLE_USER_SITE = {self._user_site_packages}
                # First, drop system-sites related paths.
                original_sys_path = sys.path[:]
                known_paths = set()
                for path in site.getsitepackages():
                    site.addsitedir(path, known_paths=known_paths)
                system_paths = sys.path[len(original_sys_path):]
                for path in system_paths:
                    if path in original_sys_path:
                        original_sys_path.remove(path)
                sys.path = original_sys_path
                # Second, add user-site.
                if {self._user_site_packages}:
                    site.addsitedir(site.getusersitepackages())
                # Third, add back system-sites related paths.
                for path in site.getsitepackages():
                    site.addsitedir(path)
            """
        ).strip()
        if self._sitecustomize is not None:
            contents += "\n" + self._sitecustomize
        sitecustomize = self.site / "sitecustomize.py"
        sitecustomize.write_text(contents)
        # Make sure bytecode is up-to-date too.
        assert compileall.compile_file(str(sitecustomize), quiet=1, force=True)

    def clear(self) -> None:
        self._create(clear=True)

    def move(self, location: Union[Path, str]) -> None:
        shutil.move(os.fspath(self.location), location)
        self.location = Path(location)
        self._update_paths()

    @property
    def sitecustomize(self) -> Optional[str]:
        return self._sitecustomize

    @sitecustomize.setter
    def sitecustomize(self, value: str) -> None:
        self._sitecustomize = value
        self._customize_site()

    @property
    def user_site_packages(self) -> bool:
        return self._user_site_packages

    @user_site_packages.setter
    def user_site_packages(self, value: bool) -> None:
        self._user_site_packages = value
        self._customize_site()
<|endoftext|>