Skip to content

Support single values instead of (min, max) tuples as summary search filters#672

Merged
munrojm merged 1 commit intomaterialsproject:mainfrom
janosh:main
Sep 23, 2022
Merged

Support single values instead of (min, max) tuples as summary search filters#672
munrojm merged 1 commit intomaterialsproject:mainfrom
janosh:main

Conversation

@janosh
Copy link
Copy Markdown
Member

@janosh janosh commented Sep 15, 2022

This query currently errors

from pymatgen.ext.matproj import MPRester

MPRester().summary.search(
    num_elements=1, fields=["material_id", "pretty_formula", "energy_per_atom"]
)

with

TypeError: 'int' object is not subscriptable

since it should be num_elements=[1, 1] instead of num_elements=1.

The error message isn't very clear so one improvement might be to ask the user to pass a tuple instead. This PR instead converts the value to a tuple on the fly when detecting int:

if isinstance(num_elements, int):
    num_elements = (num_elements, num_elements)

In the case of summary.search, it converts all values to tuple when detecting int or float.

        for param, value in locals().items():
            if param in min_max_name_dict and value:
                if isinstance(value, (int, float)):
                    value = (value, value)

Let me know if that's an acceptable change?

e.g. num_elements=1 instead of num_elements=[1, 1]
@janosh
Copy link
Copy Markdown
Member Author

janosh commented Sep 23, 2022

@munrojm I think the CI errors here might be unrelated?

try:
    data = json.loads(response.text)["detail"]
except (JSONDecodeError, KeyError):
    data = f"Response {response.text}"
if isinstance(data, str):
    message = data
else:
    try:
        message = ", ".join(
            f"{entry['loc'][1]} - {entry['msg']}" for entry in data
        )
    except (KeyError, IndexError):
        message = str(data)

raise MPRestError(
    f"REST query returned with error status code {response.status_code} "
    f"on URL {response.url} with message:\n{message}"
)
mp_api.client.core.client.MPRestError: REST query returned with error status code 401 on URL https://api.materialsproject.org/bonds/?_all_fields=True&material_ids=mp-149&_limit=1 with message:
Response {
  "message":"No API key found in request"
}

@munrojm
Copy link
Copy Markdown
Member

munrojm commented Sep 23, 2022

@janosh, yes sorry for not getting back to you on this. If your local tests have passed I am good with merging.

@munrojm munrojm added the release:patch Patch release label Sep 23, 2022
@janosh
Copy link
Copy Markdown
Member Author

janosh commented Sep 23, 2022

Ah, I'd previously only run the thermo and summary tests which had passed. Just reran all tests locally and got 1 failure in tests/test_materials.py which again looks unrelated:

mp_api.client.core.client.MPRestError: REST query returned with error status code 504 on URL https://api.materialsproject.org/materials/?deprecated=False&exclude_elements=Si&_all_fields=True&_limit=1 with message:
E           Server timed out trying to obtain data. Try again with a smaller request.

@munrojm
Copy link
Copy Markdown
Member

munrojm commented Sep 23, 2022

Yup, no worries. Thanks for the change! Merging now.

@munrojm munrojm merged commit f29b2e0 into materialsproject:main Sep 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release:patch Patch release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants