Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImportError: cannot import name 'mmr_auxe' from partially initialized module 'niftypet.nipet' #48

Open
Melissa1909 opened this issue Aug 10, 2023 · 11 comments

Comments

@Melissa1909
Copy link

Dear developers,
I have installed niftypet inside a docker container using the attached Dockerfile. However, if I start it and enter the Python3 venv (/venv/bin/python3) and import nipet from niftypet as given in the example scripts (from niftypet import nipet), I get the following error:

>>> from niftypet import nipet
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/venv/NIPET/niftypet/nipet/__init__.py", line 45, in <module>
    from . import img, lm, mmr_auxe, mmraux, mmrnorm, prj
  File "/venv/NIPET/niftypet/nipet/img/__init__.py", line 4, in <module>
    from . import auximg, mmrimg
  File "/venv/NIPET/niftypet/nipet/img/mmrimg.py", line 16, in <module>
[Dockerfile.txt](https://github.com/NiftyPET/NiftyPET/files/12313850/Dockerfile.txt)

    from .. import mmraux
  File "/venv/NIPET/niftypet/nipet/mmraux.py", line 21, in <module>
    from . import mmr_auxe, resources
ImportError: cannot import name 'mmr_auxe' from partially initialized module 'niftypet.nipet' (most likely due to a circular import) (/venv/NIPET/niftypet/nipet/__init__.py)

I have also tried to install nipet via niftypet and directly via pip but the error still occurs. Any feedback or suggestions are appreciated!
Best, Melissa
Dockerfile.txt

@casperdcl
Copy link
Member

casperdcl commented Aug 10, 2023

Hey @Melissa1909 - I hadn't tested installing within Docker before.

NiftyPET detects your GPUs during the build process, but unfortunately Docker doesn't expose GPUs during build time (you can't docker build --gpus all).

Unfortunately the hack for now would be to install at runtime (docker run --gpus all):

# CUDA development toolkit
FROM nvidia/cuda:12.2.0-devel-ubuntu22.04
# Install Python3 dev depedencies
RUN apt-get update -qq && apt-get install -yqq python3-venv python3-dev && apt-get clean
# Create and activate a virtual environment
RUN python3 -m venv /venv
ENV PATH="/venv/bin:$PATH"
# Install useful Python packages inside virtual environment
RUN pip install --no-cache numpy scipy scikit-image matplotlib nibabel pydicom pandas ipykernel ipywidgets brainweb spm12

# Install NiftyPET
ENV HMUDIR=/mmr_hardwareumaps
# install after starting the container (requires --gpus all)
COPY <<EOF /entry.sh
#!/bin/bash
set -euo pipefail
if test -z "\$(pip list | grep nipet)"; then
  echo 'installing NiftyPET (requires `docker run --gpus all`)'
  pip install --no-cache nimpa[cuda,dcm2niix,niftyreg] nipet
fi
exec "\$@"
EOF
RUN chmod 755 /entry.sh
ENTRYPOINT ["/entry.sh"]
CMD ["/bin/bash"]

@casperdcl
Copy link
Member

☝️ @pjmark you might have to tackle the last bullet point as I don't have much time :)

@casperdcl casperdcl transferred this issue from NiftyPET/NiftyPET Aug 10, 2023
@casperdcl casperdcl assigned pjmark and unassigned casperdcl Aug 10, 2023
@casperdcl
Copy link
Member

casperdcl commented Aug 10, 2023

nevermind - I just tested and I think NIPET does install properly after all...

Here's what I'm using @Melissa1909:

# CUDA development toolkit (~7GB)
FROM nvidia/cuda:12.2.0-devel-ubuntu22.04 as build
# Install Python3 dev depedencies
RUN apt-get update -qq && apt-get install -yqq python3-venv python3-dev && apt-get clean
# Create and activate a virtual environment
RUN python3 -m venv /venv
ENV PATH="/venv/bin:$PATH"
# Install NiftyPET
ENV HMUDIR=/mmr_hardwareumaps
RUN pip install --no-cache 'nimpa[cuda,dcm2niix,niftyreg]>=2.6.3' nipet

# Make a smaller image without development toolkit (~1GB)
FROM nvidia/cuda:12.2.0-base-ubuntu22.04 as nipet
RUN apt-get update -qq && apt-get install -yqq python3-venv python3-dev && apt-get clean
# Copy virtual environment & NiftyPET build artefacts
COPY --from=build --link /venv /venv
ENV PATH="/venv/bin:$PATH"
ENV HMUDIR=/mmr_hardwareumaps
COPY --from=build --link /root/.niftypet /root/.niftypet
# Install useful Python packages
RUN pip install --no-cache numpy scipy scikit-image matplotlib nibabel pydicom pandas ipykernel ipywidgets brainweb spm12

Should really add this to the docs (source)

@VikNeu
Copy link

VikNeu commented Aug 23, 2023

Dear Developers,

regarding this exact docker installation, I was unable to access nipet and nimpa as there are some resources lacking, giving me this exact error code:

NiftyPET resources file <resources.py> could not be imported.
It should be in ~/.niftypet/resources.py (Linux) or
in //Users//USERNAME//AppData//Local//niftypet//resources.py (Windows)
but likely it does not exists.

Traceback (most recent call last):
File "", line 1, in
File "/venv/lib/python3.10/site-packages/niftypet/nipet/init.py", line 41, in
from niftypet.ninst.tools import LOG_FORMAT, LogHandler, path_resources, resources
File "/venv/lib/python3.10/site-packages/niftypet/ninst/tools.py", line 47, in
resources = cs.get_resources()
File "/venv/lib/python3.10/site-packages/niftypet/ninst/cudasetup.py", line 206, in get_resources
import resources
ModuleNotFoundError: No module named 'resources'

@casperdcl
Copy link
Member

casperdcl commented Aug 23, 2023

I can't reproduce your error @VikNeu ... can run echo $HOME and ls $HOME/.niftypet in the container?

@Melissa1909
Copy link
Author

Hi @casperdcl,
@VikNeu and I are in the same group and we're testing the container together. I actually also couldn't reproduce VikNeu's error but for me, it says that niftypet is not installed at all. I start the container with the command docker run --rm -it --gpus all niftypet:latest /bin/bash.
When running ls $HOME/.niftypet inside, I receivels: cannot access '/home/BRAIN/mthalhammer/.niftypet': No such file or directory.

Maybe we should mention that our Linux machines (with NVIDIA GPU) in the office due not have access to the Internet. That's why we have to build a container to ship the ready-to-use software from other machines to it. However, I have created the image on a Windows without a GPU, whereas the image was tested on the aforementioned Linux - might that cause the installation problems?

Thank you for your help!
Best, Melissa

@casperdcl
Copy link
Member

casperdcl commented Aug 28, 2023

Based on $HOME being /home/BRAIN/mthalhammer/ in your container, you seem to be doing something messing with home dirs. Make sure you install after such changes (or copy the old home dir to the new one).

I've just pushed this to https://github.com/NiftyPET/NIPET/pkgs/container/nipet so you can use the same image:

> docker run --rm --gpus all -it ghcr.io/niftypet/nipet /bin/bash
root@f42f473070d9:/# ls $HOME/.niftypet
__pycache__  resources.py
root@f42f473070d9:/# pip list | grep -E 'nipet|nimpa'
nimpa              2.6.3
nipet              2.2.0

@Melissa1909
Copy link
Author

The link does not yet exist unfortunately...

@casperdcl
Copy link
Member

try now?

@Melissa1909
Copy link
Author

Worked, could download it!

However, when using your image, ls $HOME/.niftypet still gives me cannot access '/home/BRAIN/mthalhammer/.niftypet': No such file or directory.

I once had this problem using another image that required having some folder in the home directory that I could just copy to my Linux machine (netneurolab/neuromaps#69). Could there be a workaround like that? I can't find .niftypet in my home directory either though.

@Heven-Pan
Copy link

Hi @casperdcl and @Melissa1909, I had the same problem, but I installed it on Linux using conda.
>>> from niftypet import nipet Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/root/NIPET/niftypet/nipet/__init__.py", line 45, in <module> from . import img, lm, mmr_auxe, mmraux, mmrnorm, prj File "/root/NIPET/niftypet/nipet/img/__init__.py", line 4, in <module> from . import auximg, mmrimg File "/root/NIPET/niftypet/nipet/img/mmrimg.py", line 16, in <module> from .. import mmraux File "/root/NIPET/niftypet/nipet/mmraux.py", line 21, in <module> from . import mmr_auxe, resources ImportError: cannot import name 'mmr_auxe' from partially initialized module 'niftypet.nipet' (most likely due to a circular import) (/root/NIPET/niftypet/nipet/__init__.py)
I am sorry that I am a beginner, so I have little understanding of what you have discussed above. So I would like to ask you how to solve this problem at last, and hope to hear from you soon in your spare [time!]
Attached below is my installation and build process
Best, Hewen!
My install and build steps.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants