ChristianGaser / cat12

Computational Anatomy Toolbox for SPM12
https://neuro-jena.github.io/cat
GNU General Public License v2.0
32 stars 5 forks source link

Report files missing when using MCR Linux CAT12 version #27

Open jhuguetn opened 3 months ago

jhuguetn commented 3 months ago

At running the Segmentation module from the latest standalone release (12.9 r2556, see CAT12.9_R2017b_MCR_Linux.zip), CAT12 complains about missing some of the QC report output files. See trace below:

> cat_standalone.sh -b /opt/spm/standalone/cat_standalone_segment.m /data/sub-1_ses-01_T1w.nii.gz
...

  Remove "/data/report/catreport_sub-1_ses-01_T1w.pdf" from dependency list because it does not exist!
One or multiple files do not exist and were removed from the dependency list
and following batches will may not work correctly!

  Remove "/data/report/catreportj_sub-1_ses-01_T1w.jpg" from dependency list because it does not exist!
One or multiple files do not exist and were removed from the dependency list
and following batches will may not work correctly!

03-Jun-2024 08:12:35 - Done    'CAT12: Segmentation'
03-Jun-2024 08:12:35 - Done

Bye for now...

Any ideas of what might be going on?

psadil commented 2 months ago

I'm noticing the same issue (with a different container). Would be happy to dig into this, but do you have any recommendations about troubleshooting? For example, is there a "verbose" option that could lead to a more helpful error log?

psadil commented 1 month ago

Bumping in case there are any ideas. For what it's worth, using MATLAB 2024a (Update 5), I can compile a standalone version of CAT v12.9 that runs and produces the report outside of a container, but when it is run inside the container the pdf is not produced (as reported above, there are no errors except for the message about a lack of pdf and jpg outputs). Any help troubleshooting would be appreciated.

psadil commented 1 month ago

Some progress: the issue is around here https://github.com/ChristianGaser/cat12/blob/57ca3e82b2de21e4d22e2d6deeded1a9bdfab56c/cat_main_reportfig.m#L1608-L1625

Inside the containers, the renderer is painter, and so hSD is created by cat_surf_renderv. The output of cat_surf_renderv only has the field cdata according to these rules: https://github.com/ChristianGaser/cat12/blob/57ca3e82b2de21e4d22e2d6deeded1a9bdfab56c/cat_surf_renderv.m#L276-L288.

In the containers that I'm testing, opt.h is an

Axes with properties

XLim: [0 1]
YLim: [0 1]
XScale: 'Linear'
YScale: 'Linear'
GridLineStyle: '-'
Position: [0.6800 0.1800 0.3000 0.1700]
Units: 'normalized'

Since axes > 1 always evaluates to 0, the cdata field is never assigned, and so cat_main_reportfig returns before writing the pdf and jpg.

psadil commented 1 month ago

Also, the check for opt.h > 1 was introduced between the last two releases (https://github.com/ChristianGaser/cat12/commit/c4d6a675e9c193457049e715e5ddb0a72689260b#diff-17b3a61bcf5582e69cfa6c633f51d5329d8af72c0ad1f17ee3e90f7332699c2c), which explains why this was an issue for jhuguetn/cat12: r2556 but not jhuguetn/cat12: r2166

jhuguetn commented 1 month ago

Hi @psadil, sorry for the long hiatus in responding.

I am glad you have put some work/willingness on trying to fix this problem, it's a blocking issue for us so we are still using an older working release. To be honest I haven't done much since I posted the issue and I am afraid I cannot help much debugging the code, my expertise in MATLAB is rather limited.

That said, I find it very interesting and does make sense that only container executions are somehow affected but I cannot see how the finding you posted above on opt.h would only affect container-based executions of CAT12. Do you have a fix proposal or an explanation of why could this issue be occurring?

psadil commented 1 month ago

It's a blocking issue for a project that I'm working on, so I'm also very motivated to come up with a solution.

I think it only affects containers because of a difference in renderers, and that the renderer used by containers does not result in a call to cat_surf_renderv. Here, there is an if statement based on renderer (there are a few such statements in cat_main_reportfig.m): https://github.com/ChristianGaser/cat12/blob/57ca3e82b2de21e4d22e2d6deeded1a9bdfab56c/cat_main_reportfig.m#L1510,

So, when the renderer is 'opengl', the figures are drawn with cat_surf_display, but otherwise the figures are drawn with cat_surf_renderv.

When I am working outside of a container on my linux machine (MATLAB R2024a), the renderer is 'opengl'

>> fg = spm_figure('Create', 'Graphics', 'visible', 'off');
>> get(fg, 'Renderer')

ans = 
    'opengl'

>> close all;

This is the same when I run the standalone version outside of a container.

But when I run that inside of a container

$ docker run --init --rm -it --entrypoint=/bin/bash jhuguetn/cat12 

# now inside the container -- use the standalone version to eval a matlab statement
$ /opt/spm/run_spm12.sh /opt/mcr/v93 eval "fg = spm_figure('Create', 'Graphics', 'visible', 'off'); get(fg, 'Renderer'), close all;"
------------------------------------------
Setting up environment variables
---
LD_LIBRARY_PATH is .:/opt/mcr/v93/runtime/glnxa64:/opt/mcr/v93/bin/glnxa64:/opt/mcr/v93/sys/os/glnxa64:/opt/mcr/v93/sys/opengl/lib/glnxa64
SPM12, version 7771 (standalone)
MATLAB, version 9.3.0.713579 (R2017b)
 ___  ____  __  __                                            
/ __)(  _ \(  \/  )                                           
\__ \ )___/ )    (   Statistical Parametric Mapping           
(___/(__)  (_/\/\_)  SPM12 - https://www.fil.ion.ucl.ac.uk/spm/

ans =

    'painters'

So, inside the container the renderer is not 'opengl', meaning that cat_surf_renderv won't be called, meaning comparison opt.h > 1 never happens.

I'm not familiar enough with the package to be confident about a solution. Maybe @ChristianGaser has input?

psadil commented 1 month ago

Possibilities I can think of include

psadil commented 2 weeks ago

FWIW, I haven't found a way to set the default renderer inside a container to opengl.

What was that opt.h > 1 case meant to cover?

psadil commented 5 days ago

FWIW, it is possible to compile a standalone version of CAT12 and package it into a container such that the renderer is opengl. The default function for compiling SPM is here: https://github.com/spm/spm12/blob/03ac9473cad402407b6472228377c0167fdc54b8/config/spm_make_standalone.m#L117-L129 and includes the suspiciously helpful flag -softwareopengl.

MATLAB's documentation on creating standalone applications describes calling docker with a few extra arguments

$ docker run --rm -e "DISPLAY=:0" -v /tmp/.X11-unix:/tmp/.X11-unix [image] [...]

So, it still seems like there is a bug in cat12 when the renderer is painters. But the approach of a custom standalone version and container works (repo: https://github.com/psadil/cat12, container: https://hub.docker.com/r/psadil/cat12)

jhuguetn commented 3 days ago

Thanks @psadil for all the work put on understanding and disentangling this issue. In my opinion, this task requires direct action from main developers of CAT12 (cc @ChristianGaser) to be properly fixed. Otherwise the solutions we might come up with at a posteriori are mere patches.