import pickle

df_loc = "output/adaptive/vConv2d.pickle"
with open(df_loc, "rb") as f:
    df = pickle.load(f)

debug_outputs = df["debug_outputs"]
message_by_loop = {}

for i, output in enumerate(debug_outputs):
    for line in output:
        try:
            if line.startswith("Start recovery"):
                log = (i, "R")
                for loop_id in message_by_loop.keys():
                    message_by_loop[loop_id].append(log)
                continue
            loop_index_string = "estimation for loop"
            loc = line.find(loop_index_string)
            assert(loc > -1)
            loop_index = loc + len(loop_index_string) + 1
            loop_id = line[loop_index:].split()[0]

            update_string = "is updated to"
            loc = line.find(update_string)
            assert(loc > -1)
            val_index = loc + len(update_string) + 1
            updated_val = line[val_index:].split()[0]

            if line.startswith("(recovery)"):
                updated_val = "!" + updated_val

            if loop_id not in message_by_loop:
                message_by_loop[loop_id] = []
            
            log = (i, updated_val)
            message_by_loop[loop_id].append(log)

        except:
            print("incomplete string:", line)

for key in sorted(message_by_loop.keys()):
    # print(key, message_by_loop[key])
    outputs = []
    current_iter = -1
    for log in message_by_loop[key]:
        i, val = log
        while current_iter < i:
            current_iter += 1
            outputs.append(f"({current_iter})")
        outputs.append(val)
    print(f"{key}:", "  ".join(outputs))
