<commit_before>"""
usage: atlasDustCleanup.py --inputAtlasPath=<argument> --outputAtlasPath=<argument> --inputT1Path=<argument> --inputT2Path=<argument> --label=<argument> --maximumIslandVoxelCount=<argument>
atlasDustCleanup.py -h | --help
"""

import SimpleITK as sitk
import math

class DustCleanup():

  def __init__(self, arguments):
    self.inputAtlasPath = arguments['--inputAtlasPath']
    self.outputAtlasPath = arguments['--outputAtlasPath']
    self.inputT1Path = arguments['--inputT1Path']
    self.inputT2Path = arguments['--inputT2Path']
    self.label = int(arguments['--label'])
    self.maximumIslandVoxelCount = int(arguments['--maximumIslandVoxelCount'])

  def main(self):
    inputLabelImage = sitk.Cast(sitk.ReadImage(self.inputAtlasPath), sitk.sitkInt16)
    relabeledConnectedRegion = self.thresholdAtlas(inputLabelImage)
    inputT1VolumeImage = sitk.ReadImage(self.inputT1Path),
    inputT2VolumeImage = sitk.ReadImage(self.inputT2Path)
    labelStatsT1WithRelabeledConnectedRegion = self.getLabelStatsObject(inputT1VolumeImage, relabeledConnectedRegion)
    labelStatsT2WithRelabeledConnectedRegion = self.getLabelStatsObject(inputT2VolumeImage, relabeledConnectedRegion)
    labelStatsT1WithInputLabelImage = self.getLabelStatsObject(inputT1VolumeImage, inputLabelImage)
    labelStatsT2WithInputLabelImage = self.getLabelStatsObject(inputT2VolumeImage, inputLabelImage)
    labelList = self.getLabelListFromLabelStatsObject(labelStatsT1WithRelabeledConnectedRegion)
    labelList.reverse()
    print labelList[0:2]

    for currentLabel in labelList[0:2]:
      islandVoxelCount = labelStatsT1WithRelabeledConnectedRegion.GetCount(currentLabel)
      print islandVoxelCount
      if islandVoxelCount <= self.maximumIslandVoxelCount:
        meanT1Intesity = labelStatsT1WithRelabeledConnectedRegion.GetMean(currentLabel)
        meanT2Intesity = labelStatsT2WithRelabeledConnectedRegion.GetMean(currentLabel)
        targetLabels = self.getTargetLabels(inputLabelImage, inputT1VolumeImage, currentLabel)
        diffDict = self.calculateLabelIntensityDifferenceValue(meanT1Intesity, meanT2Intesity,
                                                               targetLabels,
                                                               labelStatsT1WithInputLabelImage,
                                                               labelStatsT2WithInputLabelImage)
        print diffDict

  def thresholdAtlas(self, inputLabelImage):
    binaryThresholdImage = sitk.BinaryThreshold(inputLabelImage, self.label, self.label)
    connectedRegion = sitk.ConnectedComponent(binaryThresholdImage, True)
    relabeledConnectedRegion = sitk.RelabelComponent(connectedRegion)
    return relabeledConnectedRegion

  def getLabelStatsObject(self, volumeImage, labelImage):
    labelStatsObject = sitk.LabelStatisticsImageFilter()
    labelStatsObject.Execute(volumeImage, labelImage)

    return labelStatsObject

  def getLabelListFromLabelStatsObject(self, labelStatsObject):
    if sitk.Version().MajorVersion() > 0 or sitk.Version().MinorVersion() >= 9:
      compontentLabels = labelStatsObject.GetLabels()
    else: #if sitk version < 0.9 then use older function call GetValidLabels
      compontentLabels = labelStatsObject.GetValidLabels()
    return list(compontentLabels)

  def getTargetLabels(self, inputLabelImage, inputVolumeImage, currentLabel):
    currentLabelBinaryThresholdImage = sitk.BinaryThreshold(inputLabelImage, currentLabel, currentLabel)
    castedCurrentLabelBinaryThresholdImage = sitk.Cast(currentLabelBinaryThresholdImage, sitk.sitkInt16)

    dialatedBinaryLabelMap = self.dialateLabelMap(castedCurrentLabelBinaryThresholdImage)
    reducedLabelMapImage = sitk.Multiply(dialatedBinaryLabelMap, inputLabelImage)

    reducedLabelMapT1LabelStats = self.getLabelStatsObject(inputVolumeImage, reducedLabelMapImage)
    # targetLabels = reducedLabelMapT1LabelStats.GetLabels()
    targetLabels = self.getLabelListFromLabelStatsObject(reducedLabelMapT1LabelStats)
    return targetLabels

  def dialateLabelMap(self, inputLabelImage):
    myFilter = sitk.BinaryDilateImageFilter()
    myFilter.SetBackgroundValue(0.0)
    myFilter.SetBoundaryToForeground(False)
    myFilter.SetDebug(False)
    myFilter.SetForegroundValue(1.0)
    myFilter.SetKernelRadius((1, 1, 1))
    myFilter.SetKernelType(2)  # Kernel Type=Box
    myFilter.SetNumberOfThreads(8)
    output = myFilter.Execute(inputLabelImage)
    castedOutput = sitk.Cast(output, sitk.sitkInt16)

    return castedOutput

  def calculateLabelIntensityDifferenceValue(self, averageT1IntensitySuspiciousLabel,
                                             averageT2IntensitySuspiciousLabel,
                                             targetLabels, T1LabelStats, T2LabelStats):
    """
    Calculates a measurement for each label that is on the border of the suspicious label.
    This value is the square root of the sum of the squared difference in the average T1
    intensity values and the squared difference in the average T2 intensity values of the
    two islands in the comparison. The calculated value for each border label will later be
    sorted in ascending order - meaning that the smallest value has the "closest" average
    intensity to the suspicious label.
    """

    squareRootDiffLabelDict = dict()

    for targetLabel in targetLabels:
      if targetLabel == 0:
        continue
      averageT1IntensityTargetLabel = T1LabelStats.GetMean(targetLabel)
      averageT2IntensityTargetLabel = T2LabelStats.GetMean(targetLabel)

      squareDiffAverageT1 = math.pow(averageT1IntensitySuspiciousLabel -
                                     averageT1IntensityTargetLabel, 2)
      squareDiffAverageT2 = math.pow(averageT2IntensitySuspiciousLabel -
                                     averageT2IntensityTargetLabel, 2)
      squareRootDiff = math.sqrt(squareDiffAverageT1 + squareDiffAverageT2)

      squareRootDiffLabelDict[int(targetLabel)] = squareRootDiff

    return squareRootDiffLabelDict

if __name__ == '__main__':
  from docopt import docopt
  arguments = docopt(__doc__)
  print arguments
  Object = DustCleanup(arguments)
  Object.main()
<commit_msg>BUG: Uses relabeledConnectedRegion to create binary threshold image with current label<commit_after>"""
usage: atlasDustCleanup.py --inputAtlasPath=<argument> --outputAtlasPath=<argument> --inputT1Path=<argument> --inputT2Path=<argument> --label=<argument> --maximumIslandVoxelCount=<argument>
atlasDustCleanup.py -h | --help
"""

import SimpleITK as sitk
import math

class DustCleanup():

  def __init__(self, arguments):
    self.inputAtlasPath = arguments['--inputAtlasPath']
    self.outputAtlasPath = arguments['--outputAtlasPath']
    self.inputT1Path = arguments['--inputT1Path']
    self.inputT2Path = arguments['--inputT2Path']
    self.label = int(arguments['--label'])
    self.maximumIslandVoxelCount = int(arguments['--maximumIslandVoxelCount'])

  def main(self):
    inputLabelImage = sitk.Cast(sitk.ReadImage(self.inputAtlasPath), sitk.sitkInt16)
    relabeledConnectedRegion = sitk.Cast(self.thresholdAtlas(inputLabelImage), sitk.sitkInt16)
    inputT1VolumeImage = sitk.ReadImage(self.inputT1Path)
    inputT2VolumeImage = sitk.ReadImage(self.inputT2Path)
    labelStatsT1WithRelabeledConnectedRegion = self.getLabelStatsObject(inputT1VolumeImage, relabeledConnectedRegion)
    labelStatsT2WithRelabeledConnectedRegion = self.getLabelStatsObject(inputT2VolumeImage, relabeledConnectedRegion)
    labelStatsT1WithInputLabelImage = self.getLabelStatsObject(inputT1VolumeImage, inputLabelImage)
    labelStatsT2WithInputLabelImage = self.getLabelStatsObject(inputT2VolumeImage, inputLabelImage)
    labelList = self.getLabelListFromLabelStatsObject(labelStatsT1WithRelabeledConnectedRegion)
    labelList.reverse()
    print labelList[700:702]

    for currentLabel in labelList[700:702]:
      islandVoxelCount = labelStatsT1WithRelabeledConnectedRegion.GetCount(currentLabel)
      print islandVoxelCount
      if islandVoxelCount <= self.maximumIslandVoxelCount:
        meanT1Intesity = labelStatsT1WithRelabeledConnectedRegion.GetMean(currentLabel)
        meanT2Intesity = labelStatsT2WithRelabeledConnectedRegion.GetMean(currentLabel)
        targetLabels = self.getTargetLabels(inputLabelImage, relabeledConnectedRegion, inputT1VolumeImage, currentLabel)
        print targetLabels
        diffDict = self.calculateLabelIntensityDifferenceValue(meanT1Intesity, meanT2Intesity,
                                                               targetLabels,
                                                               labelStatsT1WithInputLabelImage,
                                                               labelStatsT2WithInputLabelImage)
        print diffDict

  def thresholdAtlas(self, inputLabelImage):
    binaryThresholdImage = sitk.BinaryThreshold(inputLabelImage, self.label, self.label)
    connectedRegion = sitk.ConnectedComponent(binaryThresholdImage, True)
    relabeledConnectedRegion = sitk.RelabelComponent(connectedRegion)
    return relabeledConnectedRegion

  def getLabelStatsObject(self, volumeImage, labelImage):
    labelStatsObject = sitk.LabelStatisticsImageFilter()
    labelStatsObject.Execute(volumeImage, labelImage)

    return labelStatsObject

  def getLabelListFromLabelStatsObject(self, labelStatsObject):
    if sitk.Version().MajorVersion() > 0 or sitk.Version().MinorVersion() >= 9:
      compontentLabels = labelStatsObject.GetLabels()
    else: #if sitk version < 0.9 then use older function call GetValidLabels
      compontentLabels = labelStatsObject.GetValidLabels()
    return list(compontentLabels)

  def getTargetLabels(self, inputLabelImage, relabeledConnectedRegion, inputVolumeImage, currentLabel):
    currentLabelBinaryThresholdImage = sitk.BinaryThreshold(relabeledConnectedRegion, currentLabel, currentLabel)
    castedCurrentLabelBinaryThresholdImage = sitk.Cast(currentLabelBinaryThresholdImage, sitk.sitkInt16)

    dialatedBinaryLabelMap = self.dialateLabelMap(castedCurrentLabelBinaryThresholdImage)
    reducedLabelMapImage = sitk.Multiply(dialatedBinaryLabelMap, inputLabelImage)

    reducedLabelMapT1LabelStats = self.getLabelStatsObject(inputVolumeImage, reducedLabelMapImage)
    # targetLabels = reducedLabelMapT1LabelStats.GetLabels()
    targetLabels = self.getLabelListFromLabelStatsObject(reducedLabelMapT1LabelStats)
    return targetLabels

  def dialateLabelMap(self, inputLabelImage):
    myFilter = sitk.BinaryDilateImageFilter()
    myFilter.SetBackgroundValue(0.0)
    myFilter.SetBoundaryToForeground(False)
    myFilter.SetDebug(False)
    myFilter.SetForegroundValue(1.0)
    myFilter.SetKernelRadius((1, 1, 1))
    myFilter.SetKernelType(2)  # Kernel Type=Box
    myFilter.SetNumberOfThreads(8)
    output = myFilter.Execute(inputLabelImage)
    castedOutput = sitk.Cast(output, sitk.sitkInt16)

    return castedOutput

  def calculateLabelIntensityDifferenceValue(self, averageT1IntensitySuspiciousLabel,
                                             averageT2IntensitySuspiciousLabel,
                                             targetLabels, T1LabelStats, T2LabelStats):
    """
    Calculates a measurement for each label that is on the border of the suspicious label.
    This value is the square root of the sum of the squared difference in the average T1
    intensity values and the squared difference in the average T2 intensity values of the
    two islands in the comparison. The calculated value for each border label will later be
    sorted in ascending order - meaning that the smallest value has the "closest" average
    intensity to the suspicious label.
    """

    squareRootDiffLabelDict = dict()

    for targetLabel in targetLabels:
      # if targetLabel == 0:
      #   continue
      averageT1IntensityTargetLabel = T1LabelStats.GetMean(targetLabel)
      averageT2IntensityTargetLabel = T2LabelStats.GetMean(targetLabel)

      squareDiffAverageT1 = math.pow(averageT1IntensitySuspiciousLabel -
                                     averageT1IntensityTargetLabel, 2)
      squareDiffAverageT2 = math.pow(averageT2IntensitySuspiciousLabel -
                                     averageT2IntensityTargetLabel, 2)
      squareRootDiff = math.sqrt(squareDiffAverageT1 + squareDiffAverageT2)

      squareRootDiffLabelDict[int(targetLabel)] = squareRootDiff

    return squareRootDiffLabelDict

if __name__ == '__main__':
  from docopt import docopt
  arguments = docopt(__doc__)
  print arguments
  Object = DustCleanup(arguments)
  Object.main()
<|endoftext|>


<commit_before>#!/usr/bin/env python
"""Temporal segmentation utilities"""

import numpy as np
import scipy
import scipy.signal

import sklearn
import sklearn.cluster
import sklearn.feature_extraction

def stack_memory(data, n_steps=2, delay=1, trim=True):
    """Short-term history embedding.

    Each column ``data[:, i]`` is mapped to

    ``data[:,i] ->  [   data[:, i].T, data[:, i - delay].T ...  data[:, i - (n_steps-1)*delay].T ].T``

    :usage:
        >>> mfccs       = librosa.feature.mfcc(y=y, sr=sr)
        >>> mfcc_stack  = librosa.segment.stack_memory(mfccs)

    :parameters:
      - data : np.ndarray
          feature matrix (d-by-t)
      - n_steps : int > 0
          embedding dimension, the number of steps back in time to stack
      - delay : int > 0
          the number of columns to step
      - trim : bool
          Crop dimension to original number of columns

    :returns:
      - data_history : np.ndarray, shape=(d*m, t)
          data augmented with lagged copies of itself.

      .. note:: zeros are padded for the initial columns

    """

    t = data.shape[1]
    # Pad the end with zeros, which will roll to the front below
    data = np.pad(data, [(0, 0), (0, (n_steps-1) * delay)], mode='constant')

    history = data

    for i in range(1, n_steps):
        history = np.vstack([history, np.roll(data, i * delay, axis=1)])

    # Trim to original width
    if trim:
        history = history[:, :t]

    return history

def recurrence_matrix(data, k=None, width=1, metric='sqeuclidean', sym=False):
    '''Compute the binary recurrence matrix from a time-series.

    ``rec[i,j] == True`` <=> (``data[:,i]``, ``data[:,j]``) are k-nearest-neighbors and ``|i-j| >= width``

    :usage:
        >>> mfcc    = librosa.feature.mfcc(y=y, sr=sr)
        >>> R       = librosa.segment.recurrence_matrix(mfcc)

        >>> # Or fix the number of nearest neighbors to 5
        >>> R       = librosa.segment.recurrence_matrix(mfcc, k=5)

        >>> # Suppress neighbors within +- 7 samples
        >>> R       = librosa.segment.recurrence_matrix(mfcc, width=7)

        >>> # Use cosine similarity instead of Euclidean distance
        >>> R       = librosa.segment.recurrence_matrix(mfcc, metric='cosine')

        >>> # Require mutual nearest neighbors
        >>> R       = librosa.segment.recurrence_matrix(mfcc, sym=True)

    :parameters:
      - data : np.ndarray
          feature matrix (d-by-t)
      - k : int > 0 or None
          the number of nearest-neighbors for each sample
          Default: ``k = ceil(sqrt(t - 2 * width + 1))``
      - width : int > 0
          only link neighbors ``(data[:, i], data[:, j])`` if ``|i-j| >= width``
      - metric : see ``scipy.spatial.distance.pdist()``
          distance metric to use for nearest-neighbor calculation
      - sym : bool
          set ``sym=True`` to only link mutual nearest-neighbors

    :returns:
      - rec : np.ndarray, shape=(t,t), dtype=bool
          Binary recurrence matrix
    '''

    t = data.shape[1]

    if k is None:
        k = np.ceil(np.sqrt(t - 2 * width + 1))

    def _band_infinite():
        '''Suppress the diagonal+- of a distance matrix'''
        band       = np.empty( (t, t) )
        band[:]    = np.inf
        band[np.triu_indices_from(band, width)] = 0
        band[np.tril_indices_from(band, -width)] = 0

        return band

    # Build the distance matrix
    D = scipy.spatial.distance.squareform(
            scipy.spatial.distance.pdist(data.T, metric=metric))

    # Max out the diagonal band
    D = D + _band_infinite()

    # build the recurrence plot

    rec = np.zeros( (t, t), dtype=bool)

    # get the k nearest neighbors for each point
    for i in range(t):
        for j in np.argsort(D[i])[:k]:
            rec[i, j] = True

    # symmetrize
    if sym:
        rec = rec * rec.T

    return rec

def structure_feature(rec, pad=True, inverse=False):
    '''Compute the structure feature from a recurrence matrix.

    The i'th column of the recurrence matrix is shifted up by i.
    The resulting matrix is indexed horizontally by time,
    and vertically by lag.

    :usage:
        >>> # Build the structure feature over mfcc similarity
        >>> mfccs   = librosa.feature.mfcc(y=y, sr=sr)
        >>> R       = librosa.feature.recurrence_matrix(mfccs)
        >>> S       = librosa.feature.structure_feature(R)

        >>> # Invert the structure feature to get a recurrence matrix
        >>> R_hat   = librosa.feature.structure_feature(S, inverse=True)

    :parameters:
      - rec   : np.ndarray, shape=(t,t)
          recurrence matrix (see `librosa.segment.recurrence_matrix`)

      - pad : bool
          Pad the matrix with t rows of zeros to avoid looping.

      - inverse : bool
          Unroll the opposite direction. This is useful for converting
          structure features back into recurrence plots.

          .. note: Reversing with ``pad==True`` will truncate the inferred padding.

    :returns:
      - struct : np.ndarray
          ``struct[i, t]`` = the recurrence at time ``t`` with lag ``i``.

      .. note:: negative lag values are supported by wrapping to the end of the array.
    '''

    t = rec.shape[1]

    if pad and not inverse:
        # If we don't assume that the signal loops,
        # stack zeros underneath in the recurrence plot.
        struct = np.pad(rec, [(0, t), (0, 0)], mode='constant')
    else:
        struct = rec.copy()

    if inverse:
        direction = +1
    else:
        direction = -1

    for i in range(1, t):
        struct[:, i] = np.roll(struct[:, i], direction * i, axis=-1)

    if inverse and pad:
        struct = struct[:t]

    return struct

def agglomerative(data, k):
    """Bottom-up temporal segmentation.

    Use a temporally-constrained agglomerative clustering routine to partition
    ``data`` into ``k`` contiguous segments.

    :usage:
        >>> # Cluster by Mel spectrogram similarity
        >>> # Break into 32 segments
        >>> S                   = librosa.feature.melspectrogram(y=y, sr=sr, n_fft=2048, hop_length=512)
        >>> boundary_frames     = librosa.segment.agglomerative(S, 32)
        >>> boundary_times      = librosa.frames_to_time(boundary_frames, sr=sr, hop_length=512)

    :parameters:
      - data     : np.ndarray
          feature matrix (d-by-t)

      - k        : int > 0
          number of segments to produce

    :returns:
      - boundaries : np.ndarray, shape=(k,1)
          left-boundaries (frame numbers) of detected segments

    """

    # Connect the temporal connectivity graph
    grid = sklearn.feature_extraction.image.grid_to_graph(  n_x=data.shape[1],
                                                            n_y=1,
                                                            n_z=1)

    # Instantiate the clustering object
    ward = sklearn.cluster.Ward(n_clusters=k, connectivity=grid)

    # Fit the model
    ward.fit(data.T)

    # Find the change points from the labels
    boundaries = [0]
    boundaries.extend(
        list(1 + np.nonzero(np.diff(ward.labels_))[0].astype(int)))
    return boundaries

<commit_msg>inted the knn index<commit_after>#!/usr/bin/env python
"""Temporal segmentation utilities"""

import numpy as np
import scipy
import scipy.signal

import sklearn
import sklearn.cluster
import sklearn.feature_extraction

def stack_memory(data, n_steps=2, delay=1, trim=True):
    """Short-term history embedding.

    Each column ``data[:, i]`` is mapped to

    ``data[:,i] ->  [   data[:, i].T, data[:, i - delay].T ...  data[:, i - (n_steps-1)*delay].T ].T``

    :usage:
        >>> mfccs       = librosa.feature.mfcc(y=y, sr=sr)
        >>> mfcc_stack  = librosa.segment.stack_memory(mfccs)

    :parameters:
      - data : np.ndarray
          feature matrix (d-by-t)
      - n_steps : int > 0
          embedding dimension, the number of steps back in time to stack
      - delay : int > 0
          the number of columns to step
      - trim : bool
          Crop dimension to original number of columns

    :returns:
      - data_history : np.ndarray, shape=(d*m, t)
          data augmented with lagged copies of itself.

      .. note:: zeros are padded for the initial columns

    """

    t = data.shape[1]
    # Pad the end with zeros, which will roll to the front below
    data = np.pad(data, [(0, 0), (0, (n_steps-1) * delay)], mode='constant')

    history = data

    for i in range(1, n_steps):
        history = np.vstack([history, np.roll(data, i * delay, axis=1)])

    # Trim to original width
    if trim:
        history = history[:, :t]

    return history

def recurrence_matrix(data, k=None, width=1, metric='sqeuclidean', sym=False):
    '''Compute the binary recurrence matrix from a time-series.

    ``rec[i,j] == True`` <=> (``data[:,i]``, ``data[:,j]``) are k-nearest-neighbors and ``|i-j| >= width``

    :usage:
        >>> mfcc    = librosa.feature.mfcc(y=y, sr=sr)
        >>> R       = librosa.segment.recurrence_matrix(mfcc)

        >>> # Or fix the number of nearest neighbors to 5
        >>> R       = librosa.segment.recurrence_matrix(mfcc, k=5)

        >>> # Suppress neighbors within +- 7 samples
        >>> R       = librosa.segment.recurrence_matrix(mfcc, width=7)

        >>> # Use cosine similarity instead of Euclidean distance
        >>> R       = librosa.segment.recurrence_matrix(mfcc, metric='cosine')

        >>> # Require mutual nearest neighbors
        >>> R       = librosa.segment.recurrence_matrix(mfcc, sym=True)

    :parameters:
      - data : np.ndarray
          feature matrix (d-by-t)
      - k : int > 0 or None
          the number of nearest-neighbors for each sample
          Default: ``k = ceil(sqrt(t - 2 * width + 1))``
      - width : int > 0
          only link neighbors ``(data[:, i], data[:, j])`` if ``|i-j| >= width``
      - metric : see ``scipy.spatial.distance.pdist()``
          distance metric to use for nearest-neighbor calculation
      - sym : bool
          set ``sym=True`` to only link mutual nearest-neighbors

    :returns:
      - rec : np.ndarray, shape=(t,t), dtype=bool
          Binary recurrence matrix
    '''

    t = data.shape[1]

    if k is None:
        k = np.ceil(np.sqrt(t - 2 * width + 1))

    k = int(k)

    def _band_infinite():
        '''Suppress the diagonal+- of a distance matrix'''
        band       = np.empty( (t, t) )
        band[:]    = np.inf
        band[np.triu_indices_from(band, width)] = 0
        band[np.tril_indices_from(band, -width)] = 0

        return band

    # Build the distance matrix
    D = scipy.spatial.distance.squareform(
            scipy.spatial.distance.pdist(data.T, metric=metric))

    # Max out the diagonal band
    D = D + _band_infinite()

    # build the recurrence plot

    rec = np.zeros( (t, t), dtype=bool)

    # get the k nearest neighbors for each point
    for i in range(t):
        for j in np.argsort(D[i])[:k]:
            rec[i, j] = True

    # symmetrize
    if sym:
        rec = rec * rec.T

    return rec

def structure_feature(rec, pad=True, inverse=False):
    '''Compute the structure feature from a recurrence matrix.

    The i'th column of the recurrence matrix is shifted up by i.
    The resulting matrix is indexed horizontally by time,
    and vertically by lag.

    :usage:
        >>> # Build the structure feature over mfcc similarity
        >>> mfccs   = librosa.feature.mfcc(y=y, sr=sr)
        >>> R       = librosa.feature.recurrence_matrix(mfccs)
        >>> S       = librosa.feature.structure_feature(R)

        >>> # Invert the structure feature to get a recurrence matrix
        >>> R_hat   = librosa.feature.structure_feature(S, inverse=True)

    :parameters:
      - rec   : np.ndarray, shape=(t,t)
          recurrence matrix (see `librosa.segment.recurrence_matrix`)

      - pad : bool
          Pad the matrix with t rows of zeros to avoid looping.

      - inverse : bool
          Unroll the opposite direction. This is useful for converting
          structure features back into recurrence plots.

          .. note: Reversing with ``pad==True`` will truncate the inferred padding.

    :returns:
      - struct : np.ndarray
          ``struct[i, t]`` = the recurrence at time ``t`` with lag ``i``.

      .. note:: negative lag values are supported by wrapping to the end of the array.
    '''

    t = rec.shape[1]

    if pad and not inverse:
        # If we don't assume that the signal loops,
        # stack zeros underneath in the recurrence plot.
        struct = np.pad(rec, [(0, t), (0, 0)], mode='constant')
    else:
        struct = rec.copy()

    if inverse:
        direction = +1
    else:
        direction = -1

    for i in range(1, t):
        struct[:, i] = np.roll(struct[:, i], direction * i, axis=-1)

    if inverse and pad:
        struct = struct[:t]

    return struct

def agglomerative(data, k):
    """Bottom-up temporal segmentation.

    Use a temporally-constrained agglomerative clustering routine to partition
    ``data`` into ``k`` contiguous segments.

    :usage:
        >>> # Cluster by Mel spectrogram similarity
        >>> # Break into 32 segments
        >>> S                   = librosa.feature.melspectrogram(y=y, sr=sr, n_fft=2048, hop_length=512)
        >>> boundary_frames     = librosa.segment.agglomerative(S, 32)
        >>> boundary_times      = librosa.frames_to_time(boundary_frames, sr=sr, hop_length=512)

    :parameters:
      - data     : np.ndarray
          feature matrix (d-by-t)

      - k        : int > 0
          number of segments to produce

    :returns:
      - boundaries : np.ndarray, shape=(k,1)
          left-boundaries (frame numbers) of detected segments

    """

    # Connect the temporal connectivity graph
    grid = sklearn.feature_extraction.image.grid_to_graph(  n_x=data.shape[1],
                                                            n_y=1,
                                                            n_z=1)

    # Instantiate the clustering object
    ward = sklearn.cluster.Ward(n_clusters=k, connectivity=grid)

    # Fit the model
    ward.fit(data.T)

    # Find the change points from the labels
    boundaries = [0]
    boundaries.extend(
        list(1 + np.nonzero(np.diff(ward.labels_))[0].astype(int)))
    return boundaries

<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

"""Tests of sparse matrix structures."""

#------------------------------------------------------------------------------
# Imports
#------------------------------------------------------------------------------

from numpy.testing import assert_array_equal
from pytest import raises

from ....ext.six import itervalues, iterkeys
from ..cluster_metadata import _cluster_info, ClusterMetadata

#------------------------------------------------------------------------------
# Tests
#------------------------------------------------------------------------------

def test_structure():
    """Test the structure holding all cluster metadata."""
    data = _cluster_info([('a', 1), ('b', 2)])

    assert isinstance(data[3], dict)
    assert data[3]['a'] == 1
    assert data[3]['b'] == 2

    data[3]['b'] = 10
    assert data[3]['b'] == 10

    with raises(KeyError):
        data[3]['c']

def test_default_function():
    meta = ClusterMetadata([('field', lambda: 9)])
    assert meta.data[3]['field'] == 9

def test_cluster_metadata():
    meta = ClusterMetadata()
    assert meta.data is not None

    assert meta[0]['group'] is not None

    assert meta[2]['color'] == 1
    assert meta[2]['group'] == 3

    assert meta[10]['color'] == 1
    assert meta[10]['group'] == 3

    meta.set([10], 'color', 5)
    assert meta[10]['color'] == 5

    # Alternative __setitem__ syntax.
    meta.set([10, 11], 'color', 5)
    assert meta[10]['color'] == 5
    assert meta[11]['color'] == 5

    meta.set([10, 11], 'color', [6, 7])
    assert meta[10]['color'] == 6
    assert meta.data[11]['color'] == 7

    meta[10]['color'] = 10
    assert meta[10]['color'] == 10

def test_metadata_history():
    """Test ClusterMetadata history."""

    data = {2: {'group': 2, 'color': 7}, 4: {'group': 5}}

    meta = ClusterMetadata(data=data)

    # Values set in 'data'.
    assert meta.get(2, 'group') == 2
    assert meta.get(2, 'color') == 7

    # Default values.
    assert meta.get(3, 'group') == 3
    assert meta.get(3, 'color') == 1

    assert meta.get(4, 'group') == 5
    assert meta.get(4, 'color') == 1

    ###########

    meta.undo()
    meta.redo()

    # Action 1.
    meta.set(2, 'group', 20)
    assert meta.get(2, 'group') == 20

    # Action 2.
    meta.set(3, 'color', 30)
    assert meta.get(3, 'color') == 30

    # Action 3.
    meta.set(2, 'color', 40)
    assert meta.get(2, 'color') == 40

    ###########

    # Undo 3.
    meta.undo()
    assert meta.get(2, 'color') == 7

    # Undo 2.
    meta.undo()
    assert meta.get(3, 'color') == 1

    # Redo 2.
    meta.redo()
    assert meta.get(3, 'color') == 30
    assert meta.get(2, 'group') == 20

    # Undo 2.
    meta.undo()
    # Undo 1.
    meta.undo()
    assert meta.get(2, 'group') == 2

    meta.undo()
    meta.undo()
<commit_msg>Test UpdateInfo for cluster metadata.<commit_after># -*- coding: utf-8 -*-

"""Tests of sparse matrix structures."""

#------------------------------------------------------------------------------
# Imports
#------------------------------------------------------------------------------

from numpy.testing import assert_array_equal
from pytest import raises

from ....ext.six import itervalues, iterkeys
from ..cluster_metadata import _cluster_info, ClusterMetadata

#------------------------------------------------------------------------------
# Tests
#------------------------------------------------------------------------------

def test_structure():
    """Test the structure holding all cluster metadata."""
    data = _cluster_info([('a', 1), ('b', 2)])

    assert isinstance(data[3], dict)
    assert data[3]['a'] == 1
    assert data[3]['b'] == 2

    data[3]['b'] = 10
    assert data[3]['b'] == 10

    with raises(KeyError):
        data[3]['c']

def test_default_function():
    meta = ClusterMetadata([('field', lambda: 9)])
    assert meta.data[3]['field'] == 9

def test_cluster_metadata():
    meta = ClusterMetadata()
    assert meta.data is not None

    assert meta[0]['group'] is not None

    assert meta[2]['color'] == 1
    assert meta[2]['group'] == 3

    assert meta[10]['color'] == 1
    assert meta[10]['group'] == 3

    info = meta.set([10], 'color', 5)
    assert meta[10]['color'] == 5
    assert info.description == 'color'
    assert info.metadata_changed == [10]

    # Alternative __setitem__ syntax.
    info = meta.set([10, 11], 'color', 5)
    assert meta[10]['color'] == 5
    assert meta[11]['color'] == 5
    assert info.description == 'color'
    assert info.metadata_changed == [10, 11]

    info = meta.set([10, 11], 'color', [6, 7])
    assert meta[10]['color'] == 6
    assert meta.data[11]['color'] == 7
    assert info.description == 'color'
    assert info.metadata_changed == [10, 11]

    meta[10]['color'] = 10
    assert meta[10]['color'] == 10

def test_metadata_history():
    """Test ClusterMetadata history."""

    data = {2: {'group': 2, 'color': 7}, 4: {'group': 5}}

    meta = ClusterMetadata(data=data)

    # Values set in 'data'.
    assert meta.get(2, 'group') == 2
    assert meta.get(2, 'color') == 7

    # Default values.
    assert meta.get(3, 'group') == 3
    assert meta.get(3, 'color') == 1

    assert meta.get(4, 'group') == 5
    assert meta.get(4, 'color') == 1

    ###########

    meta.undo()
    meta.redo()

    # Action 1.
    info = meta.set(2, 'group', 20)
    assert meta.get(2, 'group') == 20
    assert info.description == 'group'
    assert info.metadata_changed == [2]

    # Action 2.
    info = meta.set(3, 'color', 30)
    assert meta.get(3, 'color') == 30
    assert info.description == 'color'
    assert info.metadata_changed == [3]

    # Action 3.
    info = meta.set(2, 'color', 40)
    assert meta.get(2, 'color') == 40
    assert info.description == 'color'
    assert info.metadata_changed == [2]

    ###########

    # Undo 3.
    info = meta.undo()
    assert meta.get(2, 'color') == 7
    assert info.description == 'color'
    assert info.metadata_changed == [2]

    # Undo 2.
    info = meta.undo()
    assert meta.get(3, 'color') == 1
    assert info.description == 'color'
    assert info.metadata_changed == [3]

    # Redo 2.
    info = meta.redo()
    assert meta.get(3, 'color') == 30
    assert meta.get(2, 'group') == 20
    assert info.description == 'color'
    assert info.metadata_changed == [3]

    # Undo 2.
    info = meta.undo()
    assert info.description == 'color'
    assert info.metadata_changed == [3]

    # Undo 1.
    info = meta.undo()
    assert meta.get(2, 'group') == 2
    assert info.description == 'group'
    assert info.metadata_changed == [2]

    info = meta.undo()
    assert info is None

    info = meta.undo()
    assert info is None
<|endoftext|>


<commit_before>"""
Command-line interface for asana-hub.
"""

import argparse
import logging
import sys
import os
import traceback

try:
    from asana import Client
    from github import Github

    import urllib3
    import certifi

    # Setup pool manager and SSL verification.
    http = urllib3.PoolManager(
        cert_reqs='CERT_REQUIRED', # Force certificate check.
        ca_certs=certifi.where(),  # Path to the Certifi bundle.
    )

    try:
        import urllib3.contrib.pyopenssl
        urllib3.contrib.pyopenssl.inject_into_urllib3()
    except:
        logging.debug("pyopenssl not detected.\n"
            "to install pyopenssl: "
            "pip install pyopenssl ndg-httpsclient pyasn1")

except ImportError:
    raise Exception("Could not import required packages.\n"
        "Did you pip install -r requirements.txt ?")

from .json_data import JSONData
from .action import Action

class ToolApp(object):

    def authenticate(self):
        """Connects to Github and Asana and authenticates via OAuth."""
        if self.oauth:
            return False

        # Save asana.
        self.settings.apply('api-asana', self.args.asana_api,
            "enter asana api key")

        # Save github.com
        self.settings.apply('api-github', self.args.github_api,
            "enter github.com token")

        logging.debug("authenticating asana api.")
        self.asana = Client.basic_auth(self.settings['api-asana'])
        self.asana_me = self.asana.users.me()
        logging.debug("authenticating github api")
        self.github = Github(self.settings['api-github'])
        self.github_user = self.github.get_user()

        self.oauth = True

    @classmethod
    def _list_select(cls, lst, prompt, offset=0):
        """Given a list of values and names, accepts the index value or name."""

        inp = raw_input("select %s: " % prompt)
        assert inp, "value required."

        try:
            return lst[int(inp)+offset]
        except ValueError:
            return inp
        except IndexError:
            assert False, "bad value."

    def prompt_repo(self):

        # Select org
        orgs = [None]
        print "0) %s [you]" % self.github_user.name
        for idx, org in enumerate(self.github_user.get_orgs()):
            orgs.append(org)
            print "%d) %s" % (idx+1, org.name)

        org = self._list_select(orgs, "org")
        ctx = org or self.github_user

        repos = []
        for idx, repo in enumerate(ctx.get_repos()):
            repos.append(repo)
            print "%d) %s" % (idx, repo.name)

        repo = self._list_select(repos, "repo")
        logging.debug("repo id: %d", repo.id)

        return repo

    def prompt_project(self):

        # Select workspace
        workspaces = []
        for idx, workspace in enumerate(self.asana_me['workspaces']):
            workspaces.append(workspace)
            print "%d) %s" % (idx, workspace['name'])

        workspace = self._list_select(workspaces, "workspace")

        # Select workspace
        as_projects = self.asana.projects.find_by_workspace(workspace['id'],
            iterator_type=None)

        projects = []

        for idx, project in enumerate(as_projects):
            projects.append(project)
            print "%d) %s" % (idx, project['name'])

        project = self._list_select(projects, "project")

        return project

    @classmethod
    def make_asana_url(cls, project_id, task_id):
        """Returns a URL to an asana task."""
        return "https://app.asana.com/0/%d/%d" % (project_id, task_id)

    @classmethod
    def _issue_data_key(cls, namespace):
        """Returns key for issue_data in data."""
        return 'issue_data_%s' % namespace

    def save_issue_data_task(self, issue, task, namespace='open'):
        """Saves a issue data (tasks, etc.) to local data.

        Args:
            issue:
                `int`. Github issue number.
            task:
                `int`. Asana task ID.
            namespace:
                `str`. Namespace for storing this issue.
        """

        if isinstance(issue, int):
            issue_number = str(issue)
        elif isinstance(issue, basestring):
            issue_number = issue
        else:
            issue_number = issue.number

        issue_data = self.get_saved_issue_data(issue, namespace)

        if not issue_data.has_key("tasks"):
            issue_data[issue_number] = [task]
        elif task not in issue_data['tasks']:
            issue_data[issue_number].append(task)

    def has_saved_issue_data(self, issue, namespace='open'):
        issue_data_key = self._issue_data_key(namespace)
        issue_data = self.data.get(issue_data_key,
            {})

        if isinstance(issue, int):
            issue_number = str(issue)
        elif isinstance(issue, basestring):
            issue_number = issue
        else:
            issue_number = issue.number

        return issue_data.has_key(str(issue_number))

    def get_saved_issue_data(self, issue, namespace='open'):
        """Returns issue data from local data.

        Args:
            issue:
                `int`. Github issue number.
            namespace:
                `str`. Namespace for storing this issue.
        """

        if isinstance(issue, int):
            issue_number = str(issue)
        elif isinstance(issue, basestring):
            issue_number = issue
        else:
            issue_number = issue.number

        issue_data_key = self._issue_data_key(namespace)
        issue_data = self.data.get(issue_data_key,
            {})

        _data = issue_data.get(str(issue_number), {})
        issue_data[str(issue_number)] = _data
        return _data

    def move_saved_issue_data(self, issue, ns, other_ns):
        """Moves an issue_data from one namespace to another."""

        if isinstance(issue, int):
            issue_number = str(issue)
        elif isinstance(issue, basestring):
            issue_number = issue
        else:
            issue_number = issue.number

        issue_data_key = self._issue_data_key(ns)
        other_issue_data_key = self._issue_data_key(other_ns)
        issue_data = self.data.get(issue_data_key,
            {})
        other_issue_data = self.data.get(other_issue_data_key,
            {})

        _id = issue_data.pop(issue_number, None)
        if _id:
            other_issue_data[issue_number] = _id

        self.data[other_issue_data_key] = other_issue_data
        self.data[issue_data_key] = issue_data

    def __init__(self, version):
        """Accepts version of the app."""

        # Setup settings
        self.version = version
        self.exit_code = 999
        self.oauth = False

        # Setup logging
        self.logger = logging.getLogger()
        formatter = logging.Formatter("%(message)s")
        self.logger.setLevel(logging.DEBUG)
        self.logger.propagate = False
        self.logger.handlers = []
        ch = logging.StreamHandler()
        ch.setLevel(logging.INFO)
        ch.setFormatter(formatter)
        self.logger.addHandler(ch)

        parser = argparse.ArgumentParser(description='Loudr Utility Tool')

        # Default paths for settings and data
        def_settings_file = os.path.join(
            os.path.expanduser("~"),
            '.asana-hub',
            )

        def_data_file = os.path.expanduser("./.asana-hub.proj")

        # Load actions
        actions = {}
        choices = []
        help_msgs = ''

        for action in Action.iter_actions():
            actions[action.name] = action
            choices.append(action.name)
            help_msgs += '\n-  %s: %s' % (action.name, action.__doc__)

        help_msgs += '\n'

        parser.add_argument(
            'action',
            action='store',
            nargs=1,
            help=help_msgs,
            choices=choices,
            )

        parser.add_argument(
            '-vv', '--verbose',
            action='store_true',
            dest='verbose',
            help="use debugging verbosity.",
            )

        parser.add_argument(
            '-s', '--settings-file',
            action='store',
            nargs='?',
            dest='settings_file',
            default=def_settings_file,
            help="path to save oauth api keys.",
            )

        parser.add_argument(
            '-d', '--data-file',
            action='store',
            nargs='?',
            dest='data_file',
            default=def_data_file,
            help="path to save repository and project based data.",
            )

        parser.add_argument(
            '--asana-api',
            action='store',
            nargs='?',
            const='',
            dest='asana_api',
            help="asana api key.",
            )

        parser.add_argument(
            '--github-token',
            action='store',
            nargs='?',
            const='',
            dest='github_api',
            help="github api token.",
            )

        # Add action arguments.
        for action in actions.values():
            action.add_arguments(parser)

        parser.add_argument('-v', '--version', action='version',
            version='%(prog)s ' + '%s' % version)

        self.args = parser.parse_args()

        if len(sys.argv) < 2:
            parser.print_help()
            self.exit_code = 1
            return

        if self.args.verbose:
            ch.setLevel(logging.DEBUG)

        # Load settings
        self.settings = JSONData(filename=self.args.settings_file,
            args=self.args, version=version)

        self.data = JSONData(filename=self.args.data_file,
            args=self.args, version=version)

        # Load action method and call.
        try:
            action_name = self.args.action[0]
            action_class = actions.get(action_name)
            if not action_class:
                raise NotImplementedError(
                    "%s is not implemented." % action_name)

            # Instantiate and run
            action = action_class(app=self, args=self.args)
            action.run()
        except AssertionError as exc:
            logging.error("Error: %s", unicode(exc))
            logging.debug("%s", traceback.format_exc())
            self.exit_code = 1
            return
        except Exception as exc:
            logging.exception("Exception: %r", exc)
            self.exit_code = 129
            return
        finally:

            # Save settings
            self.settings.save()
            # Save data
            self.data.save()

        self.exit_code = 0

<commit_msg>restore tasks lookup<commit_after>"""
Command-line interface for asana-hub.
"""

import argparse
import logging
import sys
import os
import traceback

try:
    from asana import Client
    from github import Github

    import urllib3
    import certifi

    # Setup pool manager and SSL verification.
    http = urllib3.PoolManager(
        cert_reqs='CERT_REQUIRED', # Force certificate check.
        ca_certs=certifi.where(),  # Path to the Certifi bundle.
    )

    try:
        import urllib3.contrib.pyopenssl
        urllib3.contrib.pyopenssl.inject_into_urllib3()
    except:
        logging.debug("pyopenssl not detected.\n"
            "to install pyopenssl: "
            "pip install pyopenssl ndg-httpsclient pyasn1")

except ImportError:
    raise Exception("Could not import required packages.\n"
        "Did you pip install -r requirements.txt ?")

from .json_data import JSONData
from .action import Action

class ToolApp(object):

    def authenticate(self):
        """Connects to Github and Asana and authenticates via OAuth."""
        if self.oauth:
            return False

        # Save asana.
        self.settings.apply('api-asana', self.args.asana_api,
            "enter asana api key")

        # Save github.com
        self.settings.apply('api-github', self.args.github_api,
            "enter github.com token")

        logging.debug("authenticating asana api.")
        self.asana = Client.basic_auth(self.settings['api-asana'])
        self.asana_me = self.asana.users.me()
        logging.debug("authenticating github api")
        self.github = Github(self.settings['api-github'])
        self.github_user = self.github.get_user()

        self.oauth = True

    @classmethod
    def _list_select(cls, lst, prompt, offset=0):
        """Given a list of values and names, accepts the index value or name."""

        inp = raw_input("select %s: " % prompt)
        assert inp, "value required."

        try:
            return lst[int(inp)+offset]
        except ValueError:
            return inp
        except IndexError:
            assert False, "bad value."

    def prompt_repo(self):

        # Select org
        orgs = [None]
        print "0) %s [you]" % self.github_user.name
        for idx, org in enumerate(self.github_user.get_orgs()):
            orgs.append(org)
            print "%d) %s" % (idx+1, org.name)

        org = self._list_select(orgs, "org")
        ctx = org or self.github_user

        repos = []
        for idx, repo in enumerate(ctx.get_repos()):
            repos.append(repo)
            print "%d) %s" % (idx, repo.name)

        repo = self._list_select(repos, "repo")
        logging.debug("repo id: %d", repo.id)

        return repo

    def prompt_project(self):

        # Select workspace
        workspaces = []
        for idx, workspace in enumerate(self.asana_me['workspaces']):
            workspaces.append(workspace)
            print "%d) %s" % (idx, workspace['name'])

        workspace = self._list_select(workspaces, "workspace")

        # Select workspace
        as_projects = self.asana.projects.find_by_workspace(workspace['id'],
            iterator_type=None)

        projects = []

        for idx, project in enumerate(as_projects):
            projects.append(project)
            print "%d) %s" % (idx, project['name'])

        project = self._list_select(projects, "project")

        return project

    @classmethod
    def make_asana_url(cls, project_id, task_id):
        """Returns a URL to an asana task."""
        return "https://app.asana.com/0/%d/%d" % (project_id, task_id)

    @classmethod
    def _issue_data_key(cls, namespace):
        """Returns key for issue_data in data."""
        return 'issue_data_%s' % namespace

    def save_issue_data_task(self, issue, task, namespace='open'):
        """Saves a issue data (tasks, etc.) to local data.

        Args:
            issue:
                `int`. Github issue number.
            task:
                `int`. Asana task ID.
            namespace:
                `str`. Namespace for storing this issue.
        """

        if isinstance(issue, int):
            issue_number = str(issue)
        elif isinstance(issue, basestring):
            issue_number = issue
        else:
            issue_number = issue.number

        issue_data = self.get_saved_issue_data(issue, namespace)

        if not issue_data.has_key(issue_number):
            issue_data[issue_number] = [task]
        elif task not in issue_data[issue_number]:
            issue_data[issue_number].append(task)

    def has_saved_issue_data(self, issue, namespace='open'):
        issue_data_key = self._issue_data_key(namespace)
        issue_data = self.data.get(issue_data_key,
            {})

        if isinstance(issue, int):
            issue_number = str(issue)
        elif isinstance(issue, basestring):
            issue_number = issue
        else:
            issue_number = issue.number

        return issue_data.has_key(str(issue_number))

    def get_saved_issue_data(self, issue, namespace='open'):
        """Returns issue data from local data.

        Args:
            issue:
                `int`. Github issue number.
            namespace:
                `str`. Namespace for storing this issue.
        """

        if isinstance(issue, int):
            issue_number = str(issue)
        elif isinstance(issue, basestring):
            issue_number = issue
        else:
            issue_number = issue.number

        issue_data_key = self._issue_data_key(namespace)
        issue_data = self.data.get(issue_data_key,
            {})

        _data = issue_data.get(str(issue_number), {})
        issue_data[str(issue_number)] = _data
        return _data

    def move_saved_issue_data(self, issue, ns, other_ns):
        """Moves an issue_data from one namespace to another."""

        if isinstance(issue, int):
            issue_number = str(issue)
        elif isinstance(issue, basestring):
            issue_number = issue
        else:
            issue_number = issue.number

        issue_data_key = self._issue_data_key(ns)
        other_issue_data_key = self._issue_data_key(other_ns)
        issue_data = self.data.get(issue_data_key,
            {})
        other_issue_data = self.data.get(other_issue_data_key,
            {})

        _id = issue_data.pop(issue_number, None)
        if _id:
            other_issue_data[issue_number] = _id

        self.data[other_issue_data_key] = other_issue_data
        self.data[issue_data_key] = issue_data

    def __init__(self, version):
        """Accepts version of the app."""

        # Setup settings
        self.version = version
        self.exit_code = 999
        self.oauth = False

        # Setup logging
        self.logger = logging.getLogger()
        formatter = logging.Formatter("%(message)s")
        self.logger.setLevel(logging.DEBUG)
        self.logger.propagate = False
        self.logger.handlers = []
        ch = logging.StreamHandler()
        ch.setLevel(logging.INFO)
        ch.setFormatter(formatter)
        self.logger.addHandler(ch)

        parser = argparse.ArgumentParser(description='Loudr Utility Tool')

        # Default paths for settings and data
        def_settings_file = os.path.join(
            os.path.expanduser("~"),
            '.asana-hub',
            )

        def_data_file = os.path.expanduser("./.asana-hub.proj")

        # Load actions
        actions = {}
        choices = []
        help_msgs = ''

        for action in Action.iter_actions():
            actions[action.name] = action
            choices.append(action.name)
            help_msgs += '\n-  %s: %s' % (action.name, action.__doc__)

        help_msgs += '\n'

        parser.add_argument(
            'action',
            action='store',
            nargs=1,
            help=help_msgs,
            choices=choices,
            )

        parser.add_argument(
            '-vv', '--verbose',
            action='store_true',
            dest='verbose',
            help="use debugging verbosity.",
            )

        parser.add_argument(
            '-s', '--settings-file',
            action='store',
            nargs='?',
            dest='settings_file',
            default=def_settings_file,
            help="path to save oauth api keys.",
            )

        parser.add_argument(
            '-d', '--data-file',
            action='store',
            nargs='?',
            dest='data_file',
            default=def_data_file,
            help="path to save repository and project based data.",
            )

        parser.add_argument(
            '--asana-api',
            action='store',
            nargs='?',
            const='',
            dest='asana_api',
            help="asana api key.",
            )

        parser.add_argument(
            '--github-token',
            action='store',
            nargs='?',
            const='',
            dest='github_api',
            help="github api token.",
            )

        # Add action arguments.
        for action in actions.values():
            action.add_arguments(parser)

        parser.add_argument('-v', '--version', action='version',
            version='%(prog)s ' + '%s' % version)

        self.args = parser.parse_args()

        if len(sys.argv) < 2:
            parser.print_help()
            self.exit_code = 1
            return

        if self.args.verbose:
            ch.setLevel(logging.DEBUG)

        # Load settings
        self.settings = JSONData(filename=self.args.settings_file,
            args=self.args, version=version)

        self.data = JSONData(filename=self.args.data_file,
            args=self.args, version=version)

        # Load action method and call.
        try:
            action_name = self.args.action[0]
            action_class = actions.get(action_name)
            if not action_class:
                raise NotImplementedError(
                    "%s is not implemented." % action_name)

            # Instantiate and run
            action = action_class(app=self, args=self.args)
            action.run()
        except AssertionError as exc:
            logging.error("Error: %s", unicode(exc))
            logging.debug("%s", traceback.format_exc())
            self.exit_code = 1
            return
        except Exception as exc:
            logging.exception("Exception: %r", exc)
            self.exit_code = 129
            return
        finally:

            # Save settings
            self.settings.save()
            # Save data
            self.data.save()

        self.exit_code = 0

<|endoftext|>


<commit_before>import numpy as np
from IPython.display import display
import pandas as pd
import matplotlib.pyplot as plt

def compute_error(x_obj,x_approx):
    """
    Relative or absolute error between x_obj and x_approx.
    """
    if np.linalg.norm(x_obj) > np.nextafter(0,1):
        Err=np.linalg.norm(x_obj-x_approx)/np.linalg.norm(x_obj)
    else:
        Err=np.linalg.norm(x_obj-x_approx)
    return Err
def print_iterations(data, columns):
    """
    Auxiliary function to print table with list of named columns
    and values in data dictionary.
    """
    pd.set_option('display.float_format', lambda x: "%.2e" % x)
    df = pd.DataFrame.from_dict(data, orient='index',
                                 columns=columns)
    display(df)
def plot_error_of_optimum_value(err):
    """
    Auxiliary function for plotting inner iterations error.
    """
    plt.yscale('log') #logarithmic scale for y axis
    plt.plot(np.arange(err.size),err,'.-')
    plt.title("Error of optimum value between $f_o(x^{(k)})$ and $p^*$")
    plt.ylabel("log error",size=12)
    plt.xlabel("Inner iterations",size=12)
    plt.grid()
    plt.show()
def plot_sequence_of_approximations(x_plot):
    """
    Auxiliar function for plotting sequence of approximations.
    """
    plt.plot(x_plot[0,:],x_plot[1,:],"-*")
    plt.ylabel("$x_2$")
    plt.xlabel("$x_1$")
    plt.annotate("$x^{(0)}$",(x_plot[0,0],x_plot[1,0]),fontsize=12)
    plt.title("Descent method sequence of approximations")
    plt.grid()
    plt.show()<commit_msg>case when BL method is used<commit_after>import numpy as np
from IPython.display import display
import pandas as pd
import matplotlib.pyplot as plt

def compute_error(x_obj,x_approx):
    """
    Relative or absolute error between x_obj and x_approx.
    """
    if np.linalg.norm(x_obj) > np.nextafter(0,1):
        Err=np.linalg.norm(x_obj-x_approx)/np.linalg.norm(x_obj)
    else:
        Err=np.linalg.norm(x_obj-x_approx)
    return Err
def print_iterations(data, columns):
    """
    Auxiliary function to print table with list of named columns
    and values in data dictionary.
    """
    pd.set_option('display.float_format', lambda x: "%.2e" % x)
    df = pd.DataFrame.from_dict(data, orient='index',
                                 columns=columns)
    display(df)
def plot_error_of_optimum_value(err):
    """
    Auxiliary function for plotting inner iterations error.
    """
    plt.yscale('log') #logarithmic scale for y axis
    plt.plot(np.arange(err.size),err,'.-')
    plt.title("Error of optimum value between $f_o(x^{(k)})$ and $p^*$")
    plt.ylabel("log error",size=12)
    plt.xlabel("Inner iterations",size=12)
    plt.grid()
    plt.show()
def plot_sequence_of_approximations(x_iterations,
                                   title="Descent method sequence of approximations"):
    """
    Auxiliar function for plotting sequence of approximations.
    """
    plt.plot(x_iterations[0,:],
             x_iterations[1,:],"-*")
    plt.ylabel("$x_2$")
    plt.xlabel("$x_1$")
    plt.annotate("$x^{(0)}$",(x_iterations[0,0],
                              x_iterations[1,0]),fontsize=12)
    plt.title(title)
    if title == "Primal-dual BL method sequence of approximations":
        plt.legend(["Central path"], bbox_to_anchor=(1,1))
    plt.grid()
    plt.show()<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from django.conf import settings
from django.shortcuts import get_object_or_404

from pusher import Pusher
from rest_framework import views, viewsets
from rest_framework.response import Response
from socketIO_client import SocketIO

from apps.broadcasts.models import Broadcast, Host, Raid, Series
from apps.games.models import Game, Platform
from apps.quotes.models import Quote
from apps.subscribers.models import Ticket

from .serializers import (BroadcastSerializer, GameSerializer, HostSerializer,
    PlatformSerializer, QuoteSerializer, RaidSerializer, SeriesSerializer,
    TicketSerializer)

def notify(event, data):
    pusher['live'].trigger(event, data)

    data = data.copy()
    data['event'] = event

    with SocketIO('socket.avalonstar.tv') as socketIO:
        socketIO.emit('event sent', data)
        socketIO.wait(seconds=1)

class BroadcastViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = Broadcast.objects.order_by('-number')
    serializer_class = BroadcastSerializer

class GameViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = Game.objects.all()
    serializer_class = GameSerializer

class PlatformViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = Platform.objects.all()
    serializer_class = PlatformSerializer

class QuoteViewSet(viewsets.ModelViewSet):
    queryset = Quote.objects.all()
    serializer_class = QuoteSerializer

    def retrieve(self, request, pk=None):
        if pk is '0':
            quote = Quote.objects.order_by('?').first()
            serializer = QuoteSerializer(quote)
            return Response(serializer.data)
        else:
            return super(QuoteViewSet, self).retrieve(request, pk)

class HostViewSet(viewsets.ModelViewSet):
    queryset = Host.objects.order_by('-timestamp')
    serializer_class = HostSerializer

    def create(self, request, *args, **kwargs):
        notify('host', {'username': request.data['username']})
        return super(HostViewSet, self).create(request, *args, **kwargs)

class RaidViewSet(viewsets.ModelViewSet):
    queryset = Raid.objects.order_by('-timestamp')
    serializer_class = RaidSerializer

class TicketViewSet(viewsets.ModelViewSet):
    queryset = Ticket.objects.order_by('-updated')
    serializer_class = TicketSerializer

    def create(self, request, *args, **kwargs):
        # TODO: Somehow sync the use of "name" and "username" across methods.
        notify('subscription', {'username': request.data['name']})
        return super(TicketViewSet, self).create(request, *args, **kwargs)

    def retrieve(self, request, pk=None):
        queryset = Ticket.objects.all()
        ticket = get_object_or_404(queryset, name=pk)
        serializer = TicketSerializer(ticket)
        return Response(serializer.data)

    def update(self, request, pk=None):
        data = request.data.copy()
        queryset = Ticket.objects.all()
        ticket = get_object_or_404(queryset, name=pk)

        data['name'] = ticket.name
        serializer = TicketSerializer(ticket, data=data)
        serializer.is_valid(raise_exception=True)
        self.perform_update(serializer)

        # If 'streak' is included in the payload, then we consider it a
        # "substreak" and should notify() as such.
        if 'streak' in request.data:
            notify('substreak', {
                'length': data['streak'],
                'username': ticket.name})
        else:
            notify('resubscription', {'username': ticket.name})
        return Response(serializer.data)

# Pusher.
pusher = Pusher(
    app_id=settings.PUSHER_APP_ID,
    key=settings.PUSHER_KEY,
    secret=settings.PUSHER_SECRET)

class PusherDonationView(views.APIView):
    def post(self, request):
        notify('donation', request.data)
        return Response(status=202)

class PusherHostView(views.APIView):
    def post(self, request):
        notify('host', request.data)
        return Response(status=202)

class PusherResubscriptionView(views.APIView):
    def post(self, request):
        notify('resubscription', request.data)
        return Response(status=202)

class PusherSubscriptionView(views.APIView):
    def post(self, request):
        notify('subscription', request.data)
        return Response(status=202)

class PusherSubstreakView(views.APIView):
    def post(self, request):
        notify('substreak', request.data)
        return Response(status=202)
<commit_msg>is -> ==.<commit_after># -*- coding: utf-8 -*-
from django.conf import settings
from django.shortcuts import get_object_or_404

from pusher import Pusher
from rest_framework import views, viewsets
from rest_framework.response import Response
from socketIO_client import SocketIO

from apps.broadcasts.models import Broadcast, Host, Raid, Series
from apps.games.models import Game, Platform
from apps.quotes.models import Quote
from apps.subscribers.models import Ticket

from .serializers import (BroadcastSerializer, GameSerializer, HostSerializer,
    PlatformSerializer, QuoteSerializer, RaidSerializer, SeriesSerializer,
    TicketSerializer)

def notify(event, data):
    pusher['live'].trigger(event, data)

    data = data.copy()
    data['event'] = event

    with SocketIO('socket.avalonstar.tv') as socketIO:
        socketIO.emit('event sent', data)
        socketIO.wait(seconds=1)

class BroadcastViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = Broadcast.objects.order_by('-number')
    serializer_class = BroadcastSerializer

class GameViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = Game.objects.all()
    serializer_class = GameSerializer

class PlatformViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = Platform.objects.all()
    serializer_class = PlatformSerializer

class QuoteViewSet(viewsets.ModelViewSet):
    queryset = Quote.objects.all()
    serializer_class = QuoteSerializer

    def retrieve(self, request, pk=None):
        if pk == '0':
            quote = Quote.objects.order_by('?').first()
            serializer = QuoteSerializer(quote)
            return Response(serializer.data)
        else:
            return super(QuoteViewSet, self).retrieve(request, pk)

class HostViewSet(viewsets.ModelViewSet):
    queryset = Host.objects.order_by('-timestamp')
    serializer_class = HostSerializer

    def create(self, request, *args, **kwargs):
        notify('host', {'username': request.data['username']})
        return super(HostViewSet, self).create(request, *args, **kwargs)

class RaidViewSet(viewsets.ModelViewSet):
    queryset = Raid.objects.order_by('-timestamp')
    serializer_class = RaidSerializer

class TicketViewSet(viewsets.ModelViewSet):
    queryset = Ticket.objects.order_by('-updated')
    serializer_class = TicketSerializer

    def create(self, request, *args, **kwargs):
        # TODO: Somehow sync the use of "name" and "username" across methods.
        notify('subscription', {'username': request.data['name']})
        return super(TicketViewSet, self).create(request, *args, **kwargs)

    def retrieve(self, request, pk=None):
        queryset = Ticket.objects.all()
        ticket = get_object_or_404(queryset, name=pk)
        serializer = TicketSerializer(ticket)
        return Response(serializer.data)

    def update(self, request, pk=None):
        data = request.data.copy()
        queryset = Ticket.objects.all()
        ticket = get_object_or_404(queryset, name=pk)

        data['name'] = ticket.name
        serializer = TicketSerializer(ticket, data=data)
        serializer.is_valid(raise_exception=True)
        self.perform_update(serializer)

        # If 'streak' is included in the payload, then we consider it a
        # "substreak" and should notify() as such.
        if 'streak' in request.data:
            notify('substreak', {
                'length': data['streak'],
                'username': ticket.name})
        else:
            notify('resubscription', {'username': ticket.name})
        return Response(serializer.data)

# Pusher.
pusher = Pusher(
    app_id=settings.PUSHER_APP_ID,
    key=settings.PUSHER_KEY,
    secret=settings.PUSHER_SECRET)

class PusherDonationView(views.APIView):
    def post(self, request):
        notify('donation', request.data)
        return Response(status=202)

class PusherHostView(views.APIView):
    def post(self, request):
        notify('host', request.data)
        return Response(status=202)

class PusherResubscriptionView(views.APIView):
    def post(self, request):
        notify('resubscription', request.data)
        return Response(status=202)

class PusherSubscriptionView(views.APIView):
    def post(self, request):
        notify('subscription', request.data)
        return Response(status=202)

class PusherSubstreakView(views.APIView):
    def post(self, request):
        notify('substreak', request.data)
        return Response(status=202)
<|endoftext|>


<commit_before>import floppyforms

class OrganizationCreateForm(floppyforms.Form):

    short_name = floppyforms.CharField(
        label="Organization Short Name",
        max_length="48",
        widget=floppyforms.TextInput(
            attrs={'id': 'orgShortName',
                   'placeholder': "Short Name (e.g. WHO)",
                   'class': 'form-control'}))

    full_name = floppyforms.CharField(
        label="Organization Full Name",
        max_length="48",
        widget=floppyforms.TextInput(
            attrs={'id': 'orgFullName',
                   'placeholder': "Full Name (e.g. World Health Organization)",
                   'class': 'form-control'}))

    website = floppyforms.URLField(
        label="Website",
        required=False,
        widget=floppyforms.URLInput(
            attrs={'id': 'orgWebsite',
                   'placeholder': "Website (e.g. http://www.who.int",
                   'class': 'form-control'}))

    @classmethod
    def submit(cls, data, *args, **kwargs):

        # utils.ocl_requests.post()
        # if not request.ok:
        # raise a Validation Error
        # else:
        # return True
        pass
<commit_msg>Fixed form typo<commit_after>import floppyforms

class OrganizationCreateForm(floppyforms.Form):

    short_name = floppyforms.CharField(
        label="Organization Short Name",
        max_length="48",
        widget=floppyforms.TextInput(
            attrs={'id': 'orgShortName',
                   'placeholder': "Short Name (e.g. WHO)",
                   'class': 'form-control'}))

    full_name = floppyforms.CharField(
        label="Organization Full Name",
        max_length="48",
        widget=floppyforms.TextInput(
            attrs={'id': 'orgFullName',
                   'placeholder': "Full Name (e.g. World Health Organization)",
                   'class': 'form-control'}))

    website = floppyforms.URLField(
        label="Website",
        required=False,
        widget=floppyforms.URLInput(
            attrs={'id': 'orgWebsite',
                   'placeholder': "Website (e.g. http://www.who.int)",
                   'class': 'form-control'}))

    @classmethod
    def submit(cls, data, *args, **kwargs):

        # utils.ocl_requests.post()
        # if not request.ok:
        # raise a Validation Error
        # else:
        # return True
        pass
<|endoftext|>


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

import sys
import time
import socket
import pyjoy
import serial
import pygame
from math import atan, cos, pi, sin, sqrt

MAX_MOTOR_VALUE = 180
MIN_MOTOR_VALUE = 25

def get_arduino_data(joyAxis, joyButtons):
    """
    Gets passed all axis and button info from the joystick and returns a list
    containing four motor values and a button map to send to the Arduino
    """
    xAxis = joyAxis[0]
    yAxis = joyAxis[1]
    rotAxis = joyAxis[2]
    # Joystick x and y are converted into polar coordinates and rotated 45
    # degrees
    mag = sqrt(xAxis**2 + yAxis**2)
    theta = atan(yAxis/xAxis) + pi/4
    motorA = mag*cos(theta)/2
    motorB = -mag*sin(theta)/2
    motorC = motorB
    motorD = motorA
    # Create a binary button map from joyButtons
    buttonMap = 0
    for index, value in enumerate(joyButtons):
        if value:
            buttonMap += 2**index

    # Build and output command list
    return [motorA, motorB, motorC, motorD, buttonMap]

def connect():
    UDP_IP = "192.168.1.177"
    UDP_PORT = 8888
    print "UDP target IP:", UDP_IP
    print "UDP target port:", UDP_PORT
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

def init_joystick(joy_idx):
    pygame.joystick.init()
    pygame.display.init()
    j = pygame.joystick.Joystick(joy_idx)
    j.init()

    print "Flushing event queue (don't touch the joystick)..."
    pygame.event.get()
    print '\nReady'

def send_command(command):
    message = [
        chr(command[0]), chr(command[1]), chr(command[2]), chr(command[3]),
        chr(command[4]), chr(command[5])]
    print message
    sock.sendto("".join(MESSAGE), (UDP_IP, UDP_PORT))
    recvstr = sock.recv(4096)
    print "send   {} {} {} {} {} {}".format(
        ord(MESSAGE[0]), ord(MESSAGE[1]), ord(MESSAGE[2]), ord(MESSAGE[3]),
        ord(MESSAGE[4]), ord(MESSAGE[5]))
    print "recv   {} {} {} {} {} {}".format(
        ord(recvstr[0]), ord(recvstr[1]), ord(recvstr[2]), ord(recvstr[3]),
        ord(recvstr[4]), ord(recvstr[5]))

    return recvstr

def main(joy_idx):
    # A list of axis values from the joystick
    joyAxis = [0,0,0,0,0,0]
    # Each boolean represents the value from the corresponding joystick button
    joyButtons = [False, False, False, False, False, False]

    command = [0,0,0,0,0,0]

    sock = connect()
    init_joystick(joy_idx)

    transmit_time = time.time()

    while True:
        if time.time() >= transmit_time:
            transmit_time += .25
            print command

        e = pygame.event.poll()
        if e.type not in (pygame.JOYAXISMOTION, pygame.JOYBUTTONDOWN,
                pygame.JOYBUTTONUP):
            continue
        # Read and change information in joyAxis or joyButtons
        #print e
        if e.type == pygame.JOYBUTTONDOWN:
            if (e.dict['button'] in range(6,12)):
                print "Bye!\n"
                quit()
            else:
                joyButtons[e.dict['button']] = True

        if e.type == pygame.JOYBUTTONUP:
            if (e.dict['button'] in range(0,6)):
                joyButtons[e.dict['button']] = False

        if e.type == pygame.JOYAXISMOTION:
            axis = e.dict['axis']
            value = e.dict['value']
            joyAxis[axis] = value

        # Construct Arduino command from all data
        xAxis = joyAxis[0]
        if xAxis == 0: # Prevents a divide by zero error
            xAxis = 10**-24
        yAxis = -joyAxis[1]
        rotAxis = joyAxis[2]

        # Joystick x and y are converted into polar coordinates and rotated
        # 45 degrees
        mag = sqrt(xAxis**2 + yAxis**2)
        if mag > 1:
            mag = 1
        theta = atan(yAxis/xAxis) + pi/4
        #print theta/pi
        motorA = abs(mag*sin(theta)/2)
        motorA *= 2 * MAX_MOTOR_VALUE # Values usable by the Arduino Servo library
        motorB = abs(mag*cos(theta)/2)
        motorB *= 2 * MAX_MOTOR_VALUE
        motorC = motorB
        motorD = motorA

        # Motor direction sent as a binary number with each bit
        # representing the motors A-D
        # x x x x
        # A B C D
        motorDirection = 0
        if xAxis > yAxis:
            motorDirection &= 0b1001
        else:
            motorDirection |= 0b0110
        if -xAxis > yAxis:
            motorDirection &= 0b0110
        else:
            motorDirection |= 0b1001

        # Create a binary button map from joyButtons
        buttonMap = 0
        for index, value in enumerate(joyButtons):
            if value:
                buttonMap |= 2**index

        # Build and output command list
        command = [int(motorA), int(motorB), int(motorC), int(motorD),
            motorDirection, buttonMap]

if __name__ == '__main__':
    if len(sys.argv) != 2:
        print 'Usage: client.py JOYSTICK_NUMBER'
        exit(2)
    main(int(sys.argv[1]))
<commit_msg>Simplify some more input stuff<commit_after>#!/usr/bin/env python2

import sys
import time
import socket
import pyjoy
import serial
import pygame
from math import atan, cos, pi, sin, sqrt

MAX_MOTOR_VALUE = 180
MIN_MOTOR_VALUE = 25

def get_arduino_data(joyAxis, joyButtons):
    """
    Gets passed all axis and button info from the joystick and returns a list
    containing four motor values and a button map to send to the Arduino
    """
    xAxis = joyAxis[0]
    yAxis = joyAxis[1]
    rotAxis = joyAxis[2]
    # Joystick x and y are converted into polar coordinates and rotated 45
    # degrees
    mag = sqrt(xAxis**2 + yAxis**2)
    theta = atan(yAxis/xAxis) + pi/4
    motorA = mag*cos(theta)/2
    motorB = -mag*sin(theta)/2
    motorC = motorB
    motorD = motorA
    # Create a binary button map from joyButtons
    buttonMap = 0
    for index, value in enumerate(joyButtons):
        if value:
            buttonMap += 2**index

    # Build and output command list
    return [motorA, motorB, motorC, motorD, buttonMap]

def connect():
    UDP_IP = "192.168.1.177"
    UDP_PORT = 8888
    print "UDP target IP:", UDP_IP
    print "UDP target port:", UDP_PORT
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

def init_joystick(joy_idx):
    pygame.joystick.init()
    pygame.display.init()
    j = pygame.joystick.Joystick(joy_idx)
    j.init()

    print "Flushing event queue (don't touch the joystick)..."
    pygame.event.get()
    print '\nReady'

def send_command(command):
    message = [
        chr(command[0]), chr(command[1]), chr(command[2]), chr(command[3]),
        chr(command[4]), chr(command[5])]
    print message
    sock.sendto("".join(MESSAGE), (UDP_IP, UDP_PORT))
    recvstr = sock.recv(4096)
    print "send   {} {} {} {} {} {}".format(
        ord(MESSAGE[0]), ord(MESSAGE[1]), ord(MESSAGE[2]), ord(MESSAGE[3]),
        ord(MESSAGE[4]), ord(MESSAGE[5]))
    print "recv   {} {} {} {} {} {}".format(
        ord(recvstr[0]), ord(recvstr[1]), ord(recvstr[2]), ord(recvstr[3]),
        ord(recvstr[4]), ord(recvstr[5]))

    return recvstr

def main(joy_idx):
    stick = [0] * 6
    buttons = [False] * 6

    sock = connect()
    init_joystick(joy_idx)

    transmit_time = time.time()

    while True:
        if time.time() >= transmit_time:
            transmit_time += .25
            print stick, buttons

        e = pygame.event.poll()
        if e.type not in (pygame.JOYAXISMOTION, pygame.JOYBUTTONDOWN,
                pygame.JOYBUTTONUP):
            continue
        # Read and change information in joyAxis or joyButtons
        if e.type == pygame.JOYBUTTONDOWN:
            num = e.dict['button']
            if num in range(6, 12):
                print 'Bye!'
                return
            buttons[e.dict['button']] = True

        if e.type == pygame.JOYBUTTONUP:
            buttons[e.dict['button']] = False

        if e.type == pygame.JOYAXISMOTION:
            axis = e.dict['axis']
            value = e.dict['value']
            stick[axis] = value

        # Construct Arduino command from all data
        xAxis = stick[0]
        if xAxis == 0: # Prevents a divide by zero error
            xAxis = 10**-24
        yAxis = -stick[1]
        rotAxis = stick[2]

        # Joystick x and y are converted into polar coordinates and rotated
        # 45 degrees
        mag = sqrt(xAxis**2 + yAxis**2)
        if mag > 1:
            mag = 1
        theta = atan(yAxis/xAxis) + pi/4
        #print theta/pi
        motorA = abs(mag*sin(theta)/2)
        motorA *= 2 * MAX_MOTOR_VALUE # Values usable by the Arduino Servo library
        motorB = abs(mag*cos(theta)/2)
        motorB *= 2 * MAX_MOTOR_VALUE
        motorC = motorB
        motorD = motorA

if __name__ == '__main__':
    if len(sys.argv) != 2:
        print 'Usage: client.py JOYSTICK_NUMBER'
        exit(2)
    main(int(sys.argv[1]))
<|endoftext|>


<commit_before>from django.contrib import admin

from wiki.models import Document

class DocumentAdmin(admin.ModelAdmin):
    exclude = ('tags',)
    list_display = ('locale', 'title', 'category', 'is_localizable',
                    'is_archived', 'allow_discussion')
    list_display_links = ('title',)
    list_filter = ('is_template', 'is_localizable', 'category', 'locale',
                   'is_archived', 'allow_discussion')
    raw_id_fields = ('parent',)
    readonly_fields = ('id', 'current_revision', 'latest_localizable_revision')
    search_fields = ('title',)

    @staticmethod
    def _set_archival(queryset, should_be_archived):
        """Set archival bit of documents, percolating up to parents as well."""
        for doc in queryset:
            # If this is a child, change its parent instead, and saving the
            # parent will change all the children to match.
            doc_to_change = doc.parent or doc
            doc_to_change.is_archived = should_be_archived
            doc_to_change.save()

    def _show_archival_message(self, request, queryset, verb):
        count = len(queryset)
        phrase = (
            'document, along with its English version or translations, was '
            'marked as '
            if count == 1 else
            'documents, along with their English versions or translations, '
            'were marked as ')
        self.message_user(request, '%s %s %s.' % (count, phrase, verb))

    def archive(self, request, queryset):
        """Mark several documents as obsolete."""
        self._set_archival(queryset, True)
        self._show_archival_message(request, queryset, 'obsolete')
    archive.short_description = 'Mark as obsolete'

    def unarchive(self, request, queryset):
        """Mark several documents as not obsolete."""
        self._set_archival(queryset, False)
        self._show_archival_message(request, queryset, 'no longer obsolete')
    unarchive.short_description = 'Mark as not obsolete'

    def allow_discussion(self, request, queryset):
        """Allow discussion on several documents."""
        queryset.update(allow_discussion=True)
        self.message_user(request, u'Document(s) now allow discussion.')

    def disallow_discussion(self, request, queryset):
        """Disallow discussion on several documents."""
        queryset.update(allow_discussion=False)
        self.message_user(request, u'Document(s) no longer allow discussion.')

    actions = [archive, unarchive, allow_discussion, disallow_discussion]

admin.site.register(Document, DocumentAdmin)
<commit_msg>Make document.contributors a raw id field in the admin. [bug 669643]<commit_after>from django.contrib import admin

from wiki.models import Document

class DocumentAdmin(admin.ModelAdmin):
    exclude = ('tags',)
    list_display = ('locale', 'title', 'category', 'is_localizable',
                    'is_archived', 'allow_discussion')
    list_display_links = ('title',)
    list_filter = ('is_template', 'is_localizable', 'category', 'locale',
                   'is_archived', 'allow_discussion')
    raw_id_fields = ('parent', 'contributors')
    readonly_fields = ('id', 'current_revision', 'latest_localizable_revision')
    search_fields = ('title',)

    @staticmethod
    def _set_archival(queryset, should_be_archived):
        """Set archival bit of documents, percolating up to parents as well."""
        for doc in queryset:
            # If this is a child, change its parent instead, and saving the
            # parent will change all the children to match.
            doc_to_change = doc.parent or doc
            doc_to_change.is_archived = should_be_archived
            doc_to_change.save()

    def _show_archival_message(self, request, queryset, verb):
        count = len(queryset)
        phrase = (
            'document, along with its English version or translations, was '
            'marked as '
            if count == 1 else
            'documents, along with their English versions or translations, '
            'were marked as ')
        self.message_user(request, '%s %s %s.' % (count, phrase, verb))

    def archive(self, request, queryset):
        """Mark several documents as obsolete."""
        self._set_archival(queryset, True)
        self._show_archival_message(request, queryset, 'obsolete')
    archive.short_description = 'Mark as obsolete'

    def unarchive(self, request, queryset):
        """Mark several documents as not obsolete."""
        self._set_archival(queryset, False)
        self._show_archival_message(request, queryset, 'no longer obsolete')
    unarchive.short_description = 'Mark as not obsolete'

    def allow_discussion(self, request, queryset):
        """Allow discussion on several documents."""
        queryset.update(allow_discussion=True)
        self.message_user(request, u'Document(s) now allow discussion.')

    def disallow_discussion(self, request, queryset):
        """Disallow discussion on several documents."""
        queryset.update(allow_discussion=False)
        self.message_user(request, u'Document(s) no longer allow discussion.')

    actions = [archive, unarchive, allow_discussion, disallow_discussion]

admin.site.register(Document, DocumentAdmin)
<|endoftext|>


<commit_before>import re

__author__ = 'tmehta'

from django import template

register = template.Library()

@register.filter('format_distance')
def format_distance_(distance):
    distance = distance.strip()
    l = []
    for t in distance.split():
        try:
            l.append(float(t))
        except ValueError:
            pass

    l = l[0]
    l = int(l / 100) / 10
    string = "{0:.2f}".format(round(l, 2))
    string += " km"
    return string

@register.filter
def unidecode(string):
    return re.sub('[^A-Za-z0-9]+', '', string)<commit_msg>Non-regex<commit_after>__author__ = 'tmehta'

from django import template

register = template.Library()

@register.filter('format_distance')
def format_distance_(distance):
    distance = distance.strip()
    l = []
    for t in distance.split():
        try:
            l.append(float(t))
        except ValueError:
            pass

    l = l[0]
    l = int(l / 100) / 10
    string = "{0:.2f}".format(round(l, 2))
    string += " km"
    return string

@register.filter
def unidecode(string):
    return ' '.join(e for e in string if e.isalnum())<|endoftext|>


<commit_before><commit_msg>minor stuff<commit_after><|endoftext|>