|
| 1 | +# Apptainer |
| 2 | + |
| 3 | +## LLMs |
| 4 | +Claude code should be able to prepare the container and set up the kernel, if given the relevant information in `https://github.com/anders-biostat/Cytovanni/blob/main/llms.txt`. |
| 5 | + |
| 6 | +## Build Container |
| 7 | + |
| 8 | +The GitHub contains an apptainer definition file `Cytovanni/container/container.def`, from which the `.sif` file can be built using |
| 9 | +```bash |
| 10 | +apptainer build -F cytovanni-container.sif container.def |
| 11 | +``` |
| 12 | +It is then possible to run scripts within this container, which contains all necessary dependencies for the package, as well as CUDA support as long as it is running on a machine with a CUDA-enabled graphics card. |
| 13 | + |
| 14 | +Note however that the container is not writable, so if you need additional python packages you need to either modify container.def manually, or modify it through the shell with |
| 15 | +```bash |
| 16 | +sed '/ # custom packages/a\ uv pip install --system package1 package2' -i container.def |
| 17 | +``` |
| 18 | +where package1 package2 should be replaced by whatever additional packages you need. Then rebuild the container to include the new packages. |
| 19 | + |
| 20 | + |
| 21 | +## Use Container as Jupyter Kernel |
| 22 | +Our preferred way is using this container as a Jupyter kernel. |
| 23 | +For this, you need to create a script `init_kernel.sh` containing |
| 24 | +```bash |
| 25 | +#!/bin/bash |
| 26 | +apptainer exec --nv /path/to/cytovanni-container.sif python -m ipykernel "$@" |
| 27 | +``` |
| 28 | +`--nv` enables the container to access the graphics card, and `/path/to` should be replaced by the correct path structure. |
| 29 | +Apptainer automatically mounts the user directory; you can additionally mount other directories using `--bind`. |
| 30 | +You then need to create a custom Jupyter kernel file, first by creating the folder |
| 31 | +```bash |
| 32 | +mkdir -p ~/.local/share/jupyter/kernels/cytovanni-container |
| 33 | +cd ~/.local/share/jupyter/kernels/cytovanni-container |
| 34 | +``` |
| 35 | +and then add a file `kernel.json` with |
| 36 | +```json |
| 37 | +{ |
| 38 | + "argv": [ |
| 39 | + "/path/to/init_kernel.sh", |
| 40 | + "-f", |
| 41 | + "{connection_file}" |
| 42 | + ], |
| 43 | + "display_name": "Python (Cytovanni)", |
| 44 | + "language": "python" |
| 45 | +} |
| 46 | +``` |
| 47 | +Copying `logo-64x64.png` into `~/.local/share/jupyter/kernels/cytovanni-container` will additionally add a nice icon in the Jupyter launcher. |
| 48 | + |
| 49 | +This custom kernel can then be used just like any other Jupyter kernel. |
| 50 | + |
| 51 | +## R |
| 52 | +We also provide `Rcontainer.def` to run CytoNorm. Compiling works the same way as above, |
| 53 | +```bash |
| 54 | +apptainer build -F cytovanni-R-container.sif Rcontainer.def |
| 55 | +``` |
| 56 | +along with `kernel.json` |
| 57 | +```json |
| 58 | +{ |
| 59 | + "argv": [ |
| 60 | + "/path/to/init_R_kernel.sh", |
| 61 | + "-f", |
| 62 | + "{connection_file}" |
| 63 | + ], |
| 64 | + "display_name": "R (Cytovanni)", |
| 65 | + "language": "R" |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +However, due to changes in the syntax, the `init_R_kernel.sh` needs to be a bit more complex: |
| 70 | +```bash |
| 71 | +#!/bin/bash |
| 72 | +for i in "$@"; do |
| 73 | + if [[ "$prev" == "-f" ]]; then |
| 74 | + CONN_FILE="$i" |
| 75 | + fi |
| 76 | + prev="$i" |
| 77 | +done |
| 78 | +apptainer exec /path/to/cytovanni-R-container.sif R --slave -e "IRkernel::main('${CONN_FILE}')" |
| 79 | +``` |
0 commit comments