Skip to main content

Detect and visualize intracranial electrode locations from CT and MR scans.

Project description

๐Ÿง  ElecMap: Intracranial Electrode Localization Toolkit

Python Version License PyPI version

Automated detection and visualization of intracranial electrodes from CT/MR scans for neurosurgical planning and research.


๐Ÿ“ Table of Contents


๐Ÿ“˜ Introduction

ElecMap is a Python package designed for the automated detection of intracranial electrodes from CT and MR brain scans. It accurately identifies and localizes electrode centroids within medical imaging data. The package includes a visualization tool to help users review and display the detected electrode locations.

It leverages powerful libraries such as:

  • SimpleITK for image processing
  • nipype for interfacing with FSL's BET
  • matplotlib for report generation

๐ŸŒŸ Key Features

  • โœ… Precise Electrode Mapping: Localizes electrode centroids in 3D space
  • ๐Ÿ”„ Multi-Modal Integration: Combines CT and MR data for improved anatomical accuracy
  • ๐Ÿ“„ Clinical-Grade Reports: Generates standardized HTML reports for documentation
  • ๐Ÿšซ Smart Filtering: Automatic outlier rejection and duplicate detection
  • โš™๏ธ Fallback Mode: Works with CT-only if FSL or MR scan is unavailable

๐Ÿ“ฆ Installation

1. Install directly from PyPI (recommended):

pip install elecmap

2. Clone the repository from GitHub:

git clone https://github.com/saberm2837/ElecMap.git
cd ElecMap

๐Ÿ”” Note: FSL must be installed and accessible in your system PATH to use MR-based skull stripping features. If unavailable, ElecMap will still perform CT-only detection with a warning.


๐Ÿ› ๏ธ Usage

1. Electrode Detection

from elecmap import detect_electrodes
import os

# Input files
ct_file = '/path/to/sample_ct.nii'
mr_file = '/path/to/sample_mr.nii'  # Optional if FSL is unavailable

# Set FSL environment variables if needed
if 'FSLDIR' not in os.environ:
    os.environ['FSLDIR'] = '/opt/fsl'
    os.environ['FSLOUTPUTTYPE'] = 'NIFTI_GZ'

# Run detection
detect_electrodes(ct_file, mr_file)

This function performs:

  • STEP 1: Load CT and MR images
  • STEP 2: Skull-strip MR using FSL BET (optional)
  • STEP 3: Apply MR brain mask to CT (optional)
  • STEP 4: Detect candidate electrodes from CT intensities
  • STEP 5: Eliminate outliers
  • STEP 6: Save coordinates to
    processed_scans/electrodes_sample_ct.json

โœ… Tip: If no MR image or FSL is available, simply pass None as the second argument or omit it.


2. Electrode Visualization

from elecmap import display_electrode_locations

# Input files
ct_file = '/path/to/sample_ct.nii'
json_file = 'processed_scans/electrodes_sample_ct.json'

# Generate HTML report
display_electrode_locations(ct_file, json_file)

This function:

  • Overlays detected electrodes on axial, sagittal, and coronal slices
  • Saves an interactive HTML report to: reports/report_sample_ct.html

โœ… Sample Output

Console output (detection):

[12:52:13] STEP 1: Loading images ...
CT array shape: (481, 650, 529)
voxel intensity range: -4734.0 to 31743.0
Z-axis bounds: 38 to 443
Y-axis bounds: 52 to 598
X-axis bounds: 42 to 487

[12:52:15] STEP 2: Performing skull stripping on MR using FSL BET ...
Skull-stripped MR image saved at: /data/processed_scans/sample_mr_bet.nii.gz
Brain mask saved at: /data/processed_scans/sample_mr_bet_mask.nii.gz

[12:52:31] STEP 3: Loading the generated MR brain mask and applying it to the CT scan ...
MR brain mask loaded successfully.

[12:52:31] Dilating the brain mask by 30 voxels ...
Successfully Skull stripped the CT scan.

[12:52:52] STEP 4: Compute the centroid of electrodes ...
Detected total number of potential electrodes: 142

[12:52:53] STEP 5: Eliminate outliers from the list of potential electrodes ...
Processing electrodes:  52%|โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–    | 74/142 [00:00<00:00, 733.90it/s]
Electrode outside margin 1: physical = (29.92, -35.9, -83.28), voxel = (339, 414, 31)
Electrode outside margin 2: physical = (-27.22, -37.28, -88.78), voxel = (196, 417, 18)
Processing electrodes: 100%|โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ| 142/142 [00:00<00:00, 375.20it/s]
Electrode outside margin 3: physical = (-27.55, -38.4, -87.82), voxel = (195, 420, 20)
Electrode outside margin 4: physical = (-27.75, -38.8, -87.42), voxel = (195, 421, 21)
Electrode outside margin 5: physical = (-25.96, -38.0, -88.62), voxel = (199, 419, 18)
Electrode outside margin 6: physical = (-26.36, -38.4, -88.22), voxel = (198, 420, 19)
Electrode outside margin 7: physical = (26.76, -35.6, -83.43), voxel = (331, 413, 31)
Final electrode count after eliminating 7 potential outliers: 135

[12:52:53] STEP 6: Saving electrode coordinates to the disk ...
Electrode coordinates successfully saved to: processed_scans/electrodes_sample_ct.json

Console output (visualization):

Generating report: 100%|โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ| 135/135 [00:26<00:00,  5.01it/s]
HTML report saved to: reports/report_sample_ct.html

Report preview (example images): ct_image_01 ct_image_02 ct_image_03 ct_image_04 ct_image_05


๐Ÿ“‚ Project Structure

ElecMap/
โ”œโ”€โ”€ src/
|   โ””โ”€โ”€ elecmap/
โ”‚       โ”œโ”€โ”€ __init__.py
โ”‚       โ”œโ”€โ”€ electrode_detection.py
โ”‚       โ””โ”€โ”€ electrode_visualization.py
โ”œโ”€โ”€ examples/
โ”‚   โ””โ”€โ”€ run_elecmap_demo.py
โ”œโ”€โ”€ processed_scans/
โ”‚   โ””โ”€โ”€ (auto-generated outputs)
โ”œโ”€โ”€ reports/
โ”‚   โ””โ”€โ”€ (auto-generated reports)
โ”œโ”€โ”€ pyproject.toml
โ”œโ”€โ”€ requirements.txt
โ”œโ”€โ”€ LICENSE
โ””โ”€โ”€ README.md

๐Ÿ“‹ JSON Output Format

The output JSON contains a list of detected electrode centroids:

[
  {
    "physical_mm": [51.74, -40.14, -2.88],
    "voxel_coords": [394, 324, 250]
  },
  ...
]
  • physical_mm: electrode location in real-world space (millimeters)
  • voxel_coords: electrode position in image voxel space (indices)

๐Ÿ”ฌ Use Cases

  • Epilepsy and tumor surgery planning
  • Post-operative SEEG or ECoG contact localization
  • Research in brain-computer interfaces (BCIs)
  • Electrode tracking across imaging sessions
  • Quality control of electrode placement

๐Ÿงช Dependencies

  • Python 3.8+

  • Python libraries:

    • SimpleITK
    • matplotlib
    • numpy
    • termcolor
    • tqdm
    • nipype (for FSL integration)
  • System dependency:

Install all Python dependencies with:

pip install -r requirements.txt

โš ๏ธ Without FSL, only CT-based detection is performed. A warning will be printed.


๐Ÿงพ License

This project is licensed under the MIT License.


๐Ÿค Contributing

Feedback and contributions are welcome!
Please open an issue to report bugs, request features, or contribute code.


๐Ÿ“ซ Contact

Created by: Mohammad Saber
๐Ÿ“ง Email: mohammadsaber@gmail.com
๐ŸŒ GitHub: https://github.com/saberm2837/ElecMap


๐Ÿ“š Citation

If you use ElecMap in your research, please cite:

@software{ElecMap2025,
  author = {Mohammad Saber},
  title = {ElecMap: Intracranial Electrode Localization Toolkit},
  year = {2025},
  url = {https://github.com/saberm2837/ElecMap}
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

elecmap-0.1.1.tar.gz (15.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

elecmap-0.1.1-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

Details for the file elecmap-0.1.1.tar.gz.

File metadata

  • Download URL: elecmap-0.1.1.tar.gz
  • Upload date:
  • Size: 15.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.10.13

File hashes

Hashes for elecmap-0.1.1.tar.gz
Algorithm Hash digest
SHA256 31b4ca7119f017a2563eddb4160fd0aae75c3739035c0d04767bd654bcb77edf
MD5 8a1503a68c9cad4260536b3df7a658e7
BLAKE2b-256 c19e4d9f78c4086410b9c7cdde7d190ba4923ee5bab4566a9edfc61cdc7ce81f

See more details on using hashes here.

File details

Details for the file elecmap-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: elecmap-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 12.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.10.13

File hashes

Hashes for elecmap-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c041e432bb9b323e9d8dd8f202c562e09e62a8bad052eb834671599cf528e55f
MD5 bd876d615e62dec335b4ebb4435f7705
BLAKE2b-256 f427e19a2e09b0e475560bb325f99eb7dca2aee69faa97e78146a9d748e09fc3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page