Skip to content

Commit cb85f61

Browse files
committed
add dcm2niix binaries
1 parent 7275c27 commit cb85f61

File tree

8 files changed

+335
-14
lines changed

8 files changed

+335
-14
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
name: Test Included Binaries
2+
on:
3+
push:
4+
branches:
5+
- main
6+
- add-dcm2niix-binaries
7+
pull_request:
8+
branches:
9+
- main
10+
- add-dcm2niix-binaries
11+
workflow_dispatch:
12+
inputs:
13+
debug_enabled:
14+
type: boolean
15+
description: 'Run with tmate debugging enabled'
16+
required: false
17+
default: false
18+
19+
jobs:
20+
test-included-binaries:
21+
runs-on: ${{ matrix.os }}
22+
strategy:
23+
matrix:
24+
os: [ubuntu-latest, macos-latest, windows-latest]
25+
python: ['3.12']
26+
fail-fast: false
27+
28+
steps:
29+
- name: Checkout Repository
30+
uses: actions/checkout@v4
31+
32+
- name: Set up Python ${{ matrix.python }}
33+
uses: actions/setup-python@v4
34+
with:
35+
python-version: ${{ matrix.python }}
36+
cache: 'pip'
37+
cache-dependency-path: 'pypet2bids/pyproject.toml'
38+
39+
- name: Install UV
40+
if: matrix.os != 'windows-latest'
41+
run: curl -LsSf https://astral.sh/uv/install.sh | sh && echo "$HOME/.cargo/bin" >> $GITHUB_PATH
42+
43+
- name: Install UV (Windows)
44+
if: matrix.os == 'windows-latest'
45+
run: |
46+
powershell -c "irm https://astral.sh/uv/install.ps1 | iex"
47+
echo "$env:USERPROFILE\.cargo\bin" >> $env:GITHUB_PATH
48+
49+
- name: Debug Session
50+
uses: mxschmitt/action-tmate@v3
51+
if: ${{ github.event_name == 'workflow_dispatch' && inputs.debug_enabled }}
52+
timeout-minutes: 15
53+
with:
54+
limit-access-to-actor: true
55+
56+
- name: Run Tests
57+
run: |
58+
cd pypet2bids
59+
uv run pytest tests/test_included_binaries.py -v

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,5 @@ ecat_testing/**/*.nii*
6767

6868
matlab/SiemensHRRTparameters.txt
6969

70+
dcm2niix
71+
dcm2niix.exe

pypet2bids/pypet2bids/dcm2niix4pet.py

Lines changed: 64 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import subprocess
2121
import pandas as pd
2222
from os.path import join
23+
from os import chmod
2324
from os import listdir, walk, environ
2425
from pathlib import Path
2526
import json
@@ -29,6 +30,8 @@
2930
import shutil
3031
import argparse
3132
import importlib
33+
import zipfile
34+
import stat
3235

3336
try:
3437
import helper_functions
@@ -172,6 +175,50 @@ def collect_date_time_from_file_name(file_name):
172175
return date, time
173176

174177

178+
def use_included_binary():
179+
# get system platform
180+
systems_system = system().lower()
181+
module_folder = Path(__file__).parent.resolve()
182+
binary_folder = module_folder / "dcm2niix_binaries"
183+
dcm2niix_zip = "dcm2niix_{}.zip"
184+
if "darwin" in systems_system:
185+
zipped = binary_folder / dcm2niix_zip.format("mac")
186+
elif "linux" in systems_system:
187+
zipped = binary_folder / dcm2niix_zip.format("lnx")
188+
elif "windows" in systems_system:
189+
zipped = binary_folder / dcm2niix_zip.format("win")
190+
else:
191+
raise ValueError(
192+
f"System must be Darwin, Linux, or Windows to use included binaries, got {system()}"
193+
)
194+
195+
# Check if binary already exists
196+
if systems_system == "windows":
197+
dcm2niix_binary = binary_folder / "dcm2niix.exe"
198+
else:
199+
dcm2niix_binary = binary_folder / "dcm2niix"
200+
201+
# Only extract if binary doesn't exist
202+
if not dcm2niix_binary.exists() and zipped.exists():
203+
try:
204+
with zipfile.ZipFile(zipped, "r") as ref:
205+
ref.extractall(zipped.parent)
206+
except zipfile.BadZipFile as e:
207+
logger.error(
208+
f"Unable to extract {zipped}, try manually setting DCM2NIIX_PATH in env or .pypet2bids_config"
209+
)
210+
raise e
211+
212+
# Check to make sure binary is there and set permissions
213+
if dcm2niix_binary.exists():
214+
if systems_system != "windows":
215+
chmod(dcm2niix_binary, stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
216+
helper_functions.modify_config_file("DCM2NIIX_PATH", dcm2niix_binary)
217+
return dcm2niix_binary
218+
else:
219+
return None
220+
221+
175222
class Dcm2niix4PET:
176223
def __init__(
177224
self,
@@ -445,17 +492,21 @@ def check_posix():
445492
dcm2niix_path = helper_functions.check_pet2bids_config()
446493

447494
if not dcm2niix_path:
448-
pkged = "https://github.com/rordenlab/dcm2niix/releases"
449-
instructions = "https://github.com/rordenlab/dcm2niix#install"
450-
no_dcm2niix = f"""Unable to locate Dcm2niix on your system $PATH or using the path specified in
451-
$HOME/.pypet2bidsconfig. Installation instructions for dcm2niix can be found here
452-
{instructions}
453-
and packaged versions can be found at
454-
{pkged}
455-
Alternatively, you can set the path to dcm2niix in the config file at $HOME/.pet2bidsconfig
456-
using the command dcm2niix4pet --set-dcm2niix-path."""
457-
logger.error(no_dcm2niix)
458-
dcm2niix_path = None
495+
# determine if osx or linux
496+
dcm2niix_path = use_included_binary()
497+
498+
if not dcm2niix_path:
499+
pkged = "https://github.com/rordenlab/dcm2niix/releases"
500+
instructions = "https://github.com/rordenlab/dcm2niix#install"
501+
no_dcm2niix = f"""Unable to locate Dcm2niix on your system $PATH or using the path specified in
502+
$HOME/.pypet2bidsconfig. Installation instructions for dcm2niix can be found here
503+
{instructions}
504+
and packaged versions can be found at
505+
{pkged}
506+
Alternatively, you can set the path to dcm2niix in the config file at $HOME/.pet2bidsconfig
507+
using the command dcm2niix4pet --set-dcm2niix-path."""
508+
logger.error(no_dcm2niix)
509+
dcm2niix_path = None
459510

460511
return dcm2niix_path
461512

@@ -465,8 +516,8 @@ def check_for_dcm2niix(self):
465516
466517
:return: status code of the command dcm2niix -h
467518
"""
468-
469-
if system().lower() != "windows":
519+
operating_system = system().lower()
520+
if operating_system != "windows":
470521
dcm2niix_path = self.check_posix()
471522
# fall back and check the config file if it's not on the path
472523
if not dcm2niix_path:
866 KB
Binary file not shown.
972 KB
Binary file not shown.
504 KB
Binary file not shown.

pypet2bids/pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "pypet2bids"
7-
version = "1.4.4"
7+
version = "1.4.5"
88
description = "A python library for converting PET imaging and blood data to BIDS."
99
authors = [
1010
{name = "anthony galassi", email = "[email protected]"}
@@ -78,6 +78,7 @@ packages = ["pypet2bids"]
7878
include = [
7979
"/pypet2bids",
8080
"/README.md",
81+
"/pypet2bids/dcm2niix_binaries/*.zip"
8182
]
8283

8384
[tool.black]

0 commit comments

Comments
 (0)