-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathone2one_pair_mr.py
85 lines (67 loc) · 2.62 KB
/
one2one_pair_mr.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
from __future__ import print_function
import matplotlib.pyplot as plt
import SimpleITK as sitk
import numpy as np
import sys, os
import png
import cv2
import shutil
from os import listdir
from os.path import isfile, join
# place this script in the datasets/prostate/ directory
def check_on2one(directory_A, directory_B):
cwd = os.getcwd()
pathA = join(cwd,directory_A)
pathA_files = [extract(f) for f in listdir(pathA) if isfile(join(pathA, f))]
pathB = join(cwd,directory_B)
pathB_files = [extract(f) for f in listdir(pathB) if isfile(join(pathB, f))]
interAB_list = list(set(pathA_files) & set(pathB_files))
(fileA_list,fileB_list) = expand_fname(interAB_list)
return (fileA_list,fileB_list)
def extract(fname):
fname = fname.replace('.png','')
fname = fname.replace('ProstateX-','')
fname = fname.replace('tra','+')
fname = fname.replace('ADC','+')
return fname
def expand_fname(flist):
fileA_list = [expand(f,ADC=False) for f in flist]
fileB_list = [expand(f,ADC=True) for f in flist]
return(fileA_list,fileB_list)
def expand(fname,ADC=True):
if ADC == True:
fname = fname.replace('+','ADC')
else:
fname = fname.replace('+','tra')
fname = 'ProstateX-'+fname+'.png'
return fname
def generate_one2one_dataset(directoriesA, directoriesB):
for i in np.arange(len(directoriesA)):
fileA_list,fileB_list = check_on2one(directoriesA[i], directoriesB[i])
cwd = os.getcwd()
Acount = 0
Bcount = 0
directory = os.path.join(cwd,'datasets','prostate')
dest_PathA = os.path.join(directory,directoriesA[i])
dest_PathB = os.path.join(directory,directoriesB[i])
if not os.path.exists(dest_PathA):
os.makedirs(dest_PathA)
os.makedirs(dest_PathB)
pathA = os.path.join(cwd,directoriesA[i])
pathB = os.path.join(cwd,directoriesB[i])
for file in fileA_list:
new_fname = file.replace('tra','')
file_path = os.path.join(pathA,file)
shutil.copy(file_path,os.path.join(dest_PathA,new_fname))
Acount += 1
print(Acount)
for file in fileB_list:
new_fname = file.replace('ADC','')
file_path = os.path.join(pathB,file)
shutil.copy(file_path,os.path.join(dest_PathB,new_fname))
Bcount += 1
print(Bcount)
if __name__ == '__main__':
directoriesA = ['trainA','testA']
directoriesB = ['trainB','testB']
generate_one2one_dataset(directoriesA, directoriesB)