<commit_before>import cv2
import numpy as np

train_image = cv2.imread('images/in1.png', 0)
#crop the image for consistency
h1 = 0
h2 = 303
l1 = 0
l2 = 303
H1 = 0
H2 = 370
L1 = 0
L2 = 370
nu=0.1 #our value of nu

train_image = np.vectorize(lambda x: x/256.0)(train_image)
img_in = train_image[h1:h2, l1:l2]
img_out = train_image[H1:H2, L1:L2]
#make it into a long array
start = np.reshape(img_in, (1, (l2-l1)*(h2-h1)))
end = np.reshape(img_out, (1, (L2-L1)*(H2-H1)))
#Begin neural networks now
#initialize layer dimensions and transition weights
dimensions = [start.shape[1], 10, end.shape[1]]
#dimensions = [10, 5, 10]
transitions = [(2.5/dimensions[0]) * (np.random.rand(dimensions[0], dimensions[1])),\
                (2.5/dimensions[1]) * (np.random.rand(dimensions[1], dimensions[2]))]

print("Initialized transitions")
def back_propagation():
    S = start
    intermediate_values = [S]
    for x in range(0, len(transitions)):
        S = np.dot(S, transitions[x])
        S = np.tanh(S)
        intermediate_values.append(S)
    #deltas are the way that the error function changes as a specific node changes. It is in reverse order
    deltas = []
    #we use RMS error. Therefore, this is the last delta value
    deltas.append(2*(intermediate_values[-1]-end))
    #Computing the previous deltas. I believe that the previous deltas should be the next deltas,
    #times (the transpose of the transition matrices with each row scaled up by 1-value^2 of the corresponding
    #next neuron's value, which is actually stored in intermediate_values!)
    for x in range(len(intermediate_values)-2, -1, -1):
        weights    = transitions[x].copy().transpose()
        operations = intermediate_values[x+1][0]
        #multiply the ith row of weights by the ith element of operations
        #probably is a faster way to do it using np.multiply
        for y in range(0, len(operations)):
            value = 1-operations[y]**2
            weights[y] = (np.vectorize(lambda z:z*value))(weights[y])
        delta_prev = np.dot(deltas[-1], weights)
        deltas.append(delta_prev)
    deltas.reverse()
    #Now, we have the values and the deltas. It is time to update the transitions
    for x in range(0, len(transitions)):
        #take the deltas and intermediate_values and array multiply together
        deltas[x+1]*=intermediate_values[x+1]
        #Once again, there is probably a faster way to do this
        #We take the result and scale it by the previous node's value. Then, we add to the transitions, which is now the updated version
        for y in range(0, len(intermediate_values[x][0])):
            transitions[x][y]-=(nu*intermediate_values[x][0][y])*deltas[x+1][0] #move 0.1 times the gradient at a time

def backProp():
    layersin = [start]
    current = start
    transLen = len(transitions)
    deltastream = []
    for i in range(transLen):
        current = np.dot(current, transitions[i])
        current = np.tanh(current)
        layersin.append(current)
    #length of layersin is len(transitions) + 1
    #maintain a series of delta for each output neuron
    errorgrad = []
    desiredoutput = end
    actualoutput = layersin[transLen][0]
    print "desiredoutput", desiredoutput
    print "actualoutput", actualoutput
    for i in range(len(end)):
        currentdelta = np.array([1 - ((actualoutput[i]) ** 2)])
        deltacollection = [currentdelta]
        for i in range(transLen-1):
            if (i == 0):
                currentdelta = np.dot(currentdelta[0], transitions[transLen-1][i])\
                    * (1 - layersin[transLen-1] * layersin[transLen-1])
            else:
                currentdelta = np.dot(currentdelta,transitions[transLen-i-1].T)\
                    * (1 - layersin[transLen-i-1] * layersin[transLen-i-1])
            deltacollection.append(currentdelta)
        deltacollection.reverse()
        deltastream.append(deltacollection)
    for i in range(transLen):
        #this iteration we will compute the error derivative with respect to
        #the i-th weight matrix
        errorgradterm = 0
        for j in range(len(deltastream[i])):
            errorgradterm += np.dot(layersin[i].T, deltastream[j][i]) *\
                             (actualoutput[j] - desiredoutput[j])
        errorgrad.append(errorgradterm/len(transitions))
    #length of errorgrad is the same as length of transitions.
    #errorgrad[i] is gradient of error with respect to weight matrix i
    for i in range(transLen):
         transitions[i] -= nu * errorgrad[i]

def get_output():
    S = start
    for x in range(0, len(transitions)):
        S = np.dot(S, transitions[x])
        S = np.tanh(S)
    return S
#print(transitions[0])
#print(transitions[1])
for x in range(0, 1):
    print(x)
    #print("Neural Image")
    #print(get_output())
    #print("Out Image")
    #print(end)
    #print("Transitions1")
    #print(transitions[0])
    #print("Transitions2")
    #rint(transitions[1])
    backProp()

out = get_output()

out = np.reshape(out, (H2-H1, L2-L1))
#out = (np.vectorize(lambda x: (x + 1.0) / 2.0))(out)

print("out image")
print(out)
cv2.imshow('image', img_in)
cv2.imshow('image2', img_out)
cv2.imshow('output', out)
cv2.waitKey(0)
cv2.destroyAllWindows()

#cv2.imwrite('out.png', img_in)
<commit_msg>implemented and ran a version of backpropagation. Needs a lot of tweaking<commit_after>import cv2
import numpy as np

train_image = cv2.imread('images/in1.png', 0)
#crop the image for consistency
h1 = 0
h2 = 303
l1 = 0
l2 = 303
H1 = 0
H2 = 370
L1 = 0
L2 = 370
nu=0.001 #our value of nu

train_image = np.vectorize(lambda x: x/256.0)(train_image)
img_in = train_image[h1:h2, l1:l2]
img_out = train_image[H1:H2, L1:L2]
#make it into a long array
start = np.reshape(img_in, (1, (l2-l1)*(h2-h1)))
end = np.reshape(img_out, (1, (L2-L1)*(H2-H1)))
#Begin neural networks now
#initialize layer dimensions and transition weights
dimensions = [start.shape[1], 10, end.shape[1]]
#dimensions = [10, 5, 10]
transitions = [(2.5/dimensions[0]) * (np.random.rand(dimensions[0], dimensions[1])),\
                (2.5/dimensions[1]) * (np.random.rand(dimensions[1], dimensions[2]))]

print("Initialized transitions")
def back_propagation():
    S = start
    intermediate_values = [S]
    for x in range(0, len(transitions)):
        S = np.dot(S, transitions[x])
        S = np.tanh(S)
        intermediate_values.append(S)
    #deltas are the way that the error function changes as a specific node changes. It is in reverse order
    deltas = []
    #we use RMS error. Therefore, this is the last delta value
    deltas.append(2*(intermediate_values[-1]-end))
    #Computing the previous deltas. I believe that the previous deltas should be the next deltas,
    #times (the transpose of the transition matrices with each row scaled up by 1-value^2 of the corresponding
    #next neuron's value, which is actually stored in intermediate_values!)
    for x in range(len(intermediate_values)-2, -1, -1):
        weights    = transitions[x].copy().transpose()
        operations = intermediate_values[x+1][0]
        #multiply the ith row of weights by the ith element of operations
        #probably is a faster way to do it using np.multiply
        for y in range(0, len(operations)):
            value = 1-operations[y]**2
            weights[y] = (np.vectorize(lambda z:z*value))(weights[y])
        delta_prev = np.dot(deltas[-1], weights)
        deltas.append(delta_prev)
    deltas.reverse()
    #Now, we have the values and the deltas. It is time to update the transitions
    for x in range(0, len(transitions)):
        #take the deltas and intermediate_values and array multiply together
        deltas[x+1]*=intermediate_values[x+1]
        #Once again, there is probably a faster way to do this
        #We take the result and scale it by the previous node's value. Then, we add to the transitions, which is now the updated version
        for y in range(0, len(intermediate_values[x][0])):
            transitions[x][y]-=(nu*intermediate_values[x][0][y])*deltas[x+1][0] #move 0.1 times the gradient at a time

def backProp():
    layersin = [start]
    current = start
    transLen = len(transitions)
    deltastream = []
    for i in range(transLen):
        current = np.dot(current, transitions[i])
        current = np.tanh(current)
        layersin.append(current)
    #length of layersin is len(transitions) + 1
    #maintain a series of delta for each output neuron
    errorgrad = []
    desiredoutput = end[0]
    actualoutput = layersin[transLen][0]
    for i in range(len(end)):
        currentdelta = np.array([1 - ((actualoutput[i]) ** 2)])
        deltacollection = [currentdelta]
        for i in range(transLen-1):
            if (i == 0):
                derivative = (1 - layersin[transLen-1] * layersin[transLen-1])
                update = np.dot(np.matrix(currentdelta),\
                    np.matrix((transitions[transLen-1].T)[i]))
                currentdelta = derivative * np.array(update)
            else:
                derivative = 1 - layersin[transLen-i-1] *\
                    layersin[transLen-i-1]
                update = np.dot(np.matrix(currentdelta),\
                    np.matrix(transitions[transLen-i-1].T))
                currentdelta = derivative * np.array(update)
            deltacollection.append(currentdelta)
        deltacollection.reverse()
        deltastream.append(deltacollection)
    for i in range(transLen-1):
        #this iteration we will compute the error derivative with respect to
        #the i-th weight matrix
        errorgradterm = 0
        for j in range(len(end)):
            errorgradterm += np.dot(layersin[i].T, deltastream[j][i]) *\
                             (actualoutput[j] - desiredoutput[j])
        errorgrad.append(errorgradterm)
    #length of errorgrad is the same as length of transitions.
    #errorgrad[i] is gradient of error with respect to weight matrix i
    for i in range(transLen-1):
        transitions[i] -= nu * errorgrad[i]

def get_output():
    S = start
    for x in range(0, len(transitions)):
        S = np.dot(S, transitions[x])
        S = np.tanh(S)
    return S

#print(transitions[0])
#print(transitions[1])
for x in range(0, 50):
    print(x)
    print("Neural Image")
    print(get_output())
    print("Out Image")
    print(end)
    print("Transitions1")
    print(transitions[0])
    print("Transitions2")
    print(transitions[1])
    backProp() #replace this line with #back_propagation to try Jacob and
               #backProp to try Sid's implementation

out = get_output()

out = np.reshape(out, (H2-H1, L2-L1))
out = (np.vectorize(lambda x: (x + 1.0) / 2.0))(out)

print("out image")
print(out)
#cv2.imshow('image', img_in)
#cv2.imshow('image2', img_out)
cv2.imshow('output', out)
cv2.waitKey(0)
cv2.destroyAllWindows()

#cv2.imwrite('out.png', img_in)
<|endoftext|>


<commit_before># -*- coding: iso-8859-15 -*-

"""Main HTTP routes request handlers."""

import tornado.web
import tornado.escape

class MainHandler(tornado.web.RequestHandler):
    """Handles creation of new terminals."""

    @tornado.gen.coroutine
    def get(self):
        """GET verb."""
        self.status_code(403)

    @tornado.gen.coroutine
    def post(self):
        """POST verb: Create a new terminal."""
        rows = int(self.get_argument('rows', None, 23))
        cols = int(self.get_argument('cols', None, 73))
        pid = yield self.application.term_manager.create_term(rows, cols)
        self.write(pid)

class ResizeHandler(tornado.web.RequestHandler):
    """Handles resizing of terminals."""

    @tornado.gen.coroutine
    def get(self):
        """GET verb: Forbidden."""
        self.status_code(403)

    @tornado.gen.coroutine
    def post(self, pid):
        """POST verb: Resize a terminal."""
        rows = int(self.get_argument('rows', None, 23))
        cols = int(self.get_argument('cols', None, 73))
        self.application.term_manager.resize_term(pid, rows, cols)
<commit_msg>Remove unused HTTP verbs<commit_after># -*- coding: iso-8859-15 -*-

"""Main HTTP routes request handlers."""

import tornado.web
import tornado.escape

class MainHandler(tornado.web.RequestHandler):
    """Handles creation of new terminals."""

    @tornado.gen.coroutine
    def post(self):
        """POST verb: Create a new terminal."""
        rows = int(self.get_argument('rows', None, 23))
        cols = int(self.get_argument('cols', None, 73))
        pid = yield self.application.term_manager.create_term(rows, cols)
        self.write(pid)

class ResizeHandler(tornado.web.RequestHandler):
    """Handles resizing of terminals."""

    @tornado.gen.coroutine
    def post(self, pid):
        """POST verb: Resize a terminal."""
        rows = int(self.get_argument('rows', None, 23))
        cols = int(self.get_argument('cols', None, 73))
        self.application.term_manager.resize_term(pid, rows, cols)
<|endoftext|>


<commit_before>## -*- coding: utf-8 -*-
import urllib2
import re

def getstops(url, num_tr):
  try:
		page = urllib2.urlopen(url)
		pagestr = page.read()
	except:
		print "Internet connection trouble, no page received from server or something else. Check your Internet connection."
	else:
		try:
			tempstring = ""+str(num_tr)+""
			pagestr = re.split(tempstring, pagestr)
			pagestr = pagestr[1]
			pagestr = re.split("
", pagestr)
			pagestr = pagestr[1]
			pagestr = re.split("
", pagestr)
			pagestr = pagestr[0]
			pagestr = re.split("
", pagestr)
			for i in pagestr:
				i = i.strip()
				#They see me strippin'
				#They hatin'
			pagestr = pagestr[:(len(pagestr) - 1)]
			result = []
			for i in pagestr:
				m = re.search(r'(.+)', i)
				result += [[m.group(1), m.group(2)]]
			return result
		except:
			print "Page received from server is broken or no longer has the same structure."
			return 0

<commit_msg>Update getstop.py<commit_after>## -*- coding: utf-8 -*-
import urllib2
import re

def getstops(url, num_tr):
	try:
		page = urllib2.urlopen(url)
		pagestr = page.read()
	except:
		print "Internet connection trouble, no page received from server or something else. Check your Internet connection."
	else:
		try:
			tempstring = ""+str(num_tr)+""
			pagestr = re.split(tempstring, pagestr)
			pagestr = pagestr[1]
			pagestr = re.split("
", pagestr)
			pagestr = pagestr[1]
			pagestr = re.split("
", pagestr)
			pagestr = pagestr[0]
			pagestr = re.split("
", pagestr)
			for i in pagestr:
				i = i.strip()
				#They see me strippin'
				#They hatin'
			pagestr = pagestr[:(len(pagestr) - 1)]
			result = []
			for i in pagestr:
				m = re.search(r'(.+)', i)
				result += [[m.group(1), m.group(2)]]
			return result
		except:
			print "Page received from server is broken or no longer has the same structure."
			return 0

<|endoftext|>


<commit_before>import discord
import asyncio
import aiohttp
import os
from discord.ext import commands
from .utils.dataIO import dataIO
from __main__ import send_cmd_help

settings_path = "data/albion"
settings_filepath = settings_path + "/" + "channels.json"

class Albion:

    """Check Albion things"""

    def __init__(self, bot):
        self.bot = bot
        self.settings_path = settings_path
        self.settings_filepath = settings_filepath
        try:
            self.settings = dataIO.load_json(self.settings_filepath)
        except:
            self.check_folders()
            self.check_files()
            self.settings = dataIO.load_json(self.settings_filepath)
        self.check_task = bot.loop.create_task(self.checkStatus())

    def __unload(self):
        self.check_task.cancel()

    @commands.group(name="albion", pass_context=True)
    async def albion(self, ctx):
        """ Verschiedene Dinge für Albion Online """
        if ctx.invoked_subcommand is None:
            await send_cmd_help(ctx)

    @albion.command(name="statuscheck", pass_context=True)
    async def _set_statuscheck(self, ctx, status: str):
        """ Schaltet den Statuscheck für diesen Channel ein. """
        if(status == "an"):
            server = ctx.message.server
            channel = ctx.message.channel
            if server.id not in self.settings:
                self.settings[server.id] = {}
            if channel.id not in self.settings[server.id]:
                self.settings[server.id][channel.id] = {}
            self.settings[server.id][channel.id] = self._check_online()
        dataIO.save_json(self.settings_filepath, self.settings)

    async def _check_online(self):
        url = "http://live.albiononline.com/status.txt"
        headers = {'user-agent': 'Tron-cog/1.0'}
        conn = aiohttp.TCPConnector(verify_ssl=False)
        session = aiohttp.ClientSession(connector=conn)
        async with session.get(url, headers=headers) as r:
            result = await r.json()
        session.close()
        if(result[status] == "online"):
            return True
        if(result[status] == "offline"):
            return False
        return False

    async def checkStatus(self):
        while True:
            await asyncio.sleep(360)
            server_online = self._check_online()
            for serverId in self.settings:
                for channel in self.settings[serverId]:
                    if channel != server_online and server_online == False:
                        await self.bot.send_message(channel, 'Albion Online Server ist offline!')
                    else:
                        await self.bot.send_message(channel, 'Albion Online Server ist wieder online!')

def check_folders():
    if not os.path.exists(settings_path):
        print("Creating data/dates directory...")
        os.makedirs(self.settings_path)

def check_files():
    if not dataIO.is_valid_json(settings_filepath):
        print("Creating "+ self.settings_filepath +"...")
        dataIO.save_json(f, {})

def setup(bot):
    check_folders()
    check_files()
    n = Albion(bot)
    bot.add_cog(n)
<commit_msg>Fixed dumb failures<commit_after>import discord
import asyncio
import aiohttp
import os
from discord.ext import commands
from .utils.dataIO import dataIO
from __main__ import send_cmd_help

settings_path = "data/albion"
settings_filepath = settings_path + "/" + "channels.json"

class Albion:

    """Check Albion things"""

    def __init__(self, bot):
        self.bot = bot
        self.settings_path = settings_path
        self.settings_filepath = settings_filepath
        try:
            self.settings = dataIO.load_json(self.settings_filepath)
        except:
            self.check_folders()
            self.check_files()
            self.settings = dataIO.load_json(self.settings_filepath)
        self.check_task = bot.loop.create_task(self.checkStatus())

    def __unload(self):
        self.check_task.cancel()

    @commands.group(name="albion", pass_context=True)
    async def albion(self, ctx):
        """ Verschiedene Dinge für Albion Online """
        if ctx.invoked_subcommand is None:
            await send_cmd_help(ctx)

    @albion.command(name="statuscheck", pass_context=True)
    async def _set_statuscheck(self, ctx, status: str):
        """ Schaltet den Statuscheck für diesen Channel ein. """
        if(status == "an"):
            server = ctx.message.server
            channel = ctx.message.channel
            if server.id not in self.settings:
                self.settings[server.id] = {}
            if channel.id not in self.settings[server.id]:
                self.settings[server.id][channel.id] = {}
            self.settings[server.id][channel.id] = self._check_online()
        dataIO.save_json(self.settings_filepath, self.settings)

    async def _check_online(self):
        url = "http://live.albiononline.com/status.txt"
        headers = {'user-agent': 'Tron-cog/1.0'}
        conn = aiohttp.TCPConnector(verify_ssl=False)
        session = aiohttp.ClientSession(connector=conn)
        async with session.get(url, headers=headers) as r:
            result = await r.json()
        session.close()
        if(result[status] == "online"):
            return True
        if(result[status] == "offline"):
            return False
        return False

    async def checkStatus(self):
        while True:
            await asyncio.sleep(360)
            server_online = self._check_online()
            for serverId in self.settings:
                for channel in self.settings[serverId]:
                    if channel != server_online and server_online == False:
                        await self.bot.send_message(channel, 'Albion Online Server ist offline!')
                    else:
                        await self.bot.send_message(channel, 'Albion Online Server ist wieder online!')

def check_folders():
    if not os.path.exists(settings_path):
        print("Creating data/dates directory...")
        os.makedirs(settings_path)

def check_files():
    if not dataIO.is_valid_json(settings_filepath):
        print("Creating "+ settings_filepath +"...")
        dataIO.save_json(f, {})

def setup(bot):
    check_folders()
    check_files()
    n = Albion(bot)
    bot.add_cog(n)
<|endoftext|>


<commit_before># Copyright 2010 by Steve Hakusa
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Handler for dynamically loading a bubble with attributes from a subject."""

import cache
import datetime
import logging
import model
from utils import db, get_message, run, to_local_isotime, value_or_dash
from utils import ErrorMessage, Handler, HIDDEN_ATTRIBUTE_NAMES

from google.appengine.api import users

def format(value, localize=False):
    """Formats values in a way that is suitable to display in the bubble.
    If 'localize' is true, the value is treated as a localizable message key or
    list of message keys to be looked up in the 'attribute_value' namespace."""
    if localize:
        if isinstance(value, list):
            value = [get_message('attribute_value', item) for item in value]
        else:
            value = get_message('attribute_value', value)
    if isinstance(value, unicode):
        return value.encode('utf-8')
    if isinstance(value, str) and value != '':
        return value.replace('\n', ' ')
    if isinstance(value, list) and value != []:
        return ', '.join(value).encode('utf-8')
    if isinstance(value, datetime.datetime):
        return to_local_isotime(value.replace(microsecond=0))
    if isinstance(value, db.GeoPt):
        latitude = u'%.4f\u00b0 %s' % (
            abs(value.lat), value.lat >= 0 and 'N' or 'S')
        longitude = u'%.4f\u00b0 %s' % (
            abs(value.lon), value.lon >= 0 and 'E' or 'W')
        return (latitude + ', ' + longitude).encode('utf-8')
    if isinstance (value, bool):
        return value and format(_('Yes')) or format(_('No'))
    return value_or_dash(value)

class ValueInfo:
    """Simple struct used by the django template to extract values"""
    def __init__(self, name, value, localize, author=None, affiliation=None,
                 comment=None, date=None):
        self.label = get_message('attribute_name', name)
        self.specified = value is not None
        self.raw = value
        self.value = format(value, localize)
        self.author = format(author)
        self.affiliation = format(affiliation)
        self.comment = format(comment)
        self.date = format(date)

class ValueInfoExtractor:
    """Base class to determine attributes to display in the bubble"""

    # TODO(kpy): The localized_attribute_names could be determined by looking
    # for attributes with type 'choice' or 'multi'.  Specifying them here is
    # redundant but saves us having to load Attribute entities from the
    # datastore.  If the cost of loading them turns out to be small, get rid
    # of this redundancy.
    def __init__(self, special_attribute_names, localized_attribute_names):
        """Initializes the ValueInfo Extractor.

        Args:
          special_attribute_names: names of attributes that are rendered
              specially in the bubble, not in the general list of attributes
          localized_attribute_names: names of attributes whose values should
              be localized (i.e. choice or multi attributes)"""
        self.special_attribute_names = special_attribute_names
        self.localized_attribute_names = localized_attribute_names

    def extract(self, subject, attribute_names):
        """Extracts attributes from the given subject.

        Args:
          subject: subject from which to extract attributes
          attribute_names: key_name of attributes to display for the subject

        Returns:
          A 3-tuple of specially-handled ValueInfos, non-special ValueInfos,
          and ValueInfos to be displayed in change details"""
        special = dict(
            (a, ValueInfo(get_message('attribute_name', a), None, False))
            for a in self.special_attribute_names)
        general = []
        details = []

        for name in attribute_names:
            value_info = self.get_value_info(subject, name)
            if not value_info:
                continue
            if name in special:
                special[name] = value_info
            else:
                general.append(value_info)
            details.append(value_info)
        return (special, general, details)

    def get_value_info(self, subject, attribute_name):
        observed = subject.get_observed(attribute_name)
        if observed:
            return ValueInfo(
                attribute_name,
                subject.get_value(attribute_name),
                attribute_name in self.localized_attribute_names,
                subject.get_author_nickname(attribute_name),
                subject.get_author_affiliation(attribute_name),
                subject.get_comment(attribute_name),
                observed)

class HospitalValueInfoExtractor(ValueInfoExtractor):
    template_name = 'templates/hospital_bubble.html'

    def __init__(self):
        ValueInfoExtractor.__init__(
            self,
            ['title', 'location', 'available_beds', 'total_beds', 'healthc_id',
             'pcode', 'address', 'services', 'operational_status'],
            # TODO(kpy): This list is redundant; see the comment above
            # in ValueInfoExtractor.
            ['services', 'organization_type', 'category', 'construction',
             'operational_status']
        )

    def extract(self, subject, attribute_names):
        (special, general, details) = ValueInfoExtractor.extract(
            self, subject, filter(lambda n: n not in HIDDEN_ATTRIBUTE_NAMES,
                                   attribute_names))
        value_info = ValueInfoExtractor.get_value_info(self, subject,
            'operational_status')
        if value_info:
            general.append(value_info)
        return (special, general, details)

VALUE_INFO_EXTRACTORS = {
    'haiti': {
        'hospital': HospitalValueInfoExtractor(),
    }
}

class Bubble(Handler):
    def get(self):
        # Need 'view' permission to see a bubble.
        self.require_action_permitted('view')

        subject = model.Subject.get(self.subdomain, self.params.subject_name)
        if not subject:
            #i18n: Error message for request missing subject name.
            raise ErrorMessage(404, _('Invalid or missing subject name.'))

        subscribed = ''
        if self.user:
            subject_name = '%s:%s' % (self.subdomain, self.params.subject_name)
            subscribed = bool(model.Subscription.get(subject_name,
                                                     self.user.email()))

        subject_type = cache.SUBJECT_TYPES[self.subdomain][subject.type]

        value_info_extractor = VALUE_INFO_EXTRACTORS[
            self.subdomain][subject.type]
        (special, general, details) = value_info_extractor.extract(
            subject, subject_type.attribute_names)
        edit_url = self.get_url('/edit', subject_name=self.params.subject_name,
                                embed='yes')
        login_url = users.create_login_url(
            self.get_url('/', subject_name=self.params.subject_name,
                         embed='yes'))
        frequency = self.account and self.account.default_frequency or 'instant'

        self.render(value_info_extractor.template_name,
                    user=self.user,
                    login_url=login_url,
                    edit_url=edit_url,
                    subdomain=self.subdomain,
                    subscribed=subscribed,
                    frequency=frequency,
                    subject_name=self.params.subject_name,
                    last_updated=max(detail.date for detail in details),
                    special=special,
                    general=general,
                    details=details)

if __name__ == '__main__':
    run([('/bubble', Bubble)], debug=True)
<commit_msg>Add default info extractor for 'pakistan'.<commit_after># Copyright 2010 by Steve Hakusa
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Handler for dynamically loading a bubble with attributes from a subject."""

import cache
import datetime
import logging
import model
from utils import db, get_message, run, to_local_isotime, value_or_dash
from utils import ErrorMessage, Handler, HIDDEN_ATTRIBUTE_NAMES

from google.appengine.api import users

def format(value, localize=False):
    """Formats values in a way that is suitable to display in the bubble.
    If 'localize' is true, the value is treated as a localizable message key or
    list of message keys to be looked up in the 'attribute_value' namespace."""
    if localize:
        if isinstance(value, list):
            value = [get_message('attribute_value', item) for item in value]
        else:
            value = get_message('attribute_value', value)
    if isinstance(value, unicode):
        return value.encode('utf-8')
    if isinstance(value, str) and value != '':
        return value.replace('\n', ' ')
    if isinstance(value, list) and value != []:
        return ', '.join(value).encode('utf-8')
    if isinstance(value, datetime.datetime):
        return to_local_isotime(value.replace(microsecond=0))
    if isinstance(value, db.GeoPt):
        latitude = u'%.4f\u00b0 %s' % (
            abs(value.lat), value.lat >= 0 and 'N' or 'S')
        longitude = u'%.4f\u00b0 %s' % (
            abs(value.lon), value.lon >= 0 and 'E' or 'W')
        return (latitude + ', ' + longitude).encode('utf-8')
    if isinstance (value, bool):
        return value and format(_('Yes')) or format(_('No'))
    return value_or_dash(value)

class ValueInfo:
    """Simple struct used by the django template to extract values"""
    def __init__(self, name, value, localize, author=None, affiliation=None,
                 comment=None, date=None):
        self.label = get_message('attribute_name', name)
        self.specified = value is not None
        self.raw = value
        self.value = format(value, localize)
        self.author = format(author)
        self.affiliation = format(affiliation)
        self.comment = format(comment)
        self.date = format(date)

class ValueInfoExtractor:
    """Base class to determine attributes to display in the bubble"""

    # TODO(kpy): The localized_attribute_names could be determined by looking
    # for attributes with type 'choice' or 'multi'.  Specifying them here is
    # redundant but saves us having to load Attribute entities from the
    # datastore.  If the cost of loading them turns out to be small, get rid
    # of this redundancy.
    def __init__(self, special_attribute_names, localized_attribute_names):
        """Initializes the ValueInfo Extractor.

        Args:
          special_attribute_names: names of attributes that are rendered
              specially in the bubble, not in the general list of attributes
          localized_attribute_names: names of attributes whose values should
              be localized (i.e. choice or multi attributes)"""
        self.special_attribute_names = special_attribute_names
        self.localized_attribute_names = localized_attribute_names

    def extract(self, subject, attribute_names):
        """Extracts attributes from the given subject.

        Args:
          subject: subject from which to extract attributes
          attribute_names: key_name of attributes to display for the subject

        Returns:
          A 3-tuple of specially-handled ValueInfos, non-special ValueInfos,
          and ValueInfos to be displayed in change details"""
        special = dict(
            (a, ValueInfo(get_message('attribute_name', a), None, False))
            for a in self.special_attribute_names)
        general = []
        details = []

        for name in attribute_names:
            value_info = self.get_value_info(subject, name)
            if not value_info:
                continue
            if name in special:
                special[name] = value_info
            else:
                general.append(value_info)
            details.append(value_info)
        return (special, general, details)

    def get_value_info(self, subject, attribute_name):
        observed = subject.get_observed(attribute_name)
        if observed:
            return ValueInfo(
                attribute_name,
                subject.get_value(attribute_name),
                attribute_name in self.localized_attribute_names,
                subject.get_author_nickname(attribute_name),
                subject.get_author_affiliation(attribute_name),
                subject.get_comment(attribute_name),
                observed)

class HospitalValueInfoExtractor(ValueInfoExtractor):
    template_name = 'templates/hospital_bubble.html'

    def __init__(self):
        ValueInfoExtractor.__init__(
            self,
            ['title', 'location', 'available_beds', 'total_beds', 'healthc_id',
             'pcode', 'address', 'services', 'operational_status'],
            # TODO(kpy): This list is redundant; see the comment above
            # in ValueInfoExtractor.
            ['services', 'organization_type', 'category', 'construction',
             'operational_status']
        )

    def extract(self, subject, attribute_names):
        (special, general, details) = ValueInfoExtractor.extract(
            self, subject, filter(lambda n: n not in HIDDEN_ATTRIBUTE_NAMES,
                                   attribute_names))
        value_info = ValueInfoExtractor.get_value_info(self, subject,
            'operational_status')
        if value_info:
            general.append(value_info)
        return (special, general, details)

VALUE_INFO_EXTRACTORS = {
    'haiti': {
        'hospital': HospitalValueInfoExtractor(),
    },
    'pakistan': {
        'hospital': HospitalValueInfoExtractor(),
    }
}

class Bubble(Handler):
    def get(self):
        # Need 'view' permission to see a bubble.
        self.require_action_permitted('view')

        subject = model.Subject.get(self.subdomain, self.params.subject_name)
        if not subject:
            #i18n: Error message for request missing subject name.
            raise ErrorMessage(404, _('Invalid or missing subject name.'))

        subscribed = ''
        if self.user:
            subject_name = '%s:%s' % (self.subdomain, self.params.subject_name)
            subscribed = bool(model.Subscription.get(subject_name,
                                                     self.user.email()))

        subject_type = cache.SUBJECT_TYPES[self.subdomain][subject.type]

        value_info_extractor = VALUE_INFO_EXTRACTORS[
            self.subdomain][subject.type]
        (special, general, details) = value_info_extractor.extract(
            subject, subject_type.attribute_names)
        edit_url = self.get_url('/edit', subject_name=self.params.subject_name,
                                embed='yes')
        login_url = users.create_login_url(
            self.get_url('/', subject_name=self.params.subject_name,
                         embed='yes'))
        frequency = self.account and self.account.default_frequency or 'instant'

        self.render(value_info_extractor.template_name,
                    user=self.user,
                    login_url=login_url,
                    edit_url=edit_url,
                    subdomain=self.subdomain,
                    subscribed=subscribed,
                    frequency=frequency,
                    subject_name=self.params.subject_name,
                    last_updated=max(detail.date for detail in details),
                    special=special,
                    general=general,
                    details=details)

if __name__ == '__main__':
    run([('/bubble', Bubble)], debug=True)
<|endoftext|>


<commit_before>import numpy as np
import math
import tarfile
import pandas as pd

small_graph = np.array([[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                     [1,0,0,0,0,0,0,0,0,0,0,0],
                     [0,1,0,0,0,0,0,0,0,0,0,0],
                     [0,0,0,0,0.5,0.5,0,0,0,0,0,0],
                     [0,0,0,0.5,0,0.5,0,0,0,0,0,0],
                     [0,0,0,0,1,0,0,0,0,0,0,0],
                     [0,0,0,0,0.5,0.5,0,0,0,0,0,0],
                     [0,0,0,0,0,0.5,0.5,0,0,0,0,0],
                     [0,0,1.0/3,0,0,0,0,1.0/3,0,0,0,1.0/3],
                     [0,0,0,0,0,0,0,0.5,0.5,0,0,0],
                     [0,0,0,0,0,0,0,0.5,0,0.5,0,0],
                     [0,0,0,0,0,0,0,0.5,0.5,0,0,0]])

alpha = 0.85 #damping factor
eps = 0.7

# open compressed p2p_gnut archive

comp_gnut = tarfile.open('p2p_gnut.tar.gz')
uncomp_gnut = comp_gnut.extractall()
comp_gnut.close()

# load p2p_gnut graph into panda dataframe then numpy array

df_gnut = pd.read_csv('p2p_gnut.csv', header=None)

p2p_gnut_graph = df_gnut.iloc[:,:].values

def maxnorm(x):
    norm = 0
    for i in range(len(x)):
        if (abs(x[i]) > norm):
            norm = abs(x[i])
    return norm

def maxnormdiffer(x1, x2):
    norm = 0
    for i in range(len(x1)):
        if (abs(x1[i]-x2[i]) > norm):
            norm = abs(x1[i]-x2[i])
    return norm

def euclnorm(x):
    norm = 0
    for i in range(len(x)):
        norm += abs(x[i])**(2)
    return math.sqrt(norm)

def phi(x, M):
    x1 = []
    for i in range(len(x)):
        x1.append((M*(np.matrix(x).T)).getA1()[i]-x[i])
    return (euclnorm(x1)+eps*euclnorm(x))

def pagerank (M):
    """
    The function gets a contingency matrix of a graph and computes the pagerank score with alpha = 0.85
    It implements an iterative approach and also returns the arrays of deviations
    from the optimum on every step to evaluate the rate of convergence
    args:
        M - contingency matrix of a graph, scaled to be a probabilistic matrix (numpy nxn array)
    return (weights, conv_func, conv_norm)
        weights - pagerank scores (numpy n*1 vector)
        conv_func - numpy vector with deviations from the optimum, described in terms of target function
        conv_norm - numpy vector with deviations from the optimum, described in terms of norm (euklid?)
    """

    M = M.T

    weights = np.zeros((M.shape[0], 1))
    conv_func = np.array([]) #if it is more convenient to you, you can use simple python list here

    n = len(M)

    e = [1/n for i in range(n)]

    x = []#iterations to optimum
    conv_norm = []#deviations from optimum
    x.append(e)
    k = 1

    x_new = (1-(1.0/(k+1)))*M*(np.matrix(x[k-1]).T) + (1/(k+1))*(np.matrix(e).T)
    x.append(x_new.getA1())
    k+=1

    while(phi(x[k-1], M) < phi(x[k-2], M)):
        x_new = (1-1/(k+1))*M*(np.matrix(x[k-1]).T) + (1/(k+1))*(np.matrix(e).T)
        x.append(x_new.getA1())
        k+=1

    for i in range(0, k-1):
        conv_norm.append(maxnormdiffer(x[k-1], x[i]))

    weights = x[k-1]

    return (weights, conv_norm)

print(pagerank(p2p_gnut_graph))<commit_msg>no message<commit_after>import numpy as np
import math
import tarfile
import pandas as pd

small_graph = np.array([[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                     [1,0,0,0,0,0,0,0,0,0,0,0],
                     [0,1,0,0,0,0,0,0,0,0,0,0],
                     [0,0,0,0,0.5,0.5,0,0,0,0,0,0],
                     [0,0,0,0.5,0,0.5,0,0,0,0,0,0],
                     [0,0,0,0,1,0,0,0,0,0,0,0],
                     [0,0,0,0,0.5,0.5,0,0,0,0,0,0],
                     [0,0,0,0,0,0.5,0.5,0,0,0,0,0],
                     [0,0,1.0/3,0,0,0,0,1.0/3,0,0,0,1.0/3],
                     [0,0,0,0,0,0,0,0.5,0.5,0,0,0],
                     [0,0,0,0,0,0,0,0.5,0,0.5,0,0],
                     [0,0,0,0,0,0,0,0.5,0.5,0,0,0]])

alpha = 0.85 #damping factor
eps = 5

# open compressed p2p_gnut archive

comp_gnut = tarfile.open('p2p_gnut.tar.gz')
uncomp_gnut = comp_gnut.extractall()
comp_gnut.close()

# load p2p_gnut graph into panda dataframe then numpy array

df_gnut = pd.read_csv('p2p_gnut.csv', header=None)

p2p_gnut_graph = df_gnut.iloc[:,:].values

def maxnorm(x):
    norm = 0
    for i in range(len(x)):
        if (abs(x[i]) > norm):
            norm = abs(x[i])
    return norm

def maxnormdiffer(x1, x2):
    norm = 0
    for i in range(len(x1)):
        if (abs(x1[i]-x2[i]) > norm):
            norm = abs(x1[i]-x2[i])
    return norm

def euclnorm(x):
    norm = 0
    for i in range(len(x)):
        norm += abs(x[i])**(2)
    return math.sqrt(norm)

def phi(x, M):
    x1 = []
    for i in range(len(x)):
        x1.append((M*(np.matrix(x).T)).getA1()[i]-x[i])
    return (euclnorm(x1)+eps*euclnorm(x))

def pagerank (M):
    """
    The function gets a contingency matrix of a graph and computes the pagerank score with alpha = 0.85
    It implements an iterative approach and also returns the arrays of deviations
    from the optimum on every step to evaluate the rate of convergence
    args:
        M - contingency matrix of a graph, scaled to be a probabilistic matrix (numpy nxn array)
    return (weights, conv_func, conv_norm)
        weights - pagerank scores (numpy n*1 vector)
        conv_func - numpy vector with deviations from the optimum, described in terms of target function
        conv_norm - numpy vector with deviations from the optimum, described in terms of norm (euklid?)
    """

    n = len(M)

    v = [] #personalization vector
    for i in range(n):
        v.append(1/n)

    d = []#dangling nodes column
    for i in range(n):
        iszero = 1
        for j in range(n):
            if (M[i, j] != 0): iszero = 0
        d.append(iszero)
    d = np.matrix(d).T

    M = M + d*v #matrix with fixed dangling nodes

    M = M.T

    weights = []
    conv_func = np.array([]) #if it is more convenient to you, you can use simple python list here

    n = len(M)

    e = [1/n for i in range(n)]
    e = np.matrix(e).T

    x = []#iterations to optimum
    conv_norm = []#deviations from optimum
    x.append(e)
    k = 1

    x_new = (1-1/(k+1))*M*x[k-1] + (1/(k+1))*e
    x.append(x_new)
    k+=1

    while(1):
        x_new = (1-1/(k+1))*M*x[k-1] + (1/(k+1))*e
        x.append(x_new)
        k+=1
        if (k > 10**(2)): break

    for i in range(0, k-1):
        conv_norm.append(maxnormdiffer(x[k-1].getA1(), x[i].getA1()))

    weights = x[k-1]

    return (weights, conv_norm)

v = pagerank(p2p_gnut_graph[0])
for i in range(50):
    print(v[i])

<|endoftext|>


<commit_before># Copyright (C) 2011, Quentin "mefyl" Hocquet
#
# This software is provided "as is" without warranty of any kind,
# either expressed or implied, including but not limited to the
# implied warranties of fitness for a particular purpose.
#
# See the LICENSE file for more information.

import os
import sys
import threading

DEBUG_TRACE = 1
DEBUG_TRACE_PLUS = 2
DEBUG_DEPS = 2
DEBUG_SCHED = 3

_DEBUG = 0
if 'DRAKE_DEBUG' in os.environ:
  _DEBUG = int(os.environ['DRAKE_DEBUG'])
_INDENT = 0
_DEBUG_SEM = threading.Semaphore(1)

def debug(msg, lvl = 1):
  if lvl <= _DEBUG:
    with _DEBUG_SEM:
      print >> sys.stderr, '%s%s' % (' ' * _INDENT * 2, msg)

class indentation:

  def __enter__(self):
    global _INDENT
    _INDENT += 1

  def __exit__(self, type, value, traceback):
    global _INDENT
    _INDENT -= 1
<commit_msg>Debug: always show the current job.<commit_after># Copyright (C) 2011, Quentin "mefyl" Hocquet
#
# This software is provided "as is" without warranty of any kind,
# either expressed or implied, including but not limited to the
# implied warranties of fitness for a particular purpose.
#
# See the LICENSE file for more information.

import os
import sys
import threading
import drake

DEBUG_TRACE = 1
DEBUG_TRACE_PLUS = 2
DEBUG_DEPS = 2
DEBUG_SCHED = 3

_DEBUG = 0
if 'DRAKE_DEBUG' in os.environ:
  _DEBUG = int(os.environ['DRAKE_DEBUG'])
_INDENT = 0
_DEBUG_SEM = threading.Semaphore(1)

def debug(msg, lvl = 1):
  if lvl <= _DEBUG:
    with _DEBUG_SEM:
      print >> sys.stderr, '%s%s: %s' % (' ' * _INDENT * 2, drake._SCHEDULER.current_job_id(), msg)

class indentation:

  def __enter__(self):
    global _INDENT
    _INDENT += 1

  def __exit__(self, type, value, traceback):
    global _INDENT
    _INDENT -= 1
<|endoftext|>


<commit_before>#!/usr/bin/env python
import threading, logging, time
import multiprocessing

from kafka import KafkaConsumer, KafkaProducer

class Producer(threading.Thread):
    daemon = True

    def run(self):
        producer = KafkaProducer(bootstrap_servers='localhost:9092')

        while True:
            producer.send('my-topic', b"test")
            producer.send('my-topic', b"\xc2Hola, mundo!")
            time.sleep(1)

class Consumer(multiprocessing.Process):
    daemon = True

    def run(self):
        consumer = KafkaConsumer(bootstrap_servers='localhost:9092',
                                 auto_offset_reset='earliest')
        consumer.subscribe(['my-topic'])

        for message in consumer:
            print (message)

def main():
    tasks = [
        Producer(),
        Consumer()
    ]

    for t in tasks:
        t.start()

    time.sleep(10)

if __name__ == "__main__":
    logging.basicConfig(
        format='%(asctime)s.%(msecs)s:%(name)s:%(thread)d:%(levelname)s:%(process)d:%(message)s',
        level=logging.INFO
        )
    main()
<commit_msg>Added controlled thread shutdown to example.py (#1268)<commit_after>#!/usr/bin/env python
import threading, logging, time
import multiprocessing

from kafka import KafkaConsumer, KafkaProducer

class Producer(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.stop_event = threading.Event()

    def stop(self):
        self.stop_event.set()

    def run(self):
        producer = KafkaProducer(bootstrap_servers='localhost:9092')

        while not self.stop_event.is_set():
            producer.send('my-topic', b"test")
            producer.send('my-topic', b"\xc2Hola, mundo!")
            time.sleep(1)

        producer.close()

class Consumer(multiprocessing.Process):
    def __init__(self):
        multiprocessing.Process.__init__(self)
        self.stop_event = multiprocessing.Event()

    def stop(self):
        self.stop_event.set()

    def run(self):
        consumer = KafkaConsumer(bootstrap_servers='localhost:9092',
                                 auto_offset_reset='earliest',
                                 consumer_timeout_ms=1000)
        consumer.subscribe(['my-topic'])

        while not self.stop_event.is_set():
            for message in consumer:
                print(message)
                if self.stop_event.is_set():
                    break

        consumer.close()

def main():
    tasks = [
        Producer(),
        Consumer()
    ]

    for t in tasks:
        t.start()

    time.sleep(10)

    for task in tasks:
        task.stop()

    for task in tasks:
        task.join()

if __name__ == "__main__":
    logging.basicConfig(
        format='%(asctime)s.%(msecs)s:%(name)s:%(thread)d:%(levelname)s:%(process)d:%(message)s',
        level=logging.INFO
        )
    main()
<|endoftext|>


<commit_before>__all__ = ["n3_bias_field_correction", "n3_bias_field_correction2", "n4_bias_field_correction", "abp_n4"]

from . import process_args as pargs
from .get_mask import get_mask
from .iMath import iMath

from ..core import ants_image as iio
from .. import utils

def n3_bias_field_correction(image, downsample_factor=3):
    """
    N3 Bias Field Correction

    ANTsR function: `n3BiasFieldCorrection`

    Arguments
    ---------
    image : ANTsImage
        image to be bias corrected

    downsample_factor : scalar
        how much to downsample image before performing bias correction

    Returns
    -------
    ANTsImage

    Example
    -------
    >>> import ants
    >>> image = ants.image_read( ants.get_ants_data('r16') )
    >>> image_n3 = ants.n3_bias_field_correction(image)
    """
    outimage = image.clone()
    args = [image.dimension, image, outimage, downsample_factor]
    processed_args = pargs._int_antsProcessArguments(args)
    libfn = utils.get_lib_fn("N3BiasFieldCorrection")
    libfn(processed_args)
    return outimage

def n3_bias_field_correction2(
    image,
    mask=None,
    shrink_factor=4,
    convergence={"iters": 50, "tol": 0.0},
    spline_param=None,
    number_of_fitting_levels=4,
    return_bias_field=False,
    verbose=False,
    weight_mask=None,
):
    """
    N3 Bias Field Correction

    ANTsR function: `n3BiasFieldCorrection2`

    Arguments
    ---------
    image : ANTsImage
        image to bias correct

    mask : ANTsImage
        input mask, if one is not passed one will be made

    shrink_factor : scalar
        Shrink factor for multi-resolution correction, typically integer less than 4

    convergence : dict w/ keys `iters` and `tol`
        iters : maximum number of iterations
        tol : the convergence tolerance.

    spline_param : float or vector Parameter controlling number of control
        points in spline. Either single value, indicating the spacing in each
        direction, or vector with one entry per dimension of image, indicating
        the mesh size.  Default is a mesh size of 1 per dimension.

    number_of_fitting_levels : integer
        Number of fitting levels per iteration.

    return_bias_field : boolean
        Return bias field instead of bias corrected image.

    verbose : boolean
        enables verbose output.

    weight_mask : ANTsImage (optional)
        antsImage of weight mask

    Returns
    -------
    ANTsImage

    Example
    -------
    >>> image = ants.image_read( ants.get_ants_data('r16') )
    >>> image_n3 = ants.n3_bias_field_correction2(image)
    """
    if image.pixeltype != "float":
        image = image.clone("float")
    iters = convergence["iters"]
    tol = convergence["tol"]
    if mask is None:
        mask = get_mask(image)
    if spline_param is None:
        spline_param = [1] * image.dimension

    N3_CONVERGENCE_1 = "[%i,%.10f]" % (iters, tol)
    N3_SHRINK_FACTOR_1 = str(shrink_factor)
    if (not isinstance(spline_param, (list, tuple))) or (len(spline_param) == 1):
        N3_BSPLINE_PARAMS = "[%i,%i]" % (spline_param, number_of_fitting_levels)
    elif (isinstance(spline_param, (list, tuple))) and (
        len(spline_param) == image.dimension
    ):
        N3_BSPLINE_PARAMS = "[%s,%i]" % (("x".join([str(sp) for sp in spline_param])), number_of_fitting_levels)
    else:
        raise ValueError(
            "Length of splineParam must either be 1 or dimensionality of image"
        )

    if weight_mask is not None:
        if not isinstance(weight_mask, iio.ANTsImage):
            raise ValueError("Weight Image must be an antsImage")

    outimage = image.clone("float")
    outbiasfield = image.clone("float")
    i = utils.get_pointer_string(outimage)
    b = utils.get_pointer_string(outbiasfield)
    output = "[%s,%s]" % (i, b)

    kwargs = {
        "d": outimage.dimension,
        "i": image,
        "w": weight_mask,
        "s": N3_SHRINK_FACTOR_1,
        "c": N3_CONVERGENCE_1,
        "b": N3_BSPLINE_PARAMS,
        "x": mask,
        "r": 1,
        "o": output,
        "v": int(verbose),
    }

    processed_args = pargs._int_antsProcessArguments(kwargs)
    libfn = utils.get_lib_fn("N3BiasFieldCorrection")
    libfn(processed_args)
    if return_bias_field == True:
        return outbiasfield
    else:
        return outimage

def n4_bias_field_correction(
    image,
    mask=None,
    shrink_factor=4,
    convergence={"iters": [50, 50, 50, 50], "tol": 0.0},
    spline_param=None,
    return_bias_field=False,
    verbose=False,
    weight_mask=None,
):
    """
    N4 Bias Field Correction

    ANTsR function: `n4BiasFieldCorrection`

    Arguments
    ---------
    image : ANTsImage
        image to bias correct

    mask : ANTsImage
        input mask, if one is not passed one will be made

    shrink_factor : scalar
        Shrink factor for multi-resolution correction, typically integer less than 4

    convergence : dict w/ keys `iters` and `tol`
        iters : vector of maximum number of iterations for each level
        tol : the convergence tolerance.

    spline_param : float or vector
        Parameter controlling number of control points in spline. Either single value,
        indicating the spacing in each direction, or vector with one entry per
        dimension of image, indicating the mesh size.  Default is a mesh size of
        1 per dimension.

    return_bias_field : boolean
        Return bias field instead of bias corrected image.

    verbose : boolean
        enables verbose output.

    weight_mask : ANTsImage (optional)
        antsImage of weight mask

    Returns
    -------
    ANTsImage

    Example
    -------
    >>> image = ants.image_read( ants.get_ants_data('r16') )
    >>> image_n4 = ants.n4_bias_field_correction(image)
    """
    if image.pixeltype != "float":
        image = image.clone("float")
    iters = convergence["iters"]
    tol = convergence["tol"]
    if mask is None:
        mask = get_mask(image)
    if spline_param is None:
        spline_param = [1] * image.dimension

    N4_CONVERGENCE_1 = "[%s, %.10f]" % ("x".join([str(it) for it in iters]), tol)
    N4_SHRINK_FACTOR_1 = str(shrink_factor)
    if (not isinstance(spline_param, (list, tuple))) or (len(spline_param) == 1):
        N4_BSPLINE_PARAMS = "[%i]" % spline_param
    elif (isinstance(spline_param, (list, tuple))) and (
        len(spline_param) == image.dimension
    ):
        N4_BSPLINE_PARAMS = "[%s]" % ("x".join([str(sp) for sp in spline_param]))
    else:
        raise ValueError(
            "Length of splineParam must either be 1 or dimensionality of image"
        )

    if weight_mask is not None:
        if not isinstance(weight_mask, iio.ANTsImage):
            raise ValueError("Weight Image must be an antsImage")

    outimage = image.clone("float")
    outbiasfield = image.clone("float")
    i = utils.get_pointer_string(outimage)
    b = utils.get_pointer_string(outbiasfield)
    output = "[%s,%s]" % (i, b)

    kwargs = {
        "d": outimage.dimension,
        "i": image,
        "w": weight_mask,
        "s": N4_SHRINK_FACTOR_1,
        "c": N4_CONVERGENCE_1,
        "b": N4_BSPLINE_PARAMS,
        "x": mask,
        "r": 1,
        "o": output,
        "v": int(verbose),
    }

    processed_args = pargs._int_antsProcessArguments(kwargs)
    libfn = utils.get_lib_fn("N4BiasFieldCorrection")
    libfn(processed_args)
    if return_bias_field == True:
        return outbiasfield
    else:
        return outimage

def abp_n4(image, intensity_truncation=(0.025, 0.975, 256), mask=None, usen3=False):
    """
    Truncate outlier intensities and bias correct with the N4 algorithm.

    ANTsR function: `abpN4`

    Arguments
    ---------
    image : ANTsImage
        image to correct and truncate

    intensity_truncation : 3-tuple
        quantiles for intensity truncation

    mask : ANTsImage (optional)
        mask for bias correction

    usen3 : boolean
        if True, use N3 bias correction instead of N4

    Returns
    -------
    ANTsImage

    Example
    -------
    >>> import ants
    >>> image = ants.image_read(ants.get_ants_data('r16'))
    >>> image2 = ants.abp_n4(image)
    """
    if (not isinstance(intensity_truncation, (list, tuple))) or (
        len(intensity_truncation) != 3
    ):
        raise ValueError("intensity_truncation must be list/tuple with 3 values")
    outimage = iMath(
        image,
        "TruncateIntensity",
        intensity_truncation[0],
        intensity_truncation[1],
        intensity_truncation[2],
    )
    if usen3 == True:
        outimage = n3_bias_field_correction(outimage, 4)
        outimage = n3_bias_field_correction(outimage, 2)
        return outimage
    else:
        outimage = n4_bias_field_correction(outimage, mask)
        return outimage
<commit_msg>ENH:  Expose rescale option.<commit_after>__all__ = ["n3_bias_field_correction", "n3_bias_field_correction2", "n4_bias_field_correction", "abp_n4"]

from . import process_args as pargs
from .get_mask import get_mask
from .iMath import iMath

from ..core import ants_image as iio
from .. import utils

def n3_bias_field_correction(image, downsample_factor=3):
    """
    N3 Bias Field Correction

    ANTsR function: `n3BiasFieldCorrection`

    Arguments
    ---------
    image : ANTsImage
        image to be bias corrected

    downsample_factor : scalar
        how much to downsample image before performing bias correction

    Returns
    -------
    ANTsImage

    Example
    -------
    >>> import ants
    >>> image = ants.image_read( ants.get_ants_data('r16') )
    >>> image_n3 = ants.n3_bias_field_correction(image)
    """
    outimage = image.clone()
    args = [image.dimension, image, outimage, downsample_factor]
    processed_args = pargs._int_antsProcessArguments(args)
    libfn = utils.get_lib_fn("N3BiasFieldCorrection")
    libfn(processed_args)
    return outimage

def n3_bias_field_correction2(
    image,
    mask=None,
    rescale_intensities=False,
    shrink_factor=4,
    convergence={"iters": 50, "tol": 1e-7},
    spline_param=None,
    number_of_fitting_levels=4,
    return_bias_field=False,
    verbose=False,
    weight_mask=None,
):
    """
    N3 Bias Field Correction

    ANTsR function: `n3BiasFieldCorrection2`

    Arguments
    ---------
    image : ANTsImage
        image to bias correct

    mask : ANTsImage
        input mask, if one is not passed one will be made

    rescale_intensities : boolean
        At each iteration, a new intensity mapping is
        calculated and applied but there is nothing which constrains the new
        intensity range to be within certain values. The result is that the
        range can "drift" from the original at each iteration. This option
        rescales to the [min,max] range of the original image intensities within
        the user-specified mask. A mask is required to perform rescaling.  Default
        is False in ANTsR/ANTsPy but True in ANTs.

    shrink_factor : scalar
        Shrink factor for multi-resolution correction, typically integer less than 4

    convergence : dict w/ keys `iters` and `tol`
        iters : maximum number of iterations
        tol : the convergence tolerance.  Default tolerance is 1e-7 in ANTsR/ANTsPy but 0.0 in ANTs.

    spline_param : float or vector Parameter controlling number of control
        points in spline. Either single value, indicating the spacing in each
        direction, or vector with one entry per dimension of image, indicating
        the mesh size.  Default is a mesh size of 1 per dimension.

    number_of_fitting_levels : integer
        Number of fitting levels per iteration.

    return_bias_field : boolean
        Return bias field instead of bias corrected image.

    verbose : boolean
        enables verbose output.

    weight_mask : ANTsImage (optional)
        antsImage of weight mask

    Returns
    -------
    ANTsImage

    Example
    -------
    >>> image = ants.image_read( ants.get_ants_data('r16') )
    >>> image_n3 = ants.n3_bias_field_correction2(image)
    """
    if image.pixeltype != "float":
        image = image.clone("float")
    iters = convergence["iters"]
    tol = convergence["tol"]
    if mask is None:
        mask = get_mask(image)
    if spline_param is None:
        spline_param = [1] * image.dimension

    N3_CONVERGENCE_1 = "[%i,%.10f]" % (iters, tol)
    N3_SHRINK_FACTOR_1 = str(shrink_factor)
    if (not isinstance(spline_param, (list, tuple))) or (len(spline_param) == 1):
        N3_BSPLINE_PARAMS = "[%i,%i]" % (spline_param, number_of_fitting_levels)
    elif (isinstance(spline_param, (list, tuple))) and (
        len(spline_param) == image.dimension
    ):
        N3_BSPLINE_PARAMS = "[%s,%i]" % (("x".join([str(sp) for sp in spline_param])), number_of_fitting_levels)
    else:
        raise ValueError(
            "Length of splineParam must either be 1 or dimensionality of image"
        )

    if weight_mask is not None:
        if not isinstance(weight_mask, iio.ANTsImage):
            raise ValueError("Weight Image must be an antsImage")

    outimage = image.clone("float")
    outbiasfield = image.clone("float")
    i = utils.get_pointer_string(outimage)
    b = utils.get_pointer_string(outbiasfield)
    output = "[%s,%s]" % (i, b)

    kwargs = {
        "d": outimage.dimension,
        "i": image,
        "w": weight_mask,
        "s": N3_SHRINK_FACTOR_1,
        "c": N3_CONVERGENCE_1,
        "b": N3_BSPLINE_PARAMS,
        "x": mask,
        "r": int(rescale_intensities),
        "o": output,
        "v": int(verbose),
    }

    processed_args = pargs._int_antsProcessArguments(kwargs)
    libfn = utils.get_lib_fn("N3BiasFieldCorrection")
    libfn(processed_args)
    if return_bias_field == True:
        return outbiasfield
    else:
        return outimage

def n4_bias_field_correction(
    image,
    mask=None,
    rescale_intensities=False,
    shrink_factor=4,
    convergence={"iters": [50, 50, 50, 50], "tol": 1e-7},
    spline_param=None,
    return_bias_field=False,
    verbose=False,
    weight_mask=None,
):
    """
    N4 Bias Field Correction

    ANTsR function: `n4BiasFieldCorrection`

    Arguments
    ---------
    image : ANTsImage
        image to bias correct

    mask : ANTsImage
        input mask, if one is not passed one will be made

    rescale_intensities : boolean
        At each iteration, a new intensity mapping is
        calculated and applied but there is nothing which constrains the new
        intensity range to be within certain values. The result is that the
        range can "drift" from the original at each iteration. This option
        rescales to the [min,max] range of the original image intensities within
        the user-specified mask. A mask is required to perform rescaling.  Default
        is False in ANTsR/ANTsPy but True in ANTs.

    shrink_factor : scalar
        Shrink factor for multi-resolution correction, typically integer less than 4

    convergence : dict w/ keys `iters` and `tol`
        iters : vector of maximum number of iterations for each level
        tol : the convergence tolerance.  Default tolerance is 1e-7 in ANTsR/ANTsPy but 0.0 in ANTs.

    spline_param : float or vector
        Parameter controlling number of control points in spline. Either single value,
        indicating the spacing in each direction, or vector with one entry per
        dimension of image, indicating the mesh size.  Default is a mesh size of
        1 per dimension.

    return_bias_field : boolean
        Return bias field instead of bias corrected image.

    verbose : boolean
        enables verbose output.

    weight_mask : ANTsImage (optional)
        antsImage of weight mask

    Returns
    -------
    ANTsImage

    Example
    -------
    >>> image = ants.image_read( ants.get_ants_data('r16') )
    >>> image_n4 = ants.n4_bias_field_correction(image)
    """
    if image.pixeltype != "float":
        image = image.clone("float")
    iters = convergence["iters"]
    tol = convergence["tol"]
    if mask is None:
        mask = get_mask(image)
    if spline_param is None:
        spline_param = [1] * image.dimension

    N4_CONVERGENCE_1 = "[%s, %.10f]" % ("x".join([str(it) for it in iters]), tol)
    N4_SHRINK_FACTOR_1 = str(shrink_factor)
    if (not isinstance(spline_param, (list, tuple))) or (len(spline_param) == 1):
        N4_BSPLINE_PARAMS = "[%i]" % spline_param
    elif (isinstance(spline_param, (list, tuple))) and (
        len(spline_param) == image.dimension
    ):
        N4_BSPLINE_PARAMS = "[%s]" % ("x".join([str(sp) for sp in spline_param]))
    else:
        raise ValueError(
            "Length of splineParam must either be 1 or dimensionality of image"
        )

    if weight_mask is not None:
        if not isinstance(weight_mask, iio.ANTsImage):
            raise ValueError("Weight Image must be an antsImage")

    outimage = image.clone("float")
    outbiasfield = image.clone("float")
    i = utils.get_pointer_string(outimage)
    b = utils.get_pointer_string(outbiasfield)
    output = "[%s,%s]" % (i, b)

    kwargs = {
        "d": outimage.dimension,
        "i": image,
        "w": weight_mask,
        "s": N4_SHRINK_FACTOR_1,
        "c": N4_CONVERGENCE_1,
        "b": N4_BSPLINE_PARAMS,
        "x": mask,
        "r": int(rescale_intensities),
        "o": output,
        "v": int(verbose),
    }

    processed_args = pargs._int_antsProcessArguments(kwargs)
    libfn = utils.get_lib_fn("N4BiasFieldCorrection")
    libfn(processed_args)
    if return_bias_field == True:
        return outbiasfield
    else:
        return outimage

def abp_n4(image, intensity_truncation=(0.025, 0.975, 256), mask=None, usen3=False):
    """
    Truncate outlier intensities and bias correct with the N4 algorithm.

    ANTsR function: `abpN4`

    Arguments
    ---------
    image : ANTsImage
        image to correct and truncate

    intensity_truncation : 3-tuple
        quantiles for intensity truncation

    mask : ANTsImage (optional)
        mask for bias correction

    usen3 : boolean
        if True, use N3 bias correction instead of N4

    Returns
    -------
    ANTsImage

    Example
    -------
    >>> import ants
    >>> image = ants.image_read(ants.get_ants_data('r16'))
    >>> image2 = ants.abp_n4(image)
    """
    if (not isinstance(intensity_truncation, (list, tuple))) or (
        len(intensity_truncation) != 3
    ):
        raise ValueError("intensity_truncation must be list/tuple with 3 values")
    outimage = iMath(
        image,
        "TruncateIntensity",
        intensity_truncation[0],
        intensity_truncation[1],
        intensity_truncation[2],
    )
    if usen3 == True:
        outimage = n3_bias_field_correction(outimage, 4)
        outimage = n3_bias_field_correction(outimage, 2)
        return outimage
    else:
        outimage = n4_bias_field_correction(outimage, mask)
        return outimage
<|endoftext|>


<commit_before>import commands
import os
import Queue
import settings
import time
import threading
import serial
import sys
import traceback

from thirtybirds_2_0.Network.manager import init as network_init
from thirtybirds_2_0.Updates.manager import init as updates_init

class Network(object):
    def __init__(self, hostname, network_message_handler, network_status_handler):
        self.hostname = hostname
        self.thirtybirds = network_init(
            hostname=hostname,
            role="client",
            discovery_multicastGroup=settings.discovery_multicastGroup,
            discovery_multicastPort=settings.discovery_multicastPort,
            discovery_responsePort=settings.discovery_responsePort,
            pubsub_pubPort=settings.pubsub_pubPort,
            message_callback=network_message_handler,
            status_callback=network_status_handler
        )

########################
## UTILS
########################

class Utils(object):
    def __init__(self, hostname):
        self.hostname = hostname
    def reboot(self):
        os.system("sudo reboot now")

    def remote_update_git(self, oratio, thirtybirds, update, upgrade):
        if oratio:
            subprocess.call(['sudo', 'git', 'pull'], cwd='/home/pi/oratio')
        if thirtybirds:
            subprocess.call(['sudo', 'git', 'pull'], cwd='/home/pi/thirtybirds_2_0')
        return

    def remote_update_scripts(self):
        updates_init("/home/pi/oratio", False, True)
        return

    def get_update_script_version(self):
        (updates, ghStatus, bsStatus) = updates_init("/home/pi/oratio", False, False)
        return updates.read_version_pickle()

    def get_git_timestamp(self):
        return commands.getstatusoutput("cd /home/pi/oratio/; git log -1 --format=%cd")[1]

    def get_temp(self):
        return commands.getstatusoutput("/opt/vc/bin/vcgencmd measure_temp")[1]

    def get_cpu(self):
        bash_output = commands.getstatusoutput("uptime")[1]
        split_output = bash_output.split(" ")
        return split_output[12]

    def get_uptime(self):
        bash_output = commands.getstatusoutput("uptime")[1]
        split_output = bash_output.split(" ")
        return split_output[4]

    def get_disk(self):
        # stub for now
        return "0"

    def get_client_status(self):
        return (self.hostname, self.get_update_script_version(), self.get_git_timestamp(), self.get_temp(), self.get_cpu(), self.get_uptime(), self.get_disk())

# Main handles network send/recv and can see all other classes directly
class Main(threading.Thread):
    def __init__(self, hostname):
        threading.Thread.__init__(self)
        self.network = Network(hostname, self.network_message_handler, self.network_status_handler)
        self.queue = Queue.Queue()
        self.ser=serial.Serial("/dev/ttyACM0",9600)
        self.ser.baudrate=9600
        self.utils = Utils(hostname)
        self.network.thirtybirds.subscribe_to_topic("mandala_device_status")
        self.UNSET = 0
        self.FAIL = 2000
        self.PASS = 4000
        self.mandala_device_status = None
        self.mandala_tlc_ids = {
            "avl-controller":39,
            "avl-formant-1":15,
            "avl-formant-1-amplifier":4,
            "avl-formant-2":16,
            "avl-formant-2-amplifier":5,
            "avl-formant-3":17,
            "avl-formant-3-amplifier":6,
            "avl-layer-1":40,
            "avl-layer-2":11,
            "avl-layer-3":12,
            "avl-medulla":35,
            "avl-pitch-keys":18,
            "avl-pitch-keys-sensor-1":7,
            "avl-pitch-keys-sensor-2":8,
            "avl-pitch-keys-sensor-3":9,
            "avl-pitch-keys-sensor-4":10,
            "avl-settings":34,
            "avl-settings-adcs":24,
            "avl-transport":13,
            "avl-transport-encoder":0,
            "avl-voice-1":36,
            "avl-voice-1-crystal-frequency-counter":25,
            "avl-voice-1-harmonic-generators":26,
            "avl-voice-1-harmonic-volume":27,
            "avl-voice-2":37,
            "avl-voice-2-crystal-frequency-counter":28,
            "avl-voice-2-harmonic-generators":29,
            "avl-voice-2-harmonic-volume":30,
            "avl-voice-3":38,
            "avl-voice-3-crystal-frequency-counter":31,
            "avl-voice-3-harmonic-generators":32,
            "avl-voice-3-harmonic-volume":33,
            "avl-voice-keys":14,
            "avl-voice-keys-encoder-1":1,
            "avl-voice-keys-encoder-2":2,
            "avl-voice-keys-encoder-3":3
        }

    def network_message_handler(self, topic_msg):
        # this method runs in the thread of the caller, not the tread of Main
        topic, msg =  topic_msg # separating just to eval msg.  best to do it early.  it should be done in TB.
        #if len(msg) > 0:
        #    msg = eval(msg)
        self.add_to_queue(topic, msg)

    def network_status_handler(self, topic_msg):
        # this method runs in the thread of the caller, not the tread of Main
        print "Main.network_status_handler", topic_msg

    def add_to_queue(self, topic, msg):
        self.queue.put((topic, msg))

    def run(self):
        while True:
            if self.mandala_device_status == None:
                time.sleep(1)
                self.network.thirtybirds.send("mandala_device_status_request", True)
            try:
                topic, msg = self.queue.get(True)
                if topic == "mandala_device_status":
                    self.mandala_device_status = eval(msg)
                    devicenames = self.mandala_device_status.keys()
                    devicenames.sort()
                    for devicename in devicenames:
                        tlc_id_int = self.mandala_tlc_ids[devicename] + 5000
                        tlc_id_str = "{}\n".format(tlc_id_int)
                        if self.mandala_device_status[devicename] == "unset":
                            tlc_level_int = 0
                        if self.mandala_device_status[devicename] == "fail":
                            tlc_level_int = 100
                        if self.mandala_device_status[devicename] == "pass":
                            tlc_level_int = 4000
                        tlc_level_str = "{}\n".format(tlc_level_int)
                        print tlc_id_int, tlc_level_int, devicename
                        self.ser.write(tlc_id_str)
                        self.ser.write(tlc_level_str)

                    print ""
                    print ""
                    #self.mandala_tlc_ids
                    #print topic, msg
                time.sleep(0.01)
            except Exception as e:
                exc_type, exc_value, exc_traceback = sys.exc_info()
                print e, repr(traceback.format_exception(exc_type, exc_value,exc_traceback))

def init(hostname):
    main = Main(hostname)
    main.daemon = True
    main.start()
    return main

<commit_msg>first test of mandala code<commit_after>import commands
import os
import Queue
import settings
import time
import threading
import serial
import sys
import traceback

from thirtybirds_2_0.Network.manager import init as network_init
from thirtybirds_2_0.Updates.manager import init as updates_init

class Network(object):
    def __init__(self, hostname, network_message_handler, network_status_handler):
        self.hostname = hostname
        self.thirtybirds = network_init(
            hostname=hostname,
            role="client",
            discovery_multicastGroup=settings.discovery_multicastGroup,
            discovery_multicastPort=settings.discovery_multicastPort,
            discovery_responsePort=settings.discovery_responsePort,
            pubsub_pubPort=settings.pubsub_pubPort,
            message_callback=network_message_handler,
            status_callback=network_status_handler
        )

########################
## UTILS
########################

class Utils(object):
    def __init__(self, hostname):
        self.hostname = hostname
    def reboot(self):
        os.system("sudo reboot now")

    def remote_update_git(self, oratio, thirtybirds, update, upgrade):
        if oratio:
            subprocess.call(['sudo', 'git', 'pull'], cwd='/home/pi/oratio')
        if thirtybirds:
            subprocess.call(['sudo', 'git', 'pull'], cwd='/home/pi/thirtybirds_2_0')
        return

    def remote_update_scripts(self):
        updates_init("/home/pi/oratio", False, True)
        return

    def get_update_script_version(self):
        (updates, ghStatus, bsStatus) = updates_init("/home/pi/oratio", False, False)
        return updates.read_version_pickle()

    def get_git_timestamp(self):
        return commands.getstatusoutput("cd /home/pi/oratio/; git log -1 --format=%cd")[1]

    def get_temp(self):
        return commands.getstatusoutput("/opt/vc/bin/vcgencmd measure_temp")[1]

    def get_cpu(self):
        bash_output = commands.getstatusoutput("uptime")[1]
        split_output = bash_output.split(" ")
        return split_output[12]

    def get_uptime(self):
        bash_output = commands.getstatusoutput("uptime")[1]
        split_output = bash_output.split(" ")
        return split_output[4]

    def get_disk(self):
        # stub for now
        return "0"

    def get_client_status(self):
        return (self.hostname, self.get_update_script_version(), self.get_git_timestamp(), self.get_temp(), self.get_cpu(), self.get_uptime(), self.get_disk())

# Main handles network send/recv and can see all other classes directly
class Main(threading.Thread):
    def __init__(self, hostname):
        threading.Thread.__init__(self)
        self.network = Network(hostname, self.network_message_handler, self.network_status_handler)
        self.queue = Queue.Queue()
        self.ser=serial.Serial("/dev/ttyACM0",9600)
        self.ser.baudrate=9600
        self.utils = Utils(hostname)
        self.network.thirtybirds.subscribe_to_topic("mandala_device_status")
        self.UNSET = 0
        self.FAIL = 2000
        self.PASS = 4000
        self.mandala_device_status = None
        self.mandala_tlc_ids = {
            "avl-controller":39,
            "avl-formant-1":15,
            "avl-formant-1-amplifier":4,
            "avl-formant-2":16,
            "avl-formant-2-amplifier":5,
            "avl-formant-3":17,
            "avl-formant-3-amplifier":6,
            "avl-layer-1":40,
            "avl-layer-2":11,
            "avl-layer-3":12,
            "avl-medulla":35,
            "avl-pitch-keys":18,
            "avl-pitch-keys-sensor-1":7,
            "avl-pitch-keys-sensor-2":8,
            "avl-pitch-keys-sensor-3":9,
            "avl-pitch-keys-sensor-4":10,
            "avl-settings":34,
            "avl-settings-adcs":24,
            "avl-transport":13,
            "avl-transport-encoder":0,
            "avl-voice-1":36,
            "avl-voice-1-crystal-frequency-counter":25,
            "avl-voice-1-harmonic-generators":26,
            "avl-voice-1-harmonic-volume":27,
            "avl-voice-2":37,
            "avl-voice-2-crystal-frequency-counter":28,
            "avl-voice-2-harmonic-generators":29,
            "avl-voice-2-harmonic-volume":30,
            "avl-voice-3":38,
            "avl-voice-3-crystal-frequency-counter":31,
            "avl-voice-3-harmonic-generators":32,
            "avl-voice-3-harmonic-volume":33,
            "avl-voice-keys":14,
            "avl-voice-keys-encoder-1":1,
            "avl-voice-keys-encoder-2":2,
            "avl-voice-keys-encoder-3":3
        }

    def network_message_handler(self, topic_msg):
        # this method runs in the thread of the caller, not the tread of Main
        topic, msg =  topic_msg # separating just to eval msg.  best to do it early.  it should be done in TB.
        #if len(msg) > 0:
        #    msg = eval(msg)
        self.add_to_queue(topic, msg)

    def network_status_handler(self, topic_msg):
        # this method runs in the thread of the caller, not the tread of Main
        print "Main.network_status_handler", topic_msg

    def add_to_queue(self, topic, msg):
        self.queue.put((topic, msg))

    def run(self):
        devicenames = self.mandala_device_status.keys()
        for devicename in devicenames:
            tlc_id_int = self.mandala_tlc_ids[devicename] + 5000
            tlc_id_str = "{}\n".format(tlc_id_int)
            self.ser.write(tlc_id_str)
            self.ser.write("0/n")

        while True:
            if self.mandala_device_status == None:
                time.sleep(1)
                self.network.thirtybirds.send("mandala_device_status_request", True)
            try:
                topic, msg = self.queue.get(True)
                if topic == "mandala_device_status":
                    self.mandala_device_status = eval(msg)
                    devicenames = self.mandala_device_status.keys()
                    devicenames.sort()
                    for devicename in devicenames:
                        tlc_id_int = self.mandala_tlc_ids[devicename] + 5000
                        tlc_id_str = "{}\n".format(tlc_id_int)
                        if self.mandala_device_status[devicename] == "unset":
                            tlc_level_int = 0
                        if self.mandala_device_status[devicename] == "fail":
                            tlc_level_int = 100
                        if self.mandala_device_status[devicename] == "pass":
                            tlc_level_int = 4000
                        tlc_level_str = "{}\n".format(tlc_level_int)
                        print tlc_id_int, tlc_level_int, devicename
                        self.ser.write(tlc_id_str)
                        self.ser.write(tlc_level_str)

                    print ""
                    print ""
                    #self.mandala_tlc_ids
                    #print topic, msg
                time.sleep(0.01)
            except Exception as e:
                exc_type, exc_value, exc_traceback = sys.exc_info()
                print e, repr(traceback.format_exception(exc_type, exc_value,exc_traceback))

def init(hostname):
    main = Main(hostname)
    main.daemon = True
    main.start()
    return main

<|endoftext|>