# Batch process CemrgApp using the command line tools
Two options to access CemrgApp's tools to batch process cases:
+ Use the `imatools` python library, which accesses CemrgApp tools
+ Use CemrgApp tools directly
## Prerequisites
#### 1. Download CemrgApp version 2.3-rc6
Ask for access to this particular version. Linux and Windows versions.
#### 2. Install imatools
Check full instructions [here](https://github.com/alonsoJASL/imatools?tab=readme-ov-file#readme). In a few steps:
+ Clone the repository `git clone https://github.com/alonsoJASL/imatools ~/code/imatools`
+ Install the conda environment. [Instructions here](https://github.com/alonsoJASL/imatools?tab=readme-ov-file#option-1-recommended-use-conda--poetry)
## Using `imatools`
Go to the `imatools` folder. If you have been following this tutorial that would be `cd ~/code/imatools`. Activate the conda environment `conda activate imatools`
#### 1. Resample segmentations
Resample predictions with the `segmentation_tools.py` script:
```shell
python imatools/segmentation_tools.py resample -in /path/to/data/prediction_1_ED_gt.nii.gz --resample-close
```
#### 2. Find out the folders of CemrgApp tools
In linux, assume you you downloaded CemrgApp into a folder `~/apps` so if you do `cd ~/apps/CemrgApp-Linux_v2.3-rc6` and then list `ls` the contents of the folder, you will find:
```shell
❯ ls -1
bin
bindockerDebug.txt
CemrgApp.sh
MitkCemrgAreaFromThreshold.sh
MitkCemrgClippingTool.sh
MitkCemrgColourSurface.sh
MitkCemrgFixShell.sh
MitkCemrgIM2INR.sh
MitkCemrgImageConvertFormat.sh
MitkCemrgMorphAnalysis.sh
MitkCemrgScarProjectionOptions.sh
MitkCemrgVentricleSegmRelabel.sh
MitkProjectLgeToVtkMesh.sh
MitkWorkbench.sh
```
Note the locations as mentioned in the following table
| Name | Location in this example |
| ----------- | ------------------------------------------------------ |
| `CEMRG_DIR` | `/home/username/apps/CemrgApp-Linux_v2.3-rc6` |
| `MIRTK_DIR` | `/home/username/apps/CemrgApp-Linux_v2.3-rc6/bin/MLib` |
| `M3D_DIR` | `/home/username/apps/CemrgApp-Linux_v2.3-rc6/bin/M3DLib` |
| `SCAR_CMD` | `MitkCemrgScarProjectionOptions.sh` |
#### 3. Create surface mesh
```shell
cd ~/code/imatools
conda activate imatools
```
Copy the whole command below:
```shell
python imatools/scarq_tools.py surf \
--base-dir /path/to/data/resampled_files \
--input prediction_1_ED_gt_resampled.nii \
--cemrg-dir $CEMRG_DIR \
--mirtk-dir $MIRTK_DIR \
--surf-output prediction_1_ED_gt_resampled \
--surf-multilabel # 2 or more tags in image
```
If you can put these into a loop, you could do batch processing.
# Volumetric meshes
This uses the `imatools` code. See above.
> (Optional) Resample your segmentation with the uses above.
#### 1. Convert your segmentation to INR
> Make sure you do `export PYTHONPATH=""`
```shell=
python imatools/segmentation_tools.py inr -in <input_image> -out converted.inr
```
#### 2. Create your parameter file
```shell
python imatools/m3d_parfile.py new -f <filename.par> \
-seg_dir /path/to/segmentation \
-seg_name converted.inr
-outdir /path/to/meshes
-name output_name
```
#### 3. Use `meshtools3d` binaries
Go to the `$M3D_DIR`, you will see the binary of `meshtools3d` binary. Now you can run the following command:
> Make sure the binaries have permissions `chmod 777 meshtools3d` shoud do it
```shell
./meshtools3d -f /path/to/filename.par
```
```python
import os
list_of_dir = [...]
cmd = "./meshtools3d"
for dir in list_of_dir:
command = f'{cmd} -f {dir}/parameter_file.par'
os.system(cmd)
```