Skip to content

Commit

Permalink
Initial blue/green changes (#424)
Browse files Browse the repository at this point in the history
* Capitalize settings variables

* Add db suffix to select stores

* Make fallback db suffix the db version
  • Loading branch information
munrojm committed Nov 3, 2021
1 parent b775db9 commit 1ea24a1
Show file tree
Hide file tree
Showing 14 changed files with 123 additions and 110 deletions.
25 changes: 13 additions & 12 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
default_settings = MAPISettings()

db_uri = os.environ.get("MPCONTRIBS_MONGO_HOST", None)
db_version = os.environ.get("DB_VERSION", default_settings.db_version)
debug = os.environ.get("API_DEBUG", default_settings.debug)
db_version = os.environ.get("DB_VERSION", default_settings.DB_VERSION)
db_suffix = os.environ.get("DB_NAME_SUFFIX", db_version)
debug = os.environ.get("API_DEBUG", default_settings.DEBUG)

materials_store_json = os.environ.get("MATERIALS_STORE", "materials_store.json")
formula_autocomplete_store_json = os.environ.get(
Expand Down Expand Up @@ -72,7 +73,7 @@

materials_store = MongoURIStore(
uri=f"mongodb+srv://{db_uri}",
database="mp_core",
database=f"mp_core_{db_suffix}",
key="material_id",
collection_name=f"materials.core_{db_version}",
)
Expand All @@ -93,21 +94,21 @@

thermo_store = MongoURIStore(
uri=f"mongodb+srv://{db_uri}",
database="mp_core",
database=f"mp_core_{db_suffix}",
key="material_id",
collection_name=f"thermo_{db_version}",
)

dielectric_piezo_store = MongoURIStore(
uri=f"mongodb+srv://{db_uri}",
database="mp_core",
database=f"mp_core_{db_suffix}",
key="task_id",
collection_name="dielectric",
)

magnetism_store = MongoURIStore(
uri=f"mongodb+srv://{db_uri}",
database="mp_core",
database=f"mp_core_{db_suffix}",
key="task_id",
collection_name="magnetism",
)
Expand Down Expand Up @@ -184,7 +185,7 @@

robo_store = MongoURIStore(
uri=f"mongodb+srv://{db_uri}",
database="mp_core",
database=f"mp_core_{db_suffix}",
key="material_id",
collection_name="robocrys",
)
Expand All @@ -198,7 +199,7 @@

insertion_electrodes_store = MongoURIStore(
uri=f"mongodb+srv://{db_uri}",
database="mp_core",
database=f"mp_core_{db_suffix}",
key="battery_id",
collection_name="insertion_electrodes",
)
Expand All @@ -212,28 +213,28 @@

oxi_states_store = MongoURIStore(
uri=f"mongodb+srv://{db_uri}",
database="mp_core",
database=f"mp_core_{db_suffix}",
key="material_id",
collection_name="oxi_states",
)

provenance_store = MongoURIStore(
uri=f"mongodb+srv://{db_uri}",
database="mp_core",
database=f"mp_core_{db_suffix}",
key="material_id",
collection_name="provenance",
)

summary_store = MongoURIStore(
uri=f"mongodb+srv://{db_uri}",
database="mp_core",
database=f"mp_core_{db_suffix}",
key="material_id",
collection_name="summary",
)

es_store = MongoURIStore(
uri=f"mongodb+srv://{db_uri}",
database="mp_core",
database=f"mp_core_{db_suffix}",
key="material_id",
collection_name="electronic_structure",
)
Expand Down
10 changes: 5 additions & 5 deletions src/mp_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@
settings = MAPISettings()

try: # pragma: no cover
if Path(settings.app_path).exists():
mapi = loadfn(settings.app_path)
if Path(settings.APP_PATH).exists():
mapi = loadfn(settings.APP_PATH)
app = mapi.app
else:
app = None
if settings.debug:
print(f"Failed loading App at {settings.app_path}")
if settings.DEBUG:
print(f"Failed loading App at {settings.APP_PATH}")

except Exception as e: # pragma: no cover
# Something went wrong with loading default app
if settings.debug:
if settings.DEBUG:
print("Failed loading App")
print(e)
app = None
2 changes: 1 addition & 1 deletion src/mp_api/core/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(
debug=False,
heartbeat_meta={
"pymatgen": pmg_version,
"db_version": MAPISettings().db_version,
"db_version": MAPISettings().DB_VERSION,
},
description=None,
tags_meta=None,
Expand Down
2 changes: 1 addition & 1 deletion src/mp_api/core/ratelimit.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ def check_limit():


if "api.materialsproject" in DEFAULT_ENDPOINT:
check_limit = limits(calls=MAPISettings().requests_per_min, period=60)(check_limit)
check_limit = limits(calls=MAPISettings().REQUESTS_PER_MIN, period=60)(check_limit)
check_limit = sleep_and_retry(check_limit)
14 changes: 8 additions & 6 deletions src/mp_api/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,24 @@ class MAPISettings(BaseSettings):
python module
"""

app_path: str = Field(
APP_PATH: str = Field(
"~/mapi.json", description="Path for the default MAPI JSON definition"
)

debug: bool = Field(False, description="Turns on debug mode for MAPI")
DEBUG: bool = Field(False, description="Turns on debug mode for MAPI")

test_files: str = Field(
TEST_FILES: str = Field(
os.path.join(os.path.dirname(os.path.abspath(root_dir)), "../../test_files"),
description="Directory with test files",
)

db_version: str = Field("2021_prerelease", description="Database version")
DB_VERSION: str = Field("2021_prerelease", description="Database version")

requests_per_min: int = Field(
DB_NAME_SUFFIX: str = Field(None, description="Database name suffix")

REQUESTS_PER_MIN: int = Field(
100, description="Number of requests per minute to for rate limit."
)

class Config:
env_prefix = "mapi_"
env_prefix = "MAPI_"
4 changes: 0 additions & 4 deletions src/mp_api/routes/materials/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
NumericQuery,
)

from mp_api.core.settings import MAPISettings

from mp_api.routes.materials.query_operators import (
ElementsQuery,
FormulaQuery,
Expand All @@ -25,8 +23,6 @@
FormulaAutoCompleteQuery,
)

from pymongo import MongoClient # type: ignore


def find_structure_resource(materials_store):
resource = PostOnlyResource(
Expand Down
2 changes: 1 addition & 1 deletion tests/materials/test_query_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def test_find_structure_query():
op = FindStructureQuery()

structure = Structure.from_file(
os.path.join(MAPISettings().test_files, "Si_mp_149.cif")
os.path.join(MAPISettings().TEST_FILES, "Si_mp_149.cif")
)
assert op.query(
structure=structure.as_dict(), ltol=0.2, stol=0.3, angle_tol=5, limit=1
Expand Down
2 changes: 1 addition & 1 deletion tests/mpcomplete/test_query_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def test_mpcomplete_post_query():
op = MPCompletePostQuery()

structure = Structure.from_file(
os.path.join(MAPISettings().test_files, "Si_mp_149.cif")
os.path.join(MAPISettings().TEST_FILES, "Si_mp_149.cif")
)

assert op.query(
Expand Down
14 changes: 8 additions & 6 deletions tests/synthesis/test_adaptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@


def test_string2comp():
assert string2comp('BaTiO3') == Composition('BaTiO3')
assert string2comp('LiOH·H2O') == Composition('LiOH')
assert string2comp('TiO2·BaCO3') == Composition('TiO2')
assert string2comp("BaTiO3") == Composition("BaTiO3")
assert string2comp("LiOH·H2O") == Composition("LiOH")
assert string2comp("TiO2·BaCO3") == Composition("TiO2")


def test_convert_recipe():
with open(os.path.join(MAPISettings().test_files, "synth_doc_adaptor.json")) as file:
with open(
os.path.join(MAPISettings().TEST_FILES, "synth_doc_adaptor.json")
) as file:
synth_doc = load(file)

converted = convert_recipe(synth_doc['src'])
assert converted == synth_doc['product']
converted = convert_recipe(synth_doc["src"])
assert converted == synth_doc["product"]
138 changes: 72 additions & 66 deletions tests/synthesis/test_adaptor_synpro.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,95 +4,101 @@
from pymatgen.core import Composition

from mp_api import MAPISettings
from mp_api.routes.synthesis.data_adaptor_synpro import convert_value, convert_conditions, convert_one, \
get_material_formula
from mp_api.routes.synthesis.data_adaptor_synpro import (
convert_value,
convert_conditions,
convert_one,
get_material_formula,
)


def test_convert_value():
src = {
"min": None,
"max": 350.0,
"values": [350.0],
"tok_ids": [19],
"units": "°C"
}
src = {"min": None, "max": 350.0, "values": [350.0], "tok_ids": [19], "units": "°C"}
product = {
'min_value': None,
'max_value': 350.0,
'values': [350.0],
'units': "°C",
"min_value": None,
"max_value": 350.0,
"values": [350.0],
"units": "°C",
}
assert convert_value(src) == product


def test_conditions():
src = {
"temperature": [{"min": None, "max": 350.0, "values": [350.0], "tok_ids": [19], "units": "°C"}],
"time": [{"max": 3.0, "min": 3.0, "values": [3.0], "tok_ids": [23], "units": "h"}],
"environment": ["air", "O2"]
"temperature": [
{
"min": None,
"max": 350.0,
"values": [350.0],
"tok_ids": [19],
"units": "°C",
}
],
"time": [
{"max": 3.0, "min": 3.0, "values": [3.0], "tok_ids": [23], "units": "h"}
],
"environment": ["air", "O2"],
}
product = {
'heating_temperature': [{'min_value': None, 'max_value': 350.0, 'values': [350.0], 'units': "°C", }],
'heating_time': [{'min_value': 3.0, 'max_value': 3.0, 'values': [3.0], 'units': 'h'}],
'heating_atmosphere': ["air", "O2"],
'mixing_device': None,
'mixing_media': None
"heating_temperature": [
{"min_value": None, "max_value": 350.0, "values": [350.0], "units": "°C",}
],
"heating_time": [
{"min_value": 3.0, "max_value": 3.0, "values": [3.0], "units": "h"}
],
"heating_atmosphere": ["air", "O2"],
"mixing_device": None,
"mixing_media": None,
}
assert convert_conditions(src, 'HeatingOperation') == product
assert convert_conditions(src, "HeatingOperation") == product

product = {
'heating_temperature': [{'min_value': None, 'max_value': 350.0, 'values': [350.0], 'units': "°C", }],
'heating_time': [{'min_value': 3.0, 'max_value': 3.0, 'values': [3.0], 'units': 'h'}],
'heating_atmosphere': [],
'mixing_device': 'O2',
'mixing_media': 'air'
"heating_temperature": [
{"min_value": None, "max_value": 350.0, "values": [350.0], "units": "°C",}
],
"heating_time": [
{"min_value": 3.0, "max_value": 3.0, "values": [3.0], "units": "h"}
],
"heating_atmosphere": [],
"mixing_device": "O2",
"mixing_media": "air",
}
assert convert_conditions(src, 'MixingOperation') == product
assert convert_conditions(src, "MixingOperation") == product


def test_get_material_formula():
assert get_material_formula({
"material_formula": "NH4H2PO4",
"composition": [
{
"formula": "NH4H2PO4",
"amount": "1",
"elements": {
"N": "1",
"H": "6",
"P": "1",
"O": "4"
},
}
],
}) == Composition('NH4H2PO4')
assert get_material_formula(
{
"material_formula": "NH4H2PO4",
"composition": [
{
"formula": "NH4H2PO4",
"amount": "1",
"elements": {"N": "1", "H": "6", "P": "1", "O": "4"},
}
],
}
) == Composition("NH4H2PO4")

assert get_material_formula({
"material_formula": "TiO2-2BaCO3",
"composition": [
{
"formula": "TiO2",
"amount": "1",
"elements": {
"Ti": "1",
"O": "2"
},
},
{
"formula": "BaCO3",
"amount": "2",
"elements": {
"Ba": "1",
"C": "1",
"O": "3"
assert get_material_formula(
{
"material_formula": "TiO2-2BaCO3",
"composition": [
{"formula": "TiO2", "amount": "1", "elements": {"Ti": "1", "O": "2"},},
{
"formula": "BaCO3",
"amount": "2",
"elements": {"Ba": "1", "C": "1", "O": "3"},
},
}
],
}) == Composition('TiBa2C2O8')
],
}
) == Composition("TiBa2C2O8")


def test_convert_one():
with open(os.path.join(MAPISettings().test_files, "synth_doc_adaptor_synpro.json")) as file:
with open(
os.path.join(MAPISettings().TEST_FILES, "synth_doc_adaptor_synpro.json")
) as file:
synth_doc = load(file)

assert convert_one(synth_doc['src']) == synth_doc['product']
assert convert_one(synth_doc["src"]) == synth_doc["product"]
Loading

0 comments on commit 1ea24a1

Please sign in to comment.