Datasets:

ArXiv:
License:
File size: 4,174 Bytes
83d1a10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import datasets

class LongTermPrecipitationDataset(datasets.GeneratorBasedBuilder):
    VERSION = datasets.Version("1.0.0")

    def _info(self):
        """
        Defines the dataset metadata and feature structure.
        """
        return datasets.DatasetInfo(
            description="Dataset containing .nc files per year for variables.",
            features=datasets.Features({
                "file_path": datasets.Value("string"),  # Store file paths
                "year": datasets.Value("string"),       # Track year
                "subfolder": datasets.Value("string")   # Track subfolder (sf1, sf2)
            }),
            supervised_keys=None,  # Update if supervised task is defined
            homepage="https://huggingface.co/datasets/nasa-impact/WINDSET/tree/main/long_term_precipitation_forecast",
            license="MIT",
        )

    def _split_generators(self, dl_manager):
        """
        Define the dataset splits for train, validation, and test.
        """
        # Define the directory containing the dataset
        data_dir = os.path.join(os.getcwd(), "long_term_precipitation_forecast")

        # Get the directories for each split
        train_dir = os.path.join(data_dir, "training_data")
        validation_dir = os.path.join(data_dir, "validation_data")
        test_dir = os.path.join(data_dir, "test_data")

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={"split_dir": train_dir},
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={"split_dir": validation_dir},
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={"split_dir": test_dir},
            ),
        ]

    def _get_subfolders(self, base_dir):
        """
        Get all subfolders from the base directory.
        """
        return [os.path.join(base_dir, subfolder) for subfolder in os.listdir(base_dir) if os.path.isdir(os.path.join(base_dir, subfolder))]

    def _get_year_folders(self, subfolder_dir):
        """
        Get all year folders inside a subfolder.
        """
        return [os.path.join(subfolder_dir, year_folder) for year_folder in os.listdir(subfolder_dir) if os.path.isdir(os.path.join(subfolder_dir, year_folder))]

    def _generate_data_from_files(self, data_dir):
        """
        Generate file paths for each subfolder, year, and daily file.
        """
        example_id = 0

        # Loop through subfolders
        for subfolder in os.listdir(data_dir):
            subfolder_path = os.path.join(data_dir, subfolder)

            if os.path.isdir(subfolder_path):
                # Loop through year folders inside the subfolder
                for year_folder in os.listdir(subfolder_path):
                    year_folder_path = os.path.join(subfolder_path, year_folder)

                    if os.path.isdir(year_folder_path):
                        # Loop through daily files inside the year folder
                        for daily_file in os.listdir(year_folder_path):
                            daily_file_path = os.path.join(year_folder_path, daily_file)

                            if daily_file.endswith(".nc"):  # Only select NetCDF files
                                # Yield file information for each data point
                                yield example_id, {
                                    "file_path": daily_file_path,
                                    "year": year_folder,
                                    "subfolder": subfolder,
                                }
                                example_id += 1
                            else:
                                raise FileNotFoundError(f"{daily_file_path} not found")

    def _generate_examples(self, split_dir):
        """
        Generates examples for the dataset from the split directory.
        """
        # Call the data generator to get the file paths
        for example_id, example in self._generate_data_from_files(split_dir):
            yield example_id, example