Exemple #1
0
def test_parse_expr_unexpected_parses(instr):
    """You used to be able to say a:b:c:d:e and still have it parsed"""

    with pytest.raises(TypeError) as exc:
        utils.parse_expr(instr)

    assert str(exc.value) == "interval syntax requires a tuple, 'lo:hi'"
Exemple #2
0
    def prepare(self, data, model, stat=None):

        old_filter = parse_expr(data.get_filter())
        old_group = data.grouped
        new_filter = parse_expr(data.get_filter(group=False))
        try:
            if old_group:
                data.ungroup()
                for interval in new_filter:
                    data.notice(*interval)

            (self.xlo, self.y, yerr, xerr,
             self.xlabel, self.ylabel) = data.to_plot(yfunc=model)
            self.y = self.y[1]

            if data.units != 'channel':
                elo, ehi = data._get_ebins(group=False)
            else:
                elo, ehi = (data.channel,data.channel+1.)

            self.xlo = data.apply_filter(elo, data._min)
            self.xhi = data.apply_filter(ehi, data._max)
            if data.units == 'wavelength':
                self.xlo = data._hc/self.xlo
                self.xhi = data._hc/self.xhi

        finally:
            if old_group:
                data.ignore()
                data.group()
                for interval in old_filter:
                    data.notice(*interval)
Exemple #3
0
def test_parse_expr_not_num(instr, bound):

    with pytest.raises(TypeError) as exc:
        utils.parse_expr(instr)

    emsg = "Invalid {} bound 'None'".format(bound)
    assert str(exc.value) == emsg
Exemple #4
0
def test_parse_expr(arg, expected):
    """Check parse_expr with various conditions

    Would be nice to use something like hypothesis to use
    property-based testing.
    """
    assert utils.parse_expr(arg) == expected
Exemple #5
0
    def prepare(self, data, model, stat=None):

        old_filter = parse_expr(data.get_filter())
        old_group = data.grouped
        new_filter = parse_expr(data.get_filter(group=False))
        try:
            if old_group:
                data.ungroup()
                for interval in new_filter:
                    data.notice(*interval)

            super().prepare(data, model, stat=stat)

        finally:
            if old_group:
                data.ignore()
                data.group()
                for interval in old_filter:
                    data.notice(*interval)
Exemple #6
0
    def prepare(self, data, model, stat=None):

        # We could fit this into a single try/finally group but
        # it makes it harder to see what is going on so split
        # it out into separate plot types:
        #   - ungrouped
        #   - all data has been masked out (we let prepare
        #     throw any errors or plot no data)
        #   - grouped but no filter (or the filter doesn't
        #     remove any points).
        #   - grouped and filtered
        #
        if not data.grouped or data.mask is False or \
           (data.mask is not True and not data.mask.any()):
            super().prepare(data, model, stat=stat)
            return

        # At this point mask can be True or an array.
        #
        if data.mask is True or data.mask.all():
            try:
                data.ungroup()
                super().prepare(data, model, stat=stat)

            finally:
                data.group()

            return

        # We need to convert the filter expression from grouped
        # to ungrouped, apply it, create the plot, then restore
        # the old expression. Note that we can just copy over
        # the original mask value to restore the old filter.
        #
        old_mask = data.mask.copy()
        new_filter = parse_expr(data.get_filter(group=False))
        try:
            data.ungroup()
            for interval in new_filter:
                data.notice(*interval)

            super().prepare(data, model, stat=stat)

        finally:
            data.group()
            data.mask = old_mask
Exemple #7
0
def test_parse_expr_no_ordering(instr, expected):
    """There's no requirement that lo <= hi"""

    assert utils.parse_expr(instr) == expected
Exemple #8
0
def test_parse_expr_no_range_checking(instr, expected):
    """Each range is separate, and there is no constraint."""

    assert utils.parse_expr(instr) == expected
Exemple #9
0
def test_parse_expr_empty(arg):
    assert utils.parse_expr(arg) == [(None, None)]
Exemple #10
0
    def prepare(self, data, model, orders=None, colors=None):
        self.orders = data.response_ids

        if orders is not None:
            if iterable(orders):
                self.orders = list(orders)
            else:
                self.orders = [orders]

        if colors is not None:
            self.use_default_colors=False
            if iterable(colors):
                self.colors = list(colors)
            else:
                self.colors = [colors]
        else:
            self.colors=[]
            top_color = '0xffffff'
            bot_color = '0x0000bf'
            num = len(self.orders)
            jump = (int(top_color, 16) - int(bot_color,16))/(num+1)
            for order in self.orders:
                self.colors.append(top_color)
                top_color = hex(int(top_color,16)-jump)

        if not self.use_default_colors and len(colors) != len(orders):
            raise PlotErr('ordercolors', len(orders), len(colors))

        old_filter = parse_expr(data.get_filter())
        old_group = data.grouped

        try:
            if old_group:
                data.ungroup()
                for interval in old_filter:
                    data.notice(*interval)

            self.xlo=[]
            self.xhi=[]
            self.y=[]
            (xlo, y, yerr,xerr,
             self.xlabel, self.ylabel) = data.to_plot(model)
            y = y[1]
            if data.units != 'channel':
                elo, ehi = data._get_ebins(group=False)
                xlo = data.apply_filter(elo, data._min)
                xhi = data.apply_filter(ehi, data._max)
                if data.units == 'wavelength':
                    xlo = data._hc/xlo
                    xhi = data._hc/xhi
            else:
                xhi = xlo + 1.

            for order in self.orders:
                self.xlo.append(xlo)
                self.xhi.append(xhi)
                if len(data.response_ids) > 2:
                    if order < 1 or order > len(model.rhs.orders):
                        raise PlotErr('notorder', order)
                    y = data.apply_filter(model.rhs.orders[order-1])
                    y = data._fix_y_units(y,True)
                    if data.exposure:
                        y = data.exposure * y
                self.y.append(y)

        finally:
            if old_group:
                data.ignore()
                data.group()
                for interval in old_filter:
                    data.notice(*interval)

        self.title = 'Model Orders %s' % str(self.orders)

        if len(self.xlo) != len(self.y):
            raise PlotErr("orderarrfail")