Skip to content

Commit

Permalink
Merge pull request #48 from reghbali/reghbali_bids_fix
Browse files Browse the repository at this point in the history
Reghbali bids fix
  • Loading branch information
reghbali authored Jun 20, 2024
2 parents 414b195 + bea5b3f commit 7cbe91c
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 77 deletions.
72 changes: 49 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# PyALFE

Python implementation of Automated Lesion and Feature Extraction (ALFE) pipeline.
We developed this pipeline for analysis of brain MRIs of patients suffering from conditions that cause brain lesions. It utilizes image processing tools, image registration tools, and deep learning segmentation models to produce a set of features that describe the lesion in the brain.

## Requirements

Expand All @@ -9,7 +10,7 @@ PyALFE supports Linux x86-64, Mac x86-64, and Mac arm64 and requires python 3.9+
### Image registration and processing
PyALFE can be configured to use either [Greedy](https://greedy.readthedocs.io/en/latest/) or [AntsPy](https://antspy.readthedocs.io/en/latest/registration.html) registration tools.
Similarly, PyALFE can can be configured to use [Convert3D](https://sourceforge.net/p/c3d/git/ci/master/tree/doc/c3d.md) or python native library [Nilearn](https://nilearn.github.io/stable/index.html) for image processing tasks.
To use Greedy and Convert3d, these command line tools should be downloaded using the [download command](#download-models-and-tools).
To use Greedy and Convert3d, these command line tools should be installed on your system.

## Installation

Expand Down Expand Up @@ -49,7 +50,7 @@ pip install --upgrade build
Run the following commands in the parent pyalfe directory to build the whl file and install pyalfe
```bash
python -m build
pip install dist/pyalfe-0.0.1-py3-none-any.whl
pip install dist/pyalfe-0.1.0-py3-none-any.whl
```

### Download models
Expand All @@ -76,12 +77,12 @@ pyalfe configure
```
which prompt the you to enter the following required configurations:

#### Classified directory
#### Input directory
```bash
Enter input image directory: /path/to/my_mri_data
```
The classified directory (`classified_dir`) is the input directory to PyALFE and should be organized by accessions (or session ids). Inside the directory for each accession there should be a directory for each available modality.
Here is an example that follow ALFE default structure:
The input directory (`input_dir`) contains the images that will be processed by PyALFE and should be organized by accessions (or session ids). Inside the directory for each accession there should be a directory for each available modality.
Here is an example that follows ALFE default structure:
```
my_mri_data
Expand Down Expand Up @@ -112,34 +113,51 @@ my_mri_data
└───T2
└── T2.nii.gz
```
To use this directory the user should provide `path/to/my_mri_data` as the classified directory. This config value can be overwritten when calling `pyalfe run` via `-cd` or `--classified-dir` option.
To use this directory the user should provide `path/to/my_mri_data` as the input directory. This config value can be overwritten when calling `pyalfe run` via `-id` or `--input-dir` option.

pyALFE also supports BIDS directories. Here is an example of input dir organized in BIDS format:

```
my_mri_data
│───anat
│ │───sub-123_T1w.nii.gz
│ │───sub-123_T2w.nii.gz
│ └───sub-123_FLAIR.nii.gz
│───dwi
│ │───sub-123_dwi.nii.gz
│ └───sub-123_md.nii.gz
│───swi
│ └───sub-123_swi.nii.gz
└───perf
└───sub-123_cbf.nii.gz
│───sub-01
│ │───anat
│ │ │───sub-01_T1w.nii.gz
│ │ │───sub-01_ce-gadolinium_T1w.nii.gz
│ │ │───sub-01_T2w.nii.gz
│ │ └───sub-01_FLAIR.nii.gz
│ │───dwi
│ │ │───sub-01_dwi.nii.gz
│ │ └───sub-01_md.nii.gz
│ │───swi
│ │ └───sub-01_swi.nii.gz
│ └───perf
│ └───sub-01_cbf.nii.gz
└───sub-02
. │───anat
. │ │───sub-02_T1w.nii.gz
. │ │───sub-02_ce-gadolinium_T1w.nii.gz
│ │───sub-02_T2w.nii.gz
│ └───sub-02_FLAIR.nii.gz
│───dwi
│ │───sub-02_dwi.nii.gz
│ └───sub-02_md.nii.gz
│───swi
│ └───sub-02_swi.nii.gz
└───perf
└───sub-02_cbf.nii.gz
```

#### Processed directory
#### Output directory
```bash
Enter input image directory: /path/to/processed_data_dir
Enter output image directory: /path/to/output_dir
```
The processed image directory (`processed_dir`) is where ALFE writes all its output to.
The output image directory (`output_dir`) is where pyALFE writes all its output to.
It can be any valid path in filesystem that user have write access to.
This config value can be overwritten when calling `pyalfe run` via `-pd` or `--processed-dir` option.
This config value can be overwritten when calling `pyalfe run` via `-od` or `--output-dir` option.


#### Modalities
```bash
Expand Down Expand Up @@ -173,7 +191,7 @@ image processor to use (c3d, nilearn) [c3d]:
```
Currently, pyalfe can be configures to use either Convert3D (a.k.a. c3d) or Nilearn for image processing tasks.
The default is Convert3d aka c3d. In other to use c3d,
you have to download it using the [download command](#download-models-and-tools).
you have to download it using the [download command](#download-models).
To use Nilearn, you do not need to run any extra command since it is already installed when you install pyalfe.
This config value can be overwritten when calling `pyalfe run` via `-ip` or `--image_processing` option.

Expand All @@ -182,10 +200,18 @@ This config value can be overwritten when calling `pyalfe run` via `-ip` or `--i
image registration to use (greedy, ants) [greedy]:
```
Currently, pyalfe can be configures to use either greedy or ants for image registration tasks. The default is greedy.
In other to use greedy, you have to download it using the [download command](#download-models-and-tools). To use ants,
In other to use greedy, you have to download it using the [download command](#download-models). To use ants,
install pyalfe with ants support ``pip install pyalfe[ants]``.
This config value can be overwritten when calling `pyalfe run` via `-ir` or `--image-registration` option.

#### Dierctory Data Structure
```bash
data directory structure (press enter for default) (alfe, bids) [alfe]:
```

The directory structure that pyALFE expects in the input directory and will follow when creating the output. See [Inupt directory](#input-directory) for information on ALFE and BIDS.
This config value can be overwritten when calling `payalfe run` via `-dds` or `--data-dir-structure` option.

### Running the pipeline
To run PyALFE for an accession

Expand Down
13 changes: 10 additions & 3 deletions docs/guides/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ PyALFE can be configured to use either [Greedy](https://greedy.readthedocs.io/en
Similarly, PyALFE can be configured to use [Convert3D](https://sourceforge.net/p/c3d/git/ci/master/tree/doc/c3d.md) or python native library [Nilearn](https://nilearn.github.io/stable/index.html) for image processing tasks.
To use Greedy and Convert3d, these command line tools should be downloaded using the [download command](#download-models-and-tools).

### Option 1: Pypi installation
You can install pyalfe by running:
```bash
pip install pyalfe
```

You can either instal pyalfe in [development mode](#development-mode-installation) or [build and install](#build-and-install).
### Option 1: Development mode installation
You can either install pyalfe from source after cloning the repo using [development mode](#development-mode-installation) or [build and install](#build-and-install).

### Option 2: Development mode installation

First update the setuptools
```bash
Expand All @@ -19,6 +25,7 @@ pip install --upgrade setuptools
Run the following command in the parent pyalfe directory:

```bash

pip install -e .
```

Expand All @@ -32,7 +39,7 @@ pip install --upgrade build
Run the following commands in the parent pyalfe directory to build the whl file and install pyalfe
```bash
python -m build
pip install dist/pyalfe-0.0.1-py3-none-any.whl
pip install dist/pyalfe-*-py3-none-any.whl
```

### Download models
Expand Down
4 changes: 2 additions & 2 deletions docs/guides/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ mkdir -p pyalfe-output

## Installation

Create a python virtual environment and activate
Create a python virtual environment and activate it
```bash
python3 -m venv venv
source venv/bin/activate
Expand All @@ -41,5 +41,5 @@ pyalfe download models

## Run
```bash
pyalfe run UPENNGBM0000511 --input-dir alfe/input --output-dir alfe/output
pyalfe run UPENNGBM0000511 --input-dir pyalfe-test-data --output-dir pyalfe-output
```
94 changes: 62 additions & 32 deletions docs/guides/usage.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,21 @@
## Usage

We recommend running the configure command first to create a configuration file for the pipeline.
However, you can skip to [Running the pipeline](#running-the-pipeline) section if you prefer to provide all the required configuration through command line
options.
### Configuration
To configrue the PyALFE pipeline you should run:
```bash
pyalfe configure
```
which prompt the you to enter the following required configurations:

#### Classified directory
#### Input directory
```bash
Enter input image directory: /path/to/my_mri_data
```
The classified directory (`classified_dir`) is the input directory to PyALFE and should be organized by accessions (or session ids). Inside the directory for each accession there should be a directory for each available modality.
Here is an example that follow ALFE default structure:

```
my_mri_data
│───anat
│ │───sub-123_T1w.nii.gz
│ │───sub-123_T2w.nii.gz
│ └───sub-123_FLAIR.nii.gz
│───dwi
│ │───sub-123_dwi.nii.gz
│ └───sub-123_md.nii.gz
│───swi
│ └───sub-123_swi.nii.gz
└───perf
└───sub-123_cbf.nii.gz
```
To use this directory the user should provide `path/to/my_mri_data` as the classified directory. This config value can be overwritten when calling `pyalfe run` via `-cd` or `--classified-dir` option.

pyALFE also supports BIDS directories. Here is an example of input dir organized in BIDS format:
The input directory (`input_dir`) contains the images that will be processed by PyALFE and should be organized by accessions (or session ids). Inside the directory for each accession there should be a directory for each available modality.
Here is an example that follows ALFE default structure:
```
my_mri_data
Expand All @@ -46,9 +29,10 @@ my_mri_data
│ │ └── FLAIR.nii.gz
│ │───ADC
│ │ └── ADC.nii.gz
│ └───T2
│ └── T2.nii.gz
│ │───T2
│ │ └── T2.nii.gz
│ └───CBF
│ └── CBF.nii.gz
└───12356
. │
. │───T1
Expand All @@ -62,13 +46,51 @@ my_mri_data
└───T2
└── T2.nii.gz
```
#### Processed directory
To use this directory the user should provide `path/to/my_mri_data` as the input directory. This config value can be overwritten when calling `pyalfe run` via `-id` or `--input-dir` option.

pyALFE also supports BIDS directories. Here is an example of input dir organized in BIDS format:

```
my_mri_data
│───sub-01
│ │───anat
│ │ │───sub-01_T1w.nii.gz
│ │ │───sub-01_ce-gadolinium_T1w.nii.gz
│ │ │───sub-01_T2w.nii.gz
│ │ └───sub-01_FLAIR.nii.gz
│ │───dwi
│ │ │───sub-01_dwi.nii.gz
│ │ └───sub-01_md.nii.gz
│ │───swi
│ │ └───sub-01_swi.nii.gz
│ └───perf
│ └───sub-01_cbf.nii.gz
└───sub-02
. │───anat
. │ │───sub-02_T1w.nii.gz
. │ │───sub-02_ce-gadolinium_T1w.nii.gz
│ │───sub-02_T2w.nii.gz
│ └───sub-02_FLAIR.nii.gz
│───dwi
│ │───sub-02_dwi.nii.gz
│ └───sub-02_md.nii.gz
│───swi
│ └───sub-02_swi.nii.gz
└───perf
└───sub-02_cbf.nii.gz
```

#### Output directory
```bash
Enter input image directory: /path/to/processed_data_dir
Enter output image directory: /path/to/output_dir
```
The processed image directory (`processed_dir`) is where ALFE writes all its output to.
The output image directory (`output_dir`) is where pyALFE writes all its output to.
It can be any valid path in filesystem that user have write access to.
This config value can be overwritten when calling `pyalfe run` via `-pd` or `--processed-dir` option.
This config value can be overwritten when calling `pyalfe run` via `-od` or `--output-dir` option.


#### Modalities
```bash
Expand Down Expand Up @@ -102,7 +124,7 @@ image processor to use (c3d, nilearn) [c3d]:
```
Currently, pyalfe can be configures to use either Convert3D (a.k.a. c3d) or Nilearn for image processing tasks.
The default is Convert3d aka c3d. In other to use c3d,
you have to download it using the [download command](#download-models-and-tools).
you have to download it using the [download command](#download-models).
To use Nilearn, you do not need to run any extra command since it is already installed when you install pyalfe.
This config value can be overwritten when calling `pyalfe run` via `-ip` or `--image_processing` option.

Expand All @@ -111,10 +133,18 @@ This config value can be overwritten when calling `pyalfe run` via `-ip` or `--i
image registration to use (greedy, ants) [greedy]:
```
Currently, pyalfe can be configures to use either greedy or ants for image registration tasks. The default is greedy.
In other to use greedy, you have to download it using the [download command](#download-models-and-tools). To use ants,
In other to use greedy, you have to download it using the [download command](#download-models). To use ants,
install pyalfe with ants support ``pip install pyalfe[ants]``.
This config value can be overwritten when calling `pyalfe run` via `-ir` or `--image-registration` option.

#### Dierctory Data Structure
```bash
data directory structure (press enter for default) (alfe, bids) [alfe]:
```

The directory structure that pyALFE expects in the input directory and will follow when creating the output. See [Inupt directory](#input-directory) for information on ALFE and BIDS.
This config value can be overwritten when calling `payalfe run` via `-dds` or `--data-dir-structure` option.

### Running the pipeline
To run PyALFE for an accession

Expand Down
4 changes: 2 additions & 2 deletions docs/intro.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# PyALFE

PyALFE is an open-source python Automated Lesion and Feature Extraction pipeline.
It consumes a clinical brain MRI study consisting of various pulse sequences
PyALFE is an open-source python pipeline for Automated Lesion and Feature Extraction.
It can process a clinical brain MRI study consisting of various sequences
such as T1, T1 post contrast, FLAIR, T2, ADC, and CBF. It performs
pre-processing, inter-seq registration, template registration, segmentation,
and quantification to generate a set of numerical human-interpretable features
Expand Down
18 changes: 13 additions & 5 deletions pyalfe/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from dependency_injector import containers, providers

from pyalfe.data_structure import DefaultALFEDataDir, Modality
from pyalfe.data_structure import DefaultALFEDataDir, BIDSDataDir, Modality
from pyalfe.image_processing import Convert3DProcessor, NilearnProcessor
from pyalfe.image_registration import GreedyRegistration, AntsRegistration
from pyalfe.inference import NNUnetV2
Expand Down Expand Up @@ -34,10 +34,18 @@ class Container(containers.DeclarativeContainer):
logger = logging.getLogger('Container')
config = providers.Configuration()

pipeline_dir = providers.Singleton(
DefaultALFEDataDir,
output_dir=config.options.output_dir,
input_dir=config.options.input_dir,
pipeline_dir = providers.Selector(
config.options.data_dir_structure,
alfe=providers.Singleton(
DefaultALFEDataDir,
output_dir=config.options.output_dir,
input_dir=config.options.input_dir,
),
bids=providers.Singleton(
BIDSDataDir,
output_dir=config.options.output_dir,
input_dir=config.options.input_dir,
),
)

image_processor = providers.Selector(
Expand Down
2 changes: 1 addition & 1 deletion pyalfe/data_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ def get_output_image(
'sub-{subject}[/ses-{session}]/{datatype}/sub-{subject}'
'[_ce-{ceagent}][_space-{space}][_desc-{desc}]'
'_{suffix<T1w|T2w|FLAIR|swi|asl|cbf|md|mask|dseg|probseg>}'
'{extension<.nii|.nii.gz>|.nii.gz}'
'{extension<.nii|.nii.gz|.mat>|.nii.gz}'
]
entities = {
'subject': accession,
Expand Down
Loading

0 comments on commit 7cbe91c

Please sign in to comment.