Skip to content

Commit 47bdd71

Browse files
committed
utils: Fix Python formatting and lint issues
Run ruff check --fix and ruff format across all Python utility scripts to fix lint warnings and standardise formatting. Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
1 parent a72746d commit 47bdd71

File tree

4 files changed

+211
-120
lines changed

4 files changed

+211
-120
lines changed

utils/colourspace_calcs.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,48 @@
99

1010
import numpy as np
1111

12-
BT601 = np.array([[0.299, 0.5870, 0.1140], [-0.168736, -0.331264, 0.5], [0.5, -0.418688, -0.081312]])
13-
REC709 = np.array([[0.2126, 0.7152, 0.0722], [-0.1146, -0.3854, 0.5], [0.5, -0.4542, -0.0458]])
14-
REC2020 = np.array([[0.2627, 0.6780, 0.0593], [-0.13963006, -0.36036994, 0.5], [0.5, -0.4597857, -0.0402143]])
12+
BT601 = np.array(
13+
[[0.299, 0.5870, 0.1140], [-0.168736, -0.331264, 0.5], [0.5, -0.418688, -0.081312]]
14+
)
15+
REC709 = np.array(
16+
[[0.2126, 0.7152, 0.0722], [-0.1146, -0.3854, 0.5], [0.5, -0.4542, -0.0458]]
17+
)
18+
REC2020 = np.array(
19+
[
20+
[0.2627, 0.6780, 0.0593],
21+
[-0.13963006, -0.36036994, 0.5],
22+
[0.5, -0.4597857, -0.0402143],
23+
]
24+
)
1525

1626
colour_spaces = {"select": "default"}
1727

28+
1829
def flatten(array):
1930
return [round(num) for num in list(array.flatten())]
2031

32+
2133
def add_entry(name, M, limited):
2234
offsets = np.array([0, 128, 128])
23-
scaling = np.array([[(235 - 16) / 255, 0, 0], [0, (240 - 16) / 255, 0], [0, 0, (240 - 16) / 255]])
35+
scaling = np.array(
36+
[[(235 - 16) / 255, 0, 0], [0, (240 - 16) / 255, 0], [0, 0, (240 - 16) / 255]]
37+
)
2438
if limited:
2539
offsets = np.array([16, 128, 128])
2640
M = np.matmul(scaling, M)
2741
Mi = np.linalg.inv(M)
2842
colour_spaces[name] = {}
2943
colour_spaces[name]["ycbcr"] = {}
3044
colour_spaces[name]["ycbcr"]["coeffs"] = flatten(M * 1024)
31-
colour_spaces[name]["ycbcr"]["offsets"] = flatten(offsets * (2 ** 18))
45+
colour_spaces[name]["ycbcr"]["offsets"] = flatten(offsets * (2**18))
3246
colour_spaces[name]["ycbcr_inverse"] = {}
3347
colour_spaces[name]["ycbcr_inverse"]["coeffs"] = flatten(Mi * 1024)
34-
inv_offsets = np.rint(np.dot(Mi, -offsets) * (2 ** 18))
48+
inv_offsets = np.rint(np.dot(Mi, -offsets) * (2**18))
3549
colour_spaces[name]["ycbcr_inverse"]["offsets"] = flatten(inv_offsets)
36-
if inv_offsets.min() < -2 ** 26 or inv_offsets.max() >= 2 ** 26:
50+
if inv_offsets.min() < -(2**26) or inv_offsets.max() >= 2**26:
3751
print("WARNING:", name, "will overflow!")
3852

53+
3954
add_entry("default", BT601, limited=False)
4055
add_entry("jpeg", BT601, limited=False)
4156
add_entry("smpte170m", BT601, limited=True)
@@ -44,18 +59,20 @@ def add_entry(name, M, limited):
4459
add_entry("bt2020", REC2020, limited=True)
4560
add_entry("bt2020_full", REC2020, limited=False)
4661

62+
4763
def print_dict(d, indent=0):
4864
print("{")
4965
indent += 4
5066
for i, (k, v) in enumerate(d.items()):
5167
if type(v) is dict:
52-
print(" " * indent, f'"{k}"', ": ", end='', sep='')
68+
print(" " * indent, f'"{k}"', ": ", end="", sep="")
5369
print_dict(v, indent)
5470
else:
55-
print(" " * indent, f'"{k}"', ": ", v, end='', sep='')
71+
print(" " * indent, f'"{k}"', ": ", v, end="", sep="")
5672
print("," if i < len(d) - 1 else "")
5773
indent -= 4
58-
print(" " * indent, "}", end ='', sep='')
74+
print(" " * indent, "}", end="", sep="")
75+
5976

6077
final_dict = {"colour_encoding": colour_spaces}
6178
print_dict(final_dict)

utils/generate_filter.py

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,18 @@ def mitchell(B, C, N):
2828
for i in range(N):
2929
ax = abs(x[i])
3030
if ax < 1:
31-
h[i] = ((12 - 9 * B - 6 * C) * ax**3 + (-18 + 12 * B + 6 * C) * ax**2 + (6 - 2 * B)) / 6
31+
h[i] = (
32+
(12 - 9 * B - 6 * C) * ax**3
33+
+ (-18 + 12 * B + 6 * C) * ax**2
34+
+ (6 - 2 * B)
35+
) / 6
3236
elif (ax >= 1) and (ax < 2):
33-
h[i] = ((-B - 6 * C) * ax**3 + (6 * B + 30 * C) * ax**2 + (-12 * B - 48 * C) * ax + (8 * B + 24 * C)) / 6
37+
h[i] = (
38+
(-B - 6 * C) * ax**3
39+
+ (6 * B + 30 * C) * ax**2
40+
+ (-12 * B - 48 * C) * ax
41+
+ (8 * B + 24 * C)
42+
) / 6
3443
return h
3544

3645

@@ -50,14 +59,33 @@ def bicubic_spline(a, N):
5059

5160
def main():
5261
parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter)
53-
parser.add_argument('--phases', metavar='P', type=int, help='Number of phases.', default=16)
54-
parser.add_argument('--taps', metavar='T', type=int, help='Number of filter taps per phase.', default=6)
55-
parser.add_argument('--precision', metavar='PR', type=int, help='Filter precision required.', default=10)
56-
parser.add_argument('--filter', type=str, metavar='F',
57-
help='Filter type and parameters, e.g.: \n'
58-
'"Mitchell, b = 0.333, c = 0.333"\n'
59-
'"Lanczos, order = 3"\n'
60-
'"bicubic_spline, a=-0.5"', required=True)
62+
parser.add_argument(
63+
"--phases", metavar="P", type=int, help="Number of phases.", default=16
64+
)
65+
parser.add_argument(
66+
"--taps",
67+
metavar="T",
68+
type=int,
69+
help="Number of filter taps per phase.",
70+
default=6,
71+
)
72+
parser.add_argument(
73+
"--precision",
74+
metavar="PR",
75+
type=int,
76+
help="Filter precision required.",
77+
default=10,
78+
)
79+
parser.add_argument(
80+
"--filter",
81+
type=str,
82+
metavar="F",
83+
help="Filter type and parameters, e.g.: \n"
84+
'"Mitchell, b = 0.333, c = 0.333"\n'
85+
'"Lanczos, order = 3"\n'
86+
'"bicubic_spline, a=-0.5"',
87+
required=True,
88+
)
6189

6290
args = parser.parse_args()
6391

@@ -66,24 +94,26 @@ def main():
6694
precision = args.precision
6795

6896
# Parse the filter string and pick out the needed parameters.
69-
filt = args.filter.split(',')
70-
params = {'a': 0., 'b': 0., 'c': 0., 'order': 0}
97+
filt = args.filter.split(",")
98+
params = {"a": 0.0, "b": 0.0, "c": 0.0, "order": 0}
7199
for param in filt[1:]:
72-
p = param.replace(' ', '').split('=')
100+
p = param.replace(" ", "").split("=")
73101
params[p[0]] = type(params[p[0]])(p[1])
74102

75103
# Generate the filter.
76-
if (filt[0].lower() == 'mitchell'):
77-
filter = f'"Michell - Netravali (B = {params["b"]:.3f}, C = {params["c"]:.3f})": [\n'
78-
h = mitchell(params['b'], params['c'], phases * taps)
79-
elif (filt[0].lower() == 'lanczos'):
104+
if filt[0].lower() == "mitchell":
105+
filter = (
106+
f'"Michell - Netravali (B = {params["b"]:.3f}, C = {params["c"]:.3f})": [\n'
107+
)
108+
h = mitchell(params["b"], params["c"], phases * taps)
109+
elif filt[0].lower() == "lanczos":
80110
filter = f'"Lanczos order {params["order"]}": [\n'
81-
h = lanczos(params['order'], phases * taps)
82-
elif (filt[0].lower() == 'bicubic_spline'):
111+
h = lanczos(params["order"], phases * taps)
112+
elif filt[0].lower() == "bicubic_spline":
83113
filter = f'"Bicubic-spline (a = {params["a"]:.3f})": [\n'
84-
h = bicubic_spline(params['a'], phases * taps)
114+
h = bicubic_spline(params["a"], phases * taps)
85115
else:
86-
print(f'Invalid filter ({filt[0]}) selected!')
116+
print(f"Invalid filter ({filt[0]}) selected!")
87117
exit()
88118

89119
# Normalise and convert to fixed-point.
@@ -98,13 +128,13 @@ def main():
98128
max_index = np.nonzero(ppf[i] == ppf[i].max())[0]
99129
ppf[i, max_index] += (1 << precision) - np.int32(ppf[i].sum() / max_index.size)
100130

101-
nl = '\n'
131+
nl = "\n"
102132
for i in range(phases):
103-
phase = ', '.join([f'{c:>4}' for c in ppf[i, :]])
104-
filter += f' {phase}{nl+"]" if i==phases-1 else ","+nl}'
133+
phase = ", ".join([f"{c:>4}" for c in ppf[i, :]])
134+
filter += f" {phase}{nl + ']' if i == phases - 1 else ',' + nl}"
105135

106136
print(filter)
107137

108138

109-
if __name__ == '__main__':
139+
if __name__ == "__main__":
110140
main()

0 commit comments

Comments
 (0)