예제 #1
0
def xfail_known_issues(request, known_issues: Dict[str, str],
                       config_env: Dict):
    lookup_key1 = f'{request.node.originalname}:{config_env["CO"]}:{config_env["FN"]}'
    lookup_key2 = f'{request.node.originalname}:{config_env["CO"]}:'
    lookup_key3 = f':{config_env["CO"]}:{config_env["FN"]}'
    if lookup_key1 in known_issues.keys():
        request.applymarker(mark.xfail(reason=known_issues[lookup_key1]))
    elif lookup_key2 in known_issues.keys():
        request.applymarker(mark.xfail(reason=known_issues[lookup_key2]))
    elif lookup_key3 in known_issues.keys():
        request.applymarker(mark.xfail(reason=known_issues[lookup_key3]))
def pytest_runtest_setup(item):
    """Apply custom markers.

    Marks:

     - not implemented tests as expected to fail,
     - not written tests as skipped.
    """
    not_implemented = item.get_closest_marker("notimplemented")
    not_written = item.get_closest_marker("notwritten")

    if not_implemented:
        item.add_marker(mark.xfail(reason="Not implemented"))

    if not_written:
        item.add_marker(mark.skip(reason="Not written"))
예제 #3
0
@mark.parametrize('reader_kwargs,compress_kwargs', [
    (dict(add_references=False), dict()),
    (dict(add_references=False), dict(verify=True)),
    (dict(add_references=False), dict(verify=False)),
    (dict(add_references=False), dict(verify=True, single_analytic=True)),
    (dict(add_references=False), dict(verify=False, single_analytic=True)),
    (dict(add_references=False), dict(verify=True, single_analytic=False)),
    (dict(add_references=False), dict(verify=False, single_analytic=False)),
    (dict(add_references=False), dict(single_analytic=False)),
    (dict(add_references=False), dict(single_analytic=True)),
    (dict(add_references=True), dict()),
    param(
        dict(add_references=True),
        dict(verify=True),
        marks=[mark.xfail(strict=True)],
    ),
    (dict(add_references=True), dict(verify=False)),
    param(
        dict(add_references=True),
        dict(verify=True, single_analytic=True),
        marks=[mark.xfail(strict=True)],
    ),
    (dict(add_references=True), dict(verify=False, single_analytic=True)),
    param(
        dict(add_references=True),
        dict(verify=True, single_analytic=False),
        marks=[mark.xfail(strict=True)],
    ),
    (dict(add_references=True), dict(verify=False, single_analytic=False)),
    (dict(add_references=True), dict(single_analytic=False)),
예제 #4
0
class TestPoint:
    @mark.parametrize(
        "x, y",
        [
            (-2.4, 0.8),
            (3, -1),
            param("a", 5, marks=mark.xfail(reason="str")),
            param([0], 5, marks=mark.xfail(reason="list")),
            param(-2, None, marks=mark.xfail(reason="None")),
        ],
    )
    def test_point_init(self, x, y):
        point = Point(x, y)
        assert point and point.x == x and point.y == y

    def test_length_and_dim(self):
        point = Point(0, 0)
        assert len(point) == point.dim == 2

    def test_iteration(self):
        coords = (-2, 4)
        point = Point(*coords)
        for tcoord, pcoord in zip(coords, point):
            assert tcoord == pcoord

    def test_repr_str(self):
        point = Point(-2, 2)
        assert repr(point) == f"Point({point.x}, {point.y})"
        assert str(point) == str((point.x, point.y))

    @mark.parametrize(
        "point1, point2, expected",
        [
            (Point(1, 1), Point(1, 1), True),
            (Point(0, 0), Point(3, 3), False),
        ],
    )
    def test_equality(self, point1, point2, expected):
        assert (point1 == point2) == expected

    @mark.parametrize(
        "string",
        [
            "  0   0  ",
            "	-4.7	-9.1\n",
        ],
    )
    def test_from_str(self, string):
        coords = string.split()
        assert Point.from_string(string) == Point(*coords)

    @mark.parametrize(
        "fname",
        [
            "good_file.txt",
            param("bad_file_invalid_data.txt", marks=mark.xfail),
        ],
    )
    def test_from_file(self, fname):
        fpath = sep.join(["assets", fname])
        with open(fpath) as fl:
            point = Point.from_string(fl.read())
            assert Point.from_file(fpath) == point

    @mark.parametrize(
        "point1, point2, expected",
        [
            (Point(0, 0), Point(2, 0), 2),
            (Point(0, 0), Point(1, 1), sqrt(2)),
            (Point(0, 2), Point(0, -3), 5),
        ],
    )
    def test_dist(self, point1, point2, expected):
        dst1 = point1.dist(point2)
        dst2 = point2.dist(point1)
        assert isclose(dst1, dst2)
        assert isclose(dst1, expected) and isclose(dst2, expected)
예제 #5
0
def make_test_params(value: str, type_: TokenType, xfail: bool = False):
    marks = mark.xfail(strict=True) if xfail else ()
    return param(value, Token(type_=type_, value=value, line_number=1, position=1), marks=marks)
예제 #6
0

@mark.parametrize(
    "reader_kwargs,compress_kwargs",
    [
        (dict(add_references=False), dict()),
        (dict(add_references=False), dict(verify=True)),
        (dict(add_references=False), dict(verify=False)),
        (dict(add_references=False), dict(verify=True, single_analytic=True)),
        (dict(add_references=False), dict(verify=False, single_analytic=True)),
        (dict(add_references=False), dict(verify=True, single_analytic=False)),
        (dict(add_references=False), dict(verify=False, single_analytic=False)),
        (dict(add_references=False), dict(single_analytic=False)),
        (dict(add_references=False), dict(single_analytic=True)),
        (dict(add_references=True), dict()),
        mark.xfail((dict(add_references=True), dict(verify=True)), strict=True),
        (dict(add_references=True), dict(verify=False)),
        mark.xfail((dict(add_references=True), dict(verify=True, single_analytic=True)), strict=True),
        (dict(add_references=True), dict(verify=False, single_analytic=True)),
        mark.xfail((dict(add_references=True), dict(verify=True, single_analytic=False)), strict=True),
        (dict(add_references=True), dict(verify=False, single_analytic=False)),
        (dict(add_references=True), dict(single_analytic=False)),
        (dict(add_references=True), dict(single_analytic=True)),
    ],
)
def test_compress_uuids_api(reader_kwargs, compress_kwargs):
    input_file = "tests/testdata/simple.tar.gz"
    reader = CommunicationReader(input_file, **reader_kwargs)
    it = iter(reader)

    (comm, _) = it.next()
예제 #7
0
class TestFittingMeasurement(MeasurementTests):
    # Test cases:
    # explicit dep/indep
    # check values list vs. fitter

    def _source(self, param):
        ''' return a Constant measurement returning Lorentzian curves '''
        # determine output coordinates
        xs = np.linspace(-5., 5., 100)
        lorentzian = lambda f0, amp=1.: Lorentzian.f(
            xs, f0=f0, df=0.5, offset=0., amplitude=amp)
        if param == '1d':
            # 1d data
            self.f0s = 1.
            data = lorentzian(self.f0s)
            index = pd.Float64Index(xs, name='x')
        elif param == '1d/3pk':
            # 1d data with multiple peaks
            self.f0s = [-3., 0., 3.]
            data = np.zeros_like(xs)
            for f0, amp in zip(self.f0s, [0.5, 1., 0.5]):
                data += lorentzian(f0, amp)
            index = pd.Float64Index(xs, name='x')
        elif (param == '2d>1d') or (param == '2d>1d.T'):
            # 2d data with a singleton dimension
            self.f0s = [0.]
            data = lorentzian(*self.f0s)
            if param == '2d>1d':
                index = pd.MultiIndex.from_product((xs, self.f0s),
                                                   names=['x', 'f'])
            else:
                index = pd.MultiIndex.from_product((self.f0s, xs),
                                                   names=['f', 'x'])
        elif param == '2d':
            # 2d data
            self.f0s = np.linspace(-3., 3., 5)
            data = np.array([lorentzian(f0) for f0 in self.fs]).ravel()
            index = pd.MultiIndex.from_product((xs, self.f0s),
                                               names=['x', 'f'])
        else:
            raise ValueError('Unsupported number of dimensions.')
        frame = pd.DataFrame({'data': data, 'reversed': data[::-1]}, index)
        # generate measurement
        self.npeaks = 1 if np.isscalar(self.f0s) else len(self.f0s)
        return Constant(frame)

    @fixture(params=('1d', '2d>1d', mark.xfail('2d'), '1d/3pk'))
    def source(self, request):
        ''' return a Constant measurement returning Lorentzian curves '''
        return self._source(request.param)

    def _fitter(self, param):
        ''' return different fitters '''
        if param == 'Lorentzian':
            return Lorentzian()
        else:
            return PeakFind(
                peak_args=dict(widths=np.logspace(-2, 0), noise_perc=10))

    @fixture(params=['Lorentzian', 'PeakFind'])
    def fitter(self, request):
        ''' return different fitters '''
        return self._fitter(request.param)

    @fixture(params=[
        '1d-Lorentzian', '2d>1d-Lorentzian',
        mark.xfail('2d-Lorentzian'), '1d-PeakFind', '2d>1d-PeakFind',
        mark.xfail('2d-PeakFind'), '1d/3pk-PeakFind'
    ])
    def measurement(self, request):
        source, fitter = request.param.split('-')
        return FittingMeasurement(self._source(source),
                                  self._fitter(fitter),
                                  plot='')

    def test_fit(self, measurement):
        ''' check that fitting returns the expected parameter values '''
        frame = measurement(output_data=True)
        assert np.all(np.isclose(frame['f0'], self.f0s, atol=0.1))

    def test_fit_zero_results(self):
        frame = pd.DataFrame({'data': np.zeros((50, ))},
                             pd.Index(np.linspace(-5., 5., 50), name='x'))
        source = Constant(frame)
        fitter = self._fitter('PeakFind')
        measurement = FittingMeasurement(source, fitter, plot=None)
        with raises(ContinueIteration):
            measurement()
        measurement.fail_func = lambda: False
        assert measurement(output_data=True) is None

    @mark.parametrize('type', ['str', 'Parameter'])
    def test_dep(self, type):
        source = self._source('1d')
        fitter = self._fitter('Lorentzian')
        if type == 'str':
            dep = 'reversed'
        elif type == 'Parameter':
            dep = source.values[-1]
        with raises(ValueError):
            FittingMeasurement(source, fitter, dep='invalid')
        measurement = FittingMeasurement(source, fitter, dep=dep, plot='')
        frame = measurement(output_data=True)
        assert np.all(np.isclose(frame['f0'], -self.f0s, atol=0.1))

    @mark.parametrize('type', ['str', 'Parameter'])
    def test_indep(self, type):
        #TODO: this should be tested with a 2d measurement
        source = self._source('2d>1d.T')
        fitter = self._fitter('Lorentzian')
        if type == 'str':
            indep = 'x'
        elif type == 'Parameter':
            indep = source.coordinates[-1]
        with raises(ValueError):
            FittingMeasurement(source, fitter, indep='invalid')
        measurement = FittingMeasurement(source, fitter, indep=indep, plot='')
        frame = measurement(output_data=True)
        assert np.all(np.isclose(frame['f0'], self.f0s, atol=0.1))

    @mark.parametrize('method,args', [('init', 'args'), ('init', 'kwargs'),
                                      ('property', 'args')])
    def test_test(self, method, args):
        source = self._source('1d/3pk')
        fitter = self._fitter('PeakFind')
        counter = CountingMeasurement()
        # define test func
        nfit = len(fitter.PARAMETERS)
        if args == 'args':

            def test(xs, ys, p_opt, p_std, p_est):
                # also check args passed to test
                assert xs.shape == ys.shape
                assert len(p_opt) == nfit
                assert len(p_std) == nfit
                assert (len(p_est) == nfit) or (len(p_est) == 0)
                return abs(p_opt[0]) < 1.
        elif args == 'kwargs':

            def test(p_opt, **kwargs):
                return abs(p_opt[0]) < 1

        # assign test function
        if method == 'init':
            with raises(TypeError):
                FittingMeasurement(source, fitter, test=True)
            measurement = FittingMeasurement(source,
                                             fitter,
                                             counter,
                                             test=test,
                                             plot='')
        elif method == 'property':
            measurement = FittingMeasurement(source, fitter, counter, plot='')
            with raises(TypeError):
                measurement.test = True
            measurement.test = test
        # one out of three peaks is selected, so fail_func is not triggered
        frame = measurement(output_data=True)
        assert counter.counter.get() == 0
        assert np.all(frame['fit_ok'].values == [0, 1, 0])

    @mark.parametrize('method', ['init', 'property'])
    def test_fail_func_exception(self, method):
        source = self._source('1d')
        fitter = self._fitter('Lorentzian')
        test = lambda **kwargs: False
        # assign fail_func
        if method == 'init':
            measurement = FittingMeasurement(source,
                                             fitter,
                                             test=test,
                                             plot='',
                                             fail_func=StopIteration)
        elif method == 'property':
            measurement = FittingMeasurement(source,
                                             fitter,
                                             test=test,
                                             plot='')
            measurement.fail_func = StopIteration
        # fail
        with raises(StopIteration):
            measurement()

    def test_fail_func_callable(self):
        source = self._source('1d')
        fitter = self._fitter('Lorentzian')
        test = lambda **kwargs: False
        # build fail_func
        self.fails = 0

        def fail_func(*args):
            self.fails += 1

        measurement = FittingMeasurement(source,
                                         fitter,
                                         test=test,
                                         plot='',
                                         fail_func=fail_func)
        measurement()
        assert self.fails == 1

    def test_parameter_set(self, measurement):
        ''' check setting of .values and popt_out '''
        # add a nested measurement that measures f0 in two different ways
        f0_out = Parameter('f0_out')
        pm = ParameterMeasurement(measurement.values['f0'], f0_out, name='pm')
        measurement.measurements.append(pm)
        measurement.popt_out = {f0_out: 'f0'}
        # run fitter
        store = measurement()
        ref_frame = store['/Fit']
        frame = store['/pm']
        # check for FittingMeasurement.values.set
        assert np.all(frame['f0'].values == ref_frame['f0'])
        # check for popt_out working
        assert np.all(frame['f0_out'].values == ref_frame['f0'])

    @mark.parametrize('method', ['append', 'assign'])
    def test_nested(self, measurement, method):
        ''' check that nested measurements are run '''
        m = CountingMeasurement()
        if method == 'append':
            measurement.measurements.append(m)
        elif method == 'assign':
            measurement.measurements = [m]
        measurement()
        # assert that the nested measurement was called once for each peak
        assert m.counter.get() == self.npeaks - 1

    @mark.parametrize('array, raises', [(False, BreakIteration),
                                        (False, ContinueIteration),
                                        (True, BreakIteration),
                                        (True, ContinueIteration)],
                      ids=['break', 'continue', '(break)', '(continue)'])
    def test_nested_loop_control(self, array, raises):
        ''' raise loop control exception in a nested measurement,
            nested measurement may be a single measurement or an array '''
        m1 = CountingMeasurement(raises=raises, raise_count=1)
        m2 = CountingMeasurement()
        ms = [m1, m2] if array else m1
        fit = FittingMeasurement(self._source('1d/3pk'),
                                 self._fitter('PeakFind'),
                                 ms,
                                 plot='')
        fit()
        count = 1 if (raises == BreakIteration) else (self.npeaks - 1)
        assert m1.counter.get() == count
        if array:
            assert m2.counter.get() == count - 1

    def test_plot(self, measurement, monkeypatch):
        if type(measurement.fitter).__name__ == 'PeakFind':
            skip()
            return
        monkeypatch.setattr(config, 'store', 'CSVStore')
        measurement.plot_format = 'png'
        store = measurement()
        frame = store['/Fit']
        assert list(frame['plot'].values) == [1]
        assert os.path.isfile(store.filename('/plot_1', '.png'))
예제 #8
0
class BasicElliprTest(AllDtypeTest, BaseCasesTest):
    @mark.parametrize('ours, theirs, nargs', ELLIPR_FUNCMAP)
    @given(*4 * (_complex_number, ))
    def test_values(self, ours, theirs, nargs, x, y, z, p):
        args = (x, y, z, p)[:nargs]
        assert nice_and_close(ours(*args), theirs(*args))

    # From Carlson (1995) except where stated otherwise
    test_case = BaseCasesTest.parametrize({
        elliprc: (
            ((0, 1 / 4), pi),
            ((9 / 4, 2), log(2)),
            ((0, 1j), (1 - 1j) * 1.1107207345396),
            ((-1j, 1j), 1.2260849569072 - 0.34471136988768j),
            ((0.25, -2), log(2) / 3),
            ((1j, -1), 0.77778596920447 + 0.19832484993429j),
        ),
        elliprd: (
            ((0, 2, 1), 3 * gamma(3 / 4)**2 /
             (2 * pi)**0.5),  # https://dlmf.nist.gov/19.20.E22
            ((2, 3, 4), 0.16510527294261),
            ((1j, -1j, 2), 0.65933854154220),
            ((0, 1j, -1j), 1.2708196271910 + 2.7811120159521j),
            ((0, 1j - 1, 1j), -1.8577235439239 - 0.96193450888839j),
            ((-2 - 1j, -1j, -1 + 1j), 1.8249027393704 - 1.2218475784827j),
        ),
        elliprf: (
            ((1, 2, 0), gamma(1 / 4)**2 / 4 /
             (2 * pi)**0.5),  # https://dlmf.nist.gov/19.20.E2
            ((0.5, 1, 0), 1.8540746773014),
            ((1j, -1j, 0), 1.8540746773014),
            ((1j - 1, 1j, 0), 0.79612586584234 - 1.2138566698365j),
            ((2, 3, 4), 0.58408284167715),
            ((1j, -1j, 2), 1.0441445654064),
            ((1j - 1, 1j, 1 - 1j), 0.93912050218619 - 0.53296252018635j),
        ),
        elliprg: (
            ((0, 16, 16), pi),
            ((2, 3, 4), 1.7255030280692),
            ((0, 1j, -1j), 0.42360654239699),
            ((1j - 1, 1j, 0), 0.44660591677018 + 0.70768352357515j),
            ((-1j, 1j - 1, 1j), 0.36023392184473 + 0.40348623401722j),
            ((0, 0.0796, 4), 1.0284758090288),
            # mpmath:
            ((0, 0, 0), 0),
            ((0, 0, 16), 2),
            ((1, 4, 0), 1.2110560275684595248036),
            ((1, 1j, -1 + 1j),
             0.64139146875812627545 + 0.58085463774808290907j),
        ),
        elliprj: (
            ((0, 1, 2, 3), 0.77688623778582),
            ((2, 3, 4, 5), 0.14297579667157),
            ((2, 3, 4, -1 + 1j), 0.13613945827771 - 0.38207561624427j),
            ((1j, -1j, 0, 2), 1.6490011662711),
            ((-1 + 1j, -1 - 1j, 1, 2), 0.94148358841220),
            ((1j, -1j, 0, 1 - 1j), 1.8260115229009 + 1.2290661908643j),
            ((-1 + 1j, -1 - 1j, 1, -3 + 1j),
             -0.61127970812028 - 1.0684038390007j),
            ((-1 + 1j, -2 - 1j, -1j, -1 + 1j),
             1.8249027393704 - 1.2218475784827j),
            # computed using mpmath; see comment in C95
            ((2, 3, 4, -0.5), 0.24723819703052 - 0.7509842836890j),
            ((2, 3, 4, -5), -0.12711230042964 - 0.2099064885453j),
        )
    })

    # from mpmath; Carlson's algorithm is not guaranteed, so mpmath integrates numerically
    # for now we can't handle these cases although for all but the first two cases
    # the algorith is correct
    cases_elliprj_fail = (
        ((-1 - 0.5j, -10 - 6j, -10 - 3j, -5 + 10j),
         0.128470516743927699 + 0.102175950778504625j),  # fails
        ((1.987, 4.463 - 1.614j, 0, -3.965),
         -0.341575118513811305 - 0.394703757004268486j),  # fails
        ((0.3068, -4.037 + 0.632j, 1.654, -0.9609),
         -1.14735199581485639 - 0.134450158867472264j),
        ((0.3068, -4.037 - 0.632j, 1.654, -0.9609),
         1.758765901861727 - 0.161002343366626892j),
        ((0.3068, -4.037 + 0.0632j, 1.654, -0.9609),
         -1.17157627949475577 - 0.069182614173988811j),
        ((0.3068, -4.037 + 0.00632j, 1.654, -0.9609),
         -1.17337595670549633 - 0.0623069224526925j),
        ((0.3068, -4.037 - 0.0632j, 1.654, -0.9609),
         1.77940452391261626 + 0.0388711305592447234j),
        ((0.3068, -4.037 - 0.00632j, 1.654, -0.9609),
         1.77806722756403055 + 0.0592749824572262329j))

    def test_elliprj_fail(self):
        assert elliprj(*torch.tensor(next(zip(
            *self.cases_elliprj_fail))).T).real.isnan().all()

    @given(*3 * _complex_numbers)
    def test_definitions(self, x, y, z):
        assert close(elliprc(x, y), elliprf(x, y, y))
        assert close(elliprd(x, y, z), elliprj(x, y, z, z))

    @mark.parametrize(
        'func, nsym, nadd',
        ((elliprf, 3, 0),
         param(elliprg,
               3,
               0,
               marks=mark.xfail(
                   reason='implementation not symmetric, fails for floats')),
         (elliprd, 2, 1), (elliprj, 3, 1)))
    def test_symmetry(self, func, nsym, nadd):
        @given(st.tuples(*(nsym + nadd) * _complex_numbers))
        def _test(args: torch.Tensor):
            assert all(
                close(f1, f2) for f1, f2 in combinations((
                    func(*arg, *args[nsym:])
                    for arg in permutations(args[:nsym])), 2))

        _test()
예제 #9
0
    (
        "ixmp://example/m/s#42",
        dict(name="example"),
        dict(model="m", scenario="s", version=42),
    ),
    ("ixmp://example/m/s", dict(name="example"), m_s),
    ("ixmp://local/m/s", dict(name="local"), m_s),
    (
        "ixmp://local/m/s/foo/bar",
        dict(name="local"),
        dict(model="m", scenario="s/foo/bar"),
    ),
    ("m/s#42", dict(), dict(model="m", scenario="s", version=42)),
    # Invalid values
    # Wrong scheme
    param("foo://example/m/s", None, None, marks=mark.xfail(raises=ValueError)),
    # No Scenario name
    param("ixmp://example/m", None, None, marks=mark.xfail(raises=ValueError)),
    # Version not an integer
    param(
        "ixmp://example/m#notaversion", None, None, marks=mark.xfail(raises=ValueError)
    ),
    # Query string not supported
    param(
        "ixmp://example/m/s?querystring",
        None,
        None,
        marks=mark.xfail(raises=ValueError),
    ),
]
예제 #10
0
    assert np.shape(K.RA) == np.shape(times)
    assert np.shape(K.Dec) == np.shape(times)
    assert np.shape(K.times) == np.shape(times)
    # Test that K.RA, K.Dec, K.times and K.obs_code have expected values.
    expect_RADec = _radec_interp(times, _radec_from_file(object_name, obs_code))
    assert np.all(np.isclose(K.RA, expect_RADec[0], atol=0.00000001, rtol=0))
    assert np.all(np.isclose(K.Dec, expect_RADec[1], atol=0.00000001, rtol=0))
    assert np.all(K.times == times)
    assert K.obs_code == obs_code
    print('\t Completed test_instantiate_with_object_name.')


names_of_variables = ('orbit', 'obs_code', 'times')
values_for_each_test = [
    pytest.param(aeiOoME_sedna, '568', all_times,
                 marks=mark.xfail(reason='Functionality not implemented.')),
 ]
@pytest.mark.parametrize(names_of_variables, values_for_each_test)
def test__get_orbit_RADEC(orbit, obs_code, times):
    '''
    Test the private method for getting RA/Dec from an orbit.
    '''
    K = known.Known()
    K._get_orbit_RADEC(obs_code=obs_code, times=times, orbit=orbit)
    # Test that K.RA, K.Dec exist and have expected type.
    assert isinstance(K.RA, np.ndarray)
    assert isinstance(K.Dec, np.ndarray)
    # Test that K.RA, K.Dec have expected shape.
    assert np.shape(K.RA) == np.shape(times)
    assert np.shape(K.Dec) == np.shape(times)
    # Test that K.RA, K.Dec have expected values.
예제 #11
0
def xfail_known_issues(request, known_issue: str):
    if known_issue:
        request.applymarker(mark.xfail(reason=known_issue))
예제 #12
0
class TestNormalize(MeasurementTests):
    @fixture(params=[(False, False), (True, False), (False, True),
                     mark.xfail((True, True))],
             ids=['X', '_X', 'X_', '_X_'])
    def source(self, request):
        # one to three index levels with the segment level in different positions
        prefix, suffix = request.param
        levels = []
        if prefix:
            levels.append(('x', list(range(2))))
        levels.append(('segment', list(range(10))))
        if suffix:
            levels.append(('y', list(range(3))))
        names, levels = zip(*levels)
        index = pd.MultiIndex.from_product(levels, names=names)
        segment_labels = index.get_level_values('segment').values
        data = np.concatenate(([-1, 1], np.linspace(-1, 1, 8)))[segment_labels]
        frame = pd.DataFrame({'data': data}, index)
        return Constant(frame)

    @fixture
    def measurement(self, source):
        return NormalizeAWG(source)

    @mark.parametrize('g_value,e_value', [(-1, 1), (0, 1)],
                      ids=['(-1, 1)', '(0, 1)'])
    def test_return(self, source, g_value, e_value):
        reference = np.linspace(g_value, e_value, 8)
        measurement = NormalizeAWG(source, g_value=g_value, e_value=e_value)
        series = measurement(output_data=True)['data']
        if series.index.nlevels > 1:
            series = series.unstack('segment')
        assert np.all(reference == series.values)

    def test_drop_cal(self, source):
        reference = np.concatenate(([-1, 1], np.linspace(-1, 1, 8)))
        measurement = NormalizeAWG(source, drop_cal=False)
        series = measurement(output_data=True)['data']
        if series.index.nlevels > 1:
            series = series.unstack('segment')
        assert np.all(reference == series.values)

    def test_source_attr(self, source):
        measurement = NormalizeAWG()
        measurement.source = source
        assert measurement.coordinates == source.coordinates
        assert measurement.values == source.values
        measurement()

    @mark.parametrize('chpair', [0, 1])
    def test_chpair(self, chpair):
        measurement = NormalizeAWG(chpair=chpair)
        seq = measurement.template_func()
        assert len(seq.sequences[2 * chpair].segments) == 2

    def test_gepulses_list(self):
        measurement = NormalizeAWG(g_pulses=[pulsegen.pix],
                                   e_pulses=[pulsegen.piy],
                                   chpair=0)
        seq = measurement.template_func()
        seq.sample()
        # pix is in I, and piy in Q
        sseqs = seq.sampled_sequences
        assert np.allclose(sseqs[0].waveforms[0], -sseqs[1].waveforms[1])
        assert np.allclose(sseqs[1].waveforms[0], sseqs[0].waveforms[1])

    def test_gepulses_dict(self):
        measurement = NormalizeAWG(g_pulses={
            0: [pulsegen.pix],
            1: [pulsegen.piy]
        },
                                   e_pulses={
                                       0: [pulsegen.piy],
                                       1: [pulsegen.pix]
                                   })
        seq = measurement.template_func()
        seq.sample()
        sseqs = seq.sampled_sequences
        assert np.allclose(sseqs[0].waveforms[0], -sseqs[1].waveforms[1])
        assert np.allclose(sseqs[1].waveforms[0], sseqs[0].waveforms[1])
예제 #13
0
@mark.parametrize("factory", [
    TextMessageFactory(),
    LocationMessageFactory(),
    LiveLocationMessageFactory(),
    ContactMessageFactory(),
    StickerMessageFactory(),
    DocumentMessageFactory(),
    PhotoMessageFactory(),
    VoiceMessageFactory(),
    AudioMessageFactory(),
    VideoMessageFactory(),
    VideoNoteMessageFactory(),
    AnimationMessageFactory(),
    param(DiceMessageFactory(),
          marks=mark.xfail(
              reason="Telethon has not uploaded the feature to PyPI."))
],
                  ids=str)
async def test_master_message(helper, client, bot_group, slave, channel,
                              factory: MessageFactory):
    chat = slave.chat_without_alias

    with link_chats(channel, (chat, ), bot_group):
        # Send message
        tg_msg = await factory.send_message(client, bot_group)
        efb_msg = slave.messages.get(timeout=5)
        assert efb_msg.chat == chat
        assert isinstance(efb_msg.author, SelfChatMember)
        assert efb_msg.deliver_to is slave
        assert not efb_msg.edit
        assert not efb_msg.edit_media
예제 #14
0
class TestApply(MeasurementTests):
    @fixture(params=[
        'scalar', 'vector', 'matrix', 'matrix_complex', '3darray',
        'matrix_transpose', '3darray_transpose'
    ])
    def frame(self, request):
        return frame_factory(request.param)

    def function(self, arg0, arg1):
        return arg0 + arg1

    def arg_factory(self, arg, type_):
        if type_ == 'measurement':
            return Constant(arg)
        elif type_ == 'parameter':
            return Parameter('arg', value=arg)
        elif type_ == 'const':
            return arg

    def measurement_factory(self,
                            arg0,
                            arg1,
                            type0='measurement',
                            type1='measurement',
                            **kwargs):
        return Apply(self.function, self.arg_factory(arg0, type0),
                     self.arg_factory(arg1, type1), **kwargs)

    @fixture
    def measurement(self, frame):
        self.arg0 = frame
        self.arg1 = frame
        return self.measurement_factory(frame, frame)

    @mark.parametrize('type0,type1', [('measurement', 'parameter'),
                                      ('measurement', 'const'),
                                      ('parameter', 'measurement'),
                                      ('const', 'measurement'),
                                      ('const', 'const')])
    def test_type(self, type0, type1):
        arg0 = frame_factory('vector')
        arg1 = frame_factory('vector')
        m = self.measurement_factory(arg0, arg1, type0, type1)
        frame = m(output_data=True)
        function_values = self.function(arg0.values, arg1.values)
        assert np.all(frame.values == function_values)

    def test_scalar(self):
        arg0 = frame_factory('vector')
        arg1 = 2.
        m = self.measurement_factory(arg0, arg1, 'measurement', 'const')
        frame = m(output_data=True)
        function_values = self.function(arg0.values, arg1)
        assert np.all(frame.values == function_values)

    @mark.parametrize('shape', ['scalar', 'vector', 'matrix', '3darray'])
    def test_same(self, shape):
        arg0 = frame_factory(shape)
        arg1 = frame_factory(shape)
        frame = self.measurement_factory(arg0, arg1)(output_data=True)
        function_values = self.function(arg0.values, arg1.values)
        assert np.all(frame.values == function_values)

    @mark.xfail
    @mark.parametrize('shape', ['matrix', '3darray'])
    def test_transpose(self, shape):
        arg0 = frame_factory(shape)
        arg1 = frame_factory(shape + '_transpose')
        frame = self.measurement_factory(arg0, arg1)(output_data=True)
        return  # broken in pd, remove on XPASS
        function_values = self.function(arg0.values, arg0.values)
        assert np.all(frame.values == function_values)

    @mark.parametrize('shape0,shape1', [('vector', 'scalar'),
                                        ('matrix', 'scalar'),
                                        ('3darray', 'scalar'),
                                        ('matrix', 'vector'),
                                        ('matrix_transpose', 'vector'),
                                        ('3darray', 'vector'),
                                        ('3darray_transpose', 'vector'),
                                        ('matrix', 'matrix_singleton'),
                                        mark.xfail(('3darray', 'matrix'))])
    def test_broadcast(self, shape0, shape1):
        arg0 = frame_factory(shape0)
        arg1 = frame_factory(shape1)
        frame = self.measurement_factory(arg0, arg1)(output_data=True)
        if arg0.index.nlevels == 1:
            assert frame.index.shape == arg0.index.shape
        else:
            assert frame.index.levshape == arg0.index.levshape
        assert not np.any(np.isnan(frame.values)), 'Result has NaNs'
예제 #15
0
    param(
        {
            'name': 'Czech Republic',
            'code': 'CZ',
            'timezone': 'Europe/Prague',
        },
        id='official_name',
    ),
    param(
        {
            'name': 'United States',
            'code': 'CZ',
            'timezone': 'Europe/Prague',
        },
        id='ambiguous',
        marks=mark.xfail(
            raises=ValueError,
            reason='country is ambiguous',
        ),
    )
])
def test_parse_country_strict(data):
    expected_keys = {
        'country',
        'timezone',
    }
    parsed = parsers.parse_country(data, strict=True)
    assert not expected_keys.symmetric_difference(parsed.keys())
    assert isinstance(parsed['country'], pycountry.db.Data)
    assert isinstance(parsed['timezone'], datetime.tzinfo)
예제 #16
0
m_s = dict(model='m', scenario='s')

URLS = [
    ('ixmp://example/m/s', dict(name='example'), m_s),
    ('ixmp://example/m/s#42', dict(name='example'),
     dict(model='m', scenario='s', version=42)),
    ('ixmp://example/m/s', dict(name='example'), m_s),
    ('ixmp://local/m/s', dict(name='local'), m_s),
    ('ixmp://local/m/s/foo/bar', dict(name='local'),
     dict(model='m', scenario='s/foo/bar')),
    ('m/s#42', dict(), dict(model='m', scenario='s', version=42)),

    # Invalid values
    # Wrong scheme
    param('foo://example/m/s', None, None,
          marks=mark.xfail(raises=ValueError)),
    # No Scenario name
    param('ixmp://example/m', None, None, marks=mark.xfail(raises=ValueError)),
    # Version not an integer
    param('ixmp://example/m#notaversion',
          None,
          None,
          marks=mark.xfail(raises=ValueError)),
    # Query string not supported
    param('ixmp://example/m/s?querystring',
          None,
          None,
          marks=mark.xfail(raises=ValueError)),
]

예제 #17
0
@fixture(params=[True, False], scope='module')
def standardize(request):
    return request.param


@fixture(params=['subset1', None, 'phenotype: group1', '~subset1', 'ids'],
         scope='module')
def sample_subset(request, samples):
    if request.param == 'ids':
        return samples[:10]
    else:
        return request.param


@fixture(params=[None, 'all', 'gene_category: A', 'W',
                 mark.xfail('asdf')],
         ids=[
             'none', 'all_features', 'categorical_gene_category',
             'boolean_gene_category', 'nonexistent_gene_category'
         ],
         scope='module')
def feature_subset(request):
    return request.param


@fixture(scope='module')
def splicing(splicing_data):
    from flotilla.data_model.splicing import SplicingData
    return SplicingData(splicing_data)

예제 #18
0
        assert False


@mark.parametrize('reader_kwargs,compress_kwargs', [
    (dict(add_references=False), dict()),
    (dict(add_references=False), dict(verify=True)),
    (dict(add_references=False), dict(verify=False)),
    (dict(add_references=False), dict(verify=True, single_analytic=True)),
    (dict(add_references=False), dict(verify=False, single_analytic=True)),
    (dict(add_references=False), dict(verify=True, single_analytic=False)),
    (dict(add_references=False), dict(verify=False, single_analytic=False)),
    (dict(add_references=False), dict(single_analytic=False)),
    (dict(add_references=False), dict(single_analytic=True)),
    (dict(add_references=True), dict()),
    mark.xfail(
        (dict(add_references=True), dict(verify=True)),
        strict=True,
    ),
    (dict(add_references=True), dict(verify=False)),
    mark.xfail(
        (dict(add_references=True), dict(verify=True, single_analytic=True)),
        strict=True,
    ),
    (dict(add_references=True), dict(verify=False, single_analytic=True)),
    mark.xfail(
        (dict(add_references=True), dict(verify=True, single_analytic=False)),
        strict=True,
    ),
    (dict(add_references=True), dict(verify=False, single_analytic=False)),
    (dict(add_references=True), dict(single_analytic=False)),
    (dict(add_references=True), dict(single_analytic=True)),
])
예제 #19
0
                  (10,14,15,1),
                  (10,15,15,1)
    )
    voxels = [Voxel(x,y,z, E) for (x,y,z,E) in voxel_spec]
    tracks  = make_track_graphs(voxels, np.array([1,1,1]), contiguity=1.85)

    assert len(tracks) == 1
    track_length = length(tracks[0])

    expected_length = 8 + np.sqrt(2)

    assert track_length == approx(expected_length)


@parametrize('contiguity, expected_length',
             (mark.xfail((1.2, 4), reason='contiguity is broken'),
              (1.5, 2 * sqrt(2))))
def test_length_around_bend(contiguity, expected_length):
    # Make sure that we calculate the length along the track rather
    # that the shortcut
    voxel_spec = ((0,0,0, 1),
                  (1,0,0, 1),
                  (1,1,0, 1),
                  (1,2,0, 1),
                  (0,2,0, 1))
    voxels = list(starmap(Voxel, voxel_spec))
    tracks = make_track_graphs(voxels, np.array([1,1,1]), contiguity=contiguity)
    assert len(tracks) == 1
    track_length = length(tracks[0])
    assert track_length == approx(expected_length)
예제 #20
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""DataFork, Junior Data Engineer, Question 2."""

__author__ = "Stanislav D. Kudriavtsev"

from pytest import mark, param

from question2 import solve


@mark.parametrize(
    "shops, visitors, ranges, ans",
    [(6, 5, [(3, 5), (2, 3), (4, 6), (1, 6), (5, 6)], {3, 4, 5}),
     (1, 1, [(1, 1)], {1}),
     param(1, 2, [(1, 1), (1, 2)], 1, marks=mark.xfail(reason='range(1, 2)'))])
def test_case1(shops, visitors, ranges, ans):
    """
    Test case from question2.py module docstring.

    Parameters
    ----------
    shops : int.
    visitors : int.
    ranges : List
        the range of visited shops (inclusive).
    ans : set
        expected answer.

    Returns
    -------
예제 #21
0
파일: conftest.py 프로젝트: moloney/dcm
    first = sp_out.split("\n")[0]
    return tuple(int(x) for x in re.match(DCMTK_VER_RE, first).groups())


DCMTK_VERSION = get_dcmtk_version()

dcmtk_base_port = 62760

dcmtk_base_name = "DCMTKAE"

has_dcmtk = mark.skipif(DCMTK_VERSION is None,
                        reason="can't find DCMTK command 'dcmqrscp'")

dcmtk_priv_sop_retr_xfail = mark.xfail(
    DCMTK_VERSION is None or DCMTK_VERSION < DCMTK_PRIV_RETR_VERS,
    reason="dcmqrscp version doesn't support retrieving private "
    "SOPClasses",
)

dcmtk_priv_sop_send_xfail = mark.xfail(
    DCMTK_VERSION is None or DCMTK_VERSION < DCMTK_PRIV_SEND_VERS,
    reason="dcmqrscp version doesn't support sending private "
    "SOPClasses",
)

dcmtk_config_tmpl = """\
NetworkTCPPort  = {dcmtk_node.port}
MaxPDUSize      = 16384
MaxAssociations = 16

HostTable BEGIN
예제 #22
0
m_s = dict(model='m', scenario='s')

URLS = [
    ('ixmp://example/m/s', dict(name='example'), m_s),
    ('ixmp://example/m/s#42', dict(name='example'),
     dict(model='m', scenario='s', version=42)),
    ('ixmp://example/m/s', dict(name='example'), m_s),
    ('ixmp://local/m/s', dict(name='local'), m_s),
    ('ixmp://local/m/s/foo/bar', dict(name='local'),
     dict(model='m', scenario='s/foo/bar')),
    ('m/s#42', dict(), dict(model='m', scenario='s', version=42)),

    # Invalid values
    # Wrong scheme
    param('foo://example/m/s', None, None,
          marks=mark.xfail(raises=ValueError)),
    # No Scenario name
    param('ixmp://example/m', None, None,
          marks=mark.xfail(raises=ValueError)),
    # Version not an integer
    param('ixmp://example/m#notaversion', None, None,
          marks=mark.xfail(raises=ValueError)),
    # Query string not supported
    param('ixmp://example/m/s?querystring', None, None,
          marks=mark.xfail(raises=ValueError)),
]


@pytest.mark.parametrize('url, p, s', URLS)
def test_parse_url(url, p, s):
    platform_info, scenario_info = utils.parse_url(url)
예제 #23
0
    ["0x12 345 678 901 234 567 890", 0x12345678901234567890],
]

dec = [
    ["1234", 1234],
    ["12345678901234567890", 12345678901234567890],
    ["1 234", 1234],
    ["12 345 678 901 234 567 890", 12345678901234567890],
]

idn = [
    "abc",
    "ab0",
    "ab_",
    "a0_",
    mark.xfail("0bc"),
    mark.xfail("!ab"),
    mark.xfail("_ab"),
]

exp = [
    ["1", 1],
    ["a", "a"],
    ["1 @ b", [1, "@", "b"]],
    ["1 * b", [1, "*", "b"]],
    ["1 / b", [1, "/", "b"]],
    ["1 + b", [1, "+", "b"]],
    ["1 - b", [1, "-", "b"]],
    ["1 @ b @ 3", [1, "@", ["b", "@", 3]]],
    ["1 @ b * 3", [[1, "@", "b"], "*", 3]],
    ["1 @ b / 3", [[1, "@", "b"], "/", 3]],
예제 #24
0
def standardize(request):
    return request.param


@fixture(
    params=['subset1', None, 'phenotype: group1', '~subset1', 'ids'],
    scope='module')
def sample_subset(request, samples):
    if request.param == 'ids':
        return samples[:10]
    else:
        return request.param


@fixture(
    params=[None, 'all', 'gene_category: A', 'W', mark.xfail('asdf')],
    ids=['none', 'all_features', 'categorical_gene_category',
         'boolean_gene_category', 'nonexistent_gene_category'],
    scope='module')
def feature_subset(request):
    return request.param


@fixture(scope='module')
def splicing(splicing_data):
    from flotilla.data_model.splicing import SplicingData
    return SplicingData(splicing_data)


@fixture(scope='module')
def gene_ontology_data_path(data_dir):
예제 #25
0
from operator import itemgetter, attrgetter
from functools import reduce
from argparse import Namespace
from copy import copy

import itertools as it

from pytest import mark, raises
xfail = mark.xfail
TODO = mark.xfail(reason='TODO')
GETITEM_FUNDAMENTALLY_BROKEN = xfail(
    reason="__getitem__ can't distinguish x[a,b] from x[(a,b)]")
parametrize = mark.parametrize

from hypothesis import given
from hypothesis import assume
from hypothesis.strategies import tuples
from hypothesis.strategies import integers
from hypothesis.strategies import none
from hypothesis.strategies import one_of, sampled_from

from testhelpers import *

###################################################################


def test_map():
    from liquidata import pipe
    data = list(range(10))
    f, = symbolic_functions('f')
    assert pipe(f)(data) == list(map(f, data))
예제 #26
0
    x = f(*args)
    y = wrap(f)(*args)
    assert x - y < delta and type(x) == type(y)


def make_exc_test(f, args, py_exc, stella_exc):
    with raises(py_exc):
        x = f(*args)  # noqa

    with raises(stella_exc):
        y = wrap(f)(*args)  # noqa

    assert True


unimplemented = mark.xfail(reason="Unimplemented", run=False)
bench = mark.bench


@pytest.fixture
def bench_opt(request):
    opt = request.config.getoption("--bench")
    if opt in ('l', 'long'):
        return 2
    elif opt in ('s', 'short'):
        return 1
    else:
        return 0


@pytest.fixture
예제 #27
0
    async def finalize_message(self, tg_msg: Message, efb_msg: EFBMessage):
        if efb_msg.file and not efb_msg.file.closed:
            efb_msg.file.close()


# endregion Message factory classes


@mark.parametrize("factory", [
    TextMessageFactory(),
    LocationMessageFactory(),
    LiveLocationMessageFactory(),
    ContactMessageFactory(),
    StickerMessageFactory(),
    param(DocumentMessageFactory(),
          marks=mark.xfail(reason="PTB does not support Bot API 4.5")),
    param(PhotoMessageFactory(),
          marks=mark.xfail(reason="PTB does not support Bot API 4.5")),
    param(VoiceMessageFactory(),
          marks=mark.xfail(reason="PTB does not support Bot API 4.5")),
    param(AudioMessageFactory(),
          marks=mark.xfail(reason="PTB does not support Bot API 4.5")),
    param(VideoMessageFactory(),
          marks=mark.xfail(reason="PTB does not support Bot API 4.5")),
    param(VideoNoteMessageFactory(),
          marks=mark.xfail(reason="PTB does not support Bot API 4.5")),
    param(AnimationMessageFactory(),
          marks=mark.xfail(reason="PTB does not support Bot API 4.5"))
],
                  ids=str)
async def test_master_message(helper, client, bot_group, slave, channel,
예제 #28
0
@mark.parametrize("tst,exp", [
    ["field 2 @ 255 - 19;", f"field {LIMIT};"],
])
def test_limit_condenser(tst, exp):
    t = {"limit": ast.FLD.parseString(tst).asList()[0]}
    e = {"limit": ast.FLD.parseString(exp).asList()[0]}
    LimitCondenser()(t)
    assert t == e


@mark.parametrize("tst,exp", [
    ["a = 5;", "a = 5;"],
    ["a = 5 + 5;", "a = 10;"],
    ["a = 5; b = a + 5;", "a = 5; b = 10;"],
    [f"c = {LIMIT-1} + 3;", "c = 2;"],
    mark.xfail(["a = b;", "a = b;"]),
    mark.xfail(["b = a + 5;", "b = 10;"]),
])
def test_constants_condenser(tst, exp):
    t = {"limit": LIMIT, "items": ast.BDY.parseString(tst).asList()[0]}
    e = {"limit": LIMIT, "items": ast.BDY.parseString(exp).asList()[0]}
    ConstantCondenser()(t)
    assert t == e


@mark.parametrize("tst,exp", [
    [
        "a = 5; foo(x, y)(z) { z = x + y; }",
        "a = 5; foo(x, y)(z) { z = x + y; }"
    ],
    [
예제 #29
0
    x = f(*args)
    y = wrap(f)(*args)
    assert x - y < delta and type(x) == type(y)


def make_exc_test(f, args, py_exc, stella_exc):
    with raises(py_exc):
        x = f(*args)  # noqa

    with raises(stella_exc):
        y = wrap(f)(*args)  # noqa

    assert True


unimplemented = mark.xfail(reason="Unimplemented", run=False)
bench = mark.bench


@pytest.fixture
def bench_opt(request):
    opt = request.config.getoption("--bench")
    if opt in ('l', 'long'):
        return 2
    elif opt in ('s', 'short'):
        return 1
    else:
        return 0


@pytest.fixture