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']
Пример #4
0
def verifyType2C(ds: Dataset, module: str, element: str, verbose: bool,
                 log: list, fix_trivials: bool, condition_function, mbpo: bool,
                 parent_ds: Dataset, root_ds: Dataset, multiplicityMin: uint32,
                 multiplicityMax: uint32) -> bool:
    #    Type 2C - Conditional Data Element (May be Empty)

    err_not_exists = False
    err_vr = False
    err_vm = False
    reason = ""
    if condition_function == 0:
        conditionNotSatisfied = True
    else:
        conditionNotSatisfied = not condition_function(ds, parent_ds, root_ds)
    if element in ds:
        elem = ds[element]
    else:
        if not conditionNotSatisfied:
            reason = "Error - T<{}> {}"
            reason = reason.format(ErrorType.Type2CAbsent.value,
                                   MMsgDC("MissingAttribute"))
        err_not_exists = True
    if not err_not_exists:
        if condition_function != 0 and conditionNotSatisfied and not mbpo:
            reason = "Error - T<{}> {}"
            reason = reason.format(
                ErrorType.Type2CPresent.value,
                MMsgDC(
                    "AttributePresentWhenConditionUnsatisfiedWithoutMayBePresentOtherwise"
                ))
            if fix_trivials:
                tmp = ErrorInfo(reason + "{} {}", fix_by_removing(ds, element))
                reason = tmp.getWholeMessage()
        if elem.is_empty:
            if not verifyVR(elem, module, element, verbose, log):
                reason = "Error - T<{}> {}"
                reason = reason.format(ErrorType.Type2CVR.value,
                                       MMsgDC("BadValueRepresentation"))
                err_vr = True
            else:
                if not verifyVM(elem, module, element, verbose, log,
                                multiplicityMin, multiplicityMax, "source"):
                    reason = "Error - T<{}> {}"
                    reason = reason.format(
                        ErrorType.Type2CVM.value,
                        MMsgDC("BadAttributeValueMultiplicity"))
                    err_vm = True
    if len(reason) != 0:
        if fix_trivials:
            ViolationMessage(reason, MMsgDC("Type2C"), module, element, log,
                             verbose, True)
        else:
            ViolationMessage(reason, MMsgDC("Type2C"), module, element, log,
                             verbose)
    else:
        ValidMessage(MMsgDC("Type2C"), module, element, log, verbose)
    return len(reason) == 0
Пример #5
0
def verifyType2(ds: Dataset, module: str, element: str, verbose: bool,
                log: list, fix_trivials: bool, multiplicityMin: uint32,
                multiplicityMax: uint32) -> bool:
    # Type 2 - Required Data Element (May be Empty)
    err_vr = False
    err_vm = False
    err_not_exists = False
    reason = ""
    if element in ds:
        elem = ds[element]
    else:
        reason = "Error - T<{}> {}"
        reason = reason.format(ErrorType.Type2.value,
                               MMsgDC("MissingAttribute"))
        err_not_exists = True
        if fix_trivials:  # add an empty attrib
            tmp = ErrorInfo(reason + "{} {}",
                            fix_ByAddingEmptyAttrib(ds, element))
            reason = tmp.getWholeMessage()
    if not err_not_exists:
        # do not check emptiness
        if not elem.is_empty:
            if not verifyVR(elem, module, element, verbose, log):
                reason = "Error - T<{}> {}"
                reason = reason.format(ErrorType.Type2VR.value,
                                       MMsgDC("BadValueRepresentation"))
                err_vr = True
            else:
                if not verifyVM(elem, module, element, verbose, log,
                                multiplicityMin, multiplicityMax):
                    reason = "Error - T<{}> {}"
                    reason = reason.format(
                        ErrorType.Type2VM.value,
                        MMsgDC("BadAttributeValueMultiplicity"))
                    err_vm = True
    if len(reason) != 0:
        if fix_trivials:
            ViolationMessage(reason, MMsgDC("Type2"), module, element, log,
                             verbose, True)
        else:
            ViolationMessage(reason, MMsgDC("Type2"), module, element, log,
                             verbose)
    else:
        ValidMessage(MMsgDC("Type2"), module, element, log, verbose)
    return len(reason) == 0