Package: deidentify
deidentifyA Python library to de-identify medical records with state-of-the-art NLP methods. Pre-trained models for the Dutch language are available.This repository shares the resources developed in the following paper:J. Trienes, D. Trieschnigg, C. Seifert, and D. Hiemstra. Comparing Rule-based, Feature-based and Deep Neural Methods for De-identification of Dutch Medical Records. In:Proceedings of the 1st ACM WSDM Health Search and Data Mining Workshop (HSDM), 2020.Read more about the work in ourpaperorblog post.Quick StartInstallationCreate a new virtual environment with an environment manager of your choice. Then, installdeidentify:pipinstalldeidentifyWe use the spaCy tokenizer. For good compatibility with the pre-trained models, we recommend using the same spaCy version that we used to train the de-identification models.pipinstall-U"spacy<3"https://github.com/explosion/spacy-models/releases/download/nl_core_news_sm-2.3.0/nl_core_news_sm-2.3.0.tar.gz#egg=nl_core_news_sm==2.3.0Example UsageThe code below shows how to apply a pre-trained de-identification pipeline to an example document. We provide alist of available modelsbelow.fromdeidentify.baseimportDocumentfromdeidentify.taggersimportFlairTaggerfromdeidentify.tokenizerimportTokenizerFactory# Create some texttext=("Dit is stukje tekst met daarin de naam Jan Jansen. De patient J. Jansen (e: ""j.jnsen@email.com, t: 06-12345678) is 64 jaar oud en woonachtig in Utrecht. Hij werd op 10 ""oktober door arts Peter de Visser ontslagen van de kliniek van het UMCU.")# Wrap text in documentdocuments=[Document(name='doc_01',text=text)]# Select downloaded modelmodel='model_bilstmcrf_ons_fast-v0.2.0'# Instantiate tokenizertokenizer=TokenizerFactory().tokenizer(corpus='ons',disable=("tagger","ner"))# Load tagger with a downloaded model file and tokenizertagger=FlairTagger(model=model,tokenizer=tokenizer,verbose=False)# Annotate your documentsannotated_docs=tagger.annotate(documents)This completes the annotation stage. Let's inspect the entities that the tagger found:frompprintimportpprintfirst_doc=annotated_docs[0]pprint(first_doc.annotations)This should print the entities of the first document.[Annotation(text='Jan Jansen',start=39,end=49,tag='Name',doc_id='',ann_id='T0'),Annotation(text='J. Jansen',start=62,end=71,tag='Name',doc_id='',ann_id='T1'),Annotation(text='j.jnsen@email.com',start=76,end=93,tag='Email',doc_id='',ann_id='T2'),Annotation(text='06-12345678',start=98,end=109,tag='Phone_fax',doc_id='',ann_id='T3'),Annotation(text='64 jaar',start=114,end=121,tag='Age',doc_id='',ann_id='T4'),Annotation(text='Utrecht',start=143,end=150,tag='Address',doc_id='',ann_id='T5'),Annotation(text='10 oktober',start=164,end=174,tag='Date',doc_id='',ann_id='T6'),Annotation(text='Peter de Visser',start=185,end=200,tag='Name',doc_id='',ann_id='T7'),Annotation(text='UMCU',start=234,end=238,tag='Hospital',doc_id='',ann_id='T8')]Mask AnnotationsUse masking to replace annotations with placeholders. Example:Jan Jansen -> [NAME]fromdeidentify.utilimportmask_annotationsmasked_doc=mask_annotations(first_doc)print(masked_doc.text)Which should print:Dit is stukje tekst met daarin de naam [NAME]. De patient [NAME] (e: [EMAIL], t: [PHONE_FAX]) is [AGE] oud en woonachtig in [ADDRESS]. Hij werd op [DATE] door arts [NAME] ontslagen van de kliniek van het [HOSPITAL].Replace Annotations with Surrogates [experimental]Use sorrogate generation to replace annotations with random but realistic alternatives. Example:Jan Jansen -> Bart Bakker. The surrogate replacement strategy followsStubbs et al. (2015).fromdeidentify.utilimportsurrogate_annotations# The surrogate generation process involves some randomness.# You can set a seed to make the process deterministic.iter_docs=surrogate_annotations(docs=[first_doc],seed=1)surrogate_doc=list(iter_docs)[0]print(surrogate_doc.text)This code should print:Dit is stukje tekst met daarin de naam Gijs Hermelink. De patient G. Hermelink (e:n.qvgjj@spqms.com, t: 06-83662585) is 64 jaar oud en woonachtig in Cothen. Hij werd op 28 juni door arts Jullian van Troost ontslagen van de kliniek van het UMCU.Available TaggersThere are currently three taggers that you can use:DeduceTagger: A wrapper around the DEDUCE tagger by Menger et al. (2018,code,paper)CRFTagger: A CRF tagger using the feature set by Liu et al. (2015,paper)FlairTagger: A wrapper around the FlairSequenceTaggerallowing the use of neural architectures such as BiLSTM-CRF. The pre-trained models below use contextualized string embeddings by Akbik et al. (2018,paper)All taggers implement thedeidentify.taggers.TextTaggerinterface which you can implement to provide your own taggers.Tag SetUse theTextTagger.tagsto get a list of supported tags. For theFlairTaggerin above demo this looks as follows:>>>tagger.tags['Internal_Location','Age','Phone_fax','Name','SSN','Hospital','Email','Initials','O','Organization_Company','ID','Profession','Care_Institute','Other','Date','URL_IP','Address']Pre-trained ModelsWe provide a number of pre-trained models for the Dutch language. The models were developed on the Nedap/University of Twente (NUT) dataset. The dataset consists of 1260 documents from three domains of Dutch healthcare: elderly care, mental care and disabled care (note: in the codebase we sometimes also refer to this dataset asons). More information on the design of the dataset can be found inour paper.NameTaggerLangDatasetF1*Precision*Recall*TagsDEDUCE (Menger et al., 2018)**DeduceTaggerNLNUT0.66490.81920.55958 PHI Tagsmodel_crf_ons_tuned-v0.2.0CRFTaggerNLNUT0.85110.93370.782015 PHI Tagsmodel_bilstmcrf_ons_fast-v0.2.0FlairTaggerNLNUT0.89140.91010.873515 PHI Tagsmodel_bilstmcrf_ons_large-v0.2.0FlairTaggerNLNUT0.89900.92400.875415 PHI Tags*All scores are micro-averaged entity-level precision/recall/F1 obtained on the test portion of each dataset. For additional metrics, see the corresponding model release.**DEDUCE was developed on a dataset of psychiatric nursing notes and treatment plans. The numbers reported here were obtained by applying DEDUCE to our NUT dataset. For more information on the development of DEDUCE, see the paper byMenger et al. (2018).Running Experiments and Training ModelsIf you have your own dataset of annotated documents and you want to train your own models on it, you can take a look at the following guides:Convert your data into our corpus formatTrain and evaluate your own modelsLogging and pipeline verbosityIf you want more information on the experiments in our paper, have a look here:NUT annotation guidelinesSurrogate generation procedureExperiments on English corpora: i2b2/UTHealth and nursing notesComputational EnvironmentWhen you want to run your own experiments, we assume that you clone this code base locally and execute all scripts underdeidentify/within the following conda environment:# Install package dependencies and add local files to the Python path of that environment.condaenvcreate-fenvironment.yml
condaactivatedeidentify&&exportPYTHONPATH="${PYTHONPATH}:$(pwd)"CitationPlease cite the following paper when usingdeidentify:@inproceedings{Trienes:2020:CRF,title={Comparing Rule-based, Feature-based and Deep Neural Methods for De-identification of Dutch Medical Records},author={Trienes, Jan and Trieschnigg, Dolf and Seifert, Christin and Hiemstra, Djoerd},booktitle={Proceedings of the 1st ACM WSDM Health Search and Data Mining Workshop},series={{HSDM} 2020},year={2020}}ContactIf you have any question, please contact Jan Trienes atjan.trienes@gmail.com.