<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Tests methods of the Electrode class.
"""

__author__ = "Joshua Ryan Smith (joshua.r.smith@gmail.com)"
__version__ = ""
__date__ = ""
__copyright__ = "Copyright (c) 2012 Joshua Ryan Smith"
__license__ = ""

from tec import Electrode
import unittest
import pickle

class MethodsSpecialCase(unittest.TestCase):
  """
  Tests the special cases of the methods.
  """

  def setUp(self):
    """
    Set up a dictionary that can properly instantiate an Electrode object.
    """

    input_params = {"temp":1,\
                   "barrier_ht":1,\
                   "voltage":1,\
                   "position":0,\
                   "richardson":10,\
                   "emissivity":0.5}

    self.input_params = input_params

  def test_output_current_zero_temp(self):
    """Set temp=0 and verify calc_saturation_current() returns value of 0."""
    self.input_params["temp"] = 0
    el = Electrode(self.input_params)
    self.assertEqual(el.calc_saturation_current(),0)

  def test_output_current_zero_richardson(self):
    """Set richardson=0 and verify calc_saturation_current() returns value of 0."""
    self.input_params["richardson"] = 0
    el = Electrode(self.input_params)
    self.assertEqual(el.calc_saturation_current(),0)

class MethodsValues(unittest.TestCase):
  """
  Tests the output of the methods match some expected values.
  """

  def test_vacuum_energy_without_nea(self):
    """
    I still need to be able to accurately calculate the vacuum energy when there is no nea attribute.
    """
    print "test not implemented."

  def test_vacuum_energy_with_nea(self):
    print "test not implemented."

  def test_saturation_current_values(self):
    """Compares the output against a list of standard values."""
    f = open("test/electrode_output_current_std.dat","r")
    standard_values = pickle.load(f)
    f.close()

    for params in standard_values:
      el = Electrode(params)
      # Multiplying like this to fix the units is sooooo hacky.
      self.assertAlmostEqual(1e-4 * el.calc_saturation_current(),\
	params["outpt_cur"])

if __name__ == '__main__':
  unittest.main()
<commit_msg>Added methods to test calc_vacuum_energy.<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Tests methods of the Electrode class.
"""

__author__ = "Joshua Ryan Smith (joshua.r.smith@gmail.com)"
__version__ = ""
__date__ = ""
__copyright__ = "Copyright (c) 2012 Joshua Ryan Smith"
__license__ = ""

from tec import Electrode
import unittest
import pickle

class MethodsSpecialCase(unittest.TestCase):
  """
  Tests the special cases of the methods.
  """

  def setUp(self):
    """
    Set up a dictionary that can properly instantiate an Electrode object.
    """

    input_params = {"temp":1,\
                   "barrier_ht":1,\
                   "voltage":1,\
                   "position":0,\
                   "richardson":10,\
                   "emissivity":0.5}

    self.input_params = input_params

  def test_output_current_zero_temp(self):
    """Set temp=0 and verify calc_saturation_current() returns value of 0."""
    self.input_params["temp"] = 0
    el = Electrode(self.input_params)
    self.assertEqual(el.calc_saturation_current(),0)

  def test_output_current_zero_richardson(self):
    """Set richardson=0 and verify calc_saturation_current() returns value of 0."""
    self.input_params["richardson"] = 0
    el = Electrode(self.input_params)
    self.assertEqual(el.calc_saturation_current(),0)

class MethodsValues(unittest.TestCase):
  """
  Tests the output of the methods match some expected values.
  """

  def test_vacuum_energy_without_nea(self):
    """Compares the output against a list of standard values."""
    f = open("test/electrode_vac_energy_no_nea_std.dat","r")
    standard_values = pickle.load(f)
    f.close()

    for params in standard_values:
      el = Electrode(params)
      # Multiplying like this to fix the units is sooooo hacky.
      self.assertAlmostEqual(el.calc_vacuum_energy(),\
	params["vac_energy"])

  def test_vacuum_energy_with_nea(self):
    """Compares the output against a list of standard values."""
    f = open("test/electrode_vac_energy_with_nea_std.dat","r")
    standard_values = pickle.load(f)
    f.close()

    for params in standard_values:
      el = Electrode(params)
      # Multiplying like this to fix the units is sooooo hacky.
      self.assertAlmostEqual(el.calc_vacuum_energy(),\
	params["vac_energy"])

  def test_saturation_current_values(self):
    """Compares the output against a list of standard values."""
    f = open("test/electrode_output_current_std.dat","r")
    standard_values = pickle.load(f)
    f.close()

    for params in standard_values:
      el = Electrode(params)
      # Multiplying like this to fix the units is sooooo hacky.
      self.assertAlmostEqual(1e-4 * el.calc_saturation_current(),\
	params["outpt_cur"])

if __name__ == '__main__':
  unittest.main()
<|endoftext|>


<commit_before>#!/usr/bin/env python
# coding=UTF-8

import sys
import unittest
sys.path.insert(0, "..")
from lib import response_analyse as ra
from lib import speak as rs
# FIXME: imports for rest of red

class DummyRed(object):
    def __init__(self):
        self.res_hdrs = []
        self.res_phrase = ""
        self.messages = []
        self.msg_classes = []

    def setMessage(self, name, msg, **kw):
        self.messages.append(msg(name, None, kw))
        self.msg_classes.append(msg.__name__)

class ResponseHeaderParserTester(unittest.TestCase):
    def setUp(self):
        self.red = DummyRed()
        self.parser = ra.ResponseHeaderParser(self.red)

    def parseHeader(self, name, values):
        name_token = name.lower().replace('-', '_')
        return getattr(self.parser, name_token)(name, values)

    def check_hdr(self, name, vals, expected_val, expected_msgs):
        self.red.__init__()
        val = self.parseHeader(name, vals)
        self.assertEqual(expected_val, val,
            "[%s] %s != %s" % (name, str(expected_val), str(val)))
        diff = set(
            [n.__name__ for n in expected_msgs]).symmetric_difference(
            set(self.red.msg_classes)
        )
        self.assertEqual(len(diff), 0,
            "[%s] Mismatched messages: %s" % (name, diff)
        )

    def test_unquoteString(self):
        i = 0
        for (instr, expected_str, expected_msgs) in [
            ('foo', 'foo', []),
            ('"foo"', 'foo', []),
            (r'"fo\"o"', 'fo"o', []),
            (r'"f\"o\"o"', 'f"o"o', []),
            (r'"fo\\o"', r'fo\o', []),
            (r'"f\\o\\o"', r'f\o\o', []),
            (r'"fo\o"', 'foo', []),
        ]:
            self.red.__init__()
            out_str = self.parser._unquoteString(unicode(instr))
            diff = set(
                [n.__name__ for n in expected_msgs]).symmetric_difference(
                set(self.red.msg_classes)
            )
            self.assertEqual(len(diff), 0,
                "[%s] Mismatched messages: %s" % (i, diff)
            )
            self.assertEqual(expected_str, out_str,
                "[%s] %s != %s" % (i, str(expected_str), str(out_str)))
            i += 1

    def test_splitString(self):
        i = 0
        for (instr, expected_outlist, item, split) in [
            ('"abc", "def"',
             ['"abc"', '"def"'],
             ra.QUOTED_STRING,
             r"\s*,\s*"
            ),
            (r'"\"ab", "c\d"',
             [r'"\"ab"', r'"c\d"'],
             ra.QUOTED_STRING,
             r"\s*,\s*"
            )
        ]:
            self.red.__init__()
            outlist = self.parser._splitString(unicode(instr), item, split)
            self.assertEqual(expected_outlist, outlist,
                "[%s] %s != %s" % (i, str(expected_outlist), str(outlist)))
            i += 1

    def test_parse_params(self):
        i = 0
        for (instr, expected_pd, expected_msgs) in [
            ('foo=bar', {'foo': 'bar'}, []),
            ('foo="bar"', {'foo': 'bar'}, []),
            ('foo="bar"; baz=bat', {'foo': 'bar', 'baz': 'bat'}, []),
            ('foo="bar"; baz="b=t"; bam="boom"',
             {'foo': 'bar', 'baz': 'b=t', 'bam': 'boom'}, []
            ),
            (r'foo="b\"ar"', {'foo': 'b"ar'}, []),
            (r'foo=bar; foo=baz', {'foo': 'baz'},
             [rs.PARAM_REPEATS]
            ),
            ("foo=bar; baz='bat'", {'foo': 'bar', 'baz': "'bat'"},
             [rs.PARAM_SINGLE_QUOTED]
            ),
            ("foo*=\"UTF-8''a%cc%88.txt\"",
             {'foo*': u'a\u0308.txt'},
             [rs.PARAM_STAR_QUOTED]
            ),
            ("foo*=''a%cc%88.txt",
             {},
             [rs.PARAM_STAR_NOCHARSET]
            ),
            ("foo*=utf-16''a%cc%88.txt",
             {},
             [rs.PARAM_STAR_CHARSET]
            ),
        ]:
            self.red.__init__()
            param_dict = self.parser._parse_params('test', instr)
            diff = set(
                [n.__name__ for n in expected_msgs]).symmetric_difference(
                set(self.red.msg_classes)
            )
            self.assertEqual(len(diff), 0,
                "[%s] Mismatched messages: %s" % (i, diff)
            )
            self.assertEqual(expected_pd, param_dict,
                "[%s] %s != %s" % (i, str(expected_pd), str(param_dict)))
            i += 1

    def test_content_disposition(self):
        cd = 'Content-Disposition'
        # quoted-string
        self.check_hdr(cd, ['attachment; filename="foo.txt"'],
         ('attachment', {'filename': 'foo.txt'}),
         []
        )

        # token
        self.check_hdr(cd, ['attachment; filename=foo.txt'],
         ('attachment', {'filename': 'foo.txt'}),
         []
        )

        # inline
        self.check_hdr(cd, ['inline; filename=foo.txt'],
         ('inline', {'filename': 'foo.txt'}),
         []
        )

        # token
        self.check_hdr(cd,
         ['attachment; filename=foo.txt, inline; filename=bar.txt'],
         ('inline', {'filename': 'bar.txt'}),
         [rs.SINGLE_HEADER_REPEAT]
        )

        # filename*
        self.check_hdr(cd,
         ["attachment; filename=foo.txt; filename*=UTF-8''a%cc%88.txt"],
         ('attachment', {
            'filename': 'foo.txt',
            'filename*': u'a\u0308.txt'
         }),
         []
        )

        # filename* quoted
        self.check_hdr(cd,
         ["attachment; filename=foo.txt; filename*=\"UTF-8''a%cc%88.txt\""],
         ('attachment', {
            'filename': 'foo.txt',
            'filename*': u'a\u0308.txt'
         }),
         [rs.PARAM_STAR_QUOTED]
        )

        # % in filename
        self.check_hdr(cd, ["attachment; filename=fo%22o.txt"],
         ('attachment', {
            'filename': 'fo%22o.txt',
         }),
         [rs.DISPOSITION_FILENAME_PERCENT]
        )

        # pathchar in filename
        self.check_hdr(cd, ['"attachment; filename="/foo.txt"'],
         ('attachment', {
            'filename': '/foo.txt',
         }),
         [rs.DISPOSITION_FILENAME_PATH_CHAR]
        )

    def test_link(self):
        li = 'Link'
        # basic
        self.check_hdr(li, ['<http://www.example.com/>; rel=example'],
         ('http://www.example.com/', {'rel': 'example'}),
         []
        )

        # quoted relation
        self.check_hdr(li, ['<http://www.example.com/>; rel="example"'],
         ('http://www.example.com/', {'rel': 'example'}),
         []
        )

        # relative URI
        self.check_hdr(li, ['</foo>; rel="example"'],
         ('/foo', {'rel': 'example'}),
         []
        )

        # repeating rel
        self.check_hdr(li, ['</foo>; rel="example"; rel="another"'],
         ('/foo', {'rel': 'another'}),
         [rs.PARAM_REPEATS]
        )

    def test_date(self):
        da = 'Date'
        # basic
        self.check_hdr(da, ['Mon, 04 Jul 2011 09:08:06 GMT'],
         1309770486,
         []
        )

        # blank
        self.check_hdr(da, [''],
         None,
         [rs.BAD_DATE_SYNTAX])

if __name__ == "__main__":
    unittest.main()<commit_msg>add bad syntax text for link<commit_after>#!/usr/bin/env python
# coding=UTF-8

import sys
import unittest
sys.path.insert(0, "..")
from lib import response_analyse as ra
from lib import speak as rs
# FIXME: imports for rest of red

class DummyRed(object):
    def __init__(self):
        self.res_hdrs = []
        self.res_phrase = ""
        self.messages = []
        self.msg_classes = []

    def setMessage(self, name, msg, **kw):
        self.messages.append(msg(name, None, kw))
        self.msg_classes.append(msg.__name__)

class ResponseHeaderParserTester(unittest.TestCase):
    def setUp(self):
        self.red = DummyRed()
        self.parser = ra.ResponseHeaderParser(self.red)

    def parseHeader(self, name, values):
        name_token = name.lower().replace('-', '_')
        return getattr(self.parser, name_token)(name, values)

    def check_hdr(self, name, vals, expected_val, expected_msgs):
        self.red.__init__()
        val = self.parseHeader(name, vals)
        self.assertEqual(expected_val, val,
            "[%s] %s != %s" % (name, str(expected_val), str(val)))
        diff = set(
            [n.__name__ for n in expected_msgs]).symmetric_difference(
            set(self.red.msg_classes)
        )
        self.assertEqual(len(diff), 0,
            "[%s] Mismatched messages: %s" % (name, diff)
        )

    def test_unquoteString(self):
        i = 0
        for (instr, expected_str, expected_msgs) in [
            ('foo', 'foo', []),
            ('"foo"', 'foo', []),
            (r'"fo\"o"', 'fo"o', []),
            (r'"f\"o\"o"', 'f"o"o', []),
            (r'"fo\\o"', r'fo\o', []),
            (r'"f\\o\\o"', r'f\o\o', []),
            (r'"fo\o"', 'foo', []),
        ]:
            self.red.__init__()
            out_str = self.parser._unquoteString(unicode(instr))
            diff = set(
                [n.__name__ for n in expected_msgs]).symmetric_difference(
                set(self.red.msg_classes)
            )
            self.assertEqual(len(diff), 0,
                "[%s] Mismatched messages: %s" % (i, diff)
            )
            self.assertEqual(expected_str, out_str,
                "[%s] %s != %s" % (i, str(expected_str), str(out_str)))
            i += 1

    def test_splitString(self):
        i = 0
        for (instr, expected_outlist, item, split) in [
            ('"abc", "def"',
             ['"abc"', '"def"'],
             ra.QUOTED_STRING,
             r"\s*,\s*"
            ),
            (r'"\"ab", "c\d"',
             [r'"\"ab"', r'"c\d"'],
             ra.QUOTED_STRING,
             r"\s*,\s*"
            )
        ]:
            self.red.__init__()
            outlist = self.parser._splitString(unicode(instr), item, split)
            self.assertEqual(expected_outlist, outlist,
                "[%s] %s != %s" % (i, str(expected_outlist), str(outlist)))
            i += 1

    def test_parse_params(self):
        i = 0
        for (instr, expected_pd, expected_msgs) in [
            ('foo=bar', {'foo': 'bar'}, []),
            ('foo="bar"', {'foo': 'bar'}, []),
            ('foo="bar"; baz=bat', {'foo': 'bar', 'baz': 'bat'}, []),
            ('foo="bar"; baz="b=t"; bam="boom"',
             {'foo': 'bar', 'baz': 'b=t', 'bam': 'boom'}, []
            ),
            (r'foo="b\"ar"', {'foo': 'b"ar'}, []),
            (r'foo=bar; foo=baz', {'foo': 'baz'},
             [rs.PARAM_REPEATS]
            ),
            ("foo=bar; baz='bat'", {'foo': 'bar', 'baz': "'bat'"},
             [rs.PARAM_SINGLE_QUOTED]
            ),
            ("foo*=\"UTF-8''a%cc%88.txt\"",
             {'foo*': u'a\u0308.txt'},
             [rs.PARAM_STAR_QUOTED]
            ),
            ("foo*=''a%cc%88.txt",
             {},
             [rs.PARAM_STAR_NOCHARSET]
            ),
            ("foo*=utf-16''a%cc%88.txt",
             {},
             [rs.PARAM_STAR_CHARSET]
            ),
        ]:
            self.red.__init__()
            param_dict = self.parser._parse_params('test', instr)
            diff = set(
                [n.__name__ for n in expected_msgs]).symmetric_difference(
                set(self.red.msg_classes)
            )
            self.assertEqual(len(diff), 0,
                "[%s] Mismatched messages: %s" % (i, diff)
            )
            self.assertEqual(expected_pd, param_dict,
                "[%s] %s != %s" % (i, str(expected_pd), str(param_dict)))
            i += 1

    def test_content_disposition(self):
        cd = 'Content-Disposition'
        # quoted-string
        self.check_hdr(cd, ['attachment; filename="foo.txt"'],
         ('attachment', {'filename': 'foo.txt'}),
         []
        )

        # token
        self.check_hdr(cd, ['attachment; filename=foo.txt'],
         ('attachment', {'filename': 'foo.txt'}),
         []
        )

        # inline
        self.check_hdr(cd, ['inline; filename=foo.txt'],
         ('inline', {'filename': 'foo.txt'}),
         []
        )

        # token
        self.check_hdr(cd,
         ['attachment; filename=foo.txt, inline; filename=bar.txt'],
         ('inline', {'filename': 'bar.txt'}),
         [rs.SINGLE_HEADER_REPEAT]
        )

        # filename*
        self.check_hdr(cd,
         ["attachment; filename=foo.txt; filename*=UTF-8''a%cc%88.txt"],
         ('attachment', {
            'filename': 'foo.txt',
            'filename*': u'a\u0308.txt'
         }),
         []
        )

        # filename* quoted
        self.check_hdr(cd,
         ["attachment; filename=foo.txt; filename*=\"UTF-8''a%cc%88.txt\""],
         ('attachment', {
            'filename': 'foo.txt',
            'filename*': u'a\u0308.txt'
         }),
         [rs.PARAM_STAR_QUOTED]
        )

        # % in filename
        self.check_hdr(cd, ["attachment; filename=fo%22o.txt"],
         ('attachment', {
            'filename': 'fo%22o.txt',
         }),
         [rs.DISPOSITION_FILENAME_PERCENT]
        )

        # pathchar in filename
        self.check_hdr(cd, ['"attachment; filename="/foo.txt"'],
         ('attachment', {
            'filename': '/foo.txt',
         }),
         [rs.DISPOSITION_FILENAME_PATH_CHAR]
        )

    def test_link(self):
        li = 'Link'
        # basic
        self.check_hdr(li, ['<http://www.example.com/>; rel=example'],
         ('http://www.example.com/', {'rel': 'example'}),
         []
        )

        # quoted relation
        self.check_hdr(li, ['<http://www.example.com/>; rel="example"'],
         ('http://www.example.com/', {'rel': 'example'}),
         []
        )

        # relative URI
        self.check_hdr(li, ['</foo>; rel="example"'],
         ('/foo', {'rel': 'example'}),
         []
        )

        # repeating rel
        self.check_hdr(li, ['</foo>; rel="example"; rel="another"'],
         ('/foo', {'rel': 'another'}),
         [rs.PARAM_REPEATS]
        )

        # quotes (bad syntax)
        self.check_hdr(li, ['"/foo", rel="example"'],
         None,
         [rs.BAD_SYNTAX]
        )

    def test_date(self):
        da = 'Date'
        # basic
        self.check_hdr(da, ['Mon, 04 Jul 2011 09:08:06 GMT'],
         1309770486,
         []
        )

        # blank
        self.check_hdr(da, [''],
         None,
         [rs.BAD_DATE_SYNTAX])

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


<commit_before>"""
Classes that describe how a particular model should be viewed.
"""

from django.db.models.query import QuerySet

from main.models import Variant
from main.models import VariantCallerCommonData
from main.models import VariantEvidence
from scripts.dynamic_snp_filter_key_map import MAP_KEY__COMMON_DATA
from scripts.dynamic_snp_filter_key_map import MAP_KEY__EVIDENCE

class BaseVariantView(object):
    """Common methods for model views.
    """

    @classmethod
    def get_field_order(clazz, **kwargs):
        """Get the order of the models for displaying on the front-end.
        Called by the adapter.

        Args:
            field_mask: A dictionary from field name to True/False that defines
                whether to include that key. Otherwise, the default is
                returned.
        """
        if 'visible_key_names' in kwargs:
            variant_key_map = kwargs['variant_key_map']
            visible_key_names = kwargs['visible_key_names']

            additional_common_data_field_list = [field for field in
                    visible_key_names if field in
                    variant_key_map[MAP_KEY__COMMON_DATA]]

            additional_evidence_field_list = [field for field in visible_key_names
                    if field in variant_key_map[MAP_KEY__EVIDENCE]]
        else:
            additional_common_data_field_list = []
            additional_evidence_field_list = []

        # Sort the visible keys into appropriate buckets based on the map.
        return (Variant.get_field_order() +
                VariantCallerCommonData.get_field_order(
                        additional_field_list=additional_common_data_field_list) +
                VariantEvidence.get_field_order(
                        additional_field_list=additional_evidence_field_list))

class CastVariantView(BaseVariantView):
    """View of Variant that does not expose the individual
    VariantToExperimentSample relationships.
    """
    def __init__(self, variant):
        self.variant = variant
        self.variant_caller_common_data_list = (
                variant.variantcallercommondata_set.all())
        self.variant_evidence_list = VariantEvidence.objects.filter(
                variant_caller_common_data__variant=variant)

    def custom_getattr(self, attr):
        """For an attribute of a Variant, returns what you would expect.

        For many-to-one relations, e.g. VariantCallerCommonData and
        VariantEvidence, returns a '|'-separated list of values for
        that attribute.

        Returns:
            A string representing the view for the attribute.
        """
        delegate_order = [
                self.variant,
                self.variant_caller_common_data_list,
                self.variant_evidence_list
        ]
        for delegate in delegate_order:
            result_list = []

            # Convert to list for next step.
            if isinstance(delegate, QuerySet):
                delegate_list = delegate
            else:
                delegate_list = [delegate]

            print delegate_list
            # Iterate through the delegates.
            for delegate in delegate_list:

                if hasattr(delegate, attr):
                    result_list.append(getattr(delegate, attr))
                else:
                    try: result_list.append(delegate.as_dict()[attr])
                    except: pass

            if len(result_list) == 1:
                # If one object, return it.
                return result_list[0]
            elif len(result_list) > 1:
                # Else if more than one, return a '|'-separated string.
                return ' | '.join([str(res) for res in result_list])

        # Default.
        return 'undefined'

    @classmethod
    def variant_as_cast_view(clazz, variant_obj,
            variant_id_to_metadata_dict=None):
        """Factory method returns a cast view for the given variant.

        Args:
            variant_obj: The Variant object to melt.
            variant_id_to_metadata_dict: See TODO.

        Returns:
            A CastVariantView instance.
        """
        # TODO: Do we need to modify the view based on passing samples in the
        # metadata.
        return CastVariantView(variant_obj)

class MeltedVariantView(BaseVariantView):
    """View of a Variant when showing one row per VariantToExperimentSample.
    """

    def __init__(self, variant, variant_caller_common_data=None,
            variant_evidence=None):
        self.variant = variant
        self.variant_caller_common_data = variant_caller_common_data
        self.variant_evidence = variant_evidence

    def custom_getattr(self, attr):
        delegate_order = [
                self.variant,
                self.variant_caller_common_data,
                self.variant_evidence
        ]
        for delegate in delegate_order:
            if delegate is not None:
                if hasattr(delegate, attr):
                    return getattr(delegate, attr)
                else:
                    try:
                        return delegate.as_dict()[attr]
                    except:
                        pass
        # Default.
        return '---'

    @classmethod
    def variant_as_melted_list(clazz, variant_obj,
            variant_id_to_metadata_dict=None):
        """Factory method that melts the variant into a list of objects,
        one per common data per sample.

        Args:
            variant_obj: The Variant object to melt.
            variant_id_to_metadata_dict: Restrict melted entities to those
                related to these samples.

        Returns:
            List of MeltedVariantView objects corresponding to the Variant.
            This list will always be at least length 1.
        """
        melted_list = []

        all_common_data = variant_obj.variantcallercommondata_set.all()

        # We guarantee returning at least one row.
        if len(all_common_data) == 0:
            melted_list.append(MeltedVariantView(variant_obj, None, None))
            return melted_list

        # Otherwise iterate through
        for common_data_obj in all_common_data:
            # Return one row not associated with any sample.
            melted_list.append(MeltedVariantView(variant_obj, common_data_obj, None))

            variant_evidence_list = common_data_obj.variantevidence_set.all()
            if len(variant_evidence_list) == 0:
                continue

            if variant_id_to_metadata_dict is not None:
                passing_sample_ids = variant_id_to_metadata_dict[variant_obj.id].get(
                        'passing_sample_ids', set())
            else:
                passing_sample_ids = None

            for variant_evidence_obj in variant_evidence_list:
                if passing_sample_ids is not None:
                    sample_id = variant_evidence_obj.experiment_sample.id
                    if not sample_id in passing_sample_ids:
                        continue
                melted_list.append(MeltedVariantView(variant_obj, common_data_obj,
                        variant_evidence_obj))

        return melted_list
<commit_msg>Removed errant debug print statement.<commit_after>"""
Classes that describe how a particular model should be viewed.
"""

from django.db.models.query import QuerySet

from main.models import Variant
from main.models import VariantCallerCommonData
from main.models import VariantEvidence
from scripts.dynamic_snp_filter_key_map import MAP_KEY__COMMON_DATA
from scripts.dynamic_snp_filter_key_map import MAP_KEY__EVIDENCE

class BaseVariantView(object):
    """Common methods for model views.
    """

    @classmethod
    def get_field_order(clazz, **kwargs):
        """Get the order of the models for displaying on the front-end.
        Called by the adapter.

        Args:
            field_mask: A dictionary from field name to True/False that defines
                whether to include that key. Otherwise, the default is
                returned.
        """
        if 'visible_key_names' in kwargs:
            variant_key_map = kwargs['variant_key_map']
            visible_key_names = kwargs['visible_key_names']

            additional_common_data_field_list = [field for field in
                    visible_key_names if field in
                    variant_key_map[MAP_KEY__COMMON_DATA]]

            additional_evidence_field_list = [field for field in visible_key_names
                    if field in variant_key_map[MAP_KEY__EVIDENCE]]
        else:
            additional_common_data_field_list = []
            additional_evidence_field_list = []

        # Sort the visible keys into appropriate buckets based on the map.
        return (Variant.get_field_order() +
                VariantCallerCommonData.get_field_order(
                        additional_field_list=additional_common_data_field_list) +
                VariantEvidence.get_field_order(
                        additional_field_list=additional_evidence_field_list))

class CastVariantView(BaseVariantView):
    """View of Variant that does not expose the individual
    VariantToExperimentSample relationships.
    """
    def __init__(self, variant):
        self.variant = variant
        self.variant_caller_common_data_list = (
                variant.variantcallercommondata_set.all())
        self.variant_evidence_list = VariantEvidence.objects.filter(
                variant_caller_common_data__variant=variant)

    def custom_getattr(self, attr):
        """For an attribute of a Variant, returns what you would expect.

        For many-to-one relations, e.g. VariantCallerCommonData and
        VariantEvidence, returns a '|'-separated list of values for
        that attribute.

        Returns:
            A string representing the view for the attribute.
        """
        delegate_order = [
                self.variant,
                self.variant_caller_common_data_list,
                self.variant_evidence_list
        ]
        for delegate in delegate_order:
            result_list = []

            # Convert to list for next step.
            if isinstance(delegate, QuerySet):
                delegate_list = delegate
            else:
                delegate_list = [delegate]

            # Iterate through the delegates.
            for delegate in delegate_list:

                if hasattr(delegate, attr):
                    result_list.append(getattr(delegate, attr))
                else:
                    try: result_list.append(delegate.as_dict()[attr])
                    except: pass

            if len(result_list) == 1:
                # If one object, return it.
                return result_list[0]
            elif len(result_list) > 1:
                # Else if more than one, return a '|'-separated string.
                return ' | '.join([str(res) for res in result_list])

        # Default.
        return 'undefined'

    @classmethod
    def variant_as_cast_view(clazz, variant_obj,
            variant_id_to_metadata_dict=None):
        """Factory method returns a cast view for the given variant.

        Args:
            variant_obj: The Variant object to melt.
            variant_id_to_metadata_dict: See TODO.

        Returns:
            A CastVariantView instance.
        """
        # TODO: Do we need to modify the view based on passing samples in the
        # metadata.
        return CastVariantView(variant_obj)

class MeltedVariantView(BaseVariantView):
    """View of a Variant when showing one row per VariantToExperimentSample.
    """

    def __init__(self, variant, variant_caller_common_data=None,
            variant_evidence=None):
        self.variant = variant
        self.variant_caller_common_data = variant_caller_common_data
        self.variant_evidence = variant_evidence

    def custom_getattr(self, attr):
        delegate_order = [
                self.variant,
                self.variant_caller_common_data,
                self.variant_evidence
        ]
        for delegate in delegate_order:
            if delegate is not None:
                if hasattr(delegate, attr):
                    return getattr(delegate, attr)
                else:
                    try:
                        return delegate.as_dict()[attr]
                    except:
                        pass
        # Default.
        return '---'

    @classmethod
    def variant_as_melted_list(clazz, variant_obj,
            variant_id_to_metadata_dict=None):
        """Factory method that melts the variant into a list of objects,
        one per common data per sample.

        Args:
            variant_obj: The Variant object to melt.
            variant_id_to_metadata_dict: Restrict melted entities to those
                related to these samples.

        Returns:
            List of MeltedVariantView objects corresponding to the Variant.
            This list will always be at least length 1.
        """
        melted_list = []

        all_common_data = variant_obj.variantcallercommondata_set.all()

        # We guarantee returning at least one row.
        if len(all_common_data) == 0:
            melted_list.append(MeltedVariantView(variant_obj, None, None))
            return melted_list

        # Otherwise iterate through
        for common_data_obj in all_common_data:
            # Return one row not associated with any sample.
            melted_list.append(MeltedVariantView(variant_obj, common_data_obj, None))

            variant_evidence_list = common_data_obj.variantevidence_set.all()
            if len(variant_evidence_list) == 0:
                continue

            if variant_id_to_metadata_dict is not None:
                passing_sample_ids = variant_id_to_metadata_dict[variant_obj.id].get(
                        'passing_sample_ids', set())
            else:
                passing_sample_ids = None

            for variant_evidence_obj in variant_evidence_list:
                if passing_sample_ids is not None:
                    sample_id = variant_evidence_obj.experiment_sample.id
                    if not sample_id in passing_sample_ids:
                        continue
                melted_list.append(MeltedVariantView(variant_obj, common_data_obj,
                        variant_evidence_obj))

        return melted_list
<|endoftext|>


<commit_before>"""
Classes that describe how a particular model should be viewed.
"""

from django.db.models.query import QuerySet

from main.models import Variant
from main.models import VariantCallerCommonData
from main.models import VariantEvidence
from scripts.dynamic_snp_filter_key_map import MAP_KEY__COMMON_DATA
from scripts.dynamic_snp_filter_key_map import MAP_KEY__EVIDENCE

class BaseVariantView(object):
    """Common methods for model views.
    """

    @classmethod
    def get_field_order(clazz, **kwargs):
        """Get the order of the models for displaying on the front-end.
        Called by the adapter.

        Args:
            field_mask: A dictionary from field name to True/False that defines
                whether to include that key. Otherwise, the default is
                returned.
        """
        if 'visible_key_names' in kwargs:
            variant_key_map = kwargs['variant_key_map']
            visible_key_names = kwargs['visible_key_names']

            additional_common_data_field_list = [field for field in
                    visible_key_names if field in
                    variant_key_map[MAP_KEY__COMMON_DATA]]

            additional_evidence_field_list = [field for field in visible_key_names
                    if field in variant_key_map[MAP_KEY__EVIDENCE]]
        else:
            additional_common_data_field_list = []
            additional_evidence_field_list = []

        # Sort the visible keys into appropriate buckets based on the map.
        return (Variant.get_field_order() +
                VariantCallerCommonData.get_field_order(
                        additional_field_list=additional_common_data_field_list) +
                VariantEvidence.get_field_order(
                        additional_field_list=additional_evidence_field_list))

class CastVariantView(BaseVariantView):
    """View of Variant that does not expose the individual
    VariantToExperimentSample relationships.
    """
    def __init__(self, variant):
        self.variant = variant
        self.variant_caller_common_data_list = (
                variant.variantcallercommondata_set.all())
        self.variant_evidence_list = VariantEvidence.objects.filter(
                variant_caller_common_data__variant=variant)

    def custom_getattr(self, attr):
        """For an attribute of a Variant, returns what you would expect.

        For many-to-one relations, e.g. VariantCallerCommonData and
        VariantEvidence, returns a '|'-separated list of values for
        that attribute.

        Returns:
            A string representing the view for the attribute.
        """
        delegate_order = [
                self.variant,
                self.variant_caller_common_data_list,
                self.variant_evidence_list
        ]
        for delegate in delegate_order:
            result_list = []

            # Convert to list for next step.
            if isinstance(delegate, QuerySet):
                delegate_list = delegate
            else:
                delegate_list = [delegate]

            # Iterate through the delegates.
            for delegate in delegate_list:
                if hasattr(delegate, attr):
                    result_list.append(getattr(delegate, attr))

            if len(result_list) == 1:
                # If one object, return it.
                return result_list[0]
            elif len(result_list) > 1:
                # Else if more than one, return a '|'-separated string.
                return ' | '.join([str(res) for res in result_list])

        # Default.
        return 'undefined'

    @classmethod
    def variant_as_cast_view(clazz, variant_obj,
            variant_id_to_metadata_dict=None):
        """Factory method returns a cast view for the given variant.

        Args:
            variant_obj: The Variant object to melt.
            variant_id_to_metadata_dict: See TODO.

        Returns:
            A CastVariantView instance.
        """
        # TODO: Do we need to modify the view based on passing samples in the
        # metadata.
        return CastVariantView(variant_obj)

class MeltedVariantView(BaseVariantView):
    """View of a Variant when showing one row per VariantToExperimentSample.
    """

    def __init__(self, variant, variant_caller_common_data=None,
            variant_evidence=None):
        self.variant = variant
        self.variant_caller_common_data = variant_caller_common_data
        self.variant_evidence = variant_evidence

    def custom_getattr(self, attr):
        delegate_order = [
                self.variant,
                self.variant_caller_common_data,
                self.variant_evidence
        ]
        for delegate in delegate_order:
            if delegate is not None and hasattr(delegate, attr):
                return getattr(delegate, attr)
        # Default.
        return 'undefined'

    @classmethod
    def variant_as_melted_list(clazz, variant_obj,
            variant_id_to_metadata_dict=None):
        """Factory method that melts the variant into a list of objects,
        one per common data per sample.

        Args:
            variant_obj: The Variant object to melt.
            variant_id_to_metadata_dict: Restrict melted entities to those
                related to these samples.

        Returns:
            List of MeltedVariantView objects corresponding to the Variant.
            This list will always be at least length 1.
        """
        melted_list = []

        all_common_data = variant_obj.variantcallercommondata_set.all()

        # We guarantee returning at least one row.
        if len(all_common_data) == 0:
            melted_list.append(MeltedVariantView(variant_obj, None, None))
            return melted_list

        # Otherwise iterate through
        for common_data_obj in all_common_data:
            # Return one row not associated with any sample.
            melted_list.append(MeltedVariantView(variant_obj, common_data_obj, None))

            variant_evidence_list = common_data_obj.variantevidence_set.all()
            if len(variant_evidence_list) == 0:
                continue

            if variant_id_to_metadata_dict is not None:
                passing_sample_ids = variant_id_to_metadata_dict[variant_obj.id].get(
                        'passing_sample_ids', set())
            else:
                passing_sample_ids = None

            for variant_evidence_obj in variant_evidence_list:
                if passing_sample_ids is not None:
                    sample_id = variant_evidence_obj.experiment_sample.id
                    if not sample_id in passing_sample_ids:
                        continue
                melted_list.append(MeltedVariantView(variant_obj, common_data_obj,
                        variant_evidence_obj))

        return melted_list
<commit_msg>Fixed missing INFO fields bug.<commit_after>"""
Classes that describe how a particular model should be viewed.
"""

from django.db.models.query import QuerySet

from main.models import Variant
from main.models import VariantCallerCommonData
from main.models import VariantEvidence
from scripts.dynamic_snp_filter_key_map import MAP_KEY__COMMON_DATA
from scripts.dynamic_snp_filter_key_map import MAP_KEY__EVIDENCE

class BaseVariantView(object):
    """Common methods for model views.
    """

    @classmethod
    def get_field_order(clazz, **kwargs):
        """Get the order of the models for displaying on the front-end.
        Called by the adapter.

        Args:
            field_mask: A dictionary from field name to True/False that defines
                whether to include that key. Otherwise, the default is
                returned.
        """
        if 'visible_key_names' in kwargs:
            variant_key_map = kwargs['variant_key_map']
            visible_key_names = kwargs['visible_key_names']

            additional_common_data_field_list = [field for field in
                    visible_key_names if field in
                    variant_key_map[MAP_KEY__COMMON_DATA]]

            additional_evidence_field_list = [field for field in visible_key_names
                    if field in variant_key_map[MAP_KEY__EVIDENCE]]
        else:
            additional_common_data_field_list = []
            additional_evidence_field_list = []

        # Sort the visible keys into appropriate buckets based on the map.
        return (Variant.get_field_order() +
                VariantCallerCommonData.get_field_order(
                        additional_field_list=additional_common_data_field_list) +
                VariantEvidence.get_field_order(
                        additional_field_list=additional_evidence_field_list))

class CastVariantView(BaseVariantView):
    """View of Variant that does not expose the individual
    VariantToExperimentSample relationships.
    """
    def __init__(self, variant):
        self.variant = variant
        self.variant_caller_common_data_list = (
                variant.variantcallercommondata_set.all())
        self.variant_evidence_list = VariantEvidence.objects.filter(
                variant_caller_common_data__variant=variant)

    def custom_getattr(self, attr):
        """For an attribute of a Variant, returns what you would expect.

        For many-to-one relations, e.g. VariantCallerCommonData and
        VariantEvidence, returns a '|'-separated list of values for
        that attribute.

        Returns:
            A string representing the view for the attribute.
        """
        delegate_order = [
                self.variant,
                self.variant_caller_common_data_list,
                self.variant_evidence_list
        ]
        for delegate in delegate_order:
            result_list = []

            # Convert to list for next step.
            if isinstance(delegate, QuerySet):
                delegate_list = delegate
            else:
                delegate_list = [delegate]

            print delegate_list
            # Iterate through the delegates.
            for delegate in delegate_list:

                if hasattr(delegate, attr):
                    result_list.append(getattr(delegate, attr))
                else:
                    try: result_list.append(delegate.as_dict()[attr])
                    except: pass

            if len(result_list) == 1:
                # If one object, return it.
                return result_list[0]
            elif len(result_list) > 1:
                # Else if more than one, return a '|'-separated string.
                return ' | '.join([str(res) for res in result_list])

        # Default.
        return 'undefined'

    @classmethod
    def variant_as_cast_view(clazz, variant_obj,
            variant_id_to_metadata_dict=None):
        """Factory method returns a cast view for the given variant.

        Args:
            variant_obj: The Variant object to melt.
            variant_id_to_metadata_dict: See TODO.

        Returns:
            A CastVariantView instance.
        """
        # TODO: Do we need to modify the view based on passing samples in the
        # metadata.
        return CastVariantView(variant_obj)

class MeltedVariantView(BaseVariantView):
    """View of a Variant when showing one row per VariantToExperimentSample.
    """

    def __init__(self, variant, variant_caller_common_data=None,
            variant_evidence=None):
        self.variant = variant
        self.variant_caller_common_data = variant_caller_common_data
        self.variant_evidence = variant_evidence

    def custom_getattr(self, attr):
        delegate_order = [
                self.variant,
                self.variant_caller_common_data,
                self.variant_evidence
        ]
        for delegate in delegate_order:
            if delegate is not None:
                if hasattr(delegate, attr):
                    return getattr(delegate, attr)
                else:
                    try:
                        return delegate.as_dict()[attr]
                    except:
                        pass
        # Default.
        return '---'

    @classmethod
    def variant_as_melted_list(clazz, variant_obj,
            variant_id_to_metadata_dict=None):
        """Factory method that melts the variant into a list of objects,
        one per common data per sample.

        Args:
            variant_obj: The Variant object to melt.
            variant_id_to_metadata_dict: Restrict melted entities to those
                related to these samples.

        Returns:
            List of MeltedVariantView objects corresponding to the Variant.
            This list will always be at least length 1.
        """
        melted_list = []

        all_common_data = variant_obj.variantcallercommondata_set.all()

        # We guarantee returning at least one row.
        if len(all_common_data) == 0:
            melted_list.append(MeltedVariantView(variant_obj, None, None))
            return melted_list

        # Otherwise iterate through
        for common_data_obj in all_common_data:
            # Return one row not associated with any sample.
            melted_list.append(MeltedVariantView(variant_obj, common_data_obj, None))

            variant_evidence_list = common_data_obj.variantevidence_set.all()
            if len(variant_evidence_list) == 0:
                continue

            if variant_id_to_metadata_dict is not None:
                passing_sample_ids = variant_id_to_metadata_dict[variant_obj.id].get(
                        'passing_sample_ids', set())
            else:
                passing_sample_ids = None

            for variant_evidence_obj in variant_evidence_list:
                if passing_sample_ids is not None:
                    sample_id = variant_evidence_obj.experiment_sample.id
                    if not sample_id in passing_sample_ids:
                        continue
                melted_list.append(MeltedVariantView(variant_obj, common_data_obj,
                        variant_evidence_obj))

        return melted_list
<|endoftext|>


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

# Copyright (c) 2011-2015, Camptocamp SA
# All rights reserved.

# Redistribution and use in source and
# binary forms, with or without
# modification, are permitted provided
# that the following conditions are met:

# 1.    Redistributions of source code must
#       retain the above copyright notice, this
#       list of conditions and the following disclaimer.
# 2.    Redistributions in binary form mustg
#       reproduce the above copyright notice,
#       this list of conditions and theg
#       following disclaimer in the documentation
#       and/or other materials provided
#       with the distribution.

# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
# HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND
# FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
# OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

# The views and conclusions contained in the
# software and documentation are those
# of the authors and should not be interpreted
# as representing official policies,
# either expressed or implied, of the FreeBSD Project.

import json
import logging
import re
import random
import urllib
import urllib2
from cStringIO import StringIO
from datetime import datetime

from pyramid.i18n import get_localizer, TranslationStringFactory
from pyramid.view import view_config
from pyramid.httpexceptions import HTTPUnauthorized, HTTPInternalServerError
from pyramid.httpexceptions import HTTPNotFound

from PyPDF2 import PdfFileMerger
import weasyprint

from c2cgeoportal.models import RestrictionArea, Role, Layer
from c2cgeoportal.views.printproxy import PrintProxy
from c2cgeoportal.lib.caching import NO_CACHE, get_region

from geoportailv3.models import DBSession, LuxPrintJob, LuxLayerInternalWMS

_ = TranslationStringFactory("geoportailv3-server")
log = logging.getLogger(__name__)
cache_region = get_region()

class LuxPrintProxy(PrintProxy):

    @view_config(route_name="lux_printproxy_report_create")
    def lux_report_create(self):
        token = self.config["authtkt_secret"]
        print_urls = self.config["print_urls"]
        if print_urls is not None and len(print_urls) > 0:
            print_url = print_urls[random.randint(0, len(print_urls) - 1)]
        else:
            print_url = self.config["print_url"]
        spec = json.loads(self.request.body)
        for map_layer in spec["attributes"]["map"]["layers"]:
            if "baseURL" in map_layer and\
               "ogcproxywms" in map_layer["baseURL"]:
                if "customParams" in map_layer:
                    map_layer["customParams"]["GP_TOKEN"] = token
                else:
                    map_layer["customParams"] = {"GP_TOKEN": token}

                for layer in map_layer["layers"]:
                    internal_wms = DBSession.query(LuxLayerInternalWMS).filter(
                        LuxLayerInternalWMS.layer == layer).first()
                    if internal_wms is not None and\
                       not self._is_authorized(internal_wms):
                            return HTTPUnauthorized()
        if "longUrl" in spec["attributes"]:
            opener = urllib2.build_opener(urllib2.HTTPHandler())
            data = urllib.urlencode({"url": spec["attributes"]["longUrl"]})
            content = opener.open(
                "https://map.geoportail.lu/wsgi/short/create",
                data=data).read()
            shortner = json.loads(content)
            spec["attributes"]["url"] = shortner["short_url"]
            spec["attributes"]["qrimage"] =\
                "http://map.geoportail.lu/main/wsgi/qr?url=" + \
                spec["attributes"]["url"]

        job = LuxPrintJob()
        job.spec = json.dumps(spec)

        self.request.body = job.spec

        resp, content = self._proxy("%s/report.%s" % (
            print_url,
            self.request.matchdict.get("format")
        ))
        job.id = json.loads(content)["ref"]
        job.print_url = print_url
        job.creation = datetime.now()
        DBSession.add(job)
        return self._build_response(
            resp, content, False, "print"
        )

    @view_config(route_name="lux_printproxy_status")
    def lux_status(self):
        ref = self.request.matchdict.get("ref")
        job = DBSession.query(LuxPrintJob).get(ref)

        try:
            return self._proxy_response(
                "print",
                "%s/status/%s.json" % (
                    job.print_url,
                    ref
                ),
            )
        except Exception as e:
            log.exception(e)
            job.is_error = True
            return HTTPInternalServerError()

    def _is_authorized(self, internal_wms):
        if internal_wms.public:
            return True
        if self.request.user is None:
            return False
        else:
            restriction = DBSession.query(RestrictionArea).filter(
                RestrictionArea.roles.any(
                    Role.id == self.request.user.role.id)).filter(
                        RestrictionArea.layers.any(
                            Layer.id == internal_wms.id
                        )
                    ).first()
            # If not restriction is set then return unauthorized
            if restriction is None:
                return False
        return True

    @view_config(route_name="lux_printproxy_report_cancel")
    def lux_cancel(self):
        ref = self.request.matchdict.get("ref")
        job = DBSession.query(LuxPrintJob).get(ref)
        print_url = job.print_url
        DBSession.query(LuxPrintJob).filter(
            LuxPrintJob.id == ref
        ).delete()
        return self._proxy_response(
            "print",
            "%s/cancel/%s" % (
                print_url,
                ref
            ),
        )

    def _create_legend_from_image(self, url, title, access_constraints):
        css = weasyprint.CSS(
            string="img {max-height: 800px}"
        )

        log.info("Get legend from URL:\n%s." % url)

        legend_buffer = StringIO()
        html_access_constraints = ""
        if access_constraints is not None and\
           len(access_constraints) > 0:
            h2_title = _("Access constraints")
            localizer = get_localizer(self.request)
            trans_h2_title = localizer.translate(h2_title)
            html_access_constraints = """
%(h2_title)s

            %(access_constraints)s""" % {
                'access_constraints': access_constraints,
                'h2_title': trans_h2_title}

        html = """<title>%(title)s</title>

%(title)s

        %(html_access_constraints)s
        """ % {
            'url': url,
            'title': title,
            'html_access_constraints': html_access_constraints}

        weasyprint.HTML(string=html).write_pdf(
            legend_buffer,
            stylesheets=[css]
        )
        return legend_buffer

    @cache_region.cache_on_arguments()
    def _get_legend(self, name, lang):
        css = weasyprint.CSS(
            string="img {max-height: 800px}"
        )

        url = \
            "http://wiki.geoportail.lu/doku.php?" \
            "id=%s:legend:%s&do=export_html" % \
            (lang, name)
        log.info("Get legend from URL:\n%s." % url)

        legend_buffer = StringIO()
        weasyprint.HTML(url).write_pdf(
            legend_buffer,
            stylesheets=[css]
        )
        return legend_buffer

    @view_config(route_name="lux_printproxy_report_get")
    def lux_report_get(self):
        ref = self.request.matchdict.get("ref")
        job = DBSession.query(LuxPrintJob).get(ref)
        if job is None:
            return HTTPNotFound()
        try:
            resp, content = self._proxy("%s/report/%s" % (
                job.print_url, ref
            ))

            attributes = json.loads(job.spec)["attributes"]
            is_pdf = json.loads(job.spec)["format"] == "pdf"
            print_title = attributes.get("name")
            if print_title is None or len(print_title) == 0:
                print_title = "map_geoportal_lu"
            print_title = re.sub(r" ", "_", print_title)
            print_title = re.sub(r"[^a-zA-Z0-9\-\_]", "", print_title)

            if is_pdf and "legend" in attributes and\
                    attributes["legend"] is not None:
                merger = PdfFileMerger(strict=False)
                merger.append(StringIO(content))

                lang = attributes.get("lang")

                for item in attributes["legend"]:
                    if "legendUrl" in item and item["legendUrl"] is not None:
                        legend_title = ""
                        if "legendTitle" in item and\
                           item["legendTitle"] is not None:
                            legend_title = item["legendTitle"]
                        access_constraints = ""
                        if "accessConstraints" in item and\
                           item["accessConstraints"] is not None:
                            access_constraints = item["accessConstraints"]
                        merger.append(
                            self._create_legend_from_image(
                                item["legendUrl"],
                                legend_title,
                                access_constraints))
                    elif "name" in item and item["name"] is not None:
                        merger.append(self._get_legend(item["name"], lang))

                content = StringIO()
                merger.write(content)
                content = content.getvalue()

            if is_pdf and "queryResults" in attributes and\
                    attributes["queryResults"] is not None:
                css = weasyprint.CSS(
                    string=".ng-hide {display: none !important;} " +
                           ".no-print {display: none !important;} " +
                           "body {font-size: 60%;} "
                )
                merger = PdfFileMerger(strict=False)
                merger.append(StringIO(content))
                query_results = StringIO()
                weasyprint.HTML(string=attributes["queryResults"]).write_pdf(
                    query_results,
                    stylesheets=[css]
                )
                merger.append(query_results)

                content = StringIO()
                merger.write(content)
                content = content.getvalue()

            DBSession.delete(job)
            if is_pdf:
                resp["content-disposition"] =\
                    "attachment; filename=%s.pdf" % (str(print_title))
            else:
                resp["content-disposition"] =\
                    "attachment; filename=%s.png" % (str(print_title))

            return self._build_response(
                resp, content, NO_CACHE, "print"
            )
        except Exception as e:
            log.exception(e)
            log.error("reference is : " + ref)
            if job is not None:
                job.is_error = True
            return HTTPInternalServerError()
<commit_msg>Bypass ssl error<commit_after># -*- coding: utf-8 -*-

# Copyright (c) 2011-2015, Camptocamp SA
# All rights reserved.

# Redistribution and use in source and
# binary forms, with or without
# modification, are permitted provided
# that the following conditions are met:

# 1.    Redistributions of source code must
#       retain the above copyright notice, this
#       list of conditions and the following disclaimer.
# 2.    Redistributions in binary form mustg
#       reproduce the above copyright notice,
#       this list of conditions and theg
#       following disclaimer in the documentation
#       and/or other materials provided
#       with the distribution.

# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
# HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND
# FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
# OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

# The views and conclusions contained in the
# software and documentation are those
# of the authors and should not be interpreted
# as representing official policies,
# either expressed or implied, of the FreeBSD Project.

import json
import logging
import re
import random
import urllib
import urllib2
from cStringIO import StringIO
from datetime import datetime

from pyramid.i18n import get_localizer, TranslationStringFactory
from pyramid.view import view_config
from pyramid.httpexceptions import HTTPUnauthorized, HTTPInternalServerError
from pyramid.httpexceptions import HTTPNotFound

from PyPDF2 import PdfFileMerger
import weasyprint

from c2cgeoportal.models import RestrictionArea, Role, Layer
from c2cgeoportal.views.printproxy import PrintProxy
from c2cgeoportal.lib.caching import NO_CACHE, get_region

from geoportailv3.models import DBSession, LuxPrintJob, LuxLayerInternalWMS

_ = TranslationStringFactory("geoportailv3-server")
log = logging.getLogger(__name__)
cache_region = get_region()

class LuxPrintProxy(PrintProxy):

    @view_config(route_name="lux_printproxy_report_create")
    def lux_report_create(self):
        token = self.config["authtkt_secret"]
        print_urls = self.config["print_urls"]
        if print_urls is not None and len(print_urls) > 0:
            print_url = print_urls[random.randint(0, len(print_urls) - 1)]
        else:
            print_url = self.config["print_url"]
        spec = json.loads(self.request.body)
        for map_layer in spec["attributes"]["map"]["layers"]:
            if "baseURL" in map_layer and\
               "ogcproxywms" in map_layer["baseURL"]:
                if "customParams" in map_layer:
                    map_layer["customParams"]["GP_TOKEN"] = token
                else:
                    map_layer["customParams"] = {"GP_TOKEN": token}

                for layer in map_layer["layers"]:
                    internal_wms = DBSession.query(LuxLayerInternalWMS).filter(
                        LuxLayerInternalWMS.layer == layer).first()
                    if internal_wms is not None and\
                       not self._is_authorized(internal_wms):
                            return HTTPUnauthorized()
        if "longUrl" in spec["attributes"]:
            opener = urllib2.build_opener(urllib2.HTTPHandler())
            data = urllib.urlencode({"url": spec["attributes"]["longUrl"]})
            content = opener.open(
                "http://map.geoportail.lu/wsgi/short/create",
                data=data).read()
            shortner = json.loads(content)
            spec["attributes"]["url"] = shortner["short_url"]
            spec["attributes"]["qrimage"] =\
                "http://map.geoportail.lu/main/wsgi/qr?url=" + \
                spec["attributes"]["url"]

        job = LuxPrintJob()
        job.spec = json.dumps(spec)

        self.request.body = job.spec

        resp, content = self._proxy("%s/report.%s" % (
            print_url,
            self.request.matchdict.get("format")
        ))
        job.id = json.loads(content)["ref"]
        job.print_url = print_url
        job.creation = datetime.now()
        DBSession.add(job)
        return self._build_response(
            resp, content, False, "print"
        )

    @view_config(route_name="lux_printproxy_status")
    def lux_status(self):
        ref = self.request.matchdict.get("ref")
        job = DBSession.query(LuxPrintJob).get(ref)

        try:
            return self._proxy_response(
                "print",
                "%s/status/%s.json" % (
                    job.print_url,
                    ref
                ),
            )
        except Exception as e:
            log.exception(e)
            job.is_error = True
            return HTTPInternalServerError()

    def _is_authorized(self, internal_wms):
        if internal_wms.public:
            return True
        if self.request.user is None:
            return False
        else:
            restriction = DBSession.query(RestrictionArea).filter(
                RestrictionArea.roles.any(
                    Role.id == self.request.user.role.id)).filter(
                        RestrictionArea.layers.any(
                            Layer.id == internal_wms.id
                        )
                    ).first()
            # If not restriction is set then return unauthorized
            if restriction is None:
                return False
        return True

    @view_config(route_name="lux_printproxy_report_cancel")
    def lux_cancel(self):
        ref = self.request.matchdict.get("ref")
        job = DBSession.query(LuxPrintJob).get(ref)
        print_url = job.print_url
        DBSession.query(LuxPrintJob).filter(
            LuxPrintJob.id == ref
        ).delete()
        return self._proxy_response(
            "print",
            "%s/cancel/%s" % (
                print_url,
                ref
            ),
        )

    def _create_legend_from_image(self, url, title, access_constraints):
        css = weasyprint.CSS(
            string="img {max-height: 800px}"
        )

        log.info("Get legend from URL:\n%s." % url)

        legend_buffer = StringIO()
        html_access_constraints = ""
        if access_constraints is not None and\
           len(access_constraints) > 0:
            h2_title = _("Access constraints")
            localizer = get_localizer(self.request)
            trans_h2_title = localizer.translate(h2_title)
            html_access_constraints = """
%(h2_title)s

            %(access_constraints)s""" % {
                'access_constraints': access_constraints,
                'h2_title': trans_h2_title}

        html = """<title>%(title)s</title>

%(title)s

        %(html_access_constraints)s
        """ % {
            'url': url,
            'title': title,
            'html_access_constraints': html_access_constraints}

        weasyprint.HTML(string=html).write_pdf(
            legend_buffer,
            stylesheets=[css]
        )
        return legend_buffer

    @cache_region.cache_on_arguments()
    def _get_legend(self, name, lang):
        css = weasyprint.CSS(
            string="img {max-height: 800px}"
        )

        url = \
            "http://wiki.geoportail.lu/doku.php?" \
            "id=%s:legend:%s&do=export_html" % \
            (lang, name)
        log.info("Get legend from URL:\n%s." % url)

        legend_buffer = StringIO()
        weasyprint.HTML(url).write_pdf(
            legend_buffer,
            stylesheets=[css]
        )
        return legend_buffer

    @view_config(route_name="lux_printproxy_report_get")
    def lux_report_get(self):
        ref = self.request.matchdict.get("ref")
        job = DBSession.query(LuxPrintJob).get(ref)
        if job is None:
            return HTTPNotFound()
        try:
            resp, content = self._proxy("%s/report/%s" % (
                job.print_url, ref
            ))

            attributes = json.loads(job.spec)["attributes"]
            is_pdf = json.loads(job.spec)["format"] == "pdf"
            print_title = attributes.get("name")
            if print_title is None or len(print_title) == 0:
                print_title = "map_geoportal_lu"
            print_title = re.sub(r" ", "_", print_title)
            print_title = re.sub(r"[^a-zA-Z0-9\-\_]", "", print_title)

            if is_pdf and "legend" in attributes and\
                    attributes["legend"] is not None:
                merger = PdfFileMerger(strict=False)
                merger.append(StringIO(content))

                lang = attributes.get("lang")

                for item in attributes["legend"]:
                    if "legendUrl" in item and item["legendUrl"] is not None:
                        legend_title = ""
                        if "legendTitle" in item and\
                           item["legendTitle"] is not None:
                            legend_title = item["legendTitle"]
                        access_constraints = ""
                        if "accessConstraints" in item and\
                           item["accessConstraints"] is not None:
                            access_constraints = item["accessConstraints"]
                        merger.append(
                            self._create_legend_from_image(
                                item["legendUrl"],
                                legend_title,
                                access_constraints))
                    elif "name" in item and item["name"] is not None:
                        merger.append(self._get_legend(item["name"], lang))

                content = StringIO()
                merger.write(content)
                content = content.getvalue()

            if is_pdf and "queryResults" in attributes and\
                    attributes["queryResults"] is not None:
                css = weasyprint.CSS(
                    string=".ng-hide {display: none !important;} " +
                           ".no-print {display: none !important;} " +
                           "body {font-size: 60%;} "
                )
                merger = PdfFileMerger(strict=False)
                merger.append(StringIO(content))
                query_results = StringIO()
                weasyprint.HTML(string=attributes["queryResults"]).write_pdf(
                    query_results,
                    stylesheets=[css]
                )
                merger.append(query_results)

                content = StringIO()
                merger.write(content)
                content = content.getvalue()

            DBSession.delete(job)
            if is_pdf:
                resp["content-disposition"] =\
                    "attachment; filename=%s.pdf" % (str(print_title))
            else:
                resp["content-disposition"] =\
                    "attachment; filename=%s.png" % (str(print_title))

            return self._build_response(
                resp, content, NO_CACHE, "print"
            )
        except Exception as e:
            log.exception(e)
            log.error("reference is : " + ref)
            if job is not None:
                job.is_error = True
            return HTTPInternalServerError()
<|endoftext|>


<commit_before>from __future__ import absolute_import, division, print_function

import torch
import math
from gpytorch.models import AbstractVariationalGP
from gpytorch.mlls import AddedLossTerm
from gpytorch.likelihoods import GaussianLikelihood
from gpytorch import settings
from torch.nn.functional import softplus

class NegativeKLDivergence(AddedLossTerm):
    def __init__(self, variational_strategy):
        self.variational_strategy = variational_strategy

    def loss(self):
        return -1 * self.variational_strategy.kl_divergence().sum()

class AbstractDeepGPHiddenLayer(AbstractVariationalGP):
    def __init__(self, variational_strategy, input_dims, output_dims, num_samples):
        """
        Represents a layer in a deep GP where inference is performed via the doubly stochastic method of
        Salimbeni et al., 2017. Upon calling, instead of returning a variational distribution q(f), returns samples
        from the variational distribution.

        See the documentation for __call__ below for more details below. Note that the behavior of __call__
        will change to be much more elegant with multiple batch dimensions; however, the interface doesn't really
        change.

        Args:
            - variational_strategy (VariationalStrategy): Strategy for changing q(u) -> q(f) (see other VI docs)
            - input_dims (int): Dimensionality of input data expected by each GP
            - output_dims (int): Number of GPs in this layer, equivalent to output dimensionality.
            - last_layer (bool): True if this is to be the last layer in the deep GP, false otherwise.
            - num_samples (int): Number of samples to draw from q(f) for returning
        """
        super(AbstractDeepGPHiddenLayer, self).__init__(variational_strategy)
        self.input_dims = input_dims
        self.output_dims = output_dims
        self.num_samples = num_samples
        self.register_added_loss_term("hidden_kl_divergence")

    def forward(self, x):
        raise NotImplementedError

    def _reshape_input(self, inputs):
        inputs = inputs.contiguous()
        # Forward samples through the VariationalStrategy and return *samples* from q(f)
        if inputs.dim() == 2:
            # Assume new input entirely
            inputs = inputs.unsqueeze(0)
            inputs = inputs.expand(self.output_dims, inputs.size(-2), -1)
        elif inputs.dim() == 3:
            # Assume batch dim is samples, not output_dim
            inputs = inputs.unsqueeze(0)
            inputs = inputs.expand(self.output_dims, inputs.size(1), inputs.size(-2), -1)

        return inputs

    def __call__(self, inputs):
        """
        Forward data through this hidden GP layer.

        If the input is 2 dimensional, we pass the input through each hidden GP, resulting in a `h x n` batch
        Gaussian distribution. We then draw `s` samples from these Gaussians and reshape the result to be
        `s x n x h` (e.g., h becomes the dimensionality of the output).

        If the input is 3 dimensional, we assume that the input is `s x n x d`, e.g. the batch dimension
        corresponds to the number of samples. We use this as the number of samples to draw, and just propagate
        each sample through the hidden layer. The output will be `s x n x h`.

        If the input is 4 dimensional, we assume that for some reason the user has already reshaped things to be
        `h x s x n x d`. We reshape this internally to be `h x sn x d`, and the output will be `s x n x h`.

        The goal of these last two points is that if you have a tensor `x` that is `n x d`, then:
            >>> hidden_gp2(hidden_gp(x))

        will just work, and return a tensor of size `s x n x h2`, where `h2` is the output dimensionality of
        hidden_gp2. In this way, hidden GP layers are easily composable.
        """
        if inputs.size(-1) != self.input_dims:
            raise RuntimeError(
                f"Input shape did not match self.input_dims. Got total feature dims [{x.size(-1)}],"
                f" expected [{self.input_dims}]"
            )

        inputs = self._reshape_input(inputs)
        if inputs.dim() == 4:
            num_samples = inputs.size(-3)
            inputs = inputs.view(self.output_dims, inputs.size(-2) * inputs.size(-3), -1)
            reshape_output = True
        else:
            reshape_output = False
            num_samples = self.num_samples

        variational_dist_f = super(AbstractDeepGPHiddenLayer, self).__call__(inputs)
        mean_qf = variational_dist_f.mean
        std_qf = variational_dist_f.variance.sqrt()

        if reshape_output:
            samples = torch.distributions.Normal(mean_qf, std_qf).rsample()
            samples = samples.view(self.output_dims, num_samples, -1).permute(1, 2, 0)
        else:
            samples = torch.distributions.Normal(mean_qf, std_qf).rsample(torch.Size([num_samples]))
            samples = samples.transpose(-2, -1)

        loss_term = NegativeKLDivergence(self.variational_strategy)
        self.update_added_loss_term("hidden_kl_divergence", loss_term)

        return samples

class AbstractDeepGP(AbstractDeepGPHiddenLayer):
    def __init__(self, variational_strategy, input_dims, output_dims, num_samples):
        super(AbstractDeepGP, self).__init__(variational_strategy, input_dims, output_dims, num_samples)

    def forward(self, x):
        raise NotImplementedError

    def __call__(self, inputs):
        # Forward samples through the VariationalStrategy and return q(f)
        last_layer_inputs = inputs.contiguous().view(-1, self.input_dims)
        last_layer_inputs = last_layer_inputs.unsqueeze(0)
        last_layer_inputs = last_layer_inputs.expand(
            self.output_dims,
            self.num_samples * inputs.size(-2),
            self.input_dims
        )

        return AbstractVariationalGP.__call__(self, last_layer_inputs)

class DeepGaussianLikelihood(GaussianLikelihood):
    def __init__(
        self,
        num_samples,
        noise_prior=None,
        batch_size=1,
        param_transform=softplus,
        inv_param_transform=None,
        **kwargs
    ):
        # TODO: Rewrite to be a general DeepLikelihood wrapper once the remainder of GPyTorch supports multiple
        # batch dimensions.
        super(DeepGaussianLikelihood, self).__init__(
            noise_prior=None,
            batch_size=1,
            param_transform=softplus,
            inv_param_transform=None,
            **kwargs)
        self.num_samples = num_samples

    def variational_log_probability(self, input, target, **kwargs):
        mean, variance = input.mean, input.variance
        noise = self.noise_covar.noise
        num_outputs = mean.size(0)
        mean = mean.view(num_outputs, self.num_samples, -1)
        variance = variance.view(num_outputs, self.num_samples, -1)

        if mean.dim() > target.dim():
            target = target.unsqueeze(-1)

        if variance.ndimension() == 1:
            if settings.debug.on() and noise.size(0) > 1:
                raise RuntimeError("With batch_size > 1, expected a batched MultivariateNormal distribution.")
            noise = noise.squeeze(0)

        res = -0.5 * ((target - mean) ** 2 + variance) / noise
        res += -0.5 * noise.log() - 0.5 * math.log(2 * math.pi)
        return res.sum(-1).mean(-1)
<commit_msg>Update DeepGaussianLikelihood to new likelihood rework.<commit_after>from __future__ import absolute_import, division, print_function

import torch
import math
from gpytorch.models import AbstractVariationalGP
from gpytorch.mlls import AddedLossTerm
from gpytorch.likelihoods import GaussianLikelihood
from gpytorch import settings
from torch.nn.functional import softplus

class NegativeKLDivergence(AddedLossTerm):
    def __init__(self, variational_strategy):
        self.variational_strategy = variational_strategy

    def loss(self):
        return -1 * self.variational_strategy.kl_divergence().sum()

class AbstractDeepGPHiddenLayer(AbstractVariationalGP):
    def __init__(self, variational_strategy, input_dims, output_dims, num_samples):
        """
        Represents a layer in a deep GP where inference is performed via the doubly stochastic method of
        Salimbeni et al., 2017. Upon calling, instead of returning a variational distribution q(f), returns samples
        from the variational distribution.

        See the documentation for __call__ below for more details below. Note that the behavior of __call__
        will change to be much more elegant with multiple batch dimensions; however, the interface doesn't really
        change.

        Args:
            - variational_strategy (VariationalStrategy): Strategy for changing q(u) -> q(f) (see other VI docs)
            - input_dims (int): Dimensionality of input data expected by each GP
            - output_dims (int): Number of GPs in this layer, equivalent to output dimensionality.
            - last_layer (bool): True if this is to be the last layer in the deep GP, false otherwise.
            - num_samples (int): Number of samples to draw from q(f) for returning
        """
        super(AbstractDeepGPHiddenLayer, self).__init__(variational_strategy)
        self.input_dims = input_dims
        self.output_dims = output_dims
        self.num_samples = num_samples
        self.register_added_loss_term("hidden_kl_divergence")

    def forward(self, x):
        raise NotImplementedError

    def _reshape_input(self, inputs):
        inputs = inputs.contiguous()
        # Forward samples through the VariationalStrategy and return *samples* from q(f)
        if inputs.dim() == 2:
            # Assume new input entirely
            inputs = inputs.unsqueeze(0)
            inputs = inputs.expand(self.output_dims, inputs.size(-2), -1)
        elif inputs.dim() == 3:
            # Assume batch dim is samples, not output_dim
            inputs = inputs.unsqueeze(0)
            inputs = inputs.expand(self.output_dims, inputs.size(1), inputs.size(-2), -1)

        return inputs

    def __call__(self, inputs):
        """
        Forward data through this hidden GP layer.

        If the input is 2 dimensional, we pass the input through each hidden GP, resulting in a `h x n` batch
        Gaussian distribution. We then draw `s` samples from these Gaussians and reshape the result to be
        `s x n x h` (e.g., h becomes the dimensionality of the output).

        If the input is 3 dimensional, we assume that the input is `s x n x d`, e.g. the batch dimension
        corresponds to the number of samples. We use this as the number of samples to draw, and just propagate
        each sample through the hidden layer. The output will be `s x n x h`.

        If the input is 4 dimensional, we assume that for some reason the user has already reshaped things to be
        `h x s x n x d`. We reshape this internally to be `h x sn x d`, and the output will be `s x n x h`.

        The goal of these last two points is that if you have a tensor `x` that is `n x d`, then:
            >>> hidden_gp2(hidden_gp(x))

        will just work, and return a tensor of size `s x n x h2`, where `h2` is the output dimensionality of
        hidden_gp2. In this way, hidden GP layers are easily composable.
        """
        if inputs.size(-1) != self.input_dims:
            raise RuntimeError(
                f"Input shape did not match self.input_dims. Got total feature dims [{x.size(-1)}],"
                f" expected [{self.input_dims}]"
            )

        inputs = self._reshape_input(inputs)
        if inputs.dim() == 4:
            num_samples = inputs.size(-3)
            inputs = inputs.view(self.output_dims, inputs.size(-2) * inputs.size(-3), -1)
            reshape_output = True
        else:
            reshape_output = False
            num_samples = self.num_samples

        variational_dist_f = super(AbstractDeepGPHiddenLayer, self).__call__(inputs)
        mean_qf = variational_dist_f.mean
        std_qf = variational_dist_f.variance.sqrt()

        if reshape_output:
            samples = torch.distributions.Normal(mean_qf, std_qf).rsample()
            samples = samples.view(self.output_dims, num_samples, -1).permute(1, 2, 0)
        else:
            samples = torch.distributions.Normal(mean_qf, std_qf).rsample(torch.Size([num_samples]))
            samples = samples.transpose(-2, -1)

        loss_term = NegativeKLDivergence(self.variational_strategy)
        self.update_added_loss_term("hidden_kl_divergence", loss_term)

        return samples

class AbstractDeepGP(AbstractDeepGPHiddenLayer):
    def __init__(self, variational_strategy, input_dims, output_dims, num_samples):
        super(AbstractDeepGP, self).__init__(variational_strategy, input_dims, output_dims, num_samples)

    def forward(self, x):
        raise NotImplementedError

    def __call__(self, inputs):
        # Forward samples through the VariationalStrategy and return q(f)
        last_layer_inputs = inputs.contiguous().view(-1, self.input_dims)
        last_layer_inputs = last_layer_inputs.unsqueeze(0)
        last_layer_inputs = last_layer_inputs.expand(
            self.output_dims,
            self.num_samples * inputs.size(-2),
            self.input_dims
        )

        return AbstractVariationalGP.__call__(self, last_layer_inputs)

class DeepGaussianLikelihood(GaussianLikelihood):
    def __init__(
        self,
        num_samples,
        noise_prior=None,
        batch_size=1,
        param_transform=softplus,
        inv_param_transform=None,
        **kwargs
    ):
        # TODO: Rewrite to be a general DeepLikelihood wrapper once the remainder of GPyTorch supports multiple
        # batch dimensions.
        super(DeepGaussianLikelihood, self).__init__(
            noise_prior=None,
            batch_size=1,
            param_transform=softplus,
            inv_param_transform=None,
            **kwargs)
        self.num_samples = num_samples

    def expected_log_prob(self, target, input, **kwargs):
        mean, variance = input.mean, input.variance
        noise = self.noise_covar.noise
        num_outputs = mean.size(0)
        mean = mean.view(num_outputs, self.num_samples, -1)
        variance = variance.view(num_outputs, self.num_samples, -1)

        if mean.dim() > target.dim():
            target = target.unsqueeze(-1)

        if variance.ndimension() == 1:
            if settings.debug.on() and noise.size(0) > 1:
                raise RuntimeError("With batch_size > 1, expected a batched MultivariateNormal distribution.")
            noise = noise.squeeze(0)

        res = -0.5 * ((target - mean) ** 2 + variance) / noise
        res += -0.5 * noise.log() - 0.5 * math.log(2 * math.pi)
        return res.sum(-1).mean(-1)
<|endoftext|>


<commit_before>#!/usr/bin/env python3
# This bot was made by tkdrg.
# Ask #coderbus@irc.rizon.net if this breaks.
# See LICENSE-bot_folder.txt for the license of the files in this folder.
from config import *
import collections
import time
import pickle
import socket
import sys
import threading

def print_err(*msg):
    print(*msg, file=sys.stderr)

def setup_irc_socket():
    s = socket.socket()
    s.settimeout(240)

    while 1:
        try:
            s.connect((server, port))
        except socket.error:
            print_err("Unable to connect to server {0}:{1}, attempting to reconnect in 20 seconds.".format(server, port))
            time.sleep(20)
        else:
            print_err("Connection established to server {0}:{1}.".format(server, port))
            break

    s.send(bytes("NICK {0}\r\n".format(nick), "UTF-8"))
    s.send(bytes("USER {0} {1} {2} :{3}\r\n".format(ident, server, name, realname), "UTF-8"))
    time.sleep(5)
    s.send(bytes("PRIVMSG NickServ :IDENTIFY {0}\r\n".format(password), "UTF-8"))
    time.sleep(5)
    for channel in channels:
        s.send(bytes("JOIN {0}\r\n".format(channel), "UTF-8"))
    return s

def setup_nudge_socket():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind(("", 45678))  # localhost:45678
    s.listen(5)
    return s

def nudge_handler(irc):
    nudge = setup_nudge_socket()
    message_queue = collections.deque()
    while 1:
        if len(message_queue):
            message = message_queue.popleft()
        else:
            try:
                s, ip = nudge.accept()
            except:
                print_err("Nudge socket lost, attempting to reopen.")
                nudge = setup_nudge_socket()
                continue
            rawdata = s.recv(1024)
            s.close()
            data = pickle.loads(rawdata)
            print(data)
            if data["ip"][0] == "#":
                message = "{0} :AUTOMATIC ANNOUNCEMENT : {1}\r\n".format(data["ip"], str(" ".join(data["data"])))
            else:
                message = "{0} :AUTOMATIC ANNOUNCEMENT : {1} | {2}\r\n".format(defaultchannel, data["ip"], str(" ".join(data["data"])))
        try:
            irc.send(bytes("PRIVMSG {0}".format(message), "UTF-8"))
        except:
            print_err("Nudge received without IRC socket, appending to queue.")
            print_err("Message: {0}".format(message))
            message_queue.append(message)

def irc_handler(irc):
    while 1:
        try:
            buf = irc.recv(1024).decode("UTF-8").split("\n")
            for i in buf:
                print(i)
                if i[0:4] == "PING":
                    irc.send(bytes("PONG {0}\r\n".format(i[5:]), "UTF-8"))
                else:
                    l = i.split(" ")
                    if len(l) < 2:
                        continue
                    elif l[1] == "477":
                        print_err("Error: Nickname was not registered when joining {0}. Reauthing and retrying...".format(l[3]))
                        irc.send(bytes("PRIVMSG NickServ :IDENTIFY {0}\r\n".format(password), "UTF-8"))
                        time.sleep(5)
                        s.send(bytes("JOIN {0}\r\n".format(l[3]), "UTF-8"))
                    elif l[1] == "433":
                        print_err("Error: Nickname already in use. Attempting to use alt nickname if available, sleeping 60s otherwise...")
                        if(altnick):
                            s.send(bytes("NICK {0}\r\n".format(altnick), "UTF-8"))
                        else:
                            time.sleep(60)
                            irc = setup_irc_socket()
        except:
            print("Lost connection to IRC server.")
            irc = setup_irc_socket()

if __name__ == "__main__":
    irc = setup_irc_socket()
    t = threading.Thread(target=nudge_handler, args=(irc,))
    t.daemon = True
    t.start()
    irc_handler(irc)
<commit_msg>Fixes the irc bot and converts it to tabs.<commit_after>#!/usr/bin/env python3
# This bot was made by tkdrg.
# Ask #coderbus@irc.rizon.net if this breaks.
# See LICENSE-bot_folder.txt for the license of the files in this folder.
from config import *
import collections
import time
import pickle
import socket
import sys
import threading

global irc

def print_err(*msg):
	print(*msg, file=sys.stderr)

def setup_irc_socket():
	s = socket.socket()
	s.settimeout(240)

	while 1:
		try:
			s.connect((server, port))
		except socket.error:
			print_err("Unable to connect to server {0}:{1}, attempting to reconnect in 20 seconds.".format(server, port))
			time.sleep(20)
		else:
			print_err("Connection established to server {0}:{1}.".format(server, port))
			break

	s.send(bytes("NICK {0}\r\n".format(nick), "UTF-8"))
	s.send(bytes("USER {0} {1} {2} :{3}\r\n".format(ident, server, name, realname), "UTF-8"))
	return s

def setup_nudge_socket():
	s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	s.bind(("", 45678))  # localhost:45678
	s.listen(5)
	return s

def nudge_handler():
	global irc
	nudge = setup_nudge_socket()
	message_queue = collections.deque()
	while 1:
		if len(message_queue):
			message = message_queue.popleft()
		else:
			try:
				s, ip = nudge.accept()
			except:
				print_err("Nudge socket lost, attempting to reopen.")
				nudge = setup_nudge_socket()
				continue
			rawdata = s.recv(1024)
			s.close()
			data = pickle.loads(rawdata)
			print(data)
			if data["ip"][0] == "#":
				message = "{0} :AUTOMATIC ANNOUNCEMENT : {1}\r\n".format(data["ip"], str(" ".join(data["data"])))
			else:
				message = "{0} :AUTOMATIC ANNOUNCEMENT : {1} | {2}\r\n".format(defaultchannel, data["ip"], str(" ".join(data["data"])))
		try:
			irc.send(bytes("PRIVMSG {0}".format(message), "UTF-8"))
		except:
			print_err("Nudge received without IRC socket, appending to queue.")
			print_err("Message: {0}".format(message))
			message_queue.append(message)

def irc_handler():
	global irc
	while 1:
		try:
			buf = irc.recv(1024).decode("UTF-8").split("\n")
			for i in buf:
				print(i)
				if i[0:4] == "PING":
					irc.send(bytes("PONG {0}\r\n".format(i[5:]), "UTF-8"))
				else:
					l = i.split(" ")
					if len(l) < 2:
						continue
					elif l[1] == "001":
						print_err("connected and registered, identifing and joining channels")
						irc.send(bytes("PRIVMSG NickServ :IDENTIFY {0}\r\n".format(password), "UTF-8"))
						time.sleep(1)
						for channel in channels:
							irc.send(bytes("JOIN {0}\r\n".format(channel), "UTF-8"))
					elif l[1] == "477":
						print_err("Error: Nickname was not registered when joining {0}. Reauthing and retrying...".format(l[3]))
						irc.send(bytes("PRIVMSG NickServ :IDENTIFY {0}\r\n".format(password), "UTF-8"))
						time.sleep(5)
						irc.send(bytes("JOIN {0}\r\n".format(l[3]), "UTF-8"))
					elif l[1] == "433":
						print_err("Error: Nickname already in use. Attempting to use alt nickname if available, sleeping 60s otherwise...")
						if(altnick):
							irc.send(bytes("NICK {0}\r\n".format(altnick), "UTF-8"))
						else:
							time.sleep(60)
							irc = setup_irc_socket()
		except:
			print_err("Lost connection to IRC server.")
			irc = setup_irc_socket()

if __name__ == "__main__":
	irc = setup_irc_socket()
	t = threading.Thread(target=nudge_handler)
	t.daemon = True
	t.start()
	irc_handler()
<|endoftext|>


<commit_before>import os
import pickle
import random

from irc import Dispatcher, IRCBot

class MarkovDispatcher(Dispatcher):
    """
    Hacking on a markov chain bot - based on:
    http://code.activestate.com/recipes/194364-the-markov-chain-algorithm/
    """
    max_words = 10
    filename = 'markov.db'

    def __init__(self, *args, **kwargs):
        super(MarkovDispatcher, self).__init__(*args, **kwargs)
        self.load_data()

    def load_data(self):
        if os.path.exists(self.filename):
            fh = open(self.filename, 'rb')
            self.word_table = pickle.loads(fh.read())
            fh.close()
        else:
            self.word_table = {}

    def save_data(self):
        fh = open(self.filename, 'w')
        fh.write(pickle.dumps(self.word_table))
        fh.close()

    def split_message(self, message):
        words = message.split()
        for i in range(len(words) - 2):
            yield (words[i], words[i+1], words[i+2])

    def generate_message(self, person, size=15):
        person_words = len(self.word_table.get(person, []))
        if person_words < size:
            return

        rand_key = random.randint(0, person_words - 1)
        w1, w2 = self.word_table[person].keys()[rand_key]

        gen_words = []
        for i in xrange(size):
            gen_words.append(w1)
            try:
                w1, w2 = w2, random.choice(self.word_table[person][(w1, w2)])
                gen_words.append(w2)
            except KeyError:
                break

        return ' '.join(gen_words)

    def imitate(self, sender, message, channel, is_ping, reply):
        if is_ping:
            person = message.replace('imitate ', '').strip()
            return self.generate_message(person)

    def log(self, sender, message, channel, is_ping, reply):
        self.word_table.setdefault(sender, {})
        for w1, w2, w3 in self.split_message(message):
            key = (w1, w2)
            if key in self.word_table:
                self.word_table[sender][key].append(w3)
            else:
                self.word_table[sender][key] = [w3]

    def get_patterns(self):
        return (
            ('^imitate \S+', self.imitate),
            ('.*', self.log),
        )

host = 'irc.freenode.net'
port = 6667
nick = 'whatyousay'

markov = MarkovDispatcher()
greeter = IRCBot(host, port, nick, ['#lawrence'], [markov])
greeter.run_forever()
markov.save_data()
<commit_msg>Making some improvements to the markov bot including bumping the chain length up to 3<commit_after>import os
import pickle
import random

from irc import Dispatcher, IRCBot

class MarkovDispatcher(Dispatcher):
    """
    Hacking on a markov chain bot - based on:
    http://code.activestate.com/recipes/194364-the-markov-chain-algorithm/
    http://github.com/ericflo/yourmomdotcom
    """
    max_words = 10
    chain_length = 3
    stop_word = '\n'
    filename = 'markov.db'

    def __init__(self, *args, **kwargs):
        super(MarkovDispatcher, self).__init__(*args, **kwargs)
        self.load_data()

    def load_data(self):
        if os.path.exists(self.filename):
            fh = open(self.filename, 'rb')
            self.word_table = pickle.loads(fh.read())
            fh.close()
        else:
            self.word_table = {}

    def save_data(self):
        fh = open(self.filename, 'w')
        fh.write(pickle.dumps(self.word_table))
        fh.close()

    def split_message(self, message):
        words = message.split()
        if len(words) > self.chain_length:
            words.extend([self.stop_word] * self.chain_length)
            for i in range(len(words) - self.chain_length):
                yield (words[i:i + self.chain_length + 1])

    def generate_message(self, person, size=15):
        person_words = len(self.word_table.get(person, []))
        if person_words < size:
            return

        rand_key = random.randint(0, person_words - 1)
        words = self.word_table[person].keys()[rand_key]

        gen_words = []
        for i in xrange(size):
            if words[0] == self.stop_word:
                break

            gen_words.append(words[0])
            try:
                words = words[1:] + (random.choice(self.word_table[person][words]),)
            except KeyError:
                break

        return ' '.join(gen_words)

    def imitate(self, sender, message, channel, is_ping, reply):
        if is_ping:
            person = message.replace('imitate ', '').strip()
            return self.generate_message(person)

    def log(self, sender, message, channel, is_ping, reply):
        self.word_table.setdefault(sender, {})
        for words in self.split_message(message):
            key = tuple(words[:-1])
            if key in self.word_table:
                self.word_table[sender][key].append(words[-1])
            else:
                self.word_table[sender][key] = [words[-1]]

    def get_patterns(self):
        return (
            ('^imitate \S+', self.imitate),
            ('.*', self.log),
        )

host = 'irc.freenode.net'
port = 6667
nick = 'whatyousay'

markov = MarkovDispatcher()
greeter = IRCBot(host, port, nick, ['#lawrence-botwars'], [markov])
greeter.run_forever()
markov.save_data()
<|endoftext|>