Skip to content

Commit

Permalink
Merge pull request #42 from AdebayoBraimah/dev5
Browse files Browse the repository at this point in the history
Dev5
  • Loading branch information
AdebayoBraimah committed Jun 25, 2021
2 parents e00d7a7 + adb8d23 commit 1c14d46
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 51 deletions.
8 changes: 8 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
CHANGES
=========

0.2.0a10
---------

This version is an alpha release that contains several bug fixes and updates.

* BUG FIX: Removed gzip/gunzip file checks, as this caused file and BIDS validation issues.
* BUG FIX: Added support for all caps BIDS metadata, and exclusion of non-BIDS metadata rather than raising exceptions.

0.2.0a9
---------

Expand Down
10 changes: 8 additions & 2 deletions convert_source/cs_utils/bids_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,13 @@ def is_camel_case(s: str,
Boolean.
"""
if bids_case:
return s != s.lower() and s != s.upper() and s[0].isupper() and "_" not in s
return (s != s.lower() and s != s.upper() and s[0].isupper() and "_" not in s) or (s == s.upper())
else:
return s != s.lower() and s != s.upper() and "_" not in s

def construct_bids_dict(meta_dict: Optional[Dict] = None,
json_dict: Optional[Dict] = None,
raise_exec: Optional[bool] = False
) -> Dict:
"""Constructs dictionary of relevant BIDS related information that includes subject and session IDs, in addition
to various metadata. Custom BIDS fields can also be added through the metadata dictionary.
Expand All @@ -83,12 +84,14 @@ def construct_bids_dict(meta_dict: Optional[Dict] = None,
Arguments:
meta_dict: Metadata dictionary that contains the relevant BIDS metadata.
json_dict: Dictionary constructed from the BIDS JSON file, or some secondary dictionary.
raise_exec: Raises exception in the case that BIDS Metadata is not ``CamelCase``.
Returns:
Dictionary containing BIDS related metadata.
Raises:
IndexError: Error that arises if constant variables ``BIDS_INFO``'s keys and ``BIDS_ORD_ARR`` are of different lengths.
BIDSMetaDataError: Exception that is raised in the case that the one of the metadata fields are not ``CamelCase``.
"""
# BIDS informatino dictionary
bids_info: Dict = deepcopy(BIDS_INFO)
Expand Down Expand Up @@ -127,7 +130,10 @@ def construct_bids_dict(meta_dict: Optional[Dict] = None,
if is_camel_case(word,bids_case=True):
pass
else:
raise BIDSMetaDataError(f"Input metadata: {word} is not BIDS compliant.")
if raise_exec:
raise BIDSMetaDataError(f"Input metadata: {word} is not BIDS compliant.")
else:
continue

# Add field to ordered array
if word in ordered_list:
Expand Down
105 changes: 57 additions & 48 deletions convert_source/cs_utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,19 +392,22 @@ def gzip_file(file: str,
elif os.path.exists(file[:-3]):
file: str = file[:-3]

# Check if the file is already gzipped
if _is_gzipped(file=file) and ('.gz' in file.lower()):
return file
elif _is_gzipped(file=file) and ('.gz' not in file.lower()):
file_tmp: str = file + ".gz"
os.rename(file,file_tmp)
return file_tmp
elif (not _is_gzipped(file=file)) and ('.gz' in file.lower()):
file_tmp: str = file[:-3]
os.rename(file,file_tmp)
file: str = file_tmp
elif (not _is_gzipped(file=file)) and ('.gz' not in file.lower()):
pass
# NOTE: Removing gzip file checks as this caused
# file and BIDS validation issues.
#
# # Check if the file is already gzipped
# if _is_gzipped(file=file) and ('.gz' in file.lower()):
# return file
# elif _is_gzipped(file=file) and ('.gz' not in file.lower()):
# file_tmp: str = file + ".gz"
# os.rename(file,file_tmp)
# return file_tmp
# elif (not _is_gzipped(file=file)) and ('.gz' in file.lower()):
# file_tmp: str = file[:-3]
# os.rename(file,file_tmp)
# file: str = file_tmp
# elif (not _is_gzipped(file=file)) and ('.gz' not in file.lower()):
# pass

if native:
# Native implementation
Expand Down Expand Up @@ -466,19 +469,22 @@ def gunzip_file(file: str,
elif os.path.exists(file[:-3]):
file: str = file[:-3]

# Check if the file is alrady gunzipped
if _is_gzipped(file=file) and ('.gz' in file.lower()):
pass
elif _is_gzipped(file=file) and ('.gz' not in file.lower()):
file_tmp: str = file + ".gz"
os.rename(file,file_tmp)
file: str = file_tmp
elif (not _is_gzipped(file=file)) and ('.gz' in file.lower()):
file_tmp: str = file[:-3]
os.rename(file,file_tmp)
return file_tmp
elif (not _is_gzipped(file=file)) and ('.gz' not in file.lower()):
return file
# NOTE: Removing gunzip file checks as this caused
# file and BIDS validation issues.
#
# # Check if the file is alrady gunzipped
# if _is_gzipped(file=file) and ('.gz' in file.lower()):
# pass
# elif _is_gzipped(file=file) and ('.gz' not in file.lower()):
# file_tmp: str = file + ".gz"
# os.rename(file,file_tmp)
# file: str = file_tmp
# elif (not _is_gzipped(file=file)) and ('.gz' in file.lower()):
# file_tmp: str = file[:-3]
# os.rename(file,file_tmp)
# return file_tmp
# elif (not _is_gzipped(file=file)) and ('.gz' not in file.lower()):
# return file

if native:
# Native implementation
Expand Down Expand Up @@ -1679,28 +1685,31 @@ def list_dir_files(pathname: str,
file_dir_list.sort()
return file_dir_list

def _is_gzipped(file: str) -> bool:
"""Helper function that determines if a file is actually gzipped or not.
The file extension is ignored as the file itself is checked.
Usage example:
>>> _is_gzipped(file='MR0001.nii.gz')
'True'
Arguments:
file: Input file name.
Returns:
Boolean - ``True`` if the file is gzipped, and ``false`` otherwise.
"""
GZIP_MAGIC_NUMBER = "1f8b"
with open(file=file,mode='r') as f:
try:
# This should fail if the file is actually gzipped, as the
# 'utf-8' codec cannot decode byte 0x8b in position 1.
return f.read(2).encode("utf-8").hex() == GZIP_MAGIC_NUMBER
except UnicodeDecodeError:
return True
# NOTE: Commenting out this helper function as no other function depends on this
# helper function.
#
# def _is_gzipped(file: str) -> bool:
# """Helper function that determines if a file is actually gzipped or not.
# The file extension is ignored as the file itself is checked.
#
# Usage example:
# >>> _is_gzipped(file='MR0001.nii.gz')
# 'True'
#
# Arguments:
# file: Input file name.
#
# Returns:
# Boolean - ``True`` if the file is gzipped, and ``false`` otherwise.
# """
# GZIP_MAGIC_NUMBER = "1f8b"
# with open(file=file,mode='r') as f:
# try:
# # This should fail if the file is actually gzipped, as the
# # 'utf-8' codec cannot decode byte 0x8b in position 1.
# return f.read(2).encode("utf-8").hex() == GZIP_MAGIC_NUMBER
# except UnicodeDecodeError:
# return True

def sym_link(src: str,
tar: str,
Expand Down
2 changes: 1 addition & 1 deletion convert_source/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.0a9
0.2.0a10

0 comments on commit 1c14d46

Please sign in to comment.