Skip to content
Snippets Groups Projects

Data explorer series

1 file
+ 63
25
Compare changes
  • Side-by-side
  • Inline
+ 63
25
@@ -17,6 +17,7 @@ app.launch()
import datetime
import os
import re
import gradio as gr
import matplotlib
@@ -119,13 +120,13 @@ class Interface:
value="", elem_classes="btn-html h-36"
)
explorer = gr.FileExplorer(
ignore_glob="*/.*", # ignores hidden files
ignore_glob="*/.*", # ignores hidden files
root_dir=os.getcwd(),
label=os.getcwd(),
render=True,
file_count="single",
interactive=True,
elem_classes="h-256 hide-overflow",
elem_classes="h-320 hide-overflow",
)
with gr.Column(scale=1):
@@ -137,22 +138,33 @@ class Interface:
interactive=True,
)
virtual_stack = gr.Checkbox(value=False, label="Virtual stack", info="If checked, will use less memory by loading the images on demand.")
load_series = gr.Checkbox(value=False, label="Load series", info="If checked, will load the whole series of images in the same folder as the selected file.")
virtual_stack = gr.Checkbox(
value=False,
label="Virtual stack",
info="If checked, will use less memory by loading the images on demand.",
)
load_series = gr.Checkbox(
value=False,
label="Load series",
info="If checked, will load the whole series of images in the same folder as the selected file.",
)
series_contains = gr.Textbox(
label="Specify common part of file names for series",
value="",
visible=False,
)
dataset_name = gr.Textbox(
label="Dataset name (in case of H5 files, for example)",
value="exchange/data",
)
def toggle_show(checkbox):
return gr.update(visible=True) if checkbox else gr.update(visible=False)
def toggle_show(checkbox):
return (
gr.update(visible=True)
if checkbox
else gr.update(visible=False)
)
# Show series_contains only if load_series is checked
load_series.change(toggle_show, load_series, series_contains)
@@ -211,7 +223,7 @@ class Interface:
# Intensity histogram
with gr.Column(visible=False) as result_intensity_histogram:
hist_plot = gr.Plot(label="Volume intensity histogram")
# Text box with data summary
with gr.Column(visible=False) as result_data_summary:
data_summary = gr.Text(
@@ -259,7 +271,6 @@ class Interface:
min_projection_plot,
hist_plot,
data_summary,
]
### Listeners
@@ -300,7 +311,7 @@ class Interface:
# Starts a new session dictionary
session = Session()
# Tells rest of the pipeline if the session failed at some point prior, so should skip the rest
session.failed = False
session.failed = False
session.all_operations = Interface().operations
session.operations = args[0]
session.base_path = args[1]
@@ -315,20 +326,35 @@ class Interface:
session.series_contains = args[10]
if session.load_series and session.series_contains == "":
session.failed = True
raise gr.Error("For series, common part of file name must be provided in 'series_contains' field.")
# Try to guess the common part of the file names
try:
filename = session.explorer.split("/")[-1] # Extract filename from path
series_contains = re.search(r"[^0-9]+", filename).group()
gr.Info(f"Using '{series_contains}' as common file name part for loading.")
session.series_contains = series_contains
except:
session.failed = True
raise gr.Error(
"For series, common part of file name must be provided in 'series_contains' field."
)
# Get the file path from the explorer or base path
# priority is given to the explorer if file is selected
# else the base path is used
if session.explorer and (os.path.isfile(session.explorer) or session.load_series):
if session.explorer and (
os.path.isfile(session.explorer) or session.load_series
):
session.file_path = session.explorer
elif session.base_path and (os.path.isfile(session.base_path) or session.load_series):
elif session.base_path and (
os.path.isfile(session.base_path) or session.load_series
):
session.file_path = session.base_path
else:
session.failed = True
raise gr.Error("Invalid file path")
# If we are loading a series, we need to get the directory
if session.load_series:
session.file_path = os.path.dirname(session.file_path)
@@ -436,7 +462,7 @@ class Pipeline:
# skip loading if session failed at some point prior
if session.failed:
return session
try:
session.vol = load(
session.file_path,
@@ -444,8 +470,16 @@ class Pipeline:
dataset_name=session.dataset_name,
contains=session.series_contains,
)
# Incase the data is 4D (RGB for example), we take the mean of the last dimension
if session.vol.ndim == 4:
session.vol = np.mean(session.vol, axis=-1)
# The rest of the pipeline expects 3D data
if session.vol.ndim != 3:
raise gr.Error(f"Invalid data shape should be 3 dimensional, not shape: {session.vol.shape}")
raise gr.Error(
f"Invalid data shape should be 3 dimensional, not shape: {session.vol.shape}"
)
except Exception as error_message:
raise gr.Error(
f"Failed to load the image: {error_message}"
@@ -473,7 +507,7 @@ class Pipeline:
# skip loading if session failed at some point prior
if session.failed:
return []
self.session = session
outputs = []
log.info(session.all_operations)
@@ -652,12 +686,15 @@ class Pipeline:
def plot_vol_histogram(self):
# The Histogram needs results from the projections
# The Histogram needs results from the projections
if not self.session.projections_calculated:
_ = self.get_projections(self.session.vol)
vol_hist, bin_edges = self.vol_histogram(
self.session.vol, self.session.nbins, self.session.min_value, self.session.max_value
self.session.vol,
self.session.nbins,
self.session.min_value,
self.session.max_value,
)
fig, ax = plt.subplots(figsize=(6, 4))
@@ -690,11 +727,12 @@ class Pipeline:
return vol_hist, bin_edges
def run_interface(host = "0.0.0.0"):
def run_interface(host="0.0.0.0"):
gradio_interface = Interface().create_interface()
internal_tools.run_gradio_app(gradio_interface,host)
internal_tools.run_gradio_app(gradio_interface, host)
if __name__ == "__main__":
# Creates interface
run_interface()
\ No newline at end of file
run_interface()
Loading