<commit_before>from shutil import which

from coalib.bearlib.abstractions.Linter import linter
from coalib.bearlib import deprecate_settings
from coala_utils.param_conversion import negate

@linter("bash", output_format="regex",
        output_regex=r'.+:(?P<line>.+):(?P<message>.*)')
class JavaPMDBear:
    """
    Check Java code for possible issues like potential bugs, dead code or too
    complicated expressions.

    More information is available at
    <http://pmd.github.io/pmd-5.4.1/pmd-java/rules/index.html>.
    """

    LANGUAGES = {"Java"}
    AUTHORS = {'The coala developers'}
    AUTHORS_EMAILS = {'coala-devel@googlegroups.com'}
    LICENSE = 'AGPL-3.0'
    CAN_DETECT = {'Code Simplification', 'Unreachable Code', 'Smell',
                  'Duplication'}

    @classmethod
    def check_prerequisites(cls):  # pragma: no cover
        if which("bash") is None:
            return "bash is not installed."
        elif which("run.sh") is None:
            return ("PMD is missing. Make sure to install it from "
                    "<https://pmd.github.io/>")
        else:
            return True

    @staticmethod
    @deprecate_settings(allow_unnecessary_code=('check_unnecessary', negate),
                        allow_unused_code=('check_unused', negate))
    def create_arguments(filename, file, config_file,
                         check_best_practices: bool = True,
                         check_braces: bool = True,
                         check_clone_implementation: bool = True,
                         check_code_size: bool = True,
                         check_comments: bool = False,
                         check_controversial: bool = False,
                         check_design: bool = False,
                         check_imports: bool = True, check_naming: bool = True,
                         check_optimizations: bool = False,
                         check_strings: bool = False,
                         allow_unnecessary_code: bool = False,
                         allow_unused_code: bool = False):
        """
        :param check_best_practices:
            Checks for best practices.
        :param check_braces:
            Checks for the right use of braces.
        :param check_clone_implementation:
            Checks for the right implementation of the ``clone()`` function.
        :param check_code_size:
            Checks for large or complicated code constructs.
        :param check_comments:
            Checks comments for length, content and placement.
        :param check_controversial:
            Does various checks that are considered controversial.
        :param check_design:
            Checks for optimal code implementations. Alternate approaches
            are suggested.
        :param check_imports:
            Checks for duplicate and unused imports.
        :param check_naming:
            Checks the names of identifiers against some rules.
        :param check_optimizations:
            Checks for best pratices regarding optimization.
        :param check_strings:
            Checks for String, StringBuffer and StringBuilder instances.
        :param allow_unnecessary_code:
            Allows unnecessary code.
        :param allow_unused_code:
            Allows unused code.
        """
        options = {
            "java-basic": check_best_practices,
            "java-braces": check_braces,
            "java-clone": check_clone_implementation,
            "java-codesize": check_code_size,
            "java-comments": check_comments,
            "java-controversial": check_controversial,
            "java-design": check_design,
            "java-imports": check_imports,
            "java-naming": check_naming,
            "java-optimizations": check_optimizations,
            "java-strings": check_strings,
            "java-unnecessary": not allow_unnecessary_code,
            "java-unusedcode": not allow_unused_code}
        rules = ','.join(key for key in options if options[key])

        return which("run.sh"), "pmd", "-R", rules, "-d", filename
<commit_msg>JavaPMDBear: Add Mac support<commit_after>from shutil import which

from coalib.bearlib.abstractions.Linter import linter
from coalib.bearlib import deprecate_settings
from coala_utils.param_conversion import negate

@linter("bash", output_format="regex",
        output_regex=r'.+:(?P<line>.+):(?P<message>.*)')
class JavaPMDBear:
    """
    Check Java code for possible issues like potential bugs, dead code or too
    complicated expressions.

    More information is available at
    <http://pmd.github.io/pmd-5.4.1/pmd-java/rules/index.html>.
    """

    LANGUAGES = {"Java"}
    AUTHORS = {'The coala developers'}
    AUTHORS_EMAILS = {'coala-devel@googlegroups.com'}
    LICENSE = 'AGPL-3.0'
    CAN_DETECT = {'Code Simplification', 'Unreachable Code', 'Smell',
                  'Duplication'}

    @classmethod
    def check_prerequisites(cls):  # pragma: no cover
        if which("bash") is None:
            return "bash is not installed."
        elif which("pmd") is None and which("run.sh") is None:
            return ("PMD is missing. Make sure to install it from "
                    "<https://pmd.github.io/>")
        else:
            return True

    @staticmethod
    @deprecate_settings(allow_unnecessary_code=('check_unnecessary', negate),
                        allow_unused_code=('check_unused', negate))
    def create_arguments(filename, file, config_file,
                         check_best_practices: bool = True,
                         check_braces: bool = True,
                         check_clone_implementation: bool = True,
                         check_code_size: bool = True,
                         check_comments: bool = False,
                         check_controversial: bool = False,
                         check_design: bool = False,
                         check_imports: bool = True, check_naming: bool = True,
                         check_optimizations: bool = False,
                         check_strings: bool = False,
                         allow_unnecessary_code: bool = False,
                         allow_unused_code: bool = False):
        """
        :param check_best_practices:
            Checks for best practices.
        :param check_braces:
            Checks for the right use of braces.
        :param check_clone_implementation:
            Checks for the right implementation of the ``clone()`` function.
        :param check_code_size:
            Checks for large or complicated code constructs.
        :param check_comments:
            Checks comments for length, content and placement.
        :param check_controversial:
            Does various checks that are considered controversial.
        :param check_design:
            Checks for optimal code implementations. Alternate approaches
            are suggested.
        :param check_imports:
            Checks for duplicate and unused imports.
        :param check_naming:
            Checks the names of identifiers against some rules.
        :param check_optimizations:
            Checks for best pratices regarding optimization.
        :param check_strings:
            Checks for String, StringBuffer and StringBuilder instances.
        :param allow_unnecessary_code:
            Allows unnecessary code.
        :param allow_unused_code:
            Allows unused code.
        """
        options = {
            "java-basic": check_best_practices,
            "java-braces": check_braces,
            "java-clone": check_clone_implementation,
            "java-codesize": check_code_size,
            "java-comments": check_comments,
            "java-controversial": check_controversial,
            "java-design": check_design,
            "java-imports": check_imports,
            "java-naming": check_naming,
            "java-optimizations": check_optimizations,
            "java-strings": check_strings,
            "java-unnecessary": not allow_unnecessary_code,
            "java-unusedcode": not allow_unused_code}
        rules = ','.join(key for key in options if options[key])

        executable = which("pmd") or which("run.sh")  # Mac vs. Unix
        return executable, "pmd", "-R", rules, "-d", filename
<|endoftext|>


<commit_before># Copyright (c) 2014 Thomas Scholtes

# 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.

import os.path
import logging
import threading
from argparse import ArgumentParser
from concurrent import futures

import beets
from beets import util
from beets.plugins import BeetsPlugin
from beets.ui import Subcommand, get_path_formats, input_yn, UserError
from beets.library import get_query_sort, Item
from beets.util import syspath, displayable_path

from beetsplug import convert

log = logging.getLogger('beets.alternatives')

class AlternativesPlugin(BeetsPlugin):

    def __init__(self):
        super(AlternativesPlugin, self).__init__()

    def commands(self):
        return [AlternativesCommand(self)]

    def update(self, lib, options):
        try:
            alt = self.alternative(options.name, lib)
        except KeyError as e:
            raise UserError("Alternative collection '{0}' not found."
                            .format(e.args[0]))
        alt.update(create=options.create)

    def alternative(self, name, lib):
        conf = self.config['external'][name]
        if conf.exists():
            if conf['format'].exists():
                return ExternalConvert(name, conf['format'].get(unicode),
                                       lib, conf)
            else:
                return External(name, lib, conf)

        raise KeyError(name)

class AlternativesCommand(Subcommand):

    name = 'alt'
    help = 'manage alternative files'

    def __init__(self, plugin):
        parser = ArgumentParser()
        subparsers = parser.add_subparsers()
        update = subparsers.add_parser('update')
        update.set_defaults(func=plugin.update)
        update.add_argument('name')
        update.add_argument('--create', action='store_const',
                            dest='create', const=True)
        update.add_argument('--no-create', action='store_const',
                            dest='create', const=False)
        super(AlternativesCommand, self).__init__(self.name, parser)

    def func(self, lib, opts, _):
        opts.func(lib, opts)

    def parse_args(self, args):
        return self.parser.parse_args(args), []

class External(object):

    ADD = 1
    REMOVE = 2
    WRITE = 3
    MOVE = 4

    def __init__(self, name, lib, config):
        self.lib = lib
        self.path_key = 'alt.{0}'.format(name)
        if config['paths'].exists():
            path_config = config['paths']
        else:
            path_config = beets.config['paths']
        self.path_formats = get_path_formats(path_config)
        self.query, _ = get_query_sort(config['query'].get(unicode), Item)

        self.removable = config.get(dict).get('removable', True)

        dir = config['directory'].as_filename()
        if not os.path.isabs(dir):
            dir = os.path.join(lib.directory, dir)
        self.directory = dir

    def items_action(self, items):
        for item in items:
            path = item.get(self.path_key)
            if self.query.match(item):
                if path:
                    dest = self.destination(item)
                    if path != dest:
                        yield (item, self.MOVE)
                    elif (os.path.getmtime(syspath(dest))
                          < os.path.getmtime(syspath(item.path))):
                        yield (item, self.WRITE)
                else:
                    yield (item, self.ADD)
            elif path:
                yield (item, self.REMOVE)

    def ask_create(self, create=None):
        if not self.removable:
            return True
        if create is not None:
            return create

        msg = "Collection at '{0}' does not exists. " \
              "Maybe you forgot to mount it.\n" \
              "Do you want to create the collection?" \
              .format(displayable_path(self.directory))
        return input_yn(msg, require=True)

    def update(self, create=None):
        if not os.path.isdir(self.directory) and not self.ask_create(create):
            print('Skipping creation of {0}'
                  .format(displayable_path(self.directory)))
            return

        converter = self.converter()
        for (item, action) in self.items_action(self.lib.items()):
            dest = self.destination(item)
            path = item.get(self.path_key)
            if action == self.MOVE:
                print('>{0} -> {1}'.format(path, dest))
                util.mkdirall(dest)
                util.move(path, dest)
                item[self.path_key] = dest
                item.store()
            elif action == self.WRITE:
                print('*{0}'.format(path))
                item.write(path=path)
            elif action == self.ADD:
                print('+{0}'.format(dest))
                converter.submit(item)
            elif action == self.REMOVE:
                print('-{0}'.format(self.destination(item)))
                util.remove(path)
                util.prune_dirs(path)
                del item[self.path_key]
                item.store()

        for item, dest in converter.as_completed():
            item[self.path_key] = dest
            item.store()
        converter.shutdown()

    def destination(self, item):
        return item.destination(basedir=self.directory,
                                path_formats=self.path_formats)

    def converter(self):
        def _convert(item):
            dest = self.destination(item)
            util.mkdirall(dest)
            util.copy(item.path, dest)
            return item, dest
        return Worker(_convert)

class ExternalConvert(External):

    def __init__(self, name, format, lib, config):
        super(ExternalConvert, self).__init__(name, lib, config)
        self.format = format
        self.convert_cmd, self.ext = convert.get_format(self.format)

    def converter(self):
        command, ext = convert.get_format(self.format)
        fs_lock = threading.Lock()

        def _convert(item):
            dest = self.destination(item)
            with fs_lock:
                util.mkdirall(dest)

            if not self.format or self.format.lower() == item.format.lower():
                util.copy(item.path, dest)
            else:
                convert.encode(command, item.path, dest)
            return item, dest
        return Worker(_convert)

    def destination(self, item):
        dest = super(ExternalConvert, self).destination(item)
        return os.path.splitext(dest)[0] + '.' + self.ext

class Worker(futures.ThreadPoolExecutor):

    def __init__(self, fn, max_workers=None):
        super(Worker, self).__init__(max_workers=1)
        self._tasks = set()
        self._fn = fn

    def submit(self, *args, **kwargs):
        fut = super(Worker, self).submit(self._fn, *args, **kwargs)
        self._tasks.add(fut)
        return fut

    def as_completed(self):
        for f in futures.as_completed(self._tasks):
            self._tasks.remove(f)
            yield f.result()
<commit_msg>Add command help line<commit_after># Copyright (c) 2014 Thomas Scholtes

# 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.

import os.path
import logging
import threading
from argparse import ArgumentParser
from concurrent import futures

import beets
from beets import util
from beets.plugins import BeetsPlugin
from beets.ui import Subcommand, get_path_formats, input_yn, UserError
from beets.library import get_query_sort, Item
from beets.util import syspath, displayable_path

from beetsplug import convert

log = logging.getLogger('beets.alternatives')

class AlternativesPlugin(BeetsPlugin):

    def __init__(self):
        super(AlternativesPlugin, self).__init__()

    def commands(self):
        return [AlternativesCommand(self)]

    def update(self, lib, options):
        try:
            alt = self.alternative(options.name, lib)
        except KeyError as e:
            raise UserError("Alternative collection '{0}' not found."
                            .format(e.args[0]))
        alt.update(create=options.create)

    def alternative(self, name, lib):
        conf = self.config['external'][name]
        if conf.exists():
            if conf['format'].exists():
                return ExternalConvert(name, conf['format'].get(unicode),
                                       lib, conf)
            else:
                return External(name, lib, conf)

        raise KeyError(name)

class AlternativesCommand(Subcommand):

    name = 'alt'
    help = 'manage alternative files'

    def __init__(self, plugin):
        parser = ArgumentParser()
        subparsers = parser.add_subparsers()
        update = subparsers.add_parser('update')
        update.set_defaults(func=plugin.update)
        update.add_argument('name')
        update.add_argument('--create', action='store_const',
                            dest='create', const=True)
        update.add_argument('--no-create', action='store_const',
                            dest='create', const=False)
        super(AlternativesCommand, self).__init__(self.name, parser, self.help)

    def func(self, lib, opts, _):
        opts.func(lib, opts)

    def parse_args(self, args):
        return self.parser.parse_args(args), []

class External(object):

    ADD = 1
    REMOVE = 2
    WRITE = 3
    MOVE = 4

    def __init__(self, name, lib, config):
        self.lib = lib
        self.path_key = 'alt.{0}'.format(name)
        if config['paths'].exists():
            path_config = config['paths']
        else:
            path_config = beets.config['paths']
        self.path_formats = get_path_formats(path_config)
        self.query, _ = get_query_sort(config['query'].get(unicode), Item)

        self.removable = config.get(dict).get('removable', True)

        dir = config['directory'].as_filename()
        if not os.path.isabs(dir):
            dir = os.path.join(lib.directory, dir)
        self.directory = dir

    def items_action(self, items):
        for item in items:
            path = item.get(self.path_key)
            if self.query.match(item):
                if path:
                    dest = self.destination(item)
                    if path != dest:
                        yield (item, self.MOVE)
                    elif (os.path.getmtime(syspath(dest))
                          < os.path.getmtime(syspath(item.path))):
                        yield (item, self.WRITE)
                else:
                    yield (item, self.ADD)
            elif path:
                yield (item, self.REMOVE)

    def ask_create(self, create=None):
        if not self.removable:
            return True
        if create is not None:
            return create

        msg = "Collection at '{0}' does not exists. " \
              "Maybe you forgot to mount it.\n" \
              "Do you want to create the collection?" \
              .format(displayable_path(self.directory))
        return input_yn(msg, require=True)

    def update(self, create=None):
        if not os.path.isdir(self.directory) and not self.ask_create(create):
            print('Skipping creation of {0}'
                  .format(displayable_path(self.directory)))
            return

        converter = self.converter()
        for (item, action) in self.items_action(self.lib.items()):
            dest = self.destination(item)
            path = item.get(self.path_key)
            if action == self.MOVE:
                print('>{0} -> {1}'.format(path, dest))
                util.mkdirall(dest)
                util.move(path, dest)
                item[self.path_key] = dest
                item.store()
            elif action == self.WRITE:
                print('*{0}'.format(path))
                item.write(path=path)
            elif action == self.ADD:
                print('+{0}'.format(dest))
                converter.submit(item)
            elif action == self.REMOVE:
                print('-{0}'.format(self.destination(item)))
                util.remove(path)
                util.prune_dirs(path)
                del item[self.path_key]
                item.store()

        for item, dest in converter.as_completed():
            item[self.path_key] = dest
            item.store()
        converter.shutdown()

    def destination(self, item):
        return item.destination(basedir=self.directory,
                                path_formats=self.path_formats)

    def converter(self):
        def _convert(item):
            dest = self.destination(item)
            util.mkdirall(dest)
            util.copy(item.path, dest)
            return item, dest
        return Worker(_convert)

class ExternalConvert(External):

    def __init__(self, name, format, lib, config):
        super(ExternalConvert, self).__init__(name, lib, config)
        self.format = format
        self.convert_cmd, self.ext = convert.get_format(self.format)

    def converter(self):
        command, ext = convert.get_format(self.format)
        fs_lock = threading.Lock()

        def _convert(item):
            dest = self.destination(item)
            with fs_lock:
                util.mkdirall(dest)

            if not self.format or self.format.lower() == item.format.lower():
                util.copy(item.path, dest)
            else:
                convert.encode(command, item.path, dest)
            return item, dest
        return Worker(_convert)

    def destination(self, item):
        dest = super(ExternalConvert, self).destination(item)
        return os.path.splitext(dest)[0] + '.' + self.ext

class Worker(futures.ThreadPoolExecutor):

    def __init__(self, fn, max_workers=None):
        super(Worker, self).__init__(max_workers=1)
        self._tasks = set()
        self._fn = fn

    def submit(self, *args, **kwargs):
        fut = super(Worker, self).submit(self._fn, *args, **kwargs)
        self._tasks.add(fut)
        return fut

    def as_completed(self):
        for f in futures.as_completed(self._tasks):
            self._tasks.remove(f)
            yield f.result()
<|endoftext|>


<commit_before>"""Admin classes for the dated_values app."""
from django.contrib import admin

from .models import DatedValue, DatedValueType

admin.site.register(DatedValue)
admin.site.register(DatedValueType)
<commit_msg>Made admin for DatedValueType be translatable.<commit_after>"""Admin classes for the dated_values app."""
from django.contrib import admin

from hvad.admin import TranslatableAdmin

from .models import DatedValue, DatedValueType

admin.site.register(DatedValue)
admin.site.register(DatedValueType, TranslatableAdmin)
<|endoftext|>


<commit_before>import ipdb
import sys
import pickle

import jnius_config
jnius_config.add_options('-Xmx4g')
from jnius import autoclass, JavaException
from jnius import cast

from belpy.databases import hgnc_client
from belpy.statements import *

# Functions for accessing frequently used java classes with shortened path
def bp(path):
    prefix = 'org.biopax.paxtools.model.level3'
    classname = prefix + '.' + path
    return autoclass_robust(classname)

def bpp(path):
    prefix = 'org.biopax.paxtools.pattern'
    classname = prefix + '.' + path
    return autoclass_robust(classname)

def bpimpl(path):
    prefix = 'org.biopax.paxtools.impl.level3'
    postfix = 'Impl'
    classname = prefix + '.' + path + postfix
    return autoclass_robust(classname)

def autoclass_robust(path):
    try:
        cl = autoclass(path)
    except JavaException:
        print 'Could not instantiate ' + path
        return None
    return cl

def cast_biopax_element(bpe):
    """ Casts a generic BioPAXElement object into a specific type.
    This is useful when a search only returns generic elements. """
    return cast(bpe.getModelInterface().getName(), bpe)

def match_to_array(m):
    """ Returns an array consisting of the elements obtained from a pattern
    search cast into their appropriate classes. """
    return [cast_biopax_element(m.get(i)) for i in range(m.varSize())]

class BiopaxProcessor(object):
    def __init__(self, model):
        self.model = model
        self.belpy_stmts = []
        self._hgnc_cache = self._load_hgnc_cache()

    def get_complexes(self, force_contains=[]):
        pb = bpp('PatternBox')
        s = bpp('Searcher')
        p = pb.inComplexWith()
        res = s.searchPlain(self.model, p)
        res_array = [match_to_array(m) for m in res.toArray()]
        print '%d results found' % res.size()
        for r in res_array:
            members = []
            # Extract first member
            members += self._get_entity_names(r[p.indexOf('Protein 1')])
            # Extract second member
            members += self._get_entity_names(r[p.indexOf('Protein 2')])
            # Skip elements where some pre-specified species
            # are not in the complex for f in force_contains
            skip = False
            for f in force_contains:
                if f not in members:
                    skip = True
            if skip:
                continue

            cplx = r[p.indexOf('Complex')]
            print cplx.getRDFId()

            # TODO: we can't handle modification features in
            # BelPy Complexes yet
            # Modifications of first member
            # feat_1 = r[1].getFeature().toArray()
            # Modifications of second member
            # feat_2 = r[3].getFeature().toArray()
            self.belpy_stmts.append(Complex(members))

    def get_phosphorylation(self):
        stmts = self._get_generic_modification('phospho')
        for s in stmts:
            self.belpy_stmts.append(Phosphorylation(*s))

    def get_dephosphorylation(self):
        stmts = self._get_generic_modification('phospho', mod_gain=False)
        for s in stmts:
            self.belpy_stmts.append(Dephosphorylation(*s))

    def get_acetylation(self):
        stmts = self._get_generic_modification('acetyl')
        for s in stmts:
            self.belpy_stmts.append(Acetylation(*s))

    def get_glycosylation(self):
        stmts = self._get_generic_modification('glycosyl')
        for s in stmts:
            self.belpy_stmts.append(Glycosylation(*s))

    def get_palmitoylation(self):
        stmts = self._get_generic_modification('palmitoyl')
        for s in stmts:
            self.belpy_stmts.append(Palmitoylation(*s))

    def get_activity_modification(self):
        mcc = bpp('constraint.ModificationChangeConstraint')
        mcct = bpp('constraint.ModificationChangeConstraint$Type')
        p = self._construct_modification_pattern()
        mod_filter = 'residue modification, active'
        p.add(mcc(mcct.GAIN, mod_filter),
              "input simple PE", "output simple PE")

        s = bpp('Searcher')
        res = s.searchPlain(self.model, p)
        res_array = [match_to_array(m) for m in res.toArray()]
        print '%d results found' % res.size()
        for r in res_array:
            monomer_name = self._get_entity_names(r[p.indexOf('changed generic ER')])
            stmt_str = ''
            citation = self._get_citation(r[p.indexOf('Conversion')])
            evidence = ''
            annotations = ''
            outPE = r[p.indexOf('output PE')]
            activity = 'Activity'
            relationship = 'DirectlyIncreases'
            mod, mod_pos = self._get_modification_site(outPE)
            if mod:
                stmt = ActivityModification(monomer_name, mod, mod_pos,
                                            relationship, activity,
                                            stmt_str, citation, evidence,
                                            annotations)
                self.belpy_stmts.append(stmt)

    def _get_modification_site(self, modPE):
        # Do we need to look at EntityFeatures?
        modMF = [mf for mf in modPE.getFeature().toArray()
                 if isinstance(mf, bpimpl('ModificationFeature'))]
        mod_pos = []
        mod = []

        for mf in modMF:
            # ModificationFeature / SequenceModificationVocabulary
            mf_type = mf.getModificationType().getTerm().toArray()[0]
            if len(mf.getModificationType().getTerm().toArray()) != 1:
                warnings.warn('Other than one modification term')
            try:
                mod_type = self._mftype_dict[mf_type]
            except KeyError:
                warnings.warn('Unknown modification type %s' % mf_type)
                continue

            mod.append(mod_type)

            # getFeatureLocation returns SequenceLocation, which is the
            # generic parent class of SequenceSite and SequenceInterval.
            # Here we need to cast to SequenceSite in order to get to
            # the sequence position.
            mf_pos = mf.getFeatureLocation()
            if mf_pos is not None:
                mf_site = cast(bp('SequenceSite'), mf_pos)
                mf_pos_status = mf_site.getPositionStatus()
                if mf_pos_status and mf_pos_status.toString() != 'EQUAL':
                    warnings.warn('Modification site position is %s' %
                                  mf_pos_status.toString())
                mod_pos.append(mf_site.getSequencePosition())
            else:
                mod_pos.append(None)
            # Do we need to look at mf.getFeatureLocationType()?
            # It seems to be always None.
            # mf_pos_type = mf.getFeatureLocationType()
        return mod, mod_pos

    def _get_generic_modification(self, mod_filter=None, mod_gain=True):
        mcc = bpp('constraint.ModificationChangeConstraint')
        mcct = bpp('constraint.ModificationChangeConstraint$Type')
        # Start with a generic modification pattern
        p = self._construct_modification_pattern()
        # The modification type should contain the filter string
        if mod_filter is not None:
            if mod_gain:
                mod_gain_const = mcct.GAIN
            else:
                mod_gain_const = mcct.LOSS
            p.add(mcc(mod_gain_const, mod_filter),
                      "input simple PE", "output simple PE")

        s = bpp('Searcher')
        res = s.searchPlain(self.model, p)
        res_array = [match_to_array(m) for m in res.toArray()]
        print '%d results found' % res.size()
        stmts = []
        for r in res_array:
            enz_name = self._get_entity_names(r[p.indexOf('controller ER')])[0]
            sub_name = self._get_entity_names(r[p.indexOf('changed generic ER')])[0]
            stmt_str = ''
            citation = self._get_citation(r[p.indexOf('Conversion')])
            evidence = ''
            annotations = ''

            # Get the modification (s)
            # Should this be simple PE?
            if mod_gain:
                modPE = r[p.indexOf('output simple PE')]
            else:
                modPE = r[p.indexOf('input simple PE')]

            # TODO: handle case when
            # r[p.indexOf('output simple PE')].getRDFId()
            # is not equal to r[p.indexOf('output PE')].getRDFId()
            mod, mod_pos = self._get_modification_site(modPE)
            stmts.append((enz_name, sub_name, mod, mod_pos,
                          stmt_str, citation, evidence, annotations))
        return stmts

    def print_statements(self):
        for i, stmt in enumerate(self.belpy_stmts):
            print "%s: %s" % (i, stmt)

    def _get_citation(self, bpe):
        evidence = bpe.getEvidence().toArray()
        refs = []
        for e in evidence:
            pub = e.getXref().toArray()
            for p in pub:
                if p.getDb() is None:
                    refs.append(p.getUrl().toArray())
                else:
                    refs.append('%s:%s' % (p.getDb(), p.getId()))
        return refs

    def _construct_modification_pattern(self):
        pb = bpp('PatternBox')
        cb = bpp('constraint.ConBox')
        flop = bpp('constraint.Field$Operation')
        rt = bpp('util.RelType')
        tp = bpp('constraint.Type')
        cs = bpp('constraint.ConversionSide')
        cst = bpp('constraint.ConversionSide$Type')
        pt = bpp('constraint.Participant')

        # The following constrainsts were pieced together based on the
        # following two higher level constrains: pb.controlsStateChange(),
        # pb.controlsPhosphorylation(). The pattern cannot be started
        # from EntityReference because it cannot be instantiated.
        # Therefore starting with ProteinReference as controller ER
        p = bpp('Pattern')(bpimpl('ProteinReference')().getModelInterface(),
                           "controller ER")
        # Getting the generic controller EntityReference
        p.add(cb.linkedER(True), "controller ER", "generic controller ER")
        # Getting the controller PhysicalEntity
        p.add(cb.erToPE(), "generic controller ER", "controller simple PE")
        # Getting to the complex controller PhysicalEntity
        p.add(cb.linkToComplex(), "controller simple PE", "controller PE")
        # Getting the control itself
        p.add(cb.peToControl(), "controller PE", "Control")
        # Link the control to the conversion that it controls
        p.add(cb.controlToConv(), "Control", "Conversion")
        # The controller shouldn't be a participant of the conversion
        p.add(bpp('constraint.NOT')(cb.participantER()),
              "Conversion", "controller ER")
        # Get the input participant of the conversion
        p.add(pt(rt.INPUT, True), "Control", "Conversion", "input PE")
        # Make sure the participant is a protein
        p.add(tp(bpimpl('Protein')().getModelInterface()), "input PE")
        # Get the specific PhysicalEntity
        p.add(cb.linkToSpecific(), "input PE", "input simple PE")
        # Get the EntityReference for the converted entity
        p.add(cb.peToER(), "input simple PE", "changed generic ER")
        # Link to the other side of the conversion
        p.add(cs(cst.OTHER_SIDE), "input PE", "Conversion", "output PE")
        # Make sure the two sides are not the same
        p.add(bpp('constraint.Equality')(False), "input PE", "output PE")
        # Make sure the output is a Protein
        p.add(tp(bpimpl('Protein')().getModelInterface()), "output PE")
        # Get the specific PhysicalEntity
        p.add(cb.linkToSpecific(), "output PE", "output simple PE")
        # Link output to the converted EntityReference
        p.add(cb.peToER(), "output simple PE", "changed generic ER")
        # Get the specific converted EntityReference
        p.add(cb.linkedER(False), "changed generic ER", "changed ER")
        p.add(bpp('constraint.NOT')(cb.linkToSpecific()),
              "input PE", "output simple PE")
        p.add(bpp('constraint.NOT')(cb.linkToSpecific()),
              "output PE", "input simple PE")
        return p

    def _get_entity_names(self, bp_ent):
        names = []
        if isinstance(bp_ent, bp('Complex')):
            names += [self._get_entity_names(m) for
                      m in bp_ent.getComponent().toArray()]
        elif isinstance(bp_ent, bp('ProteinReference')) or \
                isinstance(bp_ent, bp('SmallMoleculeReference')) or \
                isinstance(bp_ent, bp('EntityReference')):
            hgnc_id = self._get_hgnc_id(bp_ent)
            if hgnc_id is None:
                hgnc_name = bp_ent.getDisplayName()
            else:
                hgnc_name = self._get_hgnc_name(hgnc_id)
            names += [hgnc_name]
        elif isinstance(bp_ent, bpimpl('Protein')) or \
                isinstance(bp_ent, bpimpl('SmallMolecule')) or \
                isinstance(bp_ent, bp('Protein')) or \
                isinstance(bp_ent, bp('SmallMolecule')):
            ref = bp_ent.getEntityReference()
            names += self._get_entity_names(ref)

        return names

    def _get_hgnc_id(self, bp_entref):
        xrefs = bp_entref.getXref().toArray()
        hgnc_refs = [x for x in xrefs if x.getDb() == 'HGNC']
        hgnc_id = None
        for r in hgnc_refs:
            try:
                hgnc_id = int(r.getId())
            except ValueError:
                continue
        return hgnc_id

    def _get_hgnc_name(self, hgnc_id):
        try:
            hgnc_name = self._hgnc_cache[hgnc_id]
        except KeyError:
            hgnc_name = hgnc_client.get_hgnc_name(hgnc_id)
            self._hgnc_cache[hgnc_id] = hgnc_name
        return hgnc_name

    def _load_hgnc_cache(self):
        try:
            fh = open('hgnc_cache.pkl', 'rb')
        except IOError:
            return {}
        return pickle.load(fh)

    def _dump_hgnc_cache(self):
        fh = open('hgnc_cache.pkl', 'wb')
        pickle.dump(self._hgnc_cache, fh)
        fh.close()

    _mftype_dict = {
        'phosphorylated residue': 'Phosphorylation',
        'O-phospho-L-serine': 'PhosphorylationSerine',
        'O-phospho-L-threonine': 'PhosphorylationThreonine',
        'O-phospho-L-tyrosine': 'PhosphorylationTyrosine',
        'O4\'-phospho-L-tyrosine': 'PhosphorylationTyrosine'
        }

if __name__ == '__main__':
    pass
<commit_msg>Fix breaking up modifications into elementary ones<commit_after>import ipdb
import sys
import pickle

import jnius_config
jnius_config.add_options('-Xmx4g')
from jnius import autoclass, JavaException
from jnius import cast

from belpy.databases import hgnc_client
from belpy.statements import *

# Functions for accessing frequently used java classes with shortened path
def bp(path):
    prefix = 'org.biopax.paxtools.model.level3'
    classname = prefix + '.' + path
    return autoclass_robust(classname)

def bpp(path):
    prefix = 'org.biopax.paxtools.pattern'
    classname = prefix + '.' + path
    return autoclass_robust(classname)

def bpimpl(path):
    prefix = 'org.biopax.paxtools.impl.level3'
    postfix = 'Impl'
    classname = prefix + '.' + path + postfix
    return autoclass_robust(classname)

def autoclass_robust(path):
    try:
        cl = autoclass(path)
    except JavaException:
        print 'Could not instantiate ' + path
        return None
    return cl

def cast_biopax_element(bpe):
    """ Casts a generic BioPAXElement object into a specific type.
    This is useful when a search only returns generic elements. """
    return cast(bpe.getModelInterface().getName(), bpe)

def match_to_array(m):
    """ Returns an array consisting of the elements obtained from a pattern
    search cast into their appropriate classes. """
    return [cast_biopax_element(m.get(i)) for i in range(m.varSize())]

class BiopaxProcessor(object):
    def __init__(self, model):
        self.model = model
        self.belpy_stmts = []
        self._hgnc_cache = self._load_hgnc_cache()

    def get_complexes(self, force_contains=[]):
        pb = bpp('PatternBox')
        s = bpp('Searcher')
        p = pb.inComplexWith()
        res = s.searchPlain(self.model, p)
        res_array = [match_to_array(m) for m in res.toArray()]
        print '%d results found' % res.size()
        for r in res_array:
            members = []
            # Extract first member
            members += self._get_entity_names(r[p.indexOf('Protein 1')])
            # Extract second member
            members += self._get_entity_names(r[p.indexOf('Protein 2')])
            # Skip elements where some pre-specified species
            # are not in the complex for f in force_contains
            skip = False
            for f in force_contains:
                if f not in members:
                    skip = True
            if skip:
                continue

            cplx = r[p.indexOf('Complex')]
            print cplx.getRDFId()

            # TODO: we can't handle modification features in
            # BelPy Complexes yet
            # Modifications of first member
            # feat_1 = r[1].getFeature().toArray()
            # Modifications of second member
            # feat_2 = r[3].getFeature().toArray()
            self.belpy_stmts.append(Complex(members))

    def get_phosphorylation(self):
        stmts = self._get_generic_modification('phospho')
        for s in stmts:
            self.belpy_stmts.append(Phosphorylation(*s))

    def get_dephosphorylation(self):
        stmts = self._get_generic_modification('phospho', mod_gain=False)
        for s in stmts:
            self.belpy_stmts.append(Dephosphorylation(*s))

    def get_acetylation(self):
        stmts = self._get_generic_modification('acetyl')
        for s in stmts:
            self.belpy_stmts.append(Acetylation(*s))

    def get_glycosylation(self):
        stmts = self._get_generic_modification('glycosyl')
        for s in stmts:
            self.belpy_stmts.append(Glycosylation(*s))

    def get_palmitoylation(self):
        stmts = self._get_generic_modification('palmitoyl')
        for s in stmts:
            self.belpy_stmts.append(Palmitoylation(*s))

    def get_activity_modification(self):
        mcc = bpp('constraint.ModificationChangeConstraint')
        mcct = bpp('constraint.ModificationChangeConstraint$Type')
        p = self._construct_modification_pattern()
        mod_filter = 'residue modification, active'
        p.add(mcc(mcct.GAIN, mod_filter),
              "input simple PE", "output simple PE")

        s = bpp('Searcher')
        res = s.searchPlain(self.model, p)
        res_array = [match_to_array(m) for m in res.toArray()]
        print '%d results found' % res.size()
        for r in res_array:
            monomer_name = self._get_entity_names(r[p.indexOf('changed generic ER')])
            stmt_str = ''
            citation = self._get_citation(r[p.indexOf('Conversion')])
            evidence = ''
            annotations = ''
            outPE = r[p.indexOf('output PE')]
            activity = 'Activity'
            relationship = 'DirectlyIncreases'
            mod, mod_pos = self._get_modification_site(outPE)
            if mod:
                stmt = ActivityModification(monomer_name, mod, mod_pos,
                                            relationship, activity,
                                            stmt_str, citation, evidence,
                                            annotations)
                self.belpy_stmts.append(stmt)

    def _get_modification_site(self, modPE):
        # Do we need to look at EntityFeatures?
        modMF = [mf for mf in modPE.getFeature().toArray()
                 if isinstance(mf, bpimpl('ModificationFeature'))]
        mod_pos = []
        mod = []

        for mf in modMF:
            # ModificationFeature / SequenceModificationVocabulary
            mf_type = mf.getModificationType().getTerm().toArray()[0]
            if len(mf.getModificationType().getTerm().toArray()) != 1:
                warnings.warn('Other than one modification term')
            try:
                mod_type = self._mftype_dict[mf_type]
            except KeyError:
                warnings.warn('Unknown modification type %s' % mf_type)
                continue

            mod.append(mod_type)

            # getFeatureLocation returns SequenceLocation, which is the
            # generic parent class of SequenceSite and SequenceInterval.
            # Here we need to cast to SequenceSite in order to get to
            # the sequence position.
            mf_pos = mf.getFeatureLocation()
            if mf_pos is not None:
                mf_site = cast(bp('SequenceSite'), mf_pos)
                mf_pos_status = mf_site.getPositionStatus()
                if mf_pos_status and mf_pos_status.toString() != 'EQUAL':
                    warnings.warn('Modification site position is %s' %
                                  mf_pos_status.toString())
                mod_pos.append(mf_site.getSequencePosition())
            else:
                mod_pos.append(None)
            # Do we need to look at mf.getFeatureLocationType()?
            # It seems to be always None.
            # mf_pos_type = mf.getFeatureLocationType()
        return mod, mod_pos

    def _get_generic_modification(self, mod_filter=None, mod_gain=True):
        mcc = bpp('constraint.ModificationChangeConstraint')
        mcct = bpp('constraint.ModificationChangeConstraint$Type')
        # Start with a generic modification pattern
        p = self._construct_modification_pattern()
        # The modification type should contain the filter string
        if mod_filter is not None:
            if mod_gain:
                mod_gain_const = mcct.GAIN
            else:
                mod_gain_const = mcct.LOSS
            p.add(mcc(mod_gain_const, mod_filter),
                      "input simple PE", "output simple PE")

        s = bpp('Searcher')
        res = s.searchPlain(self.model, p)
        res_array = [match_to_array(m) for m in res.toArray()]
        print '%d results found' % res.size()
        stmts = []
        for r in res_array:
            enz_name = self._get_entity_names(r[p.indexOf('controller ER')])[0]
            sub_name = self._get_entity_names(r[p.indexOf('changed generic ER')])[0]
            stmt_str = ''
            citation = self._get_citation(r[p.indexOf('Conversion')])
            evidence = ''
            annotations = ''

            # Get the modification (s)
            # Should this be simple PE?
            if mod_gain:
                modPE = r[p.indexOf('output simple PE')]
            else:
                modPE = r[p.indexOf('input simple PE')]

            # TODO: handle case when
            # r[p.indexOf('output simple PE')].getRDFId()
            # is not equal to r[p.indexOf('output PE')].getRDFId()
            mod, mod_pos = self._get_modification_site(modPE)
            for m, mp in zip(mod, mod_pos):
                stmt = (enz_name, sub_name, m, mp,
                        stmt_str, citation, evidence, annotations)
                stmts.append(stmt)
        return stmts

    def print_statements(self):
        for i, stmt in enumerate(self.belpy_stmts):
            print "%s: %s" % (i, stmt)

    def _get_citation(self, bpe):
        evidence = bpe.getEvidence().toArray()
        refs = []
        for e in evidence:
            pub = e.getXref().toArray()
            for p in pub:
                if p.getDb() is None:
                    refs.append(p.getUrl().toArray())
                else:
                    refs.append('%s:%s' % (p.getDb(), p.getId()))
        return refs

    def _construct_modification_pattern(self):
        pb = bpp('PatternBox')
        cb = bpp('constraint.ConBox')
        flop = bpp('constraint.Field$Operation')
        rt = bpp('util.RelType')
        tp = bpp('constraint.Type')
        cs = bpp('constraint.ConversionSide')
        cst = bpp('constraint.ConversionSide$Type')
        pt = bpp('constraint.Participant')

        # The following constrainsts were pieced together based on the
        # following two higher level constrains: pb.controlsStateChange(),
        # pb.controlsPhosphorylation(). The pattern cannot be started
        # from EntityReference because it cannot be instantiated.
        # Therefore starting with ProteinReference as controller ER
        p = bpp('Pattern')(bpimpl('ProteinReference')().getModelInterface(),
                           "controller ER")
        # Getting the generic controller EntityReference
        p.add(cb.linkedER(True), "controller ER", "generic controller ER")
        # Getting the controller PhysicalEntity
        p.add(cb.erToPE(), "generic controller ER", "controller simple PE")
        # Getting to the complex controller PhysicalEntity
        p.add(cb.linkToComplex(), "controller simple PE", "controller PE")
        # Getting the control itself
        p.add(cb.peToControl(), "controller PE", "Control")
        # Link the control to the conversion that it controls
        p.add(cb.controlToConv(), "Control", "Conversion")
        # The controller shouldn't be a participant of the conversion
        p.add(bpp('constraint.NOT')(cb.participantER()),
              "Conversion", "controller ER")
        # Get the input participant of the conversion
        p.add(pt(rt.INPUT, True), "Control", "Conversion", "input PE")
        # Make sure the participant is a protein
        p.add(tp(bpimpl('Protein')().getModelInterface()), "input PE")
        # Get the specific PhysicalEntity
        p.add(cb.linkToSpecific(), "input PE", "input simple PE")
        # Get the EntityReference for the converted entity
        p.add(cb.peToER(), "input simple PE", "changed generic ER")
        # Link to the other side of the conversion
        p.add(cs(cst.OTHER_SIDE), "input PE", "Conversion", "output PE")
        # Make sure the two sides are not the same
        p.add(bpp('constraint.Equality')(False), "input PE", "output PE")
        # Make sure the output is a Protein
        p.add(tp(bpimpl('Protein')().getModelInterface()), "output PE")
        # Get the specific PhysicalEntity
        p.add(cb.linkToSpecific(), "output PE", "output simple PE")
        # Link output to the converted EntityReference
        p.add(cb.peToER(), "output simple PE", "changed generic ER")
        # Get the specific converted EntityReference
        p.add(cb.linkedER(False), "changed generic ER", "changed ER")
        p.add(bpp('constraint.NOT')(cb.linkToSpecific()),
              "input PE", "output simple PE")
        p.add(bpp('constraint.NOT')(cb.linkToSpecific()),
              "output PE", "input simple PE")
        return p

    def _get_entity_names(self, bp_ent):
        names = []
        if isinstance(bp_ent, bp('Complex')):
            names += [self._get_entity_names(m) for
                      m in bp_ent.getComponent().toArray()]
        elif isinstance(bp_ent, bp('ProteinReference')) or \
                isinstance(bp_ent, bp('SmallMoleculeReference')) or \
                isinstance(bp_ent, bp('EntityReference')):
            hgnc_id = self._get_hgnc_id(bp_ent)
            if hgnc_id is None:
                hgnc_name = bp_ent.getDisplayName()
            else:
                hgnc_name = self._get_hgnc_name(hgnc_id)
            names += [hgnc_name]
        elif isinstance(bp_ent, bpimpl('Protein')) or \
                isinstance(bp_ent, bpimpl('SmallMolecule')) or \
                isinstance(bp_ent, bp('Protein')) or \
                isinstance(bp_ent, bp('SmallMolecule')):
            ref = bp_ent.getEntityReference()
            names += self._get_entity_names(ref)

        return names

    def _get_hgnc_id(self, bp_entref):
        xrefs = bp_entref.getXref().toArray()
        hgnc_refs = [x for x in xrefs if x.getDb() == 'HGNC']
        hgnc_id = None
        for r in hgnc_refs:
            try:
                hgnc_id = int(r.getId())
            except ValueError:
                continue
        return hgnc_id

    def _get_hgnc_name(self, hgnc_id):
        try:
            hgnc_name = self._hgnc_cache[hgnc_id]
        except KeyError:
            hgnc_name = hgnc_client.get_hgnc_name(hgnc_id)
            self._hgnc_cache[hgnc_id] = hgnc_name
        return hgnc_name

    def _load_hgnc_cache(self):
        try:
            fh = open('hgnc_cache.pkl', 'rb')
        except IOError:
            return {}
        return pickle.load(fh)

    def _dump_hgnc_cache(self):
        fh = open('hgnc_cache.pkl', 'wb')
        pickle.dump(self._hgnc_cache, fh)
        fh.close()

    _mftype_dict = {
        'phosphorylated residue': 'Phosphorylation',
        'O-phospho-L-serine': 'PhosphorylationSerine',
        'O-phospho-L-threonine': 'PhosphorylationThreonine',
        'O-phospho-L-tyrosine': 'PhosphorylationTyrosine',
        'O4\'-phospho-L-tyrosine': 'PhosphorylationTyrosine'
        }

if __name__ == '__main__':
    pass
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""
Created on Tue Apr 14 17:16:16 2015

Our driving forces.

@author: Richard Decal
"""
import numpy as np
from numpy import linalg, newaxis
from scipy.integrate import quad
from scipy.misc import derivative as deriv

def bias_force(rf, bias='uniform'):
    """Generate random-direction force vector at each timestep from double-
    exponential distribution given exponent term rf.

    Args:
        rf: random force distribution exponent (float)

    Returns:
        random force x and y components (array)
    """
    mag_thresh = 3e-6
    # TODO fit to Sharri's data instead
    ends = gen_symm_vecs(3)
    # following params were fitted from the Dickinson flight data
    mu = 0.600023812816
    sigma = 0.719736466122
    scale = 1.82216219069
    mag = np.random.lognormal(mean=mu, sigma=sigma, size=1)
    if mag * rf > mag_thresh: # filter out huge magnitudes
        return ends * mag_thresh

    return mag * ends * rf

#==============================================================================
### depreciated
# def random_force(rf, dim=2):
#     """Generate random-direction force vector at each timestep from double-
#     exponential distribution given exponent term rf.
#
#     Args:
#         rf: random force distribution exponent (float)
#
#     Returns:
#         random force x and y components (array)
#     """
#     mag_thresh = 3e-6
#     if dim == 2:
#         ends = gen_symm_vecs(2)
#         # following params were fitted from the Dickinson fligt data
#         mu = 0.600023812816
#         sigma = 0.719736466122
#         scale = 1.82216219069
#         mag = np.random.lognormal(mean=mu, sigma=sigma, size=1)
#         return mag * ends * rf
#     if dim == 3: # FIXME: 3d symmetric
#         ends = gen_symm_vecs(3)
#         # following params were fitted from the Dickinson fligt data
#         mu = 0.600023812816
#         sigma = 0.719736466122
#         scale = 1.82216219069
#         mag = np.random.lognormal(mean=mu, sigma=sigma, size=1)
#         if mag * rf > mag_thresh: # filter out huge magnitudes
#             return ends * mag_thresh
#         return mag * ends * rf
#     else:
#==============================================================================

def gen_symm_vecs(dims):
    """generate radially-symmetric vectors sampled from the unit circle.  These
    can then be scaled by a force to make radially symmetric distributions.

    making a scatter plot of many draws from this function makes a unit circle.
    """
    vecs = np.random.normal(size=dims)
    mags = linalg.norm(vecs, axis=-1)

    ends = vecs / mags[..., newaxis]

    return ends  # vector

def upwindBiasForce(wtf, upwind_direction=0.):
    """Biases the agent to fly upwind. Constant push with strength wtf

    [formerly]: Picks the direction +- pi/2 rads from
    the upwind direction and scales it by accelList constant magnitude, "wtf".

    Args:
        wtf: bias distribution exponent
        upwind_direction: direction of upwind (in radians)

    Returns:
        upwind bias force x and y components as an array
    """
    if wtf == None:
        return [0., 0., 0.]
    else:
        return [wtf, 0., 0.]  # wf is constant, directly upwind

#def wall_force_field(current_pos, current_velo, wallF, wallX_pos=[0., 1.], wallY_pos=[-0.15, 0.15]):
#    """If agent gets too close to wall, inflict it with repulsive forces as a function of how close it is to the wall and it's speed towards the wall.
#
#    Args:
#        current_pos: (x,y) coords of agent right now (array)
#        wallF: (None | tuple)
#            None- disabled
#            tuple- (lambda (float), y0 (float). Lambda is the decay constant in the exponentially decaying wall repulsive force, y0 is where the decaying starts.
#        wallX_pos: Wall X coords (array)
#        wallY_pos: Wall Y coords (array)
#
#    Returns:
#        wall_force: (array)
#    """
#    if wallF is None:
#        return [0., 0.]
##    elif type(wallF) == "tuple":
#    else:
#        wallF_lambda, wallF_y0 = wallF
#        wallF_x = 0.
#        wallF_y = 0.
#        posx, posy = current_pos
#        velox, veloy = current_velo
#        y_dist = 0.15 - abs(posy) # distance of agent from wall
#        force_mag_y = wallF_magnitude(y_dist, wallF_lambda, wallF_y0)
#        if (posy < 0 and veloy < 0) or (posy > 0 and veloy > 0): # heading towards top OR bottom
#            wallF_y = -1. * veloy * force_mag_y  # equal an opposite force, scaled by force mag
#        return np.array([wallF_x, wallF_y])
#
#def wallF_magnitude(distance, wallF, y0):
#    """magnitude of wall repulsion, a function of position
#
#    """
#    return y0 * np.exp(-1 * wallF * distance)

def repulsionF(position, repulsion_funcs, wallF_params):
    """repulsion as a function of position.
    """
    scalar = wallF_params[0]
    intd = 0.003 / 2 # integratal distance in mm
    pos_x, pos_y, pos_z = position
    repulsion_x, repulsion_y, repulsion_z = repulsion_funcs
    dx = 0.00001

    # solve direction of forces
    slope_rep_x, slope_rep_y, slope_rep_z = deriv(repulsion_x, pos_x, dx=dx), deriv(repulsion_y, pos_y, dx=dx), deriv(repulsion_z, pos_z, dx=dx)
    directions = []
    for slope in [slope_rep_x, slope_rep_y, slope_rep_z]:
        if slope < 0:
            directions.append(1.)
        elif slope > 0:
            directions.append(-1.)
        else: # this shouldn't happen
            directions.append(0.)

    # [0] to discard error term
    repulsionF =  scalar * np.array([directions[0] * quad(repulsion_x, pos_x-intd, pos_x+intd)[0], \
        scalar * directions[1] * quad(repulsion_y, pos_y-intd, pos_y+intd)[0],\
        scalar * directions[2] * quad(repulsion_z, pos_z-intd, pos_z+intd)[0]])

#    print "pos", position
#    print "repF", repulsionF
    return repulsionF

#def brakingF(candidate_pos, totalF_x, totalF_y, boundary):
#    # check x dim
#    if candidate_pos[0] < boundary[0]:  # too far left
#        outside_correct = True
#        brakeF_x = -6 * totalF_x
#        brakeF_y = 0.
#    # check y dim
#    if candidate_pos[1] > boundary[2]:  # too far up
#        outside_correct = True
#        brakeF_x = 0.
#        brakeF_y = -6 * totalF_y
##                        print "crash! top wall"
#    elif candidate_pos[1] < boundary[3]:  # too far down
#        outside_correct = True
#        brakeF_x = 0.
#        brakeF_y = -6 * totalF_y
#    else:
#        outside_correct, brakeF_x, brakeF_y = False, 0., 0.
#
#    return outside_correct, brakeF_x, brakeF_y
<commit_msg>refitted to Sharri's data instead of Dickinson data<commit_after># -*- coding: utf-8 -*-
"""
Created on Tue Apr 14 17:16:16 2015

Our driving forces.

@author: Richard Decal
"""
import numpy as np
from numpy import linalg, newaxis
from scipy.integrate import quad
from scipy.misc import derivative as deriv

def bias_force(rf, bias='uniform'):
    """Generate random-direction force vector at each timestep from double-
    exponential distribution given exponent term rf.

    Args:
        rf: random force distribution exponent (float)

    Returns:
        random force x and y components (array)
    """
    mag_thresh = 1e-5
    ends = gen_symm_vecs(3)
    # the following params were fitted from Sharri's flight data
    mu = -0.405632480939
    sigma = 0.932352661694 * 0.5
    scale = 0.666555094117
    # # following params were fitted from the Dickinson flight data
    # mu = 0.600023812816
    # sigma = 0.719736466122
    # scale = 1.82216219069
    mag = np.random.lognormal(mean=mu, sigma=sigma, size=1)
    if mag * rf > mag_thresh: # filter out huge magnitudes
        return ends * 0. * rf

#    print mag*rf  # check the order of the biasF
    return mag * ends * rf

#==============================================================================
### depreciated
# def random_force(rf, dim=2):
#     """Generate random-direction force vector at each timestep from double-
#     exponential distribution given exponent term rf.
#
#     Args:
#         rf: random force distribution exponent (float)
#
#     Returns:
#         random force x and y components (array)
#     """
#     mag_thresh = 3e-6
#     if dim == 2:
#         ends = gen_symm_vecs(2)
#         # following params were fitted from the Dickinson fligt data
#         mu = 0.600023812816
#         sigma = 0.719736466122
#         scale = 1.82216219069
#         mag = np.random.lognormal(mean=mu, sigma=sigma, size=1)
#         return mag * ends * rf
#     if dim == 3: # FIXME: 3d symmetric
#         ends = gen_symm_vecs(3)
#         # following params were fitted from the Dickinson fligt data
#         mu = 0.600023812816
#         sigma = 0.719736466122
#         scale = 1.82216219069
#         mag = np.random.lognormal(mean=mu, sigma=sigma, size=1)
#         if mag * rf > mag_thresh: # filter out huge magnitudes
#             return ends * mag_thresh
#         return mag * ends * rf
#     else:
#==============================================================================

def gen_symm_vecs(dims):
    """generate radially-symmetric vectors sampled from the unit circle.  These
    can then be scaled by a force to make radially symmetric distributions.

    making a scatter plot of many draws from this function makes a unit circle.
    """
    vecs = np.random.normal(size=dims)
    mags = linalg.norm(vecs, axis=-1)

    ends = vecs / mags[..., newaxis]

    return ends  # vector

def upwindBiasForce(wtf, upwind_direction=0.):
    """Biases the agent to fly upwind. Constant push with strength wtf

    [formerly]: Picks the direction +- pi/2 rads from
    the upwind direction and scales it by accelList constant magnitude, "wtf".

    Args:
        wtf: bias distribution exponent
        upwind_direction: direction of upwind (in radians)

    Returns:
        upwind bias force x and y components as an array
    """
    if wtf == None:
        return [0., 0., 0.]
    else:
        return [wtf, 0., 0.]  # wf is constant, directly upwind

#def wall_force_field(current_pos, current_velo, wallF, wallX_pos=[0., 1.], wallY_pos=[-0.15, 0.15]):
#    """If agent gets too close to wall, inflict it with repulsive forces as a function of how close it is to the wall and it's speed towards the wall.
#
#    Args:
#        current_pos: (x,y) coords of agent right now (array)
#        wallF: (None | tuple)
#            None- disabled
#            tuple- (lambda (float), y0 (float). Lambda is the decay constant in the exponentially decaying wall repulsive force, y0 is where the decaying starts.
#        wallX_pos: Wall X coords (array)
#        wallY_pos: Wall Y coords (array)
#
#    Returns:
#        wall_force: (array)
#    """
#    if wallF is None:
#        return [0., 0.]
##    elif type(wallF) == "tuple":
#    else:
#        wallF_lambda, wallF_y0 = wallF
#        wallF_x = 0.
#        wallF_y = 0.
#        posx, posy = current_pos
#        velox, veloy = current_velo
#        y_dist = 0.15 - abs(posy) # distance of agent from wall
#        force_mag_y = wallF_magnitude(y_dist, wallF_lambda, wallF_y0)
#        if (posy < 0 and veloy < 0) or (posy > 0 and veloy > 0): # heading towards top OR bottom
#            wallF_y = -1. * veloy * force_mag_y  # equal an opposite force, scaled by force mag
#        return np.array([wallF_x, wallF_y])
#
#def wallF_magnitude(distance, wallF, y0):
#    """magnitude of wall repulsion, a function of position
#
#    """
#    return y0 * np.exp(-1 * wallF * distance)

def repulsionF(position, repulsion_funcs, wallF_params):
    """repulsion as a function of position.
    """
    scalar = wallF_params[0]
    intd = 0.003 / 2 # integratal distance in mm
    pos_x, pos_y, pos_z = position
    repulsion_x, repulsion_y, repulsion_z = repulsion_funcs
    dx = 0.00001

    # solve direction of forces
    slope_rep_x, slope_rep_y, slope_rep_z = deriv(repulsion_x, pos_x, dx=dx), deriv(repulsion_y, pos_y, dx=dx), deriv(repulsion_z, pos_z, dx=dx)
    directions = []
    for slope in [slope_rep_x, slope_rep_y, slope_rep_z]:
        if slope < 0:
            directions.append(1.)
        elif slope > 0:
            directions.append(-1.)
        else: # this shouldn't happen
            directions.append(0.)

    # [0] to discard error term
    repulsionF =  scalar * np.array([directions[0] * quad(repulsion_x, pos_x-intd, pos_x+intd)[0], \
        scalar * directions[1] * quad(repulsion_y, pos_y-intd, pos_y+intd)[0],\
        scalar * directions[2] * quad(repulsion_z, pos_z-intd, pos_z+intd)[0]])

#    print "pos", position
#    print "repF", repulsionF
    return repulsionF

#def brakingF(candidate_pos, totalF_x, totalF_y, boundary):
#    # check x dim
#    if candidate_pos[0] < boundary[0]:  # too far left
#        outside_correct = True
#        brakeF_x = -6 * totalF_x
#        brakeF_y = 0.
#    # check y dim
#    if candidate_pos[1] > boundary[2]:  # too far up
#        outside_correct = True
#        brakeF_x = 0.
#        brakeF_y = -6 * totalF_y
##                        print "crash! top wall"
#    elif candidate_pos[1] < boundary[3]:  # too far down
#        outside_correct = True
#        brakeF_x = 0.
#        brakeF_y = -6 * totalF_y
#    else:
#        outside_correct, brakeF_x, brakeF_y = False, 0., 0.
#
#    return outside_correct, brakeF_x, brakeF_y
<|endoftext|>


<commit_before><commit_msg>Fix IllegalTransitionException.<commit_after><|endoftext|>


<commit_before>"""
Example script which runs a number of policies on a single problem with varying
levels of observation noise.
"""

from jug import TaskGenerator
from jug.compound import CompoundTaskGenerator

from pybo import solve_bayesopt
from pybo.domains import Discrete, Box

__all__ = ['run_instance', 'run_experiment', 'run_stack']

@TaskGenerator
def run_instance(problem, model, policy, niter, seed):
    """
    Default runner for a single instance (ie problem/policy pair). With the
    exception of the `seed` keyword, all other arguments passed to
    run_instance() can be user-defined as long as the same interface (keywords)
    is used at runtime. In other words, each element of the dictionary that
    is passed to run_experiment() should itself be a dictionary with all the
    keywords in run_instance().

    """
    # instantiate the problem
    func, kwargs = problem
    func = func(rng=seed, **kwargs)

    # get the function's bounds/domain
    if hasattr(func, 'bounds'):
        domain = Box(func.bounds)
    else:
        domain = Discrete(func.X)

    # solve the problem using the prescribed model and policy
    _, _, info = solve_bayesopt(func,
                                domain,
                                niter=niter,
                                model=model,
                                policy=policy)

    # obtain the results
    xbest = info.xbest
    fbest = func.get_f(xbest)

    return fbest

@CompoundTaskGenerator
def run_experiment(name, experiment, eparams, nreps=1):
    """
    Run `experiment()`, repeated `nreps` times, on each set of kwargs in
    `eparams`.

    Parameters:
        name: str, label associated with this set of experiments.
        experiment: function, function to run on each value of eparams.
        eparams: dict, named list (dict) of experimental parameters (dicts) to
            be passed to experiment() function.
        nrep: int, number of repetitions of each instance to run, default 1.

    Returns:
        results: dict, dictionary of jug tasks.
    """
    results = dict()

    for task, kwargs in eparams.items():
        # create the subtasks
        results[task] = [experiment(seed=seed, **kwargs)
                         for seed in xrange(nreps)]

        # rename the subtasks so they are easily viewable
        for t in results[task]:
            t.name = ':'.join([name, task])

    return results

def run_stack(stack, nreps=1):
    results = dict()

    for name, eparams in stack.items():
        results[name] = run_experiment(name, eparams, nreps)
        results[name].name = name

    return results
<commit_msg>Fix run_stack. Was not passing instance runner.<commit_after>"""
Example script which runs a number of policies on a single problem with varying
levels of observation noise.
"""

from jug import TaskGenerator
from jug.compound import CompoundTaskGenerator

from pybo import solve_bayesopt
from pybo.domains import Discrete, Box

__all__ = ['run_instance', 'run_experiment', 'run_stack']

@TaskGenerator
def run_instance(problem, model, policy, niter, seed):
    """
    Default runner for a single instance (ie problem/policy pair). With the
    exception of the `seed` keyword, all other arguments passed to
    run_instance() can be user-defined as long as the same interface (keywords)
    is used at runtime. In other words, each element of the dictionary that
    is passed to run_experiment() should itself be a dictionary with all the
    keywords in run_instance().

    """
    # instantiate the problem
    func, kwargs = problem
    func = func(rng=seed, **kwargs)

    # get the function's bounds/domain
    if hasattr(func, 'bounds'):
        domain = Box(func.bounds)
    else:
        domain = Discrete(func.X)

    # solve the problem using the prescribed model and policy
    _, _, info = solve_bayesopt(func,
                                domain,
                                niter=niter,
                                model=model,
                                policy=policy)

    # obtain the results
    xbest = info.xbest
    fbest = func.get_f(xbest)

    return fbest

@CompoundTaskGenerator
def run_experiment(name, experiment, eparams, nreps=1):
    """
    Run `experiment()`, repeated `nreps` times, on each set of kwargs in
    `eparams`.

    Parameters:
        name: str, label associated with this set of experiments.
        experiment: function, function to run on each value of eparams.
        eparams: dict, named list (dict) of experimental parameters (dicts) to
            be passed to experiment() function.
        nrep: int, number of repetitions of each instance to run, default 1.

    Returns:
        results: dict, dictionary of jug tasks.
    """
    results = dict()

    for task, kwargs in eparams.items():
        # create the subtasks
        results[task] = [experiment(seed=seed, **kwargs)
                         for seed in xrange(nreps)]

        # rename the subtasks so they are easily viewable
        for t in results[task]:
            t.name = ':'.join([name, task])

    return results

def run_stack(experiment, stack, nreps=1):
    """
    Runs `experiment()` on a stack of named experiments passed via the
    dictionary stack. Each key should correspond to a meaningful grouping of
    experiments.

    Parameters:
        experiment: function, function to run on each value of stack.
        stack: dict, stack of parameters to pass to experiment(), grouped and
            named in a dictionary.
        nreps: int, number of repetitions of each instance to run, default 1.

    Returns:
        results: dict, dictionary of dictionary of jug tasks, corresponding to
            a prescribed semantic grouping of experiments.
    """
    results = dict()

    for name, eparams in stack.items():
        results[name] = run_experiment(name, experiment, eparams, nreps)
        results[name].name = name

    return results
<|endoftext|>


<commit_before>from pybuilder.core import task, init, depends, use_plugin, description
from pybuilder.errors import BuildFailedException
import os, sys, subprocess
from pybuilder_nose.utils import getImportantDirs
from pybuilder_nose.utils import prepareArgs

use_plugin('python.core')

@init
def init_nose(project, logger):
  project.build_depends_on('nose')
  project.build_depends_on('coverage')

@task('run_unit_tests')
@description('Run your entire test suite with Node')
@depends('prepare')
def run_unit_tests(project, logger):

  # set cwd to project root
  src_dir, test_dir, html_dir, xml_file, xunit_file = getImportantDirs(project)

  logger.debug("SRC dir: %s" % src_dir)
  logger.debug("Test dir: %s" % test_dir)
  logger.debug("HTML Reports dir: %s" % html_dir)
  logger.debug("XML Report: %s" % xml_file)
  logger.debug("Xunit Report: %s" % xunit_file)

  if os.path.exists(html_dir) == False:
    os.makedirs(html_dir)

  logger.debug("Setting initial nose properties")

  project.set_property('nose_where', test_dir)
  project.set_property('nose_cover-package', 'src/main/python')
  project.set_property('nose_with-coverage', True)
  project.set_property('nose_cover-xml', True)
  project.set_property('nose_cover-xml-file', xml_file)
  project.set_property('nose_cover-html', True)
  project.set_property('nose_cover-html-dir', html_dir)
  project.set_property('nose_with-xunit', True)
  project.set_property('nose_xunit-file', xunit_file)

  if project.get_property('verbose'):
    project.set_property('nose_nocapture', True)
    project.set_property('verbose', True)

  logger.debug("Collecting nose properties")

  args = ['nosetest']

  args = prepareArgs(project)
  args.insert(0, 'nosetests')

  for arg in args:
    logger.debug('Nose arg: %s' % arg)

  noseEnv = os.environ.copy()
  noseEnv["PYTHONPATH"] = "src/main/python"

  logger.info("Launching nosetests")
  noseProc = subprocess.Popen(args, stdout=subprocess.PIPE, env=noseEnv)

  while noseProc.poll() is None:
    l = noseProc.stdout.readline()
    logger.debug(l)

  if noseProc.returncode != 0:
    logger.error('Unit tests failed with exit code %s' % noseProc.returncode)
    raise BuildFailedException('Unit tests did not pass')

@depends('run_unit_tests')
@task
def analyze(project, logger):
  logger.debug("ANALYZE TASK")
  pass

@task('clean')
def clean_coverage(project, logger):

  coverage_file = ".coverage"

  if os.path.exists(coverage_file):
    logger.debug("Removing %s" % coverage_file)
    os.remove(coverage_file)

<commit_msg>Use src_dir for --package option<commit_after>from pybuilder.core import task, init, depends, use_plugin, description
from pybuilder.errors import BuildFailedException
import os, sys, subprocess
from pybuilder_nose.utils import getImportantDirs
from pybuilder_nose.utils import prepareArgs

use_plugin('python.core')

@init
def init_nose(project, logger):
  project.build_depends_on('nose')
  project.build_depends_on('coverage')

@task('run_unit_tests')
@description('Run your entire test suite with Node')
@depends('prepare')
def run_unit_tests(project, logger):

  # set cwd to project root
  src_dir, test_dir, html_dir, xml_file, xunit_file = getImportantDirs(project)

  logger.debug("SRC dir: %s" % src_dir)
  logger.debug("Test dir: %s" % test_dir)
  logger.debug("HTML Reports dir: %s" % html_dir)
  logger.debug("XML Report: %s" % xml_file)
  logger.debug("Xunit Report: %s" % xunit_file)

  if os.path.exists(html_dir) == False:
    os.makedirs(html_dir)

  logger.debug("Setting initial nose properties")

  project.set_property('nose_where', test_dir)
  project.set_property('nose_cover-package', src_dir)
  project.set_property('nose_with-coverage', True)
  project.set_property('nose_cover-xml', True)
  project.set_property('nose_cover-xml-file', xml_file)
  project.set_property('nose_cover-html', True)
  project.set_property('nose_cover-html-dir', html_dir)
  project.set_property('nose_with-xunit', True)
  project.set_property('nose_xunit-file', xunit_file)

  if project.get_property('verbose'):
    project.set_property('nose_nocapture', True)
    project.set_property('verbose', True)

  logger.debug("Collecting nose properties")

  args = ['nosetest']

  args = prepareArgs(project)
  args.insert(0, 'nosetests')

  for arg in args:
    logger.debug('Nose arg: %s' % arg)

  noseEnv = os.environ.copy()
  noseEnv["PYTHONPATH"] = "src/main/python"

  logger.info("Launching nosetests")
  noseProc = subprocess.Popen(args, stdout=subprocess.PIPE, env=noseEnv)

  while noseProc.poll() is None:
    l = noseProc.stdout.readline()
    logger.debug(l)

  if noseProc.returncode != 0:
    logger.error('Unit tests failed with exit code %s' % noseProc.returncode)
    raise BuildFailedException('Unit tests did not pass')

@depends('run_unit_tests')
@task
def analyze(project, logger):
  logger.debug("ANALYZE TASK")
  pass

@task('clean')
def clean_coverage(project, logger):

  coverage_file = ".coverage"

  if os.path.exists(coverage_file):
    logger.debug("Removing %s" % coverage_file)
    os.remove(coverage_file)

<|endoftext|>