<commit_before>"""Tests for charm related classes and helpers."""

import os

import yaml

from jujucharm import (
    Charm,
    CharmCommand,
    local_charm_path,
    sane_charm_store_api_url,
)
from tests import (
    temp_os_env,
    TestCase,
)
from utility import (
    temp_dir,
)

class TestCharm(TestCase):

    def test_create_default(self):
        charm = Charm('test', 'a summary')
        expected = {
            'name': 'test',
            'summary': 'a summary',
            'series': ('xenial', 'trusty'),
            'maintainer': 'juju-qa@lists.canonical.com',
            'description': 'description',
        }
        self.assertEqual(charm.metadata, expected)

    def test_default_series_default(self):
        charm = Charm('test', 'a summary')
        self.assertEqual(charm.default_series, 'xenial')

    def test_default_series_unset(self):
        charm = Charm('test', 'a summary')
        del charm.metadata['series']
        self.assertEqual(charm.default_series, 'xenial')

    def test_default_series_single(self):
        charm = Charm('test', 'a summary', series='wily')
        self.assertEqual(charm.default_series, 'wily')

    def test_default_series_list(self):
        charm = Charm('test', 'a summary', series=['trusty', 'xenial'])
        self.assertEqual(charm.default_series, 'trusty')

    def test_to_dir(self):
        charm = Charm('test', 'a summary')
        charm.metadata['description'] = 'a description'
        del charm.metadata['maintainer']
        with temp_dir() as charm_dir:
            charm.to_dir(charm_dir)
            metafile = os.path.join(charm_dir, 'metadata.yaml')
            with open(metafile) as f:
                metadata = yaml.load(f)
        expected = {
            'name': 'test',
            'summary': 'a summary',
            'description': 'a description',
            'series': ['xenial', 'trusty'],
        }
        self.assertEqual(metadata, expected)

    def test_to_repo_dir(self):
        charm = Charm('test', 'a summary', series='wily')
        with temp_dir() as repo_dir:
            charm.to_repo_dir(repo_dir)
            metafile = os.path.join(repo_dir, 'wily', 'test', 'metadata.yaml')
            with open(metafile) as f:
                metadata = yaml.load(f)
        expected = {
            'name': 'test',
            'summary': 'a summary',
            'series': 'wily',
            'maintainer': Charm.DEFAULT_MAINTAINER,
            'description': Charm.DEFAULT_DESCRIPTION,
        }
        self.assertEqual(metadata, expected)

    def test_add_hook_script(self):
        charm = Charm('test', 'a summary')
        config_changed = '#!/bin/sh\necho changed'
        charm.add_hook_script('config-changed', config_changed)
        with temp_dir() as charm_dir:
            charm.to_dir(charm_dir)
            hookfile = os.path.join(charm_dir, 'hooks', 'config-changed')
            self.assertTrue(os.access(hookfile, os.X_OK))
            with open(hookfile) as f:
                self.assertEqual(f.read(), config_changed)

    def test_add_hook_multiple(self):
        charm = Charm('test', 'a summary')
        config_changed = '#!/bin/sh\necho changed'
        upgrade_charm = '#!/bin/sh\necho upgraded'
        charm.add_hook_script('config-changed', config_changed)
        charm.add_hook_script('upgrade-charm', upgrade_charm)
        with temp_dir() as charm_dir:
            charm.to_dir(charm_dir)
            changedfile = os.path.join(charm_dir, 'hooks', 'config-changed')
            self.assertTrue(os.access(changedfile, os.X_OK))
            with open(changedfile) as f:
                self.assertEqual(f.read(), config_changed)
            upgradedfile = os.path.join(charm_dir, 'hooks', 'upgrade-charm')
            self.assertTrue(os.access(upgradedfile, os.X_OK))
            with open(upgradedfile) as f:
                self.assertEqual(f.read(), upgrade_charm)

class TestLocalCharm(TestCase):

    def test_make_local_charm_1x(self):
        charm = 'mysql'
        path = local_charm_path(charm, '1.25.0')
        self.assertEqual(path, 'local:mysql')

    def test_make_local_charm_1x_series(self):
        charm = 'mysql'
        path = local_charm_path(charm, '1.25.0', series='trusty')
        self.assertEqual(path, 'local:trusty/mysql')

    def test_make_local_charm_2x(self):
        charm = 'mysql'
        path = local_charm_path(charm, '2.0.0', repository='/tmp/charms')
        self.assertEqual(path, '/tmp/charms/mysql')

    def test_make_local_charm_2x_os_env(self):
        charm = 'mysql'
        with temp_os_env('JUJU_REPOSITORY', '/home/foo/repository'):
            path = local_charm_path(charm, '2.0.0')
        self.assertEqual(path, '/home/foo/repository/charms/mysql')

    def test_make_local_charm_2x_win(self):
        charm = 'mysql'
        with temp_os_env('JUJU_REPOSITORY', '/home/foo/repository'):
            path = local_charm_path(charm, '2.0.0', platform='win')
        self.assertEqual(path, '/home/foo/repository/charms-win/mysql')

    def test_make_local_charm_2x_centos(self):
        charm = 'mysql'
        with temp_os_env('JUJU_REPOSITORY', '/home/foo/repository'):
            path = local_charm_path(charm, '2.0.0', platform='centos')
        self.assertEqual(path, '/home/foo/repository/charms-centos/mysql')

class TestSaneCharmStoreApiUrl(TestCase):

    def test_returns_default_value(self):
        self.assertEqual(
            sane_charm_store_api_url(None),
            CharmCommand.default_api_url)

    def test_replaces_places_api_characters(self):
        api = 'https://example.com'
        expected = 'https://api.example.com/charmstore'
        self.assertEqual(
            sane_charm_store_api_url(api),
            expected)

    def test_replaces_www_characters(self):
        api = 'https://www.example.com'
        expected = 'https://api.example.com/charmstore'
        self.assertEqual(
            sane_charm_store_api_url(api),
            expected)
<commit_msg>Added tests to check ensure_valid_name.<commit_after>"""Tests for charm related classes and helpers."""

import os

import yaml

from jujucharm import (
    Charm,
    CharmCommand,
    local_charm_path,
    sane_charm_store_api_url,
    )
from tests import (
    temp_os_env,
    TestCase,
    )
from utility import (
    JujuAssertionError,
    temp_dir,
    )

class TestCharm(TestCase):

    def test_create_default(self):
        charm = Charm('test', 'a summary')
        expected = {
            'name': 'test',
            'summary': 'a summary',
            'series': ('xenial', 'trusty'),
            'maintainer': 'juju-qa@lists.canonical.com',
            'description': 'description',
        }
        self.assertEqual(charm.metadata, expected)

    def test_default_series_default(self):
        charm = Charm('test', 'a summary')
        self.assertEqual(charm.default_series, 'xenial')

    def test_default_series_unset(self):
        charm = Charm('test', 'a summary')
        del charm.metadata['series']
        self.assertEqual(charm.default_series, 'xenial')

    def test_default_series_single(self):
        charm = Charm('test', 'a summary', series='wily')
        self.assertEqual(charm.default_series, 'wily')

    def test_default_series_list(self):
        charm = Charm('test', 'a summary', series=['trusty', 'xenial'])
        self.assertEqual(charm.default_series, 'trusty')

    def test_to_dir(self):
        charm = Charm('test', 'a summary')
        charm.metadata['description'] = 'a description'
        del charm.metadata['maintainer']
        with temp_dir() as charm_dir:
            charm.to_dir(charm_dir)
            metafile = os.path.join(charm_dir, 'metadata.yaml')
            with open(metafile) as f:
                metadata = yaml.load(f)
        expected = {
            'name': 'test',
            'summary': 'a summary',
            'description': 'a description',
            'series': ['xenial', 'trusty'],
        }
        self.assertEqual(metadata, expected)

    def test_to_repo_dir(self):
        charm = Charm('test', 'a summary', series='wily')
        with temp_dir() as repo_dir:
            charm.to_repo_dir(repo_dir)
            metafile = os.path.join(repo_dir, 'wily', 'test', 'metadata.yaml')
            with open(metafile) as f:
                metadata = yaml.load(f)
        expected = {
            'name': 'test',
            'summary': 'a summary',
            'series': 'wily',
            'maintainer': Charm.DEFAULT_MAINTAINER,
            'description': Charm.DEFAULT_DESCRIPTION,
        }
        self.assertEqual(metadata, expected)

    def test_add_hook_script(self):
        charm = Charm('test', 'a summary')
        config_changed = '#!/bin/sh\necho changed'
        charm.add_hook_script('config-changed', config_changed)
        with temp_dir() as charm_dir:
            charm.to_dir(charm_dir)
            hookfile = os.path.join(charm_dir, 'hooks', 'config-changed')
            self.assertTrue(os.access(hookfile, os.X_OK))
            with open(hookfile) as f:
                self.assertEqual(f.read(), config_changed)

    def test_add_hook_multiple(self):
        charm = Charm('test', 'a summary')
        config_changed = '#!/bin/sh\necho changed'
        upgrade_charm = '#!/bin/sh\necho upgraded'
        charm.add_hook_script('config-changed', config_changed)
        charm.add_hook_script('upgrade-charm', upgrade_charm)
        with temp_dir() as charm_dir:
            charm.to_dir(charm_dir)
            changedfile = os.path.join(charm_dir, 'hooks', 'config-changed')
            self.assertTrue(os.access(changedfile, os.X_OK))
            with open(changedfile) as f:
                self.assertEqual(f.read(), config_changed)
            upgradedfile = os.path.join(charm_dir, 'hooks', 'upgrade-charm')
            self.assertTrue(os.access(upgradedfile, os.X_OK))
            with open(upgradedfile) as f:
                self.assertEqual(f.read(), upgrade_charm)

    def test_ensure_valid_name(self):
        charm = Charm('BAD_NAME', 'A charm with a bad name',
                      ensure_valid_name=False)
        self.assertIsNone(Charm.NAME_REGEX.match(charm.metadata['name']))
        self.assertRaisesRegexp(
            JujuAssertionError,
            'Invalid Juju Charm Name, "BAD_NAME" does not match ".*"\.',
            Charm, 'BAD_NAME', 'A charm with a checked bad name')

class TestLocalCharm(TestCase):

    def test_make_local_charm_1x(self):
        charm = 'mysql'
        path = local_charm_path(charm, '1.25.0')
        self.assertEqual(path, 'local:mysql')

    def test_make_local_charm_1x_series(self):
        charm = 'mysql'
        path = local_charm_path(charm, '1.25.0', series='trusty')
        self.assertEqual(path, 'local:trusty/mysql')

    def test_make_local_charm_2x(self):
        charm = 'mysql'
        path = local_charm_path(charm, '2.0.0', repository='/tmp/charms')
        self.assertEqual(path, '/tmp/charms/mysql')

    def test_make_local_charm_2x_os_env(self):
        charm = 'mysql'
        with temp_os_env('JUJU_REPOSITORY', '/home/foo/repository'):
            path = local_charm_path(charm, '2.0.0')
        self.assertEqual(path, '/home/foo/repository/charms/mysql')

    def test_make_local_charm_2x_win(self):
        charm = 'mysql'
        with temp_os_env('JUJU_REPOSITORY', '/home/foo/repository'):
            path = local_charm_path(charm, '2.0.0', platform='win')
        self.assertEqual(path, '/home/foo/repository/charms-win/mysql')

    def test_make_local_charm_2x_centos(self):
        charm = 'mysql'
        with temp_os_env('JUJU_REPOSITORY', '/home/foo/repository'):
            path = local_charm_path(charm, '2.0.0', platform='centos')
        self.assertEqual(path, '/home/foo/repository/charms-centos/mysql')

class TestSaneCharmStoreApiUrl(TestCase):

    def test_returns_default_value(self):
        self.assertEqual(
            sane_charm_store_api_url(None),
            CharmCommand.default_api_url)

    def test_replaces_places_api_characters(self):
        api = 'https://example.com'
        expected = 'https://api.example.com/charmstore'
        self.assertEqual(
            sane_charm_store_api_url(api),
            expected)

    def test_replaces_www_characters(self):
        api = 'https://www.example.com'
        expected = 'https://api.example.com/charmstore'
        self.assertEqual(
            sane_charm_store_api_url(api),
            expected)
<|endoftext|>


<commit_before># Copyright (c) 2010 Chris Moyer http://coredumped.org/
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish, dis-
# tribute, sublicense, and/or sell copies of the Software, and to permit
# persons to whom the Software is furnished to do so, subject to the fol-
# lowing conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.

import xml.sax
import cgi
from StringIO import StringIO

class ResponseGroup(xml.sax.ContentHandler):
    """A Generic "Response Group", which can
    be anything from the entire list of Items to
    specific response elements within an item"""

    def __init__(self, connection=None, nodename=None):
        """Initialize this Item"""
        self._connection = connection
        self._nodename = nodename
        self._nodepath = []
        self._curobj = None
        self._xml = StringIO()

    def __repr__(self):
        return '<%s: %s>' % (self.__class__.__name__, self.__dict__)

    #
    # Attribute Functions
    #
    def get(self, name):
        return self.__dict__.get(name)

    def set(self, name, value):
        self.__dict__[name] = value

    def to_xml(self):
        return "<%s>%s</%s>" % (self._nodename, self._xml.getvalue(), self._nodename)

    #
    # XML Parser functions
    #
    def startElement(self, name, attrs, connection):
        self._xml.write("<%s>" % name)
        self._nodepath.append(name)
        if len(self._nodepath) == 1:
            obj = ResponseGroup(self._connection)
            self.set(name, obj)
            self._curobj = obj
        elif self._curobj:
            self._curobj.startElement(name, attrs, connection)
        return None

    def endElement(self, name, value, connection):
        self._xml.write("%s</%s>" % (cgi.escape(value).replace("&amp;", "&"), name))
        if len(self._nodepath) == 0:
            return
        obj = None
        curval = self.get(name)
        if len(self._nodepath) == 1:
            if value or not curval:
                self.set(name, value)
            if self._curobj:
                self._curobj = None
        #elif len(self._nodepath) == 2:
            #self._curobj = None
        elif self._curobj:
            self._curobj.endElement(name, value, connection)
        self._nodepath.pop()
        return None

class Item(ResponseGroup):
    """A single Item"""

    def __init__(self, connection=None):
        """Initialize this Item"""
        super(Item, self).__init__(connection, "Item")

class ItemSet(ResponseGroup):
    """A special ResponseGroup that has built-in paging, and
    only creates new Items on the "Item" tag"""

    def __init__(self, connection, action, params, page=0):
        super(ItemSet, self).__init__(connection, "Items")
        self.objs = []
        self.iter = None
        self.page = page
        self.action = action
        self.params = params
        self.curItem = None
        self.total_results = 0
        self.total_pages = 0

    def startElement(self, name, attrs, connection):
        if name == "Item":
            self.curItem = Item(self._connection)
        elif self.curItem is not None:
            self.curItem.startElement(name, attrs, connection)
        return None

    def endElement(self, name, value, connection):
        if name == 'TotalResults':
            self.total_results = value
        elif name == 'TotalPages':
            self.total_pages = value
        elif name == "Item":
            self.objs.append(self.curItem)
            self._xml.write(self.curItem.to_xml())
            self.curItem = None
        elif self.curItem is not None:
            self.curItem.endElement(name, value, connection)
        return None

    def next(self):
        """Special paging functionality"""
        if self.iter is None:
            self.iter = iter(self.objs)
        try:
            return self.iter.next()
        except StopIteration:
            self.iter = None
            self.objs = []
            if int(self.page) < int(self.total_pages):
                self.page += 1
                self._connection.get_response(self.action, self.params, self.page, self)
                return self.next()
            else:
                raise

    def __iter__(self):
        return self

    def to_xml(self):
        """Override to first fetch everything"""
        for item in self:
            pass
        return super(ItemSet, self).to_xml()
<commit_msg>Classes inherit from old-style classes, super breaks them<commit_after># Copyright (c) 2010 Chris Moyer http://coredumped.org/
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish, dis-
# tribute, sublicense, and/or sell copies of the Software, and to permit
# persons to whom the Software is furnished to do so, subject to the fol-
# lowing conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.

import xml.sax
import cgi
from StringIO import StringIO

class ResponseGroup(xml.sax.ContentHandler):
    """A Generic "Response Group", which can
    be anything from the entire list of Items to
    specific response elements within an item"""

    def __init__(self, connection=None, nodename=None):
        """Initialize this Item"""
        self._connection = connection
        self._nodename = nodename
        self._nodepath = []
        self._curobj = None
        self._xml = StringIO()

    def __repr__(self):
        return '<%s: %s>' % (self.__class__.__name__, self.__dict__)

    #
    # Attribute Functions
    #
    def get(self, name):
        return self.__dict__.get(name)

    def set(self, name, value):
        self.__dict__[name] = value

    def to_xml(self):
        return "<%s>%s</%s>" % (self._nodename, self._xml.getvalue(), self._nodename)

    #
    # XML Parser functions
    #
    def startElement(self, name, attrs, connection):
        self._xml.write("<%s>" % name)
        self._nodepath.append(name)
        if len(self._nodepath) == 1:
            obj = ResponseGroup(self._connection)
            self.set(name, obj)
            self._curobj = obj
        elif self._curobj:
            self._curobj.startElement(name, attrs, connection)
        return None

    def endElement(self, name, value, connection):
        self._xml.write("%s</%s>" % (cgi.escape(value).replace("&amp;", "&"), name))
        if len(self._nodepath) == 0:
            return
        obj = None
        curval = self.get(name)
        if len(self._nodepath) == 1:
            if value or not curval:
                self.set(name, value)
            if self._curobj:
                self._curobj = None
        #elif len(self._nodepath) == 2:
            #self._curobj = None
        elif self._curobj:
            self._curobj.endElement(name, value, connection)
        self._nodepath.pop()
        return None

class Item(ResponseGroup):
    """A single Item"""

    def __init__(self, connection=None):
        """Initialize this Item"""
        ResponseGroup.__init__(self, connection, "Item")

class ItemSet(ResponseGroup):
    """A special ResponseGroup that has built-in paging, and
    only creates new Items on the "Item" tag"""

    def __init__(self, connection, action, params, page=0):
        ResponseGroup.__init__(self, connection, "Items")
        self.objs = []
        self.iter = None
        self.page = page
        self.action = action
        self.params = params
        self.curItem = None
        self.total_results = 0
        self.total_pages = 0

    def startElement(self, name, attrs, connection):
        if name == "Item":
            self.curItem = Item(self._connection)
        elif self.curItem is not None:
            self.curItem.startElement(name, attrs, connection)
        return None

    def endElement(self, name, value, connection):
        if name == 'TotalResults':
            self.total_results = value
        elif name == 'TotalPages':
            self.total_pages = value
        elif name == "Item":
            self.objs.append(self.curItem)
            self._xml.write(self.curItem.to_xml())
            self.curItem = None
        elif self.curItem is not None:
            self.curItem.endElement(name, value, connection)
        return None

    def next(self):
        """Special paging functionality"""
        if self.iter is None:
            self.iter = iter(self.objs)
        try:
            return self.iter.next()
        except StopIteration:
            self.iter = None
            self.objs = []
            if int(self.page) < int(self.total_pages):
                self.page += 1
                self._connection.get_response(self.action, self.params, self.page, self)
                return self.next()
            else:
                raise

    def __iter__(self):
        return self

    def to_xml(self):
        """Override to first fetch everything"""
        for item in self:
            pass
        return ResponseGroup.to_xml(self)
<|endoftext|>


<commit_before># python3
# Copyright 2018 DeepMind Technologies Limited. 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.

"""Generic actor implementation, using TensorFlow and Sonnet."""

from acme import adders
from acme import core
from acme import types
# Internal imports.
from acme.tf import utils as tf2_utils
from acme.tf import variable_utils as tf2_variable_utils

import dm_env
import sonnet as snt
import tensorflow as tf
import tensorflow_probability as tfp
import tree

tfd = tfp.distributions

class FeedForwardActor(core.Actor):
  """A feed-forward actor.

  An actor based on a feed-forward policy which takes non-batched observations
  and outputs non-batched actions. It also allows adding experiences to replay
  and updating the weights from the policy on the learner.
  """

  def __init__(
      self,
      policy_network: snt.Module,
      adder: adders.Adder = None,
      variable_client: tf2_variable_utils.VariableClient = None,
  ):
    """Initializes the actor.

    Args:
      policy_network: the policy to run.
      adder: the adder object to which allows to add experiences to a
        dataset/replay buffer.
      variable_client: object which allows to copy weights from the learner copy
        of the policy to the actor copy (in case they are separate).
    """

    # Store these for later use.
    self._adder = adder
    self._variable_client = variable_client
    self._policy_network = tf.function(policy_network)

  def select_action(self, observation: types.NestedArray) -> types.NestedArray:
    # Add a dummy batch dimension and as a side effect convert numpy to TF.
    batched_obs = tf2_utils.add_batch_dim(observation)

    # Forward the policy network.
    policy_output = self._policy_network(batched_obs)

    # If the policy network parameterises a distribution, sample from it.
    def maybe_sample(output):
      if isinstance(output, tfd.Distribution):
        output = output.sample()
      return output

    policy_output = tree.map_structure(maybe_sample, policy_output)

    # Convert to numpy and squeeze out the batch dimension.
    action = tf2_utils.to_numpy_squeeze(policy_output)

    return action

  def observe_first(self, timestep: dm_env.TimeStep):
    if self._adder:
      self._adder.add_first(timestep)

  def observe(
      self,
      action: types.NestedArray,
      next_timestep: dm_env.TimeStep,
  ):
    if self._adder:
      self._adder.add(action, next_timestep)

  def update(self):
    if self._variable_client:
      self._variable_client.update()

class RecurrentActor(core.Actor):
  """A recurrent actor.

  An actor based on a recurrent policy which takes non-batched observations and
  outputs non-batched actions, and keeps track of the recurrent state inside. It
  also allows adding experiences to replay and updating the weights from the
  policy on the learner.
  """

  def __init__(
      self,
      policy_network: snt.RNNCore,
      adder: adders.Adder = None,
      variable_client: tf2_variable_utils.VariableClient = None,
  ):
    """Initializes the actor.

    Args:
      policy_network: the (recurrent) policy to run.
      adder: the adder object to which allows to add experiences to a
        dataset/replay buffer.
      variable_client: object which allows to copy weights from the learner copy
        of the policy to the actor copy (in case they are separate).
    """
    # Store these for later use.
    self._adder = adder
    self._variable_client = variable_client
    self._network = policy_network
    self._state = None
    self._prev_state = None

    # TODO(b/152382420): Ideally we would call tf.function(network) instead but
    # this results in an error when using acme RNN snapshots.
    self._policy = tf.function(policy_network.__call__)

  def select_action(self, observation: types.NestedArray) -> types.NestedArray:
    # Add a dummy batch dimension and as a side effect convert numpy to TF.
    batched_obs = tf2_utils.add_batch_dim(observation)

    # Initialize the RNN state if necessary.
    if self._state is None:
      self._state = self._network.initial_state(1)

    # Forward.
    policy_output, new_state = self._policy(batched_obs, self._state)

    # If the policy network parameterises a distribution, sample from it.
    def maybe_sample(output):
      if isinstance(output, tfd.Distribution):
        output = output.sample()
      return output

    policy_output = tree.map_structure(maybe_sample, policy_output)

    self._prev_state = self._state
    self._state = new_state

    # Convert to numpy and squeeze out the batch dimension.
    action = tf2_utils.to_numpy_squeeze(policy_output)

    return action

  def observe_first(self, timestep: dm_env.TimeStep):
    if self._adder:
      self._adder.add_first(timestep)

    # Set the state to None so that we re-initialize at the next policy call.
    self._state = None

  def observe(
      self,
      action: types.NestedArray,
      next_timestep: dm_env.TimeStep,
  ):
    if not self._adder:
      return

    numpy_state = tf2_utils.to_numpy_squeeze(self._prev_state)
    self._adder.add(action, next_timestep, extras=(numpy_state,))

  def update(self):
    if self._variable_client:
      self._variable_client.update()

# Internal class 1.
# Internal class 2.
<commit_msg>PyType cleanup for Tensorflow actors.<commit_after># python3
# Copyright 2018 DeepMind Technologies Limited. 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.

"""Generic actor implementation, using TensorFlow and Sonnet."""
from typing import Optional

from acme import adders
from acme import core
from acme import types
# Internal imports.
from acme.tf import utils as tf2_utils
from acme.tf import variable_utils as tf2_variable_utils

import dm_env
import sonnet as snt
import tensorflow as tf
import tensorflow_probability as tfp
import tree

tfd = tfp.distributions

class FeedForwardActor(core.Actor):
  """A feed-forward actor.

  An actor based on a feed-forward policy which takes non-batched observations
  and outputs non-batched actions. It also allows adding experiences to replay
  and updating the weights from the policy on the learner.
  """

  def __init__(
      self,
      policy_network: snt.Module,
      adder: Optional[adders.Adder] = None,
      variable_client: Optional[tf2_variable_utils.VariableClient] = None,
  ):
    """Initializes the actor.

    Args:
      policy_network: the policy to run.
      adder: the adder object to which allows to add experiences to a
        dataset/replay buffer.
      variable_client: object which allows to copy weights from the learner copy
        of the policy to the actor copy (in case they are separate).
    """

    # Store these for later use.
    self._adder = adder
    self._variable_client = variable_client
    self._policy_network = tf.function(policy_network)

  def select_action(self, observation: types.NestedArray) -> types.NestedArray:
    # Add a dummy batch dimension and as a side effect convert numpy to TF.
    batched_obs = tf2_utils.add_batch_dim(observation)

    # Forward the policy network.
    policy_output = self._policy_network(batched_obs)

    # If the policy network parameterises a distribution, sample from it.
    def maybe_sample(output):
      if isinstance(output, tfd.Distribution):
        output = output.sample()
      return output

    policy_output = tree.map_structure(maybe_sample, policy_output)

    # Convert to numpy and squeeze out the batch dimension.
    action = tf2_utils.to_numpy_squeeze(policy_output)

    return action

  def observe_first(self, timestep: dm_env.TimeStep):
    if self._adder:
      self._adder.add_first(timestep)

  def observe(self, action: types.NestedArray, next_timestep: dm_env.TimeStep):
    if self._adder:
      self._adder.add(action, next_timestep)

  def update(self):
    if self._variable_client:
      self._variable_client.update()

class RecurrentActor(core.Actor):
  """A recurrent actor.

  An actor based on a recurrent policy which takes non-batched observations and
  outputs non-batched actions, and keeps track of the recurrent state inside. It
  also allows adding experiences to replay and updating the weights from the
  policy on the learner.
  """

  def __init__(
      self,
      policy_network: snt.RNNCore,
      adder: Optional[adders.Adder] = None,
      variable_client: Optional[tf2_variable_utils.VariableClient] = None,
  ):
    """Initializes the actor.

    Args:
      policy_network: the (recurrent) policy to run.
      adder: the adder object to which allows to add experiences to a
        dataset/replay buffer.
      variable_client: object which allows to copy weights from the learner copy
        of the policy to the actor copy (in case they are separate).
    """
    # Store these for later use.
    self._adder = adder
    self._variable_client = variable_client
    self._network = policy_network
    self._state = None
    self._prev_state = None

    # TODO(b/152382420): Ideally we would call tf.function(network) instead but
    # this results in an error when using acme RNN snapshots.
    self._policy = tf.function(policy_network.__call__)

  def select_action(self, observation: types.NestedArray) -> types.NestedArray:
    # Add a dummy batch dimension and as a side effect convert numpy to TF.
    batched_obs = tf2_utils.add_batch_dim(observation)

    # Initialize the RNN state if necessary.
    if self._state is None:
      self._state = self._network.initial_state(1)

    # Forward.
    policy_output, new_state = self._policy(batched_obs, self._state)

    # If the policy network parameterises a distribution, sample from it.
    def maybe_sample(output):
      if isinstance(output, tfd.Distribution):
        output = output.sample()
      return output

    policy_output = tree.map_structure(maybe_sample, policy_output)

    self._prev_state = self._state
    self._state = new_state

    # Convert to numpy and squeeze out the batch dimension.
    action = tf2_utils.to_numpy_squeeze(policy_output)

    return action

  def observe_first(self, timestep: dm_env.TimeStep):
    if self._adder:
      self._adder.add_first(timestep)

    # Set the state to None so that we re-initialize at the next policy call.
    self._state = None

  def observe(self, action: types.NestedArray, next_timestep: dm_env.TimeStep):
    if not self._adder:
      return

    numpy_state = tf2_utils.to_numpy_squeeze(self._prev_state)
    self._adder.add(action, next_timestep, extras=(numpy_state,))

  def update(self):
    if self._variable_client:
      self._variable_client.update()

# Internal class 1.
# Internal class 2.
<|endoftext|>


<commit_before>import random
from data import MOVE_CORRECTIONS, load_data, correct_name, correct_move
from data import correct_mega

class MovePredictor(object):
    def __init__(self, poke, pokedata):
        self.pokedata = pokedata
        self.poke = poke

    def get_moves(self, poke, known_moves):
        raise NotImplementedError

    def __call__(self, *args):
        return self.get_moves(*args)

class RandomMovePredictor(MovePredictor):

    def __init__(self, poke, pokedata):
        super(RandomMovePredictor, self).__init__(poke, pokedata)
        poke_moves = self.pokedata.poke_moves[correct_mega(correct_name(self.poke))]
        random.shuffle(poke_moves)
        prob = 1.0 / len(poke_moves)
        self.predictions = [(x, prob) for x in poke_moves]

    def get_moves(self, known_moves):
        return self.predictions

class MoveFrequencyPredictor(MovePredictor):

    def __init__(self, poke, pokedata):
        super(MoveFrequencyPredictor, self).__init__(poke, pokedata)
        graph_move = self.pokedata.graph_move
        self.poke_moves = self.pokedata.poke_moves[correct_mega(correct_name(self.poke))]
        self.co = graph_move['cooccurences']
        self.freq = graph_move['frequencies']

    def get_freqs(self, freq):
        probs = {}
        total = float(sum(freq.values()))
        for move in freq:
            prob = freq[move] / total
            probs[move] = prob
        return probs

    def get_moves(self, known_moves):
        move_freq = sorted(self.get_freqs(self.freq).items(), key=lambda x: -x[1])
        new_move_freq = []
        for move in move_freq:
            if move[0] in self.poke_moves:
                new_move_freq.append(move)
        self.predictions = new_move_freq
        return self.predictions

class MoveCoPredictor(MovePredictor):

    def __init__(self, poke, pokedata):
        super(MoveCoPredictor, self).__init__(poke, pokedata)
        graph_move = self.pokedata.graph_move
        self.poke_moves = self.pokedata.poke_moves[correct_mega(correct_name(self.poke))]
        self.co = graph_move['cooccurences']
        self.freq = graph_move['frequencies']

    def get_freqs(self, freq):
        probs = {}
        total = float(sum(freq.values()))
        for move in freq:
            if move in self.poke_moves:
                prob = freq[move] / total
                probs[move] = prob
        return probs

    def get_moves(self, known_moves):
        probs = {}
        if len(known_moves) == 0:
            probs = self.get_freqs(self.freq)
        else:
            for move in self.co:
                if move in known_moves:
                    continue
                if move not in self.poke_moves:
                    continue
                prob = 1.0
                for othermove in known_moves:
                    prob *= self.co[move][othermove]
                if move in MOVE_CORRECTIONS:
                    probs[MOVE_CORRECTIONS[othermove]] = probs[othermove]
                    del probs[move]
                prob *= self.freq[move]
                probs[move] = prob

    def get_moves_assumption_two(self, known_moves):
        probs = {}
        if len(known_moves) == 0:
            probs = self.get_freqs(self.freq)
        else:
            for move in known_moves:
                if move not in self.co:
                    continue
                for othermove in self.co[move]:
                    if othermove in MOVE_CORRECTIONS:
                        probs[MOVE_CORRECTIONS[othermove]] = probs[othermove]
                        del probs[move]
                    if othermove not in self.poke_moves:
                        continue
                    if othermove in known_moves:
                        continue
                    prob = self.co[move][othermove]
                    if othermove not in probs:
                        probs[othermove] = 1
                    probs[othermove] *= prob
        if probs == {}:
            probs = self.get_freqs(self.freq)
        self.predictions = sorted(probs.items(), key=lambda x: -x[1])
        return self.predictions

class PokeFrequencyPredictor(MovePredictor):

    def __init__(self, poke, pokedata):
        super(PokeFrequencyPredictor, self).__init__(poke, pokedata)
        graph_poke = self.pokedata.graph_poke
        self.co = graph_poke['cooccurences']
        self.freq = graph_poke['frequencies']

    def get_moves(self, known_moves):
        poke = correct_name(self.poke)
        poke = correct_mega(self.poke)
        probs = {}
        if len(known_moves) == 0:
            probs = self.get_freqs(poke, self.freq)
        else:
            for move in self.co[poke]:
                if move in known_moves:
                    continue
                prob = 1.0
                for othermove in known_moves:
                    print move, othermove
                    if othermove not in self.co[poke][move]:
                        continue
                    prob *= self.co[poke][move][othermove]
                if move in MOVE_CORRECTIONS:
                    probs[MOVE_CORRECTIONS[othermove]] = probs[othermove]
                    del probs[move]
                prob *= self.freq[poke][move]
                probs[move] = prob
        #else:
            #for move in known_moves:
                #if move not in self.co[poke]:
                    #continue
                #for othermove in self.co[poke][move]:
                    #if othermove in MOVE_CORRECTIONS:
                        #probs[MOVE_CORRECTIONS[othermove]] = probs[othermove]
                        #del probs[move]
                    #if othermove in known_moves:
                        #continue
                    #prob = self.co[poke][move][othermove]
                    #if othermove not in probs:
                        #probs[othermove] = 1
                    #probs[othermove] *= prob
        if probs == {}:
            probs = self.get_freqs(poke, self.freq)
        self.predictions = sorted(probs.items(), key=lambda x: -x[1])
        return self.predictions

    def get_moves_assumption_two(self, known_moves):
        poke = correct_name(self.poke)
        poke = correct_mega(self.poke)
        probs = {}
        if len(known_moves) == 0:
            probs = self.get_freqs(poke, self.freq)
        else:
            for move in known_moves:
                if move not in self.co[poke]:
                    continue
                for othermove in self.co[poke][move]:
                    if othermove in MOVE_CORRECTIONS:
                        probs[MOVE_CORRECTIONS[othermove]] = probs[othermove]
                        del probs[move]
                    if othermove in known_moves:
                        continue
                    prob = self.co[poke][move][othermove]
                    if othermove not in probs:
                        probs[othermove] = 1
                    probs[othermove] *= prob
        if probs == {}:
            probs = self.get_freqs(poke, self.freq)
        self.predictions = sorted(probs.items(), key=lambda x: -x[1])
        return self.predictions

    def get_freqs(self, poke, freq):
        poke = correct_name(self.poke)
        poke = correct_mega(self.poke)
        probs = {}
        for move in freq[poke]:
            prob = freq[poke][move]
            probs[move] = prob
        return probs

def create_predictor(name, poke, pokedata):
    return PREDICTORS[name](poke, pokedata)

PREDICTORS = {
    'RandomMovePredictor': RandomMovePredictor,
    'PokeFrequencyPredictor': PokeFrequencyPredictor,
    'MoveFrequencyPredictor': MoveFrequencyPredictor,
    'MoveCoPredictor': MoveCoPredictor
}

if __name__ == "__main__":
    pokedata = load_data("data")
    def foo(poke, moves):
        return PokeFrequencyPredictor(poke, pokedata)(moves)
    movepredictor = PokeFrequencyPredictor("Charizard", pokedata)
<commit_msg>fixed bug<commit_after>import random
from data import MOVE_CORRECTIONS, load_data, correct_name, correct_move
from data import correct_mega

class MovePredictor(object):
    def __init__(self, poke, pokedata):
        self.pokedata = pokedata
        self.poke = poke

    def get_moves(self, poke, known_moves):
        raise NotImplementedError

    def __call__(self, *args):
        return self.get_moves(*args)

class RandomMovePredictor(MovePredictor):

    def __init__(self, poke, pokedata):
        super(RandomMovePredictor, self).__init__(poke, pokedata)
        poke_moves = self.pokedata.poke_moves[correct_mega(correct_name(self.poke))]
        random.shuffle(poke_moves)
        prob = 1.0 / len(poke_moves)
        self.predictions = [(x, prob) for x in poke_moves]

    def get_moves(self, known_moves):
        return self.predictions

class MoveFrequencyPredictor(MovePredictor):

    def __init__(self, poke, pokedata):
        super(MoveFrequencyPredictor, self).__init__(poke, pokedata)
        graph_move = self.pokedata.graph_move
        self.poke_moves = self.pokedata.poke_moves[correct_mega(correct_name(self.poke))]
        self.co = graph_move['cooccurences']
        self.freq = graph_move['frequencies']

    def get_freqs(self, freq):
        probs = {}
        total = float(sum(freq.values()))
        for move in freq:
            prob = freq[move] / total
            probs[move] = prob
        return probs

    def get_moves(self, known_moves):
        move_freq = sorted(self.get_freqs(self.freq).items(), key=lambda x: -x[1])
        new_move_freq = []
        for move in move_freq:
            if move[0] in self.poke_moves:
                new_move_freq.append(move)
        self.predictions = new_move_freq
        return self.predictions

class MoveCoPredictor(MovePredictor):

    def __init__(self, poke, pokedata):
        super(MoveCoPredictor, self).__init__(poke, pokedata)
        graph_move = self.pokedata.graph_move
        self.poke_moves = self.pokedata.poke_moves[correct_mega(correct_name(self.poke))]
        self.co = graph_move['cooccurences']
        self.freq = graph_move['frequencies']

    def get_freqs(self, freq):
        probs = {}
        total = float(sum(freq.values()))
        for move in freq:
            if move in self.poke_moves:
                prob = freq[move] / total
                probs[move] = prob
        return probs

    def get_moves(self, known_moves):
        probs = {}
        if len(known_moves) == 0:
            probs = self.get_freqs(self.freq)
        else:
            for move in self.co:
                if move in known_moves:
                    continue
                if move not in self.poke_moves:
                    continue
                prob = 1.0
                for othermove in known_moves:
                    prob *= self.co[move][othermove]
                if move in MOVE_CORRECTIONS:
                    probs[MOVE_CORRECTIONS[othermove]] = probs[othermove]
                    del probs[move]
                prob *= self.freq[move]
                probs[move] = prob

    def get_moves_assumption_two(self, known_moves):
        probs = {}
        if len(known_moves) == 0:
            probs = self.get_freqs(self.freq)
        else:
            for move in known_moves:
                if move not in self.co:
                    continue
                for othermove in self.co[move]:
                    if othermove in MOVE_CORRECTIONS:
                        probs[MOVE_CORRECTIONS[othermove]] = probs[othermove]
                        del probs[move]
                    if othermove not in self.poke_moves:
                        continue
                    if othermove in known_moves:
                        continue
                    prob = self.co[move][othermove]
                    if othermove not in probs:
                        probs[othermove] = 1
                    probs[othermove] *= prob
        if probs == {}:
            probs = self.get_freqs(self.freq)
        self.predictions = sorted(probs.items(), key=lambda x: -x[1])
        return self.predictions

class PokeFrequencyPredictor(MovePredictor):

    def __init__(self, poke, pokedata):
        super(PokeFrequencyPredictor, self).__init__(poke, pokedata)
        graph_poke = self.pokedata.graph_poke
        self.co = graph_poke['cooccurences']
        self.freq = graph_poke['frequencies']

    def get_moves(self, known_moves):
        poke = correct_name(self.poke)
        poke = correct_mega(self.poke)
        probs = {}
        if len(known_moves) == 0:
            probs = self.get_freqs(poke, self.freq)
        else:
            for move in self.co[poke]:
                if move in known_moves:
                    continue
                prob = 1.0
                for othermove in known_moves:
                    if othermove not in self.co[poke][move]:
                        prob *= 0
                        continue
                    prob *= self.co[poke][move][othermove]
                if move in MOVE_CORRECTIONS:
                    probs[MOVE_CORRECTIONS[othermove]] = probs[othermove]
                    del probs[move]
                prob *= self.freq[poke][move]
                probs[move] = prob
        #else:
            #for move in known_moves:
                #if move not in self.co[poke]:
                    #continue
                #for othermove in self.co[poke][move]:
                    #if othermove in MOVE_CORRECTIONS:
                        #probs[MOVE_CORRECTIONS[othermove]] = probs[othermove]
                        #del probs[move]
                    #if othermove in known_moves:
                        #continue
                    #prob = self.co[poke][move][othermove]
                    #if othermove not in probs:
                        #probs[othermove] = 1
                    #probs[othermove] *= prob
        if probs == {}:
            probs = self.get_freqs(poke, self.freq)
        self.predictions = sorted(probs.items(), key=lambda x: -x[1])
        return self.predictions

    def get_moves_assumption_two(self, known_moves):
        poke = correct_name(self.poke)
        poke = correct_mega(self.poke)
        probs = {}
        if len(known_moves) == 0:
            probs = self.get_freqs(poke, self.freq)
        else:
            for move in known_moves:
                if move not in self.co[poke]:
                    continue
                for othermove in self.co[poke][move]:
                    if othermove in MOVE_CORRECTIONS:
                        probs[MOVE_CORRECTIONS[othermove]] = probs[othermove]
                        del probs[move]
                    if othermove in known_moves:
                        continue
                    prob = self.co[poke][move][othermove]
                    if othermove not in probs:
                        probs[othermove] = 1
                    probs[othermove] *= prob
        if probs == {}:
            probs = self.get_freqs(poke, self.freq)
        self.predictions = sorted(probs.items(), key=lambda x: -x[1])
        return self.predictions

    def get_freqs(self, poke, freq):
        poke = correct_name(self.poke)
        poke = correct_mega(self.poke)
        probs = {}
        for move in freq[poke]:
            prob = freq[poke][move]
            probs[move] = prob
        return probs

def create_predictor(name, poke, pokedata):
    return PREDICTORS[name](poke, pokedata)

PREDICTORS = {
    'RandomMovePredictor': RandomMovePredictor,
    'PokeFrequencyPredictor': PokeFrequencyPredictor,
    'MoveFrequencyPredictor': MoveFrequencyPredictor,
    'MoveCoPredictor': MoveCoPredictor
}

if __name__ == "__main__":
    pokedata = load_data("data")
    def foo(poke, moves):
        return PokeFrequencyPredictor(poke, pokedata)(moves)
    movepredictor = PokeFrequencyPredictor("Charizard", pokedata)
<|endoftext|>


<commit_before>from __future__ import division
import numpy as np
import matplotlib.pyplot as plt

from pyhsmm.basic.distributions import Regression, Gaussian, PoissonDuration
from autoregressive.distributions import AutoRegression
from pyhsmm.util.text import progprint_xrange

from pyslds.models import WeakLimitStickyHDPHMMSLDS

###################
#  generate data  #
###################

import autoregressive

As = [np.array([[np.cos(theta), -np.sin(theta)],
                [np.sin(theta), np.cos(theta)]])
      for alpha, theta in ((1.1,0.1), (1.0,-0.1), (1., 0.))]

truemodel = autoregressive.models.ARHSMM(
    alpha=4.,init_state_concentration=4.,
    obs_distns=[AutoRegression(A=A,sigma=0.05*np.eye(2)) for A in As],
    dur_distns=[PoissonDuration(alpha_0=5*50,beta_0=5) for _ in As])

data, labels = truemodel.generate(2000)
data = data[truemodel.nlags:]

plt.figure()
plt.plot(data[:,0],data[:,1],'bx-')

#################
#  build model  #
#################

Nmax = 10          # number of latnt discrete states
P = 2              # latent linear dynamics' dimension
D = data.shape[1]  # data dimension

dynamics_distns = [
    AutoRegression(
        A=np.eye(P),sigma=np.eye(P),
        nu_0=2*P,S_0=2*P*np.eye(P),M_0=np.zeros((P,P)),K_0=np.eye(P))
    for _ in xrange(Nmax)]

emission_distns = [
    Regression(
        # A=np.eye(D),sigma=0.1*np.eye(D), # TODO remove special case
        nu_0=5,S_0=np.eye(D),M_0=np.zeros((D,P)),K_0=np.eye(P))
    for _ in xrange(Nmax)]

init_dynamics_distns = [
    Gaussian(nu_0=5,sigma_0=np.eye(P),mu_0=np.zeros(P),kappa_0=1.)
    for _ in xrange(Nmax)]

model = WeakLimitStickyHDPHMMSLDS(
    dynamics_distns=dynamics_distns,
    emission_distns=emission_distns,
    init_dynamics_distns=init_dynamics_distns,
    kappa=5.,alpha=10.,gamma=10.,init_state_concentration=1.)

##################
#  run sampling  #
##################

### initialize to ground truth
# model.add_data(data,stateseq=labels) # TODO needs init when passing in labels
# s = model.states_list[0]
# s.gaussian_states = data # setup-dependent!

### initialize to NOTHING! you get NOTHING! initialize from prior
# model.add_data(data)
# s = model.states_list[0]

### initialize to all one thing, which seems to work best
model.add_data(data,stateseq=np.zeros(data.shape[0]))
s = model.states_list[0]
s.resample_gaussian_states()

def update(model):
    model.resample_model()
    return s.stateseq.copy()

samples = [update(model) for _ in progprint_xrange(500)]

plt.matshow(np.vstack(samples+[np.tile(labels,(10,1))]))

plt.show()
<commit_msg>another demo improvement<commit_after>from __future__ import division
import numpy as np
import matplotlib.pyplot as plt

from pyhsmm.basic.distributions import Regression, Gaussian, PoissonDuration
from autoregressive.distributions import AutoRegression
from pyhsmm.util.text import progprint_xrange

from pyslds.models import WeakLimitStickyHDPHMMSLDS

###################
#  generate data  #
###################

import autoregressive

As = [np.array([[np.cos(theta), -np.sin(theta)],
                [np.sin(theta), np.cos(theta)]])
      for alpha, theta in ((1.1,0.1), (1.0,-0.1), (1., 0.))]

truemodel = autoregressive.models.ARHSMM(
    alpha=4.,init_state_concentration=4.,
    obs_distns=[AutoRegression(A=A,sigma=0.05*np.eye(2)) for A in As],
    dur_distns=[PoissonDuration(alpha_0=5*50,beta_0=5) for _ in As])

data, labels = truemodel.generate(500)
data = data[truemodel.nlags:]

plt.figure()
plt.plot(data[:,0],data[:,1],'bx-')

#################
#  build model  #
#################

Nmax = 5           # number of latnt discrete states
P = 2              # latent linear dynamics' dimension
D = data.shape[1]  # data dimension

dynamics_distns = [
    AutoRegression(
        A=np.eye(P),sigma=np.eye(P),
        nu_0=2*P,S_0=2*P*np.eye(P),M_0=np.zeros((P,P)),K_0=np.eye(P))
    for _ in xrange(Nmax)]

emission_distns = [
    Regression(
        A=np.eye(D),sigma=0.1*np.eye(D), # TODO remove special case
        nu_0=5,S_0=np.eye(D),M_0=np.zeros((D,P)),K_0=np.eye(P))
    for _ in xrange(Nmax)]

init_dynamics_distns = [
    Gaussian(nu_0=5,sigma_0=np.eye(P),mu_0=np.zeros(P),kappa_0=1.)
    for _ in xrange(Nmax)]

model = WeakLimitStickyHDPHMMSLDS(
    dynamics_distns=dynamics_distns,
    emission_distns=emission_distns,
    init_dynamics_distns=init_dynamics_distns,
    kappa=50.,alpha=5.,gamma=5.,init_state_concentration=1.)

##################
#  run sampling  #
##################

### initialize to ground truth
# model.add_data(data,stateseq=labels) # TODO needs init when passing in labels
# s = model.states_list[0]
# s.gaussian_states = data # setup-dependent!

### initialize to NOTHING! you get NOTHING! initialize from prior
# model.add_data(data)
# s = model.states_list[0]

### initialize to all one thing, which seems to work best
model.add_data(data,stateseq=np.zeros(data.shape[0]))
s = model.states_list[0]
s.resample_gaussian_states()

def update(model):
    model.resample_model()
    return s.stateseq.copy()

samples = [update(model) for _ in progprint_xrange(1000)]

plt.matshow(np.vstack(samples+[np.tile(labels,(10,1))]))

plt.show()
<|endoftext|>


<commit_before>#!/usr/bin/env python
#* This file is part of the MOOSE framework
#* https://www.mooseframework.org
#*
#* All rights reserved, see COPYRIGHT for full restrictions
#* https://github.com/idaholab/moose/blob/master/COPYRIGHT
#*
#* Licensed under LGPL 2.1, please see LICENSE for details
#* https://www.gnu.org/licenses/lgpl-2.1.html

from __future__ import print_function
import os
import collections
import subprocess
from .mooseutils import git_root_dir, colorText

def check_requirement(filename):
    """Check spec file for requirement documentation."""

    messages = []
    root = hit_load(filename)
    design = root.children[0].get('design', '')
    issues = root.children[0].get('issues', '')
    deprecated = root.children[0].get('deprecated', False)

    for child in root.children[0]:
        if child.get('deprecated', deprecated):
            continue

        if 'requirement' not in child:
            messages.append("    'requirement' parameter is missing or empty in '{}' block.".format(child.name))

        if not child.get('design', design).strip():
            messages.append("    'design' parameter is missing or empty in '{}' block.".format(child.name))

        if not child.get('issues', issues).strip():
            messages.append("    'issues' parameter is missing or empty in '{}' block.".format(child.name))

        for grandchild in child.children:
            if 'detail' not in grandchild:
                messages.append("    'detail' parameter is missing or empty in '{}' block.".format(grandchild.name))

            if 'requirement' in grandchild:
                messages.append("    'requirement' parameter in block '{}' must not be used within a group, use 'detail' instead.".format(grandchild.name))

            if 'design' in grandchild:
                messages.append("    'design' parameter in block '{}' must not be used within a group.".format(grandchild.name))

            if 'issues' in grandchild:
                messages.append("    'issues' parameter in block '{}' must not be used within a group.".format(grandchild.name))

    if messages:
        print('ERROR in {}'.format(filename))
        print('\n'.join(messages) + '\n')
        return 1
    return 0

def sqa_check(working_dir=os.getcwd(), remote='origin', branch='devel', specs=['tests'], skip=[]):
    """Check that test specifications that were modified include requirements."""
    from .hit_load import hit_load

    # Fetch
    cmd = ['git', 'fetch', remote]
    subprocess.call(cmd)

    # Root directory of repository
    root = git_root_dir(working_dir)

    # Check requirements on changed tests specs
    count = 0
    cmd = ['git', 'merge-base', '{}/{}'.format(remote, branch), 'HEAD']
    sha = subprocess.check_output(cmd).strip()
    cmd = ['git', 'diff', sha, '--name-only']
    for filename in subprocess.check_output(cmd).split('\n'):
        fullname = os.path.join(root, filename)
        if os.path.isfile(fullname) and (os.path.basename(filename) in specs) and \
           not any(s in filename for s in skip):
            count += check_requirement(fullname)

    return count

def sqa_check_requirement_duplicates(working_dir=os.getcwd(), specs=['tests'], skip=[]):
    """Check that no duplicate requirements exist."""

    requirements = collections.defaultdict(list)
    for root, dirs, files in os.walk(working_dir):
        for fname in files:
            filename = os.path.join(root, fname)
            if fname in specs and not any(s in filename for s in skip):
                node = hit_load(filename)
                for child in node.children[0]:
                    req = child.get('requirement', None)
                    if req is not None:
                        requirements[req.strip()].append((filename, child.fullpath, child.line('requirement')))

    count = 0
    for key, value in requirements.items():
        if len(value) > 1:
            if count == 0:
                print(colorText('Duplicate Requirements Found:\n', 'YELLOW'))
            count += 1
            if len(key) > 80:
                print(colorText('{}...'.format(key[:80]), 'YELLOW'))
            for filename, path, line in value:
                print('    {}:{}'.format(filename, line))

    return count
<commit_msg>Update sqa_check script for python 3<commit_after>#!/usr/bin/env python
#* This file is part of the MOOSE framework
#* https://www.mooseframework.org
#*
#* All rights reserved, see COPYRIGHT for full restrictions
#* https://github.com/idaholab/moose/blob/master/COPYRIGHT
#*
#* Licensed under LGPL 2.1, please see LICENSE for details
#* https://www.gnu.org/licenses/lgpl-2.1.html

from __future__ import print_function
import os
import collections
import subprocess
from .mooseutils import git_root_dir, colorText

def check_requirement(filename):
    """Check spec file for requirement documentation."""

    messages = []
    root = hit_load(filename)
    design = root.children[0].get('design', '')
    issues = root.children[0].get('issues', '')
    deprecated = root.children[0].get('deprecated', False)

    for child in root.children[0]:
        if child.get('deprecated', deprecated):
            continue

        if 'requirement' not in child:
            messages.append("    'requirement' parameter is missing or empty in '{}' block.".format(child.name))

        if not child.get('design', design).strip():
            messages.append("    'design' parameter is missing or empty in '{}' block.".format(child.name))

        if not child.get('issues', issues).strip():
            messages.append("    'issues' parameter is missing or empty in '{}' block.".format(child.name))

        for grandchild in child.children:
            if 'detail' not in grandchild:
                messages.append("    'detail' parameter is missing or empty in '{}' block.".format(grandchild.name))

            if 'requirement' in grandchild:
                messages.append("    'requirement' parameter in block '{}' must not be used within a group, use 'detail' instead.".format(grandchild.name))

            if 'design' in grandchild:
                messages.append("    'design' parameter in block '{}' must not be used within a group.".format(grandchild.name))

            if 'issues' in grandchild:
                messages.append("    'issues' parameter in block '{}' must not be used within a group.".format(grandchild.name))

    if messages:
        print('ERROR in {}'.format(filename))
        print('\n'.join(messages) + '\n')
        return 1
    return 0

def sqa_check(working_dir=os.getcwd(), remote='origin', branch='devel', specs=['tests'], skip=[]):
    """Check that test specifications that were modified include requirements."""
    from .hit_load import hit_load

    # Fetch
    cmd = ['git', 'fetch', remote]
    subprocess.call(cmd)

    # Root directory of repository
    root = git_root_dir(working_dir)

    # Check requirements on changed tests specs
    count = 0
    cmd = ['git', 'merge-base', '{}/{}'.format(remote, branch), 'HEAD']
    sha = subprocess.check_output(cmd).decode().strip()
    cmd = ['git', 'diff', sha, '--name-only']
    for filename in subprocess.check_output(cmd).decode().split('\n'):
        fullname = os.path.join(root, filename)
        if os.path.isfile(fullname) and (os.path.basename(filename) in specs) and \
           not any(s in filename for s in skip):
            count += check_requirement(fullname)

    return count

def sqa_check_requirement_duplicates(working_dir=os.getcwd(), specs=['tests'], skip=[]):
    """Check that no duplicate requirements exist."""

    requirements = collections.defaultdict(list)
    for root, dirs, files in os.walk(working_dir):
        for fname in files:
            filename = os.path.join(root, fname)
            if fname in specs and not any(s in filename for s in skip):
                node = hit_load(filename)
                for child in node.children[0]:
                    req = child.get('requirement', None)
                    if req is not None:
                        requirements[req.strip()].append((filename, child.fullpath, child.line('requirement')))

    count = 0
    for key, value in requirements.items():
        if len(value) > 1:
            if count == 0:
                print(colorText('Duplicate Requirements Found:\n', 'YELLOW'))
            count += 1
            if len(key) > 80:
                print(colorText('{}...'.format(key[:80]), 'YELLOW'))
            for filename, path, line in value:
                print('    {}:{}'.format(filename, line))

    return count
<|endoftext|>


<commit_before>import yaml
# sudo pip install pyyaml
import re
import random
import smtplib
import datetime
import pytz
import time
import socket
import sys
import getopt
import os

help_message = '''
To use, fill out config.yml with your own participants. You can also specify
DONT-PAIR so that people don't get assigned their significant other.

You'll also need to specify your mail server settings. An example is provided
for routing mail through gmail.

For more information, see README.
'''

REQRD = (
    'SMTP_SERVER',
    'SMTP_PORT',
    'USERNAME',
    'PASSWORD',
    'TIMEZONE',
    'PARTICIPANTS',
    'DONT-PAIR',
    'FROM',
    'SUBJECT',
    'MESSAGE',
)

HEADER = """Date: {date}
Content-Type: text/plain; charset="utf-8"
Message-Id: {message_id}
From: {frm}
To: {to}
Subject: {subject}

"""

CONFIG_PATH = os.path.join(os.path.dirname(__file__), 'config.yml.template')

class Person:
    def __init__(self, name, email, invalid_matches):
        self.name = name
        self.email = email
        self.invalid_matches = invalid_matches

    def __str__(self):
        return "%s <%s>" % (self.name, self.email)

class Pair:
    def __init__(self, giver, receiver):
        self.giver = giver
        self.receiver = receiver

    def __str__(self):
        return "%s ---> %s" % (self.giver.name, self.receiver.name)

def parse_yaml(yaml_path=CONFIG_PATH):
    return yaml.load(open(yaml_path))

def choose_receiver(giver, receivers):
    choice = random.choice(receivers)
    if choice.name in giver.invalid_matches or giver.name == choice.name:
        if len(receivers) is 1:
            raise Exception('Only one receiver left, try again')
        return choose_receiver(giver, receivers)
    else:
        return choice

def create_pairs(g, r):
    givers = g[:]
    receivers = r[:]
    pairs = []
    for giver in givers:
        try:
            receiver = choose_receiver(giver, receivers)
            receivers.remove(receiver)
            pairs.append(Pair(giver, receiver))
        except:
            return create_pairs(g, r)
    return pairs

class Usage(Exception):
    def __init__(self, msg):
        self.msg = msg

def main(argv=None):
    if argv is None:
        argv = sys.argv
    try:
        try:
            opts, args = getopt.getopt(argv[1:], "shc", ["send", "help"])
        except getopt.error, msg:
            raise Usage(msg)

        # option processing
        send = False
        for option, value in opts:
            if option in ("-s", "--send"):
                send = True
            if option in ("-h", "--help"):
                raise Usage(help_message)

        config = parse_yaml()
        for key in REQRD:
            if key not in config.keys():
                raise Exception(
                    'Required parameter %s not in yaml config file!' % (key,))

        participants = config['PARTICIPANTS']
        dont_pair = config['DONT-PAIR']
        if len(participants) < 2:
            raise Exception('Not enough participants specified.')

        givers = []
        for person in participants:
            name, email = re.match(r'([^<]*)<([^>]*)>', person).groups()
            name = name.strip()
            invalid_matches = []
            for pair in dont_pair:
                names = [n.strip() for n in pair.split(',')]
                if name in names:
                    # is part of this pair
                    for member in names:
                        if name != member:
                            invalid_matches.append(member)
            person = Person(name, email, invalid_matches)
            givers.append(person)

        receivers = givers[:]
        pairs = create_pairs(givers, receivers)
        if not send:
            print """
Test pairings:

%s

To send out emails with new pairings,
call with the --send argument:

    $ python secret_santa.py --send

            """ % ("\n".join([str(p) for p in pairs]))

        if send:
            server = smtplib.SMTP(config['SMTP_SERVER'], config['SMTP_PORT'])
            server.starttls()
            server.login(config['USERNAME'], config['PASSWORD'])
        for pair in pairs:
            zone = pytz.timezone(config['TIMEZONE'])
            now = zone.localize(datetime.datetime.now())
            date = now.strftime('%a, %d %b %Y %T %Z') # Sun, 21 Dec 2008 06:25:23 +0000
            message_id = '<%s@%s>' % (str(time.time())+str(random.random()), socket.gethostname())
            frm = config['FROM']
            to = pair.giver.email
            subject = config['SUBJECT'].format(santa=pair.giver.name, santee=pair.receiver.name)
            body = (HEADER+config['MESSAGE']).format(
                date=date,
                message_id=message_id,
                frm=frm,
                to=to,
                subject=subject,
                santa=pair.giver.name,
                santee=pair.receiver.name,
            )
            if send:
                result = server.sendmail(frm, [to], body)
                print "Emailed %s <%s>" % (pair.giver.name, to)

        if send:
            server.quit()

    except Usage, err:
        print >> sys.stderr, sys.argv[0].split("/")[-1] + ": " + str(err.msg)
        print >> sys.stderr, "\t for help use --help"
        return 2

if __name__ == "__main__":
    sys.exit(main())<commit_msg>Fixed the default template file name.<commit_after>import yaml
# sudo pip install pyyaml
import re
import random
import smtplib
import datetime
import pytz
import time
import socket
import sys
import getopt
import os

help_message = '''
To use, fill out config.yml with your own participants. You can also specify
DONT-PAIR so that people don't get assigned their significant other.

You'll also need to specify your mail server settings. An example is provided
for routing mail through gmail.

For more information, see README.
'''

REQRD = (
    'SMTP_SERVER',
    'SMTP_PORT',
    'USERNAME',
    'PASSWORD',
    'TIMEZONE',
    'PARTICIPANTS',
    'DONT-PAIR',
    'FROM',
    'SUBJECT',
    'MESSAGE',
)

HEADER = """Date: {date}
Content-Type: text/plain; charset="utf-8"
Message-Id: {message_id}
From: {frm}
To: {to}
Subject: {subject}

"""

CONFIG_PATH = os.path.join(os.path.dirname(__file__), 'config.yml')

class Person:
    def __init__(self, name, email, invalid_matches):
        self.name = name
        self.email = email
        self.invalid_matches = invalid_matches

    def __str__(self):
        return "%s <%s>" % (self.name, self.email)

class Pair:
    def __init__(self, giver, receiver):
        self.giver = giver
        self.receiver = receiver

    def __str__(self):
        return "%s ---> %s" % (self.giver.name, self.receiver.name)

def parse_yaml(yaml_path=CONFIG_PATH):
    return yaml.load(open(yaml_path))

def choose_receiver(giver, receivers):
    choice = random.choice(receivers)
    if choice.name in giver.invalid_matches or giver.name == choice.name:
        if len(receivers) is 1:
            raise Exception('Only one receiver left, try again')
        return choose_receiver(giver, receivers)
    else:
        return choice

def create_pairs(g, r):
    givers = g[:]
    receivers = r[:]
    pairs = []
    for giver in givers:
        try:
            receiver = choose_receiver(giver, receivers)
            receivers.remove(receiver)
            pairs.append(Pair(giver, receiver))
        except:
            return create_pairs(g, r)
    return pairs

class Usage(Exception):
    def __init__(self, msg):
        self.msg = msg

def main(argv=None):
    if argv is None:
        argv = sys.argv
    try:
        try:
            opts, args = getopt.getopt(argv[1:], "shc", ["send", "help"])
        except getopt.error, msg:
            raise Usage(msg)

        # option processing
        send = False
        for option, value in opts:
            if option in ("-s", "--send"):
                send = True
            if option in ("-h", "--help"):
                raise Usage(help_message)

        config = parse_yaml()
        for key in REQRD:
            if key not in config.keys():
                raise Exception(
                    'Required parameter %s not in yaml config file!' % (key,))

        participants = config['PARTICIPANTS']
        dont_pair = config['DONT-PAIR']
        if len(participants) < 2:
            raise Exception('Not enough participants specified.')

        givers = []
        for person in participants:
            name, email = re.match(r'([^<]*)<([^>]*)>', person).groups()
            name = name.strip()
            invalid_matches = []
            for pair in dont_pair:
                names = [n.strip() for n in pair.split(',')]
                if name in names:
                    # is part of this pair
                    for member in names:
                        if name != member:
                            invalid_matches.append(member)
            person = Person(name, email, invalid_matches)
            givers.append(person)

        receivers = givers[:]
        pairs = create_pairs(givers, receivers)
        if not send:
            print """
Test pairings:

%s

To send out emails with new pairings,
call with the --send argument:

    $ python secret_santa.py --send

            """ % ("\n".join([str(p) for p in pairs]))

        if send:
            server = smtplib.SMTP(config['SMTP_SERVER'], config['SMTP_PORT'])
            server.starttls()
            server.login(config['USERNAME'], config['PASSWORD'])
        for pair in pairs:
            zone = pytz.timezone(config['TIMEZONE'])
            now = zone.localize(datetime.datetime.now())
            date = now.strftime('%a, %d %b %Y %T %Z') # Sun, 21 Dec 2008 06:25:23 +0000
            message_id = '<%s@%s>' % (str(time.time())+str(random.random()), socket.gethostname())
            frm = config['FROM']
            to = pair.giver.email
            subject = config['SUBJECT'].format(santa=pair.giver.name, santee=pair.receiver.name)
            body = (HEADER+config['MESSAGE']).format(
                date=date,
                message_id=message_id,
                frm=frm,
                to=to,
                subject=subject,
                santa=pair.giver.name,
                santee=pair.receiver.name,
            )
            if send:
                result = server.sendmail(frm, [to], body)
                print "Emailed %s <%s>" % (pair.giver.name, to)

        if send:
            server.quit()

    except Usage, err:
        print >> sys.stderr, sys.argv[0].split("/")[-1] + ": " + str(err.msg)
        print >> sys.stderr, "\t for help use --help"
        return 2

if __name__ == "__main__":
    sys.exit(main())<|endoftext|>


<commit_before><commit_msg>disabled SECURE_PROXY_SSL_HEADER setting in<commit_after><|endoftext|>


<commit_before>'''
Commit Date: 2016.02.22
'''
from PyQt4 import uic
from PyQt4.QtCore import * # Qt is Nokias GUI rendering code written in C++.  PyQt4 is a library in python which binds to Qt
from PyQt4.QtGui import *
from PyQt4.QtCore import pyqtSignal as Signal
import sys, os, atexit
if sys.version_info.major==2:
    import cPickle as pickle # pickle serializes python objects so they can be saved persistantly.  It converts a python object into a savable data structure
    from urllib2 import urlopen
else:
    import pickle
    from urllib.request import urlopen
from os.path import expanduser
import numpy as np
from pyqtgraph.dockarea import *
from process.BaseProcess import BaseDialog
import pyqtgraph as pg
from plugins.plugin_manager import PluginManager, load_plugin_menu
from script_editor.ScriptEditor import ScriptEditor
from multiprocessing import cpu_count
import re, time, datetime, zipfile, shutil, subprocess

data_types = ['uint8', 'uint16', 'uint32', 'uint64', 'int8', 'int16', 'int32', 'int64', 'float16', 'float32', 'float64']

mainGuiInitialized=False

class Settings:
    initial_settings = {'filename': None,
                        'internal_data_type': np.float64,
                        'multiprocessing': True,
                        'multipleTraceWindows': False,
                        'mousemode': 'rectangle',
                        'show_windows': True,
                        'recent_scripts': [],
                        'recent_files': [],
                        'nCores':cpu_count(),
                        'debug_mode': False}
    def __init__(self):
        self.config_file=os.path.join(expanduser("~"),'.FLIKA','config.p' )
        try:
            self.d=pickle.load(open(self.config_file, "rb" ))
        except Exception as e:
            print("Failed to load settings file. %s\nDefault settings restored." % e)
            self.d=Settings.initial_settings
        self.d['mousemode'] = 'rectangle' # don't change initial mousemode
    def __getitem__(self, item):
        try:
            self.d[item]
        except KeyError:
            self.d[item]=Settings.initial_settings[item] if item in Settings.initial_settings else None
        return self.d[item]
    def __setitem__(self,key,item):
        self.d[key]=item
        self.save()
    def save(self):
        '''save to a config file.'''
        if not os.path.exists(os.path.dirname(self.config_file)):
            os.makedirs(os.path.dirname(self.config_file))
        pickle.dump(self.d, open( self.config_file, "wb" ))
    def setmousemode(self,mode):
        self['mousemode']=mode
    def setMultipleTraceWindows(self, f):
        self['multipleTraceWindows'] = f
    def setInternalDataType(self, dtype):
        self['internal_data_type'] = dtype
        print('Changed data_type to {}'.format(dtype))
    def gui(self):
        old_dtype=str(np.dtype(self['internal_data_type']))
        dataDrop = pg.ComboBox(items=data_types, default=old_dtype)
        showCheck = QCheckBox()
        showCheck.setChecked(self.d['show_windows'])
        multipleTracesCheck = QCheckBox()
        multipleTracesCheck.setChecked(self['multipleTraceWindows'])
        multiprocessing = QCheckBox()
        multiprocessing.setChecked(self['multiprocessing'])
        nCores = QComboBox()
        debug_check = QCheckBox(checked=self['debug_mode'])
        debug_check.toggled.connect(setConsoleVisible)
        for i in np.arange(cpu_count())+1:
            nCores.addItem(str(i))
        nCores.setCurrentIndex(self['nCores']-1)
        items = []
        items.append({'name': 'internal_data_type', 'string': 'Internal Data Type', 'object': dataDrop})
        items.append({'name': 'show_windows', 'string': 'Show Windows', 'object': showCheck})
        items.append({'name': 'multipleTraceWindows', 'string': 'Multiple Trace Windows', 'object': multipleTracesCheck})
        items.append({'name': 'multiprocessing', 'string': 'Multiprocessing On', 'object': multiprocessing})
        items.append({'name': 'nCores', 'string': 'Number of cores to use when multiprocessing', 'object': nCores})
        items.append({'name': 'debug_mode', 'string': 'Debug Mode', 'object': debug_check})
        def update():
            self['internal_data_type'] = np.dtype(str(dataDrop.currentText()))
            self['show_windows'] = showCheck.isChecked()
            self['multipleTraceWindows'] = multipleTracesCheck.isChecked()
            self['multiprocessing']=multiprocessing.isChecked()
            self['nCores']=int(nCores.itemText(nCores.currentIndex()))
            self['debug_mode'] = debug_check.isChecked()

        self.bd = BaseDialog(items, 'FLIKA Settings', '')
        self.bd.accepted.connect(update)
        self.bd.changeSignal.connect(update)
        self.bd.show()

def mainguiClose(event):
    global m
    for win in m.windows[:] + m.traceWindows[:] + m.dialogs[:]:
        win.close()
    ScriptEditor.close()
    PluginManager.close()
    m.settings.save()
    event.accept() # let the window close

def checkUpdates():
    try:
        data = urlopen('https://raw.githubusercontent.com/flika-org/flika/master/global_vars.py').read()
    except Exception as e:
        QMessageBox.information(None, "Connection Failed", "Cannot connect to Flika Repository. Connect to the internet to check for updates.")
        return
    latest_date = re.findall(r'Commit Date: (\d{4}\.\d{2}\.\d{2})', str(data))
    date = re.findall(r'Commit Date: (\d{4}\.\d{2}\.\d{2})', __doc__)
    message = "Current Version Date: "
    if len(date) == 0:
        date = datetime.fromtimestamp(os.path.getctime(__file__))
        message += "Not found. Using download date %s." % time.strftime('%x',date)
        # Unknown current date
    else:
        date = time.strptime(date[0], "%Y.%m.%d")
        message += time.strftime('%x',date)
    message += '\nLatest Version Date: '
    if len(latest_date) == 0:
        latest_date = time.gmtime()
        message += 'Unknown. Check Github Page'
        # Unknown latest commit date
    else:
        latest_date = time.strptime(latest_date[0], "%Y.%m.%d")
        message += time.strftime('%x', latest_date)
    if latest_date > date:
        if QMessageBox.question(None, "Update Recommended", message + '\n\nWould you like to update Flika?', QMessageBox.Yes | QMessageBox.No) == QMessageBox.Yes:
            updateFlika()
    else:
        QMessageBox.information(None, "Up to date", "Your version of Flika is up to date")

def updateFlika():
    folder = os.path.dirname(__file__)
    parent_dir = os.path.dirname(folder)
    new_folder = folder + '-new'
    url = 'https://github.com/flika-org/flika/archive/master.zip'
    data = urlopen(url).read()
    output = open("flika.zip", "wb")
    output.write(data)
    output.close()

    def remove_replace(remove, replace_with):
        os.rename(remove,remove + '-old')
        os.rename(replace_with,remove)
        shutil.rmtree(remove + '-old')
        subprocess.call(['python', 'flika.py'])

    with zipfile.ZipFile('flika.zip', "r") as z:
        folder_name = os.path.dirname(z.namelist()[0])
        if os.path.exists(os.path.join(parent_dir, folder_name)):
            shutil.rmtree(os.path.join(parent_dir, folder_name))
        z.extractall(parent_dir)
    os.remove('flika.zip')
    try:
        atexit.register(remove_replace, (folder, folder_name))
        #sys.exit(0)
    except Exception as e:
        print("Failed to remove and replace old Flika. %s" % e)

def setConsoleVisible(v):
    from ctypes import windll
    GetConsoleWindow = windll.kernel32.GetConsoleWindow
    console_window_handle = GetConsoleWindow()
    ShowWindow = windll.user32.ShowWindow
    ShowWindow(console_window_handle, v)

class SetCurrentWindowSignal(QWidget):
    sig=Signal()
    def __init__(self,parent):
        QWidget.__init__(self,parent)
        self.hide()

def init(filename):
    global m, mainGuiInitialized
    mainGuiInitialized=True
    m=uic.loadUi(filename)
    load_plugin_menu()
    m.setCurrentWindowSignal=SetCurrentWindowSignal(m)
    m.settings = Settings()
    m.windows = []
    m.traceWindows = []
    m.dialogs = []
    m.currentWindow = None
    m.currentTrace = None

    m.clipboard = None
    m.setAcceptDrops(True)
    m.closeEvent = mainguiClose

<commit_msg>mistype<commit_after>'''
Commit Date: 2016.02.23
'''
from PyQt4 import uic
from PyQt4.QtCore import * # Qt is Nokias GUI rendering code written in C++.  PyQt4 is a library in python which binds to Qt
from PyQt4.QtGui import *
from PyQt4.QtCore import pyqtSignal as Signal
import sys, os, atexit
if sys.version_info.major==2:
    import cPickle as pickle # pickle serializes python objects so they can be saved persistantly.  It converts a python object into a savable data structure
    from urllib2 import urlopen
else:
    import pickle
    from urllib.request import urlopen
from os.path import expanduser
import numpy as np
from pyqtgraph.dockarea import *
from process.BaseProcess import BaseDialog
import pyqtgraph as pg
from plugins.plugin_manager import PluginManager, load_plugin_menu
from script_editor.ScriptEditor import ScriptEditor
from multiprocessing import cpu_count
import re, time, datetime, zipfile, shutil, subprocess

data_types = ['uint8', 'uint16', 'uint32', 'uint64', 'int8', 'int16', 'int32', 'int64', 'float16', 'float32', 'float64']

mainGuiInitialized=False

class Settings:
    initial_settings = {'filename': None,
                        'internal_data_type': np.float64,
                        'multiprocessing': True,
                        'multipleTraceWindows': False,
                        'mousemode': 'rectangle',
                        'show_windows': True,
                        'recent_scripts': [],
                        'recent_files': [],
                        'nCores':cpu_count(),
                        'debug_mode': False}
    def __init__(self):
        self.config_file=os.path.join(expanduser("~"),'.FLIKA','config.p' )
        try:
            self.d=pickle.load(open(self.config_file, "rb" ))
        except Exception as e:
            print("Failed to load settings file. %s\nDefault settings restored." % e)
            self.d=Settings.initial_settings
        self.d['mousemode'] = 'rectangle' # don't change initial mousemode
    def __getitem__(self, item):
        try:
            self.d[item]
        except KeyError:
            self.d[item]=Settings.initial_settings[item] if item in Settings.initial_settings else None
        return self.d[item]
    def __setitem__(self,key,item):
        self.d[key]=item
        self.save()
    def save(self):
        '''save to a config file.'''
        if not os.path.exists(os.path.dirname(self.config_file)):
            os.makedirs(os.path.dirname(self.config_file))
        pickle.dump(self.d, open( self.config_file, "wb" ))
    def setmousemode(self,mode):
        self['mousemode']=mode
    def setMultipleTraceWindows(self, f):
        self['multipleTraceWindows'] = f
    def setInternalDataType(self, dtype):
        self['internal_data_type'] = dtype
        print('Changed data_type to {}'.format(dtype))
    def gui(self):
        old_dtype=str(np.dtype(self['internal_data_type']))
        dataDrop = pg.ComboBox(items=data_types, default=old_dtype)
        showCheck = QCheckBox()
        showCheck.setChecked(self.d['show_windows'])
        multipleTracesCheck = QCheckBox()
        multipleTracesCheck.setChecked(self['multipleTraceWindows'])
        multiprocessing = QCheckBox()
        multiprocessing.setChecked(self['multiprocessing'])
        nCores = QComboBox()
        debug_check = QCheckBox(checked=self['debug_mode'])
        debug_check.toggled.connect(setConsoleVisible)
        for i in np.arange(cpu_count())+1:
            nCores.addItem(str(i))
        nCores.setCurrentIndex(self['nCores']-1)
        items = []
        items.append({'name': 'internal_data_type', 'string': 'Internal Data Type', 'object': dataDrop})
        items.append({'name': 'show_windows', 'string': 'Show Windows', 'object': showCheck})
        items.append({'name': 'multipleTraceWindows', 'string': 'Multiple Trace Windows', 'object': multipleTracesCheck})
        items.append({'name': 'multiprocessing', 'string': 'Multiprocessing On', 'object': multiprocessing})
        items.append({'name': 'nCores', 'string': 'Number of cores to use when multiprocessing', 'object': nCores})
        items.append({'name': 'debug_mode', 'string': 'Debug Mode', 'object': debug_check})
        def update():
            self['internal_data_type'] = np.dtype(str(dataDrop.currentText()))
            self['show_windows'] = showCheck.isChecked()
            self['multipleTraceWindows'] = multipleTracesCheck.isChecked()
            self['multiprocessing']=multiprocessing.isChecked()
            self['nCores']=int(nCores.itemText(nCores.currentIndex()))
            self['debug_mode'] = debug_check.isChecked()

        self.bd = BaseDialog(items, 'FLIKA Settings', '')
        self.bd.accepted.connect(update)
        self.bd.changeSignal.connect(update)
        self.bd.show()

def mainguiClose(event):
    global m
    for win in m.windows[:] + m.traceWindows[:] + m.dialogs[:]:
        win.close()
    ScriptEditor.close()
    PluginManager.close()
    m.settings.save()
    event.accept() # let the window close

def checkUpdates():
    try:
        data = urlopen('https://raw.githubusercontent.com/flika-org/flika/master/global_vars.py').read()
    except Exception as e:
        QMessageBox.information(None, "Connection Failed", "Cannot connect to Flika Repository. Connect to the internet to check for updates.")
        return
    latest_date = re.findall(r'Commit Date: (\d{4}\.\d{2}\.\d{2})', str(data))
    date = re.findall(r'Commit Date: (\d{4}\.\d{2}\.\d{2})', __doc__)
    message = "Current Version Date: "
    if len(date) == 0:
        date = datetime.fromtimestamp(os.path.getctime(__file__))
        message += "Not found. Using download date %s." % time.strftime('%x',date)
        # Unknown current date
    else:
        date = time.strptime(date[0], "%Y.%m.%d")
        message += time.strftime('%x',date)
    message += '\nLatest Version Date: '
    if len(latest_date) == 0:
        latest_date = time.gmtime()
        message += 'Unknown. Check Github Page'
        # Unknown latest commit date
    else:
        latest_date = time.strptime(latest_date[0], "%Y.%m.%d")
        message += time.strftime('%x', latest_date)
    if latest_date > date:
        if QMessageBox.question(None, "Update Recommended", message + '\n\nWould you like to update Flika?', QMessageBox.Yes | QMessageBox.No) == QMessageBox.Yes:
            updateFlika()
    else:
        QMessageBox.information(None, "Up to date", "Your version of Flika is up to date")

def updateFlika():
    folder = os.path.dirname(__file__)
    parent_dir = os.path.dirname(folder)
    new_folder = folder + '-new'
    url = 'https://github.com/flika-org/flika/archive/master.zip'
    data = urlopen(url).read()
    output = open("flika.zip", "wb")
    output.write(data)
    output.close()

    def remove_replace(remove, replace_with):
        os.rename(remove,remove + '-old')
        os.rename(replace_with,remove)
        shutil.rmtree(remove + '-old')
        subprocess.call(['python', 'flika.py'])

    with zipfile.ZipFile('flika.zip', "r") as z:
        folder_name = os.path.dirname(z.namelist()[0])
        if os.path.exists(os.path.join(parent_dir, folder_name)):
            shutil.rmtree(os.path.join(parent_dir, folder_name))
        z.extractall(parent_dir)
    os.remove('flika.zip')
    try:
        atexit.register(remove_replace, folder, folder_name)
        #sys.exit(0)
    except Exception as e:
        print("Failed to remove and replace old Flika. %s" % e)

def setConsoleVisible(v):
    from ctypes import windll
    GetConsoleWindow = windll.kernel32.GetConsoleWindow
    console_window_handle = GetConsoleWindow()
    ShowWindow = windll.user32.ShowWindow
    ShowWindow(console_window_handle, v)

class SetCurrentWindowSignal(QWidget):
    sig=Signal()
    def __init__(self,parent):
        QWidget.__init__(self,parent)
        self.hide()

def init(filename):
    global m, mainGuiInitialized
    mainGuiInitialized=True
    m=uic.loadUi(filename)
    load_plugin_menu()
    m.setCurrentWindowSignal=SetCurrentWindowSignal(m)
    m.settings = Settings()
    m.windows = []
    m.traceWindows = []
    m.dialogs = []
    m.currentWindow = None
    m.currentTrace = None

    m.clipboard = None
    m.setAcceptDrops(True)
    m.closeEvent = mainguiClose

<|endoftext|>