File size: 15,015 Bytes
b47f3f1
 
 
 
 
 
 
8a6974b
b47f3f1
 
 
 
8a6974b
b47f3f1
 
8a6974b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b47f3f1
 
 
8a6974b
 
b47f3f1
 
 
8a6974b
 
 
 
 
 
 
 
 
 
 
b47f3f1
 
 
 
8a6974b
 
 
 
 
 
 
 
 
 
 
b47f3f1
 
 
8a6974b
b47f3f1
 
8a6974b
b47f3f1
 
 
8a6974b
b47f3f1
 
8a6974b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b47f3f1
 
8a6974b
b47f3f1
 
 
 
8a6974b
b47f3f1
 
 
8a6974b
 
 
 
 
b47f3f1
8a6974b
 
 
 
 
b47f3f1
 
 
8a6974b
b47f3f1
 
 
8a6974b
 
 
 
 
 
 
b47f3f1
 
 
8a6974b
 
 
 
 
 
 
 
b47f3f1
 
 
 
 
 
 
 
 
 
8a6974b
b47f3f1
8a6974b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b47f3f1
 
8a6974b
 
b47f3f1
8a6974b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6076fa9
b47f3f1
8a6974b
 
b47f3f1
 
8a6974b
 
 
b47f3f1
 
8a6974b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b47f3f1
8a6974b
 
 
b47f3f1
 
 
8a6974b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b47f3f1
 
 
 
8a6974b
b47f3f1
8a6974b
 
 
 
 
 
 
 
 
 
b47f3f1
8a6974b
 
 
 
 
b47f3f1
8a6974b
 
 
b47f3f1
8a6974b
 
b47f3f1
 
8a6974b
 
 
 
b47f3f1
8a6974b
 
 
 
 
b47f3f1
8a6974b
b47f3f1
8a6974b
b47f3f1
8a6974b
 
 
6076fa9
8a6974b
b47f3f1
 
8a6974b
b47f3f1
8a6974b
 
 
6076fa9
b47f3f1
 
8a6974b
 
 
 
6076fa9
b47f3f1
8a6974b
 
 
 
6076fa9
b47f3f1
8a6974b
6076fa9
b47f3f1
 
6076fa9
8a6974b
 
 
 
 
 
 
 
 
 
 
 
 
 
6076fa9
8a6974b
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
import streamlit as st
import tempfile
import os
import nibabel as nib
import numpy as np
import plotly.graph_objects as go
from scipy.ndimage import zoom
import time

# Set the page layout to wide
st.set_page_config(layout="wide")

# Optional: logo (uncomment if you have the resource)
# st.image("resources/biomedia.png", width=200)

st.markdown(
    """
    <style>
    /* Change the font size of the sidebar */
    .sidebar .sidebar-content {
        font-size: 2px;  
    }
    /* Change the line spacing of the sidebar */
    .sidebar .sidebar-content p {
        line-height: 0.2;  /* Adjust the line height as needed */
    }
    </style>
    """,
    unsafe_allow_html=True
)

st.title('MRI Scan Visualization :brain:')

st.write("""
         The **MRI Visualization** page offers an interactive platform for users to explore 3D MRI scans in detail. It is designed to enhance the understanding of anatomical structures and any associated segmentation data.
""")

with st.expander("### **Features:**"):
    st.write("""
    - **Upload MRI Scans:**
        - Users can upload their own 3D MRI scans in NIfTI format (`.nii` or `.nii.gz`).
        - An optional checkbox allows users to upload a corresponding segmentation mask if available.
    
    - **Interactive Slice Navigation:**
        - Provides axial, coronal, and sagittal views of the MRI scan.
        - Users can scroll through slices in each plane using the interactive slider.

    - **Segmentation Overlay:**
        - When a segmentation mask is provided, it overlays onto the MRI images.
        - Segmented regions are highlighted with transparent colors to maintain visibility of underlying anatomy.
    """)

with st.expander("## **Instructions:**"):
    st.write("""
    1. **Upload Your MRI Scan:**
        - Use the file uploader to select your MRI scan file.
        - Supported formats: NIfTI (`.nii`, `.nii.gz`).

    2. **Upload Segmentation Mask (Optional):**
        - If you have a segmentation mask, check the **"Include Segmentation Mask"** checkbox.
        - Upload your segmentation mask file in NIfTI format.

    3. **View MRI Slices:**
        - After uploading, the application will display the MRI scan in three views: axial, coronal, and sagittal.
        - Use the slider beneath each visualization to navigate through the slices.
    """)

uploaded_file = st.file_uploader("Upload MRI file", type=["gz", "nii", "nii.gz"])

segments = st.checkbox("Show Segmentation")
segmentation_file = None

if segments:
    segmentation_file = st.file_uploader("Upload segmentation file", type=["gz", "nii", "nii.gz"])

# Define segmentation colors
segmentation_colors = {
    0: '#000000',  # Background (black)
    2: '#759479',  # Left cerebral white matter (purple)
    3: '#f7d587',  # Left cerebral cortex (orange)
    4: '#bb7761',  # Left lateral ventricle (brown)
    5: '#53bad5',  # Left inferior lateral ventricle (cyan)
    7: '#e95c47',  # Left cerebellum white matter (red)
    8: '#ec7c5e',  # Left cerebellum cortex (light salmon)
    10: '#6cf185', # Left thalamus (green)
    11: '#ce6253', # Left caudate (brown)
    12: '#d6f600', # Left putamen (yellow)
    13: '#513e00', # Left pallidum (saddle brown)
    14: '#fffad9', # 3rd ventricle (white)
    15: '#e8dc00', # 4th ventricle (yellow)
    16: '#c8c8ee', # Brain-stem (lavender)
    17: '#fafacd', # Left hippocampus (lemon chiffon)
    18: '#fad500', # Left amygdala (yellow)
    24: '#009ad3', # CSF (cyan)
    26: '#e95c47', # Left accumbens area (red)
    28: '#bc9be1', # Left ventral DC (lavender)
    41: '#afd7d3', # Right cerebral white matter (light cyan)
    42: '#8ebed1', # Right cerebral cortex (light blue)
    43: '#53bad5', # Right lateral ventricle (cyan)
    44: '#a9d5f5', # Right inferior lateral ventricle (cyan)
    46: '#00af5c', # Right cerebellum white matter (light green)
    47: '#4ac87c', # Right cerebellum cortex (pale green)
    49: '#00bfff', # Right thalamus (cyan)
    50: '#009400', # Right caudate (green)
    51: '#d2e772', # Right putamen (green yellow)
    52: '#513e00', # Right pallidum (saddle brown)
    53: '#d0ffff', # Right hippocampus (light cyan)
    54: '#90fdfb', # Right amygdala (cyan)
    58: '#6de3e6', # Right accumbens area (cyan)
    60: '#cc3400', # Right ventral DC (red)
}

# Label names for legend
label_names = {
    2: 'Left cerebral white matter',
    3: 'Left cerebral cortex',
    4: 'Left lateral ventricle',
    5: 'Left inferior lateral ventricle',
    7: 'Left cerebellum white matter',
    8: 'Left cerebellum cortex',
    10: 'Left thalamus',
    11: 'Left caudate',
    12: 'Left putamen',
    13: 'Left pallidum',
    14: '3rd ventricle',
    15: '4th ventricle',
    16: 'Brain-stem',
    17: 'Left hippocampus',
    18: 'Left amygdala',
    24: 'CSF',
    26: 'Left accumbens area',
    28: 'Left ventral DC',
    41: 'Right cerebral white matter',
    42: 'Right cerebral cortex',
    43: 'Right lateral ventricle',
    44: 'Right inferior lateral ventricle',
    46: 'Right cerebellum white matter',
    47: 'Right cerebellum cortex',
    49: 'Right thalamus',
    50: 'Right caudate',
    51: 'Right putamen',
    52: 'Right pallidum',
    53: 'Right hippocampus',
    54: 'Right amygdala',
    58: 'Right accumbens area',
    60: 'Right ventral DC',
}

@st.cache_data
def create_plotly_colorscale(segmentation_colors):
    """Create a Plotly-compatible colorscale for segmentation"""
    max_value = max(segmentation_colors.keys())
    colorscale = []
    for i in range(max_value + 1):
        color = segmentation_colors.get(i, '#000000')
        colorscale.append([i / max_value, color])
    return colorscale

def get_label_name(value):
    """Get the label name for a segmentation value"""
    return label_names.get(value, f'Unknown ({value})')

def add_segmentation_legend_sidebar():
    """Add segmentation legend to sidebar"""
    st.sidebar.title("Segmentation Labels")
    for value, color in segmentation_colors.items():
        if value != 0:  # Skip background
            st.sidebar.markdown(f"<span style='color:{color};'>⬤</span> {get_label_name(value)}", unsafe_allow_html=True)

def plot_mri_slice(data, seg_data, view, pixel_spacing, slice_thickness):
    """
    Plot MRI slice with segmentation overlay - optimized version
    """
    # Create an empty frames list to store all slices
    frames = []
    
    # Set appropriate dimensions and orientation based on view
    if view == 'Axial':
        data = np.flip(data, axis=0)
        seg_data = np.flip(seg_data, axis=0) if seg_data is not None else None
        aspect_ratio = slice_thickness / pixel_spacing[0]
        height = 800
        num_slices = data.shape[1]
        
        # Function to extract slice for this view
        def extract_slice(i):
            img = data[:, i, :]
            seg_img = None if seg_data is None else seg_data[:, i, :]
            return img, seg_img
            
    elif view == 'Coronal':
        aspect_ratio = slice_thickness / pixel_spacing[0]
        height = 400
        num_slices = data.shape[0]
        
        # Function to extract slice for this view
        def extract_slice(i):
            img = data[i, :, :]
            seg_img = None if seg_data is None else seg_data[i, :, :]
            return img, seg_img
            
    elif view == 'Sagittal':
        aspect_ratio = pixel_spacing[1] / pixel_spacing[0]
        height = 400
        num_slices = data.shape[2]
        
        # Function to extract slice for this view
        def extract_slice(i):
            img = np.rot90(data[:, :, i])
            img = np.flip(img, axis=0)
            seg_img = None
            if seg_data is not None:
                seg_img = np.rot90(seg_data[:, :, i])
                seg_img = np.flip(seg_img, axis=0)
            return img, seg_img
    
    # Initialize figure
    fig = go.Figure()
    
    # Get colorscale for segmentation
    segmentation_colorscale = create_plotly_colorscale(segmentation_colors)
    
    # Create frames for each slice - this is the key optimization
    # We only create a limited number of frames for performance but keep smooth animation
    # For very large volumes, we sample every nth slice
    skip_factor = max(1, num_slices // 100)  # Don't create more than ~100 frames
    
    for i in range(0, num_slices, skip_factor):
        img, seg_img = extract_slice(i)
        
        frame_data = [go.Heatmap(z=img, colorscale='gray', showscale=False)]
        
        if seg_img is not None:
            # Only create hover text for visible segmentation values (non-zero)
            hover_text = np.full(seg_img.shape, '', dtype='object')
            for unique_val in np.unique(seg_img):
                if unique_val > 0 and unique_val in label_names:
                    mask = seg_img == unique_val
                    hover_text[mask] = get_label_name(unique_val)
            
            frame_data.append(go.Heatmap(
                z=seg_img,
                colorscale=segmentation_colorscale,
                showscale=False,
                opacity=0.5,
                hoverinfo='text',
                text=hover_text,
                hoverongaps=False
            ))
        
        frames.append(go.Frame(data=frame_data, name=f'slice{i}'))
    
    # Add the initial (middle) slice to the figure
    middle_idx = num_slices // 2
    middle_img, middle_seg = extract_slice(middle_idx)
    
    fig.add_trace(go.Heatmap(z=middle_img, colorscale='gray', showscale=False))
    
    if middle_seg is not None:
        # Create hover text for middle slice
        middle_hover = np.full(middle_seg.shape, '', dtype='object')
        for unique_val in np.unique(middle_seg):
            if unique_val > 0 and unique_val in label_names:
                mask = middle_seg == unique_val
                middle_hover[mask] = get_label_name(unique_val)
        
        fig.add_trace(go.Heatmap(
            z=middle_seg,
            colorscale=segmentation_colorscale,
            showscale=False,
            opacity=0.5,
            hoverinfo='text',
            text=middle_hover,
            hoverongaps=False
        ))
    
    # Add frames to the figure
    fig.frames = frames
    
    # Configure layout with slider
    sliders = [{
        'active': num_slices // (2 * skip_factor),  # Set to middle slice
        'currentvalue': {'prefix': 'Slice: ', 'visible': True},
        'pad': {'t': 50},
        'len': 0.9,
        'x': 0.1,
        'y': 0,
        'steps': [
            {
                'args': [
                    [f'slice{i}'],
                    {'frame': {'duration': 0, 'redraw': True}}
                ],
                'label': str(i),
                'method': 'animate'
            }
            for i in range(0, num_slices, skip_factor)
        ]
    }]
    
    fig.update_layout(
        title=f'{view} View',
        height=height,
        xaxis=dict(scaleanchor='y', scaleratio=aspect_ratio),
        margin=dict(l=0, r=0, t=30, b=0),
        xaxis_visible=False,
        yaxis_visible=False,
        sliders=sliders,
        updatemenus=[{
            'buttons': [
                {
                    'args': [None, {'frame': {'duration': 500, 'redraw': True}}],
                    'label': '▶',
                    'method': 'animate'
                },
                {
                    'args': [[None], {'frame': {'duration': 0, 'redraw': True}}],
                    'label': '◼',
                    'method': 'animate'
                }
            ],
            'type': 'buttons',
            'direction': 'left',
            'showactive': False,
            'x': 0.1,
            'y': 0,
            'pad': {'r': 10, 't': 60}
        }]
    )
    
    return fig

# Main execution
if uploaded_file:
    # Create progress indicator
    progress_bar = st.progress(0)
    status_text = st.empty()
    
    # Step 1: Load MRI file
    status_text.text("Loading MRI file...")
    temp_dir = tempfile.mkdtemp()
    mri_path = os.path.join(temp_dir, uploaded_file.name)
    with open(mri_path, "wb") as f:
        f.write(uploaded_file.getvalue())
    
    # Load MRI image
    progress_bar.progress(25)
    img = nib.load(mri_path)
    data = img.get_fdata()
    header = img.header
    
    # Get pixel spacing for aspect ratio
    pixel_spacing = header['pixdim'][1:3]
    slice_thickness = header['pixdim'][3]
    
    # Step 2: Load segmentation if provided
    progress_bar.progress(40)
    seg_data = None
    if segments and segmentation_file:
        status_text.text("Loading segmentation file...")
        seg_path = os.path.join(temp_dir, segmentation_file.name)
        with open(seg_path, "wb") as f:
            f.write(segmentation_file.getvalue())
        
        # Load segmentation
        seg_img = nib.load(seg_path)
        seg_data = seg_img.get_fdata()
        
        # Resample segmentation if dimensions don't match
        if seg_data.shape != data.shape:
            status_text.text("Resampling segmentation to match MRI dimensions...")
            zoom_factors = np.array(data.shape) / np.array(seg_data.shape)
            seg_data = zoom(seg_data, zoom_factors, order=0)  # Use nearest-neighbor interpolation
    
    # Step 3: Create visualizations
    progress_bar.progress(60)
    status_text.text("Creating visualizations...")
    
    # Create columns for layout
    col1, col2 = st.columns([2, 1], gap="small")
    
    # Create axial view (top-down)
    with col1:
        progress_bar.progress(70)
        status_text.text("Rendering axial view...")
        axial_fig = plot_mri_slice(data, seg_data, 'Axial', pixel_spacing, slice_thickness)
        axial_chart = st.plotly_chart(axial_fig, use_container_width=True)
    
    with col2:
        # Create coronal view (front-back)
        progress_bar.progress(85)
        status_text.text("Rendering coronal view...")
        coronal_fig = plot_mri_slice(data, seg_data, 'Coronal', pixel_spacing, slice_thickness)
        coronal_chart = st.plotly_chart(coronal_fig, use_container_width=True)
        
        # Create sagittal view (side)
        progress_bar.progress(95)
        status_text.text("Rendering sagittal view...")
        sagittal_fig = plot_mri_slice(data, seg_data, 'Sagittal', pixel_spacing, slice_thickness)
        sagittal_chart = st.plotly_chart(sagittal_fig, use_container_width=True)
    
    # Add segmentation legend to sidebar if using segmentation
    if segments and segmentation_file and seg_data is not None:
        add_segmentation_legend_sidebar()
    
    # Cleanup
    progress_bar.progress(100)
    status_text.text("Visualization complete!")
    time.sleep(1)
    status_text.empty()
    progress_bar.empty()
    
    # Clean up temporary files
    try:
        os.remove(mri_path)
        if segments and segmentation_file:
            os.remove(seg_path)
        os.rmdir(temp_dir)
    except:
        pass  # Ignore cleanup errors
else:
    st.info("Please upload an MRI file to begin visualization.")