Skip to content

Commit 4ef49fc

Browse files
committed
Add keyword aliases
1 parent 62dceae commit 4ef49fc

File tree

9 files changed

+235
-185
lines changed

9 files changed

+235
-185
lines changed

examples/cells.ipynb

Lines changed: 54 additions & 54 deletions
Large diffs are not rendered by default.

examples/cthead.ipynb

Lines changed: 33 additions & 33 deletions
Large diffs are not rendered by default.

examples/introduction.ipynb

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
"id": "3bf7058d",
1515
"metadata": {
1616
"execution": {
17-
"iopub.execute_input": "2025-05-10T20:19:40.973319Z",
18-
"iopub.status.busy": "2025-05-10T20:19:40.973120Z",
19-
"iopub.status.idle": "2025-05-10T20:19:41.539047Z",
20-
"shell.execute_reply": "2025-05-10T20:19:41.538602Z"
17+
"iopub.execute_input": "2025-05-10T22:58:52.685975Z",
18+
"iopub.status.busy": "2025-05-10T22:58:52.685879Z",
19+
"iopub.status.idle": "2025-05-10T22:58:53.242565Z",
20+
"shell.execute_reply": "2025-05-10T22:58:53.242249Z"
2121
},
2222
"vscode": {
2323
"languageId": "plaintext"
@@ -52,10 +52,10 @@
5252
"id": "cd38b675",
5353
"metadata": {
5454
"execution": {
55-
"iopub.execute_input": "2025-05-10T20:19:41.540455Z",
56-
"iopub.status.busy": "2025-05-10T20:19:41.540197Z",
57-
"iopub.status.idle": "2025-05-10T20:19:41.542208Z",
58-
"shell.execute_reply": "2025-05-10T20:19:41.541863Z"
55+
"iopub.execute_input": "2025-05-10T22:58:53.244297Z",
56+
"iopub.status.busy": "2025-05-10T22:58:53.244138Z",
57+
"iopub.status.idle": "2025-05-10T22:58:53.246088Z",
58+
"shell.execute_reply": "2025-05-10T22:58:53.245839Z"
5959
},
6060
"vscode": {
6161
"languageId": "plaintext"
@@ -84,10 +84,10 @@
8484
"id": "8518d1b2",
8585
"metadata": {
8686
"execution": {
87-
"iopub.execute_input": "2025-05-10T20:19:41.543276Z",
88-
"iopub.status.busy": "2025-05-10T20:19:41.543102Z",
89-
"iopub.status.idle": "2025-05-10T20:19:41.545044Z",
90-
"shell.execute_reply": "2025-05-10T20:19:41.544752Z"
87+
"iopub.execute_input": "2025-05-10T22:58:53.247498Z",
88+
"iopub.status.busy": "2025-05-10T22:58:53.247398Z",
89+
"iopub.status.idle": "2025-05-10T22:58:53.249282Z",
90+
"shell.execute_reply": "2025-05-10T22:58:53.249031Z"
9191
},
9292
"vscode": {
9393
"languageId": "plaintext"
@@ -120,10 +120,10 @@
120120
"id": "df9403bf",
121121
"metadata": {
122122
"execution": {
123-
"iopub.execute_input": "2025-05-10T20:19:41.546244Z",
124-
"iopub.status.busy": "2025-05-10T20:19:41.546005Z",
125-
"iopub.status.idle": "2025-05-10T20:19:41.550293Z",
126-
"shell.execute_reply": "2025-05-10T20:19:41.549955Z"
123+
"iopub.execute_input": "2025-05-10T22:58:53.250815Z",
124+
"iopub.status.busy": "2025-05-10T22:58:53.250599Z",
125+
"iopub.status.idle": "2025-05-10T22:58:53.254615Z",
126+
"shell.execute_reply": "2025-05-10T22:58:53.254382Z"
127127
},
128128
"vscode": {
129129
"languageId": "plaintext"
@@ -133,7 +133,7 @@
133133
{
134134
"data": {
135135
"text/plain": [
136-
"<libcarna._spatial.geometry.<locals>.Geometry at 0x785136d31d90>"
136+
"<libcarna._spatial.geometry.<locals>.Geometry at 0x795817f55f10>"
137137
]
138138
},
139139
"execution_count": 4,
@@ -182,10 +182,10 @@
182182
"id": "4a1a1c31",
183183
"metadata": {
184184
"execution": {
185-
"iopub.execute_input": "2025-05-10T20:19:41.551417Z",
186-
"iopub.status.busy": "2025-05-10T20:19:41.551196Z",
187-
"iopub.status.idle": "2025-05-10T20:19:41.553164Z",
188-
"shell.execute_reply": "2025-05-10T20:19:41.552855Z"
185+
"iopub.execute_input": "2025-05-10T22:58:53.256019Z",
186+
"iopub.status.busy": "2025-05-10T22:58:53.255920Z",
187+
"iopub.status.idle": "2025-05-10T22:58:53.257734Z",
188+
"shell.execute_reply": "2025-05-10T22:58:53.257486Z"
189189
},
190190
"vscode": {
191191
"languageId": "plaintext"
@@ -221,10 +221,10 @@
221221
"id": "0ca491a8",
222222
"metadata": {
223223
"execution": {
224-
"iopub.execute_input": "2025-05-10T20:19:41.554332Z",
225-
"iopub.status.busy": "2025-05-10T20:19:41.554108Z",
226-
"iopub.status.idle": "2025-05-10T20:19:41.638526Z",
227-
"shell.execute_reply": "2025-05-10T20:19:41.638149Z"
224+
"iopub.execute_input": "2025-05-10T22:58:53.259171Z",
225+
"iopub.status.busy": "2025-05-10T22:58:53.259073Z",
226+
"iopub.status.idle": "2025-05-10T22:58:53.342741Z",
227+
"shell.execute_reply": "2025-05-10T22:58:53.342431Z"
228228
},
229229
"vscode": {
230230
"languageId": "plaintext"
@@ -253,10 +253,10 @@
253253
"id": "37ff9c22",
254254
"metadata": {
255255
"execution": {
256-
"iopub.execute_input": "2025-05-10T20:19:41.639835Z",
257-
"iopub.status.busy": "2025-05-10T20:19:41.639595Z",
258-
"iopub.status.idle": "2025-05-10T20:19:41.733413Z",
259-
"shell.execute_reply": "2025-05-10T20:19:41.733085Z"
256+
"iopub.execute_input": "2025-05-10T22:58:53.344394Z",
257+
"iopub.status.busy": "2025-05-10T22:58:53.344161Z",
258+
"iopub.status.idle": "2025-05-10T22:58:53.438265Z",
259+
"shell.execute_reply": "2025-05-10T22:58:53.437994Z"
260260
},
261261
"vscode": {
262262
"languageId": "plaintext"
@@ -266,7 +266,7 @@
266266
{
267267
"data": {
268268
"text/plain": [
269-
"<matplotlib.image.AxesImage at 0x78536b5edf10>"
269+
"<matplotlib.image.AxesImage at 0x795817e174a0>"
270270
]
271271
},
272272
"execution_count": 7,
@@ -307,10 +307,10 @@
307307
"id": "10ab7a0a",
308308
"metadata": {
309309
"execution": {
310-
"iopub.execute_input": "2025-05-10T20:19:41.734755Z",
311-
"iopub.status.busy": "2025-05-10T20:19:41.734566Z",
312-
"iopub.status.idle": "2025-05-10T20:19:41.857992Z",
313-
"shell.execute_reply": "2025-05-10T20:19:41.857721Z"
310+
"iopub.execute_input": "2025-05-10T22:58:53.439871Z",
311+
"iopub.status.busy": "2025-05-10T22:58:53.439594Z",
312+
"iopub.status.idle": "2025-05-10T22:58:53.579008Z",
313+
"shell.execute_reply": "2025-05-10T22:58:53.578562Z"
314314
},
315315
"vscode": {
316316
"languageId": "plaintext"

misc/libcarna/_alias.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import functools
2+
3+
4+
def kwalias(keyword: str, *aliases: str):
5+
"""
6+
Create an alias for a keyword.
7+
8+
Arguments:
9+
keyword: The original keyword.
10+
*aliases: The aliases for the keyword.
11+
"""
12+
if keyword in aliases:
13+
raise ValueError(f"Keyword and alias cannot be the same: '{keyword}'.")
14+
15+
def decorator(func):
16+
17+
@functools.wraps(func)
18+
def wrapper(*args, **kwargs):
19+
used_aliases = list()
20+
for alias in [keyword] + list(aliases):
21+
if alias in kwargs:
22+
used_aliases.append(alias)
23+
if len(used_aliases) > 1:
24+
raise ValueError(f"Both '{used_aliases[0]}' and '{used_aliases[1]}' provided.")
25+
kwargs[keyword] = kwargs.pop(alias)
26+
return func(*args, **kwargs)
27+
28+
return wrapper
29+
30+
return decorator

misc/libcarna/_drr.py

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import libcarna
2+
from ._alias import kwalias
23

34

45
class drr(libcarna.presets.DRRStage):
@@ -7,14 +8,15 @@ class drr(libcarna.presets.DRRStage):
78
89
Arguments:
910
geometry_type: Geometry type to be rendered.
10-
sr: Sample rate for volume rendering. Larger values result in higher quality and less artifacts, but slower
11-
rendering.
12-
waterat: Water attenuation.
13-
baseint: Base intensity.
14-
lothres: Lower threshold (in Hounsfield Units).
15-
upthres: Upper threshold (in Hounsfield Units).
16-
upmulti: Upper multiplier.
17-
inverse: Inverse rendering (white on black background).
11+
sample_rate: Sample rate for volume rendering (alias: `sr`). Larger values result in higher quality and less
12+
artifacts, but slower rendering.
13+
water_attenuation: Water attenuation (alias: `waterat`).
14+
base_intensity: Base intensity (alias: `baseint`).
15+
lower_threshold: Lower threshold in Hounsfield Units (alias: `lothres`).
16+
upper_threshold: Upper threshold in Hounsfield Units (alias: `upthres`).
17+
upper_multiplier: Upper multiplier (alias: `upmulti`).
18+
render_inverse: If `True`, the image is rendered as gray-white on black background (alias: `inverse`).
19+
Otherwise, the image is rendered as gray-black on white background.
1820
1921
Example:
2022
@@ -29,38 +31,45 @@ class drr(libcarna.presets.DRRStage):
2931
:width: 400
3032
"""
3133

34+
@kwalias('sample_rate', 'sr')
35+
@kwalias('water_attenuation', 'waterat')
36+
@kwalias('base_intensity', 'baseint')
37+
@kwalias('lower_threshold', 'lothres')
38+
@kwalias('upper_threshold', 'upthres')
39+
@kwalias('upper_multiplier', 'upmulti')
40+
@kwalias('render_inverse', 'inverse')
3241
def __init__(
3342
self,
3443
geometry_type: int,
3544
*,
36-
sr: int = libcarna.presets.VolumeRenderingStage.DEFAULT_SAMPLE_RATE,
37-
waterat: float = libcarna.presets.DRRStage.DEFAULT_WATER_ATTENUATION,
38-
baseint: float = libcarna.presets.DRRStage.DEFAULT_BASE_INTENSITY,
39-
lothres: int = libcarna.presets.DRRStage.DEFAULT_LOWER_THRESHOLD,
40-
upthres: int = libcarna.presets.DRRStage.DEFAULT_UPPER_THRESHOLD,
41-
upmulti: float = libcarna.presets.DRRStage.DEFAULT_UPPER_MULTIPLIER,
42-
inverse: bool = libcarna.presets.DRRStage.DEFAULT_RENDER_INVERSE,
45+
sample_rate: int = libcarna.presets.VolumeRenderingStage.DEFAULT_SAMPLE_RATE,
46+
water_attenuation: float = libcarna.presets.DRRStage.DEFAULT_WATER_ATTENUATION,
47+
base_intensity: float = libcarna.presets.DRRStage.DEFAULT_BASE_INTENSITY,
48+
lower_threshold: int = libcarna.presets.DRRStage.DEFAULT_LOWER_THRESHOLD,
49+
upper_threshold: int = libcarna.presets.DRRStage.DEFAULT_UPPER_THRESHOLD,
50+
upper_multiplier: float = libcarna.presets.DRRStage.DEFAULT_UPPER_MULTIPLIER,
51+
render_inverse: bool = libcarna.presets.DRRStage.DEFAULT_RENDER_INVERSE,
4352
):
4453
super().__init__(geometry_type)
45-
self.sample_rate = sr
46-
self.water_attenuation = waterat
47-
self.base_intensity = baseint
48-
self.lower_threshold = lothres
49-
self.upper_threshold = upthres
50-
self.upper_multiplier = upmulti
51-
self.render_inverse = inverse
54+
self.sample_rate = sample_rate
55+
self.water_attenuation = water_attenuation
56+
self.base_intensity = base_intensity
57+
self.lower_threshold = lower_threshold
58+
self.upper_threshold = upper_threshold
59+
self.upper_multiplier = upper_multiplier
60+
self.render_inverse = render_inverse
5261

5362
def replicate(self):
5463
"""
5564
Replicate the DRR renderer.
5665
"""
5766
return drr(
5867
self.geometry_type,
59-
sr=self.sample_rate,
60-
waterat=self.water_attenuation,
61-
baseint=self.base_intensity,
62-
lothres=self.lower_threshold,
63-
upthres=self.upper_threshold,
64-
upmulti=self.upper_multiplier,
65-
inverse=self.render_inverse,
68+
sample_rate=self.sample_rate,
69+
water_attenuation=self.water_attenuation,
70+
base_intensity=self.base_intensity,
71+
lower_threshold=self.lower_threshold,
72+
upper_threshold=self.upper_threshold,
73+
upper_multiplier=self.upper_multiplier,
74+
render_inverse=self.render_inverse,
6675
)

misc/libcarna/_dvr.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import libcarna
2-
2+
from ._alias import kwalias
33
from ._colormap_helper import colormap_helper
44

55

@@ -12,12 +12,13 @@ class dvr(libcarna.presets.DVRStage):
1212
cmap: Color map to use for the DVR. If `None`, the default color map is used.
1313
clim: Color limits for the color map. If `None`, the full range of intensities [0, 1] is used (if `cmap` is
1414
`str` or `None`) or the limits of `cmap` are used (if `cmap` is a :class:`libcarna.base.ColorMap` ).
15-
sr: Sample rate for volume rendering. Larger values result in higher quality and less artifacts, but slower
16-
rendering.
17-
transl: Translucency value for the DVR, that is used on top of the translucency from the color map. A value of
18-
1 means that the overall translucency is doubled. Larger values result in more translucency.
19-
diffuse: Diffuse light value for the volume rendering. Larger values result in more diffuse light. Ambient
20-
light is one minus diffuse light.
15+
sample_rate: Sample rate for volume rendering (alias: `sr`). Larger values result in higher quality and less
16+
artifacts, but slower rendering.
17+
translucency: Translucency value for the DVR, that is used on top of the translucency from the color map
18+
(alias: `transl`). A value of 1 means that the overall translucency is doubled. Larger values result in
19+
more translucency.
20+
diffuse_light: Diffuse light value for the volume rendering. Larger values result in more diffuse light
21+
(alias: `diffuse`). Ambient light is one minus diffuse light.
2122
2223
Example:
2324
@@ -32,21 +33,24 @@ class dvr(libcarna.presets.DVRStage):
3233
:width: 400
3334
"""
3435

36+
@kwalias('sample_rate', 'sr')
37+
@kwalias('translucency', 'transl')
38+
@kwalias('diffuse_light', 'diffuse')
3539
def __init__(
3640
self,
3741
geometry_type: int,
3842
*,
3943
cmap: str | libcarna.base.ColorMap | None = None,
4044
clim: tuple[float | None, float | None] | None = None,
41-
sr: int = libcarna.presets.VolumeRenderingStage.DEFAULT_SAMPLE_RATE,
42-
transl: float = 0,
43-
diffuse: float = libcarna.presets.DVRStage.DEFAULT_DIFFUSE_LIGHT,
45+
sample_rate: int = libcarna.presets.VolumeRenderingStage.DEFAULT_SAMPLE_RATE,
46+
translucency: float = 0,
47+
diffuse_light: float = libcarna.presets.DVRStage.DEFAULT_DIFFUSE_LIGHT,
4448
):
4549
super().__init__(geometry_type)
4650
self.cmap = colormap_helper(self.color_map, cmap, clim)
47-
self.sample_rate = sr
48-
self.translucency = transl
49-
self.diffuse_light = diffuse
51+
self.sample_rate = sample_rate
52+
self.translucency = translucency
53+
self.diffuse_light = diffuse_light
5054

5155
def replicate(self):
5256
"""
@@ -56,7 +60,7 @@ def replicate(self):
5660
self.geometry_type,
5761
cmap=self.cmap.colormap,
5862
clim=None, # uses the color limits from `cmap`
59-
sr=self.sample_rate,
60-
transl=self.translucency,
61-
diffuse=self.diffuse_light,
63+
sample_rate=self.sample_rate,
64+
translucency=self.translucency,
65+
diffuse_light=self.diffuse_light,
6266
)

misc/libcarna/_mask_renderer.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import libcarna
2+
from ._alias import kwalias
23

34

45
class mask_renderer(libcarna.presets.MaskRenderingStage):
@@ -7,10 +8,10 @@ class mask_renderer(libcarna.presets.MaskRenderingStage):
78
89
Arguments:
910
geometry_type: Geometry type to be rendered.
10-
sr: Sample rate for volume rendering. Larger values result in higher quality and less artifacts, but slower
11-
rendering.
12-
color: Color to use for the mask.
13-
fill: If `True`, the mask is filled. If `False`, only the borders are rendered.
11+
sample_rate: Sample rate for volume rendering (alias: `sr`). Larger values result in higher quality and less
12+
artifacts, but slower rendering.
13+
color: Color to use for the mask (alias: `c`).
14+
filling: If `True`, the mask is filled (alias: `fill`). If `False`, only the borders are rendered.
1415
1516
Example:
1617
@@ -25,26 +26,29 @@ class mask_renderer(libcarna.presets.MaskRenderingStage):
2526
:width: 400
2627
"""
2728

29+
@kwalias('sample_rate', 'sr')
30+
@kwalias('color', 'c')
31+
@kwalias('filling', 'fill')
2832
def __init__(
2933
self,
3034
geometry_type: int,
3135
*,
32-
sr: int = libcarna.presets.VolumeRenderingStage.DEFAULT_SAMPLE_RATE,
36+
sample_rate: int = libcarna.presets.VolumeRenderingStage.DEFAULT_SAMPLE_RATE,
3337
color: libcarna.color = libcarna.presets.MaskRenderingStage.DEFAULT_COLOR,
34-
fill: bool = False,
38+
filling: bool = False,
3539
):
3640
super().__init__(geometry_type, mask_role=0)
37-
self.sample_rate = sr
41+
self.sample_rate = sample_rate
3842
self.color = color
39-
self.filling = fill
43+
self.filling = filling
4044

4145
def replicate(self):
4246
"""
4347
Replicate the mask renderer.
4448
"""
4549
return mask_renderer(
4650
self.geometry_type,
47-
sr=self.sample_rate,
51+
sample_rate=self.sample_rate,
4852
color=self.color,
49-
fill=self.filling,
53+
filling=self.filling,
5054
)

0 commit comments

Comments
 (0)