import csv
import re


def parse_logs(log_path, output_log_file):
    """
    Parses our training and validation logs produced from a Caffe training run in order to return
    them in a way we can work with.
    """
    training_iters = []
    training_loss = []
    training_accuracy = []
    for line in csv.reader(open(output_log_file + ".train"), delimiter="\t", skipinitialspace=True):
        if re.search("Iters", str(line)):
            continue

        training_iters.append(int(float(line[0])))
        training_accuracy.append(float(line[3]))
        training_loss.append(float(line[4]))

    validation_iters = []
    validation_loss = []
    validation_accuracy = []
    for line in csv.reader(open(output_log_file + ".validate"), delimiter="\t",
                            skipinitialspace=True):
        if re.search("Iters", str(line)):
            continue

        validation_iters.append(int(float(line[0])))
        validation_accuracy.append(float(line[3]))
        validation_loss.append(float(line[4]))

    return (
        {
            "iters": training_iters,
            "loss": training_loss,
            "accuracy": training_accuracy
        }, {
            "iters": validation_iters,
            "loss": validation_loss,
            "accuracy": validation_accuracy
        }
    )