-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Data] Add
FileDatasink
subclasses (#40693)
This PR is part of a larger effort to clean up Datasource interfaces (#40296). This #40691 added the new FileDatasink base class, and this PR migrates FileDatasource implementations to the new API. The primary motivation for these changes is to reduced complexity of our internal code base. For more information, see https://docs.google.com/document/d/1Bqhbzvxv7liwpOhyBzRVy5tOzXdy-NiMSFa-6hupr18/edit#heading=h.rytitv546vx5. --------- Signed-off-by: Balaji Veeramani <balaji@anyscale.com> Signed-off-by: Balaji Veeramani <bveeramani@berkeley.edu> Co-authored-by: Stephanie Wang <swang@cs.berkeley.edu>
- Loading branch information
1 parent
da23f8d
commit 7b8ec8a
Showing
9 changed files
with
260 additions
and
50 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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from typing import Any, Callable, Dict, Optional | ||
|
||
import pyarrow | ||
|
||
from ray.data.block import BlockAccessor | ||
from ray.data.datasource.file_based_datasource import _resolve_kwargs | ||
from ray.data.datasource.file_datasink import BlockBasedFileDatasink | ||
|
||
|
||
class _CSVDatasink(BlockBasedFileDatasink): | ||
def __init__( | ||
self, | ||
path: str, | ||
*, | ||
arrow_csv_args_fn: Callable[[], Dict[str, Any]] = lambda: {}, | ||
arrow_csv_args: Optional[Dict[str, Any]] = None, | ||
file_format="csv", | ||
**file_datasink_kwargs, | ||
): | ||
super().__init__(path, file_format=file_format, **file_datasink_kwargs) | ||
|
||
if arrow_csv_args is None: | ||
arrow_csv_args = {} | ||
|
||
self.arrow_csv_args_fn = arrow_csv_args_fn | ||
self.arrow_csv_args = arrow_csv_args | ||
|
||
def write_block_to_file(self, block: BlockAccessor, file: "pyarrow.NativeFile"): | ||
from pyarrow import csv | ||
|
||
writer_args = _resolve_kwargs(self.arrow_csv_args_fn, **self.arrow_csv_args) | ||
write_options = writer_args.pop("write_options", None) | ||
csv.write_csv(block.to_arrow(), file, write_options, **writer_args) |
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,24 @@ | ||
import io | ||
from typing import Any, Dict | ||
|
||
import pyarrow | ||
|
||
from ray.data.datasource.file_datasink import RowBasedFileDatasink | ||
|
||
|
||
class _ImageDatasink(RowBasedFileDatasink): | ||
def __init__( | ||
self, path: str, column: str, file_format: str, **file_datasink_kwargs | ||
): | ||
super().__init__(path, file_format=file_format, **file_datasink_kwargs) | ||
|
||
self.column = column | ||
self.file_format = file_format | ||
|
||
def write_row_to_file(self, row: Dict[str, Any], file: "pyarrow.NativeFile"): | ||
from PIL import Image | ||
|
||
image = Image.fromarray(row[self.column]) | ||
buffer = io.BytesIO() | ||
image.save(buffer, format=self.file_format) | ||
file.write(buffer.getvalue()) |
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,33 @@ | ||
from typing import Any, Callable, Dict, Optional | ||
|
||
import pyarrow | ||
|
||
from ray.data.block import BlockAccessor | ||
from ray.data.datasource.file_based_datasource import _resolve_kwargs | ||
from ray.data.datasource.file_datasink import BlockBasedFileDatasink | ||
|
||
|
||
class _JSONDatasink(BlockBasedFileDatasink): | ||
def __init__( | ||
self, | ||
path: str, | ||
*, | ||
pandas_json_args_fn: Callable[[], Dict[str, Any]] = lambda: {}, | ||
pandas_json_args: Optional[Dict[str, Any]] = None, | ||
file_format: str = "json", | ||
**file_datasink_kwargs, | ||
): | ||
super().__init__(path, file_format=file_format, **file_datasink_kwargs) | ||
|
||
if pandas_json_args is None: | ||
pandas_json_args = {} | ||
|
||
self.pandas_json_args_fn = pandas_json_args_fn | ||
self.pandas_json_args = pandas_json_args | ||
|
||
def write_block_to_file(self, block: BlockAccessor, file: "pyarrow.NativeFile"): | ||
writer_args = _resolve_kwargs(self.pandas_json_args_fn, **self.pandas_json_args) | ||
orient = writer_args.pop("orient", "records") | ||
lines = writer_args.pop("lines", True) | ||
|
||
block.to_pandas().to_json(file, orient=orient, lines=lines, **writer_args) |
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,23 @@ | ||
import numpy as np | ||
import pyarrow | ||
|
||
from ray.data.block import BlockAccessor | ||
from ray.data.datasource.file_datasink import BlockBasedFileDatasink | ||
|
||
|
||
class _NumpyDatasink(BlockBasedFileDatasink): | ||
def __init__( | ||
self, | ||
path: str, | ||
column: str, | ||
*, | ||
file_format: str = "npy", | ||
**file_datasink_kwargs, | ||
): | ||
super().__init__(path, file_format=file_format, **file_datasink_kwargs) | ||
|
||
self.column = column | ||
|
||
def write_block_to_file(self, block: BlockAccessor, file: "pyarrow.NativeFile"): | ||
value = block.to_numpy(self.column) | ||
np.save(file, value) |
Oops, something went wrong.