Skip to content

Commit 74890a0

Browse files
committed
Tomography: display by any dimension
1 parent 88df6a0 commit 74890a0

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

caterva2/services/plugins/tomography/__init__.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,17 @@ def display(
8181
base = url(f"plugins/{name}")
8282
context = {
8383
"href": f"{base}/display_one/{path}",
84-
"pages": arr.shape[0],
84+
"shape": arr.shape,
8585
}
8686
return templates.TemplateResponse(request, "display.html", context=context)
8787

8888

89-
async def __get_image(path, user, i):
89+
async def __get_image(path, user, ndim, i):
9090
# Alternatively, call abspath_and_dataprep with the corresponding slice to download data async
9191
array = await get_container(path, user)
92-
content = array[i]
92+
index = [slice(None) for x in array.shape]
93+
index[ndim] = slice(i, i + 1, 1)
94+
content = array[tuple(index)].squeeze()
9395
return PIL.Image.fromarray(content)
9496

9597

@@ -99,13 +101,14 @@ async def display_one(
99101
# Path parameters
100102
path: pathlib.Path,
101103
# Query parameters
102-
i: int,
104+
ndim: int = 0,
105+
i: int = 0,
103106
user: db.User = Depends(optional_user),
104107
):
105-
img = await __get_image(path, user, i)
108+
img = await __get_image(path, user, ndim, i)
106109

107110
base = url(f"plugins/{name}")
108-
src = f"{base}/image/{path}?{i=}"
111+
src = f"{base}/image/{path}?{ndim=}&{i=}"
109112

110113
width = 768 # Max size
111114
links = []
@@ -129,11 +132,12 @@ async def image_file(
129132
# Path parameters
130133
path: pathlib.Path,
131134
# Query parameters
135+
ndim: int,
132136
i: int,
133137
width: int | None = None,
134138
user: db.User = Depends(optional_user),
135139
):
136-
img = await __get_image(path, user, i)
140+
img = await __get_image(path, user, ndim, i)
137141
img_file = resize_image(img, width)
138142

139143
def iterfile():
Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
1-
<input type="number" min="0" max="{{ pages - 1 }}" name="i" value="0"
2-
hx-get="{{ href }}" hx-target="#image-div">
1+
<form class="d-inline-flex gap-2" hx-get="{{ href }}" hx-target="#image-div" hx-trigger="change">
2+
<select class="form-select" name="ndim" hx-on:change="updateDim(this)">
3+
{% for dim in shape %}
4+
<option value="{{ loop.index0 }}" data-size="{{ dim }}">Dim {{ loop.index0 }} ({{ dim }})</option>
5+
{% endfor %}
6+
</select>
7+
<input class="form-control" type="number" min="0" max="{{ shape.0 - 1}}" name="i" value="0">
8+
</form>
39

410
<div hx-get="{{ href }}?i=0" hx-trigger="load" id="image-div"></div>
11+
12+
<script>
13+
function updateDim(select) {
14+
const size = select.selectedOptions[0].getAttribute('data-size');
15+
const input = document.querySelector('input[name="i"]');
16+
input.setAttribute('max', size - 1);
17+
input.value = 0;
18+
}
19+
</script>

0 commit comments

Comments
 (0)