Skip to content

Commit 223c227

Browse files
committed
Refactor computation method calls to use calc instead of compute_* for consistency across the codebase
1 parent 118d0c5 commit 223c227

12 files changed

Lines changed: 80 additions & 81 deletions

File tree

datalab/baseproxy.py

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
from __future__ import annotations
2626

2727
import abc
28-
from collections.abc import Callable
2928
from contextlib import contextmanager
3029
from typing import TYPE_CHECKING, Generator
3130

@@ -148,9 +147,9 @@ def context_no_refresh(self) -> Generator[None, None, None]:
148147
149148
>>> with proxy.context_no_refresh():
150149
... proxy.add_image("image1", data1)
151-
... proxy.compute_fft()
152-
... proxy.compute_wiener()
153-
... proxy.compute_ifft()
150+
... proxy.calc("fft")
151+
... proxy.calc("wiener")
152+
... proxy.calc("ifft")
154153
... # Auto refresh is disabled during the above operations
155154
"""
156155
self.toggle_auto_refresh(False)
@@ -495,44 +494,23 @@ def import_macro_from_file(self, filename: str) -> None:
495494

496495
@abc.abstractmethod
497496
def calc(self, name: str, param: gds.DataSet | None = None) -> gds.DataSet:
498-
"""Call compute function ``name`` in current panel's processor.
497+
"""Call computation feature ``name``
499498
500-
Args:
501-
name: Compute function name
502-
param: Compute function parameter. Defaults to None.
503-
504-
Raises:
505-
ValueError: unknown function
506-
"""
499+
.. note::
507500
508-
def __getattr__(self, name: str) -> Callable:
509-
"""Return compute function ``name`` in current panel's processor.
501+
This calls either the processor's ``compute_<name>`` method (if it exists),
502+
or the processor's ``<name>`` computation feature (if it is registered,
503+
using the ``run_feature`` method).
504+
It looks for the function in all panels, starting with the current one.
510505
511506
Args:
512507
name: Compute function name
513-
514-
Returns:
515-
Callable: Compute function
508+
param: Compute function parameter. Defaults to None.
516509
517510
Raises:
518-
AttributeError: If compute function ``name`` does not exist
511+
ValueError: unknown function
519512
"""
520513

521-
def compute_func(param: gds.DataSet | None = None) -> gds.DataSet:
522-
"""Compute function.
523-
524-
Args:
525-
param: Compute function parameter. Defaults to None.
526-
527-
Returns:
528-
Compute function result
529-
"""
530-
return self.calc(name, param)
531-
532-
if name.startswith("compute_"):
533-
return compute_func
534-
raise AttributeError(f"DataLab has no compute function '{name}'")
535-
536514

537515
class BaseProxy(AbstractDLControl, metaclass=abc.ABCMeta):
538516
"""Common base class for DataLab proxies

datalab/gui/macroeditor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class Macro(QC.QObject, ObjItf, metaclass=MacroMeta):
7979
8080
z = np.random.rand(20, 20)
8181
proxy.add_image("toto", z)
82-
proxy.compute_fft()
82+
proxy.calc("fft")
8383
8484
print("All done!")
8585
"""

datalab/gui/main.py

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,7 +1181,14 @@ def set_current_panel(self, panel: str) -> None:
11811181

11821182
@remote_controlled
11831183
def calc(self, name: str, param: gds.DataSet | None = None) -> None:
1184-
"""Call compute function ``name`` in current panel's processor.
1184+
"""Call computation feature ``name``
1185+
1186+
.. note::
1187+
1188+
This calls either the processor's ``compute_<name>`` method (if it exists),
1189+
or the processor's ``<name>`` computation feature (if it is registered,
1190+
using the ``run_feature`` method).
1191+
It looks for the function in all panels, starting with the current one.
11851192
11861193
Args:
11871194
name: Compute function name
@@ -1190,26 +1197,30 @@ def calc(self, name: str, param: gds.DataSet | None = None) -> None:
11901197
Raises:
11911198
ValueError: unknown function
11921199
"""
1193-
name = name.removeprefix("compute_")
11941200
panels = [self.tabwidget.currentWidget()]
11951201
panels.extend(self.panels)
11961202
for panel in panels:
11971203
if isinstance(panel, base.BaseDataPanel):
11981204
panel: base.BaseDataPanel
1199-
try:
1200-
feature = panel.processor.get_feature(name)
1201-
panel.processor.run_feature(feature, param)
1205+
# Some computation features are wrapped in a method with a
1206+
# "compute_" prefix, so we check for this first:
1207+
func = getattr(panel.processor, f"compute_{name}", None)
1208+
if func is not None:
1209+
if param is None:
1210+
func()
1211+
else:
1212+
func(param)
12021213
return
1203-
except ValueError:
1204-
for funcname in (name, f"compute_{name}"):
1205-
func = getattr(panel.processor, funcname, None)
1206-
if func is not None:
1207-
if param is None:
1208-
func()
1209-
else:
1210-
func(param)
1211-
return
1212-
raise ValueError(f"Unknown function {name}")
1214+
else:
1215+
# If the function is not wrapped, we check if it is a
1216+
# registered feature:
1217+
try:
1218+
feature = panel.processor.get_feature(name)
1219+
panel.processor.run_feature(feature, param)
1220+
return
1221+
except ValueError:
1222+
continue
1223+
raise ValueError(f"Unknown computation function {name}")
12131224

12141225
# ------GUI refresh
12151226
def has_objects(self) -> bool:

datalab/proxy.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
The :mod:`datalab.proxy` module provides a way to access DataLab features from a proxy
88
class.
99
10-
The list of compute methods accessible from the proxy objects is available in the
11-
:ref:`processor_methods` section.
12-
1310
Remote proxy
1411
^^^^^^^^^^^^
1512
@@ -62,25 +59,17 @@
6259
- :class:`datalab.gui.processor.signal.SignalProcessor`
6360
- :class:`datalab.gui.processor.image.ImageProcessor`
6461
65-
.. seealso::
66-
67-
The list of processor methods is available in tables below.
68-
69-
There are two ways to call a processor method:
70-
71-
1. Using the :meth:`calc` method of the proxy object:
62+
To run a computation feature associated to a processor, you can use the
63+
:meth:`calc` method of the proxy object:
7264
7365
.. code-block:: python
7466
7567
# Call a method without parameter
76-
proxy.calc("compute_average")
77-
78-
# This is equivalent to:
7968
proxy.calc("average")
8069
8170
# Call a method with parameters
8271
p = sigima.params.MovingAverageParam.create(n=30)
83-
proxy.calc("compute_moving_average", p)
72+
proxy.calc("moving_average", p)
8473
8574
2. Directly calling the processor method from the proxy object:
8675
@@ -305,7 +294,14 @@ def add_object(
305294
self._datalab.add_object(obj, group_id, set_current)
306295

307296
def calc(self, name: str, param: gds.DataSet | None = None) -> None:
308-
"""Call compute function ``name`` in current panel's processor.
297+
"""Call computation feature ``name``
298+
299+
.. note::
300+
301+
This calls either the processor's ``compute_<name>`` method (if it exists),
302+
or the processor's ``<name>`` computation feature (if it is registered,
303+
using the ``run_feature`` method).
304+
It looks for the function in all panels, starting with the current one.
309305
310306
Args:
311307
name: Compute function name

datalab/remote.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,14 @@ def delete_metadata(
528528

529529
@remote_call
530530
def calc(self, name: str, param_data: list[str] | None = None) -> bool:
531-
"""Call compute function ``name`` in current panel's processor.
531+
"""Call computation feature ``name``
532+
533+
.. note::
534+
535+
This calls either the processor's ``compute_<name>`` method (if it exists),
536+
or the processor's ``<name>`` computation feature (if it is registered,
537+
using the ``run_feature`` method).
538+
It looks for the function in all panels, starting with the current one.
532539
533540
Args:
534541
name: Compute function name
@@ -975,7 +982,14 @@ def add_object(
975982
self._datalab.add_object(obj_data, group_id, set_current)
976983

977984
def calc(self, name: str, param: gds.DataSet | None = None) -> None:
978-
"""Call compute function ``name`` in current panel's processor.
985+
"""Call computation feature ``name``
986+
987+
.. note::
988+
989+
This calls either the processor's ``compute_<name>`` method (if it exists),
990+
or the processor's ``<name>`` computation feature (if it is registered,
991+
using the ``run_feature`` method).
992+
It looks for the function in all panels, starting with the current one.
979993
980994
Args:
981995
name: Compute function name

datalab/tests/backbone/main_app_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ def test_main_app():
9999

100100
# Use "calc" method with parameters
101101
param = sigima.params.MovingMedianParam.create(n=5)
102-
win.calc("compute_moving_median", param)
102+
win.calc("moving_median", param)
103103
# Use "calc" method without parameters
104-
win.calc("compute_integral")
104+
win.calc("integral")
105105
# Use "calc" and choose an unknown computation method
106106
try:
107107
win.calc("unknown_method")

datalab/tests/features/control/remoteclient_unit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def multiple_commands(remote: RemoteProxy):
5353
remote.calc("log10")
5454

5555
param = XYCalibrateParam.create(a=1.2, b=0.1)
56-
remote.calc("compute_calibration", param)
56+
remote.calc("calibration", param)
5757

5858
time.sleep(2) # Avoid permission error when trying to clean-up temporary files
5959

datalab/tests/features/macro/macroeditor_unit_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def get_macro_example_path() -> str:
4242
4343
z = np.random.rand(20, 20)
4444
proxy.add_image("toto", z)
45-
proxy.compute_fft()
45+
proxy.calc("fft")
4646
4747
print("All done! :)")
4848
"""

doc/intro/tutorials/fabry_perot.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,8 @@ To extract the intensity profile along the X axis, we have two options:
223223

224224
Let's try the first option, by selecting the "Line profile..." entry |profile|:
225225
that is the most straightforward way to extract a profile from an image, and it
226-
corresponds to the ``compute_profile`` method of DataLab's API (so it can be used
227-
in a script, a plugin or a macro).
226+
corresponds to the ``calc("line_profile")`` computation feature of DataLab's API
227+
(so it can be used in a script, a plugin or a macro).
228228

229229
.. figure:: ../../images/tutorials/fabry_perot/24.png
230230

doc/locale/fr/LC_MESSAGES/intro/tutorials/fabry_perot.po

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ msgid ""
88
msgstr ""
99
"Project-Id-Version: DataLab \n"
1010
"Report-Msgid-Bugs-To: \n"
11-
"POT-Creation-Date: 2025-03-20 16:09+0100\n"
11+
"POT-Creation-Date: 2025-07-08 15:38+0200\n"
1212
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1313
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1414
"Language: fr\n"
@@ -190,8 +190,8 @@ msgstr "Soit activer l'outil \"Profil rectiligne\" |cross_section| dans la barre
190190
msgid "cross_section"
191191
msgstr ""
192192

193-
msgid "Let's try the first option, by selecting the \"Line profile...\" entry |profile|: that is the most straightforward way to extract a profile from an image, and it corresponds to the ``compute_profile`` method of DataLab's API (so it can be used in a script, a plugin or a macro)."
194-
msgstr "Essayons la première option, en sélectionnant l'entrée \"Extraire le profil...\" |profile| : c'est la façon la plus simple d'extraire un profil d'une image, et cela correspond à la méthode ``compute_profile`` de l'API de DataLab (donc elle peut être utilisée dans un script, un plugin ou une macro)."
193+
msgid "Let's try the first option, by selecting the \"Line profile...\" entry |profile|: that is the most straightforward way to extract a profile from an image, and it corresponds to the ``calc(\"line_profile\")`` computation feature of DataLab's API (so it can be used in a script, a plugin or a macro)."
194+
msgstr "Essayons la première option, en sélectionnant l'entrée \"Profil rectiligne...\" |profile| : c'est la façon la plus simple d'extraire un profil d'une image, et cela correspond à la méthode ``calc(\"line_profile\")`` de l'API de DataLab (donc elle peut être utilisée dans un script, un plugin ou une macro)."
195195

196196
msgid "Select the \"Line profile...\" entry |profile| in the \"Operations\" menu."
197197
msgstr "Sélectionnez l'entrée \"Profil rectiligne...\" |profile| dans le menu \"Opérations\"."

0 commit comments

Comments
 (0)