import csv
import os
import sys

def feature_to_index(cases, column_nb, offset=0):
    feature_set = set()
    for case in cases:
        if len(case) > column_nb:
            feature_set.add(case[column_nb])
    l = list(feature_set)
    index = {}
    i = 0
    for f in l:
        if f != '':
            index[f] = i + offset
            i += 1
    return index

def filter_cases(cases, except_features):
    features_index = []
    features_mapping = {}
    offset = 0
    m = len(cases[0])
    for c in cases:
        if len(c) > m:
            m = len(c)
    for j, f in enumerate([i for i in range(0, m) if i not in except_features]):
        feat = feature_to_index(cases, f, offset)
        features_index.append(feat)
        features_mapping[f] = j
        offset += len(feat)

    final_cases = []
    i = 0
    for case in cases:
        final_cases.append([])
        for j, f in enumerate(case):
            if j not in except_features:
                if case[j] is None or case[j] not in ['']:
                    translation = features_index[features_mapping[j]][case[j]]
                    final_cases[i].append(translation)
        i += 1
        
    return final_cases

def read_cases(path):
    cases = []
    headers = []
    with open(path, 'rb') as csvfile:
        reader = csvfile.readlines()
        n = len(reader[0].split(' '))
        for i, row in enumerate(reader):
            cases.append(row.split(' '))

    return cases


def main():
    path = sys.argv[1]
    file_name = path.split('/')[-1].split('.')[0]
    base_name = file_name.split('.')[0]
    cases = read_cases(path)

    outcome_row = 0
    except_features_no_outcomes = [0]

    final_cases = filter_cases(cases, except_features_no_outcomes)
    casebase_output = '{}_casebase.txt'.format(base_name)
    outcomes_output = '{}_outcomes.txt'.format(base_name)

    try:
        os.remove(casebase_output)
        os.remove(outcomes_output)
    except:
        pass

    with open(casebase_output, 'a') as file:
        for case in final_cases:
            for e in case:
                file.write('{} '.format(e))
            file.write('\n')

    with open(outcomes_output, 'a') as file:
        for case in cases:
            file.write('{}\n'.format('0' if case[outcome_row] == '-1' else '1'))



if __name__ == '__main__':
    main()
@@ -1411,55 +1411,484 @@
     
-cases.append(row.split(' '))%0A%0A    return cases%0A
+r = map(str.strip, row.strip().split(' '))%0A            t = %5B%5D%0A            for i, e in enumerate(r):%0A                if i == 0:%0A                    t.append(e)%0A                else:%0A                    #print(e.split(':'))%0A                    v = '%7B%7D:%7B%7D'.format(e.split(':')%5B0%5D, reduce_precision(e.split(':')%5B1%5D, 2))%0A                    t.append(v)%0A            cases.append(t)%0A%0A    return cases%0A%0Adef reduce_precision(f, prec=2):%0A    return '%7B:.2%7D'.format(round(float(f), prec))
 %0A%0Ade

Improve ionosphere data processing