<commit_before>from blaze.interactive import Data, compute, concrete_head, expr_repr, to_html

import datetime
from odo import into, append
from odo.backends.csv import CSV
from blaze import discover
from blaze.compute.core import compute
from blaze.compute.python import compute
from blaze.expr import symbol
from datashape import dshape
from blaze.utils import tmpfile, example
from blaze.compatibility import xfail
import pytest
import sys
from types import MethodType

import pandas as pd
import pandas.util.testing as tm
import numpy as np

data = (('Alice', 100),
        ('Bob', 200))

L = [[1, 'Alice',   100],
     [2, 'Bob',    -200],
     [3, 'Charlie', 300],
     [4, 'Denis',   400],
     [5, 'Edith',  -500]]

t = Data(data, fields=['name', 'amount'])

x = np.ones((2, 2))

def test_table_raises_on_inconsistent_inputs():
    with pytest.raises(ValueError):
        t = Data(data, schema='{name: string, amount: float32}',
            dshape=dshape("{name: string, amount: float32}"))

def test_resources():
    assert t._resources() == {t: t.data}

def test_resources_fail():
    t = symbol('t', 'var * {x: int, y: int}')
    d = t[t['x'] > 100]
    with pytest.raises(ValueError):
        compute(d)

def test_compute_on_Data_gives_back_data():
    assert compute(Data([1, 2, 3])) == [1, 2, 3]

def test_len():
    assert len(t) == 2
    assert len(t.name) == 2

def test_compute():
    assert list(compute(t['amount'] + 1)) == [101, 201]

def test_create_with_schema():
    t = Data(data, schema='{name: string, amount: float32}')
    assert t.schema == dshape('{name: string, amount: float32}')

def test_create_with_raw_data():
    t = Data(data, fields=['name', 'amount'])
    assert t.schema == dshape('{name: string, amount: int64}')
    assert t.name
    assert t.data == data

def test_repr():
    result = expr_repr(t['name'])
    print(result)
    assert isinstance(result, str)
    assert 'Alice' in result
    assert 'Bob' in result
    assert '...' not in result

    result = expr_repr(t['amount'] + 1)
    print(result)
    assert '101' in result

    t2 = Data(tuple((i, i**2) for i in range(100)), fields=['x', 'y'])
    assert t2.dshape == dshape('100 * {x: int64, y: int64}')

    result = expr_repr(t2)
    print(result)
    assert len(result.split('\n')) < 20
    assert '...' in result

def test_repr_of_scalar():
    assert repr(t.amount.sum()) == '300'

def test_mutable_backed_repr():
    mutable_backed_table = Data([[0]], fields=['col1'])
    repr(mutable_backed_table)

def test_dataframe_backed_repr():
    df = pd.DataFrame(data=[0], columns=['col1'])
    dataframe_backed_table = Data(df)
    repr(dataframe_backed_table)

def test_dataframe_backed_repr_complex():
    df = pd.DataFrame([(1, 'Alice', 100),
                       (2, 'Bob', -200),
                       (3, 'Charlie', 300),
                       (4, 'Denis', 400),
                       (5, 'Edith', -500)],
                      columns=['id', 'name', 'balance'])
    t = Data(df)
    repr(t[t['balance'] < 0])

def test_repr_html_on_no_resources_symbol():
    t = symbol('t', '5 * {id: int, name: string, balance: int}')
    assert to_html(t) == 't'

def test_expr_repr_empty():
    s = repr(t[t.amount > 1e9])
    assert isinstance(s, str)
    assert 'amount' in s

def test_to_html():
    s = to_html(t)
    assert s
    assert 'Alice' in s
    assert '<table' in s

    assert to_html(1) == '1'

    assert to_html(t.count()) == '2'

def test_to_html_on_arrays():
    s = to_html(Data(np.ones((2, 2))))
    assert '1' in s
    assert 'br>' in s

def test_repr_html():
    assert '<table' in t._repr_html_()
    assert '<table' in t.name._repr_html_()

def test_into():
    assert into(list, t) == into(list, data)

def test_serialization():
    import pickle
    t2 = pickle.loads(pickle.dumps(t))

    assert t.schema == t2.schema
    assert t._name == t2._name

def test_table_resource():
    with tmpfile('csv') as filename:
        ds = dshape('var * {a: int, b: int}')
        csv = CSV(filename)
        append(csv, [[1, 2], [10, 20]], dshape=ds)

        t = Data(filename)
        assert isinstance(t.data, CSV)
        assert into(list, compute(t)) == into(list, csv)

def test_concretehead_failure():
    t = symbol('t', 'var * {x:int, y:int}')
    d = t[t['x'] > 100]
    with pytest.raises(ValueError):
        concrete_head(d)

def test_into_np_ndarray_column():
    t = Data(L, fields=['id', 'name', 'balance'])
    expr = t[t.balance < 0].name
    colarray = into(np.ndarray, expr)
    assert len(list(compute(expr))) == len(colarray)

def test_into_nd_array_selection():
    t = Data(L, fields=['id', 'name', 'balance'])
    expr = t[t['balance'] < 0]
    selarray = into(np.ndarray, expr)
    assert len(list(compute(expr))) == len(selarray)

def test_into_nd_array_column_failure():
    tble = Data(L, fields=['id', 'name', 'balance'])
    expr = tble[tble['balance'] < 0]
    colarray = into(np.ndarray, expr)
    assert len(list(compute(expr))) == len(colarray)

def test_Data_attribute_repr():
    t = Data(CSV(example('accounts-datetimes.csv')))
    result = t.when.day
    expected = pd.DataFrame({'when_day': [1,2,3,4,5]})
    assert repr(result) == repr(expected)

def test_can_trivially_create_csv_Data():
    Data(example('iris.csv'))

    # in context
    with Data(example('iris.csv')) as d:
        assert d is not None

def test_can_trivially_create_csv_Data_with_unicode():
    if sys.version[0] == '2':
        assert isinstance(Data(example(u'iris.csv')).data, CSV)

def test_can_trivially_create_sqlite_table():
    pytest.importorskip('sqlalchemy')
    Data('sqlite:///'+example('iris.db')+'::iris')

    # in context
    with Data('sqlite:///'+example('iris.db')+'::iris') as d:
        assert d is not None

@xfail(reason="h5py/pytables mismatch")
def test_can_trivially_create_pytables():
    pytest.importorskip('tables')
    with Data(example('accounts.h5')+'::/accounts') as d:
        assert d is not None

def test_data_passes_kwargs_to_resource():
    assert Data(example('iris.csv'), encoding='ascii').data.encoding == 'ascii'

def test_data_on_iterator_refies_data():
    data = [1, 2, 3]
    d = Data(iter(data))

    assert into(list, d) == data
    assert into(list, d) == data

    # in context
    with Data(iter(data)) as d:
        assert d is not None

def test_Data_on_json_is_concrete():
    d = Data(example('accounts-streaming.json'))

    assert compute(d.amount.sum()) == 100 - 200 + 300 + 400 - 500
    assert compute(d.amount.sum()) == 100 - 200 + 300 + 400 - 500

def test_repr_on_nd_array_doesnt_err():
    d = Data(np.ones((2, 2, 2)))
    repr(d + 1)

def test_generator_reprs_concretely():
    x = [1, 2, 3, 4, 5, 6]
    d = Data(x)
    expr = d[d > 2] + 1
    assert '4' in repr(expr)

def test_incompatible_types():
    d = Data(pd.DataFrame(L, columns=['id', 'name', 'amount']))

    with pytest.raises(ValueError):
        d.id == 'foo'

    result = compute(d.id == 3)
    expected = pd.Series([False, False, True, False, False], name='id')
    tm.assert_series_equal(result, expected)

def test___array__():
    x = np.ones(4)
    d = Data(x)
    assert (np.array(d + 1) == x + 1).all()

    d = Data(x[:2])
    x[2:] = d + 1
    assert x.tolist() == [1, 1, 2, 2]

def test_python_scalar_protocols():
    d = Data(1)
    assert int(d + 1) == 2
    assert float(d + 1.0) == 2.0
    assert bool(d > 0) is True
    assert complex(d + 1.0j) == 1 + 1.0j

def test_iter():
    x = np.ones(4)
    d = Data(x)
    assert list(d + 1) == [2, 2, 2, 2]

@xfail(reason="DataFrame constructor doesn't yet support __array__")
def test_DataFrame():
    x = np.array([(1, 2), (1., 2.)], dtype=[('a', 'i4'), ('b', 'f4')])
    d = Data(x)
    assert isinstance(pd.DataFrame(d), pd.DataFrame)

def test_head_compute():
    data = tm.makeMixedDataFrame()
    t = symbol('t', discover(data))
    db = into('sqlite:///:memory:::t', data, dshape=t.dshape)
    n = 2
    d = Data(db)

    # skip the header and the ... at the end of the repr
    expr = d.head(n)
    s = repr(expr)
    assert '...' not in s
    result = s.split('\n')[1:]
    assert len(result) == n

def test_scalar_sql_compute():
    t = into('sqlite:///:memory:::t', data,
            dshape=dshape('var * {name: string, amount: int}'))
    d = Data(t)
    assert repr(d.amount.sum()) == '300'

def test_no_name_for_simple_data():
    d = Data([1, 2, 3])
    assert repr(d) == '    \n0  1\n1  2\n2  3'
    assert not d._name

    d = Data(1)
    assert not d._name
    assert repr(d) == '1'

def test_coerce_date_and_datetime():
    x = datetime.datetime.now().date()
    d = Data(x)
    assert repr(d) == repr(x)

    x = datetime.datetime.now()
    d = Data(x)
    assert repr(d) == repr(x)

def test_highly_nested_repr():
    data = [[0, [[1, 2], [3]], 'abc']]
    d = Data(data)
    assert 'abc' in repr(d.head())

def test_asarray_fails_on_different_column_names():
    vs = {'first': [2., 5., 3.],
          'second': [4., 1., 4.],
          'third': [6., 4., 3.]}
    df = pd.DataFrame(vs)
    with pytest.raises(ValueError):
        Data(df, fields=list('abc'))

def test_data_does_not_accept_columns_kwarg():
    with pytest.raises(ValueError):
        Data([(1, 2), (3, 4)], columns=list('ab'))

def test_functions_as_bound_methods():
    """
    Test that all functions on an InteractiveSymbol are instance methods
    of that object.
    """
    # Filter out __class__ and friends that are special, these can be
    # callables without being instance methods.
    callable_attrs = filter(
        callable,
        (getattr(t, a, None) for a in dir(t) if not a.startswith('__')),
    )
    for attr in callable_attrs:
        assert isinstance(attr, MethodType)
        # Make sure this is bound to the correct object.
        assert attr.__self__ is t
<commit_msg>Add regression test for issue #1240.<commit_after>from blaze.interactive import Data, compute, concrete_head, expr_repr, to_html

import datetime
from re import sub
from odo import into, append
from odo.backends.csv import CSV
from blaze import discover, transform
from blaze.compute.core import compute
from blaze.compute.python import compute
from blaze.expr import symbol
from datashape import dshape
from blaze.utils import tmpfile, example
from blaze.compatibility import xfail
import pytest
import sys
from types import MethodType

import pandas as pd
import pandas.util.testing as tm
import numpy as np

data = (('Alice', 100),
        ('Bob', 200))

L = [[1, 'Alice',   100],
     [2, 'Bob',    -200],
     [3, 'Charlie', 300],
     [4, 'Denis',   400],
     [5, 'Edith',  -500]]

t = Data(data, fields=['name', 'amount'])

x = np.ones((2, 2))

def test_table_raises_on_inconsistent_inputs():
    with pytest.raises(ValueError):
        t = Data(data, schema='{name: string, amount: float32}',
            dshape=dshape("{name: string, amount: float32}"))

def test_resources():
    assert t._resources() == {t: t.data}

def test_resources_fail():
    t = symbol('t', 'var * {x: int, y: int}')
    d = t[t['x'] > 100]
    with pytest.raises(ValueError):
        compute(d)

def test_compute_on_Data_gives_back_data():
    assert compute(Data([1, 2, 3])) == [1, 2, 3]

def test_len():
    assert len(t) == 2
    assert len(t.name) == 2

def test_compute():
    assert list(compute(t['amount'] + 1)) == [101, 201]

def test_create_with_schema():
    t = Data(data, schema='{name: string, amount: float32}')
    assert t.schema == dshape('{name: string, amount: float32}')

def test_create_with_raw_data():
    t = Data(data, fields=['name', 'amount'])
    assert t.schema == dshape('{name: string, amount: int64}')
    assert t.name
    assert t.data == data

def test_repr():
    result = expr_repr(t['name'])
    print(result)
    assert isinstance(result, str)
    assert 'Alice' in result
    assert 'Bob' in result
    assert '...' not in result

    result = expr_repr(t['amount'] + 1)
    print(result)
    assert '101' in result

    t2 = Data(tuple((i, i**2) for i in range(100)), fields=['x', 'y'])
    assert t2.dshape == dshape('100 * {x: int64, y: int64}')

    result = expr_repr(t2)
    print(result)
    assert len(result.split('\n')) < 20
    assert '...' in result

def test_str_does_not_repr():
    # see GH issue #1240.
    d = Data([('aa', 1), ('b', 2)], dshape='2 * {a: string, b: int64}')
    expr = transform(d, c=d.a.strlen() + d.b)
    assert sub(r'_\d+', 'XXX', str(expr)) == "Merge(_child=XXX, children=(XXX, label(strlen(_child=XXX.a) + XXX.b, 'c')))"

def test_repr_of_scalar():
    assert repr(t.amount.sum()) == '300'

def test_mutable_backed_repr():
    mutable_backed_table = Data([[0]], fields=['col1'])
    repr(mutable_backed_table)

def test_dataframe_backed_repr():
    df = pd.DataFrame(data=[0], columns=['col1'])
    dataframe_backed_table = Data(df)
    repr(dataframe_backed_table)

def test_dataframe_backed_repr_complex():
    df = pd.DataFrame([(1, 'Alice', 100),
                       (2, 'Bob', -200),
                       (3, 'Charlie', 300),
                       (4, 'Denis', 400),
                       (5, 'Edith', -500)],
                      columns=['id', 'name', 'balance'])
    t = Data(df)
    repr(t[t['balance'] < 0])

def test_repr_html_on_no_resources_symbol():
    t = symbol('t', '5 * {id: int, name: string, balance: int}')
    assert to_html(t) == 't'

def test_expr_repr_empty():
    s = repr(t[t.amount > 1e9])
    assert isinstance(s, str)
    assert 'amount' in s

def test_to_html():
    s = to_html(t)
    assert s
    assert 'Alice' in s
    assert '<table' in s

    assert to_html(1) == '1'

    assert to_html(t.count()) == '2'

def test_to_html_on_arrays():
    s = to_html(Data(np.ones((2, 2))))
    assert '1' in s
    assert 'br>' in s

def test_repr_html():
    assert '<table' in t._repr_html_()
    assert '<table' in t.name._repr_html_()

def test_into():
    assert into(list, t) == into(list, data)

def test_serialization():
    import pickle
    t2 = pickle.loads(pickle.dumps(t))

    assert t.schema == t2.schema
    assert t._name == t2._name

def test_table_resource():
    with tmpfile('csv') as filename:
        ds = dshape('var * {a: int, b: int}')
        csv = CSV(filename)
        append(csv, [[1, 2], [10, 20]], dshape=ds)

        t = Data(filename)
        assert isinstance(t.data, CSV)
        assert into(list, compute(t)) == into(list, csv)

def test_concretehead_failure():
    t = symbol('t', 'var * {x:int, y:int}')
    d = t[t['x'] > 100]
    with pytest.raises(ValueError):
        concrete_head(d)

def test_into_np_ndarray_column():
    t = Data(L, fields=['id', 'name', 'balance'])
    expr = t[t.balance < 0].name
    colarray = into(np.ndarray, expr)
    assert len(list(compute(expr))) == len(colarray)

def test_into_nd_array_selection():
    t = Data(L, fields=['id', 'name', 'balance'])
    expr = t[t['balance'] < 0]
    selarray = into(np.ndarray, expr)
    assert len(list(compute(expr))) == len(selarray)

def test_into_nd_array_column_failure():
    tble = Data(L, fields=['id', 'name', 'balance'])
    expr = tble[tble['balance'] < 0]
    colarray = into(np.ndarray, expr)
    assert len(list(compute(expr))) == len(colarray)

def test_Data_attribute_repr():
    t = Data(CSV(example('accounts-datetimes.csv')))
    result = t.when.day
    expected = pd.DataFrame({'when_day': [1,2,3,4,5]})
    assert repr(result) == repr(expected)

def test_can_trivially_create_csv_Data():
    Data(example('iris.csv'))

    # in context
    with Data(example('iris.csv')) as d:
        assert d is not None

def test_can_trivially_create_csv_Data_with_unicode():
    if sys.version[0] == '2':
        assert isinstance(Data(example(u'iris.csv')).data, CSV)

def test_can_trivially_create_sqlite_table():
    pytest.importorskip('sqlalchemy')
    Data('sqlite:///'+example('iris.db')+'::iris')

    # in context
    with Data('sqlite:///'+example('iris.db')+'::iris') as d:
        assert d is not None

@xfail(reason="h5py/pytables mismatch")
def test_can_trivially_create_pytables():
    pytest.importorskip('tables')
    with Data(example('accounts.h5')+'::/accounts') as d:
        assert d is not None

def test_data_passes_kwargs_to_resource():
    assert Data(example('iris.csv'), encoding='ascii').data.encoding == 'ascii'

def test_data_on_iterator_refies_data():
    data = [1, 2, 3]
    d = Data(iter(data))

    assert into(list, d) == data
    assert into(list, d) == data

    # in context
    with Data(iter(data)) as d:
        assert d is not None

def test_Data_on_json_is_concrete():
    d = Data(example('accounts-streaming.json'))

    assert compute(d.amount.sum()) == 100 - 200 + 300 + 400 - 500
    assert compute(d.amount.sum()) == 100 - 200 + 300 + 400 - 500

def test_repr_on_nd_array_doesnt_err():
    d = Data(np.ones((2, 2, 2)))
    repr(d + 1)

def test_generator_reprs_concretely():
    x = [1, 2, 3, 4, 5, 6]
    d = Data(x)
    expr = d[d > 2] + 1
    assert '4' in repr(expr)

def test_incompatible_types():
    d = Data(pd.DataFrame(L, columns=['id', 'name', 'amount']))

    with pytest.raises(ValueError):
        d.id == 'foo'

    result = compute(d.id == 3)
    expected = pd.Series([False, False, True, False, False], name='id')
    tm.assert_series_equal(result, expected)

def test___array__():
    x = np.ones(4)
    d = Data(x)
    assert (np.array(d + 1) == x + 1).all()

    d = Data(x[:2])
    x[2:] = d + 1
    assert x.tolist() == [1, 1, 2, 2]

def test_python_scalar_protocols():
    d = Data(1)
    assert int(d + 1) == 2
    assert float(d + 1.0) == 2.0
    assert bool(d > 0) is True
    assert complex(d + 1.0j) == 1 + 1.0j

def test_iter():
    x = np.ones(4)
    d = Data(x)
    assert list(d + 1) == [2, 2, 2, 2]

@xfail(reason="DataFrame constructor doesn't yet support __array__")
def test_DataFrame():
    x = np.array([(1, 2), (1., 2.)], dtype=[('a', 'i4'), ('b', 'f4')])
    d = Data(x)
    assert isinstance(pd.DataFrame(d), pd.DataFrame)

def test_head_compute():
    data = tm.makeMixedDataFrame()
    t = symbol('t', discover(data))
    db = into('sqlite:///:memory:::t', data, dshape=t.dshape)
    n = 2
    d = Data(db)

    # skip the header and the ... at the end of the repr
    expr = d.head(n)
    s = repr(expr)
    assert '...' not in s
    result = s.split('\n')[1:]
    assert len(result) == n

def test_scalar_sql_compute():
    t = into('sqlite:///:memory:::t', data,
            dshape=dshape('var * {name: string, amount: int}'))
    d = Data(t)
    assert repr(d.amount.sum()) == '300'

def test_no_name_for_simple_data():
    d = Data([1, 2, 3])
    assert repr(d) == '    \n0  1\n1  2\n2  3'
    assert not d._name

    d = Data(1)
    assert not d._name
    assert repr(d) == '1'

def test_coerce_date_and_datetime():
    x = datetime.datetime.now().date()
    d = Data(x)
    assert repr(d) == repr(x)

    x = datetime.datetime.now()
    d = Data(x)
    assert repr(d) == repr(x)

def test_highly_nested_repr():
    data = [[0, [[1, 2], [3]], 'abc']]
    d = Data(data)
    assert 'abc' in repr(d.head())

def test_asarray_fails_on_different_column_names():
    vs = {'first': [2., 5., 3.],
          'second': [4., 1., 4.],
          'third': [6., 4., 3.]}
    df = pd.DataFrame(vs)
    with pytest.raises(ValueError):
        Data(df, fields=list('abc'))

def test_data_does_not_accept_columns_kwarg():
    with pytest.raises(ValueError):
        Data([(1, 2), (3, 4)], columns=list('ab'))

def test_functions_as_bound_methods():
    """
    Test that all functions on an InteractiveSymbol are instance methods
    of that object.
    """
    # Filter out __class__ and friends that are special, these can be
    # callables without being instance methods.
    callable_attrs = filter(
        callable,
        (getattr(t, a, None) for a in dir(t) if not a.startswith('__')),
    )
    for attr in callable_attrs:
        assert isinstance(attr, MethodType)
        # Make sure this is bound to the correct object.
        assert attr.__self__ is t
<|endoftext|>


<commit_before>import module
import fields
import fields.io
import fields.sensor
import fields.persistant

class AirSensor(module.Base):
    update_rate = 10
    class Air(
            fields.sensor.Air,
            fields.io.Readable,
            fields.persistant.Volatile,
            fields.Base):
        pass

    def always(self):
        print 'ceci est un petit test'
<commit_msg>fix bug with lowercase caracter<commit_after>import module
import fields
import fields.io
import fields.sensor
import fields.persistant

class AirSensor(module.Base):
    update_rate = 10
    class air(
            fields.sensor.Air,
            fields.io.Readable,
            fields.persistant.Volatile,
            fields.Base):
        pass

    def always(self):
        print 'ceci est un petit test'
<|endoftext|>


<commit_before>from django.db.models.signals import post_init

from exportdb.exporter import ExportModelResource

from bluebottle.projects.models import project_post_init, Project
from bluebottle.utils.signals import temp_disconnect_signal
from bluebottle.tasks.models import task_post_init, Task

class DateRangeResource(ExportModelResource):
    range_field = 'created'
    select_related = None

    def get_queryset(self):
        qs = super(DateRangeResource, self).get_queryset()
        if self.select_related:
            qs = qs.select_related(*self.select_related)
        frm, to = self.kwargs.get('from_date'), self.kwargs.get('to_date')
        return qs.filter(**{'%s__range' % self.range_field: (frm, to)})

class UserResource(DateRangeResource):
    range_field = 'date_joined'
    select_related = ('location',)

class ProjectResource(DateRangeResource):
    select_related = ('status', 'owner', 'location',)

    def export(self, **kwargs):
        with temp_disconnect_signal(
                signal=post_init,
                receiver=project_post_init,
                sender=Project,
                dispatch_uid='bluebottle.projects.Project.post_init'):
            data = super(ProjectResource, self).export(**kwargs)
        return data

class TaskResource(DateRangeResource):
    select_related = ('project', 'author', 'location')

    def export(self, **kwargs):
            with temp_disconnect_signal(
                    signal=post_init,
                    receiver=task_post_init,
                    sender=Task,
                    dispatch_uid='bluebottle.tasks.Task.post_init'):
                data = super(TaskResource, self).export(**kwargs)
            return data

class TaskMemberResource(DateRangeResource):
    select_related = ('member', 'task', 'task__project', 'member__location')

class DonationResource(DateRangeResource):
    select_related = ('order', 'order__user', 'order__user__location', 'project', 'project', 'fundraiser')

    def export(self, **kwargs):
        with temp_disconnect_signal(
                signal=post_init,
                receiver=project_post_init,
                sender=Project,
                dispatch_uid='bluebottle.projects.Project.post_init'):
            data = super(DonationResource, self).export(**kwargs)
        return data
<commit_msg>Maximized database optimization<commit_after>from django.db.models.signals import post_init

from exportdb.exporter import ExportModelResource

from bluebottle.projects.models import project_post_init, Project
from bluebottle.utils.signals import temp_disconnect_signal
from bluebottle.tasks.models import task_post_init, Task

class DateRangeResource(ExportModelResource):
    range_field = 'created'
    select_related = None

    def get_queryset(self):
        qs = super(DateRangeResource, self).get_queryset()
        if self.select_related:
            qs = qs.select_related(*self.select_related)
        frm, to = self.kwargs.get('from_date'), self.kwargs.get('to_date')
        return qs.filter(**{'%s__range' % self.range_field: (frm, to)})

class UserResource(DateRangeResource):
    range_field = 'date_joined'
    select_related = ('location',)

class ProjectResource(DateRangeResource):
    select_related = ('status', 'owner', 'location',)

    def export(self, **kwargs):
        with temp_disconnect_signal(
                signal=post_init,
                receiver=project_post_init,
                sender=Project,
                dispatch_uid='bluebottle.projects.Project.post_init'):
            data = super(ProjectResource, self).export(**kwargs)
        return data

class TaskResource(DateRangeResource):
    select_related = ('project', 'author', 'location')

    def export(self, **kwargs):

        task_signal = dict(
            signal=post_init,
            receiver=task_post_init,
            sender=Task,
            dispatch_uid='bluebottle.tasks.Task.post_init'
        )
        project_signal = dict(
            signal=post_init,
            receiver=project_post_init,
            sender=Project,
            dispatch_uid='bluebottle.projects.Project.post_init'
        )
        with temp_disconnect_signal(**task_signal), temp_disconnect_signal(**project_signal):
            data = super(TaskResource, self).export(**kwargs)
        return data

class TaskMemberResource(DateRangeResource):
    select_related = ('member', 'task', 'task__project', 'member__location')

    def export(self, **kwargs):
        task_signal = dict(
            signal=post_init,
            receiver=task_post_init,
            sender=Task,
            dispatch_uid='bluebottle.tasks.Task.post_init'
        )
        project_signal = dict(
            signal=post_init,
            receiver=project_post_init,
            sender=Project,
            dispatch_uid='bluebottle.projects.Project.post_init'
        )
        with temp_disconnect_signal(**task_signal), temp_disconnect_signal(**project_signal):
            data = super(TaskMemberResource, self).export(**kwargs)
        return data

class DonationResource(DateRangeResource):
    select_related = ('order', 'order__user', 'order__user__location', 'project', 'project', 'fundraiser')

    def export(self, **kwargs):
        with temp_disconnect_signal(
                signal=post_init,
                receiver=project_post_init,
                sender=Project,
                dispatch_uid='bluebottle.projects.Project.post_init'):
            data = super(DonationResource, self).export(**kwargs)
        return data
<|endoftext|>


<commit_before>from datetime import timedelta
from rest_framework import serializers
from rest_framework.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _

from bluebottle.bluebottle_drf2.serializers import (
    PrimaryKeyGenericRelatedField, FileSerializer, PrivateFileSerializer
)
from bluebottle.geo.serializers import PlaceSerializer
from bluebottle.geo.models import Place
from bluebottle.members.serializers import UserPreviewSerializer, UserProfileSerializer
from bluebottle.tasks.models import Task, TaskMember, TaskFile, Skill
from bluebottle.tasks.permissions import TaskMemberPermission, TaskManagerPermission
from bluebottle.tasks.taskmail import TaskMemberMailAdapter
from bluebottle.projects.serializers import ProjectPreviewSerializer
from bluebottle.utils.permissions import OneOf
from bluebottle.utils.serializers import PermissionField, ResourcePermissionField
from bluebottle.wallposts.serializers import TextWallpostSerializer
from bluebottle.projects.models import Project
from bluebottle.members.models import Member

class UniqueTaskMemberValidator(object):
    def set_context(self, serializer):
        self.instance = serializer.instance
        self.request = serializer.context['request']

    def __call__(self, data):
        if self.instance is None:
            queryset = TaskMember.objects.filter(
                member=self.request.user,
                task=data['task']
            ).exclude(
                status__in=(
                    TaskMember.TaskMemberStatuses.rejected,
                    TaskMember.TaskMemberStatuses.withdrew
                )
            )

            if queryset.exists():
                raise ValidationError(
                    _('It is not possible to apply twice for the same task')
                )

class BaseTaskMemberSerializer(serializers.ModelSerializer):
    member = UserPreviewSerializer()
    status = serializers.ChoiceField(
        choices=TaskMember.TaskMemberStatuses.choices,
        required=False, default=TaskMember.TaskMemberStatuses.applied)
    motivation = serializers.CharField(required=False, allow_blank=True)
    resume = PrivateFileSerializer(
        url_name='task-member-resume', url_args=('pk', ), file_attr='resume',
        required=False, allow_null=True,
        permission=OneOf(TaskManagerPermission, TaskMemberPermission)
    )
    permissions = ResourcePermissionField('task-member-detail', view_args=('id',))

    class Meta:
        model = TaskMember
        fields = ('id', 'member', 'status', 'created', 'motivation', 'task',
                  'externals', 'time_spent', 'resume', 'permissions')
        validators = (UniqueTaskMemberValidator(), )

    def validate(self, data):
        if 'time_spent' not in data or not self.instance:
            return data

        if (
            self.instance.time_spent != data['time_spent'] and
            self.context['request'].user == self.instance.member and
            self.instance.project.task_manager != self.instance.member
        ):
            raise serializers.ValidationError('User can not update their own time spent')

        return data

    def to_representation(self, obj):
        ret = super(BaseTaskMemberSerializer, self).to_representation(obj)
        if self.context['request'].method == 'GET' \
                and self.context['request'].user not in [obj.member, obj.task.author, obj.task.project.owner]:
            ret['motivation'] = ''
        return ret

class TaskMemberStatusSerializer(serializers.ModelSerializer):
    member = UserPreviewSerializer(read_only=True)
    permissions = ResourcePermissionField('task-member-status', view_args=('id',))
    message = serializers.CharField(write_only=True)

    class Meta:
        model = TaskMember
        fields = ('id', 'member', 'status', 'permissions', 'message')

    def update(self, instance, validated_data):
        message = validated_data.pop('message')
        instance.skip_mail = True

        result = super(TaskMemberStatusSerializer, self).update(instance, validated_data)

        if instance._original_status != instance.status:
            TaskMemberMailAdapter(self.instance, message=message).send_mail()

        return result

class TaskFileSerializer(serializers.ModelSerializer):
    author = UserPreviewSerializer()
    file = FileSerializer()

    class Meta:
        model = TaskFile
        fields = '__all__'

class TaskPermissionsSerializer(serializers.Serializer):
    def get_attribute(self, obj):
        return obj

    task_members = PermissionField('task-member-list')

    class Meta:
        fields = ('task_members', )

class BaseTaskSerializer(serializers.ModelSerializer):
    members = BaseTaskMemberSerializer(many=True, read_only=True)
    files = TaskFileSerializer(many=True, read_only=True)
    project = serializers.SlugRelatedField(slug_field='slug',
                                           queryset=Project.objects)
    skill = serializers.PrimaryKeyRelatedField(queryset=Skill.objects)
    place = PlaceSerializer(required=False)
    author = UserProfileSerializer(read_only=True)
    permissions = ResourcePermissionField('task_detail', view_args=('id',))
    related_permissions = TaskPermissionsSerializer(read_only=True)
    status = serializers.ChoiceField(choices=Task.TaskStatuses.choices,
                                     default=Task.TaskStatuses.open)
    time_needed = serializers.DecimalField(min_value=0.0,
                                           max_digits=5,
                                           decimal_places=2)

    def validate(self, data):
        if self.instance and data.get('deadline') and self.instance.deadline.date() == data['deadline'].date():
            # The date has not changed: Do not validate
            return data

        project_deadline = data['project'].deadline
        project_is_funding = data['project'].project_type in ['funding', 'both']
        if data.get('deadline') > project_deadline and project_is_funding:
            raise serializers.ValidationError({
                'deadline': [
                    _("The deadline can not be more than the project deadline")
                ]
            })

        project_started = data['project'].campaign_started or data['project'].created
        if data.get('deadline') > project_started + timedelta(days=366):
            raise serializers.ValidationError({
                'deadline': [
                    _("The deadline can not be more than a year after the project started")
                ]
            })

        if not data['deadline_to_apply'] or data['deadline_to_apply'] > data['deadline']:
            raise serializers.ValidationError(
                {'deadline': [_("The deadline to apply must be before the deadline.")]}
            )

        return data

    class Meta:
        model = Task
        fields = (
            'accepting',
            'author',
            'deadline',
            'deadline_to_apply',
            'description',
            'files',
            'id',
            'location',
            'place',
            'members',
            'needs_motivation',
            'people_needed',
            'permissions',
            'project',
            'related_permissions',
            'skill',
            'status',
            'time_needed',
            'title',
            'type',
        )

    def _check_project_deadline(self, instance, validated_data):
        project = validated_data['project']
        if instance.deadline > project.deadline:
            project.deadline = instance.deadline
            project.save()

    def create(self, validated_data):
        place = None
        if 'place' in validated_data:
            place = validated_data.pop('place')
        instance = super(BaseTaskSerializer, self).create(validated_data)

        if place:
            Place.objects.create(content_object=instance, **place)

        self._check_project_deadline(instance, validated_data)

        return instance

    def update(self, instance, validated_data):
        if 'place' in validated_data:
            if instance.place:
                place = instance.place
                for key, value in validated_data.pop('place').items():
                    setattr(place, key, value)
                place.save()
            else:
                Place.objects.create(content_object=instance, **validated_data.pop('place'))
        else:
            instance.place = None

        result = super(BaseTaskSerializer, self).update(instance, validated_data)

        self._check_project_deadline(instance, validated_data)
        return result

class MyTaskPreviewSerializer(serializers.ModelSerializer):
    project = ProjectPreviewSerializer()
    skill = serializers.PrimaryKeyRelatedField(queryset=Skill.objects)

    class Meta:
        model = Task
        fields = ('id', 'title', 'skill', 'project', 'time_needed', 'type')

class MyTaskMemberSerializer(BaseTaskMemberSerializer):
    task = MyTaskPreviewSerializer()
    member = serializers.PrimaryKeyRelatedField(queryset=Member.objects)

class MyTasksSerializer(BaseTaskSerializer):
    skill = serializers.PrimaryKeyRelatedField(queryset=Skill.objects)

    class Meta:
        model = Task
        fields = (
            'accepting',
            'deadline',
            'deadline_to_apply',
            'description',
            'id',
            'location',
            'needs_motivation',
            'people_needed',
            'permissions',
            'project',
            'related_permissions',
            'skill',
            'status',
            'time_needed',
            'title',
            'type'
        )

# Task Wallpost serializers

class TaskWallpostSerializer(TextWallpostSerializer):
    """ TextWallpostSerializer with task specific customizations. """

    url = serializers.HyperlinkedIdentityField(
        view_name='task-twallpost-detail', lookup_field='pk')
    task = PrimaryKeyGenericRelatedField(Task)

    class Meta(TextWallpostSerializer.Meta):
        # Add the project slug field.
        fields = TextWallpostSerializer.Meta.fields + ('task',)

class SkillSerializer(serializers.ModelSerializer):
    name = serializers.CharField()

    class Meta:
        model = Skill
        fields = ('id', 'name', 'expertise')

class TaskPreviewSerializer(serializers.ModelSerializer):
    author = UserPreviewSerializer()
    project = ProjectPreviewSerializer()
    skill = serializers.PrimaryKeyRelatedField(queryset=Skill)
    members = BaseTaskMemberSerializer(many=True, read_only=True, source="members_applied")

    class Meta:
        model = Task
        fields = '__all__'
<commit_msg>Fix requests without place<commit_after>from datetime import timedelta
from rest_framework import serializers
from rest_framework.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _

from bluebottle.bluebottle_drf2.serializers import (
    PrimaryKeyGenericRelatedField, FileSerializer, PrivateFileSerializer
)
from bluebottle.geo.serializers import PlaceSerializer
from bluebottle.geo.models import Place
from bluebottle.members.serializers import UserPreviewSerializer, UserProfileSerializer
from bluebottle.tasks.models import Task, TaskMember, TaskFile, Skill
from bluebottle.tasks.permissions import TaskMemberPermission, TaskManagerPermission
from bluebottle.tasks.taskmail import TaskMemberMailAdapter
from bluebottle.projects.serializers import ProjectPreviewSerializer
from bluebottle.utils.permissions import OneOf
from bluebottle.utils.serializers import PermissionField, ResourcePermissionField
from bluebottle.wallposts.serializers import TextWallpostSerializer
from bluebottle.projects.models import Project
from bluebottle.members.models import Member

class UniqueTaskMemberValidator(object):
    def set_context(self, serializer):
        self.instance = serializer.instance
        self.request = serializer.context['request']

    def __call__(self, data):
        if self.instance is None:
            queryset = TaskMember.objects.filter(
                member=self.request.user,
                task=data['task']
            ).exclude(
                status__in=(
                    TaskMember.TaskMemberStatuses.rejected,
                    TaskMember.TaskMemberStatuses.withdrew
                )
            )

            if queryset.exists():
                raise ValidationError(
                    _('It is not possible to apply twice for the same task')
                )

class BaseTaskMemberSerializer(serializers.ModelSerializer):
    member = UserPreviewSerializer()
    status = serializers.ChoiceField(
        choices=TaskMember.TaskMemberStatuses.choices,
        required=False, default=TaskMember.TaskMemberStatuses.applied)
    motivation = serializers.CharField(required=False, allow_blank=True)
    resume = PrivateFileSerializer(
        url_name='task-member-resume', url_args=('pk', ), file_attr='resume',
        required=False, allow_null=True,
        permission=OneOf(TaskManagerPermission, TaskMemberPermission)
    )
    permissions = ResourcePermissionField('task-member-detail', view_args=('id',))

    class Meta:
        model = TaskMember
        fields = ('id', 'member', 'status', 'created', 'motivation', 'task',
                  'externals', 'time_spent', 'resume', 'permissions')
        validators = (UniqueTaskMemberValidator(), )

    def validate(self, data):
        if 'time_spent' not in data or not self.instance:
            return data

        if (
            self.instance.time_spent != data['time_spent'] and
            self.context['request'].user == self.instance.member and
            self.instance.project.task_manager != self.instance.member
        ):
            raise serializers.ValidationError('User can not update their own time spent')

        return data

    def to_representation(self, obj):
        ret = super(BaseTaskMemberSerializer, self).to_representation(obj)
        if self.context['request'].method == 'GET' \
                and self.context['request'].user not in [obj.member, obj.task.author, obj.task.project.owner]:
            ret['motivation'] = ''
        return ret

class TaskMemberStatusSerializer(serializers.ModelSerializer):
    member = UserPreviewSerializer(read_only=True)
    permissions = ResourcePermissionField('task-member-status', view_args=('id',))
    message = serializers.CharField(write_only=True)

    class Meta:
        model = TaskMember
        fields = ('id', 'member', 'status', 'permissions', 'message')

    def update(self, instance, validated_data):
        message = validated_data.pop('message')
        instance.skip_mail = True

        result = super(TaskMemberStatusSerializer, self).update(instance, validated_data)

        if instance._original_status != instance.status:
            TaskMemberMailAdapter(self.instance, message=message).send_mail()

        return result

class TaskFileSerializer(serializers.ModelSerializer):
    author = UserPreviewSerializer()
    file = FileSerializer()

    class Meta:
        model = TaskFile
        fields = '__all__'

class TaskPermissionsSerializer(serializers.Serializer):
    def get_attribute(self, obj):
        return obj

    task_members = PermissionField('task-member-list')

    class Meta:
        fields = ('task_members', )

class BaseTaskSerializer(serializers.ModelSerializer):
    members = BaseTaskMemberSerializer(many=True, read_only=True)
    files = TaskFileSerializer(many=True, read_only=True)
    project = serializers.SlugRelatedField(slug_field='slug',
                                           queryset=Project.objects)
    skill = serializers.PrimaryKeyRelatedField(queryset=Skill.objects)
    place = PlaceSerializer(required=False)
    author = UserProfileSerializer(read_only=True)
    permissions = ResourcePermissionField('task_detail', view_args=('id',))
    related_permissions = TaskPermissionsSerializer(read_only=True)
    status = serializers.ChoiceField(choices=Task.TaskStatuses.choices,
                                     default=Task.TaskStatuses.open)
    time_needed = serializers.DecimalField(min_value=0.0,
                                           max_digits=5,
                                           decimal_places=2)

    def validate(self, data):
        if self.instance and data.get('deadline') and self.instance.deadline.date() == data['deadline'].date():
            # The date has not changed: Do not validate
            return data

        project_deadline = data['project'].deadline
        project_is_funding = data['project'].project_type in ['funding', 'both']
        if data.get('deadline') > project_deadline and project_is_funding:
            raise serializers.ValidationError({
                'deadline': [
                    _("The deadline can not be more than the project deadline")
                ]
            })

        project_started = data['project'].campaign_started or data['project'].created
        if data.get('deadline') > project_started + timedelta(days=366):
            raise serializers.ValidationError({
                'deadline': [
                    _("The deadline can not be more than a year after the project started")
                ]
            })

        if not data['deadline_to_apply'] or data['deadline_to_apply'] > data['deadline']:
            raise serializers.ValidationError(
                {'deadline': [_("The deadline to apply must be before the deadline.")]}
            )

        return data

    class Meta:
        model = Task
        fields = (
            'accepting',
            'author',
            'deadline',
            'deadline_to_apply',
            'description',
            'files',
            'id',
            'location',
            'place',
            'members',
            'needs_motivation',
            'people_needed',
            'permissions',
            'project',
            'related_permissions',
            'skill',
            'status',
            'time_needed',
            'title',
            'type',
        )

    def _check_project_deadline(self, instance, validated_data):
        project = validated_data['project']
        if instance.deadline > project.deadline:
            project.deadline = instance.deadline
            project.save()

    def create(self, validated_data):
        place = None
        if 'place' in validated_data:
            place = validated_data.pop('place')
        instance = super(BaseTaskSerializer, self).create(validated_data)

        if place:
            Place.objects.create(content_object=instance, **place)

        self._check_project_deadline(instance, validated_data)

        return instance

    def update(self, instance, validated_data):
        if 'place' in validated_data:
            if instance.place:
                place = instance.place
                for key, value in validated_data.pop('place').items():
                    setattr(place, key, value)
                place.save()
            else:
                Place.objects.create(content_object=instance, **validated_data.pop('place'))
        else:
            if instance.place:
                instance.place.delete()

        result = super(BaseTaskSerializer, self).update(instance, validated_data)

        self._check_project_deadline(instance, validated_data)
        return result

class MyTaskPreviewSerializer(serializers.ModelSerializer):
    project = ProjectPreviewSerializer()
    skill = serializers.PrimaryKeyRelatedField(queryset=Skill.objects)

    class Meta:
        model = Task
        fields = ('id', 'title', 'skill', 'project', 'time_needed', 'type')

class MyTaskMemberSerializer(BaseTaskMemberSerializer):
    task = MyTaskPreviewSerializer()
    member = serializers.PrimaryKeyRelatedField(queryset=Member.objects)

class MyTasksSerializer(BaseTaskSerializer):
    skill = serializers.PrimaryKeyRelatedField(queryset=Skill.objects)

    class Meta:
        model = Task
        fields = (
            'accepting',
            'deadline',
            'deadline_to_apply',
            'description',
            'id',
            'location',
            'needs_motivation',
            'people_needed',
            'permissions',
            'project',
            'related_permissions',
            'skill',
            'status',
            'time_needed',
            'title',
            'type'
        )

# Task Wallpost serializers

class TaskWallpostSerializer(TextWallpostSerializer):
    """ TextWallpostSerializer with task specific customizations. """

    url = serializers.HyperlinkedIdentityField(
        view_name='task-twallpost-detail', lookup_field='pk')
    task = PrimaryKeyGenericRelatedField(Task)

    class Meta(TextWallpostSerializer.Meta):
        # Add the project slug field.
        fields = TextWallpostSerializer.Meta.fields + ('task',)

class SkillSerializer(serializers.ModelSerializer):
    name = serializers.CharField()

    class Meta:
        model = Skill
        fields = ('id', 'name', 'expertise')

class TaskPreviewSerializer(serializers.ModelSerializer):
    author = UserPreviewSerializer()
    project = ProjectPreviewSerializer()
    skill = serializers.PrimaryKeyRelatedField(queryset=Skill)
    members = BaseTaskMemberSerializer(many=True, read_only=True, source="members_applied")

    class Meta:
        model = Task
        fields = '__all__'
<|endoftext|>


<commit_before>"""
==========================================
Author:             Tyler Brockett
Username:           /u/tylerbrockett
Description:        Alert Bot (Formerly sales__bot)
Date Created:       11/13/2015
Date Last Edited:   12/2/2016
Version:            v2.0
==========================================
"""

from utils.subscription import Subscription

class MatchFinder:

    @staticmethod
    def is_title_match(subscription, submission):
        title_match = False
        # Empty title_list is automatically 'True' because it has no effect on result
        if len(subscription.data[Subscription.TITLE]) == 0:
            title_match = True
        for title_list in subscription.data[Subscription.TITLE]:
            title_list_match = True
            for item in title_list:
                if item.lower() not in submission.title.lower():
                    title_list_match = False
            title_match = title_match or title_list_match
        return title_match

    @staticmethod
    def is_body_match(subscription, submission):
        body_match = False
        # Empty body_list is automatically 'True' because it has no effect on result
        if len(subscription.data[Subscription.BODY]) == 0:
            body_match = True
        for body_list in subscription.data[Subscription.BODY]:
            body_list_match = True
            for item in body_list:
                body_content = submission.selftext.lower() if submission.is_self else submission.url.lower()
                if item.lower() not in body_content:
                    body_list_match = False
            body_match = body_match or body_list_match
        return body_match

    @staticmethod
    def is_redditor_match(subscription, submission):
        redditor_match = False
        if len(subscription.data[Subscription.REDDITORS]) == 0:
            redditor_match = True
        for redditor in subscription.data[Subscription.REDDITORS]:
            if redditor.lower() == str(submission.author).lower():
                redditor_match = True
        return redditor_match

    @staticmethod
    def is_ignore_title_match(subscription, submission):
        ignore_title_match = True
        for item in subscription.data[Subscription.IGNORE_TITLE]:
            if item.lower() in submission.title.lower():
                ignore_title_match = False
        return ignore_title_match

    @staticmethod
    def is_ignore_body_match(subscription, submission):
        ignore_body_match = True
        for item in subscription.data[Subscription.IGNORE_BODY]:
            body_content = submission.selftext.lower() if submission.is_self else submission.url.lower()
            if item.lower() in body_content:
                ignore_body_match = False
        return ignore_body_match

    @staticmethod
    def is_ignore_redditors_match(subscription, submission):
        ignore_redditors_match = True
        for redditor in subscription.data[Subscription.IGNORE_REDDITORS]:
            if redditor.lower() == str(submission.author).lower():
                ignore_redditors_match = False
        return ignore_redditors_match

    @staticmethod
    def is_nsfw_match(subscription, submission):
        if submission.over_18 and not subscription.data[Subscription.NSFW]:
            return False
        return True

    @staticmethod
    def is_match(subscription, submission):
        result = True
        mismatched_keys = []
        for key in subscription.data.keys():
            key_match = True
            if Subscription.TITLE == key:
                key_match = MatchFinder.is_title_match(subscription, submission)
            elif Subscription.BODY == key:
                key_match = MatchFinder.is_body_match(subscription, submission)
            elif Subscription.REDDITORS == key:
                key_match = MatchFinder.is_redditor_match(subscription, submission)
            elif Subscription.IGNORE_TITLE == key:
                key_match = MatchFinder.is_ignore_title_match(subscription, submission)
            elif Subscription.IGNORE_BODY == key:
                key_match = MatchFinder.is_ignore_body_match(subscription, submission)
            elif Subscription.IGNORE_REDDITORS == key:
                key_match = MatchFinder.is_ignore_redditors_match(subscription, submission)
            elif Subscription.NSFW == key:
                key_match = MatchFinder.is_nsfw_match(subscription, submission)
            if not key_match:
                mismatched_keys.append(key)
            result = result and key_match
        return result, sorted(set(mismatched_keys))

    @staticmethod
    def find_matches(subscriptions, reddit, database):
        subreddits = {}
        matches = []
        for subscription in subscriptions:
            subreds = subscription.data[Subscription.SUBREDDITS]
            for subreddit in subreds:
                if subreddit.lower() not in [k.lower() for k in subreddits.keys()]:
                    print(subreddit.lower())
                    submissions = reddit.get_submissions(subreddit.lower())
                    temp = []
                    for sub in submissions:
                        temp.append(sub)
                    subreddits[subreddit.lower()] = temp
                submissions = subreddits[subreddit.lower()]
                # submissions = reddit.get_submissions(subreddit)
                num = 0
                for submission in submissions:
                    num += 1
                    is_match, mismatched_keys = MatchFinder.is_match(subscription, submission)
                    if is_match:
                        already_exists = database.check_if_match_exists(subscription.username,
                                                                        subscription.to_string(),
                                                                        submission.permalink)
                        if not already_exists:
                            matches.append((subscription, submission))
        return matches
<commit_msg>Changed log color for logging subreddits<commit_after>"""
==========================================
Author:             Tyler Brockett
Username:           /u/tylerbrockett
Description:        Alert Bot (Formerly sales__bot)
Date Created:       11/13/2015
Date Last Edited:   12/2/2016
Version:            v2.0
==========================================
"""

from utils.subscription import Subscription
from utils.logger import Logger
from utils.color import Color

class MatchFinder:

    @staticmethod
    def is_title_match(subscription, submission):
        title_match = False
        # Empty title_list is automatically 'True' because it has no effect on result
        if len(subscription.data[Subscription.TITLE]) == 0:
            title_match = True
        for title_list in subscription.data[Subscription.TITLE]:
            title_list_match = True
            for item in title_list:
                if item.lower() not in submission.title.lower():
                    title_list_match = False
            title_match = title_match or title_list_match
        return title_match

    @staticmethod
    def is_body_match(subscription, submission):
        body_match = False
        # Empty body_list is automatically 'True' because it has no effect on result
        if len(subscription.data[Subscription.BODY]) == 0:
            body_match = True
        for body_list in subscription.data[Subscription.BODY]:
            body_list_match = True
            for item in body_list:
                body_content = submission.selftext.lower() if submission.is_self else submission.url.lower()
                if item.lower() not in body_content:
                    body_list_match = False
            body_match = body_match or body_list_match
        return body_match

    @staticmethod
    def is_redditor_match(subscription, submission):
        redditor_match = False
        if len(subscription.data[Subscription.REDDITORS]) == 0:
            redditor_match = True
        for redditor in subscription.data[Subscription.REDDITORS]:
            if redditor.lower() == str(submission.author).lower():
                redditor_match = True
        return redditor_match

    @staticmethod
    def is_ignore_title_match(subscription, submission):
        ignore_title_match = True
        for item in subscription.data[Subscription.IGNORE_TITLE]:
            if item.lower() in submission.title.lower():
                ignore_title_match = False
        return ignore_title_match

    @staticmethod
    def is_ignore_body_match(subscription, submission):
        ignore_body_match = True
        for item in subscription.data[Subscription.IGNORE_BODY]:
            body_content = submission.selftext.lower() if submission.is_self else submission.url.lower()
            if item.lower() in body_content:
                ignore_body_match = False
        return ignore_body_match

    @staticmethod
    def is_ignore_redditors_match(subscription, submission):
        ignore_redditors_match = True
        for redditor in subscription.data[Subscription.IGNORE_REDDITORS]:
            if redditor.lower() == str(submission.author).lower():
                ignore_redditors_match = False
        return ignore_redditors_match

    @staticmethod
    def is_nsfw_match(subscription, submission):
        if submission.over_18 and not subscription.data[Subscription.NSFW]:
            return False
        return True

    @staticmethod
    def is_match(subscription, submission):
        result = True
        mismatched_keys = []
        for key in subscription.data.keys():
            key_match = True
            if Subscription.TITLE == key:
                key_match = MatchFinder.is_title_match(subscription, submission)
            elif Subscription.BODY == key:
                key_match = MatchFinder.is_body_match(subscription, submission)
            elif Subscription.REDDITORS == key:
                key_match = MatchFinder.is_redditor_match(subscription, submission)
            elif Subscription.IGNORE_TITLE == key:
                key_match = MatchFinder.is_ignore_title_match(subscription, submission)
            elif Subscription.IGNORE_BODY == key:
                key_match = MatchFinder.is_ignore_body_match(subscription, submission)
            elif Subscription.IGNORE_REDDITORS == key:
                key_match = MatchFinder.is_ignore_redditors_match(subscription, submission)
            elif Subscription.NSFW == key:
                key_match = MatchFinder.is_nsfw_match(subscription, submission)
            if not key_match:
                mismatched_keys.append(key)
            result = result and key_match
        return result, sorted(set(mismatched_keys))

    @staticmethod
    def find_matches(subscriptions, reddit, database):
        subreddits = {}
        matches = []
        for subscription in subscriptions:
            subreds = subscription.data[Subscription.SUBREDDITS]
            for subreddit in subreds:
                if subreddit.lower() not in [k.lower() for k in subreddits.keys()]:
                    Logger.log(subreddit.lower(), Color.CYAN)
                    submissions = reddit.get_submissions(subreddit.lower())
                    temp = []
                    for sub in submissions:
                        temp.append(sub)
                    subreddits[subreddit.lower()] = temp
                submissions = subreddits[subreddit.lower()]
                # submissions = reddit.get_submissions(subreddit)
                num = 0
                for submission in submissions:
                    num += 1
                    is_match, mismatched_keys = MatchFinder.is_match(subscription, submission)
                    if is_match:
                        already_exists = database.check_if_match_exists(subscription.username,
                                                                        subscription.to_string(),
                                                                        submission.permalink)
                        if not already_exists:
                            matches.append((subscription, submission))
        return matches
<|endoftext|>


<commit_before>"""
Numerically evolves a Bianchi Class I universe with anisotripic stress energy given initial conditions
David Clark, Kai Smith
Case Western Reserve University
2014
"""
from math import *
import numpy as np
import sympy as sp
import scipy.integrate
import matplotlib.pyplot as pplot

a0 = 0.5
b0 = 1.0
c0 = 1.5

a_dot0 = 1.0
b_dot0 = 1.5
c_dot0 = 2.0

A0 = a_dot0/a0
B0 = b_dot0/b0
C0 = c_dot0/c0

I0 = A0*B0+B0*C0+A0*C0
omega0 = 1
H0 = A0+B0+C0
V0 = a0*b0*c0

p0 = I0*omega0/3*H0/(a_dot0+b_dot0+c_dot0)

t = np.linspace(0, 10, 50)

def dydt(y, t):
	a, a_dot, b, b_dot, c, c_dot = y
	a_dot_dot = (-p0*V0/(a*b*c)*(b0/b+c0/c-a0/a)-a_dot*c_dot/(a*c)-a_dot*b_dot/(a*c)+b_dot*c_dot/(b*c))*a/2.0
	b_dot_dot = (-p0*V0/(a*b*c)*(-b0/b+c0/c+a0/a)+a_dot*c_dot/(a*c)-a_dot*b_dot/(a*c)-b_dot*c_dot/(b*c))*b/2.0
	c_dot_dot = (-p0*V0/(a*b*c)*(b0/b-c0/c-a0/a)-a_dot*c_dot/(a*c)+a_dot*b_dot/(a*c)-b_dot*c_dot/(b*c))*c/2.0
	return [a_dot, a_dot_dot, b_dot, b_dot_dot, c_dot, c_dot_dot]

def plot_stuff():
    y0 = [a0, a_dot0, b0, b_dot0, c0, c_dot0]
    y = scipy.integrate.odeint(dydt, y0, t)

    a = [value[0] for value in y]
    b = [value[2] for value in y]
    c = [value[4] for value in y]

    A = [value[1]/value[0] for value in y]
    B = [value[3]/value[2] for value in y]
    C = [value[5]/value[4] for value in y]

    V = [value[0]*value[2]*value[4] for value in y]
    #V_dot = [value[1]*value[2]*value[4] + value[0]*value[3]*value[4] + value[0]*value[2]*value[5] for value in y]

    pplot.scatter(t, np.float64(A), c = 'r')
    pplot.scatter(t, np.float64(B), c = 'g')
    pplot.scatter(t, np.float64(C), c = 'b')
    pplot.show()

    pplot.scatter(t, np.float64(a), c = 'r')
    pplot.scatter(t, np.float64(b), c = 'g')
    pplot.scatter(t, np.float64(c), c = 'b')
    pplot.show()

plot_stuff()
<commit_msg>Resolved merge conflict<commit_after>"""
Numerically evolves a Bianchi Class I universe with anisotripic stress energy given initial conditions
David Clark, Kai Smith
Case Western Reserve University
2014
"""
from math import *
import numpy as np
import sympy as sp
import scipy.integrate
import matplotlib.pyplot as pplot

a0 = 100.0
b0 = 1.0
c0 = 1.0

a_dot0 = 1.0
b_dot0 = 10.0
c_dot0 = 20.0

A0 = a_dot0/a0
B0 = b_dot0/b0
C0 = c_dot0/c0

I0 = A0*B0+B0*C0+A0*C0
omega0 = 1
H0 = A0+B0+C0
V0 = a0*b0*c0

p0 = I0*omega0/3*H0/(a_dot0+b_dot0+c_dot0)

t = np.linspace(0, 10, 50)

def dydt(y, t):
	a, a_dot, b, b_dot, c, c_dot = y
	a_dot_dot = (-p0*V0/(a*b*c)*(b0/b+c0/c-a0/a)-a_dot*c_dot/(a*c)-a_dot*b_dot/(a*c)+b_dot*c_dot/(b*c))*a/2.0
	b_dot_dot = (-p0*V0/(a*b*c)*(-b0/b+c0/c+a0/a)+a_dot*c_dot/(a*c)-a_dot*b_dot/(a*c)-b_dot*c_dot/(b*c))*b/2.0
	c_dot_dot = (-p0*V0/(a*b*c)*(b0/b-c0/c-a0/a)-a_dot*c_dot/(a*c)+a_dot*b_dot/(a*c)-b_dot*c_dot/(b*c))*c/2.0
	return [a_dot, a_dot_dot, b_dot, b_dot_dot, c_dot, c_dot_dot]

def plot_stuff():
    y0 = [a0, a_dot0, b0, b_dot0, c0, c_dot0]
    y = scipy.integrate.odeint(dydt, y0, t)

    a = [value[0] for value in y]
    b = [value[2] for value in y]
    c = [value[4] for value in y]

    A = [value[1]/value[0] for value in y]
    B = [value[3]/value[2] for value in y]
    C = [value[5]/value[4] for value in y]

    V = [value[0]*value[2]*value[4] for value in y]
    #V_dot = [value[1]*value[2]*value[4] + value[0]*value[3]*value[4] + value[0]*value[2]*value[5] for value in y]

	"""
	num = [a0/value[0] - b0/value[2] for value in y]
	denom = [((a0*b0*c0)/(value[0]*value[2]*value[4]))**(1.0/3.0) - c0/value[4] for value in y]
	frac = [num[i]/denom[i] for i in range(len(num))]
	"""

	g_prime = [p0*(a0/value[0] - b0/value[2]) for value in y]
	f_prime = [rho0*((a0*b0*c0)/(value[0]*value[2]*value[4]))**(1.0/3.0) - p0*(c0/value[4]) for value in y]
	ratio = [g_prime[i]/f_prime[i] for i in range(0, len(g_prime))]
	limit = [(1 - ratio[i])/(1 + ratio[i]) for i in range(len(ratio))]
	print(limit)

	A_over_B = [(value[1]/value[0])/(value[3]/value[2]) for value in y]
	print(A_over_B)

	ratio_of_ratios = [limit[i]/A_over_B[i] for i in range(len(A_over_B))]
	print(ratio_of_ratios)

	"""
	step_size = t[1] - t[0]
	f = [0]
	for i in range(1, len(f_inside_integral)):
		f.append(f[i-1] + f_inside_integral[i]*step_size)
	g = [0]
	for i in range(1, len(g_inside_integral)):
		g.append(g[i-1] + g_inside_integral[i]*step_size)

	g_over_f = [g[i]/f[i] for i in range(1, len(f))]

	pplot.scatter(t[1:], np.float64(g_over_f), c = 'r')
	pplot.show()

	A_over_B = [(value[1]/value[0])/(value[3]/value[2]) for value in y]
	print(A_over_B)
	"""

	"""
	pplot.scatter(t, np.float64(A), c = 'r')
	pplot.scatter(t, np.float64(B), c = 'g')
	pplot.scatter(t, np.float64(C), c = 'b')
	pplot.show()

	pplot.scatter(t, np.float64(a), c = 'r')
	pplot.scatter(t, np.float64(b), c = 'g')
	pplot.scatter(t, np.float64(c), c = 'b')
	pplot.show()
	"""

	"""

	print(frac)
	pplot.scatter(t, np.float64(frac), c = 'r')
	pplot.show()
	"""

plot_stuff()
<|endoftext|>


<commit_before>"""
.. module:: VisVTK
    :platform: Unix, Windows
    :synopsis: VTK visualization component for NURBS-Python

.. moduleauthor:: Onur Rauf Bingol <orbingol@gmail.com>

"""

from . import np
from geomdl import vis
from . import vtk_helpers as vtkh

class VisConfig(vis.VisConfigAbstract):
    """ Configuration class for VTK visualization module.

    This class is only required when you would like to change the visual defaults of the plots and the figure.

    The ``VisVTK`` module has the following configuration variables:

    * ``ctrlpts`` (bool): Control points polygon/grid visibility. *Default: True*
    * ``evalpts`` (bool): Curve/surface points visibility. *Default: True*
    * ``figure_size`` (list): Size of the figure in (x, y). *Default: (800, 600)*
    * ``line_width`` (int): Thickness of the lines on the figure. *Default: 0.5*
    """
    def __init__(self, **kwargs):
        super(VisConfig, self).__init__(**kwargs)
        self._bg = (  # background colors
            (0.5, 0.5, 0.5), (0.2, 0.2, 0.2), (0.25, 0.5, 0.75), (1.0, 1.0, 0.0),
            (1.0, 0.5, 0.0), (0.5, 0.0, 1.0), (0.0, 0.0, 0.0), (1.0, 1.0, 1.0)
        )
        self._bg_id = 0  # used for keeping track of the background numbering
        self.display_ctrlpts = kwargs.get('ctrlpts', True)
        self.display_evalpts = kwargs.get('evalpts', True)
        self.figure_size = kwargs.get('figure_size', (800, 600))  # size of the render window
        self.line_width = kwargs.get('line_width', 1.0)

    def keypress_callback(self, obj, ev):
        """ VTK callback for keypress events """
        key = obj.GetKeySym()
        # Change background
        if key == 'b':
            if self._bg_id >= len(self._bg):
                self._bg_id = 0
            obj.GetRenderWindow().GetRenderers().GetFirstRenderer().SetBackground(*self._bg[self._bg_id])
            self._bg_id += 1
            obj.GetRenderWindow().Render()

class VisCurve2D(vis.VisAbstract):
    def __init__(self, config=VisConfig()):
        super(VisCurve2D, self).__init__(config=config)
        self._module_config['others'] = "midpt"

    def render(self, **kwargs):
        """ Plots the curve and the control points polygon. """
        # Calling parent function
        super(VisCurve2D, self).render(**kwargs)
        self._module_config['other'] = "midpt"

        # Initialize a list to store VTK actors
        vtk_actors = []

        # Default focal point for VTK camera
        focal_point = [0, 0, 0]

        # Start plotting
        for plot in self._plots:
            # Plot control points
            if plot['type'] == 'ctrlpts' and self.vconf.display_ctrlpts:
                # Points as spheres
                pts = np.array(plot['ptsarr'], dtype=np.float)
                # Handle 2-dimensional data
                if pts.shape[1] == 2:
                    pts = np.c_[pts, np.zeros(pts.shape[0], dtype=np.float)]
                vtkpts = vtkh.numpy_to_vtk(pts, deep=False, array_type=vtkh.FLOAT)
                temp_actor_pts = vtkh.create_actor_pts(pts=vtkpts, color=vtkh.create_color(plot['color']))
                vtk_actors.append(temp_actor_pts)
                # Lines
                temp_actor_lines = vtkh.create_actor_polygon(pts=vtkpts, color=vtkh.create_color(plot['color']),
                                                             size=self.vconf.line_width)
                vtk_actors.append(temp_actor_lines)

            # Plot evaluated points
            if plot['type'] == 'evalpts' and self.vconf.display_evalpts:
                pts = np.array(plot['ptsarr'], dtype=np.float)
                # Handle 2-dimensional data
                if pts.shape[1] == 2:
                    pts = np.c_[pts, np.zeros(pts.shape[0], dtype=np.float)]
                vtkpts = vtkh.numpy_to_vtk(pts, deep=False, array_type=vtkh.FLOAT)
                temp_actor = vtkh.create_actor_polygon(pts=vtkpts, color=vtkh.create_color(plot['color']),
                                                       size=self.vconf.line_width * 2)
                vtk_actors.append(temp_actor)

            # Update camera focal point
            if plot['type'] == 'midpt':
                focal_point = plot['ptsarr'][0]
                if len(focal_point) == 2:
                    focal_point.append(0.0)

        # Render actors
        vtkh.create_render_window(vtk_actors, dict(KeyPressEvent=(self.vconf.keypress_callback, 1.0)),
                                  figure_size=self.vconf.figure_size,
                                  camera_focal_point=focal_point)

# VisCurve3D is an alias for VisCurve2D
VisCurve3D = VisCurve2D

class VisSurface(vis.VisAbstract):
    """ VTK visualization module for surfaces. """
    def __init__(self, config=VisConfig()):
        super(VisSurface, self).__init__(config=config)
        self._module_config['ctrlpts'] = "quadmesh"
        self._module_config['evalpts'] = "points"
        self._module_config['others'] = "midpt"

    def render(self, **kwargs):
        """ Plots the surface and the control points grid. """
        # Calling parent function
        super(VisSurface, self).render(**kwargs)

        # Initialize a list to store VTK actors
        vtk_actors = []

        # Default focal point for VTK camera
        focal_point = [0, 0, 0]

        # Start plotting
        for plot in self._plots:
            # Plot control points
            if plot['type'] == 'ctrlpts' and self.vconf.display_ctrlpts:
                vertices = [v.data for v in plot['ptsarr'][0]]
                quads = [q.data for q in plot['ptsarr'][1]]
                # Points as spheres
                pts = np.array(vertices, dtype=np.float)
                vtkpts = vtkh.numpy_to_vtk(pts, deep=False, array_type=vtkh.FLOAT)
                temp_actor_pts = vtkh.create_actor_pts(pts=vtkpts, color=vtkh.create_color(plot['color']))
                vtk_actors.append(temp_actor_pts)
                # Quad mesh
                lines = np.array(quads, dtype=np.int)
                temp_actor_lines = vtkh.create_actor_mesh(pts=vtkpts, lines=lines,
                                                          color=vtkh.create_color(plot['color']),
                                                          size=self.vconf.line_width)
                vtk_actors.append(temp_actor_lines)

            # Plot evaluated points
            if plot['type'] == 'evalpts' and self.vconf.display_evalpts:
                pts = np.array(plot['ptsarr'], dtype=np.float)
                vtkpts = vtkh.numpy_to_vtk(pts, deep=False, array_type=vtkh.FLOAT)
                temp_actor = vtkh.create_actor_tri(pts=vtkpts, color=vtkh.create_color(plot['color']), d3d=False)
                vtk_actors.append(temp_actor)

            # Update camera focal point
            if plot['type'] == 'midpt':
                focal_point = plot['ptsarr'][0]

        # Render actors
        vtkh.create_render_window(vtk_actors, dict(KeyPressEvent=(self.vconf.keypress_callback, 1.0)),
                                  figure_size=self.vconf.figure_size,
                                  camera_focal_point=focal_point)

class VisVolume(vis.VisAbstract):
    """ VTK visualization module for volumes. """
    def __init__(self, config=VisConfig()):
        super(VisVolume, self).__init__(config=config)
        self._module_config['ctrlpts'] = "points"
        self._module_config['evalpts'] = "points"
        self._module_config['others'] = "midpt"

    def render(self, **kwargs):
        """ Plots the volume and the control points. """
        # Calling parent function
        super(VisVolume, self).render(**kwargs)

        # Initialize a list to store VTK actors
        vtk_actors = []

        # Start plotting
        for plot in self._plots:
            # Plot control points
            if plot['type'] == 'ctrlpts' and self.vconf.display_ctrlpts:
                # Points as spheres
                pts = np.array(plot['ptsarr'], dtype=np.float)
                vtkpts = vtkh.numpy_to_vtk(pts, deep=False, array_type=vtkh.FLOAT)
                temp_actor_pts = vtkh.create_actor_pts(pts=vtkpts, color=plot['color'])
                vtk_actors.append(temp_actor_pts)

            # Plot evaluated points
            if plot['type'] == 'evalpts' and self.vconf.display_evalpts:
                pts = np.array(plot['ptsarr'], dtype=np.float)
                vtkpts = vtkh.numpy_to_vtk(pts, deep=False, array_type=vtkh.FLOAT)
                temp_actor = vtkh.create_actor_tri(pts=vtkpts, color=plot['color'], d3d=True)
                vtk_actors.append(temp_actor)

        # Render actors
        vtkh.create_render_window(vtk_actors, dict(KeyPressEvent=(self.vconf.keypress_callback, 1.0)),
                                  figure_size=self.vconf.figure_size)
<commit_msg>Forgot to update color updater for VisVTK.VisVolume class<commit_after>"""
.. module:: VisVTK
    :platform: Unix, Windows
    :synopsis: VTK visualization component for NURBS-Python

.. moduleauthor:: Onur Rauf Bingol <orbingol@gmail.com>

"""

from . import np
from geomdl import vis
from . import vtk_helpers as vtkh

class VisConfig(vis.VisConfigAbstract):
    """ Configuration class for VTK visualization module.

    This class is only required when you would like to change the visual defaults of the plots and the figure.

    The ``VisVTK`` module has the following configuration variables:

    * ``ctrlpts`` (bool): Control points polygon/grid visibility. *Default: True*
    * ``evalpts`` (bool): Curve/surface points visibility. *Default: True*
    * ``figure_size`` (list): Size of the figure in (x, y). *Default: (800, 600)*
    * ``line_width`` (int): Thickness of the lines on the figure. *Default: 0.5*
    """
    def __init__(self, **kwargs):
        super(VisConfig, self).__init__(**kwargs)
        self._bg = (  # background colors
            (0.5, 0.5, 0.5), (0.2, 0.2, 0.2), (0.25, 0.5, 0.75), (1.0, 1.0, 0.0),
            (1.0, 0.5, 0.0), (0.5, 0.0, 1.0), (0.0, 0.0, 0.0), (1.0, 1.0, 1.0)
        )
        self._bg_id = 0  # used for keeping track of the background numbering
        self.display_ctrlpts = kwargs.get('ctrlpts', True)
        self.display_evalpts = kwargs.get('evalpts', True)
        self.figure_size = kwargs.get('figure_size', (800, 600))  # size of the render window
        self.line_width = kwargs.get('line_width', 1.0)

    def keypress_callback(self, obj, ev):
        """ VTK callback for keypress events """
        key = obj.GetKeySym()
        # Change background
        if key == 'b':
            if self._bg_id >= len(self._bg):
                self._bg_id = 0
            obj.GetRenderWindow().GetRenderers().GetFirstRenderer().SetBackground(*self._bg[self._bg_id])
            self._bg_id += 1
            obj.GetRenderWindow().Render()

class VisCurve2D(vis.VisAbstract):
    def __init__(self, config=VisConfig()):
        super(VisCurve2D, self).__init__(config=config)
        self._module_config['others'] = "midpt"

    def render(self, **kwargs):
        """ Plots the curve and the control points polygon. """
        # Calling parent function
        super(VisCurve2D, self).render(**kwargs)
        self._module_config['other'] = "midpt"

        # Initialize a list to store VTK actors
        vtk_actors = []

        # Default focal point for VTK camera
        focal_point = [0, 0, 0]

        # Start plotting
        for plot in self._plots:
            # Plot control points
            if plot['type'] == 'ctrlpts' and self.vconf.display_ctrlpts:
                # Points as spheres
                pts = np.array(plot['ptsarr'], dtype=np.float)
                # Handle 2-dimensional data
                if pts.shape[1] == 2:
                    pts = np.c_[pts, np.zeros(pts.shape[0], dtype=np.float)]
                vtkpts = vtkh.numpy_to_vtk(pts, deep=False, array_type=vtkh.FLOAT)
                temp_actor_pts = vtkh.create_actor_pts(pts=vtkpts, color=vtkh.create_color(plot['color']))
                vtk_actors.append(temp_actor_pts)
                # Lines
                temp_actor_lines = vtkh.create_actor_polygon(pts=vtkpts, color=vtkh.create_color(plot['color']),
                                                             size=self.vconf.line_width)
                vtk_actors.append(temp_actor_lines)

            # Plot evaluated points
            if plot['type'] == 'evalpts' and self.vconf.display_evalpts:
                pts = np.array(plot['ptsarr'], dtype=np.float)
                # Handle 2-dimensional data
                if pts.shape[1] == 2:
                    pts = np.c_[pts, np.zeros(pts.shape[0], dtype=np.float)]
                vtkpts = vtkh.numpy_to_vtk(pts, deep=False, array_type=vtkh.FLOAT)
                temp_actor = vtkh.create_actor_polygon(pts=vtkpts, color=vtkh.create_color(plot['color']),
                                                       size=self.vconf.line_width * 2)
                vtk_actors.append(temp_actor)

            # Update camera focal point
            if plot['type'] == 'midpt':
                focal_point = plot['ptsarr'][0]
                if len(focal_point) == 2:
                    focal_point.append(0.0)

        # Render actors
        vtkh.create_render_window(vtk_actors, dict(KeyPressEvent=(self.vconf.keypress_callback, 1.0)),
                                  figure_size=self.vconf.figure_size,
                                  camera_focal_point=focal_point)

# VisCurve3D is an alias for VisCurve2D
VisCurve3D = VisCurve2D

class VisSurface(vis.VisAbstract):
    """ VTK visualization module for surfaces. """
    def __init__(self, config=VisConfig()):
        super(VisSurface, self).__init__(config=config)
        self._module_config['ctrlpts'] = "quadmesh"
        self._module_config['evalpts'] = "points"
        self._module_config['others'] = "midpt"

    def render(self, **kwargs):
        """ Plots the surface and the control points grid. """
        # Calling parent function
        super(VisSurface, self).render(**kwargs)

        # Initialize a list to store VTK actors
        vtk_actors = []

        # Default focal point for VTK camera
        focal_point = [0, 0, 0]

        # Start plotting
        for plot in self._plots:
            # Plot control points
            if plot['type'] == 'ctrlpts' and self.vconf.display_ctrlpts:
                vertices = [v.data for v in plot['ptsarr'][0]]
                quads = [q.data for q in plot['ptsarr'][1]]
                # Points as spheres
                pts = np.array(vertices, dtype=np.float)
                vtkpts = vtkh.numpy_to_vtk(pts, deep=False, array_type=vtkh.FLOAT)
                temp_actor_pts = vtkh.create_actor_pts(pts=vtkpts, color=vtkh.create_color(plot['color']))
                vtk_actors.append(temp_actor_pts)
                # Quad mesh
                lines = np.array(quads, dtype=np.int)
                temp_actor_lines = vtkh.create_actor_mesh(pts=vtkpts, lines=lines,
                                                          color=vtkh.create_color(plot['color']),
                                                          size=self.vconf.line_width)
                vtk_actors.append(temp_actor_lines)

            # Plot evaluated points
            if plot['type'] == 'evalpts' and self.vconf.display_evalpts:
                pts = np.array(plot['ptsarr'], dtype=np.float)
                vtkpts = vtkh.numpy_to_vtk(pts, deep=False, array_type=vtkh.FLOAT)
                temp_actor = vtkh.create_actor_tri(pts=vtkpts, color=vtkh.create_color(plot['color']), d3d=False)
                vtk_actors.append(temp_actor)

            # Update camera focal point
            if plot['type'] == 'midpt':
                focal_point = plot['ptsarr'][0]

        # Render actors
        vtkh.create_render_window(vtk_actors, dict(KeyPressEvent=(self.vconf.keypress_callback, 1.0)),
                                  figure_size=self.vconf.figure_size,
                                  camera_focal_point=focal_point)

class VisVolume(vis.VisAbstract):
    """ VTK visualization module for volumes. """
    def __init__(self, config=VisConfig()):
        super(VisVolume, self).__init__(config=config)
        self._module_config['ctrlpts'] = "points"
        self._module_config['evalpts'] = "points"
        self._module_config['others'] = "midpt"

    def render(self, **kwargs):
        """ Plots the volume and the control points. """
        # Calling parent function
        super(VisVolume, self).render(**kwargs)

        # Initialize a list to store VTK actors
        vtk_actors = []

        # Start plotting
        for plot in self._plots:
            # Plot control points
            if plot['type'] == 'ctrlpts' and self.vconf.display_ctrlpts:
                # Points as spheres
                pts = np.array(plot['ptsarr'], dtype=np.float)
                vtkpts = vtkh.numpy_to_vtk(pts, deep=False, array_type=vtkh.FLOAT)
                temp_actor_pts = vtkh.create_actor_pts(pts=vtkpts, color=plot['color'])
                vtk_actors.append(temp_actor_pts)

            # Plot evaluated points
            if plot['type'] == 'evalpts' and self.vconf.display_evalpts:
                pts = np.array(plot['ptsarr'], dtype=np.float)
                vtkpts = vtkh.numpy_to_vtk(pts, deep=False, array_type=vtkh.FLOAT)
                temp_actor = vtkh.create_actor_tri(pts=vtkpts, color=vtkh.create_color(plot['color']), d3d=True)
                vtk_actors.append(temp_actor)

        # Render actors
        vtkh.create_render_window(vtk_actors, dict(KeyPressEvent=(self.vconf.keypress_callback, 1.0)),
                                  figure_size=self.vconf.figure_size)
<|endoftext|>


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

from __future__ import division, print_function, absolute_import

__all__ = [
    "test_dtype",

    "test_constant", "test_dot_prod", "test_cosine", "test_exp_sine2",
    "test_local", "test_local", "test_polynomial",

    "test_exp", "test_exp_squared", "test_matern32", "test_matern52",
    "test_rational_quadratic", "test_combine",
]

import numpy as np

from .. import kernels
from ..gp import GP

def test_dtype(seed=123):
    np.random.seed(seed)
    kernel = 0.1 * kernels.ExpSquaredKernel(1.5)
    kernel.pars = [1, 2]
    gp = GP(kernel)
    x = np.random.rand(100)
    gp.compute(x, 1e-2)

def do_kernel_t(kernel, N=20, seed=123, eps=1.32e-6):
    np.random.seed(seed)
    t1 = np.random.randn(N, kernel.ndim)
    kernel.test_gradient(t1, eps=eps)

def test_constant():
    do_kernel_t(kernels.ConstantKernel(constant=0.1))
    do_kernel_t(kernels.ConstantKernel(constant=10.0, ndim=2))
    do_kernel_t(kernels.ConstantKernel(constant=5.0, ndim=5))

def test_dot_prod():
    do_kernel_t(kernels.DotProductKernel())
    do_kernel_t(kernels.DotProductKernel(ndim=2))
    do_kernel_t(kernels.DotProductKernel(ndim=5, axes=0))

def test_cosine():
    do_kernel_t(kernels.CosineKernel(period=1.0))
    do_kernel_t(kernels.CosineKernel(period=0.5, ndim=2))
    do_kernel_t(kernels.CosineKernel(period=0.5, ndim=2, axes=1))
    do_kernel_t(kernels.CosineKernel(period=0.75, ndim=5, axes=[2, 3]))

def test_exp_sine2():
    do_kernel_t(kernels.ExpSine2Kernel(gamma=0.4, period=1.0))
    do_kernel_t(kernels.ExpSine2Kernel(gamma=12., period=0.5, ndim=2))
    do_kernel_t(kernels.ExpSine2Kernel(gamma=17., period=0.5, ndim=2, axes=1))
    do_kernel_t(kernels.ExpSine2Kernel(gamma=13.7, ln_period=-0.75, ndim=5,
                                       axes=[2, 3]))
    do_kernel_t(kernels.ExpSine2Kernel(gamma=-0.7, period=0.75, ndim=5,
                                       axes=[2, 3]))
    do_kernel_t(kernels.ExpSine2Kernel(gamma=-10, period=0.75))

def test_local():
    do_kernel_t(kernels.LocalGaussianKernel(width=0.5, location=1.0))
    do_kernel_t(kernels.LocalGaussianKernel(width=0.1, location=0.5, ndim=2))
    do_kernel_t(kernels.LocalGaussianKernel(width=1.5, location=-0.5, ndim=2,
                                            axes=1))
    do_kernel_t(kernels.LocalGaussianKernel(width=2.0, location=0.75, ndim=5,
                                            axes=[2, 3]))

def test_linear():
    do_kernel_t(kernels.LinearKernel(order=0, ln_gamma2=0.0))
    do_kernel_t(kernels.LinearKernel(order=2, ln_gamma2=0.0))
    do_kernel_t(kernels.LinearKernel(order=2, ln_gamma2=0.0))
    do_kernel_t(kernels.LinearKernel(order=5, ln_gamma2=1.0, ndim=2))
    do_kernel_t(kernels.LinearKernel(order=3, ln_gamma2=-1.0, ndim=5,
                                     axes=2))
    k = kernels.LinearKernel(order=0, ln_gamma2=0.0)
    k += kernels.LinearKernel(order=1, ln_gamma2=-1.0)
    k += kernels.LinearKernel(order=2, ln_gamma2=-2.0)
    do_kernel_t(k)

def test_polynomial():
    do_kernel_t(kernels.PolynomialKernel(order=0, ln_sigma2=-10.0))
    do_kernel_t(kernels.PolynomialKernel(order=2, ln_sigma2=-10.0))
    do_kernel_t(kernels.PolynomialKernel(order=2, ln_sigma2=0.0))
    do_kernel_t(kernels.PolynomialKernel(order=5, ln_sigma2=1.0, ndim=2))
    do_kernel_t(kernels.PolynomialKernel(order=3, ln_sigma2=-1.0, ndim=5,
                                         axes=2))

#
# STATIONARY KERNELS
#

def do_stationary_t(kernel_type, **kwargs):
    def build_kernel(metric, **more):
        kws = dict(kwargs, **more)
        return kernel_type(metric=metric, **kws)

    kernel = build_kernel(0.1)
    do_kernel_t(kernel)

    kernel = build_kernel(1.0)
    do_kernel_t(kernel)

    kernel = build_kernel(10.0)
    do_kernel_t(kernel)

    kernel = build_kernel([1.0, 0.1, 10.0], ndim=3)
    do_kernel_t(kernel)

    kernel = build_kernel(1.0, ndim=3)
    do_kernel_t(kernel)

    try:
        kernel = build_kernel([1.0, 0.1, 10.0, 500], ndim=3)
    except:
        pass
    else:
        assert False, "This test should fail"

    kernel = build_kernel(1.0, ndim=3, axes=2)
    do_kernel_t(kernel)

    kernel = build_kernel(1.0, ndim=3, axes=2, bounds=(-0.1, 0.1))
    do_kernel_t(kernel)

def test_exp():
    do_stationary_t(kernels.ExpKernel)

def test_exp_squared():
    do_stationary_t(kernels.ExpSquaredKernel)

def test_matern32():
    do_stationary_t(kernels.Matern32Kernel)

def test_matern52():
    do_stationary_t(kernels.Matern52Kernel)

def test_rational_quadratic():
    do_stationary_t(kernels.RationalQuadraticKernel, alpha=1.0)
    do_stationary_t(kernels.RationalQuadraticKernel, alpha=0.1)
    do_stationary_t(kernels.RationalQuadraticKernel, alpha=10.0)

def test_combine():
    do_kernel_t(12 * kernels.ExpSine2Kernel(gamma=0.4, period=1.0, ndim=5))
    do_kernel_t(12 * kernels.ExpSquaredKernel(0.4, ndim=3) + 0.1)
<commit_msg>not much<commit_after># -*- coding: utf-8 -*-

from __future__ import division, print_function

__all__ = [
    "test_dtype",

    "test_constant", "test_dot_prod", "test_cosine", "test_exp_sine2",
    "test_local", "test_local", "test_polynomial",

    "test_exp", "test_exp_squared", "test_matern32", "test_matern52",
    "test_rational_quadratic", "test_combine",
]

import numpy as np

from .. import kernels
from ..gp import GP

def test_dtype(seed=123):
    np.random.seed(seed)
    kernel = 0.1 * kernels.ExpSquaredKernel(1.5)
    kernel.pars = [1, 2]
    gp = GP(kernel)
    x = np.random.rand(100)
    gp.compute(x, 1e-2)

def do_kernel_t(kernel, N=20, seed=123, eps=1.32e-6):
    np.random.seed(seed)
    t1 = np.random.randn(N, kernel.ndim)
    kernel.test_gradient(t1, eps=eps)

def test_constant():
    do_kernel_t(kernels.ConstantKernel(constant=0.1))
    do_kernel_t(kernels.ConstantKernel(constant=10.0, ndim=2))
    do_kernel_t(kernels.ConstantKernel(constant=5.0, ndim=5))

def test_dot_prod():
    do_kernel_t(kernels.DotProductKernel())
    do_kernel_t(kernels.DotProductKernel(ndim=2))
    do_kernel_t(kernels.DotProductKernel(ndim=5, axes=0))

def test_cosine():
    do_kernel_t(kernels.CosineKernel(period=1.0))
    do_kernel_t(kernels.CosineKernel(period=0.5, ndim=2))
    do_kernel_t(kernels.CosineKernel(period=0.5, ndim=2, axes=1))
    do_kernel_t(kernels.CosineKernel(period=0.75, ndim=5, axes=[2, 3]))

def test_exp_sine2():
    do_kernel_t(kernels.ExpSine2Kernel(gamma=0.4, period=1.0))
    do_kernel_t(kernels.ExpSine2Kernel(gamma=12., period=0.5, ndim=2))
    do_kernel_t(kernels.ExpSine2Kernel(gamma=17., period=0.5, ndim=2, axes=1))
    do_kernel_t(kernels.ExpSine2Kernel(gamma=13.7, ln_period=-0.75, ndim=5,
                                       axes=[2, 3]))
    do_kernel_t(kernels.ExpSine2Kernel(gamma=-0.7, period=0.75, ndim=5,
                                       axes=[2, 3]))
    do_kernel_t(kernels.ExpSine2Kernel(gamma=-10, period=0.75))

def test_local():
    do_kernel_t(kernels.LocalGaussianKernel(width=0.5, location=1.0))
    do_kernel_t(kernels.LocalGaussianKernel(width=0.1, location=0.5, ndim=2))
    do_kernel_t(kernels.LocalGaussianKernel(width=1.5, location=-0.5, ndim=2,
                                            axes=1))
    do_kernel_t(kernels.LocalGaussianKernel(width=2.0, location=0.75, ndim=5,
                                            axes=[2, 3]))

def test_linear():
    do_kernel_t(kernels.LinearKernel(order=0, ln_gamma2=0.0))
    do_kernel_t(kernels.LinearKernel(order=2, ln_gamma2=0.0))
    do_kernel_t(kernels.LinearKernel(order=2, ln_gamma2=0.0))
    do_kernel_t(kernels.LinearKernel(order=5, ln_gamma2=1.0, ndim=2))
    do_kernel_t(kernels.LinearKernel(order=3, ln_gamma2=-1.0, ndim=5,
                                     axes=2))
    k = kernels.LinearKernel(order=0, ln_gamma2=0.0)
    k += kernels.LinearKernel(order=1, ln_gamma2=-1.0)
    k += kernels.LinearKernel(order=2, ln_gamma2=-2.0)
    do_kernel_t(k)

def test_polynomial():
    do_kernel_t(kernels.PolynomialKernel(order=0, ln_sigma2=-10.0))
    do_kernel_t(kernels.PolynomialKernel(order=2, ln_sigma2=-10.0))
    do_kernel_t(kernels.PolynomialKernel(order=2, ln_sigma2=0.0))
    do_kernel_t(kernels.PolynomialKernel(order=5, ln_sigma2=1.0, ndim=2))
    do_kernel_t(kernels.PolynomialKernel(order=3, ln_sigma2=-1.0, ndim=5,
                                         axes=2))

#
# STATIONARY KERNELS
#

def do_stationary_t(kernel_type, **kwargs):
    def build_kernel(metric, **more):
        kws = dict(kwargs, **more)
        return kernel_type(metric=metric, **kws)

    kernel = build_kernel(0.1)
    do_kernel_t(kernel)

    kernel = build_kernel(1.0)
    do_kernel_t(kernel)

    kernel = build_kernel(10.0)
    do_kernel_t(kernel)

    kernel = build_kernel([1.0, 0.1, 10.0], ndim=3)
    do_kernel_t(kernel)

    kernel = build_kernel(1.0, ndim=3)
    do_kernel_t(kernel)

    try:
        kernel = build_kernel([1.0, 0.1, 10.0, 500], ndim=3)
    except:
        pass
    else:
        assert False, "This test should fail"

    kernel = build_kernel(1.0, ndim=3, axes=2)
    do_kernel_t(kernel)

    kernel = build_kernel(1.0, ndim=3, axes=2, bounds=(-0.1, 0.1))
    do_kernel_t(kernel)

def test_exp():
    do_stationary_t(kernels.ExpKernel)

def test_exp_squared():
    do_stationary_t(kernels.ExpSquaredKernel)

def test_matern32():
    do_stationary_t(kernels.Matern32Kernel)

def test_matern52():
    do_stationary_t(kernels.Matern52Kernel)

def test_rational_quadratic():
    do_stationary_t(kernels.RationalQuadraticKernel, alpha=1.0)
    do_stationary_t(kernels.RationalQuadraticKernel, alpha=0.1)
    do_stationary_t(kernels.RationalQuadraticKernel, alpha=10.0)

def test_combine():
    do_kernel_t(12 * kernels.ExpSine2Kernel(gamma=0.4, period=1.0, ndim=5))
    do_kernel_t(12 * kernels.ExpSquaredKernel(0.4, ndim=3) + 0.1)
<|endoftext|>