GoodBaiBai88 commited on
Commit
d8c62b6
1 Parent(s): b40a6b0

Upload m3d_cap_data_prepare.py

Browse files
Files changed (1) hide show
  1. m3d_cap_data_prepare.py +122 -0
m3d_cap_data_prepare.py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import numpy as np
3
+ from PIL import Image
4
+ import concurrent.futures
5
+ from tqdm import tqdm
6
+ from collections import Counter
7
+ import unicodedata
8
+ import monai.transforms as mtf
9
+ from multiprocessing import Pool
10
+ from unidecode import unidecode
11
+
12
+ # input_dir = 'PATH/M3D_Cap/ct_quizze/'
13
+ # output_dir = 'PATH/M3D_Cap_npy/ct_quizze/'
14
+
15
+ input_dir = 'PATH/M3D_Cap/ct_case/'
16
+ output_dir = 'PATH/M3D_Cap_npy/ct_case/'
17
+
18
+ # Get all subfolders [00001, 00002....]
19
+ subfolders = [folder for folder in os.listdir(input_dir) if os.path.isdir(os.path.join(input_dir, folder))]
20
+
21
+
22
+ transform = mtf.Compose([
23
+ mtf.CropForeground(),
24
+ mtf.Resize(spatial_size=[32, 256, 256], mode="bilinear")
25
+ ])
26
+
27
+
28
+ def process_subfolder(subfolder):
29
+ output_id_folder = os.path.join(output_dir, subfolder)
30
+ input_id_folder = os.path.join(input_dir, subfolder)
31
+
32
+ os.makedirs(output_id_folder, exist_ok=True)
33
+
34
+ for subsubfolder in os.listdir(input_id_folder):
35
+ if subsubfolder.endswith('.txt'):
36
+ text_path = os.path.join(input_dir, subfolder, subsubfolder)
37
+ with open(text_path, 'r') as file:
38
+ text_content = file.read()
39
+
40
+ search_text = "study_findings:"
41
+ index = text_content.find(search_text)
42
+
43
+ if index != -1:
44
+ filtered_text = text_content[index + len(search_text):].replace("\n", " ").strip()
45
+ else:
46
+ print("Specified string not found")
47
+ filtered_text = text_content.replace("\n", " ").strip()
48
+
49
+
50
+ if len(filtered_text.replace("\n", "").replace(" ", "")) < 5:
51
+ search_text = "discussion:"
52
+ index = text_content.find(search_text)
53
+ if index != -1:
54
+ filtered_text = text_content[index + len(search_text):].replace("\n", " ").strip()
55
+ else:
56
+ print("Specified string not found")
57
+ filtered_text = text_content.replace("\n", " ").strip()
58
+
59
+
60
+ if len(filtered_text.replace("\n", "").replace(" ", "")) < 5:
61
+ filtered_text = text_content.replace("\n", " ").strip()
62
+
63
+
64
+ new_text_path = os.path.join(output_dir, subfolder, subsubfolder)
65
+ with open(new_text_path, 'w') as new_file:
66
+ new_file.write(filtered_text)
67
+
68
+ subsubfolder_path = os.path.join(input_dir, subfolder, subsubfolder)
69
+
70
+ if os.path.isdir(subsubfolder_path):
71
+ subsubfolder = unidecode(subsubfolder) # "Pöschl" -> Poschl
72
+ output_path = os.path.join(output_dir, subfolder, f'{subsubfolder}.npy')
73
+
74
+ image_files = [file for file in os.listdir(subsubfolder_path) if
75
+ file.endswith('.jpeg') or file.endswith('.png')]
76
+
77
+ if len(image_files) == 0:
78
+ continue
79
+
80
+ image_files.sort(key=lambda x: int(os.path.splitext(x)[0]))
81
+
82
+ images_3d = []
83
+ for image_file in image_files:
84
+ image_path = os.path.join(subsubfolder_path, image_file)
85
+ try:
86
+ img = Image.open(image_path)
87
+ img = img.convert("L")
88
+ img_array = np.array(img)
89
+ # normalization
90
+ img_array = img_array.astype(np.float32) / 255.0
91
+ images_3d.append(img_array[None])
92
+ except:
93
+ print("This image is error: ", image_path)
94
+
95
+ images_3d_pure = []
96
+ try:
97
+ img_shapes = [img.shape for img in images_3d]
98
+ item_counts = Counter(img_shapes)
99
+ most_common_shape = item_counts.most_common(1)[0][0]
100
+ for img in images_3d:
101
+ if img.shape == most_common_shape:
102
+ images_3d_pure.append(img)
103
+ final_3d_image = np.vstack(images_3d_pure)
104
+
105
+ image = final_3d_image[np.newaxis, ...]
106
+
107
+ image = image - image.min()
108
+ image = image / np.clip(image.max(), a_min=1e-8, a_max=None)
109
+
110
+ img_trans = transform(image)
111
+
112
+ np.save(output_path, img_trans)
113
+ except:
114
+ print([img.shape for img in images_3d])
115
+ print("This folder is vstack error: ", output_path)
116
+
117
+
118
+
119
+ with Pool(processes=32) as pool:
120
+ with tqdm(total=len(subfolders), desc="Processing") as pbar:
121
+ for _ in pool.imap_unordered(process_subfolder, subfolders):
122
+ pbar.update(1)