import numpy as np
import csv

# Exports the network inputs to a csv file.
def export_inputs(path, inputs, filename="inputs.csv"):
        
    # Open a csv file.
    with open(path + filename, "w") as file:
        writer = csv.writer(file, lineterminator="\n")
    
        # Refactor and write the inputs.
        for input_layer in inputs:
            temp = map(int, np.asfarray(input_layer.split(",")[1:])*256.0)
            writer.writerow(list(temp))
    
# Exports the network weights to a csv file.
def export_weights(path, dnn):
    
    # Iterate through all of the weight matrices in the network.
    for matrix in range(dnn.depth-1):
        
        # Generate the filename for the current weight matrix.
        filename = "weights_" + str(matrix) + ".csv"    
    
        # Open a csv file.
        with open(path + filename, "w") as file:
            writer = csv.writer(file, lineterminator="\n")

            # Iterate through all of the layers
            layer = range(dnn.layers[matrix])
            for node in layer:
                
                # Write the weight matrix.
                writer.writerow(list((dnn.weights[matrix].T)[node]))           
            
# Export the network outputs to a csv file.
def export_outputs(path, outputs, filename="outputs.csv"):
    
    # Open a csv file.
    with open(path + filename, "w") as file:
        writer = csv.writer(file, lineterminator="\n")

        # Iterate through all of the outputs.
        for output_layer in outputs:
            temp = (output_layer.T)[0].T
            writer.writerow(list(temp))
            