-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample_usage.py
More file actions
executable file
·183 lines (144 loc) · 6.1 KB
/
example_usage.py
File metadata and controls
executable file
·183 lines (144 loc) · 6.1 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env python3
"""
Example usage of WRF diffusion model
"""
import os
import sys
import torch
import numpy as np
import logging
from pathlib import Path
# Add src to path
sys.path.append(os.path.join(os.path.dirname(__file__), 'src'))
from config.config import Config
from models.diffusion_model import WRFDiffusionModel
from utils.utils import setup_logging
def setup_example():
"""Setup example configuration"""
config = Config()
# Use smaller dimensions for example
config.data.spatial_dims = (32, 32)
config.data.vertical_levels = 10
config.data.time_steps = 100
# Use smaller model for example
config.model.channels = [32, 64, 128]
config.model.num_steps = 100
# Use smaller batch size
config.data.batch_size = 2
return config
def create_synthetic_data(config):
"""Create synthetic WRF-like data for demonstration"""
batch_size = config.data.batch_size
channels = config.model.in_channels
time_steps = config.data.time_steps
height, width = config.data.spatial_dims
levels = config.data.vertical_levels
# Create synthetic data with realistic patterns
data = torch.zeros(batch_size, channels, time_steps, height, width)
for b in range(batch_size):
for c in range(channels):
for t in range(time_steps):
# Create spatial patterns
x = torch.linspace(0, 4*np.pi, width)
y = torch.linspace(0, 4*np.pi, height)
X, Y = torch.meshgrid(x, y, indexing='ij')
# Add time evolution
time_factor = torch.sin(2 * np.pi * t / time_steps)
# Create different patterns for different channels
if c < 5: # Meteorological variables
pattern = torch.sin(X + time_factor) * torch.cos(Y + time_factor)
elif c < 10: # Chemical variables
pattern = torch.sin(X + Y + time_factor) * torch.exp(-0.1 * (X**2 + Y**2))
else: # Optical variables
pattern = torch.cos(X - Y + time_factor) * torch.sin(0.5 * X + 0.5 * Y)
# Add noise
noise = 0.1 * torch.randn(height, width)
data[b, c, t] = pattern + noise
return data
def main():
"""Run example usage"""
print("=== WRF Diffusion Model Example ===")
# Setup logging
setup_logging("logs/example.log", "INFO")
logger = logging.getLogger(__name__)
# Create configuration
config = setup_example()
logger.info("Configuration created")
# Create model
logger.info("Creating model...")
model = WRFDiffusionModel(config)
model.eval()
print(f"Model created with {sum(p.numel() for p in model.parameters()):,} parameters")
# Create synthetic data
logger.info("Creating synthetic data...")
data = create_synthetic_data(config)
print(f"Created synthetic data with shape: {data.shape}")
# Test diffusion process
logger.info("Testing diffusion process...")
# Forward diffusion
t = torch.randint(0, config.model.num_steps, (config.data.batch_size,))
noisy_data, noise = model.diffusion.forward(data, t)
print(f"Forward diffusion successful")
print(f" - Original data range: [{data.min():.3f}, {data.max():.3f}]")
print(f" - Noisy data range: [{noisy_data.min():.3f}, {noisy_data.max():.3f}]")
# Reverse diffusion (prediction)
logger.info("Testing reverse diffusion...")
with torch.no_grad():
predicted_noise = model.unet(noisy_data, t)
predicted_data = model.diffusion.reverse_process(noisy_data, t)
print(f"Reverse diffusion successful")
print(f" - Predicted data range: [{predicted_data.min():.3f}, {predicted_data.max():.3f}]")
# Test next step prediction
logger.info("Testing next step prediction...")
with torch.no_grad():
next_step_prediction = model.predict_next_step(data, num_steps=20)
print(f"Next step prediction successful")
print(f" - Prediction shape: {next_step_prediction.shape}")
print(f" - Prediction range: [{next_step_prediction.min():.3f}, {next_step_prediction.max():.3f}]")
# Calculate some metrics
logger.info("Calculating metrics...")
# MSE between original and predicted
mse = torch.mean((data - next_step_prediction) ** 2)
print(f"MSE between original and prediction: {mse.item():.6f}")
# Noise prediction accuracy
noise_mse = torch.mean((noise - predicted_noise) ** 2)
print(f"Noise prediction MSE: {noise_mse.item():.6f}")
# Save example outputs
logger.info("Saving example outputs...")
os.makedirs("examples", exist_ok=True)
# Save data
torch.save({
'original_data': data,
'noisy_data': noisy_data,
'predicted_data': predicted_data,
'next_step_prediction': next_step_prediction,
'noise': noise,
'predicted_noise': predicted_noise
}, "examples/example_data.pt")
# Save configuration
import json
config_dict = {}
for key, value in config.__dict__.items():
if hasattr(value, '__dict__'):
config_dict[key] = value.__dict__
else:
config_dict[key] = value
with open("examples/example_config.json", 'w') as f:
json.dump(config_dict, f, indent=2)
print("Example completed successfully!")
print("Check the 'examples' directory for saved outputs.")
# Print usage instructions
print("\n=== Usage Instructions ===")
print("1. To preprocess data:")
print(" python main.py preprocess --data_dir /path/to/wrf/data --output_dir outputs")
print()
print("2. To train the model:")
print(" python main.py train --data_dir /path/to/processed/data --output_dir outputs")
print()
print("3. To evaluate the model:")
print(" python main.py evaluate --model_path /path/to/model.pth --data_dir /path/to/test/data")
print()
print("4. To check system info:")
print(" python main.py info")
if __name__ == "__main__":
main()