From 6709038fcc64b3c69346da9574c8ab25244accb2 Mon Sep 17 00:00:00 2001
From: s204159 <s204159@student.dtu.dk>
Date: Mon, 20 Jan 2025 09:59:38 +0100
Subject: [PATCH] Gradio plot flicker fix

---
 qim3d/gui/data_explorer.py   | 20 +++++++++++++++++---
 qim3d/gui/local_thickness.py | 19 +++++++++++++++++++
 2 files changed, 36 insertions(+), 3 deletions(-)

diff --git a/qim3d/gui/data_explorer.py b/qim3d/gui/data_explorer.py
index ecabeb81..1a4aa105 100644
--- a/qim3d/gui/data_explorer.py
+++ b/qim3d/gui/data_explorer.py
@@ -163,17 +163,31 @@ class Interface(BaseInterface):
 
         # Visualization and results
         with gr.Row():
+            def create_uniform_image(intensity=1):
+                """
+                Generates a blank image with a single color.
+                Gradio `gr.Plot` components will flicker if there is no default value.
+                bug fix on gradio version 4.44.0
+                """
+                pixels = np.zeros((100, 100, 3), dtype=np.uint8) + int(intensity * 255)
+                fig, ax = plt.subplots(figsize=(10, 10))
+                ax.imshow(pixels, interpolation="nearest")
+
+                # Adjustments
+                ax.axis("off")
+                fig.subplots_adjust(left=0, right=1, bottom=0, top=1)
+                return fig
 
             # Z Slicer
             with gr.Column(visible=False) as result_z_slicer:
-                zslice_plot = gr.Plot(label="Z slice")
+                zslice_plot = gr.Plot(label="Z slice", value=create_uniform_image(1))
                 zpos = gr.Slider(
                     minimum=0, maximum=1, value=0.5, step=0.01, label="Z position"
                 )
 
             # Y Slicer
             with gr.Column(visible=False) as result_y_slicer:
-                yslice_plot = gr.Plot(label="Y slice")
+                yslice_plot = gr.Plot(label="Y slice", value=create_uniform_image(1))
 
                 ypos = gr.Slider(
                     minimum=0, maximum=1, value=0.5, step=0.01, label="Y position"
@@ -181,7 +195,7 @@ class Interface(BaseInterface):
 
             # X Slicer
             with gr.Column(visible=False) as result_x_slicer:
-                xslice_plot = gr.Plot(label="X slice")
+                xslice_plot = gr.Plot(label="X slice", value=create_uniform_image(1))
 
                 xpos = gr.Slider(
                     minimum=0, maximum=1, value=0.5, step=0.01, label="X position"
diff --git a/qim3d/gui/local_thickness.py b/qim3d/gui/local_thickness.py
index 0c76f1aa..3c7ae9aa 100644
--- a/qim3d/gui/local_thickness.py
+++ b/qim3d/gui/local_thickness.py
@@ -164,29 +164,48 @@ class Interface(InterfaceWithExamples):
 
                 
             with gr.Column(scale=4):
+                def create_uniform_image(intensity=1):
+                    """
+                    Generates a blank image with a single color.
+                    Gradio `gr.Plot` components will flicker if there is no default value.
+                    bug fix on gradio version 4.44.0
+                    """
+                    pixels = np.zeros((100, 100, 3), dtype=np.uint8) + int(intensity * 255)
+                    fig, ax = plt.subplots(figsize=(10, 10))
+                    ax.imshow(pixels, interpolation="nearest")
+
+                    # Adjustments
+                    ax.axis("off")
+                    fig.subplots_adjust(left=0, right=1, bottom=0, top=1)
+                    return fig
+                
                 with gr.Row():
                     input_vol = gr.Plot(
                         show_label=True,
                         label="Original",
                         visible=True,
+                        value=create_uniform_image(),
                     )
 
                     binary_vol = gr.Plot(
                         show_label=True,
                         label="Binary",
                         visible=True,
+                        value=create_uniform_image(),
                     )
 
                     output_vol = gr.Plot(
                         show_label=True,
                         label="Local thickness",
                         visible=True,
+                        value=create_uniform_image(),
                     )
                 with gr.Row():
                     histogram = gr.Plot(
                         show_label=True,
                         label="Thickness histogram",
                         visible=True,
+                        value=create_uniform_image(),
                     )
                 with gr.Row():
                     lt_output = gr.File(
-- 
GitLab