<commit_before># -*- coding: utf-8 -*-

""" Score the classifier and update the local leaderboard. """

from __future__ import division, print_function
import os
import requests
import sys
from astropy.table import Table, vstack
from datetime import datetime
from glob import glob
from time import time

from classifier import Classifier


# Load all training set.
training_set_paths = glob("training_set/*.csv")
if len(training_set_paths) == 0:
    print("No training set data")
    sys.exit(1)


training_set = vstack([
    Table.read(path, format="csv") for path in training_set_paths])
training_set_classifications = (training_set["srctype"] == 1)
del training_set["srctype"]


# Load the test set
test_set_paths = glob("test_set/*.csv")
if len(test_set_paths) == 0:
    print("No test set data")
    sys.exit(1)

test_set = vstack([
    Table.read(path, format="csv") for path in test_set_paths])
test_set_classifications = (test_set["srctype"] == 1)
del test_set["srctype"]


classifier = Classifier()

# Train the classifier.
t = time()
classifier.train(training_set, training_set_classifications)
t_train = time() - t

# Score the classifier.
t = time()
N_true_transients_found, N_true_transients_missed, N_false_positives, score \
    = classifier.score(test_set, test_set_classifications)
t_test = time() - t


def human_readable_time(seconds):
    if seconds < 60:
        return "{:.0f}s".format(seconds)
    else:
        return "{:.0f}m {:.0f}s".format(t/60, t % 60)


# Print a summary.
print("""Summary:
    Number of true transients found: {0}
    Number of true transients missed: {1}
    Number of false positives: {2}
    Overall score: {3:.3f}

    Train time: {4}
    Test time: {5}""".format(
        N_true_transients_found, N_true_transients_missed, N_false_positives,
        score, human_readable_time(t_train), human_readable_time(t_test)))


# If we are on Travis, then do some things.
if os.environ.get("TRAVIS"):

    entry = [
        datetime.now().strftime("%Y/%m/%d %I:%M:%s"),
        os.environ["TRAVIS_BRANCH"],
        os.environ["TRAVIS_COMMIT"],
        "https://github.com/goto-obs/goto-vegas/tree/{TRAVIS_BRANCH}"\
            .format(**os.environ),
        "https://travis-ci.org/goto-obs/goto-vegas/builds/{TRAVIS_BUILD_ID}"\
            .format(**os.environ),
        "https://github.com/goto-obs/goto-vegas/commit/{TRAVIS_COMMIT}"\
            .format(**os.environ),
        os.environ["TRAVIS_PYTHON_VERSION"],
        human_readable_time(t_train),
        human_readable_time(t_test),
        str(N_true_transients_found),
        str(N_true_transients_missed),
        str(N_false_positives),
        "{:.3f}".format(score)
    ]

    # Get the latest copy of entries.csv from GitHub, and update it with this entry
    response = requests.get(
        "https://raw.githubusercontent.com/{TRAVIS_REPO_SLUG}/master/entries.csv"\
        .format(**os.environ))

    with open("entries.csv", "w") as fp:
        fp.write("{}\n{}".format(response.content, ",".join(entry)))


    entries = Table.read("entries.csv", format="csv")
    entries.sort("score")
    entries = entries[::-1]

    column_names = ["rank", "time", "branch", "commit", "python", "train time", 
                    "test time", "transients found", "transients missed", 
                    "false positives", "score"]<commit_msg>fix sorting for python 3<commit_after># -*- coding: utf-8 -*-

""" Score the classifier and update the local leaderboard. """

from __future__ import division, print_function
import numpy as np
import os
import requests
import sys
from astropy.table import Table, vstack
from datetime import datetime
from glob import glob
from time import time

from classifier import Classifier


# Load all training set.
training_set_paths = glob("training_set/*.csv")
if len(training_set_paths) == 0:
    print("No training set data")
    sys.exit(1)


training_set = vstack([
    Table.read(path, format="csv") for path in training_set_paths])
training_set_classifications = (training_set["srctype"] == 1)
del training_set["srctype"]


# Load the test set
test_set_paths = glob("test_set/*.csv")
if len(test_set_paths) == 0:
    print("No test set data")
    sys.exit(1)

test_set = vstack([
    Table.read(path, format="csv") for path in test_set_paths])
test_set_classifications = (test_set["srctype"] == 1)
del test_set["srctype"]


classifier = Classifier()

# Train the classifier.
t = time()
classifier.train(training_set, training_set_classifications)
t_train = time() - t

# Score the classifier.
t = time()
N_true_transients_found, N_true_transients_missed, N_false_positives, score \
    = classifier.score(test_set, test_set_classifications)
t_test = time() - t


def human_readable_time(seconds):
    if seconds < 60:
        return "{:.0f}s".format(seconds)
    else:
        return "{:.0f}m {:.0f}s".format(t/60, t % 60)


# Print a summary.
print("""Summary:
    Number of true transients found: {0}
    Number of true transients missed: {1}
    Number of false positives: {2}
    Overall score: {3:.3f}

    Train time: {4}
    Test time: {5}""".format(
        N_true_transients_found, N_true_transients_missed, N_false_positives,
        score, human_readable_time(t_train), human_readable_time(t_test)))


# If we are on Travis, then do some things.
if os.environ.get("TRAVIS"):

    entry = [
        datetime.now().strftime("%Y/%m/%d %I:%M:%s"),
        os.environ["TRAVIS_BRANCH"],
        os.environ["TRAVIS_COMMIT"],
        "https://github.com/goto-obs/goto-vegas/tree/{TRAVIS_BRANCH}"\
            .format(**os.environ),
        "https://travis-ci.org/goto-obs/goto-vegas/builds/{TRAVIS_BUILD_ID}"\
            .format(**os.environ),
        "https://github.com/goto-obs/goto-vegas/commit/{TRAVIS_COMMIT}"\
            .format(**os.environ),
        os.environ["TRAVIS_PYTHON_VERSION"],
        human_readable_time(t_train),
        human_readable_time(t_test),
        str(N_true_transients_found),
        str(N_true_transients_missed),
        str(N_false_positives),
        "{:.3f}".format(score)
    ]

    # Get the latest copy of entries.csv from GitHub, and update it with this entry
    response = requests.get(
        "https://raw.githubusercontent.com/{TRAVIS_REPO_SLUG}/master/entries.csv"\
        .format(**os.environ))

    with open("entries.csv", "w") as fp:
        fp.write("{}\n{}".format(response.content, ",".join(entry)))


    entries = Table.read("entries.csv", format="csv")
    indices = np.argsort(entries["score"])[::-1]
    entries = entries[indices]

    column_names = ["rank", "time", "branch", "commit", "python", "train time", 
                    "test time", "transients found", "transients missed", 
                    "false positives", "score"]
{'fraction_non_alphanumeric': 0.14245478802253186, 'fraction_numerical': 0.007560035576638008, 'mean_word_length': 3.4534653465346534, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 8, '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': '3029872', 'n_tokens_mistral': 2316, 'n_tokens_neox': 2217, 'n_words': 506}