Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix minor typing errors in maths/ #8959

Merged
merged 10 commits into from
Aug 15, 2023
CaedenPH marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from PIL import Image


def rgb2gray(rgb: np.array) -> np.array:
def rgb2gray(rgb: np.ndarray) -> np.ndarray:
"""
Return gray image from rgb image
>>> rgb2gray(np.array([[[127, 255, 0]]]))
Expand All @@ -18,7 +18,7 @@ def rgb2gray(rgb: np.array) -> np.array:
return 0.2989 * r + 0.5870 * g + 0.1140 * b


def gray2binary(gray: np.array) -> np.array:
def gray2binary(gray: np.ndarray) -> np.ndarray:
"""
Return binary image from gray image
>>> gray2binary(np.array([[127, 255, 0]]))
Expand All @@ -35,7 +35,7 @@ def gray2binary(gray: np.array) -> np.array:
return (gray > 127) & (gray <= 255)


def erosion(image: np.array, kernel: np.array) -> np.array:
def erosion(image: np.ndarray, kernel: np.ndarray) -> np.ndarray:
"""
Return eroded image
>>> erosion(np.array([[True, True, False]]), np.array([[0, 1, 0]]))
Expand Down
4 changes: 2 additions & 2 deletions digital_image_processing/rotation/rotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ def get_rotation(
) -> np.ndarray:
"""
Get image rotation
:param img: np.array
:param img: np.ndarray
:param pt1: 3x2 list
:param pt2: 3x2 list
:param rows: columns image shape
:param cols: rows image shape
:return: np.array
:return: np.ndarray
"""
matrix = cv2.getAffineTransform(pt1, pt2)
return cv2.warpAffine(img, matrix, (rows, cols))
Expand Down
4 changes: 3 additions & 1 deletion maths/average_median.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ def median(nums: list) -> int | float:
Returns:
Median.
"""
sorted_list = sorted(nums)
# The sorted function returns list[SupportsRichComparisonT@sorted]
# which does not support `+`
sorted_list: list[int] = sorted(nums)
length = len(sorted_list)
mid_index = length >> 1
return (
Expand Down
2 changes: 1 addition & 1 deletion maths/euler_modified.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

def euler_modified(
ode_func: Callable, y0: float, x0: float, step_size: float, x_end: float
) -> np.array:
) -> np.ndarray:
"""
Calculate solution at each step to an ODE using Euler's Modified Method
The Euler Method is straightforward to implement, but can't give accurate solutions.
Expand Down
4 changes: 2 additions & 2 deletions maths/gaussian_error_linear_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import numpy as np


def sigmoid(vector: np.array) -> np.array:
def sigmoid(vector: np.ndarray) -> np.ndarray:
"""
Mathematical function sigmoid takes a vector x of K real numbers as input and
returns 1/ (1 + e^-x).
Expand All @@ -25,7 +25,7 @@ def sigmoid(vector: np.array) -> np.array:
return 1 / (1 + np.exp(-vector))


def gaussian_error_linear_unit(vector: np.array) -> np.array:
def gaussian_error_linear_unit(vector: np.ndarray) -> np.ndarray:
"""
Implements the Gaussian Error Linear Unit (GELU) function
Expand Down
40 changes: 28 additions & 12 deletions maths/jaccard_similarity.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
"""


def jaccard_similarity(set_a, set_b, alternative_union=False):
def jaccard_similarity(
set_a: set[str] | list[str] | tuple[str],
set_b: set[str] | list[str] | tuple[str],
alternative_union=False,
):
"""
Finds the jaccard similarity between two sets.
Essentially, its intersection over union.
Expand Down Expand Up @@ -48,30 +52,42 @@ def jaccard_similarity(set_a, set_b, alternative_union=False):
>>> set_b = ('c', 'd', 'e', 'f', 'h', 'i')
>>> jaccard_similarity(set_a, set_b)
0.375

>>> set_a = ('c', 'd', 'e', 'f', 'h', 'i')
>>> set_b = ['a', 'b', 'c', 'd', 'e']
>>> jaccard_similarity(set_a, set_b)
0.375

>>> set_a = {'a', 'b'}
>>> set_b = ['c', 'd']
>>> jaccard_similarity(set_a, set_b)
Traceback (most recent call last):
...
ValueError: Set a and b must either both be sets or be either a list or a tuple.
"""

if isinstance(set_a, set) and isinstance(set_b, set):
intersection = len(set_a.intersection(set_b))
intersection_length = len(set_a.intersection(set_b))

if alternative_union:
union = len(set_a) + len(set_b)
union_length = len(set_a) + len(set_b)
else:
union = len(set_a.union(set_b))
union_length = len(set_a.union(set_b))

return intersection / union
return intersection_length / union_length

if isinstance(set_a, (list, tuple)) and isinstance(set_b, (list, tuple)):
elif isinstance(set_a, (list, tuple)) and isinstance(set_b, (list, tuple)):
intersection = [element for element in set_a if element in set_b]

if alternative_union:
union = len(set_a) + len(set_b)
return len(intersection) / union
return len(intersection) / len(set_a) + len(set_b)
CaedenPH marked this conversation as resolved.
Show resolved Hide resolved
else:
union = set_a + [element for element in set_b if element not in set_a]
# Cast set_a to list because tuples cannot be mutated
union = list(set_a) + [element for element in set_b if element not in set_a]
return len(intersection) / len(union)

return len(intersection) / len(union)
return None
raise ValueError(
"Set a and b must either both be sets or be either a list or a tuple."
)


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion maths/newton_raphson.py
CaedenPH marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def newton_raphson(f, x0=0, maxiter=100, step=0.0001, maxerror=1e-6, logsteps=Fa
if logsteps:
# If logstep is true, then log intermediate steps
return a, error, steps
return a, error
return a, error, []


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion maths/qr_decomposition.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np


def qr_householder(a):
def qr_householder(a: np.ndarray):
"""Return a QR-decomposition of the matrix A using Householder reflection.
The QR-decomposition decomposes the matrix A of shape (m, n) into an
Expand Down
2 changes: 1 addition & 1 deletion maths/sigmoid.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import numpy as np


def sigmoid(vector: np.array) -> np.array:
def sigmoid(vector: np.ndarray) -> np.ndarray:
"""
Implements the sigmoid function
Expand Down
4 changes: 2 additions & 2 deletions maths/tanh.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
import numpy as np


def tangent_hyperbolic(vector: np.array) -> np.array:
def tangent_hyperbolic(vector: np.ndarray) -> np.ndarray:
"""
Implements the tanh function
Parameters:
vector: np.array
vector: np.ndarray
Returns:
tanh (np.array): The input numpy array after applying tanh.
Expand Down