|
| 1 | +from enum import Enum |
| 2 | +from typing import Any, Optional, Sequence, Tuple |
| 3 | + |
| 4 | +import pandas as pd |
| 5 | + |
| 6 | + |
| 7 | +class RayFileType(Enum): |
| 8 | + """Enum for different file types (used for overrides).""" |
| 9 | + CSV = 1 |
| 10 | + PARQUET = 2 |
| 11 | + PETASTORM = 3 |
| 12 | + |
| 13 | + |
| 14 | +class DataSource: |
| 15 | + """Abstract class for data sources. |
| 16 | +
|
| 17 | + xgboost_ray supports reading from various sources, such as files |
| 18 | + (e.g. CSV, Parquet) or distributed datasets (Ray MLDataset, Modin). |
| 19 | +
|
| 20 | + This abstract class defines an interface to read from these sources. |
| 21 | + New data sources can be added by implementing this interface. |
| 22 | +
|
| 23 | + ``DataSource`` classes are not instantiated. Instead, static and |
| 24 | + class methods are called directly. |
| 25 | + """ |
| 26 | + supports_central_loading = True |
| 27 | + supports_distributed_loading = False |
| 28 | + |
| 29 | + @staticmethod |
| 30 | + def is_data_type(data: Any, |
| 31 | + filetype: Optional[RayFileType] = None) -> bool: |
| 32 | + """Check if the supplied data matches this data source. |
| 33 | +
|
| 34 | + Args: |
| 35 | + data (Any): Dataset. |
| 36 | + filetype (Optional[RayFileType]): RayFileType of the provided |
| 37 | + dataset. Some DataSource implementations might require |
| 38 | + that this is explicitly set (e.g. if multiple sources can |
| 39 | + read CSV files). |
| 40 | +
|
| 41 | + Returns: |
| 42 | + Boolean indicating if this data source belongs to/is compatible |
| 43 | + with the data. |
| 44 | + """ |
| 45 | + return False |
| 46 | + |
| 47 | + @staticmethod |
| 48 | + def get_filetype(data: Any) -> Optional[RayFileType]: |
| 49 | + """Method to help infer the filetype. |
| 50 | +
|
| 51 | + Returns None if the supplied data type (usually a filename) |
| 52 | + is not covered by this data source, otherwise the filetype |
| 53 | + is returned. |
| 54 | +
|
| 55 | + Args: |
| 56 | + data (Any): Data set |
| 57 | +
|
| 58 | + Returns: |
| 59 | + RayFileType or None. |
| 60 | + """ |
| 61 | + return None |
| 62 | + |
| 63 | + @staticmethod |
| 64 | + def load_data(data: Any, |
| 65 | + ignore: Optional[Sequence[str]] = None, |
| 66 | + indices: Optional[Sequence[int]] = None, |
| 67 | + **kwargs) -> pd.DataFrame: |
| 68 | + """ |
| 69 | + Load data into a pandas dataframe. |
| 70 | +
|
| 71 | + Ignore specific columns, and optionally select specific indices. |
| 72 | +
|
| 73 | + Args: |
| 74 | + data (Any): Input data |
| 75 | + ignore (Optional[Sequence[str]]): Column names to ignore |
| 76 | + indices (Optional[Sequence[int]]): Indices to select. What an |
| 77 | + index indicates depends on the data source. |
| 78 | +
|
| 79 | + Returns: |
| 80 | + Pandas DataFrame. |
| 81 | + """ |
| 82 | + raise NotImplementedError |
| 83 | + |
| 84 | + @staticmethod |
| 85 | + def convert_to_series(data: Any) -> pd.Series: |
| 86 | + """Convert data from the data source type to a pandas series""" |
| 87 | + if isinstance(data, pd.DataFrame): |
| 88 | + return pd.Series(data.squeeze()) |
| 89 | + |
| 90 | + if not isinstance(data, pd.Series): |
| 91 | + return pd.Series(data) |
| 92 | + |
| 93 | + return data |
| 94 | + |
| 95 | + @classmethod |
| 96 | + def get_column(cls, data: pd.DataFrame, |
| 97 | + column: Any) -> Tuple[pd.Series, Optional[str]]: |
| 98 | + """Helper method wrapping around convert to series. |
| 99 | +
|
| 100 | + This method should usually not be overwritten. |
| 101 | + """ |
| 102 | + if isinstance(column, str): |
| 103 | + return data[column], column |
| 104 | + elif column is not None: |
| 105 | + return cls.convert_to_series(column), None |
| 106 | + return column, None |
| 107 | + |
| 108 | + @staticmethod |
| 109 | + def get_n(data: Any): |
| 110 | + """Get length of data source partitions for sharding.""" |
| 111 | + return len(list(data)) |
0 commit comments