Commit 7c25d05d authored by Paul McCarthy's avatar Paul McCarthy 🚵
Browse files

RF: ICD10 numeric conversion has been changed to use the integer node IDs in

the ICD10 hierarchy. The previous approach could not handle parent categories
and a small number of codes which do not have a <letter><number> structure.
New Hierarchy.codings method to retrieve all codings in a
hierarchy. fileinfo.has_header is lenient when there are only two columns
parent e399aaa4
......@@ -138,7 +138,14 @@ def has_header(sample,
if (hist[t0] / len(ctypes)) < thres: colcount += 1
else: colcount -= 1
return colcount > 0
# If more columns than not passed
# the threshold, let's say we have
# a header. But be lenient at low
# column counts.
threshold = collections.defaultdict(lambda : 1)
threshold[2] = 0
return colcount >= threshold[len(coltypes)]
def sniff(datafile):
......
......@@ -149,6 +149,12 @@ class Hierarchy(object):
self.__codeidxs[c] = i
@property
def codings(self):
"""Return a list of all codings in the hierarchy. """
return list(self.__codings)
def index(self, coding):
"""Return the node ID for the given ``coding``. """
return self.__codeidxs[coding]
......
......@@ -49,21 +49,31 @@ class ICD10Hierarchy(hierarchy.Hierarchy):
def codeToNumeric(code):
"""Converts an ICD10 code into a numeric version. """
# We use the node IDs as defined
# in the ICD10 hierarchy file as
# the numeric version of each
# coding.
hier = hierarchy.getHierarchyFilePath(name='icd10')
hier = hierarchy.loadHierarchyFile(hier)
try:
if code[0].isalpha() and code[1:].isdecimal():
prefix = ord(code[0].lower()) - ord('a') + 11
return int(str(prefix) + str(int(code[1:])))
else:
return np.nan
except Exception:
# hierarchy uses 0-indexing,
# whereas the hierarchy files
# use 1-indexing
return int(hier.index(code) + 1)
except KeyError:
return np.nan
def numericToCode(code):
"""Converts a numeric ICD10 code into its original version. """
code = str(int(code))
prefix = int(code[:2]) + ord('a') - 11
return chr(prefix) + code[2:]
hier = hierarchy.getHierarchyFilePath(name='icd10')
hier = hierarchy.loadHierarchyFile(hier)
try:
return hier.coding(int(code - 1))
except KeyError:
return 'NaN'
def storeCodes(codes):
......
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