generated from materialsvirtuallab/python_template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move db related code to another module.
- Loading branch information
Showing
3 changed files
with
54 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Software tools for working with MatPES.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
"""Tools for directly working with a MatPES style DB.""" | ||
|
||
from __future__ import annotations | ||
|
||
import pandas as pd | ||
from pymongo import MongoClient | ||
|
||
|
||
class MatPESDB: | ||
"""A MatPES DB object. This requires access to a MatPES style DB. Typically meant for developers.""" | ||
|
||
FUNCTIONALS = ("PBE", "r2SCAN") | ||
|
||
def __init__(self, dbname="matpes"): | ||
""" | ||
Args: | ||
dbname (str): The name of the MatPES DB. | ||
""" | ||
client = MongoClient() | ||
self.db = client[dbname] | ||
|
||
def get_df(self, functional: str) -> pd.DataFrame: | ||
""" | ||
Retrieve data for the given functional from the MongoDB database. | ||
Args: | ||
functional (str): The functional to query (e.g., 'PBE'). | ||
Returns: | ||
pd.DataFrame: Dataframe containing the data. | ||
""" | ||
collection = self.db[functional] | ||
return pd.DataFrame( | ||
collection.find( | ||
{}, | ||
projection=[ | ||
"elements", | ||
"energy", | ||
"chemsys", | ||
"cohesive_energy_per_atom", | ||
"formation_energy_per_atom", | ||
"natoms", | ||
"nelements", | ||
], | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters