Example #1
0
def test_engine_ll_complex_model():
    """
    Test log-likelihood computation under a complex model.
    """

    from cargo.log import get_logger

    get_logger("cargo.llvm.constructs", level="DEBUG")

    # test the model
    from cargo.statistics import (
        Tuple,
        ModelEngine,
        MixedBinomial,
        FiniteMixture,
    )

    model = FiniteMixture(Tuple([(MixedBinomial(), 128)]), 8)
    engine = ModelEngine(model)

    # generate some fake parameters
    sample = numpy.empty((), model.sample_dtype)
    parameter = numpy.empty((), model.parameter_dtype)

    sample["d0"]["n"] = 4
    sample["d0"]["k"] = 1

    parameter["p"] = 1.0 / 8.0
    parameter["c"]["d0"] = 0.5

    assert_almost_equal(engine.ll(parameter, sample), -177.445678223)
Example #2
0
def main(url, schema=None, apply=False, alphabetical=False, quiet=False):
    """
    Print or apply a reflected or loaded database schema.
    """

    # output
    from cargo.log import (
        get_logger,
        enable_default_logging,
    )

    enable_default_logging()

    # build the particular database engine
    from cargo.sql.alchemy import make_engine

    engine = make_engine(url)

    # load the appropriate schema
    if schema is None:
        # examine the database to construct a schema
        from sqlalchemy.schema import MetaData

        metadata = MetaData(bind=engine.connect(), reflect=True)
    else:
        # load an already-defined schema
        from cargo.sugar import value_by_name

        metadata = value_by_name(schema)

    # print or apply the schema
    if apply:
        if not quiet:
            get_logger("sqlalchemy.engine", level="DEBUG")

        metadata.create_all(engine)
    else:
        # print the DDL
        from sqlalchemy.schema import CreateTable

        if alphabetical:
            sorted_tables = sorted(metadata.sorted_tables,
                                   key=lambda t: t.name)
        else:
            sorted_tables = metadata.sorted_tables

        for table in sorted_tables:
            print CreateTable(table).compile(engine)
Example #3
0
def main(url, quiet=False):
    """
    Drop unused tables from database.
    """

    # basic setup
    from cargo.log import enable_default_logging

    enable_default_logging()

    if not quiet:
        get_logger("sqlalchemy.engine", level="WARNING")
        get_logger("cargo.sql.actions", level="DETAIL")

    # connect
    from sqlalchemy import create_engine

    engine = create_engine(url)
    connection = engine.connect()

    with connection.begin():
        # reflect the schema
        from sqlalchemy.schema import MetaData

        metadata = MetaData(bind=connection, reflect=True)

        # look for and drop empty tables
        from sqlalchemy.sql import select
        from sqlalchemy.sql.functions import count

        for table in metadata.sorted_tables:
            ((size, ), ) = connection.execute(select([count()], None, table))

            if size == 0:
                log.note("table %s is empty; dropping", table.name)

                table.drop()
            else:
                log.note("table %s has %i rows", table.name, size)

    # done
    engine.dispose()
Example #4
0
"""
@author: Bryan Silverthorn <*****@*****.**>
"""

import numpy
import scipy

from cargo.log import get_logger

log = get_logger(__name__)

#def test_dcm_random_variate():
#"""
#Test operations on the DCM distribution.
#"""

#from numpy.random         import RandomState
#from cargo.statistics.dcm import DirichletCompoundMultinomial

#random = RandomState(6995749)
#dcm    = DirichletCompoundMultinomial([0.1, 1.0], 8)

#def assert_samples_ok(samples):
#"""
#Assert that the sample statistics appear reasonable.
#"""

#from nose.tools import assert_almost_equal

#mean = numpy.sum(samples, 0, float) / len(samples)
Example #5
0
"""
@author: Bryan Silverthorn <*****@*****.**>
"""

if __name__ == "__main__":
    from plac import call
    from cargo.tools.sql.prune import main

    call(main)

from plac import annotations
from cargo.log import get_logger
from cargo.sql.alchemy import normalize_url

log = get_logger(__name__, level="NOTSET")


@annotations(
    url=("database URL", "positional", None, normalize_url),
    quiet=("be less noisy", "flag", "q"),
)
def main(url, quiet=False):
    """
    Drop unused tables from database.
    """

    # basic setup
    from cargo.log import enable_default_logging

    enable_default_logging()
Example #6
0
def main(to_url,
         schema=None,
         tables=None,
         quiet=False,
         where=None,
         fetch=8192,
         *from_urls):
    """
    Copy data from source database(s) to some single target.
    """

    # basic setup
    from cargo.log import enable_default_logging

    enable_default_logging()

    if not quiet:
        get_logger("sqlalchemy.engine", level="WARNING")
        get_logger("cargo.sql.actions", level="DETAIL")

    if where is not None and (tables is None or len(tables) != 1):
        raise ValueError(
            "exactly one table must be specified with where clause")

    # copy as requested
    from cargo.sql.alchemy import make_engine

    to_engine = make_engine(to_url)
    to_connection = to_engine.connect()

    with to_connection.begin():
        if tables is not None:
            log.debug("permitting only tables: %s", tables)

        for from_url in from_urls:
            # normalize the URL
            log.info("copying from %s, fetching %i at a time", from_url, fetch)

            # connect to this source
            from_engine = make_engine(from_url)
            from_connection = from_engine.connect()

            # load the appropriate schema
            if schema is None:
                from sqlalchemy.schema import MetaData

                metadata = MetaData(bind=from_connection, reflect=True)
            else:
                from cargo.sugar import value_by_name

                metadata = value_by_name(schema)

            # copy its data
            for sorted_table in metadata.sorted_tables:
                if tables is None or sorted_table.name in tables:
                    if sorted_table.exists(bind=from_connection):
                        from cargo.sql.actions import copy_table

                        log.debug("copying table %s", sorted_table.name)

                        copy_table(
                            from_connection,
                            to_connection,
                            sorted_table,
                            where=where,
                            fetch=fetch,
                        )
                    else:
                        log.debug("table %s does not exist in %s",
                                  sorted_table.name, from_url)

            # done
            from_engine.dispose()

    # done
    to_engine.dispose()