|
2 | 2 | import json |
3 | 3 | import os |
4 | 4 | import re |
| 5 | +import gzip |
5 | 6 | from warnings import warn |
6 | 7 |
|
7 | 8 | import pandas as pd |
8 | 9 |
|
9 | 10 | 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 |
11 | 12 | from openminds.latest.controlled_terms import UnitOfMeasurement |
12 | 13 |
|
13 | 14 |
|
14 | | - |
15 | 15 | def camel_to_snake(name): |
16 | 16 | name = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", name) |
17 | 17 | 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 |
75 | 75 | for item in list: |
76 | 76 | if item.replace(" ", "")[0:32] == "@id:https://openminds.ebrains.eu": |
77 | 77 | slash_location = item.rfind("/") |
78 | | - item_name = item[slash_location + 1 :] |
| 78 | + item_name = item[slash_location + 1:] |
79 | 79 | item_name_snake = camel_to_snake(item_name) |
80 | 80 | if not (Terminologie): |
81 | | - Terminologie = item[item[:slash_location].rfind("/") + 1 : slash_location] |
| 81 | + Terminologie = item[item[:slash_location].rfind( |
| 82 | + "/") + 1: slash_location] |
82 | 83 | Terminologie = Terminologie[0].upper() + Terminologie[1:] |
83 | 84 | controlled_class = getattr(controlled_terms, Terminologie) |
84 | 85 | openminds_item = getattr(controlled_class, item_name_snake) |
@@ -140,5 +141,66 @@ def file_hash(file_path: str, algorithm: str = "MD5"): |
140 | 141 |
|
141 | 142 | def file_storage_size(file_path: str): |
142 | 143 | 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 |
0 commit comments