Skip to content

Commit

Permalink
Creating behavioral protocol (openMetadataInitiative#52)
Browse files Browse the repository at this point in the history
* protocol

* deling with no task

* behavioral_protocol

* adding test for behavioral_protocols

* adding behavioral_protocol_number to test_bids_examples.py
  • Loading branch information
Peyman-N authored Jun 17, 2024
1 parent 14ed965 commit 89ccbb4
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 12 deletions.
7 changes: 4 additions & 3 deletions bids2openminds/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ def convert(input_path, save_output=False, output_path=None, multiple_files=Fals

subjects_id = bids_layout.get_subjects()

tasks = bids_layout.get_task()

# imprting the dataset description file containing some of the
dataset_description_path = utility.table_filter(layout_df, "description")

Expand All @@ -31,11 +29,14 @@ def convert(input_path, save_output=False, output_path=None, multiple_files=Fals
[subjects_dict, subject_state_dict, subjects_list] = main.create_subjects(
subjects_id, layout_df, bids_layout, collection)

behavioral_protocols, behavioral_protocols_dict = main.create_behavioral_protocol(
bids_layout, collection)

[files_list, file_repository] = main.create_file(
layout_df, input_path, collection)

dataset_version = main.create_dataset_version(
bids_layout, dataset_description, layout_df, subjects_list, file_repository, collection)
bids_layout, dataset_description, layout_df, subjects_list, file_repository, behavioral_protocols, collection)

dataset = main.create_dataset(
dataset_description, dataset_version, collection)
Expand Down
23 changes: 22 additions & 1 deletion bids2openminds/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,26 @@ def create_persons(dataset_description, collection):
return openminds_list


def create_behavioral_protocol(layout, collection):
behavioral_protocols_dict = {}
behavioral_protocols = []
tasks = layout.get_tasks()

if not tasks:
return None, None

for task in tasks:

behavioral_protocol = omcore.BehavioralProtocol(name=task,
internal_identifier=task,
description="To be defined")
behavioral_protocols.append(behavioral_protocol)
behavioral_protocols_dict[task] = behavioral_protocol
collection.add(behavioral_protocol)

return behavioral_protocols, behavioral_protocols_dict


def create_techniques(layout_df):
suffixs = layout_df["suffix"].unique().tolist()
techniques = []
Expand Down Expand Up @@ -126,7 +146,7 @@ def create_openminds_age(data_subject):
return None


def create_dataset_version(bids_layout, dataset_description, layout_df, studied_specimens, file_repository, collection):
def create_dataset_version(bids_layout, dataset_description, layout_df, studied_specimens, file_repository, behavioral_protocols, collection):

# Fetch the dataset type from dataset description file

Expand Down Expand Up @@ -179,6 +199,7 @@ def create_dataset_version(bids_layout, dataset_description, layout_df, studied_
techniques=techniques,
how_to_cite=how_to_cite,
repository=file_repository,
behavioral_protocols=behavioral_protocols
# other_contributions=other_contribution # needs to be a Contribution object
# version_identifier
)
Expand Down
20 changes: 12 additions & 8 deletions test/test_bids_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
import bids2openminds.converter


# Dataset information in following order dataset_label, dataset_subject_number, dataset_subject_state_number, dataset_person_number, dataset_files_number
example_dataset = [("ds003", 13, 13, 2, 58),
("ds000247", 6, 10, 5, 202),
# Dataset information in following order dataset_label, dataset_subject_number, dataset_subject_state_number, dataset_person_number, dataset_files_number, dataset_behavioral_protocol_number
example_dataset = [("ds003", 13, 13, 2, 58, 1),
("ds000247", 6, 10, 5, 202, 2),
# The authors list in 'eeg_cbm' contains non person entities 2 is not correct name (issue raied #43)
("eeg_cbm", 20, 20, 2, 104),
("asl001", 1, 1, 2, 8),
("eeg_cbm", 20, 20, 2, 104, 1),
("asl001", 1, 1, 2, 8, 0),
# Number of files in 'eeg_rest_fmri' is not correct as it doesn't contain files in derivated (issue raied #42)
("eeg_rest_fmri", 3, 3, 6, 46)]
("eeg_rest_fmri", 3, 3, 6, 46, 1)]


@pytest.mark.parametrize("dataset_label, dataset_subject_number, dataset_subject_state_number, dataset_person_number, dataset_files_number", example_dataset)
def test_example_datasets(dataset_label, dataset_subject_number, dataset_subject_state_number, dataset_person_number, dataset_files_number):
@pytest.mark.parametrize("dataset_label, dataset_subject_number, dataset_subject_state_number, dataset_person_number, dataset_files_number, dataset_behavioral_protocol_number", example_dataset)
def test_example_datasets(dataset_label, dataset_subject_number, dataset_subject_state_number, dataset_person_number, dataset_files_number, dataset_behavioral_protocol_number):
test_dir = os.path.join("bids-examples", dataset_label)
bids2openminds.converter.convert(test_dir, save_output=True)
c = Collection()
Expand All @@ -25,6 +25,7 @@ def test_example_datasets(dataset_label, dataset_subject_number, dataset_subject
subject_state_number = 0
person_number = 0
files_number = 0
behavioral_protocol_number = 0

for item in c:
match item.type_:
Expand All @@ -36,8 +37,11 @@ def test_example_datasets(dataset_label, dataset_subject_number, dataset_subject
person_number += 1
case "https://openminds.ebrains.eu/core/File":
files_number += 1
case "https://openminds.ebrains.eu/core/BehavioralProtocol":
behavioral_protocol_number += 1

assert dataset_subject_number == subject_number
assert dataset_subject_state_number == subject_state_number
assert dataset_person_number == person_number
assert dataset_files_number == files_number
assert dataset_behavioral_protocol_number == behavioral_protocol_number
25 changes: 25 additions & 0 deletions test/test_task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import pytest
from bids import BIDSLayout
from openminds import Collection
import os
from bids2openminds.main import create_behavioral_protocol

(datasets, tasks) = ("ds002", ("deterministicclassification",
"mixedeventrelatedprobe", "probabilisticclassification"))


@pytest.fixture
def creating_behavioral_protocols():
c = Collection()
test_dir = os.path.join("bids-examples", datasets)
bids_layout = BIDSLayout(test_dir)
_, behavioral_protocols_dict = create_behavioral_protocol(
bids_layout, c)
return behavioral_protocols_dict


def test_behavioral_protocols(creating_behavioral_protocols):
assert len(tasks) == len(creating_behavioral_protocols)
for item in creating_behavioral_protocols:
assert item in tasks
assert creating_behavioral_protocols[item].internal_identifier == item

0 comments on commit 89ccbb4

Please sign in to comment.