<commit_before>from __future__ import print_function

# python imports
import datetime
import logging
import os
import random
import shutil

# vizard imports
import viz
import vizproximity
import vizshape
import viztask

# local imports
import vrlab
import suit
import targets

class Trial(vrlab.Trial):
    '''Manage a single trial of the target-reaching experiment.'''

    def __init__(self, block, targets):
        super(Trial, self).__init__()

        self.block = block
        self.home = targets[0]
        self.targets = targets[1:]

        self.current_target = self.previous_target = self.home

        self.suit = block.experiment.suit
        self.records = []
        self.add_periodic(1. / 100, self.record_data)

    def record_data(self):
        self.records.append((
            viz.tick() - self.start_tick,
            self.previous_target,
            self.current_target,
            self.suit.get_markers(),
        ))

    def wait_for_touch(self, target):
        target.activate(self.block.experiment.prox)
        yield target.signal.wait()

    def target_sequence(self):
        raise NotImplementedError

    def setup(self):
        vrlab.sounds.cowbell.play()
        yield self.wait_for_touch(self.home)
        self.start_tick = viz.tick()

    def run(self):
        for target in self.target_sequence():
            self.previous_target = self.current_target
            self.current_target = target
            yield self.wait_for_touch(target)
            target.sphere.color(viz.WHITE)

    def teardown(self):
        vrlab.sounds.cowbell.play()
        self.write_records()

    def trial_description(self):
        return self.TRIAL_TYPE

    def write_records(self):
        stamp = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
        output = os.path.join(
            self.block.output,
            '{}-{}.csv'.format(stamp, self.trial_description()))

        # open file and define helper to write data
        handle = open(output, 'w')
        def w(msg, *args, **kwargs):
            handle.write(msg.format(*args, **kwargs))

        # write csv file header
        w('time,')
        w('source,source-x,source-y,source-z,')
        w('target,target-x,target-y,target-z,')
        w('effector,effector-x,effector-y,effector-z,effector-c')
        for label in suit.MARKER_LABELS:
            w(',{0}-x,{0}-y,{0}-z,{0}-c'.format(label))
        w('\n')

        # write data frames
        eff = self.block.effector
        for elapsed, prev, curr, frame in self.records:
            w('{}', elapsed)
            w(',{t.index},{t.center[0]},{t.center[1]},{t.center[2]}', t=prev)
            w(',{t.index},{t.center[0]},{t.center[1]},{t.center[2]}', t=curr)
            w(',{i},{m.pos[0]},{m.pos[1]},{m.pos[2]},{m.cond}', i=eff, m=frame[eff])
            for i in range(len(frame)):
                w(',{m.pos[0]},{m.pos[1]},{m.pos[2]},{m.cond}', m=frame[i])
            w('\n')

        # finish up
        handle.close()
        logging.info('wrote %d records to trial output %s',
                     len(self.records), os.path.basename(output))

class HubAndSpokeTrial(Trial):
    TRIAL_TYPE = 'hubspoke'

    def target_sequence(self):
        for target in self.targets:
            yield target
            yield self.home

class CircuitTrial(Trial):
    TRIAL_TYPE = 'circuit'

    def target_sequence(self):
        for target in self.targets:
            yield target

class ControlTrial(HubAndSpokeTrial):
    TRIAL_TYPE = 'control'
    TARGETS = [targets.NUMBERED[i] for i in (7, 8, 3)]

    def __init__(self, block, targets, home=targets.NUMBERED[6]):
        random.shuffle(self.TARGETS)
        assert home.index in (6, 10, 9)
        super(ControlTrial, self).__init__(block, [home] + self.TARGETS)

class Block(vrlab.Block):
    '''Manage a block of trials in the tracing experiment.

    This class handles block setup (playing a sound, making a directory for
    recording data) and generates trials in the block by choosing sequences of
    targets.
    '''

    index = 0

    def __init__(self,
                 experiment,
                 effector,
                 trial_factory,
                 num_trials,
                 ):
        super(Block, self).__init__()

        self.experiment = experiment
        self.effector = effector
        self.trial_factory = trial_factory
        self.num_trials = num_trials

        self.output = os.path.join(
            experiment.output, 'block{}-effector{}'.format(
                Block.index, self.effector))

        logging.info('NEW BLOCK -- tracer %s, trials %s',
                     self.effector, self.trial_factory.__name__)

        Block.index += 1

    def setup(self):
        self.experiment.prox.addTarget(self.experiment.leds[self.effector])

        if not os.path.isdir(self.output):
            os.makedirs(self.output)

        vrlab.sounds.gong.play()

        yield viztask.waitKeyDown(' ')

    def teardown(self):
        self.experiment.prox.clearTargets()

    def generate_trials(self):
        for ts in targets.CIRCUITS[:self.num_trials]:
            yield self.trial_factory(self, [targets.NUMBERED[t] for t in ts])

class Experiment(vrlab.Experiment):
    '''Manage a series of blocks in the reaching experiment.

    This class handles global experiment setup for a single subject. To set up,
    we turn on the motion-capture thread, create some experiment-relevant Vizard
    objects for representing the mocap leds and targets, and create a virtual
    environment for visualization.

    We ultimately generate a series of experiment blocks by using the
    parameters set at the top of the module.
    '''

    BASE = 'C:\\Documents and Settings\\vrlab\\Desktop\\target-data'

    def setup(self):
        # set up a folder to store data for a subject.
        stamp = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
        self.output = '{}\\{}-{:08x}'.format(
            self.BASE, stamp, random.randint(0, 0xffffff))
        logging.info('storing output in %s', self.output)

        # configure the phasespace.
        mocap = vrlab.Phasespace('192.168.1.230', freq=120)
        self.suit = mocap.track_points(range(len(suit.MARKER_LABELS)))
        self.leds = []
        for i in range(50):
            sphere = vizshape.addSphere(0.02, color=viz.RED)
            self.suit.link_marker(i, sphere)
            self.leds.append(sphere)
        mocap.start_thread()

        # set up a proximity manager to detect touch events.
        self.prox = vizproximity.Manager()
        #self.prox.setDebug(viz.ON)

        # add an environment and navigation to help with visualization.
        self.environment = viz.addChild('dojo.osgb')
        viz.cam.setHandler(None)

    def generate_blocks(self):
        #yield Block(self, effector=suit.MARKERS.R_FING_INDEX, trial_factory=ControlTrial, num_trials=10)
        #yield Block(self, effector=suit.MARKERS.R_FING_INDEX, trial_factory=HubAndSpokeTrial, num_trials=5)
        yield Block(self, effector=suit.MARKERS.R_FING_INDEX, trial_factory=CircuitTrial, num_trials=6)
        yield Block(self, effector=suit.MARKERS.R_FING_INDEX, trial_factory=CircuitTrial, num_trials=6)
        yield Block(self, effector=suit.MARKERS.R_FING_INDEX, trial_factory=CircuitTrial, num_trials=6)
        yield Block(self, effector=suit.MARKERS.R_FING_INDEX, trial_factory=CircuitTrial, num_trials=6)
        yield Block(self, effector=suit.MARKERS.R_FING_INDEX, trial_factory=CircuitTrial, num_trials=6)

if __name__ == '__main__':
    logging.basicConfig(
            stream=sys.stdout,
            level=logging.DEBUG,
            format='%(levelname).1s %(asctime)s %(name)s:%(lineno)d %(message)s',
        )
    Experiment().main(fullscreen=False)
<commit_msg>After a block, gzip files containing trial data.<commit_after>from __future__ import print_function

# python imports
import datetime
import gzip
import logging
import os
import random
import shutil

# vizard imports
import viz
import vizproximity
import vizshape
import viztask

# local imports
import vrlab
import suit
import targets

class Trial(vrlab.Trial):
    '''Manage a single trial of the target-reaching experiment.'''

    def __init__(self, block, targets):
        super(Trial, self).__init__()

        self.block = block
        self.home = targets[0]
        self.targets = targets[1:]

        self.current_target = self.previous_target = self.home

        self.suit = block.experiment.suit
        self.records = []
        self.add_periodic(1. / 100, self.record_data)

    def record_data(self):
        self.records.append((
            viz.tick() - self.start_tick,
            self.previous_target,
            self.current_target,
            self.suit.get_markers(),
        ))

    def wait_for_touch(self, target):
        target.activate(self.block.experiment.prox)
        yield target.signal.wait()

    def target_sequence(self):
        raise NotImplementedError

    def setup(self):
        vrlab.sounds.cowbell.play()
        yield self.wait_for_touch(self.home)
        self.start_tick = viz.tick()

    def run(self):
        for target in self.target_sequence():
            self.previous_target = self.current_target
            self.current_target = target
            yield self.wait_for_touch(target)
            target.sphere.color(viz.WHITE)

    def teardown(self):
        vrlab.sounds.cowbell.play()
        self.write_records()

    def trial_description(self):
        return self.TRIAL_TYPE

    def write_records(self):
        stamp = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
        output = os.path.join(
            self.block.output,
            '{}-{}.csv'.format(stamp, self.trial_description()))

        # open file and define helper to write data
        handle = open(output, 'w')
        def w(msg, *args, **kwargs):
            handle.write(msg.format(*args, **kwargs))

        # write csv file header
        w('time,')
        w('source,source-x,source-y,source-z,')
        w('target,target-x,target-y,target-z,')
        w('effector,effector-x,effector-y,effector-z,effector-c')
        for label in suit.MARKER_LABELS:
            w(',{0}-x,{0}-y,{0}-z,{0}-c'.format(label))
        w('\n')

        # write data frames
        eff = self.block.effector
        for elapsed, prev, curr, frame in self.records:
            w('{}', elapsed)
            w(',{t.index},{t.center[0]},{t.center[1]},{t.center[2]}', t=prev)
            w(',{t.index},{t.center[0]},{t.center[1]},{t.center[2]}', t=curr)
            w(',{i},{m.pos[0]},{m.pos[1]},{m.pos[2]},{m.cond}', i=eff, m=frame[eff])
            for i in range(len(frame)):
                w(',{m.pos[0]},{m.pos[1]},{m.pos[2]},{m.cond}', m=frame[i])
            w('\n')

        # finish up
        handle.close()
        logging.info('wrote %d records to trial output %s',
                     len(self.records), os.path.basename(output))

class HubAndSpokeTrial(Trial):
    TRIAL_TYPE = 'hubspoke'

    def target_sequence(self):
        for target in self.targets:
            yield target
            yield self.home

class CircuitTrial(Trial):
    TRIAL_TYPE = 'circuit'

    def target_sequence(self):
        for target in self.targets:
            yield target

class ControlTrial(HubAndSpokeTrial):
    TRIAL_TYPE = 'control'
    TARGETS = [targets.NUMBERED[i] for i in (7, 8, 3)]

    def __init__(self, block, targets, home=targets.NUMBERED[6]):
        random.shuffle(self.TARGETS)
        assert home.index in (6, 10, 9)
        super(ControlTrial, self).__init__(block, [home] + self.TARGETS)

class Block(vrlab.Block):
    '''Manage a block of trials in the tracing experiment.

    This class handles block setup (playing a sound, making a directory for
    recording data) and generates trials in the block by choosing sequences of
    targets.
    '''

    index = 0

    def __init__(self,
                 experiment,
                 effector,
                 trial_factory,
                 num_trials,
                 ):
        super(Block, self).__init__()

        self.experiment = experiment
        self.effector = effector
        self.trial_factory = trial_factory
        self.num_trials = num_trials

        self.output = os.path.join(
            experiment.output, 'block{}-effector{}'.format(
                Block.index, self.effector))

        logging.info('NEW BLOCK -- tracer %s, trials %s',
                     self.effector, self.trial_factory.__name__)

        Block.index += 1

    def setup(self):
        self.experiment.prox.addTarget(self.experiment.leds[self.effector])

        if not os.path.isdir(self.output):
            os.makedirs(self.output)

        vrlab.sounds.gong.play()

        yield viztask.waitKeyDown(' ')

    def teardown(self):
        self.experiment.prox.clearTargets()

        # gzip recorded trial data for this block
        for f in os.listdir(self.output):
            if f.endswith('.csv'):
                fn = os.path.join(self.output, f)
                gz = fn + '.gz'

                source = ''
                with open(fn) as src:
                    source = src.read()

                tgt = gzip.open(gz, 'wb')
                tgt.write(source)
                tgt.close()

                zipped = ''
                with open(gz, 'rb') as tgt:
                    zipped = tgt.read()

                tgt = gzip.open(gz)
                verify = tgt.read()
                tgt.close()

                logging.info('gzipped %s (%d kbytes) as %s (%d kbytes)',
                             fn, len(source) / 1000, gz, len(zipped))

                if source == verify:
                    os.unlink(fn)

    def generate_trials(self):
        for ts in targets.CIRCUITS[:self.num_trials]:
            yield self.trial_factory(self, [targets.NUMBERED[t] for t in ts])

class Experiment(vrlab.Experiment):
    '''Manage a series of blocks in the reaching experiment.

    This class handles global experiment setup for a single subject. To set up,
    we turn on the motion-capture thread, create some experiment-relevant Vizard
    objects for representing the mocap leds and targets, and create a virtual
    environment for visualization.

    We ultimately generate a series of experiment blocks by using the
    parameters set at the top of the module.
    '''

    BASE = 'C:\\Documents and Settings\\vrlab\\Desktop\\target-data'

    def setup(self):
        # set up a folder to store data for a subject.
        stamp = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
        self.output = '{}\\{}-{:08x}'.format(
            self.BASE, stamp, random.randint(0, 0xffffff))
        logging.info('storing output in %s', self.output)

        # configure the phasespace.
        mocap = vrlab.Phasespace('192.168.1.230', freq=120)
        self.suit = mocap.track_points(range(len(suit.MARKER_LABELS)))
        self.leds = []
        for i in range(50):
            sphere = vizshape.addSphere(0.02, color=viz.RED)
            self.suit.link_marker(i, sphere)
            self.leds.append(sphere)
        mocap.start_thread()

        # set up a proximity manager to detect touch events.
        self.prox = vizproximity.Manager()
        #self.prox.setDebug(viz.ON)

        # add an environment and navigation to help with visualization.
        self.environment = viz.addChild('dojo.osgb')
        viz.cam.setHandler(None)

    def generate_blocks(self):
        #yield Block(self, effector=suit.MARKERS.R_FING_INDEX, trial_factory=ControlTrial, num_trials=10)
        #yield Block(self, effector=suit.MARKERS.R_FING_INDEX, trial_factory=HubAndSpokeTrial, num_trials=5)
        yield Block(self, effector=suit.MARKERS.R_FING_INDEX, trial_factory=CircuitTrial, num_trials=6)
        yield Block(self, effector=suit.MARKERS.R_FING_INDEX, trial_factory=CircuitTrial, num_trials=6)
        yield Block(self, effector=suit.MARKERS.R_FING_INDEX, trial_factory=CircuitTrial, num_trials=6)
        yield Block(self, effector=suit.MARKERS.R_FING_INDEX, trial_factory=CircuitTrial, num_trials=6)
        yield Block(self, effector=suit.MARKERS.R_FING_INDEX, trial_factory=CircuitTrial, num_trials=6)

if __name__ == '__main__':
    logging.basicConfig(
            stream=sys.stdout,
            level=logging.DEBUG,
            format='%(levelname).1s %(asctime)s %(name)s:%(lineno)d %(message)s',
        )
    Experiment().main(fullscreen=False)
<|endoftext|>


<commit_before>from twisted.internet import reactor
from IRCMessage import IRCMessage
from IRCResponse import IRCResponse, ResponseType
from Function import Function
import GlobalVars

class Instantiate(Function):
    Help = "timer <time> - starts a countdown timer and notifies you when time's up."

    def GetResponse(self, HubbeBot, message):
        if message.Type != "PRIVMSG":
            return
        if message.Command == "timer":
            if len(message.ParameterList) != 1:
                return IRCResponse(ResponseType.Say, "Please use only 1 argument.", message.ReplyTo)
            delay = float(message.ParameterList[0])
            reactor.callLater(delay, self.notifyUser, HubbeBot, message)
            return IRCResponse(ResponseType.Say, message.User.Name + ": A timer has been started!", message.ReplyTo)

    def notifyUser(self, HubbeBot, message):
        HubbeBot.sendResponse(IRCResponse(ResponseType.Say, message.User.Name + ": Time is up!", message.ReplyTo))
<commit_msg>More things for more better.<commit_after>from twisted.internet import reactor
from IRCMessage import IRCMessage
from IRCResponse import IRCResponse, ResponseType
from Function import Function
import GlobalVars

class Instantiate(Function):
    Help = "timer <time> - starts a countdown timer and notifies you when time's up."

    def GetResponse(self, HubbeBot, message):
        if message.Type != "PRIVMSG":
            return
        if message.Command == "timer":
            if len(message.ParameterList) != 1:
                return IRCResponse(ResponseType.Say, "Please use only 1 argument.", message.ReplyTo)
            try:
                delay = float(message.ParameterList[0])
            except:
                return IRCResponse(ResponseType.Say, "That doesn't look like a number to me...", message.ReplyTo)
            reactor.callLater(delay, self.notifyUser, HubbeBot, delay, message)
            return IRCResponse(ResponseType.Say, message.User.Name + ": A timer has been started!", message.ReplyTo)

    def notifyUser(self, HubbeBot, delay, message):
        HubbeBot.sendResponse(IRCResponse(ResponseType.Say, message.User.Name + ": Your " + str(int(delay)) + " second timer is up!" , message.ReplyTo))
<|endoftext|>


<commit_before># Copyright (c) 2012, GPy authors (see AUTHORS.txt).
# Licensed under the BSD 3-clause license (see LICENSE.txt)

from kernpart import kernpart
import numpy as np

class linear(kernpart):
    """
    Linear kernel

    .. math::

       k(x,y) = \sum_{i=1}^D \sigma^2_i x_iy_i

    :param D: the number of input dimensions
    :type D: int
    :param variances: the vector of variances :math:`\sigma^2_i`
    :type variances: np.ndarray of size (1,) or (D,) depending on ARD
    :param ARD: Auto Relevance Determination. If equal to "False", the kernel is isotropic (ie. one single variance parameter \sigma^2), otherwise there is one variance parameter per dimension.
    :type ARD: Boolean
    :rtype: kernel object
    """

    def __init__(self,D,variances=None,ARD=True):
        self.D = D
        self.ARD = ARD
        if ARD == False:
            self.Nparam = 1
            self.name = 'linear'
            if variances is not None:
                if isinstance(variances, float):
                    variances = np.array([variances])

                assert variances.shape == (1,)
            else:
                variances = np.ones(1)
            self._Xcache, self._X2cache = np.empty(shape=(2,))
        else:
            self.Nparam = self.D
            self.name = 'linear_ARD'
            if variances is not None:
                assert variances.shape == (self.D,)
            else:
                variances = np.ones(self.D)
        self._set_params(variances)

    def _get_params(self):
        return self.variances

    def _set_params(self,x):
        assert x.size==(self.Nparam)
        self.variances = x
        self.variances2 = np.square(self.variances)

    def _get_param_names(self):
        if self.Nparam == 1:
            return ['variance']
        else:
            return ['variance_%i'%i for i in range(self.variances.size)]

    def K(self,X,X2,target):
        if self.ARD:
            XX = X*np.sqrt(self.variances)
            XX2 = X2*np.sqrt(self.variances)
            target += np.dot(XX, XX2.T)
        else:
            self._K_computations(X, X2)
            target += self.variances * self._dot_product

    def Kdiag(self,X,target):
        np.add(target,np.sum(self.variances*np.square(X),-1),target)

    def dK_dtheta(self,partial,X,X2,target):
        if self.ARD:
            product = X[:,None,:]*X2[None,:,:]
            target += (partial[:,:,None]*product).sum(0).sum(0)
        else:
            self._K_computations(X, X2)
            target += np.sum(self._dot_product*partial)

    def dK_dX(self,partial,X,X2,target):
        target += (((X2[:, None, :] * self.variances)) * partial[:,:, None]).sum(0)

    #---------------------------------------#
    #             PSI statistics            #
    #---------------------------------------#

    def psi0(self,Z,mu,S,target):
        expected = np.square(mu) + S
        target += np.sum(self.variances*expected)

    def dpsi0_dtheta(self,partial,Z,mu,S,target):
        expected = np.square(mu) + S
        target += (partial[:, None] * (np.sum(expected,0))).sum()

    def dpsi0_dmuS(self,partial, Z,mu,S,target_mu,target_S):
        target_mu += partial[:, None] * (2.0*mu*self.variances) * mu.shape[0]
        target_S += partial[:, None] * self.variances * mu.shape[0]

    def dpsi0_dZ(self,Z,mu,S,target):
        pass

    def psi1(self,Z,mu,S,target):
        """the variance, it does nothing"""
        self.K(mu,Z,target)

    def dpsi1_dtheta(self,partial,Z,mu,S,target):
        """the variance, it does nothing"""
        self.dK_dtheta(partial,mu,Z,target)

    def dpsi1_dmuS(self,partial,Z,mu,S,target_mu,target_S):
        """Do nothing for S, it does not affect psi1"""
        target_mu += (partial.T[:,:, None]*(Z*self.variances)).sum(1)

    def dpsi1_dZ(self,partial,Z,mu,S,target):
        self.dK_dX(partial.T,Z,mu,target)

    def psi2(self,Z,mu,S,target):
        """
        returns N,M,M matrix
        """
        mu2_S = np.square(mu)+S# N,Q,
        ZZ = Z[:,None,:]*Z[None,:,:] # M,M,Q
        psi2 = ZZ*np.square(self.variances)*mu2_S[:, None, None, :]
        target += psi2.sum(-1)

    def dpsi2_dtheta(self,partial,Z,mu,S,target):
        mu2_S = np.square(mu)+S# N,Q,
        ZZ = Z[:,None,:]*Z[None,:,:] # M,M,Q
        target += (partial[:,:,:,None]*(2.*ZZ*mu2_S[:,None,None,:]*self.variances)).sum()

    def dpsi2_dmuS(self,partial,Z,mu,S,target_mu,target_S):
        """Think N,M,M,Q """
        ZZ = Z[:,None,:]*Z[None,:,:] # M,M,Q
        tmp = ZZ*np.square(self.variances) # M,M,Q
        target_mu += (partial[:,:,:,None]*tmp*2.*mu[:,None,None,:]).sum(1).sum(1)
        target_S += (partial[:,:,:,None]*tmp).sum(1).sum(1)

    def dpsi2_dZ(self,partial,Z,mu,S,target):
        mu2_S = np.sum(np.square(mu)+S,0)# Q,
        target += (partial[:,:,:,None]* (Z * mu2_S * np.square(self.variances))).sum(0).sum(1)

    #---------------------------------------#
    #            Precomputations            #
    #---------------------------------------#

    def _K_computations(self,X,X2):
        if X2 is None:
            X2 = X
        if not (np.all(X==self._Xcache) and np.all(X2==self._X2cache)):
            self._Xcache = X
            self._X2cache = X2
            self._dot_product = np.dot(X,X2.T)
        else:
            # print "Cache hit!"
            pass # TODO: insert debug message here (logging framework)
<commit_msg>precomputations for linear psi statistics<commit_after># Copyright (c) 2012, GPy authors (see AUTHORS.txt).
# Licensed under the BSD 3-clause license (see LICENSE.txt)

from kernpart import kernpart
import numpy as np

class linear(kernpart):
    """
    Linear kernel

    .. math::

       k(x,y) = \sum_{i=1}^D \sigma^2_i x_iy_i

    :param D: the number of input dimensions
    :type D: int
    :param variances: the vector of variances :math:`\sigma^2_i`
    :type variances: np.ndarray of size (1,) or (D,) depending on ARD
    :param ARD: Auto Relevance Determination. If equal to "False", the kernel is isotropic (ie. one single variance parameter \sigma^2), otherwise there is one variance parameter per dimension.
    :type ARD: Boolean
    :rtype: kernel object
    """

    def __init__(self,D,variances=None,ARD=True):
        self.D = D
        self.ARD = ARD
        if ARD == False:
            self.Nparam = 1
            self.name = 'linear'
            if variances is not None:
                if isinstance(variances, float):
                    variances = np.array([variances])

                assert variances.shape == (1,)
            else:
                variances = np.ones(1)
            self._Xcache, self._X2cache = np.empty(shape=(2,))
        else:
            self.Nparam = self.D
            self.name = 'linear_ARD'
            if variances is not None:
                assert variances.shape == (self.D,)
            else:
                variances = np.ones(self.D)
        self._set_params(variances)

        #initialize cache
        self._Z, self._mu, self._S = np.empty(shape=(3,1))
        self._X, self._X2, self._params = np.empty(shape=(3,1))

    def _get_params(self):
        return self.variances

    def _set_params(self,x):
        assert x.size==(self.Nparam)
        self.variances = x
        self.variances2 = np.square(self.variances)

    def _get_param_names(self):
        if self.Nparam == 1:
            return ['variance']
        else:
            return ['variance_%i'%i for i in range(self.variances.size)]

    def K(self,X,X2,target):
        if self.ARD:
            XX = X*np.sqrt(self.variances)
            XX2 = X2*np.sqrt(self.variances)
            target += np.dot(XX, XX2.T)
        else:
            self._K_computations(X, X2)
            target += self.variances * self._dot_product

    def Kdiag(self,X,target):
        np.add(target,np.sum(self.variances*np.square(X),-1),target)

    def dK_dtheta(self,partial,X,X2,target):
        if self.ARD:
            product = X[:,None,:]*X2[None,:,:]
            target += (partial[:,:,None]*product).sum(0).sum(0)
        else:
            self._K_computations(X, X2)
            target += np.sum(self._dot_product*partial)

    def dK_dX(self,partial,X,X2,target):
        target += (((X2[:, None, :] * self.variances)) * partial[:,:, None]).sum(0)

    #---------------------------------------#
    #             PSI statistics            #
    #---------------------------------------#

    def psi0(self,Z,mu,S,target):
        self._psi_computations(Z,mu,S)
        target += np.sum(self.variances*self.mu2_S)

    def dpsi0_dtheta(self,partial,Z,mu,S,target):
        self._psi_computations(Z,mu,S)
        target += (partial[:, None] * (np.sum(self.mu2_S,0))).sum()

    def dpsi0_dmuS(self,partial, Z,mu,S,target_mu,target_S):
        target_mu += partial[:, None] * (2.0*mu*self.variances) * mu.shape[0]
        target_S += partial[:, None] * self.variances * mu.shape[0]

    def dpsi0_dZ(self,Z,mu,S,target):
        pass

    def psi1(self,Z,mu,S,target):
        """the variance, it does nothing"""
        self.K(mu,Z,target)

    def dpsi1_dtheta(self,partial,Z,mu,S,target):
        """the variance, it does nothing"""
        self.dK_dtheta(partial,mu,Z,target)

    def dpsi1_dmuS(self,partial,Z,mu,S,target_mu,target_S):
        """Do nothing for S, it does not affect psi1"""
        self._psi_computations(Z,mu,S)
        target_mu += (partial.T[:,:, None]*(Z*self.variances)).sum(1)

    def dpsi1_dZ(self,partial,Z,mu,S,target):
        self.dK_dX(partial.T,Z,mu,target)

    def psi2(self,Z,mu,S,target):
        """
        returns N,M,M matrix
        """
        self._psi_computations(Z,mu,S)
        psi2 = self.ZZ*np.square(self.variances)*self.mu2_S[:, None, None, :]
        target += psi2.sum(-1)

    def dpsi2_dtheta(self,partial,Z,mu,S,target):
        self._psi_computations(Z,mu,S)
        target += (partial[:,:,:,None]*(2.*self.ZZ*self.mu2_S[:,None,None,:]*self.variances)).sum()

    def dpsi2_dmuS(self,partial,Z,mu,S,target_mu,target_S):
        """Think N,M,M,Q """
        self._psi_computations(Z,mu,S)
        tmp = self.ZZ*np.square(self.variances) # M,M,Q
        target_mu += (partial[:,:,:,None]*tmp*2.*mu[:,None,None,:]).sum(1).sum(1)
        target_S += (partial[:,:,:,None]*tmp).sum(1).sum(1)

    def dpsi2_dZ(self,partial,Z,mu,S,target):
        self._psi_computations(Z,mu,S)
        mu2_S = np.sum(self.mu2_S,0)# Q,
        target += (partial[:,:,:,None]* (Z * mu2_S * np.square(self.variances))).sum(0).sum(1)

    #---------------------------------------#
    #            Precomputations            #
    #---------------------------------------#

    def _K_computations(self,X,X2):
        if X2 is None:
            X2 = X
        if not (np.all(X==self._Xcache) and np.all(X2==self._X2cache)):
            self._Xcache = X
            self._X2cache = X2
            self._dot_product = np.dot(X,X2.T)
        else:
            # print "Cache hit!"
            pass # TODO: insert debug message here (logging framework)

    def _psi_computations(self,Z,mu,S):
        #here are the "statistics" for psi1 and psi2
        if not np.all(Z==self._Z):
            #Z has changed, compute Z specific stuff
            self.ZZ = Z[:,None,:]*Z[None,:,:] # M,M,Q
            self._Z = Z
        if not (np.all(Z==self._Z) and np.all(mu==self._mu) and np.all(S==self._S)):
            self.mu2_S = np.square(mu)+S
            self._Z, self._mu, self._S = Z, mu,S
<|endoftext|>


<commit_before>from vesper.tests.test_case import TestCase
import vesper.util.yaml_utils as yaml_utils

class YamlUtilsTests(TestCase):

    def test_dump_and_load(self):
        x = {'x': 1, 'y': [1, 2, 3], 'z': {'one': 1}}
        s = yaml_utils.dump(x)
        y = yaml_utils.load(s)
        self.assertEqual(x, y)

    def test_dump_and_load_with_non_default_flow_style(self):
        x = {'x': 1, 'y': [1, 2, 3], 'z': {'one': 1}}
        s = yaml_utils.dump(x, default_flow_style=False)
        y = yaml_utils.load(s)
        self.assertEqual(x, y)

    def test_sexagesimal_load(self):

        """
        The PyYAML `load` function parses YAML 1.1, in which "12:34:56"
        is the sexagesimal number 12 * 3600 + 34 * 60 + 56 = 45296. We
        use `ruaml_yaml` rather than PyYAML because it can also parse
        YAML 1.2, in which "12:34:56" is simply the string "12:34:56".
        This test checks that `yaml_utils.load` parses its input as we
        would like.
        """

        x = yaml_utils.load('12:34:56')
        self.assertEqual(x, '12:34:56')
<commit_msg>Added YAML utils unit test that illustrates NumPy scalar dumping issue.<commit_after>from vesper.tests.test_case import TestCase
import vesper.util.yaml_utils as yaml_utils

class YamlUtilsTests(TestCase):

    def test_dump_and_load(self):
        x = {'x': 1, 'y': [1, 2, 3], 'z': {'one': 1}}
        s = yaml_utils.dump(x)
        y = yaml_utils.load(s)
        self.assertEqual(x, y)

    def test_dump_and_load_with_non_default_flow_style(self):
        x = {'x': 1, 'y': [1, 2, 3], 'z': {'one': 1}}
        s = yaml_utils.dump(x, default_flow_style=False)
        y = yaml_utils.load(s)
        self.assertEqual(x, y)

    def test_sexagesimal_load(self):

        """
        The PyYAML `load` function parses YAML 1.1, in which "12:34:56"
        is the sexagesimal number 12 * 3600 + 34 * 60 + 56 = 45296. We
        use `ruaml_yaml` rather than PyYAML because it can also parse
        YAML 1.2, in which "12:34:56" is simply the string "12:34:56".
        This test checks that `yaml_utils.load` parses its input as we
        would like.
        """

        x = yaml_utils.load('12:34:56')
        self.assertEqual(x, '12:34:56')

#     def test_numpy_scalar_dump(self):
#
#         """
#         This test shows that you can't dump a NumPy scalar, since the
#         dumper doesn't know how to represent its type. Perhaps we could
#         tell it how to, or perhaps that would be more trouble than it's
#         worth.
#         """
#
#         import numpy as np
#         x = np.arange(3)
#         s = yaml_utils.dump(x[1])
#         self.assertEqual(s, '1')
<|endoftext|>


<commit_before>"""
By using execfile(this_file, dict(__file__=this_file)) you will
activate this virtualenv environment.

This can be used when you must use an existing Python interpreter, not
the virtualenv bin/python
"""

try:
    __file__
except NameError:
    raise RuntimeError(
        "You must run this like execfile('path/to/activate_this.py', "
        "dict(__file__='path/to/activate_this.py'))"
    )

import sys
import os

old_os_path = os.environ.get('PATH', '')
os.environ['PATH'] = (
    os.path.dirname(os.path.abspath(__file__)) + os.pathsep + old_os_path
)
base = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

if sys.platform == 'win32':
    site_packages = os.path.join(base, 'Lib', 'site-packages')
else:
    site_packages = os.path.join(
        base, 'lib', 'python%s' % sys.version[:3], 'site-packages'
    )

prev_sys_path = list(sys.path)

import site

site.addsitedir(site_packages)
sys.real_prefix = sys.prefix
sys.prefix = base
# Move the added items to the front of the path:
new_sys_path = []
for item in list(sys.path):
    if item not in prev_sys_path:
        new_sys_path.append(item)
        sys.path.remove(item)
sys.path[:0] = new_sys_path
<commit_msg>Add a `flake8: noqa`.<commit_after>"""
By using execfile(this_file, dict(__file__=this_file)) you will
activate this virtualenv environment.

This can be used when you must use an existing Python interpreter, not
the virtualenv bin/python
"""

try:
    __file__
except NameError:
    raise RuntimeError(
        "You must run this like execfile('path/to/activate_this.py', "
        "dict(__file__='path/to/activate_this.py'))"
    )

import sys
import os

old_os_path = os.environ.get('PATH', '')
os.environ['PATH'] = (
    os.path.dirname(os.path.abspath(__file__)) + os.pathsep + old_os_path
)
base = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

if sys.platform == 'win32':
    site_packages = os.path.join(base, 'Lib', 'site-packages')
else:
    site_packages = os.path.join(
        base, 'lib', 'python%s' % sys.version[:3], 'site-packages'
    )

prev_sys_path = list(sys.path)

import site  # flake8: noqa

site.addsitedir(site_packages)
sys.real_prefix = sys.prefix
sys.prefix = base
# Move the added items to the front of the path:
new_sys_path = []
for item in list(sys.path):
    if item not in prev_sys_path:
        new_sys_path.append(item)
        sys.path.remove(item)
sys.path[:0] = new_sys_path
<|endoftext|>


<commit_before># -*- test-case-name: vumi.transports.intergrat.tests.test_failures -*-

from vumi.transports.failures import FailureWorker

class IntegratFailureWorker(FailureWorker):

    def do_retry(self, message, reason):
        message = self.update_retry_metadata(message)
        self.store_failure(message, reason, message['retry_metadata']['delay'])

    def handle_failure(self, message, reason):
        if reason == "connection refused":
            self.do_retry(message, reason)
        else:
            self.store_failure(message, reason)
<commit_msg>Fix link to failure tests.<commit_after># -*- test-case-name: vumi.transports.integrat.tests.test_failures -*-

from vumi.transports.failures import FailureWorker

class IntegratFailureWorker(FailureWorker):

    def do_retry(self, message, reason):
        message = self.update_retry_metadata(message)
        self.store_failure(message, reason, message['retry_metadata']['delay'])

    def handle_failure(self, message, reason):
        if reason == "connection refused":
            self.do_retry(message, reason)
        else:
            self.store_failure(message, reason)
<|endoftext|>


<commit_before>"""
Copyright (c) 2012 Anant Bhardwaj

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""

import os, sys, re, logging

if __name__ == "__main__":
	p = os.path.abspath(os.path.dirname(__file__))
	if(os.path.abspath(p+"/..") not in sys.path):
		sys.path.append(os.path.abspath(p+"/.."))
	os.environ.setdefault("DJANGO_SETTINGS_MODULE", "http_handler.settings")

from model_controller import *
from models import *
from transport.voicex import VoiceXTransport
from transport import config
#import voicex.tasks

logger = logging.getLogger(__name__)

'''
Main Handler Interface

@author: Anant Bhardwaj, Trisha Kothari
@date: Aug 3, 2012
'''

class VoiceX:
	def __init__(self, auth):
		logger.debug('__init__')
		self.mc = ModelController()
		self.v = VoiceXTransport(auth = auth)

	# only for testing (polling mode)
	def init_callback(self):
		logger.debug('init_callback')
		self.v.set_callback(callback = self.msg_new)

	def show_help(self, msg, phone_num):
		logger.debug('show_help')
		help_text = "Welcome to VoiceX! Commands: register name, unregister name, post msg, view post-id, delete post-id, search query, reply post-id msg, follow @name/#tag, unfollow @name/#tag"
		if(not msg):
			pass
		elif('register' in msg):
			help_text = "Help for register : register name. Example: register voicex"
		elif('unregister' in msg):
			help_text = "Help for unregister : unregister name. Example: unregister voicex"
		elif('post' in msg):
			help_text = "Help for post : post message. Example (simple post): post hello world. Example (tagged post): post voicex is free #voicex. Example (anonymous post): post voicex is free #anon"
		elif('search' in msg):
			help_text = "Help for search : search search-keywords. Example: search kenya election"
		elif('view' in msg):
			help_text = "Help for view : view post-id. Example: view 1"
		elif('delete' in msg):
			help_text = "Help for delete : delete post-id. Example: delete 1"
		elif('reply' in msg):
			help_text = "Help for reply : reply post-id reply-msg. Example (private reply): reply 1 welcome to voicex. Example (public reply): reply 1 welcome to voicex #public. Example (anonymous reply): reply 1 welcome to voicex #anon"
		elif('follow' in msg):
			help_text = "Help for follow : follow @name/#tag. Example: follow @voicex or follow #election"
		elif('unfollow' in msg):
			help_text = "Help for unfollow : unfollow @name/#tag. Example: unfollow @voicex or unfollow #election."
		else:
			pass
		self.v.sms(phone_num, help_text)

	def register(self, name, phone_num):
		logger.debug('register')
		res = self.mc.add_account(name , phone_num)
		if(res['status']):
			self.v.sms(phone_num, 'Successfully registered %s with %s.' %(name, phone_num))
		else:
			self.v.sms(phone_num, res['code'])

	def unregister(self, name, phone_num):
		logger.debug('unregister')
		res = self.mc.delete_account(name , phone_num)
		if(res['status']):
			self.v.sms(phone_num, 'Unregistered %s' %(name))
		else:
			self.v.sms(phone_num, res['code'])

	def post(self, text, phone_num):
		logger.debug('post')
		res = self.mc.insert_post(phone_num, text);
		if(res['status']):
			post_id = res['val']
			self.v.sms(phone_num, 'Msg successfully posted. To view the post, text -- view %s' %(str(post_id)))
			self.notify_followers(phone_num, text, post_id)
		else:
			self.v.sms(phone_num, res['code'])

	def notify_followers(self, phone_num, msg, post_id):
		logger.debug('notify_followers')
		tags = [tag.strip().lower() for tag in msg.split() if tag.startswith("#")]
		res_find = self.mc.find_account(phone_num)
		account = 'anonymous'
		if(res_find['status'] and ('#anon' not in tags)):
			account = res_find['val'].name
			tags.append('@'+account)

		res_following = self.mc.find_following(tags)
		text = "From: @%s (Post ID: %s): %s" %(account, post_id, msg)
		if(res_following['status']):
			follow_list = res_following['val']
			if(len(follow_list) == 0 ):
				return
			recipients = ','.join(follow_list)
			self.v.sms(recipients, text)
		else:
			self.v.sms(phone_num, res['code'])

	def delete(self, post_id, phone_num):
		logger.debug('delete')
		res = self.mc.delete_post(post_id)
		if(res['status']):
			self.v.sms(phone_num, "Post ID:(%s) has been successfully deleted!" %(post_id))
		else:
			self.v.sms(phone_num, res['code'])

	def view(self, post_id, phone_num):
		logger.debug('view')
		res = self.mc.find_post(post_id)
		if(res['status']):
			post = res['val']
			out = post.post
			posts = Post.objects.filter(reply_to = post, public = True)
			for p in posts:
				out =  out + "[Comment (ID:%s): %s]" %(str(p.id), p.post)
			self.v.sms(phone_num, out)
		else:
			self.v.sms(phone_num, res['code'])

	def search(self, query, phone_num):
		logger.debug('search')
		res = self.mc.search_posts(query)
		if(res['status']):
			self.v.sms(phone_num, res['val'])
		else:
			self.v.sms(phone_num, res['code'])

	def reply(self, msg_data, phone_num):
		logger.debug('reply')
		tokens = msg_data.strip().split(" ", 1)
		tokens = filter(lambda x: x!='', map(lambda x: x.strip(), tokens))
		post_id = None
		reply_text = None
		try:
			post_id = str(int(tokens[0]))
			reply_text = tokens[1]
		except:
			self.show_help('reply', phone_num)
			return
		account = 'anonymous'
		tags = [tag.strip().lower() for tag in reply_text.split() if tag.startswith("#")]
		res_find_acc = self.mc.find_account(phone_num)
		if(res_find_acc['status'] and ('#anon' not in tags)):
			account = res_find_acc['val'].name
		reply_text = tokens[1]
		res_find = self.mc.find_post(post_id)
		if(res_find['status']):
			post = res_find['val']
			res_insert = {'status': False}
			if('#public' in tags):
				res_insert = self.mc.insert_reply(phone_num, reply_text, post, public=True)
			else:
				res_insert = self.mc.insert_reply(phone_num, reply_text, post)
			if(res_insert['status']):
				reply_id = str(res_insert['val'])
				self.v.sms(phone_num, "Your reply to post (ID:%s) has been successfully submitted!" %(post_id))
				reply_to  = post.phone
				self.v.sms(reply_to, "[Reply(ID:%s, From: @%s) to Post(ID:%s)] - %s" %(str(reply_id), account, str(post_id), reply_text))
			else:
				self.v.sms(phone_num, res_insert['code'])
		else:
			self.v.sms(phone_num, res_find['code'])

	def follow(self, name, phone_num):
		logger.debug('follow')
		res = self.mc.add_following(name.strip().lower(), phone_num)
		if(res['status']):
			self.v.sms(phone_num, 'You are now following %s.' %(name))
		else:
			self.v.sms(phone_num, res['code'])

	def unfollow(self, name, phone_num):
		logger.debug('unfollow')
		res = self.mc.delete_following(name.strip().lower(), phone_num)
		if(res['status']):
			self.v.sms(phone_num, 'You are now not following %s.' %(name))
		else:
			self.v.sms(phone_num, res['code'])

	def parse(self, msg, phone_num):
		logger.debug('parse')
		try:
			msg_data = (msg.strip()).split(" ", 1)
			msg_data = map(lambda x: x.strip(), msg_data)
			cmd = msg_data[0].lower()
			text = None
			try:
				text = msg_data[1]
			except:
				pass

			if(not text):
				self.show_help(msg_data[0], phone_num)
				return

			if (cmd == "register"):
				self.register(text, phone_num)
			elif(cmd == "unregister"):
				self.unregister(text, phone_num)
			elif(cmd == "post"):
				self.post(text, phone_num)
			elif(cmd == "view"):
				self.view(text, phone_num)
			elif(cmd ==  "search"):
				self.search(text, phone_num)
			elif(cmd == "delete"):
				self.delete(text, phone_num)
			elif(cmd == "reply"):
				self.reply(text, phone_num)
			elif(cmd == "follow"):
				self.follow(text, phone_num)
			elif(cmd == "unfollow"):
				self.unfollow(text, phone_num)
			elif(cmd == "help"):
				try:
					self.show_help(msg_data[1], phone_num)
				except:
					self.show_help(None, phone_num)
			else:
				self.v.sms(phone_num, "Unknown command: '%s'. Text 'help' to get the list of commands." %s(cmd))
		except Exception, e:
			logger.exception('parse')

	def handle(self, msg_data):
		logger.debug('handle')
		msg = msg_data['text'].strip()
		phone_num = msg_data['from'].strip()
		if(not msg):
			self.show_help(None, phone_num)
		else:
			self.parse(msg, phone_num)

	def msg_new(self, msg):
		logger.debug('msg_new')
		self.handle(msg)

def main():
	v = VoiceX(auth = config.GV_VOICEX_AUTH)
	v.init_callback()

if __name__ == "__main__":
	main()

<commit_msg>force correct follow syntax.<commit_after>"""
Copyright (c) 2012 Anant Bhardwaj

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""

import os, sys, re, logging

if __name__ == "__main__":
	p = os.path.abspath(os.path.dirname(__file__))
	if(os.path.abspath(p+"/..") not in sys.path):
		sys.path.append(os.path.abspath(p+"/.."))
	os.environ.setdefault("DJANGO_SETTINGS_MODULE", "http_handler.settings")

from model_controller import *
from models import *
from transport.voicex import VoiceXTransport
from transport import config
#import voicex.tasks

logger = logging.getLogger(__name__)

'''
Main Handler Interface

@author: Anant Bhardwaj, Trisha Kothari
@date: Aug 3, 2012
'''

class VoiceX:
	def __init__(self, auth):
		logger.debug('__init__')
		self.mc = ModelController()
		self.v = VoiceXTransport(auth = auth)

	# only for testing (polling mode)
	def init_callback(self):
		logger.debug('init_callback')
		self.v.set_callback(callback = self.msg_new)

	def show_help(self, msg, phone_num):
		logger.debug('show_help')
		help_text = "Welcome to VoiceX! Commands: register name, unregister name, post msg, view post-id, delete post-id, search query, reply post-id msg, follow @name/#tag, unfollow @name/#tag"
		if(not msg):
			pass
		elif('register' in msg):
			help_text = "Help for register : register name. Example: register voicex"
		elif('unregister' in msg):
			help_text = "Help for unregister : unregister name. Example: unregister voicex"
		elif('post' in msg):
			help_text = "Help for post : post message. Example (simple post): post hello world. Example (tagged post): post voicex is free #voicex. Example (anonymous post): post voicex is free #anon"
		elif('search' in msg):
			help_text = "Help for search : search search-keywords. Example: search kenya election"
		elif('view' in msg):
			help_text = "Help for view : view post-id. Example: view 1"
		elif('delete' in msg):
			help_text = "Help for delete : delete post-id. Example: delete 1"
		elif('reply' in msg):
			help_text = "Help for reply : reply post-id reply-msg. Example (private reply): reply 1 welcome to voicex. Example (public reply): reply 1 welcome to voicex #public. Example (anonymous reply): reply 1 welcome to voicex #anon"
		elif('follow' in msg):
			help_text = "Help for follow : follow @name/#tag. Example: follow @voicex or follow #election"
		elif('unfollow' in msg):
			help_text = "Help for unfollow : unfollow @name/#tag. Example: unfollow @voicex or unfollow #election."
		else:
			pass
		self.v.sms(phone_num, help_text)

	def register(self, name, phone_num):
		logger.debug('register')
		res = self.mc.add_account(name , phone_num)
		if(res['status']):
			self.v.sms(phone_num, 'Successfully registered %s with %s.' %(name, phone_num))
		else:
			self.v.sms(phone_num, res['code'])

	def unregister(self, name, phone_num):
		logger.debug('unregister')
		res = self.mc.delete_account(name , phone_num)
		if(res['status']):
			self.v.sms(phone_num, 'Unregistered %s' %(name))
		else:
			self.v.sms(phone_num, res['code'])

	def post(self, text, phone_num):
		logger.debug('post')
		res = self.mc.insert_post(phone_num, text);
		if(res['status']):
			post_id = res['val']
			self.v.sms(phone_num, 'Msg successfully posted. To view the post, text -- view %s' %(str(post_id)))
			self.notify_followers(phone_num, text, post_id)
		else:
			self.v.sms(phone_num, res['code'])

	def notify_followers(self, phone_num, msg, post_id):
		logger.debug('notify_followers')
		tags = [tag.strip().lower() for tag in msg.split() if tag.startswith("#")]
		res_find = self.mc.find_account(phone_num)
		account = 'anonymous'
		if(res_find['status'] and ('#anon' not in tags)):
			account = res_find['val'].name
			tags.append('@'+account)

		res_following = self.mc.find_following(tags)
		text = "From: @%s (Post ID: %s): %s" %(account, post_id, msg)
		if(res_following['status']):
			follow_list = res_following['val']
			if(len(follow_list) == 0 ):
				return
			recipients = ','.join(follow_list)
			self.v.sms(recipients, text)
		else:
			self.v.sms(phone_num, res['code'])

	def delete(self, post_id, phone_num):
		logger.debug('delete')
		res = self.mc.delete_post(post_id)
		if(res['status']):
			self.v.sms(phone_num, "Post ID:(%s) has been successfully deleted!" %(post_id))
		else:
			self.v.sms(phone_num, res['code'])

	def view(self, post_id, phone_num):
		logger.debug('view')
		res = self.mc.find_post(post_id)
		if(res['status']):
			post = res['val']
			out = post.post
			posts = Post.objects.filter(reply_to = post, public = True)
			for p in posts:
				out =  out + "[Comment (ID:%s): %s]" %(str(p.id), p.post)
			self.v.sms(phone_num, out)
		else:
			self.v.sms(phone_num, res['code'])

	def search(self, query, phone_num):
		logger.debug('search')
		res = self.mc.search_posts(query)
		if(res['status']):
			self.v.sms(phone_num, res['val'])
		else:
			self.v.sms(phone_num, res['code'])

	def reply(self, msg_data, phone_num):
		logger.debug('reply')
		tokens = msg_data.strip().split(" ", 1)
		tokens = filter(lambda x: x!='', map(lambda x: x.strip(), tokens))
		post_id = None
		reply_text = None
		try:
			post_id = str(int(tokens[0]))
			reply_text = tokens[1]
		except:
			self.show_help('reply', phone_num)
			return
		account = 'anonymous'
		tags = [tag.strip().lower() for tag in reply_text.split() if tag.startswith("#")]
		res_find_acc = self.mc.find_account(phone_num)
		if(res_find_acc['status'] and ('#anon' not in tags)):
			account = res_find_acc['val'].name
		reply_text = tokens[1]
		res_find = self.mc.find_post(post_id)
		if(res_find['status']):
			post = res_find['val']
			res_insert = {'status': False}
			if('#public' in tags):
				res_insert = self.mc.insert_reply(phone_num, reply_text, post, public=True)
			else:
				res_insert = self.mc.insert_reply(phone_num, reply_text, post)
			if(res_insert['status']):
				reply_id = str(res_insert['val'])
				self.v.sms(phone_num, "Your reply to post (ID:%s) has been successfully submitted!" %(post_id))
				reply_to  = post.phone
				self.v.sms(reply_to, "[Reply(ID:%s, From: @%s) to Post(ID:%s)] - %s" %(str(reply_id), account, str(post_id), reply_text))
			else:
				self.v.sms(phone_num, res_insert['code'])
		else:
			self.v.sms(phone_num, res_find['code'])

	def follow(self, tag, phone_num):
		logger.debug('follow')
		if(tag[0] not in ['#', '@']):
			self.v.sms(phone_num, "Invalid tag: '%s'. Valid tags are either hash-tags (start with '#'), or an account name (start with'@').")
			return
		res = self.mc.add_following(tag.strip().lower(), phone_num)
		if(res['status']):
			self.v.sms(phone_num, 'You are now following %s.' %(tag))
		else:
			self.v.sms(phone_num, res['code'])

	def unfollow(self, name, phone_num):
		logger.debug('unfollow')
		res = self.mc.delete_following(name.strip().lower(), phone_num)
		if(res['status']):
			self.v.sms(phone_num, 'You are now not following %s.' %(name))
		else:
			self.v.sms(phone_num, res['code'])

	def parse(self, msg, phone_num):
		logger.debug('parse')
		try:
			msg_data = (msg.strip()).split(" ", 1)
			msg_data = map(lambda x: x.strip(), msg_data)
			cmd = msg_data[0].lower()
			text = None
			try:
				text = msg_data[1]
			except:
				pass

			if(not text):
				self.show_help(msg_data[0], phone_num)
				return

			if (cmd == "register"):
				self.register(text, phone_num)
			elif(cmd == "unregister"):
				self.unregister(text, phone_num)
			elif(cmd == "post"):
				self.post(text, phone_num)
			elif(cmd == "view"):
				self.view(text, phone_num)
			elif(cmd ==  "search"):
				self.search(text, phone_num)
			elif(cmd == "delete"):
				self.delete(text, phone_num)
			elif(cmd == "reply"):
				self.reply(text, phone_num)
			elif(cmd == "follow"):
				self.follow(text, phone_num)
			elif(cmd == "unfollow"):
				self.unfollow(text, phone_num)
			elif(cmd == "help"):
				try:
					self.show_help(msg_data[1], phone_num)
				except:
					self.show_help(None, phone_num)
			else:
				self.v.sms(phone_num, "Unknown command: '%s'. Text 'help' to get the list of commands." %s(cmd))
		except Exception, e:
			logger.exception('parse')

	def handle(self, msg_data):
		logger.debug('handle')
		msg = msg_data['text'].strip()
		phone_num = msg_data['from'].strip()
		if(not msg):
			self.show_help(None, phone_num)
		else:
			self.parse(msg, phone_num)

	def msg_new(self, msg):
		logger.debug('msg_new')
		self.handle(msg)

def main():
	v = VoiceX(auth = config.GV_VOICEX_AUTH)
	v.init_callback()

if __name__ == "__main__":
	main()

<|endoftext|>


<commit_before>import functools
import unicodedata
from django.core.cache import get_cache
from django.conf import settings

def normalize_unicode(u):
    """Replace non-ASCII characters with closest ASCII equivalents
       where possible.
       """
    return unicodedata.normalize('NFKD', u).encode('ascii', 'ignore')

def cache_result(cache_key, timeout):
    """A decorator for caching the result of a function."""
    def decorator(f):
        cache_name = settings.WAFER_CACHE

        @functools.wraps(f)
        def wrapper(*args, **kw):
            # replace this with cache.caches when we drop Django 1.6
            # compatibility
            cache = get_cache(cache_name)
            result = cache.get(cache_key)
            if result is None:
                result = f(*args, **kw)
                cache.set(cache_key, result, timeout)
            return result

        def invalidate():
            cache = get_cache(cache_name)
            cache.delete(cache_key)

        wrapper.invalidate = invalidate
        return wrapper
    return decorator
<commit_msg>Add query tracking utility for use in tests.<commit_after>import functools
import unicodedata
from django.core.cache import get_cache
from django.conf import settings

def normalize_unicode(u):
    """Replace non-ASCII characters with closest ASCII equivalents
       where possible.
       """
    return unicodedata.normalize('NFKD', u).encode('ascii', 'ignore')

def cache_result(cache_key, timeout):
    """A decorator for caching the result of a function."""
    def decorator(f):
        cache_name = settings.WAFER_CACHE

        @functools.wraps(f)
        def wrapper(*args, **kw):
            # replace this with cache.caches when we drop Django 1.6
            # compatibility
            cache = get_cache(cache_name)
            result = cache.get(cache_key)
            if result is None:
                result = f(*args, **kw)
                cache.set(cache_key, result, timeout)
            return result

        def invalidate():
            cache = get_cache(cache_name)
            cache.delete(cache_key)

        wrapper.invalidate = invalidate
        return wrapper
    return decorator

class QueryTracker(object):
    """ Track queries to database. """

    def __enter__(self):
        from django.conf import settings
        from django.db import connection
        self._debug = settings.DEBUG
        settings.DEBUG = True
        connection.queries = []
        return self

    def __exit__(self, *args, **kw):
        from django.conf import settings
        settings.DEBUG = self._debug

    @property
    def queries(self):
        from django.db import connection
        return connection.queries[:]
<|endoftext|>


<commit_before>#!/usr/bin/env python
"""
Utilities for handling subprocesses.

Mostly necessary only because of http://bugs.python.org/issue1652.

"""
import copy
import errno
import fcntl
import gevent
import gevent.socket
import os
import signal

import sys

from cStringIO import StringIO

from wal_e import subprocess
from wal_e.subprocess import PIPE

# This is not used in this module, but is imported by dependent
# modules, so do this to quiet pyflakes.
assert PIPE

# Determine the maximum number of bytes that can be written atomically
# to a pipe
PIPE_BUF_BYTES = os.pathconf('.', os.pathconf_names['PC_PIPE_BUF'])

class NonBlockPipeFileWrap(object):
    def __init__(self, fp):
        # Make the file nonblocking (but don't lose its previous flags)
        flags = fcntl.fcntl(fp, fcntl.F_GETFL)
        fcntl.fcntl(fp, fcntl.F_SETFL, flags | os.O_NONBLOCK)

        self._fp = fp

    def read(self, size=None):
        # Some adaptation from gevent's examples/processes.py
        accum = StringIO()

        while size is None or accum.tell() < size:
            try:
                if size is None:
                    max_read = PIPE_BUF_BYTES
                else:
                    max_read = min(PIPE_BUF_BYTES, size - accum.tell())

                chunk = self._fp.read(max_read)

                # End of the stream: leave the loop
                if not chunk:
                    break
                accum.write(chunk)
            except IOError, ex:
                if ex[0] != errno.EAGAIN:
                    raise
                sys.exc_clear()
            gevent.socket.wait_read(self._fp.fileno())

        return accum.getvalue()

    def write(self, data):
        # Some adaptation from gevent's examples/processes.py
        buf = StringIO(data)
        bytes_total = len(data)
        bytes_written = 0
        while bytes_written < bytes_total:
            try:
                # self._fp.write() doesn't return anything, so use
                # os.write.
                bytes_written += os.write(self._fp.fileno(),
                                          buf.read(PIPE_BUF_BYTES))
            except IOError, ex:
                if ex[0] != errno.EAGAIN:
                    raise
                sys.exc_clear()
            gevent.socket.wait_write(self._fp.fileno())

    def fileno(self):
        return self._fp.fileno()

    def close(self):
        return self._fp.close()

    def flush(self):
        return self._fp.flush()

    @property
    def closed(self):
        return self._fp.closed

def subprocess_setup(f=None):
    """
    SIGPIPE reset for subprocess workaround

    Python installs a SIGPIPE handler by default. This is usually not
    what non-Python subprocesses expect.

    Calls an optional "f" first in case other code wants a preexec_fn,
    then restores SIGPIPE to what most Unix processes expect.

    http://bugs.python.org/issue1652

    """

    def wrapper(*args, **kwargs):
        if f is not None:
            f(*args, **kwargs)

        signal.signal(signal.SIGPIPE, signal.SIG_DFL)

    return wrapper

class PopenShim(object):
    def __init__(self, sleep_time=1, max_tries=None):
        self.sleep_time = sleep_time
        self.max_tries = max_tries

    def __call__(self, *args, **kwargs):
        """
        Same as subprocess.Popen, but restores SIGPIPE

        This bug is documented (See subprocess_setup) but did not make
        it to standard library.  Could also be resolved by using the
        python-subprocess32 backport and using it appropriately (See
        'restore_signals' keyword argument to Popen)
        """

        kwargs['preexec_fn'] = subprocess_setup(kwargs.get('preexec_fn'))

        # Call Popen, but be persistent in the face of ENOMEM.
        #
        # The utility of this is that on systems with overcommit off,
        # the momentary spike in committed virtual memory from fork()
        # can be large, but is cleared soon thereafter because
        # 'subprocess' uses an 'exec' system call.  Without retrying,
        # the the backup process would lose all its progress
        # immediately with no recourse, which is undesirable.
        #
        # Because the ENOMEM error happens on fork() before any
        # meaningful work can be done, one thinks this retry would be
        # safe, and without side effects.  Because fork is being
        # called through 'subprocess' and not directly here, this
        # program has to rely on the semantics of the exceptions
        # raised from 'subprocess' to avoid retries in unrelated
        # scenarios, which could be dangerous.
        tries = 0

        while True:
            try:
                proc = subprocess.Popen(*args, **kwargs)
            except OSError, e:
                if e.errno == errno.ENOMEM:
                    should_retry = (self.max_tries is not None and
                                    tries >= self.max_tries)

                    if should_retry:
                        raise

                    gevent.sleep(self.sleep_time)
                    tries += 1
                    continue

                raise
            else:
                break

        return proc

popen_sp = PopenShim()

def popen_nonblock(*args, **kwargs):
    """
    Create a process in the same way as popen_sp, but patch the file
    descriptors so they can can be accessed from Python/gevent
    in a non-blocking manner.
    """

    proc = popen_sp(*args, **kwargs)

    # Patch up the process object to use non-blocking I/O that yields
    # to the gevent hub.
    for fp_symbol in ['stdin', 'stdout', 'stderr']:
        value = getattr(proc, fp_symbol)

        if value is not None:
            # this branch is only taken if a descriptor is sent in
            # with 'PIPE' mode.
            setattr(proc, fp_symbol, NonBlockPipeFileWrap(value))

    return proc

def pipe(*args):
    """
    Takes as parameters several dicts, each with the same
    parameters passed to popen.

    Runs the various processes in a pipeline, connecting
    the stdout of every process except the last with the
    stdin of the next process.

    Adapted from http://www.enricozini.org/2009/debian/python-pipes/

    """
    if len(args) < 2:
        raise ValueError("pipe needs at least 2 processes")

    # Set stdout=PIPE in every subprocess except the last
    for i in args[:-1]:
        i["stdout"] = subprocess.PIPE

    # Runs all subprocesses connecting stdins and stdouts to create the
    # pipeline. Closes stdouts to avoid deadlocks.
    popens = [popen_sp(**args[0])]
    for i in range(1, len(args)):
        args[i]["stdin"] = popens[i - 1].stdout
        popens.append(popen_sp(**args[i]))
        popens[i - 1].stdout.close()

    # Returns the array of subprocesses just created
    return popens

def pipe_wait(popens):
    """
    Given an array of Popen objects returned by the
    pipe method, wait for all processes to terminate
    and return the array with their return values.

    Taken from http://www.enricozini.org/2009/debian/python-pipes/

    """
    # Avoid mutating the passed copy
    popens = copy.copy(popens)
    results = [0] * len(popens)
    while popens:
        last = popens.pop(-1)
        results[len(popens)] = last.wait()
    return results
<commit_msg>Redo questionable the non-blocking-pipe I/O routines<commit_after>#!/usr/bin/env python
"""
Utilities for handling subprocesses.

Mostly necessary only because of http://bugs.python.org/issue1652.

"""
import copy
import errno
import fcntl
import gevent
import gevent.socket
import os
import signal

from cStringIO import StringIO

from wal_e import subprocess
from wal_e.subprocess import PIPE

# This is not used in this module, but is imported by dependent
# modules, so do this to quiet pyflakes.
assert PIPE

# Determine the maximum number of bytes that can be written atomically
# to a pipe
PIPE_BUF_BYTES = os.pathconf('.', os.pathconf_names['PC_PIPE_BUF'])

class NonBlockPipeFileWrap(object):
    def __init__(self, fp):
        # Make the file nonblocking (but don't lose its previous flags)
        flags = fcntl.fcntl(fp, fcntl.F_GETFL)
        fcntl.fcntl(fp, fcntl.F_SETFL, flags | os.O_NONBLOCK)

        self._fp = fp

    def read(self, size=None):
        # Some adaptation from gevent's examples/processes.py
        accum = StringIO()
        fd = self._fp.fileno()

        while size is None or accum.tell() < size:
            if size is None:
                max_read = PIPE_BUF_BYTES
            else:
                max_read = min(PIPE_BUF_BYTES, size - accum.tell())

            # Put a retry around this one write syscall to take care
            # of EAGAIN.
            succeeded = False
            while not succeeded:
                chunk = None

                try:
                    chunk = os.read(fd, max_read)
                    succeeded = True
                except EnvironmentError, ex:
                    if ex.errno == errno.EAGAIN:
                        assert chunk is None
                        gevent.socket.wait_read(fd)
                    else:
                        raise

            # 'chunk' should be assigned at least once if no exception
            # was raised, even if an immediate EOF is found.
            assert chunk is not None

            # End of the stream: leave the loop.
            if chunk == '':
                break

            accum.write(chunk)

        return accum.getvalue()

    def write(self, data):
        # Some adaptation from gevent's examples/processes.py
        buf = StringIO(data)
        bytes_total = len(data)
        bytes_written = 0
        fd = self._fp.fileno()

        while bytes_written < bytes_total:
            chunk = buf.read(PIPE_BUF_BYTES)

            # Put a retry around this one write syscall to take care
            # of EAGAIN.
            succeeded = False
            while not succeeded:
                try:
                    bytes_written += os.write(fd, chunk)
                    succeeded = True
                except EnvironmentError, ex:
                    if ex.errno == errno.EAGAIN:
                        gevent.socket.wait_write(fd)
                    else:
                        raise

    def fileno(self):
        return self._fp.fileno()

    def close(self):
        return self._fp.close()

    def flush(self):
        return self._fp.flush()

    @property
    def closed(self):
        return self._fp.closed

def subprocess_setup(f=None):
    """
    SIGPIPE reset for subprocess workaround

    Python installs a SIGPIPE handler by default. This is usually not
    what non-Python subprocesses expect.

    Calls an optional "f" first in case other code wants a preexec_fn,
    then restores SIGPIPE to what most Unix processes expect.

    http://bugs.python.org/issue1652

    """

    def wrapper(*args, **kwargs):
        if f is not None:
            f(*args, **kwargs)

        signal.signal(signal.SIGPIPE, signal.SIG_DFL)

    return wrapper

class PopenShim(object):
    def __init__(self, sleep_time=1, max_tries=None):
        self.sleep_time = sleep_time
        self.max_tries = max_tries

    def __call__(self, *args, **kwargs):
        """
        Same as subprocess.Popen, but restores SIGPIPE

        This bug is documented (See subprocess_setup) but did not make
        it to standard library.  Could also be resolved by using the
        python-subprocess32 backport and using it appropriately (See
        'restore_signals' keyword argument to Popen)
        """

        kwargs['preexec_fn'] = subprocess_setup(kwargs.get('preexec_fn'))

        # Call Popen, but be persistent in the face of ENOMEM.
        #
        # The utility of this is that on systems with overcommit off,
        # the momentary spike in committed virtual memory from fork()
        # can be large, but is cleared soon thereafter because
        # 'subprocess' uses an 'exec' system call.  Without retrying,
        # the the backup process would lose all its progress
        # immediately with no recourse, which is undesirable.
        #
        # Because the ENOMEM error happens on fork() before any
        # meaningful work can be done, one thinks this retry would be
        # safe, and without side effects.  Because fork is being
        # called through 'subprocess' and not directly here, this
        # program has to rely on the semantics of the exceptions
        # raised from 'subprocess' to avoid retries in unrelated
        # scenarios, which could be dangerous.
        tries = 0

        while True:
            try:
                proc = subprocess.Popen(*args, **kwargs)
            except OSError, e:
                if e.errno == errno.ENOMEM:
                    should_retry = (self.max_tries is not None and
                                    tries >= self.max_tries)

                    if should_retry:
                        raise

                    gevent.sleep(self.sleep_time)
                    tries += 1
                    continue

                raise
            else:
                break

        return proc

popen_sp = PopenShim()

def popen_nonblock(*args, **kwargs):
    """
    Create a process in the same way as popen_sp, but patch the file
    descriptors so they can can be accessed from Python/gevent
    in a non-blocking manner.
    """

    proc = popen_sp(*args, **kwargs)

    # Patch up the process object to use non-blocking I/O that yields
    # to the gevent hub.
    for fp_symbol in ['stdin', 'stdout', 'stderr']:
        value = getattr(proc, fp_symbol)

        if value is not None:
            # this branch is only taken if a descriptor is sent in
            # with 'PIPE' mode.
            setattr(proc, fp_symbol, NonBlockPipeFileWrap(value))

    return proc

def pipe(*args):
    """
    Takes as parameters several dicts, each with the same
    parameters passed to popen.

    Runs the various processes in a pipeline, connecting
    the stdout of every process except the last with the
    stdin of the next process.

    Adapted from http://www.enricozini.org/2009/debian/python-pipes/

    """
    if len(args) < 2:
        raise ValueError("pipe needs at least 2 processes")

    # Set stdout=PIPE in every subprocess except the last
    for i in args[:-1]:
        i["stdout"] = subprocess.PIPE

    # Runs all subprocesses connecting stdins and stdouts to create the
    # pipeline. Closes stdouts to avoid deadlocks.
    popens = [popen_sp(**args[0])]
    for i in range(1, len(args)):
        args[i]["stdin"] = popens[i - 1].stdout
        popens.append(popen_sp(**args[i]))
        popens[i - 1].stdout.close()

    # Returns the array of subprocesses just created
    return popens

def pipe_wait(popens):
    """
    Given an array of Popen objects returned by the
    pipe method, wait for all processes to terminate
    and return the array with their return values.

    Taken from http://www.enricozini.org/2009/debian/python-pipes/

    """
    # Avoid mutating the passed copy
    popens = copy.copy(popens)
    results = [0] * len(popens)
    while popens:
        last = popens.pop(-1)
        results[len(popens)] = last.wait()
    return results
<|endoftext|>


<commit_before>from __future__ import print_function
import matplotlib.pyplot as plt
import numpy as np
import SimpleITK as sitk
import sys
import os
import time

def command_iteration(method) :
     print("{0:3} = {1:10.5f} : {2}".format(method.GetOptimizerIteration(),
                                    method.GetMetricValue(),
                                    method.GetOptimizerPosition()))

def RSGD():
    outputFile = 'RSGD.txt'
    # outputFile = 'RSGD.nii.gz'
    R = sitk.ImageRegistrationMethod()
    R.SetMetricAsMeanSquares()
    R.SetOptimizerAsRegularStepGradientDescent(4.0, .01, 200)
    R.SetInitialTransform(sitk.TranslationTransform(fixed.GetDimension()))
    R.SetInterpolator(sitk.sitkLinear)
    return outputFile, R

def GDLS(fixed, moving):
    outputFile = 'GDLS.txt'
    fixed = sitk.Normalize(fixed)
    fixed = sitk.DiscreteGaussian(fixed, 2.0)
    moving = sitk.Normalize(moving)
    moving = sitk.DiscreteGaussian(moving, 2.0)
    R = sitk.ImageRegistrationMethod()
    R.SetMetricAsJointHistogramMutualInformation()
    R.SetOptimizerAsGradientDescentLineSearch(learningRate=1.0,
                                              numberOfIterations=200,
                                              convergenceMinimumValue=1e-5,
                                              convergenceWindowSize=5)
    R.SetInitialTransform(sitk.TranslationTransform(fixed.GetDimension()))
    R.SetInterpolator(sitk.sitkLinear)
    return outputFile, fixed, moving, R

def corr_RSGD(fixed, moving):
    outputFile = 'corr_RSGD.txt'
    R = sitk.ImageRegistrationMethod()
    R.SetMetricAsCorrelation()
    R.SetOptimizerAsRegularStepGradientDescent(learningRate=2.0,
                                               minStep=1e-4,
                                               numberOfIterations=500,
                                               gradientMagnitudeTolerance=1e-8)
    R.SetOptimizerScalesFromIndexShift()
    tx = sitk.CenteredTransformInitializer(fixed, moving, sitk.Similarity3DTransform())
    R.SetInitialTransform(tx)
    R.SetInterpolator(sitk.sitkLinear)
    return outputFile, R

def MMI_RSGD():
    outputFile = 'MMI_RSGD.txt'
    numberOfBins = 24
    samplingPercentage = 0.10
    R = sitk.ImageRegistrationMethod()
    R.SetMetricAsMattesMutualInformation(numberOfBins)
    R.SetMetricSamplingPercentage(samplingPercentage, sitk.sitkWallClock)
    R.SetMetricSamplingStrategy(R.RANDOM)
    R.SetOptimizerAsRegularStepGradientDescent(1.0, .001, 200)
    R.SetInitialTransform(sitk.TranslationTransform(fixed.GetDimension()))
    R.SetInterpolator(sitk.sitkLinear)
    return outputFile, R

fixed = sitk.ReadImage('./test/100307/T1mni.nii.gz', sitk.sitkFloat32)
moving = sitk.ReadImage('./test/100307/T2mni.nii.gz', sitk.sitkFloat32)

# different registration and metric systems
# outputFile, R = RSGD() # Total exection time: 32.13047194480896s
# outputFile, fixed, moving, R = GDLS(fixed, moving) # Total exection time: 219.74626207351685s
outputFile, R = corr_RSGD(fixed, moving) # Total exection time: 199.60729265213013s
# outputFile, R = MMI_RSGD() # Total exection time: 7.378397226333618s

R.AddCommand( sitk.sitkIterationEvent, lambda: command_iteration(R) )

# We'll meausure the execution time
start = time.time()

outTx = R.Execute(fixed, moving)

exec_time = time.time()-start
print('Total exection time: {}s'.format(exec_time))

print("-------")
print(outTx)
print("Optimizer stop condition: {0}".format(R.GetOptimizerStopConditionDescription()))
print(" Iteration: {0}".format(R.GetOptimizerIteration()))
print(" Metric value: {0}".format(R.GetMetricValue()))

sitk.WriteTransform(outTx,  outputFile)

if ( not "SITK_NOSHOW" in os.environ ):
    resampler = sitk.ResampleImageFilter()
    resampler.SetReferenceImage(fixed);
    resampler.SetInterpolator(sitk.sitkLinear)
    resampler.SetDefaultPixelValue(100)
    resampler.SetTransform(outTx)<commit_msg>comit<commit_after>from __future__ import print_function
import matplotlib.pyplot as plt
import numpy as np
import SimpleITK as sitk
import sys
import os
import time

def command_iteration(method) :
     print("{0:3} = {1:10.5f} : {2}".format(method.GetOptimizerIteration(),
                                    method.GetMetricValue(),
                                    method.GetOptimizerPosition()))

def RSGD():
    outputFile = 'RSGD.txt'
    # outputFile = 'RSGD.nii.gz'
    R = sitk.ImageRegistrationMethod()
    R.SetMetricAsMeanSquares()
    R.SetOptimizerAsRegularStepGradientDescent(4.0, .001, 200)
    R.SetInitialTransform(sitk.TranslationTransform(fixed.GetDimension()))
    R.SetInterpolator(sitk.sitkLinear)
    return outputFile, R

def GDLS(fixed, moving):
    outputFile = 'GDLS.txt'
    fixed = sitk.Normalize(fixed)
    fixed = sitk.DiscreteGaussian(fixed, 2.0)
    moving = sitk.Normalize(moving)
    moving = sitk.DiscreteGaussian(moving, 2.0)
    R = sitk.ImageRegistrationMethod()
    R.SetMetricAsJointHistogramMutualInformation()
    R.SetOptimizerAsGradientDescentLineSearch(learningRate=1.0,
                                              numberOfIterations=200,
                                              convergenceMinimumValue=1e-5,
                                              convergenceWindowSize=5)
    R.SetInitialTransform(sitk.TranslationTransform(fixed.GetDimension()))
    R.SetInterpolator(sitk.sitkLinear)
    return outputFile, fixed, moving, R

def corr_RSGD(fixed, moving):
    outputFile = 'corr_RSGD.txt'
    R = sitk.ImageRegistrationMethod()
    R.SetMetricAsCorrelation()
    R.SetOptimizerAsRegularStepGradientDescent(learningRate=2.0,
                                               minStep=1e-4,
                                               numberOfIterations=500,
                                               gradientMagnitudeTolerance=1e-8)
    R.SetOptimizerScalesFromIndexShift()
    tx = sitk.CenteredTransformInitializer(fixed, moving, sitk.Similarity3DTransform())
    R.SetInitialTransform(tx)
    R.SetInterpolator(sitk.sitkLinear)
    return outputFile, R

def MMI_RSGD():
    outputFile = 'MMI_RSGD.txt'
    numberOfBins = 24
    samplingPercentage = 0.10
    R = sitk.ImageRegistrationMethod()
    R.SetMetricAsMattesMutualInformation(numberOfBins)
    R.SetMetricSamplingPercentage(samplingPercentage, sitk.sitkWallClock)
    R.SetMetricSamplingStrategy(R.RANDOM)
    R.SetOptimizerAsRegularStepGradientDescent(1.0, .001, 200)
    R.SetInitialTransform(sitk.TranslationTransform(fixed.GetDimension()))
    R.SetInterpolator(sitk.sitkLinear)
    return outputFile, R

fixed = sitk.ReadImage('./test/100307/T1mni.nii.gz', sitk.sitkFloat32)
moving = sitk.ReadImage('./test/100307/T2mni.nii.gz', sitk.sitkFloat32)

# different registration and metric systems
# outputFile, R = RSGD() # Total exection time: 32.13047194480896s
outputFile, fixed, moving, R = GDLS(fixed, moving) # Total exection time: 219.74626207351685s
# outputFile, R = corr_RSGD(fixed, moving) # Total exection time: 199.60729265213013s
# outputFile, R = MMI_RSGD() # Total exection time: 7.378397226333618s

R.AddCommand( sitk.sitkIterationEvent, lambda: command_iteration(R) )

# We'll meausure the execution time
start = time.time()

outTx = R.Execute(fixed, moving)

exec_time = time.time()-start
print('Total exection time: {}s'.format(exec_time))

print("-------")
print(outTx)
print("Optimizer stop condition: {0}".format(R.GetOptimizerStopConditionDescription()))
print(" Iteration: {0}".format(R.GetOptimizerIteration()))
print(" Metric value: {0}".format(R.GetMetricValue()))

sitk.WriteTransform(outTx,  outputFile)

registered_image = sitk.Resample(moving, fixed, outTx, sitk.sitkLinear, 0.0, moving.GetPixelIDValue())
sitk.WriteImage(registered_image, 'myRegistred2.nii.gz')

if ( not "SITK_NOSHOW" in os.environ ):
    resampler = sitk.ResampleImageFilter()
    resampler.SetReferenceImage(fixed);
    resampler.SetInterpolator(sitk.sitkLinear)
    resampler.SetDefaultPixelValue(100)
    resampler.SetTransform(outTx)<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from girder.api import access
from girder.api.rest import Resource, loadmodel
from girder.api.describe import Description, describeRoute
from girder.constants import AccessType
from girder.models.model_base import ValidationException

class DatasetResource(Resource):
    def __init__(self):
        super(DatasetResource, self).__init__()
        self.resourceName = 'dataset'

        self.route('GET', (), self.find)
        self.route('GET', (':id',), self.getDataset)
        self.route('POST', (), self.createDataset)

    @describeRoute(
        Description('Return a list of lesion image datasets.')
        .pagingParams(defaultSort='lowerName')
        .errorResponse()
    )
    @access.public
    def find(self, params):
        limit, offset, sort = self.getPagingParameters(params, 'lowerName')

        return [
            {
                field: dataset[field]
                for field in
                self.model('dataset', 'isic_archive').summaryFields
            }
            for dataset in
            self.model('dataset', 'isic_archive').list(
                user=self.getCurrentUser(),
                limit=limit, offset=offset, sort=sort)
        ]

    @describeRoute(
        Description('Return a lesion image dataset\'s details.')
        .param('id', 'The ID of the dataset.', paramType='path')
        .errorResponse('ID was invalid.')
    )
    @access.public
    @loadmodel(model='dataset', plugin='isic_archive', level=AccessType.READ)
    def getDataset(self, dataset, params):
        return self.model('dataset', 'isic_archive').filter(
            dataset, self.getCurrentUser())

    @describeRoute(
        Description('Create a lesion image dataset.')
        .param('uploadFolderId', 'The ID of the folder that contains images '
               'and metadata.')
        .param('name', 'Name of the dataset.')
        .param('description', 'Description of the dataset.', required=False)
        .param('license', 'License of the dataset.', required=False)
        .param('signature', 'Signature of license agreement.', required=True)
        .param('anonymous', 'Whether to use an anonymous attribution for the '
               'dataset', dataType='boolean', required=False)
        .param('attribution', 'Attribution of the dataset.', required=False)
    )
    @access.user
    def createDataset(self, params):
        self.requireParams(('uploadFolderId', 'name'), params)

        user = self.getCurrentUser()
        uploadFolderId = params.get('uploadFolderId', None)
        if not uploadFolderId:
            raise ValidationException(
                'No files were uploaded.', 'uploadFolderId')
        uploadFolder = self.model('folder').load(
            uploadFolderId, user=user, level=AccessType.READ, force=True)
        if not uploadFolder:
            raise ValidationException(
                'Invalid upload folder ID.', 'uploadFolderId')

        name = params['name'].strip()
        description = params.get('description', '').strip()
        license = params.get('license', '').strip()

        # Enforce valid licensing metadata only at API level
        signature = params.get('signature', '').strip()
        if not signature:
            raise ValidationException(
                'Signature must be specified.', 'signature')
        anonymous = self.boolParam('anonymous', params, False)
        attribution = params.get('attribution', '').strip()
        if not anonymous and not attribution:
            raise ValidationException(
                'Attribution must be specified when not contributing '
                'anonymously.', 'attribution')

        return self.model('dataset', 'isic_archive').ingestDataset(
            uploadFolder=uploadFolder, user=user, name=name,
            description=description, license=license, signature=signature,
            anonymous=anonymous, attribution=attribution)
<commit_msg>Don't force load of upload folder<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from girder.api import access
from girder.api.rest import Resource, loadmodel
from girder.api.describe import Description, describeRoute
from girder.constants import AccessType
from girder.models.model_base import ValidationException

class DatasetResource(Resource):
    def __init__(self):
        super(DatasetResource, self).__init__()
        self.resourceName = 'dataset'

        self.route('GET', (), self.find)
        self.route('GET', (':id',), self.getDataset)
        self.route('POST', (), self.createDataset)

    @describeRoute(
        Description('Return a list of lesion image datasets.')
        .pagingParams(defaultSort='lowerName')
        .errorResponse()
    )
    @access.public
    def find(self, params):
        limit, offset, sort = self.getPagingParameters(params, 'lowerName')

        return [
            {
                field: dataset[field]
                for field in
                self.model('dataset', 'isic_archive').summaryFields
            }
            for dataset in
            self.model('dataset', 'isic_archive').list(
                user=self.getCurrentUser(),
                limit=limit, offset=offset, sort=sort)
        ]

    @describeRoute(
        Description('Return a lesion image dataset\'s details.')
        .param('id', 'The ID of the dataset.', paramType='path')
        .errorResponse('ID was invalid.')
    )
    @access.public
    @loadmodel(model='dataset', plugin='isic_archive', level=AccessType.READ)
    def getDataset(self, dataset, params):
        return self.model('dataset', 'isic_archive').filter(
            dataset, self.getCurrentUser())

    @describeRoute(
        Description('Create a lesion image dataset.')
        .param('uploadFolderId', 'The ID of the folder that contains images '
               'and metadata.')
        .param('name', 'Name of the dataset.')
        .param('description', 'Description of the dataset.', required=False)
        .param('license', 'License of the dataset.', required=False)
        .param('signature', 'Signature of license agreement.', required=True)
        .param('anonymous', 'Whether to use an anonymous attribution for the '
               'dataset', dataType='boolean', required=False)
        .param('attribution', 'Attribution of the dataset.', required=False)
    )
    @access.user
    def createDataset(self, params):
        self.requireParams(('uploadFolderId', 'name'), params)

        user = self.getCurrentUser()
        uploadFolderId = params.get('uploadFolderId', None)
        if not uploadFolderId:
            raise ValidationException(
                'No files were uploaded.', 'uploadFolderId')
        uploadFolder = self.model('folder').load(
            uploadFolderId, user=user, level=AccessType.READ)
        if not uploadFolder:
            raise ValidationException(
                'Invalid upload folder ID.', 'uploadFolderId')

        name = params['name'].strip()
        description = params.get('description', '').strip()
        license = params.get('license', '').strip()

        # Enforce valid licensing metadata only at API level
        signature = params.get('signature', '').strip()
        if not signature:
            raise ValidationException(
                'Signature must be specified.', 'signature')
        anonymous = self.boolParam('anonymous', params, False)
        attribution = params.get('attribution', '').strip()
        if not anonymous and not attribution:
            raise ValidationException(
                'Attribution must be specified when not contributing '
                'anonymously.', 'attribution')

        return self.model('dataset', 'isic_archive').ingestDataset(
            uploadFolder=uploadFolder, user=user, name=name,
            description=description, license=license, signature=signature,
            anonymous=anonymous, attribution=attribution)
<|endoftext|>