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

import enum

from sqlalchemy import (
    Column,
    String,
    Integer,
    BigInteger,
    Enum,
    ForeignKey,
)
from sqlalchemy.orm import relationship
from sqlalchemy.schema import Index, UniqueConstraint
from sqlalchemy.ext.declarative import declarative_base

class Base:
    def dict(self):
        '''
        Returns a dict of the object
        Primarily for json serialization
        '''
        return {c.key: getattr(self, c.key) for c in self.__mapper__.column_attrs}

Base = declarative_base(cls=Base)

class Config (Base):
    '''
    Stores the configuration values for the application in key value pairs
    '''
    __tablename__ = 'configuration'

    name = Column(
        String(64),
        primary_key=True,
        doc="The setting's name")
    value = Column(
        'setting', String,
        doc="The setting's value")

class Character (Base):
    '''
    Character data
    '''
    __tablename__ = 'characters'

    id = Column(
        Integer,
        primary_key=True,
        doc='An autonumber id')
    name = Column(
        String(64),
        nullable=False,
        doc='The name of the character')
    server = Column(
        String,
        nullable=False,
        doc='The server the character is on')
    user = Column(
        String,
        doc='The id of the user of the character')

    __table_args__ = (
        UniqueConstraint(name, server),
        Index('_character_index', server, user, unique=True),
    )

    resources = relationship(
        'Resource',
        order_by='Resource.name',
        back_populates='character')
    rolls = relationship(
        'Roll',
        order_by='Roll.name',
        back_populates='character')
    variables = relationship(
        'Variable',
        order_by='Variable.name',
        back_populates='character')
    initiatives = relationship(
        'Initiative',
        order_by='Initiative.channel',
        back_populates='character')
    inventory = relationship(
        'Item',
        order_by='Item.name',
        back_populates='character')
    spells = relationship(
        'Spell',
        order_by='Spell.level,Spell.name',
        back_populates='character')

    attributes = [
        'resources',
        'rolls',
        'variables',
        'initiatives',
        'inventory',
        'spells',
    ]

    def __str__(self):
        return str(self.name)

class Rest (enum.Enum):
    r"""
    The types of rest that can be taken
    """
    short = 1
    long = 2
    other = 3

class Resource (Base):
    '''
    Character resources, limited use abilities/items
    '''
    __tablename__ = 'resources'

    id = Column(
        Integer,
        primary_key=True,
        doc='An autonumber id')
    character_id = Column(
        Integer,
        ForeignKey('characters.id'),
        nullable=False,
        doc='Character foreign key')
    name = Column(
        String(64),
        nullable=False,
        doc='Resource name')
    max = Column(
        Integer,
        doc='The maximum number of uses of the resource')
    current = Column(
        Integer,
        doc='The current remaining uses of the resource')
    recover = Column(
        Enum(Rest),
        doc='How the character recovers the resource')

    __table_args__ = (
        Index('_resource_index', character_id, name, unique=True),
    )

    character = relationship(
        'Character',
        foreign_keys=[character_id],
        back_populates='resources')

    def __str__(self):
        ret = '{0.name}: {0.current}/{0.max}'.format(self)
        if self.recover != Rest.other:
            ret += ' per {} rest'.format(self.recover.name)
        return ret

class Roll (Base):
    '''
    Character rolls to store
    '''
    __tablename__ = 'rolls'

    id = Column(
        Integer,
        primary_key=True,
        doc='An autonumber id')
    character_id = Column(
        Integer,
        ForeignKey('characters.id'),
        nullable=False,
        doc='Character foreign key')
    name = Column(
        String(64),
        nullable=False,
        doc='Roll name')
    expression = Column(
        String,
        doc='The dice expression to roll')

    __table_args__ = (
        Index('_roll_index', character_id, name, unique=True),
    )

    character = relationship(
        'Character',
        foreign_keys=[character_id],
        back_populates='rolls')

    def __str__(self):
        return '{0.name}: `{0.expression}`'.format(self)

class Variable (Base):
    '''
    Character values to store
    '''
    __tablename__ = 'variables'

    id = Column(
        Integer,
        primary_key=True,
        doc='An autonumber id')
    character_id = Column(
        Integer,
        ForeignKey('characters.id'),
        nullable=False,
        doc='Character foreign key')
    name = Column(
        String(64),
        nullable=False,
        doc='Variable name')
    value = Column(
        Integer,
        doc='The value of the variable')

    __table_args__ = (
        Index('_variable_index', character_id, name, unique=True),
    )

    character = relationship(
        'Character',
        foreign_keys=[character_id],
        back_populates='variables')

    def __str__(self):
        return '{0.name}: {0.value}'.format(self)

class Initiative (Base):
    '''
    Manages character initiative by channel
    '''
    __tablename__ = 'initiative'

    character_id = Column(
        Integer,
        ForeignKey('characters.id'),
        primary_key=True,
        doc='Character foreign key')
    channel = Column(
        BigInteger,
        primary_key=True,
        doc='Channel id')
    value = Column(
        Integer,
        doc='The initiative roll')

    character = relationship(
        'Character',
        foreign_keys=[character_id],
        back_populates='initiatives')

    def __str__(self):
        return '{0.character.name}: {0.value}'.format(self)

class Item (Base):
    '''
    Character inventory
    '''
    __tablename__ = 'items'

    id = Column(
        Integer,
        primary_key=True,
        doc='An autonumber id')
    character_id = Column(
        Integer,
        ForeignKey('characters.id'),
        nullable=False,
        doc='Character foreign key')
    name = Column(
        String(64),
        nullable=False,
        doc='Item name')
    number = Column(
        Integer,
        doc='The quantity of the item possessed')
    description = Column(
        String,
        doc='A short description of the item')

    __table_args__ = (
        Index('_inventory_index', character_id, name, unique=True),
    )

    character = relationship(
        'Character',
        foreign_keys=[character_id],
        back_populates='inventory')

    def __str__(self):
        ret = '{0.name}: {0.number}'.format(self)
        if self.description:
            ret += '\n' + self.description
        return ret

class Spell (Base):
    '''
    Character spell list
    '''
    __tablename__ = 'spells'

    id = Column(
        Integer,
        primary_key=True,
        doc='An autonumber id')
    character_id = Column(
        Integer,
        ForeignKey('characters.id'),
        nullable=False,
        doc='Character foreign key')
    name = Column(
        String(64),
        nullable=False,
        doc='Spell name')
    level = Column(
        Integer,
        nullable=False,
        doc='Spell level')
    description = Column(
        String,
        doc='A short description of the spell')

    __table_args__ = (
        Index('_spell_index', character_id, name, unique=True),
    )

    character = relationship(
        'Character',
        foreign_keys=[character_id],
        back_populates='spells')

    def __str__(self):
        ret = '{0.name} | level {0.level}'.format(self)
        if self.description:
            ret += '\n' + self.description
        return ret

if __name__ == '__main__':
    from operator import attrgetter

    for table in sorted(Base.metadata.tables.values(), key=attrgetter('name')):
        print(table.name)
        for column in table.columns:
            col = '{}: {}'.format(column.name, column.type)

            if column.primary_key and column.foreign_keys:
                col += ' PK & FK'
            elif column.primary_key:
                col += ' PK'
            elif column.foreign_keys:
                col += ' FK'

            if not column.nullable:
                col += ' NOT NULL'

            doc = column.doc
            if isinstance(column.type, Enum):
                doc += ': ' + ', '.join(
                    column.type.python_type.__members__.keys())
            print('\t{}\n\t\t{}'.format(col, doc))
        print()
<commit_msg>Added information blocks to model<commit_after>#!/usr/bin/env python3

import enum

from sqlalchemy import (
    Column,
    String,
    Integer,
    BigInteger,
    Enum,
    ForeignKey,
)
from sqlalchemy.orm import relationship
from sqlalchemy.schema import Index, UniqueConstraint
from sqlalchemy.ext.declarative import declarative_base

class Base:
    def dict(self):
        '''
        Returns a dict of the object
        Primarily for json serialization
        '''
        return {c.key: getattr(self, c.key) for c in self.__mapper__.column_attrs}

Base = declarative_base(cls=Base)

class Config (Base):
    '''
    Stores the configuration values for the application in key value pairs
    '''
    __tablename__ = 'configuration'

    name = Column(
        String(64),
        primary_key=True,
        doc="The setting's name")
    value = Column(
        'setting', String,
        doc="The setting's value")

class Character (Base):
    '''
    Character data
    '''
    __tablename__ = 'characters'

    id = Column(
        Integer,
        primary_key=True,
        doc='An autonumber id')
    name = Column(
        String(64),
        nullable=False,
        doc='The name of the character')
    server = Column(
        String,
        nullable=False,
        doc='The server the character is on')
    user = Column(
        String,
        doc='The id of the user of the character')

    __table_args__ = (
        UniqueConstraint(name, server),
        Index('_character_index', server, user, unique=True),
    )

    resources = relationship(
        'Resource',
        order_by='Resource.name',
        back_populates='character')
    rolls = relationship(
        'Roll',
        order_by='Roll.name',
        back_populates='character')
    variables = relationship(
        'Variable',
        order_by='Variable.name',
        back_populates='character')
    initiatives = relationship(
        'Initiative',
        order_by='Initiative.channel',
        back_populates='character')
    inventory = relationship(
        'Item',
        order_by='Item.name',
        back_populates='character')
    spells = relationship(
        'Spell',
        order_by='Spell.level,Spell.name',
        back_populates='character')
    information = relationship(
        'Information',
        order_by='Information.name',
        back_populates='character')

    attributes = [
        'resources',
        'rolls',
        'variables',
        'initiatives',
        'inventory',
        'spells',
        'information',
    ]

    def __str__(self):
        return str(self.name)

class Rest (enum.Enum):
    r"""
    The types of rest that can be taken
    """
    short = 1
    long = 2
    other = 3

class Resource (Base):
    '''
    Character resources, limited use abilities/items
    '''
    __tablename__ = 'resources'

    id = Column(
        Integer,
        primary_key=True,
        doc='An autonumber id')
    character_id = Column(
        Integer,
        ForeignKey('characters.id'),
        nullable=False,
        doc='Character foreign key')
    name = Column(
        String(64),
        nullable=False,
        doc='Resource name')
    max = Column(
        Integer,
        doc='The maximum number of uses of the resource')
    current = Column(
        Integer,
        doc='The current remaining uses of the resource')
    recover = Column(
        Enum(Rest),
        doc='How the character recovers the resource')

    __table_args__ = (
        Index('_resource_index', character_id, name, unique=True),
    )

    character = relationship(
        'Character',
        foreign_keys=[character_id],
        back_populates='resources')

    def __str__(self):
        ret = '{0.name}: {0.current}/{0.max}'.format(self)
        if self.recover != Rest.other:
            ret += ' per {} rest'.format(self.recover.name)
        return ret

class Roll (Base):
    '''
    Character rolls to store
    '''
    __tablename__ = 'rolls'

    id = Column(
        Integer,
        primary_key=True,
        doc='An autonumber id')
    character_id = Column(
        Integer,
        ForeignKey('characters.id'),
        nullable=False,
        doc='Character foreign key')
    name = Column(
        String(64),
        nullable=False,
        doc='Roll name')
    expression = Column(
        String,
        doc='The dice expression to roll')

    __table_args__ = (
        Index('_roll_index', character_id, name, unique=True),
    )

    character = relationship(
        'Character',
        foreign_keys=[character_id],
        back_populates='rolls')

    def __str__(self):
        return '{0.name}: `{0.expression}`'.format(self)

class Variable (Base):
    '''
    Character values to store
    '''
    __tablename__ = 'variables'

    id = Column(
        Integer,
        primary_key=True,
        doc='An autonumber id')
    character_id = Column(
        Integer,
        ForeignKey('characters.id'),
        nullable=False,
        doc='Character foreign key')
    name = Column(
        String(64),
        nullable=False,
        doc='Variable name')
    value = Column(
        Integer,
        doc='The value of the variable')

    __table_args__ = (
        Index('_variable_index', character_id, name, unique=True),
    )

    character = relationship(
        'Character',
        foreign_keys=[character_id],
        back_populates='variables')

    def __str__(self):
        return '{0.name}: {0.value}'.format(self)

class Initiative (Base):
    '''
    Manages character initiative by channel
    '''
    __tablename__ = 'initiative'

    character_id = Column(
        Integer,
        ForeignKey('characters.id'),
        primary_key=True,
        doc='Character foreign key')
    channel = Column(
        BigInteger,
        primary_key=True,
        doc='Channel id')
    value = Column(
        Integer,
        doc='The initiative roll')

    character = relationship(
        'Character',
        foreign_keys=[character_id],
        back_populates='initiatives')

    def __str__(self):
        return '{0.character.name}: {0.value}'.format(self)

class Item (Base):
    '''
    Character inventory
    '''
    __tablename__ = 'items'

    id = Column(
        Integer,
        primary_key=True,
        doc='An autonumber id')
    character_id = Column(
        Integer,
        ForeignKey('characters.id'),
        nullable=False,
        doc='Character foreign key')
    name = Column(
        String(64),
        nullable=False,
        doc='Item name')
    number = Column(
        Integer,
        doc='The quantity of the item possessed')
    description = Column(
        String,
        doc='A short description of the item')

    __table_args__ = (
        Index('_inventory_index', character_id, name, unique=True),
    )

    character = relationship(
        'Character',
        foreign_keys=[character_id],
        back_populates='inventory')

    def __str__(self):
        ret = '{0.name}: {0.number}'.format(self)
        if self.description:
            ret += '\n' + self.description
        return ret

class Spell (Base):
    '''
    Character spell list
    '''
    __tablename__ = 'spells'

    id = Column(
        Integer,
        primary_key=True,
        doc='An autonumber id')
    character_id = Column(
        Integer,
        ForeignKey('characters.id'),
        nullable=False,
        doc='Character foreign key')
    name = Column(
        String(64),
        nullable=False,
        doc='Spell name')
    level = Column(
        Integer,
        nullable=False,
        doc='Spell level')
    description = Column(
        String,
        doc='A short description of the spell')

    __table_args__ = (
        Index('_spell_index', character_id, name, unique=True),
    )

    character = relationship(
        'Character',
        foreign_keys=[character_id],
        back_populates='spells')

class Information (Base):
    '''
    Character information
    '''
    __tablename__ = 'information'

    id = Column(
        Integer,
        primary_key=True,
        doc='An autonumber id')
    character_id = Column(
        Integer,
        ForeignKey('characters.id'),
        nullable=False,
        doc='Character foreign key')
    name = Column(
        String(64),
        nullable=False,
        doc='Info block name')
    description = Column(
        String,
        nullable=False, default='',
        doc='The actual info block')

    __table_args__ = (
        Index('_information_index', character_id, name, unique=True),
    )

    character = relationship(
        'Character',
        foreign_keys=[character_id],
        back_populates='information')

    def __str__(self):
        ret = '{0.name}'.format(self)
        if self.description:
            ret += '\n' + self.description
        return ret

if __name__ == '__main__':
    from operator import attrgetter

    for table in sorted(Base.metadata.tables.values(), key=attrgetter('name')):
        print(table.name)
        for column in table.columns:
            col = '{}: {}'.format(column.name, column.type)

            if column.primary_key and column.foreign_keys:
                col += ' PK & FK'
            elif column.primary_key:
                col += ' PK'
            elif column.foreign_keys:
                col += ' FK'

            if not column.nullable:
                col += ' NOT NULL'

            doc = column.doc
            if isinstance(column.type, Enum):
                doc += ': ' + ', '.join(
                    column.type.python_type.__members__.keys())
            print('\t{}\n\t\t{}'.format(col, doc))
        print()
<|endoftext|>


<commit_before># Setup file for package builtin

from setuptools import setup

setup(name="builtin",
      version="0.0.1",
      install_requires=["datawire-quark-core==0.4.2"],
      packages=['builtin', 'builtin.reflect', 'builtin.behaviors', 'builtin.concurrent', 'builtin_md'])
<commit_msg>fix up expected results after merge<commit_after># Setup file for package builtin

from setuptools import setup

setup(name="builtin",
      version="0.0.1",
      install_requires=["datawire-quark-core==0.4.2"],
      py_modules=[],
      packages=['builtin', 'builtin.reflect', 'builtin.behaviors', 'builtin.concurrent', 'builtin_md'])
<|endoftext|>


<commit_before>"""This is word2vec model for high quality representation of words."""
import numpy as np
from collections import Counter
import tensorflow as tf
import preprocess

def read_file(file_name):
	f = open(file_name, 'r')
	words = f.read().split()
	return words

def word2id(words, vocabulary_size):
	count = [['UNK', -1]]
	count.extend(Counter(words).most_common(vocabulary_size - 1))
	word2id = dict()

	for word, _ in count:
		word2id[word] = len(word2id)

	unk_count = 0
	indexed_data = []
	for word in words:
		if word in word2id:
			index = word2id[word]
		else:
			index = 0
			unk_count += 1
		indexed_data.append(index)

	count[0][1] = unk_count
	id2word = dict(zip(word2id.values(), word2id.keys()))
	return word2id, id2word, count, indexed_data

class BatchGenerator(object):
	def __init__(
			self, batch_size, window_size, n_skips, indexed_data,
			skip_gram=True):
		self.data = indexed_data
		self.window_size = window_size
		self.n_skips = n_skips
		self.batch_size = batch_size
		self._cursor = 0

		if skip_gram:
			self.next_batch = self.next_batch_skip
		else:
			self.next_batch = self.next_batch_cbow

		# batch_size should be multiple of n_skips because each target word
		# is present in batch for n_skips times.
		assert batch_size % n_skips == 0

	def next_batch_cbow(self):
		batch_input = np.ndarray(
			shape=(self.batch_size, self.n_skips), dtype=np.int32)
		batch_output = np.ndarray(shape=(self.batch_size), dtype=np.int32)

		k = 0
		for _ in range(self.n_skips):
			output, inp = self.next_batch_skip()
			for i in range(0, self.batch_size, self.n_skips):
				batch_input[k] = inp[i:i+self.n_skips]
				batch_output[k] = output[i]
				k += 1

		return batch_input, batch_output

	def next_batch_skip(self):
		batch_input = np.ndarray(shape=(self.batch_size), dtype=np.int32)
		batch_output = np.ndarray(shape=(self.batch_size), dtype=np.int32)

		words = self.data[self._cursor: self._cursor + (2 * self.window_size) + 1]
		#cursor = self._cursor
		#for i in range(2 * self.window_size + 1):
		#	words.append(self.data[cursor])
		#	cursor = (cursor + 1) % len(self.data)

		for i in range(self.batch_size // self.n_skips):
			target = words[self.window_size]

			order = range(len(words))
			order.remove(self.window_size)
			np.random.shuffle(order)

			labels = [words[x] for x in order[:self.n_skips]]
			# insert data in batch_input and output arrays.
			for k in range(self.n_skips):
				batch_input[i*self.n_skips + k] = target
				batch_output[i*self.n_skips + k] = labels[k]

			self._cursor = (self._cursor + 1) % (len(self.data) - self.window_size)
			words.pop(0)
			words.append(self.data[self._cursor])

		return batch_input, batch_output

class Word2Vec(object):
	def __init__(
			self, batch_size, window_size, n_skips, vocabulary_size,
			embedding_dim, neg_k=64, sample_vocabulary=None):
		"""Word2Vec class.

		It is assumed that data provided is numerical and contains word_ids rather
		than actual words.

		Currently support skip-gram model only.
		"""
		self.n_inputs = vocabulary_size
		self.n_hidden = embedding_dim
		self.n_outputs = self.n_inputs

		self.window_size = window_size
		self.batch_size = batch_size
		self.n_skips = n_skips
		self.vocabulary_size = vocabulary_size
		self.neg_k = neg_k
		self.sample_vocabulary = sample_vocabulary
		self.sample_vocabulary_size = int(self.vocabulary_size / 5)
		self.gen = None

		self.embeddings = preprocess.xavier_init(
			(self.n_inputs, self.n_hidden), self.n_inputs, self.n_hidden)
		self.softmax_w = preprocess.xavier_init(
			(self.n_hidden, self.n_outputs), self.n_hidden, self.n_outputs)
		self.softmax_b = np.zeros(self.n_outputs)

		self.previous_d_softmax_w = np.zeros(self.softmax_w.shape)
		self.previous_d_softmax_b = np.zeros(self.softmax_b.shape)
		self.previous_d_embeddings = np.zeros(self.embeddings.shape)

	def negative_samples(self, labels):
		vocab = xrange(self.sample_vocabulary_size)
		sample_indices = np.random.choice(vocab, self.neg_k - 1)
		samples = []
		for ind in sample_indices:
			if self.sample_vocabulary[ind] in labels:
				choice  = np.random.choice(self.vocabulary_size)
				while choice in labels:
					choice  = np.random.choice(self.vocabulary_size)
				samples.append(choice)
			else:
				samples.append(self.sample_vocabulary[ind])
		return samples

	def set_sample_vocabulary(self, word_counts, word2id):
		self.sample_vocabulary = np.array([word2id[w] for w, c in word_counts[:self.sample_vocabulary_size]])

	def Forward(self, data, labels):
		hidden = self.embeddings[data, :]

		# Do negative sampled softmax.
		samples = np.zeros((self.batch_size, self.neg_k), dtype=np.int32)
		samples[:, :-1] =  self.negative_samples(labels)
		samples[:, -1] = labels

		output = np.zeros((self.batch_size, self.neg_k))
		for i in range(data.shape[0]):
			output[i, :] = self.softmax(np.dot(hidden[i], self.softmax_w[:, samples[i]]) + self.softmax_b[samples[i]])

		cache = {
			'hidden': hidden,
			'output': output,
			'data': data,
			'samples': samples
		}
		return cache

	def Backward(self, d_out, cache):
		hidden = cache['hidden']
		data = cache['data']
		samples = cache['samples']
		d_hidden = np.zeros(hidden.shape)
		d_embeddings = np.zeros(self.embeddings.shape)
		d_softmax_b = np.zeros(self.softmax_b.shape)
		d_softmax_w = np.zeros(self.softmax_w.shape)

		for i in range(data.shape[0]):
			d_hidden[i] = np.dot(d_out[i], self.softmax_w[:, samples[i]].T)
			d_softmax_w[:, samples[i]] += np.dot(hidden[i][:, np.newaxis], d_out[i, np.newaxis])
			d_softmax_b[samples[i]] += d_out[i]
			d_embeddings[data[i], :] += d_hidden[i]

		d_cache = {
			'd_softmax_b': d_softmax_b,
			'd_softmax_w': d_softmax_w,
			'd_embeddings': d_embeddings
		}
		return d_cache

	def Train(self, data, max_epochs, learning_rate, momentum):
		# If batch generator is not present create one.
		if self.gen is None:
			self.word2id, self.id2word, word_counts, data = word2id(
				data, self.vocabulary_size)
			self.gen = BatchGenerator(
				self.batch_size, self.window_size, self.n_skips, data)

		if self.sample_vocabulary is None:
			self.set_sample_vocabulary(word_counts, self.word2id)
			del word_counts

		# Use average loss to get a good estimation.
		average_loss = 0
		for epoch in xrange(max_epochs):
			batch_input, batch_output = self.gen.next_batch()
			cache = self.Forward(batch_input, batch_output)

			samples = cache['samples']
			# Compute output gradients.
			d_out, loss = self.compute_output_gradient(cache['output'])
			# Compute other parameter gradients.
			d_cache = self.Backward(d_out, cache)

			# Apply gradients to parameters.
			d_softmax_w = self.normalize_gradient(d_cache['d_softmax_w'])
			self.previous_d_softmax_w = momentum * self.previous_d_softmax_w - learning_rate * d_softmax_w
			self.softmax_w = self.softmax_w + self.previous_d_softmax_w

			d_softmax_b = self.normalize_gradient(d_cache['d_softmax_b'])
			self.previous_d_softmax_b = momentum * self.previous_d_softmax_b - learning_rate * d_softmax_b
			self.softmax_b = self.softmax_b + self.previous_d_softmax_b

			d_embeddings = self.normalize_gradient(d_cache['d_embeddings'])
			self.previous_d_embeddings = momentum * self.previous_d_embeddings - learning_rate * d_embeddings
			self.embeddings = self.embeddings + self.previous_d_embeddings

			# Normalize embeddings.
			self.embeddings /= np.sqrt((self.embeddings ** 2).sum(axis=1)[:, np.newaxis])

			average_loss += loss
			if epoch % 100 == 0:
				if epoch > 0:
					loss = average_loss / 100
				print "Epoch: %d, Error: %.6f" % (epoch, loss)
				average_loss = 0

	def softmax(self, z):
		ez = z - z.max()
		ez = np.exp(ez)
		return ez / ez.sum()

	def compute_output_gradient(self, outputs):
		# Xentropy loss function.
		loss = np.sum(-1 * np.log(outputs)[:, -1]) / outputs.shape[0]
		d_out = outputs.copy()
		d_out[:, -1] = outputs[:, -1] - 1
		return d_out, loss

	def normalize_gradient(self, grad):
		return grad / self.batch_size

	def cosine_similarity(self, word, top=10):
		try:
			self.id2word
			self.word2id
		except Exception:
			print "Please train word2vec model with data first."
			return None

		word_id = self.word2id[word]
		simi = np.sum(self.embeddings[word_id] * self.embeddings, 1)
		sim_ids = np.argsort(simi)[-1:-top-1:-1]
		return [self.id2word[i] for i in sim_ids]
<commit_msg>Fix BatchGenerator class.<commit_after>"""This is word2vec model for high quality representation of words."""
import numpy as np
from collections import Counter
import tensorflow as tf
import preprocess

def read_file(file_name):
	f = open(file_name, 'r')
	words = f.read().split()
	return words

def word2id(words, vocabulary_size):
	count = [['UNK', -1]]
	count.extend(Counter(words).most_common(vocabulary_size - 1))
	word2id = dict()

	for word, _ in count:
		word2id[word] = len(word2id)

	unk_count = 0
	indexed_data = []
	for word in words:
		if word in word2id:
			index = word2id[word]
		else:
			index = 0
			unk_count += 1
		indexed_data.append(index)

	count[0][1] = unk_count
	id2word = dict(zip(word2id.values(), word2id.keys()))
	return word2id, id2word, count, indexed_data

class BatchGenerator(object):
	def __init__(
			self, batch_size, window_size, n_skips, indexed_data,
			skip_gram=True):
		self.data = indexed_data
		self.window_size = window_size
		self.n_skips = n_skips
		self.batch_size = batch_size
		self._cursor = 0

		if skip_gram:
			self.next_batch = self.next_batch_skip
		else:
			self.next_batch = self.next_batch_cbow

		# batch_size should be multiple of n_skips because each target word
		# is present in batch for n_skips times.
		assert batch_size % n_skips == 0

	def next_batch_cbow(self):
		batch_input = np.ndarray(
			shape=(self.batch_size, self.n_skips), dtype=np.int32)
		batch_output = np.ndarray(shape=(self.batch_size), dtype=np.int32)

		k = 0
		for _ in range(self.n_skips):
			output, inp = self.next_batch_skip()
			for i in range(0, self.batch_size, self.n_skips):
				batch_input[k] = inp[i:i+self.n_skips]
				batch_output[k] = output[i]
				k += 1

		return batch_input, batch_output

	def next_batch_skip(self):
		batch_input = np.ndarray(shape=(self.batch_size), dtype=np.int32)
		batch_output = np.ndarray(shape=(self.batch_size), dtype=np.int32)

		words = self.data[self._cursor: self._cursor + (2 * self.window_size) + 1]
		#cursor = self._cursor
		#for i in range(2 * self.window_size + 1):
		#	words.append(self.data[cursor])
		#	cursor = (cursor + 1) % len(self.data)

		for i in range(self.batch_size // self.n_skips):
			target = words[self.window_size]

			order = range(len(words))
			order.remove(self.window_size)
			np.random.shuffle(order)

			labels = [words[x] for x in order[:self.n_skips]]
			# insert data in batch_input and output arrays.
			for k in range(self.n_skips):
				batch_input[i*self.n_skips + k] = target
				batch_output[i*self.n_skips + k] = labels[k]

			self._cursor = (self._cursor + 1) % (len(self.data) - (2*self.window_size))
			words = words[1:]
			words.append(self.data[self._cursor])

		return batch_input, batch_output

class Word2Vec(object):
	def __init__(
			self, batch_size, window_size, n_skips, vocabulary_size,
			embedding_dim, neg_k=64, sample_vocabulary=None):
		"""Word2Vec class.

		It is assumed that data provided is numerical and contains word_ids rather
		than actual words.

		Currently support skip-gram model only.
		"""
		self.n_inputs = vocabulary_size
		self.n_hidden = embedding_dim
		self.n_outputs = self.n_inputs

		self.window_size = window_size
		self.batch_size = batch_size
		self.n_skips = n_skips
		self.vocabulary_size = vocabulary_size
		self.neg_k = neg_k
		self.sample_vocabulary = sample_vocabulary
		self.sample_vocabulary_size = int(self.vocabulary_size / 5)
		self.gen = None

		self.embeddings = preprocess.xavier_init(
			(self.n_inputs, self.n_hidden), self.n_inputs, self.n_hidden)
		self.softmax_w = preprocess.xavier_init(
			(self.n_hidden, self.n_outputs), self.n_hidden, self.n_outputs)
		self.softmax_b = np.zeros(self.n_outputs)

		self.previous_d_softmax_w = np.zeros(self.softmax_w.shape)
		self.previous_d_softmax_b = np.zeros(self.softmax_b.shape)
		self.previous_d_embeddings = np.zeros(self.embeddings.shape)

	def negative_samples(self, labels):
		vocab = xrange(self.sample_vocabulary_size)
		sample_indices = np.random.choice(vocab, self.neg_k - 1)
		samples = []
		for ind in sample_indices:
			if self.sample_vocabulary[ind] in labels:
				choice  = np.random.choice(self.vocabulary_size)
				while choice in labels:
					choice  = np.random.choice(self.vocabulary_size)
				samples.append(choice)
			else:
				samples.append(self.sample_vocabulary[ind])
		return samples

	def set_sample_vocabulary(self, word_counts, word2id):
		self.sample_vocabulary = np.array([word2id[w] for w, c in word_counts[:self.sample_vocabulary_size]])

	def Forward(self, data, labels):
		hidden = self.embeddings[data, :]

		# Do negative sampled softmax.
		samples = np.zeros((self.batch_size, self.neg_k), dtype=np.int32)
		samples[:, :-1] =  self.negative_samples(labels)
		samples[:, -1] = labels

		output = np.zeros((self.batch_size, self.neg_k))
		for i in range(data.shape[0]):
			output[i, :] = self.softmax(np.dot(hidden[i], self.softmax_w[:, samples[i]]) + self.softmax_b[samples[i]])

		cache = {
			'hidden': hidden,
			'output': output,
			'data': data,
			'samples': samples
		}
		return cache

	def Backward(self, d_out, cache):
		hidden = cache['hidden']
		data = cache['data']
		samples = cache['samples']
		d_hidden = np.zeros(hidden.shape)
		d_embeddings = np.zeros(self.embeddings.shape)
		d_softmax_b = np.zeros(self.softmax_b.shape)
		d_softmax_w = np.zeros(self.softmax_w.shape)

		for i in range(data.shape[0]):
			d_hidden[i] = np.dot(d_out[i], self.softmax_w[:, samples[i]].T)
			d_softmax_w[:, samples[i]] += np.dot(hidden[i][:, np.newaxis], d_out[i, np.newaxis])
			d_softmax_b[samples[i]] += d_out[i]
			d_embeddings[data[i], :] += d_hidden[i]

		d_cache = {
			'd_softmax_b': d_softmax_b,
			'd_softmax_w': d_softmax_w,
			'd_embeddings': d_embeddings
		}
		return d_cache

	def Train(self, data, max_epochs, learning_rate, momentum):
		# If batch generator is not present create one.
		if self.gen is None:
			self.word2id, self.id2word, word_counts, data = word2id(
				data, self.vocabulary_size)
			self.gen = BatchGenerator(
				self.batch_size, self.window_size, self.n_skips, data)

		if self.sample_vocabulary is None:
			self.set_sample_vocabulary(word_counts, self.word2id)
			del word_counts

		# Use average loss to get a good estimation.
		average_loss = 0
		for epoch in xrange(max_epochs):
			batch_input, batch_output = self.gen.next_batch()
			cache = self.Forward(batch_input, batch_output)

			samples = cache['samples']
			# Compute output gradients.
			d_out, loss = self.compute_output_gradient(cache['output'])
			# Compute other parameter gradients.
			d_cache = self.Backward(d_out, cache)

			# Apply gradients to parameters.
			d_softmax_w = self.normalize_gradient(d_cache['d_softmax_w'])
			self.previous_d_softmax_w = momentum * self.previous_d_softmax_w - learning_rate * d_softmax_w
			self.softmax_w = self.softmax_w + self.previous_d_softmax_w

			d_softmax_b = self.normalize_gradient(d_cache['d_softmax_b'])
			self.previous_d_softmax_b = momentum * self.previous_d_softmax_b - learning_rate * d_softmax_b
			self.softmax_b = self.softmax_b + self.previous_d_softmax_b

			d_embeddings = self.normalize_gradient(d_cache['d_embeddings'])
			self.previous_d_embeddings = momentum * self.previous_d_embeddings - learning_rate * d_embeddings
			self.embeddings = self.embeddings + self.previous_d_embeddings

			# Normalize embeddings.
			self.embeddings /= np.sqrt((self.embeddings ** 2).sum(axis=1)[:, np.newaxis])

			average_loss += loss
			if epoch % 100 == 0:
				if epoch > 0:
					loss = average_loss / 100
				print "Epoch: %d, Error: %.6f" % (epoch, loss)
				average_loss = 0

	def softmax(self, z):
		ez = z - z.max()
		ez = np.exp(ez)
		return ez / ez.sum()

	def compute_output_gradient(self, outputs):
		# Xentropy loss function.
		loss = np.sum(-1 * np.log(outputs)[:, -1]) / outputs.shape[0]
		d_out = outputs.copy()
		d_out[:, -1] = outputs[:, -1] - 1
		return d_out, loss

	def normalize_gradient(self, grad):
		return grad / self.batch_size

	def cosine_similarity(self, word, top=10):
		try:
			self.id2word
			self.word2id
		except Exception:
			print "Please train word2vec model with data first."
			return None

		word_id = self.word2id[word]
		simi = np.sum(self.embeddings[word_id] * self.embeddings, 1)
		sim_ids = np.argsort(simi)[-1:-top-1:-1]
		return [self.id2word[i] for i in sim_ids]
<|endoftext|>


<commit_before>from __future__ import division, print_function, absolute_import

import os
import sys
import subprocess

from dipy.utils.six.moves import configparser

COMMIT_INFO_FNAME = 'COMMIT_INFO.txt'

def pkg_commit_hash(pkg_path):
    """ Get short form of commit hash given directory `pkg_path`

    There should be a file called 'COMMIT_INFO.txt' in `pkg_path`.  This is a
    file in INI file format, with at least one section: ``commit hash``, and two
    variables ``archive_subst_hash`` and ``install_hash``.  The first has a
    substitution pattern in it which may have been filled by the execution of
    ``git archive`` if this is an archive generated that way.  The second is
    filled in by the installation, if the installation is from a git archive.

    We get the commit hash from (in order of preference):

    * A substituted value in ``archive_subst_hash``
    * A written commit hash value in ``install_hash`
    * git's output, if we are in a git repository

    If all these fail, we return a not-found placeholder tuple

    Parameters
    -------------
    pkg_path : str
       directory containing package

    Returns
    ---------
    hash_from : str
       Where we got the hash from - description
    hash_str : str
       short form of hash
    """
    # Try and get commit from written commit text file
    pth = os.path.join(pkg_path, COMMIT_INFO_FNAME)
    if not os.path.isfile(pth):
        raise IOError('Missing commit info file %s' % pth)
    cfg_parser = configparser.RawConfigParser()
    cfg_parser.read(pth)
    archive_subst = cfg_parser.get('commit hash', 'archive_subst_hash')
    if not archive_subst.startswith('$Format'): # it has been substituted
        return 'archive substitution', archive_subst
    install_subst = cfg_parser.get('commit hash', 'install_hash')
    if install_subst != '':
        return 'installation', install_subst
    # maybe we are in a repository
    proc = subprocess.Popen('git rev-parse --short HEAD',
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE,
                            cwd=pkg_path, shell=True)
    repo_commit, _ = proc.communicate()
    if repo_commit:
        return 'repository', repo_commit.strip()
    return '(none found)', '<not found>'

def get_pkg_info(pkg_path):
    """ Return dict describing the context of this package

    Parameters
    ------------
    pkg_path : str
       path containing __init__.py for package

    Returns
    ----------
    context : dict
       with named parameters of interest
    """
    src, hsh = pkg_commit_hash(pkg_path)
    import numpy
    import dipy
    return dict(
        pkg_path=pkg_path,
        commit_source=src,
        commit_hash=hsh,
        sys_version=sys.version,
        sys_executable=sys.executable,
        sys_platform=sys.platform,
        np_version=numpy.__version__,
        dipy_version=dipy.__version__)
<commit_msg>Configparser to configparser<commit_after>from __future__ import division, print_function, absolute_import

import os
import sys
import subprocess

import configparser

COMMIT_INFO_FNAME = 'COMMIT_INFO.txt'

def pkg_commit_hash(pkg_path):
    """ Get short form of commit hash given directory `pkg_path`

    There should be a file called 'COMMIT_INFO.txt' in `pkg_path`.  This is a
    file in INI file format, with at least one section: ``commit hash``, and
    two variables ``archive_subst_hash`` and ``install_hash``.  The first has
    a substitution pattern in it which may have been filled by the execution
    of ``git archive`` if this is an archive generated that way.  The second
    is filled in by the installation, if the installation is from a git
    archive.

    We get the commit hash from (in order of preference):

    * A substituted value in ``archive_subst_hash``
    * A written commit hash value in ``install_hash`
    * git's output, if we are in a git repository

    If all these fail, we return a not-found placeholder tuple

    Parameters
    -------------
    pkg_path : str
       directory containing package

    Returns
    ---------
    hash_from : str
       Where we got the hash from - description
    hash_str : str
       short form of hash
    """
    # Try and get commit from written commit text file
    pth = os.path.join(pkg_path, COMMIT_INFO_FNAME)
    if not os.path.isfile(pth):
        raise IOError('Missing commit info file %s' % pth)
    cfg_parser = configparser.RawConfigParser()
    cfg_parser.read(pth)
    archive_subst = cfg_parser.get('commit hash', 'archive_subst_hash')
    if not archive_subst.startswith('$Format'): # it has been substituted
        return 'archive substitution', archive_subst
    install_subst = cfg_parser.get('commit hash', 'install_hash')
    if install_subst != '':
        return 'installation', install_subst
    # maybe we are in a repository
    proc = subprocess.Popen('git rev-parse --short HEAD',
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE,
                            cwd=pkg_path, shell=True)
    repo_commit, _ = proc.communicate()
    if repo_commit:
        return 'repository', repo_commit.strip()
    return '(none found)', '<not found>'

def get_pkg_info(pkg_path):
    """ Return dict describing the context of this package

    Parameters
    ------------
    pkg_path : str
       path containing __init__.py for package

    Returns
    ----------
    context : dict
       with named parameters of interest
    """
    src, hsh = pkg_commit_hash(pkg_path)
    import numpy
    import dipy
    return dict(
        pkg_path=pkg_path,
        commit_source=src,
        commit_hash=hsh,
        sys_version=sys.version,
        sys_executable=sys.executable,
        sys_platform=sys.platform,
        np_version=numpy.__version__,
        dipy_version=dipy.__version__)
<|endoftext|>


<commit_before>from django.conf import settings
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.db.models.signals import post_save
from django.core.cache import cache

from sitetree.models import TreeItem
from symposion.proposals.models import ProposalBase

class PyConProposalCategory(models.Model):

    name = models.CharField(max_length=100)
    slug = models.SlugField()

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name = "PyCon proposal category"
        verbose_name_plural = "PyCon proposal categories"

class ThunderdomeGroup(models.Model):
    """A set of talk proposals, grouped together for consideration within
    thunderdome.
    """
    label = models.CharField(max_length=250)
    code = models.CharField(max_length=20, unique=True)
    decided = models.BooleanField(default=False, blank=True)
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)

    @property
    def as_dict(self):
        return {
            'code': self.code,
            'decided': self.decided,
            'label': self.label,
            'talks': [i.as_dict() for i in self.talks.order_by('id')],
        }

class PyConProposal(ProposalBase):

    AUDIENCE_LEVEL_NOVICE = 1
    AUDIENCE_LEVEL_EXPERIENCED = 2
    AUDIENCE_LEVEL_INTERMEDIATE = 3

    AUDIENCE_LEVELS = [
        (AUDIENCE_LEVEL_NOVICE, _(u"Novice")),
        (AUDIENCE_LEVEL_INTERMEDIATE, _(u"Intermediate")),
        (AUDIENCE_LEVEL_EXPERIENCED, _(u"Experienced")),
    ]

    STATUS_UNREVIEWED = 1
    STATUS_KITTENDOME = 2
    STATUS_THUNDERDOME = 3
    STATUS_ACCEPTED = 4
    STATUS_DAMAGED = 5
    STATUS_REJECTED = 6

    STATUS_OPTIONS = [
        (STATUS_UNREVIEWED, 'Not Yet Reviewed'),
        (STATUS_KITTENDOME, 'In Kittendome'),
        (STATUS_THUNDERDOME, 'In Thunderdome'),
        (STATUS_ACCEPTED, 'Accepted'),
        (STATUS_DAMAGED, 'Damaged'),
        (STATUS_REJECTED, 'Rejected'),
    ]

    REJECTION_POSTER = 1
    REJECTION_LIGHTNING = 2
    REJECTION_MOVED = 3
    REJECTION_DUPLICATE = 4
    REJECTION_ADMIN = 5
    REJECTION_BAD = 6

    REJECTION_OPTIONS = [
        (REJECTION_POSTER, 'Suggest re-submission as poster.'),
        (REJECTION_LIGHTNING, 'Suggest lightning talk.'),
        (REJECTION_MOVED, 'Re-submitted under appropriate category.'),
        (REJECTION_DUPLICATE, 'Duplicate'),
        (REJECTION_ADMIN, 'Administrative Action (Other)'),
        (REJECTION_BAD, "No really: rejected. It's just plain bad."),
    ]

    category = models.ForeignKey(PyConProposalCategory)
    audience_level = models.IntegerField(
        choices=AUDIENCE_LEVELS,
        help_text=_(u'Level of audience expertise assumed in Python.'),
        verbose_name=_(u'Python level'))
    overall_status = models.IntegerField(
        choices=STATUS_OPTIONS,
        default=STATUS_UNREVIEWED,
        help_text=_(u'The status of the proposal.'))
    damaged_score = models.IntegerField(
        blank=True,
        null=True,
        help_text=_(u"Numerical indicator of the amount of interest in a talk set to 'damaged' status."))
    rejection_status = models.IntegerField(
        blank=True,
        null=True,
        choices=REJECTION_OPTIONS,
        help_text=_(u'The reason the proposal was rejected.'))
    recording_release = models.BooleanField(
        default=True,
        help_text=_(u"By submitting your talk proposal, you agree to give permission to the Python Software Foundation to record, edit, and release audio and/or video of your presentation. If you do not agree to this, please uncheck this box. See PyCon 2014 Recording Release for details.")
    )

    additional_requirements = models.TextField(
        _(u"Additional requirements"),
        blank=True,
        help_text=_(u"Please let us know if you have any specific needs (A/V requirements, multiple microphones, a table, etc).  Note for example that 'audio out' is not provided for your computer unless you tell us in advance.")
    )

    class Meta:
        abstract = True

class PyConTalkProposal(PyConProposal):

    DURATION_CHOICES = [
        (0, _(u"No preference")),
        (1, _(u"I prefer a 30 minute slot")),
        (2, _(u"I prefer a 45 minute slot")),
    ]

    duration = models.IntegerField(choices=DURATION_CHOICES)

    outline = models.TextField(
        _(u"Outline")
    )
    audience = models.CharField(
        max_length=150,
        help_text=_(u'Who is the intended audience for your talk? (Be '
                    u'specific; "Python programmers" is not a good answer '
                    u'to this question.)'),
    )
    perceived_value = models.TextField(
        _(u"Objectives"),
        max_length=400,
        help_text=_(u"What will attendees get out of your talk? When they "
                    u"leave the room, what will they know that they didn't "
                    u"know before?"),
    )

    thunderdome_group = models.ForeignKey(ThunderdomeGroup,
        blank=True,
        default=None,
        null=True,
        related_name=u'talks',
    )

    class Meta:
        verbose_name = "PyCon talk proposal"

    def as_dict(self, details=False):
        answer = super(PyConTalkProposal, self).as_dict(details=details)
        if details:
            code = None
            if self.thunderdome_group:
                code = self.thunderdome_group.code
            answer['thunderdome_group'] = code
        return answer

class PyConLightningTalkProposal(PyConProposal):

    class Meta:
        verbose_name = "PyCon lightning talk proposal"

class PyConTutorialProposal(PyConProposal):
    DOMAIN_LEVEL_NOVICE = 1
    DOMAIN_LEVEL_EXPERIENCED = 2
    DOMAIN_LEVEL_INTERMEDIATE = 3

    DOMAIN_LEVELS = [
        (DOMAIN_LEVEL_NOVICE, _(u"Novice")),
        (DOMAIN_LEVEL_INTERMEDIATE, _(u"Intermediate")),
        (DOMAIN_LEVEL_EXPERIENCED, _(u"Experienced")),
    ]

    domain_level = models.IntegerField(
        choices=DOMAIN_LEVELS,
        help_text=_(u'Level of audience expertise assumed in the '
                    u'presentation\'s domain.'))

    outline = models.TextField(
        _(u"Outline")
    )
    more_info = models.TextField(
        _(u"More info"),
        help_text=_(u"More info. Will be made public "
                    u"if your talk is accepted.")
    )
    audience = models.CharField(
        max_length=150,
        help_text=_(u'Who is the intended audience for your talk? (Be '
                    u'specific; "Python programmers" is not a good answer '
                    u'to this question.)'),
    )
    perceived_value = models.TextField(
        _(u"Objectives"),
        max_length=500,
        help_text=_(u"What will attendees get out of your talk? When they "
                    u"leave the room, what will they know that they didn't "
                    u"know before?"),
    )
    handout = models.FileField(
        _(u"Student Handout"),
        blank=True,
        null=True,
        help_text=_(u'Upload a resource to be distributed to students  '
                    u'attending the tutorial session.'),
        upload_to="tutorial_handouts"
    )

    registrants = models.ManyToManyField(
        settings.AUTH_USER_MODEL,
        blank=True,
        null=True,
        help_text=_(u'CTE registered participants for this tutorial.'),
        )

    cte_tutorial_id = models.CharField(
        max_length=150,
        help_text=_(u'CTE Tutorial Identifier. This is typically auto-populated'
                    u' from a management command.'),
        blank=True,
        default="")

    max_attendees = models.IntegerField(
        blank=True,
        null=True,
        help_text=_(u'Maximum number of attendees, per CTE data'))

    class Meta:
        verbose_name = "PyCon tutorial proposal"

class PyConPosterProposal(PyConProposal):
    class Meta:
        verbose_name = "PyCon poster proposal"

class PyConSponsorTutorialProposal(ProposalBase):
    class Meta:
        verbose_name = "PyCon Sponsor Tutorial proposal"

    def __unicode__(self):
        return self.title

def _flush_treeitem_cache(sender, instance, created, **kwargs):
    cache.delete('sitetrees')
    cache.delete('tree_aliases')
post_save.connect(_flush_treeitem_cache, sender=TreeItem)
<commit_msg>added more documentation about the hack to flush the treeitems cache<commit_after>from django.conf import settings
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.db.models.signals import post_save
from django.core.cache import cache

from sitetree.models import TreeItem
from symposion.proposals.models import ProposalBase

class PyConProposalCategory(models.Model):

    name = models.CharField(max_length=100)
    slug = models.SlugField()

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name = "PyCon proposal category"
        verbose_name_plural = "PyCon proposal categories"

class ThunderdomeGroup(models.Model):
    """A set of talk proposals, grouped together for consideration within
    thunderdome.
    """
    label = models.CharField(max_length=250)
    code = models.CharField(max_length=20, unique=True)
    decided = models.BooleanField(default=False, blank=True)
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)

    @property
    def as_dict(self):
        return {
            'code': self.code,
            'decided': self.decided,
            'label': self.label,
            'talks': [i.as_dict() for i in self.talks.order_by('id')],
        }

class PyConProposal(ProposalBase):

    AUDIENCE_LEVEL_NOVICE = 1
    AUDIENCE_LEVEL_EXPERIENCED = 2
    AUDIENCE_LEVEL_INTERMEDIATE = 3

    AUDIENCE_LEVELS = [
        (AUDIENCE_LEVEL_NOVICE, _(u"Novice")),
        (AUDIENCE_LEVEL_INTERMEDIATE, _(u"Intermediate")),
        (AUDIENCE_LEVEL_EXPERIENCED, _(u"Experienced")),
    ]

    STATUS_UNREVIEWED = 1
    STATUS_KITTENDOME = 2
    STATUS_THUNDERDOME = 3
    STATUS_ACCEPTED = 4
    STATUS_DAMAGED = 5
    STATUS_REJECTED = 6

    STATUS_OPTIONS = [
        (STATUS_UNREVIEWED, 'Not Yet Reviewed'),
        (STATUS_KITTENDOME, 'In Kittendome'),
        (STATUS_THUNDERDOME, 'In Thunderdome'),
        (STATUS_ACCEPTED, 'Accepted'),
        (STATUS_DAMAGED, 'Damaged'),
        (STATUS_REJECTED, 'Rejected'),
    ]

    REJECTION_POSTER = 1
    REJECTION_LIGHTNING = 2
    REJECTION_MOVED = 3
    REJECTION_DUPLICATE = 4
    REJECTION_ADMIN = 5
    REJECTION_BAD = 6

    REJECTION_OPTIONS = [
        (REJECTION_POSTER, 'Suggest re-submission as poster.'),
        (REJECTION_LIGHTNING, 'Suggest lightning talk.'),
        (REJECTION_MOVED, 'Re-submitted under appropriate category.'),
        (REJECTION_DUPLICATE, 'Duplicate'),
        (REJECTION_ADMIN, 'Administrative Action (Other)'),
        (REJECTION_BAD, "No really: rejected. It's just plain bad."),
    ]

    category = models.ForeignKey(PyConProposalCategory)
    audience_level = models.IntegerField(
        choices=AUDIENCE_LEVELS,
        help_text=_(u'Level of audience expertise assumed in Python.'),
        verbose_name=_(u'Python level'))
    overall_status = models.IntegerField(
        choices=STATUS_OPTIONS,
        default=STATUS_UNREVIEWED,
        help_text=_(u'The status of the proposal.'))
    damaged_score = models.IntegerField(
        blank=True,
        null=True,
        help_text=_(u"Numerical indicator of the amount of interest in a talk set to 'damaged' status."))
    rejection_status = models.IntegerField(
        blank=True,
        null=True,
        choices=REJECTION_OPTIONS,
        help_text=_(u'The reason the proposal was rejected.'))
    recording_release = models.BooleanField(
        default=True,
        help_text=_(u"By submitting your talk proposal, you agree to give permission to the Python Software Foundation to record, edit, and release audio and/or video of your presentation. If you do not agree to this, please uncheck this box. See PyCon 2014 Recording Release for details.")
    )

    additional_requirements = models.TextField(
        _(u"Additional requirements"),
        blank=True,
        help_text=_(u"Please let us know if you have any specific needs (A/V requirements, multiple microphones, a table, etc).  Note for example that 'audio out' is not provided for your computer unless you tell us in advance.")
    )

    class Meta:
        abstract = True

class PyConTalkProposal(PyConProposal):

    DURATION_CHOICES = [
        (0, _(u"No preference")),
        (1, _(u"I prefer a 30 minute slot")),
        (2, _(u"I prefer a 45 minute slot")),
    ]

    duration = models.IntegerField(choices=DURATION_CHOICES)

    outline = models.TextField(
        _(u"Outline")
    )
    audience = models.CharField(
        max_length=150,
        help_text=_(u'Who is the intended audience for your talk? (Be '
                    u'specific; "Python programmers" is not a good answer '
                    u'to this question.)'),
    )
    perceived_value = models.TextField(
        _(u"Objectives"),
        max_length=400,
        help_text=_(u"What will attendees get out of your talk? When they "
                    u"leave the room, what will they know that they didn't "
                    u"know before?"),
    )

    thunderdome_group = models.ForeignKey(ThunderdomeGroup,
        blank=True,
        default=None,
        null=True,
        related_name=u'talks',
    )

    class Meta:
        verbose_name = "PyCon talk proposal"

    def as_dict(self, details=False):
        answer = super(PyConTalkProposal, self).as_dict(details=details)
        if details:
            code = None
            if self.thunderdome_group:
                code = self.thunderdome_group.code
            answer['thunderdome_group'] = code
        return answer

class PyConLightningTalkProposal(PyConProposal):

    class Meta:
        verbose_name = "PyCon lightning talk proposal"

class PyConTutorialProposal(PyConProposal):
    DOMAIN_LEVEL_NOVICE = 1
    DOMAIN_LEVEL_EXPERIENCED = 2
    DOMAIN_LEVEL_INTERMEDIATE = 3

    DOMAIN_LEVELS = [
        (DOMAIN_LEVEL_NOVICE, _(u"Novice")),
        (DOMAIN_LEVEL_INTERMEDIATE, _(u"Intermediate")),
        (DOMAIN_LEVEL_EXPERIENCED, _(u"Experienced")),
    ]

    domain_level = models.IntegerField(
        choices=DOMAIN_LEVELS,
        help_text=_(u'Level of audience expertise assumed in the '
                    u'presentation\'s domain.'))

    outline = models.TextField(
        _(u"Outline")
    )
    more_info = models.TextField(
        _(u"More info"),
        help_text=_(u"More info. Will be made public "
                    u"if your talk is accepted.")
    )
    audience = models.CharField(
        max_length=150,
        help_text=_(u'Who is the intended audience for your talk? (Be '
                    u'specific; "Python programmers" is not a good answer '
                    u'to this question.)'),
    )
    perceived_value = models.TextField(
        _(u"Objectives"),
        max_length=500,
        help_text=_(u"What will attendees get out of your talk? When they "
                    u"leave the room, what will they know that they didn't "
                    u"know before?"),
    )
    handout = models.FileField(
        _(u"Student Handout"),
        blank=True,
        null=True,
        help_text=_(u'Upload a resource to be distributed to students  '
                    u'attending the tutorial session.'),
        upload_to="tutorial_handouts"
    )

    registrants = models.ManyToManyField(
        settings.AUTH_USER_MODEL,
        blank=True,
        null=True,
        help_text=_(u'CTE registered participants for this tutorial.'),
        )

    cte_tutorial_id = models.CharField(
        max_length=150,
        help_text=_(u'CTE Tutorial Identifier. This is typically auto-populated'
                    u' from a management command.'),
        blank=True,
        default="")

    max_attendees = models.IntegerField(
        blank=True,
        null=True,
        help_text=_(u'Maximum number of attendees, per CTE data'))

    class Meta:
        verbose_name = "PyCon tutorial proposal"

class PyConPosterProposal(PyConProposal):
    class Meta:
        verbose_name = "PyCon poster proposal"

class PyConSponsorTutorialProposal(ProposalBase):
    class Meta:
        verbose_name = "PyCon Sponsor Tutorial proposal"

    def __unicode__(self):
        return self.title

def _flush_treeitem_cache(sender, instance, created, **kwargs):
    """This is a hack added to make sure, each time we'll added or changed or
    deleted a treeitem, their cache will be purge. It is supposed to be already
    the case but it doesn't act like it.
    TODO: Find out why the cache is not freed or why the the original saved
          signal is not called
    """
    cache.delete('sitetrees')
    cache.delete('tree_aliases')
post_save.connect(_flush_treeitem_cache, sender=TreeItem)
<|endoftext|>


<commit_before>'''  Phos is a faster 3d engine than using only wxPython and OpenGL.
    At the moment it is working only for line plotting.
    When loaded use Arrow, Home and End keys to navigate.

    Examples:
    -------------
    >>> from dipy.viz import phos
    >>> phos.trajs=line([100*np.random.rand(1000,3)])
    >>> phos.show()
'''
try:
    import wx
    import sys
    from wx import glcanvas
except ImportError:
    ImportError('wxPython is not installed')

try:
    from OpenGL.GL import *
    from OpenGL.GLUT import *
    from OpenGL.GLU import *
except ImportError:
    ImportError('PyOpenGL is not installed')

try:
    import numpy as np
except ImportError:
    ImportError('Numpy is not installed')
import types

trajs=None
colors=None

def axes():

    glNewList(1, GL_COMPILE)
    glBegin(GL_LINES)

    glColor3f(1.0,0.0,0.0)		# Red
    glVertex3f(0.0, 0.0, 0.0) # origin of the line
    glVertex3f(100.0, 0.0, 0.0) # ending point of the line

    glColor3f(0.0,1.0,0.0)			# Green
    glVertex3f(0.0, 0.0, 0.0) # origin of the line
    glVertex3f(0.0, 100.0, 0.0) # ending point of the line

    glColor3f(0.0,0.0,1.0)			# Blue
    glVertex3f(0.0, 0.0, 0.0) # origin of the line
    glVertex3f(0.0, 0.0, 100.0) # ending point of the line

    glEnd()
    glEndList()

def line(lines,colors=None,opacity=1,linewidth=1):

    if not isinstance(lines,types.ListType):
        lines=[lines]

    scalar=1.0

    if colors!=None:
        lit=iter(colors)
    else:
        colors=np.random.rand(len(lines),3)
        lit=iter(colors)

    glNewList(2, GL_COMPILE)
    nol=0
    for Line in lines:

        inw=True
        mit=iter(Line)
        nit=iter(Line)
        nit.next()

        scalar=lit.next()
        glBegin(GL_LINE_STRIP)
        glColor3f(scalar[0],scalar[1],scalar[2])
        while(inw):
            try:
                m=mit.next()
                glVertex3f(m[0], m[1], m[2]) # point
            except StopIteration:
                break

        glEnd()

        nol+=1
        if nol%1000==0:
            print(nol,'Lines Loaded')

    glEndList()

class Interactor(glcanvas.GLCanvas):

    def __init__(self, parent):

        glcanvas.GLCanvas.__init__(self, parent, -1)
        self.init = False

        # initial mouse position
        self.lastx = self.x = 30
        self.lasty = self.y = 30

        # world coordinates
        self.xw=0.0
        self.yw=0.0
        self.zw=0.0

        self.size = None
        self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
        self.Bind(wx.EVT_SIZE, self.OnSize)
        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.Bind(wx.EVT_LEFT_DOWN, self.OnMouseDown)
        self.Bind(wx.EVT_LEFT_UP, self.OnMouseUp)
        self.Bind(wx.EVT_MOTION, self.OnMouseMotion)
        self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)

    def OnEraseBackground(self, event):
        pass # Do nothing, to avoid flashing on MSW.

    def OnSize(self, event):
        size = self.size = self.GetClientSize()
        if self.GetContext():
            self.SetCurrent()
            glViewport(0, 0, size.width, size.height)
        event.Skip()

    def OnPaint(self, event):
        dc = wx.PaintDC(self)
        self.SetCurrent()
        if not self.init:
            self.InitGL()
            self.init = True
        self.OnDraw()

    def OnMouseDown(self, evt):
        self.CaptureMouse()
        self.x, self.y = self.lastx, self.lasty = evt.GetPosition()

    def OnMouseUp(self, evt):
        self.ReleaseMouse()

    def OnMouseMotion(self, evt):
        if evt.Dragging() and evt.LeftIsDown():
            self.lastx, self.lasty = self.x, self.y
            self.x, self.y = evt.GetPosition()
            self.Refresh(False)

    def OnKeyDown(self,evt):
        if evt.KeyCode()==wx.WXK_UP:
            print 'Up'
            self.zw-=1
            print (self.xw,self.yw,self.zw)
            self.OnDraw()
        elif evt.KeyCode()==wx.WXK_DOWN:
            print 'Down'
            self.zw+=1
            print (self.xw,self.yw,self.zw)
            self.OnDraw()
        elif evt.KeyCode()==wx.WXK_LEFT:
            print 'Left'
            self.xw-=1
            print (self.xw,self.yw,self.zw)
            self.OnDraw()
        elif evt.KeyCode()==wx.WXK_RIGHT:
            print 'Right'
            self.xw+=1
            print (self.xw,self.yw,self.zw)
            self.OnDraw()
        elif evt.KeyCode()==wx.WXK_HOME:
            print 'Home'
            self.yw-=1
            print (self.xw,self.yw,self.zw)
            self.OnDraw()
        elif evt.KeyCode()==wx.WXK_END:
            print 'End'
            self.yw+=1
            print (self.xw,self.yw,self.zw)
            self.OnDraw()

        print evt.GetPosition()

class Renderer(Interactor):

    def InitGL(self):

        glEnable(GL_CULL_FACE)
        glEnable(GL_DEPTH_TEST)
        glDepthMask(GL_TRUE)

        glClearColor( 1, 0.9,0.5, 0 )
        #glViewport( 0, 0,1024, 800 )
        glMatrixMode( GL_PROJECTION )
        glLoadIdentity()
        gluPerspective( 60.0, float(1024)/float(800), 0.1, 300.0 )
        #glDepthMask(1)
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()

        self.xw=-91
        self.yw=-105
        self.zw=-220
        #'''
        self.on=False
        glutInit()

    def LoadActors(self):
        axes()
        #line([100*np.random.rand(1000,3)])
        line(trajs,colors)

    def OnDraw(self):

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glLoadIdentity()
        glTranslatef(self.xw, self.yw, self.zw)

        if not self.on:
            #LoadObjects()
            self.LoadActors()
            self.on=True
        else:
            #glutWireTeapot(15)
            glCallList(1)
            glCallList(2)
            #glPopMatrix()

        self.SwapBuffers()

class Window(wx.Frame):
    def __init__(self, parent = None, id = -1, title = "Phos"):
        # Init
        wx.Frame.__init__(
                self, parent, id, title, size = (1024,800),
                style = wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE
        )

        box = wx.BoxSizer(wx.HORIZONTAL)
        box.Add(Renderer(self), 1, wx.EXPAND)
        #box.Add(Renderer2(self), 1, wx.EXPAND)
        self.SetAutoLayout(True)
        self.SetSizer(box)
        self.Layout()
        # StatusBar
        self.CreateStatusBar()
        # Filemenu
        filemenu = wx.Menu()
        # Filemenu - About
        menuitem = filemenu.Append(-1, "&About", "Information about this program")
        self.Bind(wx.EVT_MENU, self.OnAbout, menuitem) # here comes the event-handler
        # Filemenu - Separator
        filemenu.AppendSeparator()
        # Filemenu - Exit
        menuitem = filemenu.Append(-1, "E&xit", "Terminate the program")
        self.Bind(wx.EVT_MENU, self.OnExit, menuitem) # here comes the event-handler
        # Menubar
        menubar = wx.MenuBar()
        menubar.Append(filemenu,"&File")
        self.SetMenuBar(menubar)
        # Show
        self.Show(True)

    def OnAbout(self,event):
        message = "Phos 3D engine"
        caption = "Dipy Team"
        wx.MessageBox(message, caption, wx.OK)

    def OnExit(self,event):
        self.Close(True)  # Close the frame.

def show():
    app = wx.PySimpleApp()
    frame = Window()
    app.MainLoop()

    del frame
    del app

if __name__ == "__main__":

    show()

<commit_msg>Example corrected in phos.py<commit_after>'''  Phos is a faster 3d engine than using only wxPython and OpenGL.
    At the moment it is working only for line plotting.
    When loaded use Arrow, Home and End keys to navigate.

    Examples:
    -------------
    >>> from dipy.viz import phos
    >>> phos.trajs=[100*np.random.rand(1000,3)]
    >>> phos.show()
'''
try:
    import wx
    import sys
    from wx import glcanvas
except ImportError:
    ImportError('wxPython is not installed')

try:
    from OpenGL.GL import *
    from OpenGL.GLUT import *
    from OpenGL.GLU import *
except ImportError:
    ImportError('PyOpenGL is not installed')

try:
    import numpy as np
except ImportError:
    ImportError('Numpy is not installed')
import types

trajs=None
colors=None

def axes():

    glNewList(1, GL_COMPILE)
    glBegin(GL_LINES)

    glColor3f(1.0,0.0,0.0)		# Red
    glVertex3f(0.0, 0.0, 0.0) # origin of the line
    glVertex3f(100.0, 0.0, 0.0) # ending point of the line

    glColor3f(0.0,1.0,0.0)			# Green
    glVertex3f(0.0, 0.0, 0.0) # origin of the line
    glVertex3f(0.0, 100.0, 0.0) # ending point of the line

    glColor3f(0.0,0.0,1.0)			# Blue
    glVertex3f(0.0, 0.0, 0.0) # origin of the line
    glVertex3f(0.0, 0.0, 100.0) # ending point of the line

    glEnd()
    glEndList()

def line(lines,colors=None,opacity=1,linewidth=1):

    if not isinstance(lines,types.ListType):
        lines=[lines]

    scalar=1.0

    if colors!=None:
        lit=iter(colors)
    else:
        colors=np.random.rand(len(lines),3)
        lit=iter(colors)

    glNewList(2, GL_COMPILE)
    nol=0
    for Line in lines:

        inw=True
        mit=iter(Line)
        nit=iter(Line)
        nit.next()

        scalar=lit.next()
        glBegin(GL_LINE_STRIP)
        glColor3f(scalar[0],scalar[1],scalar[2])
        while(inw):
            try:
                m=mit.next()
                glVertex3f(m[0], m[1], m[2]) # point
            except StopIteration:
                break

        glEnd()

        nol+=1
        if nol%1000==0:
            print(nol,'Lines Loaded')

    glEndList()

class Interactor(glcanvas.GLCanvas):

    def __init__(self, parent):

        glcanvas.GLCanvas.__init__(self, parent, -1)
        self.init = False

        # initial mouse position
        self.lastx = self.x = 30
        self.lasty = self.y = 30

        # world coordinates
        self.xw=0.0
        self.yw=0.0
        self.zw=0.0

        self.size = None
        self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
        self.Bind(wx.EVT_SIZE, self.OnSize)
        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.Bind(wx.EVT_LEFT_DOWN, self.OnMouseDown)
        self.Bind(wx.EVT_LEFT_UP, self.OnMouseUp)
        self.Bind(wx.EVT_MOTION, self.OnMouseMotion)
        self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)

    def OnEraseBackground(self, event):
        pass # Do nothing, to avoid flashing on MSW.

    def OnSize(self, event):
        size = self.size = self.GetClientSize()
        if self.GetContext():
            self.SetCurrent()
            glViewport(0, 0, size.width, size.height)
        event.Skip()

    def OnPaint(self, event):
        dc = wx.PaintDC(self)
        self.SetCurrent()
        if not self.init:
            self.InitGL()
            self.init = True
        self.OnDraw()

    def OnMouseDown(self, evt):
        self.CaptureMouse()
        self.x, self.y = self.lastx, self.lasty = evt.GetPosition()

    def OnMouseUp(self, evt):
        self.ReleaseMouse()

    def OnMouseMotion(self, evt):
        if evt.Dragging() and evt.LeftIsDown():
            self.lastx, self.lasty = self.x, self.y
            self.x, self.y = evt.GetPosition()
            self.Refresh(False)

    def OnKeyDown(self,evt):
        if evt.KeyCode()==wx.WXK_UP:
            print 'Up'
            self.zw-=1
            print (self.xw,self.yw,self.zw)
            self.OnDraw()
        elif evt.KeyCode()==wx.WXK_DOWN:
            print 'Down'
            self.zw+=1
            print (self.xw,self.yw,self.zw)
            self.OnDraw()
        elif evt.KeyCode()==wx.WXK_LEFT:
            print 'Left'
            self.xw-=1
            print (self.xw,self.yw,self.zw)
            self.OnDraw()
        elif evt.KeyCode()==wx.WXK_RIGHT:
            print 'Right'
            self.xw+=1
            print (self.xw,self.yw,self.zw)
            self.OnDraw()
        elif evt.KeyCode()==wx.WXK_HOME:
            print 'Home'
            self.yw-=1
            print (self.xw,self.yw,self.zw)
            self.OnDraw()
        elif evt.KeyCode()==wx.WXK_END:
            print 'End'
            self.yw+=1
            print (self.xw,self.yw,self.zw)
            self.OnDraw()

        print evt.GetPosition()

class Renderer(Interactor):

    def InitGL(self):

        glEnable(GL_CULL_FACE)
        glEnable(GL_DEPTH_TEST)
        glDepthMask(GL_TRUE)

        glClearColor( 1, 0.9,0.5, 0 )
        #glViewport( 0, 0,1024, 800 )
        glMatrixMode( GL_PROJECTION )
        glLoadIdentity()
        gluPerspective( 60.0, float(1024)/float(800), 0.1, 300.0 )
        #glDepthMask(1)
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()

        self.xw=-91
        self.yw=-105
        self.zw=-220
        #'''
        self.on=False
        glutInit()

    def LoadActors(self):
        axes()
        #line([100*np.random.rand(1000,3)])
        line(trajs,colors)

    def OnDraw(self):

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glLoadIdentity()
        glTranslatef(self.xw, self.yw, self.zw)

        if not self.on:
            #LoadObjects()
            self.LoadActors()
            self.on=True
        else:
            #glutWireTeapot(15)
            glCallList(1)
            glCallList(2)
            #glPopMatrix()

        self.SwapBuffers()

class Window(wx.Frame):
    def __init__(self, parent = None, id = -1, title = "Phos"):
        # Init
        wx.Frame.__init__(
                self, parent, id, title, size = (1024,800),
                style = wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE
        )

        box = wx.BoxSizer(wx.HORIZONTAL)
        box.Add(Renderer(self), 1, wx.EXPAND)
        #box.Add(Renderer2(self), 1, wx.EXPAND)
        self.SetAutoLayout(True)
        self.SetSizer(box)
        self.Layout()
        # StatusBar
        self.CreateStatusBar()
        # Filemenu
        filemenu = wx.Menu()
        # Filemenu - About
        menuitem = filemenu.Append(-1, "&About", "Information about this program")
        self.Bind(wx.EVT_MENU, self.OnAbout, menuitem) # here comes the event-handler
        # Filemenu - Separator
        filemenu.AppendSeparator()
        # Filemenu - Exit
        menuitem = filemenu.Append(-1, "E&xit", "Terminate the program")
        self.Bind(wx.EVT_MENU, self.OnExit, menuitem) # here comes the event-handler
        # Menubar
        menubar = wx.MenuBar()
        menubar.Append(filemenu,"&File")
        self.SetMenuBar(menubar)
        # Show
        self.Show(True)

    def OnAbout(self,event):
        message = "Phos 3D engine"
        caption = "Dipy Team"
        wx.MessageBox(message, caption, wx.OK)

    def OnExit(self,event):
        self.Close(True)  # Close the frame.

def show():
    app = wx.PySimpleApp()
    frame = Window()
    app.MainLoop()

    del frame
    del app

if __name__ == "__main__":

    show()

<|endoftext|>