-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.py
More file actions
executable file
·162 lines (141 loc) · 3.59 KB
/
core.py
File metadata and controls
executable file
·162 lines (141 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env python
#-*- coding: utf-8 -*-
"""
********** DICOnyM Core ***************
DICOnyM is a pythonfoo project based in
the pydicom lib:
http://code.google.com/p/pydicom/
Project Home:
https://github.com/pythonfoo/diconym
Project Members:
Mechtilde
Mikeadvo
bison
dodo
dr1ll
Oerb
"""
__author__ = "pyhtonfoo"
__copyright__ = "GPL 2013"
__license__ = "GPL v3 Plus"
import os
import dicom
import random
def isDicom(fullpath):
"""
Check if a file is a DICOM file
Args:
fullpath (str): the FULL path of the file to check
"""
isOk = True
try:
dcm = dicom.read_file(fullpath)
except Exception as ex:
isOk = False
# print fullpath
# print ex
return isOk
def getFilesFromDir(dirPath):
"""
Get a dictionary of fullpath:filename from a given path
that contains ONLY valid DICOM files
Args:
dirPath (str): the FULL path of the directory to scan
Returns:
dictionary of fullpath:filename
Raises:
None
"""
dirList = os.listdir(dirPath)
dcmFiles = {}
for fname in dirList:
fPath = os.path.join(dirPath, fname)
if isDicom(fPath):
dcmFiles[fPath] = fname
#dcmFiles.append(fPath)
return dcmFiles
def get_valuesfromImage(filename):
"""
Read data element tag, value, VR, VM, description from DICOM file.
"""
datalist = []
def tagbased_callback(ds, data_element):
datalist.append((data_element.tag, data_element.value, data_element.VR, data_element.VM, data_element.description))
# Load the current dicom file to get tag- and valuelist
dataset = dicom.read_file(filename)
# write tag and value into datalist
dataset.walk(tagbased_callback)
return datalist
def anonymize_byWhitelist_bak(whitelist, filename, newfilename):
"""
"""
def tagbased_callback(ds, data_element):
"""
Delete the value in non Whitelisted Tags
TODO:
"""
cnt = 0
for whitelisttag in whitelist:
if str(data_element.tag) != str(whitelisttag):
cnt+=1
data_element.value = ""
else:
print 'keep:', data_element.tag
print cnt
# Load the current dicom file to get tag- and valuelist
dataset = dicom.read_file(filename)
# write delete nonwhitelist tag values into datalist
dataset.walk(tagbased_callback)
# save file to newfilename
dataset.save_as(newfilename)
def anonymize_byWhitelist(whitelist, filename, newfilename):
"""
"""
# fix whitelist string-desaster!
nwhitelist = []
for ar in whitelist:
for subAr in ar:
nwhitelist.append(str(subAr).replace("'", ''))
#print nwhitelist
# Load the current dicom file to get tag- and valuelist
dataset = dicom.read_file(filename)
# write delete nonwhitelist tag values into datalist
#dataset.walk(tagbased_callback)
for dt in dataset:
#print type(dt)
#print dt, dt.__dict__
#print dt.tag
if str(dt.tag) in nwhitelist:
pass
#print 'keep:', str(dt)
#print dataset[dt]
#insatnce number
#series number
else:
if 'value' in dt.__dict__:
print 'del:', dt
print 'val:', dataset.value
dataset.value = ''
else:
print 'hard reset:', dt
tmpType = type(dt._value)
if tmpType is str:
dt._value = ''
elif tmpType is int or tmpType is float:
dt._value = 0
elif tmpType is list:
dt._value = []
elif str(tmpType) == "<class 'dicom.UID.UID'>":
dt._value = "0"
#print 'UID cant be reset', tmpType
else:
print 'unknown type:', '#'+str(tmpType)+'#'
dt._value = ""
#for whitelisttag in whitelist:
# if str(data_element.tag) != str(whitelisttag):
# cnt+=1
# data_element.value = ""
# else:
# print 'keep:', data_element.tag
# save file to newfilename
dataset.save_as(newfilename)