[PcbDraw] Removed numpy as dependency

- 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
This commit is contained in:
Salvador E. Tropea 2022-10-18 08:56:29 -03:00
parent a6f4173226
commit 749618782b
4 changed files with 39 additions and 14 deletions

View File

@ -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

29
kibot/PcbDraw/np.py Normal file
View File

@ -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]

View File

@ -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__)

View File

@ -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
# """