Update Aiice.py
Browse files
Aiice.py
CHANGED
|
@@ -3,6 +3,8 @@ import numpy as np
|
|
| 3 |
import glob
|
| 4 |
import os
|
| 5 |
from datetime import datetime
|
|
|
|
|
|
|
| 6 |
|
| 7 |
_DESCRIPTION = """\
|
| 8 |
Dataset for Arctic sea ice concentration spatio-temporal forecasting task.
|
|
@@ -42,14 +44,29 @@ class Aiice(datasets.GeneratorBasedBuilder):
|
|
| 42 |
|
| 43 |
def _split_generators(self, dl_manager):
|
| 44 |
"""Define splits and handle data downloading."""
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
return [
|
| 48 |
datasets.SplitGenerator(
|
| 49 |
name=datasets.Split.TRAIN,
|
| 50 |
gen_kwargs={
|
| 51 |
-
"data_dir":
|
| 52 |
-
"file_pattern": "**/*.npy",
|
| 53 |
"date_range": ("1979-01-01", "2015-12-31"),
|
| 54 |
"split_name": "train",
|
| 55 |
},
|
|
@@ -57,8 +74,7 @@ class Aiice(datasets.GeneratorBasedBuilder):
|
|
| 57 |
datasets.SplitGenerator(
|
| 58 |
name=datasets.Split.VALIDATION,
|
| 59 |
gen_kwargs={
|
| 60 |
-
"data_dir":
|
| 61 |
-
"file_pattern": "**/*.npy",
|
| 62 |
"date_range": ("2016-01-01", "2020-12-31"),
|
| 63 |
"split_name": "validation",
|
| 64 |
},
|
|
@@ -66,49 +82,54 @@ class Aiice(datasets.GeneratorBasedBuilder):
|
|
| 66 |
datasets.SplitGenerator(
|
| 67 |
name=datasets.Split.TEST,
|
| 68 |
gen_kwargs={
|
| 69 |
-
"data_dir":
|
| 70 |
-
"file_pattern": "**/*.npy",
|
| 71 |
"date_range": ("2021-01-01", "2025-12-31"),
|
| 72 |
"split_name": "test",
|
| 73 |
},
|
| 74 |
),
|
| 75 |
]
|
| 76 |
|
| 77 |
-
def _generate_examples(self, data_dir,
|
| 78 |
"""Generate examples for each split."""
|
| 79 |
-
#
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
for file in files:
|
| 83 |
-
if file.endswith('.npy'):
|
| 84 |
-
file_paths.append(os.path.join(root, file))
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
| 88 |
start_date = datetime.strptime(date_range[0], "%Y-%m-%d")
|
| 89 |
end_date = datetime.strptime(date_range[1], "%Y-%m-%d")
|
| 90 |
-
|
| 91 |
filtered_files = []
|
| 92 |
-
for file_path in
|
| 93 |
filename = os.path.basename(file_path)
|
|
|
|
| 94 |
date_str = filename.replace('.npy', '').replace('osisaf_', '')
|
| 95 |
-
|
| 96 |
try:
|
| 97 |
file_date = datetime.strptime(date_str, "%Y%m%d")
|
| 98 |
if start_date <= file_date <= end_date:
|
| 99 |
filtered_files.append((file_path, filename, file_date))
|
| 100 |
-
except ValueError:
|
|
|
|
| 101 |
continue
|
| 102 |
-
|
| 103 |
filtered_files.sort(key=lambda x: x[2])
|
| 104 |
logger.info(f"After date filtering: {len(filtered_files)} files for {split_name} split")
|
| 105 |
-
|
| 106 |
for idx, (file_path, filename, file_date) in enumerate(filtered_files):
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import glob
|
| 4 |
import os
|
| 5 |
from datetime import datetime
|
| 6 |
+
from huggingface_hub import snapshot_download
|
| 7 |
+
import tempfile
|
| 8 |
|
| 9 |
_DESCRIPTION = """\
|
| 10 |
Dataset for Arctic sea ice concentration spatio-temporal forecasting task.
|
|
|
|
| 44 |
|
| 45 |
def _split_generators(self, dl_manager):
|
| 46 |
"""Define splits and handle data downloading."""
|
| 47 |
+
# Download the entire dataset repository
|
| 48 |
+
repo_id = "ITMO-NSS/Aiice"
|
| 49 |
+
|
| 50 |
+
# Use dl_manager to download files
|
| 51 |
+
data_dir = dl_manager.download_and_extract(
|
| 52 |
+
f"https://huggingface.co/datasets/{repo_id}/resolve/main/global_series.zip"
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
# If zip doesn't exist, download the entire repo
|
| 56 |
+
if not os.path.exists(data_dir):
|
| 57 |
+
logger.info("Downloading dataset files...")
|
| 58 |
+
data_dir = snapshot_download(
|
| 59 |
+
repo_id=repo_id,
|
| 60 |
+
repo_type="dataset",
|
| 61 |
+
cache_dir=dl_manager.download_cache_dir,
|
| 62 |
+
allow_patterns="global_series/**/*.npy"
|
| 63 |
+
)
|
| 64 |
|
| 65 |
return [
|
| 66 |
datasets.SplitGenerator(
|
| 67 |
name=datasets.Split.TRAIN,
|
| 68 |
gen_kwargs={
|
| 69 |
+
"data_dir": data_dir,
|
|
|
|
| 70 |
"date_range": ("1979-01-01", "2015-12-31"),
|
| 71 |
"split_name": "train",
|
| 72 |
},
|
|
|
|
| 74 |
datasets.SplitGenerator(
|
| 75 |
name=datasets.Split.VALIDATION,
|
| 76 |
gen_kwargs={
|
| 77 |
+
"data_dir": data_dir,
|
|
|
|
| 78 |
"date_range": ("2016-01-01", "2020-12-31"),
|
| 79 |
"split_name": "validation",
|
| 80 |
},
|
|
|
|
| 82 |
datasets.SplitGenerator(
|
| 83 |
name=datasets.Split.TEST,
|
| 84 |
gen_kwargs={
|
| 85 |
+
"data_dir": data_dir,
|
|
|
|
| 86 |
"date_range": ("2021-01-01", "2025-12-31"),
|
| 87 |
"split_name": "test",
|
| 88 |
},
|
| 89 |
),
|
| 90 |
]
|
| 91 |
|
| 92 |
+
def _generate_examples(self, data_dir, date_range, split_name):
|
| 93 |
"""Generate examples for each split."""
|
| 94 |
+
# Find all .npy files
|
| 95 |
+
npy_files = []
|
| 96 |
+
search_path = os.path.join(data_dir, "global_series", "**", "*.npy")
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
+
for file_path in glob.glob(search_path, recursive=True):
|
| 99 |
+
npy_files.append(file_path)
|
| 100 |
+
|
| 101 |
+
logger.info(f"Found {len(npy_files)} total files")
|
| 102 |
+
|
| 103 |
start_date = datetime.strptime(date_range[0], "%Y-%m-%d")
|
| 104 |
end_date = datetime.strptime(date_range[1], "%Y-%m-%d")
|
| 105 |
+
|
| 106 |
filtered_files = []
|
| 107 |
+
for file_path in npy_files:
|
| 108 |
filename = os.path.basename(file_path)
|
| 109 |
+
# Extract date from filename like 'osisaf_19790101.npy'
|
| 110 |
date_str = filename.replace('.npy', '').replace('osisaf_', '')
|
| 111 |
+
|
| 112 |
try:
|
| 113 |
file_date = datetime.strptime(date_str, "%Y%m%d")
|
| 114 |
if start_date <= file_date <= end_date:
|
| 115 |
filtered_files.append((file_path, filename, file_date))
|
| 116 |
+
except ValueError as e:
|
| 117 |
+
logger.warning(f"Could not parse date from {filename}: {e}")
|
| 118 |
continue
|
| 119 |
+
|
| 120 |
filtered_files.sort(key=lambda x: x[2])
|
| 121 |
logger.info(f"After date filtering: {len(filtered_files)} files for {split_name} split")
|
| 122 |
+
|
| 123 |
for idx, (file_path, filename, file_date) in enumerate(filtered_files):
|
| 124 |
+
try:
|
| 125 |
+
matrix = np.load(file_path).astype(np.float32)
|
| 126 |
+
date_iso = file_date.strftime("%Y-%m-%d")
|
| 127 |
+
|
| 128 |
+
yield idx, {
|
| 129 |
+
"date": date_iso,
|
| 130 |
+
"matrix": matrix,
|
| 131 |
+
"filename": filename,
|
| 132 |
+
}
|
| 133 |
+
except Exception as e:
|
| 134 |
+
logger.warning(f"Could not load {file_path}: {e}")
|
| 135 |
+
continue
|