Skip to content

Commit 9f37771

Browse files
committed
rebase from main and format for func files
1 parent b415b08 commit 9f37771

5 files changed

Lines changed: 48 additions & 50 deletions

File tree

src/rbc/core/functional/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
must be corrected before analysis.
88
"""
99

10-
from .initialization import truncate_trs, scale
10+
from .initialization import scale, truncate_trs
1111
from .motion import generate_motion_reference, motion_correction
1212

13-
__all__ = ["truncate_trs", "scale", "generate_motion_reference", "motion_correction"]
13+
__all__ = ["truncate_trs", "scale", "generate_motion_reference", "motion_correction"]

src/rbc/core/functional/initialization.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
from niwrap import afni
66

7+
78
def truncate_trs(
8-
in_file: Path, output_fname: str, start_tr: int
9+
in_file: Path, output_fname: str, start_tr: int
910
) -> afni.V3dcalcOutputs:
1011
"""Remove first N TRs from BOLD timeseries using AFNI 3dcalc.
1112
@@ -19,17 +20,14 @@ def truncate_trs(
1920
"""
2021
return afni.v_3dcalc(
2122
dataset_a=afni.v_3dcalc_dataset_a_file(
22-
file=in_file,
23-
selectors_=f"[{start_tr}..$]"
23+
file=in_file, selectors_=f"[{start_tr}..$]"
2424
),
2525
expression="a",
2626
prefix=output_fname,
2727
)
2828

2929

30-
def scale(
31-
in_file: Path, scale_factor: float = 0.1
32-
) -> afni.V3drefitOutputs:
30+
def scale(in_file: Path, scale_factor: float = 0.1) -> afni.V3drefitOutputs:
3331
"""Scale BOLD voxel dimensions using AFNI 3drefit.
3432
3533
Args:
@@ -42,4 +40,4 @@ def scale(
4240
return afni.v_3drefit(
4341
in_file=in_file,
4442
xyzscale=scale_factor,
45-
)
43+
)

src/rbc/core/functional/motion.py

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
from niwrap import afni, fsl
77

88

9-
def generate_motion_reference(
10-
in_file: Path, output_fname: str
11-
) -> afni.V3dcalcOutputs:
9+
def generate_motion_reference(in_file: Path, output_fname: str) -> afni.V3dcalcOutputs:
1210
"""Creates reference volume for motion correction by extracting middle volume.
1311
1412
Args:
@@ -18,23 +16,17 @@ def generate_motion_reference(
1816
Returns:
1917
AFNI 3dcalc output object.
2018
"""
19+
total_vols = afni.v_3dinfo(dataset=[in_file], nv=True)
2120

22-
total_vols = afni.v_3dinfo(
23-
dataset=[in_file],
24-
nv=True
25-
)
26-
2721
mid_vol = (int(total_vols.info[0])) // 2
2822

2923
return afni.v_3dcalc(
30-
dataset_a=afni.v_3dcalc_dataset_a_file(
31-
file=in_file,
32-
selectors_=f"[{mid_vol}]"
33-
),
24+
dataset_a=afni.v_3dcalc_dataset_a_file(file=in_file, selectors_=f"[{mid_vol}]"),
3425
expression="a",
3526
prefix=output_fname,
3627
)
3728

29+
3830
def motion_correction(
3931
in_file: Path, ref_file: Path, output_prefix: str
4032
) -> SimpleNamespace:
@@ -46,9 +38,8 @@ def motion_correction(
4638
output_prefix: Prefix for output files.
4739
4840
Returns:
49-
Namespace with paths to motion corrected BOLD, motion parameters, displacement files, and directory to transformation matrices.
41+
Namespace with paths to BOLD motion correction outputs and transformation matrices.
5042
"""
51-
5243
mc_result = fsl.mcflirt(
5344
in_file=in_file,
5445
ref_file=ref_file,
@@ -59,12 +50,14 @@ def motion_correction(
5950
out_file=output_prefix,
6051
)
6152

62-
motion_mat_dir = [d for d in Path(mc_result.root).iterdir() if d.is_dir() and d.suffix == ".mat"]
53+
motion_mat_dir = [
54+
d for d in Path(mc_result.root).iterdir() if d.is_dir() and d.suffix == ".mat"
55+
]
6356

6457
return SimpleNamespace(
65-
bold = Path(mc_result.out_file),
66-
par = Path(mc_result.par_file),
67-
rms_rel = Path(mc_result.rmsrel_files),
68-
rms_abs = Path(mc_result.rmsabs_files),
69-
mat_dir=motion_mat_dir[0]
70-
)
58+
bold=Path(mc_result.out_file),
59+
par=Path(mc_result.par_file),
60+
rms_rel=Path(mc_result.rmsrel_files),
61+
rms_abs=Path(mc_result.rmsabs_files),
62+
mat_dir=motion_mat_dir[0],
63+
)

src/rbc/core/utils.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,17 @@ def get_base_entities(in_file: Path) -> dict[str, str]:
3939
A string-mapping of BIDS entities to values.
4040
"""
4141
file_entities = parse_bids_entities(in_file)
42-
return {k: v for k, v in file_entities.items() if k in ["sub", "ses", "task", "run"]}
42+
return {
43+
k: v for k, v in file_entities.items() if k in ["sub", "ses", "task", "run"]
44+
}
4345

4446

4547
def rename(in_file: str | Path, new_name: str | Path) -> Path:
4648
"""Rename a file, keeping it in the same directory."""
4749
in_file = Path(in_file)
4850
return in_file.rename(in_file.with_name(Path(new_name).name))
4951

52+
5053
def save_directory(in_dir: str | Path, out_dir: str | Path, name: str) -> Path:
5154
"""Save a directory by copying it to a new location.
5255
@@ -62,4 +65,4 @@ def save_directory(in_dir: str | Path, out_dir: str | Path, name: str) -> Path:
6265
raise ValueError(f"Input path {in_dir} is not a directory.")
6366

6467
shutil.copytree(in_dir, target, dirs_exist_ok=True)
65-
return target
68+
return target

src/rbc/workflows/functional.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,45 @@
66
import niwrap_helper
77

88
from rbc.core.common import reorient
9-
from rbc.core.functional import truncate_trs, generate_motion_reference, motion_correction
9+
from rbc.core.functional import (
10+
generate_motion_reference,
11+
motion_correction,
12+
truncate_trs,
13+
)
1014
from rbc.core.utils import get_base_entities, rename, save_directory
1115

1216

13-
1417
def single_session(in_bold: Path, output_dir: Path, start_tr: int = 2) -> None:
1518
"""Workflow for preprocessing functional data.
16-
19+
1720
Args:
1821
in_bold: Input BOLD timeseries to process.
1922
output_dir: Parent output directory to save data to.
2023
start_tr: Number of initial TRs to remove (default: 2).
2124
"""
22-
2325
bids_entities = get_base_entities(in_bold)
2426
bids = partial(niwrap_helper.bids_path, **bids_entities)
25-
27+
2628
reoriented_bold = reorient(
2729
in_file=in_bold,
28-
output_fname=str(bids(desc="reoriented", suffix="bold", ext=".nii.gz"))
30+
output_fname=str(bids(desc="reoriented", suffix="bold", ext=".nii.gz")),
2931
)
3032

3133
truncated_bold = truncate_trs(
3234
in_file=reoriented_bold.out_file,
3335
output_fname=str(bids(suffix="bold", ext=".nii.gz")),
34-
start_tr=start_tr
35-
)
36-
37-
motion_reference = generate_motion_reference(
36+
start_tr=start_tr,
37+
)
38+
39+
motion_reference = generate_motion_reference(
3840
in_file=truncated_bold.output_file,
39-
output_fname=str(bids(suffix="sbref", ext=".nii.gz"))
41+
output_fname=str(bids(suffix="sbref", ext=".nii.gz")),
4042
)
4143

4244
motion_corrected = motion_correction(
4345
in_file=truncated_bold.output_file,
4446
ref_file=motion_reference.output_file,
45-
output_prefix=str(bids())
47+
output_prefix=str(bids()),
4648
)
4749

4850
# Prep files to save
@@ -54,20 +56,22 @@ def single_session(in_bold: Path, output_dir: Path, start_tr: int = 2) -> None:
5456
(motion_corrected.par, "movementParameters", "motion", ".1D"),
5557
(motion_corrected.rms_rel, "relsDisplacement", "motion", ".rms"),
5658
(motion_corrected.rms_abs, "absDisplacement", "motion", ".rms"),
57-
]
59+
]
5860

5961
func_out_dir = output_dir / bids(datatype="func", directory=True)
6062

6163
save_directory(
62-
motion_corrected.mat_dir,
63-
func_out_dir,
64-
bids(desc="motion", suffix="mat")
64+
motion_corrected.mat_dir, func_out_dir, bids(desc="motion", suffix="mat")
6565
)
6666

6767
renamed_files = [
68-
rename(out_file, bids(desc=desc, suffix=suffix, ext=ext) if desc else bids(suffix=suffix, ext=ext))
68+
rename(
69+
out_file,
70+
bids(desc=desc, suffix=suffix, ext=ext)
71+
if desc
72+
else bids(suffix=suffix, ext=ext),
73+
)
6974
for out_file, desc, suffix, ext in outputs
7075
]
7176

7277
niwrap_helper.save(renamed_files, out_dir=func_out_dir)
73-

0 commit comments

Comments
 (0)