From 5daa54fafb75695f191a3f2be405fae93b432641 Mon Sep 17 00:00:00 2001 From: Nicolas Kruse Date: Thu, 4 Dec 2025 18:19:33 +0100 Subject: [PATCH] code style adjustments --- src/copapy/_stencils.py | 8 +++--- src/copapy/_target.py | 6 ++--- src/copapy/filters.py | 7 +++--- tests/test_ast_gen.py | 12 ++++----- tests/test_comp_timing.py | 16 ++++++------ tests/test_matrix.py | 52 +++++++++++++++++++-------------------- tests/test_ops_aarch64.py | 4 +-- tests/test_ops_armv7.py | 4 +-- tests/test_ops_x86.py | 20 +++++++-------- 9 files changed, 65 insertions(+), 64 deletions(-) diff --git a/src/copapy/_stencils.py b/src/copapy/_stencils.py index 3c74561..75d16f0 100644 --- a/src/copapy/_stencils.py +++ b/src/copapy/_stencils.py @@ -167,7 +167,7 @@ class stencil_database(): # cache miss: cache: list[relocation_entry] = [] self._relocation_cache[cache_key] = cache - + symbol = self.elf.symbols[symbol_name] if stencil: start_index, end_index = get_stencil_position(symbol) @@ -353,11 +353,11 @@ class stencil_database(): def get_symbol_size(self, name: str) -> int: """Returns the size of a specified symbol name.""" return self.elf.symbols[name].fields['st_size'] - + def get_symbol_offset(self, name: str) -> int: """Returns the offset of a specified symbol in the section.""" return self.elf.symbols[name].fields['st_value'] - + def get_symbol_section_index(self, name: str) -> int: """Returns the section index for a specified symbol name.""" return self.elf.symbols[name].fields['st_shndx'] @@ -365,7 +365,7 @@ class stencil_database(): def get_section_size(self, index: int) -> int: """Returns the size of a section specified by index.""" return self.elf.sections[index].fields['sh_size'] - + def get_section_alignment(self, index: int) -> int: """Returns the required alignment of a section specified by index.""" return self.elf.sections[index].fields['sh_addralign'] diff --git a/src/copapy/_target.py b/src/copapy/_target.py index 6039ce5..13fe4f6 100644 --- a/src/copapy/_target.py +++ b/src/copapy/_target.py @@ -75,11 +75,11 @@ class Target(): """ if isinstance(net, Iterable): return [self.read_value(ni) if isinstance(ni, variable) else ni for ni in net] - + if isinstance(net, float | int): - print(f"Warning: value is not a copypy value") + print("Warning: value is not a copypy value") return net - + assert isinstance(net, Net), "Variable must be a copapy variable object" assert net in self._variables, f"Variable {net} not found. It might not have been compiled for the target." addr, lengths, var_type = self._variables[net] diff --git a/src/copapy/filters.py b/src/copapy/filters.py index 8639b1f..abd6d44 100644 --- a/src/copapy/filters.py +++ b/src/copapy/filters.py @@ -1,5 +1,6 @@ from . import variable, vector -from ._basic_types import iif, unifloat, TNum +from ._basic_types import iif, unifloat +from._helper_types import TNum from typing import Any, Iterable @@ -18,7 +19,7 @@ def _inv_argsort(input_vector: vector[TNum]) -> vector[int]: def argsort(input_vector: vector[TNum]) -> vector[int]: """ - Perform an indirect sort. It returns an array of indices that index data + Perform an indirect sort. It returns an array of indices that index data in sorted order. Args: @@ -61,4 +62,4 @@ def mean(input_vector: vector[Any]) -> unifloat: Returns: unifloat: The mean value of the input vector. """ - return input_vector.sum() / len(input_vector) \ No newline at end of file + return input_vector.sum() / len(input_vector) diff --git a/tests/test_ast_gen.py b/tests/test_ast_gen.py index 0c3ebb0..89ee086 100644 --- a/tests/test_ast_gen.py +++ b/tests/test_ast_gen.py @@ -1,6 +1,6 @@ from copapy import variable from copapy.backend import Write -import copapy.backend as cpbe +import copapy.backend as cpb def test_ast_generation(): @@ -33,27 +33,27 @@ def test_ast_generation(): print(out) print('-- get_edges:') - edges = list(cpbe.get_all_dag_edges(out)) + edges = list(cpb.get_all_dag_edges(out)) for p in edges: print('#', p) print('-- get_ordered_ops:') - ordered_ops = list(cpbe.stable_toposort(edges)) + ordered_ops = cpb.stable_toposort(edges) for p in ordered_ops: print('#', p) print('-- get_consts:') - const_list = cpbe.get_const_nets(ordered_ops) + const_list = cpb.get_const_nets(ordered_ops) for p in const_list: print('#', p) print('-- add_read_ops:') - output_ops = list(cpbe.add_read_ops(ordered_ops)) + output_ops = list(cpb.add_read_ops(ordered_ops)) for p in output_ops: print('#', p) print('-- add_write_ops:') - extended_output_ops = list(cpbe.add_write_ops(output_ops, const_list)) + extended_output_ops = list(cpb.add_write_ops(output_ops, const_list)) for p in extended_output_ops: print('#', p) print('--') diff --git a/tests/test_comp_timing.py b/tests/test_comp_timing.py index 9dc52b0..301851e 100644 --- a/tests/test_comp_timing.py +++ b/tests/test_comp_timing.py @@ -1,16 +1,16 @@ import time -from copapy import variable from copapy import backend from copapy.backend import Write, stencil_db_from_package -import copapy.backend as cpbe +import copapy.backend as cpb import copapy as cp import copapy._binwrite as binw from copapy._compiler import get_nets, get_section_layout, get_data_layout from copapy._compiler import patch_entry, CPConstant, get_aux_func_layout + def test_timing_compiler(): t1 = cp.vector([10, 11]*128) + cp.vector(cp.variable(v) for v in range(256)) - t2 = t1.sum() + #t2 = t1.sum() t3 = cp.vector(cp.variable(1 / (v + 1)) for v in range(256)) t5 = ((t3 * t1) * 2).magnitude() out = [Write(t5)] @@ -19,7 +19,7 @@ def test_timing_compiler(): print('-- get_edges:') t0 = time.time() - edges = list(cpbe.get_all_dag_edges(out)) + edges = list(cpb.get_all_dag_edges(out)) t1 = time.time() print(f' found {len(edges)} edges') #for p in edges: @@ -28,7 +28,7 @@ def test_timing_compiler(): print('-- get_ordered_ops:') t0 = time.time() - ordered_ops = list(cpbe.stable_toposort(edges)) + ordered_ops = cpb.stable_toposort(edges) t1 = time.time() print(f' found {len(ordered_ops)} ops') #for p in ordered_ops: @@ -37,7 +37,7 @@ def test_timing_compiler(): print('-- get_consts:') t0 = time.time() - const_net_list = cpbe.get_const_nets(ordered_ops) + const_net_list = cpb.get_const_nets(ordered_ops) t1 = time.time() #for p in const_list: # print('#', p) @@ -45,7 +45,7 @@ def test_timing_compiler(): print('-- add_read_ops:') t0 = time.time() - output_ops = list(cpbe.add_read_ops(ordered_ops)) + output_ops = list(cpb.add_read_ops(ordered_ops)) t1 = time.time() #for p in output_ops: # print('#', p) @@ -53,7 +53,7 @@ def test_timing_compiler(): print('-- add_write_ops:') t0 = time.time() - extended_output_ops = list(cpbe.add_write_ops(output_ops, const_net_list)) + extended_output_ops = list(cpb.add_write_ops(output_ops, const_net_list)) t1 = time.time() #for p in extended_output_ops: # print('#', p) diff --git a/tests/test_matrix.py b/tests/test_matrix.py index 2930f34..324b481 100644 --- a/tests/test_matrix.py +++ b/tests/test_matrix.py @@ -25,7 +25,7 @@ def test_matrix_addition(): m1 = cp.matrix([[1, 2], [3, 4]]) m2 = cp.matrix([[5, 6], [7, 8]]) m3 = m1 + m2 - + assert m3[0] == (6, 8) assert m3[1] == (10, 12) @@ -34,7 +34,7 @@ def test_matrix_scalar_addition(): """Test matrix addition with scalar""" m1 = cp.matrix([[1, 2], [3, 4]]) m2 = m1 + 5 - + assert m2[0] == (6, 7) assert m2[1] == (8, 9) @@ -44,7 +44,7 @@ def test_matrix_subtraction(): m1 = cp.matrix([[5, 6], [7, 8]]) m2 = cp.matrix([[1, 2], [3, 4]]) m3 = m1 - m2 - + assert m3[0] == (4, 4) assert m3[1] == (4, 4) @@ -53,7 +53,7 @@ def test_matrix_scalar_subtraction(): """Test matrix subtraction with scalar""" m1 = cp.matrix([[5, 6], [7, 8]]) m2 = m1 - 2 - + assert m2[0] == (3, 4) assert m2[1] == (5, 6) @@ -62,7 +62,7 @@ def test_matrix_negation(): """Test matrix negation""" m1 = cp.matrix([[1, 2], [3, 4]]) m2 = -m1 - + assert m2[0] == (-1, -2) assert m2[1] == (-3, -4) @@ -72,7 +72,7 @@ def test_matrix_element_wise_multiplication(): m1 = cp.matrix([[1, 2], [3, 4]]) m2 = cp.matrix([[5, 6], [7, 8]]) m3 = m1 * m2 - + assert m3[0] == (5, 12) assert m3[1] == (21, 32) @@ -81,7 +81,7 @@ def test_matrix_scalar_multiplication(): """Test matrix multiplication with scalar""" m1 = cp.matrix([[1, 2], [3, 4]]) m2 = m1 * 3 - + assert m2[0] == (3, 6) assert m2[1] == (9, 12) @@ -91,7 +91,7 @@ def test_matrix_element_wise_division(): m1 = cp.matrix([[6.0, 8.0], [12.0, 16.0]]) m2 = cp.matrix([[2.0, 2.0], [3.0, 4.0]]) m3 = m1 / m2 - + assert m3[0][0] == pytest.approx(3.0) # pyright: ignore[reportUnknownMemberType] assert m3[0][1] == pytest.approx(4.0) # pyright: ignore[reportUnknownMemberType] assert m3[1][0] == pytest.approx(4.0) # pyright: ignore[reportUnknownMemberType] @@ -102,7 +102,7 @@ def test_matrix_scalar_division(): """Test matrix division by scalar""" m1 = cp.matrix([[6.0, 8.0], [12.0, 16.0]]) m2 = m1 / 2.0 - + assert m2[0] == pytest.approx((3.0, 4.0)) # pyright: ignore[reportUnknownMemberType] assert m2[1] == pytest.approx((6.0, 8.0)) # pyright: ignore[reportUnknownMemberType] @@ -112,7 +112,7 @@ def test_matrix_vector_multiplication(): m = cp.matrix([[1, 2, 3], [4, 5, 6]]) v = cp.vector([7, 8, 9]) result = m @ v - + assert isinstance(result, cp.vector) assert len(result.values) == 2 assert result.values[0] == 1*7 + 2*8 + 3*9 @@ -124,7 +124,7 @@ def test_matrix_matrix_multiplication(): m1 = cp.matrix([[1, 2], [3, 4]]) m2 = cp.matrix([[5, 6], [7, 8]]) result = m1 @ m2 - + assert isinstance(result, cp.matrix) assert result.rows == 2 assert result.cols == 2 @@ -138,7 +138,7 @@ def test_matrix_transpose(): """Test matrix transpose""" m = cp.matrix([[1, 2, 3], [4, 5, 6]]) mt = m.transpose() - + assert mt.rows == 3 assert mt.cols == 2 assert mt[0] == (1, 4) @@ -150,7 +150,7 @@ def test_matrix_transpose_property(): """Test matrix transpose using .T property""" m = cp.matrix([[1, 2, 3], [4, 5, 6]]) mt = m.T - + assert mt.rows == 3 assert mt.cols == 2 assert mt[0] == (1, 4) @@ -160,7 +160,7 @@ def test_matrix_row_access(): """Test getting a row as a vector""" m = cp.matrix([[1, 2, 3], [4, 5, 6]]) row0 = m.row(0) - + assert isinstance(row0, cp.vector) assert row0.values == (1, 2, 3) @@ -169,7 +169,7 @@ def test_matrix_col_access(): """Test getting a column as a vector""" m = cp.matrix([[1, 2, 3], [4, 5, 6]]) col1 = m.col(1) - + assert isinstance(col1, cp.vector) assert col1.values == (2, 5) @@ -178,7 +178,7 @@ def test_matrix_trace(): """Test matrix trace (sum of diagonal elements)""" m = cp.matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) trace = m.trace() - + assert trace == 1 + 5 + 9 @@ -194,7 +194,7 @@ def test_matrix_map(): """Test mapping a function over matrix elements""" m = cp.matrix([[1, 2], [3, 4]]) m_doubled = m.map(lambda x: x * 2) - + assert m_doubled[0] == (2, 4) assert m_doubled[1] == (6, 8) @@ -203,7 +203,7 @@ def test_matrix_homogenize(): """Test homogenizing matrix (converting to all variables)""" m = cp.matrix([[1, cp.variable(2)], [3, 4]]) m_homo = m.homogenize() - + for row in m_homo: for elem in row: assert isinstance(elem, cp.variable) @@ -212,7 +212,7 @@ def test_matrix_homogenize(): def test_identity_matrix(): """Test identity matrix creation""" m = cp.identity(3) - + assert m.rows == 3 assert m.cols == 3 assert m[0] == (1, 0, 0) @@ -223,7 +223,7 @@ def test_identity_matrix(): def test_zeros_matrix(): """Test zeros matrix creation""" m = cp.zeros(2, 3) - + assert m.rows == 2 assert m.cols == 3 assert m[0] == (0, 0, 0) @@ -233,7 +233,7 @@ def test_zeros_matrix(): def test_ones_matrix(): """Test ones matrix creation""" m = cp.ones(2, 3) - + assert m.rows == 2 assert m.cols == 3 assert m[0] == (1, 1, 1) @@ -244,7 +244,7 @@ def test_diagonal_matrix(): """Test diagonal matrix creation from vector""" v = cp.vector([1, 2, 3]) m = cp.diagonal(v) - + assert m.rows == 3 assert m.cols == 3 assert m[0] == (1, 0, 0) @@ -257,17 +257,17 @@ def test_matrix_with_variables_compiled(): m = cp.matrix([[cp.variable(1), 2], [3, cp.variable(4)]]) v = cp.vector([cp.variable(5), 6]) result = m @ v - + # result[0] = 1*5 + 2*6 = 17 # result[1] = 3*5 + 4*6 = 39 - + tg = cp.Target() tg.compile(result) tg.run() - + assert tg.read_value(result.values[0]) == pytest.approx(17) # pyright: ignore[reportUnknownMemberType] assert tg.read_value(result.values[1]) == pytest.approx(39) # pyright: ignore[reportUnknownMemberType] if __name__ == "__main__": - pytest.main([__file__, "-v"]) \ No newline at end of file + pytest.main([__file__, "-v"]) diff --git a/tests/test_ops_aarch64.py b/tests/test_ops_aarch64.py index e912edf..017def3 100644 --- a/tests/test_ops_aarch64.py +++ b/tests/test_ops_aarch64.py @@ -42,7 +42,7 @@ def check_for_qemu() -> bool: command = qemu_command + ['--version'] try: result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False) - except: + except Exception: return False return result.returncode == 0 @@ -128,7 +128,7 @@ def test_compile(): if not os.path.isfile('build/runner/coparun-aarch64'): warnings.warn("aarch64 runner not found, aarch64 test skipped!", UserWarning) return - + command = qemu_command + ['build/runner/coparun-aarch64', 'build/runner/test-arm64.copapy'] + ['build/runner/test-arm64.copapy.bin'] #try: result = run_command(command) diff --git a/tests/test_ops_armv7.py b/tests/test_ops_armv7.py index 1b53ca9..4941f9b 100644 --- a/tests/test_ops_armv7.py +++ b/tests/test_ops_armv7.py @@ -44,7 +44,7 @@ def check_for_qemu() -> bool: command = qemu_command + ['--version'] try: result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False) - except: + except Exception: return False return result.returncode == 0 @@ -130,7 +130,7 @@ def test_compile(): if not os.path.isfile('build/runner/coparun-armv7'): warnings.warn("armv7 runner not found, armv7 test skipped!", UserWarning) return - + command = qemu_command + ['build/runner/coparun-armv7', 'build/runner/test-armv7.copapy'] + ['build/runner/test-armv7.copapy.bin'] #try: result = run_command(command) diff --git a/tests/test_ops_x86.py b/tests/test_ops_x86.py index 29f2c96..069f341 100644 --- a/tests/test_ops_x86.py +++ b/tests/test_ops_x86.py @@ -1,4 +1,4 @@ -from copapy import NumLike, iif, variable, sin +from copapy import NumLike, iif, variable from copapy.backend import Write, compile_to_dag, add_read_command import subprocess from copapy import _binwrite @@ -70,16 +70,16 @@ def iiftests(c1: NumLike) -> list[NumLike]: @pytest.mark.runner def test_compile(): - t1 = cp.vector([10, 11, 12]) + cp.vector(cp.variable(v) for v in range(3)) - t2 = t1.sum() + #t1 = cp.vector([10, 11, 12]) + cp.vector(cp.variable(v) for v in range(3)) + #t2 = t1.sum() - t3 = cp.vector(cp.variable(1 / (v + 1)) for v in range(3)) - t4 = ((t3 * t1) * 2).sum() - t5 = ((t3 * t1) * 2).magnitude() + #t3 = cp.vector(cp.variable(1 / (v + 1)) for v in range(3)) + #t4 = ((t3 * t1) * 2).sum() + #t5 = ((t3 * t1) * 2).magnitude() c_i = variable(9) - c_f = variable(1.111) - c_b = variable(True) + #c_f = variable(1.111) + #c_b = variable(True) #ret_test = function1(c_i) + function1(c_f) + function2(c_i) + function2(c_f) + function3(c_i) + function4(c_i) + function5(c_b) + [c_i % 2, sin(c_f)] + iiftests(c_i) + iiftests(c_f) #ret_ref = function1(9) + function1(1.111) + function2(9) + function2(1.111) + function3(9) + function4(9) + function5(True) + [9 % 2, sin(1.111)] + iiftests(9) + iiftests(1.111) @@ -104,7 +104,7 @@ def test_compile(): ret_test = [cp.get_42(c_i)] ret_ref = [cp.get_42(9)] - out = [Write(r) for r in ret_test] + out = [Write(r) for r in ret_test] #ret_test += [c_i, v2] #ret_ref += [9, 4.44, -4.44] @@ -143,7 +143,7 @@ def test_compile(): try: result = run_command(command) except FileNotFoundError: - warnings.warn(f"Test skipped, executable not found.", UserWarning) + warnings.warn("Test skipped, executable not found.", UserWarning) return print('* Output from runner:\n--')