Datasets:

ArXiv:
License:
omshinde commited on
Commit
1143910
·
verified ·
1 Parent(s): 956e8b0

file for loading data with HF datasets load_dataset() module

Browse files
weather_forecast_discussion/dataset.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import pandas as pd
3
+ import numpy as np
4
+ import datasets
5
+
6
+ class CSVhrrrDataset(datasets.GeneratorBasedBuilder):
7
+ """
8
+ A custom dataset to load CSV and corresponding hrrr files.
9
+ The CSV files are loaded using pandas and the hrrr files using numpy.
10
+ """
11
+
12
+ # Define dataset name and version
13
+ VERSION = datasets.Version("1.0.0")
14
+
15
+ def _info(self):
16
+ # Dataset description and features
17
+ return datasets.DatasetInfo(
18
+ description="Dataset containing CSV and corresponding hrrr files.",
19
+ features=datasets.Features({
20
+ "csv_data": datasets.Value("string"), # CSV content as a string or specific columns
21
+ "hrrr_file_path": datasets.Value("string"), # Assuming hrrr files contain float32 data
22
+ "filename": datasets.Value("string"), # Filename of the CSV file
23
+ }),
24
+ supervised_keys=None,
25
+ homepage="https://huggingface.co/datasets/nasa-impact/WINDSET/tree/main/weather_forecast_discussion",
26
+ license="MIT",
27
+ )
28
+
29
+ def _split_generators(self, dl_manager):
30
+ """
31
+ Define dataset splits for this dataset (train, validation, test).
32
+ In this case, we just load all the files into a single split.
33
+ """
34
+ # Get the directory paths
35
+ csv_dir = os.path.join(os.getcwd(), "weather_forecast_discussion/csv_reports")
36
+ hrrr_dir = os.path.join(os.getcwd(), "weather_forecast_discussion/hrrr")
37
+
38
+ # Check that both directories exist
39
+ if not os.path.isdir(csv_dir):
40
+ raise FileNotFoundError(f"CSV directory {csv_dir} not found!")
41
+ if not os.path.isdir(hrrr_dir):
42
+ raise FileNotFoundError(f"hrrr directory {hrrr_dir} not found!")
43
+
44
+ # List CSV and hrrr files
45
+ csv_files = [f for f in os.listdir(csv_dir) if f.endswith('.csv')]
46
+ hrrr_files = [f for f in os.listdir(hrrr_dir) if f.endswith('.grib2')]
47
+
48
+ # Ensure CSV and hrrr files are paired correctly by date
49
+ file_pairs = []
50
+ for csv_file in csv_files:
51
+ # Extract the date from the CSV file (assuming format is 'date.csv')
52
+ date_str = os.path.splitext(csv_file)[0]
53
+
54
+ # Search for matching hrrr files in the hrrr directory
55
+ matching_hrrr_files = [hrrr for hrrr in hrrr_files if f"hrrr.{date_str}." in hrrr]
56
+
57
+ if len(matching_hrrr_files) == 1: # Ensure exactly one match
58
+ file_pairs.append((csv_file, matching_hrrr_files[0]))
59
+ elif len(matching_hrrr_files) == 0:
60
+ print(f"Warning: No matching hrrr file found for CSV file: {csv_file}")
61
+ else:
62
+ print(f"Warning: Multiple matching hrrr files found for CSV file: {csv_file}. Using the first match.")
63
+ file_pairs.append((csv_file, matching_hrrr_files[0])) # Use the first match if multiple are found
64
+
65
+ # If no valid file pairs were found, raise an error
66
+ if not file_pairs:
67
+ raise ValueError("No valid CSV-hrrr file pairs found. Check the directory structure and file names.")
68
+
69
+ # Create a split generator
70
+ return [
71
+ datasets.SplitGenerator(
72
+ name=datasets.Split.TRAIN,
73
+ gen_kwargs={
74
+ "file_pairs": file_pairs,
75
+ "csv_dir": csv_dir,
76
+ "hrrr_dir": hrrr_dir,
77
+ }
78
+ )
79
+ ]
80
+
81
+ def _generate_examples(self, file_pairs, csv_dir, hrrr_dir):
82
+ """
83
+ Yield examples from the CSV and hrrr files.
84
+ Each example contains data from a CSV file and its corresponding hrrr file.
85
+ """
86
+ example_id = 0
87
+
88
+ for csv_file, hrrr_file in file_pairs:
89
+ # Load CSV file using pandas
90
+ csv_file_path = os.path.join(csv_dir, csv_file)
91
+ csv_data = pd.read_csv(csv_file_path)
92
+
93
+ # Load corresponding hrrr file using numpy
94
+ hrrr_file_path = os.path.join(hrrr_dir, hrrr_file)
95
+
96
+ # Yield example with both CSV and hrrr data
97
+ yield example_id, {
98
+ "csv_data": csv_data["discussion"].to_string(), # Store content under discussion only
99
+ "hrrr_file_path": hrrr_file_path,
100
+ "filename": csv_file,
101
+ }
102
+ example_id += 1