def add_tag_to_json()

in pathology/transformation_pipeline/ingestion_lib/dicom_gen/dicom_schema_util.py [0:0]


  def add_tag_to_json(self, dataset: MutableMapping[str, Any]):
    """Output tag value to json.

    Args:
      dataset: Json Dict to write tag to.

    Raises:
      DICOMSchemaTagError: error in schema tag def
    """
    if len(self.vr) == 1:
      vr = list(self.vr)[0]
    else:
      schema_vr_type = self._schema_def.get('VR')
      if schema_vr_type is None:
        raise DICOMSchemaTagError(
            self,
            'Tag defines multiple VR types. Specify type. "VR": "<value>" ',
        )
      if schema_vr_type in self.vr:
        vr = schema_vr_type
      else:
        raise DICOMSchemaTagError(self, 'Specified VR type does not match tag.')
    data_element = {'vr': vr}
    if not self.is_seq:
      # if setting a tag value
      if self._value is not None:
        # if tag has a value set data element to write
        value = str(self._value)
        if dicom_standard.dicom_standard_util().is_vr_float_type(set([vr])):
          value = float(value)
        elif dicom_standard.dicom_standard_util().is_vr_int_type(set([vr])):
          value = int(value)
        if vr == 'PN':
          data_element['Value'] = [{'Alphabetic': value}]
        elif vr in ('OB', 'OD', 'OF', 'OW', 'UN'):
          data_element['InlineBinary'] = value
        elif vr == 'AT':
          data_element['Value'] = [dicomtag.DicomTag.standardize_address(value)]
        elif isinstance(value, list):
          data_element['Value'] = value
        else:
          data_element['Value'] = [value]
    else:
      # if setting a sequence initialize the sequence to write
      child_datasets = []
      for child in self._value:
        if child.is_any_value_set():
          dataset_json = child.get_dicom_json()
          child_datasets.append(dataset_json)
      if child_datasets:
        data_element['Value'] = child_datasets
    # set the output to either just a null initialized if tag vr type is 2
    # requiring null initialization.
    # null initialization = "address": { "vr": "TagValue"}
    # or initialized  = "address": { "vr": "TagValue", "Value": [ stuff]}
    if self.write_uninitialized or 'Value' in data_element:
      dataset[self._address] = data_element