flimanalyzer 0.6.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (110) hide show
  1. flim/__init__.py +2 -0
  2. flim/_version.py +5 -0
  3. flim/analysis/__init__.py +1 -0
  4. flim/analysis/ml/__init__.py +0 -0
  5. flim/analysis/ml/autoencoder.py +188 -0
  6. flim/analyzerapp.py +131 -0
  7. flim/batch.py +235 -0
  8. flim/configs/__init__.py +77 -0
  9. flim/configs/analysis.py +554 -0
  10. flim/configs/data.py +278 -0
  11. flim/configs/plot.py +543 -0
  12. flim/configs/workflow.py +204 -0
  13. flim/core/__init__.py +0 -0
  14. flim/core/calculator.py +0 -0
  15. flim/core/config.py +305 -0
  16. flim/core/execution.py +110 -0
  17. flim/core/fields.py +226 -0
  18. flim/core/filter.py +179 -0
  19. flim/core/graph.py +111 -0
  20. flim/core/headers.py +72 -0
  21. flim/core/importer.py +269 -0
  22. flim/core/parser.py +305 -0
  23. flim/core/paths.py +66 -0
  24. flim/core/prefect_server.py +152 -0
  25. flim/core/preprocessor.py +287 -0
  26. flim/core/registry.py +108 -0
  27. flim/core/runner.py +703 -0
  28. flim/core/schema.py +114 -0
  29. flim/core/settings.py +419 -0
  30. flim/core/tool.py +284 -0
  31. flim/core/tools.py +76 -0
  32. flim/core/types.py +113 -0
  33. flim/core/validation.py +272 -0
  34. flim/core/workflow.py +148 -0
  35. flim/gui/__init__.py +12 -0
  36. flim/gui/app.py +953 -0
  37. flim/gui/configdialog.py +495 -0
  38. flim/gui/datapanel.py +841 -0
  39. flim/gui/delimpanel.py +56 -0
  40. flim/gui/dialogs/__init__.py +473 -0
  41. flim/gui/dialogs/autoencoder.py +89 -0
  42. flim/gui/dialogs/categoryorder.py +130 -0
  43. flim/gui/dialogs/filter.py +86 -0
  44. flim/gui/dialogs/unpivot.py +72 -0
  45. flim/gui/dicttablepanel.py +178 -0
  46. flim/gui/editors.py +1578 -0
  47. flim/gui/events.py +162 -0
  48. flim/gui/icons.py +16 -0
  49. flim/gui/importdlg.py +391 -0
  50. flim/gui/listcontrol.py +472 -0
  51. flim/gui/macos.py +77 -0
  52. flim/gui/plotframe.py +73 -0
  53. flim/gui/progressdlg.py +290 -0
  54. flim/resources/aeaugment.png +0 -0
  55. flim/resources/aefeatureworkflow.png +0 -0
  56. flim/resources/aerun.png +0 -0
  57. flim/resources/aetrain.png +0 -0
  58. flim/resources/aetuneworkflow.png +0 -0
  59. flim/resources/barplot.png +0 -0
  60. flim/resources/boxplot.png +0 -0
  61. flim/resources/categorize.png +0 -0
  62. flim/resources/concatenate.png +0 -0
  63. flim/resources/fileopen.png +0 -0
  64. flim/resources/filter.png +0 -0
  65. flim/resources/flimanalyzer.ico +0 -0
  66. flim/resources/flimanalyzer.png +0 -0
  67. flim/resources/flirrplot.png +0 -0
  68. flim/resources/flirrworkflow.png +0 -0
  69. flim/resources/gmm.png +0 -0
  70. flim/resources/heatmap.png +0 -0
  71. flim/resources/histogram.png +0 -0
  72. flim/resources/import.png +0 -0
  73. flim/resources/kde.png +0 -0
  74. flim/resources/kmeans.png +0 -0
  75. flim/resources/ks.png +0 -0
  76. flim/resources/lineplot.png +0 -0
  77. flim/resources/merge.png +0 -0
  78. flim/resources/order.png +0 -0
  79. flim/resources/pairplot.png +0 -0
  80. flim/resources/pca.png +0 -0
  81. flim/resources/pivot.png +0 -0
  82. flim/resources/randomforest.png +0 -0
  83. flim/resources/relchange.png +0 -0
  84. flim/resources/scatter.png +0 -0
  85. flim/resources/seriesanalysis.png +0 -0
  86. flim/resources/sort.png +0 -0
  87. flim/resources/split.png +0 -0
  88. flim/resources/summary.png +0 -0
  89. flim/resources/swarmplot.png +0 -0
  90. flim/resources/unpivot.png +0 -0
  91. flim/resources/violinplot.png +0 -0
  92. flim/resources/workflow.png +0 -0
  93. flim/results.py +70 -0
  94. flim/tools/__init__.py +7 -0
  95. flim/tools/autoencoder.py +662 -0
  96. flim/tools/common.py +62 -0
  97. flim/tools/data.py +301 -0
  98. flim/tools/ml.py +311 -0
  99. flim/tools/plot.py +1439 -0
  100. flim/tools/statistics.py +348 -0
  101. flim/utils.py +43 -0
  102. flim/workflow/__init__.py +3 -0
  103. flim/workflow/aefeature.py +145 -0
  104. flim/workflow/aetune.py +163 -0
  105. flim/workflow/basicflow.py +99 -0
  106. flimanalyzer-0.6.0.dist-info/METADATA +262 -0
  107. flimanalyzer-0.6.0.dist-info/RECORD +110 -0
  108. flimanalyzer-0.6.0.dist-info/WHEEL +4 -0
  109. flimanalyzer-0.6.0.dist-info/entry_points.txt +5 -0
  110. flimanalyzer-0.6.0.dist-info/licenses/LICENSE +19 -0
@@ -0,0 +1,152 @@
1
+ """Connects flimanalyzer to a Prefect server, starting a local one if needed."""
2
+
3
+ import atexit
4
+ import contextlib
5
+ import logging
6
+ import os
7
+ import shutil
8
+ import subprocess
9
+ import sys
10
+ import time
11
+ import urllib.request
12
+ from pathlib import Path
13
+
14
+ DEFAULT_HOST = "127.0.0.1"
15
+ DEFAULT_PORT = 4200
16
+
17
+
18
+ def _is_healthy(api_url: str, timeout: float = 2.0) -> bool:
19
+ try:
20
+ with urllib.request.urlopen(f"{api_url.rstrip('/')}/health", timeout=timeout) as r:
21
+ return r.status == 200
22
+ except Exception:
23
+ return False
24
+
25
+
26
+ def _configured_server_error():
27
+ """Checks the server of the active Prefect settings with Prefect's client, which
28
+ also sends the API key (needed for Prefect Cloud).
29
+
30
+ Returns:
31
+ None if the server is healthy, otherwise the error.
32
+ """
33
+ from prefect.client.orchestration import get_client
34
+
35
+ try:
36
+ with get_client(sync_client=True) as client:
37
+ return client.api_healthcheck()
38
+ except Exception as err:
39
+ return err
40
+
41
+
42
+ def _prefect_cli() -> list:
43
+ """Command that runs the Prefect CLI of the current environment."""
44
+ exe = Path(sys.executable).parent / ("prefect.exe" if os.name == "nt" else "prefect")
45
+ if exe.exists():
46
+ return [str(exe)]
47
+ found = shutil.which("prefect")
48
+ if found:
49
+ return [found]
50
+ return [sys.executable, "-m", "prefect"]
51
+
52
+
53
+ def ensure_prefect_server(
54
+ host: str = DEFAULT_HOST,
55
+ port: int = DEFAULT_PORT,
56
+ start: bool = True,
57
+ timeout: float = 60.0,
58
+ log_file: str = "prefect-server.log",
59
+ ):
60
+ """Finds or starts the Prefect server that flows report to.
61
+
62
+ 1. If ``PREFECT_API_URL`` (environment or active Prefect profile) points to a
63
+ responding server, that server is used.
64
+ 2. Otherwise, if a server answers at ``http://<host>:<port>/api``, it is used.
65
+ 3. Otherwise, if ``start`` is True, ``prefect server start`` is launched in the
66
+ background and stopped again when the application exits.
67
+
68
+ If no server can be reached, Prefect falls back to a temporary server that only
69
+ lives as long as the application.
70
+
71
+ Args:
72
+ host, port: where to look for / start a local server.
73
+ start: whether to start a server if none is running.
74
+ timeout: seconds to wait for a started server to become healthy.
75
+ log_file: file that receives the started server's output.
76
+
77
+ Returns:
78
+ str or None: API URL in use, or None for Prefect's temporary server.
79
+ """
80
+ from prefect.settings import PREFECT_API_URL
81
+
82
+ configured = PREFECT_API_URL.value()
83
+ if configured:
84
+ error = _configured_server_error()
85
+ if error is None:
86
+ logging.info(f"Using Prefect server at {configured}")
87
+ return configured
88
+ logging.warning(
89
+ f"Configured Prefect server at {configured} is not available ({error}),"
90
+ " looking for a local server"
91
+ )
92
+
93
+ api_url = f"http://{host}:{port}/api"
94
+ if not _is_healthy(api_url):
95
+ if not start:
96
+ logging.info("No Prefect server found, using a temporary server")
97
+ return None
98
+ cmd = _prefect_cli() + ["server", "start", "--host", host, "--port", str(port)]
99
+ logging.info(f"Starting Prefect server: {' '.join(cmd)}")
100
+ log = open(log_file, "a")
101
+ try:
102
+ process = subprocess.Popen(cmd, stdout=log, stderr=subprocess.STDOUT)
103
+ except OSError as err:
104
+ log.close()
105
+ logging.error(f"Could not start Prefect server: {err}")
106
+ return None
107
+ atexit.register(_stop, process, log)
108
+ deadline = time.monotonic() + timeout
109
+ while not _is_healthy(api_url):
110
+ if process.poll() is not None or time.monotonic() > deadline:
111
+ logging.error(
112
+ f"Prefect server did not start, see {log_file}; using a"
113
+ " temporary server"
114
+ )
115
+ _stop(process, log)
116
+ return None
117
+ time.sleep(0.5)
118
+ logging.info(f"Using Prefect server at {api_url}")
119
+ # subprocesses (e.g. Dask workers) pick the server up from the environment
120
+ os.environ["PREFECT_API_URL"] = api_url
121
+ return api_url
122
+
123
+
124
+ def _stop(process, log):
125
+ if process.poll() is None:
126
+ logging.info("Stopping Prefect server")
127
+ process.terminate()
128
+ try:
129
+ process.wait(timeout=10)
130
+ except subprocess.TimeoutExpired:
131
+ process.kill()
132
+ log.close()
133
+
134
+
135
+ @contextlib.contextmanager
136
+ def use_prefect_server(**kwargs):
137
+ """Context manager that runs its body against the server found or started by
138
+ :func:`ensure_prefect_server` (kwargs are passed on).
139
+
140
+ Prefect reads its settings when it is imported, so the server URL is applied
141
+ through Prefect's settings context rather than the environment.
142
+ """
143
+ from prefect.settings import PREFECT_API_URL, temporary_settings
144
+
145
+ api_url = ensure_prefect_server(**kwargs)
146
+ if api_url:
147
+ settings = temporary_settings(updates={PREFECT_API_URL: api_url})
148
+ else:
149
+ settings = temporary_settings(restore_defaults={PREFECT_API_URL})
150
+ with settings:
151
+ yield api_url
152
+
@@ -0,0 +1,287 @@
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ """
4
+ Created on Fri May 4 05:16:56 2018
5
+
6
+ @author: khs3z
7
+ """
8
+
9
+ import logging
10
+ import numpy as np
11
+ import numbers
12
+ import flim.core
13
+
14
+ TRP_RZERO = 2.1
15
+ ONE_SIXTH = 1.0 / 6
16
+
17
+
18
+ def percentile(n):
19
+ def percentile_(x):
20
+ return np.nanpercentile(x, n)
21
+
22
+ percentile_.__name__ = f"{n} percentile"
23
+ return percentile_
24
+
25
+
26
+ def nadph_perc(nadph_t2):
27
+ return ((nadph_t2 - 1500) / (4400 - 1500)) * 100
28
+
29
+
30
+ def nadh_perc(nadph_perc):
31
+ return 100.0 - nadph_perc
32
+
33
+
34
+ def tm(a1perc, t1, a2perc, t2):
35
+ return ((a1perc * t1) + (a2perc * t2)) / 100
36
+
37
+
38
+ def trp_Eperc_1(trp_tm, const=3100):
39
+ if const != 0:
40
+ return (1.0 - (trp_tm / const)) * 100
41
+ else:
42
+ return np.nan
43
+
44
+
45
+ def trp_Eperc_2(trp_t1, trp_t2):
46
+ if trp_t2 != 0:
47
+ return (1.0 - (trp_t1 / trp_t2)) * 100
48
+ else:
49
+ return np.nan
50
+
51
+
52
+ def trp_Eperc_3(trp_t1, const=3100):
53
+ if const != 0:
54
+ return (1.0 - (trp_t1 / const)) * 100
55
+ else:
56
+ return np.nan
57
+
58
+
59
+ def trp_r(trp_Eperc):
60
+ # 0<= Eperc < 100
61
+ if trp_Eperc != 0:
62
+ t = 100.0 / trp_Eperc - 1
63
+ if t >= 0:
64
+ return TRP_RZERO * t**ONE_SIXTH
65
+ return np.nan
66
+
67
+
68
+ def ratio(v1, v2):
69
+ if v2 != 0:
70
+ # force float values
71
+ return float(v1) / v2
72
+ else:
73
+ return np.nan
74
+
75
+
76
+ class defaultpreprocessor:
77
+ def __init__(self):
78
+ self.newheaders = {
79
+ "Exc1_-Ch1-_": "trp ",
80
+ "Exc1_-Ch2-_": "NAD(P)H ",
81
+ "Exc2_-Ch3-_": "FAD ",
82
+ }
83
+ self.dropcolumns = ["Exc1_", "Exc2_", "Exc3_"]
84
+ self.calccolumns = [
85
+ "NAD(P)H tm",
86
+ "NAD(P)H a2[%]/a1[%]",
87
+ "NADPH %",
88
+ "NADPH/NADH",
89
+ "NADH %",
90
+ "trp tm",
91
+ "trp E%1",
92
+ "trp E%2",
93
+ "trp E%3",
94
+ "trp r1",
95
+ "trp r2",
96
+ "trp r3",
97
+ "trp a1[%]/a2[%]",
98
+ "FAD tm",
99
+ "FAD a1[%]/a2[%]",
100
+ "FAD photons/NAD(P)H photons",
101
+ "NAD(P)H tm/FAD tm",
102
+ "FLIRR",
103
+ "NADPH a2/FAD a1",
104
+ ]
105
+ # self.additional_columns = []
106
+ self.functions = {
107
+ "NADPH %": [nadph_perc.__name__, ["NAD(P)H t2"]],
108
+ "NAD(P)H tm": [
109
+ tm.__name__,
110
+ ["NAD(P)H a1[%]", "NAD(P)H t1", "NAD(P)H a2[%]", "NAD(P)H t2"],
111
+ ],
112
+ "NAD(P)H a2[%]/a1[%]": [ratio.__name__, ["NAD(P)H a2[%]", "NAD(P)H a1[%]"]],
113
+ "NADH %": [nadh_perc.__name__, ["NADPH %"]],
114
+ "NADPH/NADH": [ratio.__name__, ["NADPH %", "NADH %"]],
115
+ "trp tm": [tm.__name__, ["trp a1[%]", "trp t1", "trp a2[%]", "trp t2"]],
116
+ "trp E%1": [trp_Eperc_1.__name__, ["trp tm"]],
117
+ "trp E%2": [trp_Eperc_2.__name__, ["trp t1", "trp t2"]],
118
+ "trp E%3": [trp_Eperc_3.__name__, ["trp t1"]],
119
+ "trp r1": [trp_r.__name__, ["trp E%1"]],
120
+ "trp r2": [trp_r.__name__, ["trp E%2"]],
121
+ "trp r3": [trp_r.__name__, ["trp E%3"]],
122
+ "trp a1[%]/a2[%]": [ratio.__name__, ["trp a1[%]", "trp a2[%]"]],
123
+ "FAD tm": [tm.__name__, ["FAD a1[%]", "FAD t1", "FAD a2[%]", "FAD t2"]],
124
+ "FAD a1[%]/a2[%]": [ratio.__name__, ["FAD a1[%]", "FAD a2[%]"]],
125
+ "FAD photons/NAD(P)H photons": [
126
+ ratio.__name__,
127
+ ["FAD photons", "NAD(P)H photons"],
128
+ ],
129
+ "NAD(P)H tm/FAD tm": [ratio.__name__, ["NAD(P)H tm", "FAD tm"]],
130
+ "FLIRR": [ratio.__name__, ["NAD(P)H a2[%]", "FAD a1[%]"]],
131
+ "NADPH a2/FAD a1": [ratio.__name__, ["NAD(P)H a2", "FAD a1"]],
132
+ }
133
+
134
+ def settings(self):
135
+ """The preprocess settings (PreprocessSettings) of this preprocessor."""
136
+ from flim.core.settings import PreprocessSettings
137
+
138
+ return PreprocessSettings(
139
+ calculate=dict(self.functions),
140
+ drop=list(self.dropcolumns or []),
141
+ rename=dict(self.newheaders or {}),
142
+ )
143
+
144
+ def columns_available(self, data, args):
145
+ for arg in args:
146
+ if not isinstance(arg, numbers.Number) and data.get(arg) is None:
147
+ return False
148
+ return True
149
+
150
+ def set_replacementheaders(self, headers):
151
+ self.newheaders = headers
152
+
153
+ def get_replacementheaders(self):
154
+ return self.newheaders
155
+
156
+ def set_dropcolumns(self, cols):
157
+ self.dropcolumns = cols
158
+
159
+ def rename_headers(self, data, newheaders=None, preview=False):
160
+ if data is None:
161
+ return None
162
+ if newheaders is None:
163
+ newheaders = self.newheaders
164
+ oldheaders = list(data.columns.values)
165
+ currentheaders = [c for c in oldheaders]
166
+ for newheader in newheaders:
167
+ currentheaders = [
168
+ c.replace(newheader, newheaders[newheader]) for c in currentheaders
169
+ ]
170
+ # for newheader in newheaders:
171
+ # currentheaders = currentheaders.replace(newheader, newheaders[newheader])
172
+ # currentheaders = currentheaders.split('\t')
173
+ changedheaders = {
174
+ oldheaders[i]: currentheaders[i]
175
+ for i in range(len(oldheaders))
176
+ if oldheaders[i] != currentheaders[i]
177
+ }
178
+ if not preview:
179
+ data.columns = currentheaders
180
+ return data, changedheaders
181
+
182
+ def calculate(self, data, inplace=True):
183
+ calculated = []
184
+ skipped = []
185
+ if not inplace:
186
+ data = data.copy()
187
+ for acol in self.calccolumns:
188
+ # if acol == 'NADH tm':
189
+ # (NADH-a1% * NADH-t1) + (NADH-a2% * NADH-t2)/100
190
+ if acol in self.functions:
191
+ # NAD(P)H % = (('NAD(P)H t2') - 1500 / (4400-1500)) *100
192
+ funcname = self.functions[acol][0]
193
+ funcargs = self.functions[acol][1]
194
+ funcobj = getattr(
195
+ flim.core.preprocessor, funcname
196
+ ) # self.functions[acol][0]
197
+ func = np.vectorize(funcobj)
198
+ if not self.columns_available(data, funcargs):
199
+ skipped.append(self.functions[acol])
200
+ continue
201
+ data[acol] = func(*np.transpose(data[funcargs].values))
202
+ calculated.append(self.functions[acol])
203
+ else:
204
+ skipped.append(self.functions[acol])
205
+ return data, calculated, skipped
206
+
207
+ def reorder_columns(self, data, first=[]):
208
+ if first is None or len(first) == 0:
209
+ first = sorted(data.select_dtypes(["object"]).columns.values)
210
+ first.extend(sorted(data.select_dtypes(["category"]).columns.values))
211
+ newheaders = first
212
+ newheaders.extend(sorted(set(data.columns.values) - set(first)))
213
+ if len(newheaders) == len(data.columns):
214
+ data = data.reindex(newheaders, axis=1)
215
+ logging.debug(f"Reordered headers: {data.columns.values}")
216
+ return data
217
+
218
+ def drop_columns(
219
+ self,
220
+ data,
221
+ drops=None,
222
+ func="contains",
223
+ dropemptyheader=True,
224
+ inplace=True,
225
+ preview=False,
226
+ ):
227
+ if data is None:
228
+ return None
229
+ if drops is None:
230
+ drops = self.dropcolumns
231
+ # need to create list of str in order for any function to work
232
+ # drops = [d.decode() for d in drops if len(d) > 0]
233
+ currentheaders = list(data.columns.values)
234
+ droplist = []
235
+ for header in currentheaders:
236
+ found = False
237
+ if func == "startswith":
238
+ found = any(header.startswith(d) for d in drops)
239
+ elif func == "endswith":
240
+ found = any(header.endswith(d) for d in drops)
241
+ elif func == "contains":
242
+ found = any([d in header for d in drops])
243
+ elif func == "is":
244
+ found = any(header == d for d in drops)
245
+ found = found or (dropemptyheader and header == " ")
246
+ if found:
247
+ droplist.append(header)
248
+ if not preview:
249
+ if inplace:
250
+ data.drop(droplist, axis=1, inplace=inplace)
251
+ else:
252
+ data = data.drop(droplist, axis=1, inplace=inplace)
253
+ return data, droplist
254
+
255
+
256
+ """
257
+ def drop_columns(self, data, drops=None, func='startswith', inplace=True, preview=False):
258
+ if data is None:
259
+ return None
260
+ if drops is None:
261
+ drops = self.dropcolumns
262
+ currentheaders = list(data.columns.values)
263
+ droplist = []
264
+ for header in currentheaders:
265
+ found = False
266
+ for d in drops:
267
+ print d, d.strip('*')
268
+ if d.startswith('*'):
269
+ if d.endswith('*'):
270
+ func = 'contains'
271
+ found = header.contains(d.strip('*'))
272
+ else:
273
+ func = 'endswith'
274
+ found = header.endswith(d.strip('*'))
275
+ elif d.endswith('*'):
276
+ found = header.startswith(d.strip('*'))
277
+ else:
278
+ found = header == d
279
+ if found:
280
+ droplist.append(header)
281
+ if not preview:
282
+ if inplace:
283
+ data.drop(droplist, axis=1, inplace=inplace)
284
+ else:
285
+ data = data.drop(droplist, axis=1, inplace=inplace)
286
+ return data, droplist
287
+ """
flim/core/registry.py ADDED
@@ -0,0 +1,108 @@
1
+ """Registry of tools: by name, by category (menu order) and by config class.
2
+
3
+ Tools of other packages are found through the ``flimanalyzer.tools`` entry point group;
4
+ importing the module an entry point names registers its tools.
5
+ """
6
+
7
+ import logging
8
+ from importlib.metadata import entry_points
9
+ from typing import Optional
10
+
11
+ API_VERSION = 1 # tools declaring another api_version are not registered
12
+
13
+ # menu and tool panel order; categories not listed here follow, sorted by name
14
+ CATEGORY_ORDER = ("Image", "Data", "Analysis", "Plot", "Workflow")
15
+
16
+ TOOLS_GROUP = "flimanalyzer.tools"
17
+ GUI_GROUP = "flimanalyzer.gui"
18
+
19
+
20
+ class ToolRegistry:
21
+ def __init__(self):
22
+ self._tools = {} # name -> class
23
+
24
+ def register(self, cls):
25
+ """Registers a tool class; it needs ``name``, ``category`` and ``config_class``."""
26
+ for attr in ("name", "category", "config_class"):
27
+ if not getattr(cls, attr, None):
28
+ raise ValueError(f"{cls.__qualname__} has no {attr}")
29
+ api = getattr(cls, "api_version", API_VERSION)
30
+ if api != API_VERSION:
31
+ raise ValueError(
32
+ f"{cls.__qualname__} needs FLIMAnalyzer tool API {api}, this is {API_VERSION}"
33
+ )
34
+ existing = self._tools.get(cls.name)
35
+ if existing is not None and existing is not cls:
36
+ raise ValueError(
37
+ f"Tool name conflict: '{cls.name}' is used by {existing.__qualname__}"
38
+ f" and {cls.__qualname__}"
39
+ )
40
+ self._tools[cls.name] = cls
41
+ return cls
42
+
43
+ def unregister(self, name: str):
44
+ self._tools.pop(name, None)
45
+
46
+ def get(self, name: str):
47
+ return self._tools[name]
48
+
49
+ def __contains__(self, name: str) -> bool:
50
+ return name in self._tools
51
+
52
+ def __len__(self) -> int:
53
+ return len(self._tools)
54
+
55
+ def categories(self) -> list[str]:
56
+ """Categories of the registered tools, in menu order."""
57
+ present = {cls.category for cls in self._tools.values()}
58
+ known = [c for c in CATEGORY_ORDER if c in present]
59
+ return known + sorted(present - set(CATEGORY_ORDER))
60
+
61
+ def by_category(self) -> dict[str, list]:
62
+ """Tools grouped by category (menu order), each group sorted by name."""
63
+ return {
64
+ category: sorted(
65
+ (cls for cls in self._tools.values() if cls.category == category),
66
+ key=lambda cls: cls.name,
67
+ )
68
+ for category in self.categories()
69
+ }
70
+
71
+ def for_config(self, config) -> Optional[type]:
72
+ """The tool for a config class or instance; a config subclass without its own
73
+ tool falls back to the tool of its nearest base class."""
74
+ config_class = config if isinstance(config, type) else type(config)
75
+ by_config = {cls.config_class: cls for cls in self._tools.values()}
76
+ for base in config_class.__mro__:
77
+ if base in by_config:
78
+ return by_config[base]
79
+ return None
80
+
81
+
82
+ tools = ToolRegistry()
83
+
84
+
85
+ def register_tool(cls):
86
+ """Class decorator registering a tool in the default registry."""
87
+ return tools.register(cls)
88
+
89
+
90
+ def discover(group: str = TOOLS_GROUP) -> list[str]:
91
+ """Imports the modules of the entry points in ``group``.
92
+
93
+ A module that fails to import (e.g. an incompatible tool) is logged and skipped,
94
+ so one broken add-on never stops the application.
95
+
96
+ Returns:
97
+ Names of the entry points that loaded.
98
+ """
99
+ loaded = []
100
+ for ep in entry_points(group=group):
101
+ try:
102
+ ep.load()
103
+ loaded.append(ep.name)
104
+ except Exception:
105
+ logging.exception(
106
+ f"Could not load {group} entry point '{ep.name}' ({ep.value})"
107
+ )
108
+ return loaded