def _read_file(filename): try: with open(os.path.join(resources, 'cache.pickle'), 'rb') as infile: result, v = pickle.load(infile) assert v == __version__ assert not get_versions()['dirty'] return result except: icd9cm_expanded_vocab = ICD9CM(use_decimals=True) icd9pcs_vocab = ICD9PCS(use_decimals=True) icd9mixed_vocab = ICD9CM(use_decimals=True) | ICD9PCS( use_decimals=True) icd10cm_vocab = ICD10CM(use_decimals=True) icd10pcs_vocab = ICD10PCS(use_decimals=True) icd10mixed_vocab = ICD10CM(use_decimals=True) | ICD10PCS( use_decimals=False) result = {} with open(filename, 'rt') as infile: reader = csv.reader(infile) next(reader) for row in reader: if row[3].strip().replace('/', '').upper() != 'NA': all_codes = icd9mixed_vocab.parse(row[3]) result[(row[1], row[2], icd9cm_expanded_vocab.vocab_domain, icd9cm_expanded_vocab.vocab_name )] = icd9cm_expanded_vocab.filter(all_codes) result[(row[1], row[2], icd9pcs_vocab.vocab_domain, icd9pcs_vocab.vocab_name )] = icd9pcs_vocab.filter(all_codes) # res = (all_codes) - (result[(row[1], row[2], 'icd9cm')] | result[(row[1], row[2], 'icd9pcs')]) # if res: # print 'icd9:', res if row[4].strip().replace('/', '').upper() != 'NA': all_codes = icd10mixed_vocab.parse(row[4]) result[(row[1], row[2], icd10cm_vocab.vocab_domain, icd10cm_vocab.vocab_name )] = icd10cm_vocab.filter(all_codes) result[(row[1], row[2], icd10pcs_vocab.vocab_domain, icd10pcs_vocab.vocab_name )] = icd10pcs_vocab.filter(all_codes) # res = (all_codes) - (result[(row[1], row[2], 'icd10cm')] | result[(row[1], row[2], 'icd10pcs')]) # if res: # print 'icd10:', res with open(os.path.join(resources, 'cache.pickle'), 'wb') as outfile: pickle.dump((result, __version__), outfile) return result
def test_icd9_cm(): vocab = ICD9CM(match_terminal_only=True) assert_equal(vocab.parse("250.33, 250.40, 250.41, 250.42, 250.43,"), {'250.33', '250.40', '250.41', '250.42', '250.43'}) assert_equal(vocab.parse("250.33, 250.40-250.43,"), {'250.33', '250.40', '250.41', '250.42', '250.43'}) assert_equal(vocab.parse("250.33, 250.4*,"), {'250.33', '250.40', '250.41', '250.42', '250.43'}) assert_equal(vocab.parse('42'), {'042'})
def test_code_sets(): icd9cm = ICD9CM() icd9pcs = ICD9PCS() icd10cm = ICD10CM() icd10pcs = ICD10PCS() for k, v in code_sets.collectlevels().items(): assert k[-1] in {'ICD9CM', 'ICD10CM', 'ICD9PCS', 'ICD10PCS'} assert k[-2] in {'DX', 'PX'} if k[-1] == 'icd9cm': assert_equal(icd9cm.filter(v), v) assert_equal(k[-2], 'DX') elif k[-1] == 'icd10cm': assert_equal(icd10cm.filter(v), v) assert_equal(k[-2], 'DX') if k[-1] == 'icd9pcs': assert_equal(icd9pcs.filter(v), v) assert_equal(k[-2], 'PX') elif k[-1] == 'icd10pcs': assert_equal(icd10pcs.filter(v), v) assert_equal(k[-2], 'PX')
def code_system_standardizers(icd10cm=dict(), icd10pcs=dict(), icd9cm=dict(), icd9pcs=dict(), hcpcs=dict(), loinc=dict(), ndc=dict(), ubrev=dict()): result = dict( ICD10PCS=ICD10PCS(**icd10cm).standardize, ICD10CM=ICD10CM(**icd10pcs).standardize, ICD9CM=ICD9CM(**icd9cm).standardize, ICD9PCS=ICD9PCS(**icd9pcs).standardize, HCPCS=HCPCS(**hcpcs).standardize, LOINC=LOINC(**loinc).standardize, NDC=NDC(**ndc).standardize, UBREV=UBREV(**ubrev).standardize, ) return result
# from clinvoc.icd9 import ICD9PCS, ICD9CM import resources import os import re from base import CCS from clinvoc.icd9 import ICD9CM, ICD9PCS import pandas as pd from toolz.dicttoolz import merge # ''' # Single category parsing # ''' icd9cm_vocab = ICD9CM(use_decimals=False) icd9pcs_vocab = ICD9PCS(use_decimals=False) def _get_icd9_codes(filename, code_type): assert code_type in ['dx', 'px'] vocab = icd9cm_vocab if code_type == 'dx' else icd9pcs_vocab file_path = os.path.join(resources.resources, filename) df = pd.read_csv(file_path) code_column = df.columns[0] result = {} for _, row in df.iterrows(): key = (re.sub('\[[^\]]*\]', '', row[2]).strip(), re.sub('\[[^\]]*\]', '', row[4]).strip(), re.sub('\[[^\]]*\]', '', row[6]).strip(), vocab.vocab_domain, vocab.vocab_name) # if not key[2]: # key = key[:2] + key[1:2] + key[3:] if key not in result: result[key] = set() result[key].add(vocab.standardize(row[code_column].strip('\''))) return result
from clinvoc.icd9 import ICD9CM # This string describes a set of ICD 9 codes codestring = '745.0-745.3, 745.6*, 746, 747.1-747.49, 747.81, 747.89, 35.8, 35.81, 35.82, 35.83, 35.84' # Use clinvoc to parse and standardize the above codes vocab = ICD9CM() codeset = vocab.parse(codestring) print(sorted(codeset))
import os import pandas as pd from clinvoc.icd9 import ICD9CM from clinvoc.code_collections import CodeCollection from modulecache.invalidators import FileChangeInvalidator from modulecache.backends import PickleBackend from .resources import resources vocab = ICD9CM(use_decimals=True) def _construct_icd9_categories(def_column="Enhanced ICD-9-CM"): # args? # df = pd.read_csv(os.path.join(resources, 'NDF_January_2016.xlsx')) df = pd.read_csv(os.path.join(resources, 'elixhauser_definitions.csv')) elix_cats = {} for _, row in df.iterrows(): category = row['Category'] codes = row[def_column] parsed_codes = vocab.parse(codes) elix_cats[(category, vocab.vocab_domain, vocab.vocab_name)] = parsed_codes return elix_cats cache_filename = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'elix_icd9_cache.pkl') suppress = globals().keys() with PickleBackend(cache_filename, suppress) as cache, FileChangeInvalidator(cache, os.path.abspath(__file__)): code_set = CodeCollection(*_construct_icd9_categories().items(), name='elix_icd9')
from clinvoc.code_collections import CodeCollection import os from clinvoc.icd9 import ICD9CM from clinvoc.code_maps import CodeMap from clinvoc.icd10 import ICD10CM # This CSV file contains sets of codes in several categories # See: https://www.ncbi.nlm.nih.gov/pmc/articles/PMC4134331/ filename = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'pccccs2.csv') # Create a CodeCollection based on the CSV file collection = CodeCollection.from_csv(filename, vocabs={'ICD-9': ICD9CM()}, header=True, ignore=[0, 'ICD-10']) # Create a CodeMap from the collection code_map = CodeMap.from_code_collection(collection, standardizers={ 'ICD-9': ICD9CM().standardize, 'ICD-10': ICD10CM().standardize }) # Map ICD9 code V45.2 to see that it's a neurologic or neuromuscular device print(code_map[('ICD-9', 'V45.2')])