# Path: rivus/main/rivus.py
# def get_timeseries(prob):
#     """Retrieve time-dependent variables/quantities.
# 
#     Example:
#         source, flows, hubs, proc_io, proc_tau = get_timeseries(prob)
# 
#     Args:
#         prob: a rivus model instance
# 
#     Returns:
#         (source, flows, hubs, proc_io, proc_tau) tuple
#     """
# 
#     source = get_entity(prob, 'Rho')
#     flows = get_entities(prob, ['Pin', 'Pot', 'Psi', 'Sigma'])
#     hubs = get_entity(prob, 'Epsilon_hub')
#     proc_io = get_entities(prob, ['Epsilon_in', 'Epsilon_out'])
#     proc_tau = get_entity(prob, 'Tau')
# 
#     # fill NaN's
#     flows.fillna(0, inplace=True)
#     proc_io.fillna(0, inplace=True)
# 
#     # drop all-zero rows
#     source = source[source > 0].unstack()
#     flows = flows[flows.sum(axis=1) > 0].applymap(round)
# 
#     hubs = hubs[hubs > 0].unstack().fillna(0)
#     if hubs.empty:
#         hubs = pd.DataFrame([])
#     else:
#         hubs = hubs.applymap(round)
# 
#     proc_io = proc_io[proc_io.sum(axis=1) > 0]
#     if not proc_io.empty:
#         proc_io = proc_io.applymap(round)
# 
#     proc_tau = proc_tau[proc_tau.apply(round) > 0]
#     if not proc_tau.empty:
#         proc_tau = proc_tau.unstack().fillna(0).applymap(round)
# 
#     return source, flows, hubs, proc_io, proc_tau
# 
# def get_constants(prob):
#     """Retrieve time-independent variables/quantities.
# 
#     Args:
#         prob: a rivus model instance
# 
#     Returns:
#         (costs, pmax, kappa_hub, kappa_process) tuple
# 
#     Example:
#         costs, pmax, kappa_hub, kappa_process = get_constants(prob)
#     """
#     costs = get_entity(prob, 'costs')
#     Pmax = get_entity(prob, 'Pmax')
#     Kappa_hub = get_entity(prob, 'Kappa_hub')
#     Kappa_process = get_entity(prob, 'Kappa_process')
# 
#     # nicer index names
#     Pmax.index.names = ['Vertex1', 'Vertex2', 'commodity']
#     Kappa_hub.index.names = ['Vertex1', 'Vertex2', 'process']
# 
#     # drop all-zero rows
#     Pmax = Pmax[Pmax > 0].unstack().fillna(0)
#     Kappa_hub = Kappa_hub[Kappa_hub > 0].unstack().fillna(0)
#     Kappa_process = Kappa_process[Kappa_process > 0].unstack().fillna(0)
# 
#     # Drop all 0 commodities
#     for column in Pmax:
#         if all(Pmax[column] == 0):
#             del Pmax[column]
# 
#     # round to integers
#     if Pmax.empty:
#         Pmax = pd.DataFrame([])
#     else:
#         Pmax = Pmax.applymap(round)
#     if Kappa_hub.empty:
#         Kappa_hub = pd.DataFrame([])
#     else:
#         Kappa_hub = Kappa_hub.applymap(round)
#     if Kappa_process.empty:
#         Kappa_process = pd.DataFrame([])
#     else:
#         Kappa_process = Kappa_process.applymap(round)
#     costs = costs.apply(round)
# 
#     return costs, Pmax, Kappa_hub, Kappa_process


import warnings
import json
from datetime import datetime
from pandas import Series, DataFrame, read_sql
from geopandas import GeoDataFrame
from shapely.wkt import loads as wkt_load
from ..main.rivus import get_timeseries, get_constants
    ------
    Exception caught during data export.
    """
    if run_id is not None:
        run_id = int(run_id)
    else:
        run_id = init_run(engine, **run_data) if run_data else init_run(engine)
    print('\tStore params for run <{}>'.format(run_id))

    try:
        # Parameter DataFrames
        # --------------------
        # The order of frames -> table does matter.
        # Followings apply to `frames`:
        # `process_commodity` ---> after `process` and `commodity`
        # `vertex`, `area_demand` and `time` ---> after `commodity`
        # `edge` ---> after `area_demand`
        independend = ['commodity', 'process']
        comm_dependent = ['process_commodity', 'area_demand', 'vertex', 'time']
        area_dependent = ['edge']
        frames = independend + comm_dependent + area_dependent
        for frame in frames:
            # frame should be the same as df.name... but GeoDataFrames does not
            # have a name etc..
            df = prob.params[frame]
            _fill_table(engine, frame, df, run_id)

        # Result DataFrames
        # -----------------
        series_names = ['source', 'flow', 'hub', 'proc_io', 'proc_tau']

# Path: rivus/main/rivus.py
# def get_timeseries(prob):
#     """Retrieve time-dependent variables/quantities.
# 
#     Example:
#         source, flows, hubs, proc_io, proc_tau = get_timeseries(prob)
# 
#     Args:
#         prob: a rivus model instance
# 
#     Returns:
#         (source, flows, hubs, proc_io, proc_tau) tuple
#     """
# 
#     source = get_entity(prob, 'Rho')
#     flows = get_entities(prob, ['Pin', 'Pot', 'Psi', 'Sigma'])
#     hubs = get_entity(prob, 'Epsilon_hub')
#     proc_io = get_entities(prob, ['Epsilon_in', 'Epsilon_out'])
#     proc_tau = get_entity(prob, 'Tau')
# 
#     # fill NaN's
#     flows.fillna(0, inplace=True)
#     proc_io.fillna(0, inplace=True)
# 
#     # drop all-zero rows
#     source = source[source > 0].unstack()
#     flows = flows[flows.sum(axis=1) > 0].applymap(round)
# 
#     hubs = hubs[hubs > 0].unstack().fillna(0)
#     if hubs.empty:
#         hubs = pd.DataFrame([])
#     else:
#         hubs = hubs.applymap(round)
# 
#     proc_io = proc_io[proc_io.sum(axis=1) > 0]
#     if not proc_io.empty:
#         proc_io = proc_io.applymap(round)
# 
#     proc_tau = proc_tau[proc_tau.apply(round) > 0]
#     if not proc_tau.empty:
#         proc_tau = proc_tau.unstack().fillna(0).applymap(round)
# 
#     return source, flows, hubs, proc_io, proc_tau
# 
# def get_constants(prob):
#     """Retrieve time-independent variables/quantities.
# 
#     Args:
#         prob: a rivus model instance
# 
#     Returns:
#         (costs, pmax, kappa_hub, kappa_process) tuple
# 
#     Example:
#         costs, pmax, kappa_hub, kappa_process = get_constants(prob)
#     """
#     costs = get_entity(prob, 'costs')
#     Pmax = get_entity(prob, 'Pmax')
#     Kappa_hub = get_entity(prob, 'Kappa_hub')
#     Kappa_process = get_entity(prob, 'Kappa_process')
# 
#     # nicer index names
#     Pmax.index.names = ['Vertex1', 'Vertex2', 'commodity']
#     Kappa_hub.index.names = ['Vertex1', 'Vertex2', 'process']
# 
#     # drop all-zero rows
#     Pmax = Pmax[Pmax > 0].unstack().fillna(0)
#     Kappa_hub = Kappa_hub[Kappa_hub > 0].unstack().fillna(0)
#     Kappa_process = Kappa_process[Kappa_process > 0].unstack().fillna(0)
# 
#     # Drop all 0 commodities
#     for column in Pmax:
#         if all(Pmax[column] == 0):
#             del Pmax[column]
# 
#     # round to integers
#     if Pmax.empty:
#         Pmax = pd.DataFrame([])
#     else:
#         Pmax = Pmax.applymap(round)
#     if Kappa_hub.empty:
#         Kappa_hub = pd.DataFrame([])
#     else:
#         Kappa_hub = Kappa_hub.applymap(round)
#     if Kappa_process.empty:
#         Kappa_process = pd.DataFrame([])
#     else:
#         Kappa_process = Kappa_process.applymap(round)
#     costs = costs.apply(round)
# 
#     return costs, Pmax, Kappa_hub, Kappa_process


# Path: rivus/io/db.py
import warnings
import json
from datetime import datetime
from pandas import Series, DataFrame, read_sql
from geopandas import GeoDataFrame
from shapely.wkt import loads as wkt_load
from ..main.rivus import get_timeseries, get_constants

    ------
    Exception caught during data export.
    """
    if run_id is not None:
        run_id = int(run_id)
    else:
        run_id = init_run(engine, **run_data) if run_data else init_run(engine)
    print('\tStore params for run <{}>'.format(run_id))

    try:
        # Parameter DataFrames
        # --------------------
        # The order of frames -> table does matter.
        # Followings apply to `frames`:
        # `process_commodity` ---> after `process` and `commodity`
        # `vertex`, `area_demand` and `time` ---> after `commodity`
        # `edge` ---> after `area_demand`
        independend = ['commodity', 'process']
        comm_dependent = ['process_commodity', 'area_demand', 'vertex', 'time']
        area_dependent = ['edge']
        frames = independend + comm_dependent + area_dependent
        for frame in frames:
            # frame should be the same as df.name... but GeoDataFrames does not
            # have a name etc..
            df = prob.params[frame]
            _fill_table(engine, frame, df, run_id)

        # Result DataFrames
        # -----------------
        series_names = ['source', 'flow', 'hub', 'proc_io', 'proc_tau']

        series = get_timeseries(prob)  # source, flows, hubs, proc_io, proc_tau