Skip to content

Commit

Permalink
Merge pull request Deep-MI#18 from engrosamaali91/BrainPrint_TestCases
Browse files Browse the repository at this point in the history
Brain print test cases
  • Loading branch information
m-reuter authored Oct 12, 2023
2 parents c9fadfa + d3d47d7 commit f1fc057
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 1 deletion.
43 changes: 42 additions & 1 deletion brainprint/brainprint.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Definition of the brainprint analysis execution functions.
Definition of the brainprint analysis execution functions..
"""

import shutil
Expand Down Expand Up @@ -359,6 +359,21 @@ def __init__(
test_freesurfer()

def run(self, subject_id: str, destination: Path = None) -> Dict[str, Path]:
"""
Run Brainprint analysis for a specified subject.
Parameters
----------
subject_id : str
The ID of the subject to analyze.
destination : Path, optional
The destination directory for analysis results, by default None.
Returns
-------
Dict[str, Path]
A dictionary containing paths to the generated analysis results.
"""
self._eigenvalues = self._eigenvectors = self._distances = None
subject_dir = validate_subject_dir(self.subjects_dir, subject_id)
destination = create_output_paths(
Expand Down Expand Up @@ -389,12 +404,38 @@ def run(self, subject_id: str, destination: Path = None) -> Dict[str, Path]:
return self.export_results(destination=destination, subject_id=subject_id)

def export_results(self, destination: Path, subject_id: str) -> None:
"""
Export Brainprint analysis results to a CSV file.
Parameters
----------
destination : Path
The destination directory for analysis results.
subject_id : str
The ID of the subject being analyzed.
Returns
-------
None
"""
csv_name = "{subject_id}.brainprint.csv".format(subject_id=subject_id)
csv_path = destination / csv_name
return export_brainprint_results(
csv_path, self._eigenvalues, self._eigenvectors, self._distances
)

def cleanup(self, destination: Path) -> None:
"""
Clean up temporary files generated during the analysis.
Parameters
----------
destination : Path
The destination directory for analysis results.
Returns
-------
None
"""
if not self.keep_temp:
shutil.rmtree(destination / "temp")
65 changes: 65 additions & 0 deletions brainprint/surfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,21 @@ def create_aseg_surface(


def create_aseg_surfaces(subject_dir: Path, destination: Path) -> Dict[str, Path]:
"""
Create surfaces from FreeSurfer aseg labels.
Parameters
----------
subject_dir : Path
Path to the subject's FreeSurfer directory.
destination : Path
Path to the destination directory for saving surfaces.
Returns
-------
Dict[str, Path]
Dictionary of label names mapped to corresponding surface Path objects.
"""
# Define aseg labels

# combined and individual aseg labels:
Expand Down Expand Up @@ -131,6 +146,21 @@ def create_aseg_surfaces(subject_dir: Path, destination: Path) -> Dict[str, Path


def create_cortical_surfaces(subject_dir: Path, destination: Path) -> Dict[str, Path]:
"""
Create cortical surfaces from FreeSurfer labels.
Parameters
----------
subject_dir : Path
Path to the subject's FreeSurfer directory.
destination : Path
Path to the destination directory where the surfaces will be saved.
Returns
-------
Dict[str, Path]
Dictionary mapping label names to associated surface Paths.
"""
cortical_labels = {
"lh-white-2d": "lh.white",
"rh-white-2d": "rh.white",
Expand All @@ -149,6 +179,23 @@ def create_cortical_surfaces(subject_dir: Path, destination: Path) -> Dict[str,
def create_surfaces(
subject_dir: Path, destination: Path, skip_cortex: bool = False
) -> Dict[str, Path]:
"""
Create surfaces based on FreeSurfer labels.
Parameters
----------
subject_dir : Path
Path to the subject's FreeSurfer directory.
destination : Path
Path to the destination directory where the surfaces will be saved.
skip_cortex : bool, optional
If True, cortical surfaces will not be created (default is False).
Returns
-------
Dict[str, Path]
Dict mapping label names to the corresponding Path objects of created surfaces.
"""
surfaces = create_aseg_surfaces(subject_dir, destination)
if not skip_cortex:
cortical_surfaces = create_cortical_surfaces(subject_dir, destination)
Expand All @@ -157,6 +204,24 @@ def create_surfaces(


def read_vtk(path: Path):
"""
Read a VTK file and return a triangular mesh.
Parameters
----------
path : Path
Path to the VTK file to be read.
Returns
-------
TriaMesh
A triangular mesh object representing the contents of the VTK file.
Raises
------
RuntimeError
If there is an issue reading the VTK file or if the file is empty.
"""
try:
triangular_mesh = TriaMesh.read_vtk(path)
except Exception:
Expand Down

0 comments on commit f1fc057

Please sign in to comment.