# Open detector

## CO2

Nieco o CO2.

### Init general models from local database

Pre ladiace ucely bola vytvorena lokalna databaza, ktora obsahuje predspracovane udaje a následne umoznuje rýchle pocitanie pouzitých atribútov. Ukazkovy priklad toho ako moze vyzerat vytvorenie modelu z databaze

    """
    """
    from os.path import dirname, abspath, join
    import sys
    sys.path.append(abspath(join(dirname(__file__), '../..', '')))

    from dm.models.ModelsUtil import ModelsUtil
    from dm.models.open_detector.generic_training_file_from_local_db import *
    import os

    __author__ = ''
    __email__ = ''

    if __name__ == '__main__':
        logging.basicConfig(level=logging.DEBUG,
                            format='%(asctime)s %(levelname)s %(message)s')

        no_event_shift = int(cu.open_detector('attrs.no_event.time_shift'))

        # tabulka s CO2, ktora neprekroci hranicu 2000ppm
        table_name = 'measured_filtered_peto'
        columns = ColumnMapper.OPEN_CO2

        directory = cu.open_detector('generic.directory')
        if not os.path.isdir(directory):
            os.mkdir(directory)

        co2_csv = cu.open_detector('generic.co2.data_file.name') + '_from_local_db.bin'
        co2_model = cu.open_detector('generic.co2.model.name') + '_from_local_db.bin'
        data_co2 = training_set_co2(cu.package('co2.event_file.name'), no_event_shift, table_name,
                                    co2_csv, columns)
        ModelsUtil.write_model(data_co2, co2_model, ModelsUtil.replace_nothing_open)

        t_h_csv = cu.open_detector('generic.t_h.data_file.name') + '_from_local_db.bin'
        t_h_model = cu.open_detector('generic.t_h.model.name') + '_from_local_db.bin'
        data_t_h = training_set_t_h(cu.package('t_h.event_file.name'), no_event_shift, table_name,
                                    t_h_csv, columns)
        ModelsUtil.write_model(data_t_h, t_h_model, ModelsUtil.replace_nothing_open)

Output is binary file contains model … vytvoreny pomocou kniznice `sklearn` a modelu `SVM`. Vsetky konfiguracne parametre su ulozene v konfiguracnom subore `config.ini`, ktory je popisany v sekcii Configuration File.

Ukazka spustenia vytvorenia modelu z lokalnej databaze:

    python3 examples2/0300_open_detector/init_general_models_from_local_db.py

### Init general models

    """
    """
    from os.path import dirname, abspath, join
    import sys
    sys.path.append(abspath(join(dirname(__file__), '../..', '')))

    import logging
    import json
    from dm.models.open_detector.generic_training_file import generic_training_file
    from dm.ConnectionUtil import ConnectionUtil as cu
    from dm.WundergroundCom import WundergroundCom
    from dm.BeeeOnClient import BeeeOnClient
    from dm.models.open_detector.create_attrs import ColumnMapper
    from dm.models.ModelsUtil import ModelsUtil

    __author__ = ''
    __email__ = ''

    if __name__ == '__main__':
        logging.basicConfig(level=logging.DEBUG,
                            format='%(asctime)s %(levelname)s %(message)s')

        cls = {
            'ant-work': BeeeOnClient('ant-work.fit.vutbr.cz', 8010),
            'rehivetech': BeeeOnClient('beeeon.rehivetech.com', 8010),
        }
        cls['ant-work'].api_key = cu.ant_work_api_key('dm')
        cls['rehivetech'].api_key = cu.rehivetech_api_key('acontroller')

        no_event_shift = int(cu.open_detector('attrs.no_event.time_shift'))

        # file with devices for training
        with open(cu.open_detector('generic.devices.path'), 'r') as f:
            devs = json.load(f)

        w = WundergroundCom()
        w.api_key = cu.wunderground_api_key()

        # co2
        data_co2 = generic_training_file(cu.package('co2.event_file.name'),
                                         no_event_shift, 'co2', ColumnMapper.OPEN_CO2, cls,
                                         devs, w)
        ModelsUtil.write_model(data_co2, cu.open_detector('generic.co2.model.name'),
                               ModelsUtil.replace_nothing_open)

        # t_h
        data_t_h = generic_training_file(cu.package('t_h.event_file.name'),
                                         no_event_shift, 't_h', ColumnMapper.OPEN_T_H, cls,
                                         devs, w)
        ModelsUtil.write_model(data_t_h, cu.open_detector('generic.t_h.model.name'),
                               ModelsUtil.replace_nothing_open)