Data Loader¤
The DataLoader Class¤
The DataLoader class is the main class for loading datasets in various formats. It tries to be "smart" detecting the format as well as can be done so that you can use a common interface to load content regardless of source.
Dataset
dataclass
¤
DataLoader
pydantic-model
¤
Bases: BaseLoader
DataLoader.
Config:
arbitrary_types_allowed:True
Fields:
-
paths(list) -
mime_types(list) -
names(list) -
texts(list) -
errors(list)
Source code in lexos/io/data_loader.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | |
data: dict[str, list]
property
¤
Get the data as a dictionary.
Returns:
| Type | Description |
|---|---|
dict[str, list]
|
dict[str, list]: A dictionary containing the paths, mime_types, names, texts, and errors. |
df: pd.DataFrame
property
¤
Get a pandas DataFrame of file records.
Returns:
| Type | Description |
|---|---|
DataFrame
|
pandas.DataFrame: A DataFrame containing file metadata and content. |
errors: list = []
pydantic-field
¤
The list of loading errors.
mime_types: list = []
pydantic-field
¤
The list of text mime types.
names: list = []
pydantic-field
¤
The list of text names.
paths: list = []
pydantic-field
¤
The list of paths.
records: list[dict[str, str]]
property
¤
Get a list of file records.
Returns:
| Type | Description |
|---|---|
list[dict[str, str]]
|
list[dict]: List of dictionaries containing file metadata and content. |
list[dict[str, str]]
|
Each dict has keys: path, mime_type, name, text |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the lengths of paths, mime_types, names and texts don't match. |
Note
Validates that all lists have the same length before returning the records.
texts: list = []
pydantic-field
¤
The list of loaded texts.
__init__()
¤
__iter__() -> Generator[Dataset, None, None]
¤
Make the class iterable.
Yields:
| Name | Type | Description |
|---|---|---|
Dataset |
Dataset
|
A Dataset object containing the name, path, mime_type, and text of each dataset item. |
Note: Overrides the BaseLoader's iter method to yield Dataset objects.
Source code in lexos/io/data_loader.py
dedupe(subset: Optional[list[str]] = None) -> pd.DataFrame
¤
Deduplicate a DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
subset
|
Optional[list[str]]
|
The columns to consider for deduplication. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: The deduplicated DataFrame. |
Source code in lexos/io/base_loader.py
load_csv(path: io.StringIO | os.PathLike | Path | str, name_col: Optional[str] = 'name', text_col: Optional[str] = 'text', **kwargs) -> None
¤
Load a csv file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
StringIO | PathLike | Path | str
|
The path to the file. |
required |
name_col
|
Optional[str]
|
The column name for the names. |
'name'
|
text_col
|
Optional[str]
|
The column name for the texts. |
'text'
|
Source code in lexos/io/data_loader.py
load_dataset(dataset: Self) -> None
¤
Load a dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
DataLoader
|
The dataset to load. |
required |
As of v2.10.5, Pydantic does not support recursive types (Self).
As a result, this method performs its own check to see if the
value of dataset is of type DataSet.
Source code in lexos/io/data_loader.py
load_excel(path: Path | str, name_col: str, text_col: str, **kwargs) -> None
¤
Load an Excel file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
The path to the file. |
required |
name_col
|
str
|
The column name for the names. |
required |
text_col
|
str
|
The column name for the texts. |
required |
Source code in lexos/io/data_loader.py
load_json(path: io.StringIO | os.PathLike | Path | str, name_field: Optional[str] = 'name', text_field: Optional[str] = 'text', **kwargs) -> None
¤
Load a JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
StringIO | PathLike | Path | str
|
The path to the file. |
required |
name_field
|
Optional[str] =
|
The field name for the names. |
'name'
|
text_field
|
Optional[str] =
|
The field name for the texts. |
'text'
|
Source code in lexos/io/data_loader.py
load_lineated_text(path: io.StringIO | os.PathLike | Path | str, names: Optional[list[str]] = None, start: Optional[int] = 1, zero_pad: Optional[str] = '03') -> None
¤
Load a list of texts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
StringIO | PathLike | Path | str
|
The path to the file. |
required |
names
|
Optional[list[str]]
|
The list of names for the texts. |
None
|
start
|
Optional[int]
|
The starting index for the names if no list is provided. |
1
|
zero_pad
|
Optional[str]
|
The zero padding for the names increments if no list is provided. |
'03'
|
Source code in lexos/io/data_loader.py
reset() -> None
¤
Reset the class attributes to empty lists.
show_duplicates(subset: Optional[list[str]] = None) -> pd.DataFrame | None
¤
Show duplicates in a DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
subset
|
Optional[list[str]] = None
|
The columns to consider for checking duplicates. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame | None
|
pd.DataFrame: The DataFrame with duplicates. |
Source code in lexos/io/base_loader.py
to_csv(path: Path | str, **kwargs) -> None
¤
Save the data to a csv file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
The path to save the csv file. |
required |
to_excel(path: Path | str, **kwargs) -> None
¤
Save the data to an Excel file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
The path to save the csv file. |
required |
to_json(path: Path | str, **kwargs) -> None
¤
Save the data to a json file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
The path to save the csv file. |
required |
__init__()
¤
__iter__() -> Generator[Dataset, None, None]
¤
Make the class iterable.
Yields:
| Name | Type | Description |
|---|---|---|
Dataset |
Dataset
|
A Dataset object containing the name, path, mime_type, and text of each dataset item. |
Note: Overrides the BaseLoader's iter method to yield Dataset objects.
Source code in lexos/io/data_loader.py
_update_data(path: Path | str, df: pd.DataFrame, mime_type: str = 'text/plain') -> None
¤
Update the DataLoader.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
The path to the file. |
required |
df
|
DataFrame
|
The DataFrame to update with. |
required |
mime_type
|
str
|
The mime type of the file. |
'text/plain'
|
Source code in lexos/io/data_loader.py
load_csv(path: io.StringIO | os.PathLike | Path | str, name_col: Optional[str] = 'name', text_col: Optional[str] = 'text', **kwargs) -> None
¤
Load a csv file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
StringIO | PathLike | Path | str
|
The path to the file. |
required |
name_col
|
Optional[str]
|
The column name for the names. |
'name'
|
text_col
|
Optional[str]
|
The column name for the texts. |
'text'
|
Source code in lexos/io/data_loader.py
load_dataset(dataset: Self) -> None
¤
Load a dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
DataLoader
|
The dataset to load. |
required |
As of v2.10.5, Pydantic does not support recursive types (Self).
As a result, this method performs its own check to see if the
value of dataset is of type DataSet.
Source code in lexos/io/data_loader.py
load_excel(path: Path | str, name_col: str, text_col: str, **kwargs) -> None
¤
Load an Excel file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
The path to the file. |
required |
name_col
|
str
|
The column name for the names. |
required |
text_col
|
str
|
The column name for the texts. |
required |
Source code in lexos/io/data_loader.py
load_json(path: io.StringIO | os.PathLike | Path | str, name_field: Optional[str] = 'name', text_field: Optional[str] = 'text', **kwargs) -> None
¤
Load a JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
StringIO | PathLike | Path | str
|
The path to the file. |
required |
name_field
|
Optional[str] =
|
The field name for the names. |
'name'
|
text_field
|
Optional[str] =
|
The field name for the texts. |
'text'
|
Source code in lexos/io/data_loader.py
load_lineated_text(path: io.StringIO | os.PathLike | Path | str, names: Optional[list[str]] = None, start: Optional[int] = 1, zero_pad: Optional[str] = '03') -> None
¤
Load a list of texts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
StringIO | PathLike | Path | str
|
The path to the file. |
required |
names
|
Optional[list[str]]
|
The list of names for the texts. |
None
|
start
|
Optional[int]
|
The starting index for the names if no list is provided. |
1
|
zero_pad
|
Optional[str]
|
The zero padding for the names increments if no list is provided. |
'03'
|
Source code in lexos/io/data_loader.py
show_duplicates(subset: Optional[list[str]] = None) -> pd.DataFrame | None
¤
Show duplicates in a DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
subset
|
Optional[list[str]] = None
|
The columns to consider for checking duplicates. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame | None
|
pd.DataFrame: The DataFrame with duplicates. |
Source code in lexos/io/base_loader.py
to_csv(path: Path | str, **kwargs) -> None
¤
Save the data to a csv file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
The path to save the csv file. |
required |
to_excel(path: Path | str, **kwargs) -> None
¤
Save the data to an Excel file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
The path to save the csv file. |
required |
to_json(path: Path | str, **kwargs) -> None
¤
Save the data to a json file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
The path to save the csv file. |
required |