Example #1
0
def test_capture():

    table = (('id', 'variable', 'value'), ('1', 'A1', '12'), ('2', 'A2', '15'),
             ('3', 'B1', '18'), ('4', 'C12', '19'))

    expectation = (('id', 'value', 'treat', 'time'), ('1', '12', 'A', '1'),
                   ('2', '15', 'A', '2'), ('3', '18', 'B', '1'), ('4', '19',
                                                                  'C', '12'))

    result = capture(table, 'variable', '(\\w)(\\d+)', ('treat', 'time'))
    ieq(expectation, result)
    result = capture(table,
                     'variable',
                     '(\\w)(\\d+)', ('treat', 'time'),
                     include_original=False)
    ieq(expectation, result)

    # what about including the original field?
    expectation = (('id', 'variable', 'value', 'treat', 'time'),
                   ('1', 'A1', '12', 'A', '1'), ('2', 'A2', '15', 'A', '2'),
                   ('3', 'B1', '18', 'B', '1'), ('4', 'C12', '19', 'C', '12'))
    result = capture(table,
                     'variable',
                     '(\\w)(\\d+)', ('treat', 'time'),
                     include_original=True)
    ieq(expectation, result)

    # what about if number of captured groups is different from new fields?
    expectation = (('id', 'value'), ('1', '12', 'A', '1'),
                   ('2', '15', 'A', '2'), ('3', '18', 'B', '1'), ('4', '19',
                                                                  'C', '12'))
    result = capture(table, 'variable', '(\\w)(\\d+)')
    ieq(expectation, result)
Example #2
0
def test_capture_nonmatching():

    table = (('id', 'variable', 'value'), ('1', 'A1', '12'), ('2', 'A2', '15'),
             ('3', 'B1', '18'), ('4', 'C12', '19'))

    expectation = (('id', 'value', 'treat', 'time'), ('1', '12', 'A', '1'),
                   ('2', '15', 'A', '2'), ('3', '18', 'B', '1'))

    # default behaviour, raise exception
    result = capture(table, 'variable', r'([A-B])(\d+)', ('treat', 'time'))
    it = iter(result)
    eq_(expectation[0], it.next())  # header
    eq_(expectation[1], it.next())
    eq_(expectation[2], it.next())
    eq_(expectation[3], it.next())
    try:
        it.next()  # doesn't match
    except TransformError:
        pass  # expected
    else:
        assert False, 'expected exception'

    # explicit fill
    result = capture(table,
                     'variable',
                     r'([A-B])(\d+)',
                     newfields=('treat', 'time'),
                     fill=['', 0])
    it = iter(result)
    eq_(expectation[0], it.next())  # header
    eq_(expectation[1], it.next())
    eq_(expectation[2], it.next())
    eq_(expectation[3], it.next())
    eq_(('4', '19', '', 0), it.next())
Example #3
0
def test_melt_and_capture():

    table = (('id', 'parad0', 'parad1', 'parad2'), ('1', '12', '34', '56'),
             ('2', '23', '45', '67'))

    expectation = (('id', 'parasitaemia', 'day'), ('1', '12', '0'),
                   ('1', '34', '1'), ('1', '56', '2'), ('2', '23', '0'),
                   ('2', '45', '1'), ('2', '67', '2'))

    step1 = melt(table, key='id', valuefield='parasitaemia')
    step2 = capture(step1, 'variable', 'parad(\\d+)', ('day', ))
    ieq(expectation, step2)
Example #4
0
def test_capture():

    table = (('id', 'variable', 'value'),
            ('1', 'A1', '12'),
            ('2', 'A2', '15'),
            ('3', 'B1', '18'),
            ('4', 'C12', '19'))

    expectation = (('id', 'value', 'treat', 'time'),
                   ('1', '12', 'A', '1'),
                   ('2', '15', 'A', '2'),
                   ('3', '18', 'B', '1'),
                   ('4', '19', 'C', '12'))

    result = capture(table, 'variable', '(\\w)(\\d+)', ('treat', 'time'))
    ieq(expectation, result)
    result = capture(table, 'variable', '(\\w)(\\d+)', ('treat', 'time'),
                     include_original=False)
    ieq(expectation, result)

    # what about including the original field?
    expectation = (('id', 'variable', 'value', 'treat', 'time'),
                   ('1', 'A1', '12', 'A', '1'),
                   ('2', 'A2', '15', 'A', '2'),
                   ('3', 'B1', '18', 'B', '1'),
                   ('4', 'C12', '19', 'C', '12'))
    result = capture(table, 'variable', '(\\w)(\\d+)', ('treat', 'time'),
                     include_original=True)
    ieq(expectation, result)

    # what about if number of captured groups is different from new fields?
    expectation = (('id', 'value'),
                   ('1', '12', 'A', '1'),
                   ('2', '15', 'A', '2'),
                   ('3', '18', 'B', '1'),
                   ('4', '19', 'C', '12'))
    result = capture(table, 'variable', '(\\w)(\\d+)')
    ieq(expectation, result)
Example #5
0
def test_capture_nonmatching():

    table = (('id', 'variable', 'value'),
             ('1', 'A1', '12'),
             ('2', 'A2', '15'),
             ('3', 'B1', '18'),
             ('4', 'C12', '19'))

    expectation = (('id', 'value', 'treat', 'time'),
                   ('1', '12', 'A', '1'),
                   ('2', '15', 'A', '2'),
                   ('3', '18', 'B', '1'))

    # default behaviour, raise exception
    result = capture(table, 'variable', r'([A-B])(\d+)', ('treat', 'time'))
    it = iter(result)
    eq_(expectation[0], it.next())  # header
    eq_(expectation[1], it.next())
    eq_(expectation[2], it.next())
    eq_(expectation[3], it.next())
    try:
        it.next()  # doesn't match
    except TransformError:
        pass  # expected
    else:
        assert False, 'expected exception'

    # explicit fill
    result = capture(table, 'variable', r'([A-B])(\d+)',
                     newfields=('treat', 'time'), fill=['', 0])
    it = iter(result)
    eq_(expectation[0], it.next())  # header
    eq_(expectation[1], it.next())
    eq_(expectation[2], it.next())
    eq_(expectation[3], it.next())
    eq_(('4', '19', '', 0), it.next())
Example #6
0
def test_melt_and_capture():

    table = (('id', 'parad0', 'parad1', 'parad2'),
             ('1', '12', '34', '56'),
             ('2', '23', '45', '67'))

    expectation = (('id', 'parasitaemia', 'day'),
                   ('1', '12', '0'),
                   ('1', '34', '1'),
                   ('1', '56', '2'),
                   ('2', '23', '0'),
                   ('2', '45', '1'),
                   ('2', '67', '2'))

    step1 = melt(table, key='id', valuefield='parasitaemia')
    step2 = capture(step1, 'variable', 'parad(\\d+)', ('day',))
    ieq(expectation, step2)
Example #7
0
def test_capture_empty():
    table = (('foo', 'bar'),)
    expect = (('foo', 'baz', 'qux'),)
    actual = capture(table, 'bar', r'(\w)(\d)', ('baz', 'qux'))
    ieq(expect, actual)
Example #8
0
def test_capture_empty():
    table = (('foo', 'bar'), )
    expect = (('foo', 'baz', 'qux'), )
    actual = capture(table, 'bar', r'(\w)(\d)', ('baz', 'qux'))
    ieq(expect, actual)