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_VM1(ds, log): fixed = False elemsTobeCorrected = [] for key, a in ds.items(): a = ds[key] if key.is_private: continue try: dvr = dictionary_VM(key) except (BaseException): return fixed if dvr != '1': continue if a.VM <= 1: continue if (a.VR != 'LT' or a.VR != 'LO'): continue concat = '/'.join(a.value) ds[key] = DataElementX(key, a.VR, concat) fixed = True err = "<{}> {}".format(a.keyword, validate_vr.tag2str(a.tag)) msg = ErrorInfo( "General Fix - Value Multiplicity for {} "\ "is not allowed to be more than one".format(err), "fixed by concatenating all itmes into one {}".format(concat)) log.append(msg.getWholeMessage()) fixed = True 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
def generalfix_TrailingNulls(ds: Dataset, log: list) -> bool: fixed = False elemsTobeCorrected = [] for key, a in ds.items(): a = ds[key] if key.is_private: continue if a.VR == 'UI' or a.VR == 'OB' or a.VR == 'OW' or a.VR == 'UN': continue if type(a) == pydicom.dataelem.RawDataElement: a = pydicom.dataelem.DataElement_from_raw(a) if type(a.value) == Sequence: for item in a.value: fixed = fixed or generalfix_TrailingNulls(item, log) elif type(a.value) == Dataset: fixed = fixed or generalfix_TrailingNulls(a.value, log) else: partial_fixed = subfix_HasTrailingNulls(a) if partial_fixed: err = "<{}> {}".format(a.keyword, validate_vr.tag2str(a.tag)) msg = ErrorInfo( "General Fix - Trailing null bytesz", "fixed by removing the trailing null bytes for {}".format( err)) log.append(msg.getWholeMessage()) elemsTobeCorrected.append(a) fixed = True return fixed
def generalfix_RemoveEmptyCodes(parent_ds: Dataset, log: list, kw_to_be_removed: list = []) -> bool: global iod_dict if 'SOPClassUID' in parent_ds: iod_dict = get_full_attrib_list(parent_ds) fixed = False for key, elem in parent_ds.items(): elem = parent_ds[key] if type(elem) == pydicom.dataelem.RawDataElement: elem = pydicom.dataelem.DataElement_from_raw(elem) if type(elem.value) == Sequence: if elem.keyword.endswith("CodeSequence"): if iod_dict is None or elem.keyword not in iod_dict: type_ = None else: type_ = iod_dict[elem.keyword][0]['type'] if type_ != '1' or type_ != '1C' or type_ != '2'\ or type_ != '2C': fixed = fixed or subfix_checkandfixBasicCodeSeq(elem, log) if elem.is_empty: kw_to_be_removed.append((parent_ds, elem.keyword)) else: for (item, idx) in zip(elem.value, range(0, len(elem.value))): fixed = fixed or generalfix_RemoveEmptyCodes( item, log, kw_to_be_removed) elif type(elem.value) == Dataset: fixed = fixed or generalfix_RemoveEmptyCodes( elem.value, log, kw_to_be_removed, ) if 'SOPClassUID' in parent_ds: for ddss, k in kw_to_be_removed: if k in ddss: a = ddss[k] # print('{} is present and going to be removed'.format(k)) else: # print('{} is not present and going to continue anyway'.format(k)) continue if iod_dict is None or k not in iod_dict: type_ = None else: type_ = iod_dict[k][0]['type'] if type_ != '1' or type_ != '1C' or type_ != '2' or type_ != '2C': err = "{} <{}>".format(validate_vr.tag2str(a.tag), a.keyword) msg = ErrorInfo( "General Fix - Attribute type SQ has no items", "fixed by removing the whole attribute for {}".format(err)) log.append(msg.getWholeMessage()) del ddss[k]
def check(v, n, N, a): u = v.upper() if u != v: err = "<{}> {}".format(a.keyword, validate_vr.tag2str(a.tag)) msg = ErrorInfo( "General Fix - {} lower case is not allowed for Code "\ "String itme number {}/ {}".format(err, n, N), "fixed by capitalizing the value from {} to {}".format( v, u)) log.append(msg.getWholeMessage()) return u else: return None
def generalfix_WindowWidth(ds, log): fixed = False wwkw = 'WindowWidth' wwtg = tag_for_keyword(wwkw) if wwtg in ds: if ds[wwtg].value == 0: ds[wwtg].value = 1 err = "<{}> {}".format(wwkw, validate_vr.tag2str(wwtg)) msg = ErrorInfo( "General Fix - Window width {} "\ "is not allowed to be 0".format(err), "fixed by replacing it with 1") log.append(msg.getWholeMessage()) fixed = True return fixed
def generalfix_RealWorldValueMappingSequence(ds, log): kw = 'RealWorldValueMappingSequence' tg = tag_for_keyword(kw) if tg in ds: v = ds[tg].value for i, item in enumerate(v): in_key = 'LUTLabel' in_tg = tag_for_keyword(in_key) if in_tg not in item: new_el = DataElementX(in_tg, 'SH', 'Unknown') item[in_tg] = new_el err = "<{}> {}".format(in_key, validate_vr.tag2str(in_tg)) msg = ErrorInfo( "General Fix - The item number {} lacks {}".format(i, err), "fixed by adding a new element with value <{}>".format( new_el.value)) log.append(msg.getWholeMessage())
def check(v, n, N, a): v = str(v) if len(v) > 16: f = float(v) u = '{:1.10E}'.format(f) n = 10 while len(u) > 16 and n > 0: n -= 1 ptrn = '{{:1.{}E}}'.format(n) u = ptrn.format(f) err = "<{}> {}".format(a.keyword, validate_vr.tag2str(a.tag)) msg = ErrorInfo( "General Fix - {} value length greater than 16 is not allowed"\ "for DS value representation in itme number {}/ {}".format( err, n, N), "fixed by modifying the precision from {} to {}".format( v, u)) log.append(msg.getWholeMessage()) return u else: return None
def generalfix_CT_RescaleType(ds, log): fixed = False modality = None if "Modality" not in ds else ds['Modality'].value if modality == 'CT': if 'RescaleType' not in ds: return fixed if ds['RescaleType'] == 'HU': return fixed contains_localizer = False ImageType_v = [] if 'ImageType' not in ds else ds['ImageType'].value for i in ImageType_v: if i == 'LOCALIZER': contains_localizer = True break if not contains_localizer: oldval = ds['RescaleType'].value ds['RescaleType'].value = "HU" msg = mesgtext_cc.ErrorInfo() msg.msg = "General Fix - <RescaletType> is not HU (Hounsfield Units) " msg.fix = "fixed by changing the attribute from {} to {}".format( oldval, 'HU') log.append(msg.getWholeMessage()) fixed = True return fixed
def generalfix_MisplacedAttributes(ds: Dataset, log: list): paths = {} current_folder = os.path.dirname(__file__) with open(os.path.join(current_folder, 'config.json')) as json_file: fix_config = json.load(json_file) get_all_kw_paths(ds, [], paths) standard_ds = get_full_attrib_list(ds) not_in_std = {} misplaced = {} for kw, pp in paths.items(): tg = tag_for_keyword(kw) if kw not in standard_ds: not_in_std[kw] = pp else: std__ = standard_ds[kw] if len(std__) > 1: continue for parent, path in pp: correct_path = std__[0]['path'] if path != correct_path: if kw not in misplaced: misplaced[kw] = [(parent, path, correct_path)] else: misplaced[kw].append((parent, path, correct_path)) for kw, val in misplaced.items(): if not fix_config["MisplacedAttributes"][ "DisplaceIfAttributeIsInWrongPath"]: break for parent, path, correct_path in val: # print(parent, path, correct_path) delem = parent[kw] del parent[kw] new_paretn = ds inner_kw = kw parent_ds = ds for p in correct_path: if p not in parent_ds: new_ds = Dataset() parent_ds[p] = DataElementX(tag_for_keyword(p), 'SQ', DataElementSequence([new_ds])) elif parent_ds[p].is_empty: new_ds = Dataset() parent_ds[p].value = DataElementSequence([new_ds]) parent_ds = parent_ds[p].value[0] if kw not in parent_ds: parent_ds[kw] = delem msg = ErrorInfo( "General Fix - The DICOM attribute <{}> is not in " "correct place. " "current-path = {} vs. correct-path = {}".format( kw, path, correct_path), "fixed by attribute displacement") else: msg = ErrorInfo( "General Fix - The DICOM attribute <{}> is not in " "correct place. " "current-path = {} vs. correct-path = {}".format( kw, path, correct_path), "couldn't fix because there is already " "an attribute in correct place") log.append(msg.getWholeMessage()) for kw, val in not_in_std.items(): if not fix_config["MisplacedAttributes"]["RemoveIfAttributeIsNotInIOD"]: break for parent, path in val: if kw in parent: del parent[kw] msg = ErrorInfo( "General Fix - The keyword <{}> is not in " "standard DICOM IOD".format(kw), "fixed by removing the attribute") log.append(msg.getWholeMessage())