-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make download_component concurrent (#354)
This PR makes the `download_images` component concurrent. This is just a quick fix, ideally we rewrite the component to use an async http client like httpx. I will pick this up as a separate PR.
- Loading branch information
1 parent
9f5d3a6
commit e580189
Showing
9 changed files
with
149 additions
and
133 deletions.
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,2 +1,3 @@ | ||
albumentations==1.3.0 | ||
opencv-python-headless>=4.5.5.62,<5 | ||
opencv-python-headless>=4.5.5.62,<5 | ||
httpx==0.24.1 |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pytest==7.4.0 | ||
respx==0.20.2 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import io | ||
import os | ||
|
||
import pandas as pd | ||
from httpx import Response | ||
|
||
from src.main import DownloadImagesComponent | ||
|
||
|
||
def test_transform(respx_mock): | ||
"""Test the component transform method.""" | ||
# Define input data and arguments | ||
# These can be parametrized in the future | ||
ids = [ | ||
"a", | ||
"b", | ||
"c", | ||
] | ||
urls = [ | ||
"http://host/path.png", | ||
"https://host/path.png", | ||
"https://host/path.jpg", | ||
] | ||
image_size = 256 | ||
|
||
# Mock httpx to prevent network calls and return test images | ||
image_dir = "tests/images" | ||
images = [] | ||
images = [ | ||
open(os.path.join(image_dir, image), "rb").read() for image in os.listdir(image_dir) # noqa | ||
] | ||
for url, image in zip(urls, images): | ||
respx_mock.get(url).mock(return_value=Response(200, content=image)) | ||
|
||
component = DownloadImagesComponent( | ||
timeout=10, | ||
retries=0, | ||
image_size=image_size, | ||
resize_mode="border", | ||
resize_only_if_bigger=False, | ||
min_image_size=0, | ||
max_aspect_ratio=float("inf"), | ||
) | ||
|
||
input_dataframe = pd.DataFrame( | ||
{ | ||
("images", "url"): urls, | ||
}, | ||
index=pd.Index(ids, name="id"), | ||
) | ||
|
||
# Use the resizer from the component to generate the expected output images | ||
# But use the image_size argument to validate actual resizing | ||
resized_images = [component.resizer(io.BytesIO(image))[0] for image in images] | ||
expected_dataframe = pd.DataFrame( | ||
{ | ||
("images", "data"): resized_images, | ||
("images", "width"): [image_size] * len(ids), | ||
("images", "height"): [image_size] * len(ids), | ||
}, | ||
index=pd.Index(ids, name="id"), | ||
) | ||
|
||
output_dataframe = component.transform(input_dataframe) | ||
|
||
pd.testing.assert_frame_equal( | ||
left=expected_dataframe, | ||
right=output_dataframe, | ||
check_dtype=False, | ||
) |