def _init_db(self, models):
        """Initialize database"""

        # db setup script
        _createTablesScript = """
            create table models (
                model_idx      integer UNIQUE NOT NULL,
                pickled_model  blob NOT NULL
            );

            create table fitted_models (
                model_idx      integer NOT NULL,
                fold_idx       integer NOT NULL,
                pickled_model  blob NOT NULL
            );

            create table model_scores (
                model_idx      integer UNIQUE NOT NULL,
                score          real NOT NULL,
                probs          blob NOT NULL
            );

            create table ensemble (
                model_idx      integer NOT NULL,
                weight         integer NOT NULL
            );
        """

        # barf if db file exists and we're making a new model
        if (models and os.path.exists(self.db_file)):
            raise ValueError("db_file '%s' already exists!" % self.db_file)

        db_conn = sqlite3.connect(self.db_file)
        with db_conn:
            db_conn.execute("pragma journal_mode = off")

        if (models):
            # build database
            with db_conn:
                db_conn.executescript(_createTablesScript)

            # populate model table
            insert_stmt = """insert into models (model_idx, pickled_model)
                             values (?, ?)"""
            with db_conn:
                vals = ((i, buffer(dumps(m))) for i, m in enumerate(models))
                db_conn.executemany(insert_stmt, vals)
                create_stmt = "create index models_index on models (model_idx)"
                db_conn.execute(create_stmt)

            self._n_models = len(models)

        else:
            curs = db_conn.cursor()
            curs.execute("select count(*) from models")
            self._n_models = curs.fetchone()[0]

            curs.execute("select model_idx, weight from ensemble")
            for k, v in curs.fetchall():
                self._ensemble[k] = v

            # clumsy hack to get n_classes
            curs.execute("select probs from model_scores limit 1")
            r = curs.fetchone()
            probs = loads(str(r[0]))
            self._n_classes = probs.shape[1]

        db_conn.close()
dataset/ETHPy150Open dclambert/pyensemble/ensemble.py/EnsembleSelectionClassifier._init_db/original