<commit_before>from __future__ import division

import random
import math
from bitarray import bitarray

from tpg.instruction import Instruction
import tpg.action

"""
A Learner. Contains a program that makes a bid to use its action, based on the
current input and perhaps previous outcomes.
"""
class Learner:

    learnerIdCounter = 0 # counter for id
    registerSize = 8 # size of registers

    def __init__(self, action=0, maxProgSize=8, randSeed=0, learner=None,
            makeNew=False, birthGen=0):

        self.randSeed = randSeed
        self.rand = random.Random()
        if randSeed == 0:
            self.rand.seed()
        else:
            self.rand.seed(randSeed)

        # reconstruct a learner
        if learner is not None:
            if makeNew: # make new from existing
                self.id = Learner.learnerIdCounter
                Learner.learnerIdCounter += 1
                self.birthGen = birthGen
                self.teamRefCount = 0
            else: # remake existing
                self.id = learner.id
                self.birthGen = learner.birthGen
                self.teamRefCount = learner.teamRefCount

            # these copied either way
            self.action = learner.action
            self.program = [i for i in learner.program]
            return

        # or make a brand new one
        self.id = Learner.learnerIdCounter
        Learner.learnerIdCounter += 1
        self.birthGen = birthGen
        self.action = tpg.action.Action(action)
        self.teamRefCount = 0
        self.program = [] # program is list of instructions

        # amount of instruction in program
        progSize = self.rand.randint(1, maxProgSize)
        for i in range(progSize):
            ins = Instruction(randSeed=randSeed)
            self.program.append(ins)

    """
    Gets the bid from this learner based on the observation, bid being the
    weight this learner has in decision making (roughly).
    Args:
        obs:
            (Float[]) The current state of the environment.
        regDict:
            (Dict<Int,Float[]>) Dictionary of registers, find the registers of
            this learner with the key self.id. If None, uses default (all 0)
            register.
    Returns:
        (Float) The bid value.
    """
    def bid(self, obs, regDict=None):
        # choose register appropriately
        registers = None
        if regDict is None:
            registers = [0]*Learner.registerSize
        else:
            if self.id not in regDict:
                regDict[self.id] = [0]*Learner.registerSize
            registers = regDict[self.id]

        # math overflow error happens sometimes
        try:
            progResult = self.runProgram(obs,registers)
            return 1 / (1 + math.exp(-progResult))
        except:
            return 0

    """
    Runs this learner's program.
    Args:
        obs:
            (Float[]) The current state of the environment.
        registers:
            (Float[]) Registers to be used for doing calculations with.
    Returns:
        (Float) What the destination register's value is at the end.
    """
    def runProgram(self, obs, registers):
        # iterate over instructions in the program
        for inst in self.program:
            sourceVal = 0
            # first get an initial value from register or observation
            if Instruction.equalBitArrays(
                    inst.getBitArraySeg(Instruction.slcMode),Instruction.mode0):
                # instruction is mode0, source value comes from register
                sourceVal = registers[Instruction.getIntVal(
                    inst.getBitArraySeg(Instruction.slcSrc)) %
                        Learner.registerSize]
            else:
                # instruction not mode0, source value form obs
                sourceVal = obs[Instruction.getIntVal(
                    inst.getBitArraySeg(Instruction.slcSrc)) % len(obs)]

            # the operation to do on the register
            operation = inst.getBitArraySeg(Instruction.slcOp)
            # the register to fiddle with
            destReg = Instruction.getIntVal(
                inst.getBitArraySeg(Instruction.slcDest))

            if Instruction.equalBitArrays(operation, Instruction.opSum):
                registers[destReg] += sourceVal
            elif Instruction.equalBitArrays(operation, Instruction.opDiff):
                registers[destReg] -= sourceVal
            elif Instruction.equalBitArrays(operation, Instruction.opProd):
                registers[destReg] *= sourceVal
            elif Instruction.equalBitArrays(operation, Instruction.opDiv):
                if sourceVal != 0:
                    registers[destReg] /= sourceVal
            elif Instruction.equalBitArrays(operation, Instruction.opCos):
                registers[destReg] = math.cos(sourceVal)
            elif Instruction.equalBitArrays(operation, Instruction.opLog):
                if sourceVal > 0:
                    registers[destReg] = math.log(sourceVal)
            elif Instruction.equalBitArrays(operation, Instruction.opExp):
                registers[destReg] = math.exp(sourceVal)
            elif Instruction.equalBitArrays(operation, Instruction.opCond):
                if registers[destReg] < sourceVal:
                    registers[destReg] *= -1
            else:
                print(operation.to01())
                print("Invalid operation in learner.run")

            # default register to 0 if invalid value
            if (math.isnan(registers[destReg]) or
                    registers[destReg] == float('inf') or
                    registers[destReg] == float('-inf')):
                registers[destReg] = 0

        return registers[0]

    """
    Mutates this learners program.
    Returns:
        (Bool) Whether actually mutated.
    """
    def mutateProgram(self, pProgramDelete, pProgramAdd, pProgramSwap,
            pProgramMutate, maxProgramSize):

        changed = False # whether a change occured

        # maybe delete instruction
        if (len(self.program) > 1 and self.rand.uniform(0,1) < pProgramDelete):
            del self.program[self.rand.choice(range(len(self.program)))]
            changed = True

        # maybe insert instruction
        if (len(self.program) < maxProgramSize and
                self.rand.uniform(0,1) < pProgramAdd):
            ins = Instruction(randSeed=self.randSeed)
            self.program.insert(self.rand.choice(range(len(self.program))), ins)
            changed = True

        # maybe flip an instruction's bit
        if self.rand.uniform(0,1) < pProgramMutate:
            self.program[self.rand.choice(range(len(self.program)))].flip(
                    self.rand.choice(range(Instruction.instructionSize)))
            changed = True

        # maybe swap two instructions
        if len(self.program) > 1 and self.rand.uniform(0,1) < pProgramSwap:
            # indices to swap
            idx1 = self.rand.choice(range(len(self.program)))
            idx2 = self.rand.choice(
                    [x for x in range(len(self.program)) if x != idx1])
            # do swap
            tmp = self.program[idx1]
            self.program[idx1] = self.program[idx2]
            self.program[idx2] = tmp
            changed = True

        return changed

    """
    Changes the learners action to the argument action.
    Args:
        action:
            (Action) an Action object.
    Returns:
        (Bool) Whether the action is different after mutation.
    """
    def mutateAction(self, action):
        act = self.action
        self.action = action
        return act.equals(action)
<commit_msg>fixed annoying typo<commit_after>from __future__ import division

import random
import math
from bitarray import bitarray

from tpg.instruction import Instruction
import tpg.action

"""
A Learner. Contains a program that makes a bid to use its action, based on the
current input and perhaps previous outcomes.
"""
class Learner:

    learnerIdCounter = 0 # counter for id
    registerSize = 8 # size of registers

    def __init__(self, action=0, maxProgSize=8, randSeed=0, learner=None,
            makeNew=False, birthGen=0):

        self.randSeed = randSeed
        self.rand = random.Random()
        if randSeed == 0:
            self.rand.seed()
        else:
            self.rand.seed(randSeed)

        # reconstruct a learner
        if learner is not None:
            if makeNew: # make new from existing
                self.id = Learner.learnerIdCounter
                Learner.learnerIdCounter += 1
                self.birthGen = birthGen
                self.teamRefCount = 0
            else: # remake existing
                self.id = learner.id
                self.birthGen = learner.birthGen
                self.teamRefCount = learner.teamRefCount

            # these copied either way
            self.action = learner.action
            self.program = [i for i in learner.program]
            return

        # or make a brand new one
        self.id = Learner.learnerIdCounter
        Learner.learnerIdCounter += 1
        self.birthGen = birthGen
        self.action = tpg.action.Action(action)
        self.teamRefCount = 0
        self.program = [] # program is list of instructions

        # amount of instruction in program
        progSize = self.rand.randint(1, maxProgSize)
        for i in range(progSize):
            ins = Instruction(randSeed=randSeed)
            self.program.append(ins)

    """
    Gets the bid from this learner based on the observation, bid being the
    weight this learner has in decision making (roughly).
    Args:
        obs:
            (Float[]) The current state of the environment.
        regDict:
            (Dict<Int,Float[]>) Dictionary of registers, find the registers of
            this learner with the key self.id. If None, uses default (all 0)
            register.
    Returns:
        (Float) The bid value.
    """
    def bid(self, obs, regDict=None):
        # choose register appropriately
        registers = None
        if regDict is None:
            registers = [0]*Learner.registerSize
        else:
            if self.id not in regDict:
                regDict[self.id] = [0]*Learner.registerSize
            registers = regDict[self.id]

        # math overflow error happens sometimes
        try:
            progResult = self.runProgram(obs,registers)
            return 1 / (1 + math.exp(-progResult))
        except:
            return 0

    """
    Runs this learner's program.
    Args:
        obs:
            (Float[]) The current state of the environment.
        registers:
            (Float[]) Registers to be used for doing calculations with.
    Returns:
        (Float) What the destination register's value is at the end.
    """
    def runProgram(self, obs, registers):
        # iterate over instructions in the program
        for inst in self.program:
            sourceVal = 0
            # first get an initial value from register or observation
            if Instruction.equalBitArrays(
                    inst.getBitArraySeg(Instruction.slcMode),Instruction.mode0):
                # instruction is mode0, source value comes from register
                sourceVal = registers[Instruction.getIntVal(
                    inst.getBitArraySeg(Instruction.slcSrc)) %
                        Learner.registerSize]
            else:
                # instruction not mode0, source value from obs
                sourceVal = obs[Instruction.getIntVal(
                    inst.getBitArraySeg(Instruction.slcSrc)) % len(obs)]

            # the operation to do on the register
            operation = inst.getBitArraySeg(Instruction.slcOp)
            # the register to fiddle with
            destReg = Instruction.getIntVal(
                inst.getBitArraySeg(Instruction.slcDest))

            if Instruction.equalBitArrays(operation, Instruction.opSum):
                registers[destReg] += sourceVal
            elif Instruction.equalBitArrays(operation, Instruction.opDiff):
                registers[destReg] -= sourceVal
            elif Instruction.equalBitArrays(operation, Instruction.opProd):
                registers[destReg] *= sourceVal
            elif Instruction.equalBitArrays(operation, Instruction.opDiv):
                if sourceVal != 0:
                    registers[destReg] /= sourceVal
            elif Instruction.equalBitArrays(operation, Instruction.opCos):
                registers[destReg] = math.cos(sourceVal)
            elif Instruction.equalBitArrays(operation, Instruction.opLog):
                if sourceVal > 0:
                    registers[destReg] = math.log(sourceVal)
            elif Instruction.equalBitArrays(operation, Instruction.opExp):
                registers[destReg] = math.exp(sourceVal)
            elif Instruction.equalBitArrays(operation, Instruction.opCond):
                if registers[destReg] < sourceVal:
                    registers[destReg] *= -1
            else:
                print(operation.to01())
                print("Invalid operation in learner.run")

            # default register to 0 if invalid value
            if (math.isnan(registers[destReg]) or
                    registers[destReg] == float('inf') or
                    registers[destReg] == float('-inf')):
                registers[destReg] = 0

        return registers[0]

    """
    Mutates this learners program.
    Returns:
        (Bool) Whether actually mutated.
    """
    def mutateProgram(self, pProgramDelete, pProgramAdd, pProgramSwap,
            pProgramMutate, maxProgramSize):

        changed = False # whether a change occured

        # maybe delete instruction
        if (len(self.program) > 1 and self.rand.uniform(0,1) < pProgramDelete):
            del self.program[self.rand.choice(range(len(self.program)))]
            changed = True

        # maybe insert instruction
        if (len(self.program) < maxProgramSize and
                self.rand.uniform(0,1) < pProgramAdd):
            ins = Instruction(randSeed=self.randSeed)
            self.program.insert(self.rand.choice(range(len(self.program))), ins)
            changed = True

        # maybe flip an instruction's bit
        if self.rand.uniform(0,1) < pProgramMutate:
            self.program[self.rand.choice(range(len(self.program)))].flip(
                    self.rand.choice(range(Instruction.instructionSize)))
            changed = True

        # maybe swap two instructions
        if len(self.program) > 1 and self.rand.uniform(0,1) < pProgramSwap:
            # indices to swap
            idx1 = self.rand.choice(range(len(self.program)))
            idx2 = self.rand.choice(
                    [x for x in range(len(self.program)) if x != idx1])
            # do swap
            tmp = self.program[idx1]
            self.program[idx1] = self.program[idx2]
            self.program[idx2] = tmp
            changed = True

        return changed

    """
    Changes the learners action to the argument action.
    Args:
        action:
            (Action) an Action object.
    Returns:
        (Bool) Whether the action is different after mutation.
    """
    def mutateAction(self, action):
        act = self.action
        self.action = action
        return act.equals(action)
<|endoftext|>


<commit_before># coding=utf-8
from simpleai.search.utils import FifoList, BoundedPriorityQueue
from simpleai.search.models import (SearchNode, SearchNodeHeuristicOrdered,
                                    SearchNodeStarOrdered,
                                    SearchNodeCostOrdered)

def breadth_first(problem, graph_search=False, viewer=None):
    '''
    Breadth first search.

    If graph_search=True, will avoid exploring repeated states.
    Requires: SearchProblem.actions, SearchProblem.result, and
    SearchProblem.is_goal.
    '''
    return _search(problem,
                   FifoList(),
                   graph_search=graph_search,
                   viewer=viewer)

def depth_first(problem, graph_search=False, viewer=None):
    '''
    Depth first search.

    If graph_search=True, will avoid exploring repeated states.
    Requires: SearchProblem.actions, SearchProblem.result, and
    SearchProblem.is_goal.
    '''
    return _search(problem,
                   [],
                   graph_search=graph_search,
                   viewer=viewer)

def limited_depth_first(problem, depth_limit, graph_search=False, viewer=None):
    '''
    Limited depth first search.

    Depth_limit is the maximum depth allowed, being depth 0 the initial state.
    If graph_search=True, will avoid exploring repeated states.
    Requires: SearchProblem.actions, SearchProblem.result, and
    SearchProblem.is_goal.
    '''
    return _search(problem,
                   [],
                   graph_search=graph_search,
                   depth_limit=depth_limit,
                   viewer=viewer)

def iterative_limited_depth_first(problem, graph_search=False, viewer=None):
    '''
    Iterative limited depth first search.

    If graph_search=True, will avoid exploring repeated states.
    Requires: SearchProblem.actions, SearchProblem.result, and
    SearchProblem.is_goal.
    '''
    solution = None
    limit = 0

    while not solution:
        solution = limited_depth_first(problem,
                                       depth_limit=limit,
                                       graph_search=graph_search,
                                       viewer=viewer)
        limit += 1

    return solution

def uniform_cost(problem, graph_search=False, viewer=None):
    '''
    Uniform cost search.

    If graph_search=True, will avoid exploring repeated states.
    Requires: SearchProblem.actions, SearchProblem.result,
    SearchProblem.is_goal, and SearchProblem.cost.
    '''
    return _search(problem,
                   BoundedPriorityQueue(),
                   graph_search=graph_search,
                   node_factory=SearchNodeCostOrdered,
                   graph_replace_when_better=True,
                   viewer=viewer)

def greedy(problem, graph_search=False, viewer=None):
    '''
    Greedy search.

    If graph_search=True, will avoid exploring repeated states.
    Requires: SearchProblem.actions, SearchProblem.result,
    SearchProblem.is_goal, SearchProblem.cost, and SearchProblem.heuristic.
    '''
    return _search(problem,
                   BoundedPriorityQueue(),
                   graph_search=graph_search,
                   node_factory=SearchNodeHeuristicOrdered,
                   graph_replace_when_better=True,
                   viewer=viewer)

def astar(problem, graph_search=False, viewer=None):
    '''
    A* search.

    If graph_search=True, will avoid exploring repeated states.
    Requires: SearchProblem.actions, SearchProblem.result,
    SearchProblem.is_goal, SearchProblem.cost, and SearchProblem.heuristic.
    '''
    return _search(problem,
                   BoundedPriorityQueue(),
                   graph_search=graph_search,
                   node_factory=SearchNodeStarOrdered,
                   graph_replace_when_better=True,
                   viewer=viewer)

def _search(problem, fringe, graph_search=False, depth_limit=None,
            node_factory=SearchNode, graph_replace_when_better=False,
            viewer=None):
    '''
    Basic search algorithm, base of all the other search algorithms.
    '''
    if viewer: viewer.started()

    memory = {}
    initial_node = node_factory(state=problem.initial_state,
                                problem=problem)
    fringe.append(initial_node)
    memory[problem.initial_state] = initial_node

    while fringe:
        if viewer: viewer.new_iteration([n for n in fringe])

        node = fringe.pop()

        if problem.is_goal(node.state):
            if viewer:
                viewer.chosen_node(node, True)
                viewer.finished(node, 'goal found')
            return node
        else:
            if viewer: viewer.chosen_node(node, False)

        if depth_limit is None or node.depth < depth_limit:
            childs = []
            expanded = node.expand()
            if viewer: viewer.expanded(node, expanded)

            for n in expanded:
                if graph_search:
                    if n.state not in memory:
                        memory[n.state] = n
                        childs.append(n)
                    elif graph_replace_when_better:
                        other = memory[n.state]
                        if n < other:
                            childs.append(n)
                            if other in fringe:
                                fringe.remove(other)
                else:
                    childs.append(n)

            for n in childs:
                fringe.append(n)

    if viewer: viewer.finished(None, 'goal not found')
<commit_msg>Traditional search fixes for new expanded parameter format<commit_after># coding=utf-8
from simpleai.search.utils import FifoList, BoundedPriorityQueue
from simpleai.search.models import (SearchNode, SearchNodeHeuristicOrdered,
                                    SearchNodeStarOrdered,
                                    SearchNodeCostOrdered)

def breadth_first(problem, graph_search=False, viewer=None):
    '''
    Breadth first search.

    If graph_search=True, will avoid exploring repeated states.
    Requires: SearchProblem.actions, SearchProblem.result, and
    SearchProblem.is_goal.
    '''
    return _search(problem,
                   FifoList(),
                   graph_search=graph_search,
                   viewer=viewer)

def depth_first(problem, graph_search=False, viewer=None):
    '''
    Depth first search.

    If graph_search=True, will avoid exploring repeated states.
    Requires: SearchProblem.actions, SearchProblem.result, and
    SearchProblem.is_goal.
    '''
    return _search(problem,
                   [],
                   graph_search=graph_search,
                   viewer=viewer)

def limited_depth_first(problem, depth_limit, graph_search=False, viewer=None):
    '''
    Limited depth first search.

    Depth_limit is the maximum depth allowed, being depth 0 the initial state.
    If graph_search=True, will avoid exploring repeated states.
    Requires: SearchProblem.actions, SearchProblem.result, and
    SearchProblem.is_goal.
    '''
    return _search(problem,
                   [],
                   graph_search=graph_search,
                   depth_limit=depth_limit,
                   viewer=viewer)

def iterative_limited_depth_first(problem, graph_search=False, viewer=None):
    '''
    Iterative limited depth first search.

    If graph_search=True, will avoid exploring repeated states.
    Requires: SearchProblem.actions, SearchProblem.result, and
    SearchProblem.is_goal.
    '''
    solution = None
    limit = 0

    while not solution:
        solution = limited_depth_first(problem,
                                       depth_limit=limit,
                                       graph_search=graph_search,
                                       viewer=viewer)
        limit += 1

    return solution

def uniform_cost(problem, graph_search=False, viewer=None):
    '''
    Uniform cost search.

    If graph_search=True, will avoid exploring repeated states.
    Requires: SearchProblem.actions, SearchProblem.result,
    SearchProblem.is_goal, and SearchProblem.cost.
    '''
    return _search(problem,
                   BoundedPriorityQueue(),
                   graph_search=graph_search,
                   node_factory=SearchNodeCostOrdered,
                   graph_replace_when_better=True,
                   viewer=viewer)

def greedy(problem, graph_search=False, viewer=None):
    '''
    Greedy search.

    If graph_search=True, will avoid exploring repeated states.
    Requires: SearchProblem.actions, SearchProblem.result,
    SearchProblem.is_goal, SearchProblem.cost, and SearchProblem.heuristic.
    '''
    return _search(problem,
                   BoundedPriorityQueue(),
                   graph_search=graph_search,
                   node_factory=SearchNodeHeuristicOrdered,
                   graph_replace_when_better=True,
                   viewer=viewer)

def astar(problem, graph_search=False, viewer=None):
    '''
    A* search.

    If graph_search=True, will avoid exploring repeated states.
    Requires: SearchProblem.actions, SearchProblem.result,
    SearchProblem.is_goal, SearchProblem.cost, and SearchProblem.heuristic.
    '''
    return _search(problem,
                   BoundedPriorityQueue(),
                   graph_search=graph_search,
                   node_factory=SearchNodeStarOrdered,
                   graph_replace_when_better=True,
                   viewer=viewer)

def _search(problem, fringe, graph_search=False, depth_limit=None,
            node_factory=SearchNode, graph_replace_when_better=False,
            viewer=None):
    '''
    Basic search algorithm, base of all the other search algorithms.
    '''
    if viewer: viewer.started()

    memory = {}
    initial_node = node_factory(state=problem.initial_state,
                                problem=problem)
    fringe.append(initial_node)
    memory[problem.initial_state] = initial_node

    while fringe:
        if viewer: viewer.new_iteration([n for n in fringe])

        node = fringe.pop()

        if problem.is_goal(node.state):
            if viewer:
                viewer.chosen_node(node, True)
                viewer.finished(node, 'goal found')
            return node
        else:
            if viewer: viewer.chosen_node(node, False)

        if depth_limit is None or node.depth < depth_limit:
            childs = []
            expanded = node.expand()
            if viewer: viewer.expanded([node], [expanded])

            for n in expanded:
                if graph_search:
                    if n.state not in memory:
                        memory[n.state] = n
                        childs.append(n)
                    elif graph_replace_when_better:
                        other = memory[n.state]
                        if n < other:
                            childs.append(n)
                            if other in fringe:
                                fringe.remove(other)
                else:
                    childs.append(n)

            for n in childs:
                fringe.append(n)

    if viewer: viewer.finished(None, 'goal not found')
<|endoftext|>


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

class TextReplacer:
    def __init__(self):
        self.res = []

    def add(self, reg, rep):
        self.res.append((reg, rep))
        return self

    def replace(self, text):
        for r in self.res:
            reg = r[0]
            rep = r[1]
            text = text.replace(reg, rep)
        return text

def is_windows() -> bool:
    import platform
    return platform.system() == 'Windows'

def get_vs_root():
    if is_windows():
        vs = subprocess.check_output(
            ['reg', 'QUERY', r"HKLM\SOFTWARE\Microsoft\VisualStudio\14.0", '/v', 'InstallDir', '/reg:32'])
        print("This is the vs solution path...", vs)
        sys.stdout.flush()
    vs_root = r'C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE'
    return vs_root

def globals():
    vs_root = r'C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE'
    sys.stdout.flush()

    root = os.getcwd()
    install_dist = os.path.join(root, 'install-dist')
    install = os.path.join(root, 'install')
    wx_root = os.path.join(install_dist, 'wx')
    proto_root = os.path.join(install_dist, 'proto')
    proto_root_root = os.path.join(proto_root, 'protobuf-2.6.1')
    build = os.path.join(root, 'build')

def appveyor_msbuild():
    return r'/logger:%programfiles%\AppVeyor\BuildAgent\Appveyor.MSBuildLogger.dll'

def is_platform_64bit() -> bool:
    if os.environ.get('PLATFORM', 'unknown') == 'x86':
        return False
    else:
        return True

def platform_as_string():
    if is_platform_64bit():
        return 'x64'
    else:
        return 'win32'

def visual_studio_generator():
    if is_platform_64bit():
        return 'Visual Studio 15 Win64'
    else:
        return 'Visual Studio 15'

def verify_dir_exist(path: str):
    if not os.path.isdir(path):
        os.makedirs(path)

def download_file(url: str, path: str):
    import urllib.request
    import shutil
    if not os.path.isfile(path):
        print("Downloading ", path)
        # urllib.request.urlretrieve(url, path)
        with urllib.request.urlopen(url) as response, open(path, 'wb') as out_file:
            shutil.copyfileobj(response, out_file)
    else:
        print("Already downloaded", path)

def list_projects_in_solution(path: str) -> typing.List[str]:
    ret = []
    dir = os.path.dirname(path)
    pl = re.compile(r'Project\("[^"]+"\) = "[^"]+", "([^"]+)"')
    with open(path) as sln:
        for line in sln:
            # Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "richtext", "wx_richtext.vcxproj", "{7FB0902D-8579-5DCE-B883-DAF66A885005}"
            m = pl.match(line)
            if m:
                subfolder = m.group(1)
                if not is_windows():
                    subfolder = subfolder.replace('\\', '/')
                print(subfolder)
                ret.append(os.path.join(dir, subfolder))
    return ret

def add_definition_to_project(path: str, define: str):
    # <PreprocessorDefinitions>WIN32;_LIB;_CRT_SECURE_NO_DEPRECATE=1;_CRT_NON_CONFORMING_SWPRINTFS=1;_SCL_SECURE_NO_WARNINGS=1;__WXMSW__;NDEBUG;_UNICODE;WXBUILDING;%(PreprocessorDefinitions)</PreprocessorDefinitions>
    pp = re.compile(r'([ ]*<PreprocessorDefinitions>)([^<]*</PreprocessorDefinitions>)')
    lines = []
    with open(path) as project:
        for line in project:
            m = pp.match(line)
            if m:
                lines.append('{0}{1};{2}'.format(m.group(1), define, m.group(2)))
            else:
                lines.append(line.rstrip())
    with open(path, mode='w') as project:
        for line in lines:
            project.write(line + '\n')

# change from:
# <RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary> to <RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
# <RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary> to <RuntimeLibrary>MultiThreaded</RuntimeLibrary>
def change_to_static_link(path: str):
    mtdebug = re.compile(r'([ ]*)<RuntimeLibrary>MultiThreadedDebugDLL')
    mtrelease = re.compile(r'([ ]*)<RuntimeLibrary>MultiThreadedDLL')
    lines = []
    with open(path) as project:
        for line in project:
            mdebug = mtdebug.match(line)
            mrelease = mtrelease.match(line)
            if mdebug:
                print('in {project} changed to static debug'.format(project=path))
                lines.append(
                    '{spaces}<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>'.format(spaces=mdebug.group(1)))
            elif mrelease:
                print('in {project} changed to static release'.format(project=path))
                lines.append('{spaces}<RuntimeLibrary>MultiThreaded</RuntimeLibrary>'.format(spaces=mrelease.group(1)))
            else:
                lines.append(line.rstrip())
    with open(path, mode='w') as project:
        for line in lines:
            project.write(line + '\n')

def change_all_projects_to_static(sln: str):
    projects = list_projects_in_solution(sln)
    for p in projects:
        change_to_static_link(p)

def add_definition_to_solution(sln: str, definition: str):
    projects = list_projects_in_solution(sln)
    for p in projects:
        add_definition_to_project(p, definition)

def make_single_project_64(project_path: str, rep: TextReplacer):
    if not os.path.isfile(project_path):
        print('missing ' + project_path)
        return
    lines = []
    with open(project_path) as project:
        for line in project:
            nl = rep.replace(line.rstrip())
            lines.append(nl)
    with open(project_path, 'w') as project:
        for line in lines:
            project.write(line + '\n')

def make_projects_64(sln: str):
    projects = list_projects_in_solution(sln)
    rep = TextReplacer()
    rep.add('Win32', 'x64')
    rep.add('<DebugInformationFormat>EditAndContinue</DebugInformationFormat>',
            '<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>')
    rep.add('<TargetMachine>MachineX86</TargetMachine>', '<TargetMachine>MachineX64</TargetMachine>')
    # protobuf specific hack since cmake looks in x64 folder
    rep.add('<OutDir>Release\</OutDir>', '<OutDir>x64\Release\</OutDir>')
    rep.add('<OutDir>Debug\</OutDir>', '<OutDir>x64\Debug\</OutDir>')
    for project in projects:
        make_single_project_64(project, rep)

def make_solution_64(sln: str):
    rep = TextReplacer()
    rep.add('Win32', 'x64')

    lines = []
    with open(sln) as slnlines:
        for line in slnlines:
            nl = rep.replace(line.rstrip())
            lines.append(nl)

    with open(sln, 'w') as f:
        for line in lines:
            f.write(line + '\n')

def convert_sln_to_64(sln: str):
    make_solution_64(sln)
    make_projects_64(sln)

def install_dependency_wx(install_dist: str, wx_root: str, build: bool):
    wx_url = "https://github.com/wxWidgets/wxWidgets/releases/download/v3.1.0/wxWidgets-3.1.0.zip"
    wx_zip = os.path.join(install_dist, "wx.zip")
    wx_sln = os.path.join(wx_root, 'build', 'msw', 'wx_vc14.sln')
    verify_dir_exist(install_dist)
    verify_dir_exist(wx_root)
    print("downloading wx...")
    download_file(wx_url, os.path.join(install_dist, wx_zip))
    print("extracting wx")
    with zipfile.ZipFile(wx_zip, 'r') as z:
        z.extractall(wx_root)
    print("changing wx to static")
    change_all_projects_to_static(wx_sln)

    print("building wxwidgets")
    print("----------------------------------")

    if build:
        sys.stdout.flush()
        wx_msbuild_cmd = ['msbuild', '/p:Configuration=Release', '/p:Platform='+platform_as_string(), appveyor_msbuild(), wx_sln]
        if is_windows():
            subprocess.check_call(wx_msbuild_cmd)

def install_dependency_proto(install_dist: str, proto_root: str, build: bool, vs_root: str):
    proto_url = "https://github.com/google/protobuf/releases/download/v2.6.1/protobuf-2.6.1.zip"
    proto_root_root = os.path.join(proto_root, 'protobuf-2.6.1')
    proto_zip = os.path.join(install_dist, 'proto.zip')
    proto_sln = os.path.join(proto_root_root, 'vsprojects', 'protobuf.sln')
    verify_dir_exist(install_dist)
    verify_dir_exist(proto_root)
    print("downloding proto...")
    download_file(proto_url, os.path.join(install_dist, proto_zip))
    print("extracting proto")
    with zipfile.ZipFile(proto_zip, 'r') as z:
        z.extractall(proto_root)
    print("upgrading protobuf")
    print("-----------------------------------")
    devenv = os.path.join(vs_root, 'devenv.exe')
    if build:
        sys.stdout.flush()
        if is_windows():
            subprocess.check_call([devenv, proto_sln, '/upgrade'])
    add_definition_to_solution(proto_sln, '_SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS')

    print("changing proto to static")
    change_all_projects_to_static(proto_sln)

    if is_platform_64bit():
        print('64 bit build, hacking proto to 64 bit')
        convert_sln_to_64(proto_sln)

    print("building protobuf")
    print("-----------------------------------")
    if build:
        sys.stdout.flush()
        proto_msbuild_cmd = ['msbuild', '/t:libprotobuf;protoc', '/p:Configuration=Release', '/p:Platform='+platform_as_string(), proto_sln]
        if is_windows():
            subprocess.check_call(proto_msbuild_cmd)

<commit_msg>not building to a subfolder<commit_after>#!/usr/bin/python3
import os
import sys
import re
import typing
import zipfile
import subprocess

class TextReplacer:
    def __init__(self):
        self.res = []

    def add(self, reg, rep):
        self.res.append((reg, rep))
        return self

    def replace(self, text):
        for r in self.res:
            reg = r[0]
            rep = r[1]
            text = text.replace(reg, rep)
        return text

def is_windows() -> bool:
    import platform
    return platform.system() == 'Windows'

def get_vs_root():
    if is_windows():
        vs = subprocess.check_output(
            ['reg', 'QUERY', r"HKLM\SOFTWARE\Microsoft\VisualStudio\14.0", '/v', 'InstallDir', '/reg:32'])
        print("This is the vs solution path...", vs)
        sys.stdout.flush()
    vs_root = r'C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE'
    return vs_root

def globals():
    vs_root = r'C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE'
    sys.stdout.flush()

    root = os.getcwd()
    install_dist = os.path.join(root, 'install-dist')
    install = os.path.join(root, 'install')
    wx_root = os.path.join(install_dist, 'wx')
    proto_root = os.path.join(install_dist, 'proto')
    proto_root_root = os.path.join(proto_root, 'protobuf-2.6.1')
    build = os.path.join(root, 'build')

def appveyor_msbuild():
    return r'/logger:%programfiles%\AppVeyor\BuildAgent\Appveyor.MSBuildLogger.dll'

def is_platform_64bit() -> bool:
    if os.environ.get('PLATFORM', 'unknown') == 'x86':
        return False
    else:
        return True

def platform_as_string():
    if is_platform_64bit():
        return 'x64'
    else:
        return 'win32'

def visual_studio_generator():
    if is_platform_64bit():
        return 'Visual Studio 15 Win64'
    else:
        return 'Visual Studio 15'

def verify_dir_exist(path: str):
    if not os.path.isdir(path):
        os.makedirs(path)

def download_file(url: str, path: str):
    import urllib.request
    import shutil
    if not os.path.isfile(path):
        print("Downloading ", path)
        # urllib.request.urlretrieve(url, path)
        with urllib.request.urlopen(url) as response, open(path, 'wb') as out_file:
            shutil.copyfileobj(response, out_file)
    else:
        print("Already downloaded", path)

def list_projects_in_solution(path: str) -> typing.List[str]:
    ret = []
    dir = os.path.dirname(path)
    pl = re.compile(r'Project\("[^"]+"\) = "[^"]+", "([^"]+)"')
    with open(path) as sln:
        for line in sln:
            # Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "richtext", "wx_richtext.vcxproj", "{7FB0902D-8579-5DCE-B883-DAF66A885005}"
            m = pl.match(line)
            if m:
                subfolder = m.group(1)
                if not is_windows():
                    subfolder = subfolder.replace('\\', '/')
                print(subfolder)
                ret.append(os.path.join(dir, subfolder))
    return ret

def add_definition_to_project(path: str, define: str):
    # <PreprocessorDefinitions>WIN32;_LIB;_CRT_SECURE_NO_DEPRECATE=1;_CRT_NON_CONFORMING_SWPRINTFS=1;_SCL_SECURE_NO_WARNINGS=1;__WXMSW__;NDEBUG;_UNICODE;WXBUILDING;%(PreprocessorDefinitions)</PreprocessorDefinitions>
    pp = re.compile(r'([ ]*<PreprocessorDefinitions>)([^<]*</PreprocessorDefinitions>)')
    lines = []
    with open(path) as project:
        for line in project:
            m = pp.match(line)
            if m:
                lines.append('{0}{1};{2}'.format(m.group(1), define, m.group(2)))
            else:
                lines.append(line.rstrip())
    with open(path, mode='w') as project:
        for line in lines:
            project.write(line + '\n')

# change from:
# <RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary> to <RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
# <RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary> to <RuntimeLibrary>MultiThreaded</RuntimeLibrary>
def change_to_static_link(path: str):
    mtdebug = re.compile(r'([ ]*)<RuntimeLibrary>MultiThreadedDebugDLL')
    mtrelease = re.compile(r'([ ]*)<RuntimeLibrary>MultiThreadedDLL')
    lines = []
    with open(path) as project:
        for line in project:
            mdebug = mtdebug.match(line)
            mrelease = mtrelease.match(line)
            if mdebug:
                print('in {project} changed to static debug'.format(project=path))
                lines.append(
                    '{spaces}<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>'.format(spaces=mdebug.group(1)))
            elif mrelease:
                print('in {project} changed to static release'.format(project=path))
                lines.append('{spaces}<RuntimeLibrary>MultiThreaded</RuntimeLibrary>'.format(spaces=mrelease.group(1)))
            else:
                lines.append(line.rstrip())
    with open(path, mode='w') as project:
        for line in lines:
            project.write(line + '\n')

def change_all_projects_to_static(sln: str):
    projects = list_projects_in_solution(sln)
    for p in projects:
        change_to_static_link(p)

def add_definition_to_solution(sln: str, definition: str):
    projects = list_projects_in_solution(sln)
    for p in projects:
        add_definition_to_project(p, definition)

def make_single_project_64(project_path: str, rep: TextReplacer):
    if not os.path.isfile(project_path):
        print('missing ' + project_path)
        return
    lines = []
    with open(project_path) as project:
        for line in project:
            nl = rep.replace(line.rstrip())
            lines.append(nl)
    with open(project_path, 'w') as project:
        for line in lines:
            project.write(line + '\n')

def make_projects_64(sln: str):
    projects = list_projects_in_solution(sln)
    rep = TextReplacer()
    rep.add('Win32', 'x64')
    rep.add('<DebugInformationFormat>EditAndContinue</DebugInformationFormat>',
            '<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>')
    rep.add('<TargetMachine>MachineX86</TargetMachine>', '<TargetMachine>MachineX64</TargetMachine>')
    # protobuf specific hack since cmake looks in x64 folder
    rep.add('<OutDir>Release\</OutDir>', '<OutDir>x64\Release\</OutDir>')
    rep.add('<OutDir>Debug\</OutDir>', '<OutDir>x64\Debug\</OutDir>')
    for project in projects:
        make_single_project_64(project, rep)

def make_solution_64(sln: str):
    rep = TextReplacer()
    rep.add('Win32', 'x64')

    lines = []
    with open(sln) as slnlines:
        for line in slnlines:
            nl = rep.replace(line.rstrip())
            lines.append(nl)

    with open(sln, 'w') as f:
        for line in lines:
            f.write(line + '\n')

def convert_sln_to_64(sln: str):
    make_solution_64(sln)
    make_projects_64(sln)

def install_dependency_wx(install_dist: str, wx_root: str, build: bool):
    wx_url = "https://github.com/wxWidgets/wxWidgets/releases/download/v3.1.0/wxWidgets-3.1.0.zip"
    wx_zip = os.path.join(install_dist, "wx.zip")
    wx_sln = os.path.join(wx_root, 'build', 'msw', 'wx_vc14.sln')
    verify_dir_exist(install_dist)
    verify_dir_exist(wx_root)
    print("downloading wx...")
    download_file(wx_url, os.path.join(install_dist, wx_zip))
    print("extracting wx")
    with zipfile.ZipFile(wx_zip, 'r') as z:
        z.extractall(wx_root)
    print("changing wx to static")
    change_all_projects_to_static(wx_sln)

    print("building wxwidgets")
    print("----------------------------------")

    if build:
        sys.stdout.flush()
        wx_msbuild_cmd = ['msbuild', '/p:Configuration=Release', '/p:Platform='+platform_as_string(), appveyor_msbuild(), wx_sln]
        if is_windows():
            subprocess.check_call(wx_msbuild_cmd)

def install_dependency_proto(install_dist: str, proto_root: str, build: bool, vs_root: str):
    proto_url = "https://github.com/google/protobuf/releases/download/v2.6.1/protobuf-2.6.1.zip"
    # why are we building to a subroot of our proto folder?
    proto_root_root = proto_root # os.path.join(proto_root, 'protobuf-2.6.1')
    proto_zip = os.path.join(install_dist, 'proto.zip')
    proto_sln = os.path.join(proto_root_root, 'vsprojects', 'protobuf.sln')
    verify_dir_exist(install_dist)
    verify_dir_exist(proto_root)
    print("downloding proto...")
    download_file(proto_url, os.path.join(install_dist, proto_zip))
    print("extracting proto")
    with zipfile.ZipFile(proto_zip, 'r') as z:
        z.extractall(proto_root)
    print("upgrading protobuf")
    print("-----------------------------------")
    devenv = os.path.join(vs_root, 'devenv.exe')
    if build:
        sys.stdout.flush()
        if is_windows():
            subprocess.check_call([devenv, proto_sln, '/upgrade'])
    add_definition_to_solution(proto_sln, '_SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS')

    print("changing proto to static")
    change_all_projects_to_static(proto_sln)

    if is_platform_64bit():
        print('64 bit build, hacking proto to 64 bit')
        convert_sln_to_64(proto_sln)

    print("building protobuf")
    print("-----------------------------------")
    if build:
        sys.stdout.flush()
        proto_msbuild_cmd = ['msbuild', '/t:libprotobuf;protoc', '/p:Configuration=Release', '/p:Platform='+platform_as_string(), proto_sln]
        if is_windows():
            subprocess.check_call(proto_msbuild_cmd)

<|endoftext|>


<commit_before>"""
Search engines for images through a REST API
============================================

This example starts a :epkg:`waitress` server, creates
a :epkg:`WSGI` application based on :epkg:`falcon`
and queries the REST API. This application takes an image and
searches for similar images based on features produced
by a deep learning model.
"""

####################
# Settings.
host = '127.0.0.1'
port = 8081

########################
# Creates the search engine and starts a server in a different process.
# See :func:`search_images_dogcat <ensae_projects.restapi.search_images_dogcat.search_images_dogcat>`.

def process_server(host, port):
    import logging
    logger = logging.getLogger('search_images_dogcat')
    logger.setLevel(logging.INFO)
    hdlr = logging.FileHandler('search_images_dogcat.log')
    formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
    hdlr.setFormatter(formatter)
    logger.addHandler(hdlr)

    # If not specified, the application looks for zip file:
    # http://www.xavierdupre.fr/enseignement/complements/dog-cat-pixabay.zip
    url = None
    from ensae_projects.restapi import search_images_dogcat
    app = search_images_dogcat(url_images=url)

    from waitress import serve
    serve(app, host=host, port=port)

##########################
# Saves this code into a file and we start it
# from a different process.

import os
import ensae_projects

header = """
import sys
sys.path.append(r'{0}')
""".format(os.path.abspath(os.path.join(os.path.dirname(ensae_projects.__file__), '..')))

import inspect
code = "".join(inspect.getsourcelines(process_server)[0])
code = header + code + "\nprocess_server('{0}', {1})\n".format(host, port)
dest = os.path.abspath('temp_scripts')
if not os.path.exists(dest):
    os.mkdir(dest)
code_file = os.path.join(dest, "_start_server.py")
with open(code_file, "w") as f:
    f.write(code)

import sys
from subprocess import Popen
cmd = '{0} -u "{1}"'.format(sys.executable.replace("pythonw",
                                                   "python"), code_file)
proc = Popen(cmd)
print('Start server, process id', proc.pid)

##########################
# Let's wait.
from time import sleep
sleep(15)

####################
# Let's load an image.

from lightmlrestapi.args import image2base64
import ensae_projects.datainc.search_images as si
imgfile = os.path.join(os.path.dirname(si.__file__), "cat-1192026__480.jpg")

from PIL import Image
img = Image.open(imgfile)
img.show()

############################
# Let's query the server.

import requests
import ujson

b64 = image2base64(imgfile)[1]
features = ujson.dumps({'X': b64})
r = requests.post('http://127.0.0.1:8081', data=features)
js = r.json()
if 'description' in js:
    # This is an error.
    print(js['description'])
    res = None
else:
    print(js)
    res = []
    for ans in js['Y']:
        print("Number of neighbors:", len(ans))
        for n in ans:
            print("score, id, name", n)
            res.append((n[0], n[2]['name']))

#######################
# Let's display the images.

txts = list(map(lambda x: str(x[0]), res))
imgs = list(map(lambda x: os.path.join('images', x[1]), res))

from mlinsights.plotting import plot_gallery_images
plot_gallery_images(imgs, txts)

import matplotlib.pyplot as plt
# plt.show()

####################
# Let's stop the server.
proc.kill()

############################
# You can check that the process disappeared.
import psutil
sleep(1)
nb = 0
while nb < 5 and proc.pid in psutil.pids():
    print("Let's wait for the server to terminate.")
    sleep(1)
    nb += 1
<commit_msg>update script example on search engine<commit_after>"""
Search engines for images through a REST API
============================================

This example starts a :epkg:`waitress` server, creates
a :epkg:`WSGI` application based on :epkg:`falcon`
and queries the REST API. This application takes an image and
searches for similar images based on features produced
by a deep learning model.
"""

####################
# Settings.
host = '127.0.0.1'
port = 8081

########################
# Creates the search engine and starts a server in a different process.
# See :func:`search_images_dogcat <ensae_projects.restapi.search_images_dogcat.search_images_dogcat>`.

def process_server(host, port):
    import logging
    logger = logging.getLogger('search_images_dogcat')
    logger.setLevel(logging.INFO)
    hdlr = logging.FileHandler('search_images_dogcat.log')
    formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
    hdlr.setFormatter(formatter)
    logger.addHandler(hdlr)

    # If not specified, the application looks for zip file:
    # http://www.xavierdupre.fr/enseignement/complements/dog-cat-pixabay.zip
    url = None
    from ensae_projects.restapi import search_images_dogcat
    app = search_images_dogcat(url_images=url)

    from waitress import serve
    serve(app, host=host, port=port)

##########################
# Saves this code into a file and we start it
# from a different process.

import os
import ensae_projects

header = """
import sys
sys.path.append(r'{0}')
""".format(os.path.abspath(os.path.join(os.path.dirname(ensae_projects.__file__), '..')))

import inspect
code = "".join(inspect.getsourcelines(process_server)[0])
code = header + code + "\nprocess_server('{0}', {1})\n".format(host, port)
dest = os.path.abspath('temp_scripts')
if not os.path.exists(dest):
    os.mkdir(dest)
code_file = os.path.join(dest, "_start_server.py")
with open(code_file, "w") as f:
    f.write(code)

import sys
from subprocess import Popen
cmd = '{0} -u "{1}"'.format(sys.executable.replace("pythonw",
                                                   "python"), code_file)
proc = Popen(cmd)
print('Start server, process id', proc.pid)

##########################
# Let's wait.
from time import sleep
sleep(15)

####################
# Let's load an image.

from lightmlrestapi.args import image2base64
import ensae_projects.datainc.search_images as si
imgfile = os.path.join(os.path.dirname(si.__file__), "cat-1192026__480.jpg")

from PIL import Image
img = Image.open(imgfile)

import numpy
from matplotlib.pyplot import imshow
imshow(numpy.asarray(img))

############################
# Let's query the server.

import requests
import ujson

b64 = image2base64(imgfile)[1]
features = ujson.dumps({'X': b64})
r = requests.post('http://127.0.0.1:8081', data=features)
js = r.json()
if 'description' in js:
    # This is an error.
    print(js['description'])
    res = None
else:
    print(js)
    res = []
    for ans in js['Y']:
        print("Number of neighbors:", len(ans))
        for n in ans:
            print("score, id, name", n)
            res.append((n[0], n[2]['name']))

#######################
# Let's display the images.

txts = list(map(lambda x: str(x[0]), res))
imgs = list(map(lambda x: os.path.join('images', x[1]), res))

from mlinsights.plotting import plot_gallery_images
plot_gallery_images(imgs, txts)

import matplotlib.pyplot as plt
# plt.show()

####################
# Let's stop the server.
proc.kill()

############################
# You can check that the process disappeared.
import psutil
sleep(1)
nb = 0
while nb < 5 and proc.pid in psutil.pids():
    print("Let's wait for the server to terminate.")
    sleep(1)
    nb += 1
<|endoftext|>


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

# Copyright (c) 2017 shmilee

r'''
History figures
-------------------

This module needs history diagnosis data in group 'history' get by gdr.
'''

import os
import logging
import numpy as np
from gdpy3.plot import tools

__all__ = ['Group' 'Figures' 'get_figurestructure']

log = logging.getLogger('gdp')

__ver = '110922'
__FigInfo = dict()
# field modes: phi, apara, fluidne. 1-8
__ModeFigInfo = {
    'fieldmode%s_%s' % (i, f):
    dict(index=i - 1, field=f,
         real='fieldmode-%s-real' % f, imag='fieldmode-%s-imag' % f)
    for i in range(1, 9) for f in ['phi', 'apara', 'fluidne']
}
__FigInfo.update(__ModeFigInfo)

__keysgrp = 'history/'
__paragrp = 'gtcout/'

Group = 'history'
Figures = tuple(__FigInfo.keys())

# field modes: phi, apara, fluidne. 1-8
def __getax_fieldmode(dictobj, name):
    '''
    Return field modes axesstructures, calculation
    '''

    # check key, get data
    index = __ModeFigInfo[name]['index']
    field = __ModeFigInfo[name]['field']
    real = __keysgrp + __ModeFigInfo[name]['real']
    imag = __keysgrp + __ModeFigInfo[name]['imag']
    tstep, ndiag, nmodes, mmodes, qiflux, rgiflux, rho0 = (
        __paragrp + key for key in
        ('tstep', 'ndiag', 'nmodes', 'mmodes', 'qiflux', 'rgiflux', 'rho0'))
    ndstep = __keysgrp + 'ndstep'
    try:
        yreal = dictobj[real][index]
        yimag = dictobj[imag][index]
        (tstep, ndiag, nmodes, mmodes, qiflux, rgiflux, rho0, ndstep) = \
            dictobj.get_many(tstep, ndiag, nmodes, mmodes,
                             qiflux, rgiflux, rho0, ndstep)
        time = np.arange(1, ndstep + 1) * tstep * ndiag
        n = nmodes[index]
        m = mmodes[index]
        kthetarhoi = n * qiflux / rgiflux * rho0
    except Exception as exc:
        log.error("Failed to get data '%s' from %s! %s" %
                  (name, dictobj.file, exc))
        return None, None

    # 1 original
    axes1 = {
        'data': [
                [1, 'plot', (time, yreal), dict(label='real component')],
                [2, 'plot', (time, yimag), dict(label='imag component')],
                [3, 'legend', (), dict(loc='upper left')],
        ],
        'layout': [
            221,
            dict(title='n=%d, m=%d' % (n, m),
                 xlabel=r'time($R_0/c_s$)',
                 xlim=[0, np.max(time)])
        ],
    }

    # 2 log(amplitude), growth rate
    ya = np.sqrt(yreal**2 + yimag**2)
    logya = tools.savgol_golay_filter(np.log(ya), 47, 3)
    # find growth region
    tmpga = [1 if g > 0 else -ndstep for g in np.gradient(logya)]
    region_len = tools.max_subarray(tmpga)
    for region_start in range(ndstep):
        if sum(tmpga[region_start:region_start + region_len]) == region_len:
            break
    reg1, reg2 = region_start, region_start + region_len
    log.debug("Find growth region: [%s,%s]." % (time[reg1], time[reg2 - 1]))
    # polyfit [0,1] or [0.5,0.9]*growth_region
    result, line1 = tools.fitline(time[reg1:reg2], logya[reg1:reg2], 1,
                                  info='[0,1] growth region')
    growth1 = result[0][0]
    reg3 = int(reg1 + 0.5 * region_len)
    reg4 = int(reg1 + 0.9 * region_len)
    result, line2 = tools.fitline(time[reg3:reg4], logya[reg3:reg4], 1,
                                  info='[0.5,0.9] growth region')
    growth2 = result[0][0]
    axes2 = {
        'data': [
                [1, 'plot', (time, logya), dict()],
                [2, 'plot', (time[reg1:reg2], line1),
                    dict(label=r'Fitting, $\gamma=%.6f$' % growth1)],
                [3, 'plot', (time[reg3:reg4], line2),
                    dict(label=r'Fitting, $\gamma=%.6f$' % growth2)],
                [4, 'legend', (), dict(loc='lower right')],
        ],
        'layout': [
            222,
            dict(title=r'smooth(log(amplitude)), $k_{\theta}\rho_i$=%.6f'
                 % kthetarhoi,
                 xlabel=r'time($R_0/c_s$)',
                 xlim=[0, np.max(time)])
        ],
    }

    # 3 amplitude normalized by growth rate, real frequency
    normreal = tools.savgol_golay_filter(
        np.divide(yreal, np.exp(growth2 * time)), 47, 3)
    normimag = tools.savgol_golay_filter(
        np.divide(yimag, np.exp(growth2 * time)), 47, 3)
    index = [i for i in tools.argrelextrema(normreal, m='both')
             if reg1 + 0.1 * region_len <= i < reg1 + 0.9 * region_len]
    log.debug("real argrelextrema: %s" % index)
    if len(index) >= 2:
        reg3, reg4 = index[0], index[-1]
        omega1 = np.pi * (len(index) - 1) / (time[reg4] - time[reg3])
    else:
        reg3, reg4 = reg1, reg2
        omega1 = 0
    index = [i for i in tools.argrelextrema(normimag, m='both')
             if reg1 + 0.1 * region_len <= i < reg1 + 0.9 * region_len]
    log.debug("imag argrelextrema: %s" % index)
    if len(index) >= 2:
        reg5, reg6, nT = index[0], index[-1], (len(index) - 1) / 2
        omega2 = 2 * np.pi * nT / (time[reg6] - time[reg5])
    else:
        reg5, reg6, nT = reg1, reg2, 0
        omega2 = 0
    axes3 = {
        'data': [
                [1, 'plot', (time, normreal), dict(label='real component')],
                [2, 'plot', (time, normimag), dict(label='imag component')],
                [3, 'axvspan', (time[reg1], time[reg2 - 1]),
                    dict(alpha=0.12, label='(224) FFT region')],
                [4, 'plot', ((time[reg3], time[reg4]),
                             (normreal[reg3], normreal[reg4]), 'D--'),
                    dict(markersize=5, label=r'$\omega=%.6f,nT=%.1f$'
                         % (omega1, nT))],
                [5, 'plot', ((time[reg5], time[reg6]),
                             (normimag[reg5], normimag[reg6]), 'D--'),
                    dict(markersize=5, label=r'$\omega=%.6f,nT=%.1f$'
                         % (omega2, nT))],
                [6, 'legend', (), dict(loc='best')],
        ],
        'layout': [
            223,
            dict(xlabel=r'time($R_0/c_s$)',
                 ylabel='smooth normalized amplitude',
                 xlim=[0, np.max(time)])
        ],
    }
    ymin = min(min(normreal[reg3:reg4]), min(normimag[reg5:reg6]))
    ymax = max(max(normreal[reg3:reg4]), max(normimag[reg5:reg6]))
    if (min(min(normreal), min(normimag)) < 20 * ymin
            or max(max(normreal), max(normimag)) > 20 * ymax):
        axes3['layout'][1]['ylim'] = [3 * ymin, 3 * ymax]

    # 4 FFT, real frequency, calculate by real or imag in growth region
    fft_f, fft_ar, fft_pr = tools.fft(tstep * ndiag, normreal[reg1:reg2])
    fft_f1, fft_ai, fft_pi = tools.fft(tstep * ndiag, normimag[reg1:reg2])
    index = int(region_len / 2)
    index = index + np.argmax(fft_pr[index:])
    log.debug("Get frequency: %s, %s" % (index, fft_f[index]))
    omega3, xlim = fft_f[index], 4 * abs(fft_f[index])
    axes4 = {
        'data': [
                [1, 'plot', (fft_f, fft_pr), dict()],
                [2, 'plot', (fft_f, fft_pi), dict()],
                [3, 'axvline', (omega3,),
                    dict(ls='--', label=r'$\omega=%.6f$' % omega3)],
                [4, 'legend', (), dict(loc='best')]
        ],
        'layout': [
            224,
            dict(xlabel=r'$\omega$($c_s/R_0$)',
                 xlim=[-xlim, xlim])
        ],
    }

    calculation = {'n': n, 'm': m, 'kthetarhoi': kthetarhoi,
                   'growth_long': growth1, 'growth_short': growth2,
                   'omega1': omega1, 'omega2': omega2, 'omega3': omega3}
    return [axes1, axes2, axes3, axes4], calculation

def get_figurestructure(dictobj, name, figurestyle=[]):
    '''
    Get the FigureStructure by name.
    Return FigureStructure and calculation results.

    Parameters
    ----------
    dictobj: a dictionary-like object
        instance of :class:`gdpy3.read.readnpz.ReadNpz`, etc.
    name: figure name
    figurestyle: a list of mplstyles
    '''

    # check name
    if name not in Figures:
        log.error("'%s' is not in the '%s' Figures!" % (name, Group))
        return None, None

    log.debug("Get FigureStructure '%s/%s' ..." % (Group, name))

    # axesstructures
    axesstructures, calculation = None, None
    try:
        if name in __ModeFigInfo:
            axesstructures, calculation = __getax_fieldmode(dictobj, name)
            figurestyle += [{'axes.grid.which': 'both'}]
        elif name in 'TODO':
            pass
    except Exception as exc:
        log.error("Failed to get FigureStructure: %s" % exc)
    if not axesstructures:
        return None, None

    figurestructure = {
        'Style': figurestyle,
        'AxesStructures': axesstructures,
    }
    return figurestructure, calculation
<commit_msg>gdp: history110922, [0.05,0.95] growth region<commit_after># -*- coding: utf-8 -*-

# Copyright (c) 2017 shmilee

r'''
History figures
-------------------

This module needs history diagnosis data in group 'history' get by gdr.
'''

import os
import logging
import numpy as np
from gdpy3.plot import tools

__all__ = ['Group' 'Figures' 'get_figurestructure']

log = logging.getLogger('gdp')

__ver = '110922'
__FigInfo = dict()
# field modes: phi, apara, fluidne. 1-8
__ModeFigInfo = {
    'fieldmode%s_%s' % (i, f):
    dict(index=i - 1, field=f,
         real='fieldmode-%s-real' % f, imag='fieldmode-%s-imag' % f)
    for i in range(1, 9) for f in ['phi', 'apara', 'fluidne']
}
__FigInfo.update(__ModeFigInfo)

__keysgrp = 'history/'
__paragrp = 'gtcout/'

Group = 'history'
Figures = tuple(__FigInfo.keys())

# field modes: phi, apara, fluidne. 1-8
def __getax_fieldmode(dictobj, name):
    '''
    Return field modes axesstructures, calculation
    '''

    # check key, get data
    index = __ModeFigInfo[name]['index']
    field = __ModeFigInfo[name]['field']
    real = __keysgrp + __ModeFigInfo[name]['real']
    imag = __keysgrp + __ModeFigInfo[name]['imag']
    tstep, ndiag, nmodes, mmodes, qiflux, rgiflux, rho0 = (
        __paragrp + key for key in
        ('tstep', 'ndiag', 'nmodes', 'mmodes', 'qiflux', 'rgiflux', 'rho0'))
    ndstep = __keysgrp + 'ndstep'
    try:
        yreal = dictobj[real][index]
        yimag = dictobj[imag][index]
        (tstep, ndiag, nmodes, mmodes, qiflux, rgiflux, rho0, ndstep) = \
            dictobj.get_many(tstep, ndiag, nmodes, mmodes,
                             qiflux, rgiflux, rho0, ndstep)
        time = np.arange(1, ndstep + 1) * tstep * ndiag
        n = nmodes[index]
        m = mmodes[index]
        kthetarhoi = n * qiflux / rgiflux * rho0
    except Exception as exc:
        log.error("Failed to get data '%s' from %s! %s" %
                  (name, dictobj.file, exc))
        return None, None

    # 1 original
    axes1 = {
        'data': [
                [1, 'plot', (time, yreal), dict(label='real component')],
                [2, 'plot', (time, yimag), dict(label='imag component')],
                [3, 'legend', (), dict(loc='upper left')],
        ],
        'layout': [
            221,
            dict(title='n=%d, m=%d' % (n, m),
                 xlabel=r'time($R_0/c_s$)',
                 xlim=[0, np.max(time)])
        ],
    }

    # 2 log(amplitude), growth rate
    ya = np.sqrt(yreal**2 + yimag**2)
    logya = tools.savgol_golay_filter(np.log(ya), 47, 3)
    # find growth region
    tmpga = [1 if g > 0 else -ndstep for g in np.gradient(logya)]
    region_len = tools.max_subarray(tmpga)
    for region_start in range(ndstep):
        if sum(tmpga[region_start:region_start + region_len]) == region_len:
            break
    # [0,1] -> [0.05,0.95], resist smooth
    region_start = int(region_start + 0.05 * region_len)
    region_len = int(0.9 * region_len)
    reg1, reg2 = region_start, region_start + region_len
    log.debug("Find growth region: [%s,%s]." % (time[reg1], time[reg2 - 1]))
    # polyfit [0,1] or [0.5,0.9]*growth_region
    result, line1 = tools.fitline(time[reg1:reg2], logya[reg1:reg2], 1,
                                  info='[0,1] growth region')
    growth1 = result[0][0]
    reg3 = int(reg1 + 0.5 * region_len)
    reg4 = int(reg1 + 0.9 * region_len)
    result, line2 = tools.fitline(time[reg3:reg4], logya[reg3:reg4], 1,
                                  info='[0.5,0.9] growth region')
    growth2 = result[0][0]
    axes2 = {
        'data': [
                [1, 'plot', (time, logya), dict()],
                [2, 'plot', (time[reg1:reg2], line1),
                    dict(label=r'Fitting, $\gamma=%.6f$' % growth1)],
                [3, 'plot', (time[reg3:reg4], line2),
                    dict(label=r'Fitting, $\gamma=%.6f$' % growth2)],
                [4, 'legend', (), dict(loc='lower right')],
        ],
        'layout': [
            222,
            dict(title=r'smooth(log(amplitude)), $k_{\theta}\rho_i$=%.6f'
                 % kthetarhoi,
                 xlabel=r'time($R_0/c_s$)',
                 xlim=[0, np.max(time)])
        ],
    }

    # 3 amplitude normalized by growth rate, real frequency
    normreal = tools.savgol_golay_filter(
        np.divide(yreal, np.exp(growth2 * time)), 47, 3)
    normimag = tools.savgol_golay_filter(
        np.divide(yimag, np.exp(growth2 * time)), 47, 3)
    index = [i for i in tools.argrelextrema(normreal, m='both')
             if reg1 + 0.1 * region_len <= i < reg1 + 0.9 * region_len]
    log.debug("real argrelextrema: %s" % index)
    if len(index) >= 2:
        reg3, reg4 = index[0], index[-1]
        omega1 = np.pi * (len(index) - 1) / (time[reg4] - time[reg3])
    else:
        reg3, reg4 = reg1, reg2
        omega1 = 0
    index = [i for i in tools.argrelextrema(normimag, m='both')
             if reg1 + 0.1 * region_len <= i < reg1 + 0.9 * region_len]
    log.debug("imag argrelextrema: %s" % index)
    if len(index) >= 2:
        reg5, reg6, nT = index[0], index[-1], (len(index) - 1) / 2
        omega2 = 2 * np.pi * nT / (time[reg6] - time[reg5])
    else:
        reg5, reg6, nT = reg1, reg2, 0
        omega2 = 0
    axes3 = {
        'data': [
                [1, 'plot', (time, normreal), dict(label='real component')],
                [2, 'plot', (time, normimag), dict(label='imag component')],
                [3, 'axvspan', (time[reg1], time[reg2 - 1]),
                    dict(alpha=0.12, label='(224) FFT region')],
                [4, 'plot', ((time[reg3], time[reg4]),
                             (normreal[reg3], normreal[reg4]), 'D--'),
                    dict(markersize=5, label=r'$\omega=%.6f,nT=%.1f$'
                         % (omega1, nT))],
                [5, 'plot', ((time[reg5], time[reg6]),
                             (normimag[reg5], normimag[reg6]), 'D--'),
                    dict(markersize=5, label=r'$\omega=%.6f,nT=%.1f$'
                         % (omega2, nT))],
                [6, 'legend', (), dict(loc='best')],
        ],
        'layout': [
            223,
            dict(xlabel=r'time($R_0/c_s$)',
                 ylabel='smooth normalized amplitude',
                 xlim=[0, np.max(time)])
        ],
    }
    ymin = min(min(normreal[reg3:reg4]), min(normimag[reg5:reg6]))
    ymax = max(max(normreal[reg3:reg4]), max(normimag[reg5:reg6]))
    if (min(min(normreal), min(normimag)) < 20 * ymin
            or max(max(normreal), max(normimag)) > 20 * ymax):
        axes3['layout'][1]['ylim'] = [3 * ymin, 3 * ymax]

    # 4 FFT, real frequency, calculate by real or imag in growth region
    fft_f, fft_ar, fft_pr = tools.fft(tstep * ndiag, normreal[reg1:reg2])
    fft_f1, fft_ai, fft_pi = tools.fft(tstep * ndiag, normimag[reg1:reg2])
    index = int(region_len / 2)
    index = index + np.argmax(fft_pr[index:])
    log.debug("Get frequency: %s, %s" % (index, fft_f[index]))
    omega3, xlim = fft_f[index], 4 * abs(fft_f[index])
    axes4 = {
        'data': [
                [1, 'plot', (fft_f, fft_pr), dict()],
                [2, 'plot', (fft_f, fft_pi), dict()],
                [3, 'axvline', (omega3,),
                    dict(ls='--', label=r'$\omega=%.6f$' % omega3)],
                [4, 'legend', (), dict(loc='best')]
        ],
        'layout': [
            224,
            dict(xlabel=r'$\omega$($c_s/R_0$)',
                 xlim=[-xlim, xlim])
        ],
    }

    calculation = {'n': n, 'm': m, 'kthetarhoi': kthetarhoi,
                   'growth_long': growth1, 'growth_short': growth2,
                   'omega1': omega1, 'omega2': omega2, 'omega3': omega3}
    return [axes1, axes2, axes3, axes4], calculation

def get_figurestructure(dictobj, name, figurestyle=[]):
    '''
    Get the FigureStructure by name.
    Return FigureStructure and calculation results.

    Parameters
    ----------
    dictobj: a dictionary-like object
        instance of :class:`gdpy3.read.readnpz.ReadNpz`, etc.
    name: figure name
    figurestyle: a list of mplstyles
    '''

    # check name
    if name not in Figures:
        log.error("'%s' is not in the '%s' Figures!" % (name, Group))
        return None, None

    log.debug("Get FigureStructure '%s/%s' ..." % (Group, name))

    # axesstructures
    axesstructures, calculation = None, None
    try:
        if name in __ModeFigInfo:
            axesstructures, calculation = __getax_fieldmode(dictobj, name)
            figurestyle += [{'axes.grid.which': 'both'}]
        elif name in 'TODO':
            pass
    except Exception as exc:
        log.error("Failed to get FigureStructure: %s" % exc)
    if not axesstructures:
        return None, None

    figurestructure = {
        'Style': figurestyle,
        'AxesStructures': axesstructures,
    }
    return figurestructure, calculation
<|endoftext|>


<commit_before>import os
import argparse
from cStringIO import StringIO

from django.db import connections

from openquake.nrmllib.hazard.parsers import HazardCurveParser
from openquake.engine.db import models
from openquake.engine.engine import get_current_user

def import_hazard_curves(fileobj, user=None):
    """
    Parse the file with the hazard curves and import it into the tables
    hazard_curve and hazard_curve_data. It also creates a new output record,
    unrelated to a job.

    :param fileobj:
        a file-like object associated to an XML file
    :returns:
        the generated :class:`openquake.engine.db.models.Output` object
        and the generated :class:`openquake.engine.db.models.HazardCalculation`
        object.
    """
    fname = fileobj.name
    curs = connections['reslt_writer'].cursor().cursor.cursor  # DB API cursor
    owner = models.OqUser.objects.get(user_name=user) \
        if user else get_current_user()

    hc = models.HazardCalculation.objects.create(
        owner=owner,
        base_path=os.path.dirname(fname),
        description='HazardCurve importer, file %s' % os.path.basename(fname),
        calculation_mode='classical', maximum_distance=100)
    # XXX: what about the maximum_distance?

    out = models.Output.objects.create(
        owner=owner, display_name='Imported from %r' % fname,
        output_type='hazard_curve')

    f = StringIO()
    # convert the XML into a tab-separated StringIO
    hazcurve = HazardCurveParser(fileobj).parse()
    haz_curve = models.HazardCurve.objects.create(
        investigation_time=hazcurve.investigation_time,
        imt=hazcurve.imt,
        imls=hazcurve.imls,
        quantile=hazcurve.quantile_value,
        statistics=hazcurve.statistics,
        sa_damping=hazcurve.sa_damping,
        sa_period=hazcurve.sa_period,
        output=out)
    hazard_curve_id = str(haz_curve.id)
    for node in hazcurve:
        loc = node.location
        poes = node.poes
        poes = '{%s}' % str(poes)[1:-1]
        print >> f, '\t'.join([hazard_curve_id, poes,
                               'SRID=4326;POINT(%s %s)' % (loc.x, loc.y)])
    f.reset()
    ## import the file-like object with a COPY FROM
    try:
        curs.copy_expert(
            'copy hzrdr.hazard_curve_data (hazard_curve_id, poes, location) '
            'from stdin', f)
    except:
        curs.connection.rollback()
        raise
    else:
        curs.connection.commit()
    finally:
        f.close()
    return out, hc

if __name__ == '__main__':
    p = argparse.ArgumentParser()
    p.add_argument('fname')
    arg = p.parse_args()
    print 'Imported', import_hazard_curves(arg.fname)
<commit_msg>tools/import_hazard_curves:<commit_after>import os
import argparse
from cStringIO import StringIO

from django.db import connections

from openquake.nrmllib.hazard.parsers import HazardCurveXMLParser
from openquake.engine.db import models
from openquake.engine.engine import get_current_user

def import_hazard_curves(fileobj, user=None):
    """
    Parse the file with the hazard curves and import it into the tables
    hazard_curve and hazard_curve_data. It also creates a new output record,
    unrelated to a job.

    :param fileobj:
        a file-like object associated to an XML file
    :returns:
        the generated :class:`openquake.engine.db.models.Output` object
        and the generated :class:`openquake.engine.db.models.HazardCalculation`
        object.
    """
    fname = fileobj.name
    curs = connections['reslt_writer'].cursor().cursor.cursor  # DB API cursor
    owner = models.OqUser.objects.get(user_name=user) \
        if user else get_current_user()

    hc = models.HazardCalculation.objects.create(
        owner=owner,
        base_path=os.path.dirname(fname),
        description='HazardCurve importer, file %s' % os.path.basename(fname),
        calculation_mode='classical', maximum_distance=100)
    # XXX: what about the maximum_distance?

    out = models.Output.objects.create(
        owner=owner, display_name='Imported from %r' % fname,
        output_type='hazard_curve')

    f = StringIO()
    # convert the XML into a tab-separated StringIO
    hazcurve = HazardCurveXMLParser(fileobj).parse()
    haz_curve = models.HazardCurve.objects.create(
        investigation_time=hazcurve.investigation_time,
        imt=hazcurve.imt,
        imls=hazcurve.imls,
        quantile=hazcurve.quantile_value,
        statistics=hazcurve.statistics,
        sa_damping=hazcurve.sa_damping,
        sa_period=hazcurve.sa_period,
        output=out)
    hazard_curve_id = str(haz_curve.id)
    for node in hazcurve:
        loc = node.location
        poes = node.poes
        poes = '{%s}' % str(poes)[1:-1]
        print >> f, '\t'.join([hazard_curve_id, poes,
                               'SRID=4326;POINT(%s %s)' % (loc.x, loc.y)])
    f.reset()
    ## import the file-like object with a COPY FROM
    try:
        curs.copy_expert(
            'copy hzrdr.hazard_curve_data (hazard_curve_id, poes, location) '
            'from stdin', f)
    except:
        curs.connection.rollback()
        raise
    else:
        curs.connection.commit()
    finally:
        f.close()
    return out, hc

if __name__ == '__main__':
    p = argparse.ArgumentParser()
    p.add_argument('fname')
    arg = p.parse_args()
    print 'Imported', import_hazard_curves(arg.fname)
<|endoftext|>


<commit_before><commit_msg>fixed class name<commit_after><|endoftext|>


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

from ..base import *  # noqa

class idmen(Variable):
    column = IntCol
    entity_class = Individus
    is_permanent = True
    label = u"Identifiant du ménage"

class idfoy(Variable):
    column = IntCol
    entity_class = Individus
    is_permanent = True
    label = u"Identifiant du foyer"

class idfam(Variable):
    column = IntCol
    entity_class = Individus
    is_permanent = True
    label = u"Identifiant de la famille"

class quimen(Variable):
    column = EnumCol(enum = QUIMEN)
    entity_class = Individus
    is_permanent = True

class quifoy(Variable):
    column = EnumCol(enum = QUIFOY)
    entity_class = Individus
    is_permanent = True

class quifam(Variable):
    column = EnumCol(enum = QUIFAM)
    entity_class = Individus
    is_permanent = True

class date_naissance(Variable):
    column = DateCol(default = date(1970, 1, 1))
    entity_class = Individus
    is_permanent = True
    label = u"Date de naissance"

class adoption(Variable):
    column = BoolCol
    entity_class = Individus
    label = u"Enfant adopté"

class garde_alternee(Variable):
    column = BoolCol
    entity_class = Individus
    label = u'Enfant en garde alternée'
    base_function = requested_period_last_or_next_value

class activite(Variable):
    column = EnumCol(
        default = 4,
        enum = Enum([u'Actif occupé',
            u'Chômeur',
            u'Étudiant, élève',
            u'Retraité',
            u'Autre inactif']),
        )
    entity_class = Individus
    label = u"Activité"

class enceinte(Variable):
    column = BoolCol
    entity_class = Individus
    label = u"Est enceinte"

class statmarit(Variable):
    column = EnumCol(
        default = 2,
        enum = Enum([u"Marié",
            u"Célibataire",
            u"Divorcé",
            u"Veuf",
            u"Pacsé",
            u"Jeune veuf"], start = 1),
        )
    entity_class = Individus
    label = u"Statut marital"

class nbN(Variable):
    cerfa_field = u"N"
    column = PeriodSizeIndependentIntCol
    entity_class = FoyersFiscaux
    label = u"Nombre d'enfants mariés/pacsés et d'enfants non mariés chargés de famille"

class nbR(Variable):
    cerfa_field = u"R"
    column = PeriodSizeIndependentIntCol
    entity_class = FoyersFiscaux
    label = u"Nombre de titulaires (autres que les enfants) de la carte invalidité d'au moins 80 %"

class caseE(Variable):
    cerfa_field = u"E"
    column = BoolCol
    entity_class = FoyersFiscaux
    label = u"Situation pouvant donner droit à une demi-part supplémentaire : vous vivez seul au 1er janvier de l'année de perception des revenus et vous avez élevé un enfant pendant moins de 5 ans durant la période où vous viviez seul"
    stop_date = date(2012, 12, 31)

class caseF(Variable):
    cerfa_field = u"F"
    column = BoolCol
    entity_class = FoyersFiscaux
    label = u"Situation pouvant donner droit à une demi-part supplémentaire : conjoint titulaire d'une pension ou d'une carte d'invalidité (vivant ou décédé l'année de perception des revenus)"

class caseG(Variable):
    cerfa_field = u"G"
    column = BoolCol
    entity_class = FoyersFiscaux
    label = u"Titulaire d'une pension de veuve de guerre"

  # attention, ne pas confondre caseG et nbG qui se rapportent toutes les 2 à une "case" G, l'une étant une vraie case que l'on remplt et l'autre une case que l'on coche
class caseH(Variable):
    cerfa_field = u"H"
    column = PeriodSizeIndependentIntCol
    entity_class = FoyersFiscaux
    label = u"Année de naissance des enfants à charge en garde alternée"

# il ne s'agit pas à proprement parlé de la case H, les cases permettant d'indiquer l'année de naissance
#    se rapportent bien à nbH mais ne sont pas nommées, choisissons nous de laisser cerfa_field = u'H' pour caseH ?
#    De plus les caseH peuvent être multiples puisqu'il peut y avoir plusieurs enfants? donc faut-il les nommer caseH1, caseH2...caseH6 (les 6 présentes dans la déclaration) ?
#    il faut aussi créer les cases F, G, R et I qui donnent également les années de naissances des PAC

class caseK(Variable):
    cerfa_field = u"K"
    column = BoolCol
    entity_class = FoyersFiscaux
    label = u"Situation pouvant donner droit à une demi-part supplémentaire: vous avez eu un enfant décédé après l’âge de 16 ans ou par suite de faits de guerre"
    stop_date = date(2011, 12, 31)

class caseL(Variable):
    cerfa_field = u"L"
    column = BoolCol
    entity_class = FoyersFiscaux
    label = u"Situation pouvant donner droit à une demi-part supplémentaire: vous vivez seul au 1er janvier de l'année de perception des revenus et vous avez élevé un enfant pendant au moins 5 ans durant la période où vous viviez seul"

class caseN(Variable):
    cerfa_field = u"N"
    column = BoolCol
    entity_class = FoyersFiscaux
    label = u"Vous ne viviez pas seul au 1er janvier de l'année de perception des revenus"

class caseP(Variable):
    cerfa_field = u"P"
    column = BoolCol
    entity_class = FoyersFiscaux
    label = u"Titulaire d'une pension pour une invalidité d'au moins 40 % ou d'une carte d'invalidité d'au moins 80%"

class caseS(Variable):
    cerfa_field = u"S"
    column = BoolCol
    entity_class = FoyersFiscaux
    label = u"Vous êtes mariés/pacsés et l'un des deux déclarants âgé de plus de 75 ans est titulaire de la carte du combattant ou d'une pension militaire d'invalidité ou de victime de guerre"

class caseT(Variable):
    cerfa_field = u"T"
    column = BoolCol
    entity_class = FoyersFiscaux
    label = u"Vous êtes parent isolé au 1er janvier de l'année de perception des revenus"

class caseW(Variable):
    cerfa_field = u"W"
    column = BoolCol
    entity_class = FoyersFiscaux
    label = u"Vous ou votre conjoint (même s'il est décédé), âgés de plus de 75 ans, êtes titulaire de la carte du combattant ou d'une pension militaire d'invalidité ou de victime de guerre"

class handicap(Variable):
    column = BoolCol
    entity_class = Individus
    label= u"Individu en situation de handicap"

class invalidite(Variable):
  column = BoolCol
  entity_class = Individus
  label=u"Individu titulaire d'une carte d'invalidité"

class nb_parents(Variable):
    column = PeriodSizeIndependentIntCol(default = 0)
    entity_class = Familles
    label = u"Nombre d'adultes (parents) dans la famille"

    def function(self, simulation, period):
        # Note : Cette variable est "instantanée" : quelque soit la période demandée, elle retourne la valeur au premier
        # jour, sans changer la période.
        quifam_holder = simulation.compute('quifam', period)

        quifam = self.filter_role(quifam_holder, role = PART)

        return period, 1 + 1 * (quifam == PART)

class maries(Variable):
    column = BoolCol(default = False)
    entity_class = Familles
    label = u"maries"

    def function(self, simulation, period):
        """couple = 1 si couple marié sinon 0 TODO: faire un choix avec couple ?"""
        # Note : Cette variable est "instantanée" : quelque soit la période demandée, elle retourne la valeur au premier
        # jour, sans changer la période.
        statmarit_holder = simulation.compute('statmarit', period)

        statmarit = self.filter_role(statmarit_holder, role = CHEF)

        return period, statmarit == 1

class en_couple(Variable):
    column = BoolCol
    entity_class = Familles
    label = u"Indicatrice de vie en couple"

    def function(self, simulation, period):
        '''
        en_couple = 1 si vie en couple TODO: pas très heureux
        '''
        # Note : Cette variable est "instantanée" : quelque soit la période demandée, elle retourne la valeur au premier
        # jour, sans changer la période.
        nb_parents = simulation.calculate('nb_parents', period)

        return period, nb_parents == 2

class est_enfant_dans_famille(Variable):
    column = BoolCol
    entity_class = Individus
    label = u"Indique qe l'individu est un enfant dans une famille"

    def function(self, simulation, period):
        quifam = simulation.calculate('quifam', period)
        return period, quifam > PART

class etudiant(Variable):
    column = BoolCol(default = False)
    entity_class = Individus
    label = u"Indicatrice individuelle étudiant"

    def function(self, simulation, period):
        # Note : Cette variable est "instantanée" : quelque soit la période demandée, elle retourne la valeur au premier
        # jour, sans changer la période.
        activite = simulation.calculate('activite', period)

        return period, activite == 2

class rempli_obligation_scolaire(Variable):
    column = BoolCol(default = True)
    entity_class = Individus
    label = u"Rempli l'obligation scolaire"

class ressortissant_eee(Variable):
    column = BoolCol(default = True)
    entity_class = Individus
    label = u"Ressortissant de l'EEE ou de la Suisse."

class duree_possession_titre_sejour(Variable):
    column = IntCol
    entity_class = Individus
    label = u"Durée depuis laquelle l'individu possède un titre de séjour (en années)"
<commit_msg>Introduce enfant_place<commit_after># -*- coding: utf-8 -*-

from ..base import *  # noqa

class idmen(Variable):
    column = IntCol
    entity_class = Individus
    is_permanent = True
    label = u"Identifiant du ménage"

class idfoy(Variable):
    column = IntCol
    entity_class = Individus
    is_permanent = True
    label = u"Identifiant du foyer"

class idfam(Variable):
    column = IntCol
    entity_class = Individus
    is_permanent = True
    label = u"Identifiant de la famille"

class quimen(Variable):
    column = EnumCol(enum = QUIMEN)
    entity_class = Individus
    is_permanent = True

class quifoy(Variable):
    column = EnumCol(enum = QUIFOY)
    entity_class = Individus
    is_permanent = True

class quifam(Variable):
    column = EnumCol(enum = QUIFAM)
    entity_class = Individus
    is_permanent = True

class date_naissance(Variable):
    column = DateCol(default = date(1970, 1, 1))
    entity_class = Individus
    is_permanent = True
    label = u"Date de naissance"

class adoption(Variable):
    column = BoolCol
    entity_class = Individus
    label = u"Enfant adopté"

class garde_alternee(Variable):
    column = BoolCol
    entity_class = Individus
    label = u'Enfant en garde alternée'
    base_function = requested_period_last_or_next_value

class activite(Variable):
    column = EnumCol(
        default = 4,
        enum = Enum([u'Actif occupé',
            u'Chômeur',
            u'Étudiant, élève',
            u'Retraité',
            u'Autre inactif']),
        )
    entity_class = Individus
    label = u"Activité"

class enceinte(Variable):
    column = BoolCol
    entity_class = Individus
    label = u"Est enceinte"

class statmarit(Variable):
    column = EnumCol(
        default = 2,
        enum = Enum([u"Marié",
            u"Célibataire",
            u"Divorcé",
            u"Veuf",
            u"Pacsé",
            u"Jeune veuf"], start = 1),
        )
    entity_class = Individus
    label = u"Statut marital"

class nbN(Variable):
    cerfa_field = u"N"
    column = PeriodSizeIndependentIntCol
    entity_class = FoyersFiscaux
    label = u"Nombre d'enfants mariés/pacsés et d'enfants non mariés chargés de famille"

class nbR(Variable):
    cerfa_field = u"R"
    column = PeriodSizeIndependentIntCol
    entity_class = FoyersFiscaux
    label = u"Nombre de titulaires (autres que les enfants) de la carte invalidité d'au moins 80 %"

class caseE(Variable):
    cerfa_field = u"E"
    column = BoolCol
    entity_class = FoyersFiscaux
    label = u"Situation pouvant donner droit à une demi-part supplémentaire : vous vivez seul au 1er janvier de l'année de perception des revenus et vous avez élevé un enfant pendant moins de 5 ans durant la période où vous viviez seul"
    stop_date = date(2012, 12, 31)

class caseF(Variable):
    cerfa_field = u"F"
    column = BoolCol
    entity_class = FoyersFiscaux
    label = u"Situation pouvant donner droit à une demi-part supplémentaire : conjoint titulaire d'une pension ou d'une carte d'invalidité (vivant ou décédé l'année de perception des revenus)"

class caseG(Variable):
    cerfa_field = u"G"
    column = BoolCol
    entity_class = FoyersFiscaux
    label = u"Titulaire d'une pension de veuve de guerre"

  # attention, ne pas confondre caseG et nbG qui se rapportent toutes les 2 à une "case" G, l'une étant une vraie case que l'on remplt et l'autre une case que l'on coche
class caseH(Variable):
    cerfa_field = u"H"
    column = PeriodSizeIndependentIntCol
    entity_class = FoyersFiscaux
    label = u"Année de naissance des enfants à charge en garde alternée"

# il ne s'agit pas à proprement parlé de la case H, les cases permettant d'indiquer l'année de naissance
#    se rapportent bien à nbH mais ne sont pas nommées, choisissons nous de laisser cerfa_field = u'H' pour caseH ?
#    De plus les caseH peuvent être multiples puisqu'il peut y avoir plusieurs enfants? donc faut-il les nommer caseH1, caseH2...caseH6 (les 6 présentes dans la déclaration) ?
#    il faut aussi créer les cases F, G, R et I qui donnent également les années de naissances des PAC

class caseK(Variable):
    cerfa_field = u"K"
    column = BoolCol
    entity_class = FoyersFiscaux
    label = u"Situation pouvant donner droit à une demi-part supplémentaire: vous avez eu un enfant décédé après l’âge de 16 ans ou par suite de faits de guerre"
    stop_date = date(2011, 12, 31)

class caseL(Variable):
    cerfa_field = u"L"
    column = BoolCol
    entity_class = FoyersFiscaux
    label = u"Situation pouvant donner droit à une demi-part supplémentaire: vous vivez seul au 1er janvier de l'année de perception des revenus et vous avez élevé un enfant pendant au moins 5 ans durant la période où vous viviez seul"

class caseN(Variable):
    cerfa_field = u"N"
    column = BoolCol
    entity_class = FoyersFiscaux
    label = u"Vous ne viviez pas seul au 1er janvier de l'année de perception des revenus"

class caseP(Variable):
    cerfa_field = u"P"
    column = BoolCol
    entity_class = FoyersFiscaux
    label = u"Titulaire d'une pension pour une invalidité d'au moins 40 % ou d'une carte d'invalidité d'au moins 80%"

class caseS(Variable):
    cerfa_field = u"S"
    column = BoolCol
    entity_class = FoyersFiscaux
    label = u"Vous êtes mariés/pacsés et l'un des deux déclarants âgé de plus de 75 ans est titulaire de la carte du combattant ou d'une pension militaire d'invalidité ou de victime de guerre"

class caseT(Variable):
    cerfa_field = u"T"
    column = BoolCol
    entity_class = FoyersFiscaux
    label = u"Vous êtes parent isolé au 1er janvier de l'année de perception des revenus"

class caseW(Variable):
    cerfa_field = u"W"
    column = BoolCol
    entity_class = FoyersFiscaux
    label = u"Vous ou votre conjoint (même s'il est décédé), âgés de plus de 75 ans, êtes titulaire de la carte du combattant ou d'une pension militaire d'invalidité ou de victime de guerre"

class handicap(Variable):
    column = BoolCol
    entity_class = Individus
    label= u"Individu en situation de handicap"

class invalidite(Variable):
  column = BoolCol
  entity_class = Individus
  label=u"Individu titulaire d'une carte d'invalidité"

class nb_parents(Variable):
    column = PeriodSizeIndependentIntCol(default = 0)
    entity_class = Familles
    label = u"Nombre d'adultes (parents) dans la famille"

    def function(self, simulation, period):
        # Note : Cette variable est "instantanée" : quelque soit la période demandée, elle retourne la valeur au premier
        # jour, sans changer la période.
        quifam_holder = simulation.compute('quifam', period)

        quifam = self.filter_role(quifam_holder, role = PART)

        return period, 1 + 1 * (quifam == PART)

class maries(Variable):
    column = BoolCol(default = False)
    entity_class = Familles
    label = u"maries"

    def function(self, simulation, period):
        """couple = 1 si couple marié sinon 0 TODO: faire un choix avec couple ?"""
        # Note : Cette variable est "instantanée" : quelque soit la période demandée, elle retourne la valeur au premier
        # jour, sans changer la période.
        statmarit_holder = simulation.compute('statmarit', period)

        statmarit = self.filter_role(statmarit_holder, role = CHEF)

        return period, statmarit == 1

class en_couple(Variable):
    column = BoolCol
    entity_class = Familles
    label = u"Indicatrice de vie en couple"

    def function(self, simulation, period):
        '''
        en_couple = 1 si vie en couple TODO: pas très heureux
        '''
        # Note : Cette variable est "instantanée" : quelque soit la période demandée, elle retourne la valeur au premier
        # jour, sans changer la période.
        nb_parents = simulation.calculate('nb_parents', period)

        return period, nb_parents == 2

class est_enfant_dans_famille(Variable):
    column = BoolCol
    entity_class = Individus
    label = u"Indique qe l'individu est un enfant dans une famille"

    def function(self, simulation, period):
        quifam = simulation.calculate('quifam', period)
        return period, quifam > PART

class etudiant(Variable):
    column = BoolCol(default = False)
    entity_class = Individus
    label = u"Indicatrice individuelle étudiant"

    def function(self, simulation, period):
        # Note : Cette variable est "instantanée" : quelque soit la période demandée, elle retourne la valeur au premier
        # jour, sans changer la période.
        activite = simulation.calculate('activite', period)

        return period, activite == 2

class rempli_obligation_scolaire(Variable):
    column = BoolCol(default = True)
    entity_class = Individus
    label = u"Rempli l'obligation scolaire"

class ressortissant_eee(Variable):
    column = BoolCol(default = True)
    entity_class = Individus
    label = u"Ressortissant de l'EEE ou de la Suisse."

class duree_possession_titre_sejour(Variable):
    column = IntCol
    entity_class = Individus
    label = u"Durée depuis laquelle l'individu possède un titre de séjour (en années)"

class enfant_place(Variable):
    column = BoolCol
    entity_class = Individus
    label = u"Enfant placé en structure spécialisée ou famille d'accueil"
<|endoftext|>