<commit_before>from aiida import load_dbenv
load_dbenv()

from aiida.orm import load_node, load_workflow
from aiida.orm import Code, DataFactory

import matplotlib.pyplot as plt

StructureData = DataFactory('structure')
ParameterData = DataFactory('parameter')
ArrayData = DataFactory('array')
KpointsData = DataFactory('array.kpoints')

import numpy as np

def get_plot(band_data, q_path, title='', ylabel='', labels=None, q_points=None, freq_tolerance=1e-5):

    for i, (data, p) in enumerate(zip(band_data, q_path)):
        if q_points is not None:
            q_norm = np.linalg.norm(q_points[i], axis=1)
            indices = np.where(q_norm > freq_tolerance)[0]
            plt.plot(p[indices], data[indices], color='r')
        else:
            plt.plot(p, data, color='r')

    plt.axes().get_xaxis().set_ticks([])
    plt.ylabel(ylabel)
    plt.xlabel('Wave vector')
    plt.xlim([0, q_path[-1][-1]])
    plt.axhline(y=0, color='k', ls='dashed')
    plt.suptitle(title)

    if labels is not None:
        plt.rcParams.update({'mathtext.default':  'regular' })
        labels_e = []
        x_labels = []
        for i, data in enumerate(q_path):
            if labels[i][0] == labels[i-1][1]:
                labels_e.append('$'+labels[i][0].replace('GAMMA', '\Gamma')+'$')
            else:
                labels_e.append('$'+labels[i-1][1].replace('GAMMA', '\Gamma')+'/'+labels[i][0].replace('GAMMA', '\Gamma')+'$')
            x_labels.append(q_path[i][0])
        x_labels.append(q_path[-1][-1])
        labels_e.append('$'+labels[-1][1].replace('GAMMA', '\Gamma')+'$')
        labels_e[0]='$'+labels[0][0].replace('GAMMA', '\Gamma')+'$'

        plt.xticks(x_labels, labels_e, rotation='horizontal')

#######################
wf = load_workflow(465)
#######################

# Band structure

bs = wf.get_result('band_structure')

plt.figure(1)
get_plot(bs.get_array('frequencies'), bs.get_array('q_path'),
         title='Phonon band structure',
         ylabel='Frequency [THz]',
         labels=bs.get_array('labels'))

plt.figure(2)
get_plot(bs.get_array('eigenvalues'), bs.get_array('q_path'),
         title='Eigenvalues',
         labels=bs.get_array('labels'))
plt.figure(3)
get_plot(bs.get_array('gruneisen'), bs.get_array('q_path'),
         title='Mode Gruneisen parameter',
         ylabel='$\gamma$',
         labels=bs.get_array('labels'),
         q_points=bs.get_array('q_points'))

plt.figure(4)
get_plot(bs.get_array('distances').T, bs.get_array('q_path'),
         title='Distances',
         ylabel='',
         labels=bs.get_array('labels'))

# Mesh
plt.figure(5)

mesh = wf.get_result('mesh')

plt.xlabel('Frequency [THz]')
plt.ylabel('$\gamma$')
plt.suptitle('Mode Gruneisen parameter')

for (g, freq) in zip(mesh.get_array('frequencies').T, mesh.get_array('gruneisen').T):
    plt.plot(g, freq, marker='o', linestyle='None', markeredgecolor='black', color='red')

plt.show()

<commit_msg>Add gruneisen lauch example<commit_after>from aiida import load_dbenv
load_dbenv()

from aiida.orm import load_node, load_workflow
from aiida.orm import Code, DataFactory

import matplotlib.pyplot as plt

StructureData = DataFactory('structure')
ParameterData = DataFactory('parameter')
ArrayData = DataFactory('array')
KpointsData = DataFactory('array.kpoints')

import numpy as np

def get_plot(band_data, q_path, title='', ylabel='', labels=None, q_points=None, freq_tolerance=1e-5):

    for i, (data, p) in enumerate(zip(band_data, q_path)):
        if q_points is not None:
            q_norm = np.linalg.norm(q_points[i], axis=1)
            indices = np.where(q_norm > freq_tolerance)[0]
            plt.plot(p[indices], data[indices], color='r')
        else:
            plt.plot(p, data, color='r')

    plt.axes().get_xaxis().set_ticks([])
    plt.ylabel(ylabel)
    plt.xlabel('Wave vector')
    plt.xlim([0, q_path[-1][-1]])
    plt.axhline(y=0, color='k', ls='dashed')
    plt.suptitle(title)

    if labels is not None:
        plt.rcParams.update({'mathtext.default':  'regular' })
        labels_e = []
        x_labels = []
        for i, data in enumerate(q_path):
            if labels[i][0] == labels[i-1][1]:
                labels_e.append('$'+labels[i][0].replace('GAMMA', '\Gamma')+'$')
            else:
                labels_e.append('$'+labels[i-1][1].replace('GAMMA', '\Gamma')+'/'+labels[i][0].replace('GAMMA', '\Gamma')+'$')
            x_labels.append(q_path[i][0])
        x_labels.append(q_path[-1][-1])
        labels_e.append('$'+labels[-1][1].replace('GAMMA', '\Gamma')+'$')
        labels_e[0]='$'+labels[0][0].replace('GAMMA', '\Gamma')+'$'

        plt.xticks(x_labels, labels_e, rotation='horizontal')

#######################
wf = load_workflow(465)
#######################

# Band structure

bs = wf.get_result('band_structure')

plt.figure(1)
get_plot(bs.get_array('frequencies'), bs.get_array('q_path'),
         title='Phonon band structure',
         ylabel='Frequency [THz]',
         labels=bs.get_array('labels'))

plt.figure(2)
get_plot(bs.get_array('eigenvalues'), bs.get_array('q_path'),
         title='Eigenvalues',
         labels=bs.get_array('labels'))
plt.figure(3)
get_plot(bs.get_array('gruneisen'), bs.get_array('q_path'),
         title='Mode Gruneisen parameter',
         ylabel='$\gamma$',
         labels=bs.get_array('labels'),
         q_points=bs.get_array('q_points'))

# Mesh
plt.figure(4)

mesh = wf.get_result('mesh')

plt.xlabel('Frequency [THz]')
plt.ylabel('$\gamma$')
plt.suptitle('Mode Gruneisen parameter')

for (g, freq) in zip(mesh.get_array('frequencies').T, mesh.get_array('gruneisen').T):
    plt.plot(g, freq, marker='o', linestyle='None', markeredgecolor='black', color='red')

plt.show()

<|endoftext|>


<commit_before>import json
import faerie1
from pyspark.sql import Row
import Toolkit
import time

def processDoc(line, d):
    uri = line["uri"]
    city = line["locality"]
    state = line["region"]
    country = line["country"]
    start_time = time.clock()

    country_can = faerie1.processDoc2(uri, country, d.value.all_faerie_dict["countries_dict"])

    cities_can = search(country_can, uri, state, city, d)
    process_time = str((time.clock() - start_time)*1000)
    print "Time take to process: " + json.dumps(line) + " is " + process_time
    jsent = []
    if cities_can and 'entities' in cities_can:
        for eid in cities_can["entities"]:
            entity = cities_can["entities"][eid]
            snc = Toolkit.get_value_json(eid + "$snc", d.value.all_city_dict,'$')
            if snc != '':
                temp = Row(id=eid,value=entity["value"] + ","+snc,candwins=entity["candwins"])
                jsent.append(temp)

        jsdoc = Row(id=cities_can["document"]["id"],value=cities_can["document"]["value"] + ","+state+","+country)
        jsonline = Row(document=jsdoc,entities=jsent, processtime=process_time)
        return jsonline
    else:
        "cities_can has no entities:", city + "," + state + "," + country + "," + uri

    return ''

def search(country_can, uri, state, city, d):
    states_can = {}
    cities_can = {}
    if country_can and country_can != {} and country_can["entities"] != {}:
        for country_uri in country_can["entities"]:
            if state != "":
                if 'entities' in states_can:
                    states_can["entities"] = dict(states_can["entities"],
                                          **faerie1.processDoc2(uri, state, d.value.all_faerie_dict[country_uri]["states_dict"])[
                                                  "entities"])
                else:
                    states_can = faerie1.processDoc2(uri, state, d.value.all_faerie_dict[country_uri]["states_dict"])
            if states_can == None or states_can == {} or states_can["entities"] == {}:
                # if input state is empty, get the state uris from all_dicts in that country
                states_can["entities"] = d.value.all_faerie_dict[country_uri]["states_dict"][4].values()

            cities_can = searchcity(states_can,uri,city, d)
    else:
        if state != "":
            states_can = faerie1.processDoc2(uri, state, d.value.state_faerie_dict)

        cities_can = searchcity(states_can, uri, city, d)

    return cities_can

def searchcity(states_can, uri, city, d):
    cities_can = {}
    if states_can and states_can != {} and states_can["entities"] != {}:
        for state_uri in states_can["entities"]:
            country_uri = d.value.state_dict[state_uri]["country_uri"]
            if country_uri != '':
                if 'entities' in cities_can:
                    cities_can_add = faerie1.processDoc2(uri, city, d.value.all_faerie_dict[country_uri][state_uri]["cities"])
                    if cities_can_add != {}:
                        cities_can["entities"] = dict(cities_can["entities"],
                                              **cities_can_add["entities"])
                else:
                    cities_can = faerie1.processDoc2(uri, city, d.value.all_faerie_dict[country_uri][state_uri]["cities"])
            else:
                print "Line 73:" + state_uri
    else:
        cities_can = faerie1.processDoc2(uri,city,d.value.city_faerie_dict)

    return cities_can

def run(d, input_rdd):

    candidates = input_rdd.map(lambda line : processDoc(json.loads(line), d))
    return candidates

<commit_msg>change the city candidates threshold<commit_after>import json
import faerie1
from pyspark.sql import Row
import Toolkit
import time

def processDoc(line, d):
    uri = line["uri"]
    city = line["locality"]
    state = line["region"]
    country = line["country"]
    start_time = time.clock()

    country_can = faerie1.processDoc2(uri, country, d.value.all_faerie_dict["countries_dict"])

    cities_can = search(country_can, uri, state, city, d)
    process_time = str((time.clock() - start_time)*1000)
    print "Time take to process: " + json.dumps(line) + " is " + process_time
    jsent = []
    if cities_can and 'entities' in cities_can:
        for eid in cities_can["entities"]:
            entity = cities_can["entities"][eid]
            snc = Toolkit.get_value_json(eid + "$snc", d.value.all_city_dict,'$')
            if snc != '':
                temp = Row(id=eid,value=entity["value"] + ","+snc,candwins=entity["candwins"])
                jsent.append(temp)

        jsdoc = Row(id=cities_can["document"]["id"],value=cities_can["document"]["value"] + ","+state+","+country)
        jsonline = Row(document=jsdoc,entities=jsent, processtime=process_time)
        return jsonline
    else:
        "cities_can has no entities:", city + "," + state + "," + country + "," + uri

    return ''

def search(country_can, uri, state, city, d):
    states_can = {}
    cities_can = {}
    if country_can and country_can != {} and country_can["entities"] != {}:
        for country_uri in country_can["entities"]:
            if state != "":
                if 'entities' in states_can:
                    states_can["entities"] = dict(states_can["entities"],
                                          **faerie1.processDoc2(uri, state, d.value.all_faerie_dict[country_uri]["states_dict"])[
                                                  "entities"])
                else:
                    states_can = faerie1.processDoc2(uri, state, d.value.all_faerie_dict[country_uri]["states_dict"])
            if states_can == None or states_can == {} or states_can["entities"] == {}:
                # if input state is empty, get the state uris from all_dicts in that country
                states_can["entities"] = d.value.all_faerie_dict[country_uri]["states_dict"][4].values()

            cities_can = searchcity(states_can,uri,city, d)
    else:
        if state != "":
            states_can = faerie1.processDoc2(uri, state, d.value.state_faerie_dict)

        cities_can = searchcity(states_can, uri, city, d)

    return cities_can

def searchcity(states_can, uri, city, d):
    cities_can = {}
    if states_can and states_can != {} and states_can["entities"] != {}:
        for state_uri in states_can["entities"]:
            country_uri = d.value.state_dict[state_uri]["country_uri"]
            if country_uri != '':
                if 'entities' in cities_can:
                    cities_can_add = faerie1.processDoc2(uri, city, d.value.all_faerie_dict[country_uri][state_uri]["cities"],0.5)
                    if cities_can_add != {}:
                        cities_can["entities"] = dict(cities_can["entities"],
                                              **cities_can_add["entities"])
                else:
                    cities_can = faerie1.processDoc2(uri, city, d.value.all_faerie_dict[country_uri][state_uri]["cities"],0.5)
            else:
                print "Line 73:" + state_uri
    else:
        cities_can = faerie1.processDoc2(uri,city,d.value.city_faerie_dict,0.5)

    return cities_can

def run(d, input_rdd):

    candidates = input_rdd.map(lambda line : processDoc(json.loads(line), d))
    return candidates

<|endoftext|>


<commit_before>version_number = "v7.1.0"
<commit_msg>v7.2.0<commit_after>version_number = "v7.2.0"
<|endoftext|>


<commit_before>from __future__ import absolute_import

import sys
import os.path

sys.path.insert(0, os.path.join(os.path.dirname(__file__)))

pytest_plugins = [
    'sentry.utils.pytest'
]
<commit_msg>Bind tests to sqlite<commit_after>from __future__ import absolute_import

import sys
import os.path

sys.path.insert(0, os.path.join(os.path.dirname(__file__)))

# Run tests against sqlite for simplicity
import os
os.environ.setdefault('DB', 'sqlite')

pytest_plugins = [
    'sentry.utils.pytest'
]
<|endoftext|>


<commit_before>from collections import defaultdict

import math
import random

from algorithms.samplers import RouletteWheelSampler

from datamodels.output_models import Transaction
from generators.transaction_generator.customer_inventory import CustomerInventoryBuilder

import simulation_parameters as sim_param

class CustomerTransactionParameters(object):
    def __init__(self, pet_counts=None,
                 avg_trans_trigger_time=None,
                 avg_purch_trigger_time=None):
        self.pet_counts = pet_counts
        self.average_transaction_trigger_time=avg_trans_trigger_time
        self.average_purchase_trigger_time=avg_trans_trigger_time

class CustomerTransactionParametersGenerator(object):
    def _generate_pets(self):
        num_pets = random.randint(sim_param.MIN_PETS, sim_param.MAX_PETS)
        num_dogs = random.randint(0, num_pets)
        num_cats = num_pets - num_dogs

        pets = dict()
        pets["dog"] = num_dogs
        pets["cat"] = num_cats

        return pets

    def _generate_trigger_times(self):
        # days
        r = random.normalvariate(sim_param.TRANSACTION_TRIGGER_TIME_AVERAGE,
                                 sim_param.TRANSACTION_TRIGGER_TIME_VARIANCE)
        r = max(r, sim_param.TRANSACTION_TRIGGER_TIME_MIN)
        r = min(r, sim_param.TRANSACTION_TRIGGER_TIME_MAX)
        average_transaction_trigger_time = r

        r = random.normalvariate(sim_param.PURCHASE_TRIGGER_TIME_AVERAGE,
                                 sim_param.PURCHASE_TRIGGER_TIME_VARIANCE)
        r = max(r, sim_param.PURCHASE_TRIGGER_TIME_MIN)
        r = min(r, sim_param.PURCHASE_TRIGGER_TIME_MAX)
        average_purchase_trigger_time = r

        return average_transaction_trigger_time, \
            average_purchase_trigger_time

    def generate(self):
        pet_counts = self._generate_pets()
        trans_trigger_time, purch_trigger_time = self._generate_trigger_times()

        return CustomerTransactionParameters(pet_counts=pet_counts,
                                             avg_trans_trigger_time=trans_trigger_time,
                                             avg_purch_trigger_time=purch_trigger_time)

class TransactionPurchasesGenerator(object):
    """
    The purchases are simulated using a Hidden Markov Model.

    The states of the model are purchases or stop states.
    The purchse states are composed of the

        * transaction time
        * customer inventory
        * purchased item (not in the initial state)
        * number of purchases made in this transaction

    The initial state is the customer inventory at the start
    of the simulation, 0 purchases, and no item purchased.

    For each state, the probabilities of the state transitions
    are computed from the exhaustion time of each item category
    in the customer's inventory and the probabilities of each
    product that can be purchased according to the state of the
    Markov Models in the customer purchasing profile.

    There are no cycles in the transition graph (it's a DAG) -- the
    HMM can only keep moving to previously undiscovered states.

    Since customers can technically make an infinite number of purchases,
    the number of possible states is infinite.  Therefore, we can't
    enumerate all of the states like we normally would for a markov
    model.

    Since there are a finite number of products, however, the number of
    states reachable from the current state is simply the number of
    products plus a stop state.  This makes the problem tractable.

    The weight of a particular transition is given by:
              p(product|category) p(category)

    The probability of each category is determined from when the customer's
    inventory will be exhausted and a Poisson process.  The probability
    of purchasing an item in a given category is given by the current state
    of the category's markov model in the purchasing profile.

    We take advantage of this structure as follows:
        1. Compute the probability of each category
        2. Choose a category or stop state
        3. Choose a product to purchase by progressing the state of the
           category's markov model

   This saves us from having to enumerate all possible next states
   """

    def __init__(self, purchasing_profile, trans_params):
        self.purchasing_processes = purchasing_profile.build_processes()

        self.trans_params = trans_params

    def _category_weight(self, exhaustion_time, trans_time):
        """
        TODO: Discussion of Poisson process
        """
        trigger_time = self.trans_params.average_purchase_trigger_time
        remaining_time = max(exhaustion_time - trans_time, 0.0)
        lambd = 1.0 / trigger_time
        weight = lambd * math.exp(-lambd * remaining_time)
        return weight

    def _choose_category(self, customer_inventory, trans_time, num_purchases):
        category_weights = []

        for category, exhaustion_time in customer_inventory.get_exhaustion_times().iteritems():
            weight = self._category_weight(exhaustion_time, trans_time)
            category_weights.append((category, weight))

        if num_purchases != 0:
            category_weights.append(("stop", 0.1))

        sampler = RouletteWheelSampler(category_weights)

        return sampler.sample()

    def _choose_product(self, category):
        return self.purchasing_processes.simulate_purchase(category)

    def simulate(self, customer_inventory, trans_time):
        trans_products = []
        purchases = 0

        while True:
            category = self._choose_category(customer_inventory,
                                            trans_time,
                                            purchases)

            if category == "stop":
                break

            product = self._choose_product(category)

            customer_inventory.record_purchase(trans_time, product)

            purchases += 1

            trans_products.append(product)

        return trans_products

class TransactionTimeSampler(object):
    def __init__(self, customer_trans_params):
        self.customer_trans_params = customer_trans_params

    def _category_proposed_time(self, exhaustion_time):
        trigger_time = self.customer_trans_params.average_transaction_trigger_time
        lambd = 1.0 / trigger_time
        time_until_transaction = random.expovariate(lambd)
        transaction_time = max(exhaustion_time - time_until_transaction, 0.0)
        return transaction_time

    def _propose_transaction_time(self, customer_inventory):
        proposed_transaction_times = []
        for exhaustion_time in customer_inventory.get_exhaustion_times().values():
            transaction_time = self._category_proposed_time(exhaustion_time)
            proposed_transaction_times.append(transaction_time)

        return min(proposed_transaction_times)

    def _transaction_time_probability(self, proposed_trans_time, \
                                      last_trans_time):
        if proposed_trans_time >= last_trans_time:
            return 1.0
        else:
            return 0.0

    def sample(self, customer_inventory, last_trans_time):
        while True:
            proposed_time = self._propose_transaction_time(customer_inventory)

            prob = self._transaction_time_probability(proposed_time, \
                                                      last_trans_time)
            r = random.random()
            if r < prob:
                return proposed_time

class TransactionGenerator(object):
    def __init__(self, stores=None,
                 product_categories=None):

        self.product_categories = product_categories
        self.stores = stores

        self.trans_count = 0

        self.params_generator = CustomerTransactionParametersGenerator()
        self.inventory_builder = CustomerInventoryBuilder(product_categories=self.product_categories)

    def simulate(self, customer, purchasing_profile, end_time):
        customer_trans_params = self.params_generator.generate()

        trans_time_sampler = TransactionTimeSampler(customer_trans_params)

        purchase_sim = TransactionPurchasesGenerator(purchasing_profile,
                                                     customer_trans_params)

        customer_inventory = self.inventory_builder.build(customer_trans_params)

        last_trans_time = 0.0
        while True:
            trans_time = trans_time_sampler.sample(customer_inventory,
                                                   last_trans_time)

            if trans_time > end_time:
                break

            purchased_items = purchase_sim.simulate(customer_inventory,
                                                    trans_time)

            store = random.choice(self.stores)

            trans = Transaction(customer=customer,
                                purchased_items=purchased_items,
                                trans_time=trans_time,
                                trans_count=self.trans_count,
                                store=store)

            self.trans_count += 1
            last_trans_time = trans_time

            yield trans

<commit_msg>Seperate out transaction purchases HMM<commit_after>from collections import defaultdict

import math
import random

from algorithms.samplers import RouletteWheelSampler

from datamodels.output_models import Transaction
from generators.transaction_generator.customer_inventory import CustomerInventoryBuilder

import simulation_parameters as sim_param

class CustomerTransactionParameters(object):
    def __init__(self, pet_counts=None,
                 avg_trans_trigger_time=None,
                 avg_purch_trigger_time=None):
        self.pet_counts = pet_counts
        self.average_transaction_trigger_time=avg_trans_trigger_time
        self.average_purchase_trigger_time=avg_trans_trigger_time

class CustomerTransactionParametersGenerator(object):
    def _generate_pets(self):
        num_pets = random.randint(sim_param.MIN_PETS, sim_param.MAX_PETS)
        num_dogs = random.randint(0, num_pets)
        num_cats = num_pets - num_dogs

        pets = dict()
        pets["dog"] = num_dogs
        pets["cat"] = num_cats

        return pets

    def _generate_trigger_times(self):
        # days
        r = random.normalvariate(sim_param.TRANSACTION_TRIGGER_TIME_AVERAGE,
                                 sim_param.TRANSACTION_TRIGGER_TIME_VARIANCE)
        r = max(r, sim_param.TRANSACTION_TRIGGER_TIME_MIN)
        r = min(r, sim_param.TRANSACTION_TRIGGER_TIME_MAX)
        average_transaction_trigger_time = r

        r = random.normalvariate(sim_param.PURCHASE_TRIGGER_TIME_AVERAGE,
                                 sim_param.PURCHASE_TRIGGER_TIME_VARIANCE)
        r = max(r, sim_param.PURCHASE_TRIGGER_TIME_MIN)
        r = min(r, sim_param.PURCHASE_TRIGGER_TIME_MAX)
        average_purchase_trigger_time = r

        return average_transaction_trigger_time, \
            average_purchase_trigger_time

    def generate(self):
        pet_counts = self._generate_pets()
        trans_trigger_time, purch_trigger_time = self._generate_trigger_times()

        return CustomerTransactionParameters(pet_counts=pet_counts,
                                             avg_trans_trigger_time=trans_trigger_time,
                                             avg_purch_trigger_time=purch_trigger_time)

class TransactionPurchasesHiddenMarkovModel(object):
    """
    The purchases are simulated using a Hidden Markov Model.

    The states of the model are purchases or stop states.
    The purchse states are composed of the

        * transaction time
        * customer inventory
        * purchased item (not in the initial state)
        * number of purchases made in this transaction

    The initial state is the customer inventory at the start
    of the simulation, 0 purchases, and no item purchased.

    For each state, the probabilities of the state transitions
    are computed from the exhaustion time of each item category
    in the customer's inventory and the probabilities of each
    product that can be purchased according to the state of the
    Markov Models in the customer purchasing profile.

    There are no cycles in the transition graph (it's a DAG) -- the
    HMM can only keep moving to previously undiscovered states.

    Since customers can technically make an infinite number of purchases,
    the number of possible states is infinite.  Therefore, we can't
    enumerate all of the states like we normally would for a markov
    model.

    Since there are a finite number of products, however, the number of
    states reachable from the current state is simply the number of
    products plus a stop state.  This makes the problem tractable.

    The weight of a particular transition is given by:
              p(product|category) p(category)

    The probability of each category is determined from when the customer's
    inventory will be exhausted and a Poisson process.  The probability
    of purchasing an item in a given category is given by the current state
    of the category's markov model in the purchasing profile.

    We take advantage of this structure as follows:
        1. Compute the probability of each category
        2. Choose a category or stop state
        3. Choose a product to purchase by progressing the state of the
           category's markov model

   This saves us from having to enumerate all possible next states
   """
    def __init__(self, purchasing_processes, trans_params, customer_inventory, trans_time):
        self.purchasing_processes = purchasing_processes
        self.trans_params = trans_params
        self.num_purchases = 0
        self.trans_time = trans_time
        self.customer_inventory = customer_inventory

    def _category_weight(self, exhaustion_time):
        """
        TODO: Discussion of Poisson process
        """
        trigger_time = self.trans_params.average_purchase_trigger_time
        remaining_time = max(exhaustion_time - self.trans_time, 0.0)
        lambd = 1.0 / trigger_time
        weight = lambd * math.exp(-lambd * remaining_time)
        return weight

    def _choose_category(self):
        category_weights = []

        exhaustion_times = self.customer_inventory.get_exhaustion_times()
        for category, exhaustion_time in exhaustion_times.iteritems():
            weight = self._category_weight(exhaustion_time)
            category_weights.append((category, weight))

        if self.num_purchases != 0:
            category_weights.append(("stop", 0.1))

        sampler = RouletteWheelSampler(category_weights)

        return sampler.sample()

    def _choose_product(self, category):
        return self.purchasing_processes.simulate_purchase(category)

    def progress_state(self):
        category = self._choose_category()

        if category == "stop":
            return None

        product = self._choose_product(category)
        self.customer_inventory.record_purchase(self.trans_time, product)
        self.num_purchases += 1

        return product

class TransactionPurchasesGenerator(object):
    def __init__(self, purchasing_profile, trans_params):
        self.purchasing_processes = purchasing_profile.build_processes()
        self.trans_params = trans_params

    def simulate(self, customer_inventory, trans_time):
        hmm = TransactionPurchasesHiddenMarkovModel(self.purchasing_processes,
                                                    self.trans_params,
                                                    customer_inventory,
                                                    trans_time)

        trans_products = []

        while True:
            product = hmm.progress_state()
            if product is None:
                return trans_products
            trans_products.append(product)

class TransactionTimeSampler(object):
    def __init__(self, customer_trans_params):
        self.customer_trans_params = customer_trans_params

    def _category_proposed_time(self, exhaustion_time):
        trigger_time = self.customer_trans_params.average_transaction_trigger_time
        lambd = 1.0 / trigger_time
        time_until_transaction = random.expovariate(lambd)
        transaction_time = max(exhaustion_time - time_until_transaction, 0.0)
        return transaction_time

    def _propose_transaction_time(self, customer_inventory):
        proposed_transaction_times = []
        for exhaustion_time in customer_inventory.get_exhaustion_times().values():
            transaction_time = self._category_proposed_time(exhaustion_time)
            proposed_transaction_times.append(transaction_time)

        return min(proposed_transaction_times)

    def _transaction_time_probability(self, proposed_trans_time, \
                                      last_trans_time):
        if proposed_trans_time >= last_trans_time:
            return 1.0
        else:
            return 0.0

    def sample(self, customer_inventory, last_trans_time):
        while True:
            proposed_time = self._propose_transaction_time(customer_inventory)

            prob = self._transaction_time_probability(proposed_time, \
                                                      last_trans_time)
            r = random.random()
            if r < prob:
                return proposed_time

class TransactionGenerator(object):
    def __init__(self, stores=None,
                 product_categories=None):

        self.product_categories = product_categories
        self.stores = stores

        self.trans_count = 0

        self.params_generator = CustomerTransactionParametersGenerator()
        self.inventory_builder = CustomerInventoryBuilder(product_categories=self.product_categories)

    def simulate(self, customer, purchasing_profile, end_time):
        customer_trans_params = self.params_generator.generate()

        trans_time_sampler = TransactionTimeSampler(customer_trans_params)

        purchase_sim = TransactionPurchasesGenerator(purchasing_profile,
                                                     customer_trans_params)

        customer_inventory = self.inventory_builder.build(customer_trans_params)

        last_trans_time = 0.0
        while True:
            trans_time = trans_time_sampler.sample(customer_inventory,
                                                   last_trans_time)

            if trans_time > end_time:
                break

            purchased_items = purchase_sim.simulate(customer_inventory,
                                                    trans_time)

            store = random.choice(self.stores)

            trans = Transaction(customer=customer,
                                purchased_items=purchased_items,
                                trans_time=trans_time,
                                trans_count=self.trans_count,
                                store=store)

            self.trans_count += 1
            last_trans_time = trans_time

            yield trans

<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
# @Author: Zachary Priddy
# @Date:   2016-04-19 21:57:10
# @Last Modified by:   Zachary Priddy
# @Last Modified time: 2016-10-09 22:40:00

import pychromecast
import requests
from core import deviceDB, ffCommand
from config import ServiceConfig
import logging

config = ServiceConfig()
chromecasts = pychromecast.get_chromecasts()
cc_devices = [cc.device.friendly_name.lower() for cc in chromecasts]

class Notification(object):
  def __init__(self, deviceID, message, priority=0, cast=False):
    self._deviceID = deviceID
    self._message = message
    self._priority = priority

    if cast:
      if not config.get_boolean('SPEECH','enable'):
         return
      self.send_cast(self._deviceID)

    if self._deviceID.lower() in cc_devices:
      self.send_cast(self._deviceID)

    if self._deviceID.lower() == 'all':
      self.send_all()
    else:
      self.send()

  def send_cast(self, device):
    polly_server = config.get_item('SPEECH', 'polly_server')
    media_url = requests.post(polly_server, json={'speech': self._message}).text

    cast = next(cc for cc in chromecasts if cc.device.friendly_name.lower() == device.lower())
    cast.set_volume(.5)
    mc = cast.media_controller
    mc.play_media(media_url, 'audio/mp3')

  def send_all(self):
    for device in deviceDB.find({"config.subType":"notification"}):
      dID = device.get('id')
      ffCommand(str(dID), {'notify': {'message' :self._message}})
    if config.get_boolean('SPEECH','enable'):
      self.send_cast(config.get_item('SPEECH','default_device').lower())

  def send(self):
    ffCommand(self._deviceID, {'notify': {'message' :self._message}})
<commit_msg>try better delay/error handling<commit_after># -*- coding: utf-8 -*-
# @Author: Zachary Priddy
# @Date:   2016-04-19 21:57:10
# @Last Modified by:   Zachary Priddy
# @Last Modified time: 2016-10-09 22:40:00

import pychromecast
import requests
from core import deviceDB, ffCommand
from config import ServiceConfig
from core import ffScheduler
import logging

config = ServiceConfig()
chromecasts = pychromecast.get_chromecasts()
cc_devices = [cc.device.friendly_name.lower() for cc in chromecasts]

class Notification(object):
  def __init__(self, deviceID, message, priority=0, cast=False):
    self._deviceID = deviceID
    self._message = message
    self._priority = priority

    if cast:
      if not config.get_boolean('SPEECH','enable'):
         return
      self.send_cast(self._deviceID)

    if self._deviceID.lower() in cc_devices:
      self.send_cast(self._deviceID)

    if self._deviceID.lower() == 'all':
      self.send_all()
    else:
      self.send()

  def send_cast(self, device):
    ffScheduler.runInS(1,self.send_cast_run, args=[device])

  def send_cast_run(self, device):
    cast = None
    try:
      polly_server = config.get_item('SPEECH', 'polly_server')
      media_url = requests.post(polly_server, json={'speech': self._message}).text
      cast = next(cc for cc in chromecasts if cc.device.friendly_name.lower() == device.lower())
      cast.set_volume(.5)
    except:
      chromecasts = pychromecast.get_chromecasts()
      polly_server = config.get_item('SPEECH', 'polly_server')
      media_url = requests.post(polly_server, json={'speech': self._message}).text
      cast = next(cc for cc in chromecasts if cc.device.friendly_name.lower() == device.lower())
      cast.set_volume(.5)
    if not cast:
      return
    mc = cast.media_controller
    mc.play_media(media_url, 'audio/mp3')

  def send_all(self):
    for device in deviceDB.find({"config.subType":"notification"}):
      dID = device.get('id')
      ffCommand(str(dID), {'notify': {'message' :self._message}})
    if config.get_boolean('SPEECH','enable'):
      self.send_cast(config.get_item('SPEECH','default_device').lower())

  def send(self):
    ffCommand(self._deviceID, {'notify': {'message' :self._message}})
<|endoftext|>


<commit_before>from django.http import Http404
from channelguide import util
from channelguide.guide import templateutil
from channelguide.guide.models import Channel, Cobranding, User
from channelguide.guide.forms import CobrandingAdminForm

def admin(request, cobrand_name):
    if Cobranding.query(Cobranding.c.name==cobrand_name).count(request.connection) == 0: # doesn't exist
        if not request.user.is_admin():
            raise Http404
        c = Cobranding()
        c.name = c.html_title = c.page_title = c.url = c.description = cobrand_name
        c.save(request.connection)
    if not request.user.is_admin() or cobrand_name == request.user.username:
        return util.send_to_login_page(request)
    cobrand = Cobranding.get(request.connection, cobrand_name)
    if request.method != 'POST':
        form = CobrandingAdminForm(request.connection, cobrand)
    else:
        form = CobrandingAdminForm(request.connection, cobrand,
                request.POST.copy())
        if form.is_valid():
            form.update_cobrand()
    return util.render_to_response(request, 'cobranding-admin.html',
            {'cobrand': cobrand, 'form': form})

def cobranding(request, cobrand_name):
    try:
        cobrand = Cobranding.get(request.connection, cobrand_name)
    except:
        raise Http404
    user = User.query(User.c.username==cobrand_name).join('channels').execute(request.connection)[0]
    query = Channel.query().where(Channel.c.owner_id==user.id)
    query.where(Channel.c.state.in_((Channel.APPROVED, Channel.AUDIO)))
    pager = templateutil.Pager(6, query, request)
    return util.render_to_response(request, 'cobranding.html', {
        'cobrand': cobrand,
        'pager': pager
        })
<commit_msg>raise Http404 if we can't create the user<commit_after>from django.http import Http404
from channelguide import util
from channelguide.guide import templateutil
from channelguide.guide.models import Channel, Cobranding, User
from channelguide.guide.forms import CobrandingAdminForm

def admin(request, cobrand_name):
    if Cobranding.query(Cobranding.c.name==cobrand_name).count(request.connection) == 0: # doesn't exist
        if not request.user.is_admin():
            raise Http404
        c = Cobranding()
        c.name = c.html_title = c.page_title = c.url = c.description = cobrand_name
        try:
            c.save(request.connection)
        except:
            raise Http404
    if not request.user.is_admin() or cobrand_name == request.user.username:
        return util.send_to_login_page(request)
    cobrand = Cobranding.get(request.connection, cobrand_name)
    if request.method != 'POST':
        form = CobrandingAdminForm(request.connection, cobrand)
    else:
        form = CobrandingAdminForm(request.connection, cobrand,
                request.POST.copy())
        if form.is_valid():
            form.update_cobrand()
    return util.render_to_response(request, 'cobranding-admin.html',
            {'cobrand': cobrand, 'form': form})

def cobranding(request, cobrand_name):
    try:
        cobrand = Cobranding.get(request.connection, cobrand_name)
    except:
        raise Http404
    user = User.query(User.c.username==cobrand_name).join('channels').execute(request.connection)[0]
    query = Channel.query().where(Channel.c.owner_id==user.id)
    query.where(Channel.c.state.in_((Channel.APPROVED, Channel.AUDIO)))
    pager = templateutil.Pager(6, query, request)
    return util.render_to_response(request, 'cobranding.html', {
        'cobrand': cobrand,
        'pager': pager
        })
<|endoftext|>


<commit_before>#!/usr/bin/env python
# VTK Viewer
# Written 2012-2013 Hal Canary <http://cs.unc.edu/~hal>
# Copyright 2012-2013 University of North Carolina at Chapel Hill.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you
# may not use this file except in compliance with the License.  You
# may obtain a copy of the License at
#
#   LICENSE.md in this repository or
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.  See the License for the specific language governing
# permissions and limitations under the License.

"""
CONTROLS:

left mouse button:  rotation
right mouse button:  zooming
middle mouse button:  panning
ctrl + left mouse button:  spinning

ctrl + shift + left mouse button:  zooming
shift + left mouse button:  panning

j:  joystick
t:  trackball

c:  camera mode
a:  actor mode

3: toggle  stereo mode

e: exit the application
e: quit the application

f: fly to the picked point
r: reset the camera

p: pick

s: surface representation
w: wireframe representation

Set the STEREO_TYPE environment variable to control stereo type.
	STEREO_TYPE=CRYSTAL_EYES
	STEREO_TYPE=RED_BLUE
	STEREO_TYPE=INTERLACED
	STEREO_TYPE=LEFT
	STEREO_TYPE=RIGHT
	STEREO_TYPE=DRESDEN
	STEREO_TYPE=ANAGLYPH
	STEREO_TYPE=CHECKERBOARD
	STEREO_TYPE=SPLITVIEWPORT_HORIZONTAL
"""

useage = """
Useage: vtkviewer.py FILE [MORE FILES...]
Supported File Formats:
  *.vtk - VTK Legacy File
  *.vtp - VTK Polygonal Data File
  *.vtu - VTK Unstructured Grid Data File
  *.ply - Stanford Polygon File
  *.obj - Wavefront Object file
  *.stl - Stereolithography File
  *.pdb - Protein Data Bank File
Controls:
  's' - surface
  'w' - wireframe
  'r' - reset and center camera
  'q' - quit
  '3' - toggle stereo mode
More Info:
  https://github.com/HalCanary/vtkviewer
"""

import vtk
import sys
import os
import glob

class VTKViewer(object):
	def __init__(self):
		self.renWin = vtk.vtkRenderWindow()
		self.renWin.SetSize(800,600)
		self.renWin.SetWindowName("VTK Viewer")
		self.iren = vtk.vtkRenderWindowInteractor()
		self.iren.SetRenderWindow(self.renWin)
		self.renderer = vtk.vtkRenderer()
		self.renWin.AddRenderer(self.renderer)
		interactorStyle = self.iren.GetInteractorStyle()
		interactorStyle.SetCurrentStyleToTrackballCamera()
		if "STEREO_TYPE" in os.environ:
			vtkStereoType = VTKViewer.GetVTKStereoType(
				os.environ["STEREO_TYPE"])
			if vtkStereoType is not None:
				self.renWin.SetStereoType (vtkStereoType)
			else:
				print '?%s?' % stereoType

	def Start(self):
		self.renWin.Render()
		self.iren.Start()

	@staticmethod
	def GetDefaultColorMap(dataRange):
		colorMap = vtk.vtkColorTransferFunction()
		colorMap.SetColorSpaceToLab()
		colorMap.AddRGBPoint(dataRange[0], 0.865, 0.865, 0.865)
		colorMap.AddRGBPoint(dataRange[1], 0.706, 0.016, 0.150)
		colorMap.Build()
		return colorMap

	def AddPolyData(self, polyData, colorMap=None):
		if colorMap is None:
			colorMap = VTKViewer.GetDefaultColorMap(polyData.GetScalarRange())
		polyDataMapper = vtk.vtkPolyDataMapper()
		polyDataMapper.SetLookupTable(colorMap)

		if polyData.GetPointData().GetNormals() is None:
			polyDataNormals = vtk.vtkPolyDataNormals()
			try:
				polyDataNormals.SetInputData(polyData)
			except:
				polyDataNormals.SetInput(polyData)
			polyDataNormals.SetFeatureAngle(90.0)
			polyDataMapper.SetInputConnection(
				polyDataNormals.GetOutputPort())
		else:
			try:
				polyDataMapper.SetInputData(polyData)
			except:
				polyDataMapper.SetInput(polyData)
		actor = vtk.vtkActor()
		actor.GetProperty().SetPointSize(3)
		actor.SetMapper(polyDataMapper)
		self.renderer.AddActor(actor)

	def AddFile(self, file_name):
		file_name_lower = file_name.lower()
		if file_name_lower.endswith('.vtk'):
			polyData = VTKViewer.ReadLegacyVTK(file_name)
		elif file_name_lower.endswith(".vtp"):
			polyData = VTKViewer.readPolyData(
				file_name, vtk.vtkXMLPolyDataReader)
		elif file_name_lower.endswith(".ply"):
			polyData = VTKViewer.readPolyData(
				file_name, vtk.vtkPLYReader)
		elif file_name_lower.endswith(".obj"):
			polyData = VTKViewer.readPolyData(
				file_name, vtk.vtkOBJReader)
		elif file_name_lower.endswith(".stl"):
			polyData = VTKViewer.readPolyData(
				file_name, vtk.vtkSTLReader)
		elif file_name_lower.endswith(".vtu"):
			polyData = VTKViewer.readDataSet(
				file_name, vtk.vtkXMLUnstructuredGridReader)
		elif file_name_lower.endswith(".pdb"):
			polyData = VTKViewer.ReadPDB(file_name)
		elif file_name_lower.endswith(".vti"):
			polyData = VTKViewer.readDataSet(
				file_name, vtk.vtkXMLImageDataReader)
		elif file_name_lower.endswith(".vts"):
			polyData = VTKViewer.readDataSet(
				file_name, vtk.vtkXMLStructuredGridReader)
		elif file_name_lower.endswith(".vtr"):
			polyData = VTKViewer.readDataSet(
				file_name, vtk.vtkXMLRectilinearGridReader)
		else:
			print file_name, ": BAD FILE NAME.  Should end",
			print "in VTK, VTP, PLY, OBJ, STL, VTU, or PDB."
			raise Exception()
		self.AddPolyData(polyData)
		return

	@staticmethod
	def ReadPDB(file_name):
		pdb = vtk.vtkPDBReader()
		pdb.SetFileName(file_name)
		pdb.SetHBScale(1.0)
		pdb.SetBScale(1.0)
		pdb.Update()

		sphere = vtk.vtkSphereSource()
		sphere.SetCenter(0, 0, 0)
		sphere.SetRadius(1)

		glyph = vtk.vtkGlyph3D()
		glyph.SetInputConnection(pdb.GetOutputPort())
		glyph.SetSourceConnection(sphere.GetOutputPort())
		glyph.SetOrient(1)
		glyph.SetColorMode(1)
		glyph.SetScaleMode(2)
		glyph.SetScaleFactor(.25)
		glyph.Update()

		tube = vtk.vtkTubeFilter()
		tube.SetInputConnection(pdb.GetOutputPort())
		tube.SetNumberOfSides(6)
		tube.CappingOff()
		tube.SetRadius(0.2)
		tube.SetVaryRadius(0)
		tube.SetRadiusFactor(10)
		tube.Update()

		tubeMesh = vtk.vtkPolyData()
		tubeMesh.ShallowCopy(tube.GetOutput())
		N = tubeMesh.GetNumberOfPoints()

		rgb_colors = tubeMesh.GetPointData().GetArray("rgb_colors")
		if rgb_colors is not None:
			if rgb_colors.GetNumberOfComponents() == 3:
				for i in xrange(N):
					rgb_colors.SetTupleValue(i, (127,127,127))

		appendFilter = vtk.vtkAppendPolyData()
		appendFilter.AddInputConnection(glyph.GetOutputPort())
		try:
			appendFilter.AddInputData(tubeMesh)
		except:
			appendFilter.AddInput(tubeMesh)
		appendFilter.Update()

		polyData = vtk.vtkPolyData()
		polyData.ShallowCopy(appendFilter.GetOutput())
		return polyData

	@staticmethod
	def ConvertDataSetToSurface(algorithmOutputPort):
		dataSetSurfaceFilter = vtk.vtkDataSetSurfaceFilter()
		dataSetSurfaceFilter.SetInputConnection(algorithmOutputPort)
		dataSetSurfaceFilter.Update()
		polyData = vtk.vtkPolyData()
		polyData.ShallowCopy(dataSetSurfaceFilter.GetOutput())
		return polyData

	@staticmethod
	def readPolyData(file_name, readerType):
		reader = readerType()
		reader.SetFileName(file_name)
		reader.Update()
		polyData = vtk.vtkPolyData()
		polyData.ShallowCopy(reader.GetOutput())
		return polyData

	@staticmethod
	def readDataSet(file_name, readerType):
		reader = readerType()
		reader.SetFileName(file_name)
		reader.Update()
		return VTKViewer.ConvertDataSetToSurface(
			reader.GetOutputPort())

	@staticmethod
	def ReadLegacyVTK(file_name):
		reader = vtk.vtkDataSetReader()
		reader.SetFileName(file_name)
		reader.Update()
		if None != reader.GetPolyDataOutput():
			polyData = vtk.vtkPolyData()
			polyData.ShallowCopy(reader.GetPolyDataOutput())
			return polyData
		if None != reader.GetUnstructuredGridOutput():
			return VTKViewer.ConvertDataSetToSurface(reader.GetOutputPort())
		if None != reader.GetStructuredPointsOutput():
			return VTKViewer.ConvertDataSetToSurface(reader.GetOutputPort())
		if None != reader.GetStructuredGridOutput():
			return VTKViewer.ConvertDataSetToSurface(reader.GetOutputPort())
		if None != reader.GetRectilinearGridOutput():
			return VTKViewer.ConvertDataSetToSurface(reader.GetOutputPort())
		else:
			raise Exception("unsupported: ????????\n")

	@staticmethod
	def GetVTKStereoType(stereoType):
		if (stereoType == "CRYSTAL_EYES"):
			return (vtk.VTK_STEREO_CRYSTAL_EYES)
		elif (stereoType == "RED_BLUE"):
			return (vtk.VTK_STEREO_RED_BLUE)
		elif (stereoType == "INTERLACED"):
			return (vtk.VTK_STEREO_INTERLACED)
		elif (stereoType == "LEFT"):
			return (vtk.VTK_STEREO_LEFT)
		elif (stereoType == "RIGHT"):
			return (vtk.VTK_STEREO_RIGHT)
		elif (stereoType == "DRESDEN"):
			return (vtk.VTK_STEREO_DRESDEN)
		elif (stereoType == "ANAGLYPH"):
			return (vtk.VTK_STEREO_ANAGLYPH)
		elif (stereoType == "CHECKERBOARD"):
			return (vtk.VTK_STEREO_CHECKERBOARD)
		elif (stereoType == "SPLITVIEWPORT_HORIZONTAL"):
			return (vtk.VTK_STEREO_SPLITVIEWPORT_HORIZONTAL)
		else:
			return None

if __name__ == '__main__':
	if len(sys.argv) == 1:
		print useage
		exit(1)
	vtkviewer = VTKViewer()
	for arg in sys.argv[1:]:
		fileNames = glob.glob(arg)
		if len(fileNames) == 0:
			print "what:", arg
		else:
			for fileName in fileNames:
				if os.path.isfile(fileName):
					vtkviewer.AddFile(fileName)
				else:
					print "what:", fileName
	vtkviewer.Start()
<commit_msg>Modified VTKViewer.AddFile() to accept an optional colorMap parameter.<commit_after>#!/usr/bin/env python
# VTK Viewer
# Written 2012-2013 Hal Canary <http://cs.unc.edu/~hal>
# Copyright 2012-2013 University of North Carolina at Chapel Hill.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you
# may not use this file except in compliance with the License.  You
# may obtain a copy of the License at
#
#   LICENSE.md in this repository or
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.  See the License for the specific language governing
# permissions and limitations under the License.

"""
CONTROLS:

left mouse button:  rotation
right mouse button:  zooming
middle mouse button:  panning
ctrl + left mouse button:  spinning

ctrl + shift + left mouse button:  zooming
shift + left mouse button:  panning

j:  joystick
t:  trackball

c:  camera mode
a:  actor mode

3: toggle  stereo mode

e: exit the application
e: quit the application

f: fly to the picked point
r: reset the camera

p: pick

s: surface representation
w: wireframe representation

Set the STEREO_TYPE environment variable to control stereo type.
	STEREO_TYPE=CRYSTAL_EYES
	STEREO_TYPE=RED_BLUE
	STEREO_TYPE=INTERLACED
	STEREO_TYPE=LEFT
	STEREO_TYPE=RIGHT
	STEREO_TYPE=DRESDEN
	STEREO_TYPE=ANAGLYPH
	STEREO_TYPE=CHECKERBOARD
	STEREO_TYPE=SPLITVIEWPORT_HORIZONTAL
"""

useage = """
Useage: vtkviewer.py FILE [MORE FILES...]
Supported File Formats:
  *.vtk - VTK Legacy File
  *.vtp - VTK Polygonal Data File
  *.vtu - VTK Unstructured Grid Data File
  *.ply - Stanford Polygon File
  *.obj - Wavefront Object file
  *.stl - Stereolithography File
  *.pdb - Protein Data Bank File
Controls:
  's' - surface
  'w' - wireframe
  'r' - reset and center camera
  'q' - quit
  '3' - toggle stereo mode
More Info:
  https://github.com/HalCanary/vtkviewer
"""

import vtk
import sys
import os
import glob

class VTKViewer(object):
	def __init__(self):
		self.renWin = vtk.vtkRenderWindow()
		self.renWin.SetSize(800,600)
		self.renWin.SetWindowName("VTK Viewer")
		self.iren = vtk.vtkRenderWindowInteractor()
		self.iren.SetRenderWindow(self.renWin)
		self.renderer = vtk.vtkRenderer()
		self.renWin.AddRenderer(self.renderer)
		interactorStyle = self.iren.GetInteractorStyle()
		interactorStyle.SetCurrentStyleToTrackballCamera()
		if "STEREO_TYPE" in os.environ:
			vtkStereoType = VTKViewer.GetVTKStereoType(
				os.environ["STEREO_TYPE"])
			if vtkStereoType is not None:
				self.renWin.SetStereoType (vtkStereoType)
			else:
				print '?%s?' % stereoType

	def Start(self):
		self.renWin.Render()
		self.iren.Start()

	@staticmethod
	def GetDefaultColorMap(dataRange):
		colorMap = vtk.vtkColorTransferFunction()
		colorMap.SetColorSpaceToLab()
		colorMap.AddRGBPoint(dataRange[0], 0.865, 0.865, 0.865)
		colorMap.AddRGBPoint(dataRange[1], 0.706, 0.016, 0.150)
		colorMap.Build()
		return colorMap

	def AddPolyData(self, polyData, colorMap=None):
		if colorMap is None:
			colorMap = VTKViewer.GetDefaultColorMap(polyData.GetScalarRange())
		polyDataMapper = vtk.vtkPolyDataMapper()
		polyDataMapper.SetLookupTable(colorMap)

		if polyData.GetPointData().GetNormals() is None:
			polyDataNormals = vtk.vtkPolyDataNormals()
			try:
				polyDataNormals.SetInputData(polyData)
			except:
				polyDataNormals.SetInput(polyData)
			polyDataNormals.SetFeatureAngle(90.0)
			polyDataMapper.SetInputConnection(
				polyDataNormals.GetOutputPort())
		else:
			try:
				polyDataMapper.SetInputData(polyData)
			except:
				polyDataMapper.SetInput(polyData)
		actor = vtk.vtkActor()
		actor.GetProperty().SetPointSize(3)
		actor.SetMapper(polyDataMapper)
		self.renderer.AddActor(actor)

	def AddFile(self, file_name, colorMap=None):
		file_name_lower = file_name.lower()
		if file_name_lower.endswith('.vtk'):
			polyData = VTKViewer.ReadLegacyVTK(file_name)
		elif file_name_lower.endswith(".vtp"):
			polyData = VTKViewer.readPolyData(
				file_name, vtk.vtkXMLPolyDataReader)
		elif file_name_lower.endswith(".ply"):
			polyData = VTKViewer.readPolyData(
				file_name, vtk.vtkPLYReader)
		elif file_name_lower.endswith(".obj"):
			polyData = VTKViewer.readPolyData(
				file_name, vtk.vtkOBJReader)
		elif file_name_lower.endswith(".stl"):
			polyData = VTKViewer.readPolyData(
				file_name, vtk.vtkSTLReader)
		elif file_name_lower.endswith(".vtu"):
			polyData = VTKViewer.readDataSet(
				file_name, vtk.vtkXMLUnstructuredGridReader)
		elif file_name_lower.endswith(".pdb"):
			polyData = VTKViewer.ReadPDB(file_name)
		elif file_name_lower.endswith(".vti"):
			polyData = VTKViewer.readDataSet(
				file_name, vtk.vtkXMLImageDataReader)
		elif file_name_lower.endswith(".vts"):
			polyData = VTKViewer.readDataSet(
				file_name, vtk.vtkXMLStructuredGridReader)
		elif file_name_lower.endswith(".vtr"):
			polyData = VTKViewer.readDataSet(
				file_name, vtk.vtkXMLRectilinearGridReader)
		else:
			print file_name, ": BAD FILE NAME.  Should end",
			print "in VTK, VTP, PLY, OBJ, STL, VTU, or PDB."
			raise Exception()
		self.AddPolyData(polyData, colorMap)
		return

	@staticmethod
	def ReadPDB(file_name):
		pdb = vtk.vtkPDBReader()
		pdb.SetFileName(file_name)
		pdb.SetHBScale(1.0)
		pdb.SetBScale(1.0)
		pdb.Update()

		sphere = vtk.vtkSphereSource()
		sphere.SetCenter(0, 0, 0)
		sphere.SetRadius(1)

		glyph = vtk.vtkGlyph3D()
		glyph.SetInputConnection(pdb.GetOutputPort())
		glyph.SetSourceConnection(sphere.GetOutputPort())
		glyph.SetOrient(1)
		glyph.SetColorMode(1)
		glyph.SetScaleMode(2)
		glyph.SetScaleFactor(.25)
		glyph.Update()

		tube = vtk.vtkTubeFilter()
		tube.SetInputConnection(pdb.GetOutputPort())
		tube.SetNumberOfSides(6)
		tube.CappingOff()
		tube.SetRadius(0.2)
		tube.SetVaryRadius(0)
		tube.SetRadiusFactor(10)
		tube.Update()

		tubeMesh = vtk.vtkPolyData()
		tubeMesh.ShallowCopy(tube.GetOutput())
		N = tubeMesh.GetNumberOfPoints()

		rgb_colors = tubeMesh.GetPointData().GetArray("rgb_colors")
		if rgb_colors is not None:
			if rgb_colors.GetNumberOfComponents() == 3:
				for i in xrange(N):
					rgb_colors.SetTupleValue(i, (127,127,127))

		appendFilter = vtk.vtkAppendPolyData()
		appendFilter.AddInputConnection(glyph.GetOutputPort())
		try:
			appendFilter.AddInputData(tubeMesh)
		except:
			appendFilter.AddInput(tubeMesh)
		appendFilter.Update()

		polyData = vtk.vtkPolyData()
		polyData.ShallowCopy(appendFilter.GetOutput())
		return polyData

	@staticmethod
	def ConvertDataSetToSurface(algorithmOutputPort):
		dataSetSurfaceFilter = vtk.vtkDataSetSurfaceFilter()
		dataSetSurfaceFilter.SetInputConnection(algorithmOutputPort)
		dataSetSurfaceFilter.Update()
		polyData = vtk.vtkPolyData()
		polyData.ShallowCopy(dataSetSurfaceFilter.GetOutput())
		return polyData

	@staticmethod
	def readPolyData(file_name, readerType):
		reader = readerType()
		reader.SetFileName(file_name)
		reader.Update()
		polyData = vtk.vtkPolyData()
		polyData.ShallowCopy(reader.GetOutput())
		return polyData

	@staticmethod
	def readDataSet(file_name, readerType):
		reader = readerType()
		reader.SetFileName(file_name)
		reader.Update()
		return VTKViewer.ConvertDataSetToSurface(
			reader.GetOutputPort())

	@staticmethod
	def ReadLegacyVTK(file_name):
		reader = vtk.vtkDataSetReader()
		reader.SetFileName(file_name)
		reader.Update()
		if None != reader.GetPolyDataOutput():
			polyData = vtk.vtkPolyData()
			polyData.ShallowCopy(reader.GetPolyDataOutput())
			return polyData
		if None != reader.GetUnstructuredGridOutput():
			return VTKViewer.ConvertDataSetToSurface(reader.GetOutputPort())
		if None != reader.GetStructuredPointsOutput():
			return VTKViewer.ConvertDataSetToSurface(reader.GetOutputPort())
		if None != reader.GetStructuredGridOutput():
			return VTKViewer.ConvertDataSetToSurface(reader.GetOutputPort())
		if None != reader.GetRectilinearGridOutput():
			return VTKViewer.ConvertDataSetToSurface(reader.GetOutputPort())
		else:
			raise Exception("unsupported: ????????\n")

	@staticmethod
	def GetVTKStereoType(stereoType):
		if (stereoType == "CRYSTAL_EYES"):
			return (vtk.VTK_STEREO_CRYSTAL_EYES)
		elif (stereoType == "RED_BLUE"):
			return (vtk.VTK_STEREO_RED_BLUE)
		elif (stereoType == "INTERLACED"):
			return (vtk.VTK_STEREO_INTERLACED)
		elif (stereoType == "LEFT"):
			return (vtk.VTK_STEREO_LEFT)
		elif (stereoType == "RIGHT"):
			return (vtk.VTK_STEREO_RIGHT)
		elif (stereoType == "DRESDEN"):
			return (vtk.VTK_STEREO_DRESDEN)
		elif (stereoType == "ANAGLYPH"):
			return (vtk.VTK_STEREO_ANAGLYPH)
		elif (stereoType == "CHECKERBOARD"):
			return (vtk.VTK_STEREO_CHECKERBOARD)
		elif (stereoType == "SPLITVIEWPORT_HORIZONTAL"):
			return (vtk.VTK_STEREO_SPLITVIEWPORT_HORIZONTAL)
		else:
			return None

if __name__ == '__main__':
	if len(sys.argv) == 1:
		print useage
		exit(1)
	vtkviewer = VTKViewer()
	for arg in sys.argv[1:]:
		fileNames = glob.glob(arg)
		if len(fileNames) == 0:
			print "what:", arg
		else:
			for fileName in fileNames:
				if os.path.isfile(fileName):
					vtkviewer.AddFile(fileName)
				else:
					print "what:", fileName
	vtkviewer.Start()
<|endoftext|>


<commit_before><commit_msg>TST: setup_package function for testing with nosetests<commit_after>
def setup_package():
    """Setup function for testing with nosetests.
    """

    # remove noise coming from numpy regarding log(0) and log(-inf),
    # those cases are already taken care of in the tests
    import numpy
    numpy.seterr(divide='ignore', invalid='ignore')
<|endoftext|>


<commit_before>import pytest
import json
import os
import logging
from pps.utils.configuration import ConfBorg

# py.test tweaking
# in this exemple: initialize configuration borg for the whole test session

def pytest_report_header(config):
    """ return a string in test report header """
    return "Hey this are the tests"

def pytest_addoption(parser):
    """create a confpath command line arg for py.test"""
    parser.addoption("--confpath", action="append", default=[], help="configuration full path")

@pytest.fixture(scope="function")
def carriagereturn(request):
    """ will be run for each test function see pytest.ini """
    print(" ")

@pytest.fixture(scope="session")
def initloggers(request):
    """ will be run for each test session see pytest.ini """
    # initialize basic loggers
    print "****** INIT LOGGERS ******"
    logging.basicConfig(level=logging.DEBUG)

@pytest.fixture(scope="session")
def confborg(request):
    """
    - allow confborg to be a valid test parameter,
    this code will be executed before the test code
    once per session
    """

    def run_only_at_session_end():
        print "\n ********** End of test session **********"
    request.addfinalizer(run_only_at_session_end)

    print "********** Test session init **********"

    if pytest.config.getoption('confpath') is not None:
        print "Launch test with conf:", pytest.config.getoption('confpath', default=None)
        conf_path = pytest.config.getoption('confpath')
        # previously returns a list, get the string
        if len(pytest.config.getoption('confpath')) > 0:
            conf_path = pytest.config.getoption('confpath')[0]

    if os.path.exists(conf_path):
        with open(conf_path, 'rt') as f:
            conf = json.load(f)

        # add data path which is calculated at run time
        conf['data_path'] = os.path.dirname(os.path.dirname(conf_path))
        conf['data_path'] = os.path.join(conf['data_path'], 'data')
        res = ConfBorg(conf)
        print type(res)
    return res
<commit_msg>pytest crash on python 3.x<commit_after>import pytest
import json
import os
import logging
from pps.utils.configuration import ConfBorg

# py.test tweaking
# in this exemple: initialize configuration borg for the whole test session

def pytest_report_header(config):
    """ return a string in test report header """
    return "Hey this are the tests"

def pytest_addoption(parser):
    """create a confpath command line arg for py.test"""
    parser.addoption("--confpath", action="append", default=[], help="configuration full path")

@pytest.fixture(scope="function")
def carriagereturn(request):
    """ will be run for each test function see pytest.ini """
    print(" ")

@pytest.fixture(scope="session")
def initloggers(request):
    """ will be run for each test session see pytest.ini """
    # initialize basic loggers
    print("****** INIT LOGGERS ******")
    logging.basicConfig(level=logging.DEBUG)

@pytest.fixture(scope="session")
def confborg(request):
    """
    - allow confborg to be a valid test parameter,
    this code will be executed before the test code
    once per session
    """

    def run_only_at_session_end():
        print("\n ********** End of test session **********")
    request.addfinalizer(run_only_at_session_end)

    print("********** Test session init **********")

    if pytest.config.getoption('confpath') is not None:
        print("Launch test with conf:", pytest.config.getoption('confpath', default=None))
        conf_path = pytest.config.getoption('confpath')
        # previously returns a list, get the string
        if len(pytest.config.getoption('confpath')) > 0:
            conf_path = pytest.config.getoption('confpath')[0]

    if os.path.exists(conf_path):
        with open(conf_path, 'rt') as f:
            conf = json.load(f)

        # add data path which is calculated at run time
        conf['data_path'] = os.path.dirname(os.path.dirname(conf_path))
        conf['data_path'] = os.path.join(conf['data_path'], 'data')
        res = ConfBorg(conf)
        print type(res)
    return res
<|endoftext|>


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

import logging
from openerp import api, fields
from openerp import exceptions
from openerp.tools.translate import _
from openerp.addons.base_geoengine import geo_model
from openerp.addons.base_geoengine import fields as geo_fields

try:
    import requests
except ImportError:
    logger = logging.getLogger(__name__)
    logger.warning('Shapely or geojson are not available in the sys path')

_logger = logging.getLogger(__name__)

class ResPartner(geo_model.GeoModel):
    """Add geo_point to partner using a function field"""
    _inherit = "res.partner"

    @api.one
    def geocode_address(self):
        """Get the latitude and longitude by requesting "mapquestapi"
        see http://open.mapquestapi.com/geocoding/
        """
        url = 'http://nominatim.openstreetmap.org/search'
        pay_load = {
            'limit': 1,
            'format': 'json',
            'street': self.street or '',
            'postalCode': self.zip or '',
            'city': self.city or '',
            'state':  self.state_id and self.state_id.name or '',
            'country': self.country_id and self.country_id.name or ''}

        r = requests.get(url, params=pay_load)
        try:
            r.raise_for_status()
        except Exception as e:
            _logger.exception('Geocoding error')
            raise exceptions.Warning(_(
                'Geocoding error. \n %s') % e.message)
        vals = r.json()
        vals = vals and vals[0] or {}
        self.write({
            'partner_latitude': vals.get('lat'),
            'partner_longitude': vals.get('lon'),
            'date_localization': fields.Date.today()})

    @api.one
    def geo_localize(self):
        self.geocode_address()
        return True

    @api.one
    @api.depends('partner_latitude', 'partner_longitude')
    def _get_geo_point(self):
        if not self.partner_latitude or not self.partner_longitude:
            self.geo_point = False
        else:
            self.geo_point = geo_fields.GeoPoint.from_latlon(
                self.env.cr, self.partner_latitude, self.partner_longitude)

    geo_point = geo_fields.GeoPoint(
        readonly=True, store=True, compute='_get_geo_point')
<commit_msg>[IMP] Use the countryCode when querying OSM<commit_after># -*- coding: utf-8 -*-
##############################################################################
#
#   Author: Laurent Mignon
#   Copyright (c) 2015 Acsone SA/NV (http://www.acsone.eu)
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import logging
from openerp import api, fields
from openerp import exceptions
from openerp.tools.translate import _
from openerp.addons.base_geoengine import geo_model
from openerp.addons.base_geoengine import fields as geo_fields

try:
    import requests
except ImportError:
    logger = logging.getLogger(__name__)
    logger.warning('Shapely or geojson are not available in the sys path')

_logger = logging.getLogger(__name__)

class ResPartner(geo_model.GeoModel):
    """Add geo_point to partner using a function field"""
    _inherit = "res.partner"

    @api.one
    def geocode_address(self):
        """Get the latitude and longitude by requesting "mapquestapi"
        see http://open.mapquestapi.com/geocoding/
        """
        url = 'http://nominatim.openstreetmap.org/search'
        pay_load = {
            'limit': 1,
            'format': 'json',
            'street': self.street or '',
            'postalCode': self.zip or '',
            'city': self.city or '',
            'state':  self.state_id and self.state_id.name or '',
            'countryCodes': self.country_id and self.country_id.code or ''}

        r = requests.get(url, params=pay_load)
        try:
            r.raise_for_status()
        except Exception as e:
            _logger.exception('Geocoding error')
            raise exceptions.Warning(_(
                'Geocoding error. \n %s') % e.message)
        vals = r.json()
        vals = vals and vals[0] or {}
        self.write({
            'partner_latitude': vals.get('lat'),
            'partner_longitude': vals.get('lon'),
            'date_localization': fields.Date.today()})

    @api.one
    def geo_localize(self):
        self.geocode_address()
        return True

    @api.one
    @api.depends('partner_latitude', 'partner_longitude')
    def _get_geo_point(self):
        if not self.partner_latitude or not self.partner_longitude:
            self.geo_point = False
        else:
            self.geo_point = geo_fields.GeoPoint.from_latlon(
                self.env.cr, self.partner_latitude, self.partner_longitude)

    geo_point = geo_fields.GeoPoint(
        readonly=True, store=True, compute='_get_geo_point')
<|endoftext|>


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

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

import sys
import copy
import logging

from google.protobuf.internal import decoder
from google.protobuf.internal import encoder
import google.protobuf.text_format as text_format
from modules.prediction.proto import feature_pb2
from modules.prediction.proto import offline_features_pb2

def readVarint32(stream):
    """
    read block size from file stream
    """
    mask = 0x80  #(1 << 7)
    raw_varint32 = []
    while 1:
        b = stream.read(1)
        if b == "":
            break
        raw_varint32.append(b)
        if not (ord(b) & mask):
            break
    return raw_varint32

def load_protobuf(filename):
    """
    read a file in protobuf binary
    """
    features = []
    offline_features = offline_features_pb2.Features()
    with open(filename, 'rb') as file_in:
        offline_features.ParseFromString(file_in.read())
    for i in range(len(offline_features.feature)):
        features.append(offline_features.feature[i])

    return features

def load_label_feature(filename):
    features = []
    with open(filename, 'rb') as f:
        size = readVarint32(f)
        while size:
            read_bytes, _ = decoder._DecodeVarint32(size, 0)
            data = f.read(read_bytes)
            if len(data) < read_bytes:
                print "Fail to load protobuf"
                break
            fea = feature_pb2.Feature()
            fea.ParseFromString(data)
            features.append(fea)
            size = readVarint32(f)
    return features

def save_protobuf(filename, feature_trajectories):
    """
    save a features in the given filename
    """
    with open(filename, 'wb') as f:
        for features in feature_trajectories:
            for fea in features:
                serializedMessage = fea.SerializeToString()
                delimiter = encoder._VarintBytes(len(serializedMessage))
                f.write(delimiter + serializedMessage)

    f.close()

def build_trajectory(features):
    """
    classify features by id and build trajectories of feature
    """
    fea_dict = dict()
    for fea in features:
        if fea.id in fea_dict.keys():
            fea_dict[fea.id].append(fea)
        else:
            fea_dict[fea.id] = [fea]

    for k in fea_dict.keys():
        if len(fea_dict[k]) < 2:
            del fea_dict[k]
            continue
        fea_dict[k].sort(key=lambda x: x.timestamp)
    feature_trajectories = fea_dict

    return feature_trajectories
<commit_msg>tools: optimize feature-io python code<commit_after>#!/usr/bin/env python

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

import sys
import copy
import logging

from google.protobuf.internal import decoder
from google.protobuf.internal import encoder
import google.protobuf.text_format as text_format
from modules.prediction.proto import feature_pb2
from modules.prediction.proto import offline_features_pb2

def readVarint32(stream):
    """
    read block size from file stream
    """
    mask = 0x80  #(1 << 7)
    raw_varint32 = []
    while 1:
        b = stream.read(1)
        if b == "":
            break
        raw_varint32.append(b)
        if not (ord(b) & mask):
            break
    return raw_varint32

def load_protobuf(filename):
    """
    read a file in protobuf binary
    """
    offline_features = offline_features_pb2.Features()
    with open(filename, 'rb') as file_in:
        offline_features.ParseFromString(file_in.read())
    return offline_features.feature

def load_label_feature(filename):
    features = []
    with open(filename, 'rb') as f:
        size = readVarint32(f)
        while size:
            read_bytes, _ = decoder._DecodeVarint32(size, 0)
            data = f.read(read_bytes)
            if len(data) < read_bytes:
                print "Fail to load protobuf"
                break
            fea = feature_pb2.Feature()
            fea.ParseFromString(data)
            features.append(fea)
            size = readVarint32(f)
    return features

def save_protobuf(filename, feature_trajectories):
    """
    save a features in the given filename
    """
    with open(filename, 'wb') as f:
        for features in feature_trajectories:
            for fea in features:
                serializedMessage = fea.SerializeToString()
                delimiter = encoder._VarintBytes(len(serializedMessage))
                f.write(delimiter + serializedMessage)

    f.close()

def build_trajectory(features):
    """
    classify features by id and build trajectories of feature
    """
    fea_dict = dict()
    for fea in features:
        if fea.id in fea_dict.keys():
            fea_dict[fea.id].append(fea)
        else:
            fea_dict[fea.id] = [fea]

    for k in fea_dict.keys():
        if len(fea_dict[k]) < 2:
            del fea_dict[k]
            continue
        fea_dict[k].sort(key=lambda x: x.timestamp)

    return fea_dict
<|endoftext|>