<commit_before>import gym
import numpy as np
import unittest
from gym.spaces import Box, Discrete, Tuple

import ray

from ray.rllib.models import ModelCatalog, MODEL_DEFAULTS
from ray.rllib.models.model import Model
from ray.rllib.models.tf.tf_action_dist import TFActionDistribution
from ray.rllib.models.preprocessors import (NoPreprocessor, OneHotPreprocessor,
                                            Preprocessor)
from ray.rllib.models.tf.fcnet_v1 import FullyConnectedNetwork
from ray.rllib.models.tf.visionnet_v1 import VisionNetwork
from ray.rllib.utils import try_import_tf

tf = try_import_tf()

class CustomPreprocessor(Preprocessor):
    def _init_shape(self, obs_space, options):
        return [1]

class CustomPreprocessor2(Preprocessor):
    def _init_shape(self, obs_space, options):
        return [1]

class CustomModel(Model):
    def _build_layers(self, *args):
        return tf.constant([[0] * 5]), None

class CustomActionDistribution(TFActionDistribution):
    @staticmethod
    def required_model_output_shape(action_space, model_config=None):
        custom_options = model_config["custom_options"] or {}
        if custom_options is not None and custom_options.get("output_dim"):
            return custom_options.get("output_dim")
        return action_space.shape

    def _build_sample_op(self):
        custom_options = self.model_config["custom_options"]
        if "output_dim" in custom_options:
            output_shape = tf.concat(
                [tf.shape(self.inputs)[:1], custom_options["output_dim"]],
                axis=0)
        else:
            output_shape = tf.shape(self.inputs)
        return tf.random_uniform(output_shape)

class ModelCatalogTest(unittest.TestCase):
    def tearDown(self):
        ray.shutdown()

    def testGymPreprocessors(self):
        p1 = ModelCatalog.get_preprocessor(gym.make("CartPole-v0"))
        self.assertEqual(type(p1), NoPreprocessor)

        p2 = ModelCatalog.get_preprocessor(gym.make("FrozenLake-v0"))
        self.assertEqual(type(p2), OneHotPreprocessor)

    def testTuplePreprocessor(self):
        ray.init()

        class TupleEnv(object):
            def __init__(self):
                self.observation_space = Tuple(
                    [Discrete(5),
                     Box(0, 5, shape=(3, ), dtype=np.float32)])

        p1 = ModelCatalog.get_preprocessor(TupleEnv())
        self.assertEqual(p1.shape, (8, ))
        self.assertEqual(
            list(p1.transform((0, np.array([1, 2, 3])))),
            [float(x) for x in [1, 0, 0, 0, 0, 1, 2, 3]])

    def testCustomPreprocessor(self):
        ray.init()
        ModelCatalog.register_custom_preprocessor("foo", CustomPreprocessor)
        ModelCatalog.register_custom_preprocessor("bar", CustomPreprocessor2)
        env = gym.make("CartPole-v0")
        p1 = ModelCatalog.get_preprocessor(env, {"custom_preprocessor": "foo"})
        self.assertEqual(str(type(p1)), str(CustomPreprocessor))
        p2 = ModelCatalog.get_preprocessor(env, {"custom_preprocessor": "bar"})
        self.assertEqual(str(type(p2)), str(CustomPreprocessor2))
        p3 = ModelCatalog.get_preprocessor(env)
        self.assertEqual(type(p3), NoPreprocessor)

    def testDefaultModels(self):
        ray.init()

        with tf.variable_scope("test1"):
            p1 = ModelCatalog.get_model({
                "obs": tf.zeros((10, 3), dtype=tf.float32)
            }, Box(0, 1, shape=(3, ), dtype=np.float32), Discrete(5), 5, {})
            self.assertEqual(type(p1), FullyConnectedNetwork)

        with tf.variable_scope("test2"):
            p2 = ModelCatalog.get_model({
                "obs": tf.zeros((10, 84, 84, 3), dtype=tf.float32)
            }, Box(0, 1, shape=(84, 84, 3), dtype=np.float32), Discrete(5), 5,
                                        {})
            self.assertEqual(type(p2), VisionNetwork)

    def testCustomModel(self):
        ray.init()
        ModelCatalog.register_custom_model("foo", CustomModel)
        p1 = ModelCatalog.get_model({
            "obs": tf.constant([1, 2, 3])
        }, Box(0, 1, shape=(3, ), dtype=np.float32), Discrete(5), 5,
                                    {"custom_model": "foo"})
        self.assertEqual(str(type(p1)), str(CustomModel))

    def testCustomActionDistribution(self):
        ray.init()
        # registration
        ModelCatalog.register_custom_action_dist("test",
                                                 CustomActionDistribution)
        action_space = Box(0, 1, shape=(5, 3), dtype=np.float32)

        # test retrieving it
        model_config = MODEL_DEFAULTS.copy()
        model_config["custom_action_dist"] = "test"
        dist_cls, param_shape = ModelCatalog.get_action_dist(
            action_space, model_config)
        self.assertEqual(str(dist_cls), str(CustomActionDistribution))
        self.assertEqual(param_shape, action_space.shape)

        # test the class works as a distribution
        dist_input = tf.placeholder(tf.float32, (None, ) + param_shape)
        dist = dist_cls(dist_input, model_config=model_config)
        self.assertEqual(dist.sample().shape[1:], dist_input.shape[1:])
        self.assertIsInstance(dist.sample(), tf.Tensor)
        with self.assertRaises(NotImplementedError):
            dist.entropy()

        # test passing the options to it
        model_config["custom_options"].update({"output_dim": (3, )})
        dist_cls, param_shape = ModelCatalog.get_action_dist(
            action_space, model_config)
        self.assertEqual(param_shape, (3, ))
        dist_input = tf.placeholder(tf.float32, (None, ) + param_shape)
        dist = dist_cls(dist_input, model_config=model_config)
        self.assertEqual(dist.sample().shape[1:], dist_input.shape[1:])
        self.assertIsInstance(dist.sample(), tf.Tensor)
        with self.assertRaises(NotImplementedError):
            dist.entropy()

if __name__ == "__main__":
    unittest.main(verbosity=2)
<commit_msg>[hotfix] fix Travis action dist test (#5428)<commit_after>import gym
import numpy as np
import unittest
from gym.spaces import Box, Discrete, Tuple

import ray

from ray.rllib.models import ModelCatalog, MODEL_DEFAULTS
from ray.rllib.models.model import Model
from ray.rllib.models.tf.tf_action_dist import TFActionDistribution
from ray.rllib.models.preprocessors import (NoPreprocessor, OneHotPreprocessor,
                                            Preprocessor)
from ray.rllib.models.tf.fcnet_v1 import FullyConnectedNetwork
from ray.rllib.models.tf.visionnet_v1 import VisionNetwork
from ray.rllib.utils import try_import_tf

tf = try_import_tf()

class CustomPreprocessor(Preprocessor):
    def _init_shape(self, obs_space, options):
        return [1]

class CustomPreprocessor2(Preprocessor):
    def _init_shape(self, obs_space, options):
        return [1]

class CustomModel(Model):
    def _build_layers(self, *args):
        return tf.constant([[0] * 5]), None

class CustomActionDistribution(TFActionDistribution):
    @staticmethod
    def required_model_output_shape(action_space, model_config=None):
        custom_options = model_config["custom_options"] or {}
        if custom_options is not None and custom_options.get("output_dim"):
            return custom_options.get("output_dim")
        return action_space.shape

    def _build_sample_op(self):
        custom_options = self.model.model_config["custom_options"]
        if "output_dim" in custom_options:
            output_shape = tf.concat(
                [tf.shape(self.inputs)[:1], custom_options["output_dim"]],
                axis=0)
        else:
            output_shape = tf.shape(self.inputs)
        return tf.random_uniform(output_shape)

class ModelCatalogTest(unittest.TestCase):
    def tearDown(self):
        ray.shutdown()

    def testGymPreprocessors(self):
        p1 = ModelCatalog.get_preprocessor(gym.make("CartPole-v0"))
        self.assertEqual(type(p1), NoPreprocessor)

        p2 = ModelCatalog.get_preprocessor(gym.make("FrozenLake-v0"))
        self.assertEqual(type(p2), OneHotPreprocessor)

    def testTuplePreprocessor(self):
        ray.init()

        class TupleEnv(object):
            def __init__(self):
                self.observation_space = Tuple(
                    [Discrete(5),
                     Box(0, 5, shape=(3, ), dtype=np.float32)])

        p1 = ModelCatalog.get_preprocessor(TupleEnv())
        self.assertEqual(p1.shape, (8, ))
        self.assertEqual(
            list(p1.transform((0, np.array([1, 2, 3])))),
            [float(x) for x in [1, 0, 0, 0, 0, 1, 2, 3]])

    def testCustomPreprocessor(self):
        ray.init()
        ModelCatalog.register_custom_preprocessor("foo", CustomPreprocessor)
        ModelCatalog.register_custom_preprocessor("bar", CustomPreprocessor2)
        env = gym.make("CartPole-v0")
        p1 = ModelCatalog.get_preprocessor(env, {"custom_preprocessor": "foo"})
        self.assertEqual(str(type(p1)), str(CustomPreprocessor))
        p2 = ModelCatalog.get_preprocessor(env, {"custom_preprocessor": "bar"})
        self.assertEqual(str(type(p2)), str(CustomPreprocessor2))
        p3 = ModelCatalog.get_preprocessor(env)
        self.assertEqual(type(p3), NoPreprocessor)

    def testDefaultModels(self):
        ray.init()

        with tf.variable_scope("test1"):
            p1 = ModelCatalog.get_model({
                "obs": tf.zeros((10, 3), dtype=tf.float32)
            }, Box(0, 1, shape=(3, ), dtype=np.float32), Discrete(5), 5, {})
            self.assertEqual(type(p1), FullyConnectedNetwork)

        with tf.variable_scope("test2"):
            p2 = ModelCatalog.get_model({
                "obs": tf.zeros((10, 84, 84, 3), dtype=tf.float32)
            }, Box(0, 1, shape=(84, 84, 3), dtype=np.float32), Discrete(5), 5,
                                        {})
            self.assertEqual(type(p2), VisionNetwork)

    def testCustomModel(self):
        ray.init()
        ModelCatalog.register_custom_model("foo", CustomModel)
        p1 = ModelCatalog.get_model({
            "obs": tf.constant([1, 2, 3])
        }, Box(0, 1, shape=(3, ), dtype=np.float32), Discrete(5), 5,
                                    {"custom_model": "foo"})
        self.assertEqual(str(type(p1)), str(CustomModel))

    def testCustomActionDistribution(self):
        class Model():
            pass

        ray.init()
        # registration
        ModelCatalog.register_custom_action_dist("test",
                                                 CustomActionDistribution)
        action_space = Box(0, 1, shape=(5, 3), dtype=np.float32)

        # test retrieving it
        model_config = MODEL_DEFAULTS.copy()
        model_config["custom_action_dist"] = "test"
        dist_cls, param_shape = ModelCatalog.get_action_dist(
            action_space, model_config)
        self.assertEqual(str(dist_cls), str(CustomActionDistribution))
        self.assertEqual(param_shape, action_space.shape)

        # test the class works as a distribution
        dist_input = tf.placeholder(tf.float32, (None, ) + param_shape)
        model = Model()
        model.model_config = model_config
        dist = dist_cls(dist_input, model=model)
        self.assertEqual(dist.sample().shape[1:], dist_input.shape[1:])
        self.assertIsInstance(dist.sample(), tf.Tensor)
        with self.assertRaises(NotImplementedError):
            dist.entropy()

        # test passing the options to it
        model_config["custom_options"].update({"output_dim": (3, )})
        dist_cls, param_shape = ModelCatalog.get_action_dist(
            action_space, model_config)
        self.assertEqual(param_shape, (3, ))
        dist_input = tf.placeholder(tf.float32, (None, ) + param_shape)
        model.model_config = model_config
        dist = dist_cls(dist_input, model=model)
        self.assertEqual(dist.sample().shape[1:], dist_input.shape[1:])
        self.assertIsInstance(dist.sample(), tf.Tensor)
        with self.assertRaises(NotImplementedError):
            dist.entropy()

if __name__ == "__main__":
    unittest.main(verbosity=1)
<|endoftext|>


<commit_before>"""Project metadata."""

__version__ = '0.3.0'

__title__ = 'discurses'
__summary__ = 'Discord CLI client written in Python, based on urwid.'
__uri__ = 'https://github.com/topisani/discurses'

__author__ = 'Topisani'
__email__ = 'topisani@hamsterpoison.com'

__license__ = 'MIT'
<commit_msg>Update to v0.3.1<commit_after>"""Project metadata."""

__version__ = '0.3.1'

__title__ = 'discurses'
__summary__ = 'Discord CLI client written in Python, based on urwid.'
__uri__ = 'https://github.com/topisani/discurses'

__author__ = 'Topisani'
__email__ = 'topisani@hamsterpoison.com'

__license__ = 'MIT'
<|endoftext|>


<commit_before># Copyright 2012 the Melange authors.
#
# 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/LICENSE2.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.

"""Module containing template with a list of GCIOrganization entities.
"""

from google.appengine.ext import db

from django.utils.dateformat import format
from django.utils.translation import ugettext

from soc.views.helper import addresses
from soc.views.helper import lists
from soc.views.helper.url import urlize
from soc.views.template import Template

from soc.modules.gci.models.profile import GCIStudentInfo
from soc.modules.gci.models.score import GCIOrgScore
from soc.modules.gci.views.helper import url_names

DATE_FORMAT = 'd-m-Y'

class StudentList(Template):
  """Component for listing all the students in GCI.
  """

  class ListPrefetcher(lists.Prefetcher):
    """Prefetcher for StudentList.

    See lists.Prefetcher for specification.
    """

    def prefetch(self, entities):
      """Prefetches GCIProfiles corresponding to the specified list of
      GCIStudentInfo entities.

      See lists.Prefetcher.prefetch for specification.

      Args:
        entities: the specified list of GCIStudentInfo instances

      Returns:
        prefetched GCIProfile entities in a structure whose format is
        described in lists.Prefetcher.prefetch
      """
      keys = []

      for entity in entities:
        key = entity.parent_key()
        if key:
          keys.append(key)

      entities = db.get(keys)
      sp = dict((i.key(), i) for i in entities if i)

      return [sp], {}

  def __init__(self, data):
    self.data = data
    self.idx = 1

    list_config = lists.ListConfiguration(add_key_column=False)
    list_config.addPlainTextColumn('key', 'Key', (lambda ent, *args: "%s" % (
        ent.parent().key().id_or_name())), hidden=True)

    list_config.setRowAction(
        lambda e, sp, *args: data.redirect.profile(
            sp[e.parent_key()].link_id).urlOf(url_names.GCI_STUDENT_TASKS))

    list_config.addPlainTextColumn(
        'name', 'Name', lambda e, sp, *args: sp[e.parent_key()].name())
    list_config.addPlainTextColumn(
        'link_id', 'Username', lambda e, sp, *args: sp[e.parent_key()].link_id)
    list_config.addPlainTextColumn(
        'email', 'Email', lambda e, sp, *args: sp[e.parent_key()].email)
    list_config.addPlainTextColumn('given_name', 'Given name',
        (lambda e, sp, *args: sp[e.parent_key()].given_name), hidden=True)
    list_config.addPlainTextColumn('surname', 'Surname',
        (lambda e, sp, *args: sp[e.parent_key()].surname), hidden=True)
    list_config.addPlainTextColumn('name_on_documents', 'Legal name',
        (lambda e, sp, *args: sp[e.parent_key()].name_on_documents),
        hidden=True)
    list_config.addPlainTextColumn(
        'birth_date', 'Birthdate',
        (lambda e, sp, *args: format(
            sp[e.parent_key()].birth_date, DATE_FORMAT)),
        hidden=True)
    list_config.addPlainTextColumn('gender', 'Gender',
        (lambda e, sp, *args: sp[e.parent_key()].gender))

    self._addAddressColumns(list_config)

    list_config.addPlainTextColumn('school_name', 'School name',
        (lambda e, sp, *args: sp[e.parent_key()].student_info.school_name),
        hidden=True)
    list_config.addPlainTextColumn('school_country', 'School Country',
        (lambda e, sp, *args: sp[e.parent_key()].student_info.school_country),
        hidden=True)
    list_config.addPlainTextColumn('school_type', 'School Type',
        (lambda e, sp, *args: sp[e.parent_key()].student_info.school_type),
        hidden=True)
    list_config.addPlainTextColumn('major', 'Major',
        (lambda e, sp, *args: sp[e.parent_key()].student_info.major),
        hidden=True)
    list_config.addPlainTextColumn('degree', 'Degree',
        (lambda e, sp, *args: sp[e.parent_key()].student_info.degree),
        hidden=True)
    list_config.addPlainTextColumn('grade', 'Grade',
        (lambda e, sp, *args: sp[e.parent_key()].student_info.grade),
        hidden=True)
    list_config.addPlainTextColumn('expected_graduation', 'Expected Graduation',
        (lambda e, sp, *args: sp[e.parent_key()].student_info.expected_graduation),
        hidden=True)

    list_config.addNumericalColumn('completed_tasks', 'Completed tasks',
        lambda e, *args: e.number_of_completed_tasks)

    def formsSubmitted(e, sp, form):
      """Returns "Yes" if form has been submitted otherwise "No".

      form takes either 'consent' or 'student_id' as values which stand
      for parental consent form and student id form respectively.
      """
      info = sp[e.parent_key()].student_info
      if form == 'consent':
        consent = GCIStudentInfo.consent_form.get_value_for_datastore(info)
        if consent:
          return 'Yes'
      if form == 'student_id':
        student_id = GCIStudentInfo.student_id_form.get_value_for_datastore(info)
        if student_id:
          return 'Yes'
      return 'No'

    list_config.addPlainTextColumn(
        'consent_form', 'Consent Form Submitted',
        (lambda e, sp, *args: formsSubmitted(e, sp, 'consent')))
    list_config.addPlainTextColumn(
        'student_id_form', 'Student ID Form Submitted',
        (lambda e, sp, *args: formsSubmitted(e, sp, 'student_id')))

    list_config.addPlainTextColumn('im_network', 'IM Network',
        (lambda e, sp, *args: sp[e.parent_key()].im_network),
        hidden=True)
    list_config.addPlainTextColumn('im_handle', 'IM Handle',
        (lambda e, sp, *args: sp[e.parent_key()].im_handle),
        hidden=True)
    list_config.addPlainTextColumn('home_page', 'Home Page',
        (lambda e, sp, *args: sp[e.parent_key()].home_page),
        hidden=True)
    list_config.addPlainTextColumn('blog', 'Blog',
        (lambda e, sp, *args: sp[e.parent_key()].blog),
        hidden=True)
    list_config.addPlainTextColumn('photo_url', 'Photo URL',
        (lambda e, sp, *args: urlize(sp[e.parent_key()].photo_url)),
        hidden=True)

    list_config.addPlainTextColumn('program_knowledge', 'Program Knowledge',
        (lambda e, sp, *args: sp[e.parent_key()].program_knowledge),
        hidden=True)

    list_config.addPlainTextColumn('created_on', 'Profile Created On',
        (lambda e, sp, *args: format(
            sp[e.parent_key()].created_on, DATE_FORMAT)),
        hidden=True)
    list_config.addPlainTextColumn('modified_on', 'Last Modified On',
        (lambda e, sp, *args: format(
            sp[e.parent_key()].modified_on, DATE_FORMAT)),
        hidden=True)

    self._list_config = list_config

  def getListData(self):
    idx = lists.getListIndex(self.data.request)

    if idx != self.idx:
      return None

    query = GCIStudentInfo.all()
    query.filter('program', self.data.program)

    starter = lists.keyStarter
    prefetcher = StudentList.ListPrefetcher()

    response_builder = lists.RawQueryContentResponseBuilder(
        self.data.request, self._list_config, query, starter,
        prefetcher=prefetcher)

    return response_builder.build()

  def templatePath(self):
    return'v2/modules/gci/students_info/_students_list.html'

  def context(self):
    list = lists.ListConfigurationResponse(
        self.data, self._list_config, idx=self.idx)

    return {
        'name': 'students',
        'title': 'Participating students',
        'lists': [list],
        'description': ugettext(
            'List of participating students'),
    }

  def _addAddressColumns(self, list_config):
    """Adds address columns to the specified list config.

    Columns added:
      * res_street
      * res_street_extra
      * res_city
      * res_state
      * res_country
      * res_postalcode
      * phone
      * ship_name
      * ship_street
      * ship_street_extra
      * ship_city
      * ship_state
      * ship_country
      * ship_postalcode
      * tshirt_style
      * tshirt_size
    """
    list_config.addPlainTextColumn('res_street', 'Street',
        (lambda e, sp, *args: sp[e.parent_key()].res_street), hidden=True)
    list_config.addPlainTextColumn('res_street_extra', 'Street Extra',
        (lambda e, sp, *args: sp[e.parent_key()].res_street_extra), hidden=True)
    list_config.addPlainTextColumn('res_city', 'City',
        (lambda e, sp, *args: sp[e.parent_key()].res_city), hidden=True)
    list_config.addPlainTextColumn('res_state', 'State',
        (lambda e, sp, *args: sp[e.parent_key()].res_state), hidden=True)
    list_config.addPlainTextColumn('res_country', 'Country',
        (lambda e, sp, *args: sp[e.parent_key()].res_country), hidden=True)
    list_config.addPlainTextColumn('res_postalcode', 'Postalcode',
        (lambda e, sp, *args: sp[e.parent_key()].res_postalcode), hidden=True)
    list_config.addPlainTextColumn('phone', 'Phone',
        (lambda e, sp, *args: sp[e.parent_key()].phone), hidden=True)
    list_config.addPlainTextColumn('ship_name', 'Ship Name',
        (lambda e, sp, *args: sp[e.parent_key()].shipping_name()), hidden=True)
    list_config.addPlainTextColumn('ship_street', 'Ship Street',
        (lambda e, sp, *args: sp[e.parent_key()].shipping_street()), hidden=True)
    list_config.addPlainTextColumn('ship_street_extra', 'Ship Street Extra',
        (lambda e, sp, *args: sp[e.parent_key()].shipping_street_extra()),
        hidden=True)
    list_config.addPlainTextColumn('ship_city', 'Ship City',
        (lambda e, sp, *args: sp[e.parent_key()].shipping_city()), hidden=True)
    list_config.addPlainTextColumn('ship_state', 'Ship State',
        (lambda e, sp, *args: sp[e.parent_key()].shipping_state()), hidden=True)
    list_config.addPlainTextColumn('ship_country', 'Ship Country',
        (lambda e, sp, *args: sp[e.parent_key()].shipping_country()), hidden=True)
    list_config.addPlainTextColumn('ship_postalcode', 'Ship Postalcode',
        (lambda e, sp, *args: sp[e.parent_key()].shipping_postalcode()),
        hidden=True)
    list_config.addPlainTextColumn('tshirt_style', 'T-Shirt Style',
        (lambda e, sp, *args: sp[e.parent_key()].tshirt_style), hidden=True)
    list_config.addPlainTextColumn('tshirt_size', 'T-Shirt Size',
        (lambda e, sp, *args: sp[e.parent_key()].tshirt_size), hidden=True)
<commit_msg>More descriptive names used in prefetch() function of StudentList.<commit_after># Copyright 2012 the Melange authors.
#
# 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/LICENSE2.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.

"""Module containing template with a list of GCIOrganization entities.
"""

from google.appengine.ext import db

from django.utils.dateformat import format
from django.utils.translation import ugettext

from soc.views.helper import addresses
from soc.views.helper import lists
from soc.views.helper.url import urlize
from soc.views.template import Template

from soc.modules.gci.models.profile import GCIStudentInfo
from soc.modules.gci.models.score import GCIOrgScore
from soc.modules.gci.views.helper import url_names

DATE_FORMAT = 'd-m-Y'

class StudentList(Template):
  """Component for listing all the students in GCI.
  """

  class ListPrefetcher(lists.Prefetcher):
    """Prefetcher for StudentList.

    See lists.Prefetcher for specification.
    """

    def prefetch(self, entities):
      """Prefetches GCIProfiles corresponding to the specified list of
      GCIStudentInfo entities.

      See lists.Prefetcher.prefetch for specification.

      Args:
        entities: the specified list of GCIStudentInfo instances

      Returns:
        prefetched GCIProfile entities in a structure whose format is
        described in lists.Prefetcher.prefetch
      """
      keys = []

      for entity in entities:
        key = entity.parent_key()
        if key:
          keys.append(key)

      profiles = db.get(keys)

      return (
          [dict((profile.key(), profile) for profile in profiles if profile)],
          {})

  def __init__(self, data):
    self.data = data
    self.idx = 1

    list_config = lists.ListConfiguration(add_key_column=False)
    list_config.addPlainTextColumn('key', 'Key', (lambda ent, *args: "%s" % (
        ent.parent().key().id_or_name())), hidden=True)

    list_config.setRowAction(
        lambda e, sp, *args: data.redirect.profile(
            sp[e.parent_key()].link_id).urlOf(url_names.GCI_STUDENT_TASKS))

    list_config.addPlainTextColumn(
        'name', 'Name', lambda e, sp, *args: sp[e.parent_key()].name())
    list_config.addPlainTextColumn(
        'link_id', 'Username', lambda e, sp, *args: sp[e.parent_key()].link_id)
    list_config.addPlainTextColumn(
        'email', 'Email', lambda e, sp, *args: sp[e.parent_key()].email)
    list_config.addPlainTextColumn('given_name', 'Given name',
        (lambda e, sp, *args: sp[e.parent_key()].given_name), hidden=True)
    list_config.addPlainTextColumn('surname', 'Surname',
        (lambda e, sp, *args: sp[e.parent_key()].surname), hidden=True)
    list_config.addPlainTextColumn('name_on_documents', 'Legal name',
        (lambda e, sp, *args: sp[e.parent_key()].name_on_documents),
        hidden=True)
    list_config.addPlainTextColumn(
        'birth_date', 'Birthdate',
        (lambda e, sp, *args: format(
            sp[e.parent_key()].birth_date, DATE_FORMAT)),
        hidden=True)
    list_config.addPlainTextColumn('gender', 'Gender',
        (lambda e, sp, *args: sp[e.parent_key()].gender))

    self._addAddressColumns(list_config)

    list_config.addPlainTextColumn('school_name', 'School name',
        (lambda e, sp, *args: sp[e.parent_key()].student_info.school_name),
        hidden=True)
    list_config.addPlainTextColumn('school_country', 'School Country',
        (lambda e, sp, *args: sp[e.parent_key()].student_info.school_country),
        hidden=True)
    list_config.addPlainTextColumn('school_type', 'School Type',
        (lambda e, sp, *args: sp[e.parent_key()].student_info.school_type),
        hidden=True)
    list_config.addPlainTextColumn('major', 'Major',
        (lambda e, sp, *args: sp[e.parent_key()].student_info.major),
        hidden=True)
    list_config.addPlainTextColumn('degree', 'Degree',
        (lambda e, sp, *args: sp[e.parent_key()].student_info.degree),
        hidden=True)
    list_config.addPlainTextColumn('grade', 'Grade',
        (lambda e, sp, *args: sp[e.parent_key()].student_info.grade),
        hidden=True)
    list_config.addPlainTextColumn('expected_graduation', 'Expected Graduation',
        (lambda e, sp, *args: sp[e.parent_key()].student_info.expected_graduation),
        hidden=True)

    list_config.addNumericalColumn('completed_tasks', 'Completed tasks',
        lambda e, *args: e.number_of_completed_tasks)

    def formsSubmitted(e, sp, form):
      """Returns "Yes" if form has been submitted otherwise "No".

      form takes either 'consent' or 'student_id' as values which stand
      for parental consent form and student id form respectively.
      """
      info = sp[e.parent_key()].student_info
      if form == 'consent':
        consent = GCIStudentInfo.consent_form.get_value_for_datastore(info)
        if consent:
          return 'Yes'
      if form == 'student_id':
        student_id = GCIStudentInfo.student_id_form.get_value_for_datastore(info)
        if student_id:
          return 'Yes'
      return 'No'

    list_config.addPlainTextColumn(
        'consent_form', 'Consent Form Submitted',
        (lambda e, sp, *args: formsSubmitted(e, sp, 'consent')))
    list_config.addPlainTextColumn(
        'student_id_form', 'Student ID Form Submitted',
        (lambda e, sp, *args: formsSubmitted(e, sp, 'student_id')))

    list_config.addPlainTextColumn('im_network', 'IM Network',
        (lambda e, sp, *args: sp[e.parent_key()].im_network),
        hidden=True)
    list_config.addPlainTextColumn('im_handle', 'IM Handle',
        (lambda e, sp, *args: sp[e.parent_key()].im_handle),
        hidden=True)
    list_config.addPlainTextColumn('home_page', 'Home Page',
        (lambda e, sp, *args: sp[e.parent_key()].home_page),
        hidden=True)
    list_config.addPlainTextColumn('blog', 'Blog',
        (lambda e, sp, *args: sp[e.parent_key()].blog),
        hidden=True)
    list_config.addPlainTextColumn('photo_url', 'Photo URL',
        (lambda e, sp, *args: urlize(sp[e.parent_key()].photo_url)),
        hidden=True)

    list_config.addPlainTextColumn('program_knowledge', 'Program Knowledge',
        (lambda e, sp, *args: sp[e.parent_key()].program_knowledge),
        hidden=True)

    list_config.addPlainTextColumn('created_on', 'Profile Created On',
        (lambda e, sp, *args: format(
            sp[e.parent_key()].created_on, DATE_FORMAT)),
        hidden=True)
    list_config.addPlainTextColumn('modified_on', 'Last Modified On',
        (lambda e, sp, *args: format(
            sp[e.parent_key()].modified_on, DATE_FORMAT)),
        hidden=True)

    self._list_config = list_config

  def getListData(self):
    idx = lists.getListIndex(self.data.request)

    if idx != self.idx:
      return None

    query = GCIStudentInfo.all()
    query.filter('program', self.data.program)

    starter = lists.keyStarter
    prefetcher = StudentList.ListPrefetcher()

    response_builder = lists.RawQueryContentResponseBuilder(
        self.data.request, self._list_config, query, starter,
        prefetcher=prefetcher)

    return response_builder.build()

  def templatePath(self):
    return'v2/modules/gci/students_info/_students_list.html'

  def context(self):
    list = lists.ListConfigurationResponse(
        self.data, self._list_config, idx=self.idx)

    return {
        'name': 'students',
        'title': 'Participating students',
        'lists': [list],
        'description': ugettext(
            'List of participating students'),
    }

  def _addAddressColumns(self, list_config):
    """Adds address columns to the specified list config.

    Columns added:
      * res_street
      * res_street_extra
      * res_city
      * res_state
      * res_country
      * res_postalcode
      * phone
      * ship_name
      * ship_street
      * ship_street_extra
      * ship_city
      * ship_state
      * ship_country
      * ship_postalcode
      * tshirt_style
      * tshirt_size
    """
    list_config.addPlainTextColumn('res_street', 'Street',
        (lambda e, sp, *args: sp[e.parent_key()].res_street), hidden=True)
    list_config.addPlainTextColumn('res_street_extra', 'Street Extra',
        (lambda e, sp, *args: sp[e.parent_key()].res_street_extra), hidden=True)
    list_config.addPlainTextColumn('res_city', 'City',
        (lambda e, sp, *args: sp[e.parent_key()].res_city), hidden=True)
    list_config.addPlainTextColumn('res_state', 'State',
        (lambda e, sp, *args: sp[e.parent_key()].res_state), hidden=True)
    list_config.addPlainTextColumn('res_country', 'Country',
        (lambda e, sp, *args: sp[e.parent_key()].res_country), hidden=True)
    list_config.addPlainTextColumn('res_postalcode', 'Postalcode',
        (lambda e, sp, *args: sp[e.parent_key()].res_postalcode), hidden=True)
    list_config.addPlainTextColumn('phone', 'Phone',
        (lambda e, sp, *args: sp[e.parent_key()].phone), hidden=True)
    list_config.addPlainTextColumn('ship_name', 'Ship Name',
        (lambda e, sp, *args: sp[e.parent_key()].shipping_name()), hidden=True)
    list_config.addPlainTextColumn('ship_street', 'Ship Street',
        (lambda e, sp, *args: sp[e.parent_key()].shipping_street()), hidden=True)
    list_config.addPlainTextColumn('ship_street_extra', 'Ship Street Extra',
        (lambda e, sp, *args: sp[e.parent_key()].shipping_street_extra()),
        hidden=True)
    list_config.addPlainTextColumn('ship_city', 'Ship City',
        (lambda e, sp, *args: sp[e.parent_key()].shipping_city()), hidden=True)
    list_config.addPlainTextColumn('ship_state', 'Ship State',
        (lambda e, sp, *args: sp[e.parent_key()].shipping_state()), hidden=True)
    list_config.addPlainTextColumn('ship_country', 'Ship Country',
        (lambda e, sp, *args: sp[e.parent_key()].shipping_country()), hidden=True)
    list_config.addPlainTextColumn('ship_postalcode', 'Ship Postalcode',
        (lambda e, sp, *args: sp[e.parent_key()].shipping_postalcode()),
        hidden=True)
    list_config.addPlainTextColumn('tshirt_style', 'T-Shirt Style',
        (lambda e, sp, *args: sp[e.parent_key()].tshirt_style), hidden=True)
    list_config.addPlainTextColumn('tshirt_size', 'T-Shirt Size',
        (lambda e, sp, *args: sp[e.parent_key()].tshirt_size), hidden=True)
<|endoftext|>


<commit_before>"""
WSGI config for ProyectoDAI project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/1.7/howto/deployment/wsgi/
"""

import os
from django.core.wsgi import get_wsgi_application
from dj_static import Cling
from whitenoise.django import DjangoWhiteNoise

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ProyectoDAI.settings")

application = get_wsgi_application()
#application = Cling(get_wsgi_application())

application = DjangoWhiteNoise(application)

<commit_msg>heroku<commit_after>"""
WSGI config for ProyectoDAI project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/1.7/howto/deployment/wsgi/
"""

import os
from django.core.wsgi import get_wsgi_application
from dj_static import Cling
#from whitenoise.django import DjangoWhiteNoise

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ProyectoDAI.settings")

application = get_wsgi_application()
application = Cling(get_wsgi_application())

#application = DjangoWhiteNoise(application)

<|endoftext|>


<commit_before>
__author__    = "Ole Weidner"
__copyright__ = "Copyright 2012-2013, The SAGA Project"
__license__   = "MIT"

""" This examples shows how to run a job on a remote PBS/TORQUE cluster
    using the 'PBS' job adaptor.

    More information about the saga-python job API can be found at:
    http://saga-project.github.com/saga-python/doc/library/job/index.html
"""

import sys
import saga

# ----------------------------------------------------------------------------
# This is an example for a callback function. Callback functions can be
# registered with a saga.Job object and get 'fired' asynchronously on
# certain conditions.
def job_state_change_cb(src_obj, fire_on, value):
    print "Callback    : job state changed to '%s'\n" % value
    return True

# ----------------------------------------------------------------------------
#
def main():

    try:
        # Your ssh identity on the remote machine.
        ctx = saga.Context("ssh")

        # Change e.g., if you have a differnent username on the remote machine
        #ctx.user_id = "your_ssh_username"

        session = saga.Session()
        session.add_context(ctx)

        # Create a job service object that represent a remote pbs cluster.
        # The keyword 'pbs' in the url scheme triggers the PBS adaptors
        # and '+ssh' enables PBS remote access via SSH.
        js = saga.job.Service("pbs+ssh://hotel.futuregrid.org",
                              session=session)

        # Next, we describe the job we want to run. A complete set of job
        # description attributes can be found in the API documentation.
        jd = saga.job.Description()
        jd.environment       = {'FILENAME': 'testfile'}
        jd.wall_time_limit   = 1 # minutes

        jd.executable        = '/bin/touch'
        jd.arguments         = ['$FILENAME']

        #jd.total_cpu_count   = 12 # for lonestar this has to be a multiple of 12
        #jd.spmd_variation    = '12way' # translates to the qsub -pe flag

        jd.queue             = "batch"
        #jd.project           = "TG-MCB090174"

        jd.working_directory = "$HOME/A/B/C"
        jd.output            = "examplejob.out"
        jd.error             = "examplejob.err"
        jd.spmd_variation    = 's'

        # Create a new job from the job description. The initial state of
        # the job is 'New'.
        touchjob = js.create_job(jd)

        # Register our callback. We want it to 'fire' on job state change
        touchjob.add_callback(saga.STATE, job_state_change_cb)

        # Check our job's id and state
        print "Job ID      : %s" % (touchjob.id)
        print "Job State   : %s" % (touchjob.state)

        # Now we can start our job.
        print "\n...starting job...\n"
        touchjob.run()

        print "Job ID      : %s" % (touchjob.id)

        # List all jobs that are known by the adaptor.
        # This should show our job as well.
        print "\nListing active jobs: "
        for job in js.list():
            print " * %s" % job

        # wait for our job to complete
        print "\n...waiting for job...\n"
        touchjob.wait()

        print "Job State   : %s" % (touchjob.state)
        print "Exitcode    : %s" % (touchjob.exit_code)
        print "Exec. hosts : %s" % (touchjob.execution_hosts)
        print "Create time : %s" % (touchjob.created)
        print "Start time  : %s" % (touchjob.started)
        print "End time    : %s" % (touchjob.finished)

        return 0

    except saga.SagaException, ex:
        # Catch all saga exceptions
        print "An exception occured: (%s) %s " % (ex.type, (str(ex)))
        # Get the whole traceback in case of an exception -
        # this can be helpful for debugging the problem
        print " \n*** Backtrace:\n %s" % ex.traceback
        return -1

if __name__ == "__main__":
    sys.exit(main())
<commit_msg>small change<commit_after>
__author__    = "Ole Weidner"
__copyright__ = "Copyright 2012-2013, The SAGA Project"
__license__   = "MIT"

""" This examples shows how to run a job on a remote PBS/TORQUE cluster
    using the 'PBS' job adaptor.

    More information about the saga-python job API can be found at:
    http://saga-project.github.com/saga-python/doc/library/job/index.html
"""

import sys
import saga

# ----------------------------------------------------------------------------
# This is an example for a callback function. Callback functions can be
# registered with a saga.Job object and get 'fired' asynchronously on
# certain conditions.
def job_state_change_cb(src_obj, fire_on, value):
    print "Callback    : job state changed to '%s'\n" % value
    return True

# ----------------------------------------------------------------------------
#
def main():

    try:
        # Your ssh identity on the remote machine.
        ctx = saga.Context("ssh")

        # Change e.g., if you have a differnent username on the remote machine
        #ctx.user_id = "your_ssh_username"

        session = saga.Session()
        session.add_context(ctx)

        # Create a job service object that represent a remote pbs cluster.
        # The keyword 'pbs' in the url scheme triggers the PBS adaptors
        # and '+ssh' enables PBS remote access via SSH.
        js = saga.job.Service("pbs+ssh://hotel.futuregrid.org",
                              session=session)

        # Next, we describe the job we want to run. A complete set of job
        # description attributes can be found in the API documentation.
        jd = saga.job.Description()
        jd.environment       = {'FILENAME': 'testfile'}
        jd.wall_time_limit   = 1 # minutes

        jd.executable        = '/bin/touch'
        jd.arguments         = ['$FILENAME']

        #jd.total_cpu_count   = 12 # for lonestar this has to be a multiple of 12
        #jd.spmd_variation    = '12way' # translates to the qsub -pe flag

        jd.queue             = "batch"
        #jd.project           = "TG-MCB090174"

        jd.working_directory = "$HOME/A/B/C"
        jd.output            = "examplejob.out"
        jd.error             = "examplejob.err"

        # Create a new job from the job description. The initial state of
        # the job is 'New'.
        touchjob = js.create_job(jd)

        # Register our callback. We want it to 'fire' on job state change
        touchjob.add_callback(saga.STATE, job_state_change_cb)

        # Check our job's id and state
        print "Job ID      : %s" % (touchjob.id)
        print "Job State   : %s" % (touchjob.state)

        # Now we can start our job.
        print "\n...starting job...\n"
        touchjob.run()

        print "Job ID      : %s" % (touchjob.id)

        # List all jobs that are known by the adaptor.
        # This should show our job as well.
        print "\nListing active jobs: "
        for job in js.list():
            print " * %s" % job

        # wait for our job to complete
        print "\n...waiting for job...\n"
        touchjob.wait()

        print "Job State   : %s" % (touchjob.state)
        print "Exitcode    : %s" % (touchjob.exit_code)
        print "Exec. hosts : %s" % (touchjob.execution_hosts)
        print "Create time : %s" % (touchjob.created)
        print "Start time  : %s" % (touchjob.started)
        print "End time    : %s" % (touchjob.finished)

        return 0

    except saga.SagaException, ex:
        # Catch all saga exceptions
        print "An exception occured: (%s) %s " % (ex.type, (str(ex)))
        # Get the whole traceback in case of an exception -
        # this can be helpful for debugging the problem
        print " \n*** Backtrace:\n %s" % ex.traceback
        return -1

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


<commit_before>##############################################################################
# Copyright (c) 2013-2017, Lawrence Livermore National Security, LLC.
# Produced at the Lawrence Livermore National Laboratory.
#
# This file is part of Spack.
# Created by Todd Gamblin, tgamblin@llnl.gov, All rights reserved.
# LLNL-CODE-647188
#
# For details, see https://github.com/llnl/spack
# Please also see the LICENSE file for our notice and the LGPL.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License (as
# published by the Free Software Foundation) version 2.1, February 1999.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms and
# conditions of the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##############################################################################
"""Check that Spack complies with minimum supported python versions.

We ensure that all Spack files work with Python2 >= 2.6 and Python3 >= 3.0.

We'd like to drop 2.6 support at some point, but there are still many HPC
systems that ship with RHEL6/CentOS 6, which have Python 2.6 as the
default version.  Once those go away, we can likely drop 2.6 and increase
the minimum supported Python 3 version, as well.
"""
from __future__ import print_function

import os
import sys
import re
import unittest

import llnl.util.tty as tty
import spack

#
# This test uses pyqver, by Greg Hewgill, which is a dual-source module.
# That means we need to do different checks depending on whether we're
# running Python 2 or Python 3.
#
if sys.version_info[0] < 3:
    import pyqver2 as pyqver
    spack_min_supported = (2, 6)

    # Exclude Python 3 versions of dual-source modules when using Python 2
    exclude_paths = [
        os.path.join(spack.lib_path, 'external', 'yaml', 'lib3'),
        os.path.join(spack.lib_path, 'external', 'pyqver3.py')]

else:
    import pyqver3 as pyqver
    spack_min_supported = (3, 0)

    # Exclude Python 2 versions of dual-source modules when using Python 3
    exclude_paths = [
        os.path.join(spack.lib_path, 'external', 'yaml', 'lib'),
        os.path.join(spack.lib_path, 'external', 'pyqver2.py')]

class PythonVersionTest(unittest.TestCase):

    def pyfiles(self, search_paths, exclude=()):
        """List python search files in a set of search paths, excluding
        any paths in the exclude list"""
        # first file is the spack script.
        yield spack.spack_file

        # Iterate through the whole spack source tree.
        for path in search_paths:
            for root, dirnames, filenames in os.walk(path):
                for filename in filenames:
                    realpath = os.path.realpath(os.path.join(root, filename))
                    if any(realpath.startswith(p) for p in exclude):
                        continue

                    if re.match(r'^[^.#].*\.py$', filename):
                        yield os.path.join(root, filename)

    def check_python_versions(self, files):
        # This is a dict dict mapping:
        #   version -> filename -> reasons
        #
        # Reasons are tuples of (lineno, string), where the string is the
        # cause for a version incompatibility.
        all_issues = {}

        # Parse files and run pyqver on each file.
        for path in files:
            with open(path) as pyfile:
                full_text = pyfile.read()
            versions = pyqver.get_versions(full_text, path)

            for ver, reasons in versions.items():
                if ver <= spack_min_supported:
                    continue

                # Record issues. Mark exceptions with '# nopyqver' comment
                for lineno, cause in reasons:
                    lines = full_text.split('\n')
                    if not re.search(r'#\s*nopyqver\s*$', lines[lineno - 1]):
                        all_issues.setdefault(ver, {})[path] = reasons

        # Print a message if there are are issues
        if all_issues:
            tty.msg("Spack must remain compatible with Python version %d.%d"
                    % spack_min_supported)

        # Print out a table showing which files/linenos require which
        # python version, and a string describing why.
        for v in sorted(all_issues.keys(), reverse=True):
            messages = []
            for path in sorted(all_issues[v].keys()):
                short_path = path
                if path.startswith(spack.prefix):
                    short_path = path[len(spack.prefix):]

                reasons = [r for r in set(all_issues[v][path]) if r]
                for lineno, cause in reasons:
                    file_line = "%s:%s" % (short_path.lstrip('/'), lineno)
                    messages.append((file_line, cause))

            print()
            tty.msg("These files require version %d.%d:" % v)
            maxlen = max(len(f) for f, prob in messages)
            fmt = "%%-%ds%%s" % (maxlen + 3)
            print(fmt % ('File', 'Reason'))
            print(fmt % ('-' * (maxlen), '-' * 20))
            for msg in messages:
                print(fmt % msg)

        # Fail this test if there were issues.
        self.assertTrue(len(all_issues) == 0)

    def test_core_module_compatibility(self):
        self.check_python_versions(
            self.pyfiles([spack.lib_path], exclude=exclude_paths))

    def test_package_module_compatibility(self):
        self.check_python_versions(self.pyfiles([spack.packages_path]))
<commit_msg>test/python_version.py: ported to pytest (#3438)<commit_after>##############################################################################
# Copyright (c) 2013-2017, Lawrence Livermore National Security, LLC.
# Produced at the Lawrence Livermore National Laboratory.
#
# This file is part of Spack.
# Created by Todd Gamblin, tgamblin@llnl.gov, All rights reserved.
# LLNL-CODE-647188
#
# For details, see https://github.com/llnl/spack
# Please also see the LICENSE file for our notice and the LGPL.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License (as
# published by the Free Software Foundation) version 2.1, February 1999.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms and
# conditions of the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##############################################################################
"""Check that Spack complies with minimum supported python versions.

We ensure that all Spack files work with Python2 >= 2.6 and Python3 >= 3.0.

We'd like to drop 2.6 support at some point, but there are still many HPC
systems that ship with RHEL6/CentOS 6, which have Python 2.6 as the
default version.  Once those go away, we can likely drop 2.6 and increase
the minimum supported Python 3 version, as well.
"""
from __future__ import print_function

import os
import sys
import re

import llnl.util.tty as tty
import spack

#
# This test uses pyqver, by Greg Hewgill, which is a dual-source module.
# That means we need to do different checks depending on whether we're
# running Python 2 or Python 3.
#
if sys.version_info[0] < 3:
    import pyqver2 as pyqver
    spack_min_supported = (2, 6)

    # Exclude Python 3 versions of dual-source modules when using Python 2
    exclude_paths = [
        os.path.join(spack.lib_path, 'external', 'yaml', 'lib3'),
        os.path.join(spack.lib_path, 'external', 'pyqver3.py')]

else:
    import pyqver3 as pyqver
    spack_min_supported = (3, 0)

    # Exclude Python 2 versions of dual-source modules when using Python 3
    exclude_paths = [
        os.path.join(spack.lib_path, 'external', 'yaml', 'lib'),
        os.path.join(spack.lib_path, 'external', 'pyqver2.py')]

def pyfiles(search_paths, exclude=()):
    """Generator that yields all the python files in the search paths.

    :param search_paths: list of paths to search for python files
    :param exclude: file paths to exclude from search
    :return: python files
    """
    # first file is the spack script.
    yield spack.spack_file

    # Iterate through the whole spack source tree.
    for path in search_paths:
        for root, dirnames, filenames in os.walk(path):
            for filename in filenames:
                realpath = os.path.realpath(os.path.join(root, filename))
                if any(realpath.startswith(p) for p in exclude):
                    continue

                if re.match(r'^[^.#].*\.py$', filename):
                    yield os.path.join(root, filename)

def check_python_versions(files):
    """Check that a set of Python files works with supported Ptyhon versions"""
    # This is a dict dict mapping:
    #   version -> filename -> reasons
    #
    # Reasons are tuples of (lineno, string), where the string is the
    # cause for a version incompatibility.
    all_issues = {}

    # Parse files and run pyqver on each file.
    for path in files:
        with open(path) as pyfile:
            full_text = pyfile.read()
        versions = pyqver.get_versions(full_text, path)

        for ver, reasons in versions.items():
            if ver <= spack_min_supported:
                continue

            # Record issues. Mark exceptions with '# nopyqver' comment
            for lineno, cause in reasons:
                lines = full_text.split('\n')
                if not re.search(r'#\s*nopyqver\s*$', lines[lineno - 1]):
                    all_issues.setdefault(ver, {})[path] = reasons

    # Print a message if there are are issues
    if all_issues:
        tty.msg("Spack must remain compatible with Python version %d.%d"
                % spack_min_supported)

    # Print out a table showing which files/linenos require which
    # python version, and a string describing why.
    for v in sorted(all_issues.keys(), reverse=True):
        messages = []
        for path in sorted(all_issues[v].keys()):
            short_path = path
            if path.startswith(spack.prefix):
                short_path = path[len(spack.prefix):]

            reasons = [r for r in set(all_issues[v][path]) if r]
            for lineno, cause in reasons:
                file_line = "%s:%s" % (short_path.lstrip('/'), lineno)
                messages.append((file_line, cause))

        print()
        tty.msg("These files require version %d.%d:" % v)
        maxlen = max(len(f) for f, prob in messages)
        fmt = "%%-%ds%%s" % (maxlen + 3)
        print(fmt % ('File', 'Reason'))
        print(fmt % ('-' * (maxlen), '-' * 20))
        for msg in messages:
            print(fmt % msg)

    # Fail this test if there were issues.
    assert not all_issues

def test_core_module_compatibility():
    """Test that all core spack modules work with supported Python versions."""
    check_python_versions(pyfiles([spack.lib_path], exclude=exclude_paths))

def test_package_module_compatibility():
    """Test that all spack packages work with supported Python versions."""
    check_python_versions(pyfiles([spack.packages_path]))
<|endoftext|>


<commit_before>#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Interpreter version: python 2.7
#
# Imports =====================================================================
import os.path

from ZODB import DB
from ZODB.config import storageFromFile
from ZODB.POSException import ConnectionStateError

from BTrees.OOBTree import OOBTree

from ..settings import ZCONF_PATH
from ..settings import PROJECT_KEY

# Variables ===================================================================
_ROOT = None

# Functions & classes =========================================================
def get_zeo_connection(on_close_callback=None):
    path = os.path.join(ZCONF_PATH, "zeo_client.conf")
    db = DB(storageFromFile(open(path)))
    connection = db.open()

    if on_close_callback:
        connection.onCloseCallback(on_close_callback)

    return connection

def get_zeo_root(cached=True):
    global _ROOT
    if _ROOT and cached:
        return _ROOT

    def unset_root_cache():
        global _ROOT
        _ROOT = None

    connection = get_zeo_connection(on_close_callback=unset_root_cache)

    try:
        dbroot = connection.root()
    except ConnectionStateError:
        return get_zeo_root(cached=False)

    if PROJECT_KEY not in dbroot:
        dbroot[PROJECT_KEY] = OOBTree()

    _ROOT = dbroot[PROJECT_KEY]
    return _ROOT

def get_zeo_key(key, new_type=OOBTree):
    root = get_zeo_root()

    if not root.get(key, None):
        root[key] = new_type()

    return root[key]
<commit_msg>#10: Added docstrings for ZODB init.<commit_after>#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Interpreter version: python 2.7
#
# Imports =====================================================================
import os.path

from ZODB import DB
from ZODB.config import storageFromFile
from ZODB.POSException import ConnectionStateError

from BTrees.OOBTree import OOBTree

from ..settings import ZCONF_PATH
from ..settings import PROJECT_KEY

# Variables ===================================================================
_ROOT = None  #: cache for one connection for all calls to :meth:`get_zeo_key`

# Functions & classes =========================================================
def get_zeo_connection(on_close_callback=None):
    """
    Return connection to the database. You can get root of the database from
    this connection.

    Warning:
        Don't try to put one object into multiple connections. It won't work.

    Args:
        on_close_callback (fn pointer, default None): Function which should be
                          used when the connection is closed.

    Returns:
        obj: ZODB connection object.
    """
    path = os.path.join(ZCONF_PATH, "zeo_client.conf")
    db = DB(storageFromFile(open(path)))
    connection = db.open()

    if on_close_callback:
        connection.onCloseCallback(on_close_callback)

    return connection

def get_zeo_root(cached=True):
    """
    Return :attr:`.PROJECT_KEY` from the root of the database.

    Args:
        cached (bool, default True): Cache object. This will prevent nasty
               problems with putting same object into multiple connections.

    Returns:
        OOBTree: Project key from the root of the database.
    """
    global _ROOT
    if _ROOT and cached:
        return _ROOT

    def unset_root_cache():
        global _ROOT
        _ROOT = None

    connection = get_zeo_connection(on_close_callback=unset_root_cache)

    try:
        dbroot = connection.root()
    except ConnectionStateError:
        return get_zeo_root(cached=False)

    if PROJECT_KEY not in dbroot:
        dbroot[PROJECT_KEY] = OOBTree()

    _ROOT = dbroot[PROJECT_KEY]
    return _ROOT

def get_zeo_key(key, new_type=OOBTree):
    """
    Get key from the PROJECT_KEY root. Use `new_type` as the new type of the
    key, if not found.

    Args:
        key (str): Name of the key which will be returned from the root.
        new_type (obj, default OOBTree): Put `new_type` into key if the key
                 wasn't found.

    Returns:
        obj: Object at `key`. `new_type` instance if not found.
    """
    root = get_zeo_root()

    if not root.get(key, None):
        root[key] = new_type()

    return root[key]
<|endoftext|>


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

'''
Este es un script que integra el espectro del Sol
e integra la funcion de Planck
'''

import matplotlib.pyplot as plt
import numpy as np
from astropy import constants as cons
from astropy import units as u
from scipy import integrate as integrate
import time

#Lectura del archivo que contiene el espectro del Sol
D = np.loadtxt("sun_AM0.dat")
x = D[:,0]
y = D[:,1]

#Conversion de los datos a Flujo [cgs] y Longitud de onda [um]
xdato= x*u.nm
x_um= xdato.to('um')
ydato= y*u.W*(u.m**-2)*(u.nm**-1)
y_cgs= ydato.to('erg/(s cm2 um)')

#Plotear Flujo vs Longitud de onda
plt.clf()
plt.plot(x_um, y_cgs)
plt.xlim(0,7)
plt.xlabel('Longitud de onda [$ \mu m $]')
plt.ylabel('Flujo [$erg / s * cm^2 * \mu m$]')
plt.title('Grafico de Flujo versus Longitud de onda')
plt.savefig('Figura.png', bbox_inches='tight')
plt.show()

#Determinar tiempo de ejecucion Algoritmo Trapecio Propio
a= time.time()

#Integracion del espectro en longitud de onda usando el metodo del trapecio
#"sum" corresponde al valor de la integral del espectro (Constante solar)
#y "Ls" al valor de la Luminosidad total del sol
Ks=0
for i in range(1696):
    valor= ((y_cgs[i]+y_cgs[i+1])*(x_um[i+1] - x_um[i])) / 2
    Ks += valor
print ('Integral del espectro del Sol: CONSTANTE SOLAR')
print (Ks)

b= time.time() - a

r= cons.au #Distancia entre el Sol y la Tierra en m
rcm= r.to('cm')
Ls= 4*np.pi*(rcm*rcm)*Ks
print ('Luminosidad total del Sol')
print (Ls)

#Calculo del tiempo de ejecucion del Metodo de Simpson propio
c=time.time()
#Integracion de la funcion de Planck
#A partir de la ecuacion dada en la tarea, se realiza el cambio de
#variable y= arctan(x). Ahora calcularemos esta integral con el metodo de
#Simpson, pero usando una formula vista en clases que ocupa el metodo del
#Trapecio para llegar al metodo de Simpson

#Definir vectores x e y(x)
xin=0.01
xfi=3.14 / 2
x_pl= np.arange(xin, xfi, 0.01)
y_pl= (np.tan(x_pl)**3)/((np.cos(x_pl)**2)*(np.exp(np.tan(x_pl))-1))
elementos=len(x_pl)

#Iteracion Trapecio para n puntos (n=elementos del vector x_pl)
Tn=0
for j in range(elementos-1):
    ar= ((y_pl[j]+y_pl[j+1])*(x_pl[j+1] - x_pl[j])) / 2
    Tn += ar
print('Valor integral con Trapecio con n puntos')
print (Tn)

#Definir nuevos vectores x e y(x) con el doble de elementos
xinn=0.01
xfin=3.14 / 2
x_pln= np.arange(xin, xfi, 0.005)
y_pln= (np.tan(x_pln)**3)/((np.cos(x_pln)**2)*(np.exp(np.tan(x_pln))-1))
elementoss=len(x_pln)

#Iteracion Trapecio para 2n puntos
T_2n=0
for k in range(elementoss-1):
    inte= ((y_pln[k]+y_pln[k+1])*(x_pln[k+1] - x_pln[k])) / 2
    T_2n += inte
print ('Valor integral con Trapecio 2n puntos')
print (T_2n)

#Aplicar formula vista en clases para Simpson
S= (4*T_2n / 3) - (Tn)/3
print ('Valor integral con metodo de Simpson (formula vista en clases que utiliza Trapecio)')
print (S)

#Usamos el metodo del punto medio para calcular la integral cuando la
#funcion diverge, en este caso en 0
yin_medio= (np.tan(xin/2)**3)/((np.cos(xin/2)**2)*(np.exp(np.tan(xin/2))-1))
area_0=xin*yin_medio
print ('Valor de integral con metodo punto medio en x=0, debido a que es una divergencia')
print (area_0)

#Valor final de la integral
Sfinal= S+area_0
print ('Valor de la integral total: Simpson + Punto medio')
print (Sfinal)

#Valor de la integral de la funcion de Planck: que es constantes * Integral
t= 5778 * u.K
cplanck= ((2*np.pi*cons.h)* (((cons.k_B*t)/(cons.h))**4))/((cons.c)**2)
planck= cplanck * Sfinal
print ('Valor de integral de funcion de Planck: integral calculada * constantes . FLUJO DE ENERGIA ')
print(planck)

d=time.time() - c

#Conversion de la constante solar a J/m^2 * s
KsJ=Ks.to('J / (m2 s)')

#Calcular el radio del Sol con Formula que relaciona Constante Solar, el flujo
#de energia y la unidad astronomica
rSOL= ((KsJ*(cons.au**2))/planck)**0.5
print ('Valor del radio del Sol')
print (rSOL)

#Recalcular las integrales anteriores con los metodos predeterminados de python

#Valor del tiempo de ejecucion para trapz
e= time.time()

#Calculo integral del espectro con metodo de trapecio predeterminado
esp_trapz= np.trapz(y_cgs, x_um)
print ('Valor integral del espectro con metodo trapecio predeterminado')
print (esp_trapz)

f= time.time() - e

#Tiempo de ejecucion trapz
g= time.time()
#Calculo de integral de funcion de Planck con metodo de trapecio predeterminado
pln_trapz= np.trapz(y_pl, x_pl)
plntrapz= pln_trapz * cplanck
print ('Valor integral de Planck con metodo trapecio predeterminado')
print (plntrapz)

h= time.time() - g

#Tiempo de ejecucion quad
l= time.time()
#Calculo de la integral de funcion de Planck con quad
fu= lambda x: (x**3)/(np.exp(x) - 1)
pln_quad= integrate.quad( fu , 0, np.inf)
print ('Valor de la integral desde 0 a infinito de (x^3 / exp(x) - 1)')
print (pln_quad)

pln_quad1= pln_quad[0]
plnquad= pln_quad1 * cplanck
print ('Valor de la integral de la funcion de Planck con metodo predeterminado quad')
print (plnquad)

m= time.time() - l

#Calculo del tiempo de ejecucion
print ('Tiempo de ejecucion algoritmo propio de Trapecio para Integral del Espectro Solar')
print (b)
print ('Tiempo de ejecucion algoritmo propio Simpson para Integral Funcion de Planck ')
print (d)
print ('Tiempo de ejecucion trapz para Integral del Espectro Solar')
print (f)
print ('Tiempo de ejecucion trapz para Integral Funcion de Planck')
print (h)
print ('Tiempo de ejecucion quad para Integral Funcion de Planck')
print (m)
<commit_msg>Cambio de comentarios.<commit_after>#! /usr/bin/env python

'''
Este es un script que integra el espectro del Sol
e integra la funcion de Planck
'''

import matplotlib.pyplot as plt
import numpy as np
from astropy import constants as cons
from astropy import units as u
from scipy import integrate as integrate
import time

#Lectura del archivo que contiene el espectro del Sol
D = np.loadtxt("sun_AM0.dat")
x = D[:,0]
y = D[:,1]

#Conversion de los datos a Flujo [cgs] y Longitud de onda [um]
xdato= x*u.nm
x_um= xdato.to('um')
ydato= y*u.W*(u.m**-2)*(u.nm**-1)
y_cgs= ydato.to('erg/(s cm2 um)')

#Plotear Flujo vs Longitud de onda
plt.clf()
plt.plot(x_um, y_cgs)
plt.xlim(0,7)
plt.xlabel('Longitud de onda [$ \mu m $]')
plt.ylabel('Flujo [$erg / s * cm^2 * \mu m$]')
plt.title('Grafico de Flujo versus Longitud de onda')
plt.savefig('Figura.png', bbox_inches='tight')
plt.show()

#Determinar tiempo de ejecucion Algoritmo Trapecio Propio
a= time.time()

#Integracion del espectro en longitud de onda usando el metodo del trapecio
#"sum" corresponde al valor de la integral del espectro (Constante solar)
#y "Ls" al valor de la Luminosidad total del sol
Ks=0
for i in range(1696):
    valor= ((y_cgs[i]+y_cgs[i+1])*(x_um[i+1] - x_um[i])) / 2
    Ks += valor
print ('Integral del espectro del Sol: CONSTANTE SOLAR')
print (Ks)

b= time.time() - a

r= cons.au #Distancia entre el Sol y la Tierra en m
rcm= r.to('cm')
Ls= 4*np.pi*(rcm*rcm)*Ks
print ('Luminosidad total del Sol')
print (Ls)

#Calculo del tiempo de ejecucion del Metodo de Simpson propio
c=time.time()
#Integracion de la funcion de Planck
#A partir de la ecuacion dada en la tarea, se realiza el cambio de
#variable y= arctan(x). Ahora calcularemos esta integral con el metodo de
#Simpson, pero usando una formula vista en clases que ocupa el metodo del
#Trapecio para llegar al metodo de Simpson

#Definir vectores x e y(x)
xin=0.01
xfi=3.14 / 2
x_pl= np.arange(xin, xfi, 0.01)
y_pl= (np.tan(x_pl)**3)/((np.cos(x_pl)**2)*(np.exp(np.tan(x_pl))-1))
elementos=len(x_pl)

#Iteracion Trapecio para n puntos (n=elementos del vector x_pl)
Tn=0
for j in range(elementos-1):
    ar= ((y_pl[j]+y_pl[j+1])*(x_pl[j+1] - x_pl[j])) / 2
    Tn += ar
print('Valor integral con Trapecio con n puntos')
print (Tn)

#Definir nuevos vectores x e y(x) con el doble de elementos
xinn=0.01
xfin=3.14 / 2
x_pln= np.arange(xin, xfi, 0.005)
y_pln= (np.tan(x_pln)**3)/((np.cos(x_pln)**2)*(np.exp(np.tan(x_pln))-1))
elementoss=len(x_pln)

#Iteracion Trapecio para 2n puntos
T_2n=0
for k in range(elementoss-1):
    inte= ((y_pln[k]+y_pln[k+1])*(x_pln[k+1] - x_pln[k])) / 2
    T_2n += inte
print ('Valor integral con Trapecio 2n puntos')
print (T_2n)

#Aplicar formula vista en clases para Simpson
S= (4*T_2n / 3) - (Tn)/3
print ('Valor integral con metodo de Simpson (formula vista en clases que utiliza Trapecio)')
print (S)

#Usamos el metodo del punto medio para calcular la integral cuando la
#funcion diverge, en este caso en 0
yin_medio= (np.tan(xin/2)**3)/((np.cos(xin/2)**2)*(np.exp(np.tan(xin/2))-1))
area_0=xin*yin_medio
print ('Valor de integral con metodo punto medio en x=0, debido a que es una divergencia')
print (area_0)

#Valor final de la integral
Sfinal= S+area_0
print ('Valor de la integral total: Simpson + Punto medio')
print (Sfinal)

#Valor de la integral de la funcion de Planck: que es constantes * Integral
t= 5778 * u.K
cplanck= ((2*np.pi*cons.h)* (((cons.k_B*t)/(cons.h))**4))/((cons.c)**2)
planck= cplanck * Sfinal
print ('Valor de integral de funcion de Planck: integral calculada * constantes . FLUJO DE ENERGIA ')
print(planck)

d=time.time() - c

#Conversion de la constante solar a J/m^2 * s
KsJ=Ks.to('J / (m2 s)')

#Calcular el radio del Sol con Formula que relaciona Constante Solar, el flujo
#de energia y la unidad astronomica
rSOL= ((KsJ*(cons.au**2))/planck)**0.5
print ('Valor del radio del Sol')
print (rSOL)

#Recalcular las integrales anteriores con los metodos predeterminados de python

#Valor del tiempo de ejecucion para trapz
e= time.time()

#Calculo integral del espectro con metodo de trapecio predeterminado
esp_trapz= np.trapz(y_cgs, x_um)
print ('Valor integral del espectro con metodo trapecio predeterminado')
print (esp_trapz)

f= time.time() - e

#Tiempo de ejecucion trapz
g= time.time()
#Calculo de integral de funcion de Planck con metodo de trapecio predeterminado
pln_trapz= np.trapz(y_pl, x_pl)
plntrapz= pln_trapz * cplanck
print ('Valor integral de Planck con metodo trapecio predeterminado')
print (plntrapz)

h= time.time() - g

#Tiempo de ejecucion quad
l= time.time()
#Calculo de la integral de funcion de Planck con quad
fu= lambda x: (x**3)/(np.exp(x) - 1)
pln_quad= integrate.quad( fu , 0, np.inf)
print ('Valor de la integral desde 0 a infinito de (x^3 / exp(x) - 1)')
print (pln_quad)

pln_quad1= pln_quad[0]
plnquad= pln_quad1 * cplanck
print ('Valor de la integral de la funcion de Planck con metodo predeterminado quad')
print (plnquad)

m= time.time() - l

#Tiempos de ejecucion
print ('Tiempo de ejecucion algoritmo propio de Trapecio para Integral del Espectro Solar')
print (b)
print ('Tiempo de ejecucion algoritmo propio Simpson para Integral Funcion de Planck ')
print (d)
print ('Tiempo de ejecucion trapz para Integral del Espectro Solar')
print (f)
print ('Tiempo de ejecucion trapz para Integral Funcion de Planck')
print (h)
print ('Tiempo de ejecucion quad para Integral Funcion de Planck')
print (m)
<|endoftext|>


<commit_before>import unittest

import numpy

import chainer
from chainer import cuda
from chainer import functions
from chainer import gradient_check
from chainer import testing
from chainer.testing import attr

def _sigmoid(x):
    xp = cuda.get_array_module(x)
    return 1 / (1 + xp.exp(-x))

def _gru(func, h, x):
    xp = cuda.get_array_module(h, x)

    r = _sigmoid(func.W_r.forward((x,))[0] + func.U_r.forward((h,))[0])
    z = _sigmoid(func.W_z.forward((x,))[0] + func.U_z.forward((h,))[0])
    h_bar = xp.tanh(func.W.forward((x,))[0] + func.U.forward((r * h,))[0])
    y = (1 - z) * h + z * h_bar
    return y

class TestGRU(unittest.TestCase):

    def setUp(self):
        self.func = functions.GRU(8)
        self.x = numpy.random.uniform(-1, 1, (3, 8)).astype(numpy.float32)
        self.h = numpy.random.uniform(-1, 1, (3, 8)).astype(numpy.float32)
        self.gy = numpy.random.uniform(-1, 1, (3, 8)).astype(numpy.float32)

    def check_forward(self, h_data, x_data):
        h = chainer.Variable(h_data)
        x = chainer.Variable(x_data)
        y = self.func(h, x)
        self.assertEqual(y.data.dtype, numpy.float32)

        y_expect = _gru(self.func, h_data, x_data)
        gradient_check.assert_allclose(y_expect, y.data)

    def test_forward_cpu(self):
        self.check_forward(self.h, self.x)

    @attr.gpu
    def test_forward_gpu(self):
        self.func.to_gpu()
        self.check_forward(cuda.to_gpu(self.h),
                           cuda.to_gpu(self.x))

    def check_backward(self, h_data, x_data, y_grad):
        h = chainer.Variable(h_data)
        x = chainer.Variable(x_data)
        y = self.func(h, x)

        y.grad = y_grad
        y.backward()

        f = lambda: (_gru(self.func, h.data, x.data),)
        gh, gx = gradient_check.numerical_grad(f, (h.data, x.data), (y.grad,))

        gradient_check.assert_allclose(gh, h.grad, atol=1e-4)
        gradient_check.assert_allclose(gx, x.grad, atol=1e-4)

    def test_backward_cpu(self):
        self.check_backward(self.h, self.x, self.gy)

    @attr.gpu
    def test_backward_gpu(self):
        self.func.to_gpu()
        self.check_backward(cuda.to_gpu(self.h),
                            cuda.to_gpu(self.x),
                            cuda.to_gpu(self.gy))

testing.run_module(__name__, __file__)
<commit_msg>Change test condition for GRU<commit_after>import unittest

import numpy

import chainer
from chainer import cuda
from chainer import functions
from chainer import gradient_check
from chainer import testing
from chainer.testing import attr

def _sigmoid(x):
    xp = cuda.get_array_module(x)
    return 1 / (1 + xp.exp(-x))

def _gru(func, h, x):
    xp = cuda.get_array_module(h, x)

    r = _sigmoid(func.W_r.forward((x,))[0] + func.U_r.forward((h,))[0])
    z = _sigmoid(func.W_z.forward((x,))[0] + func.U_z.forward((h,))[0])
    h_bar = xp.tanh(func.W.forward((x,))[0] + func.U.forward((r * h,))[0])
    y = (1 - z) * h + z * h_bar
    return y

class TestGRU(unittest.TestCase):

    def setUp(self):
        self.func = functions.GRU(8)
        self.x = numpy.random.uniform(-1, 1, (3, 8)).astype(numpy.float32)
        self.h = numpy.random.uniform(-1, 1, (3, 8)).astype(numpy.float32)
        self.gy = numpy.random.uniform(-1, 1, (3, 8)).astype(numpy.float32)

    def check_forward(self, h_data, x_data):
        h = chainer.Variable(h_data)
        x = chainer.Variable(x_data)
        y = self.func(h, x)
        self.assertEqual(y.data.dtype, numpy.float32)

        y_expect = _gru(self.func, h_data, x_data)
        gradient_check.assert_allclose(y_expect, y.data)

    def test_forward_cpu(self):
        self.check_forward(self.h, self.x)

    @attr.gpu
    def test_forward_gpu(self):
        self.func.to_gpu()
        self.check_forward(cuda.to_gpu(self.h),
                           cuda.to_gpu(self.x))

    def check_backward(self, h_data, x_data, y_grad):
        h = chainer.Variable(h_data)
        x = chainer.Variable(x_data)
        y = self.func(h, x)

        y.grad = y_grad
        y.backward()

        f = lambda: (_gru(self.func, h.data, x.data),)
        gh, gx = gradient_check.numerical_grad(f, (h.data, x.data), (y.grad,))

        gradient_check.assert_allclose(gh, h.grad, atol=1e-3)
        gradient_check.assert_allclose(gx, x.grad, atol=1e-3)

    def test_backward_cpu(self):
        self.check_backward(self.h, self.x, self.gy)

    @attr.gpu
    def test_backward_gpu(self):
        self.func.to_gpu()
        self.check_backward(cuda.to_gpu(self.h),
                            cuda.to_gpu(self.x),
                            cuda.to_gpu(self.gy))

testing.run_module(__name__, __file__)
<|endoftext|>


<commit_before># Copyright 2013 the Melange authors.
#
# 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.

"""Module for slot allocation."""

import logging

from google.appengine.ext import db

from django import http
from django.utils import simplejson

from soc.logic import exceptions
from soc.views.helper import lists
from soc.views.helper import url_patterns

from soc.modules.gsoc.models import organization as org_model
from soc.modules.gsoc.models import proposal as proposal_model
from soc.modules.gsoc.templates import org_list
from soc.modules.gsoc.views import base
from soc.modules.gsoc.views.helper import url_patterns as gsoc_url_patterns

class SlotsList(org_list.OrgList):
  """Template for list of accepted organizations to allocate slots."""

  def _getDescription(self):
    """See org_list.OrgList._getDescription for specification."""
    return org_list.ACCEPTED_ORG_LIST_DESCRIPTION % self.data.program.name

  def _getListConfig(self):
    """See org_list.OrgList._getListConfig for specification."""
    list_config = lists.ListConfiguration()

    list_config.addPlainTextColumn('name', 'Name',
        lambda e, *args: e.name.strip())

    list_config.addSimpleColumn('link_id', 'Organization ID', hidden=True)

    options = [('', 'All'), ('true', 'New'), ('false', 'Veteran')]
    list_config.addPlainTextColumn('new_org', 'New/Veteran',
        lambda e, *args: 'New' if e.new_org else 'Veteran', width=60,
        options=options)
    list_config.setColumnEditable('new_org', True, 'select')

    list_config.addSimpleColumn('slots_desired', 'Min',
        width=25, column_type=lists.ColumnType.NUMERICAL)
    list_config.addSimpleColumn('max_slots_desired', 'Max',
        width=25, column_type=lists.ColumnType.NUMERICAL)

    list_config.addSimpleColumn('slots', 'Slots',
        width=50, column_type=lists.ColumnType.NUMERICAL)
    list_config.setColumnEditable('slots', True)
    list_config.setColumnSummary('slots', 'sum', "Total: {0}")

    list_config.addHtmlColumn(
        'slots_unused', 'Unused slots',
        lambda ent, s, *args: ('%s'
            % (s[ent.key()])))

    list_config.addSimpleColumn('note', 'Note')
    list_config.setColumnEditable('note', True)

    list_config.setDefaultPagination(False)
    list_config.setDefaultSort('name')
    list_config.addPostEditButton('save', "Save", "", [], refresh="none")

    return list_config

  def _getQuery(self):
    """See org_list.OrgList._getQuery for specification."""
    query = org_model.GSoCOrganization.all()
    query.filter('scope', self.data.program)
    return query

  def post(self):
    """POST handler for the list actions.

    Returns:
      True if the data is successfully modified; False otherwise.
    """
    idx = lists.getListIndex(self.data.request)
    if idx != 0:
      return False

    data = self.data.POST.get('data')

    if not data:
      raise exceptions.BadRequest("Missing data")

    parsed = simplejson.loads(data)

    for key_name, properties in parsed.iteritems():
      note = properties.get('note')
      slots = properties.get('slots')
      new_org = properties.get('new_org')

      if ('note' not in properties and 'slots' not in properties and
          'new_org' not in properties):
        logging.warning("Neither note or slots present in '%s'" % properties)
        continue

      if 'slots' in properties:
        if not slots.isdigit():
          logging.warning("Non-int value for slots: '%s'" % slots)
          properties.pop('slots')
        else:
          slots = int(slots)

      if new_org:
        if not new_org in ['New', 'Veteran']:
          logging.warning("Invalid value for new_org: '%s'" % new_org)
          properties.pop('new_org')
        else:
          new_org = True if new_org == 'New' else False

      def update_org_txn():
        org = org_model.GSoCOrganization.get_by_key_name(key_name)
        if not org:
          logging.warning("Invalid org_key '%s'" % key_name)
          return
        if 'note' in properties:
          org.note = note
        if 'slots' in properties:
          org.slots = slots
        if 'new_org' in properties:
          org.new_org = new_org

        org.put()

      db.run_in_transaction(update_org_txn)

    return True

  def _getPrefetcher(self):
    """See org_list.OrgList._getPrefetcher for specification."""
    def prefetcher(orgs):
      org_slots_unused = {}

      for org in orgs:
        query = proposal_model.GSoCProposal.all(keys_only=True)
        query.filter('org', org)
        query.filter('has_mentor', True)
        query.filter('accept_as_project', True)
        slots_used = query.count()

        org_slots_unused[org.key()] = org.slots - slots_used if \
            org.slots > slots_used else 0

      return ([org_slots_unused], {})
    return prefetcher

class SlotsPage(base.GSoCRequestHandler):
  """View for the participant profile."""

  def djangoURLPatterns(self):
    return [
        gsoc_url_patterns.url(r'admin/slots/%s$' % url_patterns.PROGRAM, self,
            name='gsoc_slots'),
    ]

  def checkAccess(self, data, check, mutator):
    check.isHost()

  def templatePath(self):
    return 'v2/modules/gsoc/admin/list.html'

  def jsonContext(self, data, check, mutator):
    list_content = SlotsList(data).getListData()
    if list_content:
      return list_content.content()
    else:
      raise exceptions.AccessViolation('You do not have access to this data')

  def post(self, data, check, mutator):
    slots_list = SlotsList(data)
    if slots_list.post():
      return http.HttpResponse()
    else:
      raise exceptions.AccessViolation('You cannot change this data')

  def context(self, data, check, mutator):
    return {
      'page_name': 'Slots page',
      # TODO(nathaniel): Drop the first parameter of SlotsList.
      'list': SlotsList(data),
    }
<commit_msg>Detached TODO removed.<commit_after># Copyright 2013 the Melange authors.
#
# 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.

"""Module for slot allocation."""

import logging

from google.appengine.ext import db

from django import http
from django.utils import simplejson

from soc.logic import exceptions
from soc.views.helper import lists
from soc.views.helper import url_patterns

from soc.modules.gsoc.models import organization as org_model
from soc.modules.gsoc.models import proposal as proposal_model
from soc.modules.gsoc.templates import org_list
from soc.modules.gsoc.views import base
from soc.modules.gsoc.views.helper import url_patterns as gsoc_url_patterns

class SlotsList(org_list.OrgList):
  """Template for list of accepted organizations to allocate slots."""

  def _getDescription(self):
    """See org_list.OrgList._getDescription for specification."""
    return org_list.ACCEPTED_ORG_LIST_DESCRIPTION % self.data.program.name

  def _getListConfig(self):
    """See org_list.OrgList._getListConfig for specification."""
    list_config = lists.ListConfiguration()

    list_config.addPlainTextColumn('name', 'Name',
        lambda e, *args: e.name.strip())

    list_config.addSimpleColumn('link_id', 'Organization ID', hidden=True)

    options = [('', 'All'), ('true', 'New'), ('false', 'Veteran')]
    list_config.addPlainTextColumn('new_org', 'New/Veteran',
        lambda e, *args: 'New' if e.new_org else 'Veteran', width=60,
        options=options)
    list_config.setColumnEditable('new_org', True, 'select')

    list_config.addSimpleColumn('slots_desired', 'Min',
        width=25, column_type=lists.ColumnType.NUMERICAL)
    list_config.addSimpleColumn('max_slots_desired', 'Max',
        width=25, column_type=lists.ColumnType.NUMERICAL)

    list_config.addSimpleColumn('slots', 'Slots',
        width=50, column_type=lists.ColumnType.NUMERICAL)
    list_config.setColumnEditable('slots', True)
    list_config.setColumnSummary('slots', 'sum', "Total: {0}")

    list_config.addHtmlColumn(
        'slots_unused', 'Unused slots',
        lambda ent, s, *args: ('%s'
            % (s[ent.key()])))

    list_config.addSimpleColumn('note', 'Note')
    list_config.setColumnEditable('note', True)

    list_config.setDefaultPagination(False)
    list_config.setDefaultSort('name')
    list_config.addPostEditButton('save', "Save", "", [], refresh="none")

    return list_config

  def _getQuery(self):
    """See org_list.OrgList._getQuery for specification."""
    query = org_model.GSoCOrganization.all()
    query.filter('scope', self.data.program)
    return query

  def post(self):
    """POST handler for the list actions.

    Returns:
      True if the data is successfully modified; False otherwise.
    """
    idx = lists.getListIndex(self.data.request)
    if idx != 0:
      return False

    data = self.data.POST.get('data')

    if not data:
      raise exceptions.BadRequest("Missing data")

    parsed = simplejson.loads(data)

    for key_name, properties in parsed.iteritems():
      note = properties.get('note')
      slots = properties.get('slots')
      new_org = properties.get('new_org')

      if ('note' not in properties and 'slots' not in properties and
          'new_org' not in properties):
        logging.warning("Neither note or slots present in '%s'" % properties)
        continue

      if 'slots' in properties:
        if not slots.isdigit():
          logging.warning("Non-int value for slots: '%s'" % slots)
          properties.pop('slots')
        else:
          slots = int(slots)

      if new_org:
        if not new_org in ['New', 'Veteran']:
          logging.warning("Invalid value for new_org: '%s'" % new_org)
          properties.pop('new_org')
        else:
          new_org = True if new_org == 'New' else False

      def update_org_txn():
        org = org_model.GSoCOrganization.get_by_key_name(key_name)
        if not org:
          logging.warning("Invalid org_key '%s'" % key_name)
          return
        if 'note' in properties:
          org.note = note
        if 'slots' in properties:
          org.slots = slots
        if 'new_org' in properties:
          org.new_org = new_org

        org.put()

      db.run_in_transaction(update_org_txn)

    return True

  def _getPrefetcher(self):
    """See org_list.OrgList._getPrefetcher for specification."""
    def prefetcher(orgs):
      org_slots_unused = {}

      for org in orgs:
        query = proposal_model.GSoCProposal.all(keys_only=True)
        query.filter('org', org)
        query.filter('has_mentor', True)
        query.filter('accept_as_project', True)
        slots_used = query.count()

        org_slots_unused[org.key()] = org.slots - slots_used if \
            org.slots > slots_used else 0

      return ([org_slots_unused], {})
    return prefetcher

class SlotsPage(base.GSoCRequestHandler):
  """View for the participant profile."""

  def djangoURLPatterns(self):
    return [
        gsoc_url_patterns.url(r'admin/slots/%s$' % url_patterns.PROGRAM, self,
            name='gsoc_slots'),
    ]

  def checkAccess(self, data, check, mutator):
    check.isHost()

  def templatePath(self):
    return 'v2/modules/gsoc/admin/list.html'

  def jsonContext(self, data, check, mutator):
    list_content = SlotsList(data).getListData()
    if list_content:
      return list_content.content()
    else:
      raise exceptions.AccessViolation('You do not have access to this data')

  def post(self, data, check, mutator):
    slots_list = SlotsList(data)
    if slots_list.post():
      return http.HttpResponse()
    else:
      raise exceptions.AccessViolation('You cannot change this data')

  def context(self, data, check, mutator):
    return {
      'page_name': 'Slots page',
      'list': SlotsList(data),
    }
<|endoftext|>


<commit_before>import sys

from PyFBA import lp, log_and_message

def reaction_bounds(reactions, reactions_with_upsr, media, lower=-1000.0, mid=0.0, upper=1000.0, verbose=False):
    """
    Set the bounds for each reaction. We set the reactions to run between
    either lower/mid, mid/upper, or lower/upper depending on whether the
    reaction runs <=, =>, or <=> respectively.

    :param reactions: The dict of all reactions we know about
    :type reactions: dict of metabolism.Reaction
    :param reactions_with_upsr: The sorted list of reactions to run
    :type reactions_with_upsr: set
    :param media: The media compounds
    :type media: set
    :param lower: The default lower bound
    :type lower: float
    :param mid: The default mid value (typically 0)
    :type mid: float
    :param upper: The default upper bound
    :type upper: float
    :return: A dict of the reaction ID and the tuple of bounds
    :rtype: dict

    """

    rbvals = {}
    media_uptake_secretion_count = 0
    other_uptake_secretion_count = 0
    for r in reactions_with_upsr:
        if r == 'BIOMASS_EQN':
            rbvals[r] = (mid, upper)
            continue

        # if we already know the bounds, eg from an SBML file or from our uptake/secretion reactions
        if reactions[r].lower_bound != None and reactions[r].upper_bound != None:
            rbvals[r] = (reactions[r].lower_bound, reactions[r].upper_bound)
            continue

        if r in reactions:
            direction = reactions[r].direction
        else:
            sys.stderr.write("Did not find {} in reactions\n".format(r))
            direction = "="

        # this is where we define whether our media has the components
        if reactions[r].is_uptake_secretion or reactions[r].is_transport:
            in_media = False
            override = False # if we have external compounds that are not in the media, we don't want to run this as a media reaction
            for c in reactions[r].left_compounds:
                if c.location == 'e':
                    if c in media:
                        in_media = True
                    else:
                        override = True
            # in this case, we have some external compounds that we should not import.
            # for example, H+ is used to translocate things
            if override:
                in_media = False

            if in_media:
                rbvals[r] = (lower, upper)
                log_and_message(f"{r} {reactions[r].equation}  ({reactions[r].lower_bound}, {reactions[r].upper_bound})", stderr=True)
                media_uptake_secretion_count += 1
            else:
                #rbvals[r] = (0.0, upper)
                rbvals[r] = (lower, upper)
                other_uptake_secretion_count += 1
            continue

        if direction == "=":
            # This is what I think it should be:
            rbvals[r] = (lower, upper)
            # rbvals[r] = (mid, upper)
        elif direction == ">":
            # This is what I think it should be:
            rbvals[r] = (mid, upper)
            # rbvals[r] =  (lower, upper)
        elif direction == "<":
            # This is what I think it should be:
            # rbvals[r] = (lower, mid)
            rbvals[r] = (lower, mid)
        else:
            sys.stderr.write("DO NOT UNDERSTAND DIRECTION " + direction + " for " + r + "\n")
            rbvals[r] = (mid, upper)

    if verbose:
        sys.stderr.write("In parsing the bounds we found {} media uptake ".format(media_uptake_secretion_count) +
                         "and secretion reactions and {} other u/s reactions\n".format(other_uptake_secretion_count))

    rbounds = [rbvals[r] for r in reactions_with_upsr]
    for r in reactions_with_upsr:
        if r in reactions:
            reactions[r].lower_bound, reactions[r].upper_bound = rbvals[r]

    lp.col_bounds(rbounds)
    return rbvals

def compound_bounds(cp, lower=0, upper=0):
    """
    Impose constraints on the compounds. These constraints limit what
    the variation of each compound can be and is essentially 0 for
    most compounds except those that are in the media or otherwise
    external.

    This is the zero flux vector.

    Parameters:
        cp: the list of compound ids
        lower: the default lower value
        upper: the default upper value
    """

    cbounds = [(lower, upper) for c in cp]
    cbvals = {c: (lower, upper) for c in cp}

    lp.row_bounds(cbounds)
    return cbvals
<commit_msg>Adding is_input_reaction<commit_after>import sys

from PyFBA import lp, log_and_message

def reaction_bounds(reactions, reactions_with_upsr, media, lower=-1000.0, mid=0.0, upper=1000.0, verbose=False):
    """
    Set the bounds for each reaction. We set the reactions to run between
    either lower/mid, mid/upper, or lower/upper depending on whether the
    reaction runs <=, =>, or <=> respectively.

    :param reactions: The dict of all reactions we know about
    :type reactions: dict of metabolism.Reaction
    :param reactions_with_upsr: The sorted list of reactions to run
    :type reactions_with_upsr: set
    :param media: The media compounds
    :type media: set
    :param lower: The default lower bound
    :type lower: float
    :param mid: The default mid value (typically 0)
    :type mid: float
    :param upper: The default upper bound
    :type upper: float
    :return: A dict of the reaction ID and the tuple of bounds
    :rtype: dict

    """

    rbvals = {}
    media_uptake_secretion_count = 0
    other_uptake_secretion_count = 0
    for r in reactions_with_upsr:
        if r == 'BIOMASS_EQN':
            rbvals[r] = (mid, upper)
            continue

        # if we already know the bounds, eg from an SBML file or from our uptake/secretion reactions
        if reactions[r].lower_bound != None and reactions[r].upper_bound != None:
            rbvals[r] = (reactions[r].lower_bound, reactions[r].upper_bound)
            continue

        if r in reactions:
            direction = reactions[r].direction
        else:
            sys.stderr.write("Did not find {} in reactions\n".format(r))
            direction = "="

        # this is where we define whether our media has the components
        if reactions[r].is_uptake_secretion or reactions[r].is_transport or reactions[r].is_input_reaction():
            in_media = False
            override = False # if we have external compounds that are not in the media, we don't want to run this as a media reaction
            for c in reactions[r].left_compounds:
                if c.location == 'e':
                    if c in media:
                        in_media = True
                    else:
                        override = True
            # in this case, we have some external compounds that we should not import.
            # for example, H+ is used to translocate things
            if override:
                in_media = False

            if in_media:
                rbvals[r] = (lower, upper)
                log_and_message(f"{r} {reactions[r].equation}  ({reactions[r].lower_bound}, {reactions[r].upper_bound})", stderr=True)
                media_uptake_secretion_count += 1
            else:
                #rbvals[r] = (0.0, upper)
                rbvals[r] = (lower, upper)
                other_uptake_secretion_count += 1
            continue

        if direction == "=":
            # This is what I think it should be:
            rbvals[r] = (lower, upper)
            # rbvals[r] = (mid, upper)
        elif direction == ">":
            # This is what I think it should be:
            rbvals[r] = (mid, upper)
            # rbvals[r] =  (lower, upper)
        elif direction == "<":
            # This is what I think it should be:
            # rbvals[r] = (lower, mid)
            rbvals[r] = (lower, mid)
        else:
            sys.stderr.write("DO NOT UNDERSTAND DIRECTION " + direction + " for " + r + "\n")
            rbvals[r] = (mid, upper)

    if verbose:
        sys.stderr.write("In parsing the bounds we found {} media uptake ".format(media_uptake_secretion_count) +
                         "and secretion reactions and {} other u/s reactions\n".format(other_uptake_secretion_count))

    rbounds = [rbvals[r] for r in reactions_with_upsr]
    for r in reactions_with_upsr:
        if r in reactions:
            reactions[r].lower_bound, reactions[r].upper_bound = rbvals[r]

    lp.col_bounds(rbounds)
    return rbvals

def compound_bounds(cp, lower=0, upper=0):
    """
    Impose constraints on the compounds. These constraints limit what
    the variation of each compound can be and is essentially 0 for
    most compounds except those that are in the media or otherwise
    external.

    This is the zero flux vector.

    Parameters:
        cp: the list of compound ids
        lower: the default lower value
        upper: the default upper value
    """

    cbounds = [(lower, upper) for c in cp]
    cbvals = {c: (lower, upper) for c in cp}

    lp.row_bounds(cbounds)
    return cbvals
<|endoftext|>