<commit_before>"""KrakenSystem - objects.kraken_core module.

Classes:
KrakenSystem - Class for constructing the Fabric Engine Core client.

"""

import json
import imp
import sys
import inspect

from kraken.core.profiler import Profiler
import FabricEngine.Core

class KrakenSystem(object):
    """The KrakenSystem is a singleton object used to provide an interface with
    the FabricEngine Core and RTVal system."""

    __instance = None

    def __init__(self):
        """Initializes the Kraken System object."""

        super(KrakenSystem, self).__init__()

        self.client = None
        self.typeDescs = None
        self.registeredTypes = None
        self.loadedExtensions = []

        self.registeredComponents = {}

    def loadCoreClient(self):
        """Loads the Fabric Engine Core Client

        Return:
        None

        """

        if self.client == None:
            Profiler.getInstance().push("loadCoreClient")

            try:
                imp.find_module('cmds')
                host = 'Maya'
            except ImportError:
                try:
                    imp.find_module('sipyutils')
                    host = 'Softimage'
                except ImportError:
                    host = 'Python'

            if host == "Python":
                self.client = FabricEngine.Core.createClient()

            elif host == "Maya":
                contextID = cmds.fabricSplice('getClientContextID')
                if contextID == '':
                    cmds.fabricSplice('constructClient')
                    contextID = cmds.fabricSplice('getClientContextID')

                # Pull out the Splice client.
                self.client = FabricEngine.Core.createClient({"contextID": contextID})

            elif host == "Softimage":
                from win32com.client.dynamic import Dispatch
                si = Dispatch("XSI.Application").Application
                contextID = si.fabricSplice('getClientContextID')
                if contextID == '':
                    si.fabricSplice('constructClient')
                    contextID = si.fabricSplice('getClientContextID')

                # Pull out the Splice client.
                self.client = FabricEngine.Core.createClient({"contextID": contextID})

            self.loadExtension('Math')

            Profiler.getInstance().pop()

    def getCoreClient(self):
        """Returns the Fabric Engine Core Client owned by the KrakenSystem

        Return:
        The Fabric Engine Core Client

        """

        if self.client is None:
            self.loadCoreClient()

        return self.client

    def loadExtension(self, extension):
        """Loads the given extension and updates the registeredTypes cache.

        Arguments:
        extension -- string, The name of the extension to load.

        Return:
        None

        """

        if extension not in self.loadedExtensions:
            Profiler.getInstance().push("loadExtension:" + extension)
            self.client.loadExtension(extension)
            self.registeredTypes = self.client.RT.types
            self.typeDescs = self.client.RT.getRegisteredTypes()
            # Cache the loaded extension so that we aviod refreshing the typeDescs cache(costly)
            self.loadedExtensions.append(extension)
            Profiler.getInstance().pop()

    def constructRTVal(self, dataType, defaultValue=None):
        """Constructs a new RTVal using the given name and optional devault value.

        Arguments:
        dataType -- string, The name of the data type to construct.
        defaultValue -- value, The default value to use to initialize the RTVal

        Return:
        The constructed RTval.

        """

        self.loadCoreClient()
        klType = getattr(self.registeredTypes, dataType)

        if defaultValue is not None:
            if hasattr(defaultValue, '_rtval'):
                return defaultValue._rtval

            typeDesc = self.typeDescs[dataType]
            if 'members' in typeDesc:
                try:
                    value = klType.create()
                except:
                    value = klType()
                for i in range(0, len(typeDesc['members'])):
                    memberName = typeDesc['members'][i]['name']
                    memberType = typeDesc['members'][i]['type']
                    if memberName in defaultValue:
                        setattr(value, memberName, self.constructRTVal(memberType, getattr(defaultValue, memberName)))
                return value

            else:
                return klType(defaultValue)
        else:
            try:
                return klType.create()
            except:
                try:
                    return klType()
                except Exception as e:
                    raise Exception("Error constructing RTVal:" + dataType)

    def rtVal(self, dataType, defaultValue=None):
        """Constructs a new RTVal using the given name and optional devault value.

        Arguments:
        dataType -- string, The name of the data type to construct.
        defaultValue -- value, The default value to use to initialize the RTVal

        Return:
        The constructed RTval.

        """

        return self.constructRTVal(dataType, defaultValue)

    def isRTVal(self, value):
        """Returns true if the given value is an RTVal.

        Arguments:
        value -- value, value to test.

        Return:
        True if successful.

        """

        return str(type(value)) == "<type 'PyRTValObject'>"

    def getRTValTypeName(self, rtval):
        """Returns the name of the type, handling extracting the name from KL RTVals.

        Arguments:
        rtval -- rtval, the rtval to extract the name from.

        Return:
        True if successful.

        """

        if ks.isRTVal(rtval):
            return json.loads(rtval.type("Type").jsonDesc("String"))['name']
        else:
            return "None"

    def registerComponent(self, componentClass):
        """Registers a component Python class with the KrakenSystem so ti can be built by the rig builder.

        Arguments:
        componentClass -- string, the Python class of the component

        Return:
        None

        """

        componentModulePath = componentClass.__module__ + "." + componentClass.__name__
        if componentModulePath in self.registeredComponents:
            # we allow reregistring of components because as a componet's class is edited
            # it will be re-imported by python(in Maya), and the classes reregistered.
            pass

        self.registeredComponents[componentModulePath] = componentClass

    def getComponentClass(self, className):
        """Returns the registered Python component class with the given name

        Arguments:
        className -- string, The name of the Python component class

        Return:
        The Python component class

        """

        if className not in self.registeredComponents:
            raise Exception("Component with that class not registered:" + className)

        return self.registeredComponents[className]

    def reloadAllComponents(self):
        """Force the reload of all registered component modules

        Return:
        True if successful.

        """
        print "reloadAllComponents"
        modules = {}
        for name, componentClass in self.registeredComponents.iteritems():
            componentModulePath = componentClass.__module__
            module = sys.modules[componentModulePath]
            if module.__file__ not in modules:
                modules[module.__file__] = module

        self.registeredComponents = {}

        orderedModules = []
        def collectModuleHierarchy(module):
            print "collectModuleHierarchy:" + str(module.__name__)

            if str(module.__name__) == 'KrakenSystem':
                return

            if str(module.__name__) not in ('SceneItem', 'MathObject'):
                for name, cls in inspect.getmembers(module):
                    # print name
                    if inspect.isclass(cls):
                        baseClasses = inspect.getmro(cls)
                        # print baseClasses
                        # for i in xrange(0, len(list(baseClasses))):
                        for baseClass in list(baseClasses):
                            # print i
                            # baseClass = baseClasses[i]
                            baseModulePath = baseClass.__module__

                            print str(baseModulePath)
                            if str(baseModulePath) in ('__builtin__', '__builtins__', '__doc__', '__file__'):
                                break

                            baseModule = sys.modules[baseModulePath]
                            # print str(baseModule)
                            # importModule(baseModule)
                            if baseModule not in orderedModules:
                                orderedModules.append(baseModule)

            # if module.__file__ not in orderedModules:
            #     orderedModules.append(module)

        for path, module in modules.iteritems():
            collectModuleHierarchy(module)

        for module in reversed(orderedModules):
            print "relaod:" + str(module)
            reload(module)

        return True

ks = KrakenSystem.getInstance()
<commit_msg>re-adding getInstance.<commit_after>"""KrakenSystem - objects.kraken_core module.

Classes:
KrakenSystem - Class for constructing the Fabric Engine Core client.

"""

import json
import imp
import sys
import inspect

from kraken.core.profiler import Profiler
import FabricEngine.Core

class KrakenSystem(object):
    """The KrakenSystem is a singleton object used to provide an interface with
    the FabricEngine Core and RTVal system."""

    __instance = None

    def __init__(self):
        """Initializes the Kraken System object."""

        super(KrakenSystem, self).__init__()

        self.client = None
        self.typeDescs = None
        self.registeredTypes = None
        self.loadedExtensions = []

        self.registeredComponents = {}

    def loadCoreClient(self):
        """Loads the Fabric Engine Core Client

        Return:
        None

        """

        if self.client == None:
            Profiler.getInstance().push("loadCoreClient")

            try:
                imp.find_module('cmds')
                host = 'Maya'
            except ImportError:
                try:
                    imp.find_module('sipyutils')
                    host = 'Softimage'
                except ImportError:
                    host = 'Python'

            if host == "Python":
                self.client = FabricEngine.Core.createClient()

            elif host == "Maya":
                contextID = cmds.fabricSplice('getClientContextID')
                if contextID == '':
                    cmds.fabricSplice('constructClient')
                    contextID = cmds.fabricSplice('getClientContextID')

                # Pull out the Splice client.
                self.client = FabricEngine.Core.createClient({"contextID": contextID})

            elif host == "Softimage":
                from win32com.client.dynamic import Dispatch
                si = Dispatch("XSI.Application").Application
                contextID = si.fabricSplice('getClientContextID')
                if contextID == '':
                    si.fabricSplice('constructClient')
                    contextID = si.fabricSplice('getClientContextID')

                # Pull out the Splice client.
                self.client = FabricEngine.Core.createClient({"contextID": contextID})

            self.loadExtension('Math')

            Profiler.getInstance().pop()

    def getCoreClient(self):
        """Returns the Fabric Engine Core Client owned by the KrakenSystem

        Return:
        The Fabric Engine Core Client

        """

        if self.client is None:
            self.loadCoreClient()

        return self.client

    def loadExtension(self, extension):
        """Loads the given extension and updates the registeredTypes cache.

        Arguments:
        extension -- string, The name of the extension to load.

        Return:
        None

        """

        if extension not in self.loadedExtensions:
            Profiler.getInstance().push("loadExtension:" + extension)
            self.client.loadExtension(extension)
            self.registeredTypes = self.client.RT.types
            self.typeDescs = self.client.RT.getRegisteredTypes()
            # Cache the loaded extension so that we aviod refreshing the typeDescs cache(costly)
            self.loadedExtensions.append(extension)
            Profiler.getInstance().pop()

    def constructRTVal(self, dataType, defaultValue=None):
        """Constructs a new RTVal using the given name and optional devault value.

        Arguments:
        dataType -- string, The name of the data type to construct.
        defaultValue -- value, The default value to use to initialize the RTVal

        Return:
        The constructed RTval.

        """

        self.loadCoreClient()
        klType = getattr(self.registeredTypes, dataType)

        if defaultValue is not None:
            if hasattr(defaultValue, '_rtval'):
                return defaultValue._rtval

            typeDesc = self.typeDescs[dataType]
            if 'members' in typeDesc:
                try:
                    value = klType.create()
                except:
                    value = klType()
                for i in range(0, len(typeDesc['members'])):
                    memberName = typeDesc['members'][i]['name']
                    memberType = typeDesc['members'][i]['type']
                    if memberName in defaultValue:
                        setattr(value, memberName, self.constructRTVal(memberType, getattr(defaultValue, memberName)))
                return value

            else:
                return klType(defaultValue)
        else:
            try:
                return klType.create()
            except:
                try:
                    return klType()
                except Exception as e:
                    raise Exception("Error constructing RTVal:" + dataType)

    def rtVal(self, dataType, defaultValue=None):
        """Constructs a new RTVal using the given name and optional devault value.

        Arguments:
        dataType -- string, The name of the data type to construct.
        defaultValue -- value, The default value to use to initialize the RTVal

        Return:
        The constructed RTval.

        """

        return self.constructRTVal(dataType, defaultValue)

    def isRTVal(self, value):
        """Returns true if the given value is an RTVal.

        Arguments:
        value -- value, value to test.

        Return:
        True if successful.

        """

        return str(type(value)) == "<type 'PyRTValObject'>"

    def getRTValTypeName(self, rtval):
        """Returns the name of the type, handling extracting the name from KL RTVals.

        Arguments:
        rtval -- rtval, the rtval to extract the name from.

        Return:
        True if successful.

        """

        if ks.isRTVal(rtval):
            return json.loads(rtval.type("Type").jsonDesc("String"))['name']
        else:
            return "None"

    def registerComponent(self, componentClass):
        """Registers a component Python class with the KrakenSystem so ti can be built by the rig builder.

        Arguments:
        componentClass -- string, the Python class of the component

        Return:
        None

        """

        componentModulePath = componentClass.__module__ + "." + componentClass.__name__
        if componentModulePath in self.registeredComponents:
            # we allow reregistring of components because as a componet's class is edited
            # it will be re-imported by python(in Maya), and the classes reregistered.
            pass

        self.registeredComponents[componentModulePath] = componentClass

    def getComponentClass(self, className):
        """Returns the registered Python component class with the given name

        Arguments:
        className -- string, The name of the Python component class

        Return:
        The Python component class

        """

        if className not in self.registeredComponents:
            raise Exception("Component with that class not registered:" + className)

        return self.registeredComponents[className]

    def reloadAllComponents(self):
        """Force the reload of all registered component modules

        Return:
        True if successful.

        """
        print "reloadAllComponents"
        modules = {}
        for name, componentClass in self.registeredComponents.iteritems():
            componentModulePath = componentClass.__module__
            module = sys.modules[componentModulePath]
            if module.__file__ not in modules:
                modules[module.__file__] = module

        self.registeredComponents = {}

        orderedModules = []
        def collectModuleHierarchy(module):
            print "collectModuleHierarchy:" + str(module.__name__)

            if str(module.__name__) == 'KrakenSystem':
                return

            if str(module.__name__) not in ('SceneItem', 'MathObject'):
                for name, cls in inspect.getmembers(module):
                    # print name
                    if inspect.isclass(cls):
                        baseClasses = inspect.getmro(cls)
                        # print baseClasses
                        # for i in xrange(0, len(list(baseClasses))):
                        for baseClass in list(baseClasses):
                            # print i
                            # baseClass = baseClasses[i]
                            baseModulePath = baseClass.__module__

                            print str(baseModulePath)
                            if str(baseModulePath) in ('__builtin__', '__builtins__', '__doc__', '__file__'):
                                break

                            baseModule = sys.modules[baseModulePath]
                            # print str(baseModule)
                            # importModule(baseModule)
                            if baseModule not in orderedModules:
                                orderedModules.append(baseModule)

            # if module.__file__ not in orderedModules:
            #     orderedModules.append(module)

        for path, module in modules.iteritems():
            collectModuleHierarchy(module)

        for module in reversed(orderedModules):
            print "relaod:" + str(module)
            reload(module)

        return True

    @classmethod
    def getInstance(cls):
        """This class method returns the singleton instance for the KrakenSystem

        Return:
        The singleton instance.

        """

        if cls.__instance is None:
            cls.__instance = KrakenSystem()

        return cls.__instance

ks = KrakenSystem.getInstance()
<|endoftext|>


<commit_before>#!/usr/bin/python

import sys, os

import mmap  # Thanks Steven @ http://stackoverflow.com/questions/4940032/search-for-string-in-txt-file-python

import subprocess

import readline

readline.set_completer_delims(' \t\n;')
readline.parse_and_bind("tab: complete")
readline.parse_and_bind("set match-hidden-files off")

import re

from pychord import Chord
from pychord import ChordProgression

halfTones = 0

def query(question, default):
    sys.stdout.write(question + " [" + default + "] ? ")
    choice = raw_input()
    if choice == '':
        return default
    return choice

def process( stringToProcess, processed ):
    print 'String to process "' + stringToProcess + '".'
    afterSplit = re.split(" |-|!|\.", stringToProcess, 1)  # 3rd parameter is maxsplit
    print afterSplit
    if len(afterSplit[0]) != 0:
        chord = Chord(afterSplit[0])
        print '* Extracted "' + chord.chord + '" chord.'
        chord.transpose( halfTones )
        print '* Transposed to "' + chord.chord + '" chord.'
        processed += chord.chord
    if len(afterSplit) == 1:
        return processed
    delimiterWas = stringToProcess[len(afterSplit[0]):-len(afterSplit[1])]
    print '* Delimiter was "' + delimiterWas + '".'
    processed += delimiterWas
    print '* Processed now "' + processed + '".'
    print '* Still must process "' + afterSplit[1] + '".'
    return process( afterSplit[1], processed )

def transpose(matchobj):
    # debug
    print "--- " + matchobj.group(0)
    #exceptions:
    if matchobj.group(0) == "(riff)":
        return matchobj.group(0)
    if matchobj.group(0).find("(chords") != -1:
        return matchobj.group(0)
    if matchobj.group(0).find("(Chords") != -1:
        return matchobj.group(0)
    #actual process:
    betweenParenthesis = matchobj.group(0).replace("(","").replace(")","")
    #print betweenParenthesis
    final = process( betweenParenthesis, "" )
    # debug
    print "+++ " + "(" + final + ")"
    return "(" + final + ")"

if __name__ == '__main__':

    print("-----------------------")
    print("Welcome to transposeDir")
    print("-----------------------")

    # Query song directory path string
    songDirectory = query("Please specify the path of the input song directory","/opt/Dropbox/lyrics/english")
    print("Will use song directory (input): " + songDirectory)

    # Query transposed song directory path string
    transposedSongDirectory = query("Please specify the path of the input song directory","/opt/Dropbox/lyrics/transposed_english")

    if os.path.isdir(transposedSongDirectory):
        yesNo = query('Path "' + transposedSongDirectory + '" already exists, are you sure (confirm with "yes" without quotes)','yes')
        if yesNo != "yes":
            print "Ok, bye!"
            quit()
        else:
            print("Will use (existing) transposed song directory (output): " + transposedSongDirectory)
    else:
        os.makedirs(transposedSongDirectory)
        print("Will use (newly created) transposed song directory (output): " + transposedSongDirectory)

    # Query transposition
    halfTones = int( query("Please specify half tones of transposition","0") )
    print("Will use half tones of transposition: " + str(halfTones))

    print("----------------------")

    for dirname, dirnames, filenames in os.walk( songDirectory ):
        for filename in sorted(filenames):
            #debug
            print filename
            name, extension = os.path.splitext(filename)
            songIn = open( os.path.join(dirname, filename) )
            songOut = open( os.path.join(transposedSongDirectory, filename), "w" )
            contents = songIn.read()
            contents = re.sub("\([^)]*\)", transpose, contents)
            songOut.write(contents)
            songOut.close()
            songIn.close()

<commit_msg>more output<commit_after>#!/usr/bin/python

import sys, os

import mmap  # Thanks Steven @ http://stackoverflow.com/questions/4940032/search-for-string-in-txt-file-python

import subprocess

import readline

readline.set_completer_delims(' \t\n;')
readline.parse_and_bind("tab: complete")
readline.parse_and_bind("set match-hidden-files off")

import re

from pychord import Chord
from pychord import ChordProgression

halfTones = 0

def query(question, default):
    sys.stdout.write(question + " [" + default + "] ? ")
    choice = raw_input()
    if choice == '':
        return default
    return choice

def process( stringToProcess, processed ):
    print 'String to process "' + stringToProcess + '".'
    afterSplit = re.split(" |-|!|\.", stringToProcess, 1)  # 3rd parameter is maxsplit
    print afterSplit
    if len(afterSplit[0]) != 0:
        chord = Chord(afterSplit[0])
        print '* Extracted "' + chord.chord + '" chord.'
        chord.transpose( halfTones )
        print '* Transposed to "' + chord.chord + '" chord.'
        processed += chord.chord
        print '* Processed now "' + processed + '".'
    if len(afterSplit) == 1:
        return processed
    delimiterWas = stringToProcess[len(afterSplit[0]):-len(afterSplit[1])]
    print '* Delimiter was "' + delimiterWas + '".'
    processed += delimiterWas
    print '* Processed now "' + processed + '".'
    print '* Still must process "' + afterSplit[1] + '".'
    return process( afterSplit[1], processed )

def transpose(matchobj):
    # debug
    print "--- " + matchobj.group(0)
    #exceptions:
    if matchobj.group(0) == "(riff)":
        return matchobj.group(0)
    if matchobj.group(0).find("(chords") != -1:
        return matchobj.group(0)
    if matchobj.group(0).find("(Chords") != -1:
        return matchobj.group(0)
    #actual process:
    betweenParenthesis = matchobj.group(0).replace("(","").replace(")","")
    #print betweenParenthesis
    final = process( betweenParenthesis, "" )
    # debug
    print "+++ " + "(" + final + ")"
    return "(" + final + ")"

if __name__ == '__main__':

    print("-----------------------")
    print("Welcome to transposeDir")
    print("-----------------------")

    # Query song directory path string
    songDirectory = query("Please specify the path of the input song directory","/opt/Dropbox/lyrics/english")
    print("Will use song directory (input): " + songDirectory)

    # Query transposed song directory path string
    transposedSongDirectory = query("Please specify the path of the input song directory","/opt/Dropbox/lyrics/transposed_english")

    if os.path.isdir(transposedSongDirectory):
        yesNo = query('Path "' + transposedSongDirectory + '" already exists, are you sure (confirm with "yes" without quotes)','yes')
        if yesNo != "yes":
            print "Ok, bye!"
            quit()
        else:
            print("Will use (existing) transposed song directory (output): " + transposedSongDirectory)
    else:
        os.makedirs(transposedSongDirectory)
        print("Will use (newly created) transposed song directory (output): " + transposedSongDirectory)

    # Query transposition
    halfTones = int( query("Please specify half tones of transposition","0") )
    print("Will use half tones of transposition: " + str(halfTones))

    print("----------------------")

    for dirname, dirnames, filenames in os.walk( songDirectory ):
        for filename in sorted(filenames):
            #debug
            print filename
            name, extension = os.path.splitext(filename)
            songIn = open( os.path.join(dirname, filename) )
            songOut = open( os.path.join(transposedSongDirectory, filename), "w" )
            contents = songIn.read()
            contents = re.sub("\([^)]*\)", transpose, contents)
            songOut.write(contents)
            songOut.close()
            songIn.close()

<|endoftext|>


<commit_before># SonicScrewdriver.py
# Version June 8, 2014

def addtodict(word, count, lexicon):
	'''Adds an integer (count) to dictionary (lexicon) under
	the key (word), or increments lexicon[word] if key present. '''

	if word in lexicon:
		lexicon[word] += count
	else:
		lexicon[word] = count

def sortkeysbyvalue(lexicon, whethertoreverse = False):
	'''Accepts a dictionary where keys point to a (presumably numeric) value, and
	returns a list of keys sorted by value.'''

	tuplelist = list()
	for key, value in lexicon.items():
		tuplelist.append((value, key))

	tuplelist = sorted(tuplelist, reverse = whethertoreverse)
	return tuplelist

## REVISED utility
## that reads my standard tab-separated metadata table,
## and returns three data objects: 1) a list of row indexes
## stored in the first column (e.g. volume ids).
## 2) a list of column names, and
## 3) a dictionary-of-dictionaries called table where
## table[columnname][rowindex] = the value of that cell.
## the difference here is thatthe first column, containing
## row indexes, is also returned as a column of the table.
## In the original version, it stupidly wasn't.
##
## This is equivalent to FileUtils.readtsv2

def readtsv(filepath):
    with open(filepath, encoding='utf-8') as file:
        filelines = file.readlines()

    header = filelines[0].rstrip()
    fieldnames = header.split('\t')
    numcolumns = len(fieldnames)
    indexfieldname = fieldnames[0]

    table = dict()
    indices = list()

    for i in range(0, numcolumns):
        table[fieldnames[i]] = dict()

    for line in filelines[1:]:
        line = line.rstrip()
        if len(line) < 1:
        	continue
        fields = line.split('\t')
        rowindex = fields[0]
        indices.append(rowindex)
        for thisfield in range(0, numcolumns):
            thiscolumn = fieldnames[thisfield]
            thisentry = fields[thisfield]
            table[thiscolumn][rowindex] = thisentry

    return indices, fieldnames, table

def writetsv(columns, rowindices, table, filepath):

    import os

    headerstring = ""
    numcols = len(columns)
    filebuffer = list()

    ## Only create a header if the file does not yet exist.

    if not os.path.exists(filepath):

        headerstring = ""
        for index, column in enumerate(columns):
            headerstring = headerstring + column
            if index < (numcols -1):
                headerstring += '\t'
            else:
                headerstring += '\n'

        filebuffer.append(headerstring)

    for rowindex in rowindices:
        rowstring = ""
        for idx, column in enumerate(columns):
            rowstring += table[column][rowindex]
            if idx < (numcols -1):
                rowstring += '\t'
            else:
                rowstring += '\n'

        filebuffer.append(rowstring)

    with open(filepath, mode='a', encoding = 'utf-8') as file:
        for line in filebuffer:
            file.write(line)

    return len(filebuffer)

def easywritetsv(columns, rowindices, table, filepath):
    '''This version does not assume the table contains a dict for rowindices'''
    firstcolumn = columns[0]
    table[firstcolumn] = dict()
    for idx in rowindices:
        table[firstcolumn][idx] = idx

    import os

    headerstring = ""
    numcols = len(columns)
    filebuffer = list()

    ## Only create a header if the file does not yet exist.

    if not os.path.exists(filepath):

        headerstring = ""
        for index, column in enumerate(columns):
            headerstring = headerstring + column
            if index < (numcols -1):
                headerstring += '\t'
            else:
                headerstring += '\n'

        filebuffer.append(headerstring)

    for rowindex in rowindices:
        rowstring = ""
        for idx, column in enumerate(columns):
            rowstring += table[column][rowindex]
            if idx < (numcols -1):
                rowstring += '\t'
            else:
                rowstring += '\n'

        filebuffer.append(rowstring)

    with open(filepath, mode='a', encoding = 'utf-8') as file:
        for line in filebuffer:
            file.write(line)

    return len(filebuffer)

def pairtreefile(htid):
    ''' Given a dirty htid, returns a clean one that can be used
    as a filename.'''

    if ':' in htid or '/' in htid:
        htid = htid.replace(':','+')
        htid = htid.replace('/','=')

    return htid

def pairtreelabel(htid):
    ''' Given a clean htid, returns a dirty one that will match
    the metadata table.'''

    if '+' in htid or '=' in htid:
        htid = htid.replace('+',':')
        htid = htid.replace('=','/')

    return htid
<commit_msg>New sonic screwdriver.<commit_after># SonicScrewdriver.py
# Version July 4, 2014

def addtodict(word, count, lexicon):
	'''Adds an integer (count) to dictionary (lexicon) under
	the key (word), or increments lexicon[word] if key present. '''

	if word in lexicon:
		lexicon[word] += count
	else:
		lexicon[word] = count

def sortkeysbyvalue(lexicon, whethertoreverse = False):
	'''Accepts a dictionary where keys point to a (presumably numeric) value, and
	returns a list of keys sorted by value.'''

	tuplelist = list()
	for key, value in lexicon.items():
		tuplelist.append((value, key))

	tuplelist = sorted(tuplelist, reverse = whethertoreverse)
	return tuplelist

def sortvaluesbykey(lexicon):
    '''Accepts a dictionary of numeric keys, such as page numbers, and
    returns a tuplelist of key-value pairs sorted by the key.'''

    tuplelist = list()
    for key, value in lexicon.items():
        tuplelist.append((key, value))

    tuplelist = sorted(tuplelist)
    return tuplelist

def add_dicts(source, target):
    '''Adds the values in source to corresponding
    keys in target, or creates them if missing.'''

    for key, value in source.items():
        if key in target:
            target[key] += value
        else:
            target[key] = value

## REVISED utility
## that reads my standard tab-separated metadata table,
## and returns three data objects: 1) a list of row indexes
## stored in the first column (e.g. volume ids).
## 2) a list of column names, and
## 3) a dictionary-of-dictionaries called table where
## table[columnname][rowindex] = the value of that cell.
## the difference here is thatthe first column, containing
## row indexes, is also returned as a column of the table.
## In the original version, it stupidly wasn't.
##
## This is equivalent to FileUtils.readtsv2

def readtsv(filepath):
    with open(filepath, encoding='utf-8') as file:
        filelines = file.readlines()

    header = filelines[0].rstrip()
    fieldnames = header.split('\t')
    numcolumns = len(fieldnames)
    indexfieldname = fieldnames[0]

    table = dict()
    indices = list()

    for i in range(0, numcolumns):
        table[fieldnames[i]] = dict()

    for line in filelines[1:]:
        line = line.rstrip()
        if len(line) < 1:
        	continue
        fields = line.split('\t')
        rowindex = fields[0]
        indices.append(rowindex)
        for thisfield in range(0, numcolumns):
            thiscolumn = fieldnames[thisfield]
            thisentry = fields[thisfield]
            table[thiscolumn][rowindex] = thisentry

    return indices, fieldnames, table

def writetsv(columns, rowindices, table, filepath):

    import os

    headerstring = ""
    numcols = len(columns)
    filebuffer = list()

    ## Only create a header if the file does not yet exist.

    if not os.path.exists(filepath):

        headerstring = ""
        for index, column in enumerate(columns):
            headerstring = headerstring + column
            if index < (numcols -1):
                headerstring += '\t'
            else:
                headerstring += '\n'

        filebuffer.append(headerstring)

    for rowindex in rowindices:
        rowstring = ""
        for idx, column in enumerate(columns):
            rowstring += table[column][rowindex]
            if idx < (numcols -1):
                rowstring += '\t'
            else:
                rowstring += '\n'

        filebuffer.append(rowstring)

    with open(filepath, mode='a', encoding = 'utf-8') as file:
        for line in filebuffer:
            file.write(line)

    return len(filebuffer)

def easywritetsv(columns, rowindices, table, filepath):
    '''This version does not assume the table contains a dict for rowindices'''
    firstcolumn = columns[0]
    table[firstcolumn] = dict()
    for idx in rowindices:
        table[firstcolumn][idx] = idx

    import os

    headerstring = ""
    numcols = len(columns)
    filebuffer = list()

    ## Only create a header if the file does not yet exist.

    if not os.path.exists(filepath):

        headerstring = ""
        for index, column in enumerate(columns):
            headerstring = headerstring + column
            if index < (numcols -1):
                headerstring += '\t'
            else:
                headerstring += '\n'

        filebuffer.append(headerstring)

    for rowindex in rowindices:
        rowstring = ""
        for idx, column in enumerate(columns):
            rowstring += table[column][rowindex]
            if idx < (numcols -1):
                rowstring += '\t'
            else:
                rowstring += '\n'

        filebuffer.append(rowstring)

    with open(filepath, mode='a', encoding = 'utf-8') as file:
        for line in filebuffer:
            file.write(line)

    return len(filebuffer)

def pairtreefile(htid):
    ''' Given a dirty htid, returns a clean one that can be used
    as a filename.'''

    if ':' in htid or '/' in htid:
        htid = htid.replace(':','+')
        htid = htid.replace('/','=')

    return htid

def pairtreelabel(htid):
    ''' Given a clean htid, returns a dirty one that will match
    the metadata table.'''

    if '+' in htid or '=' in htid:
        htid = htid.replace('+',':')
        htid = htid.replace('=','/')

    return htid
<|endoftext|>


<commit_before>#!/usr/bin/python

import sys, os

import mmap  # Thanks Steven @ http://stackoverflow.com/questions/4940032/search-for-string-in-txt-file-python

import subprocess

import readline

readline.set_completer_delims(' \t\n;')
readline.parse_and_bind("tab: complete")
readline.parse_and_bind("set match-hidden-files off")

import re

from pychord import Chord
from pychord import ChordProgression

halfTones = 0

def query(question, default):
    sys.stdout.write(question + " [" + default + "] ? ")
    choice = raw_input()
    if choice == '':
        return default
    return choice

def process( stringToProcess, processed ):
    #print 'String to process "' + stringToProcess + '".'
    afterSplit = re.split("  |-|!|\.\.\.|\.\.|: |high|open|bass|riff|m6|7sus2", stringToProcess, 1)  # 3rd parameter is maxsplit # Also works with single space, do this to catch faulty txt.
    #print '* Split by delimiters "' + str(afterSplit) + '".'
    if len(afterSplit[0]) != 0:
        chord = Chord(afterSplit[0])
        #print '* Extracted "' + chord.chord + '" chord.'
        chord.transpose( halfTones )
        #print '* Transposed to "' + chord.chord + '" chord.'
        processed += chord.chord
        #print '* Processed after chord "' + processed + '".'
    #else:
        #print '* No chord to extract.'
    if len(afterSplit) == 1:
        return processed
    delimiterWas = ''
    if len(afterSplit[1]) == 0:
        delimiterWas = stringToProcess[len(afterSplit[0]):]
    else:
        delimiterWas = stringToProcess[len(afterSplit[0]):-len(afterSplit[1])]
    #print '* Delimiter was "' + delimiterWas + '".'
    processed += delimiterWas
    #print '* Processed after delimiter "' + processed + '".'
    #print '* Still must process "' + afterSplit[1] + '".'
    return process( afterSplit[1], processed )

def transpose(matchobj):
    # debug
    print "--- " + matchobj.group(0)
    #exceptions:
    if matchobj.group(0).find("(chords") != -1:
        return matchobj.group(0)
    if matchobj.group(0).find("(Chords") != -1:
        return matchobj.group(0)
    if matchobj.group(0).find("bpm") != -1:
        return matchobj.group(0)
    if matchobj.group(0).find("(all") != -1:
        return matchobj.group(0)
    if matchobj.group(0).find("(mute)") != -1:
        return matchobj.group(0)
    #actual process:
    betweenParenthesis = matchobj.group(0).replace("(","").replace(")","")
    #print betweenParenthesis
    final = process( betweenParenthesis, "" )
    # debug
    print "+++ " + "(" + final + ")"
    return "(" + final + ")"

if __name__ == '__main__':

    print("-----------------------")
    print("Welcome to transposeDir")
    print("-----------------------")

    # Query song directory path string
    songDirectory = query("Please specify the path of the input song directory","/opt/Dropbox/lyrics/english")
    print("Will use song directory (input): " + songDirectory)

    # Query transposed song directory path string
    transposedSongDirectory = query("Please specify the path of the input song directory","/opt/Dropbox/lyrics/transposed_english")

    if os.path.isdir(transposedSongDirectory):
        yesNo = query('Path "' + transposedSongDirectory + '" already exists, are you sure (confirm with "y" or "yes" without quotes)','yes')
        if yesNo != "yes" and yesNo != "y":
            print "Ok, bye!"
            quit()
        else:
            print("Will use (existing) transposed song directory (output): " + transposedSongDirectory)
    else:
        os.makedirs(transposedSongDirectory)
        print("Will use (newly created) transposed song directory (output): " + transposedSongDirectory)

    # Query transposition
    halfTones = int( query("Please specify half tones of transposition","0") )
    print("Will use half tones of transposition: " + str(halfTones))

    print("----------------------")

    for dirname, dirnames, filenames in os.walk( songDirectory ):
        for filename in sorted(filenames):
            #debug
            print filename
            name, extension = os.path.splitext(filename)
            songIn = open( os.path.join(dirname, filename) )
            songOut = open( os.path.join(transposedSongDirectory, filename), "w" )
            contents = songIn.read()
            contents = re.sub("\([^)]*\)", transpose, contents)
            songOut.write(contents)
            songOut.close()
            songIn.close()

<commit_msg>remove 1, allow notes<commit_after>#!/usr/bin/python

import sys, os

import mmap  # Thanks Steven @ http://stackoverflow.com/questions/4940032/search-for-string-in-txt-file-python

import subprocess

import readline

readline.set_completer_delims(' \t\n;')
readline.parse_and_bind("tab: complete")
readline.parse_and_bind("set match-hidden-files off")

import re

from pychord import Chord
from pychord import ChordProgression

halfTones = 0

def query(question, default):
    sys.stdout.write(question + " [" + default + "] ? ")
    choice = raw_input()
    if choice == '':
        return default
    return choice

def process( stringToProcess, processed ):
    #print 'String to process "' + stringToProcess + '".'
    afterSplit = re.split("  |-|!|\.\.\.|\.\.|: |high|open|bass|riff|notes|m6|7sus2", stringToProcess, 1)  # 3rd parameter is maxsplit # Also works with single space, do this to catch faulty txt.
    #print '* Split by delimiters "' + str(afterSplit) + '".'
    if len(afterSplit[0]) != 0:
        chord = Chord(afterSplit[0])
        #print '* Extracted "' + chord.chord + '" chord.'
        chord.transpose( halfTones )
        #print '* Transposed to "' + chord.chord + '" chord.'
        processed += chord.chord
        #print '* Processed after chord "' + processed + '".'
    #else:
        #print '* No chord to extract.'
    if len(afterSplit) == 1:
        return processed
    delimiterWas = ''
    if len(afterSplit[1]) == 0:
        delimiterWas = stringToProcess[len(afterSplit[0]):]
    else:
        delimiterWas = stringToProcess[len(afterSplit[0]):-len(afterSplit[1])]
    #print '* Delimiter was "' + delimiterWas + '".'
    processed += delimiterWas
    #print '* Processed after delimiter "' + processed + '".'
    #print '* Still must process "' + afterSplit[1] + '".'
    return process( afterSplit[1], processed )

def transpose(matchobj):
    # debug
    print "--- " + matchobj.group(0)
    #exceptions:
    if matchobj.group(0).find("(chords") != -1:
        return matchobj.group(0)
    if matchobj.group(0).find("(Chords") != -1:
        return matchobj.group(0)
    if matchobj.group(0).find("bpm") != -1:
        return matchobj.group(0)
    if matchobj.group(0).find("(all") != -1:
        return matchobj.group(0)
    if matchobj.group(0).find("(mute)") != -1:
        return matchobj.group(0)
    #actual process:
    betweenParenthesis = matchobj.group(0).replace("(","").replace(")","")
    #print betweenParenthesis
    final = process( betweenParenthesis, "" )
    # debug
    print "+++ " + "(" + final + ")"
    return "(" + final + ")"

if __name__ == '__main__':

    print("-----------------------")
    print("Welcome to transposeDir")
    print("-----------------------")

    # Query song directory path string
    songDirectory = query("Please specify the path of the input song directory","/opt/Dropbox/lyrics/english")
    print("Will use song directory (input): " + songDirectory)

    # Query transposed song directory path string
    transposedSongDirectory = query("Please specify the path of the input song directory","/opt/Dropbox/lyrics/transposed_english")

    if os.path.isdir(transposedSongDirectory):
        yesNo = query('Path "' + transposedSongDirectory + '" already exists, are you sure (confirm with "y" or "yes" without quotes)','yes')
        if yesNo != "yes" and yesNo != "y":
            print "Ok, bye!"
            quit()
        else:
            print("Will use (existing) transposed song directory (output): " + transposedSongDirectory)
    else:
        os.makedirs(transposedSongDirectory)
        print("Will use (newly created) transposed song directory (output): " + transposedSongDirectory)

    # Query transposition
    halfTones = int( query("Please specify half tones of transposition","0") )
    print("Will use half tones of transposition: " + str(halfTones))

    print("----------------------")

    for dirname, dirnames, filenames in os.walk( songDirectory ):
        for filename in sorted(filenames):
            #debug
            print filename
            name, extension = os.path.splitext(filename)
            songIn = open( os.path.join(dirname, filename) )
            songOut = open( os.path.join(transposedSongDirectory, filename), "w" )
            contents = songIn.read()
            contents = re.sub("\([^)]*\)", transpose, contents)
            songOut.write(contents)
            songOut.close()
            songIn.close()

<|endoftext|>


<commit_before>import topo
import param
import imagen

from topo import projection, responsefn, learningfn, transferfn, sheet
import topo.learningfn.optimized
import topo.transferfn.optimized
import topo.responsefn.optimized
import topo.sheet.optimized
import topo.transferfn.misc

from topo.submodel import Model, order_projections
from topo.submodel.earlyvision import ColorEarlyVisionModel

class ModelGCAL(ColorEarlyVisionModel):

    cortex_density=param.Number(default=47.0,bounds=(0,None),
        inclusive_bounds=(False,True),doc="""
        The nominal_density to use for V1.""")

    homeostasis = param.Boolean(default=True, doc="""
        Whether or not the homeostatic adaption should be applied in V1"""),

    t_init = param.Number(default=0.15, doc="""
        The initial V1 threshold value. This value is static in the L and GCL models
        and adaptive in the AL and GCAL models.""")

    latexc_radius=param.Number(default=0.104,bounds=(0,None),doc="""
        Size of the radius of the lateral excitatory connections within V1.""")

    latinh_radius=param.Number(default=0.22917,bounds=(0,None),doc="""
        Size of the radius of the lateral inhibitory connections within V1.""")

    aff_lr=param.Number(default=0.1,bounds=(0.0,None),doc="""
        Learning rate for the afferent projection(s) to V1.""")

    exc_lr=param.Number(default=0.0,bounds=(0.0,None),doc="""
        Learning rate for the lateral excitatory projection to V1.""")

    inh_lr=param.Number(default=0.3,bounds=(0.0,None),doc="""
        Learning rate for the lateral inhibitory projection to V1.""")

    aff_strength=param.Number(default=1.0,bounds=(0.0,None),doc="""
        Overall strength of the afferent projection to V1.""")

    exc_strength=param.Number(default=1.7,bounds=(0.0,None),doc="""
        Overall strength of the lateral excitatory projection to V1.""")

    inh_strength=param.Number(default=1.4,bounds=(0.0,None),doc="""
        Overall strength of the lateral inhibitory projection to V1.""")

    expand_sf_test_range=param.Boolean(default=False,doc="""
        By default, measure_sine_pref() measures SF at the sizes of RF
        used, for speed, but if expand_sf_test_range is True, it will
        test over a larger range, including half the size of the
        smallest and twice the size of the largest.""")

    def property_setup(self, properties):
        properties = super(ModelGCAL, self).property_setup(properties)
        "Specify weight initialization, response function, and learning function"

        projection.CFProjection.cf_shape=imagen.Disk(smoothing=0.0)
        projection.CFProjection.response_fn=responsefn.optimized.CFPRF_DotProduct_opt()
        projection.CFProjection.learning_fn=learningfn.optimized.CFPLF_Hebbian_opt()
        projection.CFProjection.weights_output_fns=[transferfn.optimized.CFPOF_DivisiveNormalizeL1_opt()]
        projection.SharedWeightCFProjection.response_fn=responsefn.optimized.CFPRF_DotProduct_opt()
        return properties

    def sheet_setup(self):
        sheets = super(ModelGCAL,self).sheet_setup()
        sheets['V1'] = [{}]
        return sheets

    @Model.SettlingCFSheet
    def V1(self, properties):
        return Model.SettlingCFSheet.params(
            tsettle=16,
            plastic=True,
            joint_norm_fn=topo.sheet.optimized.compute_joint_norm_totals_opt,
            output_fns=[transferfn.misc.HomeostaticResponse(t_init=self.t_init,
                                                            learning_rate=0.01 if self.homeostasis else 0.0)],
            nominal_density=self.cortex_density,
            nominal_bounds=sheet.BoundingBox(radius=self.area/2.0))

    @Model.matchconditions('V1', 'V1_afferent')
    def V1_afferent_conditions(self, properties):
        return {'level': 'LGN'}

    @Model.CFProjection
    def V1_afferent(self, src_properties, dest_properties):
        sf_channel = src_properties['SF'] if 'SF' in src_properties else 1
        # Adjust delays so same measurement protocol can be used with and without gain control.
        LGN_V1_delay = 0.05 if self.gain_control else 0.10

        name=''
        if 'eye' in src_properties: name+=src_properties['eye']
        if 'opponent' in src_properties:
            name+=src_properties['opponent']+src_properties['surround']
        name+=('LGN'+src_properties['polarity']+'Afferent')
        if sf_channel>1: name+=('SF'+str(src_properties['SF']))

        gaussian_size = 2.0 * self.v1aff_radius *self.sf_spacing**(sf_channel-1)
        weights_generator = imagen.random.GaussianCloud(gaussian_size=gaussian_size)

        return [Model.CFProjection.params(
                delay=LGN_V1_delay+lag,
                dest_port=('Activity','JointNormalize','Afferent'),
                name= name if lag==0 else name+('Lag'+str(lag)),
                learning_rate=self.aff_lr,
                strength=self.aff_strength*(1.0 if not self.gain_control else 1.5),
                weights_generator=weights_generator,
                nominal_bounds_template=sheet.BoundingBox(radius=
                                            self.v1aff_radius*self.sf_spacing**(sf_channel-1)))
                for lag in self['lags']]

    @Model.matchconditions('V1', 'lateral_excitatory')
    def lateral_excitatory_conditions(self, properties):
        return {'level': 'V1'}

    @Model.CFProjection
    def lateral_excitatory(self, src_properties, dest_properties):
        return Model.CFProjection.params(
            delay=0.05,
            name='LateralExcitatory',
            weights_generator=imagen.Gaussian(aspect_ratio=1.0, size=0.05),
            strength=self.exc_strength,
            learning_rate=self.exc_lr,
            nominal_bounds_template=sheet.BoundingBox(radius=self.latexc_radius))

    @Model.matchconditions('V1', 'lateral_inhibitory')
    def lateral_inhibitory_conditions(self, properties):
        return {'level': 'V1'}

    @Model.CFProjection
    def lateral_inhibitory(self, src_properties, dest_properties):
        return Model.CFProjection.params(
            delay=0.05,
            name='LateralInhibitory',
            weights_generator=imagen.random.GaussianCloud(gaussian_size=0.15),
            strength=-1.0*self.inh_strength,
            learning_rate=self.inh_lr,
            nominal_bounds_template=sheet.BoundingBox(radius=self.latinh_radius))

    def analysis_setup(self):
        # TODO: This is different in gcal.ty, stevens/gcal.ty and gcal_od.ty
        # And depends whether gain control is used or not
        import topo.analysis.featureresponses
        topo.analysis.featureresponses.FeatureMaps.selectivity_multiplier=2.0
        topo.analysis.featureresponses.FeatureCurveCommand.contrasts=[1, 10, 30, 50, 100]
        if 'dr' in self.dims:
            topo.analysis.featureresponses.MeasureResponseCommand.durations=[(max(self['lags'])+1)*1.0]
        if 'sf' in self.dims:
            from topo.analysis.command import measure_sine_pref
            sf_relative_sizes = [self.sf_spacing**(sf_channel-1) for sf_channel in self['SF']]
            wide_relative_sizes=[0.5*sf_relative_sizes[0]] + sf_relative_sizes + [2.0*sf_relative_sizes[-1]]
            relative_sizes=(wide_relative_sizes if self.expand_sf_test_range else sf_relative_sizes)
            #The default 2.4 spatial frequency value here is
            #chosen because it results in a sine grating with bars whose
            #width approximately matches the width of the Gaussian training
            #patterns, and thus the typical width of an ON stripe in one of the
            #receptive fields
            measure_sine_pref.frequencies = [2.4*s for s in relative_sizes]

class ExamplesGCAL(ModelGCAL):
    """
    Reproduces the results of the legacy examples/gcal.ty file.
    """

    def __init__(self, **params):
        super(ExamplesGCAL, self).__init__(time_dependent=False, **params)
        if set(self.dims) != set([ 'xy', 'or']):
            raise Exception("ExamplesGCAL only reproducible for dims = ['xy', 'or']")

    def setup(self,setup_options):
        model = super(ExamplesGCAL, self).setup(setup_options)
        if setup_options is True or 'sheets' in setup_options:
            model.sheets.Retina.update(nominal_bounds=sheet.BoundingBox(radius=self.area/2.0+1.125))
            model.sheets.LGNOn.update(nominal_bounds=sheet.BoundingBox(radius=self.area/2.0+0.75))
            model.sheets.LGNOff.update(nominal_bounds=sheet.BoundingBox(radius=self.area/2.0+0.75))
            model.sheets.V1.update(nominal_density=48)
        if setup_options is True or 'projections' in setup_options:
            order_projections(model, ['afferent',
                                      'lateral_gain_control',
                                      ('V1_afferent', {'polarity':'On'}),
                                      ('V1_afferent', {'polarity':'Off'}),
                                      'lateral_excitatory',
                                      'lateral_inhibitory'])
        return model
<commit_msg>Fixed signature of the ExamplesGCAL setup method<commit_after>import topo
import param
import imagen

from topo import projection, responsefn, learningfn, transferfn, sheet
import topo.learningfn.optimized
import topo.transferfn.optimized
import topo.responsefn.optimized
import topo.sheet.optimized
import topo.transferfn.misc

from topo.submodel import Model, order_projections
from topo.submodel.earlyvision import ColorEarlyVisionModel

class ModelGCAL(ColorEarlyVisionModel):

    cortex_density=param.Number(default=47.0,bounds=(0,None),
        inclusive_bounds=(False,True),doc="""
        The nominal_density to use for V1.""")

    homeostasis = param.Boolean(default=True, doc="""
        Whether or not the homeostatic adaption should be applied in V1"""),

    t_init = param.Number(default=0.15, doc="""
        The initial V1 threshold value. This value is static in the L and GCL models
        and adaptive in the AL and GCAL models.""")

    latexc_radius=param.Number(default=0.104,bounds=(0,None),doc="""
        Size of the radius of the lateral excitatory connections within V1.""")

    latinh_radius=param.Number(default=0.22917,bounds=(0,None),doc="""
        Size of the radius of the lateral inhibitory connections within V1.""")

    aff_lr=param.Number(default=0.1,bounds=(0.0,None),doc="""
        Learning rate for the afferent projection(s) to V1.""")

    exc_lr=param.Number(default=0.0,bounds=(0.0,None),doc="""
        Learning rate for the lateral excitatory projection to V1.""")

    inh_lr=param.Number(default=0.3,bounds=(0.0,None),doc="""
        Learning rate for the lateral inhibitory projection to V1.""")

    aff_strength=param.Number(default=1.0,bounds=(0.0,None),doc="""
        Overall strength of the afferent projection to V1.""")

    exc_strength=param.Number(default=1.7,bounds=(0.0,None),doc="""
        Overall strength of the lateral excitatory projection to V1.""")

    inh_strength=param.Number(default=1.4,bounds=(0.0,None),doc="""
        Overall strength of the lateral inhibitory projection to V1.""")

    expand_sf_test_range=param.Boolean(default=False,doc="""
        By default, measure_sine_pref() measures SF at the sizes of RF
        used, for speed, but if expand_sf_test_range is True, it will
        test over a larger range, including half the size of the
        smallest and twice the size of the largest.""")

    def property_setup(self, properties):
        properties = super(ModelGCAL, self).property_setup(properties)
        "Specify weight initialization, response function, and learning function"

        projection.CFProjection.cf_shape=imagen.Disk(smoothing=0.0)
        projection.CFProjection.response_fn=responsefn.optimized.CFPRF_DotProduct_opt()
        projection.CFProjection.learning_fn=learningfn.optimized.CFPLF_Hebbian_opt()
        projection.CFProjection.weights_output_fns=[transferfn.optimized.CFPOF_DivisiveNormalizeL1_opt()]
        projection.SharedWeightCFProjection.response_fn=responsefn.optimized.CFPRF_DotProduct_opt()
        return properties

    def sheet_setup(self):
        sheets = super(ModelGCAL,self).sheet_setup()
        sheets['V1'] = [{}]
        return sheets

    @Model.SettlingCFSheet
    def V1(self, properties):
        return Model.SettlingCFSheet.params(
            tsettle=16,
            plastic=True,
            joint_norm_fn=topo.sheet.optimized.compute_joint_norm_totals_opt,
            output_fns=[transferfn.misc.HomeostaticResponse(t_init=self.t_init,
                                                            learning_rate=0.01 if self.homeostasis else 0.0)],
            nominal_density=self.cortex_density,
            nominal_bounds=sheet.BoundingBox(radius=self.area/2.0))

    @Model.matchconditions('V1', 'V1_afferent')
    def V1_afferent_conditions(self, properties):
        return {'level': 'LGN'}

    @Model.CFProjection
    def V1_afferent(self, src_properties, dest_properties):
        sf_channel = src_properties['SF'] if 'SF' in src_properties else 1
        # Adjust delays so same measurement protocol can be used with and without gain control.
        LGN_V1_delay = 0.05 if self.gain_control else 0.10

        name=''
        if 'eye' in src_properties: name+=src_properties['eye']
        if 'opponent' in src_properties:
            name+=src_properties['opponent']+src_properties['surround']
        name+=('LGN'+src_properties['polarity']+'Afferent')
        if sf_channel>1: name+=('SF'+str(src_properties['SF']))

        gaussian_size = 2.0 * self.v1aff_radius *self.sf_spacing**(sf_channel-1)
        weights_generator = imagen.random.GaussianCloud(gaussian_size=gaussian_size)

        return [Model.CFProjection.params(
                delay=LGN_V1_delay+lag,
                dest_port=('Activity','JointNormalize','Afferent'),
                name= name if lag==0 else name+('Lag'+str(lag)),
                learning_rate=self.aff_lr,
                strength=self.aff_strength*(1.0 if not self.gain_control else 1.5),
                weights_generator=weights_generator,
                nominal_bounds_template=sheet.BoundingBox(radius=
                                            self.v1aff_radius*self.sf_spacing**(sf_channel-1)))
                for lag in self['lags']]

    @Model.matchconditions('V1', 'lateral_excitatory')
    def lateral_excitatory_conditions(self, properties):
        return {'level': 'V1'}

    @Model.CFProjection
    def lateral_excitatory(self, src_properties, dest_properties):
        return Model.CFProjection.params(
            delay=0.05,
            name='LateralExcitatory',
            weights_generator=imagen.Gaussian(aspect_ratio=1.0, size=0.05),
            strength=self.exc_strength,
            learning_rate=self.exc_lr,
            nominal_bounds_template=sheet.BoundingBox(radius=self.latexc_radius))

    @Model.matchconditions('V1', 'lateral_inhibitory')
    def lateral_inhibitory_conditions(self, properties):
        return {'level': 'V1'}

    @Model.CFProjection
    def lateral_inhibitory(self, src_properties, dest_properties):
        return Model.CFProjection.params(
            delay=0.05,
            name='LateralInhibitory',
            weights_generator=imagen.random.GaussianCloud(gaussian_size=0.15),
            strength=-1.0*self.inh_strength,
            learning_rate=self.inh_lr,
            nominal_bounds_template=sheet.BoundingBox(radius=self.latinh_radius))

    def analysis_setup(self):
        # TODO: This is different in gcal.ty, stevens/gcal.ty and gcal_od.ty
        # And depends whether gain control is used or not
        import topo.analysis.featureresponses
        topo.analysis.featureresponses.FeatureMaps.selectivity_multiplier=2.0
        topo.analysis.featureresponses.FeatureCurveCommand.contrasts=[1, 10, 30, 50, 100]
        if 'dr' in self.dims:
            topo.analysis.featureresponses.MeasureResponseCommand.durations=[(max(self['lags'])+1)*1.0]
        if 'sf' in self.dims:
            from topo.analysis.command import measure_sine_pref
            sf_relative_sizes = [self.sf_spacing**(sf_channel-1) for sf_channel in self['SF']]
            wide_relative_sizes=[0.5*sf_relative_sizes[0]] + sf_relative_sizes + [2.0*sf_relative_sizes[-1]]
            relative_sizes=(wide_relative_sizes if self.expand_sf_test_range else sf_relative_sizes)
            #The default 2.4 spatial frequency value here is
            #chosen because it results in a sine grating with bars whose
            #width approximately matches the width of the Gaussian training
            #patterns, and thus the typical width of an ON stripe in one of the
            #receptive fields
            measure_sine_pref.frequencies = [2.4*s for s in relative_sizes]

class ExamplesGCAL(ModelGCAL):
    """
    Reproduces the results of the legacy examples/gcal.ty file.
    """

    def __init__(self, **params):
        super(ExamplesGCAL, self).__init__(time_dependent=False, **params)
        if set(self.dims) != set([ 'xy', 'or']):
            raise Exception("ExamplesGCAL only reproducible for dims = ['xy', 'or']")

    def setup(self,setup_options=True):
        model = super(ExamplesGCAL, self).setup(setup_options)
        if setup_options is True or 'sheets' in setup_options:
            model.sheets.Retina.update(nominal_bounds=sheet.BoundingBox(radius=self.area/2.0+1.125))
            model.sheets.LGNOn.update(nominal_bounds=sheet.BoundingBox(radius=self.area/2.0+0.75))
            model.sheets.LGNOff.update(nominal_bounds=sheet.BoundingBox(radius=self.area/2.0+0.75))
            model.sheets.V1.update(nominal_density=48)
        if setup_options is True or 'projections' in setup_options:
            order_projections(model, ['afferent',
                                      'lateral_gain_control',
                                      ('V1_afferent', {'polarity':'On'}),
                                      ('V1_afferent', {'polarity':'Off'}),
                                      'lateral_excitatory',
                                      'lateral_inhibitory'])
        return model
<|endoftext|>


<commit_before>from . import data
from . import datasets
from . import utils

__version__ = '0.2.2'

__all__ = ['data',
           'datasets',
           'utils']
<commit_msg>bump version for pypi fix<commit_after>from . import data
from . import datasets
from . import utils

__version__ = '0.2.3'

__all__ = ['data',
           'datasets',
           'utils']
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
#
# Copyright (C) 2015 Edgewall Software
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at http://trac.edgewall.org/wiki/TracLicense.
#
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://trac.edgewall.org/log/.

import os

def do_upgrade(env, version, cursor):
    authz_file = env.config.get('authz_policy', 'authz_file')
    if authz_file and not os.path.isabs(authz_file):
        parts = os.path.split(authz_file)
        if len(parts) == 2 and parts[0] == 'conf':
            env.config.set('authz_policy', 'authz_file', parts[1])
            env.config.save()
<commit_msg>1.1.4dev: Backup the config file during upgrade step. Refs #11529.<commit_after># -*- coding: utf-8 -*-
#
# Copyright (C) 2015 Edgewall Software
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at http://trac.edgewall.org/wiki/TracLicense.
#
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://trac.edgewall.org/log/.

import os

from trac.upgrades import backup_config_file

def do_upgrade(env, version, cursor):
    authz_file = env.config.get('authz_policy', 'authz_file')
    if authz_file and not os.path.isabs(authz_file):
        parts = os.path.split(authz_file)
        if len(parts) == 2 and parts[0] == 'conf':
            env.config.set('authz_policy', 'authz_file', parts[1])
            backup_config_file(env, '.db36.bak')
            env.config.save()
<|endoftext|>


<commit_before>#!/usr/bin/env python3
import sys
import subprocess
import re
import os

class GmPublish:
	"""Wrapper for the GMPublish program"""

	def __init__(self, addon):
		self.addon = addon

	def create(self, logo):
		"""Upload to the workshop as a new addon.
		precondition: Assumes that the files of the addon have been verified.
		"""
		outfile = 'temp.gma'
		self.addon.compress(outfile)

		# Call gmpublish to create the addon
		output = subprocess.check_output([self._get_executable(), 'create',
			'-addon', outfile,
			'-icon', logo
		])

		output = output.decode('utf-8')
		match = re.search('UID: ([0-9]+)', output)

		# Try to find the addon ID
		if match and match.group(1):
			self.addon.set_workshopid(int(match.group(1)))
			print("Publishing to workshop succeeded!")
			print("Workshop ID set to", match.group(1))
		else:
			print("Publishing to workshop failed!")
			print(output)

		os.remove(outfile)

	def update(self, message=None):
		"""Push an update of the addon to the workshop"""
		message = message or self.addon.getdefault_changelog()
		outfile = 'temp.gma'
		self.addon.compress(outfile)

		# Call GMPublish to update
		output = subprocess.check_output([self._get_executable(), 'update',
			'-addon', outfile,
			'-id', str(self.addon.getworkshopid()),
			'-changes', message
		])

		print(output.decode('utf-8'))

		os.remove(outfile)

	def _get_executable(self):
		"""Retrieve the path of the gmpublish executable"""
		platform = sys.platform
		if platform == 'linux':
			return 'gmpublish_linux'
		elif platform == 'windows':
			return 'gmpublish.exe'
		elif platform == 'darwin': #untested
			return 'gmpublish_osx'

		raise NameError("Could not find executable for platform " + platform)<commit_msg>Fixed Windows platform error<commit_after>#!/usr/bin/env python3
import sys
import subprocess
import re
import os

class GmPublish:
	"""Wrapper for the GMPublish program"""

	def __init__(self, addon):
		self.addon = addon

	def create(self, logo):
		"""Upload to the workshop as a new addon.
		precondition: Assumes that the files of the addon have been verified.
		"""
		outfile = 'temp.gma'
		self.addon.compress(outfile)

		# Call gmpublish to create the addon
		output = subprocess.check_output([self._get_executable(), 'create',
			'-addon', outfile,
			'-icon', logo
		])

		output = output.decode('utf-8')
		match = re.search('UID: ([0-9]+)', output)

		# Try to find the addon ID
		if match and match.group(1):
			self.addon.set_workshopid(int(match.group(1)))
			print("Publishing to workshop succeeded!")
			print("Workshop ID set to", match.group(1))
		else:
			print("Publishing to workshop failed!")
			print(output)

		os.remove(outfile)

	def update(self, message=None):
		"""Push an update of the addon to the workshop"""
		message = message or self.addon.getdefault_changelog()
		outfile = 'temp.gma'
		self.addon.compress(outfile)

		# Call GMPublish to update
		output = subprocess.check_output([self._get_executable(), 'update',
			'-addon', outfile,
			'-id', str(self.addon.getworkshopid()),
			'-changes', message
		])

		print(output.decode('utf-8'))

		os.remove(outfile)

	def _get_executable(self):
		"""Retrieve the path of the gmpublish executable"""
		platform = sys.platform
		if platform == 'linux':
			return 'gmpublish_linux'
		elif platform == 'windows' or platform == "win32":
			return 'gmpublish.exe'
		elif platform == 'darwin': #untested
			return 'gmpublish_osx'

		raise NameError("Could not find executable for platform " + platform)
<|endoftext|>


<commit_before>import sys
import math
import numpy as np
from cylp.cy import CyClpSimplex
from cylp.py.modeling import CyLPArray
DISPLAY_ENABLED = True
try:
    from src.grumpy.polyhedron2D import Polyhedron2D, add_line
    import matplotlib.pyplot as plt
except ImportError:
    DISPLAY_ENABLED = False

epsilon = 0.01
maxiter = 1000

def isInt(x):
    '''
    Return True if x is an integer, or if x is a numpy array
    with all integer elements, False otherwise
    '''
    if isinstance(x, (int, long, float)):
        return abs(math.floor(x) - x) < epsilon
    return (np.abs(np.floor(x) - x) < epsilon).all()

def getFraction(x):
    'Return the fraction part of x: x - floor(x)'
    return x - math.floor(x)

def gomoryCut(lp, rowInd, value, sense):
    'Return the Gomory cut of row ``rowInd`` of lp (a CyClpSimplex object)'
    f0 = getFraction(value)
    f = np.array([getFraction(x) for x in lp.tableau[rowInd,:lp.nVariables]])
    pi = np.array([f[j]/f0 if f[j] <= f0
                   else (1-f[j])/(1-f0) for j in range(lp.nVariables)])
    pi_slacks = np.array([x/f0 if x > 0 else -x/(1-f0)
                         for x in lp.tableau[rowInd,lp.nVariables:]])
    pi -= pi_slacks * lp.coefMatrix
    pi0 = (1 - np.dot(pi_slacks, lp.constraintsUpper) if sense[1] == '<='
           else 1 + np.dot(pi_slacks, lp.constraintsUpper))
    return pi, pi0

def disp_relaxation(A, b):
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.grid()
    p = Polyhedron2D(A = A, b = b)
    p.draw(ax, color = 'blue', linestyle = 'solid')
    ax.set_xlim(p.plot_min[0], p.plot_max[0])
    ax.set_ylim(p.plot_min[1], p.plot_max[1])
    pI = p.make_integer_hull()
    pI.draw(ax, color = 'red', linestyle = 'dashed')
    plt.show()

max_cuts = 1
display = True
if not DISPLAY_ENABLED:
    display = False

lp = CyClpSimplex()

sys.path.append('../instances')

from MIP6 import numVars, numCons, A, b, c, sense, integerIndices
try:
    from MIP6 import x_u
except ImportError:
    x_u = None
else:
    x_u = CyLPArray(x_u)

if display:
    disp_relaxation(A, b)

myA = A
A = np.matrix(A)
#print np.linalg.cond(A)
myb = b
b = CyLPArray(b)

#We assume variables have zero lower bounds
x_l = CyLPArray([0 for i in range(numVars)])

#Integrality tolerance
epsilon = .01

x = lp.addVariable('x', numVars)

lp += x >= x_l
if x_u is not None:
    lp += x <= x_u

lp += (A * x <= b if sense[1] == '<=' else
       A * x >= b)

c = CyLPArray(c)
lp.objective = -c * x if sense[0] == 'Max' else c * x

for i in xrange(maxiter):
    print '______________________________________________________'
    print 'iteration ', i
    lp.primal(startFinishOptions = 'x')
    bv = lp.basicVariables

#    B = np.zeros(shape = (lp.nConstraints, lp.nConstraints))
#    for i in range(lp.nVariables, lp.nVariables+lp.nConstraints):
#        lp.getBInvACol(i, B[i-lp.nVariables,:])
    print 'Current basis inverse:'
    print lp.basisInverse
    print np.linalg.cond(lp.basisInverse)

    print "Current tableaux:"
    print lp.tableau
    print "Current right hand side:"
    print lp.rhs
    rhs = lp.rhs
    sol = lp.primalVariableSolution['x']
    print 'Current solution: ', sol
    if isInt(sol[integerIndices]):
        print 'Integer solution found!'
        break
    num_cuts = 0
    for rowInd in xrange(lp.nConstraints):
        if num_cuts >= max_cuts:
            break
        basicVarInd = bv[rowInd]
        if basicVarInd in integerIndices and not isInt(sol[basicVarInd]):
            num_cuts += 1
            coef, r = gomoryCut(lp, rowInd, sol[basicVarInd], sense)
            if sense[1] == '>=':
                print 'Adding cut: ', coef, '>=', r
                lp += CyLPArray(coef) * x >= r
                myA.append(coef.tolist())
                myb.append(r)
            else:
                print 'Adding cut: ', -coef, '<=', -r
                lp += CyLPArray(-coef) * x <= -r
                myA.append((-coef).tolist())
                myb.append(-r)
            print myA, myb
            if display:
                disp_relaxation(myA, myb)

disp_relaxation(myA, myb)

<commit_msg>Lot of updates including modularization and the addition of split cuts. The file will be renamed soon to reflect generality<commit_after>import sys
import math
import numpy as np
from cylp.cy import CyClpSimplex
from cylp.py.modeling import CyLPArray, CyLPModel
from copy import deepcopy
from MIP1 import integerIndices
sys.path.append('../instances')

DISPLAY_ENABLED = True
try:
    from src.grumpy.polyhedron2D import Polyhedron2D, Figure
    import matplotlib.pyplot as plt
except ImportError:
    DISPLAY_ENABLED = False

def isInt(x):
    '''
    Return True if x is an integer, or if x is a numpy array
    with all integer elements, False otherwise
    '''
    if isinstance(x, (int, long, float)):
        return abs(math.floor(x) - x) < epsilon
    return (np.abs(np.floor(x) - x) < epsilon).all()

def getFraction(x):
    'Return the fraction part of x: x - floor(x)'
    return x - math.floor(x)

def splitCuts(lp, integerIndices = None, sense = '>=', sol = None):
    A = lp.coefMatrix
    b = CyLPArray(lp.constraintsUpper)
    if integerIndices is None:
        integerIndices = range(lp.nVariables)
    if sol is None:
        sol = lp.primalVariableSolution['x']
    s = A*sol - b
    best = lp.getCoinInfinity()

    for theta in [0.1, 0.2, 0.3, 0.4, 0.5]:
        sp = CyLPModel()
        u = sp.addVariable('u', lp.nConstraints, isInt = False)
        v = sp.addVariable('v', lp.nConstraints, isInt = False)
        pi = sp.addVariable('pi', lp.nVariables, isInt = True)
        pi0 = sp.addVariable('pi0', 1, isInt = True)

        sp += pi + A.transpose()*u - A.transpose()*v == 0
        sp += pi0 + b*u - b*v == theta - 1
        if sense == '<=':
            sp += u >= 0
            sp += v >= 0
        else:
            #TODO this direction is not debugged
            # Is this all we need?
            sp += u <= 0
            sp += v <= 0

        sp.objective = (theta-1)*s*u - theta*s*v
        for i in xrange(lp.nVariables):
            if i in integerIndices:
                sp += pi[i] <= 1
                sp += pi[i] >= -1
            else:
                sp[i] += pi[i] == 0

        cbcModel = CyClpSimplex(sp).getCbcModel()
        cbcModel.logLevel = -1
        #cbcModel.maximumSeconds = 5
        cbcModel.solve()
        if debug_print:
            print theta, cbcModel.objectiveValue
            print cbcModel.primalVariableSolution['pi'], cbcModel.primalVariableSolution['pi0']
        if cbcModel.objectiveValue < best:
            best = cbcModel.objectiveValue
            multu = cbcModel.primalVariableSolution['u']
            multv = cbcModel.primalVariableSolution['v']
            disjunction = cbcModel.primalVariableSolution['pi']
            rhs = cbcModel.primalVariableSolution['pi0']
            best_theta = theta
    return A.transpose()*multu + best_theta*disjunction, np.dot(lp.constraintsUpper, multu) + best_theta*rhs

def gomoryCut(lp, integerIndices = None, sense = '>=', rowInds = None, value = None):
    'Return the Gomory cut of row ``rowInd`` of lp (a CyClpSimplex object)'
    cuts = []
    sol = lp.primalVariableSolution['x']
    if rowInds is None:
        rowInds = range(lp.nConstraints)
    if integerIndices is None:
        integerIndices = range(lp.nVariables)
    for row in rowInds:
        basicVarInd = lp.basicVariables[row]
        if (basicVarInd in integerIndices) and (not isInt(sol[basicVarInd])):
            f0 = getFraction(sol[basicVarInd])
            f = []
            for i in range(lp.nVariables):
                if i in lp.basicVariables:
                    #This is to try to avoid getting very small numbers that should be zero
                    f.append(0)
                else:
                    f.append(getFraction(lp.tableau[row, i]))
            pi = np.array([f[j]/f0 if f[j] <= f0
                           else (1-f[j])/(1-f0) for j in range(lp.nVariables)])
            pi_slacks = np.array([x/f0 if x > 0 else -x/(1-f0)
                                 for x in lp.tableau[row, lp.nVariables:]])
            pi -= pi_slacks * lp.coefMatrix
            pi0 = (1 - np.dot(pi_slacks, lp.constraintsUpper) if sense == '<='
                   else 1 + np.dot(pi_slacks, lp.constraintsUpper))
            if sense == '>=':
                cuts.append((pi, pi0))
            else:
                cuts.append((-pi, -pi0))
    return cuts

def disp_relaxation(A, b, cuts = [], sol = None):
    #TODO: Check sense of inequalities by looking explicitly at
    #      lp.constraintsUpper and lp.constraintsLower
    p = Polyhedron2D(A = A, b = b)
    f = Figure()
    f.add_polyhedron(p, label = 'Polyhedron $P$')
    f.set_xlim(p.plot_min[0], p.plot_max[0])
    f.set_ylim(p.plot_min[1], p.plot_max[1])
    pI = p.make_integer_hull()
    f.add_polyhedron(pI, show_int_points = True, color = 'red',
                     linestyle = 'dashed',
                     label = 'Convex hull of integer points')
    for (coeff, r) in cuts:
        f.add_line(coeff, r, plot_max = p.plot_max, plot_min = p.plot_min,
                   color = 'green', linestyle = 'dashed')
    if sol is not None:
        f.add_point(sol, radius = .05)
    f.show()

def read_instance(import_instance = True, filename = None):

    if import_instance:
        lp = CyClpSimplex()

        from MIP6 import numVars, A, b, c, sense, integerIndices
        try:
            from MIP6 import x_u
        except ImportError:
            x_u = None
        else:
            x_u = CyLPArray(x_u)

        myA = A
        A = np.matrix(A)
        #print np.linalg.cond(A)
        myb = b
        b = CyLPArray(b)

        #We assume variables have zero lower bounds
        x_l = CyLPArray([0 for i in range(numVars)])

        x = lp.addVariable('x', numVars)

        lp += x >= x_l
        if x_u is not None:
            lp += x <= x_u

        lp += (A * x <= b if sense[1] == '<=' else
               A * x >= b)
        c = CyLPArray(c)
        lp.objective = -c * x if sense[0] == 'Max' else c * x
    else:
        #TODO Change sense of inequalities so they are all the same
        #     by explicitly checking lp.constraintsUpper and lp.constraintsLower
        #Warning: Reading MP not well tested
        lp.extractCyLPModel(filename)
        myA, myb = None, None
        x = lp.cyLPModel.getVarByName('x')
        sense = ('Min', '>=')

    return lp, x, myA, myb, sense, integerIndices

generate_splits = True
generate_GMI = True
debug_print = False
epsilon = 0.01
maxiter = 10
max_cuts = 10
display = True
if not DISPLAY_ENABLED:
    display = False

lp, x, A, b, sense, integerIndices = read_instance(import_instance = True)
infinity = lp.getCoinInfinity()

if display:
    disp_relaxation(A, b)

for i in xrange(maxiter):
    print '______________________________________________________'
    print 'iteration ', i
    lp.primal(startFinishOptions = 'x')
    bv = lp.basicVariables
    #Binv = np.zeros(shape = (lp.nConstraints, lp.nConstraints))
    #for i in range(lp.nVariables, lp.nVariables+lp.nConstraints):
    #    lp.getBInvACol(i, Binv[i-lp.nVariables,:])
    #rhs = lp.rhs
    rhs = np.dot(lp.basisInverse, b)
    sol = lp.primalVariableSolution['x']
    if debug_print:
        print 'Current basis inverse:'
        print lp.basisInverse
        print 'Condition number of basis inverse'
        print np.linalg.cond(lp.basisInverse)
        print "Current tableaux:"
        print lp.tableau
        print lp.rhs
        print "Current right hand side:\n", rhs
        print 'Current solution: ', sol
    if isInt(sol[integerIndices]):
        print 'Integer solution found!'
        break
    cuts = []
    if generate_splits:
        cuts.append(splitCuts(lp, integerIndices, sense[1]))
    if generate_GMI:
        cuts += gomoryCut(lp, integerIndices, sense[1])
    if display:
        disp_relaxation(A, b, cuts, sol)
    for (coeff, r) in cuts[:max_cuts]:
        #TODO sort cuts by degree of violation
        if sense[1] == '<=':
            print 'Adding cut: ', coeff, '<=', r
            lp += CyLPArray(coeff) * x <= r
        else:
            print 'Adding cut: ', coeff, '>=', r
            lp += CyLPArray(coeff) * x >= r
        A.append(coeff.tolist())
        b.append(r)

disp_relaxation(A, b)

<|endoftext|>


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

"""Hybrid blind/optical tracking of objects.

This program uses a feedback control loop to cause a telescope mount to track an object based on
both predicted position and computer vision detection of the object with a camera. It includes
a union of the features in optical_track and blind_track. Transitions between blind and optical
tracking mode are handled automatically.
"""

from __future__ import print_function
import sys
import ephem
import track

def main():

    parser = track.ArgParser()
    parser.add_argument(
        '--camera',
        help='device node path for tracking webcam',
        default='/dev/video0'
    )
    parser.add_argument(
        '--camera-res',
        help='webcam resolution in arcseconds per pixel',
        required=True,
        type=float
    )
    parser.add_argument(
        '--camera-bufs',
        help='number of webcam capture buffers',
        required=True,
        type=int
    )
    parser.add_argument(
        '--camera-exposure',
        help='webcam exposure level',
        default=2000,
        type=int)
    parser.add_argument(
        '--mount-type',
        help='select mount type (nexstar or gemini)',
        default='gemini'
    )
    parser.add_argument(
        '--mount-path',
        help='serial device node or hostname for mount command interface',
        default='/dev/ttyACM0'
    )
    parser.add_argument(
        '--meridian-side',
        help='side of meridian for equatorial mounts to prefer',
        default='west'
    )
    parser.add_argument(
        '--lat',
        required=True,
        help='latitude of observer (+N)'
    )
    parser.add_argument(
        '--lon',
        required=True,
        help='longitude of observer (+E)'
    )
    parser.add_argument(
        '--elevation',
        required=True,
        help='elevation of observer (m)',
        type=float
    )
    parser.add_argument(
        '--loop-bw',
        help='control loop bandwidth (Hz)',
        default=0.5,
        type=float
    )
    parser.add_argument(
        '--loop-damping',
        help='control loop damping factor',
        default=2.0,
        type=float
    )
    parser.add_argument(
        '--max-divergence',
        help='max divergence of optical and blind sources (degrees)',
        default=2.0,
        type=float
    )
    parser.add_argument(
        '--telem-enable',
        help='enable logging of telemetry to database',
        action='store_true'
    )
    parser.add_argument(
        '--telem-db-host',
        help='hostname of InfluxDB database server',
        default='localhost'
    )
    parser.add_argument(
        '--telem-db-port',
        help='port number of InfluxDB database server',
        default=8086,
        type=int
    )
    parser.add_argument(
        '--telem-period',
        help='telemetry sampling period in seconds',
        default=1.0,
        type=float
    )

    subparsers = parser.add_subparsers(title='modes', dest='mode')

    parser_tle = subparsers.add_parser('tle', help='TLE file mode')
    parser_tle.add_argument(
        'file',
        help='filename of two-line element (TLE) target ephemeris')

    parser_coord = subparsers.add_parser('coord', help='fixed-body coordinate mode')
    parser_coord.add_argument(
        'ra',
        help='fixed body\'s right ascension',
        type=float)
    parser_coord.add_argument(
        'dec',
        help='fixed body\'s declination',
        type=float)

    parser_star = subparsers.add_parser('star', help='named star mode')
    parser_star.add_argument(
        'name',
        help='name of star')

    parser_star = subparsers.add_parser('solarsystem', help='named solar system body mode')
    parser_star.add_argument(
        'name',
        help='name of planet or moon')

    args = parser.parse_args()

    # Create object with base type TelescopeMount
    if args.mount_type == 'nexstar':
        mount = track.NexStarMount(args.mount_path)
    elif args.mount_type == 'gemini':
        mount = track.LosmandyGeminiMount(args.mount_path)
    else:
        print('mount-type not supported: ' + args.mount_type)
        sys.exit(1)

    # Create a PyEphem Observer object
    observer = ephem.Observer()
    observer.lat = args.lat
    observer.lon = args.lon
    observer.elevation = args.elevation

    # Create a PyEphem Body object corresonding to the TLE file
    if args.mode == 'tle':
        print('In TLE file mode: \'{}\'.'.format(args.file))
        tle = []
        with open(args.file) as tlefile:
            for line in tlefile:
                tle.append(line)
        target = ephem.readtle(tle[0], tle[1], tle[2])

    # Create a PyEphem Body object corresonding to the given fixed coordinates
    if args.mode == 'coord':
        print('In fixed-body coordinate mode: (RA {}, dec {}).'.format(args.ra, args.dec))
        target = ephem.FixedBody(_ra=args.ra, _dec=args.dec)

    # Get the PyEphem Body object corresonding to the given named star
    if args.mode == 'star':
        print('In named star mode: \'{}\''.format(args.name))
        target = ephem.star(args.name)

    # Get the PyEphem Body object corresonding to the given named solar system body
    if args.mode == 'solarsystem':
        print('In named solar system body mode: \'{}\''.format(args.name))
        ss_objs = [name for _, _, name in ephem._libastro.builtin_planets()]
        if args.name in ss_objs:
            body_type = getattr(ephem, args.name)
            target = body_type()
        else:
            raise Exception(
                'The solar system body \'{}\' isn\'t present in PyEphem.'.format(
                    args.name))

    # Create object with base type ErrorSource
    error_source = track.HybridErrorSource(
        mount=mount,
        observer=observer,
        target=target,
        cam_dev_path=args.camera,
        arcsecs_per_pixel=args.camera_res,
        cam_num_buffers=args.camera_bufs,
        cam_ctlval_exposure=args.camera_exposure,
        max_divergence=args.max_divergence,
        meridian_side=args.meridian_side
    )

    try:
        # Create gamepad object and register callback
        game_pad = track.Gamepad(
            left_gain=2.0,  # left stick degrees per second
            right_gain=0.5,  # right stick degrees per second
            int_limit=5.0,  # max correction in degrees for either axis
        )
        game_pad.integrator_mode = True
        error_source.register_blind_offset_callback(game_pad.get_integrator)
        print('Gamepad found and registered.')
    except RuntimeError:
        print('No gamepads found.')

    tracker = track.Tracker(
        mount=mount,
        error_source=error_source,
        loop_bandwidth=args.loop_bw,
        damping_factor=args.loop_damping
    )

    if args.telem_enable:
        telem_logger = track.TelemLogger(
            host=args.telem_db_host,
            port=args.telem_db_port,
            period=args.telem_period,
            sources={
                'tracker': tracker,
                'error_hybrid': error_source,
                'error_blind': error_source.blind,
                'error_optical': error_source.optical,
                'gamepad': game_pad
            }
        )
        telem_logger.start()

    try:
        tracker.run()
    except KeyboardInterrupt:
        print('Goodbye!')

if __name__ == "__main__":
    main()
<commit_msg>hybrid_track: Prints error when no target is specified<commit_after>#!/usr/bin/env python3

"""Hybrid blind/optical tracking of objects.

This program uses a feedback control loop to cause a telescope mount to track an object based on
both predicted position and computer vision detection of the object with a camera. It includes
a union of the features in optical_track and blind_track. Transitions between blind and optical
tracking mode are handled automatically.
"""

from __future__ import print_function
import sys
import ephem
import track

def main():

    parser = track.ArgParser()
    parser.add_argument(
        '--camera',
        help='device node path for tracking webcam',
        default='/dev/video0'
    )
    parser.add_argument(
        '--camera-res',
        help='webcam resolution in arcseconds per pixel',
        required=True,
        type=float
    )
    parser.add_argument(
        '--camera-bufs',
        help='number of webcam capture buffers',
        required=True,
        type=int
    )
    parser.add_argument(
        '--camera-exposure',
        help='webcam exposure level',
        default=2000,
        type=int)
    parser.add_argument(
        '--mount-type',
        help='select mount type (nexstar or gemini)',
        default='gemini'
    )
    parser.add_argument(
        '--mount-path',
        help='serial device node or hostname for mount command interface',
        default='/dev/ttyACM0'
    )
    parser.add_argument(
        '--meridian-side',
        help='side of meridian for equatorial mounts to prefer',
        default='west'
    )
    parser.add_argument(
        '--lat',
        required=True,
        help='latitude of observer (+N)'
    )
    parser.add_argument(
        '--lon',
        required=True,
        help='longitude of observer (+E)'
    )
    parser.add_argument(
        '--elevation',
        required=True,
        help='elevation of observer (m)',
        type=float
    )
    parser.add_argument(
        '--loop-bw',
        help='control loop bandwidth (Hz)',
        default=0.5,
        type=float
    )
    parser.add_argument(
        '--loop-damping',
        help='control loop damping factor',
        default=2.0,
        type=float
    )
    parser.add_argument(
        '--max-divergence',
        help='max divergence of optical and blind sources (degrees)',
        default=2.0,
        type=float
    )
    parser.add_argument(
        '--telem-enable',
        help='enable logging of telemetry to database',
        action='store_true'
    )
    parser.add_argument(
        '--telem-db-host',
        help='hostname of InfluxDB database server',
        default='localhost'
    )
    parser.add_argument(
        '--telem-db-port',
        help='port number of InfluxDB database server',
        default=8086,
        type=int
    )
    parser.add_argument(
        '--telem-period',
        help='telemetry sampling period in seconds',
        default=1.0,
        type=float
    )

    subparsers = parser.add_subparsers(title='modes', dest='mode')

    parser_tle = subparsers.add_parser('tle', help='TLE file mode')
    parser_tle.add_argument(
        'file',
        help='filename of two-line element (TLE) target ephemeris')

    parser_coord = subparsers.add_parser('coord', help='fixed-body coordinate mode')
    parser_coord.add_argument(
        'ra',
        help='fixed body\'s right ascension',
        type=float)
    parser_coord.add_argument(
        'dec',
        help='fixed body\'s declination',
        type=float)

    parser_star = subparsers.add_parser('star', help='named star mode')
    parser_star.add_argument(
        'name',
        help='name of star')

    parser_star = subparsers.add_parser('solarsystem', help='named solar system body mode')
    parser_star.add_argument(
        'name',
        help='name of planet or moon')

    args = parser.parse_args()

    # Create object with base type TelescopeMount
    if args.mount_type == 'nexstar':
        mount = track.NexStarMount(args.mount_path)
    elif args.mount_type == 'gemini':
        mount = track.LosmandyGeminiMount(args.mount_path)
    else:
        print('mount-type not supported: ' + args.mount_type)
        sys.exit(1)

    # Create a PyEphem Observer object
    observer = ephem.Observer()
    observer.lat = args.lat
    observer.lon = args.lon
    observer.elevation = args.elevation

    # Create a PyEphem Body object corresonding to the TLE file
    if args.mode == 'tle':
        print('In TLE file mode: \'{}\'.'.format(args.file))
        tle = []
        with open(args.file) as tlefile:
            for line in tlefile:
                tle.append(line)
        target = ephem.readtle(tle[0], tle[1], tle[2])

    # Create a PyEphem Body object corresonding to the given fixed coordinates
    elif args.mode == 'coord':
        print('In fixed-body coordinate mode: (RA {}, dec {}).'.format(args.ra, args.dec))
        target = ephem.FixedBody(_ra=args.ra, _dec=args.dec)

    # Get the PyEphem Body object corresonding to the given named star
    elif args.mode == 'star':
        print('In named star mode: \'{}\''.format(args.name))
        target = ephem.star(args.name)

    # Get the PyEphem Body object corresonding to the given named solar system body
    elif args.mode == 'solarsystem':
        print('In named solar system body mode: \'{}\''.format(args.name))
        ss_objs = [name for _, _, name in ephem._libastro.builtin_planets()]
        if args.name in ss_objs:
            body_type = getattr(ephem, args.name)
            target = body_type()
        else:
            raise Exception(
                'The solar system body \'{}\' isn\'t present in PyEphem.'.format(
                    args.name))

    else:
        print('No target specified!')
        sys.exit(1)

    # Create object with base type ErrorSource
    error_source = track.HybridErrorSource(
        mount=mount,
        observer=observer,
        target=target,
        cam_dev_path=args.camera,
        arcsecs_per_pixel=args.camera_res,
        cam_num_buffers=args.camera_bufs,
        cam_ctlval_exposure=args.camera_exposure,
        max_divergence=args.max_divergence,
        meridian_side=args.meridian_side
    )

    try:
        # Create gamepad object and register callback
        game_pad = track.Gamepad(
            left_gain=2.0,  # left stick degrees per second
            right_gain=0.5,  # right stick degrees per second
            int_limit=5.0,  # max correction in degrees for either axis
        )
        game_pad.integrator_mode = True
        error_source.register_blind_offset_callback(game_pad.get_integrator)
        print('Gamepad found and registered.')
    except RuntimeError:
        print('No gamepads found.')

    tracker = track.Tracker(
        mount=mount,
        error_source=error_source,
        loop_bandwidth=args.loop_bw,
        damping_factor=args.loop_damping
    )

    if args.telem_enable:
        telem_logger = track.TelemLogger(
            host=args.telem_db_host,
            port=args.telem_db_port,
            period=args.telem_period,
            sources={
                'tracker': tracker,
                'error_hybrid': error_source,
                'error_blind': error_source.blind,
                'error_optical': error_source.optical,
                'gamepad': game_pad
            }
        )
        telem_logger.start()

    try:
        tracker.run()
    except KeyboardInterrupt:
        print('Goodbye!')

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


<commit_before>from __future__ import absolute_import

from .parsed_params import param_defs, TRACKTYPES

# http://genome-source.cse.ucsc.edu/gitweb/
#       ?p=kent.git;a=blob;f=src/hg/makeDb/trackDb/README;hb=HEAD

param_dict = {i.name: i for i in param_defs}

# These should at least be first...
initial_params = ['track', 'bigDataUrl', 'shortLabel', 'longLabel', 'type']

track_fields = {i: initial_params[:] for i in TRACKTYPES}

observed_types = set()
for param in param_defs:
    observed_types.update(param.types)

for tracktype in observed_types:
    lst = track_fields[tracktype]
    for param in param_defs:
        if tracktype in param.types:
            lst.append(param.name)

INDENT = '    '
<commit_msg>add missing constants.trackhub_specific list<commit_after>from __future__ import absolute_import

from .parsed_params import param_defs, TRACKTYPES

# http://genome-source.cse.ucsc.edu/gitweb/
#       ?p=kent.git;a=blob;f=src/hg/makeDb/trackDb/README;hb=HEAD

param_dict = {i.name: i for i in param_defs}

# These should at least be first...
initial_params = ['track', 'bigDataUrl', 'shortLabel', 'longLabel', 'type']
trackhub_specific = ["source", "tracktype", "name"]

track_fields = {i: initial_params[:] for i in TRACKTYPES}

observed_types = set()
for param in param_defs:
    observed_types.update(param.types)

for tracktype in observed_types:
    lst = track_fields[tracktype]
    for param in param_defs:
        if tracktype in param.types:
            lst.append(param.name)

INDENT = '    '
<|endoftext|>