Coverage for ui/EquatorWindowh.py: 72%

119 statements  

« prev     ^ index     » next       coverage.py v7.0.4, created at 2023-01-10 09:27 -0600

1""" 

2Copyright 1999 Illinois Institute of Technology 

3 

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: 

11 

12The above copyright notice and this permission notice shall be 

13included in all copies or substantial portions of the Software. 

14 

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. 

22 

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""" 

28 

29import sys 

30import json 

31import os 

32import traceback 

33from musclex import __version__ 

34try: 

35 from .pyqt_utils import * 

36 from ..utils.file_manager import getImgFiles 

37 from ..modules.EquatorImage import EquatorImage 

38 from ..utils.image_processor import * 

39 from ..csv_manager import EQ_CSVManager 

40except: # for coverage 

41 from ui.pyqt_utils import * 

42 from utils.file_manager import getImgFiles 

43 from modules.EquatorImage import EquatorImage 

44 from utils.image_processor import * 

45 from csv_manager import EQ_CSVManager 

46 

47class EquatorWindowh: 

48 """ 

49 Window displaying all information of a selected image. 

50 This window contains 3 tabs : image, fitting, results 

51 """ 

52 def __init__(self, filename, inputsettings, delcache, settingspath='musclex/settings/eqsettings.json'): 

53 """ 

54 :param filename: selected file name 

55 :param inputsettings: flag for input setting file 

56 :param delcache: flag for deleting cache 

57 :param settingspath: setting file directory 

58 """ 

59 self.version = __version__ 

60 self.editableVars = {} 

61 self.bioImg = None # Current EquatorImage object 

62 self.default_img_zoom = None # default zoom calculated after processing image 

63 # self.img_zoom = None # Params for x and y ranges of displayed image in image tab 

64 self.graph_zoom = None # Params for x and y ranges of displayed graph in fitting tab 

65 self.function = None # Current active function 

66 

67 self.in_batch_process = False 

68 self.fixedIntArea = None 

69 self.orientationModel = None 

70 self.modeOrientation = None 

71 self.dir_path, self.imgList, self.currentImg, self.fileList, self.ext = getImgFiles(str(filename)) 

72 if len(self.imgList) == 0: 

73 self.inputerror() 

74 return 

75 self.csvManager = EQ_CSVManager(self.dir_path) # Create a CSV Manager object 

76 self.inputsettings=inputsettings 

77 self.delcache=delcache 

78 self.settingspath=settingspath 

79 

80 self.onImageChanged() # Toggle window to process current image 

81 

82 def inputerror(self): 

83 """ 

84 Display input error to screen 

85 """ 

86 print('Invalid Input') 

87 print("Please select non empty failedcases.txt or an image\n\n") 

88 

89 def onImageChanged(self): 

90 """ 

91 This will create a new EquatorImage object for the new image 

92 Process the new image if there's no cache. 

93 """ 

94 fileName = self.imgList[self.currentImg] 

95 file=fileName+'.info' 

96 cache_path = os.path.join(self.dir_path, "eq_cache", file) 

97 cache_exist = os.path.isfile(cache_path) 

98 if self.delcache: 

99 if os.path.isfile(cache_path): 

100 os.remove(cache_path) 

101 

102 #prevInfo = self.bioImg.info if self.bioImg is not None else None 

103 self.bioImg = EquatorImage(self.dir_path, fileName, self, self.fileList, self.ext) 

104 self.bioImg.skeletalVarsNotSet = not ('isSkeletal' in self.bioImg.info and self.bioImg.info['isSkeletal']) 

105 

106 settings = None 

107 settings = self.getSettings() 

108 print("Settings in onImageChange before update") 

109 print(settings) 

110 

111 # Process new image 

112 if 'paramInfo' in settings: 

113 paramInfo = settings['paramInfo'] 

114 #settings.pop('paramInfo') 

115 self.processImage(paramInfo) 

116 else: 

117 self.processImage() 

118 

119 print('---------------------------------------------------') 

120 

121 if self.inputsettings and cache_exist and not self.delcache: 

122 print('cache exists, provided setting file was not used ') 

123 elif self.inputsettings and (not cache_exist or self.delcache): 

124 print('setting file provided and used for fitting') 

125 elif not self.inputsettings and cache_exist and not self.delcache: 

126 print('cache exist, no fitting was performed') 

127 elif not self.inputsettings and (self.delcache or not cache_exist): 

128 print('fitting with default settings') 

129 

130 print('---------------------------------------------------') 

131 

132 def processImage(self, paramInfo=None): 

133 """ 

134 Process Image by getting all settings and call process() of EquatorImage object 

135 Then, write data 

136 """ 

137 if self.bioImg is None: 

138 return 

139 

140 settings = self.getSettings() 

141 print("Settings in processImage:") 

142 print(settings) 

143 try: 

144 self.bioImg.process(settings, paramInfo) 

145 except Exception: 

146 print('Unexpected error') 

147 msg = 'Please report the problem with error message below and the input image\n\n' 

148 msg += "Error : " + str(sys.exc_info()[0]) + '\n\n' + str(traceback.format_exc()) 

149 print(msg) 

150 raise 

151 

152 self.updateParams() 

153 self.csvManager.writeNewData(self.bioImg) 

154 self.csvManager.writeNewData2(self.bioImg) 

155 

156 def updateParams(self): 

157 """ 

158 Update the parameters 

159 """ 

160 info = self.bioImg.info 

161 if 'orientation_model' in info: 

162 self.orientationModel = info['orientation_model'] 

163 

164 if self.bioImg.quadrant_folded: 

165 cx, cy = self.bioImg.info['center'] 

166 xlim, ylim = self.bioImg.initialImgDim 

167 xlim, ylim = int(xlim/2), int(ylim/2) 

168 self.default_img_zoom = [(cx-xlim, cx+xlim), (cy-ylim, cy+ylim)] 

169 

170 def getSettings(self): 

171 """ 

172 Get all settings for EquatorImage process() from widgets 

173 :return: settings (dict) 

174 """ 

175 settings = {} 

176 settingspath=self.settingspath 

177 

178 if self.inputsettings: 

179 try: 

180 with open(settingspath) as f: 

181 settings=json.load(f) 

182 except Exception: 

183 print("Can't load setting file") 

184 self.inputsettings=False 

185 settings={"left_fix_sigmac": 1.0, "right_fix_sigmac": 1.0, \ 

186 "left_fix_sigmas": 0.0001, "right_fix_sigmas": 0.0001, "fix_k":0, \ 

187 "orientation_model": 0, "model": "Gaussian", "isSkeletal": False, \ 

188 "isExtraPeak": False, "mask_thres": 0.0, "90rotation": False,\ 

189 "blank_mask": False} 

190 else: 

191 settings={"left_fix_sigmac": 1.0, "right_fix_sigmac": 1.0, \ 

192 "left_fix_sigmas": 0.0001, "right_fix_sigmas": 0.0001, "fix_k":0, \ 

193 "orientation_model": 0, "model": "Gaussian", "isSkeletal": False, \ 

194 "isExtraPeak": False, "mask_thres": 0.0, "90rotation": False,\ 

195 "blank_mask": False} 

196 

197 for k in settings.keys(): 

198 if self.isDynamicParameter(k): 

199 settings.pop(k) 

200 

201 return settings 

202 

203 def isDynamicParameter(self, paramName): 

204 ''' 

205 Checks whether parameter is dynamically handelled by fitting mechanism 

206 :param paramName: Name of the parameter to be checked 

207 :return: bool True if it is in the dynamic parameter list 

208 ''' 

209 dynamicParams = ['Speak', 'left_area', 'right_area'] 

210 for p in dynamicParams: 

211 if p in paramName: 

212 return True 

213 return False 

214 

215 def statusPrint(self, text): 

216 """ 

217 Print the text in the window or in the terminal depending on if we are using GUI or headless. 

218 :param text: text to print 

219 :return: - 

220 """ 

221 print(text) 

222