#!/usr/bin/env python

import numpy as np, os, sys
from get_sepsis_score import load_sepsis_model, get_sepsis_score

def load_challenge_data(file):
    with open(file, 'r') as f:
        header = f.readline().strip()
        column_names = header.split('|')
        data = np.loadtxt(f, delimiter='|')

    # Ignore SepsisLabel column if present.
    if column_names[-1] == 'SepsisLabel':
        column_names = column_names[:-1]
        data = data[:, :-1]

    return data

def save_challenge_predictions(file, scores, labels):
    with open(file, 'w') as f:
        f.write('PredictedProbability|PredictedLabel\n')
        for (s, l) in zip(scores, labels):
            f.write('%g|%d\n' % (s, l))

if __name__ == '__main__':
    # Parse arguments.
    if len(sys.argv) != 3:
        raise Exception('Include the input and output directories as arguments, e.g., python driver.py input output.')

    input_directory = sys.argv[1]
    output_directory = sys.argv[2]

    # Find files.
    files = []
    for f in os.listdir(input_directory):
        if os.path.isfile(os.path.join(input_directory, f)) and not f.lower().startswith('.') and f.lower().endswith('psv'):
            files.append(f)

    if not os.path.isdir(output_directory):
        os.mkdir(output_directory)

    # Load model.
    model = load_sepsis_model()
    print(model)

    # Iterate over files.
    for f in files:
        # Load data.
        input_file = os.path.join(input_directory, f)
        data = load_challenge_data(input_file)

#        print(type(data))

        # Make predictions.
        num_rows = len(data)
        scores = np.zeros(num_rows)
        labels = np.zeros(num_rows)
        for t in range(num_rows):
            current_data = data[:t+1]
            current_score, current_label = get_sepsis_score(current_data, model)
            scores[t] = current_score
            labels[t] = current_label

        # Save results.
        output_file = os.path.join(output_directory, f)
        save_challenge_predictions(output_file, scores, labels)

[(35, 'os.listdir', 'os.listdir', False, 'import numpy as np, os, sys\n'), (43, 'get_sepsis_score.load_sepsis_model', 'load_sepsis_model', False, 'from get_sepsis_score import load_sepsis_model, get_sepsis_score\n'), (10, 'numpy.loadtxt', 'np.loadtxt', True, 'import numpy as np, os, sys\n'), (39, 'os.path.isdir', 'os.path.isdir', False, 'import numpy as np, os, sys\n'), (40, 'os.mkdir', 'os.mkdir', False, 'import numpy as np, os, sys\n'), (49, 'os.path.join', 'os.path.join', False, 'import numpy as np, os, sys\n'), (56, 'numpy.zeros', 'np.zeros', True, 'import numpy as np, os, sys\n'), (57, 'numpy.zeros', 'np.zeros', True, 'import numpy as np, os, sys\n'), (65, 'os.path.join', 'os.path.join', False, 'import numpy as np, os, sys\n'), (60, 'get_sepsis_score.get_sepsis_score', 'get_sepsis_score', False, 'from get_sepsis_score import load_sepsis_model, get_sepsis_score\n'), (36, 'os.path.join', 'os.path.join', False, 'import numpy as np, os, sys\n')]