Skip to content

Upgrade to Python 3.13 #11588

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

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: 3.12
python-version: 3.13
allow-prereleases: true
- uses: actions/cache@v4
with:
Expand All @@ -26,6 +26,10 @@ jobs:
# TODO: #8818 Re-enable quantum tests
run: pytest
--ignore=quantum/q_fourier_transform.py
--ignore=computer_vision/cnn_classification.py
--ignore=dynamic_programming/k_means_clustering_tensorflow.py
--ignore=machine_learning/lstm/lstm_prediction.py
--ignore=neural_network/input_data.py
--ignore=project_euler/
--ignore=scripts/validate_solutions.py
--cov-report=term-missing:skip-covered
Expand Down
1 change: 0 additions & 1 deletion DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -1343,7 +1343,6 @@
* [Get Ip Geolocation](web_programming/get_ip_geolocation.py)
* [Get Top Billionaires](web_programming/get_top_billionaires.py)
* [Get Top Hn Posts](web_programming/get_top_hn_posts.py)
* [Get User Tweets](web_programming/get_user_tweets.py)
* [Giphy](web_programming/giphy.py)
* [Instagram Crawler](web_programming/instagram_crawler.py)
* [Instagram Pic](web_programming/instagram_pic.py)
Expand Down
8 changes: 4 additions & 4 deletions computer_vision/haralick_descriptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def root_mean_square_error(original: np.ndarray, reference: np.ndarray) -> float
>>> root_mean_square_error(np.array([1, 2, 3]), np.array([6, 4, 2]))
3.1622776601683795
"""
return np.sqrt(((original - reference) ** 2).mean())
return float(np.sqrt(((original - reference) ** 2).mean()))


def normalize_image(
Expand Down Expand Up @@ -273,7 +273,7 @@ def haralick_descriptors(matrix: np.ndarray) -> list[float]:
>>> morphological = opening_filter(binary)
>>> mask_1 = binary_mask(gray, morphological)[0]
>>> concurrency = matrix_concurrency(mask_1, (0, 1))
>>> haralick_descriptors(concurrency)
>>> [float(f) for f in haralick_descriptors(concurrency)]
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
"""
# Function np.indices could be used for bigger input types,
Expand Down Expand Up @@ -335,7 +335,7 @@ def get_descriptors(
return np.concatenate(descriptors, axis=None)


def euclidean(point_1: np.ndarray, point_2: np.ndarray) -> np.float32:
def euclidean(point_1: np.ndarray, point_2: np.ndarray) -> float:
"""
Simple method for calculating the euclidean distance between two points,
with type np.ndarray.
Expand All @@ -346,7 +346,7 @@ def euclidean(point_1: np.ndarray, point_2: np.ndarray) -> np.float32:
>>> euclidean(a, b)
3.3166247903554
"""
return np.sqrt(np.sum(np.square(point_1 - point_2)))
return float(np.sqrt(np.sum(np.square(point_1 - point_2))))


def get_distances(descriptors: np.ndarray, base: int) -> list[tuple[int, float]]:
Expand Down
6 changes: 3 additions & 3 deletions data_structures/heap/binomial_heap.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class BinomialHeap:
30

Deleting - delete() test
>>> [first_heap.delete_min() for _ in range(20)]
>>> [int(first_heap.delete_min()) for _ in range(20)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

Create a new Heap
Expand Down Expand Up @@ -118,7 +118,7 @@ class BinomialHeap:
values in merged heap; (merge is inplace)
>>> results = []
>>> while not first_heap.is_empty():
... results.append(first_heap.delete_min())
... results.append(int(first_heap.delete_min()))
>>> results
[17, 20, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 31, 34]
"""
Expand Down Expand Up @@ -354,7 +354,7 @@ def delete_min(self):
# Merge heaps
self.merge_heaps(new_heap)

return min_value
return int(min_value)

def pre_order(self):
"""
Expand Down
6 changes: 3 additions & 3 deletions electronics/circular_convolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def circular_convolution(self) -> list[float]:
Usage:
>>> convolution = CircularConvolution()
>>> convolution.circular_convolution()
[10, 10, 6, 14]
[10.0, 10.0, 6.0, 14.0]

>>> convolution.first_signal = [0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6]
>>> convolution.second_signal = [0.1, 0.3, 0.5, 0.7, 0.9, 1.1, 1.3, 1.5]
Expand All @@ -54,7 +54,7 @@ def circular_convolution(self) -> list[float]:
>>> convolution.first_signal = [1, -1, 2, 3, -1]
>>> convolution.second_signal = [1, 2, 3]
>>> convolution.circular_convolution()
[8, -2, 3, 4, 11]
[8.0, -2.0, 3.0, 4.0, 11.0]

"""

Expand Down Expand Up @@ -91,7 +91,7 @@ def circular_convolution(self) -> list[float]:
final_signal = np.matmul(np.transpose(matrix), np.transpose(self.first_signal))

# rounding-off to two decimal places
return [round(i, 2) for i in final_signal]
return [float(round(i, 2)) for i in final_signal]


if __name__ == "__main__":
Expand Down
18 changes: 9 additions & 9 deletions fractals/julia_sets.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@
def eval_exponential(c_parameter: complex, z_values: np.ndarray) -> np.ndarray:
"""
Evaluate $e^z + c$.
>>> eval_exponential(0, 0)
>>> float(eval_exponential(0, 0))
1.0
>>> abs(eval_exponential(1, np.pi*1.j)) < 1e-15
>>> bool(abs(eval_exponential(1, np.pi*1.j)) < 1e-15)
True
>>> abs(eval_exponential(1.j, 0)-1-1.j) < 1e-15
>>> bool(abs(eval_exponential(1.j, 0)-1-1.j) < 1e-15)
True
"""
return np.exp(z_values) + c_parameter
Expand Down Expand Up @@ -98,20 +98,20 @@ def iterate_function(

>>> iterate_function(eval_quadratic_polynomial, 0, 3, np.array([0,1,2])).shape
(3,)
>>> np.round(iterate_function(eval_quadratic_polynomial,
>>> complex(np.round(iterate_function(eval_quadratic_polynomial,
... 0,
... 3,
... np.array([0,1,2]))[0])
... np.array([0,1,2]))[0]))
0j
>>> np.round(iterate_function(eval_quadratic_polynomial,
>>> complex(np.round(iterate_function(eval_quadratic_polynomial,
... 0,
... 3,
... np.array([0,1,2]))[1])
... np.array([0,1,2]))[1]))
(1+0j)
>>> np.round(iterate_function(eval_quadratic_polynomial,
>>> complex(np.round(iterate_function(eval_quadratic_polynomial,
... 0,
... 3,
... np.array([0,1,2]))[2])
... np.array([0,1,2]))[2]))
(256+0j)
"""

Expand Down
8 changes: 4 additions & 4 deletions graphics/bezier_curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ def basis_function(self, t: float) -> list[float]:
returns the x, y values of basis function at time t

>>> curve = BezierCurve([(1,1), (1,2)])
>>> curve.basis_function(0)
>>> [float(x) for x in curve.basis_function(0)]
[1.0, 0.0]
>>> curve.basis_function(1)
>>> [float(x) for x in curve.basis_function(1)]
[0.0, 1.0]
"""
assert 0 <= t <= 1, "Time t must be between 0 and 1."
Expand All @@ -55,9 +55,9 @@ def bezier_curve_function(self, t: float) -> tuple[float, float]:
The last point in the curve is when t = 1.

>>> curve = BezierCurve([(1,1), (1,2)])
>>> curve.bezier_curve_function(0)
>>> tuple(float(x) for x in curve.bezier_curve_function(0))
(1.0, 1.0)
>>> curve.bezier_curve_function(1)
>>> tuple(float(x) for x in curve.bezier_curve_function(1))
(1.0, 2.0)
"""

Expand Down
2 changes: 1 addition & 1 deletion graphs/dijkstra_binary_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def dijkstra(
x, y = predecessors[x, y]
path.append(source) # add the source manually
path.reverse()
return matrix[destination], path
return float(matrix[destination]), path

for i in range(len(dx)):
nx, ny = x + dx[i], y + dy[i]
Expand Down
2 changes: 1 addition & 1 deletion linear_algebra/src/power_iteration.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def power_iteration(
if is_complex:
lambda_ = np.real(lambda_)

return lambda_, vector
return float(lambda_), vector


def test_power_iteration() -> None:
Expand Down
32 changes: 16 additions & 16 deletions linear_programming/simplex.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ def generate_col_titles(self) -> list[str]:

def find_pivot(self) -> tuple[Any, Any]:
"""Finds the pivot row and column.
>>> Tableau(np.array([[-2,1,0,0,0], [3,1,1,0,6], [1,2,0,1,7.]]),
... 2, 0).find_pivot()
>>> tuple(int(x) for x in Tableau(np.array([[-2,1,0,0,0], [3,1,1,0,6],
... [1,2,0,1,7.]]), 2, 0).find_pivot())
(1, 0)
"""
objective = self.objectives[-1]
Expand Down Expand Up @@ -215,56 +215,56 @@ def run_simplex(self) -> dict[Any, Any]:
Max: x1 + x2
ST: x1 + 3x2 <= 4
3x1 + x2 <= 4
>>> Tableau(np.array([[-1,-1,0,0,0],[1,3,1,0,4],[3,1,0,1,4.]]),
... 2, 0).run_simplex()
>>> {key: float(value) for key, value in Tableau(np.array([[-1,-1,0,0,0],
... [1,3,1,0,4],[3,1,0,1,4.]]), 2, 0).run_simplex().items()}
{'P': 2.0, 'x1': 1.0, 'x2': 1.0}

# Standard linear program with 3 variables:
Max: 3x1 + x2 + 3x3
ST: 2x1 + x2 + x3 ≤ 2
x1 + 2x2 + 3x3 ≤ 5
2x1 + 2x2 + x3 ≤ 6
>>> Tableau(np.array([
>>> {key: float(value) for key, value in Tableau(np.array([
... [-3,-1,-3,0,0,0,0],
... [2,1,1,1,0,0,2],
... [1,2,3,0,1,0,5],
... [2,2,1,0,0,1,6.]
... ]),3,0).run_simplex() # doctest: +ELLIPSIS
... ]),3,0).run_simplex().items()} # doctest: +ELLIPSIS
{'P': 5.4, 'x1': 0.199..., 'x3': 1.6}


# Optimal tableau input:
>>> Tableau(np.array([
>>> {key: float(value) for key, value in Tableau(np.array([
... [0, 0, 0.25, 0.25, 2],
... [0, 1, 0.375, -0.125, 1],
... [1, 0, -0.125, 0.375, 1]
... ]), 2, 0).run_simplex()
... ]), 2, 0).run_simplex().items()}
{'P': 2.0, 'x1': 1.0, 'x2': 1.0}

# Non-standard: >= constraints
Max: 2x1 + 3x2 + x3
ST: x1 + x2 + x3 <= 40
2x1 + x2 - x3 >= 10
- x2 + x3 >= 10
>>> Tableau(np.array([
>>> {key: float(value) for key, value in Tableau(np.array([
... [2, 0, 0, 0, -1, -1, 0, 0, 20],
... [-2, -3, -1, 0, 0, 0, 0, 0, 0],
... [1, 1, 1, 1, 0, 0, 0, 0, 40],
... [2, 1, -1, 0, -1, 0, 1, 0, 10],
... [0, -1, 1, 0, 0, -1, 0, 1, 10.]
... ]), 3, 2).run_simplex()
... ]), 3, 2).run_simplex().items()}
{'P': 70.0, 'x1': 10.0, 'x2': 10.0, 'x3': 20.0}

# Non standard: minimisation and equalities
Min: x1 + x2
ST: 2x1 + x2 = 12
6x1 + 5x2 = 40
>>> Tableau(np.array([
>>> {key: float(value) for key, value in Tableau(np.array([
... [8, 6, 0, 0, 52],
... [1, 1, 0, 0, 0],
... [2, 1, 1, 0, 12],
... [6, 5, 0, 1, 40.],
... ]), 2, 2).run_simplex()
... ]), 2, 2).run_simplex().items()}
{'P': 7.0, 'x1': 5.0, 'x2': 2.0}


Expand All @@ -275,15 +275,15 @@ def run_simplex(self) -> dict[Any, Any]:
2x1 + 4x2 <= 48
x1 + x2 >= 10
x1 >= 2
>>> Tableau(np.array([
>>> {key: float(value) for key, value in Tableau(np.array([
... [2, 1, 0, 0, 0, -1, -1, 0, 0, 12.0],
... [-8, -6, 0, 0, 0, 0, 0, 0, 0, 0.0],
... [1, 3, 1, 0, 0, 0, 0, 0, 0, 33.0],
... [4, 2, 0, 1, 0, 0, 0, 0, 0, 60.0],
... [2, 4, 0, 0, 1, 0, 0, 0, 0, 48.0],
... [1, 1, 0, 0, 0, -1, 0, 1, 0, 10.0],
... [1, 0, 0, 0, 0, 0, -1, 0, 1, 2.0]
... ]), 2, 2).run_simplex() # doctest: +ELLIPSIS
... ]), 2, 2).run_simplex().items()} # doctest: +ELLIPSIS
{'P': 132.0, 'x1': 12.000... 'x2': 5.999...}
"""
# Stop simplex algorithm from cycling.
Expand All @@ -307,11 +307,11 @@ def run_simplex(self) -> dict[Any, Any]:
def interpret_tableau(self) -> dict[str, float]:
"""Given the final tableau, add the corresponding values of the basic
decision variables to the `output_dict`
>>> Tableau(np.array([
>>> {key: float(value) for key, value in Tableau(np.array([
... [0,0,0.875,0.375,5],
... [0,1,0.375,-0.125,1],
... [1,0,-0.125,0.375,1]
... ]),2, 0).interpret_tableau()
... ]),2, 0).interpret_tableau().items()}
{'P': 5.0, 'x1': 1.0, 'x2': 1.0}
"""
# P = RHS of final tableau
Expand Down
8 changes: 4 additions & 4 deletions machine_learning/decision_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ def mean_squared_error(self, labels, prediction):
>>> tester = DecisionTree()
>>> test_labels = np.array([1,2,3,4,5,6,7,8,9,10])
>>> test_prediction = float(6)
>>> tester.mean_squared_error(test_labels, test_prediction) == (
>>> bool(tester.mean_squared_error(test_labels, test_prediction) == (
... TestDecisionTree.helper_mean_squared_error_test(test_labels,
... test_prediction))
... test_prediction)))
True
>>> test_labels = np.array([1,2,3])
>>> test_prediction = float(2)
>>> tester.mean_squared_error(test_labels, test_prediction) == (
>>> bool(tester.mean_squared_error(test_labels, test_prediction) == (
... TestDecisionTree.helper_mean_squared_error_test(test_labels,
... test_prediction))
... test_prediction)))
True
"""
if labels.ndim != 1:
Expand Down
8 changes: 4 additions & 4 deletions machine_learning/forecasting/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def linear_regression_prediction(
input : training data (date, total_user, total_event) in list of float
output : list of total user prediction in float
>>> n = linear_regression_prediction([2,3,4,5], [5,3,4,6], [3,1,2,4], [2,1], [2,2])
>>> abs(n - 5.0) < 1e-6 # Checking precision because of floating point errors
>>> bool(abs(n - 5.0) < 1e-6) # Checking precision because of floating point errors
True
"""
x = np.array([[1, item, train_mtch[i]] for i, item in enumerate(train_dt)])
Expand Down Expand Up @@ -56,7 +56,7 @@ def sarimax_predictor(train_user: list, train_match: list, test_match: list) ->
)
model_fit = model.fit(disp=False, maxiter=600, method="nm")
result = model_fit.predict(1, len(test_match), exog=[test_match])
return result[0]
return float(result[0])


def support_vector_regressor(x_train: list, x_test: list, train_user: list) -> float:
Expand All @@ -75,7 +75,7 @@ def support_vector_regressor(x_train: list, x_test: list, train_user: list) -> f
regressor = SVR(kernel="rbf", C=1, gamma=0.1, epsilon=0.1)
regressor.fit(x_train, train_user)
y_pred = regressor.predict(x_test)
return y_pred[0]
return float(y_pred[0])


def interquartile_range_checker(train_user: list) -> float:
Expand All @@ -92,7 +92,7 @@ def interquartile_range_checker(train_user: list) -> float:
q3 = np.percentile(train_user, 75)
iqr = q3 - q1
low_lim = q1 - (iqr * 0.1)
return low_lim
return float(low_lim)


def data_safety_checker(list_vote: list, actual_result: float) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion machine_learning/k_nearest_neighbours.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def _euclidean_distance(a: np.ndarray[float], b: np.ndarray[float]) -> float:
>>> KNN._euclidean_distance(np.array([1, 2, 3]), np.array([1, 8, 11]))
10.0
"""
return np.linalg.norm(a - b)
return float(np.linalg.norm(a - b))

def classify(self, pred_point: np.ndarray[float], k: int = 5) -> str:
"""
Expand Down
4 changes: 2 additions & 2 deletions machine_learning/logistic_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def sigmoid_function(z: float | np.ndarray) -> float | np.ndarray:
@returns: returns value in the range 0 to 1

Examples:
>>> sigmoid_function(4)
>>> float(sigmoid_function(4))
0.9820137900379085
>>> sigmoid_function(np.array([-3, 3]))
array([0.04742587, 0.95257413])
Expand Down Expand Up @@ -100,7 +100,7 @@ def cost_function(h: np.ndarray, y: np.ndarray) -> float:
References:
- https://en.wikipedia.org/wiki/Logistic_regression
"""
return (-y * np.log(h) - (1 - y) * np.log(1 - h)).mean()
return float((-y * np.log(h) - (1 - y) * np.log(1 - h)).mean())


def log_likelihood(x, y, weights):
Expand Down
Loading