<jupyter_start><jupyter_text>Task 1 > Image Compression<jupyter_code>Nx = img.shape[0]
Ny = img.shape[1]
N = Nx * Ny

# vec(F)
vec_F = img.flatten()

# two DCT matrices
D = construct_DCT_Mat(Nx, Ny)
D_inv = construct_iDCT_Mat(Nx, Ny)

# vec(DCT(F))
f_DCT = np.dot(D, vec_F)

plt.plot(f_DCT)
plt.title("DCT coefficients")
# iterate each wanted percentage of DCT coeffs
# and reconstrct image
percentage = [0.05, 0.1, 0.2, 0.4]
idx = np.abs(f_DCT).argsort()
idx = idx[::-1]
fig, ax = plt.subplots(1, len(percentage), figsize=(15, 15))
for ii, i in enumerate(percentage):
    f_thres = [f_DCT[j] if j in idx[: int(i * N)] else 0 for j in range(N)]
    result = np.dot(D_inv, f_thres).reshape(Nx, Ny)
    ax[ii].imshow(result, cmap="gray")
    ax[ii].set_title(f" with {100*i:0.0f} % DCT coef")<jupyter_output><empty_output><jupyter_text>Task 2>Compressed Image Recovery<jupyter_code>M = [0.2, 0.4, 0.6]
for i in M:
    fig, ax = plt.subplots(1, 3, figsize=(10, 4))
    for j in range(3):
        rng = np.random.default_rng(seed=j + 1)
        rand_ = rng.choice(np.eye(N), size=int(i * N), replace=False)
        B = rand_
        y = np.dot(B, vec_F)

        A = np.dot(B, D_inv)

        x_l1 = cvx.Variable(N)

        tol = 10**-2

        objective_l1 = cvx.Minimize(cvx.norm(x_l1, 1))
        constraints_l1 = [-tol <= A @ x_l1 - y, A @ x_l1 - y <= tol]
        prob_l1 = cvx.Problem(objective_l1, constraints_l1)

        prob_l1.solve(verbose=True, reltol=1e-2)
        ax[j].imshow(np.dot(D_inv, x_l1.value).reshape(Nx, Ny), cmap="gray")
    fig.suptitle(f"M = {i:0.1f}*N")<jupyter_output><empty_output><jupyter_text>Task 3>A Mysterious Image<jupyter_code>y = np.load(
    "/content/drive/MyDrive/Colab Notebooks/amath 582 lecture notes/reports /5/y.npy"
)
B = np.load(
    "/content/drive/MyDrive/Colab Notebooks/amath 582 lecture notes/reports /5/B.npy"
)


Nx, Ny = 50, 50
N = Nx * Ny

D_inv = construct_iDCT_Mat(Nx, Ny)

A = np.dot(B, D_inv)

x_l1 = cvx.Variable(N)

tol = 10**-2

objective_l1 = cvx.Minimize(cvx.norm(x_l1, 1))
constraints_l1 = [-tol <= A @ x_l1 - y, A @ x_l1 - y <= tol]
prob_l1 = cvx.Problem(objective_l1, constraints_l1)

prob_l1.solve(verbose=False, solver="CVXOPT", max_iter=1000, reltol=1e-2, featol=1e-2)


plt.imshow(np.dot(D_inv, x_l1.value).reshape(Nx, Ny), cmap="gray")
#!pip install black[jupyter]
!black /content/drive/MyDrive/Colab\ Notebooks/amath\ 582\ lecture\ notes/reports\ /5/hw5.ipynb<jupyter_output>[1mAll done! ✨ 🍰 ✨[0m
[34m1 file [0mleft unchanged.
{'fraction_non_alphanumeric': 0.1692614770459082, 'fraction_numerical': 0.04151696606786427, 'mean_word_length': 3.6928838951310863, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '68596', 'license': 'MIT', 'n_tokens_mistral': 1220, 'n_tokens_neox': 1148, 'n_words': 224, 'path': 'reports/5/HW5.ipynb', 'repo_name': 'raph651/Amath-582-Data-Analysis', 'size': '2505'}