def run_from_config(params, *args):
    if len(args) < 2:
        print "No input file or test file given: %s:%s" % (args, len(args))
        sys.exit(-1)
    input_file = args[0]
    test_file = args[1]
    if len(args) > 2:
        num_epochs = int(args[2])
    else:
        num_epochs = 1000

    params["D"] = 2**params["logD"]
    params["conv_layer1_nfilters"] = 2**params["log_conv_layer1_nfilters"]
    params["conv_layer2_nfilters"] = 2**params["log_conv_layer2_nfilters"]

    model = ConvolutionalCopyAttentionalRecurrentLearner(params)
    model.train(input_file, max_epochs=num_epochs)

    test_data, original_names = model.naming_data.get_data_in_recurrent_copy_convolution_format(
        test_file, model.padding_size)
    test_name_targets, test_code_sentences, test_code, test_target_is_unk, test_copy_vectors = test_data
    eval = F1Evaluator(model)
    point_suggestion_eval = eval.compute_names_f1(
        test_code, original_names,
        model.naming_data.all_tokens_dictionary.get_all_names())
    return -point_suggestion_eval.get_f1_at_all_ranks()[1]
def run_from_config(params, *args):
    if len(args) < 2:
        print "No input file or test file given: %s:%s" % (args, len(args))
        sys.exit(-1)
    input_file = args[0]
    test_file = args[1]
    if len(args) > 2:
        num_epochs = int(args[2])
    else:
        num_epochs = 1000

    params["D"] = 2**params["logD"]
    params["conv_layer1_nfilters"] = 2**params["log_conv_layer1_nfilters"]
    params["conv_layer2_nfilters"] = 2**params["log_conv_layer2_nfilters"]

    model = ConvolutionalAttentionalLearner(params)
    model.train(input_file, max_epochs=num_epochs)

    test_data, original_names = model.naming_data.get_data_in_convolution_format(
        test_file, model.name_cx_size, model.padding_size)
    test_name_targets, test_name_contexts, test_code_sentences, test_original_name_ids = test_data
    ids, unique_idx = np.unique(test_original_name_ids, return_index=True)
    eval = F1Evaluator(model)
    point_suggestion_eval = eval.compute_names_f1(
        test_code_sentences[unique_idx], original_names,
        model2.naming_data.all_tokens_dictionary.get_all_names())
    return -point_suggestion_eval.get_f1_at_all_ranks()[1]
    def evaluate_suggestion_decisions(self, input_file):
        test_data, original_names = self.naming_data.get_data_in_copy_convolution_format(input_file, self.name_cx_size, self.padding_size)
        name_targets, orignal_targets, name_contexts, code_sentences, code, copy_vectors, target_is_unk, original_name_ids = test_data
        code = np.array(code, dtype=np.object)

        ids, unique_idx = np.unique(original_name_ids, return_index=True)
        eval = F1Evaluator(self)
        point_suggestion_eval = eval.compute_names_f1(code[unique_idx], original_names,
                                                      self.naming_data.all_tokens_dictionary.get_all_names())
        print point_suggestion_eval
        return point_suggestion_eval.get_f1_at_all_ranks()
    params["train_file"] = input_file
    params["test_file"] = sys.argv[4]
    with ExperimentLogger("ConvolutionalCopyAttentionalRecurrentLearner",
                          params) as experiment_log:
        model = ConvolutionalCopyAttentionalRecurrentLearner(params)
        model.train(input_file, max_epochs=max_num_epochs)
        model.save("copy_convolutional_att_rec_model" +
                   os.path.basename(params["train_file"]) + ".pkl")

        model2 = ConvolutionalCopyAttentionalRecurrentLearner.load(
            "copy_convolutional_att_rec_model" +
            os.path.basename(params["train_file"]) + ".pkl")

        test_data, original_names = model2.naming_data.get_data_in_recurrent_copy_convolution_format(
            params["test_file"], model2.padding_size)
        test_name_targets, test_code_sentences, test_code, test_target_is_unk, test_copy_vectors = test_data
        #name_ll = model2.model.log_prob_with_targets(test_code_sentences, test_name_targets)
        #print "Test name_ll=%s" % name_ll

        eval = F1Evaluator(model2)
        point_suggestion_eval = eval.compute_names_f1(
            test_code, original_names,
            model2.naming_data.all_tokens_dictionary.get_all_names())
        print point_suggestion_eval
        results = point_suggestion_eval.get_f1_at_all_ranks()
        print results
        experiment_log.record_results({
            "f1_at_rank1": results[0],
            "f1_at_rank5": results[1]
        })