Coverage for csv_manager/DI_CSVManager.py: 85%

73 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 pandas as pd 

30try: 

31 from ..modules.ScanningDiffraction import * 

32except: # for coverage 

33 from modules.ScanningDiffraction import * 

34 

35class DI_CSVManager(): 

36 """ 

37 The CSV manager object is used to gather information and save them in results files. 

38 """ 

39 def __init__(self, dir_path): 

40 self.df_sum = None 

41 self.df_rings = None 

42 result_path = fullPath(dir_path, 'di_results') 

43 createFolder(result_path) 

44 self.sum_file = fullPath(result_path, "summary.csv") 

45 self.rings_file = fullPath(result_path, "rings.csv") 

46 self.sum_header = ['filename', 'total intensity (hull)', 'total intensity', 'number of rings'] 

47 self.rings_header = ['filename', 'ring', 'S', 'd', 'peak sigma', 'peak intensity', 'angle', 'angle sigma', 'angle amplitude', 'angle fitting error'] 

48 self.load_all() 

49 

50 def load_all(self): 

51 """ 

52 Load summary.csv and rings.csv file and keep data in self.df_sum and self.df_rings 

53 :return: 

54 """ 

55 if exists(self.sum_file): 

56 self.df_sum = pd.read_csv(self.sum_file) 

57 else: 

58 self.df_sum = pd.DataFrame(columns=self.sum_header) 

59 if exists(self.rings_file): 

60 self.df_rings = pd.read_csv(self.rings_file) 

61 else: 

62 self.df_rings = pd.DataFrame(columns=self.rings_header) 

63 

64 def remove_data(self, filename): 

65 """ 

66 Remove data from dataframe 

67 :param file_name: (str) 

68 :return: 

69 """ 

70 self.df_sum = self.df_sum[self.df_sum["filename"] != filename] 

71 self.df_rings = self.df_rings[self.df_rings["filename"] != filename] 

72 

73 def write_new_data(self, cir_proj): 

74 """ 

75 Add new data to dataframe, then re-write summary.csv and rings.csv 

76 :param cir_proj: ScanningDiffraction object with results in its info dict 

77 :return: - 

78 """ 

79 

80 file_name = cir_proj.filename 

81 info = cir_proj.info 

82 self.remove_data(file_name) 

83 

84 # Add data to summary.csv 

85 new_sum_data = { 

86 'filename' : file_name, 

87 'total intensity (hull)' : info['area'], 

88 'total intensity' : info['simple_total_intensity'] if 'simple_total_intensity' in info else info['area'] 

89 } 

90 

91 if 'model_peaks' in info: 

92 new_sum_data['number of rings'] = len(info['model_peaks']) 

93 

94 self.df_sum = pd.concat([self.df_sum, pd.DataFrame.from_records([new_sum_data])]) 

95 # self.df_sum = self.df_sum.append(new_sum_data, ignore_index = True) # Future warning deprecated 

96 self.df_sum.reset_index() 

97 self.df_sum.to_csv(self.sum_file, index=False, columns=self.sum_header) # Write to csv file 

98 

99 # Add data to rings.csv 

100 if 'model_peaks' in info.keys() and len(info['model_peaks']) > 0 and len(info['merged_peaks']) > 0: 

101 new_datas = [] 

102 nRings = len(info['model_peaks']) 

103 models = info['ring_models'] 

104 errors = info['ring_errors'] 

105 fit_result = info['fitResult'] 

106 for i in range(nRings): 

107 if i not in models: 

108 continue 

109 ring = models[i] 

110 new_data = {'filename':file_name} 

111 new_data['ring'] = i+1 

112 new_data['S'] = fit_result['u' + str(i + 1)] 

113 new_data['peak sigma'] = fit_result['sigmad' + str(i + 1)] 

114 new_data['peak intensity'] = fit_result['alpha' + str(i + 1)] 

115 new_data['angle'] = ring['u'] % np.pi 

116 if '90rotation' in info and info['90rotation']: 

117 new_data['angle'] = (ring['u'] + np.pi/2) % np.pi 

118 new_data['angle sigma'] = ring['sigma'] 

119 new_data['angle amplitude'] = ring['alpha'] 

120 new_data['angle fitting error'] = errors[i] 

121 if 'peak_ds' in info: 

122 new_data['d'] = info['peak_ds'][i] 

123 else: 

124 new_data['d'] = '-' 

125 new_datas.append(new_data) 

126 try: 

127 self.df_rings = pd.concat([self.df_rings, pd.DataFrame.from_records(new_datas)]) 

128 # self.df_rings = self.df_rings.append(new_datas, ignore_index=True) # Future warning deprecated 

129 except Exception: 

130 pass 

131 else: 

132 for k in self.rings_header: 

133 new_data = {} 

134 if k == 'filename': 

135 new_data[k] = file_name 

136 else: 

137 new_data[k] = '-' 

138 self.df_rings = pd.concat([self.df_rings, pd.DataFrame.from_records([new_data])]) 

139 # self.df_rings = self.df_rings.append(new_data, ignore_index = True) # Future warning deprecated 

140 

141 self.df_rings.reset_index() 

142 self.df_rings.to_csv(self.rings_file, index=False, columns=self.rings_header) # Write to csv file