From f879b8b886f8f5ed5bd62b27e70c64b5901795cf Mon Sep 17 00:00:00 2001 From: paulojamorim Date: Mon, 23 Jan 2012 18:41:00 +0000 Subject: [PATCH] ENH: Added conversion between DICOM and Python encoding --- invesalius/constants.py | 22 +++++++++++++++++++++- invesalius/reader/dicom.py | 152 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------------------------------------------------------- invesalius/reader/dicom_reader.py | 45 +++++++++++++++++++++++++-------------------- 3 files changed, 118 insertions(+), 101 deletions(-) diff --git a/invesalius/constants.py b/invesalius/constants.py index 19fbea5..cb86376 100644 --- a/invesalius/constants.py +++ b/invesalius/constants.py @@ -441,7 +441,8 @@ VTK_WARNING = 0 [ID_DICOM_IMPORT, ID_PROJECT_OPEN, ID_PROJECT_SAVE_AS, ID_PROJECT_SAVE, ID_PROJECT_CLOSE, ID_PROJECT_INFO, ID_SAVE_SCREENSHOT, ID_DICOM_LOAD_NET, -ID_PRINT_SCREENSHOT, ID_EXIT, ID_IMPORT_OTHERS_FILES, ID_ANALYZE_IMPORT, ID_PREFERENCES] = [wx.NewId() for number in range(13)] +ID_PRINT_SCREENSHOT, ID_EXIT, ID_IMPORT_OTHERS_FILES, ID_ANALYZE_IMPORT, ID_PREFERENCES, +ID_DICOM_NETWORK] = [wx.NewId() for number in range(14)] [ID_EDIT_UNDO, ID_EDIT_REDO, ID_EDIT_LIST] =\ @@ -509,3 +510,22 @@ STYLE_LEVEL = {SLICE_STATE_EDITOR: 1, RENDERING = 0 SURFACE_INTERPOLATION = 1 LANGUAGE = 2 + + +#Correlaction extracted from pyDicom +DICOM_ENCODING_TO_PYTHON = { + 'None':'iso8859', + None:'iso8859', + '': 'iso8859', + 'ISO_IR 6': 'iso8859', + 'ISO_IR 100': 'latin_1', + 'ISO 2022 IR 87': 'iso2022_jp', + 'ISO 2022 IR 13': 'iso2022_jp', + 'ISO 2022 IR 149': 'euc_kr', + 'ISO_IR 192': 'UTF8', + 'GB18030': 'GB18030', + 'ISO_IR 126': 'iso_ir_126', + 'ISO_IR 127': 'iso_ir_127', + 'ISO_IR 138': 'iso_ir_138', + 'ISO_IR 144': 'iso_ir_144', + } diff --git a/invesalius/reader/dicom.py b/invesalius/reader/dicom.py index 76f761c..d85379c 100644 --- a/invesalius/reader/dicom.py +++ b/invesalius/reader/dicom.py @@ -22,7 +22,7 @@ import time #import vtkgdcm import sys import utils - +import constants as const # In DICOM file format, if multiple values are present for the # "Window Center" (Level) and "Window Width", both attributes # shall have the same number of values and shall be considered as @@ -204,7 +204,7 @@ class Parser(): to the number of columns on the image. Return "" if not defined. """ - data = self.data_image[0x028][0x011] + data = self.data_image[str(0x028)][str(0x011)] if (data): return int(str(data)) return "" @@ -216,7 +216,7 @@ class Parser(): to the number of rows on the image. Return "" if not defined. """ - data = self.data_image[0x028][0x010] + data = self.data_image[str(0x028)][str(0x010)] if (data): return int(str(data)) return "" @@ -303,7 +303,7 @@ class Parser(): """ # TODO: internationalize data try: - date = self.data_image[0x0008][0x0022] + date = self.data_image[str(0x0008)][str(0x0022)] except(KeyError): return "" @@ -318,7 +318,7 @@ class Parser(): DICOM standard tag (0x0020, 0x0012) was used. """ - data = self.data_image[0x0020][0x0012] + data = self.data_image[str(0x0020)][str(0x0012)] if (data): return int(str(data)) return "" @@ -329,7 +329,8 @@ class Parser(): DICOM standard tag (0x0008, 0x0050) was used. """ - data = self.data_image[0x008][0x050] + #data = self.data_image[0x008][0x050] + return "" if (data): try: value = int(str(data)) @@ -346,7 +347,7 @@ class Parser(): DICOM standard tag (0x0008,0x0032) was used. """ - data = self.data_image[0x008][0x032] + data = self.data_image[str(0x008)][str(0x032)] if (data) and (data != ''): return self.__format_time(str(data)) return "" @@ -381,7 +382,7 @@ class Parser(): DICOM standard tag (0x0028,0x1050) was used. """ try: - data = self.data_image[0x028][0x1050] + data = self.data_image[str(0x028)][str(0x1050)] except(KeyError): return "300" if (data): @@ -413,7 +414,7 @@ class Parser(): DICOM standard tag (0x0028,0x1051) was used. """ try: - data = self.data_image[0x028][0x1051] + data = self.data_image[str(0x028)][str(0x1051)] except(KeyError): return "2000" @@ -442,7 +443,7 @@ class Parser(): DICOM standard tag (0x0020, 0x0032) was used. """ try: - data = self.data_image[0x020][0x032] + data = self.data_image[str(0x020)][str(0x032)] except(KeyError): return "" if (data): @@ -457,7 +458,7 @@ class Parser(): DICOM standard tag (0x0020, 0x0032) was used. """ - data = self.data_image[0x020][0x1041] + data = self.data_image[str(0x020)][str(0x1041)] if (data): return eval(data) return "" @@ -470,7 +471,7 @@ class Parser(): DICOM standard tag (0x7fe0, 0x0010) was used. """ try: - data = self.data_image[0x7fe0][0x0010] + data = self.data_image[str(0x7fe0)][str(0x0010)] except(KeyError): return "" @@ -488,7 +489,7 @@ class Parser(): DICOM standard tag (0x0020, 0x0011) was used. """ try: - data = self.data_image[0x020][0x011] + data = self.data_image[str(0x020)][str(0x011)] except(KeyError): return "" @@ -508,7 +509,7 @@ class Parser(): DICOM standard tag (0x0028, 0x0030) was used. """ try: - data = self.data_image[0x0028][0x0030] + data = self.data_image[str(0x0028)][str(0x0030)] except(KeyError): return "" if (data): @@ -523,7 +524,7 @@ class Parser(): DICOM standard tag (0x0010, 0x1030) was used. """ try: - data = self.data_image[0x0010][0x1030] + data = self.data_image[str(0x0010)][str(0x1030)] except(KeyError): return "" @@ -539,7 +540,7 @@ class Parser(): DICOM standard tag (0x0010, 0x1030) was used. """ try: - data = self.data_image[0x010][0x1020] + data = self.data_image[str(0x010)][str(0x1020)] except(KeyError): return "" @@ -554,7 +555,7 @@ class Parser(): DICOM standard tag (0x0010, 0x1040) was used. """ try: - data = self.data_image[0x010][0x1040] + data = self.data_image[str(0x010)][str(0x1040)] except(KeyError): return "" if (data): @@ -569,7 +570,7 @@ class Parser(): DICOM standard tag (0x0010, 0x1080) was used. """ try: - data = self.data_image[0x010][0x1080] + data = self.data_image[str(0x010)][str(0x1080)] except(KeyError): return "" if (data): @@ -586,7 +587,7 @@ class Parser(): DICOM standard tag (0x0010, 0x1081) was used. """ try: - data = self.data_image[0x010][0x1081] + data = self.data_image[str(0x010)][str(0x1081)] except(KeyError): return "" if (data): @@ -602,7 +603,7 @@ class Parser(): DICOM standard tag (0x0010, 0x2150) was used. """ try: - data = self.data_image[0x0010][0x2150] + data = self.data_image[str(0x0010)][str(0x2150)] except(KeyError): return "" @@ -619,7 +620,7 @@ class Parser(): DICOM standard tag (0x0010, 0x2152) was used. """ try: - data = self.data_image[0x0010][0x2152] + data = self.data_image[str(0x0010)][str(0x2152)] except(KeyError): return "" @@ -635,7 +636,7 @@ class Parser(): DICOM standard tag (0x0010, 0x2154) was used. """ try: - data = self.data_image[0x0010][0x2154] + data = self.data_image[str(0x0010)][str(0x2154)] except(KeyError): return "" @@ -652,7 +653,7 @@ class Parser(): DICOM standard tag (0x0010, 0x2297) was used. """ try: - data = self.data_image[0x0010][0x2297] + data = self.data_image[str(0x0010)][str(0x2297)] except(KeyError): return "" @@ -669,7 +670,7 @@ class Parser(): DICOM standard tag (0x0010, 0x2298) was used. """ try: - data = self.data_image[0x0010][0x2298] + data = self.data_image[str(0x0010)][str(0x2298)] except(KeyError): return "" @@ -686,7 +687,7 @@ class Parser(): DICOM standard tag (0x0010, 0x2299) was used. """ try: - data = self.data_image[0x0010][0x2299] + data = self.data_image[str(0x0010)][str(0x2299)] except(KeyError): return "" @@ -703,7 +704,7 @@ class Parser(): DICOM standard tag (0x0010, 0x2000) was used. """ try: - data = self.data_image[0x0010][0x2000] + data = self.data_image[str(0x0010)][str(0x2000)] except(KeyError): return "" @@ -720,7 +721,7 @@ class Parser(): DICOM standard tag (0x0008, 0x2110) was used. """ try: - data = self.data_image[0x0008][0x2110] + data = self.data_image[str(0x0008)][str(0x2110)] except(KeyError): return "" @@ -738,7 +739,7 @@ class Parser(): DICOM standard tag (0x0008, 0x0090) was used. """ try: - data = self.data_image[0x0008][0x0090] + data = self.data_image[str(0x0008)][str(0x0090)] except(KeyError): return "" @@ -757,7 +758,7 @@ class Parser(): DICOM standard tag (0x0008, 0x0092) was used. """ try: - data = self.data_image[0x0008][0x0092] + data = self.data_image[str(0x0008)][str(0x0092)] except(KeyError): return "" @@ -773,7 +774,7 @@ class Parser(): DICOM standard tag (0x0008, 0x0094) was used. """ try: - data = self.data_image[0x0008][0x0094] + data = self.data_image[str(0x0008)][str(0x0094)] except(KeyError): return "" @@ -789,7 +790,7 @@ class Parser(): DICOM standard tag (0x0018, 0x1030) was used. """ try: - data = self.data_image[0x0018][0x1030] + data = self.data_image[str(0x0018)][str(0x1030)] except(KeyError): return None @@ -807,7 +808,7 @@ class Parser(): Critical DICOM tag (0x0008, 0x0008). Cannot be editted. """ try: - data = self.data_image[0x008][0x008] + data = self.data_image[str(0x008)][str(0x008)] except(IndexError): return [] @@ -827,7 +828,7 @@ class Parser(): Critical DICOM tag (0x0008, 0x0016). Cannot be edited. """ try: - data = self.data_image[0x0008][0x0016] + data = self.data_image[str(0x0008)][str(0x0016)] except(KeyError): return "" @@ -844,7 +845,7 @@ class Parser(): Critical DICOM tag (0x0008, 0x0018). Cannot be edited. """ try: - data = self.data_image[0x0008][0x0018] + data = self.data_image[str(0x0008)][str(0x0018)] except(KeyError): return "" @@ -861,7 +862,7 @@ class Parser(): Critical DICOM Tag (0x0020,0x000D). Cannot be edited. """ try: - data = self.data_image[0x0020][0x000D] + data = self.data_image[str(0x0020)][str(0x000D)] except(KeyError): return "" @@ -881,7 +882,7 @@ class Parser(): Critical DICOM tag (0x0020,0x0037). Cannot be edited. """ try: - data = self.data_image[0x0020][0x0037] + data = self.data_image[str(0x0020)][str(0x0037)] except(KeyError): return [1.0, 0.0, 0.0, 0.0, 1.0, 0.0] @@ -899,7 +900,7 @@ class Parser(): Critical DICOM tag (0x0020,0x0037). Cannot be edited. """ try: - data = self.data_image[0x0020][0x0037] + data = self.data_image[str(0x0020)][str(0x0037)] except(KeyError): return [0.0, 1.0, 0.0] @@ -917,7 +918,7 @@ class Parser(): Critical DICOM tag (0x0020,0x0037). Cannot be edited. """ try: - data = self.data_image[0x0020][0x0037] + data = self.data_image[str(0x0020)][str(0x0037)] except(KeyError): return [1.0, 0.0, 0.0] @@ -933,7 +934,7 @@ class Parser(): Critical DICOM tag (0x0020,0x0052). Cannot be edited. """ try: - data = self.data_image[0x0020][0x0052] + data = self.data_image[str(0x0020)][str(0x0052)] except(KeyError): return "" @@ -1013,7 +1014,7 @@ class Parser(): DICOM standard tag (0x0018, 0x1030) was used. """ try: - data = self.data_image[0x0018][0x1030] + data = self.data_image[str(0x0018)][str(0x1030)] if (data): return data except(KeyError): @@ -1037,7 +1038,7 @@ class Parser(): Critical DICOM tag (0x0018, 0x0020). Cannot be edited. """ try: - data = self.data_image[0x0018][0x0020] + data = self.data_image[str(0x0018)][str(0x0020)] except(KeyError): return "" @@ -1053,7 +1054,7 @@ class Parser(): DICOM standard tag (0x0008, 0x0080) was used. """ try: - data = self.data_image[0x0008][0x0080] + data = self.data_image[str(0x0008)][str(0x0080)] except(KeyError): return "" @@ -1071,7 +1072,7 @@ class Parser(): DICOM standard tag (0x0008, 0x0081) was used. """ try: - data = self.data_image[0x0008][0x0081] + data = self.data_image[str(0x0008)][str(0x0081)] except(KeyError): return "" @@ -1088,7 +1089,7 @@ class Parser(): Critical DICOM tag (0x0020, 0x000D). Cannot be edited. """ try: - data = self.data_image[0x0020][0x000D] + data = self.data_image[str(0x0020)][str(0x000D)] except(KeyError): return "" @@ -1104,7 +1105,7 @@ class Parser(): DICOM standard tag (0x0010,0x2180) was used. """ try: - data = self.data_image[0x0010][0x2180] + data = self.data_image[str(0x0010)][str(0x2180)] except(KeyError): return "" @@ -1142,7 +1143,7 @@ class Parser(): #sf.SetFile(self.gdcm_reader.GetFile()) #res = sf.ToStringPair(tag) try: - data = self.data_image[0x0028][0x0100] + data = self.data_image[str(0x0028)][str(0x0100)] except(KeyError): return "" @@ -1161,7 +1162,7 @@ class Parser(): """ # TODO: internationalize data try: - data = self.data_image[0x0010][0x0030] + data = self.data_image[str(0x0010)][str(0x0030)] except(KeyError): return "" @@ -1178,7 +1179,7 @@ class Parser(): DICOM standard tag (0x0020,0x0010) was used. """ try: - data = self.data_image[0x0020][0x0010] + data = self.data_image[str(0x0020)][str(0x0010)] except(KeyError): return "" @@ -1195,7 +1196,7 @@ class Parser(): DICOM standard tag (0x0018,0x1120) was used. """ try: - data = self.data_image[0x0018][0x1120] + data = self.data_image[str(0x0018)][str(0x1120)] except(KeyError): return 0.0 @@ -1214,7 +1215,7 @@ class Parser(): DICOM standard tag (0x0010,0x0040) was used. """ try: - data = self.data_image[0x0010][0x0040] + data = self.data_image[str(0x0010)][str(0x0040)] except(KeyError): return "" @@ -1231,7 +1232,7 @@ class Parser(): DICOM standard tag (0x0010, 0x1010) was used. """ try: - data = self.data_image[0x0010][0x1010] + data = self.data_image[str(0x0010)][str(0x1010)] except(KeyError): return "" @@ -1251,7 +1252,7 @@ class Parser(): DICOM standard tag (0x0010,0x0010) was used. """ try: - data = self.data_image[0x0010][0x0010] + data = self.data_image[str(0x0010)][str(0x0010)] except(KeyError): return "" @@ -1271,7 +1272,7 @@ class Parser(): DICOM standard tag (0x0010,0x0020) was used. """ try: - data = self.data_image[0x0010][0x0020] + data = self.data_image[str(0x0010)][str(0x0020)] except(KeyError): return "" @@ -1291,7 +1292,7 @@ class Parser(): DICOM standard tag (0x0018,0x1151) was used. """ try: - data = self.data_image[0x0018][0x1151] + data = self.data_image[str(0x0018)][str(0x1151)] except(KeyError): return "" @@ -1308,7 +1309,7 @@ class Parser(): DICOM standard tag (0x0018, 0x1152) was used. """ try: - data = self.data_image[0x0018][0x1152] + data = self.data_image[str(0x0018)][str(0x1152)] except(KeyError): return "" @@ -1325,7 +1326,7 @@ class Parser(): DICOM standard tag (0x0018,0x0060) was used. """ try: - data = self.data_image[0x0018][0x0060] + data = self.data_image[str(0x0018)][str(0x0060)] except(KeyError): return "" @@ -1342,7 +1343,7 @@ class Parser(): DICOM standard tag (0x0018,0x0050) was used. """ try: - data = self.data_image[0x0018][0x0050] + data = self.data_image[str(0x0018)][str(0x0050)] except(KeyError): return 0 if (data): @@ -1360,7 +1361,7 @@ class Parser(): DICOM standard tag (0x0018,0x1210) was used. """ try: - data = self.data_image[0x0018][0x1210] + data = self.data_image[str(0x0018)][str(0x1210)] except(KeyError): return "" @@ -1377,7 +1378,7 @@ class Parser(): DICOM standard tag (0x0008,0x0080) was used. """ try: - data = self.data_image[0x0008][0x0080] + data = self.data_image[str(0x0008)][str(0x0080)] except(KeyError): return "" @@ -1394,7 +1395,7 @@ class Parser(): DICOM standard tag (0x0008, 0x1010) was used. """ try: - data = self.data_image[0x0008][0x1010] + data = self.data_image[str(0x0008)][str(0x1010)] except(KeyError): return "" @@ -1411,7 +1412,7 @@ class Parser(): DICOM standard tag (0x0008,0x1090) was used. """ try: - data = self.data_image[0x0008][0x1090] + data = self.data_image[str(0x0008)][str(0x1090)] except(KeyError): return "" @@ -1427,7 +1428,7 @@ class Parser(): DICOM standard tag (0x0008, 0x1010) was used. """ try: - data = self.data_image[0x0008][0x1010] + data = self.data_image[str(0x0008)][str(0x1010)] except(KeyError): return "" @@ -1445,7 +1446,7 @@ class Parser(): DICOM standard tag (0x0008,0x0060) was used. """ try: - data = self.data_image[0x0008][0x0060] + data = self.data_image[str(0x0008)][str(0x0060)] except(KeyError): return "" @@ -1462,7 +1463,7 @@ class Parser(): DICOM standard tag (0x0020,0x0013) was used. """ try: - data = self.data_image[0x0020][0x0013] + data = self.data_image[str(0x0020)][str(0x0013)] except(KeyError): return "" @@ -1478,7 +1479,7 @@ class Parser(): DICOM standard tag (0x0008,0x1030) was used. """ try: - data = self.data_image[0x0008][0x1030] + data = self.data_image[str(0x0008)][str(0x1030)] if (data): encoding = self.GetEncoding() return data.decode(encoding) @@ -1509,7 +1510,7 @@ class Parser(): DICOM standard tag (0x0008, 0x103E) was used. """ try: - data = self.data_image[0x0008][0x103E] + data = self.data_image[str(0x0008)][str(0x103E)] if data == "None": return _("unnamed") if (data): @@ -1525,7 +1526,7 @@ class Parser(): DICOM standard tag (0x0008,0x0033) was used. """ try: - data = self.data_image[0x0008][0x0033] + data = self.data_image[str(0x0008)][str(0x0033)] except(KeyError): return "" @@ -1539,7 +1540,7 @@ class Parser(): DICOM standard tag (0x0008,0x032) was used. """ try: - data = self.data_image[0x0008][0x0032] + data = self.data_image[str(0x0008)][str(0x0032)] except(KeyError): return "" @@ -1553,7 +1554,7 @@ class Parser(): DICOM standard tag (0x0020, 0x0011) was used. """ try: - data = self.data_image[0x0020][0x0011] + data = self.data_image[str(0x0020)][str(0x0011)] except(KeyError): return "" @@ -1567,20 +1568,11 @@ class Parser(): DICOM standard tag (0x0008, 0x0005) was used. """ try: - encoding = self.data_image[0x0008][0x0005] + encoding_value = self.data_image[str(0x0008)][str(0x0005)] + return const.DICOM_ENCODING_TO_PYTHON[encoding_value] except(KeyError): return 'ISO_IR_100' - if encoding is None or encoding == "None" or not encoding.strip(): - return 'ISO_IR 100' - else: - #Problem with 0051 anonymized - if (encoding.split(":"))[0] == "Loaded": - return 'ISO_IR 100' - else: - return encoding - return 'ISO_IR 100' - class DicomWriter: diff --git a/invesalius/reader/dicom_reader.py b/invesalius/reader/dicom_reader.py index c1fb04f..768725d 100644 --- a/invesalius/reader/dicom_reader.py +++ b/invesalius/reader/dicom_reader.py @@ -100,7 +100,6 @@ class LoadDicom: def run(self): grouper = self.grouper - reader = gdcm.ImageReader() reader.SetFileName(self.filepath) if (reader.Read()): @@ -120,11 +119,13 @@ class LoadDicom: tag = gdcm.Tag(0x0008, 0x0005) ds = reader.GetFile().GetDataSet() if ds.FindDataElement(tag): - encoding = str(ds.GetDataElement(tag).GetValue()) - if encoding is None or encoding == "None" or encoding.startswith("Loaded"): + encoding_value = str(ds.GetDataElement(tag).GetValue()) + + if encoding_value.startswith("Loaded"): encoding = "ISO_IR 100" - else: - encoding = "ISO_IR_100" + else: + encoding = const.DICOM_ENCODING_TO_PYTHON[encoding_value] + # Iterate through the Header iterator = header.GetDES().begin() while (not iterator.equal(header.GetDES().end())): @@ -134,8 +135,8 @@ class LoadDicom: data = stf.ToStringPair(tag) stag = tag.PrintAsPipeSeparatedString() - group = tag.GetGroup() - field = tag.GetElement() + group = str(tag.GetGroup()) + field = str(tag.GetElement()) tag_labels[stag] = data[0] @@ -158,8 +159,8 @@ class LoadDicom: data = stf.ToStringPair(tag) stag = tag.PrintAsPipeSeparatedString() - group = tag.GetGroup() - field = tag.GetElement() + group = str(tag.GetGroup()) + field = str(tag.GetElement()) tag_labels[stag] = data[0] @@ -179,9 +180,9 @@ class LoadDicom: rvtk.Update() try: - data = data_dict[0x028][0x1050] + data = data_dict[str(0x028)][str(0x1050)] level = [float(value) for value in data.split('\\')][0] - data = data_dict[0x028][0x1051] + data = data_dict[str(0x028)][str(0x1051)] window = [float(value) for value in data.split('\\')][0] except(KeyError): level = 300.0 @@ -229,7 +230,7 @@ class LoadDicom: #---------- Verify is DICOMDir ------------------------------- is_dicom_dir = 1 try: - if (data_dict[0x002][0x002] != "1.2.840.10008.1.3.10"): #DICOMDIR + if (data_dict[str(0x002)][str(0x002)] != "1.2.840.10008.1.3.10"): #DICOMDIR is_dicom_dir = 0 except(KeyError): is_dicom_dir = 0 @@ -244,13 +245,16 @@ class LoadDicom: grouper.AddFile(dcm) #self.l.release() - - #========== used in test ======================================= - #main_dict = dict( - # data = dict_file, - # labels = tag_labels) - - #plistlib.writePlist(main_dict, ".//teste.plist")""" + + + #========== used in test ======================================= + #print dict_file + #main_dict = dict( + # data = dict_file, + # labels = tag_labels) + #print main_dict + #print "\n" + #plistlib.writePlist(main_dict, ".//teste.plist") def yGetDicomGroups(directory, recursive=True, gui=True): @@ -338,7 +342,7 @@ class ProgressDicomReader: fow.SetFileName(log_path) ow = vtk.vtkOutputWindow() ow.SetInstance(fow) - + y = yGetDicomGroups(path, recursive) for value_progress in y: if not self.running: @@ -354,3 +358,4 @@ class ProgressDicomReader: self.UpdateLoadFileProgress(None) self.stoped = False + -- libgit2 0.21.2