File size: 3,177 Bytes
c26788d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
import datasets
import h5py
import json
import os

class MyDatasetConfig(datasets.BuilderConfig):
    def __init__(self, name, split_file=None, **kwargs):
        super().__init__(name=name, **kwargs)
        self.split_file = split_file


class MyDataset(datasets.GeneratorBasedBuilder):
    BUILDER_CONFIGS = [
        MyDatasetConfig(name="Arizona", split_file="Train-Test-Split/split_by_patches_Arizona_split_h5.json"),
        # Add other state configs similarly
    ]

    def _info(self):
        return datasets.DatasetInfo(
            description="Patch-level irrigation dataset stored as separate .h5 files",
            features=datasets.Features({
                "id": datasets.Value("string"),
                "rgb": datasets.Array3D(shape=(None, None, 3), dtype="float32"),
                "agri_index": datasets.Array2D(shape=(None, None), dtype="float32"),
                "land_mask": datasets.Array2D(shape=(None, None), dtype="int8"),
                "crop_mask": datasets.Array2D(shape=(None, None), dtype="int8"),
                "irr_mask": datasets.Array2D(shape=(None, None), dtype="int8"),
                "subirr_mask": datasets.Array2D(shape=(None, None), dtype="int8"),
                "image_path": datasets.Value("string"),
                "label_type": datasets.Value("string"),
                "text_prompt": datasets.Value("string"),
                "polygon": datasets.Value("string"),
                "crs": datasets.Value("string"),
                "split": datasets.Value("string"),
            })
        )

    def _split_generators(self, dl_manager):
        # Download full dataset and split JSON
        data_dir = dl_manager.download_and_extract("Data/label")
        split_path = dl_manager.download(self.config.split_file)

        with open(split_path, "r") as f:
            split_dict = json.load(f)

        def get_file_paths(split_list):
            return [os.path.join(data_dir, item["h5_path"].replace("Data/label/", "")) for item in split_list]

        return [
            datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"files": get_file_paths(split_dict["train"])}),
            datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"files": get_file_paths(split_dict["test"])}),
        ]

    def _generate_examples(self, files):
        for h5_path in files:
            with h5py.File(h5_path, "r") as f:
                patch_id = os.path.basename(h5_path).replace(".h5", "")
                yield patch_id, {
                    "id": patch_id,
                    "rgb": f["rgb"][()],
                    "agri_index": f["agri_index"][()],
                    "land_mask": f["land_mask"][()],
                    "crop_mask": f["crop_mask"][()],
                    "irr_mask": f["irr_mask"][()],
                    "subirr_mask": f["subirr_mask"][()],
                    "image_path": f.attrs["image_path"],
                    "label_type": f.attrs["label_type"],
                    "text_prompt": f.attrs["text_prompt"],
                    "polygon": f.attrs["polygon"],
                    "crs": f.attrs["crs"],
                    "split": f.attrs["split"],
                }