Datasets:

Size:
n<1K
ArXiv:
DOI:
License:
File size: 6,638 Bytes
6b5bf53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fff0586
6b5bf53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4851a70
6b5bf53
 
 
4274ecc
 
 
 
 
 
6b5bf53
 
4274ecc
 
 
 
 
6b5bf53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


from typing import List

import datasets
import h5py

# Find for instance the citation on arxiv or on the dataset repo/website
_CITATION = """\
@article{cabuar,
  title={Ca{B}u{A}r: California {B}urned {A}reas dataset for delineation},
  author={Rege Cambrin, Daniele and Colomba, Luca and Garza, Paolo},
  journal={IEEE Geoscience and Remote Sensing Magazine},
  doi={10.1109/MGRS.2023.3292467},
  year={2023} 
}
"""

# You can copy an official description
_DESCRIPTION = """\
CaBuAr dataset contains images from Sentinel-2 satellites taken before and after a wildfire. 
The ground truth masks are provided by the California Department of Forestry and Fire Protection and they are mapped on the images.
"""

_HOMEPAGE = "https://huggingface.co/datasets/DarthReca/california_burned_areas"

_LICENSE = "OPENRAIL"

_URLS = "raw/patched/512x512.hdf5"


class CaBuArConfig(datasets.BuilderConfig):
    """BuilderConfig for CaBuAr.

    Parameters
    ----------

    load_prefire: bool
        whether to load prefire data
    train_folds: List[int]
        list of folds to use for training
    validation_folds: List[int]
        list of folds to use for validation
    test_folds: List[int]
        list of folds to use for testing
    **kwargs
        keyword arguments forwarded to super.
    """

    def __init__(
        self,
        load_prefire: bool,
        train_folds: List[int],
        validation_folds: List[int],
        test_folds: List[int],
        **kwargs
    ):
        super(CaBuArConfig, self).__init__(**kwargs)
        self.load_prefire = load_prefire
        self.train_folds = train_folds
        self.validation_folds = validation_folds
        self.test_folds = test_folds


class CaBuAr(datasets.GeneratorBasedBuilder):
    """California Burned Areas dataset."""

    VERSION = datasets.Version("1.0.0")

    BUILDER_CONFIGS = [
        CaBuArConfig(
            name="post-fire",
            version=VERSION,
            description="Post-fire only version of the dataset",
            load_prefire=False,
            train_folds=None,
            validation_folds=None,
            test_folds=None,
        ),
        CaBuArConfig(
            name="pre-post-fire",
            version=VERSION,
            description="Pre-fire and post-fire version of the dataset",
            load_prefire=True,
            train_folds=None,
            validation_folds=None,
            test_folds=None,
        ),
    ]

    DEFAULT_CONFIG_NAME = "post-fire"
    BUILDER_CONFIG_CLASS = CaBuArConfig

    def _info(self):
        if self.config.name == "pre-post-fire":
            features = datasets.Features(
                {
                    "post_fire": datasets.Array3D((512, 512, 12), dtype="uint16"),
                    "pre_fire": datasets.Array3D((512, 512, 12), dtype="uint16"),
                    "mask": datasets.Array3D((512, 512, 1), dtype="uint16"),
                }
            )
        else:
            features = datasets.Features(
                {
                    "post_fire": datasets.Array3D((512, 512, 12), dtype="uint16"),
                    "mask": datasets.Array3D((512, 512, 12), dtype="uint16"),
                }
            )

        return datasets.DatasetInfo(
            # This is the description that will appear on the datasets page.
            description=_DESCRIPTION,
            # This defines the different columns of the dataset and their types
            features=features,
            # Homepage of the dataset for documentation
            homepage=_HOMEPAGE,
            # License for the dataset if available
            license=_LICENSE,
            # Citation for the dataset
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        h5_file = dl_manager.download(_URLS)
        # Raise ValueError if train_folds, validation_folds or test_folds are not set
        if (
            self.config.train_folds is None
            or self.config.validation_folds is None
            or self.config.test_folds is None
        ):
            raise ValueError("train_folds, validation_folds and test_folds must be set")

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                # These kwargs will be passed to _generate_examples
                gen_kwargs={
                    "folds": self.config.train_folds,
                    "load_prefire": self.config.load_prefire,
                    "filepath": h5_file,
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                # These kwargs will be passed to _generate_examples
                gen_kwargs={
                    "folds:": self.config.validation_folds,
                    "load_prefire": self.config.load_prefire,
                    "filepath": h5_file,
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                # These kwargs will be passed to _generate_examples
                gen_kwargs={
                    "folds": self.config.test_folds,
                    "load_prefire": self.config.load_prefire,
                    "filepath": h5_file,
                },
            ),
        ]

    # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
    def _generate_examples(self, folds: List[int], load_prefire: bool, filepath):
        with h5py.File(filepath, "r") as f:
            for uuid, values in f.items():
                if values.attrs["fold"] not in folds:
                    continue
                if load_prefire and "pre_fire" not in values:
                    continue
                sample = {
                    "post_fire": values["post_fire"][...],
                    "mask": values["mask"][...],
                }
                if load_prefire:
                    sample["pre_fire"] = values["pre_fire"][...]
                yield uuid, sample