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

Implementation of an OPTIMADE client #1154

Merged
merged 5 commits into from May 25, 2022
Merged

Implementation of an OPTIMADE client #1154

merged 5 commits into from May 25, 2022

Conversation

ml-evs
Copy link
Member

@ml-evs ml-evs commented May 8, 2022

An implementation of a client that pulls all known databases from the providers (or uses a defined base URL) and asynchronously queries them with a nice rich interface.

Still to-do for this PR:

  • Remove non-async version and replace with throttled async (i.e. one worker) Refactored such that keeping the non-async version is not a huge burden, which makes things easier to test in Jupyter etc.
  • Enable Rich output inside Jupyter and use the Jupyter async event loop
  • Return results in a sensible way:
    • big dictionary broken down by base URL
    • save to disk by default
    • read caches from disk for repeated queries by default
  • Add way to limit the number of responses from any single provider (to avoid e.g. downloading 4m structures from NOMAD)
  • Support for response fields
  • Pre-flight filter validation using the filtertransformer
  • Error handling and recovery (e.g., using unsupported pagination/sort)

Future design to-do:

  • Make cache work with limits and enable resumable queries
  • Add direct support for @CasperWA's gateway instead of directly querying providers
  • Dynamic/provider-specific filter validation (supported keywords/provider fields/query params etc)
  • For big queries, add async methods for paginating through a single provider
  • Optional validation of incoming data with models
  • Make async work with a running event loop in a better way

@codecov
Copy link

codecov bot commented May 10, 2022

Codecov Report

Merging #1154 (697f294) into master (afce99c) will decrease coverage by 0.45%.
The diff coverage is 83.89%.

❗ Current head 697f294 differs from pull request most recent head 94c9b6a. Consider uploading reports for the commit 94c9b6a to get more accurate results

@@            Coverage Diff             @@
##           master    #1154      +/-   ##
==========================================
- Coverage   92.41%   91.95%   -0.46%     
==========================================
  Files          68       72       +4     
  Lines        3914     4265     +351     
==========================================
+ Hits         3617     3922     +305     
- Misses        297      343      +46     
Flag Coverage Δ
project 91.95% <83.89%> (-0.46%) ⬇️
validator 91.20% <74.85%> (-1.21%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
optimade/client/client.py 82.05% <82.05%> (ø)
optimade/client/utils.py 84.21% <84.21%> (ø)
optimade/client/cli.py 87.75% <87.75%> (ø)
optimade/client/__init__.py 100.00% <100.00%> (ø)
optimade/utils.py 98.33% <100.00%> (+21.86%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update afce99c...94c9b6a. Read the comment docs.

@ml-evs ml-evs force-pushed the ml-evs/client branch 2 times, most recently from ded5573 to 64a9e48 Compare May 19, 2022 19:34
@ml-evs ml-evs marked this pull request as ready for review May 19, 2022 19:34
@ml-evs
Copy link
Member Author

ml-evs commented May 19, 2022

There is probably enough here now for people to test... not sure if we want to split some of the other features mentioned above into separate PRs.

Would be helpful to have at least a minimal implementation of this working before the workshop.

Copy link
Member Author

@ml-evs ml-evs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My last changes will break the tests for now, will update them on the train home!

Copy link
Contributor

@JPBergsma JPBergsma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work writing this in a short time.

optimade/client/client.py Show resolved Hide resolved
optimade/client/utils.py Outdated Show resolved Hide resolved
optimade/client/utils.py Show resolved Hide resolved
docs/getting_started/client.md Outdated Show resolved Hide resolved
docs/getting_started/client.md Outdated Show resolved Hide resolved
INSTALL.md Show resolved Hide resolved
docs/getting_started/client.md Outdated Show resolved Hide resolved
docs/getting_started/client.md Outdated Show resolved Hide resolved
@ml-evs
Copy link
Member Author

ml-evs commented May 24, 2022

Thanks for the ongoing reviews @JPBergsma, I think I have addressed everything and that this is now in reasonable shape for this PR. I have reworked the wishlist of new features above and we can raise some issues to track them, when we are happy with this.

I will rebase this PR into 3 commits: implementation, tests and docs.

Have you been able to get the client running yourself yet?

@ml-evs
Copy link
Member Author

ml-evs commented May 24, 2022

Final to-do(ish):

The client now falls back to synchronous mode when a running event loop is detected, which is the case in Jupyter notebooks and in pytest.

This is a slightly tricky feature that I would like to work on in a new PR, but it does mean the test coverage for this PR will be slightly lower until we can run the async stuff properly.

@ml-evs
Copy link
Member Author

ml-evs commented May 24, 2022

Final to-do(ish):

The client now falls back to synchronous mode when a running event loop is detected, which is the case in Jupyter notebooks and in pytest.

This is a slightly tricky feature that I would like to work on in a new PR, but it does mean the test coverage for this PR will be slightly lower until we can run the async stuff properly.

Just played around with @CasperWA's test setup for the gateway without much luck. The additional trouble is that this client sets up multiple httpx.AsyncClients and executes them with asyncio.run and asyncio.gather. I've tried a few tricks with the nest_asyncio library, without much luck.

@JPBergsma
Copy link
Contributor

I have still have a problem

When I execute the line: optimade-get --filter 'elements HAS "Ag"'

I get the error message:
Traceback (most recent call last): File "/home/kwibus/miniconda3/envs/optimade/bin/optimade-get", line 33, in <module> sys.exit(load_entry_point('optimade', 'console_scripts', 'optimade-get')()) File "/home/kwibus/miniconda3/envs/optimade/bin/optimade-get", line 25, in importlib_load_entry_point return next(matches).load() StopIteration

@JPBergsma
Copy link
Contributor

I think I am missing files that are in this PR. So it seems Git did something wrong. I'll try to delete my local branch and to fetch it again.

@JPBergsma
Copy link
Contributor

Ok, that seems to have worked the command line client is now working.
I'll try to review the changes you made tomorrow.

@ml-evs ml-evs changed the title Initial async client implementation Implementation of an OPTIMADE client May 25, 2022
@ml-evs ml-evs added the enhancement New feature or request label May 25, 2022
@ml-evs ml-evs added the client Issues/PRs relating to the OPTIMADE client. label May 25, 2022
@ml-evs ml-evs added this to the v0.18.0 milestone May 25, 2022
@ml-evs ml-evs force-pushed the ml-evs/client branch 3 times, most recently from 8251d4f to d581e01 Compare May 25, 2022 09:39
@ml-evs
Copy link
Member Author

ml-evs commented May 25, 2022

Just removed the CI tests for assessing coverage for the validator only; I don't think the numbers are correct (the report seems to also include all other coverage tests) and I have never found myself looking at them (cuts our build time by about 20% too).

docs/getting_started/client.md Show resolved Hide resolved
docs/getting_started/client.md Outdated Show resolved Hide resolved
requirements-http-client.txt Outdated Show resolved Hide resolved
@JPBergsma
Copy link
Contributor

I could not see the comments that I made before, so I will now check whether they still apply.

@ml-evs
Copy link
Member Author

ml-evs commented May 25, 2022

I could not see the comments that I made before, so I will now check whether they still apply.

They should all be listed as "resolved" above, you can double check them

@JPBergsma
Copy link
Contributor

I could not find back the pending comments for the review, so I could not see whether they still applied with the latest patches.

JPBergsma
JPBergsma previously approved these changes May 25, 2022
Copy link
Contributor

@JPBergsma JPBergsma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok everything seems to work now.
I had some issues with this PR and I noticed that the first commit is from 20 hours ago even though this PR was opened 17 days ago. Perhaps this caused some of the issues I experienced, and it is perhaps also the reason the codecov report seems to give a wrong percentage. Perhaps you can try to make a new branch and PR. Hopefully the code coverage is calculated correctly in that case.

@ml-evs
Copy link
Member Author

ml-evs commented May 25, 2022

Ok everything seems to work now. I had some issues with this PR and I noticed that the first commit is from 20 hours ago even though this PR was opened 17 days ago. Perhaps this caused some of the issues I experienced, and it is perhaps also the reason the codecov report seems to give a wrong percentage. Perhaps you can try to make a new branch and PR. Hopefully the code coverage is calculated correctly in that case.

Thanks for the review, glad it is working for you now.

I will actually reintroduce those codecov tests before merging and see if they fix themselves on master, we can always remove in a separate PR.

I will also hold off on releasing v0.18.0 until the weekend; I will write some tutorial notebooks with this development version and make sure everything is working.

@ml-evs
Copy link
Member Author

ml-evs commented May 25, 2022

I have just pushed one additional docs section on querying other endpoints. If the tests pass, I rebase this PR and merge, unless you wanted to have one last look @JPBergsma.

@@ -28,11 +28,20 @@ By default, the client will query all OPTIMADE API URLs that it can find via the
results = client.get()
```

At the command line, it may be immediately useful to redirect or save these results to a file (see [Accessing the results](#accessing-the-results):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can leave out "immediately" I do not see what it adds to the sentence

@JPBergsma
Copy link
Contributor

Maybee as a more general note, You could mention that not all the databases have implemented all the feature or strictly adhere to the standard. When people are going to use this client in it's current form they will see quite some error messages popping up, so it may be good to mention it is not their fault.

@JPBergsma
Copy link
Contributor

I have just pushed one additional docs section on querying other endpoints. If the tests pass, I rebase this PR and merge, unless you wanted to have one last look @JPBergsma.

I have had a look and given my comments above.

@ml-evs
Copy link
Member Author

ml-evs commented May 25, 2022

Maybee as a more general note, You could mention that not all the databases have implemented all the feature or strictly adhere to the standard. When people are going to use this client in it's current form they will see quite some error messages popping up, so it may be good to mention it is not their fault.

This is a very good point, I will put that at the top of the client guide.

- Bump httpx & pytest-httpx
- Add --pretty-print flag and disable by default for performance
- Add a simple async CLI test in CI
- Documentation tweaks
- Added docs section about endpoints
- Setting `max_results_per_provider` to -1 or 0 will remove the limit
- Allow `extensions/<example>` endpoints to be queried
- Final code review changes
- Addded feature list to top of docs

Co-authored-by: Johan Bergsma <29785380+JPBergsma@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
client Issues/PRs relating to the OPTIMADE client. enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants