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

import json
import random
from PIL import Image
from pprint import pprint

file_loc = "../images/croppedPokeParts/"

def get_metadatalist():
  pass

def get_metadata():
  f = open("../metadata/body.json", 'r')
  bodies = json.loads(f.read())

  f = open("../metadata/head.json", 'r')
  heads = json.loads(f.read())

  f = open("../metadata/tail.json", 'r')
  tails = json.loads(f.read())

  return bodies, heads, tails

# paste of the paste coords of the first image
# pinhole_a is the pinhold for the first image
# pinhole_b is the pinhold for the second image
# returns the coords to paste b to match pinholes
def get_pinhole_match(paste, pinhole_a, pinhole_b, imagea, imageb):
  x = paste[0] + pinhole_a[0] - pinhole_b[0]
  y = paste[1] + (imagea.size[1] - pinhole_a[1]) - pinhole_b[1]
  return (x, y)

def get_pinhole_match_beta(paste, pinhole_a, pinhole_b, imagea, imageb):
  x = paste[0] + pinhole_a[0] - pinhole_b[0]
  y = paste[1] + (imagea.size[1] - pinhole_a[1]) - (imageb.size[1] - pinhole_b[1])
  return (x, y)

def paste_in():
  pass

def crop(img):
    data = img.load()
    l,w = img.size
    a = (l,w)
    b = (l,0)
    c = (0,w)
    d = (0,0)

    for x in range(0,l-1):
        for y in range(0,w-1):
            if data[x,y][3] != 0:
                a = (min(a[0],x),min(a[1],y))
                b = (min(b[0],x),max(b[1],y))
                c = (max(c[0],x),min(c[1],y))
                d = (max(d[0],x),max(d[1],y))

    a = (a[0] -1, a[1]-1)
    d = (d[0] +1,d[1]+1)

    return img.crop(a+d)

def same_size():
  pass

FACTOR = 3
def embiggen(img):
  return img.resize((FACTOR*img.size[0], FACTOR*img.size[1]), 0)

def generate_image():

  bodies, heads, tails = get_metadata()
  body = random.choice(bodies)
  head = random.choice(heads)
  tail = random.choice(tails)

#  pprint(body)
#  pprint(head)
#  pprint(tail)

  body_image = Image.open(file_loc+body["filename"])
  head_image = Image.open(file_loc+head["filename"])
  tail_image = Image.open(file_loc+tail["filename"])

  master = Image.new(
      mode='RGBA',
      size=(300, 300),
      color=(0,0,0,0))

  head_pinhole = head["pinhole"]
  tail_pinhole = tail["needs"][0]["pinhole"]
  if len(body["needs"])>1:
    body_pinhole = body["needs"][1]["pinhole"]
    coords = get_pinhole_match_beta((150,150),
                               body_pinhole,tail_pinhole,
                               body_image, tail_image)
    master.paste(tail_image, coords, tail_image)
  body_pinhole = body["needs"][0]["pinhole"]
  master.paste(body_image,(150, 150),body_image)
  coords = get_pinhole_match((150, 150),
                              body_pinhole, head_pinhole,
                              body_image, head_image)
  master.paste(head_image, coords, head_image)

  master = crop(master)
  master = embiggen(master)
  master.save("../site/static/imgs/horror.png")

 # for i in body_pinhole["needs"]:
 #   body_pinhole = i["pinhole"]
<commit_msg>all images are 300 px square<commit_after>#!/usr/local/bin/python3

import json
import random
from PIL import Image
from pprint import pprint

file_loc = "../images/croppedPokeParts/"

def get_metadatalist():
  pass

def get_metadata():
  f = open("../metadata/body.json", 'r')
  bodies = json.loads(f.read())

  f = open("../metadata/head.json", 'r')
  heads = json.loads(f.read())

  f = open("../metadata/tail.json", 'r')
  tails = json.loads(f.read())

  return bodies, heads, tails

# paste of the paste coords of the first image
# pinhole_a is the pinhold for the first image
# pinhole_b is the pinhold for the second image
# returns the coords to paste b to match pinholes
def get_pinhole_match(paste, pinhole_a, pinhole_b, imagea, imageb):
  x = paste[0] + pinhole_a[0] - pinhole_b[0]
  y = paste[1] + (imagea.size[1] - pinhole_a[1]) - pinhole_b[1]
  return (x, y)

def get_pinhole_match_beta(paste, pinhole_a, pinhole_b, imagea, imageb):
  x = paste[0] + pinhole_a[0] - pinhole_b[0]
  y = paste[1] + (imagea.size[1] - pinhole_a[1]) - (imageb.size[1] - pinhole_b[1])
  return (x, y)

def paste_in():
  pass

def crop(img):
    data = img.load()
    l,w = img.size
    a = (l,w)
    b = (l,0)
    c = (0,w)
    d = (0,0)

    for x in range(0,l-1):
        for y in range(0,w-1):
            if data[x,y][3] != 0:
                a = (min(a[0],x),min(a[1],y))
                b = (min(b[0],x),max(b[1],y))
                c = (max(c[0],x),min(c[1],y))
                d = (max(d[0],x),max(d[1],y))

    a = (a[0] -1, a[1]-1)
    d = (d[0] +1,d[1]+1)

    return img.crop(a+d)

def same_size(img):
  w, h = img.size

  master = Image.new(
      mode='RGBA',
      size=(300, 300),
      color=(0,0,0,0))

  master.paste(img, (150-w/2, 150-h/2), img)
  return master

FACTOR = 3
def embiggen(img):
  return img.resize((FACTOR*img.size[0], FACTOR*img.size[1]), 0)

def generate_image():

  bodies, heads, tails = get_metadata()
  body = random.choice(bodies)
  head = random.choice(heads)
  tail = random.choice(tails)

#  pprint(body)
#  pprint(head)
#  pprint(tail)

  body_image = Image.open(file_loc+body["filename"])
  head_image = Image.open(file_loc+head["filename"])
  tail_image = Image.open(file_loc+tail["filename"])

  master = Image.new(
      mode='RGBA',
      size=(300, 300),
      color=(0,0,0,0))

  head_pinhole = head["pinhole"]
  tail_pinhole = tail["needs"][0]["pinhole"]
  if len(body["needs"])>1:
    body_pinhole = body["needs"][1]["pinhole"]
    coords = get_pinhole_match_beta((150,150),
                               body_pinhole,tail_pinhole,
                               body_image, tail_image)
    master.paste(tail_image, coords, tail_image)
  body_pinhole = body["needs"][0]["pinhole"]
  master.paste(body_image,(150, 150),body_image)
  coords = get_pinhole_match((150, 150),
                              body_pinhole, head_pinhole,
                              body_image, head_image)
  master.paste(head_image, coords, head_image)

  master = crop(master)
  master = embiggen(master)
  master = same_size(master)
  master.save("../site/static/imgs/horror.png")

 # for i in body_pinhole["needs"]:
 #   body_pinhole = i["pinhole"]
<|endoftext|>


<commit_before>from pyymir import *

if __name__ == "__main__":
    ap = default_ymir_ap()
    ap.add_argument("-w", "--workmode", help = "either add computed probabilities to the input file ('add', default), or print them to the separated output file as a vector ('sep')", default = "add", type = str)
    ap.add_argument("-o", "--output", help = "path to the output folder for output files (default is './ymir_genprob/')", type = str, default = "./ymir_genprob/")
    ap.add_argument("-p", "--predefined", help = "recompute or use predefined model's gene usage (default is to recompute, to change it add '-p' to your script call)", action = "store_true")
    ap.add_argument("-l", "--leave", help = "add this to leave converted files (default is to remove converted files)", action = "store_true")
    args = ap.parse_args()

    files, input_check = parse_input(args)
    model, model_check = parse_model(args)
    converter, format_check = parse_format(args)
    out_files, out_check = parse_output_files(files, args)

    print()
    if model_check and input_check and format_check and out_check:
        for i in range(len(files)):
            print(i + 1, ":")
            conv_file, convert_flag = convert(files[i], converter)
            if convert_flag:
                print()
                os.system(" ".join(["./build/Compute", conv_file, model, out_files[i], "0" if args.predefined else "1"]))

                if not args.leave:
                    os.remove(conv_file)
            print()
    else:
        print("Can't process further, too many errors for me! T_T")
<commit_msg>don't remove ymir format files<commit_after>from pyymir import *

if __name__ == "__main__":
    ap = default_ymir_ap()
    ap.add_argument("-w", "--workmode", help = "either add computed probabilities to the input file ('add', default), or print them to the separated output file as a vector ('sep')", default = "add", type = str)
    ap.add_argument("-o", "--output", help = "path to the output folder for output files (default is './ymir_genprob/')", type = str, default = "./ymir_genprob/")
    ap.add_argument("-p", "--predefined", help = "recompute or use predefined model's gene usage (default is to recompute, to change it add '-p' to your script call)", action = "store_true")
    ap.add_argument("-l", "--leave", help = "add this to leave converted files (default is to remove converted files)", action = "store_true")
    args = ap.parse_args()

    files, input_check = parse_input(args)
    model, model_check = parse_model(args)
    converter, format_check = parse_format(args)
    out_files, out_check = parse_output_files(files, args)

    print()
    if model_check and input_check and format_check and out_check:
        for i in range(len(files)):
            print(i + 1, ":")
            conv_file, convert_flag = convert(files[i], converter)
            if convert_flag:
                print()
                os.system(" ".join(["./build/Compute", conv_file, model, out_files[i], "0" if args.predefined else "1"]))

                if not args.leave and args.format != 'ymir':
                    os.remove(conv_file)
            print()
    else:
        print("Can't process further, too many errors for me! T_T")
<|endoftext|>


<commit_before>"""
Module for computing infix expressions and equations
"""

import operator
import math
from collections import namedtuple, deque
from io import BytesIO
from tokenize import tokenize, NUMBER, ENCODING

from mpl_toolkits.axes_grid.axislines import SubplotZero
import matplotlib.pyplot as plt
import numpy as np

class Calculator:
    """
    Class for computing infix expressions.

    >>> computer = Calculator()
    >>> computer.solve('10+2 - (3*3)')
    3
    """

    OpInfo = namedtuple('Operator', 'precedence assoc operation operand_count')

    def __init__(self):
        ## TODO: consider moving this to a seperate config file
        self.OPS = {
            'neg': self.OpInfo(precedence=5, assoc='R', operation=operator.neg, operand_count=1),
            'abs': self.OpInfo(precedence=4, assoc='L', operation=operator.abs, operand_count=1),
            'sin': self.OpInfo(precedence=4, assoc='L', operation=math.sin, operand_count=1),
            'cos': self.OpInfo(precedence=4, assoc='L', operation=math.cos, operand_count=1),
            'tan': self.OpInfo(precedence=4, assoc='L', operation=math.tan, operand_count=1),
            'sqrt': self.OpInfo(precedence=4, assoc='L', operation=math.sqrt, operand_count=1),
            'log': self.OpInfo(precedence=4, assoc='L', operation=math.log10, operand_count=1),
            'ln': self.OpInfo(precedence=4, assoc='L', operation=math.log, operand_count=1),
            '!': self.OpInfo(precedence=4, assoc='R', operation=math.factorial, operand_count=1),
            '^': self.OpInfo(precedence=3, assoc='R', operation=operator.pow, operand_count=2),
            '*': self.OpInfo(precedence=2, assoc='L', operation=operator.mul, operand_count=2),
            '/': self.OpInfo(precedence=2, assoc='L', operation=operator.truediv, operand_count=2),
            '+': self.OpInfo(precedence=1, assoc='L', operation=operator.add, operand_count=2),
            '-': self.OpInfo(precedence=1, assoc='L', operation=operator.sub, operand_count=2)
        }

    def graph(self, equation, dimensions=(10, 10)):
        """
        Graphs a function of the form 'y = x + 5'.

        Args:
            equation: a string containing the right side of an equation (the left side is implied)
            dimensions: a tuple of the form (x, y). domain will go from -x to x, and range from -y to y
        """

        # Note: if performance is an issue, consider rewriting to use np operators
        #       to operate on the entire np array rather than individual elements

        x, y = dimensions

        increments = np.linspace(-x, x, 1000)
        points = np.fromiter((self.solve(equation, replacement=value) for value in increments), np.float)

        # Necessary for functions with discontinuous lines.
        points[points > y] = np.nan
        points[points < -y] = np.nan

        self.create_plot(increments, points, y)

    def create_plot(self, scale, points, y_bounds):
        """
        Using matplotlib, graphs the given points on a Cartesian plane.

        Adapted from the matplotlib documentation:
        https://matplotlib.org/examples/axes_grid/demo_axisline_style.html.

        args:
            scale: Numpy array of increments for the x-axis.
            points: Numpy array of points' Y-values to be plotted.
            y_bounds: integer determining scale of y axis. range will go from -y_bounds to y_bounds.
        """

        fig = plt.figure(1)
        ax = SubplotZero(fig, 111)
        fig.add_subplot(ax)

        for direction in ['xzero', 'yzero']:
            ax.axis[direction].set_axisline_style('-|>')
            ax.axis[direction].set_visible(True)

        for direction in ['left', 'right', 'bottom', 'top']:
            ax.axis[direction].set_visible(False)

        ax.set_ylim([-y_bounds, y_bounds])
        ax.plot(scale, points)

        plt.show()

    def solve(self, expression, replacement=None):
        """
        Solves an infix algebraic expression.

        Adapted from the pseudocode shown here:
        https://en.wikipedia.org/wiki/Reverse_Polish_notation#Postfix_evaluation_algorithm.

        Args:
            expression: a string containing an algebraic expression in infix notation
            replacement: optional value to replace the variable 'x' if it occurs in expression

        Returns:
            integer or float result of the expression
        """

        if replacement is not None:
            expression = expression.replace('x', f'({replacement})')

        postfix_queue = self.convert_infix(expression)
        eval_stack = []

        try:
            for token in postfix_queue:
                if token in self.OPS:
                    *_, operation, operand_count = self.OPS[token]

                    # Pops a variable number of items off the stack.
                    operands = eval_stack[-operand_count:]
                    eval_stack = eval_stack[:-operand_count]

                    #print(f'{token} {operands}')
                    result = operation(*operands)
                    eval_stack.append(result)
                else:
                    eval_stack.append(token)

            return eval_stack.pop()
        except ValueError:
            return None

    def convert_infix(self, expression):
        """
        Converts an infix expression to a postfix expression using the Shunting-Yard algorithm.

        Adapted from the pseudocode shown here:
        https://en.wikipedia.org/wiki/Shunting-yard_algorithm.

        Args:
            expression: a string containing an algebraic expression in infix notation

        Returns:
            a queue of tuples containing operands and operators in postfix order
        """

        # Breaks the expression string into a list of tokens, expressed as 5-tuples.
        tokens = list(tokenize(BytesIO(expression.encode('utf_8')).readline))

        out_queue = deque()
        op_stack = []
        prev_token = (None, None)

        for tok_type, tok_string, *_ in tokens:
            prev_tok_type, prev_tok_string = prev_token

            if tok_type == NUMBER:
                out_queue.append(float(tok_string))

            elif tok_string == '(':
                # Allow implicit multiplication (eg 5(x) or (x)(y))
                if prev_tok_type == NUMBER or prev_tok_string == ')':
                    op_stack.append('*')

                op_stack.append(tok_string)

            elif tok_string == ')':
                while op_stack and op_stack[-1] != '(':
                    out_queue.append(op_stack.pop())
                op_stack.pop()

            elif tok_string in self.OPS:
                # Allow unary minus operator
                if tok_string == '-' and (prev_tok_string in self.OPS or prev_tok_string == '('
                    or prev_tok_string == None) and prev_tok_string != ')':

                    tok_string = 'neg'

                while op_stack:
                    top = op_stack[-1]
                    if top == '(':
                        break

                    top_info, current_info = self.OPS[top], self.OPS[tok_string]
                    if top_info.assoc == 'L' and top_info.precedence >= current_info.precedence:
                        out_queue.append(op_stack.pop())
                    else:
                        break
                op_stack.append(tok_string)

            if tok_type != ENCODING:
                prev_token = (tok_type, tok_string)

        while op_stack:
            out_queue.append(op_stack.pop())

        return out_queue

com = Calculator()
com.graph('sqrt(x) + 5')
com.graph('log(x) + 5')
com.graph('ln(x) + 5')
<commit_msg>Refactored calculations to use Numpy functions. - Speeds have been increased by literally 500x<commit_after>"""
Module for computing infix expressions and equations
"""

from collections import namedtuple, deque
from io import BytesIO
from tokenize import tokenize, NUMBER, ENCODING

from mpl_toolkits.axes_grid.axislines import SubplotZero
import matplotlib.pyplot as plt
import numpy as np

NP_ARRAY = 999
class Calculator:
    """
    Class for computing infix expressions.

    >>> computer = Calculator()
    >>> computer.solve('10+2 - (3*3)')
    3
    """

    OpInfo = namedtuple('Operator', 'precedence assoc operation operand_count')

    def __init__(self):
        ## TODO: consider moving this to a seperate config file
        self.ops = {
            'neg': self.OpInfo(precedence=5, assoc='R', operation=np.negative, operand_count=1),
            'abs': self.OpInfo(precedence=4, assoc='L', operation=np.absolute, operand_count=1),
            'sin': self.OpInfo(precedence=4, assoc='L', operation=np.sin, operand_count=1),
            'cos': self.OpInfo(precedence=4, assoc='L', operation=np.cos, operand_count=1),
            'tan': self.OpInfo(precedence=4, assoc='L', operation=np.tan, operand_count=1),
            'sqrt': self.OpInfo(precedence=4, assoc='L', operation=np.sqrt, operand_count=1),
            'log': self.OpInfo(precedence=4, assoc='L', operation=np.log10, operand_count=1),
            'ln': self.OpInfo(precedence=4, assoc='L', operation=np.log, operand_count=1),
            '^': self.OpInfo(precedence=3, assoc='R', operation=np.power, operand_count=2),
            '*': self.OpInfo(precedence=2, assoc='L', operation=np.multiply, operand_count=2),
            '/': self.OpInfo(precedence=2, assoc='L', operation=np.true_divide, operand_count=2),
            '+': self.OpInfo(precedence=1, assoc='L', operation=np.add, operand_count=2),
            '-': self.OpInfo(precedence=1, assoc='L', operation=np.subtract, operand_count=2)
        }

    def graph(self, equation, dimensions=(10, 10)):
        """
        Graphs a function of the form 'y = x + 5'.

        Args:
            equation: a string containing the right side of an equation (the left side is implied).
            dimensions: a tuple of form (x, y). domain will go from -x to x, and range from -y to y.
        """

        # Note: if performance is an issue, consider rewriting to use np operators
        #       to operate on the entire np array rather than individual elements

        x_bounds, y_bounds = dimensions

        increments = np.linspace(-x_bounds, x_bounds, 1000)
        points = self.solve(equation, replacement=increments)

        # Filters out too large/small values. Necessary for functions with discontinuous lines (e.g., tan).
        points[points > y_bounds] = np.nan
        points[points < -y_bounds] = np.nan

        self.create_plot(increments, points, y_bounds)

    def create_plot(self, scale, points, y_bounds):
        """
        Using matplotlib, graphs the given points on a Cartesian plane.

        Adapted from the matplotlib documentation:
        https://matplotlib.org/examples/axes_grid/demo_axisline_style.html.

        args:
            scale: Numpy array of increments for the x-axis.
            points: Numpy array of points' Y-values to be plotted.
            y_bounds: integer determining scale of y axis. range will go from -y_bounds to y_bounds.
        """

        fig = plt.figure(1)
        subplot = SubplotZero(fig, 111)
        fig.add_subplot(subplot)

        for direction in ['xzero', 'yzero']:
            subplot.axis[direction].set_axisline_style('-|>')
            subplot.axis[direction].set_visible(True)

        for direction in ['left', 'right', 'bottom', 'top']:
            subplot.axis[direction].set_visible(False)

        subplot.set_ylim([-y_bounds, y_bounds])
        subplot.plot(scale, points)

        # TODO: return some sort of serializable version of graph
        plt.show()

    def solve(self, expression, replacement=None):
        """
        Solves an infix algebraic expression.

        Adapted from the pseudocode shown here:
        https://en.wikipedia.org/wiki/Reverse_Polish_notation#Postfix_evaluation_algorithm.

        Args:
            expression: a string containing an algebraic expression in infix notation
            replacement: optional Numpy array to replace the variable 'x' if it occurs in expression

        Returns:
            float result of the expression
        """

        if replacement is not None:
            # Necessary for supporting expressions like 5x.
            expression = expression.replace('x', '(x)')

        postfix_queue = self.convert_infix(expression, replacement)
        eval_stack = []

        try:
            for token in postfix_queue:
                if isinstance(token, np.ndarray):
                    eval_stack.append(token)
                elif token in self.ops:
                    *_, operation, operand_count = self.ops[token]

                    # Pops a variable number of items off the stack.
                    operands = eval_stack[-operand_count:]
                    eval_stack = eval_stack[:-operand_count]

                    #print(f'{token} {operands}')
                    result = operation(*operands)
                    eval_stack.append(result)
                else:
                    eval_stack.append(token)

            return eval_stack.pop()
        except ValueError:
            return None

    def convert_infix(self, expression, replacement=None):
        """
        Converts an infix expression to a postfix expression using the Shunting-Yard algorithm.

        Adapted from the pseudocode shown here:
        https://en.wikipedia.org/wiki/Shunting-yard_algorithm.

        Args:
            expression: a string containing an algebraic expression in infix notation
            replacement: optional Numpy array to replace the variable 'x' if it occurs in expression

        Returns:
            a queue of tuples containing operands and operators in postfix order
        """

        # Breaks the expression string into a list of tokens, expressed as 5-tuples.
        tokens = list(tokenize(BytesIO(expression.encode('utf_8')).readline))

        # Replaces X value with Numpy array.
        if replacement is not None:
            tokens = [(NP_ARRAY, replacement, token[2], token[2], token[3])
                      if token[1] == 'x' else token
                      for token in tokens]

        out_queue = deque()
        op_stack = []
        prev_token = (None, None)

        for tok_type, tok_string, *_ in tokens:
            prev_tok_type, prev_tok_string = prev_token

            if tok_type == NUMBER:
                out_queue.append(float(tok_string))
            elif tok_type == NP_ARRAY:
                out_queue.append(tok_string)
            elif tok_string == '(':
                # Allow implicit multiplication (e.g., c(x) or (x)(y)).
                if prev_tok_type == NUMBER or prev_tok_type == NP_ARRAY or prev_tok_string == ')':
                    op_stack.append('*')

                op_stack.append(tok_string)

            elif tok_string == ')':
                while op_stack and op_stack[-1] != '(':
                    out_queue.append(op_stack.pop())
                op_stack.pop()

            elif tok_string in self.ops:
                # Allow unary minus operator.
                if (tok_string == '-' and prev_tok_string != ')' and
                        (prev_tok_type == NP_ARRAY or prev_tok_string in self.ops
                         or prev_tok_string == '(' or prev_tok_string is None)):

                    tok_string = 'neg'
                while op_stack:
                    top = op_stack[-1]
                    if top == '(':
                        break

                    top_info, current_info = self.ops[top], self.ops[tok_string]
                    if top_info.assoc == 'L' and top_info.precedence >= current_info.precedence:
                        out_queue.append(op_stack.pop())
                    else:
                        break
                op_stack.append(tok_string)

            if tok_type != ENCODING:
                prev_token = (tok_type, tok_string)

        while op_stack:
            out_queue.append(op_stack.pop())

        return out_queue

com = Calculator()
com.graph('tan(x)')
<|endoftext|>


<commit_before>import threading

import matplotlib.figure
import time
import numpy as np
import pyaudio
from kivy.app import App
from kivy.clock import mainthread
from kivy.core.image import Image
from kivy.garden.matplotlib.backend_kivyagg import FigureCanvasKivyAgg
from kivy.graphics.context_instructions import Color
from kivy.graphics.vertex_instructions import Rectangle
from kivy.uix.slider import Slider
from kivy.uix.widget import Widget

CHUNKSIZE = 2048
RATE = 44100

avatar = None
volume = 0.7

figure = matplotlib.figure.Figure()
lines = []

axs = figure.subplots(2, 1)

lines.extend(axs[0].plot(np.zeros(CHUNKSIZE)))
axs[0].set_xlim((0, CHUNKSIZE))
axs[0].set_ylim((-.2, .2))

FFT_CHUNK = 10 * CHUNKSIZE

fftfreq = np.fft.fftfreq(FFT_CHUNK, d=RATE / CHUNKSIZE / 1000000)
lines.extend(axs[1].plot(fftfreq[:FFT_CHUNK // 2], np.zeros(FFT_CHUNK // 2)))
axs[1].plot([0, FFT_CHUNK], [1, 1], "r-")
axs[1].set_ylim((0, 2))
axs[1].set_xlim((0, 3000))

@mainthread
def plot(y):
    lines[0].set_ydata(y)

    fft = np.fft.fft(y, n=FFT_CHUNK)
    fft = np.sqrt(np.abs(fft))

    lines[1].set_ydata(fft[:FFT_CHUNK // 2])

    figure.canvas.draw()

    max_freq = fftfreq[np.argmax(fft[:FFT_CHUNK // 2])]
    max_freq_vol = fft[np.argmax(fft[:FFT_CHUNK // 2])]
    if max_freq_vol > 1 and max_freq > 100:
        avatar.mouth = "open"
    else:
        avatar.mouth = "closed"

class Avatar(Widget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        global avatar
        avatar = self

        self._textures = {}

        with self.canvas:
            Color(0., 1., 0.)
            self._background = Rectangle(pos=self.pos, size=(self.width, self.width))
            Color(1., 1., 1.)
            self._body = Rectangle(texture=self.get_texture("body"), pos=self.pos, size=(self.width, self.width))
            self._eyes = Rectangle(texture=self.get_texture("eyes.open"), pos=self.pos, size=(self.width, self.width))
            self._mouth = Rectangle(texture=self.get_texture("mouth.closed"), pos=self.pos, size=(self.width, self.width))

        self.bind(size=self._update_rect, pos=self._update_rect)

    def _update_rect(self, instance, value):
        self._background.pos = instance.pos
        self._background.size = instance.size

        mn = min(instance.width, instance.height)
        size = (mn, mn)
        pos = (instance.x + (instance.width - mn) / 2, instance.y)

        self._body.pos = pos
        self._body.size = size
        self._eyes.pos = pos
        self._eyes.size = size
        self._mouth.pos = pos
        self._mouth.size = size

    def get_texture(self, name):
        if name not in self._textures:
            self._textures[name] = Image("data/images/layers/{}.png".format(name)).texture
        return self._textures[name]

    @property
    @mainthread
    def eyes(self):
        raise NotImplementedError()

    @eyes.setter
    @mainthread
    def eyes(self, value):
        self._eyes.texture = self.get_texture("eyes.{}".format(value))

    @property
    @mainthread
    def mouth(self):
        raise NotImplementedError()

    @mouth.setter
    @mainthread
    def mouth(self, value):
        self._mouth.texture = self.get_texture("mouth.{}".format(value))

class Plot(FigureCanvasKivyAgg):
    def __init__(self, *args, **kwargs):
        super().__init__(figure, *args, **kwargs)

class ConceptApp(App):
    pass

class VolumeSlider(Slider):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.value = volume

        self.bind(value=self.on_value_change)

    def on_value_change(self, instance, value):
        global volume
        volume = value

def plotter():
    p = pyaudio.PyAudio()
    stream = p.open(
        format=pyaudio.paFloat32,
        channels=1,
        rate=RATE,
        input=True,
        output=True,
        frames_per_buffer=CHUNKSIZE,
    )

    i = 0
    while True:
        data = stream.read(CHUNKSIZE)
        numpydata = np.fromstring(data, dtype=np.float32)
        numpydata *= volume
        # fft = np.abs(np.fft.fft(numpydata, n=10*CHUNKSIZE))
        # ifft = np.fft.ifft(fft)[:CHUNKSIZE]
        # stream.write(ifft.astype(np.float32).tostring())

        if i % 2 == 0:
            plot(numpydata)
        i += 1

plotter_thread = threading.Thread(target=plotter)
plotter_thread.daemon = True
plotter_thread.start()

def blinker():
    while True:
        time.sleep(6)
        avatar.eyes = "closed"
        time.sleep(.1)
        avatar.eyes = "open"

blinker_thread = threading.Thread(target=blinker)
blinker_thread.daemon = True
blinker_thread.start()

ConceptApp().run()
<commit_msg>add persistent volume<commit_after>import threading

import matplotlib.figure
import time
import numpy as np
import pyaudio
from kivy.app import App
from kivy.config import Config
from kivy.clock import mainthread
from kivy.core.image import Image
from kivy.garden.matplotlib.backend_kivyagg import FigureCanvasKivyAgg
from kivy.graphics.context_instructions import Color
from kivy.graphics.vertex_instructions import Rectangle
from kivy.uix.slider import Slider
from kivy.uix.widget import Widget

CHUNKSIZE = 2048
RATE = 44100

avatar = None

Config.read("concept.rc")
Config.adddefaultsection("avatar")
Config.setdefault("avatar", "volume", .5)
Config.write()

volume = Config.getfloat("avatar", "volume")

figure = matplotlib.figure.Figure()
lines = []

axs = figure.subplots(2, 1)

lines.extend(axs[0].plot(np.zeros(CHUNKSIZE)))
axs[0].set_xlim((0, CHUNKSIZE))
axs[0].set_ylim((-.2, .2))

FFT_CHUNK = 10 * CHUNKSIZE

fftfreq = np.fft.fftfreq(FFT_CHUNK, d=RATE / CHUNKSIZE / 1000000)
lines.extend(axs[1].plot(fftfreq[:FFT_CHUNK // 2], np.zeros(FFT_CHUNK // 2)))
axs[1].plot([0, FFT_CHUNK], [1, 1], "r-")
axs[1].set_ylim((0, 2))
axs[1].set_xlim((0, 3000))

@mainthread
def plot(y):
    lines[0].set_ydata(y)

    fft = np.fft.fft(y, n=FFT_CHUNK)
    fft = np.sqrt(np.abs(fft))

    lines[1].set_ydata(fft[:FFT_CHUNK // 2])

    figure.canvas.draw()

    max_freq = fftfreq[np.argmax(fft[:FFT_CHUNK // 2])]
    max_freq_vol = fft[np.argmax(fft[:FFT_CHUNK // 2])]
    if max_freq_vol > 1 and max_freq > 100:
        avatar.mouth = "open"
    else:
        avatar.mouth = "closed"

class Avatar(Widget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        global avatar
        avatar = self

        self._textures = {}

        with self.canvas:
            Color(0., 1., 0.)
            self._background = Rectangle(pos=self.pos, size=(self.width, self.width))
            Color(1., 1., 1.)
            self._body = Rectangle(texture=self.get_texture("body"), pos=self.pos, size=(self.width, self.width))
            self._eyes = Rectangle(texture=self.get_texture("eyes.open"), pos=self.pos, size=(self.width, self.width))
            self._mouth = Rectangle(texture=self.get_texture("mouth.closed"), pos=self.pos, size=(self.width, self.width))

        self.bind(size=self._update_rect, pos=self._update_rect)

    def _update_rect(self, instance, value):
        self._background.pos = instance.pos
        self._background.size = instance.size

        mn = min(instance.width, instance.height)
        size = (mn, mn)
        pos = (instance.x + (instance.width - mn) / 2, instance.y)

        self._body.pos = pos
        self._body.size = size
        self._eyes.pos = pos
        self._eyes.size = size
        self._mouth.pos = pos
        self._mouth.size = size

    def get_texture(self, name):
        if name not in self._textures:
            self._textures[name] = Image("data/images/layers/{}.png".format(name)).texture
        return self._textures[name]

    @property
    @mainthread
    def eyes(self):
        raise NotImplementedError()

    @eyes.setter
    @mainthread
    def eyes(self, value):
        self._eyes.texture = self.get_texture("eyes.{}".format(value))

    @property
    @mainthread
    def mouth(self):
        raise NotImplementedError()

    @mouth.setter
    @mainthread
    def mouth(self, value):
        self._mouth.texture = self.get_texture("mouth.{}".format(value))

class Plot(FigureCanvasKivyAgg):
    def __init__(self, *args, **kwargs):
        super().__init__(figure, *args, **kwargs)

class ConceptApp(App):
    pass

class VolumeSlider(Slider):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.value = volume

        self.bind(value=self.on_value_change)

    def on_touch_up(self, touch):
        super().on_touch_up(touch)

        Config.set("avatar", "volume", volume)
        Config.write()

    def on_value_change(self, instance, value):
        global volume
        volume = value

def plotter():
    p = pyaudio.PyAudio()
    stream = p.open(
        format=pyaudio.paFloat32,
        channels=1,
        rate=RATE,
        input=True,
        output=True,
        frames_per_buffer=CHUNKSIZE,
    )

    i = 0
    while True:
        data = stream.read(CHUNKSIZE)
        numpydata = np.fromstring(data, dtype=np.float32)
        numpydata *= volume
        # fft = np.abs(np.fft.fft(numpydata, n=10*CHUNKSIZE))
        # ifft = np.fft.ifft(fft)[:CHUNKSIZE]
        # stream.write(ifft.astype(np.float32).tostring())

        if i % 2 == 0:
            plot(numpydata)
        i += 1

plotter_thread = threading.Thread(target=plotter)
plotter_thread.daemon = True
plotter_thread.start()

def blinker():
    while True:
        time.sleep(6)
        avatar.eyes = "closed"
        time.sleep(.1)
        avatar.eyes = "open"

blinker_thread = threading.Thread(target=blinker)
blinker_thread.daemon = True
blinker_thread.start()

ConceptApp().run()
<|endoftext|>


<commit_before>import numpy as np
import numpy.random as rng
import matplotlib.pyplot as plt
from copy import deepcopy

from Particle import Particle

class Sampler:
	"""
	An object of this class is a sampler.
	"""

	def __init__(self, num_particles):
		"""
		Constructor: pass in the number of particles
		"""
		self.num_particles = num_particles
		self.particles = [Particle() for i in range(0, num_particles)]

		# Open and close output file to clear it
		f = open('output.txt', 'w')
		f.close()

		# Rectangles that have been forbidden so far
		self.forbidden_rectangles = None

		# Fraction of prior mass still in play
		self.log_prior_mass = 0.

	def initialise(self):
		"""
		Generate all the particles from the prior
		"""
		self.all_scalars = []	# Values of scalars for all particles
		for particle in self.particles:
			particle.from_prior()
			self.all_scalars.append(particle.scalars)
		self.all_scalars = np.array(self.all_scalars)
		self.forbidden_rectangles = np.empty((0, self.all_scalars.shape[1]))
		self.iteration = 0
		return

	def do_iteration(self):
		"""
		Do an NS iteration
		"""
		counts = self.rectangle_counts

		# Choose one with minimum count to discard
		temp = np.nonzero(counts == counts.min())[0]
		which = temp[rng.randint(len(temp))]

		# Need to figure out what happens in this case
		if counts.min() != 0:
			print("counts.min() != 0")

		# Estimate the fraction of *remaining* prior mass being eliminated
		frac = float(1 + counts[which])/self.num_particles

		# Copy out the scalars for returning purposes
		keep = self.all_scalars[which, :].copy()

		# Write discarded particle info to disk
		# log prior mass estimate, then scalars
		f = open('output.txt', 'a')
		logw = np.log(frac) + self.log_prior_mass
		line = str(logw)
		for s in self.all_scalars[which, :]:
			line += ' ' + str(s)
		f.write(line + '\n')
		f.close()

		# Reduce prior mass remaining
		self.log_prior_mass += np.log(1. - frac)

		# Forbid another rectangle
		self.forbidden_rectangles = np.vstack([self.forbidden_rectangles, \
										self.all_scalars[which, :]])
		self.refresh_particle(which)

		return keep

	def refresh_particle(self, which, mcmc_steps=1000):
		"""
		Replace a particle by cloning another and doing MCMC.
		"""
		# Choose a particle to clone
		copy = rng.randint(self.num_particles)
		while copy == which:
			copy = rng.randint(self.num_particles)

		# Clone it
		self.particles[which] = deepcopy(self.particles[copy])

		# Do MCMC
		num_accepted = 0
		for i in range(0, mcmc_steps):
			proposal = deepcopy(self.particles[which])
			logH = proposal.proposal()
			proposal_scalars = proposal.scalars

			if rng.rand() <= np.exp(logH) and not np.any\
				(Sampler.is_in_rectangle\
					(proposal_scalars, self.forbidden_rectangles)):
				self.particles[which] = proposal
				self.all_scalars[which, :] = proposal_scalars
				num_accepted += 1

		print('Iteration {i}. Accepted {a}/{b}.'.format(i=(self.iteration+1), a=num_accepted, b=mcmc_steps))
		self.iteration += 1

	@property
	def rectangle_counts(self):
		"""
		Count how many other particles are within the rectangle
		of each particle.
		"""
		counts = np.empty(self.num_particles, dtype='int64')
		for i in range(0, self.num_particles):
			counts[i] = np.sum(Sampler.is_in_rectangle\
									(self.all_scalars, self.all_scalars[i]))
		return counts

	@staticmethod
	def is_in_rectangle(scalars, rectangle):
		"""
		Is 'scalars' inside the rectangle defined by 'rectangle'?
		Can vectorise on one argument or the other by passing in a 2D array.
		"""
		return np.all(scalars < rectangle, axis=1)

if __name__ == '__main__':
	num_particles = 100
	depth = 50.			# How far to go
	sampler = Sampler(num_particles)
	sampler.initialise()

	plt.ion()
	plt.hold(True)

	for i in range(0, int(num_particles*depth)):
		keep = sampler.do_iteration()
		plt.plot(keep[0], keep[1], 'b.')
		plt.xlabel('Scalar 1')
		plt.ylabel('Scalar 2')
		plt.title('Discarded Points')
		plt.draw()

	plt.ioff()
	plt.show()

<commit_msg>rectangle_counts --> corner_counts<commit_after>import numpy as np
import numpy.random as rng
import matplotlib.pyplot as plt
from copy import deepcopy

from Particle import Particle

class Sampler:
	"""
	An object of this class is a sampler.
	"""

	def __init__(self, num_particles):
		"""
		Constructor: pass in the number of particles
		"""
		self.num_particles = num_particles
		self.particles = [Particle() for i in range(0, num_particles)]

		# Open and close output file to clear it
		f = open('output.txt', 'w')
		f.close()

		# Rectangles that have been forbidden so far
		self.forbidden_rectangles = None

		# Fraction of prior mass still in play
		self.log_prior_mass = 0.

	def initialise(self):
		"""
		Generate all the particles from the prior
		"""
		self.all_scalars = []	# Values of scalars for all particles
		for particle in self.particles:
			particle.from_prior()
			self.all_scalars.append(particle.scalars)
		self.all_scalars = np.array(self.all_scalars)
		self.forbidden_rectangles = np.empty((0, self.all_scalars.shape[1]))
		self.iteration = 0
		return

	def do_iteration(self):
		"""
		Do an NS iteration
		"""
		counts = self.corner_counts

		# Choose one with minimum count to discard
		temp = np.nonzero(counts == counts.min())[0]
		which = temp[rng.randint(len(temp))]

		# Need to figure out what happens in this case
		if counts.min() != 0:
			print("counts.min() != 0")

		# Estimate the fraction of *remaining* prior mass being eliminated
		frac = float(1 + counts[which])/self.num_particles

		# Copy out the scalars for returning purposes
		keep = self.all_scalars[which, :].copy()

		# Write discarded particle info to disk
		# log prior mass estimate, then scalars
		f = open('output.txt', 'a')
		logw = np.log(frac) + self.log_prior_mass
		line = str(logw)
		for s in self.all_scalars[which, :]:
			line += ' ' + str(s)
		f.write(line + '\n')
		f.close()

		# Reduce prior mass remaining
		self.log_prior_mass += np.log(1. - frac)

		# Forbid another rectangle
		self.forbidden_rectangles = np.vstack([self.forbidden_rectangles, \
										self.all_scalars[which, :]])
		self.refresh_particle(which)

		return keep

	def refresh_particle(self, which, mcmc_steps=1000):
		"""
		Replace a particle by cloning another and doing MCMC.
		"""
		# Choose a particle to clone
		copy = rng.randint(self.num_particles)
		while copy == which:
			copy = rng.randint(self.num_particles)

		# Clone it
		self.particles[which] = deepcopy(self.particles[copy])

		# Do MCMC
		num_accepted = 0
		for i in range(0, mcmc_steps):
			proposal = deepcopy(self.particles[which])
			logH = proposal.proposal()
			proposal_scalars = proposal.scalars

			if rng.rand() <= np.exp(logH) and not np.any\
				(Sampler.is_in_rectangle\
					(proposal_scalars, self.forbidden_rectangles)):
				self.particles[which] = proposal
				self.all_scalars[which, :] = proposal_scalars
				num_accepted += 1

		print('Iteration {i}. Accepted {a}/{b}.'.format(i=(self.iteration+1), a=num_accepted, b=mcmc_steps))
		self.iteration += 1

	@property
	def corner_counts(self):
		"""
		Count how many other particles are within the rectangle
		of each particle.
		"""
		counts = np.empty(self.num_particles, dtype='int64')
		for i in range(0, self.num_particles):
			counts[i] = np.sum(Sampler.is_in_rectangle\
									(self.all_scalars, self.all_scalars[i]))
		return counts

	@staticmethod
	def is_in_rectangle(scalars, rectangle):
		"""
		Is 'scalars' inside the rectangle defined by 'rectangle'?
		Can vectorise on one argument or the other by passing in a 2D array.
		"""
		return np.all(scalars < rectangle, axis=1)

if __name__ == '__main__':
	num_particles = 100
	depth = 50.			# How far to go
	sampler = Sampler(num_particles)
	sampler.initialise()

	plt.ion()
	plt.hold(True)

	for i in range(0, int(num_particles*depth)):
		keep = sampler.do_iteration()
		plt.plot(keep[0], keep[1], 'b.')
		plt.xlabel('Scalar 1')
		plt.ylabel('Scalar 2')
		plt.title('Discarded Points')
		plt.draw()

	plt.ioff()
	plt.show()

<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from django.core.management.base import BaseCommand
from django.utils.translation import activate

from kirppu.accounting import accounting_receipt

class Command(BaseCommand):
    help = 'Dump accounting CSV to standard output'

    def add_arguments(self, parser):
        parser.add_argument('--lang', type=str, help="Change language, for example: en")

    def handle(self, *args, **options):
        if "lang" in options:
            activate(options["lang"])
        accounting_receipt(self.stdout)
<commit_msg>Fix accounting data dump command.<commit_after># -*- coding: utf-8 -*-
from django.core.management.base import BaseCommand
from django.utils.translation import activate

from kirppu.accounting import accounting_receipt

class Command(BaseCommand):
    help = 'Dump accounting CSV to standard output'

    def add_arguments(self, parser):
        parser.add_argument('--lang', type=str, help="Change language, for example: en")
        parser.add_argument('event', type=str, help="Event slug to dump data for")

    def handle(self, *args, **options):
        if "lang" in options:
            activate(options["lang"])

        from kirppu.models import Event
        event = Event.objects.get(slug=options["event"])
        accounting_receipt(self.stdout, event)
<|endoftext|>


<commit_before># -*- encoding: utf-8 -*-
##############################################################################
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp import models, fields, api
import mock

class AccountInvoice(models.Model):

    _inherit = 'account.invoice'

    perception_ids = fields.One2many('account.invoice.perception', 'invoice_id', 'Percepciones')

    @api.onchange('perception_ids')
    def onchange_perception_ids(self):
        self._onchange_invoice_line_ids()

    @api.multi
    def get_taxes_values(self):
        """
        Agregamos a las lineas de impuestos las percepciones cargadas
        :return dict: Impuestos agrupados con importe y base {'tax': {'amount': '0.0', 'base': 0.0}}
        """
        res = super(AccountInvoice, self).get_taxes_values()

        for perception in self.perception_ids:
            tax = self.env['account.tax'].browse(perception.perception_id.tax_id.id)
            tax_vals = self._get_perception_tax_vals(perception)

            # El mock deberia ser una invoice.tax.line. Como no la necesitamos la mockeamos
            vals = self._prepare_tax_line_vals(mock.Mock(), tax_vals)
            key = self.env['account.tax'].browse(tax.id).get_grouping_key(vals)

            if key not in res:
                res[key] = vals
            else:
                res[key]['amount'] += vals['amount']
                res[key]['base'] += vals['base']

        return res

    @staticmethod
    def _get_perception_tax_vals(perception):
        """
        Obtiene los valores para crear impuestos desde la percepcion
        :param perception: account.perception.tax con los datos a obtener para setear los impuestos
        :return dict: Datos para crear account.invoice.tax
        """

        tax = perception.perception_id.tax_id
        vals = {
            'analytic': False,
            'amount': perception.amount,
            'base': perception.base,
            'name': tax.name,
            'account_id': tax.account_id.id,
            'refund_account_id': tax.refund_account_id.id,
            'id': tax.id,
            'sequence': 1
        }

        return vals

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
<commit_msg>[ADD] Cuando se realizan notas de credito desde la factura arrastra las percepciones<commit_after># -*- encoding: utf-8 -*-
##############################################################################
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp import models, fields, api
import mock

class AccountInvoice(models.Model):

    _inherit = 'account.invoice'

    perception_ids = fields.One2many('account.invoice.perception', 'invoice_id', 'Percepciones')

    @api.onchange('perception_ids')
    def onchange_perception_ids(self):
        self._onchange_invoice_line_ids()

    @api.multi
    def get_taxes_values(self):
        """
        Agregamos a las lineas de impuestos las percepciones cargadas
        :return dict: Impuestos agrupados con importe y base {'tax': {'amount': '0.0', 'base': 0.0}}
        """
        res = super(AccountInvoice, self).get_taxes_values()

        for perception in self.perception_ids:
            tax = self.env['account.tax'].browse(perception.perception_id.tax_id.id)
            tax_vals = self._get_perception_tax_vals(perception)

            # El mock deberia ser una invoice.tax.line. Como no la necesitamos la mockeamos
            vals = self._prepare_tax_line_vals(mock.Mock(), tax_vals)
            key = self.env['account.tax'].browse(tax.id).get_grouping_key(vals)

            if key not in res:
                res[key] = vals
            else:
                res[key]['amount'] += vals['amount']
                res[key]['base'] += vals['base']

        return res

    @staticmethod
    def _get_perception_tax_vals(perception):
        """
        Obtiene los valores para crear impuestos desde la percepcion
        :param perception: account.perception.tax con los datos a obtener para setear los impuestos
        :return dict: Datos para crear account.invoice.tax
        """

        tax = perception.perception_id.tax_id
        vals = {
            'analytic': False,
            'amount': perception.amount,
            'base': perception.base,
            'name': tax.name,
            'account_id': tax.account_id.id,
            'refund_account_id': tax.refund_account_id.id,
            'id': tax.id,
            'sequence': 1
        }

        return vals

    @api.model
    def _prepare_refund(self, invoice, date_invoice=None, date=None, description=None, journal_id=None):
        """ Agregamos las percepciones a las facturas que se cancelan con nota de credito """

        values = super(AccountInvoice, self)._prepare_refund(invoice, date_invoice, date, description, journal_id)
        values['perception_ids'] = self._refund_cleanup_lines(invoice.perception_ids)

        return values

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
# Copyright (C) 2016-Today - KMEE (<http://kmee.com.br>).
#  Luis Felipe Miléo - mileo@kmee.com.br
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
    'name': 'Brazilian Payment Order',
    'version': '10.0.2.0.0',
    'license': 'AGPL-3',
    'author': "KMEE, "
              "Odoo Community Association (OCA)",
    'website': 'https://github.com/OCA/l10n-brazil',
    'category': 'Banking addons',
    'depends': [
        'l10n_br_account',
        'account_payment_order',
        'account_due_list',
    ],
    'data': [
        'views/account_due_list.xml',
        'views/account_payment_line.xml',
        'views/account_payment_mode.xml',
    ],
    'demo': [
        'demo/account_payment_order_demo.xml',
        'demo/account_payment_mode_demo.xml'
    ],
    'installable': True,
}
<commit_msg>[12.0][MIG] Updated version and dependencies.<commit_after># -*- coding: utf-8 -*-
# Copyright (C) 2016-Today - KMEE (<http://kmee.com.br>).
#  Luis Felipe Miléo - mileo@kmee.com.br
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
    'name': 'Brazilian Payment Order',
    'version': '12.0.1.0.0',
    'license': 'AGPL-3',
    'author': "KMEE, "
              "Odoo Community Association (OCA)",
    'website': 'https://github.com/OCA/l10n-brazil',
    'category': 'Banking addons',
    'depends': [
        'l10n_br',
        'l10n_br_base',
        'account_payment_order',
        'account_due_list',
    ],
    'data': [
        'views/account_due_list.xml',
        'views/account_payment_line.xml',
        'views/account_payment_mode.xml',
    ],
    'demo': [
        'demo/account_payment_order_demo.xml',
        'demo/account_payment_mode_demo.xml'
    ],
    'installable': True,
}
<|endoftext|>


<commit_before>from __future__ import print_function, division, absolute_import
import re
import sys
from os.path import isdir, isfile, join

from conda.compat import iteritems
from conda.utils import memoized, md5_file
import conda.config as config
from conda.resolve import MatchSpec

from conda_build.config import CONDA_PY, CONDA_NPY
import os

def ns_cfg():
    # Remember to update the docs of any of this changes
    plat = config.subdir
    py = CONDA_PY
    np = CONDA_NPY
    for x in py, np:
        assert isinstance(x, int), x
    return dict(
        linux = plat.startswith('linux-'),
        linux32 = bool(plat == 'linux-32'),
        linux64 = bool(plat == 'linux-64'),
        armv6 = bool(plat == 'linux-armv6l'),
        osx = plat.startswith('osx-'),
        unix = plat.startswith(('linux-', 'osx-')),
        win = plat.startswith('win-'),
        win32 = bool(plat == 'win-32'),
        win64 = bool(plat == 'win-64'),
        py = py,
        py3k = bool(30 <= py < 40),
        py2k = bool(20 <= py < 30),
        py26 = bool(py == 26),
        py27 = bool(py == 27),
        py33 = bool(py == 33),
        np = np,
    )

sel_pat = re.compile(r'(.+?)\s*\[(.+)\]$')
def select_lines(data, namespace):
    lines = []
    for i, line in enumerate(data.splitlines()):
        line = line.rstrip()
        m = sel_pat.match(line)
        if m:
            cond = m.group(2)
            try:
                if eval(cond, namespace, {}):
                    lines.append(m.group(1))
            except:
                sys.exit('''\
Error: Invalid selector in meta.yaml line %d:
%s
''' % (i + 1, line))
                sys.exit(1)
            continue
        lines.append(line)
    return '\n'.join(lines) + '\n'

@memoized
def yamlize(data):
    try:
        import yaml
    except ImportError:
        sys.exit('Error: could not import yaml (required to read meta.yaml '
                 'files of conda recipes)')

    return yaml.load(data)

def parse(data):
    data = select_lines(data, ns_cfg())
    res = yamlize(data)
    # ensure the result is a dict
    if res is None:
        res = {}
    # ensure those are lists
    for field in ('source/patches',
                  'build/entry_points',
                  'build/features', 'build/track_features',
                  'requirements/build', 'requirements/run',
                  'requirements/conflicts', 'test/requires',
                  'test/files', 'test/commands', 'test/imports'):
        section, key = field.split('/')
        if res.get(section) is None:
            res[section] = {}
        if res[section].get(key, None) is None:
            res[section][key] = []
    # ensure those are strings
    for field in ('package/version', 'build/string', 'source/svn_rev',
                  'source/git_tag', 'source/git_branch', 'source/md5'):
        section, key = field.split('/')
        if res.get(section) is None:
            res[section] = {}
        res[section][key] = str(res[section].get(key, ''))
    return res

FIELDS = {
    'package': ['name', 'version'],
    'source': ['fn', 'url', 'md5', 'sha1',
               'git_url', 'git_tag', 'git_branch',
               'hg_url', 'hg_tag',
               'svn_url', 'svn_rev', 'svn_ignore_externals',
               'patches'],
    'build': ['number', 'string', 'entry_points', 'osx_is_app', 'rm_py',
              'features', 'track_features', 'preserve_egg_dir',
              'no_softlink', 'script'],
    'requirements': ['build', 'run', 'conflicts'],
    'app': ['entry', 'icon', 'summary', 'type', 'cli_opts'],
    'test': ['requires', 'commands', 'files', 'imports'],
    'about': ['home', 'license', 'summary'],
}

def check_bad_chrs(s, field):
    bad_chrs = '=!@#$%^&*:;"\'\\|<>?/ '
    if field in ('package/version', 'build/string'):
        bad_chrs += '-'
    for c in bad_chrs:
        if c in s:
            sys.exit("Error: bad character '%s' in %s: %s" % (c, field, s))

def get_contents(meta_path):
    '''
    Get the contents of the [meta.yaml|conda.yaml] file.
    If jinja is installed, then the template.render function is called
    before standard conda macro processors
    '''
    try:
        import jinja2
    except ImportError:
        print("There was an error importing jinja2.")
        print("Please run `conda install jinja2` to enable jinja template support")
        with open(meta_path) as fd:
            return fd.read()

    from conda_build.jinja_context import context_processor

    path, filename = os.path.split(meta_path)
    loaders = [jinja2.PackageLoader('conda_build'),
               jinja2.FileSystemLoader(path)
               ]
    env = jinja2.Environment(loader=jinja2.ChoiceLoader(loaders))
    env.globals.update(context_processor())

    template = env.get_or_select_template(filename)

    contents = template.render(environment=env)
    return contents

class MetaData(object):

    def __init__(self, path):
        assert isdir(path)
        self.path = path
        self.meta_path = join(path, 'meta.yaml')
        if not isfile(self.meta_path):
            self.meta_path = join(path, 'conda.yaml')
            if not isfile(self.meta_path):
                sys.exit("Error: meta.yaml or conda.yaml not found in %s" % path)

        self.meta = parse(get_contents(self.meta_path))

    def get_section(self, section):
        return self.meta.get(section, {})

    def get_value(self, field, default=None):
        section, key = field.split('/')
        return self.get_section(section).get(key, default)

    def check_fields(self):
        for section, submeta in iteritems(self.meta):
            if section not in FIELDS:
                sys.exit("Error: unknown section: %s" % section)
            for key in submeta:
                if key not in FIELDS[section]:
                    sys.exit("Error: in section %r: unknown key %r" %
                             (section, key))

    def name(self):
        res = self.get_value('package/name')
        if not res:
            sys.exit('Error: package/name missing in: %r' % self.meta_path)
        res = str(res)
        if res != res.lower():
            sys.exit('Error: package/name must be lowercase, got: %r' % res)
        check_bad_chrs(res, 'package/name')
        return res

    def version(self):
        res = self.get_value('package/version')
        check_bad_chrs(res, 'package/version')
        return res

    def build_number(self):
        return int(self.get_value('build/number', 0))

    def ms_depends(self, typ='run'):
        res = []
        for spec in self.get_value('requirements/' + typ):
            try:
                ms = MatchSpec(spec)
            except AssertionError:
                raise RuntimeError("Invalid package specification: %r" % spec)
            for name, ver in [('python', CONDA_PY), ('numpy', CONDA_NPY)]:
                if ms.name == name:
                    if ms.strictness != 1:
                        sys.exit("""Error:
    You cannot specify a version for package '%s' in the requirements.
    Please use the environment variables CONDA_PY or CONDA_NPY.
""" % name)
                    ms = MatchSpec('%s %s*' % (name, '.'.join(str(ver))))
            res.append(ms)
        return res

    def build_id(self):
        ret = self.get_value('build/string')
        if ret:
            check_bad_chrs(ret, 'build/string')
            return ret
        res = []
        for name, s in (('numpy', 'np'), ('python', 'py')):
            for ms in self.ms_depends():
                if ms.name == name:
                    v = ms.spec.split()[1]
                    res.append(s + v[0] + v[2])
                    break
        if res:
            res.append('_')
        res.append('%d' % self.build_number())
        return ''.join(res)

    def dist(self):
        return '%s-%s-%s' % (self.name(), self.version(), self.build_id())

    def is_app(self):
        return bool(self.get_value('app/entry'))

    def app_meta(self):
        d = {'type': 'app'}
        if self.get_value('app/icon'):
            d['icon'] = '%s.png' % md5_file(join(
                    self.path, self.get_value('app/icon')))

        for field, key in [('app/entry', 'app_entry'),
                           ('app/type', 'app_type'),
                           ('app/cli_opts', 'app_cli_opts'),
                           ('app/summary', 'summary')]:
            value = self.get_value(field)
            if value:
                d[key] = value
        return d

    def info_index(self):
        d = dict(
            name = self.name(),
            version = self.version(),
            build = self.build_id(),
            build_number = self.build_number(),
            license = self.get_value('about/license'),
            platform = config.platform,
            arch = config.arch_name,
            depends = sorted(ms.spec for ms in self.ms_depends())
        )
        if self.is_app():
            d.update(self.app_meta())
        return d

if __name__ == '__main__':
    from pprint import pprint
    from os.path import expanduser

    m = MetaData(expanduser('~/conda-recipes/pycosat'))
    pprint(m.info_index())
<commit_msg>Add a comment<commit_after>from __future__ import print_function, division, absolute_import
import re
import sys
from os.path import isdir, isfile, join

from conda.compat import iteritems
from conda.utils import memoized, md5_file
import conda.config as config
from conda.resolve import MatchSpec

from conda_build.config import CONDA_PY, CONDA_NPY
import os

def ns_cfg():
    # Remember to update the docs of any of this changes
    plat = config.subdir
    py = CONDA_PY
    np = CONDA_NPY
    for x in py, np:
        assert isinstance(x, int), x
    return dict(
        linux = plat.startswith('linux-'),
        linux32 = bool(plat == 'linux-32'),
        linux64 = bool(plat == 'linux-64'),
        armv6 = bool(plat == 'linux-armv6l'),
        osx = plat.startswith('osx-'),
        unix = plat.startswith(('linux-', 'osx-')),
        win = plat.startswith('win-'),
        win32 = bool(plat == 'win-32'),
        win64 = bool(plat == 'win-64'),
        py = py,
        py3k = bool(30 <= py < 40),
        py2k = bool(20 <= py < 30),
        py26 = bool(py == 26),
        py27 = bool(py == 27),
        py33 = bool(py == 33),
        np = np,
    )

sel_pat = re.compile(r'(.+?)\s*\[(.+)\]$')
def select_lines(data, namespace):
    lines = []
    for i, line in enumerate(data.splitlines()):
        line = line.rstrip()
        m = sel_pat.match(line)
        if m:
            cond = m.group(2)
            try:
                if eval(cond, namespace, {}):
                    lines.append(m.group(1))
            except:
                sys.exit('''\
Error: Invalid selector in meta.yaml line %d:
%s
''' % (i + 1, line))
                sys.exit(1)
            continue
        lines.append(line)
    return '\n'.join(lines) + '\n'

@memoized
def yamlize(data):
    try:
        import yaml
    except ImportError:
        sys.exit('Error: could not import yaml (required to read meta.yaml '
                 'files of conda recipes)')

    return yaml.load(data)

def parse(data):
    data = select_lines(data, ns_cfg())
    res = yamlize(data)
    # ensure the result is a dict
    if res is None:
        res = {}
    # ensure those are lists
    for field in ('source/patches',
                  'build/entry_points',
                  'build/features', 'build/track_features',
                  'requirements/build', 'requirements/run',
                  'requirements/conflicts', 'test/requires',
                  'test/files', 'test/commands', 'test/imports'):
        section, key = field.split('/')
        if res.get(section) is None:
            res[section] = {}
        if res[section].get(key, None) is None:
            res[section][key] = []
    # ensure those are strings
    for field in ('package/version', 'build/string', 'source/svn_rev',
                  'source/git_tag', 'source/git_branch', 'source/md5'):
        section, key = field.split('/')
        if res.get(section) is None:
            res[section] = {}
        res[section][key] = str(res[section].get(key, ''))
    return res

# If you update this please update the example in conda-docs/docs/source/build.rst
FIELDS = {
    'package': ['name', 'version'],
    'source': ['fn', 'url', 'md5', 'sha1',
               'git_url', 'git_tag', 'git_branch',
               'hg_url', 'hg_tag',
               'svn_url', 'svn_rev', 'svn_ignore_externals',
               'patches'],
    'build': ['number', 'string', 'entry_points', 'osx_is_app', 'rm_py',
              'features', 'track_features', 'preserve_egg_dir',
              'no_softlink', 'script'],
    'requirements': ['build', 'run', 'conflicts'],
    'app': ['entry', 'icon', 'summary', 'type', 'cli_opts'],
    'test': ['requires', 'commands', 'files', 'imports'],
    'about': ['home', 'license', 'summary'],
}

def check_bad_chrs(s, field):
    bad_chrs = '=!@#$%^&*:;"\'\\|<>?/ '
    if field in ('package/version', 'build/string'):
        bad_chrs += '-'
    for c in bad_chrs:
        if c in s:
            sys.exit("Error: bad character '%s' in %s: %s" % (c, field, s))

def get_contents(meta_path):
    '''
    Get the contents of the [meta.yaml|conda.yaml] file.
    If jinja is installed, then the template.render function is called
    before standard conda macro processors
    '''
    try:
        import jinja2
    except ImportError:
        print("There was an error importing jinja2.")
        print("Please run `conda install jinja2` to enable jinja template support")
        with open(meta_path) as fd:
            return fd.read()

    from conda_build.jinja_context import context_processor

    path, filename = os.path.split(meta_path)
    loaders = [jinja2.PackageLoader('conda_build'),
               jinja2.FileSystemLoader(path)
               ]
    env = jinja2.Environment(loader=jinja2.ChoiceLoader(loaders))
    env.globals.update(context_processor())

    template = env.get_or_select_template(filename)

    contents = template.render(environment=env)
    return contents

class MetaData(object):

    def __init__(self, path):
        assert isdir(path)
        self.path = path
        self.meta_path = join(path, 'meta.yaml')
        if not isfile(self.meta_path):
            self.meta_path = join(path, 'conda.yaml')
            if not isfile(self.meta_path):
                sys.exit("Error: meta.yaml or conda.yaml not found in %s" % path)

        self.meta = parse(get_contents(self.meta_path))

    def get_section(self, section):
        return self.meta.get(section, {})

    def get_value(self, field, default=None):
        section, key = field.split('/')
        return self.get_section(section).get(key, default)

    def check_fields(self):
        for section, submeta in iteritems(self.meta):
            if section not in FIELDS:
                sys.exit("Error: unknown section: %s" % section)
            for key in submeta:
                if key not in FIELDS[section]:
                    sys.exit("Error: in section %r: unknown key %r" %
                             (section, key))

    def name(self):
        res = self.get_value('package/name')
        if not res:
            sys.exit('Error: package/name missing in: %r' % self.meta_path)
        res = str(res)
        if res != res.lower():
            sys.exit('Error: package/name must be lowercase, got: %r' % res)
        check_bad_chrs(res, 'package/name')
        return res

    def version(self):
        res = self.get_value('package/version')
        check_bad_chrs(res, 'package/version')
        return res

    def build_number(self):
        return int(self.get_value('build/number', 0))

    def ms_depends(self, typ='run'):
        res = []
        for spec in self.get_value('requirements/' + typ):
            try:
                ms = MatchSpec(spec)
            except AssertionError:
                raise RuntimeError("Invalid package specification: %r" % spec)
            for name, ver in [('python', CONDA_PY), ('numpy', CONDA_NPY)]:
                if ms.name == name:
                    if ms.strictness != 1:
                        sys.exit("""Error:
    You cannot specify a version for package '%s' in the requirements.
    Please use the environment variables CONDA_PY or CONDA_NPY.
""" % name)
                    ms = MatchSpec('%s %s*' % (name, '.'.join(str(ver))))
            res.append(ms)
        return res

    def build_id(self):
        ret = self.get_value('build/string')
        if ret:
            check_bad_chrs(ret, 'build/string')
            return ret
        res = []
        for name, s in (('numpy', 'np'), ('python', 'py')):
            for ms in self.ms_depends():
                if ms.name == name:
                    v = ms.spec.split()[1]
                    res.append(s + v[0] + v[2])
                    break
        if res:
            res.append('_')
        res.append('%d' % self.build_number())
        return ''.join(res)

    def dist(self):
        return '%s-%s-%s' % (self.name(), self.version(), self.build_id())

    def is_app(self):
        return bool(self.get_value('app/entry'))

    def app_meta(self):
        d = {'type': 'app'}
        if self.get_value('app/icon'):
            d['icon'] = '%s.png' % md5_file(join(
                    self.path, self.get_value('app/icon')))

        for field, key in [('app/entry', 'app_entry'),
                           ('app/type', 'app_type'),
                           ('app/cli_opts', 'app_cli_opts'),
                           ('app/summary', 'summary')]:
            value = self.get_value(field)
            if value:
                d[key] = value
        return d

    def info_index(self):
        d = dict(
            name = self.name(),
            version = self.version(),
            build = self.build_id(),
            build_number = self.build_number(),
            license = self.get_value('about/license'),
            platform = config.platform,
            arch = config.arch_name,
            depends = sorted(ms.spec for ms in self.ms_depends())
        )
        if self.is_app():
            d.update(self.app_meta())
        return d

if __name__ == '__main__':
    from pprint import pprint
    from os.path import expanduser

    m = MetaData(expanduser('~/conda-recipes/pycosat'))
    pprint(m.info_index())
<|endoftext|>


<commit_before>import json

with open("/etc/ssowat/conf.json.persistent", "r") as jsonFile:
    data = json.load(jsonFile)
    data["unprotected_urls"].remove("/seafhttp")
    data["unprotected_urls"].remove("/seafdav")

with open("/etc/ssowat/conf.json.persistent", "w") as jsonFile:
    jsonFile.write(json.dumps(data, indent=4, sort_keys=True))<commit_msg>Update remove_sso_conf.py<commit_after>import json

with open("/etc/ssowat/conf.json.persistent", "r") as jsonFile:
    data = json.load(jsonFile)
    data["skipped_urls"].remove("/seafhttp")
    data["skipped_urls"].remove("/seafdav")

with open("/etc/ssowat/conf.json.persistent", "w") as jsonFile:
    jsonFile.write(json.dumps(data, indent=4, sort_keys=True))
<|endoftext|>


<commit_before><commit_msg>Yo B why this in't workn?<commit_after><|endoftext|>


<commit_before><commit_msg>Fix extra ) syntax error<commit_after><|endoftext|>


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

try:
    from StringIO import StringIO
except ImportError:
    from io import StringIO

import numpy as np

from sebastian.lilypond.interp import parse
from sebastian.midi.write_midi import SMF
from sebastian.core.transforms import stretch
from sebastian.core import notes

def note_classes(arr, scale):
    '''
    Get note classes from data range.

    Parameters
    ----------
    arr : arr
        array to be arranged as note classes.
    scale : an `build_scale` object
        Consists of a Tone scaled. (C maj, pentatonic C, C min, etc.)

    Returns
    -------
    Parametreized values of musical notes based on input array.
    '''
    minr = np.nanmin(arr)
    maxr = np.nanmax(arr)
    _, bins = np.histogram(arr, bins=len(scale) - 1, range=(minr, maxr))
    return bins

def note_number(arr, scale):
    '''
    Get a relative number of notes, included in a choiced scale.

    Parameters
    ----------
    arr : arr
        array to be arranged as note classes.
    scale : an `build_scale` object

    Returns
    -------
    mapping : arr
        Note number of input array parameterized with choiced scale.
        Note number follows the Sebastian sequence, and it can be
        visualized for any number with:
        sebastian.core.notes.name('2') will return musical note "E".
    '''
    x_notes = note_classes(arr, scale)
    mapping = np.searchsorted(x_notes, arr, side='left').astype('f8')
    mapping[np.isnan(arr)] = np.nan
    return mapping

def note_on_classes(note, arr, scale):
    if np.isnan(note):
        return np.nan

    x_notes = note_classes(arr, scale)
    return np.searchsorted(x_notes, note, side='left').astype('f8')

def pentatonic_scale(tonic):
    '''
    Pentatonic scale, based on Major Pentatonic.
    Not implemented on Sebastian.

    References
    ----------
    http://en.wikipedia.org/wiki/Pentatonic_scale
    '''
    return [tonic + i for i in [0, 2, 4, 1, 3]]

def blues_scale(tonic):
    '''
    Blues scale

    References
    ----------
    http://en.wikipedia.org/wiki/Blues_scale
    '''
    return [tonic + i for i in [0, 2, 4, -1, 1, 3, 5]]

def build_scale(key, mode='major', octaves=1):
    '''
    Build a scale from a refference note.

    Parameters
    ----------
    key : Musical key.
        Can be setted as a parameter while building scale.
        Key should be written as "C", for C and "C#" for C sharp and
        "Cb" for C flat.

    mode : Musica mode.
        'major' and 'minor' and 'pentatonic' are accetable parameters.

    octaves : int
        number of octaves to be evaluated.

    Returns
    -------
    scale_notes : sebastian.core.elements
        Sequence of scale notes.
    '''
    if mode == 'major':
        scale = notes.major_scale
    elif mode == 'minor':
        scale = notes.minor_scale
    elif mode == 'pentatonic':
        scale = pentatonic_scale
    elif mode == 'blues':
        scale = blues_scale

    scale_notes = [notes.name(s).lower() + ("'" * octave)
                   for octave in range(octaves)
                   for s in scale(notes.value(key))]
    return scale_notes

def note_name(number, scale):
    '''
    Transform a number to a note string, including np.nan as musical rests.
    '''
    if np.isnan(number):
        return "r"
    else:
        return scale[int(number)].replace('#', 'is')

def chord_scaled(arr, scale, period=12):
    '''
    Scales an note's array
    '''
    remainder = arr.size % period
    if remainder:
        fill = period - remainder
        arr = np.append(arr, np.zeros(fill) * np.nan)

    arr_scaled = np.int32([np.nansum(row) / len(row)
                           for row in arr.reshape((-1, period))])

    root_scaled = [note_on_classes(note, arr, scale) for note in arr_scaled]
    root = []
    third = []
    fifth = []
    for note in root_scaled:
        root.append(note_name(note, scale))
        third.append(note_name(note, scale))
        fifth.append(note_name(note, scale))

    seq1 = parse(" ".join(root))
    seq2 = parse(" ".join(third))
    seq3 = parse(" ".join(fifth))

    # chords = (seq1 * period) // (seq2 * period) // (seq3 * period)
    chords = seq1 // seq2 // seq3
    # return (chords | add({DURATION_64: chords[0][DURATION_64] * period}))
    return (chords | stretch(period))
    # return chords

def get_music(series, key='C', mode='major', octaves=2,
              instruments=None, period=12):
    '''
    Return a music generated from an inserted series.

    Parameters
    ----------
    series : an array that could be an 2d-array.

    key : Musical key.
        Can be setted as a parameter while building scale.
        Key should be written as "C", for C and "C#" for C sharp and
        "Cb" for C flat.

    mode : Musica mode.
        'major' and 'minor' and 'pentatonic' are accetable parameters.

    octaves : Number of available scales for musical construction.
        As higher are the octaves higher pitch differeneces will occur
        while representing your data.

    instruments : list of MIDI instruments.
        General MIDI Level 1 Instrument Patch Map can be found at:
        http://en.wikipedia.org/wiki/General_MIDI
        Gran Piano is the default usage value '[0]' if any instruments
        are declared.
        Fewer examples:
        [0] Acoustic Grand Piano
        [18] Rock Organ
        [23] Tango Accordion
        [32] Acoustic Bass
        [73] Flute

    period : int
        parameter of chord_scaled function.

    Returns
    -------
    midi_out : StringIO object.
        It can be written on a file or used by your way.

    Example
    -------
    >>> serie = np.random.random(10).reshape(2,5)
    array([[ 0.13536875,  0.42212475,  0.26360219,  0.30153336,  0.62150923],
       [ 0.49384405,  0.32503762,  0.85549822,  0.80212442,  0.70702405]])

    >>> get_music(serie, octaves=2, instruments=(0,23))
    <StringIO.StringIO instance at 0x7f98201c9d40>

    '''
    midi_out = StringIO()

    series = np.array(series)
    scale = build_scale(key, mode, octaves)
    melodies = []
    for i in range(series.shape[0]):
        if all(np.isnan(series[i])):
            melody = []
            melodies.append(melody)
        else:
            notes = note_number(series[i], scale)
            melody = parse(' '.join([note_name(x, scale) for x in notes]))
            melodies.append(melody)

        # chords = chord_scaled(series, scale, period)
        # Transform it to a MIDI file with chords.
        # s = SMF([melody, chords], instruments=[0, 23])
    melodies = np.array(melodies)
    if instruments is None:
        s = SMF(melodies)
    else:
        s = SMF(melodies, instruments)

    s.write(midi_out)
    return midi_out
<commit_msg>Remove useless lines<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

try:
    from StringIO import StringIO
except ImportError:
    from io import StringIO

import numpy as np

from sebastian.lilypond.interp import parse
from sebastian.midi.write_midi import SMF
from sebastian.core.transforms import stretch
from sebastian.core import notes

def note_classes(arr, scale):
    '''
    Get note classes from data range.

    Parameters
    ----------
    arr : arr
        array to be arranged as note classes.
    scale : an `build_scale` object
        Consists of a Tone scaled. (C maj, pentatonic C, C min, etc.)

    Returns
    -------
    Parametreized values of musical notes based on input array.
    '''
    minr = np.nanmin(arr)
    maxr = np.nanmax(arr)
    _, bins = np.histogram(arr, bins=len(scale) - 1, range=(minr, maxr))
    return bins

def note_number(arr, scale):
    '''
    Get a relative number of notes, included in a choiced scale.

    Parameters
    ----------
    arr : arr
        array to be arranged as note classes.
    scale : an `build_scale` object

    Returns
    -------
    mapping : arr
        Note number of input array parameterized with choiced scale.
        Note number follows the Sebastian sequence, and it can be
        visualized for any number with:
        sebastian.core.notes.name('2') will return musical note "E".
    '''
    x_notes = note_classes(arr, scale)
    mapping = np.searchsorted(x_notes, arr, side='left').astype('f8')
    mapping[np.isnan(arr)] = np.nan
    return mapping

def note_on_classes(note, arr, scale):
    if np.isnan(note):
        return np.nan

    x_notes = note_classes(arr, scale)
    return np.searchsorted(x_notes, note, side='left').astype('f8')

def pentatonic_scale(tonic):
    '''
    Pentatonic scale, based on Major Pentatonic.
    Not implemented on Sebastian.

    References
    ----------
    http://en.wikipedia.org/wiki/Pentatonic_scale
    '''
    return [tonic + i for i in [0, 2, 4, 1, 3]]

def blues_scale(tonic):
    '''
    Blues scale

    References
    ----------
    http://en.wikipedia.org/wiki/Blues_scale
    '''
    return [tonic + i for i in [0, 2, 4, -1, 1, 3, 5]]

def build_scale(key, mode='major', octaves=1):
    '''
    Build a scale from a refference note.

    Parameters
    ----------
    key : Musical key.
        Can be setted as a parameter while building scale.
        Key should be written as "C", for C and "C#" for C sharp and
        "Cb" for C flat.

    mode : Musica mode.
        'major' and 'minor' and 'pentatonic' are accetable parameters.

    octaves : int
        number of octaves to be evaluated.

    Returns
    -------
    scale_notes : sebastian.core.elements
        Sequence of scale notes.
    '''
    if mode == 'major':
        scale = notes.major_scale
    elif mode == 'minor':
        scale = notes.minor_scale
    elif mode == 'pentatonic':
        scale = pentatonic_scale
    elif mode == 'blues':
        scale = blues_scale

    scale_notes = [notes.name(s).lower() + ("'" * octave)
                   for octave in range(octaves)
                   for s in scale(notes.value(key))]
    return scale_notes

def note_name(number, scale):
    '''
    Transform a number to a note string, including np.nan as musical rests.
    '''
    if np.isnan(number):
        return "r"
    else:
        return scale[int(number)].replace('#', 'is')

def chord_scaled(arr, scale, period=12):
    '''
    Scales an note's array
    '''
    remainder = arr.size % period
    if remainder:
        fill = period - remainder
        arr = np.append(arr, np.zeros(fill) * np.nan)

    arr_scaled = np.int32([np.nansum(row) / len(row)
                           for row in arr.reshape((-1, period))])

    root_scaled = [note_on_classes(note, arr, scale) for note in arr_scaled]
    root = []
    third = []
    fifth = []
    for note in root_scaled:
        root.append(note_name(note, scale))
        third.append(note_name(note, scale))
        fifth.append(note_name(note, scale))

    seq1 = parse(" ".join(root))
    seq2 = parse(" ".join(third))
    seq3 = parse(" ".join(fifth))

    # chords = (seq1 * period) // (seq2 * period) // (seq3 * period)
    chords = seq1 // seq2 // seq3
    # return (chords | add({DURATION_64: chords[0][DURATION_64] * period}))
    return (chords | stretch(period))
    # return chords

def get_music(series, key='C', mode='major', octaves=2,
              instruments=None, period=12):
    '''
    Return a music generated from an inserted series.

    Parameters
    ----------
    series : an array that could be an 2d-array.

    key : Musical key.
        Can be setted as a parameter while building scale.
        Key should be written as "C", for C and "C#" for C sharp and
        "Cb" for C flat.

    mode : Musica mode.
        'major' and 'minor' and 'pentatonic' are accetable parameters.

    octaves : Number of available scales for musical construction.
        As higher are the octaves higher pitch differeneces will occur
        while representing your data.

    instruments : list of MIDI instruments.
        General MIDI Level 1 Instrument Patch Map can be found at:
        http://en.wikipedia.org/wiki/General_MIDI
        Gran Piano is the default usage value '[0]' if any instruments
        are declared.
        Fewer examples:
        [0] Acoustic Grand Piano
        [18] Rock Organ
        [23] Tango Accordion
        [32] Acoustic Bass
        [73] Flute

    period : int
        parameter of chord_scaled function.

    Returns
    -------
    midi_out : StringIO object.
        It can be written on a file or used by your way.

    Example
    -------
    >>> serie = np.random.random(10).reshape(2,5)
    array([[ 0.13536875,  0.42212475,  0.26360219,  0.30153336,  0.62150923],
       [ 0.49384405,  0.32503762,  0.85549822,  0.80212442,  0.70702405]])

    >>> get_music(serie, octaves=2, instruments=(0,23))
    <StringIO.StringIO instance at 0x7f98201c9d40>

    '''
    midi_out = StringIO()

    series = np.array(series)
    scale = build_scale(key, mode, octaves)
    melodies = []
    for i in range(series.shape[0]):
        if all(np.isnan(series[i])):
            melody = []
            melodies.append(melody)
        else:
            notes = note_number(series[i], scale)
            melody = parse(' '.join([note_name(x, scale) for x in notes]))
            melodies.append(melody)

        # chords = chord_scaled(series, scale, period)
        # Transform it to a MIDI file with chords.
        # s = SMF([melody, chords], instruments=[0, 23])
    if instruments is None:
        s = SMF(melodies)
    else:
        s = SMF(melodies, instruments)

    s.write(midi_out)
    return midi_out
<|endoftext|>


<commit_before><commit_msg>Simulator.py icons back to Unicode<commit_after><|endoftext|>


<commit_before><commit_msg>Publish attribute types at top-level<commit_after><|endoftext|>


<commit_before><commit_msg>Commented out section setting rivids of -1 to zero<commit_after><|endoftext|>


<commit_before>from __future__ import division, print_function, absolute_import

from functools import reduce

import numpy as np
import numpy.testing as npt

from dipy.reconst.multi_voxel import _squash, multi_voxel_fit, CallableArray
from dipy.core.sphere import unit_icosahedron

def test_squash():
    A = np.ones((3, 3), dtype=float)
    B = np.asarray(A, object)
    npt.assert_array_equal(A, _squash(B))
    npt.assert_equal(_squash(B).dtype, A.dtype)

    B[2, 2] = None
    A[2, 2] = 0
    npt.assert_array_equal(A, _squash(B))
    npt.assert_equal(_squash(B).dtype, A.dtype)

    for ijk in np.ndindex(*B.shape):
        B[ijk] = np.ones((2,))
    A = np.ones((3, 3, 2))
    npt.assert_array_equal(A, _squash(B))
    npt.assert_equal(_squash(B).dtype, A.dtype)

    B[2, 2] = None
    A[2, 2] = 0
    npt.assert_array_equal(A, _squash(B))
    npt.assert_equal(_squash(B).dtype, A.dtype)

    # sub-arrays have different shapes ( (3,) and (2,) )
    B[0, 0] = np.ones((3,))
    npt.assert_(_squash(B) is B)

    # Check dtypes for arrays and scalars
    arr_arr = np.zeros((2,), dtype=object)
    scalar_arr = np.zeros((2,), dtype=object)
    numeric_types = sum(
        [np.sctypes[t] for t in ('int', 'uint', 'float', 'complex')],
        [np.bool_])
    for dt0 in numeric_types:
        arr_arr[0] = np.zeros((3,), dtype=dt0)
        scalar_arr[0] = dt0(0)
        for dt1 in numeric_types:
            arr_arr[1] = np.zeros((3,), dtype=dt1)
            npt.assert_equal(_squash(arr_arr).dtype,
                             reduce(np.add, arr_arr).dtype)
            scalar_arr[1] = dt0(1)
            npt.assert_equal(_squash(scalar_arr).dtype,
                             reduce(np.add, scalar_arr).dtype)

    # Check masks and Nones
    arr = np.ones((3, 4), dtype=float)
    obj_arr = arr.astype(object)
    arr[1, 1] = 99
    obj_arr[1, 1] = None
    npt.assert_array_equal(_squash(obj_arr, mask=None, fill=99), arr)
    msk = arr == 1
    npt.assert_array_equal(_squash(obj_arr, mask=msk, fill=99), arr)
    msk[1, 1] = 1 # unmask None - object array back
    npt.assert_array_equal(_squash(obj_arr, mask=msk, fill=99), obj_arr)
    msk[1, 1] = 0 # remask, back to fill again
    npt.assert_array_equal(_squash(obj_arr, mask=msk, fill=99), arr)
    obj_arr[2, 3] = None # add another unmasked None, object again
    npt.assert_array_equal(_squash(obj_arr, mask=msk, fill=99), obj_arr)

    # Check array of arrays
    obj_arrs = np.zeros((3,), dtype=object)
    for i in range(3):
        obj_arrs[i] = np.ones((4, 5))
    arr_arrs = np.ones((3, 4, 5))
    # No Nones
    npt.assert_array_equal(_squash(obj_arrs, mask=None, fill=99), arr_arrs)
    # None, implicit masking
    obj_masked = obj_arrs.copy()
    obj_masked[1] = None
    arr_masked = arr_arrs.copy()
    arr_masked[1] = 99
    npt.assert_array_equal(_squash(obj_masked, mask=None, fill=99),
                           arr_masked)
    msk = np.array([1, 0, 1], dtype=np.bool_) # explicit mask
    npt.assert_array_equal(_squash(obj_masked, mask=msk, fill=99),
                           arr_masked)
    msk[1] = True # unmask None, object array back
    npt.assert_array_equal(_squash(obj_masked, mask=msk, fill=99),
                           obj_masked)

def test_CallableArray():
    callarray = CallableArray((2, 3), dtype=object)

    # Test without Nones
    callarray[:] = np.arange
    expected = np.empty([2, 3, 4])
    expected[:] = range(4)
    npt.assert_array_equal(callarray(4), expected)

    # Test with Nones
    callarray[0, 0] = None
    expected[0, 0] = 0
    npt.assert_array_equal(callarray(4), expected)

def test_multi_voxel_fit():

    class SillyModel(object):

        @multi_voxel_fit
        def fit(self, data, mask=None):
            return SillyFit(model, data)

        def predict(self, S0):
            return np.ones(10) * S0

    class SillyFit(object):

        def __init__(self, model, data):
            self.model = model
            self.data = data

        model_attr = 2.

        def odf(self, sphere):
            return np.ones(len(sphere.phi))

        @property
        def directions(self):
            n = np.random.randint(0, 10)
            return np.zeros((n, 3))

        def predict(self, S0):
            return np.ones(self.data.shape) * S0

    # Test the single voxel case
    model = SillyModel()
    single_voxel = np.zeros(64)
    fit = model.fit(single_voxel)
    npt.assert_equal(type(fit), SillyFit)

    # Test without a mask
    many_voxels = np.zeros((2, 3, 4, 64))
    fit = model.fit(many_voxels)
    expected = np.empty((2, 3, 4))
    expected[:] = 2.
    npt.assert_array_equal(fit.model_attr, expected)
    expected = np.ones((2, 3, 4, 12))
    npt.assert_array_equal(fit.odf(unit_icosahedron), expected)
    npt.assert_equal(fit.directions.shape, (2, 3, 4))
    S0 = np.random.randn()
    npt.assert_equal(fit.predict(S0=S0), np.ones(many_voxels.shape) * S0)

    # Test with a mask
    mask = np.zeros((3, 3, 3)).astype('bool')
    mask[0, 0] = 1
    mask[1, 1] = 1
    mask[2, 2] = 1
    data = np.zeros((3, 3, 3, 64))
    fit = model.fit(data, mask)
    npt.assert_array_equal(fit.model_attr, np.eye(3) * 2)
    odf = fit.odf(unit_icosahedron)
    npt.assert_equal(odf.shape, (3, 3, 12))
    npt.assert_array_equal(odf[~mask], 0)
    npt.assert_array_equal(odf[mask], 1)
    predicted = np.zeros(data.shape)
    predicted[mask] = S0
    npt.assert_equal(fit.predict(S0=S0), predicted)

    # Test fit.shape
    npt.assert_equal(fit.shape, (3, 3))

    # Test indexing into a fit
    npt.assert_equal(type(fit[0, 0]), SillyFit)
    npt.assert_equal(fit[:2, :2].shape, (2, 2))
<commit_msg>TST: More realistic mask testing.<commit_after>from __future__ import division, print_function, absolute_import

from functools import reduce

import numpy as np
import numpy.testing as npt

from dipy.reconst.multi_voxel import _squash, multi_voxel_fit, CallableArray
from dipy.core.sphere import unit_icosahedron

def test_squash():
    A = np.ones((3, 3), dtype=float)
    B = np.asarray(A, object)
    npt.assert_array_equal(A, _squash(B))
    npt.assert_equal(_squash(B).dtype, A.dtype)

    B[2, 2] = None
    A[2, 2] = 0
    npt.assert_array_equal(A, _squash(B))
    npt.assert_equal(_squash(B).dtype, A.dtype)

    for ijk in np.ndindex(*B.shape):
        B[ijk] = np.ones((2,))
    A = np.ones((3, 3, 2))
    npt.assert_array_equal(A, _squash(B))
    npt.assert_equal(_squash(B).dtype, A.dtype)

    B[2, 2] = None
    A[2, 2] = 0
    npt.assert_array_equal(A, _squash(B))
    npt.assert_equal(_squash(B).dtype, A.dtype)

    # sub-arrays have different shapes ( (3,) and (2,) )
    B[0, 0] = np.ones((3,))
    npt.assert_(_squash(B) is B)

    # Check dtypes for arrays and scalars
    arr_arr = np.zeros((2,), dtype=object)
    scalar_arr = np.zeros((2,), dtype=object)
    numeric_types = sum(
        [np.sctypes[t] for t in ('int', 'uint', 'float', 'complex')],
        [np.bool_])
    for dt0 in numeric_types:
        arr_arr[0] = np.zeros((3,), dtype=dt0)
        scalar_arr[0] = dt0(0)
        for dt1 in numeric_types:
            arr_arr[1] = np.zeros((3,), dtype=dt1)
            npt.assert_equal(_squash(arr_arr).dtype,
                             reduce(np.add, arr_arr).dtype)
            scalar_arr[1] = dt0(1)
            npt.assert_equal(_squash(scalar_arr).dtype,
                             reduce(np.add, scalar_arr).dtype)

    # Check masks and Nones
    arr = np.ones((3, 4), dtype=float)
    obj_arr = arr.astype(object)
    arr[1, 1] = 99
    obj_arr[1, 1] = None
    npt.assert_array_equal(_squash(obj_arr, mask=None, fill=99), arr)
    msk = arr == 1
    npt.assert_array_equal(_squash(obj_arr, mask=msk, fill=99), arr)
    msk[1, 1] = 1 # unmask None - object array back
    npt.assert_array_equal(_squash(obj_arr, mask=msk, fill=99), obj_arr)
    msk[1, 1] = 0 # remask, back to fill again
    npt.assert_array_equal(_squash(obj_arr, mask=msk, fill=99), arr)
    obj_arr[2, 3] = None # add another unmasked None, object again
    npt.assert_array_equal(_squash(obj_arr, mask=msk, fill=99), obj_arr)

    # Check array of arrays
    obj_arrs = np.zeros((3,), dtype=object)
    for i in range(3):
        obj_arrs[i] = np.ones((4, 5))
    arr_arrs = np.ones((3, 4, 5))
    # No Nones
    npt.assert_array_equal(_squash(obj_arrs, mask=None, fill=99), arr_arrs)
    # None, implicit masking
    obj_masked = obj_arrs.copy()
    obj_masked[1] = None
    arr_masked = arr_arrs.copy()
    arr_masked[1] = 99
    npt.assert_array_equal(_squash(obj_masked, mask=None, fill=99),
                           arr_masked)
    msk = np.array([1, 0, 1], dtype=np.bool_) # explicit mask
    npt.assert_array_equal(_squash(obj_masked, mask=msk, fill=99),
                           arr_masked)
    msk[1] = True # unmask None, object array back
    npt.assert_array_equal(_squash(obj_masked, mask=msk, fill=99),
                           obj_masked)

def test_CallableArray():
    callarray = CallableArray((2, 3), dtype=object)

    # Test without Nones
    callarray[:] = np.arange
    expected = np.empty([2, 3, 4])
    expected[:] = range(4)
    npt.assert_array_equal(callarray(4), expected)

    # Test with Nones
    callarray[0, 0] = None
    expected[0, 0] = 0
    npt.assert_array_equal(callarray(4), expected)

def test_multi_voxel_fit():

    class SillyModel(object):

        @multi_voxel_fit
        def fit(self, data, mask=None):
            return SillyFit(model, data)

        def predict(self, S0):
            return np.ones(10) * S0

    class SillyFit(object):

        def __init__(self, model, data):
            self.model = model
            self.data = data

        model_attr = 2.

        def odf(self, sphere):
            return np.ones(len(sphere.phi))

        @property
        def directions(self):
            n = np.random.randint(0, 10)
            return np.zeros((n, 3))

        def predict(self, S0):
            return np.ones(self.data.shape) * S0

    # Test the single voxel case
    model = SillyModel()
    single_voxel = np.zeros(64)
    fit = model.fit(single_voxel)
    npt.assert_equal(type(fit), SillyFit)

    # Test without a mask
    many_voxels = np.zeros((2, 3, 4, 64))
    fit = model.fit(many_voxels)
    expected = np.empty((2, 3, 4))
    expected[:] = 2.
    npt.assert_array_equal(fit.model_attr, expected)
    expected = np.ones((2, 3, 4, 12))
    npt.assert_array_equal(fit.odf(unit_icosahedron), expected)
    npt.assert_equal(fit.directions.shape, (2, 3, 4))
    S0 = np.random.randn()
    npt.assert_equal(fit.predict(S0=S0), np.ones(many_voxels.shape) * S0)

    # Test with a mask
    mask = np.zeros((3, 3, 3)).astype('bool')
    mask[0, 0] = 1
    mask[1, 1] = 1
    mask[2, 2] = 1
    data = np.zeros((3, 3, 3, 64))
    fit = model.fit(data, mask)
    expected = np.zeros((3,3,3))
    expected[0, 0] = 2
    expected[1, 1] = 2
    expected[2, 2] = 2
    npt.assert_array_equal(fit.model_attr, expected)
    odf = fit.odf(unit_icosahedron)
    npt.assert_equal(odf.shape, (3, 3, 3, 12))
    npt.assert_array_equal(odf[~mask], 0)
    npt.assert_array_equal(odf[mask], 1)
    predicted = np.zeros(data.shape)
    predicted[mask] = S0
    npt.assert_equal(fit.predict(S0=S0), predicted)

    # Test fit.shape
    npt.assert_equal(fit.shape, (3, 3, 3))

    # Test indexing into a fit
    npt.assert_equal(type(fit[0, 0, 0]), SillyFit)
    npt.assert_equal(fit[:2, :2, :2].shape, (2, 2, 2))
<|endoftext|>