Skip to content

Commit 2e42cff

Browse files
committed
Use unreleased whippersnappy from git
Address github co-pilot suggestions replace libegl with libmesa in docker for no-gpu rendering
1 parent 49b0607 commit 2e42cff

4 files changed

Lines changed: 31 additions & 18 deletions

File tree

CorpusCallosum/cc_visualization.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55

66
import nibabel as nib
77
import numpy as np
8-
from nibabel.affines import apply_affine
98

109
from CorpusCallosum.data.fsaverage_cc_template import load_fsaverage_cc_template
1110
from CorpusCallosum.shape.contour import CCContour
1211
from CorpusCallosum.shape.mesh import CCMesh
12+
from FastSurferCNN.utils import AffineMatrix4x4
1313
from FastSurferCNN.utils.logging import get_logger, setup_logging
1414
from FastSurferCNN.utils.lta import read_lta
1515

@@ -221,8 +221,8 @@ def main(
221221
# we need to get the upright image header, which is the same as cc_up.lta applied to orig.
222222
elif Path(template_dir / "mri/orig.mgz").exists() and Path(template_dir / "mri/transforms/cc_up.lta").exists():
223223
image = nib.load(template_dir / "mri" / "orig.mgz")
224-
lta_mat = read_lta(template_dir / "mri/transforms/cc_up.lta")["lta"]
225-
image.affine = apply_affine(lta_mat, image.affine)
224+
lta_mat: AffineMatrix4x4 = read_lta(template_dir / "mri/transforms/cc_up.lta")["lta"]
225+
image.affine = lta_mat @ image.affine
226226
header = image.header
227227
else:
228228
header = None
@@ -245,9 +245,10 @@ def main(
245245
try:
246246
cc_mesh.snap_cc_picture(str(output_dir / "cc_mesh_snap.png"), ref_header=header)
247247
logger.info(f"Writing 3D snapshot image to {output_dir / 'cc_mesh_snap.png'}")
248-
except RuntimeError:
248+
except Exception:
249249
logger.warning("The cc_visualization script requires whippersnappy>=2.0 to makes screenshots, install with "
250-
"`pip install whippersnappy>=2.0` !")
250+
"`pip install whippersnappy>=2.0` !")
251+
raise
251252
return 0
252253

253254
if __name__ == "__main__":

CorpusCallosum/shape/mesh.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ def __create_cc_viewmat() -> "Matrix44":
477477
- -8 degrees around z-axis
478478
3. Adds a small translation for better centering
479479
"""
480-
from whippersnappy.gl.views import ViewType, get_view_matrix
480+
from whippersnappy import ViewType, get_view_matrix
481481

482482
if not HAS_PYRR:
483483
raise ImportError("Pyrr not installed, install pyrr with `pip install pyrr`.")
@@ -511,12 +511,14 @@ def snap_cc_picture(
511511
----------
512512
output_path : Path, str
513513
Path where to save the snapshot image.
514-
ref_header : Path, str, nibabelImage, optional
515-
Path to reference image to use for tkr creation. If None, ignores the file for saving.
514+
ref_header : Path, str, nibabelHeader, optional
515+
Path to reference image header to use for tkr creation. If None, ignores the file for saving.
516516
517517
Raises
518518
------
519-
Warning
519+
ImportError
520+
If whippersnappy is not installed or if the version is too old.
521+
ValueError
520522
If the mesh has no faces and cannot create a snapshot.
521523
"""
522524
from packaging.version import parse
@@ -539,20 +541,26 @@ def snap_cc_picture(
539541
)
540542
# Skip snapshot if there are no faces
541543
if len(self.t) == 0:
542-
logger.warning("Cannot create snapshot - no faces in mesh")
543-
return
544+
raise ValueError("Cannot create snapshot - no faces in mesh")
544545

545546
self.__make_parent_folder(output_path)
546547

547-
if ref_header is not None:
548-
v = apply_affine(ref_header.get_vox2ras_tkr(), self.v)
549-
else:
548+
if ref_header is None:
550549
v = self.v
550+
else:
551+
from nibabel.freesurfer.mghformat import MGHHeader
552+
553+
# if header is a file, load its header from the file
554+
if isinstance(ref_header, (str, Path)):
555+
ref_header = nib.load(ref_header).header
556+
# if header is not already an MGHHeader, convert it to MGHHeader, so we have the get_vox2ras_tkr function
557+
mgh_header = ref_header if isinstance(ref_header, MGHHeader) else MGHHeader.from_header(ref_header)
558+
v = apply_affine(mgh_header.get_vox2ras_tkr(), self.v)
559+
551560
whippersnappy.snap1(
552561
mesh=(v, self.t),
553562
overlay=self.mesh_vertex_colors,
554-
view=None,
555-
viewmat=self.__create_cc_viewmat(),
563+
view=self.__create_cc_viewmat(),
556564
width=3 * 500,
557565
height=3 * 300,
558566
outpath=str(output_path),

pyproject.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ dependencies = [
6060

6161
[project.optional-dependencies]
6262
qc = [
63-
'whippersnappy>=2.0',
63+
'whippersnappy@git+https://github.com/Deep-MI/WhipperSnapPy@main',
64+
# 'whippersnappy>=2.0.1',
6465
]
6566
doc = [
6667
'fastsurfer[qc]',
@@ -171,3 +172,6 @@ exclude_lines = [
171172
'if __name__ == .__main__.:',
172173
]
173174
precision = 2
175+
176+
[tool.uv.sources]
177+
whippersnappy = { git = "https://github.com/Deep-MI/WhipperSnapPy", rev = "main" }

tools/Docker/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ RUN <<EOF
272272
apt-get update
273273
shell_deps="time bc"
274274
freesurfer_deps="gawk libgomp1 libquadmath0 tcsh"
275-
whippersnappy_egl_deps="libegl1 libgl1 libfontconfig1"
275+
whippersnappy_egl_deps="libosmesa6 libgl1 libfontconfig1"
276276
apt-get install -y --no-install-recommends \
277277
$shell_deps \
278278
$freesurfer_deps \

0 commit comments

Comments
 (0)