Skip to content

Commit

Permalink
bidsify output
Browse files Browse the repository at this point in the history
  • Loading branch information
Remi-Gau committed Dec 14, 2023
1 parent d707ac6 commit 547d888
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 19 deletions.
28 changes: 23 additions & 5 deletions giga_connectome/postprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ def run_postprocessing_dataset(
Smoothing kernel size, passed to nilearn masker.
output_path:
Full path to output file, named in the following format:
output_dir / atlas-<atlas>_desc-<strategy_name>.h5
Full path to output directory.
Will be used to name the output file in the following format:
[sub-<sub>][_ses-<ses>_]atlas-<atlas>_desc-<strategy_name>_relmat.h5
analysis_level : str
Level of analysis, either "participant" or "group".
Expand Down Expand Up @@ -105,6 +106,16 @@ def run_postprocessing_dataset(
)
# parse file name
subject, session, specifier = utils.parse_bids_name(img.path)

connectome_path = output_path / subject
if session:
connectome_path = connectome_path / session
filename = utils.output_filename(
Path(img.filename).stem, atlas, strategy["name"]
)
connectome_path = connectome_path / "func" / filename
connectome_path = utils.check_path(connectome_path, verbose=True)

for desc, masker in atlas_maskers.items():
attribute_name = f"{subject}_{specifier}_atlas-{atlas}_desc-{desc}"
if not denoised_img:
Expand All @@ -126,8 +137,8 @@ def run_postprocessing_dataset(
connectomes[desc].append(correlation_matrix)

# dump to h5
flag = _set_file_flag(output_path)
with h5py.File(output_path, flag) as f:
flag = _set_file_flag(connectome_path)
with h5py.File(connectome_path, flag) as f:
group = _fetch_h5_group(f, subject, session)
timeseries_dset = group.create_dataset(
f"{attribute_name}_timeseries", data=time_series_atlas
Expand All @@ -141,11 +152,18 @@ def run_postprocessing_dataset(

if analysis_level == "group":
print("create group connectome")
connectome_path = (
output_path
/ "group"
/ utils.output_filename("", atlas, strategy["name"])
)
connectome_path = utils.check_path(connectome_path, verbose=True)
print(connectome_path)
for desc in connectomes:
average_connectome = np.mean(
np.array(connectomes[desc]), axis=0
).astype(np.float32)
with h5py.File(output_path, "a") as f:
with h5py.File(connectome_path, "a") as f:
f.create_dataset(
f"atlas-{atlas}_desc-{desc}_connectome",
data=average_connectome,
Expand Down
1 change: 0 additions & 1 deletion giga_connectome/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ def test_help(capsys):

@pytest.mark.smoke
def test_smoke(tmp_path, capsys):

bids_dir = resource_filename(
"giga_connectome",
"data/test_data/ds000017-fmriprep22.0.1-downsampled-nosurface",
Expand Down
67 changes: 66 additions & 1 deletion giga_connectome/utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
from typing import List, Tuple, Union
from __future__ import annotations

from typing import List, Tuple, Union, Any
import json
from pathlib import Path

from nilearn.interfaces.bids import parse_bids_filename
from bids.layout import Query
from bids import BIDSLayout

from giga_connectome import __version__


def get_bids_images(
subjects: List[str],
Expand Down Expand Up @@ -184,6 +190,8 @@ def check_path(path: Path, verbose=True):
ext = path.suffix
path_parent = path.parent

path_parent.mkdir(parents=True, exist_ok=True)

if path.exists():
similar_paths = [
str(p).replace(ext, "")
Expand All @@ -199,3 +207,60 @@ def check_path(path: Path, verbose=True):
if verbose:
print(f"Specified path already exists, using {path} instead.")
return path


def create_ds_description(output_dir: Path) -> None:
"""Create a dataset_description.json file."""
ds_desc: dict[str, Any] = {
"BIDSVersion": "1.9.0",
"License": None,
"Name": None,
"ReferencesAndLinks": [],
"Authors": [
"Foo",
"Bar",
],
"DatasetDOI": None,
"DatasetType": "derivative",
"GeneratedBy": [
{
"Name": "giga_connectome",
"Version": __version__,
"CodeURL": "https://github.com/SIMEXP/giga_connectome.git",
}
],
"HowToAcknowledge": (
"Please refer to our repository: "
"https://github.com/SIMEXP/giga_connectome.git."
),
}
with open(output_dir / "dataset_description.json", "w") as f:
json.dump(ds_desc, f, indent=4)


def create_sidecar(output_path: Path) -> None:
"""Create a JSON sidecar for the connectivity data."""
metadata: dict[str, Any] = {
"NodeFiles": "REQUIRED",
"Measure": "Pearson correlation",
"MeasureDescription": "Pearson correlation",
"Weighted": "REQUIRED",
"Directed": False,
"ValidDiagonal": True,
"StorageFormat": "Full",
"NonNegative": "",
"Code": "https://github.com/SIMEXP/giga_connectome.git",
}
with open(output_path, "w") as f:
json.dump(metadata, f, indent=4)


def output_filename(source_file: str, atlas: str, strategy: str) -> str:
root = source_file.split("_")[:-1]
root = "_".join(root)
if len(root) > 0:
root += "_"
return (
f"{root}atlas-{atlas}_meas-PearsonCorrelation_desc-{strategy}"
"_relmat.h5"
)
21 changes: 9 additions & 12 deletions giga_connectome/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ def workflow(args):
)
print("Indexing BIDS directory")

utils.create_ds_description(output_dir)
utils.create_sidecar(
output_dir
/ f"meas-PearsonCorrelation_desc-{args.denoise_strategy}_relmat.json"
)

# create subject ts and connectomes
# refactor the two cases into one

Expand All @@ -51,11 +57,7 @@ def workflow(args):
group_mask, resampled_atlases = generate_gm_mask_atlas(
working_dir, atlas, template, subj_data["mask"]
)
connectome_path = output_dir / (
f"sub-{subject}_atlas-{atlas['name']}"
f"_desc-{strategy['name']}.h5"
)
connectome_path = utils.check_path(connectome_path, verbose=True)

print("Generate subject level connectomes")
run_postprocessing_dataset(
strategy,
Expand All @@ -64,7 +66,7 @@ def workflow(args):
group_mask,
standardize,
smoothing_fwhm,
connectome_path,
output_dir,
analysis_level,
calculate_average_correlation,
)
Expand All @@ -77,11 +79,6 @@ def workflow(args):
group_mask, resampled_atlases = generate_gm_mask_atlas(
working_dir, atlas, template, subj_data["mask"]
)
connectome_path = (
output_dir / f"atlas-{atlas['name']}_desc-{strategy['name']}.h5"
)
connectome_path = utils.check_path(connectome_path, verbose=True)
print(connectome_path)
print("Generate subject level connectomes")
run_postprocessing_dataset(
strategy,
Expand All @@ -90,7 +87,7 @@ def workflow(args):
group_mask,
standardize,
smoothing_fwhm,
connectome_path,
output_dir,
analysis_level,
calculate_average_correlation,
)
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 547d888

Please sign in to comment.