-
Notifications
You must be signed in to change notification settings - Fork 4
Adds createMovie function to generate movie
#73
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
d70695b
merge upstream
akash5100 f1d3a91
refactor createMovie function
akash5100 f7591bd
add missing params in the docstring
akash5100 8bf6c8f
Checks for errors and suggested changes
akash5100 263badd
fix failing test
akash5100 0ff4e74
Test to hit the missing lines
akash5100 a6c0589
Remove try-block from func and fix failing py310 test
akash5100 1cd20ac
Suggested changes for the filename and format
akash5100 8af46f2
Timeout for the infinity loop
akash5100 54a6aa3
timeout params to decide the timeout by the user
akash5100 e3fc395
update docstring and change import from top level
akash5100 8c2aa59
various tweaks
nabobalis 4ed4553
Adds example in docstring
akash5100 2adf633
Fix failing doctest
akash5100 b6722a8
tweaks to auto generated file name
akash5100 ceb22c8
update example
nabobalis 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 hidden or 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| import time | ||
| from typing import Union, Optional | ||
| from pathlib import Path | ||
| from datetime import datetime | ||
|
|
||
| from hvpy.api_groups.movies.queue_movie import queueMovieInputParameters | ||
| from hvpy.facade import downloadMovie, getMovieStatus, queueMovie | ||
| from hvpy.utils import _add_shared_docstring, save_file | ||
|
|
||
| __all__ = [ | ||
| "createMovie", | ||
| ] | ||
|
|
||
|
|
||
| @_add_shared_docstring(queueMovieInputParameters) | ||
| def createMovie( | ||
| startTime: datetime, | ||
| endTime: datetime, | ||
| layers: str, | ||
| events: str, | ||
| eventsLabels: bool, | ||
| imageScale: float, | ||
| format: Optional[str] = "mp4", | ||
| frameRate: Optional[str] = "15", | ||
| maxFrames: Optional[str] = None, | ||
| scale: Optional[bool] = None, | ||
| scaleType: Optional[str] = None, | ||
| scaleX: Optional[float] = None, | ||
| scaleY: Optional[float] = None, | ||
| movieLength: Optional[float] = None, | ||
| watermark: Optional[bool] = True, | ||
| width: Optional[str] = None, | ||
| height: Optional[str] = None, | ||
| x0: Optional[str] = None, | ||
| y0: Optional[str] = None, | ||
| x1: Optional[str] = None, | ||
| y1: Optional[str] = None, | ||
| x2: Optional[str] = None, | ||
| y2: Optional[str] = None, | ||
| size: Optional[int] = None, | ||
| movieIcons: Optional[int] = None, | ||
| followViewport: Optional[int] = None, | ||
| reqObservationDate: Optional[datetime] = None, | ||
| overwrite: bool = False, | ||
| filename: Union[str, Path] = None, | ||
| hq: bool = False, | ||
| timeout: float = 5, | ||
| ) -> Path: | ||
| """ | ||
| Automatically creates a movie using `queueMovie`, `getMovieStatus` and | ||
| `downloadMovie` functions. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| {Insert} | ||
| overwrite | ||
| Whether to overwrite the file if it already exists. | ||
| Default is `False`. | ||
| filename | ||
| The path to save the file to. | ||
| Optional, will default to ``f"{starttime}_{endtime}.{format}"``. | ||
| hq | ||
| Download a higher-quality movie file (valid for "mp4" movies only, ignored otherwise). | ||
| Default is `False`, optional. | ||
| timeout | ||
| The timeout in minutes to wait for the movie to be created. | ||
| Default is 5 minutes. | ||
|
|
||
| Examples | ||
| -------- | ||
| >>> from hvpy import createMovie, DataSource, create_events, create_layers | ||
| >>> from datetime import datetime, timedelta | ||
| >>> movie_location = createMovie( | ||
| ... startTime=datetime.today() - timedelta(days=15, minutes=5), | ||
| ... endTime=datetime.today() - timedelta(days=15), | ||
| ... layers=create_layers([(DataSource.AIA_171, 100)]), | ||
| ... events=create_events(["AR"]), | ||
| ... eventsLabels=True, | ||
| ... imageScale=1, | ||
| ... filename="my_movie", | ||
| ... ) | ||
| >>> # This is to cleanup the file created from the example | ||
| >>> # you don't need to do this | ||
| >>> from pathlib import Path | ||
| >>> Path('my_movie.mp4').unlink() | ||
| """ | ||
| input_params = locals() | ||
| # These are used later on but we want to avoid passing | ||
| # them into queueMovie. | ||
| input_params.pop("overwrite") | ||
| input_params.pop("filename") | ||
| input_params.pop("hq") | ||
| input_params.pop("timeout") | ||
| res = queueMovie(**input_params) | ||
| if res.get("error"): | ||
| raise RuntimeError(res["error"]) | ||
| timeout_counter = time.time() + 60 * timeout # Default 5 minutes | ||
| while True: | ||
| status = getMovieStatus( | ||
| id=res["id"], | ||
| format=format, | ||
| token=res["token"], | ||
| ) | ||
| if status["status"] in [0, 1]: | ||
| time.sleep(3) | ||
| if status["status"] == 2: | ||
| break | ||
| if time.time() > timeout_counter: | ||
| raise RuntimeError(f"Exceeded timeout of {timeout} minutes.") | ||
| if status["status"] == 3: | ||
| raise RuntimeError(status["error"]) | ||
| binary_data = downloadMovie( | ||
| id=res["id"], | ||
| format=format, | ||
| hq=hq, | ||
| ) | ||
| if filename is None: | ||
| filename = f"{res['id']}_{startTime.date()}_{endTime.date()}.{format}" | ||
| else: | ||
| filename = f"{filename}.{format}" | ||
| save_file( | ||
| data=binary_data, | ||
| filename=filename, | ||
| overwrite=overwrite, | ||
| ) | ||
| return Path(filename) | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| from pathlib import Path | ||
| from datetime import datetime | ||
|
|
||
| import pytest | ||
|
|
||
| from hvpy.datasource import DataSource | ||
| from hvpy.helpers import createMovie | ||
| from hvpy.utils import create_events, create_layers | ||
|
|
||
|
|
||
| def test_createMovie(start_time, end_time, tmp_path): | ||
| f1 = tmp_path / "movie" | ||
| result = createMovie( | ||
| startTime=start_time, | ||
| endTime=end_time, | ||
| layers=create_layers([(DataSource.AIA_171, 100)]), | ||
| events=create_events(["AR"]), | ||
| eventsLabels=True, | ||
| imageScale=1, | ||
| filename=f1, | ||
| ) | ||
| assert isinstance(result, Path) | ||
| assert result.exists() | ||
| assert result == tmp_path / "movie.mp4" | ||
|
|
||
| result = createMovie( | ||
| startTime=start_time, | ||
| endTime=end_time, | ||
| layers=create_layers([(DataSource.AIA_171, 100)]), | ||
| events=create_events(["AR"]), | ||
| eventsLabels=True, | ||
| imageScale=1, | ||
| filename=f1, | ||
| overwrite=True, | ||
| ) | ||
| assert isinstance(result, Path) | ||
| assert result.exists() | ||
| assert result == tmp_path / "movie.mp4" | ||
|
|
||
|
|
||
| def test_createMovie_with_none_filename(start_time, end_time): | ||
| result = createMovie( | ||
| startTime=start_time, | ||
| endTime=end_time, | ||
| layers=create_layers([(DataSource.AIA_171, 100)]), | ||
| events=create_events(["AR"]), | ||
| eventsLabels=True, | ||
| imageScale=1, | ||
| ) | ||
| assert isinstance(result, Path) | ||
| assert result.exists() | ||
| result.unlink() # clean up | ||
|
|
||
|
|
||
| def test_createMovie_timeout(start_time, end_time, tmp_path): | ||
| f1 = tmp_path / "movie" | ||
| with pytest.raises(RuntimeError): | ||
| createMovie( | ||
| startTime=start_time, | ||
| endTime=end_time, | ||
| layers=create_layers([(DataSource.AIA_171, 100)]), | ||
| events=create_events(["AR"]), | ||
| eventsLabels=True, | ||
| imageScale=1, | ||
| filename=f1, | ||
| timeout=0.5, | ||
| ) | ||
|
|
||
|
|
||
| def test_error_handling2(tmp_path): | ||
| f1 = tmp_path / "movie" | ||
| with pytest.raises(RuntimeError): | ||
| createMovie( | ||
| startTime=datetime(2010, 1, 1), | ||
| endTime=datetime(2010, 1, 2), | ||
| layers=create_layers([(DataSource.AIA_171, 100)]), | ||
| events=create_events(["AR"]), | ||
| eventsLabels=True, | ||
| imageScale=1, | ||
| filename=f1, | ||
| ) | ||
|
|
||
|
|
||
| def test_error_handling(tmp_path): | ||
| f1 = tmp_path / "movie" | ||
| with pytest.raises(RuntimeError): | ||
| createMovie( | ||
| startTime=datetime(2010, 1, 1), | ||
| endTime=datetime(2010, 1, 2), | ||
| layers=create_layers([(DataSource.AIA_171, 100)]), | ||
| events=create_events(["AR"]), | ||
| eventsLabels=True, | ||
| imageScale=1, | ||
| filename=f1, | ||
| ) |
This file contains hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.