import csv


def read_bodyplan(file):
    """Read body plan from csv file.

    Args:
        file: path to file containing a bodyplan
    Returns:
         A list of L layers representing the model structure with layer
         keys: "layer", "n", "activation", "lreg", "regval", and "desc"
         indicating the layer index, integer number of units in layer, a string
         name of the activation function, the value (1 or 2) of the L-norm
         regularization to employ, the float regularization constant, and a
         string description of layer.
    """
    #init bodyplan as empty list
    bodyplan = []

    with open(file, 'r') as csvfile:
        reader = csv.reader(csvfile)
        keys = list(next(reader)) #get headers
        for line in reader:
            layer = {}
            layer["layer"] = int(line[keys.index("layer")])
            layer["n"] = int(line[keys.index("n")])
            layer["activation"] = str(line[keys.index("activation")])
            if "regval" in keys:
                layer["regval"] = float(line[keys.index("regval")])
            else:
                layer["regval"] = float(0) #default regularization parameter
            if "lreg" in keys:
                layer["lreg"] = int(line[keys.index("lreg")])
            else:
                layer["lreg"] = int(1) #default regularization is L1
            if "desc" in keys:
                layer["desc"] = str(line[keys.index("desc")])
            else:
                layer["desc"] = 'fully connected'
            bodyplan.append(layer)
    return bodyplan