Skip to main content

SQLAlchemy model for ASE Atoms objects

Project description

ASE DB

This package provides a SQL Alchemy model to the ASE Atoms object.

For more information, see the documentation.

Basic Usage

The AtomsModel is the main model that translates an atoms object (and attached calculator info) into a SQL model.

from asedb import AtomsModel, Element, initialize_engine, make_sqlite_engine
import ase
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine


engine = make_sqlite_engine("foo.db")  # Helper function to create a SQL Alchemy engine for sqlite
initialize_engine(engine)  # Create necessary schema/tables

Session = sessionmaker(bind=engine)
session = Session()

atoms = ase.Atoms('H2',
           positions=[[0, 0, 0],
                      [0, 0, 0.7]])

model = AtomsModel.from_atoms(atoms)

session.add(model)
session.commit()

model_id = model.id

loaded = session.query(AtomsModel).where(AtomsModel.id == model_id).scalar()
atoms_loaded = loaded.to_atoms()  # The atoms object loaded from the database

print(atoms_loaded)  # Atoms(symbols='H2', pbc=False, tags=...)

Here, we used the make_sqlite_engine helper function to create the SQL Alchemy engine object for a sqlite database. You can also create your own engine, e.g. for postgres:

import urllib.parse
from sqlalchemy import create_engine

def make_engine():
    database = os.environ["PG_DATABASE"]
    user = os.environ["PG_USER"]
    password = urllib.parse.quote_plus(os.environ["PG_PASSWORD"])
    host = os.environ["PG_HOST"]
    port = os.environ["PG_PORT"]
    connection_string = (
        f"postgresql+psycopg2://{user}:{password}@{host}:{port}/{database}"
    )
    return create_engine(connection_string)

where the parameters for the connection parameters have been stored in the environment variables.

Querying

We can construct queries using the SQL Alchemy models, e.g. looking for all atoms objects with at least 2 hydrogen atoms:

# Fetch all AtomsModel objects that fit the query
results = (session.query(AtomsModel)
                  .join(Element)
                  .where((Element.symbol == 'H') & (Element.count >= 2))
                  .all())
# Convert them into actual Atoms objects
all_atoms = [res.to_atoms() for res in results]

Schema

By default, the schema asedb is used - except for sqlite databases, which doesn't utilize schemas. In this case, the make_sqlite_engine helper function ensures the schema is remapped to None.

For any "real" database connections, such as Postgres, the schema asedb will be used.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

asedb-0.0.1b0.tar.gz (23.5 kB view details)

Uploaded Source

Built Distribution

asedb-0.0.1b0-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file asedb-0.0.1b0.tar.gz.

File metadata

  • Download URL: asedb-0.0.1b0.tar.gz
  • Upload date:
  • Size: 23.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.8

File hashes

Hashes for asedb-0.0.1b0.tar.gz
Algorithm Hash digest
SHA256 e73b6edb14f866038c87e5da9ad9b4cbc43f69b0ddb3549868c4153611bcd982
MD5 509891493885c4a7bb8a931df00de68b
BLAKE2b-256 999cfe7dc6a3bdfd9c87948046a716f9dd97780df477498830e5fb621cf11148

See more details on using hashes here.

File details

Details for the file asedb-0.0.1b0-py3-none-any.whl.

File metadata

  • Download URL: asedb-0.0.1b0-py3-none-any.whl
  • Upload date:
  • Size: 11.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.8

File hashes

Hashes for asedb-0.0.1b0-py3-none-any.whl
Algorithm Hash digest
SHA256 9b6a50c7e684db3b6d01e0a3601cac53e228f991f5009cd29da075c37a8866f2
MD5 062be6fe6af0957dd78b333fbacbf90d
BLAKE2b-256 a164580ccabfcd362ba0904524968029f6b9737671999343942326de97592d8d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page