<commit_before>import pytest
import numpy
from hypothesis import given, settings
from numpy.testing import assert_allclose

from .. import strategies
from ...neural.ops import NumpyOps

MAX_EXAMPLES = 10

@pytest.fixture
def ops():
    return NumpyOps()

def test_get_dropout_empty(ops):
    shape = (2,2)
    drop = 0.0
    mask = ops.get_dropout_mask(shape, drop)
    if drop <= 0.0:
        assert mask is None
    else:
        assert mask is not None

def test_get_dropout_not_empty(ops):
    shape = (2,2)
    drop = 0.1
    mask = ops.get_dropout_mask(shape, drop)
    if drop <= 0.0:
        assert mask is None
    else:
        assert mask is not None
    assert mask.shape == shape
    assert all(value >= 0 for value in mask.flatten())

def test_seq2col_window_one(ops):
    seq = ops.asarray([[1.], [3.], [4.], [5]], dtype='float32')
    cols = ops.seq2col(seq, 1)
    assert_allclose(cols[0], [0., 1., 3.])
    assert_allclose(cols[1], [1., 3., 4.])
    assert_allclose(cols[2], [3., 4., 5.])
    assert_allclose(cols[3], [4., 5., 0.])

def test_backprop_seq2col_window_one(ops):
    cols = ops.asarray([
        [0., 0., 0.],
        [-1., 0., 1.],
        [2., 0., 0.],
    ], dtype='float32')
    expected = [[-1.], [2.], [1.]]
    seq = ops.backprop_seq2col(cols, 1)
    assert_allclose(seq, expected)

@settings(max_examples=MAX_EXAMPLES)
@given(X=strategies.arrays_BI())
def test_dropout_forward(ops, X):
    drop_prob = 0.25
    def drop_first_cell(shape, drop_prob_):
        assert drop_prob_ == drop_prob
        drop_mask = numpy.ones(shape)
        drop_mask /= (1. - drop_prob)
        drop_mask[0, 0] = 0.
        return drop_mask

    ops.get_dropout_mask = drop_first_cell
    output, backprop = ops.dropout(X, drop_prob)
    assert output[0, 0] == 0.
    for i in range(1, output.shape[0]):
        for j in range(output.shape[1]):
            assert output[i, j] == X[i, j] * (1. / 0.75)

@settings(max_examples=MAX_EXAMPLES)
@given(X=strategies.arrays_BI())
def test_dropout_backward(ops, X):
    drop_prob = 0.25
    def drop_first_cell(shape, drop_prob_):
        assert drop_prob_ == drop_prob
        drop_mask = numpy.ones(shape)
        drop_mask /= (1. - drop_prob)
        drop_mask[0, 0] = 0.
        return drop_mask

    ops.get_dropout_mask = drop_first_cell
    output, backprop = ops.dropout(X, drop_prob)
    gradient = numpy.ones(output.shape)
    def finish_update(d, *args, **kwargs):
        return d
    output_gradient = backprop(finish_update)(gradient)
    assert output_gradient[0, 0] == 0.
    for i in range(1, output.shape[0]):
        for j in range(output.shape[1]):
            assert output_gradient[i, j] == 1. * (4. / 3.)

@settings(max_examples=MAX_EXAMPLES)
@given(X=strategies.arrays_BI())
def test_softmax_sums_to_one(ops, X):
    y = ops.softmax(X)
    for row in y:
        assert 0.99999 <= row.sum() <= 1.00001

@settings(max_examples=MAX_EXAMPLES)
@given(X=strategies.arrays_BI())
def test_softmax_works_inplace(ops, X):
    ops.softmax(X, inplace=True)
    for row in X:
        assert 0.99999 <= row.sum() <= 1.00001

@settings(max_examples=MAX_EXAMPLES)
@given(W_b_inputs=strategies.arrays_OI_O_BI())
def test_batch_dot_computes_correctly(ops, W_b_inputs):
    W, _, inputs = W_b_inputs
    y = ops.batch_dot(inputs, W)
    expected = numpy.tensordot(inputs, W, axes=[[1], [1]])
    assert_allclose(y, expected)

@settings(max_examples=MAX_EXAMPLES)
@given(arrays_BI_BO=strategies.arrays_BI_BO())
def test_batch_outer_computes_correctly(ops, arrays_BI_BO):
    bi, bo = arrays_BI_BO
    assert bi.shape[0] == bo.shape[0]
    assert len(bi.shape) == len(bo.shape) == 2
    expected = numpy.tensordot(bo, bi, axes=[[0], [0]])
    assert expected.shape == (bo.shape[1], bi.shape[1])
    oi = ops.batch_outer(bo, bi)
    assert_allclose(oi, expected)

@settings(max_examples=MAX_EXAMPLES)
@given(X=strategies.arrays_BI())
def test_norm_computes_correctly(ops, X):
    for row in X:
        assert_allclose([numpy.linalg.norm(row)], [ops.norm(row)],
            rtol=1e-04, atol=0.0001)

@settings(max_examples=MAX_EXAMPLES)
@given(W_b_X=strategies.arrays_OI_O_BI())
def test_dot_computes_correctly(ops, W_b_X):
    W, b, X = W_b_X
    for x in X:
        expected = numpy.dot(W, x)
        y = numpy.dot(W, x)
        assert_allclose(expected, y)

@settings(max_examples=MAX_EXAMPLES)
@given(X=strategies.arrays_BI())
def test_argmax_computes_correctly(ops, X):
    which = ops.argmax(X, axis=-1)
    for i in range(X.shape[0]):
        assert max(X[i]) == X[i, which[i]]

@settings(max_examples=MAX_EXAMPLES)
@given(X=strategies.arrays_BI())
def test_clip_low_computes_correctly_for_zero(ops, X):
    expected = X * (X > 0.)
    y = ops.clip_low(X, 0.)
    assert_allclose(expected, y)

@settings(max_examples=MAX_EXAMPLES)
@given(X=strategies.arrays_BOP())
def test_take_which_computes_correctly(ops, X):
    which = numpy.argmax(X, axis=-1)
    best = ops.take_which(X, which)
    for i in range(best.shape[0]):
        for j in range(best.shape[1]):
            assert best[i, j] == max(X[i, j])

@settings(max_examples=MAX_EXAMPLES)
@given(X=strategies.arrays_BI())
def test_flatten_unflatten_roundtrip(ops, X):
    flat = ops.flatten([x for x in X])
    assert flat.ndim == 1
    unflat = ops.unflatten(flat, [len(x) for x in X])
    assert_allclose(X, unflat)
<commit_msg>Test random bytes<commit_after>import pytest
import numpy
from hypothesis import given, settings
from numpy.testing import assert_allclose

from .. import strategies
from ...neural.ops import NumpyOps

MAX_EXAMPLES = 10

@pytest.fixture
def ops():
    return NumpyOps()

def test_get_dropout_empty(ops):
    shape = (2,2)
    drop = 0.0
    mask = ops.get_dropout_mask(shape, drop)
    if drop <= 0.0:
        assert mask is None
    else:
        assert mask is not None

def test_get_dropout_not_empty(ops):
    shape = (2,2)
    drop = 0.1
    mask = ops.get_dropout_mask(shape, drop)
    if drop <= 0.0:
        assert mask is None
    else:
        assert mask is not None
    assert mask.shape == shape
    assert all(value >= 0 for value in mask.flatten())

def test_seq2col_window_one(ops):
    seq = ops.asarray([[1.], [3.], [4.], [5]], dtype='float32')
    cols = ops.seq2col(seq, 1)
    assert_allclose(cols[0], [0., 1., 3.])
    assert_allclose(cols[1], [1., 3., 4.])
    assert_allclose(cols[2], [3., 4., 5.])
    assert_allclose(cols[3], [4., 5., 0.])

def test_backprop_seq2col_window_one(ops):
    cols = ops.asarray([
        [0., 0., 0.],
        [-1., 0., 1.],
        [2., 0., 0.],
    ], dtype='float32')
    expected = [[-1.], [2.], [1.]]
    seq = ops.backprop_seq2col(cols, 1)
    assert_allclose(seq, expected)

def test_random_bytes(ops):
    byte_str = ops.random_bytes(10)
    assert len(byte_str) == 10
    assert not all(b == byte_str[0] for b in byte_str)

@settings(max_examples=MAX_EXAMPLES)
@given(X=strategies.arrays_BI())
def test_dropout_forward(ops, X):
    drop_prob = 0.25
    def drop_first_cell(shape, drop_prob_):
        assert drop_prob_ == drop_prob
        drop_mask = numpy.ones(shape)
        drop_mask /= (1. - drop_prob)
        drop_mask[0, 0] = 0.
        return drop_mask

    ops.get_dropout_mask = drop_first_cell
    output, backprop = ops.dropout(X, drop_prob)
    assert output[0, 0] == 0.
    for i in range(1, output.shape[0]):
        for j in range(output.shape[1]):
            assert output[i, j] == X[i, j] * (1. / 0.75)

@settings(max_examples=MAX_EXAMPLES)
@given(X=strategies.arrays_BI())
def test_dropout_backward(ops, X):
    drop_prob = 0.25
    def drop_first_cell(shape, drop_prob_):
        assert drop_prob_ == drop_prob
        drop_mask = numpy.ones(shape)
        drop_mask /= (1. - drop_prob)
        drop_mask[0, 0] = 0.
        return drop_mask

    ops.get_dropout_mask = drop_first_cell
    output, backprop = ops.dropout(X, drop_prob)
    gradient = numpy.ones(output.shape)
    def finish_update(d, *args, **kwargs):
        return d
    output_gradient = backprop(finish_update)(gradient)
    assert output_gradient[0, 0] == 0.
    for i in range(1, output.shape[0]):
        for j in range(output.shape[1]):
            assert output_gradient[i, j] == 1. * (4. / 3.)

@settings(max_examples=MAX_EXAMPLES)
@given(X=strategies.arrays_BI())
def test_softmax_sums_to_one(ops, X):
    y = ops.softmax(X)
    for row in y:
        assert 0.99999 <= row.sum() <= 1.00001

@settings(max_examples=MAX_EXAMPLES)
@given(X=strategies.arrays_BI())
def test_softmax_works_inplace(ops, X):
    ops.softmax(X, inplace=True)
    for row in X:
        assert 0.99999 <= row.sum() <= 1.00001

@settings(max_examples=MAX_EXAMPLES)
@given(W_b_inputs=strategies.arrays_OI_O_BI())
def test_batch_dot_computes_correctly(ops, W_b_inputs):
    W, _, inputs = W_b_inputs
    y = ops.batch_dot(inputs, W)
    expected = numpy.tensordot(inputs, W, axes=[[1], [1]])
    assert_allclose(y, expected)

@settings(max_examples=MAX_EXAMPLES)
@given(arrays_BI_BO=strategies.arrays_BI_BO())
def test_batch_outer_computes_correctly(ops, arrays_BI_BO):
    bi, bo = arrays_BI_BO
    assert bi.shape[0] == bo.shape[0]
    assert len(bi.shape) == len(bo.shape) == 2
    expected = numpy.tensordot(bo, bi, axes=[[0], [0]])
    assert expected.shape == (bo.shape[1], bi.shape[1])
    oi = ops.batch_outer(bo, bi)
    assert_allclose(oi, expected)

@settings(max_examples=MAX_EXAMPLES)
@given(X=strategies.arrays_BI())
def test_norm_computes_correctly(ops, X):
    for row in X:
        assert_allclose([numpy.linalg.norm(row)], [ops.norm(row)],
            rtol=1e-04, atol=0.0001)

@settings(max_examples=MAX_EXAMPLES)
@given(W_b_X=strategies.arrays_OI_O_BI())
def test_dot_computes_correctly(ops, W_b_X):
    W, b, X = W_b_X
    for x in X:
        expected = numpy.dot(W, x)
        y = numpy.dot(W, x)
        assert_allclose(expected, y)

@settings(max_examples=MAX_EXAMPLES)
@given(X=strategies.arrays_BI())
def test_argmax_computes_correctly(ops, X):
    which = ops.argmax(X, axis=-1)
    for i in range(X.shape[0]):
        assert max(X[i]) == X[i, which[i]]

@settings(max_examples=MAX_EXAMPLES)
@given(X=strategies.arrays_BI())
def test_clip_low_computes_correctly_for_zero(ops, X):
    expected = X * (X > 0.)
    y = ops.clip_low(X, 0.)
    assert_allclose(expected, y)

@settings(max_examples=MAX_EXAMPLES)
@given(X=strategies.arrays_BOP())
def test_take_which_computes_correctly(ops, X):
    which = numpy.argmax(X, axis=-1)
    best = ops.take_which(X, which)
    for i in range(best.shape[0]):
        for j in range(best.shape[1]):
            assert best[i, j] == max(X[i, j])

@settings(max_examples=MAX_EXAMPLES)
@given(X=strategies.arrays_BI())
def test_flatten_unflatten_roundtrip(ops, X):
    flat = ops.flatten([x for x in X])
    assert flat.ndim == 1
    unflat = ops.unflatten(flat, [len(x) for x in X])
    assert_allclose(X, unflat)
<|endoftext|>


<commit_before>from thinglang.execution.errors import ReturnInConstructorError, EmptyMethodBody, EmptyThingDefinition, \
    ArgumentCountMismatch
from thinglang.parser.symbols.classes import MethodDefinition, ThingDefinition
from thinglang.parser.symbols.functions import ReturnStatement, MethodCall

def verify_method_definitions(ast):
    for child in ast.find(lambda x: isinstance(x, MethodDefinition)):
        if child.is_constructor() and list(child.find(lambda x: isinstance(x, ReturnStatement))):
            raise ReturnInConstructorError(child)

        if not child.children:
            raise EmptyMethodBody()

def verify_thing_definitions(ast):
    for child in ast.find(lambda x: isinstance(x, ThingDefinition)):
        if not child.children:
            raise EmptyThingDefinition()

def analyze_method_resolution(ast):
    for child in ast.find(MethodCall):
        if child.target[0].is_self():
            thing_definition = child.upwards(ThingDefinition)
        else:
            thing_definition = ast.find(ThingDefinition, lambda x: x.name == child.target[0], single=True)

        if thing_definition is None:
            print('Warning: method resolution cannot be validated!')
        elif len(child.arguments) != len(thing_definition.find(MethodDefinition, lambda x: x.name == child.target[1], single=True).arguments):
            raise ArgumentCountMismatch()

def validate_tree_hierarchy(node, parent=None):
    assert node.parent is parent, 'expected node {} to have parent {} but got {}'.format(node, parent, node.parent)

    for child in node.children:
        validate_tree_hierarchy(child, node)
class Analysis(object):
    def __init__(self, ast):
        self.ast = ast
        self.errors = []
        self.scoping = Frame(expected_key_type=object)
        heap = {LexicalIdentifier("Output"): {LexicalIdentifier("write"): {}}}
        heap.update({
            x.name: x for x in self.ast.children
        })
        self.resolver = Resolver(self.scoping, heap)

    def run(self):
        self.traverse()
        return self

    def traverse(self, node=None, parent=None):
        node = node or self.ast

        if node.implements(AssignmentOperation):
            self.scoping[node.name] = True

        if node.references():
            for ref in (x for x in emit_recursively(node.references(), (Access, LexicalIdentifier)) if not self.validate_scoping(x)):
                self.report_exception(UnresolvedReference(ref))

        if node.SCOPING:
            self.scoping.enter()

        for child in node.children:
            self.traverse(child, node)

        if node.SCOPING:
            self.scoping.exit()

    def validate_scoping(self, reference):
        return self.resolver.lookup(reference) is not Resolver.UNRESOLVED_REFERENCE

def analyze(ast):
    validate_tree_hierarchy(ast)
    analyze_method_resolution(ast)
    verify_method_definitions(ast)
    verify_thing_definitions(ast)
<commit_msg>Generic decorator to specify node assertions<commit_after>from thinglang.execution.errors import ReturnInConstructorError, EmptyMethodBody, EmptyThingDefinition, \
    ArgumentCountMismatch
from thinglang.parser.symbols.classes import MethodDefinition, ThingDefinition
from thinglang.parser.symbols.functions import ReturnStatement, MethodCall

def verify_method_definitions(ast):
    for child in ast.find(lambda x: isinstance(x, MethodDefinition)):
        if child.is_constructor() and list(child.find(lambda x: isinstance(x, ReturnStatement))):
            raise ReturnInConstructorError(child)

        if not child.children:
            raise EmptyMethodBody()

def verify_thing_definitions(ast):
    for child in ast.find(lambda x: isinstance(x, ThingDefinition)):
        if not child.children:
            raise EmptyThingDefinition()

def analyze_method_resolution(ast):
    for child in ast.find(MethodCall):
        if child.target[0].is_self():
            thing_definition = child.upwards(ThingDefinition)
        else:
            thing_definition = ast.find(ThingDefinition, lambda x: x.name == child.target[0], single=True)

        if thing_definition is None:
            print('Warning: method resolution cannot be validated!')
        elif len(child.arguments) != len(thing_definition.find(MethodDefinition, lambda x: x.name == child.target[1], single=True).arguments):
            raise ArgumentCountMismatch()

def inspects(*args):
    def decorator(func):
        func.inspected_types = args
        return func
    return decorator

def validate_tree_hierarchy(node, parent=None):
    assert node.parent is parent, 'expected node {} to have parent {} but got {}'.format(node, parent, node.parent)

    for child in node.children:
        validate_tree_hierarchy(child, node)
class Analysis(object):
    def __init__(self, ast):
        self.ast = ast
        self.errors = []
        self.scoping = Frame(expected_key_type=object)
        heap = {LexicalIdentifier("Output"): {LexicalIdentifier("write"): {}}}
        heap.update({
            x.name: x for x in self.ast.children
        })
        self.resolver = Resolver(self.scoping, heap)

    def run(self):
        self.traverse()
        return self

    def traverse(self, node=None, parent=None):
        node = node or self.ast

        if node.implements(AssignmentOperation):
            self.scoping[node.name] = True

        if node.references():
            for ref in (x for x in emit_recursively(node.references(), (Access, LexicalIdentifier)) if not self.validate_scoping(x)):
                self.report_exception(UnresolvedReference(ref))

        if node.SCOPING:
            self.scoping.enter()

        for child in node.children:
            self.traverse(child, node)

        if node.SCOPING:
            self.scoping.exit()

    def validate_scoping(self, reference):
        return self.resolver.lookup(reference) is not Resolver.UNRESOLVED_REFERENCE

def analyze(ast):
    validate_tree_hierarchy(ast)
    analyze_method_resolution(ast)
    verify_method_definitions(ast)
    verify_thing_definitions(ast)
<|endoftext|>


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

from time import sleep

from django.contrib.auth.models import User
from django.test import LiveServerTestCase
from selenium import webdriver

from ..models import Publication
from ..tests import tests

class LiveTests(LiveServerTestCase):
    fixtures = ['initial_data.json', 'test_data.json']
    urls = 'publications_bootstrap.tests.urls'

    @classmethod
    def setUpClass(cls):
        cls.selenium = webdriver.PhantomJS()
        super(LiveTests, cls).setUpClass()

    @classmethod
    def tearDownClass(cls):
        cls.selenium.quit()
        super(LiveTests, cls).tearDownClass()

    def setUp(self):
        User.objects.create_superuser('admin', 'admin@test.de', 'admin')

        # give the browser a little time
        sleep(1.)

        # login
        self.selenium.get('{0}{1}'.format(self.live_server_url, '/admin/'))
        username_input = self.selenium.find_element_by_name("username")
        username_input.send_keys('admin')
        password_input = self.selenium.find_element_by_name("password")
        password_input.send_keys('admin')
        self.selenium.find_element_by_xpath('//input[@value="Log in"]').click()

    def test_import_bibtex(self):
        count = Publication.objects.count()

        self.selenium.get('{0}{1}'.format(self.live_server_url,
                                          '/admin/publications/publication/import_bibtex/'))
        bibliography_input = self.selenium.find_element_by_name("bibliography")
        bibliography_input.send_keys(tests.TEST_BIBLIOGRAPHY)
        self.selenium.find_element_by_xpath('//input[@value="Import"]').click()

        self.assertEqual(Publication.objects.count() - count, tests.TEST_BIBLIOGRAPHY_COUNT)

    def test_import_bibtex_button(self):
        count = Publication.objects.count()

        self.selenium.get(
            '{0}{1}'.format(self.live_server_url, '/admin/publications/publication/'))
        self.selenium.find_element_by_link_text('Import BibTex').click()
        self.selenium.find_element_by_xpath('//input[@value="Import"]').click()
<commit_msg>Fix NoSuchElementExceptions<commit_after># -*- coding: utf-8 -*-

from time import sleep

from django.contrib.auth.models import User
from django.test import LiveServerTestCase
from selenium import webdriver

from ..models import Publication
from ..tests import tests

class LiveTests(LiveServerTestCase):
    fixtures = ['initial_data.json', 'test_data.json']
    urls = 'publications_bootstrap.tests.urls'

    @classmethod
    def setUpClass(cls):
        cls.selenium = webdriver.PhantomJS()
        super(LiveTests, cls).setUpClass()

    @classmethod
    def tearDownClass(cls):
        cls.selenium.quit()
        super(LiveTests, cls).tearDownClass()

    def setUp(self):
        User.objects.create_superuser('admin', 'admin@test.de', 'admin')

        # give the browser a little time
        sleep(1.)

        # login
        self.selenium.get('{0}{1}'.format(self.live_server_url, '/admin/'))
        username_input = self.selenium.find_element_by_name("username")
        username_input.send_keys('admin')
        password_input = self.selenium.find_element_by_name("password")
        password_input.send_keys('admin')
        self.selenium.find_element_by_xpath('//input[@value="Log in"]').click()

    def test_import_bibtex(self):
        count = Publication.objects.count()

        self.selenium.get('{0}{1}'.format(
            self.live_server_url,
            '/admin/publications_bootstrap/publication/import_bibtex/'))
        bibliography_input = self.selenium.find_element_by_name("bibliography")
        bibliography_input.send_keys(tests.TEST_BIBLIOGRAPHY)
        self.selenium.find_element_by_xpath('//input[@value="Import"]').click()

        self.assertEqual(Publication.objects.count() - count, tests.TEST_BIBLIOGRAPHY_COUNT)

    def test_import_bibtex_button(self):
        count = Publication.objects.count()

        self.selenium.get(
            '{0}{1}'.format(self.live_server_url, '/admin/publications_bootstrap/publication/'))
        self.selenium.find_element_by_link_text('Import BibTex').click()
        self.selenium.find_element_by_xpath('//input[@value="Import"]').click()
<|endoftext|>


<commit_before>#!/usr/bin/env ccp4-python
'''
Created on 29 Dec 2015

@author: jmht
'''

import glob
import os
import sys

from ample.constants import SHARE_DIR
from ample.testing import test_funcs
from ample.testing.integration_util import AMPLEBaseTest

INPUT_DIR = os.path.join(SHARE_DIR, "examples", "toxd-example", "input")
TEST_DICT = {}
TESTFILES_DIR = os.path.join(SHARE_DIR, "testfiles")

# vanilla test
args_vanilla = [
    [ '-fasta', os.path.join(INPUT_DIR, 'toxd_.fasta') ],
    [ '-mtz', os.path.join(INPUT_DIR, '1dtx.mtz') ],
    [ '-percent', '50' ]
]

if not sys.platform.startswith('win'):
    ###############################################################################
    #
    # Making models
    #
    ###############################################################################

    args_rosetta_modelling = args_vanilla + [
        [ '-rosetta_dir', '/opt/rosetta-3.5' ],
        [ '-frags_3mers', os.path.join(INPUT_DIR, 'aat000_03_05.200_v1_3') ],
        [ '-frags_9mers', os.path.join(INPUT_DIR, 'aat000_09_05.200_v1_3') ],
        [ '-nmodels', '40']
    ]

    # Test class that holds the functions to test the RESULTS_PKL file that will be passed in
    class AMPLETest(AMPLEBaseTest):
        def test_rosetta_modelling(self):
            self.assertTrue(self.AMPLE_DICT['AMPLE_finished'])
            self.assertIn('models_dir', self.AMPLE_DICT)
            nmodels = len(glob.glob(self.AMPLE_DICT['models_dir']+"/*.pdb"))
            self.assertEqual(nmodels, 40, "Only {0} models produced".format(nmodels))
            self.assertIn('ensembles', self.AMPLE_DICT)
            self.assertGreater(len(self.AMPLE_DICT['ensembles']), 0, "No ensembles produced")
            self.assertIn('mrbump_results', self.AMPLE_DICT)
            self.assertGreater(len(self.AMPLE_DICT['mrbump_results']), 0, "No MRBUMP results")
            self.assertTrue(self.AMPLE_DICT['success'])
            self.assertGreater(self.AMPLE_DICT['mrbump_results'][0]['SHELXE_CC'], 25,"SHELXE_CC criteria not met")
            return

    TEST_DICT['rosetta_modelling'] = { 'args' : args_rosetta_modelling,
                                       'test' :  AMPLETest,
                                        }

###############################################################################
#
# test from pre-existing models (also used as an opportunity to test the benchmark mode)
#
###############################################################################
args_from_existing_models = args_vanilla + [
    [ '-models', os.path.join(TESTFILES_DIR, 'models') ],
    [ '-native_pdb', os.path.join(INPUT_DIR, '1DTX.pdb') ],
]

# Test class that holds the functions to test the RESULTS_PKL file that will be passed in
class AMPLETest(AMPLEBaseTest):
    def test_from_existing_models(self):

        nclusters = 10
        radii = set([1,3])
        sidechains = set(['polyAla'])
        self.assertTrue(self.AMPLE_DICT['AMPLE_finished'])
        self.assertEqual(nclusters, self.AMPLE_DICT['num_clusters'])
        self.assertEqual(list(radii), self.AMPLE_DICT['subcluster_radius_thresholds'])
        self.assertEqual(list(sidechains), self.AMPLE_DICT['side_chain_treatments'])
        self.assertIn('ensembles', self.AMPLE_DICT)
        nensembles = len(self.AMPLE_DICT['ensembles'])
        self.assertEqual(nensembles, 4, "Incorrect number of ensembles produced: {0}".format(nensembles))
        ensembles_data = self.AMPLE_DICT['ensembles_data']
        # We need to make sure that only the set sidechains and radii are present in the ensembles, so we need to check that what we
        # find is a subset of the allowed values
        for i in xrange(1, len(ensembles_data) + 1):
            cluster_ensembles = [ens for ens in ensembles_data if ens['cluster_num']==i]
            side_chain_treatments = set([ens['side_chain_treatment'] for ens in ensembles_data if ens['cluster_num']==i ]  )
            subcluster_radius_thresholds = set([ens['subcluster_radius_threshold'] for ens in ensembles_data if ens['cluster_num']==i ]  )
            self.assertTrue(side_chain_treatments.issubset(sidechains), side_chain_treatments)
            self.assertTrue(subcluster_radius_thresholds.issubset(radii), subcluster_radius_thresholds)

        self.assertTrue(self.AMPLE_DICT['AMPLE_finished'])
        self.assertGreater(len(self.AMPLE_DICT['mrbump_results']), 0, "No MRBUMP results")
        self.assertTrue(self.AMPLE_DICT['success'],"Job did not succeed")
        self.assertGreater(self.AMPLE_DICT['mrbump_results'][0]['SHELXE_CC'], 25,"SHELXE_CC criteria not met")
        # Check some random benchmarking data
        self.assertTrue(os.path.isfile(os.path.join(self.AMPLE_DICT['benchmark_dir'],'results.csv')),"Missing benchmark results.csv")
        result = self.AMPLE_DICT['benchmark_results'][0]
        self.assertTrue(result['MR_program'], 'PHASER')
        self.assertTrue(result['native_pdb_solvent_content'], 47.67)
        self.assertGreater(result['PHASER_TFZ'], 5.0, "Low PHASER_TFZ: {0}".format(result['PHASER_TFZ']))
        return

TEST_DICT['from_existing_models'] = { 'args' : args_from_existing_models,
                                     'test' :  AMPLETest,
                                      }

###############################################################################
#
# test from pre-existing models with classic_mode- to check we can still run in
# the old modality
#
###############################################################################
args_from_existing_models_classic = args_vanilla + [
                                                    [ '-models', os.path.join(TESTFILES_DIR, 'models') ],
                                                    [ '-classic_mode', True ],
]

# Test class that holds the functions to test the RESULTS_PKL file that will be passed in
class AMPLETest(AMPLEBaseTest):
    def test_from_existing_models_classic(self):
        self.assertTrue(self.AMPLE_DICT['AMPLE_finished'])
        self.assertIn('ensembles', self.AMPLE_DICT)
        nensembles = len(self.AMPLE_DICT['ensembles'])
        self.assertEqual(nensembles, 12, "Incorrect number of ensembles produced: {0}".format(nensembles))
        self.assertIn('mrbump_results', self.AMPLE_DICT)
        self.assertGreater(len(self.AMPLE_DICT['mrbump_results']), 0, "No MRBUMP results")
        self.assertTrue(self.AMPLE_DICT['success'],"Job did not succeed")
        self.assertGreater(self.AMPLE_DICT['mrbump_results'][0]['SHELXE_CC'], 25,"SHELXE_CC criteria not met")
        return

TEST_DICT['from_existing_models_classic'] = { 'args' : args_from_existing_models_classic,
                                             'test' :  AMPLETest,
                                             }

###############################################################################
#
# test from quark models
#
###############################################################################
args_from_quark_models = args_vanilla + [
    [ '-models', os.path.join(TESTFILES_DIR, 'decoys_200.tar.gz') ],
    [ '-do_mr', 'False' ]
]

# Test class that holds the functions to test the RESULTS_PKL file that will be passed in
class AMPLETest(AMPLEBaseTest):
    def test_from_quark_models(self):
        self.assertTrue(self.AMPLE_DICT['AMPLE_finished'])
        nmodels = len(glob.glob(self.AMPLE_DICT['models_dir']+"/*.pdb"))
        self.assertEqual(nmodels, 200, "Only {0} models produced".format(nmodels))
        self.assertIn('ensembles', self.AMPLE_DICT)
        nensembles = len(self.AMPLE_DICT['ensembles'])
        ref_nensembles = 18 if self.AMPLE_DICT["use_scwrl"] else 35 # unmod ensembles without side-chains - Scwrl4 required
        self.assertEqual(nensembles, ref_nensembles, "Incorrect number of ensembles produced: {0} vs {1}".format(nensembles,ref_nensembles))
        return

TEST_DICT['from_quark_models'] = { 'args' : args_from_quark_models,
                                   'test' :  AMPLETest,
                                 }

###############################################################################
#
# End Test Setup
#
###############################################################################

if __name__ == '__main__':
    test_funcs.parse_args(TEST_DICT)
<commit_msg>refactored for style<commit_after>#!/usr/bin/env ccp4-python

__author__ = "Jens Thomas"
__date__ = "29 Dec 2015"
__version__ = "0.1"

import glob
import os
import sys

from ample.constants import SHARE_DIR
from ample.testing import test_funcs
from ample.testing.integration_util import AMPLEBaseTest

INPUT_DIR = os.path.join(SHARE_DIR, "examples", "toxd-example", "input")
TEST_DICT = {}
TESTFILES_DIR = os.path.join(SHARE_DIR, "testfiles")

# vanilla test
args_vanilla = [
    [ '-fasta', os.path.join(INPUT_DIR, 'toxd_.fasta') ],
    [ '-mtz', os.path.join(INPUT_DIR, '1dtx.mtz') ],
    [ '-percent', '50' ]
]

if not sys.platform.startswith('win'):
    ###############################################################################
    #
    # Making models
    #
    ###############################################################################

    args_rosetta_modelling = args_vanilla + [
        [ '-rosetta_dir', '/opt/rosetta-3.5' ],
        [ '-frags_3mers', os.path.join(INPUT_DIR, 'aat000_03_05.200_v1_3') ],
        [ '-frags_9mers', os.path.join(INPUT_DIR, 'aat000_09_05.200_v1_3') ],
        [ '-nmodels', '40']
    ]

    # Test class that holds the functions to test the RESULTS_PKL file that will be passed in
    class AMPLETest(AMPLEBaseTest):
        def test_rosetta_modelling(self):
            self.assertTrue(self.AMPLE_DICT['AMPLE_finished'])
            self.assertIn('models_dir', self.AMPLE_DICT)
            nmodels = len(glob.glob(self.AMPLE_DICT['models_dir']+"/*.pdb"))
            self.assertEqual(nmodels, 40, "Only {0} models produced".format(nmodels))
            self.assertIn('ensembles', self.AMPLE_DICT)
            self.assertGreater(len(self.AMPLE_DICT['ensembles']), 0, "No ensembles produced")
            self.assertIn('mrbump_results', self.AMPLE_DICT)
            self.assertGreater(len(self.AMPLE_DICT['mrbump_results']), 0, "No MRBUMP results")
            self.assertTrue(self.AMPLE_DICT['success'])
            self.assertGreater(self.AMPLE_DICT['mrbump_results'][0]['SHELXE_CC'], 25,"SHELXE_CC criteria not met")

    TEST_DICT['rosetta_modelling'] = {
        'args': args_rosetta_modelling,
        'test':  AMPLETest,
    }

###############################################################################
#
# test from pre-existing models (also used as an opportunity to test the benchmark mode)
#
###############################################################################
args_from_existing_models = args_vanilla + [
    [ '-models', os.path.join(TESTFILES_DIR, 'models') ],
    [ '-native_pdb', os.path.join(INPUT_DIR, '1DTX.pdb') ],
]

# Test class that holds the functions to test the RESULTS_PKL file that will be passed in
class AMPLETest(AMPLEBaseTest):
    def test_from_existing_models(self):

        nclusters = 10
        radii = set([1, 3])
        sidechains = set(['polyAla'])
        self.assertTrue(self.AMPLE_DICT['AMPLE_finished'])
        self.assertEqual(nclusters, self.AMPLE_DICT['num_clusters'])
        self.assertEqual(list(radii), self.AMPLE_DICT['subcluster_radius_thresholds'])
        self.assertEqual(list(sidechains), self.AMPLE_DICT['side_chain_treatments'])
        self.assertIn('ensembles', self.AMPLE_DICT)
        nensembles = len(self.AMPLE_DICT['ensembles'])
        self.assertEqual(nensembles, 4, "Incorrect number of ensembles produced: {0}".format(nensembles))
        ensembles_data = self.AMPLE_DICT['ensembles_data']
        # We need to make sure that only the set sidechains and radii are present in the ensembles, so we need to check that what we
        # find is a subset of the allowed values
        for i in xrange(1, len(ensembles_data) + 1):
            cluster_ensembles = [ens for ens in ensembles_data if ens['cluster_num']==i]
            side_chain_treatments = set([ens['side_chain_treatment'] for ens in ensembles_data if ens['cluster_num']==i ]  )
            subcluster_radius_thresholds = set([ens['subcluster_radius_threshold'] for ens in ensembles_data if ens['cluster_num']==i ]  )
            self.assertTrue(side_chain_treatments.issubset(sidechains), side_chain_treatments)
            self.assertTrue(subcluster_radius_thresholds.issubset(radii), subcluster_radius_thresholds)

        self.assertTrue(self.AMPLE_DICT['AMPLE_finished'])
        self.assertGreater(len(self.AMPLE_DICT['mrbump_results']), 0, "No MRBUMP results")
        self.assertTrue(self.AMPLE_DICT['success'],"Job did not succeed")
        self.assertGreater(self.AMPLE_DICT['mrbump_results'][0]['SHELXE_CC'], 25,"SHELXE_CC criteria not met")
        # Check some random benchmarking data
        self.assertTrue(os.path.isfile(os.path.join(self.AMPLE_DICT['benchmark_dir'],'results.csv')),"Missing benchmark results.csv")
        result = self.AMPLE_DICT['benchmark_results'][0]
        self.assertTrue(result['MR_program'], 'PHASER')
        self.assertTrue(result['native_pdb_solvent_content'], 47.67)
        self.assertGreater(result['PHASER_TFZ'], 5.0, "Low PHASER_TFZ: {0}".format(result['PHASER_TFZ']))

TEST_DICT['from_existing_models'] = {
    'args': args_from_existing_models,
    'test':  AMPLETest,
}

###############################################################################
#
# test from pre-existing models with classic_mode- to check we can still run in
# the old modality
#
###############################################################################
args_from_existing_models_classic = args_vanilla + [
                                                    [ '-models', os.path.join(TESTFILES_DIR, 'models') ],
                                                    [ '-classic_mode', True ],
]

# Test class that holds the functions to test the RESULTS_PKL file that will be passed in
class AMPLETest(AMPLEBaseTest):
    def test_from_existing_models_classic(self):
        self.assertTrue(self.AMPLE_DICT['AMPLE_finished'])
        self.assertIn('ensembles', self.AMPLE_DICT)
        nensembles = len(self.AMPLE_DICT['ensembles'])
        self.assertEqual(nensembles, 12, "Incorrect number of ensembles produced: {0}".format(nensembles))
        self.assertIn('mrbump_results', self.AMPLE_DICT)
        self.assertGreater(len(self.AMPLE_DICT['mrbump_results']), 0, "No MRBUMP results")
        self.assertTrue(self.AMPLE_DICT['success'],"Job did not succeed")
        self.assertGreater(self.AMPLE_DICT['mrbump_results'][0]['SHELXE_CC'], 25,"SHELXE_CC criteria not met")

TEST_DICT['from_existing_models_classic'] = {
    'args': args_from_existing_models_classic,
    'test':  AMPLETest,
}

###############################################################################
#
# test from quark models
#
###############################################################################
args_from_quark_models = args_vanilla + [
    [ '-models', os.path.join(TESTFILES_DIR, 'decoys_200.tar.gz') ],
    [ '-do_mr', 'False' ]
]

# Test class that holds the functions to test the RESULTS_PKL file that will be passed in
class AMPLETest(AMPLEBaseTest):
    def test_from_quark_models(self):
        self.assertTrue(self.AMPLE_DICT['AMPLE_finished'])
        nmodels = len(glob.glob(
            os.path.join(self.AMPLE_DICT['models_dir'], "*.pdb")
        ))
        self.assertEqual(nmodels, 200, "Only {0} models produced".format(nmodels))
        self.assertIn('ensembles', self.AMPLE_DICT)
        nensembles = len(self.AMPLE_DICT['ensembles'])
        ref_nensembles = 18 if self.AMPLE_DICT["use_scwrl"] else 35 # unmod ensembles without side-chains - Scwrl4 required
        msg = "Incorrect number of ensembles produced: {0} vs {1}".format(nensembles, ref_nensembles)
        self.assertEqual(nensembles, ref_nensembles, msg)

TEST_DICT['from_quark_models'] = {
    'args': args_from_quark_models,
    'test':  AMPLETest,
}

###############################################################################
#
# End Test Setup
#
###############################################################################

if __name__ == '__main__':
    test_funcs.parse_args(TEST_DICT)
<|endoftext|>


<commit_before># -*- coding: utf8 -*-
# This file is part of PYBOSSA.
#
# Copyright (C) 2017 Scifabric LTD.
#
# PYBOSSA is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PYBOSSA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with PYBOSSA.  If not, see <http://www.gnu.org/licenses/>.

from sqlalchemy.exc import IntegrityError

from pybossa.repositories import Repository
from pybossa.model.helpingmaterial import HelpingMaterial
from pybossa.exc import WrongObjectError, DBIntegrityError

class HelpingMaterialRepository(Repository):

    def __init__(self, db):
        self.db = db

    def get(self, id):
        return self.db.session.query(HelpingMaterial).get(id)

    def get_by(self, **attributes):
        return self.db.session.query(HelpingMaterial).filter_by(**attributes).first()

    def filter_by(self, limit=None, offset=0, yielded=False,
                  last_id=None, fulltextsearch=None, desc=False, **filters):
        return self._filter_by(HelpingMaterial, limit, offset, yielded,
                               last_id, **filters)

    def save(self, blogpost):
        self._validate_can_be('saved', blogpost)
        try:
            self.db.session.add(blogpost)
            self.db.session.commit()
        except IntegrityError as e:
            self.db.session.rollback()
            raise DBIntegrityError(e)

    def update(self, blogpost):
        self._validate_can_be('updated', blogpost)
        try:
            self.db.session.merge(blogpost)
            self.db.session.commit()
        except IntegrityError as e:
            self.db.session.rollback()
            raise DBIntegrityError(e)

    def delete(self, blogpost):
        self._validate_can_be('deleted', blogpost)
        blog = self.db.session.query(HelpingMaterial).filter(HelpingMaterial.id==blogpost.id).first()
        self.db.session.delete(blog)
        self.db.session.commit()

    def _validate_can_be(self, action, blogpost):
        if not isinstance(blogpost, HelpingMaterial):
            name = blogpost.__class__.__name__
            msg = '%s cannot be %s by %s' % (name, action, self.__class__.__name__)
            raise WrongObjectError(msg)
<commit_msg>Fixes.<commit_after># -*- coding: utf8 -*-
# This file is part of PYBOSSA.
#
# Copyright (C) 2017 Scifabric LTD.
#
# PYBOSSA is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PYBOSSA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with PYBOSSA.  If not, see <http://www.gnu.org/licenses/>.

from sqlalchemy.exc import IntegrityError

from pybossa.repositories import Repository
from pybossa.model.helpingmaterial import HelpingMaterial
from pybossa.exc import WrongObjectError, DBIntegrityError

class HelpingMaterialRepository(Repository):

    def get(self, id):
        return self.db.session.query(HelpingMaterial).get(id)

    def get_by(self, **attributes):
        return self.db.session.query(HelpingMaterial).filter_by(**attributes).first()

    def filter_by(self, limit=None, offset=0, yielded=False,
                  last_id=None, fulltextsearch=None, desc=False, **filters):
        return self._filter_by(HelpingMaterial, limit, offset, yielded,
                               last_id, fulltextsearch, desc, **filters)

    def save(self, blogpost):
        self._validate_can_be('saved', blogpost)
        try:
            self.db.session.add(blogpost)
            self.db.session.commit()
        except IntegrityError as e:
            self.db.session.rollback()
            raise DBIntegrityError(e)

    def update(self, blogpost):
        self._validate_can_be('updated', blogpost)
        try:
            self.db.session.merge(blogpost)
            self.db.session.commit()
        except IntegrityError as e:
            self.db.session.rollback()
            raise DBIntegrityError(e)

    def delete(self, blogpost):
        self._validate_can_be('deleted', blogpost)
        blog = self.db.session.query(HelpingMaterial).filter(HelpingMaterial.id==blogpost.id).first()
        self.db.session.delete(blog)
        self.db.session.commit()

    def _validate_can_be(self, action, blogpost):
        if not isinstance(blogpost, HelpingMaterial):
            name = blogpost.__class__.__name__
            msg = '%s cannot be %s by %s' % (name, action, self.__class__.__name__)
            raise WrongObjectError(msg)
<|endoftext|>


<commit_before>#! /usr/bin/env python
import sys
import argparse
import cv2

def main():
    args = parse_args()
    print(args.file)
    print(args.width)
    print(args.height)
    print(args.color)
    print(args.codec)
    print(args.fps)
    record_video()
    return 0

def record_video(filename='output.avi', width=640, height=480, color=True,
                 fps=20, codec='MJPG'):

    # TODO: Add ability to select the camera
    # Open video
    cap = cv2.VideoCapture(1)

    # Define the codec and create the VideoWriterobject
    fourcc = cv2.VideoWriter_fourcc(*codec)
    out = cv2.VideoWriter(filename, fourcc, fps, (width, height))

    while(cap.isOpened()):
        ret, frame = cap.read()
        if ret is True:
            frame = cv2.flip(frame, 0)

            # write the flipped frame
            out.write(frame)

            cv2.imshow('frame', frame)
            if cv2.waitKey(1) & 0xFF == 27:
                break
        else:
            break

    # Release everything if job is finished
    cap.release()
    out.release()
    cv2.destroyAllWindows()
    return 0

def parse_args():
    parser = argparse.ArgumentParser(description='Record video from a webcam.')
    parser.add_argument('file', type=str, help='Path to file to be created')
    # TODO: Add allowable options instead (VGA, QVGA etc.)
    parser.add_argument('-sw', '--width', nargs='?', default=640, type=int,
                        help='Video width', dest='width')
    parser.add_argument('-sh', '--height', nargs='?', default=480, type=int,
                        help='Video height', dest='height')
    # TODO: Have y/n as tuple of options
    parser.add_argument('-c', '--color', nargs='?', default=True, type=bool,
                        help='Record in color', dest='color')
    # TODO: Add options
    parser.add_argument('-cc', '--codec', nargs='?', default='MJPG', type=str,
                        help='Codec to use', dest='codec')
    parser.add_argument('-f', '--fps', nargs='?', default=20, type=int,
                        help='Frames per second', dest='fps')
    args = parser.parse_args()

    return args

if __name__ == "__main__":
    # main should return 0 for success, something else for error.
    sys.exit(main())
<commit_msg>Dummy change<commit_after>#! /usr/bin/env python
import sys
import argparse
import cv2

def main():
    args = parse_args()
    print(args.file)
    print(args.width)
    print(args.height)
    print(args.color)
    print(args.codec)
    print(args.fps)
    record_video()
    return 0

def record_video(filename='output.avi', width=640, height=480, color=True,
                 fps=20, codec='MJPG'):

    # TODO: Add ability to select the camera
    # Open video
    cap = cv2.VideoCapture(1)

    # Define the codec and create the VideoWriterobject
    # TODO: Check validity of the codec
    fourcc = cv2.VideoWriter_fourcc(*codec)
    out = cv2.VideoWriter(filename, fourcc, fps, (width, height))

    while(cap.isOpened()):
        ret, frame = cap.read()
        if ret is True:
            frame = cv2.flip(frame, 0)

            # write the flipped frame
            out.write(frame)

            cv2.imshow('frame', frame)
            if cv2.waitKey(1) & 0xFF == 27:
                break
        else:
            break

    # Release everything if job is finished
    cap.release()
    out.release()
    cv2.destroyAllWindows()
    return 0

def parse_args():
    parser = argparse.ArgumentParser(description='Record video from a webcam.')
    parser.add_argument('file', type=str, help='Path to file to be created')
    # TODO: Add allowable options instead (VGA, QVGA etc.)
    parser.add_argument('-sw', '--width', nargs='?', default=640, type=int,
                        help='Video width', dest='width')
    parser.add_argument('-sh', '--height', nargs='?', default=480, type=int,
                        help='Video height', dest='height')
    # TODO: Have y/n as tuple of options
    parser.add_argument('-c', '--color', nargs='?', default=True, type=bool,
                        help='Record in color', dest='color')
    # TODO: Add options
    parser.add_argument('-cc', '--codec', nargs='?', default='MJPG', type=str,
                        help='Codec to use', dest='codec')
    parser.add_argument('-f', '--fps', nargs='?', default=20, type=int,
                        help='Frames per second', dest='fps')
    args = parser.parse_args()

    return args

if __name__ == "__main__":
    # main should return 0 for success, something else for error.
    sys.exit(main())
<|endoftext|>


<commit_before>"""
This file imports all the relevant classes for daily use.
"""

# This snippet ensures all submodules get reloaded properly as we like to
# modify things when using it.
from importlib import reload
import pycqed.analysis_v2.base_analysis as ba
reload(ba)
import pycqed.analysis_v2.simple_analysis as sa
reload(sa)
import pycqed.analysis_v2.timedomain_analysis as ta
reload(ta)
import pycqed.analysis_v2.readout_analysis as ra
reload(ra)
import pycqed.analysis_v2.syndrome_analysis as synda
reload(sa)
# only one of these two files should exist in the end
import pycqed.analysis_v2.cryo_scope_analysis as csa
reload(csa)
import pycqed.analysis_v2.cryo_spectrumanalyzer_analysis as csa
reload(csa)
import pycqed.analysis_v2.distortions_analysis as da
import pycqed.analysis_v2.optimization_analysis as oa
reload(da)
import pycqed.analysis_v2.coherence_analysis as cs
reload(cs)
import pycqed.analysis_v2.spectroscopy_analysis as sa
reload(sa)
import pycqed.analysis_v2.dac_scan_analysis as da
reload(da)
import pycqed.analysis_v2.quantum_efficiency_analysis as qea
reload(qea)
import pycqed.analysis_v2.cross_dephasing_analysis as cda
reload(cda)
import pycqed.analysis_v2.randomized_benchmarking_analysis as rba
reload(rba)
import pycqed.analysis_v2.gate_set_tomography_analysis as gsa
reload(gsa)

from pycqed.analysis_v2.base_analysis import *
from pycqed.analysis_v2.simple_analysis import (
    Basic1DAnalysis, Basic1DBinnedAnalysis,
    Basic2DAnalysis, Basic2DInterpolatedAnalysis)
from pycqed.analysis_v2.timedomain_analysis import (
    FlippingAnalysis, Intersect_Analysis, CZ_1QPhaseCal_Analysis,
    Oscillation_Analysis,
    Conditional_Oscillation_Analysis, Idling_Error_Rate_Analyisis,
    Grovers_TwoQubitAllStates_Analysis)
from pycqed.analysis_v2.readout_analysis import Singleshot_Readout_Analysis, \
    Multiplexed_Readout_Analysis
from pycqed.analysis_v2.syndrome_analysis import (
    Single_Qubit_RoundsToEvent_Analysis, One_Qubit_Paritycheck_Analysis)

from pycqed.analysis_v2.cryo_scope_analysis import RamZFluxArc, \
    SlidingPulses_Analysis, Cryoscope_Analysis
from pycqed.analysis_v2.cryo_spectrumanalyzer_analysis import Cryospec_Analysis
from pycqed.analysis_v2.distortions_analysis import Scope_Trace_analysis

from pycqed.analysis_v2.optimization_analysis import OptimizationAnalysis
from pycqed.analysis_v2.timing_cal_analysis import Timing_Cal_Flux_Coarse

from pycqed.analysis_v2.coherence_analysis import CoherenceTimesAnalysis, CoherenceTimesAnalysisSingle
from pycqed.analysis_v2.spectroscopy_analysis import Spectroscopy, ResonatorSpectroscopy, VNA_analysis, complex_spectroscopy
from pycqed.analysis_v2.dac_scan_analysis import FluxFrequency
from pycqed.analysis_v2.quantum_efficiency_analysis import QuantumEfficiencyAnalysis, DephasingAnalysisSingleScans, DephasingAnalysisSweep, SSROAnalysisSingleScans, SSROAnalysisSweep, QuantumEfficiencyAnalysisTWPA
from pycqed.analysis_v2.cross_dephasing_analysis import CrossDephasingAnalysis
from pycqed.analysis_v2.randomized_benchmarking_analysis import (
    RandomizedBenchmarking_SingleQubit_Analysis,
    RandomizedBenchmarking_TwoQubit_Analysis)
from pycqed.analysis_v2.gate_set_tomography_analysis import \
    GST_SingleQubit_DataExtraction, GST_TwoQubit_DataExtraction
<commit_msg>Import fine timing analysis when loading  ma2<commit_after>"""
This file imports all the relevant classes for daily use.
"""

# This snippet ensures all submodules get reloaded properly as we like to
# modify things when using it.
from importlib import reload
import pycqed.analysis_v2.base_analysis as ba
reload(ba)
import pycqed.analysis_v2.simple_analysis as sa
reload(sa)
import pycqed.analysis_v2.timedomain_analysis as ta
reload(ta)
import pycqed.analysis_v2.readout_analysis as ra
reload(ra)
import pycqed.analysis_v2.syndrome_analysis as synda
reload(sa)
# only one of these two files should exist in the end
import pycqed.analysis_v2.cryo_scope_analysis as csa
reload(csa)
import pycqed.analysis_v2.cryo_spectrumanalyzer_analysis as csa
reload(csa)
import pycqed.analysis_v2.distortions_analysis as da
import pycqed.analysis_v2.optimization_analysis as oa
reload(da)
import pycqed.analysis_v2.coherence_analysis as cs
reload(cs)
import pycqed.analysis_v2.spectroscopy_analysis as sa
reload(sa)
import pycqed.analysis_v2.dac_scan_analysis as da
reload(da)
import pycqed.analysis_v2.quantum_efficiency_analysis as qea
reload(qea)
import pycqed.analysis_v2.cross_dephasing_analysis as cda
reload(cda)
import pycqed.analysis_v2.randomized_benchmarking_analysis as rba
reload(rba)
import pycqed.analysis_v2.gate_set_tomography_analysis as gsa
reload(gsa)

from pycqed.analysis_v2.base_analysis import *
from pycqed.analysis_v2.simple_analysis import (
    Basic1DAnalysis, Basic1DBinnedAnalysis,
    Basic2DAnalysis, Basic2DInterpolatedAnalysis)
from pycqed.analysis_v2.timedomain_analysis import (
    FlippingAnalysis, Intersect_Analysis, CZ_1QPhaseCal_Analysis,
    Oscillation_Analysis,
    Conditional_Oscillation_Analysis, Idling_Error_Rate_Analyisis,
    Grovers_TwoQubitAllStates_Analysis)
from pycqed.analysis_v2.readout_analysis import Singleshot_Readout_Analysis, \
    Multiplexed_Readout_Analysis
from pycqed.analysis_v2.syndrome_analysis import (
    Single_Qubit_RoundsToEvent_Analysis, One_Qubit_Paritycheck_Analysis)

from pycqed.analysis_v2.cryo_scope_analysis import RamZFluxArc, \
    SlidingPulses_Analysis, Cryoscope_Analysis
from pycqed.analysis_v2.cryo_spectrumanalyzer_analysis import Cryospec_Analysis
from pycqed.analysis_v2.distortions_analysis import Scope_Trace_analysis

from pycqed.analysis_v2.optimization_analysis import OptimizationAnalysis
from pycqed.analysis_v2.timing_cal_analysis import Timing_Cal_Flux_Coarse, Timing_Cal_Flux_Fine

from pycqed.analysis_v2.coherence_analysis import CoherenceTimesAnalysis, CoherenceTimesAnalysisSingle
from pycqed.analysis_v2.spectroscopy_analysis import Spectroscopy, ResonatorSpectroscopy, VNA_analysis, complex_spectroscopy
from pycqed.analysis_v2.dac_scan_analysis import FluxFrequency
from pycqed.analysis_v2.quantum_efficiency_analysis import QuantumEfficiencyAnalysis, DephasingAnalysisSingleScans, DephasingAnalysisSweep, SSROAnalysisSingleScans, SSROAnalysisSweep, QuantumEfficiencyAnalysisTWPA
from pycqed.analysis_v2.cross_dephasing_analysis import CrossDephasingAnalysis
from pycqed.analysis_v2.randomized_benchmarking_analysis import (
    RandomizedBenchmarking_SingleQubit_Analysis,
    RandomizedBenchmarking_TwoQubit_Analysis)
from pycqed.analysis_v2.gate_set_tomography_analysis import \
    GST_SingleQubit_DataExtraction, GST_TwoQubit_DataExtraction
<|endoftext|>


<commit_before>__author__ = 'christopher'
import numpy as np
from pyiid.wrappers.scatter import Scatter
from pyiid.tests import setup_atoms, generate_experiment
from pyiid.testing.decorators import known_fail_if

def test_scatter_fq():
    for i in range(4):
        if i == 0:
            exp = None
        else:
            exp = generate_experiment()
        scat = Scatter(exp_dict=exp)
        # Test a set of different sized ensembles
        for n in np.logspace(1, 3, 3):
            atoms = setup_atoms(int(n))
            fq = scat.get_fq(atoms)
            # Check that Scatter gave back something
            assert fq is not None
            # Check that all the values are not zero
            assert np.any(fq)

def test_scatter_sq():
    for i in range(4):
        if i == 0:
            exp = None
        else:
            exp = generate_experiment()
        scat = Scatter(exp_dict=exp)
        # Test a set of different sized ensembles
        for n in np.logspace(1, 3, 3):
            atoms = setup_atoms(int(n))
            sq = scat.get_sq(atoms)
            # Check that Scatter gave back something
            assert sq is not None
            # Check that all the values are not zero
            assert np.any(sq)

def test_scatter_iq():
    for i in range(4):
        if i == 0:
            exp = None
        else:
            exp = generate_experiment()
        scat = Scatter(exp_dict=exp)
        # Test a set of different sized ensembles
        for n in np.logspace(1, 3, 3):
            atoms = setup_atoms(int(n))
            iq = scat.get_iq(atoms)
            # Check that Scatter gave back something
            assert iq is not None
            # Check that all the values are not zero
            assert np.any(iq)

def test_scatter_pdf():
    for i in range(4):
        if i == 0:
            exp = None
        else:
            exp = generate_experiment()
        scat = Scatter(exp_dict=exp)
        # Test a set of different sized ensembles
        for n in np.logspace(1, 3, 3):
            atoms = setup_atoms(int(n))
            pdf = scat.get_pdf(atoms)
            # Check that Scatter gave back something
            assert pdf is not None
            # Check that all the values are not zero
            assert np.any(pdf)

@known_fail_if(True)
def test_gpu_scatter_fail():
    for i in range(4):
        if i == 0:
            exp = None
        else:
            exp = generate_experiment()
        scat = Scatter(exp_dict=exp)
        scat.set_processor('Multi-GPU')
        assert scat.processor == 'Multi-GPU'

if __name__ == '__main__':
    import nose
    nose.runmodule(argv=['-s', '--with-doctest'], exit=False)<commit_msg>TST: add grad fq, grad pdf<commit_after>__author__ = 'christopher'
import numpy as np
from pyiid.wrappers.scatter import Scatter
from pyiid.tests import setup_atoms, generate_experiment
from pyiid.testing.decorators import known_fail_if

def test_scatter_fq():
    for i in range(4):
        if i == 0:
            exp = None
        else:
            exp = generate_experiment()
        scat = Scatter(exp_dict=exp)
        # Test a set of different sized ensembles
        for n in np.logspace(1, 3, 3):
            atoms = setup_atoms(int(n))
            fq = scat.get_fq(atoms)
            # Check that Scatter gave back something
            assert fq is not None
            # Check that all the values are not zero
            assert np.any(fq)

def test_scatter_sq():
    for i in range(4):
        if i == 0:
            exp = None
        else:
            exp = generate_experiment()
        scat = Scatter(exp_dict=exp)
        # Test a set of different sized ensembles
        for n in np.logspace(1, 3, 3):
            atoms = setup_atoms(int(n))
            sq = scat.get_sq(atoms)
            # Check that Scatter gave back something
            assert sq is not None
            # Check that all the values are not zero
            assert np.any(sq)

def test_scatter_iq():
    for i in range(4):
        if i == 0:
            exp = None
        else:
            exp = generate_experiment()
        scat = Scatter(exp_dict=exp)
        # Test a set of different sized ensembles
        for n in np.logspace(1, 3, 3):
            atoms = setup_atoms(int(n))
            iq = scat.get_iq(atoms)
            # Check that Scatter gave back something
            assert iq is not None
            # Check that all the values are not zero
            assert np.any(iq)

def test_scatter_pdf():
    for i in range(4):
        if i == 0:
            exp = None
        else:
            exp = generate_experiment()
        scat = Scatter(exp_dict=exp)
        # Test a set of different sized ensembles
        for n in np.logspace(1, 3, 3):
            atoms = setup_atoms(int(n))
            pdf = scat.get_pdf(atoms)
            # Check that Scatter gave back something
            assert pdf is not None
            # Check that all the values are not zero
            assert np.any(pdf)

def test_scatter_grad_fq():
    for i in range(4):
        if i == 0:
            exp = None
        else:
            exp = generate_experiment()
        scat = Scatter(exp_dict=exp)
        # Test a set of different sized ensembles
        for n in np.logspace(1, 3, 3):
            atoms = setup_atoms(int(n))
            grad_fq = scat.get_grad_fq(atoms)
            # Check that Scatter gave back something
            assert grad_fq is not None
            # Check that all the values are not zero
            assert np.any(grad_fq)

def test_scatter_grad_pdf():
    for i in range(4):
        if i == 0:
            exp = None
        else:
            exp = generate_experiment()
        scat = Scatter(exp_dict=exp)
        # Test a set of different sized ensembles
        for n in np.logspace(1, 3, 3):
            atoms = setup_atoms(int(n))
            grad_pdf = scat.get_grad_pdf(atoms)
            # Check that Scatter gave back something
            assert grad_pdf is not None
            # Check that all the values are not zero
            assert np.any(grad_pdf)

@known_fail_if(True)
def test_gpu_scatter_fail():
    for i in range(4):
        if i == 0:
            exp = None
        else:
            exp = generate_experiment()
        scat = Scatter(exp_dict=exp)
        scat.set_processor('Multi-GPU')
        assert scat.processor == 'Multi-GPU'

if __name__ == '__main__':
    import nose
    nose.runmodule(argv=['-s', '--with-doctest'], exit=False)<|endoftext|>


<commit_before>#!/usr/bin/python
#A script to add icons to an existing Openbox menu file
#This is untested and may not work at all as expected
#By Charles Bos

import sys
import os

def usage() :
    print('''Usage:
  obiconadder.py <path to menu file>''')

def getDotDesktop(item, identifier) :
    for x in dotDesktops :
        file = open(x, "r", encoding = "utf-8")
        fileText = file.read()
        file.close()
        idStart = fileText.find(identifier)
        idEnd = fileText.find("\n", idStart)
        if item == fileText[idStart + 5:idEnd] : return x
    #As a fallback position, try and match the execute line from the file with
    #the .desktop filename
    if identifier == "Exec=" :
        for x in dotDesktops :
            if item == x.split("/")[-1].split(".desktop")[0] : return x
    return None

def getIcon(dotDesktop) :
    file = open(dotDesktop, encoding = "utf-8")
    fileText = file.read()
    file.close()
    iconStart = fileText.find("Icon=")
    if iconStart != -1 :
        iconEnd = fileText.find("\n", iconStart)
        for x in iconsList :
            if fileText[iconStart + 5:iconEnd] == x.split("/")[-1].split(".")[0].split(" %")[0] :
                return x
        return None
    else : return None

#Handle args
args = sys.argv
if (len(args) != 2) or (args[1] == '-h') or (args[1] == '--help') :
    usage()
    os._exit(0)
elif os.path.isfile(args[1]) : menuFile = args[1]
else :
    usage()
    os._exit(0)

#Get locations of icons and .desktops
iconDirs = ["/usr/share/pixmaps", "/usr/share/icons/hicolor/48x48"]
iconsList = []
for x in iconDirs :
    for root, dirs, files in os.walk(x, topdown=False):
        for name in files:
            iconsList.append(os.path.join(root, name))
dotDesktops = os.listdir("/usr/share/applications")
for x in dotDesktops : dotDesktops[dotDesktops.index(x)] = "/usr/share/applications/" + x

#Start scanning menu file
file = open(menuFile, "r")
fileText = file.read()
file.close()
newFile = fileText[:]

#Get icons for applications
nameStart = fileText.find('<item label="')
nameEnd = fileText.find('">', nameStart)
while 0 <= nameStart <= len(fileText) :
    dotDesktop = getDotDesktop(fileText[nameStart + 13:nameEnd].split('" icon="')[0], "Name=")
    if dotDesktop != None : icon = getIcon(dotDesktop)
    else :
        #If identification by name fails, try by exec instead
        execStart = fileText.find("<execute>", nameEnd)
        execEnd = fileText.find("</execute>", execStart)
        dotDesktop = getDotDesktop(fileText[execStart + 9:execEnd], "Exec=")
        if dotDesktop != None : icon = getIcon(dotDesktop)
        else : icon = None
    if icon != None :
        newFile = newFile.replace(fileText[nameStart + 13:nameEnd], fileText[nameStart + 13:nameEnd].split('" icon="')[0] + '" icon="' + icon)
    nameStart = fileText.find('<item label="', nameEnd)
    nameEnd = fileText.find('">', nameStart)

#Get icons for categories (needs gnome-icon-theme, I might improve this later on)
catStart = fileText.find('<menu id="')
catEnd = fileText.find('" ', catStart)
labelStart = fileText.find('label="', catEnd)
labelEnd = fileText.find('">', labelStart)
while 0 <= catStart <= len(fileText) :
    icon = "/usr/share/icons/gnome/48x48/categories/applications-" + fileText[labelStart + 7:labelEnd].lower() + ".png"
    if os.path.isfile(icon) :
        newFile = newFile.replace(fileText[catStart:labelStart - 1], fileText[catStart:catEnd + 1] + ' icon="' + icon + '"')
    catStart = fileText.find('<menu id="', labelEnd)
    catEnd = fileText.find('" ', catStart)
    labelStart = fileText.find('label="', catEnd)
    labelEnd = fileText.find('">', labelStart)

#Now add the icons
#This will replace the contents of the old file
file = open(menuFile, "w")
print(newFile.rstrip("\n"), file = file)
file.close()<commit_msg>obiconadder.py - add support for the user set icon theme<commit_after>#!/usr/bin/python
#A script to add icons to an existing Openbox menu file
#This is untested and may not work at all as expected
#By Charles Bos

import sys
import os

def usage() :
    print('''Usage:
  obiconadder.py <path to menu file>''')

def getDotDesktop(item, identifier) :
    for x in dotDesktops :
        file = open(x, "r", encoding = "utf-8")
        fileText = file.read()
        file.close()
        idStart = fileText.find(identifier)
        idEnd = fileText.find("\n", idStart)
        if item == fileText[idStart + 5:idEnd] : return x
    #As a fallback position, try and match the execute line from the file with
    #the .desktop filename
    if identifier == "Exec=" :
        for x in dotDesktops :
            if item == x.split("/")[-1].split(".desktop")[0] : return x
    return None

def getIcon(dotDesktop) :
    file = open(dotDesktop, encoding = "utf-8")
    fileText = file.read()
    file.close()
    iconStart = fileText.find("Icon=")
    if iconStart != -1 :
        iconEnd = fileText.find("\n", iconStart)
        for x in iconsList :
            if fileText[iconStart + 5:iconEnd] == x.split("/")[-1].split(".")[0].split(" %")[0] :
                return x
        return None
    else : return None

def getIconTheme() :
    #Try and get the icon theme from ~/.gtkrc-2.0. If we cannot do this, try and
    #find the gnome-icon-theme instead.
    homeDir = os.path.expanduser('~')
    try :
        file = open(homeDir + "/.gtkrc-2.0", "r")
        fileText = file.read()
        file.close()
        contents = contents.split("\n")
        for x in contents :
            y = x.split("=")
            if y[0].strip() == "gtk-icon-theme-name" : theme = y[-1].strip().strip('"')
        if os.path.exists("/usr/share/icons/" + theme) : return theme
        elif os.path.exists("/usr/share/icons/gnome") : return "gnome"
        else : return None
    except IOError :
        if os.path.exists("/usr/share/icons/gnome") : return "gnome"
        else : return None

#Handle args
args = sys.argv
if (len(args) != 2) or (args[1] == '-h') or (args[1] == '--help') :
    usage()
    os._exit(0)
elif os.path.isfile(args[1]) : menuFile = args[1]
else :
    usage()
    os._exit(0)

#Get locations of icons and .desktops
iconDirs = ["/usr/share/pixmaps", "/usr/share/icons/hicolor/48x48"]
if getIconTheme() != None : iconDirs.append("/usr/share/icons/" + getIconTheme())
iconsList = []
for x in iconDirs :
    for root, dirs, files in os.walk(x, topdown=False):
        for name in files:
            iconsList.append(os.path.join(root, name))
dotDesktops = os.listdir("/usr/share/applications")
for x in dotDesktops : dotDesktops[dotDesktops.index(x)] = "/usr/share/applications/" + x

#Start scanning menu file
file = open(menuFile, "r")
fileText = file.read()
file.close()
newFile = fileText[:]

#Get icons for applications
nameStart = fileText.find('<item label="')
nameEnd = fileText.find('">', nameStart)
while 0 <= nameStart <= len(fileText) :
    dotDesktop = getDotDesktop(fileText[nameStart + 13:nameEnd].split('" icon="')[0], "Name=")
    if dotDesktop != None : icon = getIcon(dotDesktop)
    else :
        #If identification by name fails, try by exec instead
        execStart = fileText.find("<execute>", nameEnd)
        execEnd = fileText.find("</execute>", execStart)
        dotDesktop = getDotDesktop(fileText[execStart + 9:execEnd], "Exec=")
        if dotDesktop != None : icon = getIcon(dotDesktop)
        else : icon = None
    if icon != None :
        newFile = newFile.replace(fileText[nameStart + 13:nameEnd], fileText[nameStart + 13:nameEnd].split('" icon="')[0] + '" icon="' + icon)
    nameStart = fileText.find('<item label="', nameEnd)
    nameEnd = fileText.find('">', nameStart)

#Get icons for categories
catStart = fileText.find('<menu id="')
catEnd = fileText.find('" ', catStart)
labelStart = fileText.find('label="', catEnd)
labelEnd = fileText.find('">', labelStart)
while 0 <= catStart <= len(fileText) :
    if getIconTheme() != None : icon = "/usr/share/icons/" + getIconTheme() + "/48x48/categories/applications-" + fileText[labelStart + 7:labelEnd].lower() + ".png"
    else : icon = None
    if (icon != None) and (os.path.isfile(icon)) :
        newFile = newFile.replace(fileText[catStart:labelStart - 1], fileText[catStart:catEnd + 1] + ' icon="' + icon + '"')
    catStart = fileText.find('<menu id="', labelEnd)
    catEnd = fileText.find('" ', catStart)
    labelStart = fileText.find('label="', catEnd)
    labelEnd = fileText.find('">', labelStart)

#Now add the icons
#This will replace the contents of the old file
file = open(menuFile, "w")
print(newFile.rstrip("\n"), file = file)
file.close()<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
import tw2.core as twc
import tw2.forms as twf
import tw2.jqplugins.select2 as twsel
import tg

class GeocompleteField(twsel.Select2AjaxSingleSelectField):
    attrs = dict(style='width: 100%;')
    options = []
    opts = dict(
        placeholder=u'Rechercher une localisation...',
        minimumInputLength=3,
        maximumInputLength=125,
        allowClear=True,
        ajax=dict(
            url='/geocomplete',
            dataType='json',
            type='POST',
            quietMillis=1000,
            cache=True,
            data=twc.js_callback(
                """
                function (term, page) {
                    return {address: term};
                }
                """
            ),
            results=twc.js_callback(
                """
                function (data, page) {
                    var results = [];

                    $.each(data['results'], function(i, v) {
                        var o = {};
                        o.id = v['to_submit'];
                        o.name = v['to_display'];
                        o.value = v['to_display'];
                        results.push(o);
                    });

                    return {
                        results: results
                    };
                }
                """
            )
        ),
        escapeMarkup=twc.js_callback(
            """
            function(markup) {
                return markup;
            }
            """
        ),
        formatResult=twc.js_callback(
            """
            function(location) {
                var markup = '<option value="' + location.value + '">'
                    + location.name
                    + '</option>';
                return markup;
            }
            """
        ),
        formatSelection=twc.js_callback(
            """
            function(location) {
                return location.value || location.text
            }
            """
        ),
        formatInputTooShort=twc.js_callback(
            """
            function(a, b) {
                var c = b - a.length;
                return 'Veuillez saisir ' + c + ' caract\\xE8re(s) '
                    + 'suppl\\xE9mentaire(s).';
            }
            """
        ),
        formatInputTooLong=twc.js_callback(
            """
            function(a, b) {
                return 'Saisie trop longue.';
            }
            """
        ),
        formatSearching=twc.js_callback(
            """
            function() {
                return 'Recherche en cours...';
            }
            """
        ),
        formatNoMatches=twc.js_callback(
            """
            function() {
                return 'Aucun r\\xE9sultat ne correpond \\xE0 votre recherche.';
            }
            """
        )
    )

class ResearchForm(twf.Form):
    class child(twf.widgets.BaseLayout):
        inline_engine_name = "mako"
        template = \
            u'''

Rechercher une offre d'emploi

            <form class="form-horizontal" role="form">

                    Requête :

                        ${w.children.query.display()|n}

                    Autour de :

                        ${w.children.center.display()|n}

                    Dans un rayon de :

                        ${w.children.radius.display()|n}

                        ${w.submit.display()|n}

                ${w.children.unit.display()|n}
            </form>
            '''

        query = twsel.Select2MultipleSelectField(
            name='query',
            label='',
            options=[],
            value='',
            attrs=dict(style='width: 100%;'),
            placeholder=u"Mots clés recherchés...",
            opts=dict(
                tags=['Python', 'Django', 'Turbogears', 'Pypi'],
                maximumSelectionSize=10,
                tokenSeparators=[','],
                formatSelectionTooBig=twc.js_callback(
                    """
                    function(a) {
                        return 'Nombre maximum de mots cl\\xE9s atteint.';
                    }
                    """
                )
            ),
            ondemand=True
        )

        center = GeocompleteField(name="center", label="")

        distances = [
            "5", "10", "25", "50", "100", "200", "200+"
        ]
        tmp_options = []
        for i, d in enumerate(distances):
            distances_km = "{}{}".format(d, "km")
            if i == len(distances) - 1:
                option = ('infty', distances_km)
            else:
                option = (d, distances_km)

            tmp_options.append(option)

        radius = twsel.Select2SingleSelectField(
            name='radius',
            label='',
            options=tmp_options,
            value='',
            attrs=dict(style='width: 100%'),
            placeholder=u'Distance maximale',
            opts=dict(
                allowClear=True,
                formatNoMatches=twc.js_callback(
                    """
                    function() {
                        return 'Aucun r\\xE9sultat ne correpond \\xE0 votre recherche.';
                    }
                    """
                )
            )
        )

        unit = twf.HiddenField(name="unit", label="", value="km")

        submit = twf.SubmitButton("submit")
        submit.value = "J'effectue ma recherche"
        submit.css_class = "btn btn-default form-control"

    submit = None
<commit_msg>Small keywords default tags changes<commit_after># -*- coding: utf-8 -*-
import tw2.core as twc
import tw2.forms as twf
import tw2.jqplugins.select2 as twsel
import tg

class GeocompleteField(twsel.Select2AjaxSingleSelectField):
    attrs = dict(style='width: 100%;')
    options = []
    opts = dict(
        placeholder=u'Rechercher une localisation...',
        minimumInputLength=3,
        maximumInputLength=125,
        allowClear=True,
        ajax=dict(
            url='/geocomplete',
            dataType='json',
            type='POST',
            quietMillis=1000,
            cache=True,
            data=twc.js_callback(
                """
                function (term, page) {
                    return {address: term};
                }
                """
            ),
            results=twc.js_callback(
                """
                function (data, page) {
                    var results = [];

                    $.each(data['results'], function(i, v) {
                        var o = {};
                        o.id = v['to_submit'];
                        o.name = v['to_display'];
                        o.value = v['to_display'];
                        results.push(o);
                    });

                    return {
                        results: results
                    };
                }
                """
            )
        ),
        escapeMarkup=twc.js_callback(
            """
            function(markup) {
                return markup;
            }
            """
        ),
        formatResult=twc.js_callback(
            """
            function(location) {
                var markup = '<option value="' + location.value + '">'
                    + location.name
                    + '</option>';
                return markup;
            }
            """
        ),
        formatSelection=twc.js_callback(
            """
            function(location) {
                return location.value || location.text
            }
            """
        ),
        formatInputTooShort=twc.js_callback(
            """
            function(a, b) {
                var c = b - a.length;
                return 'Veuillez saisir ' + c + ' caract\\xE8re(s) '
                    + 'suppl\\xE9mentaire(s).';
            }
            """
        ),
        formatInputTooLong=twc.js_callback(
            """
            function(a, b) {
                return 'Saisie trop longue.';
            }
            """
        ),
        formatSearching=twc.js_callback(
            """
            function() {
                return 'Recherche en cours...';
            }
            """
        ),
        formatNoMatches=twc.js_callback(
            """
            function() {
                return 'Aucun r\\xE9sultat ne correpond \\xE0 votre recherche.';
            }
            """
        )
    )

class ResearchForm(twf.Form):
    class child(twf.widgets.BaseLayout):
        inline_engine_name = "mako"
        template = \
            u'''

Rechercher une offre d'emploi

            <form class="form-horizontal" role="form">

                    Requête :

                        ${w.children.query.display()|n}

                    Autour de :

                        ${w.children.center.display()|n}

                    Dans un rayon de :

                        ${w.children.radius.display()|n}

                        ${w.submit.display()|n}

                ${w.children.unit.display()|n}
            </form>
            '''

        query = twsel.Select2MultipleSelectField(
            name='query',
            label='',
            options=[],
            value='',
            attrs=dict(style='width: 100%;'),
            placeholder=u"Mots clés recherchés...",
            opts=dict(
                tags=[
                    'Python', 'Django', 'Flask', 'Pyramid', 'Turbogears'
                ],
                maximumSelectionSize=10,
                tokenSeparators=[','],
                formatSelectionTooBig=twc.js_callback(
                    """
                    function(a) {
                        return 'Nombre maximum de mots cl\\xE9s atteint.';
                    }
                    """
                )
            ),
            ondemand=True
        )

        center = GeocompleteField(name="center", label="")

        distances = [
            "5", "10", "25", "50", "100", "200", "200+"
        ]
        tmp_options = []
        for i, d in enumerate(distances):
            distances_km = "{}{}".format(d, "km")
            if i == len(distances) - 1:
                option = ('infty', distances_km)
            else:
                option = (d, distances_km)

            tmp_options.append(option)

        radius = twsel.Select2SingleSelectField(
            name='radius',
            label='',
            options=tmp_options,
            value='',
            attrs=dict(style='width: 100%'),
            placeholder=u'Distance maximale',
            opts=dict(
                allowClear=True,
                formatNoMatches=twc.js_callback(
                    """
                    function() {
                        return 'Aucun r\\xE9sultat ne correpond \\xE0 votre recherche.';
                    }
                    """
                )
            )
        )

        unit = twf.HiddenField(name="unit", label="", value="km")

        submit = twf.SubmitButton("submit")
        submit.value = "J'effectue ma recherche"
        submit.css_class = "btn btn-default form-control"

    submit = None
<|endoftext|>


<commit_before>
import unittest

from pymatgen.util.testing import PymatgenTest
from pymatgen.analysis.adsorption import *
from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
from pymatgen import Structure, Lattice, Molecule
from pymatgen.util.coord import in_coord_list

test_dir = os.path.join(os.path.dirname(__file__), "..", "..", "..", "..",
                        'test_files')

class AdsorbateSiteFinderTest(PymatgenTest):
    def setUp(self):
        self.structure = Structure.from_spacegroup("Fm-3m", Lattice.cubic(3.5),
                                                   ["Ni"], [[0, 0, 0]])
        lattice = Lattice.cubic(3.010)
        frac_coords = [[0.00000, 0.00000, 0.00000],
                       [0.00000, 0.50000, 0.50000],
                       [0.50000, 0.00000, 0.50000],
                       [0.50000, 0.50000, 0.00000],
                       [0.50000, 0.00000, 0.00000],
                       [0.50000, 0.50000, 0.50000],
                       [0.00000, 0.00000, 0.50000],
                       [0.00000, 0.50000, 0.00000]]
        species = ['Mg', 'Mg', 'Mg', 'Mg', 'O', 'O', 'O', 'O']
        self.MgO = Structure(lattice, species, frac_coords)

        slabs = generate_all_slabs(self.structure, max_index=2,
                                   min_slab_size=6.0, min_vacuum_size=15.0,
                                   max_normal_search=1, center_slab=True)
        self.slab_dict = {''.join([str(i) for i in slab.miller_index]):
                              slab for slab in slabs}
        self.asf_211 = AdsorbateSiteFinder(self.slab_dict["211"])
        self.asf_100 = AdsorbateSiteFinder(self.slab_dict["100"])
        self.asf_111 = AdsorbateSiteFinder(self.slab_dict["111"])
        self.asf_110 = AdsorbateSiteFinder(self.slab_dict["110"])
        self.asf_struct = AdsorbateSiteFinder(
            Structure.from_sites(self.slab_dict["111"].sites))

    def test_init(self):
        asf_100 = AdsorbateSiteFinder(self.slab_dict["100"])
        asf_111 = AdsorbateSiteFinder(self.slab_dict["111"])

    def test_from_bulk_and_miller(self):
        # Standard site finding
        asf = AdsorbateSiteFinder.from_bulk_and_miller(self.structure, (1, 1, 1))
        sites = asf.find_adsorption_sites()
        self.assertEqual(len(sites['hollow']), 2)
        self.assertEqual(len(sites['bridge']), 1)
        self.assertEqual(len(sites['ontop']), 1)
        self.assertEqual(len(sites['all']), 4)
        asf = AdsorbateSiteFinder.from_bulk_and_miller(self.structure, (1, 0, 0))
        sites = asf.find_adsorption_sites()
        self.assertEqual(len(sites['all']), 3)
        self.assertEqual(len(sites['bridge']), 2)
        asf = AdsorbateSiteFinder.from_bulk_and_miller(self.structure, (1, 1, 0),
                                                       undercoord_threshold=0.1)
        self.assertEqual(len(asf.surface_sites), 1)
        # Subsurface site finding
        asf = AdsorbateSiteFinder.from_bulk_and_miller(self.structure, (1, 1, 1))
        sites = asf.find_adsorption_sites(positions=["ontop", "subsurface", "bridge"])
        self.assertEqual(len(sites['all']), 4)
        self.assertEqual(len(sites['subsurface']), 3)

    def test_find_adsorption_sites(self):
        sites = self.asf_100.find_adsorption_sites()
        self.assertEqual(len(sites['all']), 3)
        self.assertEqual(len(sites['hollow']), 0)
        self.assertEqual(len(sites['bridge']), 2)
        self.assertEqual(len(sites['ontop']), 1)
        sites = self.asf_111.find_adsorption_sites()
        self.assertEqual(len(sites['all']), 4)
        sites = self.asf_110.find_adsorption_sites()
        self.assertEqual(len(sites['all']), 4)
        sites = self.asf_211.find_adsorption_sites()
        # Test on structure
        sites = self.asf_struct.find_adsorption_sites()

    def test_generate_adsorption_structures(self):
        co = Molecule("CO", [[0, 0, 0], [0, 0, 1.23]])
        structures = self.asf_111.generate_adsorption_structures(co, repeat=[2, 2, 1])
        self.assertEqual(len(structures), 4)
        sites = self.asf_111.find_adsorption_sites()
        # Check repeat functionality
        self.assertEqual(len([site for site in structures[0] if
                              site.properties['surface_properties'] != 'adsorbate']),
                         4 * len(self.asf_111.slab))
        for n, structure in enumerate(structures):
            self.assertArrayAlmostEqual(structure[-2].coords, sites['all'][n])
        find_args = {"positions": ["hollow"]}
        structures_hollow = self.asf_111. \
            generate_adsorption_structures(co, find_args=find_args)
        self.assertEqual(len(structures_hollow), len(sites['hollow']))
        for n, structure in enumerate(structures_hollow):
            self.assertTrue(in_coord_list(sites['hollow'], structure[-2].coords))

    def test_adsorb_both_surfaces(self):

        # Test out for monatomic adsorption
        o = Molecule("O", [[0, 0, 0]])
        adslabs = self.asf_100.adsorb_both_surfaces(o)
        adslabs_one = self.asf_100.generate_adsorption_structures(o)
        self.assertEqual(len(adslabs), len(adslabs_one))
        for adslab in adslabs:
            sg = SpacegroupAnalyzer(adslab)
            sites = sorted(adslab, key=lambda site: site.frac_coords[2])
            self.assertTrue(sites[0].species_string == "O")
            self.assertTrue(sites[-1].species_string == "O")
            self.assertTrue(sg.is_laue())

        # Test out for molecular adsorption
        oh = Molecule(["O", "H"], [[0, 0, 0], [0, 0, 1]])
        adslabs = self.asf_100.adsorb_both_surfaces(oh)
        adslabs_one = self.asf_100.generate_adsorption_structures(oh)
        self.assertEqual(len(adslabs), len(adslabs_one))
        for adslab in adslabs:
            sg = SpacegroupAnalyzer(adslab)
            sites = sorted(adslab, key=lambda site: site.frac_coords[2])
            self.assertTrue(sites[0].species_string in ["O", "H"])
            self.assertTrue(sites[-1].species_string in ["O", "H"])
            self.assertTrue(sg.is_laue())

    def test_generate_substitution_structures(self):

        # Test this for a low miller index halite structure
        slabs = generate_all_slabs(self.MgO, 1, 10, 10, center_slab=True,
                                   max_normal_search=1)
        for slab in slabs:
            adsgen = AdsorbateSiteFinder(slab)

            adslabs = adsgen.generate_substitution_structures("Ni")
            # There should be 2 configs (sub O and sub
            # Mg) for (110) and (100), 1 for (111)
            if tuple(slab.miller_index) != (1,1,1):
                self.assertEqual(len(adslabs), 2)
            else:
                self.assertEqual(len(adslabs), 1)

            # Test out whether it can correctly dope both
            # sides. Avoid (111) becasue it is not symmetric
            if tuple(slab.miller_index) != (1,1,1):
                adslabs = adsgen.generate_substitution_structures("Ni", sub_both_sides=True,
                                                                  target_species=["Mg"])
                # Test if default parameters dope the surface site
                for i, site in enumerate(adslabs[0]):
                    if adsgen.slab[i].surface_properties == "surface" \
                            and site.species_string == "Mg":
                        print(adslabs[0][i].surface_properties,
                              adsgen.slab[i].surface_properties)
                        self.assertTrue(adslabs[0][i].surface_properties == "substitute")

                self.assertTrue(adslabs[0].is_symmetric())
                # Correctly dope the target species
                self.assertEqual(adslabs[0].composition.as_dict()["Mg"],
                                 slab.composition.as_dict()["Mg"]-2)
                # There should be one config (sub Mg)
                self.assertEqual(len(adslabs), 1)

    def test_functions(self):
        slab = self.slab_dict["111"]
        rot = get_rot(slab)
        reoriented = reorient_z(slab)

if __name__ == '__main__':
    unittest.main()
<commit_msg>add test for adsorbate translation<commit_after>
import unittest

from pymatgen.util.testing import PymatgenTest
from pymatgen.analysis.adsorption import *
from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
from pymatgen import Structure, Lattice, Molecule
from pymatgen.util.coord import in_coord_list

test_dir = os.path.join(os.path.dirname(__file__), "..", "..", "..", "..",
                        'test_files')

class AdsorbateSiteFinderTest(PymatgenTest):
    def setUp(self):
        self.structure = Structure.from_spacegroup("Fm-3m", Lattice.cubic(3.5),
                                                   ["Ni"], [[0, 0, 0]])
        lattice = Lattice.cubic(3.010)
        frac_coords = [[0.00000, 0.00000, 0.00000],
                       [0.00000, 0.50000, 0.50000],
                       [0.50000, 0.00000, 0.50000],
                       [0.50000, 0.50000, 0.00000],
                       [0.50000, 0.00000, 0.00000],
                       [0.50000, 0.50000, 0.50000],
                       [0.00000, 0.00000, 0.50000],
                       [0.00000, 0.50000, 0.00000]]
        species = ['Mg', 'Mg', 'Mg', 'Mg', 'O', 'O', 'O', 'O']
        self.MgO = Structure(lattice, species, frac_coords)

        slabs = generate_all_slabs(self.structure, max_index=2,
                                   min_slab_size=6.0, min_vacuum_size=15.0,
                                   max_normal_search=1, center_slab=True)
        self.slab_dict = {''.join([str(i) for i in slab.miller_index]):
                              slab for slab in slabs}
        self.asf_211 = AdsorbateSiteFinder(self.slab_dict["211"])
        self.asf_100 = AdsorbateSiteFinder(self.slab_dict["100"])
        self.asf_111 = AdsorbateSiteFinder(self.slab_dict["111"])
        self.asf_110 = AdsorbateSiteFinder(self.slab_dict["110"])
        self.asf_struct = AdsorbateSiteFinder(
            Structure.from_sites(self.slab_dict["111"].sites))

    def test_init(self):
        asf_100 = AdsorbateSiteFinder(self.slab_dict["100"])
        asf_111 = AdsorbateSiteFinder(self.slab_dict["111"])

    def test_from_bulk_and_miller(self):
        # Standard site finding
        asf = AdsorbateSiteFinder.from_bulk_and_miller(self.structure, (1, 1, 1))
        sites = asf.find_adsorption_sites()
        self.assertEqual(len(sites['hollow']), 2)
        self.assertEqual(len(sites['bridge']), 1)
        self.assertEqual(len(sites['ontop']), 1)
        self.assertEqual(len(sites['all']), 4)
        asf = AdsorbateSiteFinder.from_bulk_and_miller(self.structure, (1, 0, 0))
        sites = asf.find_adsorption_sites()
        self.assertEqual(len(sites['all']), 3)
        self.assertEqual(len(sites['bridge']), 2)
        asf = AdsorbateSiteFinder.from_bulk_and_miller(self.structure, (1, 1, 0),
                                                       undercoord_threshold=0.1)
        self.assertEqual(len(asf.surface_sites), 1)
        # Subsurface site finding
        asf = AdsorbateSiteFinder.from_bulk_and_miller(self.structure, (1, 1, 1))
        sites = asf.find_adsorption_sites(positions=["ontop", "subsurface", "bridge"])
        self.assertEqual(len(sites['all']), 4)
        self.assertEqual(len(sites['subsurface']), 3)

    def test_find_adsorption_sites(self):
        sites = self.asf_100.find_adsorption_sites()
        self.assertEqual(len(sites['all']), 3)
        self.assertEqual(len(sites['hollow']), 0)
        self.assertEqual(len(sites['bridge']), 2)
        self.assertEqual(len(sites['ontop']), 1)
        sites = self.asf_111.find_adsorption_sites()
        self.assertEqual(len(sites['all']), 4)
        sites = self.asf_110.find_adsorption_sites()
        self.assertEqual(len(sites['all']), 4)
        sites = self.asf_211.find_adsorption_sites()
        # Test on structure
        sites = self.asf_struct.find_adsorption_sites()

    def test_generate_adsorption_structures(self):
        co = Molecule("CO", [[0, 0, 0], [0, 0, 1.23]])
        structures = self.asf_111.generate_adsorption_structures(co, repeat=[2, 2, 1])
        self.assertEqual(len(structures), 4)
        sites = self.asf_111.find_adsorption_sites()
        # Check repeat functionality
        self.assertEqual(len([site for site in structures[0] if
                              site.properties['surface_properties'] != 'adsorbate']),
                         4 * len(self.asf_111.slab))
        for n, structure in enumerate(structures):
            self.assertArrayAlmostEqual(structure[-2].coords, sites['all'][n])
        find_args = {"positions": ["hollow"]}
        structures_hollow = self.asf_111. \
            generate_adsorption_structures(co, find_args=find_args)
        self.assertEqual(len(structures_hollow), len(sites['hollow']))
        for n, structure in enumerate(structures_hollow):
            self.assertTrue(in_coord_list(sites['hollow'], structure[-2].coords))
        # Check molecule not changed after run
        co = Molecule("CO", [[1.0, -0.5, 3], [0.8, 0.46, 3.75]])
        structures = self.asf_211.generate_adsorption_structures(co)
        self.assertEqual(co, Molecule("CO", [[1.0, -0.5, 3], [0.8, 0.46, 3.75]]))
        # Check translation
        sites = self.asf_211.find_adsorption_sites()
        ads_site_coords = sites['all'][0]
        c_site = structures[0].sites[-2]
        self.assertEqual(str(c_site.specie), 'C')
        self.assertArrayAlmostEqual(c_site.coords, sites['all'][0])
        # Check no translation
        structures = self.asf_111.generate_adsorption_structures(co, translate = False)
        self.assertEqual(co, Molecule("CO", [[1.0, -0.5, 3], [0.8, 0.46, 3.75]]))
        sites = self.asf_111.find_adsorption_sites()
        ads_site_coords = sites['all'][0]
        c_site = structures[0].sites[-2]
        self.assertArrayAlmostEqual(c_site.coords, ads_site_coords+np.array([1.0, -0.5, 3]))

    def test_adsorb_both_surfaces(self):

        # Test out for monatomic adsorption
        o = Molecule("O", [[0, 0, 0]])
        adslabs = self.asf_100.adsorb_both_surfaces(o)
        adslabs_one = self.asf_100.generate_adsorption_structures(o)
        self.assertEqual(len(adslabs), len(adslabs_one))
        for adslab in adslabs:
            sg = SpacegroupAnalyzer(adslab)
            sites = sorted(adslab, key=lambda site: site.frac_coords[2])
            self.assertTrue(sites[0].species_string == "O")
            self.assertTrue(sites[-1].species_string == "O")
            self.assertTrue(sg.is_laue())

        # Test out for molecular adsorption
        oh = Molecule(["O", "H"], [[0, 0, 0], [0, 0, 1]])
        adslabs = self.asf_100.adsorb_both_surfaces(oh)
        adslabs_one = self.asf_100.generate_adsorption_structures(oh)
        self.assertEqual(len(adslabs), len(adslabs_one))
        for adslab in adslabs:
            sg = SpacegroupAnalyzer(adslab)
            sites = sorted(adslab, key=lambda site: site.frac_coords[2])
            self.assertTrue(sites[0].species_string in ["O", "H"])
            self.assertTrue(sites[-1].species_string in ["O", "H"])
            self.assertTrue(sg.is_laue())

    def test_generate_substitution_structures(self):

        # Test this for a low miller index halite structure
        slabs = generate_all_slabs(self.MgO, 1, 10, 10, center_slab=True,
                                   max_normal_search=1)
        for slab in slabs:
            adsgen = AdsorbateSiteFinder(slab)

            adslabs = adsgen.generate_substitution_structures("Ni")
            # There should be 2 configs (sub O and sub
            # Mg) for (110) and (100), 1 for (111)
            if tuple(slab.miller_index) != (1,1,1):
                self.assertEqual(len(adslabs), 2)
            else:
                self.assertEqual(len(adslabs), 1)

            # Test out whether it can correctly dope both
            # sides. Avoid (111) becasue it is not symmetric
            if tuple(slab.miller_index) != (1,1,1):
                adslabs = adsgen.generate_substitution_structures("Ni", sub_both_sides=True,
                                                                  target_species=["Mg"])
                # Test if default parameters dope the surface site
                for i, site in enumerate(adslabs[0]):
                    if adsgen.slab[i].surface_properties == "surface" \
                            and site.species_string == "Mg":
                        print(adslabs[0][i].surface_properties,
                              adsgen.slab[i].surface_properties)
                        self.assertTrue(adslabs[0][i].surface_properties == "substitute")

                self.assertTrue(adslabs[0].is_symmetric())
                # Correctly dope the target species
                self.assertEqual(adslabs[0].composition.as_dict()["Mg"],
                                 slab.composition.as_dict()["Mg"]-2)
                # There should be one config (sub Mg)
                self.assertEqual(len(adslabs), 1)

    def test_functions(self):
        slab = self.slab_dict["111"]
        rot = get_rot(slab)
        reoriented = reorient_z(slab)

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