<jupyter_start><jupyter_text>Combinations of 3 Algorithms<jupyter_code># 3
# ADC -> LAVA -> Decimation/Demodulation -> Requantization

# Inverse Requantization -> Interpolation/Modulation -> Interpolation

def LAVA_Dec_Dem_Requant():

    dataset = 'contrast_speckle'
    threshold = 0.00193345

    f_sampling = 20.832 * (10 ** 6)
    f_carrier = 5.2083 * (10 ** 6)

    requant_bits = 9

    # Get input data
    rf_original_data = get_original_rf_data("../datasets/archive/archive_to_download/database/experiments/" + dataset + "/rf_dataset.hdf5")

    # LAVA
    kept_val_LAVA, indexes_LAVA = LAVA(threshold, rf_original_data)
    print("Done with LAVA.")

    # Recontructed LAVA array
    range_of_samples = [range(0,rf_original_data.shape[2])]
    reconstr_data = np.zeros((75, 128, 3328), dtype=np.float32)
    no_samples = 0
    no_indexes = 0

    print("Starting demodulation, requantization.")
    # Convert individual channel acquisitions from channel to list
    for i in range(len(kept_val_LAVA)):
        for k in range(len(kept_val_LAVA[0])):

            # Find perfect divider for each length
            for n in range(16,0,-1):

                if (len(kept_val_LAVA[i][k])%n == 0):
                    compression_ratio = n
                    #print("Compression ratio: {}".format(compression_ratio))
                    break

            compr_data_values = []

            current_kept_chann = np.zeros((1,len(kept_val_LAVA[i][k])))
            current_kept_chann[0] = np.array(kept_val_LAVA[i][k], dtype=object)

            # Demodulate and decimate
            compr_data_values.append(demodulate_decimate(current_kept_chann, f_sampling, f_carrier, compression_ratio))
            arr_data_values = compr_data_values[0][0]

            demod_values = np.zeros((1, arr_data_values.shape[0]),dtype=np.complex64)
            demod_values[0] = arr_data_values

            # Requantization
            int_real = convert_to_int(demod_values[0].real, 12, 2)
            int_imag = convert_to_int(demod_values[0].imag, 12, 2)

            requant_real = requantization(requant_bits, int_real, 0,1800,0,44,1800,2200,45,444,2200,4095,445,511)
            requant_imag = requantization(requant_bits, int_imag, 0,1800,0,44,1800,2200,45,444,2200,4095,445,511)

            # Calculate number of samples
            no_samples = no_samples + arr_data_values.shape[0]
            no_indexes = no_indexes + len(indexes_LAVA[i][k])
            
            # Inverse Requantization
            inv_req_real = inverse_requantization(requant_real,0,1800,0,44,1800,2200,45,444,2200,4095,445,511)
            inv_req_imag = inverse_requantization(requant_imag,0,1800,0,44,1800,2200,45,444,2200,4095,445,511)

            # Convert back to floats
            divider = ((2**12)-1)/2
            rf_range_real = inv_req_real/divider
            rf_range_imag = inv_req_imag/divider

            rf_data_real = rf_range_real - 1
            rf_data_imag = rf_range_imag - 1

            # Generate complex number from the above arrays

            complex_numb = np.empty((demod_values.shape), dtype=np.complex64)
            complex_numb.real = rf_data_real
            complex_numb.imag = rf_data_imag

            # Interpolate and modulate

            inter_data = interpolate_modulate(complex_numb, f_sampling/compression_ratio, f_carrier, compression_ratio)

            inter_data_real = inter_data.real

            # Inverse LAVA / Interpolation
            reconstr_data[i][k] = np.interp(range_of_samples, indexes_LAVA[i][k], inter_data_real[0])       
            sys.stdout.write('\r')
            sys.stdout.write("Done with Acq {} Chan {}".format(i,k))
            
    # Calculate compression ratio
    # Transfer demodulated data and all the indexes as they are after LAVA
    initial_number_of_bits = (rf_original_data.size)*12
    compressed_number_of_bits = requant_bits*2*(no_samples) + 12*(no_indexes)
    
    # Compression ratio
    compr_ratio = (compressed_number_of_bits/initial_number_of_bits)*100
    print("LAVA_Dec_Dem_Requant is {:.2f}% of the original data.".format(compr_ratio))

    # Beamform and plot the compressed image

    dataset_path = "../datasets/archive/archive_to_download/database/experiments/" + dataset + "/rf_dataset.hdf5"

    data_loader_obj = DataLoader(dataset_path)

    ### Specify Image settings and create corresponding object ###

    img_res = [400, 600]
    image_x_range = [-0.019, 0.019]
    image_z_range = [0.005, 0.05]

    db_range = 50

    LATERAL_PIXEL_DENSITY_DEFAULT = 5

    img_config = ImageSettings(image_x_range[0],
                               image_x_range[1],
                               image_z_range[0],
                               image_z_range[1],
                               LATERAL_PIXEL_DENSITY_DEFAULT,
                               data_loader_obj.transducer)

    ### Specify preprocessing parameters for RF data ###

    decimation_factor = 1
    interpolation_factor = 10

    ### Specify TX strategy and Apodization parameters ###

    start_time = 0
    correction_time_shift = 0

    alpha_fov_apod = 40

    ### Specify Sampling Frequency ###

    SAMPLING_FREQ = 20.832 * (10 ** 6)

    filters_params = [1 * 10 **6, 8 * 10 **6, 0.5 * 10 **6]

    bf = BFCartesianRealTime(data_loader_obj.f_sampling,
                             data_loader_obj.tx_strategy,
                             data_loader_obj.transducer,
                             decimation_factor,
                             interpolation_factor,
                             img_res,
                             img_config,
                             start_time=start_time,
                             correction_time_shift=correction_time_shift,
                             alpha_fov_apod=alpha_fov_apod,
                             bp_filter_params=filters_params,
                             envelope_detector='hilbert',
                             picmus_dataset=True)


    # Beamforming
    img_data = bf.beamform(reconstr_data, numba_active=True)

    _ = plot_image(np.abs(img_data), 
                   scatters_coords_xz=None,
                   elements_coords_xz=None,
                   framework='plotly',
                   title='LAVA_Dec_Dem_Requant: {:.2f}% of the original data'.format(compr_ratio),
                   image_x_range=image_x_range,
                   image_z_range=image_z_range,
                   db_range=db_range,
                   colorscale='Greys',
                   save_fig=True, 
                   show=True,
                   path_to_save='./LAVA_Dec_Dem_Requant')
    
    # Read info from file
    f_d = h5py.File("../datasets/archive/archive_to_download/database/experiments/"+dataset+"/"+dataset+"_expe_dataset_rf.hdf5", "r")

    attributes = f_d.attrs

    US = f_d['US']
    attributes = US.attrs

    US_DATASET0000 = US['US_DATASET0000']
    attributes = US_DATASET0000.attrs

    type_s = attributes.__getitem__('type')

    attributes = US_DATASET0000.attrs

    keys_1 = list(f_d.keys());

    US = f_d['US']

    US_DATASET0000 = US['US_DATASET0000']

    data = US_DATASET0000['data']

    imag_n = data['imag']

    real_n = data['real'][()]

    PRF = US_DATASET0000['PRF']

    angles = US_DATASET0000['angles']

    initial_time = US_DATASET0000['initial_time']

    modulation_frequency = US_DATASET0000['modulation_frequency']

    probe_geometry = US_DATASET0000['probe_geometry']

    sampling_frequency = US_DATASET0000['sampling_frequency']

    sound_speed = US_DATASET0000['sound_speed']

    # Create dataset suitable for calculating metrics

    imag_np = np.zeros((75, 128, 3328))

    # Creation of dataset

    out_filename = out_filename = './LAVA_Dec_Dem_Requant/LAVA_Dec_Dem_Requant_th{}.hdf5'.format(threshold)
    comp_file = h5py.File(out_filename,'w')
    comp_file.close()
    comp_file = h5py.File(out_filename,'a')

    # Complete the structure

    comp_file.attrs.create('version', [b'v.0.0.40'], dtype='|S9')

    US_path = 'US'
    US_group = comp_file.require_group(US_path)

    US_dataset_group = US_group.require_group('US_DATASET0000')

    US_dataset_group.attrs.create('type', [b"US"], dtype='|S2')
    US_dataset_group.attrs.create('subtype', [b"CPW"], dtype='|S3')
    US_dataset_group.attrs.create('signal_format', [b"RF"], dtype='|S2')
    US_dataset_group.attrs.create('name', [b"CIRS 040GSE Wires"], dtype='|S18')
    US_dataset_group.attrs.create('version', [b"v1.96"], dtype='|S6')
    US_dataset_group.attrs.create('creation_date', [b"2016/03/9 17:25:22.38"], dtype='|S22')

    # angles
    US_dataset_group.create_dataset( 'angles', data=angles)

    # PRF
    US_dataset_group.create_dataset( 'PRF', data=PRF)

    # initial_time
    US_dataset_group.create_dataset( 'initial_time', data=initial_time)

    # modulation frequency
    US_dataset_group.create_dataset( 'modulation_frequency', data=modulation_frequency)

    # probe geometry
    US_dataset_group.create_dataset( 'probe_geometry', data=probe_geometry)

    # sampling frequency
    US_dataset_group.create_dataset( 'sampling_frequency', data=sampling_frequency)

    # sound speed
    US_dataset_group.create_dataset( 'sound_speed', data=sound_speed)

    # imag data
    data_group = US_dataset_group.require_group('data')
    data_group.create_dataset('imag', data=imag_np)

    # real data
    data_group.create_dataset('real', data=reconstr_data)

    comp_file.close()
# 4
# ADC -> Dec/Dem -> LAVA -> Requantization

# Inverse Requantization -> Interpolation -> Interpolation/Modulation
def Dec_Dem_LAVA_Requant():

    dataset = 'contrast_speckle'
    threshold = 0.00193345

    f_sampling = 20.832 * (10 ** 6)
    f_carrier = 5.2083 * (10 ** 6)
    compression_ratio = 8

    # Get input data
    rf_original_data = get_original_rf_data("../datasets/archive/archive_to_download/database/experiments/" + dataset + "/rf_dataset.hdf5")

    # High-pass filter data before conversion
    #rf_data_high_pass = filter_band_pass(rf_original_data, f_sampling, f_sampling/20.832, f_sampling/2.5, f_sampling/100)

    # Decimate and demodulate for all shots
    compr_data = np.ndarray(shape=(rf_original_data.shape[0], rf_original_data.shape[1], int(rf_original_data.shape[2]/compression_ratio)), dtype=np.complex64)

    for shot in range(0, rf_original_data.shape[0]):
        compr_data[shot] = demodulate_decimate(rf_original_data[shot], f_sampling, f_carrier, compression_ratio)

    print('Compressed data shape: ', compr_data.shape)
    print('Done with Dem/Dec.')

    # LAVA
    kept_val_LAVA, indexes_LAVA = LAVA(threshold, compr_data.real)
    kept_val_imag_LAVA, indexes_LAVA_imag = LAVA(threshold, compr_data.imag)

    print('Done with LAVA.')

    range_of_samples = [range(0,compr_data.shape[2])]
    reconstr_data_real = np.zeros((75, 128, compr_data.shape[2]), dtype=np.float32)
    reconstr_data_imag = np.zeros((75, 128, compr_data.shape[2]), dtype=np.float32)
    no_samples = 0
    no_indexes = 0
    no_samples_imag = 0
    no_indexes_imag = 0

    print('Starting Requantization.')

    # Requantization
    # REMEMBER FOR THE INDEXES WE NEED ONLY AS MANY BITS AS LOG2(TOTAL_NUMBER_OF_SAMPLES)
    # Convert individual channel acquisitions from list to array
    for i in range(len(kept_val_LAVA)):
        for k in range(len(kept_val_LAVA[0])):

            current_kept_real = np.zeros((len(kept_val_LAVA[i][k])))
            current_kept_real = np.array(kept_val_LAVA[i][k], dtype=object)

            current_kept_imag = np.zeros((len(kept_val_imag_LAVA[i][k])))
            current_kept_imag = np.array(kept_val_imag_LAVA[i][k], dtype=object)

            int_real = convert_to_int(current_kept_real, 12, 2)
            int_imag = convert_to_int(current_kept_imag, 12, 2)

            requant_real = requantization(9, int_real, 0,1800,0,44,1800,2200,45,444,2200,4095,445,511)
            requant_imag = requantization(9, int_imag, 0,1800,0,44,1800,2200,45,444,2200,4095,445,511)       

            # Calculate number of samples
            no_samples = no_samples + len(kept_val_LAVA[i][k])
            no_indexes = no_indexes + len(indexes_LAVA[i][k])
            
            no_samples_imag = no_samples_imag + len(kept_val_imag_LAVA[i][k])
            no_indexes_imag = no_indexes_imag + len(indexes_LAVA_imag[i][k])
            
            # Inverse Requantization
            inv_req_real = inverse_requantization(requant_real,0,1800,0,44,1800,2200,45,444,2200,4095,445,511)
            inv_req_imag = inverse_requantization(requant_imag,0,1800,0,44,1800,2200,45,444,2200,4095,445,511)

            # Convert back to floats
            divider = ((2**12)-1)/2
            rf_range_real = inv_req_real/divider
            rf_range_imag = inv_req_imag/divider

            rf_data_real = rf_range_real - 1
            rf_data_imag = rf_range_imag - 1

            # Inverse LAVA / Interpolate

            reconstr_data_real[i][k] = np.interp(range_of_samples, indexes_LAVA[i][k], rf_data_real)
            reconstr_data_imag[i][k] = np.interp(range_of_samples, indexes_LAVA_imag[i][k], rf_data_imag)
            sys.stdout.write('\r')
            sys.stdout.write('Done with Acq {} Chan {}.'.format(i, k))

    # Generate complex number from the above arrays
    import cmath

    complex_numb = np.empty((75, 128, compr_data.shape[2]), dtype=np.complex64)
    complex_numb.real = reconstr_data_real
    complex_numb.imag = reconstr_data_imag

    # Interpolate / Modulate
    inter_data = np.zeros(rf_original_data.shape, dtype=np.complex64)
    for shot in range(complex_numb.shape[0]):
        inter_data[shot] = interpolate_modulate(complex_numb[shot], f_sampling/compression_ratio, f_carrier, compression_ratio)

    print('Done.')
    
    # Calculate compression ratio
    # Number of bits needed for indexes varies according to compression ratio of decimation/demodulation
    no_bits_indexes = int(np.ceil(np.log2(compr_data.shape[2])))
    initial_number_of_bits = (rf_original_data.size)*12
    compressed_number_of_bits = 9*(no_samples) + no_bits_indexes*(no_indexes) + 9*(no_samples_imag) + no_bits_indexes*(no_indexes_imag)
    
    # Compression ratio
    compr_ratio = (compressed_number_of_bits/initial_number_of_bits)*100
    print("Dec_Dem_LAVA_Requant is {:.2f}% of the original data.".format(compr_ratio))


    # Beamform and plot the compressed image

    dataset_path = "../datasets/archive/archive_to_download/database/experiments/" + dataset + "/rf_dataset.hdf5"

    data_loader_obj = DataLoader(dataset_path)

    ### Specify Image settings and create corresponding object ###

    img_res = [400, 600]
    image_x_range = [-0.019, 0.019]
    image_z_range = [0.005, 0.05]

    db_range = 50

    LATERAL_PIXEL_DENSITY_DEFAULT = 5

    img_config = ImageSettings(image_x_range[0],
                               image_x_range[1],
                               image_z_range[0],
                               image_z_range[1],
                               LATERAL_PIXEL_DENSITY_DEFAULT,
                               data_loader_obj.transducer)

    ### Specify preprocessing parameters for RF data ###

    decimation_factor = 1
    interpolation_factor = 10

    ### Specify TX strategy and Apodization parameters ###

    start_time = 0
    correction_time_shift = 0

    alpha_fov_apod = 40

    ### Specify Sampling Frequency ###

    SAMPLING_FREQ = 20.832 * (10 ** 6)

    filters_params = [1 * 10 **6, 8 * 10 **6, 0.5 * 10 **6]

    bf = BFCartesianRealTime(data_loader_obj.f_sampling,
                             data_loader_obj.tx_strategy,
                             data_loader_obj.transducer,
                             decimation_factor,
                             interpolation_factor,
                             img_res,
                             img_config,
                             start_time=start_time,
                             correction_time_shift=correction_time_shift,
                             alpha_fov_apod=alpha_fov_apod,
                             bp_filter_params=filters_params,
                             envelope_detector='hilbert',
                             picmus_dataset=True)


    # Beamforming
    img_data = bf.beamform(inter_data.real, numba_active=True)

    _ = plot_image(np.abs(img_data), 
                   scatters_coords_xz=None,
                   elements_coords_xz=None,
                   framework='plotly',
                   title='Dec_Dem_LAVA_Requant: {:.2f}% of the original data'.format(compr_ratio),
                   image_x_range=image_x_range,
                   image_z_range=image_z_range,
                   db_range=db_range,
                   colorscale='Greys',
                   save_fig=True, 
                   show=True,
                   path_to_save='./Dec_Dem_LAVA_Requant')
    
    # Read info from file
    f_d = h5py.File("../datasets/archive/archive_to_download/database/experiments/"+dataset+"/"+dataset+"_expe_dataset_rf.hdf5", "r")

    attributes = f_d.attrs

    US = f_d['US']
    attributes = US.attrs

    US_DATASET0000 = US['US_DATASET0000']
    attributes = US_DATASET0000.attrs

    type_s = attributes.__getitem__('type')

    attributes = US_DATASET0000.attrs

    keys_1 = list(f_d.keys());

    US = f_d['US']

    US_DATASET0000 = US['US_DATASET0000']

    data = US_DATASET0000['data']

    imag_n = data['imag']

    real_n = data['real'][()]

    PRF = US_DATASET0000['PRF']

    angles = US_DATASET0000['angles']

    initial_time = US_DATASET0000['initial_time']

    modulation_frequency = US_DATASET0000['modulation_frequency']

    probe_geometry = US_DATASET0000['probe_geometry']

    sampling_frequency = US_DATASET0000['sampling_frequency']

    sound_speed = US_DATASET0000['sound_speed']

    # Create dataset suitable for calculating metrics

    imag_np = np.zeros((75, 128, 3328))

    # Creation of dataset

    out_filename = out_filename = './Dec_Dem_LAVA_Requant/Dec_Dem_LAVA_Requant_th{}_cr{}.hdf5'.format(threshold,compression_ratio)
    comp_file = h5py.File(out_filename,'w')
    comp_file.close()
    comp_file = h5py.File(out_filename,'a')

    # Complete the structure

    comp_file.attrs.create('version', [b'v.0.0.40'], dtype='|S9')

    US_path = 'US'
    US_group = comp_file.require_group(US_path)

    US_dataset_group = US_group.require_group('US_DATASET0000')

    US_dataset_group.attrs.create('type', [b"US"], dtype='|S2')
    US_dataset_group.attrs.create('subtype', [b"CPW"], dtype='|S3')
    US_dataset_group.attrs.create('signal_format', [b"RF"], dtype='|S2')
    US_dataset_group.attrs.create('name', [b"CIRS 040GSE Wires"], dtype='|S18')
    US_dataset_group.attrs.create('version', [b"v1.96"], dtype='|S6')
    US_dataset_group.attrs.create('creation_date', [b"2016/03/9 17:25:22.38"], dtype='|S22')

    # angles
    US_dataset_group.create_dataset( 'angles', data=angles)

    # PRF
    US_dataset_group.create_dataset( 'PRF', data=PRF)

    # initial_time
    US_dataset_group.create_dataset( 'initial_time', data=initial_time)

    # modulation frequency
    US_dataset_group.create_dataset( 'modulation_frequency', data=modulation_frequency)

    # probe geometry
    US_dataset_group.create_dataset( 'probe_geometry', data=probe_geometry)

    # sampling frequency
    US_dataset_group.create_dataset( 'sampling_frequency', data=sampling_frequency)

    # sound speed
    US_dataset_group.create_dataset( 'sound_speed', data=sound_speed)

    # imag data
    data_group = US_dataset_group.require_group('data')
    data_group.create_dataset('imag', data=imag_np)

    # real data
    data_group.create_dataset('real', data=inter_data.real)

    comp_file.close()
# 7
# ADC -> Decimation/Demodulation -> Requantization -> LAVA

# Interpolation -> Inverse Requantization -> Interpolation/Modulation

def Dec_Dem_Requant_LAVA(compr_ratio, req_bits, thres):

    dataset = 'resolution_distorsion'

    f_sampling = 20.832 * (10 ** 6)
    f_carrier = 5.2083 * (10 ** 6)
    compression_ratio = compr_ratio
    req_bits = req_bits
    threshold = thres

    # Get input data
    rf_original_data = get_original_rf_data("../datasets/archive/archive_to_download/database/experiments/" + dataset + "/rf_dataset.hdf5")

    # High-pass filter data before conversion
    #rf_data_high_pass = filter_band_pass(rf_original_data, f_sampling, f_sampling/20.832, f_sampling/2.5, f_sampling/100)

    # Decimate and demodulate for all shots
    compr_data = np.ndarray(shape=(rf_original_data.shape[0], rf_original_data.shape[1], int(np.ceil(rf_original_data.shape[2]/compression_ratio))), dtype=np.complex64)

    for shot in range(0, rf_original_data.shape[0]):
        compr_data[shot] = demodulate_decimate(rf_original_data[shot], f_sampling, f_carrier, compression_ratio)

    print('Compressed data shape: ', compr_data.shape)

    # Requantization
    int_real = convert_to_int_2(compr_data.real, 10, 2)
    int_imag = convert_to_int_2(compr_data.imag, 10, 2)

    # Reduce values to 9 bits
    requant_bits = req_bits
    divider = 512/(2**(req_bits-1))
    int_9_real = np.floor(int_real/divider)
    int_9_real = int_9_real.astype(int)

    # Reduce values to 9 bits
    int_9_imag = np.floor(int_imag/divider)
    int_9_imag = int_9_imag.astype(int)

    # LAVA
    kept_val_LAVA, indexes_LAVA = LAVA(threshold, int_9_real)
    kept_val_imag_LAVA, indexes_LAVA_imag = LAVA(threshold, int_9_imag)

    # Inverse LAVA / Interpolate
    range_of_samples = [range(0,compr_data.shape[2])]
    reconstr_data_real = np.zeros((75, 128, compr_data.shape[2]), dtype=np.float32)
    reconstr_data_imag = np.zeros((75, 128, compr_data.shape[2]), dtype=np.float32)

    no_samples = 0
    no_indexes = 0
    no_samples_imag = 0
    no_indexes_imag = 0

    for i in range(len(kept_val_LAVA)):
        for k in range(len(kept_val_LAVA[0])):

            # Calculate number of samples transmitted
            no_samples = no_samples + len(kept_val_LAVA[i][k])
            no_indexes = no_indexes + len(indexes_LAVA[i][k])

            no_samples_imag = no_samples_imag + len(kept_val_imag_LAVA[i][k])
            no_indexes_imag = no_indexes_imag + len(indexes_LAVA_imag[i][k])

            if (len(kept_val_LAVA[i][k]) == 0):
                continue

            reconstr_data_real[i][k] = np.interp(range_of_samples, indexes_LAVA[i][k], kept_val_LAVA[i][k])

            if (len(kept_val_imag_LAVA[i][k]) == 0):
                continue        

            reconstr_data_imag[i][k] = np.interp(range_of_samples, indexes_LAVA_imag[i][k], kept_val_imag_LAVA[i][k])

            #print("Acq {} Chan {}".format(i,k))


    # Generate complex number from the above arrays
    import cmath

    complex_numb = np.empty((75, 128, compr_data.shape[2]), dtype=np.complex64)
    complex_numb.real = int_9_real
    complex_numb.imag = int_9_imag

    # Interpolate / Modulate
    inter_data = np.zeros(shape=(rf_original_data.shape[0], rf_original_data.shape[1], int(np.ceil(rf_original_data.shape[2]/compression_ratio))*compression_ratio), dtype=np.complex64)
    for shot in range(complex_numb.shape[0]):
        inter_data[shot] = interpolate_modulate(complex_numb[shot], f_sampling/compression_ratio, f_carrier, compression_ratio)

    # Calculate compression ratio
    # Number of bits needed for indexes varies according to compression ratio of decimation/demodulation
    no_bits_indexes = int(np.ceil(np.log2(compr_data.shape[2])))
    initial_number_of_bits = (rf_original_data.size)*10
    compressed_number_of_bits = req_bits*(no_samples) + no_bits_indexes*(no_indexes) + req_bits*(no_samples_imag) + no_bits_indexes*(no_indexes_imag)

    
    # Compression ratio
    compr_ratio = (compressed_number_of_bits/initial_number_of_bits)*100
    print("Dec_Dem_Requant_LAVA is {}% of the original data.".format(compr_ratio))

    # Beamform and plot the compressed image

    dataset_path = "../datasets/archive/archive_to_download/database/experiments/" + dataset + "/rf_dataset.hdf5"

    data_loader_obj = DataLoader(dataset_path)

    ### Specify Image settings and create corresponding object ###

    img_res = [400, 600]
    image_x_range = [-0.019, 0.019]
    image_z_range = [0.005, 0.05]

    db_range = 50

    LATERAL_PIXEL_DENSITY_DEFAULT = 5

    img_config = ImageSettings(image_x_range[0],
                               image_x_range[1],
                               image_z_range[0],
                               image_z_range[1],
                               LATERAL_PIXEL_DENSITY_DEFAULT,
                               data_loader_obj.transducer)

    ### Specify preprocessing parameters for RF data ###

    decimation_factor = 1
    interpolation_factor = 10

    ### Specify TX strategy and Apodization parameters ###

    start_time = 0
    correction_time_shift = 0

    alpha_fov_apod = 40

    ### Specify Sampling Frequency ###

    SAMPLING_FREQ = 20.832 * (10 ** 6)

    filters_params = [1 * 10 **6, 8 * 10 **6, 0.5 * 10 **6]

    bf = BFCartesianRealTime(data_loader_obj.f_sampling,
                             data_loader_obj.tx_strategy,
                             data_loader_obj.transducer,
                             decimation_factor,
                             interpolation_factor,
                             img_res,
                             img_config,
                             start_time=start_time,
                             correction_time_shift=correction_time_shift,
                             alpha_fov_apod=alpha_fov_apod,
                             bp_filter_params=filters_params,
                             envelope_detector='hilbert',
                             picmus_dataset=True)


    # Beamforming
    img_data = bf.beamform(inter_data.real, numba_active=True)

    _ = plot_image(np.abs(img_data), 
                   scatters_coords_xz=None,
                   elements_coords_xz=None,
                   framework='plotly',
                   title='Dec_Dem_Requant_LAVA_{}_{}_{}: {}% of the original data'.format(threshold,compression_ratio,req_bits,compr_ratio),
                   image_x_range=image_x_range,
                   image_z_range=image_z_range,
                   db_range=db_range,
                   colorscale='Greys',
                   save_fig=True, 
                   show=True,
                   path_to_save='./Dec_Dem_Requant_LAVA')
    
    # Read info from file
    dataset = 'resolution_distorsion'
    f_d = h5py.File("../datasets/archive/archive_to_download/database/experiments/"+dataset+"/"+dataset+"_expe_dataset_rf.hdf5", "r")

    attributes = f_d.attrs

    US = f_d['US']
    attributes = US.attrs

    US_DATASET0000 = US['US_DATASET0000']
    attributes = US_DATASET0000.attrs

    type_s = attributes.__getitem__('type')

    attributes = US_DATASET0000.attrs

    keys_1 = list(f_d.keys());

    US = f_d['US']

    US_DATASET0000 = US['US_DATASET0000']

    data = US_DATASET0000['data']

    imag_n = data['imag']

    real_n = data['real'][()]

    PRF = US_DATASET0000['PRF']

    angles = US_DATASET0000['angles']

    initial_time = US_DATASET0000['initial_time']

    modulation_frequency = US_DATASET0000['modulation_frequency']

    probe_geometry = US_DATASET0000['probe_geometry']

    sampling_frequency = US_DATASET0000['sampling_frequency']

    sound_speed = US_DATASET0000['sound_speed']

    # Create dataset suitable for calculating metrics

    imag_np = np.zeros(shape = inter_data.real.shape)

    # Creation of dataset

    out_filename = out_filename = './Dec_Dem_Requant_LAVA/Dec_Dem_Requant_LAVA_{}_{}_{}.hdf5'.format(threshold,compression_ratio,req_bits)
    comp_file = h5py.File(out_filename,'w')
    comp_file.close()
    comp_file = h5py.File(out_filename,'a')

    # Complete the structure

    comp_file.attrs.create('version', [b'v.0.0.40'], dtype='|S9')

    US_path = 'US'
    US_group = comp_file.require_group(US_path)

    US_dataset_group = US_group.require_group('US_DATASET0000')

    US_dataset_group.attrs.create('type', [b"US"], dtype='|S2')
    US_dataset_group.attrs.create('subtype', [b"CPW"], dtype='|S3')
    US_dataset_group.attrs.create('signal_format', [b"RF"], dtype='|S2')
    US_dataset_group.attrs.create('name', [b"CIRS 040GSE Wires"], dtype='|S18')
    US_dataset_group.attrs.create('version', [b"v1.96"], dtype='|S6')
    US_dataset_group.attrs.create('creation_date', [b"2016/03/9 17:25:22.38"], dtype='|S22')

    # angles
    US_dataset_group.create_dataset( 'angles', data=angles)

    # PRF
    US_dataset_group.create_dataset( 'PRF', data=PRF)

    # initial_time
    US_dataset_group.create_dataset( 'initial_time', data=initial_time)

    # modulation frequency
    US_dataset_group.create_dataset( 'modulation_frequency', data=modulation_frequency)

    # probe geometry
    US_dataset_group.create_dataset( 'probe_geometry', data=probe_geometry)

    # sampling frequency
    US_dataset_group.create_dataset( 'sampling_frequency', data=sampling_frequency)

    # sound speed
    US_dataset_group.create_dataset( 'sound_speed', data=sound_speed)

    # imag data
    data_group = US_dataset_group.require_group('data')
    data_group.create_dataset('imag', data=imag_np)

    # real data
    data_group.create_dataset('real', data=inter_data.real)

    comp_file.close()
# 8
# ADC -> Requantization -> Decimation/Demodulation -> LAVA

# Interpolation -> Interpolation/Modulation -> Inverse Requantization

def Requant_Dec_Dem_LAVA():

    dataset = 'contrast_speckle'
    threshold = 0.00193345

    f_sampling = 20.832 * (10 ** 6)
    f_carrier = 5.2083 * (10 ** 6)

    compression_ratio = 8

    # Get input data
    rf_original_data = get_original_rf_data("../datasets/archive/archive_to_download/database/experiments/" + dataset + "/rf_dataset.hdf5")

    with open('./BL_Coding/'+dataset+'/int_converted_values.npy', 'rb') as f:
        int_out_values = np.load(f)
    f.close()

    # Requantize
    with open('./Requantization/'+dataset+'_forw_requant_9_data.npy', 'rb') as f:
        requant_data = np.load(f)
    f.close()

    # Convert to floats
    divider = ((2**9)-1)/2
    rf_range_data = requant_data/divider
    rf_data = rf_range_data - 1

    # Decimation and Demodulation
    compr_data = np.ndarray(shape=(rf_data.shape[0], rf_original_data.shape[1], int(rf_original_data.shape[2]/compression_ratio)), dtype=np.complex64)

    for shot in range(0, rf_data.shape[0]):
        compr_data[shot] = demodulate_decimate(rf_data[shot], f_sampling, f_carrier, compression_ratio)

    print('Compressed data shape: ', compr_data.shape)
    print('Done with Dem/Dec.')


    # LAVA
    kept_val_LAVA, indexes_LAVA = LAVA(threshold, compr_data.real)
    kept_val_imag_LAVA, indexes_LAVA_imag = LAVA(threshold, compr_data.imag)
    
    no_samples = 0
    no_indexes = 0
    no_samples_imag = 0
    no_indexes_imag = 0

    # Inverse LAVA / Interpolate
    range_of_samples = [range(0,compr_data.shape[2])]
    reconstr_data_real = np.zeros((75, 128, compr_data.shape[2]), dtype=np.float32)
    reconstr_data_imag = np.zeros((75, 128, compr_data.shape[2]), dtype=np.float32)

    for i in range(len(kept_val_LAVA)):
        for k in range(len(kept_val_LAVA[0])):
            
            # Calculate number of samples transmitted
            no_samples = no_samples + len(kept_val_LAVA[i][k])
            no_indexes = no_indexes + len(indexes_LAVA[i][k])
            
            no_samples_imag = no_samples_imag + len(kept_val_imag_LAVA[i][k])
            no_indexes_imag = no_indexes_imag + len(indexes_LAVA_imag[i][k])
            
            reconstr_data_real[i][k] = np.interp(range_of_samples, indexes_LAVA[i][k], kept_val_LAVA[i][k])
            reconstr_data_imag[i][k] = np.interp(range_of_samples, indexes_LAVA_imag[i][k], kept_val_imag_LAVA[i][k])


    # Generate complex number from the above arrays
    import cmath

    complex_numb = np.empty((75, 128, compr_data.shape[2]), dtype=np.complex64)
    complex_numb.real = reconstr_data_real
    complex_numb.imag = reconstr_data_imag

    # Interpolate / Modulate
    inter_data = np.zeros(rf_original_data.shape, dtype=np.complex64)
    for shot in range(complex_numb.shape[0]):
        inter_data[shot] = interpolate_modulate(complex_numb[shot], f_sampling/compression_ratio, f_carrier, compression_ratio)

    print('Interpolated data shape: ', inter_data.shape)


    # Inverse Requantization
    inter_data_real = inter_data.real

    # Convert to integers
    inter_integers = convert_to_int(inter_data_real, 9, 2)

    inv_req_real = inverse_requantization(inter_integers,0,1800,0,44,1800,2200,45,444,2200,4095,445,511)

    # Calculate compression ratio
    # Number of bits needed for indexes varies according to compression ratio of decimation/demodulation
    no_bits_indexes = int(np.ceil(np.log2(compr_data.shape[2])))
    initial_number_of_bits = (rf_original_data.size)*12
    compressed_number_of_bits = 9*(no_samples) + no_bits_indexes*(no_indexes) + 9*(no_samples_imag) + no_bits_indexes*(no_indexes_imag)
    
    # Compression ratio
    compr_ratio = (compressed_number_of_bits/initial_number_of_bits)*100
    print("Requant_Dec_Dem_LAVA is {:.2f}% of the original data.".format(compr_ratio))
    
    

    # Beamform and plot the compressed image

    dataset_path = "../datasets/archive/archive_to_download/database/experiments/" + dataset + "/rf_dataset.hdf5"

    data_loader_obj = DataLoader(dataset_path)

    ### Specify Image settings and create corresponding object ###

    img_res = [400, 600]
    image_x_range = [-0.019, 0.019]
    image_z_range = [0.005, 0.05]

    db_range = 50

    LATERAL_PIXEL_DENSITY_DEFAULT = 5

    img_config = ImageSettings(image_x_range[0],
                               image_x_range[1],
                               image_z_range[0],
                               image_z_range[1],
                               LATERAL_PIXEL_DENSITY_DEFAULT,
                               data_loader_obj.transducer)

    ### Specify preprocessing parameters for RF data ###

    decimation_factor = 1
    interpolation_factor = 10

    ### Specify TX strategy and Apodization parameters ###

    start_time = 0
    correction_time_shift = 0

    alpha_fov_apod = 40

    ### Specify Sampling Frequency ###

    SAMPLING_FREQ = 20.832 * (10 ** 6)

    filters_params = [1 * 10 **6, 8 * 10 **6, 0.5 * 10 **6]

    bf = BFCartesianRealTime(data_loader_obj.f_sampling,
                             data_loader_obj.tx_strategy,
                             data_loader_obj.transducer,
                             decimation_factor,
                             interpolation_factor,
                             img_res,
                             img_config,
                             start_time=start_time,
                             correction_time_shift=correction_time_shift,
                             alpha_fov_apod=alpha_fov_apod,
                             bp_filter_params=filters_params,
                             envelope_detector='hilbert',
                             picmus_dataset=True)


    # Beamforming
    img_data = bf.beamform(inv_req_real, numba_active=True)

    _ = plot_image(np.abs(img_data), 
                   scatters_coords_xz=None,
                   elements_coords_xz=None,
                   framework='plotly',
                   title='Requant_Dec_Dem_LAVA: {:.2f}% of the original data'.format(compr_ratio),
                   image_x_range=image_x_range,
                   image_z_range=image_z_range,
                   db_range=db_range,
                   colorscale='Greys',
                   save_fig=True, 
                   show=True,
                   path_to_save='./Requant_Dec_Dem_LAVA')
    
    # Read info from file
    f_d = h5py.File("../datasets/archive/archive_to_download/database/experiments/"+dataset+"/"+dataset+"_expe_dataset_rf.hdf5", "r")

    attributes = f_d.attrs

    US = f_d['US']
    attributes = US.attrs

    US_DATASET0000 = US['US_DATASET0000']
    attributes = US_DATASET0000.attrs

    type_s = attributes.__getitem__('type')

    attributes = US_DATASET0000.attrs

    keys_1 = list(f_d.keys());

    US = f_d['US']

    US_DATASET0000 = US['US_DATASET0000']

    data = US_DATASET0000['data']

    imag_n = data['imag']

    real_n = data['real'][()]

    PRF = US_DATASET0000['PRF']

    angles = US_DATASET0000['angles']

    initial_time = US_DATASET0000['initial_time']

    modulation_frequency = US_DATASET0000['modulation_frequency']

    probe_geometry = US_DATASET0000['probe_geometry']

    sampling_frequency = US_DATASET0000['sampling_frequency']

    sound_speed = US_DATASET0000['sound_speed']

    # Create dataset suitable for calculating metrics

    imag_np = np.zeros((75, 128, 3328))

    # Creation of dataset

    out_filename = out_filename = './Requant_Dec_Dem_LAVA/Requant_Dec_Dem_LAVA_th{}_cr{}.hdf5'.format(threshold,compression_ratio)
    comp_file = h5py.File(out_filename,'w')
    comp_file.close()
    comp_file = h5py.File(out_filename,'a')

    # Complete the structure

    comp_file.attrs.create('version', [b'v.0.0.40'], dtype='|S9')

    US_path = 'US'
    US_group = comp_file.require_group(US_path)

    US_dataset_group = US_group.require_group('US_DATASET0000')

    US_dataset_group.attrs.create('type', [b"US"], dtype='|S2')
    US_dataset_group.attrs.create('subtype', [b"CPW"], dtype='|S3')
    US_dataset_group.attrs.create('signal_format', [b"RF"], dtype='|S2')
    US_dataset_group.attrs.create('name', [b"CIRS 040GSE Wires"], dtype='|S18')
    US_dataset_group.attrs.create('version', [b"v1.96"], dtype='|S6')
    US_dataset_group.attrs.create('creation_date', [b"2016/03/9 17:25:22.38"], dtype='|S22')

    # angles
    US_dataset_group.create_dataset( 'angles', data=angles)

    # PRF
    US_dataset_group.create_dataset( 'PRF', data=PRF)

    # initial_time
    US_dataset_group.create_dataset( 'initial_time', data=initial_time)

    # modulation frequency
    US_dataset_group.create_dataset( 'modulation_frequency', data=modulation_frequency)

    # probe geometry
    US_dataset_group.create_dataset( 'probe_geometry', data=probe_geometry)

    # sampling frequency
    US_dataset_group.create_dataset( 'sampling_frequency', data=sampling_frequency)

    # sound speed
    US_dataset_group.create_dataset( 'sound_speed', data=sound_speed)

    # imag data
    data_group = US_dataset_group.require_group('data')
    data_group.create_dataset('imag', data=imag_np)

    # real data
    rf_range_data = inv_req_real/2047.5
    rf_data = rf_range_data - 1
    data_group.create_dataset('real', data=rf_data)

    comp_file.close()
    

# 11

# ADC -> LAVA -> Requantization -> Decimation/Demodulation

# Interpolation/Modulation -> Inverse Requantization -> Interpolation

def LAVA_Requant_Dec_Dem():

    dataset = 'contrast_speckle'
    threshold = 2

    f_sampling = 20.832 * (10 ** 6)
    f_carrier = 5.2083 * (10 ** 6)

    requant_bits = 9

    # Read input integers
    with open('./BL_Coding/'+dataset+'/int_converted_values.npy', 'rb') as f:
        int_out_values = np.load(f)
    f.close()

    # LAVA
    kept_val_LAVA, indexes_LAVA = LAVA(threshold, int_out_values)
    
    no_samples = 0
    no_indexes = 0

    # Arrays for inverse LAVA
    range_of_samples = [range(0,int_out_values.shape[2])]
    reconstr_data_real = np.zeros((75, 128, int_out_values.shape[2]), dtype=np.float32)

    print('Starting Requantization.')

    # Requantization
    # REMEMBER FOR THE INDEXES WE NEED ONLY AS MANY BITS AS LOG2(TOTAL_NUMBER_OF_SAMPLES)
    # Convert individual channel acquisitions from list to array
    for i in range(len(kept_val_LAVA)):
        for k in range(len(kept_val_LAVA[0])):

            current_kept_chann = np.zeros((1,len(kept_val_LAVA[i][k])))
            current_kept_chann[0] = np.array(kept_val_LAVA[i][k], dtype=object)

            requant_real = requantization(requant_bits, current_kept_chann, 0,1800,0,44,1800,2200,45,444,2200,4095,445,511)

            #requant_floats = convert_to_float(requant_real, requant_bits)

            # Decimation and Demodulation
            # Find perfect divider for each length
            for n in range(16,0,-1):

                if (len(kept_val_LAVA[i][k])%n == 0):
                    compression_ratio = n
                    #print("Compression ratio: {}".format(compression_ratio))
                    break

            compr_data_values = []


            # Demodulate and decimate
            compr_data_values.append(demodulate_decimate(requant_real, f_sampling, f_carrier, compression_ratio))
            arr_data_values = compr_data_values[0][0]

            demod_values = np.zeros((1, arr_data_values.shape[0]),dtype=np.complex64)
            demod_values[0] = arr_data_values
            
            # Calculate number of samples
            no_samples = no_samples + arr_data_values.shape[0]
            no_indexes = no_indexes + len(indexes_LAVA[i][k])

            # Interpolate and modulate

            inter_data = interpolate_modulate(demod_values, f_sampling/compression_ratio, f_carrier, compression_ratio)

            inter_data_real = inter_data.real

            # Inverse Requantization

            # Convert to integers
            #inter_integers = convert_to_int(inter_data_real, requant_bits, 2)

            inv_req_real = inverse_requantization(inter_data_real,0,1800,0,44,1800,2200,45,444,2200,4095,445,511)

            # Inverse LAVA / Interpolation
            reconstr_data_real[i][k] = np.interp(range_of_samples, indexes_LAVA[i][k], inv_req_real[0])
            sys.stdout.write('\r')
            sys.stdout.write('Done with Acq {} Chan {}.'.format(i, k))
            
    # Calculate compression ratio
    # Transfer demodulated data and all the indexes as they are after LAVA
    initial_number_of_bits = (int_out_values.size)*12
    compressed_number_of_bits = requant_bits*2*(no_samples) + 12*(no_indexes)
    
    # Compression ratio
    compr_ratio = (compressed_number_of_bits/initial_number_of_bits)*100
    print("LAVA_Requant_Dec_Dem is {:.2f}% of the original data.".format(compr_ratio))

    # Beamform and plot the compressed image

    dataset_path = "../datasets/archive/archive_to_download/database/experiments/" + dataset + "/rf_dataset.hdf5"

    data_loader_obj = DataLoader(dataset_path)

    ### Specify Image settings and create corresponding object ###

    img_res = [400, 600]
    image_x_range = [-0.019, 0.019]
    image_z_range = [0.005, 0.05]

    db_range = 50

    LATERAL_PIXEL_DENSITY_DEFAULT = 5

    img_config = ImageSettings(image_x_range[0],
                               image_x_range[1],
                               image_z_range[0],
                               image_z_range[1],
                               LATERAL_PIXEL_DENSITY_DEFAULT,
                               data_loader_obj.transducer)

    ### Specify preprocessing parameters for RF data ###

    decimation_factor = 1
    interpolation_factor = 10

    ### Specify TX strategy and Apodization parameters ###

    start_time = 0
    correction_time_shift = 0

    alpha_fov_apod = 40

    ### Specify Sampling Frequency ###

    SAMPLING_FREQ = 20.832 * (10 ** 6)

    filters_params = [1 * 10 **6, 8 * 10 **6, 0.5 * 10 **6]

    bf = BFCartesianRealTime(data_loader_obj.f_sampling,
                             data_loader_obj.tx_strategy,
                             data_loader_obj.transducer,
                             decimation_factor,
                             interpolation_factor,
                             img_res,
                             img_config,
                             start_time=start_time,
                             correction_time_shift=correction_time_shift,
                             alpha_fov_apod=alpha_fov_apod,
                             bp_filter_params=filters_params,
                             envelope_detector='hilbert',
                             picmus_dataset=True)


    # Beamforming
    img_data = bf.beamform(reconstr_data_real, numba_active=True)

    _ = plot_image(np.abs(img_data), 
                   scatters_coords_xz=None,
                   elements_coords_xz=None,
                   framework='plotly',
                   title='LAVA_Requant_Dec_Dem: {:.2f}% of the original data'.format(compr_ratio),
                   image_x_range=image_x_range,
                   image_z_range=image_z_range,
                   db_range=db_range,
                   colorscale='Greys',
                   save_fig=True, 
                   show=True,
                   path_to_save='./LAVA_Requant_Dec_Dem')  
    
    # Read info from file
    f_d = h5py.File("../datasets/archive/archive_to_download/database/experiments/"+dataset+"/"+dataset+"_expe_dataset_rf.hdf5", "r")

    attributes = f_d.attrs

    US = f_d['US']
    attributes = US.attrs

    US_DATASET0000 = US['US_DATASET0000']
    attributes = US_DATASET0000.attrs

    type_s = attributes.__getitem__('type')

    attributes = US_DATASET0000.attrs

    keys_1 = list(f_d.keys());

    US = f_d['US']

    US_DATASET0000 = US['US_DATASET0000']

    data = US_DATASET0000['data']

    imag_n = data['imag']

    real_n = data['real'][()]

    PRF = US_DATASET0000['PRF']

    angles = US_DATASET0000['angles']

    initial_time = US_DATASET0000['initial_time']

    modulation_frequency = US_DATASET0000['modulation_frequency']

    probe_geometry = US_DATASET0000['probe_geometry']

    sampling_frequency = US_DATASET0000['sampling_frequency']

    sound_speed = US_DATASET0000['sound_speed']

    # Create dataset suitable for calculating metrics

    imag_np = np.zeros((75, 128, 3328))

    # Creation of dataset

    out_filename = out_filename = './LAVA_Requant_Dec_Dem/LAVA_Requant_Dec_Dem_th{}.hdf5'.format(threshold)
    comp_file = h5py.File(out_filename,'w')
    comp_file.close()
    comp_file = h5py.File(out_filename,'a')

    # Complete the structure

    comp_file.attrs.create('version', [b'v.0.0.40'], dtype='|S9')

    US_path = 'US'
    US_group = comp_file.require_group(US_path)

    US_dataset_group = US_group.require_group('US_DATASET0000')

    US_dataset_group.attrs.create('type', [b"US"], dtype='|S2')
    US_dataset_group.attrs.create('subtype', [b"CPW"], dtype='|S3')
    US_dataset_group.attrs.create('signal_format', [b"RF"], dtype='|S2')
    US_dataset_group.attrs.create('name', [b"CIRS 040GSE Wires"], dtype='|S18')
    US_dataset_group.attrs.create('version', [b"v1.96"], dtype='|S6')
    US_dataset_group.attrs.create('creation_date', [b"2016/03/9 17:25:22.38"], dtype='|S22')

    # angles
    US_dataset_group.create_dataset( 'angles', data=angles)

    # PRF
    US_dataset_group.create_dataset( 'PRF', data=PRF)

    # initial_time
    US_dataset_group.create_dataset( 'initial_time', data=initial_time)

    # modulation frequency
    US_dataset_group.create_dataset( 'modulation_frequency', data=modulation_frequency)

    # probe geometry
    US_dataset_group.create_dataset( 'probe_geometry', data=probe_geometry)

    # sampling frequency
    US_dataset_group.create_dataset( 'sampling_frequency', data=sampling_frequency)

    # sound speed
    US_dataset_group.create_dataset( 'sound_speed', data=sound_speed)

    # imag data
    data_group = US_dataset_group.require_group('data')
    data_group.create_dataset('imag', data=imag_np)

    # real data
    rf_range_data = reconstr_data_real/2047.5
    rf_data = rf_range_data - 1
    data_group.create_dataset('real', data=rf_data)

    comp_file.close()
# 12
# ADC -> Requantization -> LAVA -> Decimation/Demodulation

# Interpolation/Modulation -> Interpolation -> Inverse Requantization

def Requant_LAVA_Dec_Dem():

    dataset = 'contrast_speckle'
    threshold = 14

    f_sampling = 20.832 * (10 ** 6)
    f_carrier = 5.2083 * (10 ** 6)

    compression_ratio = 8

    # Get input data
    rf_original_data = get_original_rf_data("../datasets/archive/archive_to_download/database/experiments/" + dataset + "/rf_dataset.hdf5")

    with open('./BL_Coding/'+dataset+'/int_converted_values.npy', 'rb') as f:
        int_out_values = np.load(f)
    f.close()

    # Arrays for inverse LAVA
    range_of_samples = [range(0,int_out_values.shape[2])]
    reconstr_data_real = np.zeros((75, 128, int_out_values.shape[2]), dtype=np.float32)

    # Requantize
    with open('./Requantization/'+dataset+'_forw_requant_9_data.npy', 'rb') as f:
        requant_data = np.load(f)
    f.close()

    # LAVA
    kept_val_LAVA, indexes_LAVA = LAVA(threshold, requant_data)
    
    no_samples = 0
    no_indexes = 0


    # Convert individual channel acquisitions from list to array
    for i in range(len(kept_val_LAVA)):
        for k in range(len(kept_val_LAVA[0])):

            # Find perfect divider for each length
            for n in range(16,0,-1):

                if (len(kept_val_LAVA[i][k])%n == 0):
                    compression_ratio = n
                    #print("Compression ratio: {}".format(compression_ratio))
                    break

            compr_data_values = []

            current_kept_chann = np.zeros((1,len(kept_val_LAVA[i][k])))
            current_kept_chann[0] = np.array(kept_val_LAVA[i][k], dtype=object)

            # Convert to float
            current_kept_float = convert_to_float(current_kept_chann, 9)

            # Demodulate and decimate
            compr_data_values.append(demodulate_decimate(current_kept_float, f_sampling, f_carrier, compression_ratio))
            arr_data_values = compr_data_values[0][0]

            demod_values = np.zeros((1, arr_data_values.shape[0]),dtype=np.complex64)
            demod_values[0] = arr_data_values
            
            # Calculate number of samples
            no_samples = no_samples + arr_data_values.shape[0]
            no_indexes = no_indexes + len(indexes_LAVA[i][k])            

            # Interpolate and modulate

            inter_data = interpolate_modulate(demod_values, f_sampling/compression_ratio, f_carrier, compression_ratio)

            inter_data_real = inter_data.real

            # Inverse LAVA / Interpolation
            reconstr_data_real[i][k] = np.interp(range_of_samples, indexes_LAVA[i][k], inter_data_real[0])       

    # Convert to integers
    reconstr_data_integers = convert_to_int(reconstr_data_real, 9, 2)

    # Inverse Requantization
    inv_req_real = inverse_requantization(reconstr_data_integers,0,1800,0,44,1800,2200,45,444,2200,4095,445,511)

    # Calculate compression ratio
    # Transfer demodulated data and all the indexes as they are after LAVA
    initial_number_of_bits = (rf_original_data.size)*12
    compressed_number_of_bits = 9*2*(no_samples) + 12*(no_indexes)
    
    # Compression ratio
    compr_ratio = (compressed_number_of_bits/initial_number_of_bits)*100
    print("Requant_LAVA_Dec_Dem is {:.2f}% of the original data.".format(compr_ratio))    

    # Beamform and plot the compressed image

    dataset_path = "../datasets/archive/archive_to_download/database/experiments/" + dataset + "/rf_dataset.hdf5"

    data_loader_obj = DataLoader(dataset_path)

    ### Specify Image settings and create corresponding object ###

    img_res = [400, 600]
    image_x_range = [-0.019, 0.019]
    image_z_range = [0.005, 0.05]

    db_range = 50

    LATERAL_PIXEL_DENSITY_DEFAULT = 5

    img_config = ImageSettings(image_x_range[0],
                               image_x_range[1],
                               image_z_range[0],
                               image_z_range[1],
                               LATERAL_PIXEL_DENSITY_DEFAULT,
                               data_loader_obj.transducer)

    ### Specify preprocessing parameters for RF data ###

    decimation_factor = 1
    interpolation_factor = 10

    ### Specify TX strategy and Apodization parameters ###

    start_time = 0
    correction_time_shift = 0

    alpha_fov_apod = 40

    ### Specify Sampling Frequency ###

    SAMPLING_FREQ = 20.832 * (10 ** 6)

    filters_params = [1 * 10 **6, 8 * 10 **6, 0.5 * 10 **6]

    bf = BFCartesianRealTime(data_loader_obj.f_sampling,
                             data_loader_obj.tx_strategy,
                             data_loader_obj.transducer,
                             decimation_factor,
                             interpolation_factor,
                             img_res,
                             img_config,
                             start_time=start_time,
                             correction_time_shift=correction_time_shift,
                             alpha_fov_apod=alpha_fov_apod,
                             bp_filter_params=filters_params,
                             envelope_detector='hilbert',
                             picmus_dataset=True)


    # Beamforming
    img_data = bf.beamform(inv_req_real, numba_active=True)

    _ = plot_image(np.abs(img_data), 
                   scatters_coords_xz=None,
                   elements_coords_xz=None,
                   framework='plotly',
                   title='Requant_LAVA_Dec_Dem: {:.2f}% of the original data'.format(compr_ratio),
                   image_x_range=image_x_range,
                   image_z_range=image_z_range,
                   db_range=db_range,
                   colorscale='Greys',
                   save_fig=True, 
                   show=True,
                   path_to_save='./Requant_LAVA_Dec_Dem')
    
    # Read info from file
    f_d = h5py.File("../datasets/archive/archive_to_download/database/experiments/"+dataset+"/"+dataset+"_expe_dataset_rf.hdf5", "r")

    attributes = f_d.attrs

    US = f_d['US']
    attributes = US.attrs

    US_DATASET0000 = US['US_DATASET0000']
    attributes = US_DATASET0000.attrs

    type_s = attributes.__getitem__('type')

    attributes = US_DATASET0000.attrs

    keys_1 = list(f_d.keys());

    US = f_d['US']

    US_DATASET0000 = US['US_DATASET0000']

    data = US_DATASET0000['data']

    imag_n = data['imag']

    real_n = data['real'][()]

    PRF = US_DATASET0000['PRF']

    angles = US_DATASET0000['angles']

    initial_time = US_DATASET0000['initial_time']

    modulation_frequency = US_DATASET0000['modulation_frequency']

    probe_geometry = US_DATASET0000['probe_geometry']

    sampling_frequency = US_DATASET0000['sampling_frequency']

    sound_speed = US_DATASET0000['sound_speed']

    # Create dataset suitable for calculating metrics

    imag_np = np.zeros((75, 128, 3328))

    # Creation of dataset

    out_filename = out_filename = './Requant_LAVA_Dec_Dem/Requant_LAVA_Dec_Dem_th{}.hdf5'.format(threshold)
    comp_file = h5py.File(out_filename,'w')
    comp_file.close()
    comp_file = h5py.File(out_filename,'a')

    # Complete the structure

    comp_file.attrs.create('version', [b'v.0.0.40'], dtype='|S9')

    US_path = 'US'
    US_group = comp_file.require_group(US_path)

    US_dataset_group = US_group.require_group('US_DATASET0000')

    US_dataset_group.attrs.create('type', [b"US"], dtype='|S2')
    US_dataset_group.attrs.create('subtype', [b"CPW"], dtype='|S3')
    US_dataset_group.attrs.create('signal_format', [b"RF"], dtype='|S2')
    US_dataset_group.attrs.create('name', [b"CIRS 040GSE Wires"], dtype='|S18')
    US_dataset_group.attrs.create('version', [b"v1.96"], dtype='|S6')
    US_dataset_group.attrs.create('creation_date', [b"2016/03/9 17:25:22.38"], dtype='|S22')

    # angles
    US_dataset_group.create_dataset( 'angles', data=angles)

    # PRF
    US_dataset_group.create_dataset( 'PRF', data=PRF)

    # initial_time
    US_dataset_group.create_dataset( 'initial_time', data=initial_time)

    # modulation frequency
    US_dataset_group.create_dataset( 'modulation_frequency', data=modulation_frequency)

    # probe geometry
    US_dataset_group.create_dataset( 'probe_geometry', data=probe_geometry)

    # sampling frequency
    US_dataset_group.create_dataset( 'sampling_frequency', data=sampling_frequency)

    # sound speed
    US_dataset_group.create_dataset( 'sound_speed', data=sound_speed)

    # imag data
    data_group = US_dataset_group.require_group('data')
    data_group.create_dataset('imag', data=imag_np)

    # real data
    rf_range_data = inv_req_real/2047.5
    rf_data = rf_range_data - 1
    data_group.create_dataset('real', data=rf_data)

    comp_file.close()
# Flow to run all the algorithms

# For Decimation-Demodulation -> Re-quantization -> LAVA see next cells

# If a progress feedback is required, uncomment the "sys.stdout.write" commands
# in the respective functions

LAVA_Requant_Dec_Dem()
LAVA_Dec_Dem_Requant()
Dec_Dem_LAVA_Requant()
Requant_Dec_Dem_LAVA()
Requant_LAVA_Dec_Dem()
# Decimation-Demodulation -> Re-quantization -> LAVA
# def Dec_Dem_Requant_LAVA(compr_ratio, req_bits, thres)

Dec_Dem_Requant_LAVA(16, 9, 10)
Dec_Dem_Requant_LAVA(16, 8, 8)
Dec_Dem_Requant_LAVA(16, 7, 6)
Dec_Dem_Requant_LAVA(16, 6, 5)
Dec_Dem_Requant_LAVA(16, 5, 4)
Dec_Dem_Requant_LAVA(16, 4, 2)
Dec_Dem_Requant_LAVA(16, 3, 2)
Dec_Dem_Requant_LAVA(16, 2, 1)
Dec_Dem_Requant_LAVA(16, 1, 1)

Dec_Dem_Requant_LAVA(32, 9, 10)
Dec_Dem_Requant_LAVA(32, 8, 8)
Dec_Dem_Requant_LAVA(32, 7, 6)
Dec_Dem_Requant_LAVA(32, 6, 5)
Dec_Dem_Requant_LAVA(32, 5, 4)
Dec_Dem_Requant_LAVA(32, 4, 2)
Dec_Dem_Requant_LAVA(32, 3, 2)
Dec_Dem_Requant_LAVA(32, 2, 1)
Dec_Dem_Requant_LAVA(32, 1, 1)<jupyter_output>RF data shape:  (75, 128, 3328)
Compressed data shape:  (75, 128, 208)
Done with channel 127 of acq 74Done with LAVA compression.
Done with channel 127 of acq 74Done with LAVA compression.
Dec_Dem_Requant_LAVA is 0.015477263621794872% of the original data.
The highest resolution for the system is:  (633, 205)
Delays precalculation...
TX strategy: plane waves
Number of plane waves:  75
Maximum angle:  16.0 °
Apodization precalculation...
Beamforming...
 
Time of execution: 32.24982213973999 seconds
BF Final dB range (-50.0,0.0)
RF data shape:  (75, 128, 3328)
Compressed data shape:  (75, 128, 104)
Done with channel 127 of acq 74Done with LAVA compression.
Done with channel 127 of acq 74Done with LAVA compression.
Dec_Dem_Requant_LAVA is 0.0026163736979166667% of the original data.
The highest resolution for the system is:  (633, 205)
Delays precalculation...
TX strategy: plane waves
Number of plane waves:  75
Maximum angle:  16.0 °
Apodization precalculation...
Beamforming...
 
Time of [...]
{'fraction_non_alphanumeric': 0.10599536838493867, 'fraction_numerical': 0.03688137919204049, 'mean_word_length': 2.840316205533597, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 17, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '171024', 'license': 'Apache-2.0', 'n_tokens_mistral': 21267, 'n_tokens_neox': 19248, 'n_words': 3204, 'path': 'Simulations/Combinations of 3.ipynb', 'repo_name': 'alksarioglou/compression_algorithms_fpga_ultrasound', 'size': '58295'}