Skip to content

Commit f6fece0

Browse files
committed
improve instrument behaviour
env_functions.py - add function to find instrument from directory config.py - get_config now replaces user values to beamline defaults when changing beamline cli.py - added check for beamline in given directory title_window.py - added time logging
1 parent bfd1149 commit f6fece0

6 files changed

Lines changed: 42 additions & 5 deletions

File tree

mmg_toolbox/tkguis/cli.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import sys
66
import os
77

8-
from mmg_toolbox.utils.env_functions import get_beamline
8+
from mmg_toolbox.utils.env_functions import get_beamline, get_beamline_from_directory
99
from .misc.config import BEAMLINE_CONFIG, get_config
1010
from .apps.experiment import create_title_window
1111
from .apps.data_viewer import create_data_viewer
@@ -26,13 +26,16 @@ def run(*args):
2626
return
2727

2828
beamline = next((bm for bm in BEAMLINE_CONFIG if bm in args), get_beamline())
29-
config = get_config(beamline=beamline)
3029

3130
for n, arg in enumerate(args):
3231
if os.path.isdir(arg):
32+
beamline = get_beamline_from_directory(os.path.abspath(arg), beamline)
33+
config = get_config(beamline=beamline)
3334
create_data_viewer(arg, config=config)
3435
return
3536
elif os.path.isfile(arg):
37+
beamline = get_beamline_from_directory(os.path.abspath(arg), beamline)
38+
config = get_config(beamline=beamline)
3639
create_nexus_viewer(arg, config=config)
3740
return
3841
create_title_window(beamline)

mmg_toolbox/tkguis/misc/config.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,15 @@ def check_config_filename(config_filename: str | None) -> str:
155155

156156

157157
def load_config(config_filename: str = CONFIG_FILE) -> dict:
158+
"""Loads a config dict from file, by default from the default location"""
158159
if os.path.isfile(config_filename):
159160
with open(config_filename, 'r') as f:
160161
return json.load(f)
161162
return {}
162163

163164

164165
def default_config(beamline: str | None = None) -> dict:
166+
"""Returns the default beamline config dict"""
165167
config = CONFIG.copy()
166168
if beamline is None:
167169
beamline = get_beamline()
@@ -171,9 +173,14 @@ def default_config(beamline: str | None = None) -> dict:
171173

172174

173175
def get_config(config_filename: str | None = None, beamline: str | None = None) -> dict:
176+
"""merge loaded config into default beamline config and return the config dict"""
174177
config_filename = check_config_filename(config_filename)
175178
user_config = load_config(config_filename)
176179
config = default_config(beamline)
180+
if beamline and user_config.get(C.beamline) and beamline != user_config.get(C.beamline):
181+
# default config overrides user config when changing beamline
182+
user_config.update(config)
183+
return user_config
177184
config.update(user_config)
178185
return config
179186

@@ -186,13 +193,15 @@ def reset_config(config: dict) -> None:
186193

187194

188195
def save_config(config: dict):
196+
"""Save the config dict into the file location referenced in config['config_file']"""
189197
config_filename = config.get(C.conf_file, CONFIG_FILE)
190198
with open(config_filename, 'w') as f:
191199
json.dump(config, f)
192200
print('Saved config to {}'.format(config_filename))
193201

194202

195203
def save_config_as(config_filename: str | None = None, **kwargs):
204+
"""Save the config dict into a new file location"""
196205
config = get_config(config_filename)
197206
config.update(kwargs)
198207
config[C.conf_file] = config_filename

mmg_toolbox/tkguis/misc/styles.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class RootWithStyle(tk.Tk):
7070

7171
def create_root(window_title: str, parent: tk.Misc | RootWithStyle | None = None) -> RootWithStyle:
7272
"""Create tkinter root object with style attribute"""
73+
# TODO: this is the slow part of the startup, investigate further.
7374
if parent:
7475
root = tk.Toplevel(parent)
7576
# root.geometry(f"+{parent.winfo_x()+100}+{parent.winfo_y()+100}")

mmg_toolbox/tkguis/widgets/title_window.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import os
55
import tkinter as tk
66
from tkinter import ttk
7+
from time import time
8+
from threading import Thread
79

810
from mmg_toolbox.utils.env_functions import get_dls_visits, MMG_BEAMLINES
911
from mmg_toolbox.utils.file_functions import folder_summary_line
@@ -16,6 +18,7 @@
1618

1719
class TitleWindow:
1820
def __init__(self, root: tk.Misc, config: dict | None = None):
21+
t0 = time()
1922
self.root = root
2023
self.config = config or get_config()
2124

@@ -66,11 +69,15 @@ def __init__(self, root: tk.Misc, config: dict | None = None):
6669
ttk.Button(frm, text='Log Viewer', command=self.open_log_viewer, width=20).pack(side=tk.LEFT)
6770
ttk.Button(frm, text='Notebook Browser', command=self.open_notebook_browser, width=20).pack(side=tk.LEFT)
6871
ttk.Button(frm, text='Processing', command=self.open_script_runner, width=20).pack(side=tk.LEFT)
69-
70-
self.choose_beamline(self.config.get(C.beamline, 'i16'))
72+
t1 = time()
73+
logger.info(f"init time: {t1-t0}s")
74+
# run file-system functions in a thread to speed up the start time
75+
th = Thread(target=lambda: self.choose_beamline(self.config.get(C.beamline, 'i16')))
76+
th.start()
7177

7278
def choose_beamline(self, beamline: str):
7379
from ..misc.config import BEAMLINE_CONFIG
80+
t0 = time()
7481
bl_config = BEAMLINE_CONFIG[beamline].copy()
7582
self.config.update(bl_config)
7683
self.beamline.set('MMG Toolbox: ' + beamline)
@@ -79,6 +86,8 @@ def choose_beamline(self, beamline: str):
7986
current_visit = next(iter(self.visits.keys()))
8087
self.visit_menu.set_menu(current_visit, *self.visits)
8188
self.visit.set(current_visit)
89+
t1 = time()
90+
logger.info(f"choose_beamline time: {t1-t0}s")
8291
self.dls_directories(self.visits[current_visit])
8392

8493
def menu_items(self):
@@ -95,6 +104,7 @@ def menu_items(self):
95104
return menu
96105

97106
def dls_directories(self, data_dir: str):
107+
t0 = time()
98108
if not os.access(data_dir, os.R_OK):
99109
show_error(f"Warning path is not readable: '{data_dir}'", self.root, raise_exception=False)
100110
self.summary.set(folder_summary_line(data_dir))
@@ -105,6 +115,8 @@ def dls_directories(self, data_dir: str):
105115
self.proc_dir.set(proc_dir)
106116
if os.path.isdir(notebook_dir) and os.access(notebook_dir, os.R_OK):
107117
self.notebook_dir.set(notebook_dir)
118+
t1 = time()
119+
logger.info(f"dls_directories time: {t1-t0}s")
108120

109121
def update_config(self):
110122
save_config(self.config)

mmg_toolbox/utils/env_functions.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
Environment functions
33
"""
44

5+
import sys
56
import os
7+
import re
68
import subprocess
7-
import sys
89
import tempfile
910
from datetime import datetime
1011

@@ -57,6 +58,14 @@ def get_beamline(default=''):
5758
return os.environ.get(BEAMLINE, default)
5859

5960

61+
def get_beamline_from_directory(directory: str, default: str = ''):
62+
"""Return current beamline from given directory"""
63+
beamlines = re.findall('/([a-zA-Z][0-9]{2}-?[1-9]?)', directory)
64+
if beamlines:
65+
return beamlines[0]
66+
return default
67+
68+
6069
def get_user(default=''):
6170
"""Return current user from environment variable"""
6271
return next((os.environ[u] for u in USER if u in os.environ), default)

start_dataviewer.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66

77
# start_gui('/dls/science/groups/das/ExampleData/i16/azimuths/1108750.nxs')
88
# start_gui('/dls/science/groups/das/ExampleData/i16/azimuths')
9+
# start_gui('/dls/b07/data/2025/cm40617-5')
910
start_gui()
1011

1112
# from mmg_toolbox.tkguis.apps.nexus import create_nexus_plot_and_image
1213
# create_nexus_plot_and_image('/dls/science/groups/das/ExampleData/i16/azimuths/1108750.nxs')
1314
# create_nexus_plot_and_image('/dls/i16/data/2025/nt43883-1/1113041.nxs')
15+
16+

0 commit comments

Comments
 (0)