Skip to content

Commit

Permalink
Add support for custom reduce functions (#338)
Browse files Browse the repository at this point in the history
* Add support for custom reducefn

* Add tutorial of custom reducefn

* Fix rendering in README
  • Loading branch information
eigenvivek authored Oct 27, 2024
1 parent 7e453e0 commit 66b7b5f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
5 changes: 5 additions & 0 deletions diffdrr/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,16 @@ def _get_voxel(volume, xyzs, img, mode, align_corners):
return img

# %% ../notebooks/api/01_renderers.ipynb 9
from typing import Callable


def reduce(img, reducefn):
if reducefn == "sum":
return img.sum(dim=-1)
elif reducefn == "max":
return img.max(dim=-1).values
elif isinstance(reducefn, Callable):
return reducefn(img)
else:
raise ValueError(f"Only supports reducefn 'sum' or 'max', not {reducefn}")

Expand Down
5 changes: 5 additions & 0 deletions notebooks/api/01_renderers.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,16 @@
"outputs": [],
"source": [
"#| exporti\n",
"from typing import Callable\n",
"\n",
"\n",
"def reduce(img, reducefn):\n",
" if reducefn == \"sum\":\n",
" return img.sum(dim=-1)\n",
" elif reducefn == \"max\":\n",
" return img.max(dim=-1).values\n",
" elif isinstance(reducefn, Callable):\n",
" return reducefn(img)\n",
" else:\n",
" raise ValueError(f\"Only supports reducefn 'sum' or 'max', not {reducefn}\")"
]
Expand Down
2 changes: 1 addition & 1 deletion notebooks/index.ipynb

Large diffs are not rendered by default.

44 changes: 44 additions & 0 deletions notebooks/tutorials/introduction.ipynb

Large diffs are not rendered by default.

0 comments on commit 66b7b5f

Please sign in to comment.