diff --git a/kibot/PcbDraw/README.md b/kibot/PcbDraw/README.md index b9c8bc50..ab23da08 100644 --- a/kibot/PcbDraw/README.md +++ b/kibot/PcbDraw/README.md @@ -175,3 +175,10 @@ index f8990722..17f90185 100644 - Changed `pcbnewTransition` to be locally included. - Just 2.8 kiB no worth the effort of pulling a dependency + +- Replaced `numpy` by a very simple code + - Currently svgpathtool is disabled, it really needs numpy + - `numpy` is used to: + - Multiply matrices (1 line code) + - Find the index of the smaller element (1 line code) + - I added a replacemt for the `array` function, it just makes all matrix elements float diff --git a/kibot/PcbDraw/np.py b/kibot/PcbDraw/np.py new file mode 100644 index 00000000..eeaf6ad1 --- /dev/null +++ b/kibot/PcbDraw/np.py @@ -0,0 +1,29 @@ +# Author: Salvador E. Tropea +# License: MIT +# numpy replacement for PcbDraw +from operator import itemgetter + +# A value that is not None +float32 = 1 + +def argmin(vector): + """ Index of the minimum element in a vector. + See https://stackoverflow.com/questions/13300962/python-find-index-of-minimum-item-in-list-of-floats """ + return min(enumerate(vector), key=itemgetter(1))[0] + + +def array(data, dtype=None): + """ Just make all elements float, or let unchanged """ + if dtype: + for r in data: + for c, val in enumerate(r): + r[c] = float(val) + return data + + +def matmul(A, B): + """ Matrix multiplication. + See: https://geekflare.com/multiply-matrices-in-python/ """ + # Ensure the number of cols in A is the same as the number of rows in B + # assert len(A[0]) == len(B) + return [[sum(a*b for a, b in zip(A_row, B_col)) for B_col in zip(*B)] for A_row in A] diff --git a/kibot/PcbDraw/plot.py b/kibot/PcbDraw/plot.py index 2ab20fac..5bb5aee1 100644 --- a/kibot/PcbDraw/plot.py +++ b/kibot/PcbDraw/plot.py @@ -14,19 +14,7 @@ from dataclasses import dataclass, field from decimal import Decimal from typing import Callable, Dict, List, Optional, Tuple, TypeVar, Union, Any -import numpy as np -# We import the typing under try-catch to allow runtime for systems that have -# old Numpy that don't feature the numpy.typing module, but we want to preserve -# type checking. -try: - import numpy.typing - - # Note that we also have to define all the numpy-related types under the - # try-catch values as annotations can be ignored, but value can't - Matrix = np.typing.NDArray[np.float32] - -except ImportError: - pass +from . import np from .unit import read_resistance # import svgpathtools # type: ignore from lxml import etree, objectify # type: ignore @@ -36,6 +24,7 @@ T = TypeVar("T") Numeric = Union[int, float] Point = Tuple[Numeric, Numeric] Box = Tuple[Numeric, Numeric, Numeric, Numeric] +Matrix = List[List[float]] PKG_BASE = os.path.dirname(__file__) diff --git a/kibot/out_pcbdraw.py b/kibot/out_pcbdraw.py index e1417e9f..179354ae 100644 --- a/kibot/out_pcbdraw.py +++ b/kibot/out_pcbdraw.py @@ -3,7 +3,7 @@ # Copyright (c) 2020-2022 Instituto Nacional de TecnologĂ­a Industrial # License: GPL-3.0 # Project: KiBot (formerly KiPlot) -# TODO: PIL dependency? numpy? +# TODO: PIL dependency? # TODO: Package resources # TODO: replace unit.py # """