-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #254 from juaml/feat/workdirmanager
[ENH]: Introduce `WorkDirManager`
- Loading branch information
Showing
15 changed files
with
535 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Introduce :class:`junifer.pipeline.WorkDirManager` singleton class to manage working and temporary directories across pipeline by `Synchon Mandal`_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,21 +4,19 @@ | |
# Federico Raimondo <[email protected]> | ||
# License: AGPL | ||
|
||
import shutil | ||
import subprocess | ||
import tempfile | ||
import typing | ||
from functools import lru_cache | ||
from pathlib import Path | ||
from typing import TYPE_CHECKING, Any, Dict, Optional, Tuple, Union | ||
|
||
import nibabel as nib | ||
import numpy as np | ||
from nilearn import image as nimg | ||
from scipy.fft import fft, fftfreq | ||
|
||
from ...pipeline import WorkDirManager | ||
from ...pipeline.singleton import singleton | ||
from ...utils import logger, raise_error | ||
from ..utils import singleton | ||
|
||
|
||
if TYPE_CHECKING: | ||
|
@@ -41,19 +39,24 @@ class ALFFEstimator: | |
use_afni : bool | ||
Whether to use afni for computation. If False, will use python. | ||
Attributes | ||
---------- | ||
temp_dir_path : pathlib.Path | ||
Path to the temporary directory for assets storage. | ||
""" | ||
|
||
def __init__(self) -> None: | ||
self._file_path = None | ||
# Create temporary directory for intermittent storage of assets during | ||
# computation via afni's 3dReHo | ||
self.temp_dir_path = Path(tempfile.mkdtemp()) | ||
# computation via afni's 3dRSFC | ||
self.temp_dir_path = None | ||
|
||
def __del__(self) -> None: | ||
"""Cleanup.""" | ||
print("Cleaning up temporary directory...") | ||
# Delete temporary directory and ignore errors for read-only files | ||
shutil.rmtree(self.temp_dir_path, ignore_errors=True) | ||
if self.temp_dir_path is not None: | ||
WorkDirManager().delete_tempdir(self.temp_dir_path) | ||
|
||
@staticmethod | ||
def _run_afni_cmd(cmd: str) -> None: | ||
|
@@ -123,6 +126,8 @@ def _compute_alff_afni( | |
If the AFNI commands fails due to some issues | ||
""" | ||
# Note: self.temp_dir_path is sure to exist before proceeding, so | ||
# types checks are ignored further on. | ||
|
||
# Save niimg to nii.gz | ||
nifti_in_file_path = self.temp_dir_path / "input.nii" | ||
|
@@ -162,7 +167,7 @@ def _compute_alff_afni( | |
self._run_afni_cmd(convert_cmd) | ||
|
||
# Cleanup intermediate files | ||
for fname in self.temp_dir_path.glob("temp_*"): | ||
for fname in self.temp_dir_path.glob("temp_*"): # type: ignore | ||
fname.unlink() | ||
|
||
# Load niftis | ||
|
@@ -268,6 +273,8 @@ def _compute( | |
fALFF map. | ||
""" | ||
if use_afni: | ||
# Create new temporary directory before using AFNI | ||
self.temp_dir_path = WorkDirManager().get_tempdir(prefix="falff") | ||
output = self._compute_alff_afni( | ||
data=data, | ||
highpass=highpass, | ||
|
@@ -318,8 +325,8 @@ def fit_transform( | |
# Clear the cache | ||
self._compute.cache_clear() | ||
# Clear temporary directory files | ||
for file_ in self.temp_dir_path.iterdir(): | ||
file_.unlink(missing_ok=True) | ||
if self.temp_dir_path is not None: | ||
WorkDirManager().delete_tempdir(self.temp_dir_path) | ||
# Set the new file path | ||
self._file_path = bold_path | ||
else: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.