<commit_before>#! /usr/bin/env python

import argparse
import logging

import keras
import pandas as pd

from uno_data import DataFeeder
from uno_baseline_keras2 import evaluate_prediction, log_evaluation

def log_evaluation(metric_outputs, description='Comparing y_true and y_pred:'):
    print(description)
    for metric, value in metric_outputs.items():
        print('  {}: {:.4f}'.format(metric, value))


def get_parser():
    parser = argparse.ArgumentParser(description='Uno infer script')
    parser.add_argument("--data",
                       help="data file to infer on. expect exported file from uno_baseline_keras2.py")
    parser.add_argument("--model_file", help="json model description file")
    parser.add_argument("--weights_file", help="model weights file")

    return parser


def main():
    parser = get_parser()
    args = parser.parse_args()

    test_gen = DataFeeder(filename=args.data, batch_size=1024)

    if 'json' == args.model_file.split('.')[-1]:
        with open(args.model_file, 'r') as f:
            model_json = f.read()
            model = keras.models.model_from_json(model_json)
            model.load_weights(args.weights_file)
    else:
        model = keras.models.load_model(args.model_file)

    model.summary()

    y_test_pred = model.predict_generator(test_gen, test_gen.steps)
    y_test_pred = y_test_pred.flatten()

    df_y = test_gen.get_response(copy=True)
    y_test = df_y['Growth'].values
    scores = evaluate_prediction(y_test, y_test_pred)
    log_evaluation(scores, description='Testing on data from {} ({})'.format(args.data, len(y_test_pred)))

    # save to tsv
    df_pred = df_y.assign(PredictedGrowth=y_test_pred, GrowthError=y_test_pred-y_test)
    df_pred.sort_values(['Sample', 'Drug1', 'Drug2', 'Dose1', 'Dose2', 'Growth'], inplace=True)
    df_pred.to_csv('prediction.tsv', sep='\t', index=False, float_format='%.4g')


if __name__ == '__main__':
    main()
<commit_msg>can choose partition, train, val or all(default)<commit_after>#! /usr/bin/env python

import argparse
import logging

import keras
import pandas as pd

from uno_data import DataFeeder
from uno_baseline_keras2 import evaluate_prediction, log_evaluation

def log_evaluation(metric_outputs, description='Comparing y_true and y_pred:'):
    print(description)
    for metric, value in metric_outputs.items():
        print('  {}: {:.4f}'.format(metric, value))


def get_parser():
    parser = argparse.ArgumentParser(description='Uno infer script')
    parser.add_argument("--data",
                       help="data file to infer on. expect exported file from uno_baseline_keras2.py")
    parser.add_argument("--model_file", help="json model description file")
    parser.add_argument("--weights_file", help="model weights file")
    parser.add_argument("--partition", default='all',
                        choices=['train', 'val', 'all'], help="partition of test dataset")

    return parser


def main():
    parser = get_parser()
    args = parser.parse_args()

    if 'json' == args.model_file.split('.')[-1]:
        with open(args.model_file, 'r') as f:
            model_json = f.read()
            model = keras.models.model_from_json(model_json)
            model.load_weights(args.weights_file)
    else:
        model = keras.models.load_model(args.model_file)

    model.summary()

    df_pred_list = []
    dataset = ['train', 'val'] if args.partition == 'all' else [args.partition]
    for partition in dataset:
        test_gen = DataFeeder(filename=args.data, partition=partition, batch_size=1024)
        y_test_pred = model.predict_generator(test_gen, test_gen.steps)
        y_test_pred = y_test_pred.flatten()

        df_y = test_gen.get_response(copy=True)
        y_test = df_y['Growth'].values

        df_pred = df_y.assign(PredictedGrowth=y_test_pred, GrowthError=y_test_pred-y_test)
        df_pred_list.append(df_pred)


    df_pred = pd.concat(df_pred_list)
    scores = evaluate_prediction(df_pred['Growth'], df_pred['PredictedGrowth'])
    log_evaluation(scores, description='Testing on data from {} on partition {}, ({})'.format(args.data, args.partition, len(y_test_pred)))

    # save to tsv
    df_pred.sort_values(['Sample', 'Drug1', 'Drug2', 'Dose1', 'Dose2', 'Growth'], inplace=True)
    df_pred.to_csv('prediction.{}.tsv'.format(args.partition), sep='\t', index=False, float_format='%.4g')


if __name__ == '__main__':
    main()

{'fraction_non_alphanumeric': 0.13085804844917365, 'fraction_numerical': 0.005886348200135839, 'mean_word_length': 3.807399347116431, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5180636', 'n_tokens_mistral': 1477, 'n_tokens_neox': 1425, 'n_words': 297}