File size: 28,317 Bytes
22f2401
 
a106fa3
22f2401
 
 
a106fa3
 
 
 
 
22f2401
 
 
 
 
 
 
 
a106fa3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22f2401
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a106fa3
22f2401
a106fa3
22f2401
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a106fa3
 
 
 
 
 
22f2401
a106fa3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22f2401
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a106fa3
 
 
 
 
 
22f2401
 
 
 
 
 
 
 
a106fa3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22f2401
a106fa3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22f2401
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a106fa3
22f2401
 
 
 
 
 
 
a106fa3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22f2401
a106fa3
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
import streamlit as st
import numpy as np
import pandas as pd
from PIL import Image
import cv2
from scipy.ndimage import gaussian_filter
import tensorflow as tf
import matplotlib.pyplot as plt
import io
from matplotlib.figure import Figure
import base64

# ------------------ TC CENTERING UTILS ------------------

def find_tc_center(ir_image, smoothing_sigma=3):
    smoothed_image = gaussian_filter(ir_image, sigma=smoothing_sigma)
    min_coords = np.unravel_index(np.argmin(smoothed_image), smoothed_image.shape)
    return min_coords[::-1]  # Return as (x, y)

# Function to generate comparison chart
def generate_comparison_chart(models, mae_values, rmse_values, predicted_values=None):
    # Calculate improvement percentages relative to the first model
    baseline_mae = mae_values[0]
    baseline_rmse = rmse_values[0]
    
    mae_improvements = [0] + [((baseline_mae - val) / baseline_mae) * 100 for val in mae_values[1:]]
    rmse_improvements = [0] + [((baseline_rmse - val) / baseline_rmse) * 100 for val in rmse_values[1:]]
    
    # Create figure with subplots (2 or 3 depending on if we have predictions)
    if predicted_values:
        fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(18, 8))
    else:
        fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(16, 8))
    
    # Plot MAE
    bars1 = ax1.bar(range(len(models)), mae_values, color='skyblue', edgecolor='black')
    ax1.set_title('Mean Absolute Error (MAE)', fontsize=14, fontweight='bold')
    ax1.set_ylabel('MAE (knots)', fontsize=12)
    ax1.set_xticks(range(len(models)))
    ax1.set_xticklabels(models, fontsize=12, rotation=45, ha='right')
    ax1.grid(axis='y', linestyle='--', alpha=0.3, color='lightgray')
    ax1.set_ylim(0, max(mae_values) * 1.2)
    
    # Plot RMSE
    bars2 = ax2.bar(range(len(models)), rmse_values, color='lightcoral', edgecolor='black')
    ax2.set_title('Root Mean Square Error (RMSE)', fontsize=14, fontweight='bold')
    ax2.set_ylabel('RMSE (knots)', fontsize=12)
    ax2.set_xticks(range(len(models)))
    ax2.set_xticklabels(models, fontsize=12, rotation=45, ha='right')
    ax2.grid(axis='y', linestyle='--', alpha=0.3, color='lightgray')
    ax2.set_ylim(0, max(rmse_values) * 1.2)
    
    # Add values on top of the bars for MAE
    for i, bar in enumerate(bars1):
        height = bar.get_height()
        ax1.text(bar.get_x() + bar.get_width()/2., height + 0.3,
                f'{height:.2f}', ha='center', va='bottom', fontsize=12)
        
        # Add improvement percentage for all except the first bar
        if i > 0:
            ax1.text(bar.get_x() + bar.get_width()/2., height/2,
                    f'↓{mae_improvements[i]:.1f}%', ha='center', va='center',
                    color='blue', fontsize=12, fontweight='bold')
    
    # Add values on top of the bars for RMSE
    for i, bar in enumerate(bars2):
        height = bar.get_height()
        ax2.text(bar.get_x() + bar.get_width()/2., height + 0.3,
                f'{height:.2f}', ha='center', va='bottom', fontsize=12)
        
        # Add improvement percentage for all except the first bar
        if i > 0:
            ax2.text(bar.get_x() + bar.get_width()/2., height/2,
                    f'↓{rmse_improvements[i]:.1f}%', ha='center', va='center',
                    color='darkred', fontsize=12, fontweight='bold')
    
    # Add horizontal reference lines for best performance
    min_mae = min(mae_values)
    min_rmse = min(rmse_values)
    ax1.axhline(y=min_mae, color='blue', linestyle='--', alpha=0.5)
    ax2.axhline(y=min_rmse, color='red', linestyle='--', alpha=0.5)
    
    # Add predictions comparison if provided
    if predicted_values:
        bars3 = ax3.bar(range(len(models)), predicted_values, color='lightgreen', edgecolor='black')
        ax3.set_title('Predicted Vmax', fontsize=14, fontweight='bold')
        ax3.set_ylabel('Wind Speed (knots)', fontsize=12)
        ax3.set_xticks(range(len(models)))
        ax3.set_xticklabels(models, fontsize=12, rotation=45, ha='right')
        ax3.grid(axis='y', linestyle='--', alpha=0.3, color='lightgray')
        
        # Add values on top of the bars for predictions
        for i, bar in enumerate(bars3):
            height = bar.get_height()
            ax3.text(bar.get_x() + bar.get_width()/2., height + 0.3,
                    f'{height:.2f}', ha='center', va='bottom', fontsize=12)
    
    # Add a label at the bottom explaining the reduction percentages
    fig.text(0.5, 0.01, 'Note: Reduction percentages (↓%) are calculated relative to TCIP-Net (3DCNN)',
             ha='center', fontsize=12, fontstyle='italic')
    
    plt.tight_layout(rect=[0, 0.03, 1, 0.95])
    
    return fig

# Function to convert matplotlib figure to Streamlit-compatible image
def fig_to_streamlit(fig):
    buf = io.BytesIO()
    fig.savefig(buf, format='png', dpi=300, bbox_inches='tight')
    buf.seek(0)
    return buf

def extract_local_region(ir_image, center, region_size=95):
    h, w = ir_image.shape
    half_size = region_size // 2
    x_min = max(center[0] - half_size, 0)
    x_max = min(center[0] + half_size, w)
    y_min = max(center[1] - half_size, 0)
    y_max = min(center[1] + half_size, h)
    region = np.full((region_size, region_size), np.nan)
    extracted = ir_image[y_min:y_max, x_min:x_max]
    region[:extracted.shape[0], :extracted.shape[1]] = extracted
    return region

def generate_hovmoller(X_data):
    hovmoller_list = []
    for ir_images in X_data:  # ir_images: shape (8, 95, 95)
        time_steps = ir_images.shape[0]
        hovmoller_data = np.zeros((time_steps, 95, 95))
        for t in range(time_steps):
            tc_center = find_tc_center(ir_images[t])
            hovmoller_data[t] = extract_local_region(ir_images[t], tc_center, 95)
        hovmoller_list.append(hovmoller_data)
    return np.array(hovmoller_list)

def reshape_vmax(vmax_values, chunk_size=8):
    trimmed_size = (len(vmax_values) // chunk_size) * chunk_size
    vmax_values_trimmed = vmax_values[:trimmed_size]
    return vmax_values_trimmed.reshape(-1, chunk_size)
def create_3d_vmax(vmax_2d_array):
    # Initialize a 3D array of shape (N, 8, 8) filled with zeros
    vmax_3d_array = np.zeros((vmax_2d_array.shape[0], 8, 8))

    # Fill the diagonal for each row in the 3D array
    for i in range(vmax_2d_array.shape[0]):
        np.fill_diagonal(vmax_3d_array[i], vmax_2d_array[i])

    # Reshape to (N*8, 8, 8, 1)
    vmax_3d_array = vmax_3d_array.reshape(-1, 8, 8, 1)
    # Trim last element if needed (original comment, but not implemented)
    return vmax_3d_array

def process_lat_values(data):
    lat_values = data # Convert to NumPy array

    # Trim the array to make its length divisible by 8
    trimmed_size = (len(lat_values) // 8) * 8
    lat_values_trimmed = lat_values[:trimmed_size]
    lat_values_trimmed=np.array(lat_values_trimmed)  # Convert to NumPy array
    # Reshape into a 2D array (rows of 8 values each) and remove the last row
    lat_2d_array = lat_values_trimmed.reshape(-1, 8)

    return lat_2d_array

def process_lon_values(data):
    lon_values =data  # Convert to NumPy array
    lon_values = np.array(lon_values)  # Convert to NumPy array
    # Trim the array to make its length divisible by 8
    trimmed_size = (len(lon_values) // 8) * 8
    lon_values_trimmed = lon_values[:trimmed_size]

    # Reshape into a 2D array (rows of 8 values each) and remove the last row
    lon_2d_array = lon_values_trimmed.reshape(-1, 8)

    return lon_2d_array

import numpy as np

def calculate_intensity_difference(vmax_2d_array):
    """Calculates intensity difference for each row in Vmax 2D array."""
    int_diff = []
    
    for i in vmax_2d_array:
        k = abs(i[0] - i[-1])  # Absolute difference between first & last element
        i = np.append(i, k)  # Append difference as the 9th element
        int_diff.append(i)
    
    return np.array(int_diff)

import numpy as np

# Function to process and reshape image data
def process_images(images, batch_size=8, img_size=(95, 95, 1)):
    num_images = images.shape[0]
    
    # Trim the dataset to make it divisible by batch_size
    trimmed_size = (num_images // batch_size) * batch_size
    images_trimmed = images[:trimmed_size]

    # Reshape into (x, batch_size, img_size[0], img_size[1], img_size[2])
    images_reshaped = images_trimmed.reshape(-1, batch_size, *img_size)

    return images_reshaped

import numpy as np

def process_cc_mask(cc_data):
    """Processes CC mask images by trimming and reshaping into (x, 8, 95, 95, 1)."""
    num_images = cc_data.shape[0]
    batch_size = 8
    trimmed_size = (num_images // batch_size) * batch_size  # Ensure divisibility by 8

    images_trimmed = cc_data[:trimmed_size]  # Trim excess images
    cc_images = images_trimmed.reshape(-1, batch_size, 95, 95, 1)  # Reshape

    return cc_images
def extract_convective_cores(ir_data):
    """
    Extract Convective Cores (CCs) from IR imagery based on the criteria in the paper.
    Args:
        ir_data: IR imagery of shape (height, width).
    Returns:
        cc_mask: Binary mask of CCs (1 for CC, 0 otherwise) of shape (height, width).
    """
    height, width,c = ir_data.shape
    cc_mask = np.zeros_like(ir_data, dtype=np.float32)  # Initialize CC mask

    # Define the neighborhood (8-connected)
    neighbors = [(-1, -1), (-1, 0), (-1, 1),
                 (0, -1),   (0,0)  ,     (0, 1),
                 (1, -1),  (1, 0), (1, 1)]

    for i in range(1, height - 1):  # Avoid boundary pixels
        for j in range(1, width - 1):
            bt_ij = ir_data[i, j]

            # Condition 1: BT < 253K
            if (bt_ij >= 253).any():
                continue

            # Condition 2: BT <= BT_n for all neighbors
            is_local_min = True
            for di, dj in neighbors:
                if ir_data[i + di, j + dj] < bt_ij:
                    is_local_min = False
                    break
            if not is_local_min:
                continue

            # Condition 3: Gradient condition
            numerator1 = (ir_data[i - 1, j] + ir_data[i + 1, j] - 2 * bt_ij) / 3.1
            numerator2 = (ir_data[i, j - 1] + ir_data[i, j + 1] - 2 * bt_ij) / 8.0
            lhs = numerator1 + numerator2
            rhs = (4 / 5.8) * np.exp(0.0826 * (bt_ij - 217))

            if lhs > rhs:
                cc_mask[i, j] = 1  # Mark as CC

    return cc_mask

def compute_convective_core_masks(ir_data):
    """Extracts convective core masks for each IR image."""
    cc_mask = []
    
    for i in ir_data:
        c = extract_convective_cores(i)  # Assuming this function is defined
        c = np.array(c)
        cc_mask.append(c)
    
    return np.array(cc_mask)


# ------------------ Streamlit UI ------------------
# Configure the page with wide layout and custom title
st.set_page_config(
    page_title="Tropical Cyclone U-Net Wind Speed Predictor",
    layout="wide",
    initial_sidebar_state="expanded"
)

# Main title with emoji and styling
st.markdown("<h1 style='text-align: center;'>πŸŒ€ Tropical Cyclone U-Net Wind Speed (Intensity) Predictor</h1><br>", unsafe_allow_html=True)

# Authors section with ORCID links
st.markdown("""
<div style='text-align: center;'>
    <p>
        <b>By:</b>
        <a href="https://orcid.org/0009-0006-0342-429X" target="_blank" style="text-decoration: none">Dharun Krishna K B</a>,
        <a href="https://orcid.org/0009-0008-3214-8065" target="_blank" style="text-decoration: none">Nanduri Prudhvi Reddy</a> and
        <a href="https://orcid.org/0009-0006-9052-3623" target="_blank" style="text-decoration: none">Ravipati Venkata Madan Mohan</a>; School of Computing.<br>
        <b>Under the guidance of:</b>
        <a href="https://orcid.org/0000-0003-1969-3559" target="_blank" style="text-decoration: none">Dr. Gowri L</a>,
        Assistant Professor, School of Computing.<br>
        SASTRA Deemed University, Thanjavur, Tamil Nadu, India.<br><br>
        <b>For:</b>
        Main project titled <i>"Tropical Cyclone Intensity Prediction Using Deep Learning Models"</i><br>
        May 2025
    </p>
</div>
""", unsafe_allow_html=True)

# Add a divider before the main content
st.markdown('<div class="divider"></div>', unsafe_allow_html=True)

# Add spacing
st.markdown("<br>", unsafe_allow_html=True)

# App description
st.info('''The *Tropical Cyclone Wind Speed Predictor interface* enables the prediction of maximum sustained wind speeds of tropical cyclones (in knots) using IR and PMW imagery, along with physical attributes from the past 24 hours, while also facilitating comparison between state-of-the-art models and our proposed model.
''')

ir_images = st.file_uploader("Upload 8 IR images", type=["jpg", "jpeg", "png"], accept_multiple_files=True)
pmw_images = st.file_uploader("Upload 8 PMW images", type=["jpg", "jpeg", "png"], accept_multiple_files=True)

if len(ir_images) != 8 or len(pmw_images) != 8:
    st.warning("Please upload exactly 8 IR and 8 PMW images.")
else:
    st.success("Uploaded 8 IR and 8 PMW images successfully.")

st.header("Input Latitude, Longitude, Vmax")
lat_values, lon_values, vmax_values = [], [], []

import pandas as pd

import numpy as np

# File uploader
csv_file = st.file_uploader("Upload CSV file", type=["csv"])

if csv_file is not None:
    try:
        df = pd.read_csv(csv_file)
        required_columns = {'Latitude', 'Longitude', 'Vmax'}

        if required_columns.issubset(df.columns):
            lat_values = df['Latitude'].values
            lon_values = df['Longitude'].values
            vmax_values = df['Vmax'].values

            lat_values = np.array(lat_values)
            lon_values = np.array(lon_values)
            vmax_values = np.array(vmax_values)

            st.success("CSV file loaded and processed successfully!")
            
            # Display the dataframe in a scrollable container
            st.markdown("<h4>Preview of uploaded data:</h4>", unsafe_allow_html=True)
            preview_df = df.head(10).reset_index(drop=True)
            preview_df.index += 1  # Shift index to start from 1
            st.dataframe(preview_df, height=200)

        else:
            st.error("CSV file must contain 'Latitude', 'Longitude', and 'Vmax' columns.")
    except Exception as e:
        st.error(f"Error reading CSV: {e}")
else:
    st.warning("Please upload a CSV file.")

# Define data for ablation study
ablation_data = {
    "Model": [
        "TCIP-Net (3DCNN)",
        "TCIP-Net (ST-LSTM)",
        "TCIP-Net (ConvLSTM)",
        "TCIP-Net (TrajGRU)",
        "TCIP-Net (ConvGRU)",
        "TCUWSP-Net (Proposed)"
    ],
    "RMSE": [12.63, 12.52, 12.36, 12.24, 11.17, 8.6549],
    "MAE": [10.15, 10.12, 9.97, 9.93, 8.92, 6.309]
}

# Improved Prediction Model Section with better UI
st.markdown("<br>", unsafe_allow_html=True)
st.markdown("<h2 style='text-align: center;'>Select Prediction Model</h2>", unsafe_allow_html=True)

# Create columns for better layout
col1, col2, col3 = st.columns([1, 2, 1])

with col2:
    model_choice = st.selectbox(
        "Choose a model for prediction",
        ("TCIP-Net ConvGRU", "TCIP-Net ConvLSTM", "TCIP-Net Traj-GRU", "TCIP-Net 3DCNN", "TCIP-Net Spatio-temporal LSTM", "TCUWSP-Net (Proposed Model)"),
        index=0
    )
    
    # Center-aligned, more attractive submit button
    st.markdown("<br>", unsafe_allow_html=True)
    col_btn1, col_btn2 = st.columns(2)
    with col_btn1:
        submit_button = st.button("Predict Intensity", use_container_width=True)
    with col_btn2:
        all_models_button = st.button("Predict Intensity for All Models", use_container_width=True)    # ------------------ Process Single Model Button ------------------
    if submit_button:
        if len(ir_images) == 8 and len(pmw_images) == 8:
            # st.success("Starting preprocessing...")
            if model_choice == "TCUWSP-Net (Proposed Model)":
                from unetlstm import predict_unetlstm
                model_predict_fn = predict_unetlstm
            elif model_choice == "TCIP-Net ConvGRU":
                from gru_model import predict
                model_predict_fn = predict
            elif model_choice == "TCIP-Net ConvLSTM":
                from convlstm import predict_lstm
                model_predict_fn = predict_lstm
            elif model_choice == "TCIP-Net 3DCNN":
                from cnn3d import predict_3dcnn
                model_predict_fn = predict_3dcnn
            elif model_choice == "TCIP-Net Traj-GRU":
                from trjgru import predict_trajgru
                model_predict_fn = predict_trajgru
            elif model_choice == "TCIP-Net Spatio-temporal LSTM":
                from spaio_temp import predict_stlstm
                model_predict_fn = predict_stlstm

            ir_arrays = []
            pmw_arrays = []
            train_vmax_2d = reshape_vmax(np.array(vmax_values))

            train_vmax_3d= create_3d_vmax(train_vmax_2d)

            lat_processed = process_lat_values(lat_values)
            lon_processed = process_lon_values(lon_values)

            v_max_diff = calculate_intensity_difference(train_vmax_2d)

            for ir in ir_images:
                img = Image.open(ir).convert("L")
                arr = np.array(img).astype(np.float32)
                bt_arr = (arr / 255.0) * (310 - 190) + 190
                resized = cv2.resize(bt_arr, (95, 95), interpolation=cv2.INTER_CUBIC)
                ir_arrays.append(resized)

            for pmw in pmw_images:
                img = Image.open(pmw).convert("L")
                arr = np.array(img).astype(np.float32) / 255.0
                resized = cv2.resize(arr, (95, 95), interpolation=cv2.INTER_CUBIC)
                pmw_arrays.append(resized)
            ir=np.array(ir_arrays)
            pmw=np.array(pmw_arrays)
            
            # Stack into (8, 95, 95)
            ir_seq = process_images(ir)
            pmw_seq = process_images(pmw)


            # For demonstration: create batches
            X_train_new = ir_seq.reshape((1, 8, 95, 95)) # Shape: (1, 8, 95, 95)
    
            cc_mask= compute_convective_core_masks(X_train_new)
            hov_m_train = generate_hovmoller(X_train_new)
            hov_m_train[np.isnan(hov_m_train)] = 0 
            hov_m_train = hov_m_train.transpose(0, 2, 3, 1) 

            cc_mask[np.isnan(cc_mask)] = 0
            cc_mask=cc_mask.reshape(1, 8, 95, 95, 1)
            i_images=cc_mask+ir_seq
            reduced_images = np.concatenate([i_images,pmw_seq ], axis=-1)
            reduced_images[np.isnan(reduced_images)] = 0

            if model_choice == "Unet_LSTM":
                import tensorflow as tf

                def tf_gradient_magnitude(images):
                    # Sobel kernels
                    sobel_x = tf.constant([[1, 0, -1], [2, 0, -2], [1, 0, -1]], dtype=tf.float32)
                    sobel_y = tf.constant([[1, 2, 1], [0, 0, 0], [-1, -2, -1]], dtype=tf.float32)
                    sobel_x = tf.reshape(sobel_x, [3, 3, 1, 1])
                    sobel_y = tf.reshape(sobel_y, [3, 3, 1, 1])

                    images = tf.convert_to_tensor(images, dtype=tf.float32)
                    images = tf.expand_dims(images, -1)

                    gx = tf.nn.conv2d(images, sobel_x, strides=1, padding='SAME')
                    gy = tf.nn.conv2d(images, sobel_y, strides=1, padding='SAME')
                    grad_mag = tf.sqrt(tf.square(gx) + tf.square(gy) + 1e-6)

                    return tf.squeeze(grad_mag, -1).numpy()
                def GM_maps_prep(ir):
                    GM_maps=[]
                    for i in ir:
                        GM_map = tf_gradient_magnitude(i)
                        GM_maps.append(GM_map)
                    GM_maps=np.array(GM_maps)
                    return GM_maps
                ir_seq=ir_seq.reshape(8, 95, 95, 1)
                GM_maps = GM_maps_prep(ir_seq)
                print(GM_maps.shape)
                GM_maps=GM_maps.reshape(1, 8, 95, 95, 1)
                i_images=cc_mask+ir_seq+GM_maps
                reduced_images = np.concatenate([i_images,pmw_seq ], axis=-1)
                reduced_images[np.isnan(reduced_images)] = 0
                print(reduced_images.shape)
                y = model_predict_fn(reduced_images, hov_m_train, train_vmax_3d, lat_processed, lon_processed, v_max_diff)
            else:
                y = model_predict_fn(reduced_images, hov_m_train, train_vmax_3d, lat_processed, lon_processed, v_max_diff)
            st.write("Predicted Maximum Sustained Wind Speed [Vmax] (in knots):", y)
        else:
            st.error("Make sure you uploaded exactly 8 IR and 8 PMW images.")

# ------------------ Process All Models Button ------------------
    if all_models_button:
        if len(ir_images) == 8 and len(pmw_images) == 8:
            st.info("Running predictions for all models... This may take a moment.")
            
            # Store all model names and prediction functions
            all_model_names = [
                "TCIP-Net (3DCNN)",
                "TCIP-Net (ST-LSTM)",
                "TCIP-Net (ConvLSTM)",
                "TCIP-Net (TrajGRU)",
                "TCIP-Net (ConvGRU)",
                "TCUWSP-Net (Proposed)"
            ]
            
            # Process input data once for all models
            ir_arrays = []
            pmw_arrays = []
            train_vmax_2d = reshape_vmax(np.array(vmax_values))
            train_vmax_3d = create_3d_vmax(train_vmax_2d)
            lat_processed = process_lat_values(lat_values)
            lon_processed = process_lon_values(lon_values)
            v_max_diff = calculate_intensity_difference(train_vmax_2d)

            for ir in ir_images:
                img = Image.open(ir).convert("L")
                arr = np.array(img).astype(np.float32)
                bt_arr = (arr / 255.0) * (310 - 190) + 190
                resized = cv2.resize(bt_arr, (95, 95), interpolation=cv2.INTER_CUBIC)
                ir_arrays.append(resized)

            for pmw in pmw_images:
                img = Image.open(pmw).convert("L")
                arr = np.array(img).astype(np.float32) / 255.0
                resized = cv2.resize(arr, (95, 95), interpolation=cv2.INTER_CUBIC)
                pmw_arrays.append(resized)
                
            ir = np.array(ir_arrays)
            pmw = np.array(pmw_arrays)
            
            ir_seq = process_images(ir)
            pmw_seq = process_images(pmw)
            
            X_train_new = ir_seq.reshape((1, 8, 95, 95))
            cc_mask = compute_convective_core_masks(X_train_new)
            hov_m_train = generate_hovmoller(X_train_new)
            hov_m_train[np.isnan(hov_m_train)] = 0 
            hov_m_train = hov_m_train.transpose(0, 2, 3, 1)
            
            cc_mask[np.isnan(cc_mask)] = 0
            cc_mask = cc_mask.reshape(1, 8, 95, 95, 1)
            i_images = cc_mask + ir_seq
            reduced_images = np.concatenate([i_images, pmw_seq], axis=-1)
            reduced_images[np.isnan(reduced_images)] = 0
            
            # Special processing for Unet_LSTM model if needed
            import tensorflow as tf
            def tf_gradient_magnitude(images):
                # Sobel kernels
                sobel_x = tf.constant([[1, 0, -1], [2, 0, -2], [1, 0, -1]], dtype=tf.float32)
                sobel_y = tf.constant([[1, 2, 1], [0, 0, 0], [-1, -2, -1]], dtype=tf.float32)
                sobel_x = tf.reshape(sobel_x, [3, 3, 1, 1])
                sobel_y = tf.reshape(sobel_y, [3, 3, 1, 1])

                images = tf.convert_to_tensor(images, dtype=tf.float32)
                images = tf.expand_dims(images, -1)

                gx = tf.nn.conv2d(images, sobel_x, strides=1, padding='SAME')
                gy = tf.nn.conv2d(images, sobel_y, strides=1, padding='SAME')
                grad_mag = tf.sqrt(tf.square(gx) + tf.square(gy) + 1e-6)

                return tf.squeeze(grad_mag, -1).numpy()
            
            def GM_maps_prep(ir):
                GM_maps=[]
                for i in ir:
                    GM_map = tf_gradient_magnitude(i)
                    GM_maps.append(GM_map)
                GM_maps=np.array(GM_maps)
                return GM_maps
                
            # For Unet_LSTM model
            ir_seq_reshaped = ir_seq.reshape(8, 95, 95, 1)
            GM_maps = GM_maps_prep(ir_seq_reshaped)
            GM_maps = GM_maps.reshape(1, 8, 95, 95, 1)
            i_images_unet = cc_mask + ir_seq_reshaped + GM_maps
            reduced_images_unet = np.concatenate([i_images_unet, pmw_seq], axis=-1)
            reduced_images_unet[np.isnan(reduced_images_unet)] = 0
            
            # Run predictions for all models
            predictions = []
            progress_bar = st.progress(0)
            
            # Import all prediction functions
            from cnn3d import predict_3dcnn
            from spaio_temp import predict_stlstm
            from convlstm import predict_lstm
            from trjgru import predict_trajgru
            from gru_model import predict
            from unetlstm import predict_unetlstm
            
            prediction_functions = [
                predict_3dcnn,    # 3DCNN
                predict_stlstm,   # ST-LSTM
                predict_lstm,     # ConvLSTM
                predict_trajgru,  # TrajGRU
                predict,          # ConvGRU
                predict_unetlstm  # TCUWSP-Net
            ]
            
            # Run predictions
            for i, predict_fn in enumerate(prediction_functions):
                progress_bar.progress((i) / len(prediction_functions))
                
                # Special case for TCUWSP-Net (Proposed Model)
                if i == 5:  # TCUWSP-Net index
                    y = predict_fn(reduced_images_unet, hov_m_train, train_vmax_3d, lat_processed, lon_processed, v_max_diff)
                else:
                    y = predict_fn(reduced_images, hov_m_train, train_vmax_3d, lat_processed, lon_processed, v_max_diff)
                    
                predictions.append(float(y))
                
            progress_bar.progress(1.0)
            
            # Create results DataFrame
            results_data = {
                "Model": all_model_names,
                "RMSE": ablation_data["RMSE"],
                "MAE": ablation_data["MAE"],
                "Predicted Vmax (kt)": predictions
            }
            
            results_df = pd.DataFrame(results_data)
            
            # Show DataFrame
            st.subheader("Prediction Results from All Models")
            st.dataframe(results_df, use_container_width=True)
            
            # Generate and display comparison chart
            st.subheader("Visual Comparison of Models")
            
            # Prepare data for visualization
            plot_model_names = [name.replace(" ", "\n") for name in all_model_names]
            mae_values = results_df["MAE"].tolist()
            rmse_values = results_df["RMSE"].tolist()
            predicted_values = results_df["Predicted Vmax (kt)"].tolist()
            
            # Generate figure
            fig = generate_comparison_chart(plot_model_names, mae_values, rmse_values, predicted_values)
            
            # Display figure
            st.pyplot(fig)
            
            # Add some interpretation
            st.subheader("Interpretation")
            st.write("""
            - **RMSE and MAE**: Lower values indicate better model performance.
            - **Percentage Improvements**: Show reduction in error compared to the baseline TCIP-Net (3DCNN) model.
            - **Predicted Vmax**: The current intensity prediction for the tropical cyclone based on the provided imagery and historical data.
            """)
            
            # Highlight best model
            best_model_idx = rmse_values.index(min(rmse_values))
            best_model = all_model_names[best_model_idx]
            best_prediction = predicted_values[best_model_idx]
            
            st.success(f"🌟 Best performing model: **{best_model}** with RMSE: **{min(rmse_values):.2f} kt** and predicted intensity: **{best_prediction:.2f} kt**")
        else:
            st.error("Make sure you uploaded exactly 8 IR and 8 PMW images.")