From bf5a1b1cd0707182b506c2f05bc6b97751299ffc Mon Sep 17 00:00:00 2001 From: pheithar <avalverdemahou@gmail.com> Date: Fri, 31 Jan 2025 11:50:56 +0100 Subject: [PATCH] Header fix --- .../02450Toolbox_Python/Scripts/ex2_2_1.py | 72 ++++++++++--------- .../02450Toolbox_Python/Scripts/ex2_2_2.py | 2 +- .../02450Toolbox_Python/Scripts/ex3_1_4.py | 2 +- .../02450Toolbox_Python/Scripts/ex3_1_5.py | 2 +- .../02450Toolbox_Python/Scripts/ex4_1_1.py | 2 - 5 files changed, 43 insertions(+), 37 deletions(-) diff --git a/exercises/02450Toolbox_Python/Scripts/ex2_2_1.py b/exercises/02450Toolbox_Python/Scripts/ex2_2_1.py index 2958613..f95d627 100644 --- a/exercises/02450Toolbox_Python/Scripts/ex2_2_1.py +++ b/exercises/02450Toolbox_Python/Scripts/ex2_2_1.py @@ -1,4 +1,4 @@ -# exercise 3.3.1 +# exercise 2.2.1 import importlib_resources import matplotlib.pyplot as plt @@ -18,60 +18,68 @@ similarity_measure = "SMC" # Load Matlab data file to python dict structure X = loadmat(filename)["X"] # You can also try the CBCL faces dataset (remember to change 'transpose') -#X = loadmat('../Data/wildfaces_grayscale.mat')['X'] +# X = loadmat('../Data/wildfaces_grayscale.mat')['X'] N, M = X.shape -transpose = False # should the plotted images be transposed? +transpose = False # should the plotted images be transposed? -# Search the face database for similar faces +# Search the face database for similar faces # Index of all other images than i -noti = list(range(0,i)) + list(range(i+1,N)) +noti = list(range(0, i)) + list(range(i + 1, N)) # Compute similarity between image i and all others -sim = similarity(X[i,:], X[noti,:], similarity_measure) +sim = similarity(X[i, :], X[noti, :], similarity_measure) sim = sim.tolist()[0] # Tuples of sorted similarities and their indices -sim_to_index = sorted(zip(sim,noti)) +sim_to_index = sorted(zip(sim, noti)) # Visualize query image and 5 most/least similar images -plt.figure(figsize=(12,8)) -plt.subplot(3,1,1) +plt.figure(figsize=(12, 8)) +plt.subplot(3, 1, 1) img_hw = int(np.sqrt(len(X[0]))) -img = np.reshape(X[i], (img_hw,img_hw)) -if transpose: img = img.T +img = np.reshape(X[i], (img_hw, img_hw)) +if transpose: + img = img.T plt.imshow(img, cmap=plt.cm.gray) -plt.xticks([]); plt.yticks([]) -plt.title('Query image') -plt.ylabel('image #{0}'.format(i)) +plt.xticks([]) +plt.yticks([]) +plt.title("Query image") +plt.ylabel("image #{0}".format(i)) for ms in range(5): # 5 most similar images found - plt.subplot(3,5,6+ms) - im_id = sim_to_index[-ms-1][1] - im_sim = sim_to_index[-ms-1][0] - img = np.reshape(X[im_id],(img_hw,img_hw)) - if transpose: img = img.T + plt.subplot(3, 5, 6 + ms) + im_id = sim_to_index[-ms - 1][1] + im_sim = sim_to_index[-ms - 1][0] + img = np.reshape(X[im_id], (img_hw, img_hw)) + if transpose: + img = img.T plt.imshow(img, cmap=plt.cm.gray) - plt.xlabel('sim={0:.3f}'.format(im_sim)) - plt.ylabel('image #{0}'.format(im_id)) - plt.xticks([]); plt.yticks([]) - if ms==2: plt.title('Most similar images') + plt.xlabel("sim={0:.3f}".format(im_sim)) + plt.ylabel("image #{0}".format(im_id)) + plt.xticks([]) + plt.yticks([]) + if ms == 2: + plt.title("Most similar images") # 5 least similar images found - plt.subplot(3,5,11+ms) + plt.subplot(3, 5, 11 + ms) im_id = sim_to_index[ms][1] im_sim = sim_to_index[ms][0] - img = np.reshape(X[im_id],(img_hw,img_hw)) - if transpose: img = img.T + img = np.reshape(X[im_id], (img_hw, img_hw)) + if transpose: + img = img.T plt.imshow(img, cmap=plt.cm.gray) - plt.xlabel('sim={0:.3f}'.format(im_sim)) - plt.ylabel('image #{0}'.format(im_id)) - plt.xticks([]); plt.yticks([]) - if ms==2: plt.title('Least similar images') - + plt.xlabel("sim={0:.3f}".format(im_sim)) + plt.ylabel("image #{0}".format(im_id)) + plt.xticks([]) + plt.yticks([]) + if ms == 2: + plt.title("Least similar images") + plt.show() -print('Ran Exercise 2.2.1') +print("Ran Exercise 2.2.1") diff --git a/exercises/02450Toolbox_Python/Scripts/ex2_2_2.py b/exercises/02450Toolbox_Python/Scripts/ex2_2_2.py index 6d1590f..3f249ca 100644 --- a/exercises/02450Toolbox_Python/Scripts/ex2_2_2.py +++ b/exercises/02450Toolbox_Python/Scripts/ex2_2_2.py @@ -1,4 +1,4 @@ -# exercise 3.2.2 +# exercise 2.2.2 import numpy as np diff --git a/exercises/02450Toolbox_Python/Scripts/ex3_1_4.py b/exercises/02450Toolbox_Python/Scripts/ex3_1_4.py index 11fc4ab..6c6e8c9 100644 --- a/exercises/02450Toolbox_Python/Scripts/ex3_1_4.py +++ b/exercises/02450Toolbox_Python/Scripts/ex3_1_4.py @@ -1,5 +1,5 @@ # exercise 3.1.4 -# (requires data structures from ex. 2.2.1 and 2.2.3) +# (requires data structures from ex. 3.1.1) from ex3_1_1 import * from matplotlib.pyplot import figure, legend, plot, show, title, xlabel, ylabel from scipy.linalg import svd diff --git a/exercises/02450Toolbox_Python/Scripts/ex3_1_5.py b/exercises/02450Toolbox_Python/Scripts/ex3_1_5.py index 395b113..19a4445 100644 --- a/exercises/02450Toolbox_Python/Scripts/ex3_1_5.py +++ b/exercises/02450Toolbox_Python/Scripts/ex3_1_5.py @@ -1,6 +1,6 @@ # exercise 3.1.5 -# (requires data structures from ex. 2.2.1) +# (requires data structures from ex. 3.1.1) import matplotlib.pyplot as plt from ex3_1_1 import * from scipy.linalg import svd diff --git a/exercises/02450Toolbox_Python/Scripts/ex4_1_1.py b/exercises/02450Toolbox_Python/Scripts/ex4_1_1.py index f52139d..9464534 100644 --- a/exercises/02450Toolbox_Python/Scripts/ex4_1_1.py +++ b/exercises/02450Toolbox_Python/Scripts/ex4_1_1.py @@ -28,5 +28,3 @@ plot(X, ".") subplot(1, 3, 3) hist(X, bins=nbins) show() - - -- GitLab