-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplot_data.py
More file actions
executable file
·143 lines (125 loc) · 5.36 KB
/
plot_data.py
File metadata and controls
executable file
·143 lines (125 loc) · 5.36 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
"""
STONet: A Neural Operator for Modeling Solute Transport in Micro-Cracked Reservoirs
This code is part of the STONet repository: https://github.com/ehsanhaghighat/STONet
Citation:
@article{haghighat2024stonet,
title={STONet: A neural operator for modeling solute transport in micro-cracked reservoirs},
author={Haghighat, Ehsan and Adeli, Mohammad Hesan and Mousavi, S Mohammad and Juanes, Ruben},
journal={arXiv preprint arXiv:2412.05576},
year={2024}
}
Paper: https://arxiv.org/abs/2412.05576
"""
import os
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import pandas as pd
import math
from sklearn.model_selection import train_test_split
from scipy.interpolate import griddata
import time
import imageio
import torch
from src.networks import MLP, EnrichedDeepONet, Fourier, STONet
from src.optimizers import Optimizer
from src.losses import Loss
from src.data_models import DeepONetDataModel
from src.utils import *
import logging
FORMAT = '[%(asctime)s %(levelname)s %(process)d %(filename)s:%(lineno)d] %(message)s'
logging.basicConfig(format=FORMAT, level=logging.DEBUG)
logging.getLogger('matplotlib.font_manager').setLevel(logging.ERROR)
df_data = pd.read_csv('data/data500_train.csv', delimiter=',')
df_data['prob'] = 1.
print(df_data.keys())
print(df_data)
sample_ids = df_data['sample'].unique().astype(int)
sample_sizes = [len(df_data[df_data['sample'] == i]) for i in df_data['sample'].unique()]
assert len(set(sample_sizes)) == 1, 'The number of samples must be the same for all samples.'
sample_size_per_sim = sample_sizes[0]
print('sample_size_per_sim:', sample_size_per_sim)
time_steps = list(sorted(df_data['t'].unique()))
print('time_steps:', time_steps)
batches = {}
sample_per_time_step = []
for sid in sample_ids:
print('sid:', sid)
sample_data = df_data[df_data['sample'] == sid].reset_index(drop=True)
batches[sid] = {}
for t in time_steps:
batch = sample_data[sample_data['t'] == t]
if len(batch) == 0: continue
batches[sid][t] = batch.reset_index(drop=True)
sample_per_time_step.append(len(batches[sid][t]))
assert len(set(sample_per_time_step)) == 1, 'The number of samples must be the same for all time steps.'
print('sample_per_time_step:', set(sample_per_time_step))
sample_per_time_step = sample_per_time_step[0]
# ['sample', 'x', 'y', 'dp', 'k11', 'k12', 'k22', 'p', 'c', 'deltaC', 'vx', 'vy', 'orient', 'number', 't', 'cdot'],
output_vars = ['dp', 'k11', 'k12', 'k22', 'orient', 'p', 'vx', 'vy', 'c', 'cdot', 'deltaC']
paper_fig_vars = ['k11', 'k22', 'c', 'cdot']
paper_fig_symbols = ['$k_{11}$', '$k_{22}$', '$c$', '$\dot{c}$']
paper_fig_cmap = ['gray', 'gray', 'seismic', 'seismic']
batch_paths = {}
for batch in batches:
batch_paths[batch] = {}
batch_path = f'data/plots/sample-{batch}'
for t in batches[batch]:
batch_paths[batch][t] = f'{batch_path}/time-{t}'
os.makedirs(batch_paths[batch][t], exist_ok=True)
batches[batch][t].to_csv(f'{batch_paths[batch][t]}/data.csv', index=False)
for var in output_vars:
export_path = f'{batch_paths[batch][t]}/{var}.png'
xs = batches[batch][t]['x']
ys = batches[batch][t]['y']
zs = batches[batch][t][var]
# interpolate data on grid
xi = np.linspace(xs.min(), xs.max(), 100)
yi = np.linspace(ys.min(), ys.max(), 100)
zi = griddata((xs, ys), zs, (xi[None,:], yi[:,None]), method='linear')
# plot
plt.figure()
plt.pcolor(xi, yi, zi, cmap='jet')
plt.colorbar()
plt.xlabel('x')
plt.ylabel('y')
plt.title(f"{batch}: t={t} -- {var}")
plt.savefig(export_path)
plt.close()
fig, ax = plt.subplots(1, 4, figsize=(18, 3))
export_path = f'{batch_paths[batch][t]}/fig_vars.png'
for i, (var, cmap, symbol) in enumerate(zip(paper_fig_vars, paper_fig_cmap, paper_fig_symbols)):
xs = batches[batch][t]['x']
ys = batches[batch][t]['y']
zs = batches[batch][t][var]
# interpolate data on grid
xi = np.linspace(xs.min(), xs.max(), 100)
yi = np.linspace(ys.min(), ys.max(), 100)
zi = griddata((xs, ys), zs, (xi[None,:], yi[:,None]), method='linear')
# plot
if var == 'c':
vmin, vmax = 0, 1
elif var == 'cdot':
vlim = max(abs(zi.min()), abs(zi.max()))
vmin, vmax = -vlim, vlim
else:
vmin, vmax = zi.min(), zi.max()
img = ax[i].pcolor(xi, yi, zi, cmap=cmap, vmin=vmin, vmax=vmax)
ticks = np.linspace(vmin, vmax, 5)
plt.colorbar(img, ax=ax[i], ticks=ticks)
ax[i].set_title(f"{symbol}")
ax[i].set_xticks([])
ax[i].set_yticks([])
# ax[0].set_ylabel(f'Sample id: {batches[batch][t]["sample"][0]}')
ax[0].set_ylabel(f'$p_{l}-p_{r}$ = {batches[batch][t]["dp"][0]:.2f}')
plt.tight_layout()
plt.savefig(export_path)
print(f'saved: {export_path}')
plt.close()
# exit()
# create animations
for var in output_vars:
images = []
for t in batches[batch]:
images.append(imageio.imread(os.path.join(batch_paths[batch][t], f'{var}.png')))
imageio.mimsave(f'{batch_path}/{var}.gif', images, duration=2.0)