def fix_SOPReferencedMacro(ds: Dataset, log: list, suggested_SOPClassUID: dict = {}): kw = 'ReferencedStudySequence' tg = tag_for_keyword(kw) if tg not in ds: return val = ds[tg].value if len(val) == 0: del ds[tg] return i = 0 while i < len(val): item = val[i] msg = ErrorInfo() if 'ReferencedSOPInstanceUID' not in item: msg.msg = 'General Fix - item {}/{} <ReferencedStudySequence>'\ ' lacks <ReferencedSOPInstanceUID> attribute'.format(i + 1, len(val)) msg.fix = 'fixed by removint the item' log.append(msg.getWholeMessage()) val.pop(i) continue if item['ReferencedSOPInstanceUID'].is_empty: msg.msg = 'General Fix - item {}/{} <ReferencedStudySequence>'\ ' holds an empty <ReferencedSOPInstanceUID> attribute'.format(i + 1, len(val)) msg.fix = 'fixed by removint the item' log.append(msg.getWholeMessage()) val.pop(i) continue if 'ReferencedSOPClassUID' not in item or\ item['ReferencedSOPClassUID'].is_empty: uid = item['ReferencedSOPInstanceUID'].value msg.msg = 'General Fix - item {}/{} <ReferencedStudySequence>'\ ' lacks <ReferencedSOPClassUID> attribute'.format(i + 1, len(val)) if uid not in suggested_SOPClassUID: msg.fix = 'fixed by removint the item' log.append(msg.getWholeMessage()) val.pop(i) continue else: msg.fix = 'fixed by querying the attribute '\ 'ReferencedSOPClassUID filling it with {}'.format( suggested_SOPClassUID[uid] ) log.append(msg.getWholeMessage()) item['ReferencedSOPClassUID'].value = suggested_SOPClassUID[ uid] i += 1 if len(val) == 0: msg = ErrorInfo() msg.msg = 'General Fix - Attribute <{}> is empty '.format(kw) msg.fix = 'fixed by removint the attribute' log.append(msg.getWholeMessage()) del ds[tg] return
def add_anatomy(ds: Dataset, BodyPartExamined_value: str, AnatomicRegionSequence_value: tuple, log: list, check_consistency: bool = True): bpe = tag_for_keyword('BodyPartExamined') ars = tag_for_keyword('AnatomicRegionSequence') old_bpe, old_ars = get_old_anatomy(ds) if old_bpe is None: if check_consistency: new_bpe, new_ars = \ CorrectAnatomicInfo( BodyPartExamined_value, AnatomicRegionSequence_value) else: new_bpe, new_ars = (BodyPartExamined_value, AnatomicRegionSequence_value) else: new_bpe, new_ars = CorrectAnatomicInfo(old_bpe, old_ars) if old_bpe != new_bpe and new_bpe is not None: bpe_a = DataElementX(bpe, dictionary_VR(bpe), new_bpe) old_bpe_txt = old_bpe if bpe not in ds else ds[bpe].value ds[bpe] = bpe_a msg = ErrorInfo() msg.msg = 'General Fix - {}'.format("<BodyPartExamined> is absent") msg.fix = "fixed by setting the <BodyPartExamined>"\ "from {} to '{}'".format(old_bpe_txt, new_bpe) log.append(msg.getWholeMessage()) if is_accurate_code_seq(new_ars) and not is_code_equal(old_ars, new_ars): code_value, code_meaning, coding_scheme_designator = new_ars if ars in ds: old_item_text = subfix_CodeSeqItem2txt(ds[ars], 0) else: old_item_text = 'None' new_item = CodeSeqItemGenerator(str(code_value), code_meaning, coding_scheme_designator) ars_a = DataElementX(ars, 'SQ', DicomSequence([ new_item, ])) ds[ars] = ars_a msg = ErrorInfo() msg.msg = 'General Fix - {}'.format( "<AnatomicRegionSequence> is absent") msg.fix = "fixed by setting the <AnatomicRegionSequence>"\ "from {} to '{}'".format( old_item_text, subfix_CodeSeqItem2txt(ars_a, 0)) log.append(msg.getWholeMessage()) AddLaterality(ds, log)
def AddLaterality(ds: Dataset, log: list): needlaterality = checkLaterality(ds, 0, ds) old_laterality = None if "Laterality" not in ds else ds['Laterality'].value AddImageLateralityForBoth = False if needlaterality: if old_laterality is None: AddImageLateralityForBoth = True if old_laterality is not None: if old_laterality not in ['L', 'R']: if old_laterality.lower() == 'left': new_laterality = 'L' elif old_laterality.lower() == 'right': new_laterality = 'R' else: new_laterality = None if new_laterality is not None: msg = ErrorInfo() msg.msg = 'General Fix - {}'.format( "<Laterality> holds wrong value") msg.fix = "fixed by setting the <Laterality>"\ "from {} to '{}'".format(old_laterality, new_laterality) log.append(msg.getWholeMessage()) ds['Laterlaity'].value = new_laterality return else: AddImageLateralityForBoth = True else: return elif 'Laterality' in ds: del ds['Laterality']
def generalfix_AddPresentationLUTShape(ds: Dataset, log: list) -> bool: fixed = False photo_in_tg = tag_for_keyword('PhotometricInterpretation') if photo_in_tg not in ds: return fixed photo_in_v = ds[photo_in_tg].value pres_lut_shape_tg = tag_for_keyword('PresentationLUTShape') if pres_lut_shape_tg in ds: pres_lut_shape_a = ds[pres_lut_shape_tg] else: pres_lut_shape_a = DataElementX(pres_lut_shape_tg, dictionary_VR(pres_lut_shape_tg), '') old_pls = pres_lut_shape_a.value if photo_in_v == 'MONOCHROME2' and old_pls != 'IDENTITY': new_pls = 'IDENTITY' pres_lut_shape_a.value = new_pls fixed = True elif photo_in_v == 'MONOCHROME1' and old_pls != 'INVERSE': new_pls = 'INVERSE' pres_lut_shape_a.value = new_pls fixed = True if fixed: ds[pres_lut_shape_tg] = pres_lut_shape_a msg = ErrorInfo() msg.msg = 'General Fix - {}'.format( "<PresentationLUTShape> is wrong or absent") msg.fix = "fixed by setting the <PresentationLUTShape>"\ " from '{}' to '{}'".format(old_pls, new_pls) log.append(msg.getWholeMessage()) return fixed
def generalfix_CheckAndFixModality(ds: Dataset, log: list) -> bool: fixed = False modality_sop = { CTImageStorageSOPClassUID: 'CT', MRImageStorageSOPClassUID: 'MR', PETImageStorageSOPClassUID: 'PT', } if 'SOPClassUID' in ds: sop_class = ds['SOPClassUID'].value else: return False if sop_class not in modality_sop: return False mod_tg = tag_for_keyword('Modality') if mod_tg in ds: modality = ds[mod_tg].value else: modality = '' if modality == '' or modality != modality_sop[sop_class]: ds[mod_tg] = DataElementX(mod_tg, dictionary_VR(mod_tg), modality_sop[sop_class]) msg = ErrorInfo() msg.msg = 'General Fix - {}'.format("<Modality> is wrong or absent") msg.fix = "fixed by reading the <SOPClassUID> and setting <Modality>"\ " from '{}' to '{}'".format(modality, modality_sop[sop_class]) log.append(msg.getWholeMessage()) fixed = True return fixed
def generalfix_RemoveUnwanterPixelAspctRatio(ds: Dataset, log: list) -> bool: fixed = False kw = "PixelAspectRatio" is_one_to_one = False if kw in ds: elem = ds[kw] if type(elem) == MultiValue: if len(elem) == 2: is_one_to_one = (elem.value[0] == elem.value[2]) if (Condition_UnwantedPixelAspectRatioWhenPixelSpacingPresent( ds, ds, ds ) or Condition_UnwantedPixelAspectRatioWhenImagerPixelSpacingPresent( ds, ds, ds ) or Condition_UnwantedPixelAspectRatioWhenNominalScannedPixelSpacingPresent( ds, ds, ds ) or Condition_UnwantedPixelAspectRatioWhenSharedPixelMeasuresMacro( ds, ds, ds ) or Condition_UnwantedPixelAspectRatioWhenPerFramePixelMeasuresMacro( ds, ds, ds) or Condition_UnwantedPixelAspectRatioWhenMPEG2MPHLTransferSyntax( ds, ds, ds) or is_one_to_one): msg = ErrorInfo() msg.msg = '{} Error - {}'.format( ErrorType.BadValue.value, "<PixelAspectRatio> is 1:1 or redundant") msg.fix = "fixed by removing the attribute" log.append(msg.getWholeMessage()) del ds["PixelAspectRatio"] fixed = True return fixed