<commit_before>#------------------------------------------------------------------------------
# ui.py - CLI commands module
#
# November 2015, Zoe Kelly
#------------------------------------------------------------------------------

import cmd, sys, io, pydoc

from . import database, query

class _BaseCmd(cmd.Cmd):
    """Base class for command submodes."""
    def __init__(self, paging_on):
        super().__init__()
        self.paging_on = paging_on
        self.out = None
        if paging_on == True:
            self.turn_paging_on()
        else:
            self.turn_paging_off()

    def turn_paging_on(self):
        self.paging_on = True

    def turn_paging_off(self):
        self.paging_on = False
        sys.stdout = sys.__stdout__

    def precmd(self, line):
        self.start_pager()
        return (line)

    def start_pager(self):
        if self.paging_on:
            self.out = io.StringIO()
            sys.stdout = self.out

    def postcmd(self, stop, line):
        self.flush_pager()
        return (stop)

    def flush_pager(self):
       # Reset
        sys.stdout = sys.__stdout__

        if self.paging_on:
            # Page output of this command
            if self.out is not None:
                pydoc.pager(self.out.getvalue())
        self.out = None

    def do_paging_off(self, arg):
        """"Turn paging off"""
        self.turn_paging_off()

    def do_paging_on(self, arg):
        """Turn paging on"""
        self.turn_paging_on()

    def do_EOF(self, arg):
        """Handle EOF (AKA ctrl-d)."""
        return self.do_exit(None)

    def do_exit(self, arg):
        """Exit this submode."""
        return True

class UI(_BaseCmd):
    def __init__(self, db, *, paging_on=False):
        super().__init__(paging_on)
        self.file = None
        self.intro = "Welcome to JamJar.  Type help or ? to list commands.\n"
        self.prompt = "(jamjar) "
        self.database = db

    def do_targets(self, target_string):
        """Get information about targets matching a regex."""
        target_list = list(self.database.find_targets(target_string))
        if len(target_list) == 0:
            print("No targets found")
        elif len(target_list) == 1:
            TargetSubmode(target=target_list[0], paging_on=self.paging_on).cmdloop()
        else:
            for idx, target in enumerate(target_list):
                print("({}) {}".format(idx , target))
            self.flush_pager()
            while True:
                choice = input("Choose target (range 0:{}): ".format(
                                                        len(target_list) - 1))
                # Exit target selection on empty input.
                if not choice:
                    return
                try:
                    target_index = int(choice)
                    check = target_list[target_index]
                except (ValueError, IndexError):
                    pass
                else:
                    break

            TargetSubmode(target=target_list[target_index], paging_on=self.paging_on).cmdloop()

    def do_rebuilt_targets(self, target_string):
        """Get information about targets that were rebuilt matching a regex."""
        target_list = list(self.database.find_rebuilt_targets(target_string))
        if len(target_list) == 0:
            print("No targets found")
        elif len(target_list) == 1:
            TargetSubmode(target=target_list[0], paging_on=self.paging_on).cmdloop()
        else:
            for idx, target in enumerate(target_list):
                print("({}) {}".format(idx , target))
            self.flush_pager()
            while True:
                choice = input("Choose target (range 0:{}): ".format(
                                                        len(target_list) - 1))
                # Exit target selection on empty input.
                if not choice:
                    return
                try:
                    target_index = int(choice)
                    check = target_list[target_index]
                except (ValueError, IndexError):
                    pass
                else:
                    break
            TargetSubmode(target=target_list[target_index],paging_on=self.paging_on).cmdloop()

class TargetSubmode(_BaseCmd):
    def __init__(self, target, *, paging_on):
        super().__init__(paging_on)
        self.target = target
        self.prompt = "({}) ".format(self.target.brief_name())
        file = None

    def do_deps(self, arg):
        """
        Show all direct dependencies, including those arising from includes.
        """
        self._print_targets(query.deps(self.target))

    def do_deps_rebuilt(self, arg):
        """Show direct dependencies that have been rebuilt."""
        self._print_targets(query.deps_rebuilt(self.target))

    def do_dep_chains(self, arg):
        """Show all chains of dependencies below this target."""
        # Yuck.
        kwargs = self._arg_to_kwargs(arg)
        if "max_depth" in kwargs:
            kwargs["max_depth"] = int(kwargs["max_depth"])
        for chain in query.dep_chains(self.target, **kwargs):
            self._print_chain(chain)

    def do_dep_chains_rebuilt(self, arg):
        """Show all chains of dependencies below this target."""
        for chain in query.dep_chains_rebuilt(self.target):
            self._print_chain(chain)

    def do_rebuild_chains(self, arg):
        """Show Jam's view on why this target was rebuilt."""
        for chain in query.rebuild_chains(self.target):
            self._print_chain(chain)

    def do_show(self, arg):
        """Dump all available meta-data for this target."""
        print("name:", self.target.name)
        print("depends on:")
        self._print_targets(self.target.deps)
        print("depended on by:")
        self._print_targets(self.target.deps_rev)
        print("includes:")
        self._print_targets(self.target.incs)
        print("included by:")
        self._print_targets(self.target.incs_rev)
        print("timestamp:", self.target.timestamp)
        print("binding:", self.target.binding)
        print("rebuilt:", self.target.rebuilt)
        if self.target.rebuilt:
            print("    rebuilt reason:", self.target.rebuild_info.reason)
            if self.target.rebuild_info.dep is not None:
                print("    dependency:", self.target.rebuild_info.dep.name)

    def _arg_to_kwargs(self, arg):
        """Parse an input argument consisting of param=value pairs."""
        kwargs = {}
        args = arg.split()
        for arg in args:
            key, value = arg.split("=")
            kwargs[key] = value
        return kwargs

    def _print_chain(self, chain):
        """Print a sequence of targets forming a dependency chain."""
        print(" -> ".join(target.name for target in chain))

    def _print_targets(self, targets):
        """Print a sequence of targets."""
        for target in targets:
            print("    {}".format(target.name))

<commit_msg>Better handling of EOF when selecting targets<commit_after>#------------------------------------------------------------------------------
# ui.py - CLI commands module
#
# November 2015, Zoe Kelly
#------------------------------------------------------------------------------

import cmd, sys, io, pydoc

from . import database, query

class _BaseCmd(cmd.Cmd):
    """Base class for command submodes."""
    def __init__(self, paging_on):
        super().__init__()
        self.paging_on = paging_on
        self.out = None
        if paging_on == True:
            self.turn_paging_on()
        else:
            self.turn_paging_off()

    def turn_paging_on(self):
        self.paging_on = True

    def turn_paging_off(self):
        self.paging_on = False
        sys.stdout = sys.__stdout__

    def precmd(self, line):
        self.start_pager()
        return (line)

    def start_pager(self):
        if self.paging_on:
            self.out = io.StringIO()
            sys.stdout = self.out

    def postcmd(self, stop, line):
        self.flush_pager()
        return (stop)

    def flush_pager(self):
       # Reset
        sys.stdout = sys.__stdout__

        if self.paging_on:
            # Page output of this command
            if self.out is not None:
                pydoc.pager(self.out.getvalue())
        self.out = None

    def do_paging_off(self, arg):
        """"Turn paging off"""
        self.turn_paging_off()

    def do_paging_on(self, arg):
        """Turn paging on"""
        self.turn_paging_on()

    def do_EOF(self, arg):
        """Handle EOF (AKA ctrl-d)."""
        print("")
        return self.do_exit(None)

    def do_exit(self, arg):
        """Exit this submode."""
        return True

class UI(_BaseCmd):
    def __init__(self, db, *, paging_on=False):
        super().__init__(paging_on)
        self.file = None
        self.intro = "Welcome to JamJar.  Type help or ? to list commands.\n"
        self.prompt = "(jamjar) "
        self.database = db

    def do_targets(self, target_string):
        """Get information about targets matching a regex."""
        target_list = list(self.database.find_targets(target_string))
        if len(target_list) == 0:
            print("No targets found")
        elif len(target_list) == 1:
            TargetSubmode(target=target_list[0], paging_on=self.paging_on).cmdloop()
        else:
            for idx, target in enumerate(target_list):
                print("({}) {}".format(idx , target))
            self.flush_pager()
            target_index = self._target_selection(target_list)
            if target is not None:
                TargetSubmode(target=target,
                              paging_on=self.paging_on).cmdloop()

    def do_rebuilt_targets(self, target_string):
        """Get information about targets that were rebuilt matching a regex."""
        target_list = list(self.database.find_rebuilt_targets(target_string))
        if len(target_list) == 0:
            print("No targets found")
        elif len(target_list) == 1:
            TargetSubmode(target=target_list[0], paging_on=self.paging_on).cmdloop()
        else:
            for idx, target in enumerate(target_list):
                print("({}) {}".format(idx , target))
            self.flush_pager()
            target = self._target_selection(target_list)
            if target is not None:
                TargetSubmode(target=target,
                              paging_on=self.paging_on).cmdloop()

    def _target_selection(self, targets):
        target = None
        while True:
            try:
                choice = input("Choose target (range 0:{}): ".format(
                    len(targets) - 1))
            except EOFError:
                print("")
                break
            # Exit target selection on empty input.
            if not choice:
                break
            try:
                target_index = int(choice)
                target = targets[target_index]
            except (ValueError, IndexError):
                pass
            else:
                break
        return target

class TargetSubmode(_BaseCmd):
    def __init__(self, target, *, paging_on):
        super().__init__(paging_on)
        self.target = target
        self.prompt = "({}) ".format(self.target.brief_name())
        file = None

    def do_deps(self, arg):
        """
        Show all direct dependencies, including those arising from includes.
        """
        self._print_targets(query.deps(self.target))

    def do_deps_rebuilt(self, arg):
        """Show direct dependencies that have been rebuilt."""
        self._print_targets(query.deps_rebuilt(self.target))

    def do_dep_chains(self, arg):
        """Show all chains of dependencies below this target."""
        # Yuck.
        kwargs = self._arg_to_kwargs(arg)
        if "max_depth" in kwargs:
            kwargs["max_depth"] = int(kwargs["max_depth"])
        for chain in query.dep_chains(self.target, **kwargs):
            self._print_chain(chain)

    def do_dep_chains_rebuilt(self, arg):
        """Show all chains of dependencies below this target."""
        for chain in query.dep_chains_rebuilt(self.target):
            self._print_chain(chain)

    def do_rebuild_chains(self, arg):
        """Show Jam's view on why this target was rebuilt."""
        for chain in query.rebuild_chains(self.target):
            self._print_chain(chain)

    def do_show(self, arg):
        """Dump all available meta-data for this target."""
        print("name:", self.target.name)
        print("depends on:")
        self._print_targets(self.target.deps)
        print("depended on by:")
        self._print_targets(self.target.deps_rev)
        print("includes:")
        self._print_targets(self.target.incs)
        print("included by:")
        self._print_targets(self.target.incs_rev)
        print("timestamp:", self.target.timestamp)
        print("binding:", self.target.binding)
        print("rebuilt:", self.target.rebuilt)
        if self.target.rebuilt:
            print("    rebuilt reason:", self.target.rebuild_info.reason)
            if self.target.rebuild_info.dep is not None:
                print("    dependency:", self.target.rebuild_info.dep.name)

    def _arg_to_kwargs(self, arg):
        """Parse an input argument consisting of param=value pairs."""
        kwargs = {}
        args = arg.split()
        for arg in args:
            key, value = arg.split("=")
            kwargs[key] = value
        return kwargs

    def _print_chain(self, chain):
        """Print a sequence of targets forming a dependency chain."""
        print(" -> ".join(target.name for target in chain))

    def _print_targets(self, targets):
        """Print a sequence of targets."""
        for target in targets:
            print("    {}".format(target.name))

<|endoftext|>


<commit_before>"""
A set of tools which allow specifying a model consisting of sheets
organized in levels, and projections connecting these sheets. The
sheets have an attribute matchconditions allowing to specify which
other (incoming) sheets a sheet should connect to.

Instances of the RegisteredMethod decorator are offered for setting
parameters/matchconditions for a sheet within a level, as well as
parameters for projections.
"""

from collections import OrderedDict
import itertools
from functools import wraps

import param

import topo
from topo import sheet,transferfn,pattern,projection
from topo.base.sheet import Sheet

from dataviews.collector import AttrTree

from topo.misc.commandline import global_params

class Specification(object):
    @property
    def spec_type(self):
        return self._spec_type

    def update_parameters(self, params):
        self.parameters.update(params)

    def resolve(self):
        """
        Returns the actual projection after it has been instantiated.
        """
        from topo import sim
        return eval('sim.'+str(self))

    def __init__(self, spec_type):
        self._spec_type = spec_type
        self.parameters = {}
        for param_name, default_value in spec_type.params().items():
            self.parameters[param_name]=default_value.default

class SheetSpec(Specification):
    """
    SheetSpec acts as a template for sheet objects. It is also
    possible to resolve the actual sheet object after it has been
    instantiated.

    The following attributes can be accessed:

    :'properties': Dictionary specifying the properties of the
    sheet. There must be a value given for the key 'level'.
    :'spec_type': Subclass of topo.base.sheet.Sheet.(read-only)
    :'parameters': Dictionary specifying which parameters should be
    passed to the sheet object specified with spec_type. Keys are the
    parameter names, values the parameter values.
    :'matchconditions': Dictionary specifying the matchconditions of
    the sheet. This may be used to determine which other sheets this
    sheet should connect to.
    """

    name_ordering = ['eye','level', 'cone', 'polarity','SF','opponent','surround']

    @property
    def level(self):
        return self.properties['level']

    def __init__(self, spec_type, properties):
        """
        Initialize a SheetSpec object. All arguments but parameters
        are just passed to the internal attributes. For parameters,
        additional key-value pairs with all possible parameters for
        the sheet type specified with sheet_type are added. This
        allows a lookup which parameters can be set.
        """
        super(SheetSpec,self).__init__(spec_type)

        if 'level' not in properties:
            raise Exception("SheetSpec always requires 'level' in properties")

        properties = [(k, properties[k]) for k in self.name_ordering
                      if k in properties]

        self.properties = OrderedDict(properties)
        self.matchconditions={}

    def update_matchconditions(self, matchconditions):
        self.matchconditions.update(matchconditions)

    def __call__(self):
        """
        Instantiate the sheet and register it in topo.sim.
        """
        topo.sim[str(self)]=self.spec_type(**self.parameters)

    def resolve(self):
        """
        Returns the actual sheet object after it has been instantiated.
        """
        from topo import sim
        return eval('sim.'+str(self))

    def __str__(self):
        """
        Returns a string representation combined from the properties values.
        This might be used as name for the actual sheet object.
        """
        name=''
        for prop in self.properties.itervalues():
            name+=prop

        return name

    def __repr__(self):
        type_name = self._spec_type.__name__
        properties_repr = ', '.join("%r:%r" % (k,v) for (k,v)
                                    in self.properties.items())
        return "SheetSpec(%s, {%s})" % (type_name, properties_repr)

class ProjectionSpec(Specification):
    """
    ProjectionSpec acts as a template for projections. It is also
    possible to resolve the actual projection after it has been
    instantiated.

    The following attributes can be accessed:

    :'src': SheetSpec of the source sheet
    :'dest': SheetSpec of the destination sheet
    :'spec_type': Subclass of topo.base.projection.Projection
    :'match_name': Name of the matchcondition which has been used to
    set this projection up.  This might be used to set the parameters
    of the ProjectionSpec
    :'parameters': Dictionary specifying which parameters should be
    passed to the projection specified with connection_type. Keys are
    the parameter names, values the parameter values.
    """

    def __init__(self, spec_type, src, dest, match_name=None, properties=None):
        """
        Initialize a ProjectionSpec object. All arguments but
        parameters are just passed to the internal attributes. For
        parameters, additional key-value pairs with all possible
        parameters for the projection type specified with
        connection_type are added. This allows a lookup which
        parameters can be set. Furthermore, parameters['src'] and
        parameters['dest'] are removed, as these must be set with the
        'src' and 'dest' arguments instead.
        """
        super(ProjectionSpec, self).__init__(spec_type)

        self.src = src
        self.dest = dest
        self.match_name = match_name

        self.properties = {} if properties is None else properties
        # These parameters are directly passed into topo.sim.connect()!
        ignored_keys = ['src', 'dest']
        self.parameters = dict((k,v) for (k,v) in self.parameters.items()
                               if k not in ignored_keys)

    def __call__(self):
        """
        Instantiate the projection and register it in topo.sim.
        """
        topo.sim.connect(str(self.src),str(self.dest),self.spec_type,
                         **self.parameters)

    def __str__(self):
        return str(self.dest)+'.'+self.parameters['name']

    def __repr__(self):
        type_name = self._spec_type.__name__
        properties_repr = "{"+ ', '.join("%r:%r" % (k,v) for (k,v) in self.properties.items()) +"}"
        return "ProjectionSpec(%s, %r, %r, %r%s)" % (type_name, self.src, self.dest,
                                                       self.match_name,
                                                       ", %s" % properties_repr if self.properties else '')

class LabelDecorator(object):
    """
    Decorator class which can be instantiated to create a decorator
    object. This object can then be used to decorate methods or
    functions with a label argument and optionally a type.

    After decorating several methods or functions, the dictionary of
    all the decorated callables can be accessed via the registry
    attribute. Any types supplies are accessible through the types
    attribute.
    """
    def __init__(self):
        self.registry = {}
        self.types = {}

    def __call__(self, label, obj_type=None):
        def decorator(f):
            @wraps(f)
            def inner(*args, **kwargs):
                return f(*args, **kwargs)

            self.registry[label] = inner
            if obj_type is not None:
                self.types[label] = obj_type
            return inner
        return decorator

class Model(param.Parameterized):
    """
    The available setup options are:

        :'training_patterns': fills the training_patterns AttrTree
        with pattern generator instances. The path is the name of the
        input sheet. Usually calls PatternCoordinator to do this.
        :'setup_sheets': determines the number of sheets, their types
        and names sets sheet parameters according to the registered
        methods in level sets sheet matchconditions according to the
        registered methods in matchconditions
        :'projections': determines which connections should be present
        between the sheets according to the matchconditions of
        SheetSpec objects, using connect to specify the
        connection type and sets their parameters according to the
        registered methods in connect

    The available instantiate options are:

        :'sheets': instantiates all sheets and registers them in
        topo.sim
        :'projections': instantiates all projections and registers
        them in topo.sim
    """

    available_setup_options = ['training_patterns','sheets','projections','analysis']

    available_instantiate_options = ['sheets','projections']

    __abstract = True

    level = LabelDecorator()
    matchconditions = LabelDecorator()
    connect = LabelDecorator()

    def _register_global_params(self, params):
        """
        Register the parameters of this object as global parameters
        available for users to set from the command line.  Values
        supplied as global parameters will override those of the given
        dictionary of params.
        """

        for param_name, param_value in self.params().items():
            global_params.add(**{param_name:param_value})

        params.update(global_params.get_param_values())

    def __init__(self, setup_options=True, register=True, **params):
        if register:
            self._register_global_params(params)
        super(Model,self).__init__(**params)
        self.initialize()

        self.training_patterns = AttrTree()
        self.sheets = AttrTree()
        self.projections = AttrTree()

        self.setup(setup_options)

    def initialize(self):
        """
        Method to precompute any useful self attributes from the class
        parameters. For instance, if there is a ``num_lags``
        parameter, this method could compute the actual projection
        delays and store it in self.lags.
        """
        pass

    def setup(self,setup_options):
        """
        This method can be used to setup certain aspects of the
        submodel.  If setup_options=True, all setup methods are
        called.  setup_options can also be a list, whereas all list
        items of available_setup_options are accepted.
        """

        if setup_options==True:
            setup_options = self.available_setup_options

        if 'training_patterns' in setup_options:
            self.setup_training_patterns()
        if 'sheets' in setup_options:
            sheet_specs = self.setup_sheets()
            for sheet_spec in sheet_specs:
                self.sheets.set_path(str(sheet_spec), sheet_spec)

            # Should the next two methods be merged with setup_sheets?
            self._update_sheet_parameters()
            self._compute_matchconditions()
        if 'projections' in setup_options:
            self._setup_projections()
        if 'analysis' in setup_options:
            self._setup_analysis()

    def setup_training_patterns(self):
        """
        Adds new PatternGenerators to self.training_patterns, with
        the name of the input sheet where the training patterns should
        be installed as path.
        """
        raise NotImplementedError

    def setup_sheets(self):
        """
        Adds new SheetSpec items to the self.sheets AttrTree.
        Must return a list of SheetSpec objects.
        """
        raise NotImplementedError

    def _setup_projections(self):
        """
        Loop through all possible combinations of SheetSpec objects in self.sheets
        If the src_sheet fulfills all criteria specified in dest_sheet.matchconditions,
        create a new ProjectionSpec object and add this item to self.projections.
        """
        self.projections = AttrTree()

        for src_sheet, dest_sheet in itertools.product(self.sheets.path_items.values(),
                                                       self.sheets.path_items.values()):
            for matchname, matchconditions in dest_sheet.matchconditions.items():
                is_match=True
                if matchconditions is None:
                    continue
                for incoming_key, incoming_value in matchconditions.items():
                    if incoming_key in src_sheet.properties and \
                        str(src_sheet.properties[incoming_key]) not in str(incoming_value):
                            is_match=False
                            break

                if is_match:
                    proj = ProjectionSpec(self.connect.types[matchname], src_sheet, dest_sheet, matchname)
                    paramsets = self.connect.registry[matchname](self, proj)
                    paramsets = [paramsets] if isinstance(paramsets, dict) else paramsets
                    for paramset in paramsets:
                        proj = ProjectionSpec(self.connect.types[matchname], src_sheet, dest_sheet, matchname)
                        proj.update_parameters(paramset)
                        self.projections.set_path(str(dest_sheet)+'.'+str(src_sheet)+'.'+ paramset['name'], proj)

    def _update_sheet_parameters(self):
        for sheet_spec in self.sheets.path_items.values():
            param_method = self.level.registry.get(sheet_spec.level, None)
            if not param_method:
                raise Exception("Parameters for sheet level %r not specified" % sheet_spec.level)

            updated_params = param_method(self,sheet_spec.properties)
            sheet_spec.update_parameters(updated_params)

    def _compute_matchconditions(self):
        for sheet_spec in self.sheets.path_items.values():
            matchcondition = self.matchconditions.registry.get(sheet_spec.level, False)
            if matchcondition:
                sheet_spec.update_matchconditions(matchcondition(self,sheet_spec.properties))

    def _setup_analysis(self):
        """
        Set up appropriate defaults for analysis functions in topo.analysis.featureresponses
        """
        pass

    def __call__(self,instantiate_options=True):
        """
        Instantiates all sheets / projections in self.sheets /
        self.projections and registers them in topo.sim If
        instantiate_options=True, all items are initialised
        instantiate_options can also be a list, whereas all list items
        of available_instantiate_options are accepted.
        """
        if instantiate_options==True:
            instantiate_options=self.available_instantiate_options

        if 'sheets' in instantiate_options:
            self.message('Sheets:\n')
            for sheet_spec in self.sheets.path_items.itervalues():
                self.message(sheet_spec)
                sheet_spec()
            self.message('\n\n')

        if 'projections' in instantiate_options:
            self.message('Connections:\n')
            #Ugly hack to take the order to initialize sheets into account
            #As soon as weight initialization is done with time_dependent=True,
            #this can be simplified to:
            #for proj in self.projections.path_items.itervalues():
            connection_order=['Afferent','AfferentCenter','AfferentSurround',
                              'LateralGC','AfferentV1On','AfferentV1Off',
                              'LateralV1Excitatory','LateralV1Inhibitory']
            for proj in sorted(self.projections.path_items.itervalues(),
                               key=lambda projection: connection_order.index(projection.match_name)):
                self.message('Connect ' + str(proj.src) + ' with ' + str(proj.dest) + \
                             ' (Match name: ' + proj.match_name + \
                             ', connection name: ' + str(proj.parameters['name']) + ')')
                proj()
<commit_msg>Renamed the registry attribute of the decorators to labelled<commit_after>"""
A set of tools which allow specifying a model consisting of sheets
organized in levels, and projections connecting these sheets. The
sheets have an attribute matchconditions allowing to specify which
other (incoming) sheets a sheet should connect to.

Instances of the RegisteredMethod decorator are offered for setting
parameters/matchconditions for a sheet within a level, as well as
parameters for projections.
"""

from collections import OrderedDict
import itertools
from functools import wraps

import param

import topo
from topo import sheet,transferfn,pattern,projection
from topo.base.sheet import Sheet

from dataviews.collector import AttrTree

from topo.misc.commandline import global_params

class Specification(object):
    @property
    def spec_type(self):
        return self._spec_type

    def update_parameters(self, params):
        self.parameters.update(params)

    def resolve(self):
        """
        Returns the actual projection after it has been instantiated.
        """
        from topo import sim
        return eval('sim.'+str(self))

    def __init__(self, spec_type):
        self._spec_type = spec_type
        self.parameters = {}
        for param_name, default_value in spec_type.params().items():
            self.parameters[param_name]=default_value.default

class SheetSpec(Specification):
    """
    SheetSpec acts as a template for sheet objects. It is also
    possible to resolve the actual sheet object after it has been
    instantiated.

    The following attributes can be accessed:

    :'properties': Dictionary specifying the properties of the
    sheet. There must be a value given for the key 'level'.
    :'spec_type': Subclass of topo.base.sheet.Sheet.(read-only)
    :'parameters': Dictionary specifying which parameters should be
    passed to the sheet object specified with spec_type. Keys are the
    parameter names, values the parameter values.
    :'matchconditions': Dictionary specifying the matchconditions of
    the sheet. This may be used to determine which other sheets this
    sheet should connect to.
    """

    name_ordering = ['eye','level', 'cone', 'polarity','SF','opponent','surround']

    @property
    def level(self):
        return self.properties['level']

    def __init__(self, spec_type, properties):
        """
        Initialize a SheetSpec object. All arguments but parameters
        are just passed to the internal attributes. For parameters,
        additional key-value pairs with all possible parameters for
        the sheet type specified with sheet_type are added. This
        allows a lookup which parameters can be set.
        """
        super(SheetSpec,self).__init__(spec_type)

        if 'level' not in properties:
            raise Exception("SheetSpec always requires 'level' in properties")

        properties = [(k, properties[k]) for k in self.name_ordering
                      if k in properties]

        self.properties = OrderedDict(properties)
        self.matchconditions={}

    def update_matchconditions(self, matchconditions):
        self.matchconditions.update(matchconditions)

    def __call__(self):
        """
        Instantiate the sheet and register it in topo.sim.
        """
        topo.sim[str(self)]=self.spec_type(**self.parameters)

    def resolve(self):
        """
        Returns the actual sheet object after it has been instantiated.
        """
        from topo import sim
        return eval('sim.'+str(self))

    def __str__(self):
        """
        Returns a string representation combined from the properties values.
        This might be used as name for the actual sheet object.
        """
        name=''
        for prop in self.properties.itervalues():
            name+=prop

        return name

    def __repr__(self):
        type_name = self._spec_type.__name__
        properties_repr = ', '.join("%r:%r" % (k,v) for (k,v)
                                    in self.properties.items())
        return "SheetSpec(%s, {%s})" % (type_name, properties_repr)

class ProjectionSpec(Specification):
    """
    ProjectionSpec acts as a template for projections. It is also
    possible to resolve the actual projection after it has been
    instantiated.

    The following attributes can be accessed:

    :'src': SheetSpec of the source sheet
    :'dest': SheetSpec of the destination sheet
    :'spec_type': Subclass of topo.base.projection.Projection
    :'match_name': Name of the matchcondition which has been used to
    set this projection up.  This might be used to set the parameters
    of the ProjectionSpec
    :'parameters': Dictionary specifying which parameters should be
    passed to the projection specified with connection_type. Keys are
    the parameter names, values the parameter values.
    """

    def __init__(self, spec_type, src, dest, match_name=None, properties=None):
        """
        Initialize a ProjectionSpec object. All arguments but
        parameters are just passed to the internal attributes. For
        parameters, additional key-value pairs with all possible
        parameters for the projection type specified with
        connection_type are added. This allows a lookup which
        parameters can be set. Furthermore, parameters['src'] and
        parameters['dest'] are removed, as these must be set with the
        'src' and 'dest' arguments instead.
        """
        super(ProjectionSpec, self).__init__(spec_type)

        self.src = src
        self.dest = dest
        self.match_name = match_name

        self.properties = {} if properties is None else properties
        # These parameters are directly passed into topo.sim.connect()!
        ignored_keys = ['src', 'dest']
        self.parameters = dict((k,v) for (k,v) in self.parameters.items()
                               if k not in ignored_keys)

    def __call__(self):
        """
        Instantiate the projection and register it in topo.sim.
        """
        topo.sim.connect(str(self.src),str(self.dest),self.spec_type,
                         **self.parameters)

    def __str__(self):
        return str(self.dest)+'.'+self.parameters['name']

    def __repr__(self):
        type_name = self._spec_type.__name__
        properties_repr = "{"+ ', '.join("%r:%r" % (k,v) for (k,v) in self.properties.items()) +"}"
        return "ProjectionSpec(%s, %r, %r, %r%s)" % (type_name, self.src, self.dest,
                                                       self.match_name,
                                                       ", %s" % properties_repr if self.properties else '')

class LabelDecorator(object):
    """
    Decorator class which can be instantiated to create a decorator
    object. This object can then be used to decorate methods or
    functions with a label argument and optionally a type.

    After decorating several methods or functions, the dictionary of
    all the decorated callables can be accessed via the labelled
    attribute. Any types supplies are accessible through the types
    attribute.
    """
    def __init__(self):
        self.labelled = {}
        self.types = {}

    def __call__(self, label, obj_type=None):
        def decorator(f):
            @wraps(f)
            def inner(*args, **kwargs):
                return f(*args, **kwargs)

            self.labelled[label] = inner
            if obj_type is not None:
                self.types[label] = obj_type
            return inner
        return decorator

class Model(param.Parameterized):
    """
    The available setup options are:

        :'training_patterns': fills the training_patterns AttrTree
        with pattern generator instances. The path is the name of the
        input sheet. Usually calls PatternCoordinator to do this.
        :'setup_sheets': determines the number of sheets, their types
        and names sets sheet parameters according to the registered
        methods in level sets sheet matchconditions according to the
        registered methods in matchconditions
        :'projections': determines which connections should be present
        between the sheets according to the matchconditions of
        SheetSpec objects, using connect to specify the
        connection type and sets their parameters according to the
        registered methods in connect

    The available instantiate options are:

        :'sheets': instantiates all sheets and registers them in
        topo.sim
        :'projections': instantiates all projections and registers
        them in topo.sim
    """

    available_setup_options = ['training_patterns','sheets','projections','analysis']

    available_instantiate_options = ['sheets','projections']

    __abstract = True

    level = LabelDecorator()
    matchconditions = LabelDecorator()
    connect = LabelDecorator()

    def _register_global_params(self, params):
        """
        Register the parameters of this object as global parameters
        available for users to set from the command line.  Values
        supplied as global parameters will override those of the given
        dictionary of params.
        """

        for param_name, param_value in self.params().items():
            global_params.add(**{param_name:param_value})

        params.update(global_params.get_param_values())

    def __init__(self, setup_options=True, register=True, **params):
        if register:
            self._register_global_params(params)
        super(Model,self).__init__(**params)
        self.initialize()

        self.training_patterns = AttrTree()
        self.sheets = AttrTree()
        self.projections = AttrTree()

        self.setup(setup_options)

    def initialize(self):
        """
        Method to precompute any useful self attributes from the class
        parameters. For instance, if there is a ``num_lags``
        parameter, this method could compute the actual projection
        delays and store it in self.lags.
        """
        pass

    def setup(self,setup_options):
        """
        This method can be used to setup certain aspects of the
        submodel.  If setup_options=True, all setup methods are
        called.  setup_options can also be a list, whereas all list
        items of available_setup_options are accepted.
        """

        if setup_options==True:
            setup_options = self.available_setup_options

        if 'training_patterns' in setup_options:
            self.setup_training_patterns()
        if 'sheets' in setup_options:
            sheet_specs = self.setup_sheets()
            for sheet_spec in sheet_specs:
                self.sheets.set_path(str(sheet_spec), sheet_spec)

            # Should the next two methods be merged with setup_sheets?
            self._update_sheet_parameters()
            self._compute_matchconditions()
        if 'projections' in setup_options:
            self._setup_projections()
        if 'analysis' in setup_options:
            self._setup_analysis()

    def setup_training_patterns(self):
        """
        Adds new PatternGenerators to self.training_patterns, with
        the name of the input sheet where the training patterns should
        be installed as path.
        """
        raise NotImplementedError

    def setup_sheets(self):
        """
        Adds new SheetSpec items to the self.sheets AttrTree.
        Must return a list of SheetSpec objects.
        """
        raise NotImplementedError

    def _setup_projections(self):
        """
        Loop through all possible combinations of SheetSpec objects in self.sheets
        If the src_sheet fulfills all criteria specified in dest_sheet.matchconditions,
        create a new ProjectionSpec object and add this item to self.projections.
        """
        self.projections = AttrTree()

        for src_sheet, dest_sheet in itertools.product(self.sheets.path_items.values(),
                                                       self.sheets.path_items.values()):
            for matchname, matchconditions in dest_sheet.matchconditions.items():
                is_match=True
                if matchconditions is None:
                    continue
                for incoming_key, incoming_value in matchconditions.items():
                    if incoming_key in src_sheet.properties and \
                        str(src_sheet.properties[incoming_key]) not in str(incoming_value):
                            is_match=False
                            break

                if is_match:
                    proj = ProjectionSpec(self.connect.types[matchname], src_sheet, dest_sheet, matchname)
                    paramsets = self.connect.labelled[matchname](self, proj)
                    paramsets = [paramsets] if isinstance(paramsets, dict) else paramsets
                    for paramset in paramsets:
                        proj = ProjectionSpec(self.connect.types[matchname], src_sheet, dest_sheet, matchname)
                        proj.update_parameters(paramset)
                        self.projections.set_path(str(dest_sheet)+'.'+str(src_sheet)+'.'+ paramset['name'], proj)

    def _update_sheet_parameters(self):
        for sheet_spec in self.sheets.path_items.values():
            param_method = self.level.labelled.get(sheet_spec.level, None)
            if not param_method:
                raise Exception("Parameters for sheet level %r not specified" % sheet_spec.level)

            updated_params = param_method(self,sheet_spec.properties)
            sheet_spec.update_parameters(updated_params)

    def _compute_matchconditions(self):
        for sheet_spec in self.sheets.path_items.values():
            matchcondition = self.matchconditions.labelled.get(sheet_spec.level, False)
            if matchcondition:
                sheet_spec.update_matchconditions(matchcondition(self,sheet_spec.properties))

    def _setup_analysis(self):
        """
        Set up appropriate defaults for analysis functions in topo.analysis.featureresponses
        """
        pass

    def __call__(self,instantiate_options=True):
        """
        Instantiates all sheets / projections in self.sheets /
        self.projections and registers them in topo.sim If
        instantiate_options=True, all items are initialised
        instantiate_options can also be a list, whereas all list items
        of available_instantiate_options are accepted.
        """
        if instantiate_options==True:
            instantiate_options=self.available_instantiate_options

        if 'sheets' in instantiate_options:
            self.message('Sheets:\n')
            for sheet_spec in self.sheets.path_items.itervalues():
                self.message(sheet_spec)
                sheet_spec()
            self.message('\n\n')

        if 'projections' in instantiate_options:
            self.message('Connections:\n')
            #Ugly hack to take the order to initialize sheets into account
            #As soon as weight initialization is done with time_dependent=True,
            #this can be simplified to:
            #for proj in self.projections.path_items.itervalues():
            connection_order=['Afferent','AfferentCenter','AfferentSurround',
                              'LateralGC','AfferentV1On','AfferentV1Off',
                              'LateralV1Excitatory','LateralV1Inhibitory']
            for proj in sorted(self.projections.path_items.itervalues(),
                               key=lambda projection: connection_order.index(projection.match_name)):
                self.message('Connect ' + str(proj.src) + ' with ' + str(proj.dest) + \
                             ' (Match name: ' + proj.match_name + \
                             ', connection name: ' + str(proj.parameters['name']) + ')')
                proj()
<|endoftext|>


<commit_before>##
# Name: Sam Kantor
#
#
##

from tkinter import *

class LoginFrame(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent)

        self.parent = parent

        self.initUI()

    def center_window(self):
        screenWidth = self.parent.winfo_screenwidth()
        screenHeight = self.parent.winfo_screenheight()

        # Calculate (x, y) position of window
        x = (screenWidth/2) - (300/2)
        y = (screenHeight/2) - (300/2)

        self.parent.geometry('%dx%d+%d+%d' % (300, 300, x, y))

    def initUI(self):
        self.center_window()
        self.parent.title("Login")
        self.parent.resizable(width=FALSE, height=FALSE)
        self.pack(fill=BOTH, expand=1)

        self.columnconfigure(0, weight=1)
        self.rowconfigure(0, weight=1)
        self.rowconfigure(1, weight=1)
        self.rowconfigure(2, weight=1)
        self.rowconfigure(3, weight=1)
        self.rowconfigure(4, weight=1)
        self.rowconfigure(5, weight=1)
        self.rowconfigure(6, weight=1)

        loginLabel = Label(self, text="Login", font=(16))
        loginLabel.grid(row=0, column=0, sticky=S)

        idLabel = Label(self, text="Student ID:")
        idLabel.grid(row=1, column=0, sticky=S)
        idText = Entry(self)
        idText.grid(row=2, column=0)

        passwordLabel = Label(self, text="Password:")
        passwordLabel.grid(row=3, column=0, sticky=S)
        passwordText = Entry(self, show="*")
        passwordText.grid(row=4, column=0)

        loginButton = Button(self, text="Login", command = self.login)
        loginButton.grid(row=5, column=0)

        createButton = Button(self, text="Create Account", command = self.create_account)
        createButton.grid(row=6, column=0)

    def login(self):
        return

    def create_account(self):
        createAccountWin = CreateAccountFrame(self)
        return

class CreateAccountFrame(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.parent = parent
        self.initUI()

    def center_window(self):
        screenWidth = self.parent.winfo_screenwidth()
        screenHeight = self.parent.winfo_screenheight()

        # Calculate (x, y) position of window
        x = (screenWidth/2) - (300/2)
        y = (screenHeight/2) - (300/2)

        self.parent.geometry('%dx%d+%d+%d' % (300, 300, x, y))

    def initUI(self):
        self.center_window()
        self.parent.title("Create an Account")
        self.pack(fill=BOTH, expand=1)

        self.columnconfigure(0, weight=1)
        self.rowconfigure(0, weight=1)
        self.rowconfigure(1, weight=1)
        self.rowconfigure(2, weight=1)
        self.rowconfigure(3, weight=1)
        self.rowconfigure(4, weight=1)
        self.rowconfigure(5, weight=1)
        self.rowconfigure(6, weight=1)

        createAccountLabel = Label(self, text="Create an Account", font=(16))
        createAccountLabel.grid(row=0, column=0, sticky=S)

        idLabel = Label(self, text="Student ID:")
        idLabel.grid(row=1, column=0, sticky=S)
        idText = Entry(self)
        idText.grid(row=2, column=0)

        passwordLabel = Label(self, text="Password:")
        passwordLabel.grid(row=3, column=0, sticky=S)
        passwordText = Entry(self, show="*")
        passwordText.grid(row=4, column=0)

        gradeLabel = Label(self, text="Grade:")
        gradeLabel.grid(row=5, column=0, sticky=S)

        variable = StringVar(self.parent)
        variable.set("9")
        gradeSelection = OptionMenu(self, variable, "9", "10", "11", "12")
        gradeSelection.grid(row=6, column=0, sticky=N)

def main():
    root = Tk()
    app = CreateAccountFrame(root)
    root.mainloop()

if __name__ == '__main__':
    main()
<commit_msg>Opens Create account frame functionality.<commit_after>##
# Name: Sam Kantor
#
#
##

from tkinter import *

class LoginFrame(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent)

        self.parent = parent

        self.initUI()

    def center_window(self):
        screenWidth = self.parent.winfo_screenwidth()
        screenHeight = self.parent.winfo_screenheight()

        # Calculate (x, y) position of window
        x = (screenWidth/2) - (300/2)
        y = (screenHeight/2) - (300/2)

        self.parent.geometry('%dx%d+%d+%d' % (300, 300, x, y))

    def initUI(self):
        self.center_window()
        self.parent.title("Login")
        self.parent.resizable(width=FALSE, height=FALSE)
        self.pack(fill=BOTH, expand=1)

        self.columnconfigure(0, weight=1)
        self.rowconfigure(0, weight=1)
        self.rowconfigure(1, weight=1)
        self.rowconfigure(2, weight=1)
        self.rowconfigure(3, weight=1)
        self.rowconfigure(4, weight=1)
        self.rowconfigure(5, weight=1)
        self.rowconfigure(6, weight=1)

        loginLabel = Label(self, text="Login", font=(16))
        loginLabel.grid(row=0, column=0, sticky=S)

        idLabel = Label(self, text="Student ID:")
        idLabel.grid(row=1, column=0, sticky=S)
        idText = Entry(self)
        idText.grid(row=2, column=0)

        passwordLabel = Label(self, text="Password:")
        passwordLabel.grid(row=3, column=0, sticky=S)
        passwordText = Entry(self, show="*")
        passwordText.grid(row=4, column=0)

        loginButton = Button(self, text="Login", command = self.login)
        loginButton.grid(row=5, column=0)

        createButton = Button(self, text="Create Account", command = self.create_account)
        createButton.grid(row=6, column=0)

    def login(self):
        return

    def create_account(self):
        self.newWindow = Toplevel(self.master)
        self.app = CreateAccountFrame(self.newWindow)
        return

class CreateAccountFrame(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.parent = parent
        self.initUI()

    def center_window(self):
        screenWidth = self.parent.winfo_screenwidth()
        screenHeight = self.parent.winfo_screenheight()

        # Calculate (x, y) position of window
        x = (screenWidth/2) - (300/2)
        y = (screenHeight/2) - (300/2)

        self.parent.geometry('%dx%d+%d+%d' % (300, 300, x, y))

    def initUI(self):
        self.center_window()
        self.parent.title("Create an Account")
        self.pack(fill=BOTH, expand=1)

        self.columnconfigure(0, weight=1)
        self.rowconfigure(0, weight=1)
        self.rowconfigure(1, weight=1)
        self.rowconfigure(2, weight=1)
        self.rowconfigure(3, weight=1)
        self.rowconfigure(4, weight=1)
        self.rowconfigure(5, weight=1)
        self.rowconfigure(6, weight=1)

        createAccountLabel = Label(self, text="Create an Account", font=(16))
        createAccountLabel.grid(row=0, column=0, sticky=S)

        idLabel = Label(self, text="Student ID:")
        idLabel.grid(row=1, column=0, sticky=S)
        idText = Entry(self)
        idText.grid(row=2, column=0)

        passwordLabel = Label(self, text="Password:")
        passwordLabel.grid(row=3, column=0, sticky=S)
        passwordText = Entry(self, show="*")
        passwordText.grid(row=4, column=0)

        gradeLabel = Label(self, text="Grade:")
        gradeLabel.grid(row=5, column=0, sticky=S)

        variable = StringVar(self.parent)
        variable.set("9")
        gradeSelection = OptionMenu(self, variable, "9", "10", "11", "12")
        gradeSelection.grid(row=6, column=0, sticky=N)

def main():
    root = Tk()
    app = LoginFrame(root)
    root.mainloop()

if __name__ == '__main__':
    main()
<|endoftext|>


<commit_before>#!/usr/bin/env python

"""
Script to test writing GW Input for VASP.
Reads the POSCAR_name in the the current folder and outputs GW input to
subfolders name
"""

from __future__ import division

__author__ = "Michiel van Setten"
__copyright__ = " "
__version__ = "0.9"
__maintainer__ = "Michiel van Setten"
__email__ = "mjvansetten@gmail.com"
__date__ = "Oct 23, 2013"

import os

from pymatgen.io.abinitio.netcdf import NetcdfReader
from pymatgen.io.vaspio.vasp_output import Vasprun
from pymatgen.core.units import Ha_to_eV

MODULE_DIR = os.path.dirname(os.path.abspath(__file__))

class GWConvergenceData():
    def __init__(self, structure, spec):
        self.structure = structure
        self.spec = spec
        self.data = {}
        self.name = structure.composition.reduced_formula

    def read(self):
        if self.spec['code'] == 'ABINIT':
            read_next = True
            n = 3
            while read_next:
                output = os.path.join(self.name,  'work_0', 'task_' + str(n), 'outdata', 'out_SIGRES.nc')
                if os.path.isfile(output):
                    n += 1
                    data = NetcdfReader(output)
                    data.print_tree()
                    self.data.update({n: {'ecuteps': Ha_to_eV * data.read_value('ecuteps'), 'nbands': data.read_value('sigma_nband'), 'gwgap': data.read_value('egwgap')[0][0]}})
                    data.close()
                else:
                    read_next = False
        elif self.spec['code'] == 'VASP':
            tree = os.walk('.')
            n = 0
            for dirs in tree:
                if "/" + self.name + "." in dirs[0] and ('G0W0' in dirs[0] or 'GW0' in dirs[0] or 'scGW0' in dirs[0]):
                    run = os.path.join(dirs[0], 'vasprun.xml')
                    print run
                    kpoints = os.path.join(dirs[0], 'IBZKPT')
                    if os.path.isfile(run):
                        try:
                            data = Vasprun(run, ionic_step_skip=1)
                            parameters = data.__getattribute__('incar').to_dict
                            bandstructure = data.get_band_structure(kpoints)
                            self.data.update({n: {'ecuteps': parameters['ENCUTGW'], 'nbands': parameters['NBANDS'],
                                              'nomega': parameters['NOMEGA'], 'gwgap': bandstructure.get_band_gap()['energy']}})
                            n += 1
                        except BaseException:
                            pass

    def print_plot_data(self):
        data_list = []
        for k in self.data:
            if 'nomega' in self.data[k].keys():
                data_list.append([self.data[k]['nbands'], self.data[k]['ecuteps'], self.data[k]['nomega'], self.data[k]['gwgap']])
            else:
                data_list.append([self.data[k]['nbands'], self.data[k]['ecuteps'], self.data[k]['gwgap']])

        data_file = self.name + '.data'
        f = open(data_file, mode='w')
        for data in sorted(data_list):
            if data[3]:
                f.write(data[0], data[1], data[2], data[3])
            else:
                f.write(data[0], data[1], data[2])

        '''
        [u'space_group', u'primitive_vectors', u'reduced_symmetry_matrices', u'reduced_symmetry_translations',
         u'atom_species', u'reduced_atom_positions', u'valence_charges', u'atomic_numbers', u'atom_species_names',
         u'chemical_symbols', u'pseudopotential_types', u'symafm', u'kpoint_grid_shift', u'kpoint_grid_vectors',
         u'monkhorst_pack_folding', u'reduced_coordinates_of_kpoints', u'kpoint_weights', u'number_of_electrons',
         u'exchange_functional', u'correlation_functional', u'fermi_energy', u'smearing_scheme', u'smearing_width',
          u'number_of_states', u'eigenvalues', u'occupations', u'tphysel', u'occopt', u'istwfk', u'shiftk',
         u'kptrlatt', u'ngkpt_shiftk', u'ecutwfn', u'ecuteps', u'ecutsigx', u'sigma_nband', u'gwcalctyp',
         u'omegasrdmax', u'deltae', u'omegasrmax', u'scissor_ene', u'usepawu', u'kptgw', u'minbnd', u'maxbnd',
         u'degwgap', u'egwgap', u'en_qp_diago', u'e0', u'e0gap', u'sigxme', u'vxcme', u'vUme', u'degw', u'dsigmee0',
         u'egw', u'eigvec_qp', u'hhartree', u'sigmee', u'sigcmee0', u'sigcmesi', u'sigcme4sd', u'sigxcme4sd',
         u'ze0', u'omega4sd']
        '''<commit_msg>bug fixing<commit_after>#!/usr/bin/env python

"""
Script to test writing GW Input for VASP.
Reads the POSCAR_name in the the current folder and outputs GW input to
subfolders name
"""

from __future__ import division

__author__ = "Michiel van Setten"
__copyright__ = " "
__version__ = "0.9"
__maintainer__ = "Michiel van Setten"
__email__ = "mjvansetten@gmail.com"
__date__ = "Oct 23, 2013"

import os

from pymatgen.io.abinitio.netcdf import NetcdfReader
from pymatgen.io.vaspio.vasp_output import Vasprun
from pymatgen.core.units import Ha_to_eV

MODULE_DIR = os.path.dirname(os.path.abspath(__file__))

class GWConvergenceData():
    def __init__(self, structure, spec):
        self.structure = structure
        self.spec = spec
        self.data = {}
        self.name = structure.composition.reduced_formula

    def read(self):
        if self.spec['code'] == 'ABINIT':
            read_next = True
            n = 3
            while read_next:
                output = os.path.join(self.name,  'work_0', 'task_' + str(n), 'outdata', 'out_SIGRES.nc')
                if os.path.isfile(output):
                    n += 1
                    data = NetcdfReader(output)
                    data.print_tree()
                    self.data.update({n: {'ecuteps': Ha_to_eV * data.read_value('ecuteps'), 'nbands': data.read_value('sigma_nband'), 'gwgap': data.read_value('egwgap')[0][0]}})
                    data.close()
                else:
                    read_next = False
        elif self.spec['code'] == 'VASP':
            tree = os.walk('.')
            n = 0
            for dirs in tree:
                if "/" + self.name + "." in dirs[0] and ('G0W0' in dirs[0] or 'GW0' in dirs[0] or 'scGW0' in dirs[0]):
                    run = os.path.join(dirs[0], 'vasprun.xml')
                    print run
                    kpoints = os.path.join(dirs[0], 'IBZKPT')
                    if os.path.isfile(run):
                        try:
                            data = Vasprun(run, ionic_step_skip=1)
                            parameters = data.__getattribute__('incar').to_dict
                            bandstructure = data.get_band_structure(kpoints)
                            self.data.update({n: {'ecuteps': parameters['ENCUTGW'], 'nbands': parameters['NBANDS'],
                                              'nomega': parameters['NOMEGA'], 'gwgap': bandstructure.get_band_gap()['energy']}})
                            n += 1
                        except BaseException:
                            pass

    def print_plot_data(self):
        data_list = []
        for k in self.data:
            if 'nomega' in self.data[k].keys():
                data_list.append([self.data[k]['nbands'], self.data[k]['ecuteps'], self.data[k]['nomega'], self.data[k]['gwgap']])
            else:
                data_list.append([self.data[k]['nbands'], self.data[k]['ecuteps'], self.data[k]['gwgap']])

        data_file = self.name + '.data'
        f = open(data_file, mode='w')
        for data in sorted(data_list):
            if data[3]:
                f.write(str((data[0], data[1], data[2], data[3])))
            else:
                f.write(str((data[0], data[1], data[2])))
        f.close()

        '''
        [u'space_group', u'primitive_vectors', u'reduced_symmetry_matrices', u'reduced_symmetry_translations',
         u'atom_species', u'reduced_atom_positions', u'valence_charges', u'atomic_numbers', u'atom_species_names',
         u'chemical_symbols', u'pseudopotential_types', u'symafm', u'kpoint_grid_shift', u'kpoint_grid_vectors',
         u'monkhorst_pack_folding', u'reduced_coordinates_of_kpoints', u'kpoint_weights', u'number_of_electrons',
         u'exchange_functional', u'correlation_functional', u'fermi_energy', u'smearing_scheme', u'smearing_width',
          u'number_of_states', u'eigenvalues', u'occupations', u'tphysel', u'occopt', u'istwfk', u'shiftk',
         u'kptrlatt', u'ngkpt_shiftk', u'ecutwfn', u'ecuteps', u'ecutsigx', u'sigma_nband', u'gwcalctyp',
         u'omegasrdmax', u'deltae', u'omegasrmax', u'scissor_ene', u'usepawu', u'kptgw', u'minbnd', u'maxbnd',
         u'degwgap', u'egwgap', u'en_qp_diago', u'e0', u'e0gap', u'sigxme', u'vxcme', u'vUme', u'degw', u'dsigmee0',
         u'egw', u'eigvec_qp', u'hhartree', u'sigmee', u'sigcmee0', u'sigcmesi', u'sigcme4sd', u'sigxcme4sd',
         u'ze0', u'omega4sd']
        '''<|endoftext|>


<commit_before># Copyright 2015 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests the python app engine code.  Tests the JSON API.
TODO: Automatically deploy new Windows backends when the csharp code
      changes.
"""
import json
import os
import pprint
import urllib
import urllib2
import urlparse

HOST = 'https://{0}-dot-{1}.appspot.com'.format(
    os.environ['GOOGLE_APP_VERSION'], os.environ['GOOGLE_APP_ID'])

class Failure(Exception):
    def __init__(self, status, errors):
        self.status = status
        self.errors = errors

    def __str__(self):
        return "Failed with status {}.  Errors:\n{}".format(
            self.status, "\n".join(self.errors))

class BrowserState(object):
    """Behaves like the browser.  Invokes our JSON API."""
    def __init__(self, host):
        """host should include the scheme; ex: https://www.google.com"""
        self.shout_id = 0
        self.final_link = None
        self.shout_link = None
        self.host = host
        self.urlopen("{}/connect".format(host), '')

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        """Invokes final_link if we saw one."""
        if self.final_link:
            self.urlopen(self.final_link['target'], {
                'token': self.final_link['token']})

    def handle_response(self, response):
        """Expects a successful HTTP response.

        :arg response: a return value from urllib2.urlopen()
        :returns a decoded json dict.
        """
        assert response.getcode() >= 200
        assert response.getcode() < 300
        reply = json.load(response)
        pprint.pprint(reply)
        self.shout_link = reply.get('shoutLink', self.shout_link)
        self.final_link = reply.get('finalLink', self.final_link)
        return reply

    def urlopen(self, target, keyval):
        """Call urllib2.urlopen() for the target link.

        :arg target: a url or partial url relative to host name.
        :returns a decoded json dict.
        """
        url = urlparse.urljoin(self.host, target)
        print url
        pprint.pprint(keyval)
        response = urllib2.urlopen(url, urllib.urlencode(keyval))
        return self.handle_response(response)

    def shout(self, text):
        """Invokes shout on the text."""
        shout_id = self.shout_id
        self.shout_id += 1
        payload = {
            'token': self.shout_link['token'],
            'text': text,
            'shoutId': shout_id
        }
        result = None
        errors = []
        reply = self.urlopen(self.shout_link['target'], payload)
        while True:
            result = reply.get('result', result)
            if reply.get('error'):
                errors.append(reply['error'])
            if 'nextLink' in reply:
                next_link = reply['nextLink']
                payload = {
                    'token': next_link['token'],
                    'shoutId': shout_id,
                }
                reply = self.urlopen(next_link['target'], payload)
            else:
                break
        if reply['status'] == 'success':
            return result
        else:
            raise Failure(reply['status'], errors)

with BrowserState(HOST) as state:
    assert 'HELLO' == state.shout('hello')
    assert 'JEFF' == state.shout('jeff')
    try:
        state.shout('chickens')
        assert False
    except Failure:
        pass

print "ok"
<commit_msg>Use Python's unittest module.<commit_after># Copyright 2015 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests the python app engine code.  Tests the JSON API.
TODO: Automatically deploy new Windows backends when the csharp code
      changes.
"""
import json
import os
import pprint
import unittest
import urllib
import urllib2
import urlparse

HOST = 'https://{0}-dot-{1}.appspot.com'.format(
    os.environ['GOOGLE_APP_VERSION'], os.environ['GOOGLE_APP_ID'])

class Failure(Exception):
    def __init__(self, status, errors):
        self.status = status
        self.errors = errors

    def __str__(self):
        return "Failed with status {}.  Errors:\n{}".format(
            self.status, "\n".join(self.errors))

class BrowserState(object):
    """Behaves like the browser.  Invokes our JSON API."""
    def __init__(self, host):
        """host should include the scheme; ex: https://www.google.com"""
        self.shout_id = 0
        self.final_link = None
        self.shout_link = None
        self.host = host
        self.urlopen("{}/connect".format(host), '')

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        """Invokes final_link if we saw one."""
        if self.final_link:
            self.urlopen(self.final_link['target'], {
                'token': self.final_link['token']})

    def handle_response(self, response):
        """Expects a successful HTTP response.

        :arg response: a return value from urllib2.urlopen()
        :returns a decoded json dict.
        """
        assert response.getcode() >= 200
        assert response.getcode() < 300
        reply = json.load(response)
        pprint.pprint(reply)
        self.shout_link = reply.get('shoutLink', self.shout_link)
        self.final_link = reply.get('finalLink', self.final_link)
        return reply

    def urlopen(self, target, keyval):
        """Call urllib2.urlopen() for the target link.

        :arg target: a url or partial url relative to host name.
        :returns a decoded json dict.
        """
        url = urlparse.urljoin(self.host, target)
        print url
        pprint.pprint(keyval)
        response = urllib2.urlopen(url, urllib.urlencode(keyval))
        return self.handle_response(response)

    def shout(self, text):
        """Invokes shout on the text."""
        shout_id = self.shout_id
        self.shout_id += 1
        payload = {
            'token': self.shout_link['token'],
            'text': text,
            'shoutId': shout_id
        }
        result = None
        errors = []
        reply = self.urlopen(self.shout_link['target'], payload)
        while True:
            result = reply.get('result', result)
            if reply.get('error'):
                errors.append(reply['error'])
            if 'nextLink' in reply:
                next_link = reply['nextLink']
                payload = {
                    'token': next_link['token'],
                    'shoutId': shout_id,
                }
                reply = self.urlopen(next_link['target'], payload)
            else:
                break
        if reply['status'] == 'success':
            return result
        else:
            raise Failure(reply['status'], errors)

class Test(unittest.TestCase):
    def test_all(self):
        with BrowserState(HOST) as state:
            self.assertEqual('HELLO', state.shout('hello'))
            self.assertEqual('JEFF', state.shout('jeff'))
            with self.assertRaises(Failure):
                state.shout('chickens')

if __name__ == '__main__':
    unittest.main()<|endoftext|>


<commit_before>import logging
import os
from contextlib import contextmanager
from typing import TYPE_CHECKING, Dict, Iterable, cast

from funcy import compact, lremove, post_processing
from rich.prompt import Prompt
from rich.rule import Rule
from rich.syntax import Syntax

from dvc.exceptions import DvcException
from dvc.stage import PipelineStage
from dvc.stage.serialize import to_pipeline_file
from dvc.types import OptStr
from dvc.utils.serialize import dumps_yaml

if TYPE_CHECKING:
    from dvc.repo import Repo
    from dvc.dvcfile import DVCFile
    from dvc.stage import Stage

from dvc.ui import ui

PROMPTS = {
    "cmd": "[b]Command[/b] to execute",
    "code": "Path to a [b]code[/b] file/directory",
    "data": "Path to a [b]data[/b] file/directory",
    "models": "Path to a [b]model[/b] file/directory",
    "params": "Path to a [b]parameters[/b] file",
    "metrics": "Path to a [b]metrics[/b] file",
    "plots": "Path to a [b]plots[/b] file/directory",
    "live": "Path to log [b]dvclive[/b] outputs",
}

class RequiredPrompt(Prompt):
    def process_response(self, value: str):
        from rich.prompt import InvalidResponse

        ret = super().process_response(value)
        if not ret:
            raise InvalidResponse(
                "[prompt.invalid]Response required. Please try again."
            )
        return ret

    @classmethod
    def get_input(cls, *args, **kwargs) -> str:
        try:
            return super().get_input(*args, **kwargs)
        except (KeyboardInterrupt, EOFError):
            ui.error_write()
            raise

    def render_default(self, default):
        from rich.text import Text

        return Text(f"{default!s}", "green")

class SkippablePrompt(RequiredPrompt):
    skip_value: str = "n"

    def process_response(self, value: str):
        ret = super().process_response(value)
        return None if ret == self.skip_value else ret

    def make_prompt(self, default):
        prompt = self.prompt.copy()
        prompt.end = ""

        prompt.append(" [")
        if (
            default is not ...
            and self.show_default
            and isinstance(default, (str, self.response_type))
        ):
            _default = self.render_default(default)
            prompt.append(_default)
            prompt.append(", ")

        prompt.append(f"{self.skip_value} to omit", style="italic")
        prompt.append("]")
        prompt.append(self.prompt_suffix)
        return prompt

@post_processing(dict)
def _prompts(keys: Iterable[str], defaults: Dict[str, OptStr]):
    for key in keys:
        if key == "cmd":
            prompt_cls = RequiredPrompt
        else:
            prompt_cls = SkippablePrompt
        kwargs = {"default": defaults[key]} if key in defaults else {}
        prompt = PROMPTS[key]
        value = prompt_cls.ask(  # type: ignore[call-overload]
            prompt, console=ui.error_console, **kwargs
        )
        yield key, value

@contextmanager
def _disable_logging(highest_level=logging.CRITICAL):
    previous_level = logging.root.manager.disable

    logging.disable(highest_level)

    try:
        yield
    finally:
        logging.disable(previous_level)

PIPELINE_FILE_LINK = (
    "https://dvc.org/doc/user-guide/project-structure/pipelines-files"
)

def init_interactive(
    name: str,
    defaults: Dict[str, str],
    provided: Dict[str, str],
    show_tree: bool = False,
    live: bool = False,
) -> Dict[str, str]:
    primary = lremove(
        provided.keys(), ["cmd", "code", "data", "models", "params"]
    )
    secondary = lremove(
        provided.keys(), ["live"] if live else ["metrics", "plots"]
    )

    ret: Dict[str, str] = {}

    if not (primary or secondary):
        return ret

    message = ui.rich_text.assemble(
        "This command will guide you to set up a ",
        (name, "bright_blue"),
        " stage in ",
        ("dvc.yaml", "green"),
        ".",
    )
    doc_link = ui.rich_text.assemble(
        "See ", (PIPELINE_FILE_LINK, "repr.url"), "."
    )
    ui.error_write(message, doc_link, "", sep="\n", styled=True)

    if show_tree:
        from rich.tree import Tree

        tree = Tree(
            "DVC assumes the following workspace structure:",
            highlight=True,
        )
        workspace = {**defaults, **provided}
        workspace.pop("cmd", None)
        if not live and "live" not in provided:
            workspace.pop("live", None)
        for value in sorted(workspace.values()):
            tree.add(f"[green]{value}[/green]")
        ui.error_write(tree, styled=True)
        ui.error_write()

    ret.update(_prompts(primary, defaults))
    ret.update(_prompts(secondary, defaults))
    return compact(ret)

def _check_stage_exists(
    dvcfile: "DVCFile", name: str, force: bool = False
) -> None:
    if not force and dvcfile.exists() and name in dvcfile.stages:
        from dvc.stage.exceptions import DuplicateStageName

        hint = "Use '--force' to overwrite."
        raise DuplicateStageName(
            f"Stage '{name}' already exists in 'dvc.yaml'. {hint}"
        )

def init(
    repo: "Repo",
    name: str = None,
    type: str = "default",  # pylint: disable=redefined-builtin
    defaults: Dict[str, str] = None,
    overrides: Dict[str, str] = None,
    interactive: bool = False,
    force: bool = False,
) -> "Stage":
    from dvc.dvcfile import make_dvcfile

    dvcfile = make_dvcfile(repo, "dvc.yaml")
    name = name or type

    _check_stage_exists(dvcfile, name, force=force)

    defaults = defaults or {}
    overrides = overrides or {}

    with_live = type == "live"
    if interactive:
        defaults = init_interactive(
            name,
            defaults=defaults,
            live=with_live,
            provided=overrides,
            show_tree=True,
        )
    else:
        if with_live:
            # suppress `metrics`/`params` if live is selected, unless
            # it is also provided via overrides/cli.
            # This makes output to be a checkpoint as well.
            defaults.pop("metrics")
            defaults.pop("params")
        else:
            defaults.pop("live")  # suppress live otherwise

    context: Dict[str, str] = {**defaults, **overrides}
    assert "cmd" in context

    params_kv = []
    params = context.get("params")
    if params:
        from dvc.utils.serialize import LOADERS

        assert isinstance(params, str)
        _, ext = os.path.splitext(params)
        params_kv = [{params: list(LOADERS[ext](params))}]

    checkpoint_out = bool(context.get("live"))
    models = context.get("models")
    stage = repo.stage.create(
        name=name,
        cmd=context["cmd"],
        deps=compact([context.get("code"), context.get("data")]),
        params=params_kv,
        metrics_no_cache=compact([context.get("metrics")]),
        plots_no_cache=compact([context.get("plots")]),
        live=context.get("live"),
        force=force,
        **{"checkpoints" if checkpoint_out else "outs": compact([models])},
    )

    if interactive:
        ui.write(Rule(style="green"), styled=True)
        _yaml = dumps_yaml(to_pipeline_file(cast(PipelineStage, stage)))
        syn = Syntax(_yaml, "yaml", theme="ansi_dark")
        ui.error_write(syn, styled=True)

    if not interactive or ui.confirm(
        "Do you want to add the above contents to dvc.yaml?"
    ):
        scm = repo.scm
        with _disable_logging(), scm.track_file_changes(autostage=True):
            stage.dump(update_lock=False)
            stage.ignore_outs()
            scm.track_file(params)
    else:
        raise DvcException("Aborting ...")
    return stage
<commit_msg>exp init: make confirmation prompt at the end similar to others<commit_after>import logging
import os
from contextlib import contextmanager
from typing import TYPE_CHECKING, Dict, Iterable, cast

from funcy import compact, lremove, post_processing
from rich.prompt import Confirm, Prompt
from rich.rule import Rule
from rich.syntax import Syntax

from dvc.exceptions import DvcException
from dvc.stage import PipelineStage
from dvc.stage.serialize import to_pipeline_file
from dvc.types import OptStr
from dvc.utils.serialize import dumps_yaml

if TYPE_CHECKING:
    from dvc.repo import Repo
    from dvc.dvcfile import DVCFile
    from dvc.stage import Stage

from dvc.ui import ui

PROMPTS = {
    "cmd": "[b]Command[/b] to execute",
    "code": "Path to a [b]code[/b] file/directory",
    "data": "Path to a [b]data[/b] file/directory",
    "models": "Path to a [b]model[/b] file/directory",
    "params": "Path to a [b]parameters[/b] file",
    "metrics": "Path to a [b]metrics[/b] file",
    "plots": "Path to a [b]plots[/b] file/directory",
    "live": "Path to log [b]dvclive[/b] outputs",
}

class RichInputMixin:
    """Prevents exc message from printing in the same line on Ctrl + D/C."""

    @classmethod
    def get_input(cls, *args, **kwargs) -> str:
        try:
            return super().get_input(*args, **kwargs)  # type: ignore[misc]
        except (KeyboardInterrupt, EOFError):
            ui.error_write()
            raise

class ConfirmationPrompt(RichInputMixin, Confirm):
    def make_prompt(self, default):
        prompt = self.prompt.copy()
        prompt.end = ""

        prompt.append(" [")
        yes, no = self.choices
        for idx, (val, choice) in enumerate(((True, yes), (False, no))):
            if idx:
                prompt.append("/")
            if val == default:
                prompt.append(choice.upper(), "green")
            else:
                prompt.append(choice)
        prompt.append("] ")
        return prompt

class RequiredPrompt(RichInputMixin, Prompt):
    def process_response(self, value: str):
        from rich.prompt import InvalidResponse

        ret = super().process_response(value)
        if not ret:
            raise InvalidResponse(
                "[prompt.invalid]Response required. Please try again."
            )
        return ret

    def render_default(self, default):
        from rich.text import Text

        return Text(f"{default!s}", "green")

class SkippablePrompt(RequiredPrompt):
    skip_value: str = "n"

    def process_response(self, value: str):
        ret = super().process_response(value)
        return None if ret == self.skip_value else ret

    def make_prompt(self, default):
        prompt = self.prompt.copy()
        prompt.end = ""

        prompt.append(" [")
        if (
            default is not ...
            and self.show_default
            and isinstance(default, (str, self.response_type))
        ):
            _default = self.render_default(default)
            prompt.append(_default)
            prompt.append(", ")

        prompt.append(f"{self.skip_value} to omit", style="italic")
        prompt.append("]")
        prompt.append(self.prompt_suffix)
        return prompt

@post_processing(dict)
def _prompts(keys: Iterable[str], defaults: Dict[str, OptStr]):
    for key in keys:
        if key == "cmd":
            prompt_cls = RequiredPrompt
        else:
            prompt_cls = SkippablePrompt
        kwargs = {"default": defaults[key]} if key in defaults else {}
        prompt = PROMPTS[key]
        value = prompt_cls.ask(  # type: ignore[call-overload]
            prompt, console=ui.error_console, **kwargs
        )
        yield key, value

@contextmanager
def _disable_logging(highest_level=logging.CRITICAL):
    previous_level = logging.root.manager.disable

    logging.disable(highest_level)

    try:
        yield
    finally:
        logging.disable(previous_level)

PIPELINE_FILE_LINK = (
    "https://dvc.org/doc/user-guide/project-structure/pipelines-files"
)

def init_interactive(
    name: str,
    defaults: Dict[str, str],
    provided: Dict[str, str],
    show_tree: bool = False,
    live: bool = False,
) -> Dict[str, str]:
    primary = lremove(
        provided.keys(), ["cmd", "code", "data", "models", "params"]
    )
    secondary = lremove(
        provided.keys(), ["live"] if live else ["metrics", "plots"]
    )

    ret: Dict[str, str] = {}

    if not (primary or secondary):
        return ret

    message = ui.rich_text.assemble(
        "This command will guide you to set up a ",
        (name, "bright_blue"),
        " stage in ",
        ("dvc.yaml", "green"),
        ".",
    )
    doc_link = ui.rich_text.assemble(
        "See ", (PIPELINE_FILE_LINK, "repr.url"), "."
    )
    ui.error_write(message, doc_link, "", sep="\n", styled=True)

    if show_tree:
        from rich.tree import Tree

        tree = Tree(
            "DVC assumes the following workspace structure:",
            highlight=True,
        )
        workspace = {**defaults, **provided}
        workspace.pop("cmd", None)
        if not live and "live" not in provided:
            workspace.pop("live", None)
        for value in sorted(workspace.values()):
            tree.add(f"[green]{value}[/green]")
        ui.error_write(tree, styled=True)
        ui.error_write()

    ret.update(_prompts(primary, defaults))
    ret.update(_prompts(secondary, defaults))
    return compact(ret)

def _check_stage_exists(
    dvcfile: "DVCFile", name: str, force: bool = False
) -> None:
    if not force and dvcfile.exists() and name in dvcfile.stages:
        from dvc.stage.exceptions import DuplicateStageName

        hint = "Use '--force' to overwrite."
        raise DuplicateStageName(
            f"Stage '{name}' already exists in 'dvc.yaml'. {hint}"
        )

def init(
    repo: "Repo",
    name: str = None,
    type: str = "default",  # pylint: disable=redefined-builtin
    defaults: Dict[str, str] = None,
    overrides: Dict[str, str] = None,
    interactive: bool = False,
    force: bool = False,
) -> "Stage":
    from dvc.dvcfile import make_dvcfile

    dvcfile = make_dvcfile(repo, "dvc.yaml")
    name = name or type

    _check_stage_exists(dvcfile, name, force=force)

    defaults = defaults or {}
    overrides = overrides or {}

    with_live = type == "live"
    if interactive:
        defaults = init_interactive(
            name,
            defaults=defaults,
            live=with_live,
            provided=overrides,
            show_tree=True,
        )
    else:
        if with_live:
            # suppress `metrics`/`params` if live is selected, unless
            # it is also provided via overrides/cli.
            # This makes output to be a checkpoint as well.
            defaults.pop("metrics")
            defaults.pop("params")
        else:
            defaults.pop("live")  # suppress live otherwise

    context: Dict[str, str] = {**defaults, **overrides}
    assert "cmd" in context

    params_kv = []
    params = context.get("params")
    if params:
        from dvc.utils.serialize import LOADERS

        assert isinstance(params, str)
        _, ext = os.path.splitext(params)
        params_kv = [{params: list(LOADERS[ext](params))}]

    checkpoint_out = bool(context.get("live"))
    models = context.get("models")
    stage = repo.stage.create(
        name=name,
        cmd=context["cmd"],
        deps=compact([context.get("code"), context.get("data")]),
        params=params_kv,
        metrics_no_cache=compact([context.get("metrics")]),
        plots_no_cache=compact([context.get("plots")]),
        live=context.get("live"),
        force=force,
        **{"checkpoints" if checkpoint_out else "outs": compact([models])},
    )

    if interactive:
        ui.write(Rule(style="green"), styled=True)
        _yaml = dumps_yaml(to_pipeline_file(cast(PipelineStage, stage)))
        syn = Syntax(_yaml, "yaml", theme="ansi_dark")
        ui.error_write(syn, styled=True)

    if not interactive or ConfirmationPrompt.ask(
        "Do you want to add the above contents to dvc.yaml?",
        console=ui.error_console,
        default=True,
    ):
        scm = repo.scm
        with _disable_logging(), scm.track_file_changes(autostage=True):
            stage.dump(update_lock=False)
            stage.ignore_outs()
            scm.track_file(params)
    else:
        raise DvcException("Aborting ...")
    return stage
<|endoftext|>


<commit_before># -*- utf-8 -*-
from __future__ import print_function

import os.path

import pkg_resources

import cedict
import rules
from epitran.ligaturize import ligaturize

class Normalizer(object):
    punc = [(u'\uff0c', u','),
            (u'\uff01', u'!'),
            (u'\uff1f', u'?'),
            (u'\uff1b', u';'),
            (u'\uff1a', u':'),
            (u'\uff08', u'('),
            (u'\uff09', u')'),
            (u'\uff3b', u'['),
            (u'\uff3d', u']'),
            (u'\u3010', u'['),
            (u'\u3011', u']'),
            ]
    puncnorm_vals = dict(punc).values()

    def normalize_punc(self, text):
        for a, b in self.punc:
            text = text.replace(a, b)
        return text

class Epihan(Normalizer):
    def __init__(self, ligatures=False, cedict_file=None, rules_file='pinyin-to-ipa.txt'):
        rules_file = os.path.join('data', rules_file)
        rules_file = pkg_resources.resource_filename(__name__, rules_file)
        self.cedict = cedict.CEDictTrie(cedict_file)
        self.rules = rules.Rules([rules_file])

    def transliterate(self, text, normpunc=False, ligatures=False):
        tokens = self.cedict.tokenize(text)
        ipa_tokens = []
        for token in tokens:
            if token in self.cedict.hanzi:
                (pinyin, _) = self.cedict.hanzi[token]
                pinyin = u''.join(pinyin).lower()
                ipa = self.rules.apply(pinyin)
                ipa_tokens.append(ipa.replace(u',', u''))
            else:
                if normpunc:
                    token = self.normalize_punc(token)
                ipa_tokens.append(token)
            ipa_tokens = map(ligaturize, ipa_tokens) if ligatures else ipa_tokens
        return u''.join(ipa_tokens)
<commit_msg>Added exception when CEDict not specified<commit_after># -*- utf-8 -*-
from __future__ import print_function

import os.path

import pkg_resources

import cedict
import rules
from epitran.ligaturize import ligaturize

class MissingData(Exception):
    pass

class Normalizer(object):
    punc = [(u'\uff0c', u','),
            (u'\uff01', u'!'),
            (u'\uff1f', u'?'),
            (u'\uff1b', u';'),
            (u'\uff1a', u':'),
            (u'\uff08', u'('),
            (u'\uff09', u')'),
            (u'\uff3b', u'['),
            (u'\uff3d', u']'),
            (u'\u3010', u'['),
            (u'\u3011', u']'),
            ]
    puncnorm_vals = dict(punc).values()

    def normalize_punc(self, text):
        for a, b in self.punc:
            text = text.replace(a, b)
        return text

class Epihan(Normalizer):
    def __init__(self, ligatures=False, cedict_file=None, rules_file='pinyin-to-ipa.txt'):
        if not cedict_file:
            raise MissingData('Please specify a location for the CC-CEDict file.')
        rules_file = os.path.join('data', 'rules', rules_file)
        rules_file = pkg_resources.resource_filename(__name__, rules_file)
        self.cedict = cedict.CEDictTrie(cedict_file)
        self.rules = rules.Rules([rules_file])

    def transliterate(self, text, normpunc=False, ligatures=False):
        tokens = self.cedict.tokenize(text)
        ipa_tokens = []
        for token in tokens:
            if token in self.cedict.hanzi:
                (pinyin, _) = self.cedict.hanzi[token]
                pinyin = u''.join(pinyin).lower()
                ipa = self.rules.apply(pinyin)
                ipa_tokens.append(ipa.replace(u',', u''))
            else:
                if normpunc:
                    token = self.normalize_punc(token)
                ipa_tokens.append(token)
            ipa_tokens = map(ligaturize, ipa_tokens) if ligatures else ipa_tokens
        return u''.join(ipa_tokens)
<|endoftext|>


<commit_before># This file is part of BenchExec, a framework for reliable benchmarking:
# https://github.com/sosy-lab/benchexec
#
# SPDX-FileCopyrightText: 2007-2020 Dirk Beyer <https://www.sosy-lab.org>
#
# SPDX-License-Identifier: Apache-2.0

import benchexec.result as result
import benchexec.tools.template

class Tool(benchexec.tools.template.BaseTool2):
    """
    Tool info for the witness checker (witnesslint)
    (https://github.com/sosy-lab/sv-witnesses)
    """

    def executable(self, tool_locator):
        return tool_locator.find_executable("witnesslint.py")

    def name(self):
        return "witnesslint"

    def determine_result(self, run):
        if run.exit_code.value == 0:
            return result.RESULT_TRUE_PROP
        else:
            return result.RESULT_FALSE_PROP
<commit_msg>Add version and distinguish linter error from faulty witness.<commit_after># This file is part of BenchExec, a framework for reliable benchmarking:
# https://github.com/sosy-lab/benchexec
#
# SPDX-FileCopyrightText: 2007-2020 Dirk Beyer <https://www.sosy-lab.org>
#
# SPDX-License-Identifier: Apache-2.0

import benchexec.result as result
import benchexec.tools.template

class Tool(benchexec.tools.template.BaseTool2):
    """
    Tool info for the witness checker (witnesslint)
    (https://github.com/sosy-lab/sv-witnesses)
    """

    def executable(self, tool_locator):
        return tool_locator.find_executable("witnesslint.py")

    def name(self):
        return "witnesslint"

    def version(self, executable):
        version_string = self._version_from_tool(executable)
        return version_string.partition("version")[2].strip().split(" ")[0]

    def determine_result(self, run):
        if run.exit_code.value == 0:
            return result.RESULT_TRUE_PROP
        elif run.exit_code.value == 1:
            return result.RESULT_FALSE_PROP
        else:
            return result.RESULT_ERROR
<|endoftext|>


<commit_before># This is a python copy of the original five.js
# Found at https://github.com/jackdcrawford/five.git

def five():
    print 5

def roman():
    print 'V'
<commit_msg>Delete five.py<commit_after><|endoftext|>