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