Coverage for csv_manager/PT_CSVManager.py: 17%

70 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.path import exists 

30import pandas as pd 

31try: 

32 from ..utils.file_manager import fullPath, createFolder 

33except: # for coverage 

34 from utils.file_manager import fullPath, createFolder 

35 

36class PT_CSVManager: 

37 """ 

38 A class taking care of writing results including csv file of Projection Traces 

39 """ 

40 def __init__(self, dir_path, boxes, peaks): 

41 """ 

42 init with directory path 

43 :param dir_path: 

44 """ 

45 self.dataframe = None 

46 result_path = fullPath(dir_path, "pt_results") 

47 createFolder(result_path) 

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

49 self.setColumnNames(boxes=boxes, peaks=peaks) 

50 self.loadSummary() 

51 

52 def setColumnNames(self, boxes, peaks): 

53 """ 

54 Set Colume name 

55 e.g. "Filename", "L1 Meridian Sigma", "L1 Meridian Amplitude", "L1 centroid 0", "L2 Meridian Sigma",... 

56 :param boxes: boxes dictionary (box_name:box coords) 

57 :param peaks: peaks dictionary (box_name:peak list) 

58 :return: 

59 """ 

60 self.colnames = ["Filename"] 

61 for box_name in boxes.keys(): 

62 if box_name in peaks: 

63 self.colnames.append("Box " + str(box_name) + " Meridian Sigma") 

64 self.colnames.append("Box " + str(box_name) + " Meridian Area") 

65 for i in range(len(peaks[box_name])): 

66 self.colnames.append("Box " + str(box_name) + " Maximum Point " + str(i) + " (Pixel)") 

67 self.colnames.append("Box " + str(box_name) + " Maximum Point " + str(i) + " (nm)") 

68 self.colnames.append("Box " + str(box_name) + " Centroid " + str(i) + " (Pixel)") 

69 self.colnames.append("Box " + str(box_name) + " Centroid " + str(i) + " (nm)") 

70 self.colnames.append("Box " + str(box_name) + " Gaussian Peak " + str(i) + " (Pixel)") 

71 self.colnames.append("Box " + str(box_name) + " Gaussian Peak " + str(i) + " (nm)") 

72 self.colnames.append("Box " + str(box_name) + " Gaussian Sigma " + str(i)) 

73 self.colnames.append("Box " + str(box_name) + " Gaussian Area " + str(i)) 

74 self.colnames.append("Box " + str(box_name) + " error") 

75 self.colnames.append("Box " + str(box_name) + " comments") 

76 

77 def loadSummary(self): 

78 """ 

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

80 :return: 

81 """ 

82 if not exists(self.filename): 

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

84 else: 

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

86 

87 def writeNewData(self, projProc): 

88 """ 

89 Add new data to dataframe, then re-write summary.csv 

90 :param projProc: Projection Processor object with results in its info dict 

91 :return: - 

92 """ 

93 file_name = projProc.filename 

94 info = projProc.info 

95 self.removeData(file_name) 

96 new_data = { 

97 'Filename' : file_name 

98 } 

99 

100 box_names = info['box_names'] 

101 for bn in box_names: 

102 if 'fit_results' in info and bn in info['fit_results']: 

103 model = info['fit_results'][bn] 

104 new_data['Box ' + str(bn) + ' Meridian Sigma'] = model['center_sigma2'] 

105 new_data['Box ' + str(bn) + ' Meridian Area'] = model['center_amplitude2'] 

106 if 'centroids' in info and bn in info['centroids']: 

107 centroids = info['centroids'][bn] 

108 moved_peaks = info['moved_peaks'][bn] - model['centerX'] 

109 for i,c in enumerate(centroids): 

110 new_data["Box " + str(bn) + " Maximum Point " + str(i) + " (Pixel)"] = moved_peaks[i] 

111 new_data['Box ' + str(bn) + " Centroid " + str(i) + " (Pixel)"] = c 

112 new_data["Box " + str(bn) + " Gaussian Peak " + str(i) + " (Pixel)"] = model['p_'+str(i)] 

113 new_data["Box " + str(bn) + " Gaussian Sigma " + str(i)] = model['sigma'+str(i)] 

114 new_data["Box " + str(bn) + " Gaussian Area " + str(i)] = model['amplitude'+str(i)] 

115 if 'lambda_sdd' in info: 

116 new_data["Box " + str(bn) + " Maximum Point " + str(i) + " (nn)"] = 1.*info['lambda_sdd']/moved_peaks[i] 

117 new_data['Box ' + str(bn) + " Centroid " + str(i) + " (nn)"] = 1.*info['lambda_sdd']/c 

118 new_data["Box " + str(bn) + " Gaussian Peak " + str(i) + " (nn)"] = 1. * info['lambda_sdd'] / model['p_' + str(i)] 

119 new_data["Box " + str(bn) + " error"] = model['error'] 

120 if model['error'] > 0.15: 

121 new_data["Box " + str(bn) + " comments"] = "High fitting error" 

122 

123 for col in self.colnames: 

124 if col not in new_data: 

125 new_data[col] = '-' 

126 

127 self.dataframe = pd.concat([self.dataframe, pd.DataFrame.from_records([pd.Series(new_data)])]) 

128 # self.dataframe = self.dataframe.append(pd.Series(new_data), ignore_index=True) # Future warning deprecated 

129 self.dataframe.reset_index() 

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

131 

132 def removeData(self, file_name): 

133 """ 

134 Remove data from dataframe 

135 :param file_name: (str) 

136 :return: 

137 """ 

138 self.dataframe = self.dataframe[self.dataframe["Filename"] != file_name]