<commit_before>#!/usr/bin/env python
"""
Plot conditions numbers w.r.t. polynomial approximation order of reference
element matrices for various FE polynomial spaces (bases).
"""
from __future__ import absolute_import
import sys
sys.path.append('.')
from argparse import ArgumentParser
import numpy as nm
import matplotlib.pyplot as plt

from sfepy import data_dir
from sfepy.base.base import output, assert_
from sfepy.base.timing import Timer
from sfepy.discrete import FieldVariable, Material, Integral
from sfepy.discrete.fem import Mesh, FEDomain, Field
from sfepy.terms import Term
from sfepy.solvers import eig
from sfepy.mechanics.matcoefs import stiffness_from_lame

helps = {
    'basis' :
    'name of the FE basis [default: %(default)s]',
    'max_order' :
    'maximum order of polynomials [default: %(default)s]',
    'matrix_type' :
    'matrix type [default: %(default)s]',
    'geometry' :
    'reference element geometry, one of "2_3", "2_4", "3_4", "3_8"'
    ' [default: %(default)s]',
}

def main():
    parser = ArgumentParser(description=__doc__)
    parser.add_argument('--version', action='version', version='%(prog)s')
    parser.add_argument('-b', '--basis', metavar='name',
                        action='store', dest='basis',
                        default='lagrange', help=helps['basis'])
    parser.add_argument('-n', '--max-order', metavar='order', type=int,
                        action='store', dest='max_order',
                        default=10, help=helps['max_order'])
    parser.add_argument('-m', '--matrix', action='store', dest='matrix_type',
                        choices=['laplace', 'elasticity', 'smass', 'vmass'],
                        default='laplace', help=helps['matrix_type'])
    parser.add_argument('-g', '--geometry', metavar='name',
                        action='store', dest='geometry',
                        default='2_4', help=helps['geometry'])
    options = parser.parse_args()

    dim, n_ep = int(options.geometry[0]), int(options.geometry[2])
    output('reference element geometry:')
    output('  dimension: %d, vertices: %d' % (dim, n_ep))

    n_c = {'laplace' : 1, 'elasticity' : dim,
           'smass' : 1, 'vmass' : dim}[options.matrix_type]

    output('matrix type:', options.matrix_type)
    output('number of variable components:',  n_c)

    output('polynomial space:', options.basis)

    output('max. order:', options.max_order)

    mesh = Mesh.from_file(data_dir + '/meshes/elements/%s_1.mesh'
                          % options.geometry)
    domain = FEDomain('domain', mesh)
    omega = domain.create_region('Omega', 'all')

    orders = nm.arange(1, options.max_order + 1, dtype=nm.int32)
    conds = []

    order_fix = 0 if  options.geometry in ['2_4', '3_8'] else 1

    for order in orders:
        output('order:', order, '...')

        field = Field.from_args('fu', nm.float64, n_c, omega,
                                approx_order=order,
                                space='H1', poly_space_base=options.basis)

        to = field.approx_order
        quad_order = 2 * (max(to - order_fix, 0))
        output('quadrature order:', quad_order)

        integral = Integral('i', order=quad_order)
        qp, _ = integral.get_qp(options.geometry)
        output('number of quadrature points:', qp.shape[0])

        u = FieldVariable('u', 'unknown', field)
        v = FieldVariable('v', 'test', field, primary_var_name='u')

        m = Material('m', D=stiffness_from_lame(dim, 1.0, 1.0))

        if options.matrix_type == 'laplace':
            term = Term.new('dw_laplace(v, u)',
                            integral, omega, v=v, u=u)
            n_zero = 1

        elif options.matrix_type == 'elasticity':
            term = Term.new('dw_lin_elastic(m.D, v, u)',
                            integral, omega, m=m, v=v, u=u)
            n_zero = (dim + 1) * dim // 2

        elif options.matrix_type in ('smass', 'vmass'):
            term = Term.new('dw_dot(v, u)',
                            integral, omega, v=v, u=u)
            n_zero = 0

        term.setup()

        output('assembling...')
        timer = Timer(start=True)
        mtx, iels = term.evaluate(mode='weak', diff_var='u')
        output('...done in %.2f s' % timer.stop())
        mtx = mtx[0, 0]

        try:
            assert_(nm.max(nm.abs(mtx - mtx.T)) < 1e-10)

        except:
            from sfepy.base.base import debug; debug()

        output('matrix shape:', mtx.shape)

        eigs = eig(mtx, method='eig.sgscipy', eigenvectors=False)
        eigs.sort()

        # Zero 'true' zeros.
        eigs[:n_zero] = 0.0

        ii = nm.where(eigs < 0.0)[0]
        if len(ii):
            output('matrix is not positive semi-definite!')

        ii = nm.where(eigs[n_zero:] < 1e-12)[0]
        if len(ii):
            output('matrix has more than %d zero eigenvalues!' % n_zero)

        output('smallest eigs:\n', eigs[:10])

        ii = nm.where(eigs > 0.0)[0]
        emin, emax = eigs[ii[[0, -1]]]

        output('min:', emin, 'max:', emax)

        cond = emax / emin
        conds.append(cond)

        output('condition number:', cond)

        output('...done')

    plt.figure(1)
    plt.semilogy(orders, conds)
    plt.xticks(orders, orders)
    plt.xlabel('polynomial order')
    plt.ylabel('condition number')
    plt.grid()

    plt.figure(2)
    plt.loglog(orders, conds)
    plt.xticks(orders, orders)
    plt.xlabel('polynomial order')
    plt.ylabel('condition number')
    plt.grid()

    plt.show()

if __name__ == '__main__':
    main()
<commit_msg>script/plot_condition_numbers.py: remove order_fix in main()<commit_after>#!/usr/bin/env python
"""
Plot conditions numbers w.r.t. polynomial approximation order of reference
element matrices for various FE polynomial spaces (bases).
"""
from __future__ import absolute_import
import sys
sys.path.append('.')
from argparse import ArgumentParser
import numpy as nm
import matplotlib.pyplot as plt

from sfepy import data_dir
from sfepy.base.base import output, assert_
from sfepy.base.timing import Timer
from sfepy.discrete import FieldVariable, Material, Integral
from sfepy.discrete.fem import Mesh, FEDomain, Field
from sfepy.terms import Term
from sfepy.solvers import eig
from sfepy.mechanics.matcoefs import stiffness_from_lame

helps = {
    'basis' :
    'name of the FE basis [default: %(default)s]',
    'max_order' :
    'maximum order of polynomials [default: %(default)s]',
    'matrix_type' :
    'matrix type [default: %(default)s]',
    'geometry' :
    'reference element geometry, one of "2_3", "2_4", "3_4", "3_8"'
    ' [default: %(default)s]',
}

def main():
    parser = ArgumentParser(description=__doc__)
    parser.add_argument('--version', action='version', version='%(prog)s')
    parser.add_argument('-b', '--basis', metavar='name',
                        action='store', dest='basis',
                        default='lagrange', help=helps['basis'])
    parser.add_argument('-n', '--max-order', metavar='order', type=int,
                        action='store', dest='max_order',
                        default=10, help=helps['max_order'])
    parser.add_argument('-m', '--matrix', action='store', dest='matrix_type',
                        choices=['laplace', 'elasticity', 'smass', 'vmass'],
                        default='laplace', help=helps['matrix_type'])
    parser.add_argument('-g', '--geometry', metavar='name',
                        action='store', dest='geometry',
                        default='2_4', help=helps['geometry'])
    options = parser.parse_args()

    dim, n_ep = int(options.geometry[0]), int(options.geometry[2])
    output('reference element geometry:')
    output('  dimension: %d, vertices: %d' % (dim, n_ep))

    n_c = {'laplace' : 1, 'elasticity' : dim,
           'smass' : 1, 'vmass' : dim}[options.matrix_type]

    output('matrix type:', options.matrix_type)
    output('number of variable components:',  n_c)

    output('polynomial space:', options.basis)

    output('max. order:', options.max_order)

    mesh = Mesh.from_file(data_dir + '/meshes/elements/%s_1.mesh'
                          % options.geometry)
    domain = FEDomain('domain', mesh)
    omega = domain.create_region('Omega', 'all')

    orders = nm.arange(1, options.max_order + 1, dtype=nm.int32)
    conds = []

    for order in orders:
        output('order:', order, '...')

        field = Field.from_args('fu', nm.float64, n_c, omega,
                                approx_order=order,
                                space='H1', poly_space_base=options.basis)

        quad_order = 2 * field.approx_order
        output('quadrature order:', quad_order)

        integral = Integral('i', order=quad_order)
        qp, _ = integral.get_qp(options.geometry)
        output('number of quadrature points:', qp.shape[0])

        u = FieldVariable('u', 'unknown', field)
        v = FieldVariable('v', 'test', field, primary_var_name='u')

        m = Material('m', D=stiffness_from_lame(dim, 1.0, 1.0))

        if options.matrix_type == 'laplace':
            term = Term.new('dw_laplace(v, u)',
                            integral, omega, v=v, u=u)
            n_zero = 1

        elif options.matrix_type == 'elasticity':
            term = Term.new('dw_lin_elastic(m.D, v, u)',
                            integral, omega, m=m, v=v, u=u)
            n_zero = (dim + 1) * dim // 2

        elif options.matrix_type in ('smass', 'vmass'):
            term = Term.new('dw_dot(v, u)',
                            integral, omega, v=v, u=u)
            n_zero = 0

        term.setup()

        output('assembling...')
        timer = Timer(start=True)
        mtx, iels = term.evaluate(mode='weak', diff_var='u')
        output('...done in %.2f s' % timer.stop())
        mtx = mtx[0, 0]

        try:
            assert_(nm.max(nm.abs(mtx - mtx.T)) < 1e-10)

        except:
            from sfepy.base.base import debug; debug()

        output('matrix shape:', mtx.shape)

        eigs = eig(mtx, method='eig.sgscipy', eigenvectors=False)
        eigs.sort()

        # Zero 'true' zeros.
        eigs[:n_zero] = 0.0

        ii = nm.where(eigs < 0.0)[0]
        if len(ii):
            output('matrix is not positive semi-definite!')

        ii = nm.where(eigs[n_zero:] < 1e-12)[0]
        if len(ii):
            output('matrix has more than %d zero eigenvalues!' % n_zero)

        output('smallest eigs:\n', eigs[:10])

        ii = nm.where(eigs > 0.0)[0]
        emin, emax = eigs[ii[[0, -1]]]

        output('min:', emin, 'max:', emax)

        cond = emax / emin
        conds.append(cond)

        output('condition number:', cond)

        output('...done')

    plt.figure(1)
    plt.semilogy(orders, conds)
    plt.xticks(orders, orders)
    plt.xlabel('polynomial order')
    plt.ylabel('condition number')
    plt.grid()

    plt.figure(2)
    plt.loglog(orders, conds)
    plt.xticks(orders, orders)
    plt.xlabel('polynomial order')
    plt.ylabel('condition number')
    plt.grid()

    plt.show()

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


<commit_before>import os

print(os.getcwd())
from src.Bond import Bond

def test_Bond():
    b = Bond()
    assert 1==1<commit_msg>Update test_Bond.py<commit_after>from src.Bond import Bond

def test_Bond():
    b = Bond()
    assert 1==1
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""
    werkzeug.local
    ~~~~~~~~~~~~~~

    Sooner or later you have some things you want to have in every single view
    or helper function or whatever.  In PHP the way to go are global
    variables.  However that is not possible in WSGI applications without a
    major drawback:  As soon as you operate on the global namespace your
    application is not thread safe any longer.

    The python standard library comes with a utility called "thread locals".
    A thread local is a global object where you can put stuff on and get back
    later in a thread safe way.  That means whenever you set or get an object
    to / from a thread local object the thread local object checks in which
    thread you are and delivers the correct value.

    This however has a few disadvantages.  For example beside threads there
    are other ways to handle concurrency in Python.  A very popular approach
    are greenlets.  Also, whether every request gets its own thread is not
    guaranteed in WSGI.  It could be that a request is reusing a thread from
    before and data is left in the thread local object.

    Nutshell
    --------

    Here a simple example how you can use werkzeug.local::

        from werkzeug import Local, LocalManager

        local = Local()
        local_manager = LocalManager([local])

        def application(environ, start_response):
            local.request = request = Request(environ)
            ...

        application = local_manager.make_middleware(application)

    Now what this code does is binding request to `local.request`.  Every
    other piece of code executed after this assignment in the same context can
    safely access local.request and will get the same request object.  The
    `make_middleware` method on the local manager ensures that everything is
    cleaned up after the request.

    The same context means the same greenlet (if you're using greenlets) in
    the same thread and same process.

    If a request object is not yet set on the local object and you try to
    access it you will get an `AttributeError`.  You can use `getattr` to avoid
    that::

        def get_request():
            return getattr(local, 'request', None)

    This will try to get the request or return `None` if the request is not
    (yet?) available.

    Note that local objects cannot manage themselves, for that you need a local
    manager.  You can pass a local manager multiple locals or add additionals
    later by appending them to `manager.locals` and everytime the manager
    cleans up it will clean up all the data left in the locals for this
    context.

    :copyright: (c) 2008 by the Werkzeug Team, see AUTHORS for more details.
    :license: BSD, see LICENSE for more details.
"""
try:
    from py.magic import greenlet
    get_current_greenlet = greenlet.getcurrent
    del greenlet
except:
    # catch all, py.* fails in so many different errors.
    get_current_greenlet = int
try:
    from thread import get_ident as get_current_thread, allocate_lock
except ImportError:
    from dummy_thread import get_ident as get_current_thread, allocate_lock
from werkzeug.utils import ClosingIterator
from werkzeug._internal import _patch_wrapper

# get the best ident function.  if greenlets are not installed we can
# savely just use the builtin thread function and save a python methodcall
# and the cost of caculating a hash.
if get_current_greenlet is int:
    get_ident = get_current_thread
else:
    get_ident = lambda: (get_current_thread(), get_current_greenlet())

class Local(object):
    __slots__ = ('__storage__', '__lock__')

    def __init__(self):
        object.__setattr__(self, '__storage__', {})
        object.__setattr__(self, '__lock__', allocate_lock())

    def __iter__(self):
        return self.__storage__.iteritems()

    def __call__(self, proxy):
        """Create a proxy for a name."""
        return LocalProxy(self, proxy)

    def __getattr__(self, name):
        self.__lock__.acquire()
        try:
            try:
                return self.__storage__[get_ident()][name]
            except KeyError:
                raise AttributeError(name)
        finally:
            self.__lock__.release()

    def __setattr__(self, name, value):
        self.__lock__.acquire()
        try:
            ident = get_ident()
            storage = self.__storage__
            if ident in storage:
                storage[ident][name] = value
            else:
                storage[ident] = {name: value}
        finally:
            self.__lock__.release()

    def __delattr__(self, name):
        self.__lock__.acquire()
        try:
            try:
                del self.__storage__[get_ident()][name]
            except KeyError:
                raise AttributeError(name)
        finally:
            self.__lock__.release()

class LocalManager(object):
    """Local objects cannot manage themselves. For that you need a local
    manager.  You can pass a local manager multiple locals or add them later
    by appending them to `manager.locals`.  Everytime the manager cleans up
    it, will clean up all the data left in the locals for this context.
    """

    def __init__(self, locals=None):
        if locals is None:
            self.locals = []
        elif isinstance(locals, Local):
            self.locals = [locals]
        else:
            self.locals = list(locals)

    def get_ident(self):
        """Return the context identifier the local objects use internally for
        this context.  You cannot override this method to change the behavior
        but use it to link other context local objects (such as SQLAlchemy's
        scoped sessions) to the Werkzeug locals.
        """
        return get_ident()

    def cleanup(self):
        """Manually clean up the data in the locals for this context.  Call
        this at the end of the request or use `make_middleware()`.
        """
        ident = self.get_ident()
        for local in self.locals:
            local.__storage__.pop(ident, None)

    def make_middleware(self, app):
        """Wrap a WSGI application so that cleaning up happens after
        request end.
        """
        def application(environ, start_response):
            return ClosingIterator(app(environ, start_response), self.cleanup)
        return application

    def middleware(self, func):
        """Like `make_middleware` but for decorating functions.

        Example usage::

            @manager.middleware
            def application(environ, start_response):
                ...

        The difference to `make_middleware` is that the function passed
        will have all the arguments copied from the inner application
        (name, docstring, module).
        """
        return _patch_wrapper(func, self.make_middleware(func))

    def __repr__(self):
        return '<%s storages: %d>' % (
            self.__class__.__name__,
            len(self.locals)
        )

class LocalProxy(object):
    """Acts as a proxy for a werkzeug local.  Forwards all operations to
    a proxied object.  The only operations not supported for forwarding
    are right handed operands and any kind of assignment.

    Example usage::

        from werkzeug import Local
        l = Local()
        request = l('request')
        user = l('user')

    Whenever something is bound to l.user / l.request the proxy objects
    will forward all operations.  If no object is bound a `RuntimeError`
    will be raised.
    """
    __slots__ = ('__local', '__dict__', '__name__')

    def __init__(self, local, name):
        object.__setattr__(self, '_LocalProxy__local', local)
        object.__setattr__(self, '__name__', name)

    def _get_current_object(self):
        """Return the current object.  This is useful if you want the real
        object behind the proxy at a time for performance reasons or because
        you want to pass the object into a different context.
        """
        try:
            return getattr(self.__local, self.__name__)
        except AttributeError:
            raise RuntimeError('no object bound to %s' % self.__name__)
    __current_object = property(_get_current_object)

    def __dict__(self):
        try:
            return self.__current_object.__dict__
        except RuntimeError:
            return AttributeError('__dict__')
    __dict__ = property(__dict__)

    def __repr__(self):
        try:
            obj = self.__current_object
        except RuntimeError:
            return '<%s unbound>' % self.__class__.__name__
        return repr(obj)

    def __nonzero__(self):
        try:
            return bool(self.__current_object)
        except RuntimeError:
            return False

    def __unicode__(self):
        try:
            return unicode(self.__current_oject)
        except RuntimeError:
            return repr(self)

    def __dir__(self):
        try:
            return dir(self.__current_object)
        except RuntimeError:
            return []

    def __getattr__(self, name):
        if name == '__members__':
            return dir(self.__current_object)
        return getattr(self.__current_object, name)

    def __setitem__(self, key, value):
        self.__current_object[key] = value

    def __delitem__(self, key):
        del self.__current_object[key]

    def __setslice__(self, i, j, seq):
        self.__current_object[i:j] = seq

    def __delslice__(self, i, j):
        del self.__current_object[i:j]

    __setattr__ = lambda x, n, v: setattr(x.__current_object, n, v)
    __delattr__ = lambda x, n: delattr(x.__current_object, n)
    __str__ = lambda x: str(x.__current_object)
    __lt__ = lambda x, o: x.__current_object < o
    __le__ = lambda x, o: x.__current_object <= o
    __eq__ = lambda x, o: x.__current_object == o
    __ne__ = lambda x, o: x.__current_object != o
    __gt__ = lambda x, o: x.__current_object > o
    __ge__ = lambda x, o: x.__current_object >= o
    __cmp__ = lambda x, o: cmp(x.__current_object, o)
    __hash__ = lambda x: hash(x.__current_object)
    __call__ = lambda x, *a, **kw: x.__current_object(*a, **kw)
    __len__ = lambda x: len(x.__current_object)
    __getitem__ = lambda x, i: x.__current_object[i]
    __iter__ = lambda x: iter(x.__current_object)
    __contains__ = lambda x, i: i in x.__current_object
    __getslice__ = lambda x, i, j: x.__current_object[i:j]
    __add__ = lambda x, o: x.__current_object + o
    __sub__ = lambda x, o: x.__current_object - o
    __mul__ = lambda x, o: x.__current_object * o
    __floordiv__ = lambda x, o: x.__current_object // o
    __mod__ = lambda x, o: x.__current_object % o
    __divmod__ = lambda x, o: x.__current_object.__divmod__(o)
    __pow__ = lambda x, o: x.__current_object ** o
    __lshift__ = lambda x, o: x.__current_object << o
    __rshift__ = lambda x, o: x.__current_object >> o
    __and__ = lambda x, o: x.__current_object & o
    __xor__ = lambda x, o: x.__current_object ^ o
    __or__ = lambda x, o: x.__current_object | o
    __div__ = lambda x, o: x.__current_object.__div__(o)
    __truediv__ = lambda x, o: x.__current_object.__truediv__(o)
    __neg__ = lambda x: -(x.__current_object)
    __pos__ = lambda x: +(x.__current_object)
    __abs__ = lambda x: abs(x.__current_object)
    __invert__ = lambda x: ~(x.__current_object)
    __complex__ = lambda x: complex(x.__current_object)
    __int__ = lambda x: int(x.__current_object)
    __long__ = lambda x: long(x.__current_object)
    __float__ = lambda x: float(x.__current_object)
    __oct__ = lambda x: oct(x.__current_object)
    __hex__ = lambda x: hex(x.__current_object)
    __index__ = lambda x: x.__current_object.__index__()
    __coerce__ = lambda x, o: x.__coerce__(x, o)
    __enter__ = lambda x: x.__enter__()
    __exit__ = lambda x, *a, **kw: x.__exit__(*a, **kw)
<commit_msg>Corrected a typo in LocalProxy.__unicode__<commit_after># -*- coding: utf-8 -*-
"""
    werkzeug.local
    ~~~~~~~~~~~~~~

    Sooner or later you have some things you want to have in every single view
    or helper function or whatever.  In PHP the way to go are global
    variables.  However that is not possible in WSGI applications without a
    major drawback:  As soon as you operate on the global namespace your
    application is not thread safe any longer.

    The python standard library comes with a utility called "thread locals".
    A thread local is a global object where you can put stuff on and get back
    later in a thread safe way.  That means whenever you set or get an object
    to / from a thread local object the thread local object checks in which
    thread you are and delivers the correct value.

    This however has a few disadvantages.  For example beside threads there
    are other ways to handle concurrency in Python.  A very popular approach
    are greenlets.  Also, whether every request gets its own thread is not
    guaranteed in WSGI.  It could be that a request is reusing a thread from
    before and data is left in the thread local object.

    Nutshell
    --------

    Here a simple example how you can use werkzeug.local::

        from werkzeug import Local, LocalManager

        local = Local()
        local_manager = LocalManager([local])

        def application(environ, start_response):
            local.request = request = Request(environ)
            ...

        application = local_manager.make_middleware(application)

    Now what this code does is binding request to `local.request`.  Every
    other piece of code executed after this assignment in the same context can
    safely access local.request and will get the same request object.  The
    `make_middleware` method on the local manager ensures that everything is
    cleaned up after the request.

    The same context means the same greenlet (if you're using greenlets) in
    the same thread and same process.

    If a request object is not yet set on the local object and you try to
    access it you will get an `AttributeError`.  You can use `getattr` to avoid
    that::

        def get_request():
            return getattr(local, 'request', None)

    This will try to get the request or return `None` if the request is not
    (yet?) available.

    Note that local objects cannot manage themselves, for that you need a local
    manager.  You can pass a local manager multiple locals or add additionals
    later by appending them to `manager.locals` and everytime the manager
    cleans up it will clean up all the data left in the locals for this
    context.

    :copyright: (c) 2008 by the Werkzeug Team, see AUTHORS for more details.
    :license: BSD, see LICENSE for more details.
"""
try:
    from py.magic import greenlet
    get_current_greenlet = greenlet.getcurrent
    del greenlet
except:
    # catch all, py.* fails in so many different errors.
    get_current_greenlet = int
try:
    from thread import get_ident as get_current_thread, allocate_lock
except ImportError:
    from dummy_thread import get_ident as get_current_thread, allocate_lock
from werkzeug.utils import ClosingIterator
from werkzeug._internal import _patch_wrapper

# get the best ident function.  if greenlets are not installed we can
# savely just use the builtin thread function and save a python methodcall
# and the cost of caculating a hash.
if get_current_greenlet is int:
    get_ident = get_current_thread
else:
    get_ident = lambda: (get_current_thread(), get_current_greenlet())

class Local(object):
    __slots__ = ('__storage__', '__lock__')

    def __init__(self):
        object.__setattr__(self, '__storage__', {})
        object.__setattr__(self, '__lock__', allocate_lock())

    def __iter__(self):
        return self.__storage__.iteritems()

    def __call__(self, proxy):
        """Create a proxy for a name."""
        return LocalProxy(self, proxy)

    def __getattr__(self, name):
        self.__lock__.acquire()
        try:
            try:
                return self.__storage__[get_ident()][name]
            except KeyError:
                raise AttributeError(name)
        finally:
            self.__lock__.release()

    def __setattr__(self, name, value):
        self.__lock__.acquire()
        try:
            ident = get_ident()
            storage = self.__storage__
            if ident in storage:
                storage[ident][name] = value
            else:
                storage[ident] = {name: value}
        finally:
            self.__lock__.release()

    def __delattr__(self, name):
        self.__lock__.acquire()
        try:
            try:
                del self.__storage__[get_ident()][name]
            except KeyError:
                raise AttributeError(name)
        finally:
            self.__lock__.release()

class LocalManager(object):
    """Local objects cannot manage themselves. For that you need a local
    manager.  You can pass a local manager multiple locals or add them later
    by appending them to `manager.locals`.  Everytime the manager cleans up
    it, will clean up all the data left in the locals for this context.
    """

    def __init__(self, locals=None):
        if locals is None:
            self.locals = []
        elif isinstance(locals, Local):
            self.locals = [locals]
        else:
            self.locals = list(locals)

    def get_ident(self):
        """Return the context identifier the local objects use internally for
        this context.  You cannot override this method to change the behavior
        but use it to link other context local objects (such as SQLAlchemy's
        scoped sessions) to the Werkzeug locals.
        """
        return get_ident()

    def cleanup(self):
        """Manually clean up the data in the locals for this context.  Call
        this at the end of the request or use `make_middleware()`.
        """
        ident = self.get_ident()
        for local in self.locals:
            local.__storage__.pop(ident, None)

    def make_middleware(self, app):
        """Wrap a WSGI application so that cleaning up happens after
        request end.
        """
        def application(environ, start_response):
            return ClosingIterator(app(environ, start_response), self.cleanup)
        return application

    def middleware(self, func):
        """Like `make_middleware` but for decorating functions.

        Example usage::

            @manager.middleware
            def application(environ, start_response):
                ...

        The difference to `make_middleware` is that the function passed
        will have all the arguments copied from the inner application
        (name, docstring, module).
        """
        return _patch_wrapper(func, self.make_middleware(func))

    def __repr__(self):
        return '<%s storages: %d>' % (
            self.__class__.__name__,
            len(self.locals)
        )

class LocalProxy(object):
    """Acts as a proxy for a werkzeug local.  Forwards all operations to
    a proxied object.  The only operations not supported for forwarding
    are right handed operands and any kind of assignment.

    Example usage::

        from werkzeug import Local
        l = Local()
        request = l('request')
        user = l('user')

    Whenever something is bound to l.user / l.request the proxy objects
    will forward all operations.  If no object is bound a `RuntimeError`
    will be raised.
    """
    __slots__ = ('__local', '__dict__', '__name__')

    def __init__(self, local, name):
        object.__setattr__(self, '_LocalProxy__local', local)
        object.__setattr__(self, '__name__', name)

    def _get_current_object(self):
        """Return the current object.  This is useful if you want the real
        object behind the proxy at a time for performance reasons or because
        you want to pass the object into a different context.
        """
        try:
            return getattr(self.__local, self.__name__)
        except AttributeError:
            raise RuntimeError('no object bound to %s' % self.__name__)
    __current_object = property(_get_current_object)

    def __dict__(self):
        try:
            return self.__current_object.__dict__
        except RuntimeError:
            return AttributeError('__dict__')
    __dict__ = property(__dict__)

    def __repr__(self):
        try:
            obj = self.__current_object
        except RuntimeError:
            return '<%s unbound>' % self.__class__.__name__
        return repr(obj)

    def __nonzero__(self):
        try:
            return bool(self.__current_object)
        except RuntimeError:
            return False

    def __unicode__(self):
        try:
            return unicode(self.__current_object)
        except RuntimeError:
            return repr(self)

    def __dir__(self):
        try:
            return dir(self.__current_object)
        except RuntimeError:
            return []

    def __getattr__(self, name):
        if name == '__members__':
            return dir(self.__current_object)
        return getattr(self.__current_object, name)

    def __setitem__(self, key, value):
        self.__current_object[key] = value

    def __delitem__(self, key):
        del self.__current_object[key]

    def __setslice__(self, i, j, seq):
        self.__current_object[i:j] = seq

    def __delslice__(self, i, j):
        del self.__current_object[i:j]

    __setattr__ = lambda x, n, v: setattr(x.__current_object, n, v)
    __delattr__ = lambda x, n: delattr(x.__current_object, n)
    __str__ = lambda x: str(x.__current_object)
    __lt__ = lambda x, o: x.__current_object < o
    __le__ = lambda x, o: x.__current_object <= o
    __eq__ = lambda x, o: x.__current_object == o
    __ne__ = lambda x, o: x.__current_object != o
    __gt__ = lambda x, o: x.__current_object > o
    __ge__ = lambda x, o: x.__current_object >= o
    __cmp__ = lambda x, o: cmp(x.__current_object, o)
    __hash__ = lambda x: hash(x.__current_object)
    __call__ = lambda x, *a, **kw: x.__current_object(*a, **kw)
    __len__ = lambda x: len(x.__current_object)
    __getitem__ = lambda x, i: x.__current_object[i]
    __iter__ = lambda x: iter(x.__current_object)
    __contains__ = lambda x, i: i in x.__current_object
    __getslice__ = lambda x, i, j: x.__current_object[i:j]
    __add__ = lambda x, o: x.__current_object + o
    __sub__ = lambda x, o: x.__current_object - o
    __mul__ = lambda x, o: x.__current_object * o
    __floordiv__ = lambda x, o: x.__current_object // o
    __mod__ = lambda x, o: x.__current_object % o
    __divmod__ = lambda x, o: x.__current_object.__divmod__(o)
    __pow__ = lambda x, o: x.__current_object ** o
    __lshift__ = lambda x, o: x.__current_object << o
    __rshift__ = lambda x, o: x.__current_object >> o
    __and__ = lambda x, o: x.__current_object & o
    __xor__ = lambda x, o: x.__current_object ^ o
    __or__ = lambda x, o: x.__current_object | o
    __div__ = lambda x, o: x.__current_object.__div__(o)
    __truediv__ = lambda x, o: x.__current_object.__truediv__(o)
    __neg__ = lambda x: -(x.__current_object)
    __pos__ = lambda x: +(x.__current_object)
    __abs__ = lambda x: abs(x.__current_object)
    __invert__ = lambda x: ~(x.__current_object)
    __complex__ = lambda x: complex(x.__current_object)
    __int__ = lambda x: int(x.__current_object)
    __long__ = lambda x: long(x.__current_object)
    __float__ = lambda x: float(x.__current_object)
    __oct__ = lambda x: oct(x.__current_object)
    __hex__ = lambda x: hex(x.__current_object)
    __index__ = lambda x: x.__current_object.__index__()
    __coerce__ = lambda x, o: x.__coerce__(x, o)
    __enter__ = lambda x: x.__enter__()
    __exit__ = lambda x, *a, **kw: x.__exit__(*a, **kw)
<|endoftext|>


<commit_before># Copyright (c) 2015, Michael Boyle
# See LICENSE file for details: <https://github.com/moble/scri/blob/master/LICENSE>

import pytest
import os.path
import numpy as np
import h5py
import scri
import scri.SpEC

# NOTE: if test_file_io() comes after test_NRAR_extrapolation() in this file, then the
# output of the latter can be used in test_file_io(). Otherwise, test_file_io() will
# just generate the data it needs indepenedently.

@pytest.fixture(scope="session")  # The following will exist for an entire run of pytest
def tempdir(tmp_path_factory):
    """Test fixture to create a temp directory and write the fake finite-radius file"""
    tmpdir = tmp_path_factory.mktemp("test_extrapolation")
    filename = tmpdir / "rh_FiniteRadii_CodeUnits.h5"
    scri.sample_waveforms.create_fake_finite_radius_strain_h5file(
        output_file_path=filename, ell_max=3, t_1=2000.0,
    )
    return tmpdir

def test_extrapolation(tempdir):
    """Test extrapolation of waveforms with output in the scri format"""
    input_dir = str(tempdir)
    output_dir = str(tempdir / "test_extrapolation")
    scri.extrapolation.extrapolate(InputDirectory=input_dir, OutputDirectory=output_dir, ChMass=1.0)

def test_NRAR_extrapolation(tempdir):
    """Test extrapolation of waveforms with output in the NRAR format."""
    input_dir = str(tempdir)
    output_dir = str(tempdir / "test_NRAR_extrapolation")
    scri.extrapolation.extrapolate(
        InputDirectory=input_dir,
        OutputDirectory=output_dir,
        ChMass=1.0,
        UseStupidNRARFormat=True,
        DifferenceFiles="",  # Computing DifferenceFiles and making plots are tested in test_extrapolation() so
        PlotFormat="",
    )  # to save time we don't do it here.

def test_file_io(tempdir):
    """Test file I/O with a round-trip and compare H5 files"""
    output_dir = tempdir / "test_file_io"
    input_file = tempdir / "test_NRAR_extrapolation" / "rhOverM_Extrapolated_N2.h5"
    output_file = str(output_dir / "Asymptotic_GeometricUnits.h5")
    output_file_result = str(output_dir / "rhOverM_Asymptotic_GeometricUnits.h5")

    if not input_file.exists():
        scri.extrapolation.extrapolate(
            InputDirectory=str(tempdir),
            OutputDirectory=str(tempdir / "test_NRAR_extrapolation"),
            ChMass=1.0,
            UseStupidNRARFormat=True,
            DifferenceFiles="",
            PlotFormat="",
        )
    output_dir.mkdir()

    input_file = str(input_file)
    w = scri.SpEC.read_from_h5(input_file)
    scri.SpEC.write_to_h5(w, output_file)

    with h5py.File(input_file, "r") as f1, h5py.File(output_file_result, "r") as f2:
        # Check top-level attributes
        list1 = sorted(f1.attrs.items())
        list2 = sorted(f2.attrs.items())
        # attribute 3 is waveform output version
        list1.remove(list1[3])
        list2.remove(list2[3])
        assert list1 == list2

        # Check that original history is contained (with extra comment characters) in second history
        assert ("# " + "\n# ".join(f1["History.txt"][()].split("\n"))) in f2["History.txt"][()]

        # Now check each mode from the original
        for mode in list(f1):
            if mode.startswith("Y"):
                assert list(f1[mode].attrs.items()) == list(f2[mode].attrs.items())
                assert np.array_equal(f1[mode][:], f2[mode][:])
<commit_msg>Decode bytes into string before assert, if needed (#49)<commit_after># Copyright (c) 2015, Michael Boyle
# See LICENSE file for details: <https://github.com/moble/scri/blob/master/LICENSE>

import pytest
import os.path
import numpy as np
import h5py
import scri
import scri.SpEC

# NOTE: if test_file_io() comes after test_NRAR_extrapolation() in this file, then the
# output of the latter can be used in test_file_io(). Otherwise, test_file_io() will
# just generate the data it needs indepenedently.

@pytest.fixture(scope="session")  # The following will exist for an entire run of pytest
def tempdir(tmp_path_factory):
    """Test fixture to create a temp directory and write the fake finite-radius file"""
    tmpdir = tmp_path_factory.mktemp("test_extrapolation")
    filename = tmpdir / "rh_FiniteRadii_CodeUnits.h5"
    scri.sample_waveforms.create_fake_finite_radius_strain_h5file(
        output_file_path=filename, ell_max=3, t_1=2000.0,
    )
    return tmpdir

def test_extrapolation(tempdir):
    """Test extrapolation of waveforms with output in the scri format"""
    input_dir = str(tempdir)
    output_dir = str(tempdir / "test_extrapolation")
    scri.extrapolation.extrapolate(InputDirectory=input_dir, OutputDirectory=output_dir, ChMass=1.0)

def test_NRAR_extrapolation(tempdir):
    """Test extrapolation of waveforms with output in the NRAR format."""
    input_dir = str(tempdir)
    output_dir = str(tempdir / "test_NRAR_extrapolation")
    scri.extrapolation.extrapolate(
        InputDirectory=input_dir,
        OutputDirectory=output_dir,
        ChMass=1.0,
        UseStupidNRARFormat=True,
        DifferenceFiles="",  # Computing DifferenceFiles and making plots are tested in test_extrapolation() so
        PlotFormat="",
    )  # to save time we don't do it here.

def test_file_io(tempdir):
    """Test file I/O with a round-trip and compare H5 files"""
    output_dir = tempdir / "test_file_io"
    input_file = tempdir / "test_NRAR_extrapolation" / "rhOverM_Extrapolated_N2.h5"
    output_file = str(output_dir / "Asymptotic_GeometricUnits.h5")
    output_file_result = str(output_dir / "rhOverM_Asymptotic_GeometricUnits.h5")

    if not input_file.exists():
        scri.extrapolation.extrapolate(
            InputDirectory=str(tempdir),
            OutputDirectory=str(tempdir / "test_NRAR_extrapolation"),
            ChMass=1.0,
            UseStupidNRARFormat=True,
            DifferenceFiles="",
            PlotFormat="",
        )
    output_dir.mkdir()

    input_file = str(input_file)
    w = scri.SpEC.read_from_h5(input_file)
    scri.SpEC.write_to_h5(w, output_file)

    with h5py.File(input_file, "r") as f1, h5py.File(output_file_result, "r") as f2:
        # Check top-level attributes
        list1 = sorted(f1.attrs.items())
        list2 = sorted(f2.attrs.items())
        # attribute 3 is waveform output version
        list1.remove(list1[3])
        list2.remove(list2[3])
        assert list1 == list2

        # Check that original history is contained (with extra comment characters) in second history
        if isinstance(f2["History.txt"][()], str):
            assert ("# " + "\n# ".join(f1["History.txt"][()].split("\n"))) in f2["History.txt"][()]
        else:
            assert ("# " + "\n# ".join(f1["History.txt"][()].decode("utf-8").split("\n"))) in f2["History.txt"][()].decode("utf-8")

        # Now check each mode from the original
        for mode in list(f1):
            if mode.startswith("Y"):
                assert list(f1[mode].attrs.items()) == list(f2[mode].attrs.items())
                assert np.array_equal(f1[mode][:], f2[mode][:])
<|endoftext|>


<commit_before>#!/usr/bin/python
import sys, subprocess, os, time
from vcoptparse import *

control_user = "rethinkdb@deadshot"

class VM(object):
    def __init__(self, name, uuid, host):
        self.name = name
        self.uuid = uuid
        self.host = host
    def start(self):
        subprocess.Popen(["ssh %s 'VBoxManage startvm --type headless %s'" % (control_user, self.uuid)], shell = True).wait()
        start_time = time.time()
        while os.system("ssh %s 'true'" % self.host) and time.time() - start_time < 5 * 60: # timeout after 5 minutes
            pass
        if not os.system("ssh %s 'true'" % self.host):
            print "(VM successfully started)"
        else:
            sys_exit("Error: Failed to connect to VM", -1)
    def command(self, cmd, output = False):
        print "Executing on VM:", cmd
        proc = subprocess.Popen(["ssh %s '%s'" % (self.host, cmd)], stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell = True)
        while proc.poll() == None: # TODO: add timeout?
            if output:
                line = proc.stdout.readline()
                if line:
                    print line.strip()
            else:
                pass
        if proc.poll():
            sys_exit("Error: command \"%s\" finished with exit value %d." % (cmd, proc.poll()), proc.poll(), True)
        return proc
    def shut_down(self):
        subprocess.Popen(["ssh %s 'VBoxManage controlvm %s poweroff'" % (control_user, self.uuid)], shell = True).wait()

def sys_exit(message, exit_code, shut_down = False):
    print message
    if shut_down:
        target.shut_down()
    sys.exit(exit_code)

suse = VM('suse', '765127b8-2007-43ff-8668-fe4c60176a2b', 'rethinkdb@192.168.0.173')
redhat5_1 = VM('redhat5_1', '32340f79-cea9-42ca-94d5-2da13d408d02', 'rethinkdb@192.168.0.159')
ubuntu = VM('ubuntu', '1f4521a0-6e74-4d20-b4b9-9ffd8e231423', 'rethinkdb@192.168.0.172')
debian = VM('debian', 'cc76e2a5-92c0-4208-be08-5c02429c2c50', 'root@192.168.0.176')
centos5_5 = VM('centos5_5', '25710682-666f-4449-bd28-68b25abd8bea', 'root@192.168.0.153')
centos6 = VM('centos6', 'd9058650-a45a-44a5-953f-c2402253a614', 'rethinkdb@192.168.0.178')

vm_list = {"suse" : suse, "redhat5_1" : redhat5_1, "ubuntu" : ubuntu, "debian" : debian, "centos5_5" : centos5_5, "centos6" : centos6}

def help():
    print "VM Access:"
    print "       Runs a command on a remote virtual machine. Starts the virtual machine if necessary, and shuts it down on completion. If the command fails or if the virtual machine is inaccessible, then this script will throw an exception. All virtual machines are controlled through " + control_user + ". Before commands are run, the curent directory is compressed and sent over. The command is run in a temporary directory and all its resulting contents are copied back."
    print "       --help      Print this help."
    print "       --vm-name   The target virtual machine to run the command on. Options are:"
    print "                   ", vm_list.keys()
    print "       --command   The command to run on the virtual machine. Must be specified after the vm-name."

o = OptParser()
o["help"] = BoolFlag("--help")
o["vm-name"] = StringFlag("--vm-name", None)
o["command"] = AllArgsAfterFlag("--command", default = None)

try:
    opts = o.parse(sys.argv)
except OptError:
    sys_exit("Argument parsing error", -1)

if opts["help"]:
    help()
    sys_exit("", 0)

if not opts["vm-name"]:
    sys_exit("Error: must specify a VM name.", -1)

if not opts["command"]:
    sys_exit("Error: must specify a command.", -1)

if opts["vm-name"] not in vm_list:
    sys_exit("Error: invalid VM name.", -1)

target = vm_list[opts["vm-name"]]

print "Begin: Running command:", " ".join(opts["command"])
print "\ton VM", target.name

# Start VM
print "***Starting VM..."
target.start()

# Make a temporary directory on VM
print "***Making a temporary directory on the virtual machine..."
proc = target.command("cd /tmp && mktemp -d test.XXXXXXXXXX")
dir_name = "/tmp/" + proc.stdout.readline().strip()
print "***Will be working in directory " + dir_name

# Move files to VM
print "***Transferring files to virtual machine..."
subprocess.Popen(" ".join(["tar", "czf", "tmp.tar.gz", "*", "&&", "scp", "tmp.tar.gz", "%s:%s/tmp.tar.gz" % (target.host, dir_name)]), shell = True).wait()
target.command(" ".join(["cd", dir_name, "&&", "tar", "xzf", "tmp.tar.gz"]))

# Execute command
print "***Executing command..."
proc = target.command(("cd %s && " % dir_name) + " ".join(opts["command"]), output = True)

# Move files from VM
print "***Transferring files from virtual machine..."
proc = target.command(" ".join(["cd", dir_name, "&&", "rm", "tmp.tar.gz", "&&", "tar", "czf", "tmp.tar.gz", "*"]))
subprocess.Popen(" ".join(["scp", "%s:%s/tmp.tar.gz tmp.tar.gz" % (target.host, dir_name)]), shell = True).wait()
subprocess.Popen(" ".join(["tar", "xzf", "tmp.tar.gz"]), shell = True).wait()

# Shut down VM
print "***Shutting down VM..."
vm_list[opts["vm-name"]].shut_down()

sys_exit("Done.", 0)
<commit_msg>Modified vm_access to make transferring files work for the tests, because in the tests the initial directory is different (starts in output_from_test).<commit_after>#!/usr/bin/python
import sys, subprocess, os, time
from vcoptparse import *

control_user = "rethinkdb@deadshot"

class VM(object):
    def __init__(self, name, uuid, host):
        self.name = name
        self.uuid = uuid
        self.host = host
    def start(self):
        subprocess.Popen(["ssh %s 'VBoxManage startvm --type headless %s'" % (control_user, self.uuid)], shell = True).wait()
        start_time = time.time()
        while os.system("ssh %s 'true'" % self.host) and time.time() - start_time < 5 * 60: # timeout after 5 minutes
            pass
        if not os.system("ssh %s 'true'" % self.host):
            print "(VM successfully started)"
        else:
            sys_exit("Error: Failed to connect to VM", -1)
    def command(self, cmd, output = False):
        print "Executing on VM:", cmd
        proc = subprocess.Popen(["ssh %s '%s'" % (self.host, cmd)], stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell = True)
        while proc.poll() == None: # TODO: add timeout?
            if output:
                line = proc.stdout.readline()
                if line:
                    print line.strip()
            else:
                pass
        if proc.poll():
            sys_exit("Error: command \"%s\" finished with exit value %d." % (cmd, proc.poll()), proc.poll(), True)
        return proc
    def shut_down(self):
        subprocess.Popen(["ssh %s 'VBoxManage controlvm %s poweroff'" % (control_user, self.uuid)], shell = True).wait()

def sys_exit(message, exit_code, shut_down = False):
    print message
    if shut_down:
        target.shut_down()
    sys.exit(exit_code)

suse = VM('suse', '765127b8-2007-43ff-8668-fe4c60176a2b', 'rethinkdb@192.168.0.173')
redhat5_1 = VM('redhat5_1', '32340f79-cea9-42ca-94d5-2da13d408d02', 'rethinkdb@192.168.0.159')
ubuntu = VM('ubuntu', '1f4521a0-6e74-4d20-b4b9-9ffd8e231423', 'rethinkdb@192.168.0.172')
debian = VM('debian', 'cc76e2a5-92c0-4208-be08-5c02429c2c50', 'root@192.168.0.176')
centos5_5 = VM('centos5_5', '25710682-666f-4449-bd28-68b25abd8bea', 'root@192.168.0.153')
centos6 = VM('centos6', 'd9058650-a45a-44a5-953f-c2402253a614', 'rethinkdb@192.168.0.178')

vm_list = {"suse" : suse, "redhat5_1" : redhat5_1, "ubuntu" : ubuntu, "debian" : debian, "centos5_5" : centos5_5, "centos6" : centos6}

def help():
    print "VM Access:"
    print "       Runs a command on a remote virtual machine. Starts the virtual machine if necessary, and shuts it down on completion. If the command fails or if the virtual machine is inaccessible, then this script will throw an exception. All virtual machines are controlled through " + control_user + ". Before commands are run, the curent directory is compressed and sent over. The command is run in a temporary directory and all its resulting contents are copied back."
    print "       --help      Print this help."
    print "       --vm-name   The target virtual machine to run the command on. Options are:"
    print "                   ", vm_list.keys()
    print "       --command   The command to run on the virtual machine. Must be specified after the vm-name."

o = OptParser()
o["help"] = BoolFlag("--help")
o["vm-name"] = StringFlag("--vm-name", None)
o["command"] = AllArgsAfterFlag("--command", default = None)

try:
    opts = o.parse(sys.argv)
except OptError:
    sys_exit("Argument parsing error", -1)

if opts["help"]:
    help()
    sys_exit("", 0)

if not opts["vm-name"]:
    sys_exit("Error: must specify a VM name.", -1)

if not opts["command"]:
    sys_exit("Error: must specify a command.", -1)

if opts["vm-name"] not in vm_list:
    sys_exit("Error: invalid VM name.", -1)

target = vm_list[opts["vm-name"]]

print "Begin: Running command:", " ".join(opts["command"])
print "\ton VM", target.name

# Start VM
print "***Starting VM..."
target.start()

# Make a temporary directory on VM
print "***Making a temporary directory on the virtual machine..."
proc = target.command("cd /tmp && mktemp -d test.XXXXXXXXXX")
dir_name = "/tmp/" + proc.stdout.readline().strip()
print "***Will be working in directory " + dir_name

# Move files to VM
print "***Transferring files to virtual machine..."
subprocess.Popen(" ".join((["cd", os.environ["RETHINKDB"] + "/.."] if "RETHINKDB" in os.environ else []) + ["tar", "czf", "tmp.tar.gz", "*", "&&", "scp", "tmp.tar.gz", "%s:%s/tmp.tar.gz" % (target.host, dir_name)]), shell = True).wait()
target.command(" ".join(["cd", dir_name, "&&", "tar", "xzf", "tmp.tar.gz"]))

# Execute command
print "***Executing command..."
proc = target.command(("cd %s && " % dir_name) + " ".join(opts["command"]), output = True)

# Move files from VM
print "***Transferring files from virtual machine..."
proc = target.command(" ".join(["cd", dir_name, "&&", "rm", "tmp.tar.gz", "&&", "tar", "czf", "tmp.tar.gz", "*"]))
subprocess.Popen(" ".join(["scp", "%s:%s/tmp.tar.gz tmp.tar.gz" % (target.host, dir_name)]), shell = True).wait()
subprocess.Popen(" ".join(["tar", "xzf", "tmp.tar.gz"]), shell = True).wait()

# Shut down VM
print "***Shutting down VM..."
vm_list[opts["vm-name"]].shut_down()

sys_exit("Done.", 0)
<|endoftext|>


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

from EPPs.common import StepEPP

# Script for performing autoplacement of samples. If 1 input and 1 output then 1:1 placement. If multiple input plates then
# takes all samples from each plate before next plate Loops through all inputs and assigns them to the next available space
# by column-row in the output plate
class Autoplacement96well(StepEPP):
    _use_load_config = False  # prevent the loading of the config
    _max_nb_input_containers = 27

    def _run(self):

        # loop through the inputs, assemble a nested dicitonary {containers:{input.location:output} this can then be
        # queried in the order container-row-column so the order of the inputs in the Hamilton input file is
        # as efficient as possible.
        input_container_nested_dict = {}

        for art in self.artifacts:
            # obtain outputs for the input that are analytes, assume step is not configured to allow replicates
            # so will always work with output[0]
            output = self.process.outputs_per_input(art.id, Analyte=True)[0]

            # add the input_location_output_dict to the input_container_nested dict
            if art.container not in input_container_nested_dict.keys():
                input_container_nested_dict[art.container] = {}
            # start assembling one of the variables needed to use the set_placement function
            # note that .location returns a tuple with the container entity and the well location as the string in position [1]
            input_container_nested_dict[art.container][art.location[1]] = output

        # update of container requires list variable containing the containers, only one container will be present in step
        # because the container has not yet been fully populated then it must be obtained from the step rather than output
        output_container_list = self.process.step.placements.get_selected_containers()

        # need a list of tuples for set_placements
        output_placement = []

        # assemble the plate layout of the output plate as a list
        output_plate_layout_columns = ["1", "2", "3","4","5","6","7","8","9","10","11","12"]
        output_plate_layout_rows = ["A", "B", "C","D","E","F","G","H"]
        output_plate_layout = []

        for column in output_plate_layout_columns:
            for row in output_plate_layout_rows:
                output_plate_layout.append(row + ":" + column)

        # define the input plate(s) column and rows to be
        input_plate_layout_columns = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "11", "12"]
        input_plate_layout_rows = ["A", "B", "C", "D", "E", "F", "G", "H"]

        # create a counter so only use each available well in the output plate once (24 wells available in the 96 well plate
        well_counter = 0

        # loop through the input containers and place the samples in row-column order - this makes pipetting as efficient
        # as possible, particularly if only 1 input plate so 1:1 pipetting
        for container in sorted(input_container_nested_dict, key=lambda x: x.name):
            for column in input_plate_layout_columns:
                for row in input_plate_layout_rows:
                    # populate list of tuples for set_placements if well exists in input plate
                    if row + ":" + column in input_container_nested_dict[container].keys():

                        output_placement.append((input_container_nested_dict[container][row + ":" + column],
                                                 (output_container_list[0], output_plate_layout[well_counter])))
                        well_counter += 1

        # push the output locations to the LIMS
        self.process.step.set_placements(output_container_list, output_placement)

if __name__ == '__main__':
    Autoplacement96well().run()
<commit_msg>Refactored<commit_after>#!/usr/bin/env python
import itertools

from EPPs.common import StepEPP

# Script for performing autoplacement of samples from multiple input plates into a single output plate.
# If 1 input and 1 output then 1:1 placement. If multiple input plates then
# takes all samples from each plate before next plate Loops through all inputs and assigns them to the next available space
# by column-row in the output plate
class Autoplacement96well(StepEPP):
    _use_load_config = False  # prevent the loading of the config
    _max_nb_input_containers = 27
    # assemble the plate layout of the output plate as a list
    output_plate_layout_rows = ["A", "B", "C", "D", "E", "F", "G", "H"]
    output_plate_layout_columns = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"]

    output_plate_layout = [(row + ":" + column) for column, row in
                           itertools.product(output_plate_layout_columns, output_plate_layout_rows)]

    # define the input plate(s) column and rows to be
    input_plate_layout_columns = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "11", "12"]
    input_plate_layout_rows = ["A", "B", "C", "D", "E", "F", "G", "H"]

    def generate_input_container_nested_dict(self):
        # loop through the inputs, assemble a nested dicitonary {containers:{input.location:output} this can then be
        # queried in the order container-row-column so the order of the inputs in the Hamilton input file is
        # as efficient as possible.
        nested_dict = {}
        for art in self.artifacts:

            # obtain outputs for the input that are analytes, assume step is not configured to allow replicates
            # so will always work with output[0]
            output = self.process.outputs_per_input(art.id, Analyte=True)[0]

            # add the input_location_output_dict to the input_container_nested dict
            if art.container not in nested_dict.keys():
                nested_dict[art.container] = {}
            # start assembling one of the variables needed to use the set_placement function
            # note that .location returns a tuple with the container entity and the well location as the string in position [1]
            nested_dict[art.container][art.location[1]] = output
        return nested_dict

    def generate_output_placement(self, output_container):
        placement = []
        # obtain the dictionary containing the source information
        input_container_nested_dict = self.generate_input_container_nested_dict()

        # create a counter so only use each available well in the output plate once (24 wells available in the 96 well plate
        well_counter = 0

        # loop through the input containers and place the samples in row-column order - this makes pipetting as efficient
        # as possible, particularly if only 1 input plate so 1:1 pipetting
        for container in sorted(input_container_nested_dict, key=lambda x: x.name):
            for column in self.input_plate_layout_columns:
                for row in self.input_plate_layout_rows:
                    # populate list of tuples for set_placements if well exists in input plate
                    if row + ":" + column in input_container_nested_dict[container].keys():
                        placement.append((input_container_nested_dict[container][row + ":" + column],
                                          (output_container, self.output_plate_layout[well_counter])))
                        well_counter += 1
        return placement

    def _run(self):

        # update of container requires list variable containing the containers, only one container will be present in step
        # because the container has not yet been fully populated then it must be obtained from the step rather than output
        output_container_list = self.process.step.placements.get_selected_containers()

        # need a list of tuples for set_placements
        output_placement = self.generate_output_placement(output_container_list[0])

        # push the output locations to the LIMS
        self.process.step.set_placements(output_container_list, output_placement)

if __name__ == '__main__':
    Autoplacement96well().run()
<|endoftext|>


<commit_before>from dataclasses import dataclass
from typing import TYPE_CHECKING, List, cast

from rich.style import StyleType
from rich.table import Column as RichColumn
from rich.table import Table as RichTable

if TYPE_CHECKING:
    from rich.console import (
        Console,
        ConsoleOptions,
        JustifyMethod,
        OverflowMethod,
        RenderableType,
    )

@dataclass
class Column(RichColumn):
    collapse: bool = False

class Table(RichTable):
    def add_column(  # pylint: disable=arguments-differ
        self,
        header: "RenderableType" = "",
        footer: "RenderableType" = "",
        *,
        header_style: StyleType = None,
        footer_style: StyleType = None,
        style: StyleType = None,
        justify: "JustifyMethod" = "left",
        overflow: "OverflowMethod" = "ellipsis",
        width: int = None,
        min_width: int = None,
        max_width: int = None,
        ratio: int = None,
        no_wrap: bool = False,
        collapse: bool = False,
    ) -> None:
        column = Column(  # type: ignore[call-arg]
            _index=len(self.columns),
            header=header,
            footer=footer,
            header_style=header_style or "",
            footer_style=footer_style or "",
            style=style or "",
            justify=justify,
            overflow=overflow,
            width=width,
            min_width=min_width,
            max_width=max_width,
            ratio=ratio,
            no_wrap=no_wrap,
            collapse=collapse,
        )
        self.columns.append(column)

    def _calculate_column_widths(
        self, console: "Console", options: "ConsoleOptions"
    ) -> List[int]:
        """Calculate the widths of each column, including padding, not
        including borders.

        Adjacent collapsed columns will be removed until there is only a single
        truncated column remaining.
        """
        widths = super()._calculate_column_widths(console, options)
        last_collapsed = -1
        columns = cast(List[Column], self.columns)
        for i in range(len(columns) - 1, -1, -1):
            if widths[i] == 1 and columns[i].collapse:
                if last_collapsed >= 0:
                    del widths[last_collapsed]
                    del columns[last_collapsed]
                    if self.box:
                        options.max_width += 1
                    for column in columns[last_collapsed:]:
                        column._index -= 1
                last_collapsed = i
                padding = self._get_padding_width(i)
                if (
                    columns[i].overflow == "ellipsis"
                    and (sum(widths) + padding) <= options.max_width
                ):
                    # Set content width to 1 (plus padding) if we can fit a
                    # single unicode ellipsis in this column
                    widths[i] = 1 + padding
            else:
                last_collapsed = -1
        return widths

    def _collapse_widths(  # type: ignore[override]
        self, widths: List[int], wrapable: List[bool], max_width: int,
    ) -> List[int]:
        """Collapse columns right-to-left if possible to fit table into
        max_width.

        If table is still too wide after collapsing, rich's automatic overflow
        handling will be used.
        """
        columns = cast(List[Column], self.columns)
        collapsible = [column.collapse for column in columns]
        total_width = sum(widths)
        excess_width = total_width - max_width
        if any(collapsible):
            for i in range(len(widths) - 1, -1, -1):
                if collapsible[i]:
                    total_width -= widths[i]
                    excess_width -= widths[i]
                    widths[i] = 0
                    if excess_width <= 0:
                        break
        return super()._collapse_widths(widths, wrapable, max_width)
<commit_msg>exp show: fix column collapse behavior (#5753)<commit_after>from dataclasses import dataclass
from typing import TYPE_CHECKING, List, cast

from rich.style import StyleType
from rich.table import Column as RichColumn
from rich.table import Table as RichTable

if TYPE_CHECKING:
    from rich.console import (
        Console,
        ConsoleOptions,
        JustifyMethod,
        OverflowMethod,
        RenderableType,
    )

@dataclass
class Column(RichColumn):
    collapse: bool = False

class Table(RichTable):
    def add_column(  # pylint: disable=arguments-differ
        self,
        header: "RenderableType" = "",
        footer: "RenderableType" = "",
        *,
        header_style: StyleType = None,
        footer_style: StyleType = None,
        style: StyleType = None,
        justify: "JustifyMethod" = "left",
        overflow: "OverflowMethod" = "ellipsis",
        width: int = None,
        min_width: int = None,
        max_width: int = None,
        ratio: int = None,
        no_wrap: bool = False,
        collapse: bool = False,
    ) -> None:
        column = Column(  # type: ignore[call-arg]
            _index=len(self.columns),
            header=header,
            footer=footer,
            header_style=header_style or "",
            footer_style=footer_style or "",
            style=style or "",
            justify=justify,
            overflow=overflow,
            width=width,
            min_width=min_width,
            max_width=max_width,
            ratio=ratio,
            no_wrap=no_wrap,
            collapse=collapse,
        )
        self.columns.append(column)

    def _calculate_column_widths(
        self, console: "Console", options: "ConsoleOptions"
    ) -> List[int]:
        """Calculate the widths of each column, including padding, not
        including borders.

        Adjacent collapsed columns will be removed until there is only a single
        truncated column remaining.
        """
        widths = super()._calculate_column_widths(console, options)
        last_collapsed = -1
        columns = cast(List[Column], self.columns)
        for i in range(len(columns) - 1, -1, -1):
            if widths[i] == 0 and columns[i].collapse:
                if last_collapsed >= 0:
                    del widths[last_collapsed]
                    del columns[last_collapsed]
                    if self.box:
                        options.max_width += 1
                    for column in columns[last_collapsed:]:
                        column._index -= 1
                last_collapsed = i
                padding = self._get_padding_width(i)
                if (
                    columns[i].overflow == "ellipsis"
                    and (sum(widths) + padding) <= options.max_width
                ):
                    # Set content width to 1 (plus padding) if we can fit a
                    # single unicode ellipsis in this column
                    widths[i] = 1 + padding
            else:
                last_collapsed = -1
        return widths

    def _collapse_widths(  # type: ignore[override]
        self, widths: List[int], wrapable: List[bool], max_width: int,
    ) -> List[int]:
        """Collapse columns right-to-left if possible to fit table into
        max_width.

        If table is still too wide after collapsing, rich's automatic overflow
        handling will be used.
        """
        columns = cast(List[Column], self.columns)
        collapsible = [column.collapse for column in columns]
        total_width = sum(widths)
        excess_width = total_width - max_width
        if any(collapsible):
            for i in range(len(widths) - 1, -1, -1):
                if collapsible[i]:
                    total_width -= widths[i]
                    excess_width -= widths[i]
                    widths[i] = 0
                    if excess_width <= 0:
                        break
        return super()._collapse_widths(widths, wrapable, max_width)
<|endoftext|>


<commit_before>import peewee as pw
import datetime as dt

proxy = pw.Proxy()

class Role(pw.Model):
    name = pw.CharField(255, default='user')

class User(pw.Model):

    created = pw.DateTimeField(default=dt.datetime.now())
    name = pw.CharField(255)
    title = pw.CharField(127, null=True)
    active = pw.BooleanField(default=True)
    rating = pw.IntegerField(default=0)

    role = pw.ForeignKeyField(Role)

    class Meta:
        database = proxy

def test_base(db):
    assert db

def test_schema(db):
    proxy.initialize(db)
    db.create_tables([Role, User], safe=True)

    from marshmallow_peewee import ModelSchema

    class UserSchema(ModelSchema):
        class Meta:
            model = User

    role = Role.create()
    user = User.create(name='Mike', role=role)

    schema = UserSchema()
    result, errors = schema.load({'name': 'Bob', 'role': 1, 'active': False})
    assert not errors
    assert result.role
    assert not result.active

    result, errors = UserSchema().dump(user)
    assert not errors
    assert result
    assert result['id'] == 1
    assert result['name'] == 'Mike'

    result, errors = UserSchema().load(result)
    assert not errors
    assert isinstance(result, User)
    assert result.name == 'Mike'

def test_schema_related(db):
    proxy.initialize(db)
    db.create_tables([Role, User], safe=True)

    from marshmallow_peewee import ModelSchema, Related

    class UserSchema(ModelSchema):

        role = Related()

        class Meta:
            model = User
            dump_only_pk = False

    role = Role.create()
    user = User.create(name='Mike', role=role)

    assert UserSchema._declared_fields['role'].attribute == 'role'

    result, errors = UserSchema().dump(user)
    assert not errors
    assert result
    assert result['role']['name'] == 'user'

    result, errors = UserSchema().load(result)
    assert not errors
    assert isinstance(result, User)
    assert result.id == 1
    assert isinstance(result.role, Role)

    result, errors = UserSchema().load({
        'name': 'Kevin',
        'role': '1'
    })
    assert not result.id
    assert result.role

    class RoleSchema(ModelSchema):

        class Meta:
            model = Role
            fields = 'id',

    class UserSchema(ModelSchema):

        role = Related(RoleSchema)

        class Meta:
            model = User
            dump_only_pk = False

    result, errors = UserSchema().dump(user)
    assert not errors
    assert result
    assert 'name' not in result['role']

def tests_partition(db):
    proxy.initialize(db)
    db.create_tables([Role, User], safe=True)

    from marshmallow_peewee import ModelSchema

    class UserSchema(ModelSchema):
        class Meta:
            model = User

    role = Role.create()
    user = User.create(name='Mike', role=role)

    result, errors = UserSchema().load({
        'name': 'David'
    }, instance=user, partial=True)
    assert result.id == user.id
    assert user.name == 'David'
<commit_msg>More related tests.<commit_after>import peewee as pw
import datetime as dt

proxy = pw.Proxy()

class Role(pw.Model):
    name = pw.CharField(255, default='user')

class User(pw.Model):

    created = pw.DateTimeField(default=dt.datetime.now())
    name = pw.CharField(255)
    title = pw.CharField(127, null=True)
    active = pw.BooleanField(default=True)
    rating = pw.IntegerField(default=0)

    role = pw.ForeignKeyField(Role)

    class Meta:
        database = proxy

def test_base(db):
    assert db

def test_schema(db):
    proxy.initialize(db)
    db.create_tables([Role, User], safe=True)

    from marshmallow_peewee import ModelSchema

    class UserSchema(ModelSchema):
        class Meta:
            model = User

    role = Role.create()
    user = User.create(name='Mike', role=role)

    schema = UserSchema()
    result, errors = schema.load({'name': 'Bob', 'role': 1, 'active': False})
    assert not errors
    assert result.role
    assert not result.active

    result, errors = UserSchema().dump(user)
    assert not errors
    assert result
    assert result['id'] == 1
    assert result['name'] == 'Mike'

    result, errors = UserSchema().load(result)
    assert not errors
    assert isinstance(result, User)
    assert result.name == 'Mike'

def test_schema_related(db):
    proxy.initialize(db)
    db.create_tables([Role, User], safe=True)

    from marshmallow_peewee import ModelSchema, Related

    class UserSchema(ModelSchema):

        role = Related()

        class Meta:
            model = User
            dump_only_pk = False

    role = Role.create()
    user = User.create(name='Mike', role=role)

    assert UserSchema._declared_fields['role'].attribute == 'role'

    result, errors = UserSchema().dump(user)
    assert not errors
    assert result
    assert result['role']['name'] == 'user'

    result, errors = UserSchema().load(result)
    assert not errors
    assert isinstance(result, User)
    assert result.id == 1
    assert isinstance(result.role, Role)

    result, errors = UserSchema().load({
        'name': 'Kevin',
        'role': '1'
    })
    assert not result.id
    assert result.role

    class RoleSchema(ModelSchema):

        class Meta:
            model = Role
            fields = 'id',

    class UserSchema(ModelSchema):

        role = Related(RoleSchema)

        class Meta:
            model = User
            dump_only_pk = False

    result, errors = UserSchema().dump(user)
    assert not errors
    assert result
    assert 'name' not in result['role']

    class RoleSchema(ModelSchema):

        user_set = Related()

        class Meta:
            model = Role

    result, errors = RoleSchema().dump(user.role)
    assert result
    assert result['user_set']
    assert not errors

def tests_partition(db):
    proxy.initialize(db)
    db.create_tables([Role, User], safe=True)

    from marshmallow_peewee import ModelSchema

    class UserSchema(ModelSchema):
        class Meta:
            model = User

    role = Role.create()
    user = User.create(name='Mike', role=role)

    result, errors = UserSchema().load({
        'name': 'David'
    }, instance=user, partial=True)
    assert result.id == user.id
    assert user.name == 'David'
<|endoftext|>