예제 #1
0
 def __init__(self, hints_file="../resource/hints.json"):
     self.problem_template = ProblemTemplate()
     self.io_helper = IOHelper()
     self.console = Console()
     self.problem_metadata = problem_metadata
     with open(hints_file, "r", encoding="utf-8") as f:
         self.hints_json = json.load(f)
예제 #2
0
def main():

    for set_name in ConfigHelper.get_datasets():

        MetricsHelper.reset_metrics()

        data, set_target = IOHelper.read_dataset(set_name)

        feats, labels = DataHelper.extract_feature_labels(data, set_target)
        DataHelper.create_label_mapping(labels)
        max_nb_feats = DataHelper.calculate_max_nb_features(feats)

        for e in range(ConfigHelper.nb_executions):
            start = time.time()
            print("Execution " + str(e))

            train_idxs, test_idxs = DataHelper.split_in_sets(feats, labels)

            train_X = DataHelper.select_rows(feats, train_idxs, copy=False)
            train_y = DataHelper.select_rows(labels, train_idxs, copy=False)
            test_X = DataHelper.select_rows(feats, test_idxs, copy=False)
            test_y = DataHelper.select_rows(labels, test_idxs, copy=False)

            for noise_level in ConfigHelper.noise_levels:

                noisy_idxs, noisy_train_y = DataHelper.insert_noise(
                    train_y, noise_level)

                for name, clf, clean_type in ConfigHelper.get_classifiers():

                    algorithm_data = ConfigHelper.choose_algorithm(
                        clf, clean_type, train_X, noisy_train_y, noisy_idxs,
                        max_nb_feats)

                    chosen_rate = algorithm_data[0]
                    chosen_threshold = algorithm_data[1]
                    chosen_X = algorithm_data[2]
                    chosen_y = algorithm_data[3]
                    chosen_clf = algorithm_data[4]
                    true_filtered = algorithm_data[5]
                    false_filtered = algorithm_data[6]

                    chosen_clf.fit(chosen_X, chosen_y)
                    predictions = chosen_clf.predict(test_X)
                    error = MetricsHelper.calculate_error_score(
                        test_y, predictions)

                    MetricsHelper.metrics.append([
                        set_name, e, noise_level, name, chosen_rate,
                        chosen_threshold, error, true_filtered, false_filtered
                    ])
            print(str(time.time() - start))

        IOHelper.store_results(MetricsHelper.convert_metrics_to_frame(),
                               "final_" + set_name)
예제 #3
0
    def update_best_metrics(execution_results, original_classes):
        # Update the metrics based on the best objective score

        objective_score = execution_results["objective_score"]
        hyperparams = execution_results["hyperparams"]
        partition = execution_results["partition"]
        iterations = execution_results["iterations"]
        execution = execution_results["execution"]
        view_type = execution_results["view_type"]
        converged = execution_results["converged"]

        if objective_score < MetricsHelper.metrics["objective_score"][0]:
            MetricsHelper.hyperparams["hyperparams"] = hyperparams
            MetricsHelper.partition["partition"] = partition
            MetricsHelper.metrics["objective_score"] = objective_score
            MetricsHelper.metrics["iterations"] = iterations
            MetricsHelper.metrics["execution"] = execution
            MetricsHelper.metrics["view_type"] = view_type
            MetricsHelper.metrics["converged"] = converged
            MetricsHelper._calculate_cluster_sizes()
            MetricsHelper._calculate_corrected_rand_score(
                partition, original_classes)
            IOHelper.store_best_results(view_type, "info",
                                        MetricsHelper.metrics)
            IOHelper.store_best_results(view_type, "hyperparams",
                                        MetricsHelper.hyperparams)
            IOHelper.store_best_results(view_type, "partition",
                                        MetricsHelper.partition)
            IOHelper.store_best_results(view_type, "cluster_sizes",
                                        MetricsHelper.sizes)
예제 #4
0
def main():

    view_type = ConfigHelper.view_type
    filename = ConfigHelper.dataset_file

    X, y = IOHelper.read_view_with_classes(view_type, filename)

    [MetricsHelper.update_best_metrics(KCM_FGH.run(view_type, e, X), y) \
     for e in range(1, ConfigHelper.nb_executions+1)]
예제 #5
0
class ProblemSetter():
    def __init__(self, hints_file="../resource/hints.json"):
        self.problem_template = ProblemTemplate()
        self.io_helper = IOHelper()
        self.console = Console()
        self.problem_metadata = problem_metadata
        with open(hints_file, "r", encoding="utf-8") as f:
            self.hints_json = json.load(f)

    def __fill_title(self):
        self.console.showAny("title : ")
        title = self.io_helper.readStr()
        self.problem_template.set_title(title)

    def __fill_title_cn(self):
        self.console.showAny(u"标题 : ")
        title_cnt = self.io_helper.readStr()
        self.problem_template.set_title_cn(title_cnt)

    def __fill_description_cn(self):
        self.console.showAny(u"题目描述文件名 : ")
        file = self.io_helper.readStr()
        raw_content = ""
        description_cn = ""
        with open(file, "r") as f:
            raw_content = f.read()
            # TODO add mote file type support
            if file.split(".")[-1] in ["html", "htm", ".html", ".htm"]:
                description_cn = raw_content
            else:
                description_cn = Parser.markdown2html(raw_content)
        self.problem_template.set_description_cn(description_cn)

    def __fill_difficulty(self):
        self.console.showAny(u"题目难度 : ")
        difficulty = self.io_helper.readInt()
        self.problem_template.set_difficulty(difficulty)

    def __fill_input_schema(self):
        self.console.showAny(u"输入格式文件名 : ")
        file = self.io_helper.readStr()
        raw_content = ""
        input_schema = {}
        with open(file, "r") as f:
            raw_content = f.read()
            assert file.split(".")[-1] in ["json"]
            input_schema = Parser.json2dict(raw_content)
        self.problem_template.set_input_schema(input_schema)

    def __fill_output_schema(self):
        self.console.showAny(u"输出格式文件名 : ")
        file = self.io_helper.readStr()
        raw_content = ""
        output_schema = {}
        with open(file, "r") as f:
            raw_content = f.read()
            assert file.split(".")[-1] in ["json"]
            output_schema = Parser.json2dict(raw_content)
        self.problem_template.set_output_schema(output_schema)

    def __fill_solutions(self):
        self.console.showAny(u"题目答案文件名 : ")
        file = self.io_helper.readStr()
        solution = {}
        with open(file, "r") as f:
            solution['code'] = f.read()
            suffix = file.split(".")[-1].lower()
            assert suffix in ["py", "java", "cc", "rust", "golang"]
            if suffix == 'py':
                solution['lang'] = 'python3'
            elif suffix == 'java':
                solution['lang'] = 'java'
            elif suffix in ['cc', 'cpp']:
                solution['lang'] = 'cpp'
            elif suffix == 'rst':
                solution['lang'] = 'rust'
        self.problem_template.set_solutions([solution])

    def __fill_testcase_in(self):
        self.console.showAny(u"输入数据文件名 : ")
        file = self.io_helper.readStr()
        testcase_in = ""
        with open(file, "r") as f:
            testcase_in = f.read()
        self.problem_template.set_testcase_in(testcase_in)

    def __fill_testcase_out(self):
        self.console.showAny(u"输出数据文件名 : ")
        file = self.io_helper.readStr()
        testcase_out = ""
        with open(file, "r") as f:
            testcase_out = f.read()
        self.problem_template.set_testcase_out(testcase_out)

    def __fill_hints(self):
        self.console.showAny(u"提示文件名 : ")
        file = self.io_helper.readStr()
        hints = []
        with open(file, "r") as f:
            raw_content = f.read()
            # TODO parse json to vector type
            hints = raw_content.split("\n")  # Parser.json2dict(raw_content)
        res = [x for x in hints if len(x) != 0]
        self.problem_template.set_hints(res)

    def __fill_tags(self):
        self.console.showAny(u"标签文件名 : ")
        file = self.io_helper.readStr()
        tags = []
        with open(file, "r") as f:
            raw_content = f.read()
            # TODO parse json to vector type
            tags = raw_content.split("\n")  # Parser.json2dict(raw_content)
        self.problem_template.set_tags(tags)

    def __fill_question_no(self):
        self.console.showAny(u"题号 : ")
        question_no = self.io_helper.readStr()
        self.problem_template.set_question_no(question_no)
        self.problem_template.set_hints(
            self.hints_json['byProblem'][question_no])

    def __fill_book_name(self):
        self.console.showAny(u"书名 : ")
        book_name = self.io_helper.readStr()
        self.problem_template.set_book_name(book_name)

    def fill_problem_template(self):
        self.__fill_title()
        self.__fill_title_cn()
        self.__fill_description_cn()
        self.__fill_difficulty()
        self.__fill_input_schema()
        self.__fill_output_schema()
        self.__fill_solutions()
        self.__fill_testcase_in()
        self.__fill_testcase_out()
        # self.__fill_hints()
        self.__fill_tags()
        self.__fill_question_no()
        self.__fill_book_name()

    def save_problem_file(self, file: str):
        with open(file, 'w', encoding='utf-8') as f:
            f.write(self.problem_template.to_json())
예제 #6
0
###############################################################################

from io_helper import IOHelper
from statistics_helper import StatisticsHelper
from data_helper import DataHelper
from config_helper import ConfigHelper

if __name__ == "__main__":

    dataset_name = ConfigHelper.analysis_dataset

    train_data = IOHelper.read_dataset(dataset_name)
    for col in train_data.columns:
        series = train_data[col]

        stats = StatisticsHelper.get_feature_stats(series)
        StatisticsHelper.draw_feature_distribution(series, col)
        IOHelper.store_analysis(stats, col, dataset_name)

    DataHelper.fill_missing_data(train_data, is_train=True)
    for col in train_data.columns:
        series = train_data[col]

        col = col + "_filled"
        stats = StatisticsHelper.get_feature_stats(series)
        StatisticsHelper.draw_feature_distribution(series, col)
        IOHelper.store_analysis(stats, col, dataset_name)
# Do not change this for reproducibility
import random as rnd
from numpy import random as rnp

rnd.seed(2789)
rnp.seed(3056)
########################################

from io_helper import IOHelper
from data_helper import DataHelper
from config_helper import ConfigHelper
from metrics_helper import MetricsHelper

if __name__ == "__main__":

    train_data = IOHelper.read_dataset("train")
    train_X, train_y = DataHelper.extract_feature_labels(train_data)

    predef = ConfigHelper.use_predefined_cols

    DataHelper.add_nan_indication_cols(train_X)
    DataHelper.remove_high_nan_rate_cols(train_X, predef)
    DataHelper.remove_small_variance_cols(train_X, predef)

    train_y = DataHelper.remove_high_nan_rate_rows(train_X, train_y)
    DataHelper.fill_missing_data(train_X, is_train=True)
    train_X = DataHelper.split_categorical_cols(train_X, is_train=True)
    DataHelper.scale_continuous_cols(train_X, is_train=True)
    train_X = DataHelper.select_best_features(train_X,
                                              None,
                                              train_y,
import random as rnd
from numpy import random as rnp
rnd.seed(2789)
rnp.seed(3056)
########################################

import time

from io_helper import IOHelper
from data_helper import DataHelper
from config_helper import ConfigHelper
from metrics_helper import MetricsHelper

if __name__ == "__main__":

    data = IOHelper.read_dataset("train")
    feats, labels = DataHelper.extract_feature_labels(data)

    predef = ConfigHelper.use_predefined_cols

    DataHelper.add_nan_indication_cols(feats)
    DataHelper.remove_high_nan_rate_cols(feats, predef)
    DataHelper.remove_small_variance_cols(feats, predef)

    for e in xrange(ConfigHelper.nb_executions):
        print "Execution: " + str(e + 1)

        MetricsHelper.reset_metrics()

        for f, (train_idxs,
                val_idxs) in enumerate(ConfigHelper.k_fold_cv(labels)):
def run_game():

    pygame.init()

    # 设置窗口
    os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (128, 60)
    settings = Settings()
    screen = pygame.display.set_mode(
        (settings.screen_width, settings.screen_height))
    pygame.display.set_caption("Shooting Practice")

    # 背景图
    dir_path = os.path.dirname(os.path.abspath(__file__))
    background = pygame.image.load(dir_path +
                                   r'\images\background.jpg').convert()

    # 创建枪支、子弹编组
    gun = Gun(settings, screen)
    bullets = Group()

    # 创建全局计时器
    overall_timer = MyTimer()

    # 创建靶机编组、提示条编组
    target_sample = TargetSample()
    targets = Group()
    notice_bars = Group()

    # 创建用于存储游戏统计信息的实例
    stats = GameStats(settings, overall_timer)

    # 文件读写
    io_helper = IOHelper(stats)

    # 创建信息显示板
    running_info = RunningInfo(settings, screen, stats)
    win_info = WinInfo(settings, screen, stats, io_helper)
    failed_info = FailedInfo(screen, stats)

    # 创建输入框
    pregame_info = PregameInfo(settings, screen, stats)

    # 开始游戏主循环
    while True:
        # 检查事件
        func.check_events(settings, screen, stats, running_info, win_info,
                          failed_info, gun, targets, bullets, pregame_info,
                          notice_bars)
        # 更新屏幕
        func.common_update_screen(background, settings, screen, stats,
                                  running_info, gun, target_sample, targets,
                                  bullets, notice_bars)
        # 游戏进行中,更新枪支、子弹、靶机提示条、靶机位置
        if stats.game_state == GameState.RUNNING:
            gun.update()
            func.update_bullets(settings, screen, stats, targets, bullets,
                                notice_bars, win_info, io_helper)
            func.update_notice(notice_bars)
            func.update_targets(targets)
        elif stats.game_state == GameState.PREGAME:
            pregame_info.draw_pregame_info()
        elif stats.game_state == GameState.GAME_OVER:
            failed_info.show_info()
        elif stats.game_state == GameState.GAME_FINISH:
            win_info.show_info()
        # 让最近绘制的屏幕可见
        pygame.display.flip()