Skip to content

Commit 5000bb6

Browse files
Enable more rules - Part 3 (#225)
* Enable more rules Signed-off-by: Ashwin Vaidya <[email protected]> * Enable some more flake8 rules Signed-off-by: Ashwin Vaidya <[email protected]> * Enable more rules Signed-off-by: Ashwin Vaidya <[email protected]> --------- Signed-off-by: Ashwin Vaidya <[email protected]>
1 parent 56b68ca commit 5000bb6

File tree

17 files changed

+50
-63
lines changed

17 files changed

+50
-63
lines changed

model_api/python/model_api/adapters/onnx_adapter.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ def __init__(self, model: str, ort_options: dict = {}):
4242
loaded_model = onnx.load(model)
4343

4444
inferred_model = SymbolicShapeInference.infer_shapes(
45-
loaded_model,
46-
int(sys.maxsize / 2),
47-
False,
48-
False,
49-
False,
45+
in_mp=loaded_model,
46+
int_max=int(sys.maxsize / 2),
47+
auto_merge=False,
48+
guess_output_rank=False,
49+
verbose=False,
5050
)
5151

5252
self.session = ort.InferenceSession(

model_api/python/model_api/adapters/openvino_adapter.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,7 @@ def get_input_layers(self):
233233
input_layout,
234234
input.get_element_type().get_type_name(),
235235
)
236-
inputs = self._get_meta_from_ngraph(inputs)
237-
return inputs
236+
return self._get_meta_from_ngraph(inputs)
238237

239238
def get_layout_for_input(self, input, shape=None) -> str:
240239
input_layout = ""
@@ -263,8 +262,7 @@ def get_output_layers(self):
263262
list(output_shape),
264263
precision=output.get_element_type().get_type_name(),
265264
)
266-
outputs = self._get_meta_from_ngraph(outputs)
267-
return outputs
265+
return self._get_meta_from_ngraph(outputs)
268266

269267
def reshape_model(self, new_shape):
270268
new_shape = {

model_api/python/model_api/adapters/utils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,15 +273,14 @@ def crop_resize_graph(input: Output, size):
273273

274274
target_size = list(size)
275275
target_size.reverse()
276-
resized_image = opset.interpolate(
276+
return opset.interpolate(
277277
cropped_frame,
278278
target_size,
279279
scales=np.array([0.0, 0.0], dtype=np.float32),
280280
axes=[h_axis, w_axis],
281281
mode="linear",
282282
shape_calculation_mode="sizes",
283283
)
284-
return resized_image
285284

286285

287286
def resize_image_graph(

model_api/python/model_api/models/anomaly.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,7 @@ def parameters(cls) -> dict:
126126
def _normalize(self, tensor: np.ndarray, threshold: float) -> np.ndarray:
127127
"""Currently supports only min-max normalization."""
128128
normalized = ((tensor - threshold) / self.normalization_scale) + 0.5
129-
normalized = np.clip(normalized, 0, 1)
130-
return normalized
129+
return np.clip(normalized, 0, 1)
131130

132131
@staticmethod
133132
def _get_boxes(mask: np.ndarray) -> np.ndarray:

model_api/python/model_api/models/classification.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import copy
99
import json
1010
from collections import defaultdict
11+
from pathlib import Path
1112

1213
import numpy as np
1314
from openvino.preprocess import PrePostProcessor
@@ -102,7 +103,7 @@ def __init__(self, inference_adapter: InferenceAdapter, configuration: dict = {}
102103
self.load()
103104

104105
def _load_labels(self, labels_file):
105-
with open(labels_file) as f:
106+
with Path(labels_file).open() as f:
106107
labels = []
107108
for s in f:
108109
begin_idx = s.find(" ")
@@ -417,8 +418,7 @@ def get_predecessors(lbl, candidates):
417418
if new_lbl not in output_labels:
418419
output_labels.append(new_lbl)
419420

420-
output_predictions = [(self.label_to_idx[lbl], lbl, label_to_prob[lbl]) for lbl in sorted(output_labels)]
421-
return output_predictions
421+
return [(self.label_to_idx[lbl], lbl, label_to_prob[lbl]) for lbl in sorted(output_labels)]
422422

423423

424424
class ProbabilisticLabelsResolver(GreedyLabelsResolver):

model_api/python/model_api/models/instance_segmentation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def parameters(cls):
4545
)
4646
return parameters
4747

48-
def _get_outputs(self):
48+
def _get_outputs(self): # noqa: C901 TODO: Fix this method to reduce complexity
4949
if self.is_segmentoly:
5050
return self._get_segmentoly_outputs()
5151
filtered_names = []
@@ -82,7 +82,7 @@ def _get_outputs(self):
8282
if len(outputs) == 3:
8383
_append_xai_names(self.outputs, outputs)
8484
return outputs
85-
self.raise_error(f"Unexpected outputs: {self.outputs}")
85+
return self.raise_error(f"Unexpected outputs: {self.outputs}")
8686

8787
def _get_segmentoly_outputs(self):
8888
outputs = {}

model_api/python/model_api/models/model.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,9 @@ def get_model_class(cls, name):
105105
for subclass in subclasses:
106106
if name.lower() == subclass.__model__.lower():
107107
return subclass
108-
cls.raise_error(
109-
'There is no model with name "{}" in list: {}'.format(
110-
name,
111-
", ".join([subclass.__model__ for subclass in subclasses]),
112-
),
108+
return cls.raise_error(
109+
f"There is no model with name {name} in list: "
110+
f"{', '.join([subclass.__model__ for subclass in subclasses])}",
113111
)
114112

115113
@classmethod
@@ -214,8 +212,7 @@ def parameters(cls):
214212
Returns:
215213
- the dictionary with defined wrapper data parameters
216214
"""
217-
parameters = {}
218-
return parameters
215+
return {}
219216

220217
def _load_config(self, config):
221218
"""Reads the configuration and creates data attributes

model_api/python/model_api/models/result_types.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,5 +243,4 @@ def __str__(self):
243243
def _array_shape_to_str(array: np.ndarray | None) -> str:
244244
if array is not None:
245245
return f"[{','.join(str(i) for i in array.shape)}]"
246-
else:
247-
return "[]"
246+
return "[]"

model_api/python/model_api/models/segmentation.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,13 @@ def postprocess(self, outputs, meta):
209209
input_image_width = meta["original_shape"][1]
210210
result = outputs[self.output_blob_name].squeeze()
211211
result = 1 / (1 + np.exp(-result))
212-
result = cv2.resize(
212+
return cv2.resize(
213213
result,
214214
(input_image_width, input_image_height),
215215
0,
216216
0,
217217
interpolation=cv2.INTER_NEAREST,
218218
)
219-
return result
220219

221220

222221
_feature_vector_name = "feature_vector"

model_api/python/model_api/models/ssd.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ def _get_output_parser(
6161
return parser
6262
except ValueError:
6363
pass
64-
self.raise_error("Unsupported model outputs")
64+
msg = "Unsupported model outputs"
65+
raise ValueError(msg)
6566

6667
def _parse_outputs(self, outputs):
6768
return self.output_parser(outputs)
@@ -156,8 +157,7 @@ def __call__(self, outputs):
156157
labels = np.full(len(bboxes), self.default_label, dtype=bboxes.dtype)
157158
labels = labels.squeeze(0)
158159

159-
detections = [Detection(*bbox, score, label) for label, score, bbox in zip(labels, scores, bboxes)]
160-
return detections
160+
return [Detection(*bbox, score, label) for label, score, bbox in zip(labels, scores, bboxes)]
161161

162162

163163
_bbox_area_threshold = 1.0

0 commit comments

Comments
 (0)