forked from Danielhiversen/NeuroImageRegistration
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ConvertDataToDB_menigiomer.py
178 lines (145 loc) · 5.72 KB
/
ConvertDataToDB_menigiomer.py
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 19 15:19:49 2016
@author: dahoiv
"""
# pylint: disable= line-too-long
from __future__ import print_function
import glob
import os
import SimpleITK as sitk
import shutil
import sqlite3
import util
MAIN_FOLDER = "/home/dahoiv/disk/data/meningiomer/"
DWICONVERT_PATH = "/home/dahoiv/disk/kode/Slicer/Slicer-SuperBuild/Slicer-build/lib/Slicer-4.6/cli-modules/DWIConvert"
def create_db(path):
"""Make the database"""
conn = sqlite3.connect(path)
cursor = conn.cursor()
cursor.execute('''CREATE TABLE "Images" (
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
`pid` INTEGER,
`modality` TEXT,
`filepath` TEXT,
`transform` TEXT,
`fixed_image` INTEGER,
`filepath_reg` TEXT,
`comments` TEXT,
FOREIGN KEY(`pid`) REFERENCES `Patient`(`pid`))''')
cursor.execute('''CREATE TABLE "Labels" (
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
`image_id` INTEGER NOT NULL,
`description` TEXT,
`filepath` TEXT,
`filepath_reg` TEXT,
`comments` TEXT,
FOREIGN KEY(`image_id`) REFERENCES `Images`(`id`))''')
cursor.execute('''CREATE TABLE "Patient" (
`pid` INTEGER NOT NULL UNIQUE,
`comments` TEXT,
PRIMARY KEY(pid))''')
conn.commit()
cursor.close()
conn.close()
# pylint: disable= too-many-arguments, too-many-locals
def convert_and_save_dataset(pid, cursor, volume_labels, volume):
"""convert_and_save_dataset"""
util.mkdir_p(util.DATA_FOLDER + str(pid))
img_out_folder = util.DATA_FOLDER + str(pid) + "/volumes_labels/"
util.mkdir_p(img_out_folder)
cursor.execute('''SELECT pid from Patient where pid = ?''', (pid, ))
exist = cursor.fetchone()
if exist is None:
cursor.execute('''INSERT INTO Patient(pid) VALUES(?)''', (pid, ))
cursor.execute('''INSERT INTO Images(pid, modality) VALUES(?,?)''',
(pid, 'MR'))
img_id = cursor.lastrowid
_, file_extension = os.path.splitext(volume)
volume_out = img_out_folder + str(pid) + "_" + str(img_id) + "_MR_T1.nii.gz"
print("--->", volume_out)
sitk.WriteImage(sitk.ReadImage(volume), volume_out)
volume_out_db = volume_out.replace(util.DATA_FOLDER, "")
cursor.execute('''UPDATE Images SET filepath = ?, filepath_reg = ? WHERE id = ?''', (volume_out_db, None, img_id))
for volume_label in volume_labels:
_, file_extension = os.path.splitext(volume_label)
cursor.execute('''INSERT INTO Labels(image_id, description) VALUES(?,?)''',
(img_id, 'all'))
label_id = cursor.lastrowid
volume_label_out = img_out_folder + str(pid) + "_" + str(img_id) + "_MR_T1_label_all.nii.gz"
sitk.WriteImage(sitk.ReadImage(volume_label), volume_label_out)
volume_label_out_db = volume_label_out.replace(util.DATA_FOLDER, "")
cursor.execute('''UPDATE Labels SET filepath = ?, filepath_reg = ? WHERE id = ?''',
(volume_label_out_db, None, label_id))
def convert_data(path, update=False, case_ids=range(2000)):
"""Convert gbm data """
# pylint: disable= too-many-locals, too-many-branches, too-many-statements
conn = sqlite3.connect(util.DB_PATH)
cursor = conn.cursor()
log = ""
for case_id in case_ids:
data_path = path + str(case_id) + "/"
if not os.path.exists(data_path):
continue
pid = str(case_id)
if update:
for _file in glob.glob(util.DATA_FOLDER + str(pid) + "/volumes_labels/*"):
_file = _file.replace(util.DATA_FOLDER, "")
cursor.execute("DELETE FROM Images WHERE filepath=?", (_file,))
cursor.execute("DELETE FROM Labels WHERE filepath=?", (_file,))
try:
shutil.rmtree(util.DATA_FOLDER + str(pid))
except OSError:
pass
data_path = path + str(case_id) + "/"
if not os.path.exists(data_path):
continue
print(data_path)
volume_label = glob.glob(data_path + '/*label.mhd')
if len(volume_label) > 1:
log = log + "\n Warning!! More than one file with label found "
for volume_label_i in volume_label:
log = log + volume_label_i
continue
if not volume_label:
volume_label = glob.glob(data_path + '/*label.nrrd')
if not volume_label:
log = log + "\n Warning!! No label found " + data_path
continue
volume_label = volume_label[0]
volume = glob.glob(data_path + '/*vol.mhd')
if len(volume) > 1:
log = log + "\n Warning!! More than one file with volume found "
for volume_i in volume:
log = log + volume_i
continue
if not volume:
volume = glob.glob(data_path + '/*vol.nrrd')
if not volume:
log = log + "\n Warning!! No volume found " + data_path
continue
volume = volume[0]
if not os.path.exists(volume):
log = log + "\n Warning!! No volume found " + data_path
convert_and_save_dataset(pid, cursor, [volume_label], volume)
conn.commit()
with open("Log.txt", "w") as text_file:
text_file.write(log)
cursor.close()
conn.close()
def vacuum_db():
""" Clean up database"""
conn = sqlite3.connect(util.DB_PATH)
cursor = conn.execute('''VACUUM; ''')
cursor.close()
conn.close()
if __name__ == "__main__":
util.setup_paths("meningiomer")
try:
shutil.rmtree(util.DATA_FOLDER)
except OSError:
pass
util.mkdir_p(util.DATA_FOLDER)
create_db(util.DB_PATH)
convert_data(MAIN_FOLDER + "org_data/")
vacuum_db()