-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepper.py
More file actions
executable file
·159 lines (128 loc) · 3.72 KB
/
prepper.py
File metadata and controls
executable file
·159 lines (128 loc) · 3.72 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
#!/usr/bin/python3
#################################### prepper.py
# Purpose: take a files dropped into /pipeline/input,
# maake sure it is a SR, if so fetch related priors and
# pass to the next link in the pipeline
#
# Author: SG Langer
# Date : July 2021
##################################
import time, os, sys
import pydicom
def checkDICOM(path):
#############################
# Purpose: see if DICOM, if yes dispatch prefetch
# tasks, else delete.
############################
try :
ds = pydicom.dcmread(path)
tags = getDemog(ds)
tmpDir = mkTmpDir (tags, path)
fetchSR(tmpDir, tags)
moveToPending(tmpDir)
except :
print('not dicom, deleting it and exiting')
os.system('rm ' + path)
sys.exit(1)
return
def getDemog(file):
############################
# Purpose: get Patient ID and
# exam info
###########################
tags = []
for tag in file:
#print (tag)
if 'Study Description' in str(tag): tags.append(str(tag))
if 'Patient\s Name' in str(tag): tags.append(str(tag))
if 'Patient ID' in str(tag): tags.append(str(tag))
if 'SOP Class UID' in str(tag): tags.append(str(tag))
# this not working
#tags.append(file.PatientsName)
#tags.append(file.SOPClassUID)
#tags.append(file.PatientID)
#tags.append(file.StudyDescription)
#print (tags)
return tags
def moveToPending(path):
################################
#
#
#
################################
root = os.getcwd()
root = root + '/pipeline/pending'
cmdStr = 'mv -f ' + path + ' ' + root
try :
print (cmdStr)
os.system(cmdStr)
except:
os.system('rm -Rf /pipeline/pending/* ')
os.system(cmdStr)
return
def mkTmpDir(list, file):
############################
# Purpose: use PatId to make
# a temp Dir
###########################
for i in list:
#print (i)
if 'Patient ID' in str(i):
PatID=i[i.index('LO:') +5 : -1]
tmpDir = os.getcwd() + '/tmp/' + PatID
print (tmpDir)
try:
# make tmp folder for this patient
os.system('mkdir ' +tmpDir)
os.system('mkdir ' +tmpDir +'/priors')
# and now move DICOM from input to /tmp/patID
#print (file)
os.system('mv ' + file +' ' + tmpDir)
except:
print('could not make folder ' + tmpDir)
pass
return tmpDir
def fetchSR(path, list):
##########################################
# Purpose: find relevent prios based on PatID and studyType
# Ref: https://support.dcmtk.org/docs/findscu.html
# caller: chkDICOM
#########################################
# on board VNA info
# SGL 31 Jul; echoscu to this is failing, why? Orthanc is up
AET= 'orthanc'
PORT = '4242'
PEER = '127.0.0.1'
for i in list:
#print (i)
if 'Patient ID' in str(i):
PatID=i[i.index('LO:') +5 : -1]
if 'Description' in str(i):
stdyDescrip=i[i.index('LO:') +5 : -1]
cmdStr = 'findscu -P -k PatientID=\"' + PatID + '\" -k StudyDescription=\"' + stdyDescrip + '\" ' \
+ PEER + ' ' + PORT
try:
print (cmdStr)
os.system(cmdStr)
# once the C-FIND finds studies, filter on SR objects, then C-Move them to
# /tmp/patID/priors
except :
print ('C-FIND blew up')
return
if __name__ == "__main__":
#############################################################
# Purpose: be called by fwatcher.py, create a /patID folder
# in /tmp, stuff it with priors if any, then move whole
# /patID to pipeline/pending
#
# Advice, this does not need to be a class
############################################################
#os.system('clear')
print ('in prepper')
# use cmd line arg to locate projectDir
if len(sys.argv ) != 2 :
print ("Incorrect Usage: Must include -input file path- ")
print (">./prepper.py file_path ")
sys.exit(1)
checkDICOM(sys.argv[1])
sys.exit()