Example #1
0
def get_data_sqla(projections, sliders_dict, quantities, plot_info):
    """Query database using SQLAlchemy.
    
    Note: For efficiency, this uses the the sqlalchemy.sql interface which does
    not go via the (more convenient) ORM.
    """
    from import_db import automap_table, engine
    from sqlalchemy.sql import select, and_

    Table = automap_table(engine)

    selections = []
    for label in projections:
        selections.append(getattr(Table, label))

    filters = []
    for k, v in sliders_dict.items():
        if isinstance(v, RangeSlider):
            if not v.value == quantities[k]['range']:
                f = getattr(Table, k).between(v.value[0], v.value[1])
                filters.append(f)
        elif isinstance(v, CheckboxButtonGroup):
            if not len(v.active) == len(v.labels):
                f = getattr(Table, k).in_([v.tags[i] for i in v.active])
                filters.append(f)

    s = select(selections).where(and_(*filters))

    #s = select(selections)

    results = engine.connect().execute(s).fetchall()

    nresults = len(results)

    if not results:
        plot_info.text = "No matching Complexes found."
        return data_empty
    elif nresults > max_points:
        results = results[:max_points]
        plot_info.text = "{} Complexes found.\nPlotting {}...".format(
            nresults, max_points)
    else:
        plot_info.text = "{} Complexes found.\nPlotting {}...".format(
            nresults, nresults)

    # x,y position
    x, y, clrs, names, filenames = zip(*results)
    x = list(map(float, x))
    y = list(map(float, y))

    if projections[2] == 'bond_type':
        #clrs = map(lambda clr: bondtypes.index(clr), clrs)
        clrs = list(map(str, clrs))
    else:
        clrs = list(map(float, clrs))

    return dict(x=x, y=y, filename=filenames, color=clrs, name=names)
def get_sqlite_data(name, plot_info):
    """Query the sqlite database"""
    from import_db import automap_table, engine
    from sqlalchemy.orm import sessionmaker

    # configure Session class with desired options
    Session = sessionmaker(bind=engine)
    session = Session()

    Table = automap_table(engine)

    query = session.query(Table).filter_by(name=str(name))

    nresults = query.count()
    if nresults == 0:
        plot_info.text = "No matching COF found."
        return None
    return query.one()
Example #3
0
def get_data_sqla(projections, sliders_dict, quantities, plot_info):
    """Query database using SQLAlchemy.

    Note: For efficiency, this uses the the sqlalchemy.sql interface which does
    not go via the (more convenient) ORM.
    """
    from import_db import automap_table, engine
    from sqlalchemy.sql import select, and_

    Table = automap_table(engine)

    selections = []
    for label in projections:
        selections.append(getattr(Table, label))

    filters = []
    for k, v in sliders_dict.items():
        if isinstance(v, RangeSlider):
            if not v.value == quantities[k]["range"]:
                f = getattr(Table, k).between(v.value[0], v.value[1])
                filters.append(f)
        elif isinstance(v, CheckboxButtonGroup):
            if not len(v.active) == len(v.labels):
                f = getattr(Table, k).in_([v.tags[i] for i in v.active])
                filters.append(f)

    s = select(selections).where(and_(*filters))

    results = engine.connect().execute(s).fetchall()

    nresults = len(results)
    if not results:
        plot_info.text = "No matching structure found."
        return data_empty
    elif nresults > max_points:
        results = results[:max_points]
        plot_info.text = "{} frameworks found.\nPlotting {}...".format(
            nresults, max_points)
    else:
        plot_info.text = "{} frameworks found.\nPlotting {}...".format(
            nresults, nresults)

    # x,y position
    x, y, clrs, sampled, names, filenames = zip(*results)
    x = list(map(float, x))
    y = list(map(float, y))

    sampled_ = [20 if s == "sampled" else 10 for s in sampled]
    lw = [2 if s == "sampled" else 0.1 for s in sampled]

    if projections[2] == "group":
        # clrs = map(lambda clr: bondtypes.index(clr), clrs)
        clrs = list(clrs)
        # df = pd.DataFrame({
        #     'x': x,
        #     'y': y,
        #     'filename': filenames,
        #     'name': names,
        #     'color': clrs
        # })
        #
        # my_own_order = ['COFs', 'MOFs', 'zeolites', 'sampled']
        # my_own_order_dict = {key: i for i, key in enumerate(my_own_order)}
        # inv_my_own_order_dict = {v: k for k, v in my_own_order_dict.items()}
        # df['color_mapped'] = df['color'].map(my_own_order_dict)
        # df.sort_values(by=['color_mapped'], inplace=True)
        # x = df['x'].astype(float).to_list()
        # y = df['y'].astype(float).to_list()
        # filenames = df['filename'].to_list()
        # clrs = df['color'].to_list()
        # names = df['name'].to_list()
    else:
        clrs = list(map(float, clrs))

    return dict(x=x,
                y=y,
                filename=filenames,
                color=clrs,
                sampled=sampled_,
                name=names,
                lw=lw)
Example #4
0
def get_data_sqla(projections, sliders_dict, quantities, plot_info):
    """Query database using SQLAlchemy.

    Note: For efficiency, this uses the the sqlalchemy.sql interface which does
    not go via the (more convenient) ORM.
    """
    from import_db import automap_table, engine
    from sqlalchemy.sql import select, and_

    Table = automap_table(engine, table_name='mofs')

    selections = []
    for label in projections:
        selections.append(getattr(Table, label))

    filters = []
    for k, v in sliders_dict.items():
        if isinstance(v, RangeSlider):
            if not v.value == quantities[k]['range']:
                f = getattr(Table, k).between(v.value[0], v.value[1])
                filters.append(f)
        elif isinstance(v, CheckboxButtonGroup):
            if not len(v.active) == len(v.labels):
                f = getattr(Table, k).in_([v.tags[i] for i in v.active])
                filters.append(f)

    # Leopold: Some structures have void_fraction = -1
    # Pete: Likely, some structures do not have measurable pores using zeo++.
    # This doesn't necessarily mean 0 uptake however, as zeo++ uses hard
    # spheres to measure pore space, while a lennard-jones function governs the
    # adsorption measured by GCMC.
    # The selectivity ratio would be high in these cases, as even slight CO2
    # adsorption but significantly less N2 will yield a high selectivity. I
    # think I filtered these cases out of the plot, as they would be
    # uninteresting from a materials design point of view.
    filters.append(Table.void_fraction >= 0)

    s = select(selections).where(and_(*filters))

    results = engine.connect().execute(s).fetchall()

    nresults = len(results)
    if not results:
        plot_info.text = "No matching MOFs found."
        return data_empty
    elif nresults > max_points:
        results = results[:max_points]
        plot_info.text = "{} MOFs found.\nPlotting {}...".format(
            nresults, max_points)
    else:
        plot_info.text = "{} MOFs found.\nPlotting {}...".format(
            nresults, nresults)

    # x,y position
    x, y, clrs, names, filenames = zip(*results)
    x = list(map(float, x))
    y = list(map(float, y))

    if projections[2] == 'bond_type':
        #clrs = map(lambda clr: bondtypes.index(clr), clrs)
        clrs = list(map(str, clrs))
    else:
        clrs = list(map(float, clrs))

    return dict(x=x, y=y, filename=filenames, color=clrs, name=names)