<jupyter_start><jupyter_text>SVD reconstruction analysis on Synthesis Images   Utils functions definition<jupyter_code>def compute_images_path(dict_data):
    
    scene = dict_data['name']
    prefix = dict_data['prefix']
    indices = dict_data['indices']
    
    images_path = []
    for index in indices:
        path = os.path.join(data_folder, os.path.join(scene, prefix + index + ".png"))
        print(path)
        images_path.append(path)
    return images_path
def get_images_zones(dict_data, images_path):
    
    id_zone = dict_data['zone']
    
    zones_img = []
    for path in images_path:
        img = Image.open(path)
        zones = segmentation.divide_in_blocks(img, (200, 200))
        zones_img.append(zones[id_zone])
        
    return zones_img
def display_svd_reconstruction(interval, zones):
    
    output_images = []
    begin, end = interval
    for zone in zones:
        lab_img = transform.get_LAB_L(zone)
        lab_img = np.array(lab_img, 'uint8')
        
        U, s, V = svd(lab_img, full_matrices=True)
        
        smat = np.zeros((end-begin, end-begin), dtype=complex)
        smat[:, :] = np.diag(s[begin:end])
        output_img = np.dot(U[:, begin:end],  np.dot(smat, V[begin:end, :]))
        
        print(output_img)
        print(np.allclose(lab_img, output_img))
        
        output_img = np.array(output_img, 'uint8')
        output_images.append(Image.fromarray(output_img))
        
    return output_images
def display_images(dict_data, rec_images):
    
    indices = dict_data['indices']
    scene = dict_data['name']
    
    fig=plt.figure(figsize=(15, 8))
    columns = len(zones)
    rows = 1
    for i in range(1, columns*rows +1):
        index = i - 1
        fig.add_subplot(rows, columns, i)
        plt.imshow(rec_images[index], label=scene + '_' + str(indices[index]))
        img_path = 'tmp_images/' + dict_data['prefix'] + 'zone'+ str(current_dict['zone']) + '_reconstruct_' + str(indices[index]) + '.png'
        Image.fromarray(np.asarray(rec_images[index], 'uint8')).save(img_path)
    plt.show()
    
def diff_between_images(noisy, ref):
    noisy = np.asarray(noisy)
    ref = np.asarray(ref)
    
    return ref - noisy
def display_sv_data(dict_data, zones_data, interval, reduced=True):
    
    scene_name = dict_data['name']
    image_indices = dict_data['indices']
    zone_indice = dict_data['zone']
    
    plt.figure(figsize=(25, 20))
    
    sv_data = []
    begin, end = interval
    for id_img, zone in enumerate(zones_data):
        zone = np.asarray(zone)
        print(zone.shape)
        U, s, V = metrics.get_SVD(zone)

        data = s[begin:end]
        
        label_plt = 'Zone ' + str(zone_indice)
        
        if reduced:
            label_plt += ' reduced info '
        
        label_plt += 'of ' + scene_name + '_' + str(image_indices[id_img])
        
        plt.plot(data, label=label_plt)
    
    plt.legend(fontsize=18)
    plt.show()<jupyter_output><empty_output><jupyter_text>Scenes information data<jupyter_code># start 00020 - ref 00900 - step 10
dict_appart = {'name': 'Appart1opt02', 
               'prefix': 'appartAopt_', 
               'indices': ["00050","00250","00400","00550"],
               'zone': 9}

# start 00050 - ref 01200 - step 10
dict_cuisine = {'name': 'Cuisine01', 
               'prefix': 'cuisine01_', 
               'indices': ["00050", "00400", "01200"],
               'zone': 3}

# start 00020 - ref 00950 - step 10
dict_sdb_c = {'name': 'SdbCentre', 
               'prefix': 'SdB2_', 
               'indices': ["00020", "00400", "00950"],
               'zone': 3}

# start 00020 - ref 00950 - step 10
dict_sdb_d = {'name': 'SdbDroite', 
               'prefix': 'SdB2_D_', 
               'indices': ["00020", "00400", "00950"],
               'zone': 3}<jupyter_output><empty_output><jupyter_text>Definition of parameters Here we define parameters for the rest of this study :- the scene used- the reconstructed interval (give reduced information from SVD decomposition) - the displayed interval of SVD values<jupyter_code>current_dict = dict_appart
displayed_interval = (50, 200)
reconstructed_interval = (90, 200)
images_path = compute_images_path(current_dict)
zones = get_images_zones(current_dict, images_path)

# save each zone
for id, zone in enumerate(zones):
    img_name = current_dict['prefix'] + 'zone'+ str(current_dict['zone']) + '_' + current_dict['indices'][id] + '.png'
    zone.save('tmp_images/' + img_name)

Image.fromarray(np.array(transform.get_LAB_L(zones[0]), 'uint8'))
zone = zones[0]
Image.fromarray(np.array(transform.get_LAB_L(zones[0]), 'uint8')).save('tmp_images/initial_image.png')
tuples = [(0,50), (50,200)]
for t in tuples:
    begin, end = t
    lab_img = transform.get_LAB_L(zone)
    lab_img = np.array(lab_img, 'uint8')

    U, s, V = svd(lab_img, full_matrices=True)

    smat = np.zeros((end-begin, end-begin), dtype=complex)
    smat[:, :] = np.diag(s[begin:end])
    output_img = np.dot(U[:, begin:end],  np.dot(smat, V[begin:end, :]))

    print(output_img)
    print(np.allclose(lab_img, output_img))

    output_img = np.array(output_img, 'uint8')
    Image.fromarray(output_img).save('tmp_images/' + str(begin) + '_' + str(end) + '_reconstructed.png')
    #output_images.append(Image.fromarray(output_img))
reconstructed_images = display_svd_reconstruction(reconstructed_interval, zones)<jupyter_output>[[ 0.28866806+0.j -0.29094643+0.j -0.02445111+0.j ... -1.71491631+0.j
   1.31940459+0.j  0.20760834+0.j]
 [ 0.16505233+0.j -0.88660669+0.j  0.01207778+0.j ...  1.94463722+0.j
  -0.3744958 +0.j  0.12481831+0.j]
 [ 0.31383035+0.j  0.19614328+0.j -0.80474439+0.j ... -0.20867496+0.j
   0.89348205+0.j -1.22883763+0.j]
 ...
 [-0.29156436+0.j  0.4335953 +0.j  0.85923396+0.j ... -1.17487605+0.j
   0.34562434+0.j -0.08012247+0.j]
 [-0.97248126+0.j  0.58088408+0.j  0.80787694+0.j ...  0.16508712+0.j
   0.53716664+0.j -0.70613476+0.j]
 [-0.48270753+0.j  0.66080143+0.j -0.27728865+0.j ...  4.37054627+0.j
  -2.99107566+0.j -1.36264181+0.j]]
False
[[-0.14994963+0.j  0.43581967+0.j -0.09190862+0.j ... -0.47969561+0.j
  -0.31652857+0.j  0.30262608+0.j]
 [ 0.01319542+0.j  0.01037736+0.j  0.29467907+0.j ...  0.03544648+0.j
   0.34455839+0.j -0.19115319+0.j]
 [ 0.43129111+0.j -0.05703119+0.j -0.22410688+0.j ...  0.17455232+0.j
  -0.29794485+0.j -0.80585839+0.j]
 ...
 [ 0.0750385 +0.j -0.339218  +0.j -0.1[...]<jupyter_text>Overview information for each images (with samples : \["00200","00250","00300","00350"\]) reconstructed with reduced information. Images are displayed following this samples list from left to right.<jupyter_code>display_images(current_dict, reconstructed_images)<jupyter_output><empty_output><jupyter_text>Display of SV values obtained to compare  With reduction of reconstructed images<jupyter_code>display_sv_data(current_dict, reconstructed_images, displayed_interval)<jupyter_output>(200, 200)
(200, 200)
(200, 200)
(200, 200)<jupyter_text>Without reduction of information<jupyter_code>zones_lab = [Image.fromarray(np.array(metrics.get_LAB_L(z), 'uint8')) for z in zones]
display_sv_data(current_dict, zones_lab, displayed_interval, reduced=False)<jupyter_output>(200, 200)
(200, 200)
(200, 200)
(200, 200)<jupyter_text>Noise mask analysisNext part to explore (extraction of noise filter)..<jupyter_code># start 00020 - ref 00900 - step 10
dict_appart = {'name': 'Appart1opt02', 
               'prefix': 'appartAopt_', 
               'indices': ["00020", "00900"],
               'zone': 3}

# start 00050 - ref 01200 - step 10
dict_cuisine = {'name': 'Cuisine01', 
               'prefix': 'cuisine01_', 
               'indices': ["00050", "00400", "01200"],
               'zone': 3}

# start 00020 - ref 00950 - step 10
dict_sdb_c = {'name': 'SdbCentre', 
               'prefix': 'SdB2_', 
               'indices': ["00020", "00400", "00950"],
               'zone': 3}

# start 00020 - ref 00950 - step 10
dict_sdb_d = {'name': 'SdbDroite', 
               'prefix': 'SdB2_D_', 
               'indices': ["00020", "00400", "00950"],
               'zone': 3}
mask_current_dict = dict_appart
mask_images_path = compute_images_path(mask_current_dict)
mask_zones = get_images_zones(mask_current_dict, mask_images_path)
Image.fromarray(diff_between_images(mask_zones[0], mask_zones[1]))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1524390243902439, 'fraction_numerical': 0.109636537541846, 'mean_word_length': 3.1206896551724137, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '501886', 'license': 'MIT', 'n_tokens_mistral': 3686, 'n_tokens_neox': 3129, 'n_words': 574, 'path': 'analysis/svd_reconstruction_analysis.ipynb', 'repo_name': 'prise-3d/Thesis-NoiseDetection-metrics', 'size': '8364'}