예제 #1
0
def table_definition(table_name):
    """
    Get the source of a table function.

    If a table is registered DataFrame and not a function then all that is
    returned is {'type': 'dataframe'}.

    If the table is a registered function then the JSON returned has keys
    "type", "filename", "lineno", "text", and "html". "text" is the raw
    text of the function, "html" has been marked up by Pygments.

    """
    if orca.table_type(table_name) == 'dataframe':
        return jsonify(type='dataframe')

    filename, lineno, source = \
        orca.get_raw_table(table_name).func_source_data()

    html = highlight(source, PythonLexer(), HtmlFormatter())

    return jsonify(type='function',
                   filename=filename,
                   lineno=lineno,
                   text=source,
                   html=html)
예제 #2
0
def drop_table(table_name):

    if orca.is_table(table_name):

        logger.debug("drop_table dropping orca table '%s'" % table_name)

        # don't trigger function call of TableFuncWrapper
        t = orca.get_raw_table(table_name)
        t.clear_cached()

        for column_name in orca.list_columns_for_table(table_name):
            # logger.debug("pop %s.%s: %s" % (table_name, column_name, t.column_type(column_name)))
            orca.orca._COLUMNS.pop((table_name, column_name), None)

        # remove from orca's table list
        orca.orca._TABLES.pop(table_name, None)

    if table_name in _PIPELINE.replaced_tables:

        logger.debug("drop_table forgetting replaced_tables '%s'" % table_name)
        del _PIPELINE.replaced_tables[table_name]

    if table_name in _PIPELINE.last_checkpoint:

        logger.debug("drop_table removing table %s from last_checkpoint" % table_name)

        _PIPELINE.last_checkpoint[table_name] = ''
예제 #3
0
def rewrap(table_name, df=None):
    """
    Add or replace an orca registered table as a unitary DataFrame-backed DataFrameWrapper table

    if df is None, then get the dataframe from orca (table_name should be registered, or
    an error will be thrown) which may involve evaluating added columns, etc.

    If the orca table already exists, deregister it along with any associated columns before
    re-registering it.

    The net result is that the dataframe is a registered orca DataFrameWrapper table with no
    computed or added columns.

    Parameters
    ----------
    table_name
    df

    Returns
    -------
        the underlying df of the rewrapped table
    """

    logger.debug("rewrap table %s inplace=%s" % (table_name, (df is None)))

    if orca.is_table(table_name):

        if df is None:
            logger.debug("rewrap - orca.get_table(%s)" % (table_name, ))
            t = orca.get_table(table_name)
            df = t.to_frame()
        else:
            logger.debug("rewrap - orca.get_raw_table(%s)" % (table_name, ))
            # don't trigger function call of TableFuncWrapper
            t = orca.get_raw_table(table_name)

        t.clear_cached()

        for column_name in orca.list_columns_for_table(table_name):
            # logger.debug("pop %s.%s: %s" % (table_name, column_name, t.column_type(column_name)))
            orca.orca._COLUMNS.pop((table_name, column_name), None)

        # remove from orca's table list
        orca.orca._TABLES.pop(table_name, None)

    assert df is not None

    logger.debug("rewrap - orca.add_table(%s)" % (table_name, ))
    orca.add_table(table_name, df)

    return df
예제 #4
0
파일: server.py 프로젝트: AZMAG/orca
def table_definition(table_name):
    """
    Get the source of a table function.

    If a table is registered DataFrame and not a function then all that is
    returned is {'type': 'dataframe'}.

    If the table is a registered function then the JSON returned has keys
    "type", "filename", "lineno", "text", and "html". "text" is the raw
    text of the function, "html" has been marked up by Pygments.

    """
    if orca.table_type(table_name) == "dataframe":
        return jsonify(type="dataframe")

    filename, lineno, source = orca.get_raw_table(table_name).func_source_data()

    html = highlight(source, PythonLexer(), HtmlFormatter())

    return jsonify(type="function", filename=filename, lineno=lineno, text=source, html=html)
예제 #5
0
def assert_table_can_be_generated(table_name):
    """
    Does a registered table exist as a DataFrame? If a table was registered as a function
    wrapper, this assertion evaluates the function and fails is there are any errors.
    
    In other UrbanSim code, it seem like the accepted way of triggering a table to be 
    evaluated is to run .to_frame() on it. I'm using ._call_func() instead, because I 
    don't need the output and this saves the overhead of copying the DataFrame. Either of
    those methods will be aware of caching, and not regenerate the table if it already
    exists. There no way to tell externally whether a table is cached or not. That might
    be a useful thing to add to the orca API. 
    """
    assert_table_is_registered(table_name)

    if (orca.table_type(table_name) == 'function'):
        try:
            _ = orca.get_raw_table(table_name)._call_func()
        except:
            msg = "Table '%s' is registered but cannot be generated" % table_name
            raise OrcaAssertionError(msg)
    return
예제 #6
0
def household_income_category(persons):
    # calling households_for_estimation.income_category returns an non-indexed  
    # array, so converting to df for now. 
    df = orca.get_raw_table('households').to_frame()
    return misc.reindex(df.income_category, persons.household_id)