Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug with zero-only numerical part of ICD codes like A00 #1

Open
juzb opened this issue May 29, 2020 · 6 comments
Open

Bug with zero-only numerical part of ICD codes like A00 #1

juzb opened this issue May 29, 2020 · 6 comments

Comments

@juzb
Copy link

juzb commented May 29, 2020

Hey Bryan,

really appreciate this neat package! When working with ICD codes like A00, A000, or B00 that have zero-only numerical parts, I get ValueErrors due to the lstripping of all zeros in Line 126 of the init.py:

icd10.find('A00').chapter

# Traceback
    124 
    125 def in_chapter(block: str, icd10: str) -> bool:
--> 126     alpha, numeric = ord(icd10[0]), int(icd10[1:3].lstrip('0'))
    127     sblock, eblock = block.split('-')  # A00-B99
    128     salpha, snumeric = ord(sblock[0]), int(sblock[1:].lstrip('0') or 0)

ValueError: invalid literal for int() with base 10: ''

As far as I can tell, it should work fine when changing

alpha, numeric = ord(icd10[0]), int(icd10[1:3].lstrip('0'))

to

alpha, numeric = ord(icd10[0]), int(icd10[1:3].lstrip('0') or 0)

as in the following lines.

Cheers,
Julius

@vigosser
Copy link

same problem, and it's works for me

def in_chapter(block: str, icd10: str) -> bool:
    temp=icd10[1:3].lstrip('0')
    if temp=='':
        temp='0'
    alpha, numeric = ord(icd10[0]), int(temp)
    sblock, eblock = block.split('-')  # A00-B99
    salpha, snumeric = ord(sblock[0]), int(sblock[1:].lstrip('0') or 0)
    ealpha, enumeric = ord(eblock[0]), int(eblock[1:].lstrip('0') or 0)
    return salpha <= alpha <= ealpha and snumeric <= numeric <= enumeric

@Abed-AlRahman
Copy link

Abed-AlRahman commented Oct 25, 2021

Thank you guys
I ran into another error for codes such as this one "O9A212". It was not possible to conver '9A' to int.
So, the solution was to strip all the capital letters in line 126:

alpha, numeric = ord(icd10[0]), int(icd10[1:3].lstrip('0').strip('ABCDEFGHIJKLMNOPQRSTUVWXYZ') or 0)

Thanks

@najarro82
Copy link

I tried to edit line 126 to solve this issue with "9A" but also got an invalid literal for int() with base 10 error.

@Abed-AlRahman
Copy link

I tried to edit line 126 to solve this issue with "9A" but also got an invalid literal for int() with base 10 error.

Could you please show us the ICD10 code which caused the error?
@najarro82

@najarro82
Copy link

Here are some code prefixes causing errors for me.
R00.*
Z3A.*
U07.*
Z00.*

@wbazant
Copy link

wbazant commented Oct 17, 2023

I wanted to map chapters from the k4m1113/ICD-10-CSV project. I had the same problem - solved with or 0, and then the one @Abed-AlRahman solved with .strip('ABCDEFGHIJKLMNOPQRSTUVWXYZ').

Then 'C00-D48' was not including the code C49 so I changed the bounds check:

        if salpha == ealpha:
            return alpha == salpha and snumeric <= numeric <= enumeric
        else:
            return (salpha <= alpha < ealpha and snumeric <= numeric) or (salpha < alpha <= ealpha and numeric <= enumeric)

Then the chapter definitions still weren't broad enough so I modified a few. After this, I have a data frame!

Full code:

def in_chapter(block: str, icd10: str) -> bool:
    try:
        alpha, numeric = ord(icd10[0]), int(icd10[1:3].lstrip('0').strip('ABCDEFGHIJKLMNOPQRSTUVWXYZ') or 0)
        sblock, eblock = block.split('-')  # A00-B99
        salpha, snumeric = ord(sblock[0]), int(sblock[1:].lstrip('0') or 0)
        ealpha, enumeric = ord(eblock[0]), int(eblock[1:].lstrip('0') or 0)
        if salpha == ealpha:
            return alpha == salpha and snumeric <= numeric <= enumeric
        else:
            return (salpha <= alpha < ealpha and snumeric <= numeric) or (salpha < alpha <= ealpha and numeric <= enumeric)
    except ValueError as e:
        print(e, block, icd10)


# https://en.wikipedia.org/wiki/ICD-10#List
chapters = [
    ('I', 'A00-B99', 'Certain infectious and parasitic diseases'),
    ('II', 'C00-D49', 'Neoplasms'),
    ('III', 'D50-D89', 'Diseases of the blood and blood-forming organs and certain disorders involving the immune mechanism'),
    ('IV', 'E00-E90', 'Endocrine, nutritional and metabolic diseases'),
    ('V', 'F00-F99', 'Mental and behavioural disorders'),
    ('VI', 'G00-G99', 'Diseases of the nervous system'),
    ('VII', 'H00-H59', 'Diseases of the eye and adnexia'),
    ('VIII', 'H60-H95', 'Diseases of the ear and mastoid process'),
    ('IX', 'I00-I99', 'Diseases of the circulatory system'),
    ('X', 'J00-J99', 'Diseases of the respiratory system'),
    ('XI', 'K00-K95', 'Diseases of the digestive system'),
    ('XII', 'L00-L99', 'Diseases of the skin and subcutaneous tissue'),
    ('XIII', 'M00-M99', 'Diseases of the musculoskeletal system and connective tissue'),
    ('XIV', 'N00-N99', 'Diseases of the genitourinary system'),
    ('XV', 'O00-O99', 'Pregnancy, childbirth and the puerperium'),
    ('XVI', 'P00-P96', 'Certain conditions originating in the perinatal period'),
    ('XVII', 'Q00-Q99', 'Congenital malformations, deformations and chromosomal abnormalities'),
    ('XVIII', 'R00-R99', 'Symptoms, signs and abnormal clinical and laboratory findings, not elsewhere classified'),
    ('XIX', 'S00-T98', 'Injury, poisoning and certain other consequences of external causes'),
    ('XX', 'V00-Y99', 'External causes of morbidity and mortality'),
    ('XXI', 'Z00-Z99', 'Factors influencing health status and contact with health services'),
    ('XXII', 'U00-U99', 'Codes for special purposes'),
]

def find_chapter(code):
    for chapter, block, description in chapters:
        if in_chapter(block, code):
            return chapter, block, description
    raise ValueError(code)


import pandas as pd
import requests
from io import StringIO

df = pd.read_csv(StringIO(requests.get("https://raw.githubusercontent.com/k4m1113/ICD-10-CSV/master/codes.csv").text), names=["Category Code", "Diagnosis Code", "Full Code", "Abbreviated Description", "Full Description", "Category Title"], header=None)

df['Chapter'], df['Chapter Block'], df['Chapter Description'] = zip(*df['Category Code'].map(find_chapter))

df

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants