Sourcery Starbot ⭐ refactored vinothpandian/akin-generator#7
Sourcery Starbot ⭐ refactored vinothpandian/akin-generator#7SourceryAI wants to merge 1 commit intovinothpandian:mainfrom
Conversation
| response = generate_wireframe_samples(ui_design_pattern_type, sample_num=8) | ||
|
|
||
| return response | ||
| return generate_wireframe_samples(ui_design_pattern_type, sample_num=8) |
There was a problem hiding this comment.
Function generate_wireframes refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| self.gamma = self.add_weight(self.name + '_gamma', | ||
| shape=(), | ||
| initializer=tf.initializers.Zeros) | ||
| self.gamma = self.add_weight( | ||
| f'{self.name}_gamma', shape=(), initializer=tf.initializers.Zeros | ||
| ) |
There was a problem hiding this comment.
Function _Attention.build refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation)
|
|
||
| input_shape = tf.shape(query_tensor) | ||
|
|
||
| if self.data_format == 'channels_first': | ||
| height_axis = 2 | ||
| width_axis = 3 | ||
| else: | ||
| height_axis = 1 | ||
| width_axis = 2 | ||
|
|
||
| batchsize = input_shape[0] | ||
| height = input_shape[height_axis] | ||
| width = input_shape[width_axis] | ||
|
|
There was a problem hiding this comment.
Found the following improvement in Function _Attention.call:
| i = 0 | ||
| for k, v in label_color_map.items(): | ||
| for i, (k, v) in enumerate(label_color_map.items()): | ||
| img[(i * side) : (i * side) + side, 0:side, :] = [v[0], v[1], v[2]] | ||
| bottomLeftCornerOfText = (int(side * 1.5), (i * side) + int(side / 2)) | ||
| bottomLeftCornerOfText = int(side * 1.5), i * side + side // 2 | ||
| img = cv2.putText(img, str(k), bottomLeftCornerOfText, font, fontScale, fontColor, lineType) | ||
| i += 1 |
There was a problem hiding this comment.
Function ColorMapper.save_color_map refactored with the following changes:
- Replace manual loop counter with call to enumerate (
convert-to-enumerate) - Simplify division expressions (
simplify-division)
| sorted_color = [] | ||
| sorted_color.append(colors.pop(max_i)) | ||
| sorted_color = [colors.pop(max_i)] |
There was a problem hiding this comment.
Function ColorMapper.get_sorted_colors refactored with the following changes:
- Merge append into list declaration (
merge-list-append)
| if data is not None and len(data) > 0: | ||
| if data is not None and data: |
There was a problem hiding this comment.
Function create_json_file refactored with the following changes:
- Simplify sequence length comparison (
simplify-len-comparison)
| img_path = os.path.join(android_elements_base_path, img_name + ".jpg") | ||
| img_path = os.path.join(android_elements_base_path, f"{img_name}.jpg") | ||
| if not os.path.exists(img_path): | ||
| img_path = os.path.join(android_elements_base_path, img_name + ".png") | ||
| img_path = os.path.join(android_elements_base_path, f"{img_name}.png") | ||
| img = cv2.imread(img_path, cv2.IMREAD_COLOR) | ||
| else: | ||
| img = None | ||
| text = s[2] | ||
| if text is None or len(text) == 0: | ||
| text = None | ||
| else: | ||
| text = text.strip().split(",") | ||
| text = None if text is None or len(text) == 0 else text.strip().split(",") |
There was a problem hiding this comment.
Function load_all_ui_images refactored with the following changes:
- Use f-string instead of string concatenation [×2] (
use-fstring-for-concatenation) - Replace if statement with if expression (
assign-if-exp)
| else: | ||
| shapes = data["shapes"] | ||
| flags = data["flags"] | ||
| for shape in shapes: | ||
| label = shape["label"] | ||
| points = shape["points"] | ||
| elements.append( | ||
| [label, [int(points[0][0]), int(points[0][1]), int(points[1][0]), int(points[1][1])]] | ||
| ) | ||
| shapes = data["shapes"] | ||
| flags = data["flags"] | ||
| for shape in shapes: | ||
| label = shape["label"] | ||
| points = shape["points"] | ||
| elements.append( | ||
| [label, [int(points[0][0]), int(points[0][1]), int(points[1][0]), int(points[1][1])]] | ||
| ) |
There was a problem hiding this comment.
Function get_elements refactored with the following changes:
- Remove unnecessary else after guard condition (
remove-unnecessary-else)
| elif flag == 1 or flag == 5: | ||
| elif flag in [1, 5]: | ||
| return cv2.resize(img, (w, h)), 0 | ||
| elif flag == 2 or flag == 3 or flag == 4: | ||
| elif flag in [2, 3, 4]: |
There was a problem hiding this comment.
Function element_resize refactored with the following changes:
- Replace multiple comparisons of same variable with
inoperator [×2] (merge-comparisons)
| elif flag == 1 or flag == 5: | ||
| elif flag in [1, 5]: | ||
| return cv2.resize(img, (w, h)), 0 | ||
| elif flag == 2 or flag == 3 or flag == 4: | ||
| elif flag in [2, 3, 4]: |
There was a problem hiding this comment.
Function element_resize_old refactored with the following changes:
- Replace multiple comparisons of same variable with
inoperator [×2] (merge-comparisons)
| x = x1 | ||
| y = y1 | ||
| w = x2 - x1 | ||
| h = y2 - y1 | ||
| if x1 >= 0 and y1 >= 0 and x2 < img_w and y2 < img_h and w > 0 and h > 0: | ||
| x = x1 | ||
| y = y1 | ||
| if not real and (h < 20 or w < 20): | ||
| continue | ||
| elif h <= 0 or w <= 0 or y >= img_h or x >= img_w: | ||
| elif y >= img_h or x >= img_w: |
There was a problem hiding this comment.
Function create_img refactored with the following changes:
- Move assignments closer to their usage (
move-assign) - Remove redundant conditional (
remove-redundant-if) - Replace multiple comparisons of same variable with
inoperator (merge-comparisons) - Remove unnecessary call to keys() [×2] (
remove-dict-keys)
| return text, False | ||
| new_length = len(text) - 4 | ||
| r = text[:new_length] | ||
| return r, True |
There was a problem hiding this comment.
Function reduce_text_size refactored with the following changes:
- Swap if/else branches (
swap-if-else-branches) - Remove unnecessary else after guard condition (
remove-unnecessary-else) - Simplify numeric comparison (
simplify-numeric-comparison) - Replace a[0:x] with a[:x] and a[x:len(a)] with a[x:] (
remove-redundant-slice-index)
| if label_resize == 3 and fw > 0 and w > fw: | ||
| accesible_w = w - fw | ||
| else: | ||
| accesible_w = w | ||
| accesible_w = w - fw if label_resize == 3 and fw > 0 and w > fw else w |
There was a problem hiding this comment.
Function add_text_pil refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp)
| dst_file_path = os.path.join(dst_folder_path, str(count) + "_" + str(real) + ".jpg") | ||
| dst_file_path = os.path.join(dst_folder_path, f"{str(count)}_{str(real)}.jpg") |
There was a problem hiding this comment.
Lines 348-348 refactored with the following changes:
- Use f-string instead of string concatenation [×3] (
use-fstring-for-concatenation)
| x, attn1 = SelfAttnModel(curr_filters)(x) | ||
|
|
||
| for i in range(repeat_num - 4): | ||
| for _ in range(repeat_num - 4): |
There was a problem hiding this comment.
Function create_generator refactored with the following changes:
- Replace unused for index with underscore (
for-index-underscore)
| if image_label in cat_count.keys() and cat_count[image_label] >= 100: | ||
| continue | ||
| else: | ||
| if image_label not in cat_count.keys(): | ||
| images.append(image_path) | ||
| labels.append(image_label) | ||
| if image_label not in cat_count.keys(): | ||
| cat_count[image_label] = 1 | ||
| else: | ||
| cat_count[image_label] += 1 | ||
| cat_count[image_label] = 1 | ||
| elif cat_count[image_label] < 100: | ||
| images.append(image_path) | ||
| labels.append(image_label) | ||
| cat_count[image_label] += 1 |
There was a problem hiding this comment.
Function Trainer.get_data_generator refactored with the following changes:
- Remove redundant continue statement (
remove-redundant-continue) - Split conditional into multiple branches (
split-or-ifs) - Remove redundant conditional [×2] (
remove-redundant-if)
| print("epoch: {}".format(self.epoch)) | ||
| print(f"epoch: {self.epoch}") |
There was a problem hiding this comment.
Function Trainer.train refactored with the following changes:
- Replace call to format with f-string (
use-fstring-for-formatting)
| else: | ||
| label_count = UILabelFileManager.get_label_counts(label_count_file) | ||
| hierarchy_map = UILabelFileManager.get_ui_hierarchy_map(heirarchy_file) | ||
| hierarchy_count = {} | ||
| for k, v in hierarchy_map.items(): | ||
| if len(v) < level - 1: | ||
| label = v[-1] | ||
| else: | ||
| label = v[level - 1] | ||
| if k not in label_count.keys(): | ||
| count = 0 | ||
| print(k + " not in label count") | ||
| else: | ||
| count = label_count[k] | ||
| if label in hierarchy_count.keys(): | ||
| hierarchy_count[label] += count | ||
| else: | ||
| hierarchy_count[label] = count | ||
| heirarchy_count_list = list(hierarchy_count.items()) | ||
| heirarchy_count_list.sort(key=lambda x: x[1], reverse=True) | ||
| print(heirarchy_count_list) | ||
| if with_count: | ||
| return heirarchy_count_list | ||
| label_count = UILabelFileManager.get_label_counts(label_count_file) | ||
| hierarchy_map = UILabelFileManager.get_ui_hierarchy_map(heirarchy_file) | ||
| hierarchy_count = {} | ||
| for k, v in hierarchy_map.items(): | ||
| label = v[-1] if len(v) < level - 1 else v[level - 1] | ||
| if k not in label_count.keys(): | ||
| count = 0 | ||
| print(f"{k} not in label count") | ||
| else: | ||
| count = label_count[k] | ||
| if label in hierarchy_count: | ||
| hierarchy_count[label] += count | ||
| else: | ||
| sorted_labels = [v[0] for v in heirarchy_count_list] | ||
| return sorted_labels | ||
| hierarchy_count[label] = count | ||
| heirarchy_count_list = list(hierarchy_count.items()) | ||
| heirarchy_count_list.sort(key=lambda x: x[1], reverse=True) | ||
| print(heirarchy_count_list) | ||
| return ( | ||
| heirarchy_count_list | ||
| if with_count | ||
| else [v[0] for v in heirarchy_count_list] | ||
| ) |
There was a problem hiding this comment.
Function UILabelFileManager.get_sorted_labels_for_hierarchy refactored with the following changes:
- Remove unnecessary else after guard condition (
remove-unnecessary-else) - Replace if statement with if expression [×2] (
assign-if-exp) - Use f-string instead of string concatenation (
use-fstring-for-concatenation) - Remove unnecessary call to keys() (
remove-dict-keys) - Inline variable that is immediately returned (
inline-immediately-returned-variable)
| if level >= 1: | ||
| hierarchy_map = UILabelFileManager.get_ui_hierarchy_map(file) | ||
| hierarchy_label_map = {} | ||
| for k, v in hierarchy_map.items(): | ||
| if len(v) < level - 1: | ||
| label = v[-1] | ||
| else: | ||
| label = v[level - 1] | ||
| hierarchy_label_map[k] = label | ||
| return hierarchy_label_map | ||
| else: | ||
| if level < 1: | ||
| return {} | ||
| hierarchy_map = UILabelFileManager.get_ui_hierarchy_map(file) | ||
| hierarchy_label_map = {} | ||
| for k, v in hierarchy_map.items(): | ||
| label = v[-1] if len(v) < level - 1 else v[level - 1] | ||
| hierarchy_label_map[k] = label | ||
| return hierarchy_label_map |
There was a problem hiding this comment.
Function UILabelFileManager.get_hierarchy_label_map refactored with the following changes:
- Add guard clause (
last-if-guard) - Replace if statement with if expression (
assign-if-exp)
| if ".directory" not in name: | ||
| print(name) | ||
| json_name = name[:-3] + "json" | ||
| json_name = f"{name[:-3]}json" |
There was a problem hiding this comment.
Function Utils.copy_json_files refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation)
Thanks for starring sourcery-ai/sourcery ✨ 🌟 ✨
Here's your pull request refactoring your most popular Python repo.
If you want Sourcery to refactor all your Python repos and incoming pull requests install our bot.
Review changes via command line
To manually merge these changes, make sure you're on the
mainbranch, then run: