Coverage for csv_manager/QF_CSVManager.py: 85%

59 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 

29from os import makedirs 

30from os.path import exists 

31import hashlib 

32import pandas as pd 

33try: 

34 from ..utils.file_manager import fullPath 

35except: # for coverage 

36 from utils.file_manager import fullPath 

37 

38class QF_CSVManager: 

39 """ 

40 A class taking care of writing results including csv file and failedcases file 

41 """ 

42 def __init__(self, dir_path): 

43 """ 

44 init with directory path 

45 :param dir_path: 

46 """ 

47 self.dataframe = None 

48 result_path = fullPath(dir_path, "qf_results") 

49 if not exists(result_path): 

50 makedirs(result_path) 

51 self.filename = fullPath(result_path, 'summary.csv') 

52 self.colnames = ['Filename', 'centerX', 'centerY', 'rotationAngle', 'hash', 'comment'] 

53 self.loadFailedCases(dir_path) 

54 self.loadSummary() 

55 

56 def loadFailedCases(self, direc): 

57 """ 

58 Load failed cases file from the directory and keep them in self.failedcases 

59 :param direc: input directory (str) 

60 :return: - 

61 """ 

62 self.failedcasesfile = fullPath(direc, "failedcases.txt") 

63 self.failedcases = set() 

64 if exists(self.failedcasesfile): 

65 for line in open(self.failedcasesfile, 'r'): 

66 name = line.rstrip('\n') 

67 self.failedcases.add(name) 

68 

69 def loadSummary(self): 

70 """ 

71 Load summary.csv file and keep data in self.dataframe 

72 :return: 

73 """ 

74 if not exists(self.filename): 

75 self.dataframe = pd.DataFrame(columns = self.colnames) 

76 else: 

77 self.dataframe = pd.read_csv(self.filename) 

78 

79 def writeNewData(self, quadFold): 

80 """ 

81 Add new data to dataframe, then re-write summary.csv and failed cases file 

82 :param quadFold: QuadrantFolder object with results in its info dict 

83 :return: - 

84 """ 

85 img_name = quadFold.img_name 

86 cache = quadFold.imgCache 

87 self.removeData(img_name) 

88 data = {} 

89 

90 # If there is no result 

91 if "resultImg" not in cache: 

92 for k in self.dataframe.columns: 

93 data[k] = '-' 

94 data['Filename'] = img_name 

95 data['comment'] = "REJECTED" 

96 else: 

97 failed = False 

98 if 'calib_center' in quadFold.info: 

99 center = quadFold.info['calib_center'] 

100 elif 'manual_center' in quadFold.info: 

101 center = quadFold.info['manual_center'] 

102 else: 

103 center = (round(quadFold.orig_image_center[0], 2), round(quadFold.orig_image_center[1], 2)) 

104 # Get all needed infos 

105 data['Filename'] = img_name 

106 data['centerX'] = center[0] 

107 data['centerY'] = center[1] 

108 data['rotationAngle'] = quadFold.info['rotationAngle'] 

109 data['hash'] = hashlib.sha512(cache['resultImg']).hexdigest() 

110 

111 if failed: 

112 self.failedcases.add(img_name) 

113 elif img_name in self.failedcases: 

114 self.failedcases.remove(img_name) 

115 

116 self.dataframe = pd.concat([self.dataframe, pd.DataFrame.from_records([data])]) 

117 # self.dataframe = self.dataframe.append(data, ignore_index=True) # Future warning deprecated 

118 self.dataframe.reset_index() 

119 self.dataframe.to_csv(self.filename, index=False, columns=self.colnames) # Write to csv file 

120 

121 def removeData(self, img_name): 

122 """ 

123 Remove data from dataframe 

124 :param img_name: (str) 

125 :return: 

126 """ 

127 self.dataframe = self.dataframe[self.dataframe["Filename"] != img_name]