Commit 3a3674f2 authored by Paul McCarthy's avatar Paul McCarthy 🚵
Browse files

TEST: Update metaproc/hierarchy tests

parent 5f1f23a4
......@@ -235,12 +235,14 @@ def test_codeToNumeric():
vartable, proctable, cattable, _ = gen_tables([1])
vartable.loc[1, 'DataCoding'] = hierarchy.HIERARCHY_DATA_NAMES['opcs3']
dt, _ = importing.importData('data.txt',
vartable,
proctable,
cattable,
removeUnknown=False)
cfns.codeToNumeric(dt, 1, 'opcs3')
cfns.codeToNumeric(dt, 1)
got = dt[:, '1-0.0']
exp = pd.Series(exp, name='1-0.0', index=got.index)
......@@ -270,8 +272,8 @@ def _test_convertICD10Codes(lowMemory):
for i, c in enumerate(codes, 1):
f.write('{}\t{}\n'.format(i, c))
vartable, proctable, cattable, _ = gen_tables([1])
vartable.loc[1, 'DataCoding'] = hierarchy.HIERARCHY_DATA_NAMES['icd10']
dt, _ = importing.importData('data.txt',
vartable,
......
......@@ -56,12 +56,13 @@ def test_loadHierarchyFile():
def test_codeToNumeric():
icd10hier = hierarchy.getHierarchyFilePath(name='icd10')
icd10code = hierarchy.HIERARCHY_DATA_NAMES['icd10']
icd10hier = hierarchy.getHierarchyFilePath(coding=icd10code)
icd10hier = hierarchy.loadHierarchyFile(icd10hier)
codes = list(random.sample(icd10hier.codings, 20)) + ['badcode']
exp = [icd10hier.index(c) + 1 for c in codes[:-1]] + [np.nan]
conv = [hierarchy.codeToNumeric(c, 'icd10') for c in codes]
conv = [hierarchy.codeToNumeric(c, coding=icd10code) for c in codes]
exp = np.array(exp)
conv = np.array(conv)
......@@ -80,7 +81,7 @@ def test_codeToNumeric():
codes, ncodes = zip(*ncodes)
codes = [c.lower() for c in codes]
assert codes == [hierarchy.numericToCode(c, 'icd10').lower()
assert codes == [hierarchy.numericToCode(c, coding=icd10code).lower()
for c in ncodes]
......
......@@ -20,72 +20,120 @@ from . import clear_plugins, gen_DataTable, tempdir
@clear_plugins
def test_xDescriptionFromNumeric():
def test_codingDescriptionFromValue():
custom.registerBuiltIns()
for name in ['icd10', 'icd9', 'opcs4', 'opcs3']:
with tempdir():
descs = tw.dedent("""
coding meaning
30 meaning 30
40 meaning 40
50 meaning 50
20 meaning 20
60 meaning 60
10 meaning 10 hah
""").strip()
with open('coding123.txt', 'wt') as f:
f.write(descs)
hier = hierarchy.getHierarchyFilePath(name=name)
hier = hierarchy.loadHierarchyFile(hier)
dt = gen_DataTable([np.random.randint(1, 10, 10)])
code = random.choice(hier.codings)
ncode = hierarchy.codeToNumeric(code, name)
desc = hier.description(code)
dt.vartable.loc[1, 'DataCoding'] = 123
func = custom.get('metaproc', '{}.numdesc'.format(name))
gendesc = func(None, None, ncode)
func = custom.get('metaproc', 'codingdesc')
assert code in gendesc
assert desc in gendesc
with mock.patch('funpack.coding.getCodingFilePath',
return_value='coding123.txt'):
assert func(dt, 1, 30) == '30 - meaning 30'
assert func(dt, 1, 10) == '10 - meaning 10 hah'
@clear_plugins
def test_xDescriptionFromCode():
def test_hierarchicalDescriptionFromX():
custom.registerBuiltIns()
for name in ['icd10', 'icd9', 'opcs4', 'opcs3']:
hier = hierarchy.getHierarchyFilePath(name=name)
hier = hierarchy.loadHierarchyFile(hier)
with tempdir():
data = tw.dedent("""
coding\tmeaning\tnode_id\tparent_id
a\ta desc\t5\t0
b\tb desc\t1\t5
c\tc desc\t3\t5
d\td desc\t4\t3
e\te desc\t2\t1
""")
code = random.choice(hier.codings)
desc = hier.description(code)
with open('coding123.txt', 'wt') as f:
f.write(data)
func = custom.get('metaproc', '{}.codedesc'.format(name))
dt = gen_DataTable([np.random.randint(1, 10, 10)])
gendesc = func(None, None, code)
dt.vartable.loc[1, 'DataCoding'] = 123
numfunc = custom.get('metaproc', 'hierarchynumdesc')
codefunc = custom.get('metaproc', 'hierarchycodedesc')
assert code in gendesc
assert desc in gendesc
with mock.patch('funpack.hierarchy.getHierarchyFilePath',
return_value='coding123.txt'):
assert codefunc(dt, 1, 'a') == 'a - a desc'
assert codefunc(dt, 1, 'c') == 'c - c desc'
assert numfunc( dt, 1, 5) == 'a - a desc'
assert numfunc( dt, 1, 3) == 'c - c desc'
@clear_plugins
def test_codingDescriptionFromValue():
def test_xDescriptionFromNumeric():
custom.registerBuiltIns()
with tempdir():
descs = tw.dedent("""
coding meaning
30 meaning 30
40 meaning 40
50 meaning 50
20 meaning 20
60 meaning 60
10 meaning 10 hah
""").strip()
with open('coding123.txt', 'wt') as f:
f.write(descs)
dt = gen_DataTable([np.random.randint(1, 10, 10)])
for name in ['icd10', 'icd9', 'opcs4', 'opcs3']:
coding = hierarchy.HIERARCHY_DATA_NAMES[name]
dt.vartable.loc[1, 'DataCoding'] = coding
hier = hierarchy.getHierarchyFilePath(coding=coding)
hier = hierarchy.loadHierarchyFile(hier)
code = random.choice(hier.codings)
ncode = hierarchy.codeToNumeric(code, coding=coding)
desc = hier.description(code)
func = custom.get('metaproc', '{}.numdesc'.format(name))
gendesc = func(dt, 1, ncode)
assert code in gendesc
assert desc in gendesc
@clear_plugins
def test_xDescriptionFromCode():
custom.registerBuiltIns()
with tempdir():
dt = gen_DataTable([np.random.randint(1, 10, 10)])
dt.vartable.loc[1, 'DataCoding'] = 123
for name in ['icd10', 'icd9', 'opcs4', 'opcs3']:
coding = hierarchy.HIERARCHY_DATA_NAMES[name]
hier = hierarchy.getHierarchyFilePath(coding=coding)
hier = hierarchy.loadHierarchyFile(hier)
func = custom.get('metaproc', 'codingdesc')
dt.vartable.loc[1, 'DataCoding'] = coding
with mock.patch('funpack.coding.getCodingFilePath',
return_value='coding123.txt'):
assert func(dt, 1, 30) == '30 - meaning 30'
assert func(dt, 1, 10) == '10 - meaning 10 hah'
code = random.choice(hier.codings)
desc = hier.description(code)
func = custom.get('metaproc', '{}.codedesc'.format(name))
gendesc = func(dt, 1, code)
assert code in gendesc
assert desc in gendesc
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