sparknlp_jsl.annotator.deid.doccument_hashcoder#

Contains classes for Doc2Chunk.

Module Contents#

Classes#

DocumentHashCoder

Shifts date information for deidentification purposes.

class DocumentHashCoder#

Bases: sparknlp.internal.AnnotatorTransformer, sparknlp_jsl.common.AnnotatorProperties

Shifts date information for deidentification purposes.

This annotator can replace dates in a column of DOCUMENT type according with the hash code of any other column. It uses the hash of the specified column and creates a new document column containing the day shift information. In sequence, the DeIdentification annotator deidentifies the document with the shifted date information.

If the specified column contains strings that can be parsed to integers, use those numbers to make the shift in the data accordingly.

Input Annotation types

Output Annotation type

DOCUMENT

DOCUMENT

Parameters:
  • patientIdColumn (str) – column that contains string. Must be part of DOCUMENT

  • dateShiftColumn (str) – column containing the date shift information

  • newDateShift (str) – column that has a reference of where chunk begins

  • seed (int) – seed for the random number generator

  • rangeDays (int) – range of days to be sampled by the random generator

Examples

>>> import pandas as pd
>>> data = pd.DataFrame(
...     {
...         "patientID": ["A001", "A001", "A003", "A003"],
...         "text": [
...             "Chris Brown was discharged on 10/02/2022",
...             "Mark White was discharged on 10/04/2022",
...             "John was discharged on 15/03/2022",
...             "John Moore was discharged on 15/12/2022",
...         ],
...         "dateshift": ["10", "10", "30", "30"],
...     }
... )
>>> my_input_df = spark.createDataFrame(data)
>>> documentAssembler = DocumentAssembler().setInputCol("text").setOutputCol("document")
>>> documentHasher = (
...     DocumentHashCoder()
...     .setInputCols("document")
...     .setOutputCol("document2")
...     .setDateShiftColumn("dateshift")
... )
>>> tokenizer = Tokenizer().setInputCols(["document2"]).setOutputCol("token")
>>> embeddings = (
...     WordEmbeddingsModel.pretrained("embeddings_clinical", "en", "clinical/models")
...     .setInputCols(["document2", "token"])
...     .setOutputCol("word_embeddings")
... )
>>> clinical_ner = (
...     MedicalNerModel.pretrained("ner_deid_subentity_augmented", "en", "clinical/models")
...     .setInputCols(["document2", "token", "word_embeddings"])
...     .setOutputCol("ner")
... )
>>> ner_converter = (
...     NerConverter().setInputCols(["document2", "token", "ner"]).setOutputCol("ner_chunk")
... )
>>> de_identification = (
...     DeIdentification()
...     .setInputCols(["ner_chunk", "token", "document2"])
...     .setOutputCol("deid_text")
...     .setMode("obfuscate")
...     .setObfuscateDate(True)
...     .setDateTag("DATE")
...     .setLanguage("en")
...     .setObfuscateRefSource("faker")
...     .setUseShifDays(True)
... )
>>> pipeline_col = Pipeline().setStages(
...     [
...         documentAssembler,
...         documentHasher,
...         tokenizer,
...         embeddings,
...         clinical_ner,
...         ner_converter,
...         de_identification,
...     ]
... )
>>> empty_data = spark.createDataFrame([["", "", ""]]).toDF(
...     "patientID", "text", "dateshift"
... )
>>> pipeline_col_model = pipeline_col.fit(empty_data)
>>> output = pipeline_col_model.transform(my_input_df)
>>> output.select("text", "dateshift", "deid_text.result").show(truncate=False)
+----------------------------------------+---------+----------------------------------------------+
text                                    |dateshift|result                                        |
+----------------------------------------+---------+----------------------------------------------+
Chris Brown was discharged on 10/02/2022|10       |[Ellender Manual was discharged on 20/02/2022]|
Mark White was discharged on 10/04/2022 |10       |[Errol Bang was discharged on 20/04/2022]     |
John was discharged on 15/03/2022       |30       |[Ariel Null was discharged on 14/04/2022]     |
John Moore was discharged on 15/12/2022 |30       |[Jean Cotton was discharged on 14/01/2023]    |
+----------------------------------------+---------+----------------------------------------------+
dateShiftColumn#
getter_attrs = []#
inputAnnotatorTypes#
inputCols#
lazyAnnotator#
name = DocumentHashCoder#
newDateShift#
optionalInputAnnotatorTypes = []#
outputAnnotatorType#
outputCol#
patientIdColumn#
rangeDays#
seed#
clear(param)#

Clears a param from the param map if it has been explicitly set.

copy(extra=None)#

Creates a copy of this instance with the same uid and some extra params. This implementation first calls Params.copy and then make a copy of the companion Java pipeline component with extra params. So both the Python wrapper and the Java pipeline component get copied.

Parameters:

extra (dict, optional) – Extra parameters to copy to the new instance

Returns:

Copy of this instance

Return type:

JavaParams

explainParam(param)#

Explains a single param and returns its name, doc, and optional default value and user-supplied value in a string.

explainParams()#

Returns the documentation of all params with their optionally default values and user-supplied values.

extractParamMap(extra=None)#

Extracts the embedded default param values and user-supplied values, and then merges them with extra values from input into a flat param map, where the latter value is used if there exist conflicts, i.e., with ordering: default param values < user-supplied values < extra.

Parameters:

extra (dict, optional) – extra param values

Returns:

merged param map

Return type:

dict

getInputCols()#

Gets current column names of input annotations.

getLazyAnnotator()#

Gets whether Annotator should be evaluated lazily in a RecursivePipeline.

getOrDefault(param)#

Gets the value of a param in the user-supplied param map or its default value. Raises an error if neither is set.

getOutputCol()#

Gets output column name of annotations.

getParam(paramName)#

Gets a param by its name.

getParamValue(paramName)#

Gets the value of a parameter.

Parameters:

paramName (str) – Name of the parameter

hasDefault(param)#

Checks whether a param has a default value.

hasParam(paramName)#

Tests whether this instance contains a param with a given (string) name.

inputColsValidation(value)#
isDefined(param)#

Checks whether a param is explicitly set by user or has a default value.

isSet(param)#

Checks whether a param is explicitly set by user.

classmethod load(path)#

Reads an ML instance from the input path, a shortcut of read().load(path).

classmethod read()#

Returns an MLReader instance for this class.

save(path)#

Save this ML instance to the given path, a shortcut of ‘write().save(path)’.

set(param, value)#

Sets a parameter in the embedded param map.

setDateShiftColumn(value)#

Sets column to be used for hash or predefined shift.

Parameters:

value (str) – Name of the column to be used

setInputCols(*value)#

Sets column names of input annotations.

Parameters:

*value (List[str]) – Input columns for the annotator

setLazyAnnotator(value)#

Sets whether Annotator should be evaluated lazily in a RecursivePipeline.

Parameters:

value (bool) – Whether Annotator should be evaluated lazily in a RecursivePipeline

setNewDateShift(value)#

Sets column that has a reference of where chunk begins.

Parameters:

value (str) – Name of the reference column

setOutputCol(value)#

Sets output column name of annotations.

Parameters:

value (str) – Name of output column

setParamValue(paramName)#

Sets the value of a parameter.

Parameters:

paramName (str) – Name of the parameter

setParams()#

Sets the given parameters.

setPatientIdColumn(value)#

Sets column that contains the string.

Parameters:

value (str) – Name of the column containing patient ID

setRangeDays(value)#

Sets the range of dates to be sampled from.

Parameters:

value (int) – range of days to be sampled by the random generator

setSeed(value)#

Sets the seed for random number generator.

Parameters:

value (int) – seed for the random number generator

transform(dataset, params=None)#

Transforms the input dataset with optional parameters.

New in version 1.3.0.

Parameters:
  • dataset (pyspark.sql.DataFrame) – input dataset

  • params (dict, optional) – an optional param map that overrides embedded params.

Returns:

transformed dataset

Return type:

pyspark.sql.DataFrame

write()#

Returns an MLWriter instance for this ML instance.