| Conditions | 16 |
| Total Lines | 60 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like LearningResult.export_to_xlsx() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | import time |
||
| 101 | def export_to_xlsx(self, filename, home_info=None): |
||
| 102 | """Export to XLSX |
||
| 103 | |||
| 104 | Args: |
||
| 105 | filename (:obj:`str`): path to the file |
||
| 106 | home_info (:class:`pyActLearn.CASAS.fuel.CASASFuel`): dataset information |
||
| 107 | """ |
||
| 108 | if home_info is None: |
||
| 109 | home_info = CASASFuel(dir_name=self.data) |
||
| 110 | workbook = xlsxwriter.Workbook(filename) |
||
| 111 | records = self.get_record_keys() |
||
| 112 | num_performance = len(per_class_performance_index) |
||
| 113 | num_classes = self.confusion_matrix.shape[0] |
||
| 114 | # Overall Performance Summary |
||
| 115 | overall_sheet = workbook.add_worksheet('overall') |
||
| 116 | overall_sheet.merge_range(0, 0, 0, len(overall_performance_index) - 1, 'Overall Performance') |
||
| 117 | for c in range(len(overall_performance_index)): |
||
| 118 | overall_sheet.write(1, c, str(overall_performance_index[c])) |
||
| 119 | overall_sheet.write(2, c, self.overall_performance[c]) |
||
| 120 | overall_sheet.merge_range(4, 0, 4, len(per_class_performance_index), 'Per-Class Performance') |
||
| 121 | overall_sheet.write(5, 0, 'Activities') |
||
| 122 | for c in range(len(per_class_performance_index)): |
||
| 123 | overall_sheet.write(5, c + 1, str(per_class_performance_index[c])) |
||
| 124 | for r in range(num_classes): |
||
| 125 | label = home_info.get_activity_by_index(r) |
||
| 126 | overall_sheet.write(r + 6, 0, label) |
||
| 127 | for c in range(num_performance): |
||
| 128 | overall_sheet.write(r + 6, c + 1, self.per_class_performance[r][c]) |
||
| 129 | overall_sheet.merge_range(8 + num_classes, 0, 8 + num_classes, num_classes, 'Confusion Matrix') |
||
| 130 | for i in range(num_classes): |
||
| 131 | label = home_info.get_activity_by_index(i) |
||
| 132 | overall_sheet.write(9 + num_classes, i + 1, label) |
||
| 133 | overall_sheet.write(10 + num_classes + i, 0, label) |
||
| 134 | for r in range(num_classes): |
||
| 135 | for c in range(num_classes): |
||
| 136 | overall_sheet.write(10 + num_classes + r, c + 1, self.confusion_matrix[r][c]) |
||
| 137 | # Weekly Performance Summary |
||
| 138 | weekly_sheet = workbook.add_worksheet('weekly') |
||
| 139 | weekly_list_title = ['dataset', '#week'] + overall_performance_index |
||
| 140 | for c in range(len(weekly_list_title)): |
||
| 141 | weekly_sheet.write(0, c, str(weekly_list_title[c])) |
||
| 142 | r = 1 |
||
| 143 | for record_id in records: |
||
| 144 | weekly_sheet.write(r, 0, 'b1') |
||
| 145 | weekly_sheet.write(r, 1, record_id) |
||
| 146 | for c in range(len(overall_performance_index)): |
||
| 147 | weekly_sheet.write(r, c + 2, '%.5f' % self.get_record_by_key(record_id)['overall_performance'][c]) |
||
| 148 | r += 1 |
||
| 149 | dataset_list_title = ['activities'] + per_class_performance_index |
||
| 150 | # Per Week Per Class Summary |
||
| 151 | for record_id in self.get_record_keys(): |
||
| 152 | cur_sheet = workbook.add_worksheet(record_id) |
||
| 153 | for c in range(0, len(dataset_list_title)): |
||
| 154 | cur_sheet.write(0, c, str(dataset_list_title[c])) |
||
| 155 | for r in range(num_classes): |
||
| 156 | label = home_info.get_activity_by_index(r) |
||
| 157 | cur_sheet.write(r+1, 0, label) |
||
| 158 | for c in range(num_performance): |
||
| 159 | cur_sheet.write(r + 1, c + 1, self.get_record_by_key(record_id)['per_class_performance'][r][c]) |
||
| 160 | workbook.close() |
||
| 161 |