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

[data] return the output of read_webdataset in formats compatible wit… #48645

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion python/ray/data/_internal/datasource/webdataset_datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ def __init__(
filerename: Optional[Union[bool, callable, list]] = None,
suffixes: Optional[Union[bool, callable, list]] = None,
verbose_open: bool = False,
output_format: Optional[str] = "pandas",
**file_based_datasource_kwargs,
):
super().__init__(paths, **file_based_datasource_kwargs)
Expand All @@ -323,6 +324,7 @@ def __init__(
self.filerename = filerename
self.suffixes = suffixes
self.verbose_open = verbose_open
self.output_format = output_format

def _read_stream(self, stream: "pyarrow.NativeFile", path: str):
"""Read and decode samples from a stream.
Expand All @@ -343,6 +345,7 @@ def _read_stream(self, stream: "pyarrow.NativeFile", path: str):
List[Dict[str, Any]]: List of sample (list of length 1).
"""
import pandas as pd
import pyarrow as pa

def get_tar_file_iterator():
return _tar_file_iterator(
Expand All @@ -362,4 +365,17 @@ def get_tar_file_iterator():
for sample in samples:
if self.decoder is not None:
sample = _apply_list(self.decoder, sample, default=_default_decoder)
yield pd.DataFrame({k: [v] for k, v in sample.items()})
if self.output_format == "pandas":
yield pd.DataFrame(
{
k: v if isinstance(v, list) and len(v) == 1 else [v]
for k, v in sample.items()
}
)
else:
column_names = list(sample.keys())
arrays = [
pa.array(v if isinstance(v, list) and len(v) == 1 else [v])
for v in sample.values()
]
yield pa.Table.from_arrays(arrays, names=column_names)
2 changes: 2 additions & 0 deletions python/ray/data/read_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1855,6 +1855,7 @@ def read_webdataset(
file_extensions: Optional[List[str]] = None,
concurrency: Optional[int] = None,
override_num_blocks: Optional[int] = None,
output_format: Optional[str] = "pandas",
) -> Dataset:
"""Create a :class:`~ray.data.Dataset` from
`WebDataset <https://webdataset.github.io/webdataset/>`_ files.
Expand Down Expand Up @@ -1918,6 +1919,7 @@ def read_webdataset(
shuffle=shuffle,
include_paths=include_paths,
file_extensions=file_extensions,
output_format=output_format,
)
return read_datasource(
datasource,
Expand Down