Wei Ji commited on
Commit
1aa84e4
1 Parent(s): 862b160

Initial script to load embeddings from geoparquet file

Browse files

Experimenting with ArrowBasedBuilder, mostly with help from https://discuss.huggingface.co/t/how-to-tweak-a-dataset-without-a-loading-script/43533/5

Files changed (1) hide show
  1. clay_vector_embeddings.py +70 -0
clay_vector_embeddings.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Custom HuggingFace dataset loading script for GeoParquet files.
3
+
4
+ References:
5
+ - https://huggingface.co/docs/datasets/v2.15.0/en/dataset_script
6
+ - https://github.com/huggingface/datasets/blob/2.15.0/templates/new_dataset_script.py
7
+ - https://huggingface.co/docs/datasets/v2.15.0/en/package_reference/builder_classes
8
+ - https://huggingface.co/docs/datasets/v2.15.0/en/about_dataset_load
9
+ - https://discuss.huggingface.co/t/how-to-tweak-a-dataset-without-a-loading-script/43533/5
10
+ """
11
+ import datasets
12
+ import pyarrow as pa
13
+ import pyarrow.parquet as pq
14
+
15
+
16
+ _URLS = {"32VLM": " 32VLM_v01.gpq"}
17
+ _MGRS_TILES = ["32VLM"]
18
+
19
+
20
+ class ClayVectorEmbeddings(datasets.ArrowBasedBuilder):
21
+ """Clay Vector Embeddings in GeoParquet format."""
22
+
23
+ # You will be able to load one or the other configurations in the following list with
24
+ # data = datasets.load_dataset('my_dataset', 'MGRS_TILE')
25
+ BUILDER_CONFIGS = [
26
+ datasets.BuilderConfig(
27
+ name=name,
28
+ version=datasets.Version(version="0.0.1"),
29
+ description=f"Clay vector embeddings for MGRS tile {name}",
30
+ )
31
+ for name in _MGRS_TILES
32
+ ]
33
+
34
+ # DEFAULT_CONFIG_NAME = "32VLM"
35
+
36
+ def _info(self):
37
+ return datasets.DatasetInfo(
38
+ # This is the description that will appear on the datasets page.
39
+ description="Clay Vector Embeddings in GeoParquet format.",
40
+ # This defines the different columns of the dataset and their types
41
+ features=datasets.Features(
42
+ {
43
+ "source_url": datasets.Value(dtype="string"),
44
+ "date": datasets.Value(dtype="date32"),
45
+ "embeddings": datasets.Value("string"),
46
+ "geometry": datasets.Value("binary"),
47
+ # These are the features of your dataset like images, labels ...
48
+ }
49
+ ),
50
+ )
51
+
52
+ def _split_generators(self, dl_manager: datasets.download.DownloadManager):
53
+ files = _URLS[self.config.name]
54
+ downloaded_files = dl_manager.download(files)
55
+ return [
56
+ datasets.SplitGenerator(
57
+ name=datasets.Split.ALL,
58
+ # These kwargs will be passed to _generate_tables
59
+ gen_kwargs={"filepaths": downloaded_files},
60
+ )
61
+ ]
62
+
63
+ # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
64
+ def _generate_tables(self, filepaths: list[str] = ["32VLM_v01.gpq"]):
65
+ for file_idx, filepath in enumerate(filepaths):
66
+ with open(filepath, mode="rb") as f:
67
+ parquet_file = pq.ParquetFile(source=filepath)
68
+ for batch_idx, record_batch in enumerate(parquet_file.iter_batches()):
69
+ pa_table = pa.Table.from_batches([record_batch])
70
+ yield f"{file_idx_batch_idx}", pa_table