-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathrenameFiles.py
105 lines (87 loc) · 3.49 KB
/
renameFiles.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
'''
by Dong Nie
05/03
target: preprocess the files. make directories, cp specific files to each of the files
'''
import numpy
import SimpleITK as sitk
import os
from doctest import SKIP
from shutil import copyfile
class ScanFile(object):
def __init__(self,directory,prefix=None,postfix=None):
self.directory=directory
self.prefix=prefix
self.postfix=postfix
def scan_files(self):
files_list=[]
for dirpath,dirnames,filenames in os.walk(self.directory):
'''''
dirpath is a string, the path to the directory.
dirnames is a list of the names of the subdirectories in dirpath (excluding '.' and '..').
filenames is a list of the names of the non-directory files in dirpath.
'''
for special_file in filenames:
if self.postfix:
special_file.endswith(self.postfix)
files_list.append(os.path.join(dirpath,special_file))
elif self.prefix:
special_file.startswith(self.prefix)
files_list.append(os.path.join(dirpath,special_file))
else:
files_list.append(os.path.join(dirpath,special_file))
return files_list
def scan_subdir(self):
subdir_list=[]
for dirpath,dirnames,files in os.walk(self.directory):
subdir_list.append(dirpath)
return subdir_list
def main():
path='/home/dongnie/warehouse/pelvicSeg/mri_newdata/'
subpath='atkinson_lafayette'
outfn=subpath+'.nii.gz'
inputdir=path+subpath
scan=ScanFile(path)
#subdirs=scan.scan_subdir()
subfiles=scan.scan_files()
ind=0
for subfile in subfiles:
if subfile.find('nii.gz')==-1:
continue
print 'subfile is, ',subfile
ind=ind+1
# ss=subfile.split('/')
# print 'ss is, ',ss, 'and s7 is, ',ss[7]
#
# sdir=ss[7]
# sdir_moving=path+sdir+'_moving'
# sdir_fixed=path+sdir+'_fixed'
# sdir_fused=path+sdir+'_fused'
# os.mkdir(sdir_moving)
# os.mkdir(sdir_fixed)
# os.mkdir(sdir_fused)
# copyfile(path+sdir+'/*moving*', sdir_moving)
# copyfile(path+sdir+'/*fixed*', sdir_fixed)
# copyfile(path+sdir+'/*fused*', sdir_fused)
os.system('mv '+subfile+' '+path+'img%d.nii.gz'%ind)
# os.system('mv '+'%s'%path+'%s/*fixed*'%sdir+' %s'%sdir_fixed)
# os.system('mv '+'%s'%path+'%s/*fused*'%sdir+' %s'%sdir_fused)
# os.system('cp /home/dongnie/warehouse/pelvicSeg/newData/pelvic_0209/%s/*fixed*'%sdir+' %s'%sdir_fixed)
# os.system('cp /home/dongnie/warehouse/pelvicSeg/newData/pelvic_0209/%s/*fused*'%sdir+' %s'%sdir_fused)
# outfn=ss[7]+'.nii.gz'
#
# reader = sitk.ImageSeriesReader()
#
# dicom_names = reader.GetGDCMSeriesFileNames(subdir)
# reader.SetFileNames(dicom_names)
#
# image = reader.Execute()
#
# size = image.GetSize()
# print( "Image size:", size[0], size[1], size[2] )
#
# print( "Writing image:", outfn)
#
# sitk.WriteImage(image,outfn)
if __name__ == '__main__':
main()