Euniceyeee
commited on
Commit
•
7f04f33
1
Parent(s):
1e72e6b
Upload ini_pro.ipynb
Browse files- ini_pro.ipynb +165 -0
ini_pro.ipynb
ADDED
@@ -0,0 +1,165 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 1,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [],
|
8 |
+
"source": [
|
9 |
+
"import numpy as np\n",
|
10 |
+
"import os\n",
|
11 |
+
"import shutil\n",
|
12 |
+
"from sklearn.model_selection import train_test_split\n",
|
13 |
+
"import pandas as pd\n",
|
14 |
+
"import json "
|
15 |
+
]
|
16 |
+
},
|
17 |
+
{
|
18 |
+
"cell_type": "markdown",
|
19 |
+
"metadata": {},
|
20 |
+
"source": [
|
21 |
+
"Create train-test folder"
|
22 |
+
]
|
23 |
+
},
|
24 |
+
{
|
25 |
+
"cell_type": "code",
|
26 |
+
"execution_count": 3,
|
27 |
+
"metadata": {},
|
28 |
+
"outputs": [],
|
29 |
+
"source": [
|
30 |
+
"base_path = 'kidney-ct-abnormality'\n",
|
31 |
+
"images_path = os.path.join(base_path, 'imagesTr')\n",
|
32 |
+
"all_images = [f for f in os.listdir(images_path) if f.endswith('.mha')]\n",
|
33 |
+
"train_files, test_files = train_test_split(all_images, test_size=0.2, random_state=219)"
|
34 |
+
]
|
35 |
+
},
|
36 |
+
{
|
37 |
+
"cell_type": "code",
|
38 |
+
"execution_count": 4,
|
39 |
+
"metadata": {},
|
40 |
+
"outputs": [],
|
41 |
+
"source": [
|
42 |
+
"train_dir = os.path.join(base_path, 'train')\n",
|
43 |
+
"test_dir = os.path.join(base_path, 'test')\n",
|
44 |
+
"os.makedirs(train_dir, exist_ok=True)\n",
|
45 |
+
"os.makedirs(test_dir, exist_ok=True)"
|
46 |
+
]
|
47 |
+
},
|
48 |
+
{
|
49 |
+
"cell_type": "code",
|
50 |
+
"execution_count": 5,
|
51 |
+
"metadata": {},
|
52 |
+
"outputs": [],
|
53 |
+
"source": [
|
54 |
+
"def move_files(files, destination):\n",
|
55 |
+
" for f in files:\n",
|
56 |
+
" shutil.move(os.path.join(images_path, f), os.path.join(destination, f))"
|
57 |
+
]
|
58 |
+
},
|
59 |
+
{
|
60 |
+
"cell_type": "code",
|
61 |
+
"execution_count": 6,
|
62 |
+
"metadata": {},
|
63 |
+
"outputs": [],
|
64 |
+
"source": [
|
65 |
+
"move_files(train_files, train_dir)\n",
|
66 |
+
"move_files(test_files, test_dir)"
|
67 |
+
]
|
68 |
+
},
|
69 |
+
{
|
70 |
+
"cell_type": "markdown",
|
71 |
+
"metadata": {},
|
72 |
+
"source": [
|
73 |
+
"Modify the json file"
|
74 |
+
]
|
75 |
+
},
|
76 |
+
{
|
77 |
+
"cell_type": "code",
|
78 |
+
"execution_count": 2,
|
79 |
+
"metadata": {},
|
80 |
+
"outputs": [],
|
81 |
+
"source": [
|
82 |
+
"json_file_path = 'dataset.json' \n",
|
83 |
+
"with open(json_file_path, 'r') as file:\n",
|
84 |
+
" img_label = json.load(file)"
|
85 |
+
]
|
86 |
+
},
|
87 |
+
{
|
88 |
+
"cell_type": "code",
|
89 |
+
"execution_count": 3,
|
90 |
+
"metadata": {},
|
91 |
+
"outputs": [],
|
92 |
+
"source": [
|
93 |
+
"data = img_label['training']\n",
|
94 |
+
"df = pd.DataFrame(data)\n",
|
95 |
+
"df.rename(columns={'Abnormality': 'abnormality'}, inplace=True)\n",
|
96 |
+
"df['image'] = df['image'].apply(lambda x: x[11:-4]+'_0000'+x[-4:])\n",
|
97 |
+
"test_images = [f for f in os.listdir('test') if f.endswith('.mha')]\n",
|
98 |
+
"df['split'] = df['image'].apply(lambda x: 'test' if x in test_images else 'train')\n",
|
99 |
+
"# df"
|
100 |
+
]
|
101 |
+
},
|
102 |
+
{
|
103 |
+
"cell_type": "code",
|
104 |
+
"execution_count": 5,
|
105 |
+
"metadata": {},
|
106 |
+
"outputs": [],
|
107 |
+
"source": [
|
108 |
+
"# df_copy = df.copy()\n",
|
109 |
+
"# df_copy['count']=1\n",
|
110 |
+
"# df_copy.groupby(['split', 'abnormality']).sum()"
|
111 |
+
]
|
112 |
+
},
|
113 |
+
{
|
114 |
+
"cell_type": "code",
|
115 |
+
"execution_count": 4,
|
116 |
+
"metadata": {},
|
117 |
+
"outputs": [],
|
118 |
+
"source": [
|
119 |
+
"list_of_dicts = df.to_dict(orient='records')\n",
|
120 |
+
"# list_of_dicts"
|
121 |
+
]
|
122 |
+
},
|
123 |
+
{
|
124 |
+
"cell_type": "code",
|
125 |
+
"execution_count": 23,
|
126 |
+
"metadata": {},
|
127 |
+
"outputs": [],
|
128 |
+
"source": [
|
129 |
+
"df.to_json('dataset_m.json', orient='records', lines=True)"
|
130 |
+
]
|
131 |
+
},
|
132 |
+
{
|
133 |
+
"cell_type": "code",
|
134 |
+
"execution_count": null,
|
135 |
+
"metadata": {},
|
136 |
+
"outputs": [],
|
137 |
+
"source": []
|
138 |
+
}
|
139 |
+
],
|
140 |
+
"metadata": {
|
141 |
+
"interpreter": {
|
142 |
+
"hash": "5dd334af8dc9350ae3498c72a940dd1e233d0cdb387a7b49f9cbba56097a7a15"
|
143 |
+
},
|
144 |
+
"kernelspec": {
|
145 |
+
"display_name": "Python 3.9.7",
|
146 |
+
"language": "python",
|
147 |
+
"name": "python3"
|
148 |
+
},
|
149 |
+
"language_info": {
|
150 |
+
"codemirror_mode": {
|
151 |
+
"name": "ipython",
|
152 |
+
"version": 3
|
153 |
+
},
|
154 |
+
"file_extension": ".py",
|
155 |
+
"mimetype": "text/x-python",
|
156 |
+
"name": "python",
|
157 |
+
"nbconvert_exporter": "python",
|
158 |
+
"pygments_lexer": "ipython3",
|
159 |
+
"version": "3.9.7"
|
160 |
+
},
|
161 |
+
"orig_nbformat": 4
|
162 |
+
},
|
163 |
+
"nbformat": 4,
|
164 |
+
"nbformat_minor": 2
|
165 |
+
}
|