Beispiel #1
0
def test_suggesting_values_for_edit_variables():
    """Test suggesting values in different situations.

    """
    # Suggest value for an edit variable entering a weak equality
    s = Solver()
    v1 = Variable('foo')

    s.addEditVariable(v1, 'medium')
    s.addConstraint((v1 == 1) | 'weak')
    s.suggestValue(v1, 2)
    s.updateVariables()
    assert v1.value() == 2

    # Suggest a value for an edit variable entering multiple solver rows
    s.reset()
    v1 = Variable('foo')
    v2 = Variable('bar')
    s = Solver()

    s.addEditVariable(v2, 'weak')
    s.addConstraint(v1 + v2 == 0)
    s.addConstraint((v2 <= -1))
    s.addConstraint((v2 >= 0) | 'weak')
    s.suggestValue(v2, 0)
    s.updateVariables()
    assert v2.value() <= -1
def test_basic_solver():
    s = Solver()
    x0 = Variable('x0')
    x1 = Variable('x1')
    s.addConstraint(x0 >= 0)
    s.addConstraint(x1 >= 0)
    s.updateVariables()

    assert x0.value() == 0.0
    assert x1.value() == 0.0
Beispiel #3
0
def test_handling_infeasible_constraints():
    """Test that we properly handle infeasible constraints.

    We use the example of the cassowary paper to generate an infeasible
    situation after updating an edit variable which causes the solver to use
    the dual optimization.

    """
    xm = Variable('xm')
    xl = Variable('xl')
    xr = Variable('xr')
    s = Solver()

    s.addEditVariable(xm, 'strong')
    s.addEditVariable(xl, 'weak')
    s.addEditVariable(xr, 'weak')
    s.addConstraint(2*xm == xl + xr)
    s.addConstraint(xl + 20 <= xr)
    s.addConstraint(xl >= -10)
    s.addConstraint(xr <= 100)

    s.suggestValue(xm, 40)
    s.suggestValue(xr, 50)
    s.suggestValue(xl, 30)

    # First update causing a normal update.
    s.suggestValue(xm, 60)

    # Create an infeasible condition triggering a dual optimization
    s.suggestValue(xm, 90)
    s.updateVariables()
    assert xl.value() + xr.value() == 2*xm.value()
    assert xl.value() == 80
    assert xr.value() == 100
Beispiel #4
0
def test_constraint_creation2():
    """Test for errors in Constraints creation.

    """
    v = Variable('foo')

    with pytest.raises(TypeError) as excinfo:
        Constraint(1, '==')
    assert "Expression" in excinfo.exconly()

    with pytest.raises(TypeError) as excinfo:
        Constraint(v + 1, 1)
    assert "unicode" in excinfo.exconly()

    with pytest.raises(ValueError) as excinfo:
        Constraint(v + 1, '!=')
    assert "relational operator" in excinfo.exconly()
Beispiel #5
0
def test_constraint_or_operator():
    """Test modifying a constraint strength using the | operator.

    """
    v = Variable('foo')
    c = Constraint(v + 1, u'==')

    for s in (u'weak', 'medium', 'strong', u'required',
              strength.create(1, 1, 0)):
        c2 = c | s
        if isinstance(s, (type(''), type(u''))):
            assert c2.strength() == getattr(strength, s)
        else:
            assert c2.strength() == s

    with pytest.raises(ValueError):
        c | 'unknown'
Beispiel #6
0
def test_constraint_creation():
    """Test constraints creation and methods.

    """
    v = Variable('foo')
    c = Constraint(v + 1, '==')

    assert c.strength() == strength.required and c.op() == '=='
    e = c.expression()
    t = e.terms()
    assert (e.constant() == 1 and len(t) == 1 and t[0].variable() is v
            and t[0].coefficient() == 1)

    assert str(c) == '1 * foo + 1 == 0 | strength = 1.001e+09'

    for s in ('weak', 'medium', 'strong', 'required'):
        c = Constraint(v + 1, '>=', s)
        assert c.strength() == getattr(strength, s)
Beispiel #7
0
def test_solving_with_strength():
    """Test solving a system with unstatisfiable non-required constraint.

    """
    v1 = Variable('foo')
    v2 = Variable('bar')
    s = Solver()

    s.addConstraint(v1 + v2 == 0)
    s.addConstraint(v1 == 10)
    s.addConstraint((v2 >= 0) | 'weak')
    s.updateVariables()
    assert v1.value() == 10 and v2.value() == -10

    s.reset()

    s.addConstraint(v1 + v2 == 0)
    s.addConstraint((v1 >= 10) | 'medium')
    s.addConstraint((v2 == 2) | 'strong')
    s.updateVariables()
    assert v1.value() == -2 and v2.value() == 2
Beispiel #8
0
    def _mark_constrained(self, constrained: bool, max_length: int = -1):
        """
        Internal method to mark the Segment hierarchy as constrained or not.

        DO NOT CALL THIS DIRECTLY!
        """
        all_ints = False
        for segment in self:
            if not isinstance(segment, Segment):
                # __new__ only accepts a homogenous sequence of int or Segment,
                # so if an element is not a Segment, then this Segment must
                # contain only ints
                all_ints = True
                break

            segment._mark_constrained(  # pylint:disable=protected-access
                constrained, max_length)

        # If the segment only contains tokens ids, then it can be constrained.
        self.naive_max_length = max_length
        self.length = Variable() if constrained and all_ints else None
    def __getattr__(self, name):
        """ Returns a kiwi constraint variable for the given name,
        unless the name is already in the instance dictionary.

        Parameters
        ----------
        name : str
            The name of the constraint variable to return.

        """
        try:
            return super(ConstraintsNamespace, self).__getattr__(name)
        except AttributeError:
            pass

        constraints = self._constraints
        if name in constraints:
            res = constraints[name]
        else:
            label = '{0}|{1}|{2}'.format(self._name, self._owner, name)
            res = constraints[name] = Variable(label)
        return res
Beispiel #10
0
def test_term_creation():
    """Test the Term constructor.

    """
    v = Variable('foo')
    t = Term(v)
    assert t.variable() is v
    assert t.coefficient() == 1

    t = Term(v, 100)
    assert t.variable() is v
    assert t.coefficient() == 100

    assert str(t) == '100 * foo'

    with pytest.raises(TypeError) as excinfo:
        Term('')
    assert 'Variable' in excinfo.exconly()

    # ensure we test garbage collection
    del t
    gc.collect()
def test_solver():
    s = Solver()
    x0 = Variable('x0')
    x1 = Variable('x1')
    s.addConstraint(x0 >= 0)
    s.addConstraint(x1 >= 0)
    s.addConstraint(x1 == x0)
    s.updateVariables()

    assert x0.value() == 0.0
    assert x1.value() == 0.0

    with edit_context(s, [x1]):
        s.suggestValue(x1, 1.0)
        s.updateVariables()
        assert x0.value() == 1.0
        assert x1.value() == 1.0
Beispiel #12
0
def test_constraint_creation(op):
    """Test constraints creation and methods.

    """
    v = Variable('foo')
    c = Constraint(v + 1, op)

    assert c.strength() == strength.required and c.op() == op
    e = c.expression()
    t = e.terms()
    assert (e.constant() == 1 and
            len(t) == 1 and t[0].variable() is v and t[0].coefficient() == 1)

    constraint_format = r'1 \* foo \+ 1 %s 0 | strength = 1.001e\+[0]+9' % op
    assert re.match(constraint_format, str(c))

    for s in ('weak', 'medium', 'strong', 'required'):
        c = Constraint(v + 1, op, s)
        assert c.strength() == getattr(strength, s)

    # Ensure we test garbage collection.
    del c
    gc.collect()
Beispiel #13
0
def test_solving_with_strength():
    """Test solving a system with unstatisfiable non-required constraint.

    """
    v1 = Variable('foo')
    v2 = Variable('bar')
    s = Solver()

    s.addConstraint(v1 + v2 == 0)
    s.addConstraint(v1 == 10)
    s.addConstraint((v2 >= 0) | 'weak')
    s.updateVariables()
    assert v1.value() == 10 and v2.value() == -10

    s.reset()

    s.addConstraint(v1 + v2 == 0)
    s.addConstraint((v1 >= 10) | 'medium')
    s.addConstraint((v2 == 2) | 'strong')
    s.updateVariables()
    assert v1.value() == -2 and v2.value() == 2
Beispiel #14
0
def test_expression_arith_operators():
    """Test the arithmetic operation on terms.

    """
    v = Variable('foo')
    v2 = Variable('bar')
    t = Term(v, 10)
    t2 = Term(v2)
    e = t + 5
    e2 = v2 - 10

    neg = -e
    assert isinstance(neg, Expression)
    neg_t = neg.terms()
    assert (len(neg_t) == 1 and neg_t[0].variable() is v
            and neg_t[0].coefficient() == -10 and neg.constant() == -5)

    mul = e * 2
    assert isinstance(mul, Expression)
    mul_t = mul.terms()
    assert (len(mul_t) == 1 and mul_t[0].variable() is v
            and mul_t[0].coefficient() == 20 and mul.constant() == 10)

    with pytest.raises(TypeError):
        e * v

    div = e / 2
    assert isinstance(div, Expression)
    div_t = div.terms()
    assert (len(div_t) == 1 and div_t[0].variable() is v
            and div_t[0].coefficient() == 5 and div.constant() == 2.5)

    with pytest.raises(TypeError):
        e / v2

    add = e + 2
    assert isinstance(add, Expression)
    assert add.constant() == 7
    terms = add.terms()
    assert (len(terms) == 1 and terms[0].variable() is v
            and terms[0].coefficient() == 10)

    add2 = e + v2
    assert isinstance(add2, Expression)
    assert add2.constant() == 5
    terms = add2.terms()
    assert (len(terms) == 2 and terms[0].variable() is v
            and terms[0].coefficient() == 10 and terms[1].variable() is v2
            and terms[1].coefficient() == 1)

    add3 = e + t2
    assert isinstance(add3, Expression)
    assert add3.constant() == 5
    terms = add3.terms()
    assert (len(terms) == 2 and terms[0].variable() is v
            and terms[0].coefficient() == 10 and terms[1].variable() is v2
            and terms[1].coefficient() == 1)

    add4 = e + e2
    assert isinstance(add4, Expression)
    assert add4.constant() == -5
    terms = add4.terms()
    assert (len(terms) == 2 and terms[0].variable() is v
            and terms[0].coefficient() == 10 and terms[1].variable() is v2
            and terms[1].coefficient() == 1)

    sub = e - 2
    assert isinstance(sub, Expression)
    assert sub.constant() == 3
    terms = sub.terms()
    assert (len(terms) == 1 and terms[0].variable() is v
            and terms[0].coefficient() == 10)

    sub2 = e - v2
    assert isinstance(sub2, Expression)
    assert sub2.constant() == 5
    terms = sub2.terms()
    assert (len(terms) == 2 and terms[0].variable() is v
            and terms[0].coefficient() == 10 and terms[1].variable() is v2
            and terms[1].coefficient() == -1)

    sub3 = e - t2
    assert isinstance(sub3, Expression)
    assert sub3.constant() == 5
    terms = sub3.terms()
    assert (len(terms) == 2 and terms[0].variable() is v
            and terms[0].coefficient() == 10 and terms[1].variable() is v2
            and terms[1].coefficient() == -1)

    sub4 = e - e2
    assert isinstance(sub3, Expression)
    assert sub4.constant() == 15
    terms = sub4.terms()
    assert (len(terms) == 2 and terms[0].variable() is v
            and terms[0].coefficient() == 10 and terms[1].variable() is v2
            and terms[1].coefficient() == -1)
Beispiel #15
0
class Grid(Widget):
    """Widget for proportionally dividing its internal area into a grid.

    This widget will automatically set the position and size of child widgets
    according to provided constraints.

    Parameters
    ----------
    spacing : int
        Spacing between widgets.
    **kwargs : dict
        Keyword arguments to pass to `Widget`.
    """
    def __init__(self, spacing=6, **kwargs):
        """Create solver and basic grid parameters."""
        self._next_cell = [0, 0]  # row, col
        self._cells = {}
        self._grid_widgets = {}
        self.spacing = spacing
        self._n_added = 0
        self._default_class = ViewBox  # what to add when __getitem__ is used
        self._solver = Solver()
        self._need_solver_recreate = True

        # width and height of the Rect used to place child widgets
        self._var_w = Variable("w_rect")
        self._var_h = Variable("h_rect")

        self._width_grid = None
        self._height_grid = None

        # self._height_stay = None
        # self._width_stay = None

        Widget.__init__(self, **kwargs)

    def __getitem__(self, idxs):
        """Return an item or create it if the location is available."""
        if not isinstance(idxs, tuple):
            idxs = (idxs, )
        if len(idxs) == 1:
            idxs = idxs + (slice(0, 1, None), )
        elif len(idxs) != 2:
            raise ValueError('Incorrect index: %s' % (idxs, ))
        lims = np.empty((2, 2), int)
        for ii, idx in enumerate(idxs):
            if isinstance(idx, int):
                idx = slice(idx, idx + 1, None)
            if not isinstance(idx, slice):
                raise ValueError('indices must be slices or integers, not %s' %
                                 (type(idx), ))
            if idx.step is not None and idx.step != 1:
                raise ValueError('step must be one or None, not %s' % idx.step)
            start = 0 if idx.start is None else idx.start
            end = self.grid_size[ii] if idx.stop is None else idx.stop
            lims[ii] = [start, end]
        layout = self.layout_array
        existing = layout[lims[0, 0]:lims[0, 1], lims[1, 0]:lims[1, 1]] + 1
        if existing.any():
            existing = set(list(existing.ravel()))
            ii = list(existing)[0] - 1
            if len(existing) != 1 or (
                (layout == ii).sum() != np.prod(np.diff(lims))):
                raise ValueError('Cannot add widget (collision)')
            return self._grid_widgets[ii][-1]
        spans = np.diff(lims)[:, 0]
        item = self.add_widget(self._default_class(),
                               row=lims[0, 0],
                               col=lims[1, 0],
                               row_span=spans[0],
                               col_span=spans[1])
        return item

    def add_widget(self,
                   widget=None,
                   row=None,
                   col=None,
                   row_span=1,
                   col_span=1,
                   **kwargs):
        """Add a new widget to this grid.

        This will cause other widgets in the grid to be resized to make room
        for the new widget. Can be used to replace a widget as well.

        Parameters
        ----------
        widget : Widget | None
            The Widget to add. New widget is constructed if widget is None.
        row : int
            The row in which to add the widget (0 is the topmost row)
        col : int
            The column in which to add the widget (0 is the leftmost column)
        row_span : int
            The number of rows to be occupied by this widget. Default is 1.
        col_span : int
            The number of columns to be occupied by this widget. Default is 1.
        **kwargs : dict
            parameters sent to the new Widget that is constructed if
            widget is None

        Notes
        -----
        The widget's parent is automatically set to this grid, and all other
        parent(s) are removed.
        """
        if row is None:
            row = self._next_cell[0]
        if col is None:
            col = self._next_cell[1]

        if widget is None:
            widget = Widget(**kwargs)
        else:
            if kwargs:
                raise ValueError("cannot send kwargs if widget is given")

        _row = self._cells.setdefault(row, {})
        _row[col] = widget
        self._grid_widgets[self._n_added] = (row, col, row_span, col_span,
                                             widget)
        self._n_added += 1
        widget.parent = self

        self._next_cell = [row, col + col_span]

        widget._var_w = Variable("w-(row: %s | col: %s)" % (row, col))
        widget._var_h = Variable("h-(row: %s | col: %s)" % (row, col))

        # update stretch based on colspan/rowspan
        # usually, if you make something consume more grids or columns,
        # you also want it to actually *take it up*, ratio wise.
        # otherwise, it will never *use* the extra rows and columns,
        # thereby collapsing the extras to 0.
        stretch = list(widget.stretch)
        stretch[0] = col_span if stretch[0] is None else stretch[0]
        stretch[1] = row_span if stretch[1] is None else stretch[1]
        widget.stretch = stretch

        self._need_solver_recreate = True

        return widget

    def remove_widget(self, widget):
        """Remove a widget from this grid.

        Parameters
        ----------
        widget : Widget
            The Widget to remove
        """
        self._grid_widgets = dict((key, val)
                                  for (key, val) in self._grid_widgets.items()
                                  if val[-1] != widget)

        self._need_solver_recreate = True

    def resize_widget(self, widget, row_span, col_span):
        """Resize a widget in the grid to new dimensions.

        Parameters
        ----------
        widget : Widget
            The widget to resize
        row_span : int
            The number of rows to be occupied by this widget.
        col_span : int
            The number of columns to be occupied by this widget.
        """
        row = None
        col = None

        for (r, c, _rspan, _cspan, w) in self._grid_widgets.values():
            if w == widget:
                row = r
                col = c

                break

        if row is None or col is None:
            raise ValueError("%s not found in grid" % widget)

        self.remove_widget(widget)
        self.add_widget(widget, row, col, row_span, col_span)
        self._need_solver_recreate = True

    def _prepare_draw(self, view):
        self._update_child_widget_dim()

    def add_grid(self, row=None, col=None, row_span=1, col_span=1, **kwargs):
        """
        Create a new Grid and add it as a child widget.

        Parameters
        ----------
        row : int
            The row in which to add the widget (0 is the topmost row)
        col : int
            The column in which to add the widget (0 is the leftmost column)
        row_span : int
            The number of rows to be occupied by this widget. Default is 1.
        col_span : int
            The number of columns to be occupied by this widget. Default is 1.
        **kwargs : dict
            Keyword arguments to pass to the new `Grid`.
        """
        from .grid import Grid
        grid = Grid(**kwargs)
        return self.add_widget(grid, row, col, row_span, col_span)

    def add_view(self, row=None, col=None, row_span=1, col_span=1, **kwargs):
        """
        Create a new ViewBox and add it as a child widget.

        Parameters
        ----------
        row : int
            The row in which to add the widget (0 is the topmost row)
        col : int
            The column in which to add the widget (0 is the leftmost column)
        row_span : int
            The number of rows to be occupied by this widget. Default is 1.
        col_span : int
            The number of columns to be occupied by this widget. Default is 1.
        **kwargs : dict
            Keyword arguments to pass to `ViewBox`.
        """
        view = ViewBox(**kwargs)
        return self.add_widget(view, row, col, row_span, col_span)

    def next_row(self):
        self._next_cell = [self._next_cell[0] + 1, 0]

    @property
    def grid_size(self):
        rvals = [
            widget[0] + widget[2] for widget in self._grid_widgets.values()
        ]
        cvals = [
            widget[1] + widget[3] for widget in self._grid_widgets.values()
        ]
        return max(rvals + [0]), max(cvals + [0])

    @property
    def layout_array(self):
        locs = -1 * np.ones(self.grid_size, int)
        for key in self._grid_widgets.keys():
            r, c, rs, cs = self._grid_widgets[key][:4]
            locs[r:r + rs, c:c + cs] = key
        return locs

    def __repr__(self):
        return (('<Grid at %s:\n' % hex(id(self))) +
                str(self.layout_array + 1) + '>')

    @staticmethod
    def _add_total_width_constraints(solver, width_grid, _var_w):
        for ws in width_grid:
            width_expr = ws[0]
            for w in ws[1:]:
                width_expr += w
            solver.addConstraint(width_expr == _var_w)

    @staticmethod
    def _add_total_height_constraints(solver, height_grid, _var_h):
        for hs in height_grid:
            height_expr = hs[0]
            for h in hs[1:]:
                height_expr += h
            solver.addConstraint(height_expr == _var_h)

    @staticmethod
    def _add_gridding_width_constraints(solver, width_grid):
        # access widths of one "y", different x
        for ws in width_grid.T:
            for w in ws[1:]:
                solver.addConstraint(ws[0] == w)

    @staticmethod
    def _add_gridding_height_constraints(solver, height_grid):
        # access heights of one "y"
        for hs in height_grid.T:
            for h in hs[1:]:
                solver.addConstraint(hs[0] == h)

    @staticmethod
    def _add_stretch_constraints(solver, width_grid, height_grid, grid_widgets,
                                 widget_grid):
        xmax = len(height_grid)
        ymax = len(width_grid)

        stretch_widths = [[] for _ in range(0, ymax)]
        stretch_heights = [[] for _ in range(0, xmax)]

        for (y, x, ys, xs, widget) in grid_widgets.values():
            for ws in width_grid[y:y + ys]:
                total_w = np.sum(ws[x:x + xs])

                for sw in stretch_widths[y:y + ys]:
                    sw.append((total_w, widget.stretch[0]))

            for hs in height_grid[x:x + xs]:
                total_h = np.sum(hs[y:y + ys])

                for sh in stretch_heights[x:x + xs]:
                    sh.append((total_h, widget.stretch[1]))

        for (x, xs) in enumerate(widget_grid):
            for (y, widget) in enumerate(xs):
                if widget is None:
                    stretch_widths[y].append((width_grid[y][x], 1))
                    stretch_heights[x].append((height_grid[x][y], 1))

        for sws in stretch_widths:
            if len(sws) <= 1:
                continue

            comparator = sws[0][0] / sws[0][1]

            for (stretch_term, stretch_val) in sws[1:]:
                solver.addConstraint((comparator == stretch_term / stretch_val)
                                     | 'weak')

        for sws in stretch_heights:
            if len(sws) <= 1:
                continue

            comparator = sws[0][0] / sws[0][1]

            for (stretch_term, stretch_val) in sws[1:]:
                solver.addConstraint((comparator == stretch_term / stretch_val)
                                     | 'weak')

    @staticmethod
    def _add_widget_dim_constraints(solver, width_grid, height_grid,
                                    total_var_w, total_var_h, grid_widgets):
        assert (total_var_w is not None)
        assert (total_var_h is not None)

        for ws in width_grid:
            for w in ws:
                solver.addConstraint(w >= 0, )

        for hs in height_grid:
            for h in hs:
                solver.addConstraint(h >= 0)

        for (_, val) in grid_widgets.items():
            (y, x, ys, xs, widget) = val

            for ws in width_grid[y:y + ys]:
                total_w = np.sum(ws[x:x + xs])
                # assert(total_w is not None)
                solver.addConstraint(total_w >= widget.width_min)

                if widget.width_max is not None:
                    solver.addConstraint(total_w <= widget.width_max)
                else:
                    solver.addConstraint(total_w <= total_var_w)

            for hs in height_grid[x:x + xs]:
                total_h = np.sum(hs[y:y + ys])
                solver.addConstraint(total_h >= widget.height_min)

                if widget.height_max is not None:
                    solver.addConstraint(total_h <= widget.height_max)
                else:
                    solver.addConstraint(total_h <= total_var_h)

    def _recreate_solver(self):
        self._solver.reset()
        self._var_w = Variable("w_rect")
        self._var_h = Variable("h_rect")
        self._solver.addEditVariable(self._var_w, 'strong')
        self._solver.addEditVariable(self._var_h, 'strong')

        rect = self.rect.padded(self.padding + self.margin)
        ymax, xmax = self.grid_size

        self._solver.suggestValue(self._var_w, rect.width)
        self._solver.suggestValue(self._var_h, rect.height)

        self._solver.addConstraint(self._var_w >= 0)
        self._solver.addConstraint(self._var_h >= 0)

        # self._height_stay = None
        # self._width_stay = None

        # add widths
        self._width_grid = np.array([[
            Variable("width(x: %s, y: %s)" % (x, y)) for x in range(0, xmax)
        ] for y in range(0, ymax)])

        # add heights
        self._height_grid = np.array([[
            Variable("height(x: %s, y: %s" % (x, y)) for y in range(0, ymax)
        ] for x in range(0, xmax)])

        # setup stretch
        stretch_grid = np.zeros(shape=(xmax, ymax, 2), dtype=float)
        stretch_grid.fill(1)

        for (_, val) in self._grid_widgets.items():
            (y, x, ys, xs, widget) = val
            stretch_grid[x:x + xs, y:y + ys] = widget.stretch

        # even though these are REQUIRED, these should never fail
        # since they're added first, and thus the slack will "simply work".
        Grid._add_total_width_constraints(self._solver, self._width_grid,
                                          self._var_w)
        Grid._add_total_height_constraints(self._solver, self._height_grid,
                                           self._var_h)

        try:
            # these are REQUIRED constraints for width and height.
            # These are the constraints which can fail if
            # the corresponding dimension of the widget cannot be fit in the
            # grid.
            Grid._add_gridding_width_constraints(self._solver,
                                                 self._width_grid)
            Grid._add_gridding_height_constraints(self._solver,
                                                  self._height_grid)
        except UnsatisfiableConstraint:
            self._need_solver_recreate = True

        # these are WEAK constraints, so these constraints will never fail
        # with a RequiredFailure.
        Grid._add_stretch_constraints(self._solver, self._width_grid,
                                      self._height_grid, self._grid_widgets,
                                      self._widget_grid)

        Grid._add_widget_dim_constraints(self._solver, self._width_grid,
                                         self._height_grid, self._var_w,
                                         self._var_h, self._grid_widgets)

        self._solver.updateVariables()

    def _update_child_widget_dim(self):
        # think in terms of (x, y). (row, col) makes code harder to read
        ymax, xmax = self.grid_size
        if ymax <= 0 or xmax <= 0:
            return

        rect = self.rect  # .padded(self.padding + self.margin)
        if rect.width <= 0 or rect.height <= 0:
            return
        if self._need_solver_recreate:
            self._need_solver_recreate = False
            self._recreate_solver()

        # we only need to remove and add the height and width constraints of
        # the solver if they are not the same as the current value
        h_changed = abs(rect.height - self._var_h.value()) > 1e-4
        w_changed = abs(rect.width - self._var_w.value()) > 1e-4
        if h_changed:
            self._solver.suggestValue(self._var_h, rect.height)

        if w_changed:
            self._solver.suggestValue(self._var_w, rect.width)
        if h_changed or w_changed:
            self._solver.updateVariables()

        value_vectorized = np.vectorize(lambda x: x.value())

        for (_, val) in self._grid_widgets.items():
            (row, col, rspan, cspan, widget) = val

            width = np.sum(
                value_vectorized(self._width_grid[row][col:col + cspan]))
            height = np.sum(
                value_vectorized(self._height_grid[col][row:row + rspan]))
            if col == 0:
                x = 0
            else:
                x = np.sum(value_vectorized(self._width_grid[row][0:col]))

            if row == 0:
                y = 0
            else:
                y = np.sum(value_vectorized(self._height_grid[col][0:row]))

            if isinstance(widget, ViewBox):
                widget.rect = Rect(x, y, width, height)
            else:
                widget.size = (width, height)
                widget.pos = (x, y)

    @property
    def _widget_grid(self):
        ymax, xmax = self.grid_size
        widget_grid = np.array([[None for _ in range(0, ymax)]
                                for _ in range(0, xmax)])
        for (_, val) in self._grid_widgets.items():
            (y, x, ys, xs, widget) = val
            widget_grid[x:x + xs, y:y + ys] = widget

        return widget_grid
Beispiel #16
0
# The full license is in the file LICENSE, distributed with this software.
#------------------------------------------------------------------------------
"""Time updating an EditVariable in a set of constraints typical of enaml use.

"""
import perf
from kiwisolver import Variable, Solver, strength

solver = Solver()

# Create custom strength
mmedium = strength.create(0, 1, 0, 1.25)
smedium = strength.create(0, 100, 0)

# Create the variable
left = Variable('left')
height = Variable('height')
top = Variable('top')
width = Variable('width')
contents_top = Variable('contents_top')
contents_bottom = Variable('contents_bottom')
contents_left = Variable('contents_left')
contents_right = Variable('contents_right')
midline = Variable('midline')
ctleft = Variable('ctleft')
ctheight = Variable('ctheight')
cttop = Variable('cttop')
ctwidth = Variable('ctwidth')
lb1left = Variable('lb1left')
lb1height = Variable('lb1height')
lb1top = Variable('lb1top')
Beispiel #17
0
    def _recreate_solver(self):
        self._solver.reset()
        self._var_w = Variable("w_rect")
        self._var_h = Variable("h_rect")
        self._solver.addEditVariable(self._var_w, 'strong')
        self._solver.addEditVariable(self._var_h, 'strong')

        rect = self.rect.padded(self.padding + self.margin)
        ymax, xmax = self.grid_size

        self._solver.suggestValue(self._var_w, rect.width)
        self._solver.suggestValue(self._var_h, rect.height)

        self._solver.addConstraint(self._var_w >= 0)
        self._solver.addConstraint(self._var_h >= 0)

        # self._height_stay = None
        # self._width_stay = None

        # add widths
        self._width_grid = np.array([[
            Variable("width(x: %s, y: %s)" % (x, y)) for x in range(0, xmax)
        ] for y in range(0, ymax)])

        # add heights
        self._height_grid = np.array([[
            Variable("height(x: %s, y: %s" % (x, y)) for y in range(0, ymax)
        ] for x in range(0, xmax)])

        # setup stretch
        stretch_grid = np.zeros(shape=(xmax, ymax, 2), dtype=float)
        stretch_grid.fill(1)

        for (_, val) in self._grid_widgets.items():
            (y, x, ys, xs, widget) = val
            stretch_grid[x:x + xs, y:y + ys] = widget.stretch

        # even though these are REQUIRED, these should never fail
        # since they're added first, and thus the slack will "simply work".
        Grid._add_total_width_constraints(self._solver, self._width_grid,
                                          self._var_w)
        Grid._add_total_height_constraints(self._solver, self._height_grid,
                                           self._var_h)

        try:
            # these are REQUIRED constraints for width and height.
            # These are the constraints which can fail if
            # the corresponding dimension of the widget cannot be fit in the
            # grid.
            Grid._add_gridding_width_constraints(self._solver,
                                                 self._width_grid)
            Grid._add_gridding_height_constraints(self._solver,
                                                  self._height_grid)
        except UnsatisfiableConstraint:
            self._need_solver_recreate = True

        # these are WEAK constraints, so these constraints will never fail
        # with a RequiredFailure.
        Grid._add_stretch_constraints(self._solver, self._width_grid,
                                      self._height_grid, self._grid_widgets,
                                      self._widget_grid)

        Grid._add_widget_dim_constraints(self._solver, self._width_grid,
                                         self._height_grid, self._var_w,
                                         self._var_h, self._grid_widgets)

        self._solver.updateVariables()
Beispiel #18
0
def test_variable_methods():
    """Test the variable modification methods.

    """
    v = Variable()
    assert v.name() == ""
    v.setName('bar')
    assert v.name() == 'bar'
    with pytest.raises(TypeError):
        if sys.version_info >= (3, ):
            v.setName(b'r')
        else:
            v.setName(u'r')

    assert v.value() == 0.0

    ctx = object()
    v.setContext(ctx)
    assert v.context() is ctx

    assert str(v) == 'bar'
Beispiel #19
0
    def add_widget(self,
                   widget=None,
                   row=None,
                   col=None,
                   row_span=1,
                   col_span=1,
                   **kwargs):
        """Add a new widget to this grid.

        This will cause other widgets in the grid to be resized to make room
        for the new widget. Can be used to replace a widget as well.

        Parameters
        ----------
        widget : Widget | None
            The Widget to add. New widget is constructed if widget is None.
        row : int
            The row in which to add the widget (0 is the topmost row)
        col : int
            The column in which to add the widget (0 is the leftmost column)
        row_span : int
            The number of rows to be occupied by this widget. Default is 1.
        col_span : int
            The number of columns to be occupied by this widget. Default is 1.
        **kwargs : dict
            parameters sent to the new Widget that is constructed if
            widget is None

        Notes
        -----
        The widget's parent is automatically set to this grid, and all other
        parent(s) are removed.
        """
        if row is None:
            row = self._next_cell[0]
        if col is None:
            col = self._next_cell[1]

        if widget is None:
            widget = Widget(**kwargs)
        else:
            if kwargs:
                raise ValueError("cannot send kwargs if widget is given")

        _row = self._cells.setdefault(row, {})
        _row[col] = widget
        self._grid_widgets[self._n_added] = (row, col, row_span, col_span,
                                             widget)
        self._n_added += 1
        widget.parent = self

        self._next_cell = [row, col + col_span]

        widget._var_w = Variable("w-(row: %s | col: %s)" % (row, col))
        widget._var_h = Variable("h-(row: %s | col: %s)" % (row, col))

        # update stretch based on colspan/rowspan
        # usually, if you make something consume more grids or columns,
        # you also want it to actually *take it up*, ratio wise.
        # otherwise, it will never *use* the extra rows and columns,
        # thereby collapsing the extras to 0.
        stretch = list(widget.stretch)
        stretch[0] = col_span if stretch[0] is None else stretch[0]
        stretch[1] = row_span if stretch[1] is None else stretch[1]
        widget.stretch = stretch

        self._need_solver_recreate = True

        return widget
Beispiel #20
0
    def _get_constraints(self, component):
        """ Generate the grid constraints.

        This is an abstractmethod implementation which will use the
        space available on the provided component to layout the items.

        """
        grid_rows = self.grid_rows
        if not grid_rows:
            return []

        # Validate and compute the cell span for the items in the grid.
        cells = []
        cell_map = {}
        num_cols = 0
        num_rows = len(grid_rows)
        for row_idx, row in enumerate(grid_rows):
            for col_idx, item in enumerate(row):
                if item is None:
                    continue
                elif isinstance(item, ABConstrainable):
                    if item in cell_map:
                        cell_map[item].expand_to(row_idx, col_idx)
                    else:
                        cell = _GridCell(item, row_idx, col_idx)
                        cell_map[item] = cell
                        cells.append(cell)
                else:
                    m = ('Grid cells must be constrainable objects or None. '
                         'Got object of type `%s` instead.')
                    raise TypeError(m % type(item).__name__)
            num_cols = max(num_cols, col_idx + 1)

        # Setup the initial outer constraints of the grid
        if component is not None:
            # This box helper is inside a real component, not just nested
            # inside of another box helper. Check if the component is a
            # PaddingConstraints object and use it's contents anchors.
            attrs = ['top', 'bottom', 'left', 'right']
            # XXX hack!
            if hasattr(component, 'contents_top'):
                other_attrs = ['contents_' + attr for attr in attrs]
            else:
                other_attrs = attrs[:]
            constraints = [
                getattr(self, attr) == getattr(component, other)
                for (attr, other) in zip(attrs, other_attrs)
            ]
        else:
            constraints = []

        # Create the row and column constraint variables along with
        # some default limits
        row_vars = []
        col_vars = []
        cn_id = self.constraints_id
        for idx in range(num_rows + 1):
            name = 'row' + str(idx)
            var = Variable('{0}|{1}'.format(cn_id, name))
            row_vars.append(var)
            constraints.append(var >= 0)
        for idx in range(num_cols + 1):
            name = 'col' + str(idx)
            var = Variable('{0}|{1}'.format(cn_id, name))
            col_vars.append(var)
            constraints.append(var >= 0)

        # Add some neighbor relations to the row and column vars.
        for r1, r2 in zip(row_vars[:-1], row_vars[1:]):
            constraints.append(r1 >= r2)
        for c1, c2 in zip(col_vars[:-1], col_vars[1:]):
            constraints.append(c1 <= c2)

        # Setup the initial interior bounding box for the grid.
        margins = self.margins
        bottom_items = (self.bottom, EqSpacer(margins.bottom), row_vars[-1])
        top_items = (row_vars[0], EqSpacer(margins.top), self.top)
        left_items = (self.left, EqSpacer(margins.left), col_vars[0])
        right_items = (col_vars[-1], EqSpacer(margins.right), self.right)
        helpers = [
            AbutmentHelper('vertical', *bottom_items),
            AbutmentHelper('vertical', *top_items),
            AbutmentHelper('horizontal', *left_items),
            AbutmentHelper('horizontal', *right_items),
        ]

        # Setup the spacer list for constraining the cell items
        row_spacer = FlexSpacer(self.row_spacing / 2.)
        col_spacer = FlexSpacer(self.col_spacing / 2.)
        rspace = [row_spacer] * len(row_vars)
        rspace[0] = 0
        rspace[-1] = 0
        cspace = [col_spacer] * len(col_vars)
        cspace[0] = 0
        cspace[-1] = 0

        # Setup the constraints for each constrainable grid cell.
        for cell in cells:
            sr = cell.start_row
            er = cell.end_row + 1
            sc = cell.start_col
            ec = cell.end_col + 1
            item = cell.item
            row_item = (row_vars[sr], rspace[sr], item, rspace[er],
                        row_vars[er])
            col_item = (col_vars[sc], cspace[sc], item, cspace[ec],
                        col_vars[ec])
            helpers.append(AbutmentHelper('vertical', *row_item))
            helpers.append(AbutmentHelper('horizontal', *col_item))
            if isinstance(item, DeferredConstraints):
                helpers.append(item)

        # Add the row alignment constraints if given. This will only
        # apply the alignment constraint to items which do not span
        # multiple rows.
        if self.row_align:
            row_map = defaultdict(list)
            for cell in cells:
                if cell.start_row == cell.end_row:
                    row_map[cell.start_row].append(cell.item)
            for items in row_map.values():
                if len(items) > 1:
                    helpers.append(AlignmentHelper(self.row_align, *items))

        # Add the column alignment constraints if given. This will only
        # apply the alignment constraint to items which do not span
        # multiple columns.
        if self.col_align:
            col_map = defaultdict(list)
            for cell in cells:
                if cell.start_col == cell.end_col:
                    col_map[cell.start_col].append(cell.item)
            for items in row_map.values():
                if len(items) > 1:
                    helpers.append(AlignmentHelper(self.col_align, *items))

        # Add the child helpers constraints to the constraints list.
        for helper in helpers:
            constraints.extend(helper.get_constraints(None))

        return constraints
Beispiel #21
0
def test_term_arith_operators():
    """Test the arithmetic operation on terms.

    """
    v = Variable('foo')
    v2 = Variable('bar')
    t = Term(v, 10)
    t2 = Term(v2)

    neg = -t
    assert isinstance(neg, Term)
    assert neg.variable() is v and neg.coefficient() == -10

    mul = t * 2
    assert isinstance(mul, Term)
    assert mul.variable() is v and mul.coefficient() == 20

    with pytest.raises(TypeError):
        t * v

    div = t / 2
    assert isinstance(div, Term)
    assert div.variable() is v and div.coefficient() == 5

    with pytest.raises(TypeError):
        t / v2

    add = t + 2
    assert isinstance(add, Expression)
    assert add.constant() == 2
    terms = add.terms()
    assert (len(terms) == 1 and terms[0].variable() is v
            and terms[0].coefficient() == 10)

    add2 = t + v2
    assert isinstance(add2, Expression)
    assert add2.constant() == 0
    terms = add2.terms()
    assert (len(terms) == 2 and terms[0].variable() is v
            and terms[0].coefficient() == 10 and terms[1].variable() is v2
            and terms[1].coefficient() == 1)

    add2 = t + t2
    assert isinstance(add2, Expression)
    assert add2.constant() == 0
    terms = add2.terms()
    assert (len(terms) == 2 and terms[0].variable() is v
            and terms[0].coefficient() == 10 and terms[1].variable() is v2
            and terms[1].coefficient() == 1)

    sub = t - 2
    assert isinstance(sub, Expression)
    assert sub.constant() == -2
    terms = sub.terms()
    assert (len(terms) == 1 and terms[0].variable() is v
            and terms[0].coefficient() == 10)

    sub2 = t - v2
    assert isinstance(sub2, Expression)
    assert sub2.constant() == 0
    terms = sub2.terms()
    assert (len(terms) == 2 and terms[0].variable() is v
            and terms[0].coefficient() == 10 and terms[1].variable() is v2
            and terms[1].coefficient() == -1)

    sub2 = t - t2
    assert isinstance(sub2, Expression)
    assert sub2.constant() == 0
    terms = sub2.terms()
    assert (len(terms) == 2 and terms[0].variable() is v
            and terms[0].coefficient() == 10 and terms[1].variable() is v2
            and terms[1].coefficient() == -1)
def get_simple_constraint():
    foo = Variable('foo')
    bar = Variable('bar')
    return (foo == bar)
Beispiel #23
0
def test_variable_methods():
    """Test the variable modification methods.

    """
    v = Variable()
    assert v.name() == ""
    v.setName(u'γ')
    assert v.name() == 'γ'
    v.setName('foo')
    assert v.name() == 'foo'
    with pytest.raises(TypeError):
        v.setName(1)
    if sys.version_info >= (3,):
        with pytest.raises(TypeError):
            v.setName(b'r')

    assert v.value() == 0.0

    assert v.context() is None
    ctx = object()
    v.setContext(ctx)
    assert v.context() is ctx

    assert str(v) == 'foo'

    with pytest.raises(TypeError):
        Variable(1)
Beispiel #24
0
        self.model = model.model
        self.avgpool = nn.AdaptiveAvgPool2d((self.part, 1))
        # remove the final downsample
        self.model.layer4[0].downsample[0].stride = (1, 1)
        self.model.layer4[0].conv2.stride = (1, 1)

    def forward(self, x):
        x = self.model.conv1(x)
        x = self.model.bn1(x)
        x = self.model.relu(x)
        x = self.model.maxpool(x)

        x = self.model.layer1(x)
        x = self.model.layer2(x)
        x = self.model.layer3(x)
        x = self.model.layer4(x)
        x = self.avgpool(x)
        y = x.view(x.size(0), x.size(1), x.size(2))
        return y


if __name__ == '__main__':
    # debug model structure
    # net = ft_net(751)
    net = ft_net_dense(751)
    # print(net)
    input = Variable(torch.FloatTensor(8, 3, 224, 224))
    output = net(input)
    print('net output size:')
    print(output.shape)