def test_annotate_note__wrong_tool_type(self): """Wrong tool type""" with self.config,\ pytest.raises(ValueError, match="Invalid annotator_type: foo"): client.annotate_note(host=self.host, note=self.example_note, tool_type="foo")
def annotate_note(annotator_host, note_json, output, tool_type): """Annotate a note with specified annotator""" with open(note_json, "r") as note_f: notes = json.load(note_f) all_annotations = [] for note in notes: note_name = note.pop("note_name") annotations = client.annotate_note(host=annotator_host, note=note, tool_type=tool_type) annotations['annotationSource'] = { "resourceSource": { "name": note_name } } all_annotations.append(annotations) utils.stdout_or_json(all_annotations, output)
def test_annotate_note(self, tool_type, tool_func): """Test annotate note""" with self.config as config,\ self.api_client as api_client,\ patch.object(client, tool_func, return_value=self.date_response) as patch_annot: api_client.return_value = api_client api_client.__enter__ = Mock(return_value=self.api) api_client.__exit__ = Mock(return_value=None) result = client.annotate_note(host=self.host, note=self.example_note, tool_type=tool_type) config.assert_called_once_with(host=self.host) api_client.assert_called_once_with(self.configuration) patch_annot.assert_called_once_with(self.api, self.example_request) assert result == { 'textDateAnnotations': [{ 'start': 10, 'length': 10, 'text': 'foobar', 'confidence': 95.5 }] }
def create_deidentified_notes(deidentify_request=None): # noqa: E501 """Deidentify a clinical note Returns the deidentified note # noqa: E501 :param deidentify_request: :type deidentify_request: dict | bytes :rtype: DeidentifyResponse """ if connexion.request.is_json: deid_request = DeidentifyRequest.from_dict( connexion.request.get_json()) note = deid_request.note # Make set of all annotation types in the de-id request all_annotation_types = { annotation_type for annotation_types in [ deid_step.annotation_types for deid_step in deid_request.deidentification_steps ] for annotation_type in annotation_types } config = Config() # Annotations is a dict[key: list[str]] annotations = { 'text_date': [], 'text_person_name': [], 'text_location': [], 'text_contact': [], 'text_id': [] } # Convert to NLPSandboxClient's Note object client_note = client.Note(identifier=NoteId(note.identifier), text=note.text, type=note.type, patient_id=PatientId(note.patient_id)) if 'text_date' in all_annotation_types: annotations['text_date'] = client.annotate_note( host=config.date_annotator_api_url, note=client_note, tool_type='nlpsandbox:date-annotator')['textDateAnnotations'] if 'text_person_name' in all_annotation_types: annotations['text_person_name'] = client.annotate_note( host=config.person_name_annotator_api_url, note=client_note, tool_type='nlpsandbox:person-name-annotator' )['textPersonNameAnnotations'] if 'text_location' in all_annotation_types: annotations['text_location'] = client.annotate_note( host=config.location_annotator_api_url, note=client_note, tool_type='nlpsandbox:location-annotator' )['textLocationAnnotations'] if 'text_contact' in all_annotation_types: annotations['text_contact'] = client.annotate_note( host=config.contact_annotator_api_url, note=client_note, tool_type='nlpsandbox:contact-annotator' )['textContactAnnotations'] if 'text_id' in all_annotation_types: annotations['text_id'] = client.annotate_note( host=config.id_annotator_api_url, note=client_note, tool_type='nlpsandbox:id-annotator')['textIdAnnotations'] # De-identify note deidentified_note = \ Note.from_dict({note.attribute_map[key]: value for key, value in note.to_dict().items()}) deidentified_annotations = \ {key: value.copy() for key, value in annotations.items()} deid_config: DeidentificationStep for deid_config in deid_request.deidentification_steps: if deid_config.masking_char_config is not None: masking_char = \ deid_config.masking_char_config.masking_char deidentified_note, deidentified_annotations = \ apply_masking_char( deidentified_note, deidentified_annotations, deid_config.confidence_threshold, deid_config.annotation_types, masking_char ) elif deid_config.redact_config \ is not None: deidentified_note, deidentified_annotations = apply_redaction( deidentified_note, deidentified_annotations, deid_config.confidence_threshold, deid_config.annotation_types) elif deid_config.annotation_type_mask_config \ is not None: deidentified_note, deidentified_annotations = \ apply_annotation_type( deidentified_note, deidentified_annotations, deid_config.confidence_threshold, deid_config.annotation_types ) else: return "No supported de-identification method supported in " \ "request: '%s'" % ( str(deid_config.to_dict()),), 400 deidentify_response = DeidentifyResponse( deidentified_note=deidentified_note, original_annotations=AnnotationSet( text_date_annotations=annotations['text_date'], text_person_name_annotations=annotations['text_person_name'], text_location_annotations=annotations['text_location'], text_contact_annotations=annotations['text_contact'], text_id_annotations=annotations['text_id'], ), deidentified_annotations=AnnotationSet( text_date_annotations=deidentified_annotations['text_date'], text_person_name_annotations=deidentified_annotations[ 'text_person_name'], text_location_annotations=deidentified_annotations[ 'text_location'], text_contact_annotations=deidentified_annotations[ 'text_contact'], text_id_annotations=deidentified_annotations['text_id'], )) return deidentify_response, 200