#!/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)

[((1015, 1042), 'os.listdir', 'os.listdir', (['input_directory'], {}), '(input_directory)\n', (1025, 1042), False, 'import numpy as np, os, sys\n'), ((1308, 1327), 'get_sepsis_score.load_sepsis_model', 'load_sepsis_model', ([], {}), '()\n', (1325, 1327), False, 'from get_sepsis_score import load_sepsis_model, get_sepsis_score\n'), ((273, 301), 'numpy.loadtxt', 'np.loadtxt', (['f'], {'delimiter': '"""|"""'}), "(f, delimiter='|')\n", (283, 301), True, 'import numpy as np, os, sys\n'), ((1209, 1240), 'os.path.isdir', 'os.path.isdir', (['output_directory'], {}), '(output_directory)\n', (1222, 1240), False, 'import numpy as np, os, sys\n'), ((1250, 1276), 'os.mkdir', 'os.mkdir', (['output_directory'], {}), '(output_directory)\n', (1258, 1276), False, 'import numpy as np, os, sys\n'), ((1434, 1466), 'os.path.join', 'os.path.join', (['input_directory', 'f'], {}), '(input_directory, f)\n', (1446, 1466), False, 'import numpy as np, os, sys\n'), ((1617, 1635), 'numpy.zeros', 'np.zeros', (['num_rows'], {}), '(num_rows)\n', (1625, 1635), True, 'import numpy as np, os, sys\n'), ((1653, 1671), 'numpy.zeros', 'np.zeros', (['num_rows'], {}), '(num_rows)\n', (1661, 1671), True, 'import numpy as np, os, sys\n'), ((1948, 1981), 'os.path.join', 'os.path.join', (['output_directory', 'f'], {}), '(output_directory, f)\n', (1960, 1981), False, 'import numpy as np, os, sys\n'), ((1787, 1824), 'get_sepsis_score.get_sepsis_score', 'get_sepsis_score', (['current_data', 'model'], {}), '(current_data, model)\n', (1803, 1824), False, 'from get_sepsis_score import load_sepsis_model, get_sepsis_score\n'), ((1070, 1102), 'os.path.join', 'os.path.join', (['input_directory', 'f'], {}), '(input_directory, f)\n', (1082, 1102), False, 'import numpy as np, os, sys\n')]