OneScience-Group/FireCubeNet
025
1"""Generate small structured wildfire sequences with physical correlations."""2 3import argparse4from pathlib import Path5 6import numpy as np7import yaml8 9 10ROOT = Path(__file__).resolve().parents[1]11 12 13def make_split(path, count, config, seed, day_offset):14 rng = np.random.default_rng(seed)15 data_config = config["data"]16 time = int(data_config["sequence_days"])17 height = int(data_config["patch_height"])18 width = int(data_config["patch_width"])19 yy, xx = np.mgrid[:height, :width].astype(np.float32)20 inputs = np.empty((count, time, 25, height, width), dtype=np.float32)21 danger_scores = np.empty(count, dtype=np.float32)22 for sample in range(count):23 center_y = height / 2 + rng.uniform(-3, 3)24 center_x = width / 2 + rng.uniform(-3, 3)25 hotspot = np.exp(-((yy - center_y) ** 2 + (xx - center_x) ** 2) / (2 * rng.uniform(4, 7) ** 2))26 elevation = np.clip(0.25 + 0.018 * yy + 0.012 * xx + rng.normal(0, 0.02, (height, width)), 0, 1)27 slope = np.clip(np.hypot(*np.gradient(elevation)) * 15, 0, 1)28 road = np.clip(np.abs(xx - rng.uniform(5, 20)) / 20, 0, 1)29 water = np.clip(np.abs(yy - (height / 2 + 2 * np.sin(xx / 4))) / 18, 0, 1)30 population = np.exp(-((xx - rng.uniform(5, 20)) ** 2 + (yy - rng.uniform(5, 20)) ** 2) / 60)31 cover_logits = rng.normal(0, 0.8, (10, height, width))32 cover_logits += np.stack([np.sin((xx + index) / (3 + index / 3)) for index in range(10)])33 cover = np.exp(cover_logits - cover_logits.max(axis=0, keepdims=True))34 cover /= cover.sum(axis=0, keepdims=True)35 weather = rng.normal(0, 0.45)36 for day in range(time):37 weather = 0.82 * weather + rng.normal(0, 0.25)38 drying = day / max(time - 1, 1)39 spatial_noise = rng.normal(0, 0.025, (height, width))40 temperature = 0.50 + 0.16 * weather + 0.20 * drying + 0.16 * hotspot + spatial_noise41 wind = 0.32 + 0.12 * weather + 0.10 * hotspot + rng.normal(0, 0.035, (height, width))42 humidity = 0.62 - 0.19 * weather - 0.20 * drying - 0.14 * hotspot + spatial_noise43 precipitation = np.clip(0.30 - 0.13 * weather - 0.18 * drying - 0.10 * hotspot + spatial_noise, 0, 1)44 dewpoint = 0.55 * temperature + 0.40 * humidity45 pressure = 0.55 - 0.06 * weather + 0.02 * hotspot + spatial_noise46 ndvi = np.clip(0.62 - 0.14 * drying - 0.08 * hotspot + 0.08 * cover[2], 0, 1)47 day_lst = np.clip(temperature + 0.10 * hotspot, 0, 1)48 night_lst = np.clip(temperature - 0.16 + 0.04 * hotspot, 0, 1)49 soil_moisture = np.clip(0.58 * humidity + 0.42 * precipitation - 0.10 * drying, 0, 1)50 dynamic = [temperature, wind, humidity, precipitation, dewpoint, pressure,51 ndvi, day_lst, night_lst, soil_moisture]52 static = [road, water, population, elevation, slope, *cover]53 inputs[sample, day] = np.stack(dynamic + static).astype(np.float32)54 cy, cx = height // 2, width // 255 latest = inputs[sample, -1, :, cy, cx]56 danger_scores[sample] = (1.7 * latest[0] + 1.1 * latest[1] - 1.5 * latest[2]57 - 1.2 * latest[9] - 0.35 * latest[10]58 + 0.25 * latest[12] + rng.normal(0, 0.12))59 labels = (danger_scores >= np.median(danger_scores)).astype(np.float32)[:, None]60 timestamps = (np.datetime64("2018-06-01") + (np.arange(count) + day_offset).astype("timedelta64[D]"))61 timestamps = timestamps.astype("datetime64[s]").astype(np.int64)62 latitude = rng.uniform(34.0, 43.0, count).astype(np.float32)63 longitude = rng.uniform(19.0, 30.0, count).astype(np.float32)64 np.savez_compressed(65 path, inputs=inputs, labels=labels, timestamps_unix_s=timestamps,66 coords=np.column_stack((latitude, longitude)).astype(np.float32),67 format_version=np.asarray(data_config["format_version"]),68 data_source=np.asarray("structured_synthetic"), input_layout=np.asarray("BTCHW"),69 )70 71 72def main():73 parser = argparse.ArgumentParser()74 parser.add_argument("--force", action="store_true")75 args = parser.parse_args()76 config = yaml.safe_load((ROOT / "conf/config.yaml").read_text())77 output = ROOT / config["data"]["root"]78 output.mkdir(parents=True, exist_ok=True)79 splits = (("train.npz", int(config["data"]["train_samples"]), 0),80 ("test.npz", int(config["data"]["test_samples"]), 1000))81 for offset, (name, count, day_offset) in enumerate(splits):82 path = output / name83 if args.force or not path.exists():84 make_split(path, count, config, int(config["seed"]) + offset, day_offset)85 print(f"generated={path.relative_to(ROOT)} samples={count} shape={count},10,25,25,25")86 87 88if __name__ == "__main__":89 main()90 