尝试使用工具,但出现以下错误KeyError:'str'

时间:2019-07-03 08:26:03

标签: python

我正在尝试使用医学概念注释工具。可以在以下位置找到该文档: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'

我不确定我为什么会收到此错误。

我要在此行cdb = preparator.prepare_csvs(csv_paths)中加载的CSV文件有两列,看起来像这样:

A00     Cholera
A000    Malignant

谢谢!

1 个答案:

答案 0 :(得分:1)

MedCat需要您用标题标记列。您必须至少拥有cuistr。参见示例表https://github.com/CogStack/MedCAT/blob/master/examples/simple_cdb.csv