Skip to content

Commit 14ed965

Browse files
authored
Adding content type to file repository and clarifying version of NIFTI for files (#53)
* Adding content type to filerepository * dealing with gz files * one more item in openminds * using try except * using expect only where it is needed * deleting unwanted space
1 parent 9e65ac5 commit 14ed965

3 files changed

Lines changed: 74 additions & 11 deletions

File tree

bids2openminds/main.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import openminds.latest.controlled_terms as controlled_terms
1111
from openminds import IRI
1212

13-
from .utility import table_filter, pd_table_value, file_hash, file_storage_size
13+
from .utility import table_filter, pd_table_value, file_hash, file_storage_size, detect_nifti_version
1414
from .mapping import bids2openminds_instance
1515

1616

@@ -315,6 +315,7 @@ def create_subjects(subject_id, layout_df, layout, collection):
315315
def create_file(layout_df, BIDS_path, collection):
316316

317317
file_repository = omcore.FileRepository(
318+
format=omcore.ContentType.by_name("application/vnd.bids"),
318319
iri=IRI(pathlib.Path(BIDS_path).absolute().as_uri()))
319320
collection.add(file_repository)
320321
files_list = []
@@ -327,7 +328,7 @@ def create_file(layout_df, BIDS_path, collection):
327328
iri = IRI(pathlib.Path(path).absolute().as_uri())
328329
name = os.path.basename(path)
329330
hashes = file_hash(path)
330-
storage_size = file_storage_size(path)
331+
storage_size_obj, file_size = file_storage_size(path)
331332
if pd.isna(file["subject"]):
332333
if file["suffix"] == "participants":
333334
if extension == ".json":
@@ -350,7 +351,7 @@ def create_file(layout_df, BIDS_path, collection):
350351
elif extension in [".nii", ".nii.gz"]:
351352
content_description = f"Data file for {file['suffix']} of subject {file['subject']}"
352353
data_types = controlled_terms.DataType.by_name("voxel data")
353-
# file_format=omcore.ContentType.by_name("nifti")
354+
file_format = detect_nifti_version(path, extension, file_size)
354355
elif extension == [".tsv"]:
355356
if file["suffix"] == "events":
356357
content_description = f"Event file for {file['suffix']} of subject {file['subject']}"
@@ -368,7 +369,7 @@ def create_file(layout_df, BIDS_path, collection):
368369
# is_part_of=file_bundels
369370
name=name,
370371
# special_usage_role
371-
storage_size=storage_size,
372+
storage_size=storage_size_obj,
372373
)
373374
collection.add(file)
374375
files_list.append(file)

bids2openminds/utility.py

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
import json
33
import os
44
import re
5+
import gzip
56
from warnings import warn
67

78
import pandas as pd
89

910
import openminds.latest.controlled_terms as controlled_terms
10-
from openminds.latest.core import Hash, QuantitativeValue
11+
from openminds.latest.core import Hash, QuantitativeValue, ContentType
1112
from openminds.latest.controlled_terms import UnitOfMeasurement
1213

1314

14-
1515
def camel_to_snake(name):
1616
name = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", name)
1717
return re.sub("([a-z0-9])([A-Z])", r"\1_\2", name).lower()
@@ -75,10 +75,11 @@ def openminds_instance(list: list, Terminologie: str = None, is_list: bool = Tru
7575
for item in list:
7676
if item.replace(" ", "")[0:32] == "@id:https://openminds.ebrains.eu":
7777
slash_location = item.rfind("/")
78-
item_name = item[slash_location + 1 :]
78+
item_name = item[slash_location + 1:]
7979
item_name_snake = camel_to_snake(item_name)
8080
if not (Terminologie):
81-
Terminologie = item[item[:slash_location].rfind("/") + 1 : slash_location]
81+
Terminologie = item[item[:slash_location].rfind(
82+
"/") + 1: slash_location]
8283
Terminologie = Terminologie[0].upper() + Terminologie[1:]
8384
controlled_class = getattr(controlled_terms, Terminologie)
8485
openminds_item = getattr(controlled_class, item_name_snake)
@@ -140,5 +141,66 @@ def file_hash(file_path: str, algorithm: str = "MD5"):
140141

141142
def file_storage_size(file_path: str):
142143
file_stats = os.stat(file_path)
143-
file_size = QuantitativeValue(value=file_stats.st_size, unit=UnitOfMeasurement.by_name("byte"))
144-
return file_size
144+
file_size = QuantitativeValue(
145+
value=file_stats.st_size, unit=UnitOfMeasurement.by_name("byte"))
146+
return file_size, file_stats.st_size
147+
148+
149+
def detect_nifti_version(file_name, extension, file_size):
150+
151+
nii1_sizeof_hdr = 348
152+
nii2_sizeof_hdr = 540
153+
154+
if extension == ".nii":
155+
156+
with open(file_name, 'rb') as fp:
157+
byte_data = fp.read(4)
158+
159+
sizeof_hdr = int.from_bytes(byte_data, byteorder='little')
160+
161+
if sizeof_hdr == 0:
162+
return None
163+
164+
if sizeof_hdr == nii1_sizeof_hdr:
165+
return ContentType.by_name("application/vnd.nifti.1")
166+
167+
elif sizeof_hdr == nii2_sizeof_hdr:
168+
return ContentType.by_name("application/vnd.nifti.2")
169+
170+
else: # big endian
171+
sizeof_hdr = int.from_bytes(byte_data, byteorder='big')
172+
173+
if sizeof_hdr == nii1_sizeof_hdr:
174+
return ContentType.by_name("application/vnd.nifti.1")
175+
176+
elif sizeof_hdr == nii2_sizeof_hdr:
177+
return ContentType.by_name("application/vnd.nifti.2")
178+
179+
if extension == ".nii.gz":
180+
try:
181+
with gzip.open(file_name, 'rb') as fp:
182+
byte_data = fp.read(4)
183+
except gzip.BadGzipFile:
184+
return None
185+
186+
sizeof_hdr = int.from_bytes(byte_data, byteorder='little')
187+
188+
if sizeof_hdr == 0:
189+
return None
190+
191+
if sizeof_hdr == nii1_sizeof_hdr:
192+
return ContentType.by_name("application/vnd.nifti.1")
193+
194+
elif sizeof_hdr == nii2_sizeof_hdr:
195+
return ContentType.by_name("application/vnd.nifti.2")
196+
197+
else: # big endian
198+
sizeof_hdr = int.from_bytes(byte_data, byteorder='big')
199+
200+
if sizeof_hdr == nii1_sizeof_hdr:
201+
return ContentType.by_name("application/vnd.nifti.1")
202+
203+
elif sizeof_hdr == nii2_sizeof_hdr:
204+
return ContentType.by_name("application/vnd.nifti.2")
205+
206+
return None

test/test_example_datasets_click.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from bids2openminds.converter import convert_click
44
from click.testing import CliRunner
55

6-
(test_data_set, number_of_openminds_files) = ("ds003", 100)
6+
(test_data_set, number_of_openminds_files) = ("ds003", 101)
77

88

99
def test_example_datasets_click():

0 commit comments

Comments
 (0)