Commit 09cf2c48 authored by Paul McCarthy's avatar Paul McCarthy 🚵
Browse files

TEST: unit/integration tests for --add_aux_vars

parent d2418857
......@@ -15,8 +15,9 @@ import funpack.importing.filter as filter
from funpack.datatable import Column
import funpack.fileinfo as fileinfo
import funpack.processing as processing
from . import (gen_DataTable, gen_DataTableFromDataFrame,
tempdir, gen_tables, gen_test_data)
tempdir, gen_tables, gen_test_data, clear_plugins)
def test_columnsToLoad():
......@@ -197,3 +198,43 @@ def test_filter_subjects_column_not_present():
assert (got1 == df).all().all()
assert (got2 == df).all().all()
@clear_plugins
def test_addAuxillaryVariables():
data = tw.dedent("""
eid,1-0.0,2-0.0,3-0.0
1,10,20,30
2,11,21,31
3,12,22,32
4,13,23,33
""").strip()
proctable = pd.DataFrame({
'ID' : [0],
'Variable' : [['vids', [1]]],
'Process' : [{'binariseCategorical' :
processing.Process('processor',
'binariseCategorical',
(),
dict(take=[2]),
'')}]})
with tempdir():
with open('data.txt', 'wt') as f:
f.write(data)
finfo = fileinfo.FileInfo('data.txt')
assert filter.addAuxillaryVariables(
finfo, proctable, None, None) == (None, None)
assert filter.addAuxillaryVariables(
finfo, proctable, [1, 2, 3], None) == ([1, 2, 3], None)
assert filter.addAuxillaryVariables(
finfo, proctable, [1, 3], None) == ([1, 3, 2], None)
assert filter.addAuxillaryVariables(
finfo, proctable, [1, 2, 3], [2]) == ([1, 2, 3], None)
assert filter.addAuxillaryVariables(
finfo, proctable, [1, 3], [2]) == ([1, 3, 2], None)
assert filter.addAuxillaryVariables(
finfo, proctable, [1, 3], [2, 3]) == ([1, 3, 2], [3])
......@@ -1704,3 +1704,73 @@ def test_exclude_categories():
['eid', '1-0.0', '2-0.0', '7-0.0', '8-0.0']
exp = data[:, [0, 1, 2, 7, 8]]
assert np.all(df.to_numpy() == exp)
@patch_logging
@clear_plugins
def test_add_aux_vars():
@custom.processor(auxvids=['rwith'], filterMissing=False)
def replace(dtable, vids, rwith):
for vid, replvid in zip(vids, rwith):
print(f'ASKED TO REPLACE {vid} WITH {replvid}')
if not (dtable.present(vid) and dtable.present(replvid)):
print(f' -> SKIP [{dtable.present(vid)}, {dtable.present(replvid)}]')
continue
for col, replcol in zip(dtable.columns(vid),
dtable.columns(replvid)):
dtable[:, col.name] = dtable[:, replcol.name]
nvars = 6
data = np.random.randint(1, 100, (50, nvars + 1))
data[:, 0] = np.arange(1, 51)
colnames = ['eid'] + [f'{v}-0.0' for v in range(1, nvars + 1)]
if True: # with tempdir():
with open('data.tsv', 'wt') as f:
f.write('\t'.join(colnames) + '\n')
np.savetxt(f, data, fmt='%i', delimiter='\t')
cmdbase = '-ow -apr 1,2,3 "replace(rwith=[4,5,6])" out.tsv data.tsv'
# all vars present, all good
main.main(shlex.split(cmdbase))
df = pd.read_csv('out.tsv', sep='\t')
assert list(df.columns) == colnames
exp = data[:, [0, 4, 5, 6, 4, 5, 6]]
assert np.all(df.to_numpy() == exp)
# # default: processing skipped if auxvar not present
main.main(shlex.split(f'-v "1:5" {cmdbase}'))
df = pd.read_csv('out.tsv', sep='\t')
assert list(df.columns) == colnames[:-1]
exp = data[:, [0, 4, 5, 3, 4, 5]]
assert np.all(df.to_numpy() == exp)
main.main(shlex.split(f'-v "1,3,4,5" {cmdbase}'))
df = pd.read_csv('out.tsv', sep='\t')
assert list(df.columns) == [colnames[i] for i in (0, 1, 3, 4, 5)]
exp = data[:, [0, 4, 3, 4, 5]]
assert np.all(df.to_numpy() == exp)
# add_aux_vars: auxvars auto-imported
main.main(shlex.split(f'-aa -v "1:5" {cmdbase}'))
df = pd.read_csv('out.tsv', sep='\t')
assert list(df.columns) == colnames
exp = data[:, [0, 4, 5, 6, 4, 5, 6]]
assert np.all(df.to_numpy() == exp)
main.main(shlex.split(f'-aa -v "1,3,4,5" {cmdbase}'))
df = pd.read_csv('out.tsv', sep='\t')
assert list(df.columns) == [colnames[i] for i in (0, 1, 3, 4, 5, 6)]
exp = data[:, [0, 4, 6, 4, 5, 6]]
assert np.all(df.to_numpy() == exp)
# add_aux_vars: primary vars *not* auto-imported
main.main(shlex.split(f'-aa -v "2:6" {cmdbase}'))
df = pd.read_csv('out.tsv', sep='\t')
assert list(df.columns) == [colnames[i] for i in (0, 2, 3, 4, 5, 6)]
exp = data[:, [0, 5, 6, 4, 5, 6]]
assert np.all(df.to_numpy() == exp)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment