Skip to content

Commit 0b9b613

Browse files
committed
Added macro examples
1 parent af608cb commit 0b9b613

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ automation and embedding features.
3232

3333
✨ Add features to DataLab by writing your own [plugin](https://cdlapp.readthedocs.io/en/latest/features/general/plugins.html)
3434
(see [plugin examples](https://github.com/Codra-Ingenierie-Informatique/DataLab/tree/main/plugins/examples))
35+
or macro (see [macro examples](https://github.com/Codra-Ingenierie-Informatique/DataLab/tree/main/macros/examples))
3536

3637
✨ DataLab may be remotely controlled from a third-party application (such as Jupyter,
3738
Spyder or any IDE):

macros/examples/imageproc_macro.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Image Processing DataLab macro example
2+
3+
import numpy as np
4+
5+
# When using this code outside of DataLab (which is possible!), you may use the
6+
# *Simple DataLab Client* (see https://pypi.org/project/cdlclient/), installed with
7+
# `pip install cdlclient`), as it's more lightweight than the full DataLab package:
8+
#
9+
# from cdlclient import SimpleRemoteProxy as RemoteProxy
10+
from cdl.proxy import RemoteProxy
11+
12+
proxy = RemoteProxy()
13+
14+
proxy.set_current_panel("image")
15+
if len(proxy.get_object_uuids()) == 0:
16+
raise RuntimeError("No image to process!")
17+
18+
uuid = proxy.get_sel_object_uuids()[0]
19+
image = proxy.get_object(uuid)
20+
21+
# Filter image with a custom kernel
22+
data = np.array(image.data, copy=True)
23+
kernel = np.array([[0, 1, 0, 0], [1, -4, 1, 0], [0, 1, 0, 0], [0, 0, 0, 0]])
24+
pad = (data.shape[0] - kernel.shape[0]) // 2
25+
kernel = np.pad(kernel, pad, mode="constant")
26+
data = np.fft.ifft2(np.fft.fft2(data) * np.fft.fft2(kernel)).real
27+
data -= data.min()
28+
data /= data.max()
29+
data *= 65535
30+
data = data.astype(np.uint16)
31+
32+
# Add new image to the panel
33+
proxy.add_image("My custom filtered data", data)

macros/examples/simple_macro.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Simple DataLab macro example
2+
3+
import numpy as np
4+
5+
# When using this code outside of DataLab (which is possible!), you may use the
6+
# *Simple DataLab Client* (see https://pypi.org/project/cdlclient/), installed with
7+
# `pip install cdlclient`), as it's more lightweight than the full DataLab package:
8+
#
9+
# from cdlclient import SimpleRemoteProxy as RemoteProxy
10+
from cdl.proxy import RemoteProxy
11+
12+
proxy = RemoteProxy()
13+
14+
z = np.random.rand(20, 20)
15+
proxy.add_image("toto", z)
16+
proxy.compute_fft()
17+
18+
print("All done!")

0 commit comments

Comments
 (0)