|
| 1 | +# Project RoboOrchard |
| 2 | +# |
| 3 | +# Copyright (c) 2024-2026 Horizon Robotics. All Rights Reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 14 | +# implied. See the License for the specific language governing |
| 15 | +# permissions and limitations under the License. |
| 16 | +import fsspec |
| 17 | +from datasets.arrow_dataset import ( |
| 18 | + Dataset, |
| 19 | + DatasetInfo, |
| 20 | + InMemoryTable, |
| 21 | + MemoryMappedTable, |
| 22 | + Optional, |
| 23 | + Path, |
| 24 | + PathLike, |
| 25 | + Split, |
| 26 | + concat_tables, |
| 27 | + estimate_dataset_size, |
| 28 | + hf_tqdm, |
| 29 | + is_remote_filesystem, |
| 30 | + is_small_dataset, |
| 31 | + json, |
| 32 | + thread_map, |
| 33 | +) |
| 34 | +from fsspec import url_to_fs |
| 35 | + |
| 36 | +__all__ = ["load_from_disk"] |
| 37 | + |
| 38 | + |
| 39 | +def load_from_disk( |
| 40 | + dataset_path: PathLike, |
| 41 | + keep_in_memory: Optional[bool] = None, |
| 42 | + storage_options: Optional[dict] = None, |
| 43 | +) -> Dataset: |
| 44 | + """A wrapper around `datasets.load_from_disk`. |
| 45 | +
|
| 46 | + This method fix the issue when loading `Dataset` object when info.feature |
| 47 | + does not match the arrow table schema exactly. It is a bug in `Dataset` |
| 48 | + implementation, and we provide this wrapper to fix the issue. |
| 49 | + """ |
| 50 | + import posixpath |
| 51 | + |
| 52 | + fs: fsspec.AbstractFileSystem |
| 53 | + fs, dataset_path = url_to_fs(dataset_path, **(storage_options or {})) |
| 54 | + import datasets.config as config |
| 55 | + |
| 56 | + dest_dataset_path = dataset_path |
| 57 | + dataset_dict_json_path = posixpath.join( |
| 58 | + dest_dataset_path, # type: ignore |
| 59 | + config.DATASETDICT_JSON_FILENAME, # type: ignore |
| 60 | + ) |
| 61 | + dataset_state_json_path = posixpath.join( |
| 62 | + dest_dataset_path, # type: ignore |
| 63 | + config.DATASET_STATE_JSON_FILENAME, # type: ignore |
| 64 | + ) |
| 65 | + dataset_info_path = posixpath.join( |
| 66 | + dest_dataset_path, # type: ignore |
| 67 | + config.DATASET_INFO_FILENAME, # type: ignore |
| 68 | + ) |
| 69 | + |
| 70 | + dataset_dict_is_file = fs.isfile(dataset_dict_json_path) |
| 71 | + dataset_info_is_file = fs.isfile(dataset_info_path) |
| 72 | + dataset_state_is_file = fs.isfile(dataset_state_json_path) |
| 73 | + if not dataset_info_is_file and not dataset_state_is_file: |
| 74 | + if dataset_dict_is_file: |
| 75 | + raise FileNotFoundError( |
| 76 | + f"No such files: '{dataset_info_path}', nor '{dataset_state_json_path}' found. Expected to load a `Dataset` object, but got a `DatasetDict`. Please use either `datasets.load_from_disk` or `DatasetDict.load_from_disk` instead." # noqa: E501 |
| 77 | + ) |
| 78 | + raise FileNotFoundError( |
| 79 | + f"No such files: '{dataset_info_path}', nor '{dataset_state_json_path}' found. Expected to load a `Dataset` object but provided path is not a `Dataset`." # noqa: E501 |
| 80 | + ) |
| 81 | + if not dataset_info_is_file: |
| 82 | + if dataset_dict_is_file: |
| 83 | + raise FileNotFoundError( |
| 84 | + f"No such file: '{dataset_info_path}' found. Expected to load a `Dataset` object, but got a `DatasetDict`. Please use either `datasets.load_from_disk` or `DatasetDict.load_from_disk` instead." # noqa: E501 |
| 85 | + ) |
| 86 | + raise FileNotFoundError( |
| 87 | + f"No such file: '{dataset_info_path}'. Expected to load a `Dataset` object but provided path is not a `Dataset`." # noqa: E501 |
| 88 | + ) |
| 89 | + if not dataset_state_is_file: |
| 90 | + if dataset_dict_is_file: |
| 91 | + raise FileNotFoundError( |
| 92 | + f"No such file: '{dataset_state_json_path}' found. Expected to load a `Dataset` object, but got a `DatasetDict`. Please use either `datasets.load_from_disk` or `DatasetDict.load_from_disk` instead." # noqa: E501 |
| 93 | + ) |
| 94 | + raise FileNotFoundError( |
| 95 | + f"No such file: '{dataset_state_json_path}'. Expected to load a `Dataset` object but provided path is not a `Dataset`." # noqa: E501 |
| 96 | + ) |
| 97 | + |
| 98 | + # copies file from filesystem if it is remote filesystem to local |
| 99 | + # filesystem and modifies dataset_path to temp directory |
| 100 | + # containing local copies |
| 101 | + if is_remote_filesystem(fs): |
| 102 | + src_dataset_path = dest_dataset_path |
| 103 | + dest_dataset_path = Dataset._build_local_temp_path(src_dataset_path) # type: ignore |
| 104 | + fs.download( |
| 105 | + src_dataset_path, dest_dataset_path.as_posix(), recursive=True |
| 106 | + ) |
| 107 | + dataset_state_json_path = posixpath.join( |
| 108 | + dest_dataset_path, config.DATASET_STATE_JSON_FILENAME |
| 109 | + ) |
| 110 | + dataset_info_path = posixpath.join( |
| 111 | + dest_dataset_path, config.DATASET_INFO_FILENAME |
| 112 | + ) |
| 113 | + |
| 114 | + with open(dataset_state_json_path, encoding="utf-8") as state_file: |
| 115 | + state = json.load(state_file) |
| 116 | + with open(dataset_info_path, encoding="utf-8") as dataset_info_file: |
| 117 | + dataset_info = DatasetInfo.from_dict(json.load(dataset_info_file)) |
| 118 | + |
| 119 | + dataset_size = estimate_dataset_size( |
| 120 | + Path(dest_dataset_path, data_file["filename"]) # type: ignore |
| 121 | + for data_file in state["_data_files"] |
| 122 | + ) |
| 123 | + keep_in_memory = ( |
| 124 | + keep_in_memory |
| 125 | + if keep_in_memory is not None |
| 126 | + else is_small_dataset(dataset_size) |
| 127 | + ) |
| 128 | + table_cls = InMemoryTable if keep_in_memory else MemoryMappedTable |
| 129 | + |
| 130 | + arrow_table = concat_tables( |
| 131 | + thread_map( |
| 132 | + table_cls.from_file, |
| 133 | + [ |
| 134 | + posixpath.join(dest_dataset_path, data_file["filename"]) |
| 135 | + for data_file in state["_data_files"] |
| 136 | + ], |
| 137 | + tqdm_class=hf_tqdm, |
| 138 | + desc="Loading dataset from disk", |
| 139 | + # set `disable=None` rather than `disable=False` by default |
| 140 | + # to disable progress bar when no TTY attached |
| 141 | + disable=len(state["_data_files"]) <= 16 or None, |
| 142 | + ) |
| 143 | + ) |
| 144 | + |
| 145 | + split = state["_split"] |
| 146 | + split = Split(split) if split is not None else split |
| 147 | + |
| 148 | + if arrow_table.schema != dataset_info.features.arrow_schema: # type: ignore # noqa: E501 |
| 149 | + arrow_table = arrow_table.cast(dataset_info.features.arrow_schema) # type: ignore # noqa: E501 |
| 150 | + |
| 151 | + dataset = Dataset( |
| 152 | + arrow_table=arrow_table, |
| 153 | + info=dataset_info, |
| 154 | + split=split, |
| 155 | + fingerprint=state["_fingerprint"], |
| 156 | + ) |
| 157 | + |
| 158 | + format = { |
| 159 | + "type": state["_format_type"], |
| 160 | + "format_kwargs": state["_format_kwargs"], |
| 161 | + "columns": state["_format_columns"], |
| 162 | + "output_all_columns": state["_output_all_columns"], |
| 163 | + } |
| 164 | + dataset = dataset.with_format(**format) |
| 165 | + |
| 166 | + return dataset |
0 commit comments