Coverage for utils/file_manager.py: 65%
81 statements
« prev ^ index » next coverage.py v7.0.4, created at 2023-01-10 09:27 -0600
« prev ^ index » next coverage.py v7.0.4, created at 2023-01-10 09:27 -0600
1"""
2Copyright 1999 Illinois Institute of Technology
4Permission is hereby granted, free of charge, to any person obtaining
5a copy of this software and associated documentation files (the
6"Software"), to deal in the Software without restriction, including
7without limitation the rights to use, copy, modify, merge, publish,
8distribute, sublicense, and/or sell copies of the Software, and to
9permit persons to whom the Software is furnished to do so, subject to
10the following conditions:
12The above copyright notice and this permission notice shall be
13included in all copies or substantial portions of the Software.
15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18IN NO EVENT SHALL ILLINOIS INSTITUTE OF TECHNOLOGY BE LIABLE FOR ANY
19CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23Except as contained in this notice, the name of Illinois Institute
24of Technology shall not be used in advertising or otherwise to promote
25the sale, use or other dealings in this Software without prior written
26authorization from Illinois Institute of Technology.
27"""
29import os
30from os.path import split, exists, join
31import numpy as np
32import fabio
33from .hdf5_manager import loadFile
35input_types = ['adsc', 'cbf', 'edf', 'fit2d', 'mar345', 'marccd', 'hdf5', 'h5', 'pilatus', 'tif', 'tiff', 'smv']
37def getFilesAndHdf(dir_path):
38 """
39 Give the image files and hdf files in a folder selected
40 :param dir_path: directory path
41 :return: image list, hdf list
42 """
43 fileList = os.listdir(dir_path)
44 imgList = []
45 hdfList = []
47 for f in fileList:
48 full_file_name = fullPath(dir_path, f)
49 if isImg(full_file_name):
50 imgList.append(f)
51 else:
52 toks = f.split('.')
53 if toks[-1] == 'hdf':
54 hdfList.append(f)
56 return imgList, hdfList
58def getBlankImageAndMask(path):
59 """
60 Give the blank image and the mask threshold saved in settings
61 :return: blankImage, mask threshold
62 """
63 mask_file = join(join(path, 'settings'),'mask.tif')
64 blank_file = join(join(path, 'settings'),'blank.tif')
65 mask = None
66 blank_img = None
67 if exists(mask_file):
68 mask = fabio.open(mask_file).data
69 if exists(blank_file):
70 blank_img = fabio.open(blank_file).data
71 return blank_img, mask
73def getMaskOnly(path):
74 """
75 Give only the mask threshold
76 :param path: file path
77 :return: mask threshold
78 """
79 maskonly_file = join(join(path, 'settings'),'maskonly.tif')
80 if exists(maskonly_file):
81 return fabio.open(maskonly_file).data
82 return None
84def getImgFiles(fullname):
85 """
86 Get directory, all image file names in the same directory and current file index
87 :param fullname: full name of the file including directory i.e. /aaa/bbb/ccc/ddd.tif (str)
88 :return: directory (str), list of image file names, and current index i.e /aaa/bbb/ccc, ["ddd.tif","eee.tif"], 0
89 """
90 dir_path, filename = split(str(fullname)) # split directory and file name from full file name
91 dir_path = str(dir_path)
92 filename = str(filename)
93 _, ext = os.path.splitext(str(filename))
94 current = 0
95 failedcases = []
97 if ext == ".txt":
98 for line in open(fullname, "r"):
99 failedcases.append(line.rstrip('\n'))
100 else:
101 failedcases = None
103 if ext in ('.hdf5', '.h5'):
104 fileList = loadFile(fullname)
105 imgList = []
106 for f in fileList[0]:
107 imgList.append(f)
108 else:
109 fileList = os.listdir(dir_path)
110 imgList = []
111 for f in fileList:
112 if failedcases is not None and f not in failedcases:
113 continue
114 full_file_name = fullPath(dir_path, f)
115 if isImg(full_file_name) and f != "calibration.tif":
116 imgList.append(f)
117 imgList.sort()
119 if failedcases is None:
120 if ext in ('.hdf5', '.h5'):
121 current = 0
122 else:
123 current = imgList.index(filename)
124 return dir_path, imgList, current, fileList, ext
126def fullPath(filePath, fileName):
127 """
128 Combine a path and file name to get full file name
129 :param filePath: directory (string)
130 :param fileName: file name (string)
131 :return: filePath/filename (string)
132 """
133 if filePath[-1] == '/':
134 return filePath+fileName
135 else:
136 return filePath+"/"+fileName
138def isImg(fileName):
139 """
140 Check if a file name is an image file
141 :param fileName: (str)
142 :return: True or False
143 """
144 nameList = fileName.split('.')
145 return nameList[-1] in input_types
147def isHdf5(fileName):
148 """
149 Check if a file name is an hdf5 file
150 :param fileName: (str)
151 :return: True or False
152 """
153 nameList = fileName.split('.')
154 return nameList[-1] in ('hdf5', 'h5')
156def ifHdfReadConvertless(fileName, img):
157 """
158 Check if a file name is an hdf5 file
159 and convert it to be directly readable without converting to tiff
160 :param fileName, img: (str), (array)
161 :return: img converted
162 """
163 if isHdf5(fileName):
164 img = img.astype(np.int32)
165 img[img==4294967295] = -1
166 return img
168def createFolder(path):
169 """
170 Create a folder if it doesn't exist
171 :param path: full path of creating directory
172 :return:
173 """
174 if not exists(path):
175 os.makedirs(path)