<jupyter_start><jupyter_text>Microlocal analysis convolution  In this notebook we analyse the way to express a 3x3 convolutional kernel into a linear combination of the basis formed by the $\Delta_{ij}$<jupyter_code>import sys
sys.path.append("../WF_inpaint/")<jupyter_output><empty_output><jupyter_text>Load data<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import numpy.random as rnd
from ellipse.ellipseWF_factory import random_ellipse, _ellipses_parameters, _ellipse_grad_gen, _ellipse_gen, plot_WF
size = 256
dirBias = 60
center = [size/2,size/2]
width = 50
height = 70
angle = 15
opacity = 1
grad_level = rnd.uniform(-2,2)
if grad_level <= 0:
    f = 1-_ellipse_gen(center, width, height, angle, size, opacity)
else:
    f = 1-_ellipse_grad_gen(center, width, height, angle, size, opacity, grad_level)
plt.figure(figsize = (6,6))
plt.axis("off")
plt.imshow(f, cmap="gray")<jupyter_output><empty_output><jupyter_text>Defining random 3x3 filter<jupyter_code>T = np.random.rand(3,3)
plt.figure(figsize = (6,6))
plt.axis("off")
plt.imshow(T)
plt.figure(figsize = (6,6))
plt.axis("off")
plt.imshow(convolve2d(f,T), cmap="gray")<jupyter_output><empty_output><jupyter_text>Changing the basis with the matrices<jupyter_code>## Let us compute the convolution
from scipy.signal import convolve2d
D_11 = np.array([[0,0,0],[0, 1, 0], [0 , 0 , 0]])
D_12 = np.array([[0,1,0],[0, 0, 0], [0 , -1 , 0]])
D_21 = np.array([[0,0,0],[1, 0, -1], [0 , 0 , 0]])
D_22 = np.array([[1,0,-1],[0, 0, 0], [-1 , 0 , 1]])
D_13 = np.array([[0,-1,0],[0, 2, 0], [0 , -1 , 0]])
D_31 = np.array([[0,0,0],[1, -2, 1], [0 , 0 , 0]])
D_23 = np.array([[1,-2,1],[0, 0, 0], [-1 , 2 , -1]])
D_32 = np.array([[1,0,-1],[-2, 0, 2], [1 , 0 , -1]])
D_33 = np.array([[-1,2,-1],[2, -4, 2], [-1 , 2 , -1]])
D = [D_11, D_12, D_13, D_21, D_22, D_23, D_31, D_32, D_33]<jupyter_output><empty_output><jupyter_text>In order to solve the matrix equation$$\begin{split}\theta = \theta(\beta)  &= \beta_{11}\Delta_{11}  +\beta_{12}\Delta_{12}  +\beta_{21}\Delta_{21}  +\beta_{22}\Delta_{22}  +\beta_{13}\Delta_{13}  \\ & \phantom{=}  +\beta_{31}\Delta_{31}  +\beta_{32}\Delta_{32}  +\beta_{23}\Delta_{23}  +\beta_{33}\Delta_{33}\end{split}$$we are going to solve the vectorized linear equation $$\vec{\theta} = \mathbf{A}\vec{\beta}$$therefore the coefficients $\beta{ij}$ are given by the equation$$\vec{\beta} = \mathbf{A}^{-1}\vec{\theta}$$where $$A = \begin{bmatrix}0 &  0 &  0 &  0 &  1 &  1 &  0 &  1 & -1 \\0 &  1 & -1 &  0 &  0 & -2 &  0 &  0 &  2 \\0 &  0 &  0 &  0 & -1 &  1 &  0 & -1 & -1 \\0 &  0 &  0 &  1 &  0 &  0 &  1 & -2 &  2 \\1 &  0 &  2 &  0 &  0 &  0 & -2 &  0 & -4 \\0 &  0 &  0 & -1 &  0 &  0 &  1 &  2 &  2 \\0 &  0 &  0 &  0 & -1 & -1 &  0 &  1 & -1 \\0 & -1 & -1 &  0 &  0 &  2 &  0 &  0 &  2 \\0 &  0 &  0 &  0 &  1 & -1 &  0 & -1 & -1 \end{bmatrix}$$<jupyter_code># Let us define the matrix for the change of coordinates

A = np.array([
    [0, 0, 0,  0, 1, 1, 0, 1, -1],
    [0, 1, -1, 0, 0, -2, 0, 0, 2],
    [0, 0, 0, 0, -1, 1, 0, -1, -1],
    [0, 0, 0, 1, 0, 0, 1, -2, 2],
    [1, 0, 2, 0, 0, 0, -2, 0, -4],
    [0, 0, 0, -1, 0, 0, 1, 2, 2],
    [0, 0, 0, 0, -1, -1, 0, 1, -1],
    [0, -1, -1, 0, 0, 2, 0, 0, 2],
    [0, 0, 0, 0, 1, -1, 0, -1, -1]
])
A
plt.figure(figsize = (6,6))
plt.axis("off")
plt.imshow(A)
# Computing the inverse
Ainv = np.linalg.inv(A)
plt.figure(figsize = (6,6))
plt.axis("off")
plt.imshow(Ainv)
# Let us compute the flatten version of beta
Bflat = Ainv.dot(T.flatten())
B = Bflat.reshape(3,3);
plt.figure(figsize = (6,6))
plt.axis("off")
plt.imshow(B)<jupyter_output><empty_output><jupyter_text>Let us now check that the matix equation follows<jupyter_code>T_recon = sum([Bflat[i]*D[i] for i in range(len(D))])
plt.figure(figsize = (6,6))
plt.axis("off")
plt.imshow(T_recon)
plt.figure(figsize = (6,6))
plt.axis("off")
plt.imshow(T_recon)<jupyter_output><empty_output><jupyter_text>Check its application to the image Each of the coefficients represent the amound of smoothing done in such directions, for example $\beta_11$ corresponding how much the signal is intesified or diminished, $\beta_12$ representing the coefficient for $\partial_2 f$ represent the amount of smoothing in the direction $x_2$<jupyter_code>plt.plot(Bflat)<jupyter_output><empty_output><jupyter_text>We can see from the plot that for this specific filter, there is a lot of smoothing of first order the $x$ and $y$ but not of second order  Ellipticity measure From the coefficients we can know wether or not the convolutional operator is elliptic (it does not vanish any singularity). In this case the operator is elliptic if the symbol polynomial: $$\begin{aligned}p_{\theta}(\xi) =&\beta_{11} +\beta_{12}\xi_2+\beta_{21}\xi_1+\beta_{22}\xi_1\xi_2\\&+\beta_{13}\xi_2^2+\beta_{31}\xi_{1}^2+\beta_{23}\xi_{2}^2\xi_{1}+\beta_{32}\xi_{1}^2\xi_{2}+\beta_{33}\xi_1^2\xi_{2}^2. \end{aligned}$$does not vanish in the domain, in our case the grid domain $\{0,\ldots N\}\times \{0,\ldots N\}$.Let us compute the symbol for this case<jupyter_code>def pT(B, Xi): 
    p = (B[0,0]+B[0,1]*Xi[1]+B[1,0]*Xi[0]+B[1,1]*Xi[0]*Xi[1]+
        B[0,2]*Xi[1]**2+B[2,0]*Xi[1]**2+B[1,2]*Xi[0]*Xi[1]**2+
        B[2,1]*Xi[1]*Xi[0]**2+B[2,2]*(Xi[1]**2)*Xi[0]**2)
    return p<jupyter_output><empty_output><jupyter_text>Let us evaluate the symbol in the grid<jupyter_code>ellipt = np.array([pT(B,np.array([xi,yi])) for xi in range(size) for yi in range(size)]).reshape(size,size);
# Notice that the symbol acting on the fourier domain is killing the high frequencies
# meaning is smoothing out tha array
plt.imshow(ellipt)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.20198616776024117, 'fraction_numerical': 0.07820535555949637, 'mean_word_length': 4.031222123104371, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '225748', 'license': 'MIT', 'n_tokens_mistral': 2610, 'n_tokens_neox': 2383, 'n_words': 632, 'path': 'Microcanonical_NN/Numpy_microlocal/Micro_local_convolution.ipynb', 'repo_name': 'arsenal9971/DeeMicrolocalReconstruction', 'size': '5639'}