Package: cnlp-transformers
Clinical NLP Transformers (cnlp_transformers)Transformers for Clinical NLPThis library was created to add abstractions on top of the Huggingface Transformers library for many clinical NLP research use cases.
Primary use cases includesimplifying multiple tasks related to fine-tuning of transformers for building models for clinical NLP research, andcreating inference APIs that will allow downstream researchers easier access to clinical NLP outputs.This library isnotintended to serve as a place for clinical NLP applications to live. If you build something cool that uses transformer models that take advantage of our model definitions, the best practice is probably to rely on it as a library rather than treating it as your workspace. This library is also not intended as a deployment-ready tool forscalableclinical NLP. There is a lot of interest in developing methods and tools that are smaller and can process millions of records, and this library can potentially be used for research along those line. But it will probably never be extremely optimized or shrink-wrapped for applications. However, there should be plenty of examples and useful code for people who are interested in that type of deployment.InstallNote: due to some dependency issues, this package does not officially
support macOS on Apple Silicon. If you want to install it on Apple Silicon,
you are on your own; we unofficially recommend trying it with Python 3.10, or using
the docker CPU imageNote:When installing the library's dependencies,pipwill probably install
PyTorch with CUDA 10.2 support by default. If you would like to run the
library in CPU-only mode or with a newer version of CUDA,install PyTorch
to your desired specificationsin your virtual environment first before installingcnlp-transformers.Static installationIf you are installing just to fine-tune or run the REST APIs,
you can install without cloning:$# Note: if needed, install PyTorch first (see above)$pipinstallcnlp-transformersIf you prefer,prebuilt Docker imagesare also available to run the REST APIs in a network.
An exampleDocker Compose configurationis also available for reference.Editable installationIf you want to modify code (e.g., for developing new models), then install locally:Clone this repository:# Either the HTTPS method...$gitclonehttps://github.com/Machine-Learning-for-Medical-Language/cnlp_transformers.git# ...or the SSH method$gitclonegit@github.com:Machine-Learning-for-Medical-Language/cnlp_transformers.gitEnter the repo:cd cnlp_transformersInstall the development dependencies:$pipinstall-rdev-requirements.txtSee above for the note about PyTorch; if needed, manually install it now.Installcnlp-transformersin editable mode:$pipinstall-e.Fine-tuningThe main entry point for fine-tuning is thecnlp_transformers/src/cnlpt/train_system.pyscript. Run with no arguments to show an extensive list of options that are allowed, inheriting from and extending the Huggingface training options.WorkflowTo use the library for fine-tuning, you'll need to take the following steps:Write your dataset to one of the following formats in a folder with train, dev, and test files:csv or tsv: The first row should have column names separated by comma or tab. The nametexthas special meaning as the input string. Likewise if there are columns namedtext_aandtext_bit will be interpreted as two parts of a transformer input string separated by a -token equivalent. All other columns are treated as potential targets -- their names can be passed to thetrain_system.pyscript as--task_namearguments. For tagging targets, the field must consist of space-delimited labels, one per space-delimited token in thetextfield. For relation extraction targets, the field must be a,delimited list of relation tuples, where each relation tuple is (<offset 1>, <offset 2>,label), where offset 1 and 2 are token indices into the space-delimited tokens in thetextfield.json: The file format must be the following:{ 'data': [
      { 'text': <text of instance>,
        'id': <instance id>
        '<sub-task 1 name>': <instance label>,
        '<sub-task 2 name>': <instance label>,
        ... // other labels
        }
      { }, // instance 2
      ...  // instances 3...N
  ],
    'metadata': {
      'version': '<optional dataset versioning>',
      'task': <overall task/dataset name>,
      'subtasks': [
        {
          'task_name': '<sub-task 1 name>',
          'output_mode': <sub-task output mode (e.g. tagging, relex, classification)>,
        }
        ...
        {
          'task_name': '<sub-task n name>'
          'output_mode': <sub-task output mode (e.g. tagging, relex, classification)>,
        }
      ]
    }
  }Instance labels should be formatted the same way as in the csv/tsv example above, see specifically the formats for tagging and relations. The 'metadata' field can either be included in the train/dev/test files or as a separate metadata.json file.Run train_system.py with a--task_namefrom your data files and the--data-dirargument from Step 1.Step-by-step finetuning examplesWe provided the following step-by-step examples how to finetune in clinical NLP tasks:1.Classification task: usingDrug Review Dataset (Drugs.com) Data Set2.Sequence tagging task: usingChemProt websiteFine-tuning optionsRunpython -m cnlpt.train_system -hto see all the available options. In addition to inherited Huggingface Transformers options, there are options to do the following:Run simple baselines (use--model cnn --tokenizer_name roberta-base-- since there is no HF model then you must specify the tokenizer explicitly)Use a different layer's CLS token for the classification (e.g.,--layer 10)Only update the weights of the classifier head and leave the encoder weights alone (--freeze)Classify based on a token embedding instead of the CLS embedding (--token-- applies to the event/entity classification setting only, and requires the input to have xml-style tags (, ) around the tokens representing the event/entity)Use class-weighted loss function (--class_weights)Running REST APIsThere are existing REST APIs in thesrc/cnlpt/apifolder for a few important clinical NLP tasks:Negation detectionTime expression tagging (spans + time classes)Event detection (spans + document creation time relation)End-to-end temporal relation extraction (event spans+DTR+timex spans+time classes+narrative container [CONTAINS] relation extraction)Negation APITo demo the negation API:Install thecnlp-transformerspackage.Runcnlpt_negation_rest [-p PORT].Open a python console and run the following commands:Setup variables>>> import requests
>>> process_url = 'http://hostname:8000/negation/process'  ## Replace hostname with your host namePrepare the document>>> sent = 'The patient has a sore knee and headache but denies nausea and has no anosmia.'
>>> ents = [[18, 27], [32, 40], [52, 58], [70, 77]]
>>> doc = {'doc_text':sent, 'entities':ents}Process the document>>> r = requests.post(process_url, json=doc)
>>> r.json()Output: {'statuses': [-1, -1, 1, 1]}The model correctly classifies both nausea and anosmia as negated.Temporal API (End-to-end temporal information extraction)To demo the temporal API:Install thecnlp-transformerspackage.Runcnlpt_temporal_rest [-p PORT]Open a python console and run the following commands to test:Setup variables>>> import requests
>>> from pprint import pprint
>>> process_url = 'http://hostname:8000/temporal/process_sentence'  ## Replace hostname with your host namePrepare and process the document>>> sent = 'The patient was diagnosed with adenocarcinoma March 3, 2010 and will be returning for chemotherapy next week.'
>>> r = requests.post(process_url, json={'sentence':sent})
>>> pprint(r.json())should return:{'events': [[{'begin': 3, 'dtr': 'BEFORE', 'end': 3},
             {'begin': 5, 'dtr': 'BEFORE', 'end': 5},
             {'begin': 13, 'dtr': 'AFTER', 'end': 13},
             {'begin': 15, 'dtr': 'AFTER', 'end': 15}]],
 'relations': [[{'arg1': 'TIMEX-0', 'arg2': 'EVENT-0', 'category': 'CONTAINS'},
                {'arg1': 'EVENT-2', 'arg2': 'EVENT-3', 'category': 'CONTAINS'},
                {'arg1': 'TIMEX-1', 'arg2': 'EVENT-2', 'category': 'CONTAINS'},
                {'arg1': 'TIMEX-1',
                 'arg2': 'EVENT-3',
                 'category': 'CONTAINS'}]],
 'timexes': [[{'begin': 6, 'end': 9, 'timeClass': 'DATE'},
              {'begin': 16, 'end': 17, 'timeClass': 'DATE'}]]}This output indicates the token spans of events and timexes, and relations between events and timexes, where the suffixes are indices into the respective arrays (e.g., TIMEX-0 in a relation refers to the 0th time expression found, which begins at token 6 and ends at token 9 -- ["March 3, 2010"])To run only the time expression or event taggers, change the run command to:uvicorn cnlpt.api.timex_rest:app --host 0.0.0.0oruvicorn cnlpt.api.event_rest:app --host 0.0.0.0then run the same process commands as above (including the same URL). You will get similar json output, but only one of the dictionary elements (timexes or events) will be populated.