Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update/SOF 7456 #13

Merged
merged 3 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/py/mat3ra/standata/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,25 @@ def get_by_categories(cls, *tags: str) -> List[dict]:
categories = cls.data.standataConfig.convert_tags_to_categories_list(*tags)
matching_filenames = cls.data.standataConfig.get_filenames_by_categories(*categories)
return cls.data.filesMapByName.get_objects_by_filenames(matching_filenames)

@classmethod
def get_by_name_and_categories(cls, name: str, *tags: str) -> dict:
"""
Returns the first entity that matches both the name regex and all categories.

Args:
name: Name to match with regex
*tags: Category tags to match

Returns:
First matching entity
"""
categories = cls.data.standataConfig.convert_tags_to_categories_list(*tags)
category_matches = cls.data.standataConfig.get_filenames_by_categories(*categories)
name_matches = cls.data.standataConfig.get_filenames_by_regex(name)
matching_filenames = [f for f in name_matches if f in category_matches]

if not matching_filenames:
raise ValueError(f"No matches found for name '{name}' and categories {tags}")

return cls.data.filesMapByName.get_objects_by_filenames(matching_filenames)[0]
10 changes: 7 additions & 3 deletions tests/py/unit/test_materials_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,27 @@ def test_categories_data():


def test_get_by_name():
"""Assert correct information if found about a material."""
material = Materials.get_by_name_first_match("Graphene")
assert type(material) == dict
assert material["name"] == "C, Graphene, HEX (P6/mmm) 2D (Monolayer), 2dm-3993"
assert material["isNonPeriodic"] is False


def test_get_by_categories():
"""Assert correct information if found about a material."""
material = Materials.get_by_categories("2D")
assert isinstance(material, list)
assert material[0]["name"] == "C, Graphene, HEX (P6/mmm) 2D (Monolayer), 2dm-3993"


def test_get_material_data():
"""Assert correct information if found about a material."""
material = materials_data["filesMapByName"]["C-[Graphene]-HEX_[P6%2Fmmm]_2D_[Monolayer]-[2dm-3993].json"]
assert type(material) == dict
assert material["name"] == "C, Graphene, HEX (P6/mmm) 2D (Monolayer), 2dm-3993"
assert material["isNonPeriodic"] is False


def test_get_by_name_and_categories():
material = Materials.get_by_name_and_categories("MoS2", "2D")
assert type(material) == dict
assert material["name"] == "MoS2, Molybdenum Disulfide, HEX (P-6m2) 2D (Monolayer), 2dm-3150"
assert material["isNonPeriodic"] is False
Loading