Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating File bundles #51

Merged
merged 10 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 56 additions & 2 deletions bids2openminds/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,63 @@ def create_subjects(subject_id, layout_df, layout, collection):
return subjects_dict, subject_state_dict, subjects_list


def create_file_bundle(path, collection, parent_file_bundle=None):

name = str(path).replace("/", "_").replace("\\", "_")
if name[0] == "_":
name = name[1:]
openminds_file_bundle = omcore.FileBundle(content_description=f"File bundle created for {str(path)}",
name=name,
is_part_of=parent_file_bundle)
files = {}
files_size = 0
all = os.listdir(path)

for item in all:

item_path = str(pathlib.PurePath(path, item))

if os.path.isfile(item_path):
files[item_path] = [openminds_file_bundle]
files_size += os.stat(item_path).st_size

if os.path.isdir(item_path):

child_files, child_filesizes = create_file_bundle(
item_path, collection, parent_file_bundle=openminds_file_bundle)

for child_file_path in child_files.keys():
if child_file_path not in files:
files[child_file_path] = []
files[child_file_path].extend(child_files[child_file_path])
files[child_file_path].append(openminds_file_bundle)

files_size += child_filesizes

openminds_file_bundle.storage_size = omcore.QuantitativeValue(value=files_size,
unit=controlled_terms.UnitOfMeasurement.by_name(
"byte")
)
collection.add(openminds_file_bundle)

return files, files_size


def create_file(layout_df, BIDS_path, collection):

BIDS_path_absolute = pathlib.Path(BIDS_path).absolute()

file2file_bundle_dic, file_repository_storage_size = create_file_bundle(
BIDS_path_absolute, collection)

file_repository = omcore.FileRepository(
iri=IRI(pathlib.Path(BIDS_path).absolute().as_uri()))
iri=IRI(BIDS_path_absolute.as_uri()),
storage_size=omcore.QuantitativeValue(value=file_repository_storage_size,
unit=controlled_terms.UnitOfMeasurement.by_name(
"byte")
))
collection.add(file_repository)

files_list = []
for index, file in layout_df.iterrows():
file_format = None
Expand Down Expand Up @@ -358,14 +410,16 @@ def create_file(layout_df, BIDS_path, collection):
"event sequence")
file_format = omcore.ContentType.by_name(
"text/tab-separated-values")

file = omcore.File(
iri=iri,
content_description=content_description,
data_types=data_types,
file_repository=file_repository,
format=file_format,
hashes=hashes,
# is_part_of=file_bundels
is_part_of=file2file_bundle_dic[str(
pathlib.Path(path))],
name=name,
# special_usage_role
storage_size=storage_size,
Expand Down
2 changes: 1 addition & 1 deletion test/test_example_datasets_click.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from bids2openminds.converter import convert_click
from click.testing import CliRunner

(test_data_set, number_of_openminds_files) = ("ds003", 100)
(test_data_set, number_of_openminds_files) = ("ds003", 140)


def test_example_datasets_click():
Expand Down
80 changes: 80 additions & 0 deletions test/test_file_bundle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import pytest
import os
import openminds.latest.core as omcore
from openminds import Collection
from bids2openminds.main import create_file_bundle

(test_data_set, number_of_openminds_bundle, example_file, example_file_bubdels) = (
"ds007", 61, ["sub-04", "anat", "sub-04_inplaneT2.nii.gz"], ["sub-04", "sub-04_anat"])


@pytest.fixture
def test_dir():
test_dir = os.path.join("bids-examples", test_data_set)
return test_dir


@pytest.fixture
def path_name(test_dir):
name = str(test_dir).replace("/", "_").replace("\\", "_")
if name[0] == "_":
name = name[1:]
return name


@pytest.fixture
def example_file_path(test_dir):
path = test_dir
for item in example_file:
path = os.path.join(path, item)
return path


@pytest.fixture
def generate_file_bundle_collection(test_dir):
collection = Collection()
file_bundles, _ = create_file_bundle(test_dir, collection)
return file_bundles, collection


def test_file_bundles_type(generate_file_bundle_collection):
file_bundles, _ = generate_file_bundle_collection
assert type(file_bundles) is dict


def test_number_file_bundle(generate_file_bundle_collection):
_, collection = generate_file_bundle_collection
m = 0
for item in collection:
if item.type_ == "https://openminds.ebrains.eu/core/FileBundle":
m += 1
assert m == number_of_openminds_bundle


def test_dataset_description_location(test_dir, path_name, generate_file_bundle_collection):
file_bundles, collection = generate_file_bundle_collection
dataset_description_location = os.path.join(
test_dir, "dataset_description.json")
assert len(file_bundles[dataset_description_location]) == 1

for item in collection:
if item.type_ == "https://openminds.ebrains.eu/core/FileBundle" and item.name == path_name:
main_file_boundle = item

assert file_bundles[dataset_description_location][0].id == main_file_boundle.id


def test_random_file(test_dir, path_name, generate_file_bundle_collection, example_file_path):
file_bundles, _ = generate_file_bundle_collection

reference_boundels = []
for file in example_file_bubdels:
reference_boundels.append(path_name+"_"+file)
reference_boundels.append(path_name)

test_bundles = file_bundles[example_file_path]

assert len(test_bundles) == len(reference_boundels)

for bundle in test_bundles:
assert bundle.name in reference_boundels