#!/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, 13, 35, 40), 'os.listdir', 'os.listdir', ({(35, 24, 35, 39): 'input_directory'}, {}), '(input_directory)', False, 'import numpy as np, os, sys\n'), ((43, 12, 43, 31), 'get_sepsis_score.load_sepsis_model', 'load_sepsis_model', ({}, {}), '()', False, 'from get_sepsis_score import load_sepsis_model, get_sepsis_score\n'), ((10, 15, 10, 43), 'numpy.loadtxt', 'np.loadtxt', (), '', True, 'import numpy as np, os, sys\n'), ((39, 11, 39, 42), 'os.path.isdir', 'os.path.isdir', ({(39, 25, 39, 41): 'output_directory'}, {}), '(output_directory)', False, 'import numpy as np, os, sys\n'), ((40, 8, 40, 34), 'os.mkdir', 'os.mkdir', ({(40, 17, 40, 33): 'output_directory'}, {}), '(output_directory)', False, 'import numpy as np, os, sys\n'), ((49, 21, 49, 53), 'os.path.join', 'os.path.join', ({(49, 34, 49, 49): 'input_directory', (49, 51, 49, 52): 'f'}, {}), '(input_directory, f)', False, 'import numpy as np, os, sys\n'), ((56, 17, 56, 35), 'numpy.zeros', 'np.zeros', ({(56, 26, 56, 34): 'num_rows'}, {}), '(num_rows)', True, 'import numpy as np, os, sys\n'), ((57, 17, 57, 35), 'numpy.zeros', 'np.zeros', ({(57, 26, 57, 34): 'num_rows'}, {}), '(num_rows)', True, 'import numpy as np, os, sys\n'), ((65, 22, 65, 55), 'os.path.join', 'os.path.join', ({(65, 35, 65, 51): 'output_directory', (65, 53, 65, 54): 'f'}, {}), '(output_directory, f)', False, 'import numpy as np, os, sys\n'), ((60, 43, 60, 80), 'get_sepsis_score.get_sepsis_score', 'get_sepsis_score', ({(60, 60, 60, 72): 'current_data', (60, 74, 60, 79): 'model'}, {}), '(current_data, model)', False, 'from get_sepsis_score import load_sepsis_model, get_sepsis_score\n'), ((36, 26, 36, 58), 'os.path.join', 'os.path.join', ({(36, 39, 36, 54): 'input_directory', (36, 56, 36, 57): 'f'}, {}), '(input_directory, f)', False, 'import numpy as np, os, sys\n')]