Replace path-based fileinput with an upload button

This commit is contained in:
usov_i 2020-09-01 11:04:55 +02:00
parent 900d8a37b5
commit 2fcf5183c5
3 changed files with 48 additions and 41 deletions

View File

@ -10,17 +10,13 @@ parser = argparse.ArgumentParser(
prog="pyzebra", formatter_class=argparse.ArgumentDefaultsHelpFormatter prog="pyzebra", formatter_class=argparse.ArgumentDefaultsHelpFormatter
) )
parser.add_argument(
"--init-meta", metavar="PATH", type=str, default="", help="initial path to .cami file",
)
args = parser.parse_args() args = parser.parse_args()
doc = curdoc() doc = curdoc()
doc.title = "pyzebra" doc.title = "pyzebra"
# Final layout # Final layout
tab_data_viewer = panel_data_viewer.create(args.init_meta) tab_data_viewer = panel_data_viewer.create()
tab_anatric = panel_anatric.create() tab_anatric = panel_anatric.create()
doc.add_root(Tabs(tabs=[tab_data_viewer, tab_anatric])) doc.add_root(Tabs(tabs=[tab_data_viewer, tab_anatric]))

View File

@ -1,3 +1,6 @@
import base64
import io
import numpy as np import numpy as np
from bokeh.layouts import column, gridplot, row from bokeh.layouts import column, gridplot, row
from bokeh.models import ( from bokeh.models import (
@ -7,6 +10,8 @@ from bokeh.models import (
Button, Button,
ColumnDataSource, ColumnDataSource,
DataRange1d, DataRange1d,
Div,
FileInput,
Grid, Grid,
HoverTool, HoverTool,
Image, Image,
@ -23,7 +28,6 @@ from bokeh.models import (
Select, Select,
Spinner, Spinner,
TextAreaInput, TextAreaInput,
TextInput,
Title, Title,
Toggle, Toggle,
WheelZoomTool, WheelZoomTool,
@ -36,12 +40,24 @@ IMAGE_W = 256
IMAGE_H = 128 IMAGE_H = 128
def create(init_meta): def create():
curent_h5_data = np.array([]) curent_h5_data = np.array([])
current_index = None current_index = None
det_data = {} det_data = {}
roi_selection = {} roi_selection = {}
upload_div = Div(text="Open .cami file:")
def upload_button_callback(_attr, _old, new):
with io.StringIO(base64.b64decode(new).decode()) as file:
h5meta_list = pyzebra.parse_h5meta(file)
file_list = h5meta_list["filelist"]
filelist.options = file_list
filelist.value = file_list[0]
upload_button = FileInput(accept=".cami")
upload_button.on_change("value", upload_button_callback)
def update_image(): def update_image():
current_image = curent_h5_data[current_index] current_image = curent_h5_data[current_index]
proj_v_line_source.data.update( proj_v_line_source.data.update(
@ -79,12 +95,8 @@ def create(init_meta):
overview_x = np.mean(data, axis=1) overview_x = np.mean(data, axis=1)
overview_y = np.mean(data, axis=2) overview_y = np.mean(data, axis=2)
overview_plot_x_image_source.data.update( overview_plot_x_image_source.data.update(image=[overview_x], dw=[overview_x.shape[1]])
image=[overview_x], dw=[overview_x.shape[1]] overview_plot_y_image_source.data.update(image=[overview_y], dw=[overview_y.shape[1]])
)
overview_plot_y_image_source.data.update(
image=[overview_y], dw=[overview_y.shape[1]]
)
if frame_button_group.active == 0: # Frame if frame_button_group.active == 0: # Frame
overview_plot_x_image_source.data.update( overview_plot_x_image_source.data.update(
@ -105,15 +117,6 @@ def create(init_meta):
filelist = Select() filelist = Select()
filelist.on_change("value", filelist_callback) filelist.on_change("value", filelist_callback)
def fileinput_callback(_attr, _old, new):
h5meta_list = pyzebra.read_h5meta(new)
file_list = h5meta_list["filelist"]
filelist.options = file_list
filelist.value = file_list[0]
fileinput = TextInput()
fileinput.on_change("value", fileinput_callback)
def index_spinner_callback(_attr, _old, new): def index_spinner_callback(_attr, _old, new):
nonlocal current_index nonlocal current_index
if 0 <= new < curent_h5_data.shape[0]: if 0 <= new < curent_h5_data.shape[0]:
@ -488,14 +491,16 @@ def create(init_meta):
) )
tab_layout = row( tab_layout = row(
column(fileinput, filelist, layout_image, row(colormap_layout, animate_layout, hkl_layout)), column(
upload_div,
upload_button,
filelist,
layout_image,
row(colormap_layout, animate_layout, hkl_layout),
),
column(roi_avg_plot, layout_overview, row(selection_button, selection_list),), column(roi_avg_plot, layout_overview, row(selection_button, selection_list),),
) )
# initiate fileinput
if init_meta:
fileinput.value = init_meta
return Panel(child=tab_layout, title="Data Viewer") return Panel(child=tab_layout, title="Data Viewer")

View File

@ -2,7 +2,7 @@ import h5py
def read_h5meta(filepath): def read_h5meta(filepath):
"""Read and parse content of a h5meta file. """Open and parse content of a h5meta file.
Args: Args:
filepath (str): File path of a h5meta file. filepath (str): File path of a h5meta file.
@ -10,22 +10,28 @@ def read_h5meta(filepath):
Returns: Returns:
dict: A dictionary with section names and their content. dict: A dictionary with section names and their content.
""" """
h5meta_content = dict() with open(filepath) as file:
with open(filepath) as h5meta_file: content = parse_h5meta(file)
return content
def parse_h5meta(file):
content = dict()
section = None section = None
for line in h5meta_file: for line in file:
line = line.strip() line = line.strip()
if line.startswith("#begin "): if line.startswith("#begin "):
section = line[len("#begin "):] section = line[len("#begin "):]
h5meta_content[section] = [] content[section] = []
elif line.startswith("#end"): elif line.startswith("#end"):
section = None section = None
elif section: elif section:
h5meta_content[section].append(line) content[section].append(line)
return h5meta_content return content
def read_detector_data(filepath): def read_detector_data(filepath):