-
Notifications
You must be signed in to change notification settings - Fork 9
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
14 asynchronous requests #15
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f2131f1
Renamed MSP class to more general Spectra class
xtrojak 425cc35
Changed code to asynchronous using aiohttp requests
xtrojak 7730e64
Covered some special cases in aiohttp requests
xtrojak b162b1c
Fixed minor issues with NLM service
xtrojak d06cfae
Improved code structure in Converter
xtrojak 22550ab
Changed aiohttp session to property of Converter
xtrojak 46021b3
Updated tests to asynchronous version
xtrojak b16b8b2
Wrapper function to enable async calls with active aiohttp session
xtrojak f7c6ac4
Renamed session wrapper for tests
xtrojak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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 |
---|---|---|
@@ -1,11 +1,13 @@ | ||
import asyncio | ||
import aiohttp | ||
from matchms.importing import load_from_msp | ||
from matchms.exporting import save_as_msp | ||
|
||
from libs import curator | ||
from libs.Annotator import Annotator | ||
|
||
|
||
class MSP: | ||
class Spectra: | ||
def __init__(self): | ||
self.annotator = Annotator() | ||
self.spectrums = [] | ||
|
@@ -36,24 +38,41 @@ def get_available_jobs(self): | |
""" | ||
return self.annotator.get_all_conversions() | ||
|
||
def annotate_spectrums(self, jobs): | ||
async def annotate(self, jobs, batch_size, repeat): | ||
""" | ||
Annotate data using specified jobs in asynchronous mode. | ||
Spectrums are executed in batches to avoid flooding web services. | ||
|
||
:param jobs: given list of jobs to do | ||
:param batch_size: size of single batch | ||
:param repeat: if some metadata was added, all jobs are executed again | ||
""" | ||
async with aiohttp.ClientSession() as session: | ||
self.annotator.session = session | ||
results = [] | ||
for size in range(len(self.spectrums) // batch_size + 1): | ||
results += await asyncio.gather(*[self.annotator.annotate(spectra, jobs, repeat) for spectra in | ||
self.spectrums[size * batch_size:(size + 1) * batch_size]]) | ||
Comment on lines
+54
to
+55
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This operation is quite complex and could either be fit into multiple lines or into a small helper function with a descriptive name. As discussed, this might then also move to a different class. |
||
self.spectrums = results | ||
|
||
def annotate_spectrums(self, jobs, batch_size=10, repeat=False): | ||
""" | ||
Adds additional metadata to all Spectra objects. | ||
|
||
Required metadata are specified in required_annotations attribute and | ||
have to be defined in and add_ method of Annotator class (otherwise ignored). | ||
|
||
:param jobs: target annotation jobs | ||
:param batch_size: number of spectrums annotated at once (to avoid flooding web services) | ||
:param repeat: if some metadata was added, all jobs are executed again | ||
""" | ||
for i, spectrum in enumerate(self.spectrums): | ||
metadata = curator.curate_metadata(spectrum.metadata) | ||
spectrum.metadata = self.annotator.annotate(metadata, jobs) | ||
for spectrum in self.spectrums: | ||
spectrum.metadata = curator.curate_metadata(spectrum.metadata) | ||
asyncio.run(self.annotate(jobs, batch_size, repeat)) | ||
|
||
def annotate_spectrums_all_attributes(self): | ||
def annotate_spectrums_all_attributes(self, batch_size=10): | ||
""" | ||
Adds all implemented metadata to all Spectra objects. | ||
""" | ||
jobs = self.get_available_jobs() | ||
for i, spectrum in enumerate(self.spectrums): | ||
metadata = curator.curate_metadata(spectrum.metadata) | ||
spectrum.metadata = self.annotator.annotate(metadata, jobs) | ||
self.annotate_spectrums(jobs, batch_size, True) |
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
kudos for updating readme