#!/bin/bash

BASE_PATH="/home/andreas/workspace/private/ma-sw-results/outputs/"

# Copy BP param exploration results

function post_process_LERs() {
    local filename="$1"

    python3 -c "
import pandas as pd
import numpy as np

df = pd.read_csv('${filename}')
df['LER_per_round'] = 1 - (1 - df['LER'])**(1/12)
df['num_errors'] = df['num_trials'] * df['LER']
df.to_csv('${filename}', index=False)
"       
}

i=1
sp="/-\|"

# echo "Copying BP param exploration results..."
# echo -n ' '
# for decoder in "WindowingSyndromeMinSumDecoder" "WindowingSyndromeSpaDecoder"; do
#     for max_iter in 32 200 5000; do
#         for pass_soft_info in "True" "False"; do
#             for F in 1 2 3; do
#                 for W in 3 4 5; do
#                     SRC_PATH="${BASE_PATH}+rust_exp=soft_v_hard_bp,decoder.class_name=${decoder},decoder.max_iter=${max_iter},decoder.pass_soft_info=${pass_soft_info},system.F=${F},system.W=${W}/"
#                     LATEST_RESULTS_DIR=$(ls -t ${SRC_PATH} | head -1)
#                     SRC_FILE="${SRC_PATH}/${LATEST_RESULTS_DIR}/LERs.csv"
#                     DEST_DIR="res/sim/WF/${decoder}/max_iter_${max_iter}/pass_soft_info_${pass_soft_info}/F_${F}/W_${W}/"
#                     mkdir -p ${DEST_DIR}
#                     DEST_FILE="${DEST_DIR}/LERs.csv"
#                     cp ${SRC_FILE} ${DEST_FILE}
#                     post_process_LERs ${DEST_FILE}
#                     printf "\b${sp:i++%${#sp}:1}"
#                 done
#             done
#         done
#     done
# done
#
# # Copy BPGD param exploration results
#
# echo -e "\rCopying BPGD param exploration results..."
# echo -n ' '
# for max_iter in 32 200 5000; do
#     for pass_soft_info in "True" "False"; do
#         for F in 1 2 3; do
#             for W in 3 4 5; do
#                 SRC_PATH="${BASE_PATH}/+rust_exp=soft_v_hard_bpgd,decoder.class_name=WindowingSyndromeSpaGdDecoder,decoder.max_iter=${max_iter},decoder.pass_soft_info=${pass_soft_info},system.F=${F},system.W=${W}/"
#                 LATEST_RESULTS_DIR=$(ls -t ${SRC_PATH} | head -1)
#                 SRC_FILE="${SRC_PATH}/${LATEST_RESULTS_DIR}/LERs.csv"
#                 DEST_DIR="res/sim/WF/WindowingSyndromeSpaGdDecoder/max_iter_${max_iter}/pass_soft_info_${pass_soft_info}/F_${F}/W_${W}/"
#                 mkdir -p ${DEST_DIR}
#                 DEST_FILE="${DEST_DIR}/LERs.csv"
#                 cp ${SRC_FILE} ${DEST_FILE}
#                 post_process_LERs ${DEST_FILE}
#                 printf "\b${sp:i++%${#sp}:1}"
#             done
#         done
#     done
# done
#
# # Copy BP over max iter. results
#
# echo -e "\rCopying BP over max. iter. results..."
# echo -n ' '
# for decoder in "WindowingSyndromeMinSumDecoder" "WindowingSyndromeSpaDecoder"; do
#     for p in 0.001 0.0025 0.004; do
#         for pass_soft_info in "True" "False"; do
#             for F in 1 2 3; do
#                 for W in 3 4 5; do
#                     SRC_PATH="${BASE_PATH}+rust_exp=max_iter_bp,decoder.class_name=${decoder},decoder.pass_soft_info=${pass_soft_info},simulation.phy_err_rate=${p},system.F=${F},system.W=${W}/"
#                     LATEST_RESULTS_DIR=$(ls -t ${SRC_PATH} | head -1)
#                     SRC_FILE="${SRC_PATH}/${LATEST_RESULTS_DIR}/LERs.csv"
#                     DEST_DIR="res/sim/max_iter/${decoder}/p_${p}/pass_soft_info_${pass_soft_info}/F_${F}/W_${W}"
#                     mkdir -p ${DEST_DIR}
#                     DEST_FILE="${DEST_DIR}/LERs.csv"
#                     cp ${SRC_FILE} ${DEST_FILE}
#                     post_process_LERs ${DEST_FILE}
#                     printf "\b${sp:i++%${#sp}:1}"
#                 done
#             done
#         done
#     done
# done
#
# # Copy BPGD over max iter. results
#
# echo -e "\rCopying BPGD over max. iter. results..."
# echo -n ' '
# for p in 0.001 0.0025 0.004; do
#     for pass_soft_info in "True" "False"; do
#         for F in 1 2 3; do
#             for W in 3 4 5; do
#                 SRC_PATH="${BASE_PATH}+rust_exp=max_iter_bpgd,decoder.class_name=WindowingSyndromeSpaGdDecoder,decoder.pass_soft_info=${pass_soft_info},simulation.phy_err_rate=${p},system.F=${F},system.W=${W}/"
#                 LATEST_RESULTS_DIR=$(ls -t ${SRC_PATH} | head -1)
#                 SRC_FILE="${SRC_PATH}/${LATEST_RESULTS_DIR}/LERs.csv"
#                 DEST_DIR="res/sim/max_iter/WindowingSyndromeSpaGdDecoder/p_${p}/pass_soft_info_${pass_soft_info}/F_${F}/W_${W}"
#                 mkdir -p ${DEST_DIR}
#                 DEST_FILE="${DEST_DIR}/LERs.csv"
#                 cp ${SRC_FILE} ${DEST_FILE}
#                 post_process_LERs ${DEST_FILE}
#                 printf "\b${sp:i++%${#sp}:1}"
#             done
#         done
#     done
# done
#
# # Copy BP over max iter. results
#
# echo -e "\rCopying one-shot simulation results..."
# echo -n ' '
# for decoder in "SyndromeMinSumDecoder" "SyndromeSpaDecoder" "SyndromeSpaGdDecoder"; do
#     for max_iter in 32 200 5000; do
#         SRC_PATH="${BASE_PATH}+rust_exp=whole_bp_bpgd,decoder.class_name=${decoder},decoder.max_iter=${max_iter},system.F=1,system.W=5/"
#         LATEST_RESULTS_DIR=$(ls -t ${SRC_PATH} | head -1)
#         SRC_FILE="${SRC_PATH}/${LATEST_RESULTS_DIR}/LERs.csv"
#         DEST_DIR="res/sim/one-shot/${decoder}/max_iter_${max_iter}/"
#         mkdir -p ${DEST_DIR}
#         DEST_FILE="${DEST_DIR}/LERs.csv"
#         cp ${SRC_FILE} ${DEST_FILE}
#         post_process_LERs ${DEST_FILE}
#         printf "\b${sp:i++%${#sp}:1}"
#     done
# done

# Copy whole BP over max iter. results

echo -e "\rCopying whole over max_iter simulation results..."
echo -n ' '
for decoder in "SyndromeMinSumDecoder"; do
    for p in 0.001 0.0025 0.004; do
        SRC_PATH="${BASE_PATH}+rust_exp=max_iter_bp,decoder.class_name=${decoder},simulation.phy_err_rate=${p}/"
        LATEST_RESULTS_DIR=$(ls -t ${SRC_PATH} | head -1)
        SRC_FILE="${SRC_PATH}/${LATEST_RESULTS_DIR}/LERs.csv"
        DEST_DIR="res/sim/max_iter/${decoder}/p_${p}/"
        mkdir -p ${DEST_DIR}
        DEST_FILE="${DEST_DIR}/LERs.csv"
        cp ${SRC_FILE} ${DEST_FILE}
        post_process_LERs ${DEST_FILE}
        printf "\b${sp:i++%${#sp}:1}"
    done
done

# # Copy BPGD decimation passing
#
# echo -e "\rCopying BPGD param exploration results..."
# echo -n ' '
# for max_iter in 32 200 5000; do
#     for F in 1 2 3; do
#         for W in 3 4 5; do
#             SRC_PATH="${BASE_PATH}+rust_exp=soft_v_hard_bpgd_pass_channel,decoder.class_name=WindowingSyndromeSpaGdDecoder,decoder.max_iter=${max_iter},decoder.pass_soft_info=True,system.F=${F},system.W=${W}"
#             LATEST_RESULTS_DIR=$(ls -t ${SRC_PATH} | head -1)
#             SRC_FILE="${SRC_PATH}/${LATEST_RESULTS_DIR}/LERs.csv"
#             DEST_DIR="res/sim/WF/WindowingSyndromeSpaGdDecoderPassDecimation/max_iter_${max_iter}/pass_soft_info_True/F_${F}/W_${W}/"
#             mkdir -p ${DEST_DIR}
#             DEST_FILE="${DEST_DIR}/LERs.csv"
#             cp ${SRC_FILE} ${DEST_FILE}
#             post_process_LERs ${DEST_FILE}
#             printf "\b${sp:i++%${#sp}:1}"
#         done
#     done
# done

# Copy BPGD with decimation info passing over max iter. results

# echo -e "\rCopying BPGD over max. iter. results..."
# echo -n ' '
# for pass_soft_info in "True" "False"; do
#     for F in 1 2 3; do
#         for W in 3 4 5; do
#             SRC_PATH="${BASE_PATH}+rust_exp=max_iter_bpgd_pass_channel,decoder.class_name=WindowingSyndromeSpaGdDecoder,decoder.pass_soft_info=${pass_soft_info},simulation.phy_err_rate=0.0025,system.F=${F},system.W=${W}/"
#             LATEST_RESULTS_DIR=$(ls -t ${SRC_PATH} | head -1)
#             SRC_FILE="${SRC_PATH}/${LATEST_RESULTS_DIR}/LERs.csv"
#             DEST_DIR="res/sim/max_iter/WindowingSyndromeSpaGdDecoderPassDecimation/p_0.0025/pass_soft_info_${pass_soft_info}/F_${F}/W_${W}"
#             mkdir -p ${DEST_DIR}
#             DEST_FILE="${DEST_DIR}/LERs.csv"
#             cp ${SRC_FILE} ${DEST_FILE}
#             post_process_LERs ${DEST_FILE}
#             printf "\b${sp:i++%${#sp}:1}"
#         done
#     done
# done
