首页 > 解决方案 > 尝试使用工具,但出现以下错误 KeyError: 'str'

问题描述

我正在尝试使用医学概念注释工具。文档可以在这里找到:https ://github.com/CogStack/MedCAT

med_ann_norm_dict.dat文件是从本节从 github 下载的:

Building a new Concept Database (.csv) or using an existing one
First download the vocabulary from Vocabulary Download

我拥有的代码是:

from medcat.cat import CAT
from medcat.utils.vocab import Vocab
from medcat.prepare_cdb import PrepareCDB
from medcat.cdb import CDB

vocab = Vocab()
#Load the vocab model
vocab.load_dict('C:/Users/as/Desktop/Data/med_ann_norm_dict.dat')

#build a special CDB from a .csv file
preparator = PrepareCDB(vocab=vocab)
csv_paths = ['C:/Users/as/Desktop/Data/ICD10Codes.csv']
cdb = preparator.prepare_csvs(csv_paths)

#Annotate the text
doc = text
cat = CAT(cdb=cdb, vocab=vocab)
#Entities are in 
doc_spacy._.ents

追溯:

Traceback (most recent call last):

  File "<ipython-input-195-32d167ab52db>", line 1, in <module>
    runfile('C:/Users/as/Desktop/Data/Preprocessing.py', wdir='C:/Users/as/Desktop/Data')

  File "C:\Users\as\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 786, in runfile
    execfile(filename, namespace)

  File "C:\Users\as\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/as/Desktop/Data/Preprocessing.py", line 198, in <module>
    main()

  File "C:/Users/as/Desktop/Data/Preprocessing.py", line 193, in main
    preproc.parse()

  File "C:/Users/as/Desktop/Data/Preprocessing.py", line 135, in parse
    self.medcat(text)

  File "C:/Users/as/Desktop/Data/Preprocessing.py", line 181, in medcat
    cdb = preparator.prepare_csvs(csv_paths)

  File "C:\Users\as\AppData\Local\Continuum\anaconda3\lib\site-packages\medcat\prepare_cdb.py", line 55, in prepare_csvs
    names = str(df.iloc[ind]['str']).split(self.NAME_SEPARATOR)

  File "C:\Users\as\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\series.py", line 868, in __getitem__
    result = self.index.get_value(self, key)

  File "C:\Users\as\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 4389, in get_value
    raise e1

  File "C:\Users\as\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 4375, in get_value
    tz=getattr(series.dtype, 'tz', None))

  File "pandas/_libs/index.pyx", line 81, in pandas._libs.index.IndexEngine.get_value

  File "pandas/_libs/index.pyx", line 89, in pandas._libs.index.IndexEngine.get_value

  File "pandas/_libs/index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc

  File "pandas/_libs/hashtable_class_helper.pxi", line 1601, in pandas._libs.hashtable.PyObjectHashTable.get_item

  File "pandas/_libs/hashtable_class_helper.pxi", line 1608, in pandas._libs.hashtable.PyObjectHashTable.get_item

KeyError: 'str'

我不明白为什么我会收到这个错误。

我试图在这一行中加载的 CSV 文件cdb = preparator.prepare_csvs(csv_paths)有两列,如下所示:

A00     Cholera
A000    Malignant

谢谢!

标签: python

解决方案


MedCat 需要您用标题标记列。您至少需要cuistr请参阅https://github.com/CogStack/MedCAT/blob/d1be623a380881a5c8e4e17622a884b9fb3cfa85/examples/simple_cdb.csv中的示例表


推荐阅读