Skip to content

Commit

Permalink
Merge pull request #34 from AdebayoBraimah/dev5
Browse files Browse the repository at this point in the history
Dev5
  • Loading branch information
AdebayoBraimah authored Jun 10, 2021
2 parents b053eaa + de65b0a commit 2ea4d2a
Show file tree
Hide file tree
Showing 14 changed files with 317 additions and 665 deletions.
12 changes: 12 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
CHANGES
=========

0.2.0a6
---------

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

* BUG FIX: Removed the ``metadata`` fields ``EffectiveEchoSpacing`` and ``TotalReadoutTime`` as these fields were computed incorrectly, and thus failing BIDS validation.
* BUG FIX: Fixed issue in which zero and non-zero b-values were included in the ``acq`` tag for ``dwis``, instead of either one or the other.
* BUG FIX: The ``participants.tsv`` is correctly written to file.
* BUG FIX: Added functionality to obtain the acqusition date and time for source data.
* BUG FIX: Dropped unfilled BIDS metadata fields to avoid validation errors.
* BUG FIX: Fixed output ``scans.tsv`` files to include all the necessary information to pass BIDS validation.

0.2.0a5
---------

Expand Down
79 changes: 58 additions & 21 deletions convert_source/batch_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,20 +320,55 @@ def batch_proc(study_img_dir: str,
desc="Writing scan files",
position=0,
leave=True):
df: pd.DataFrame = export_bids_scans_dataframe(database=database,
sub_id=sub,
search_dict=search_dict,
gzipped=gzip)
if len(df) == 0:
pass
try:
ses_list: List[str] = list_dir_files(pathname=os.path.join(out_dir,f"sub-{sub}"),
pattern="ses-*",
file_name_only=True)
ses_list: List[str] = [ x.replace('ses-','') for x in ses_list ]
except FileNotFoundError:
ses_list: List = []

if len(ses_list) == 0:
df: pd.DataFrame = export_bids_scans_dataframe(database=database,
sub_id=sub,
search_dict=search_dict,
gzipped=gzip)

if len(df) == 0:
continue
else:
out_name: str = os.path.join(out_dir,f'sub-{sub}',f'sub-{sub}' + '_scans.tsv')

if os.path.exists(out_name):
os.remove(out_name)

df.to_csv(out_name,
sep='\t',
na_rep='n/a',
index=False,
mode="a",
encoding='utf-8')
else:
out_name: str = os.path.join(out_dir,f'sub-{sub}',f'sub-{sub}' + '_scans.tsv')
df.to_csv(out_name,
sep='\t',
na_rep='',
index=False,
mode="w",
encoding='utf-8')
for ses in ses_list:
df: pd.DataFrame = export_bids_scans_dataframe(database=database,
sub_id=sub,
search_dict=search_dict,
gzipped=gzip,
ses_id=ses)
if len(df) == 0:
continue
else:
out_name: str = os.path.join(out_dir,f'sub-{sub}',f'ses-{ses}',f'sub-{sub}_ses-{ses}' + '_scans.tsv')

if os.path.exists(out_name):
os.remove(out_name)

df.to_csv(out_name,
sep='\t',
na_rep='n/a',
index=False,
mode="a",
encoding='utf-8')

return (bids_imgs,
bids_jsons,
Expand Down Expand Up @@ -941,11 +976,12 @@ def source_to_bids(sub_data: SubDataInfo,

if (modality_type.lower() == 'dwi' or modality_label.lower() == 'dwi') and append_dwi_info:
bvals: List[int] = get_bvals(img_data.bvals[i])
echo_time: Union[int,str] = bids_dict["EchoTime"]
echo_time: Union[int,str] = bids_dict.get("EchoTime",'')
_label: str = ""
for bval in bvals:
if bval != 0:
_label += f"b{bval}"
_label += f"b{bval}"
if int(bvals[0]) == 0:
modality_label: str = "sbref"
if echo_time:
echo_time: float = float(echo_time) * 1000
_label += f"TE{int(echo_time)}"
Expand Down Expand Up @@ -1310,11 +1346,12 @@ def nifti_to_bids(sub_data: SubDataInfo,

if (modality_type.lower() == 'dwi' or modality_label.lower() == 'dwi') and append_dwi_info:
bvals: List[int] = get_bvals(img_data.bvals[0])
echo_time: Union[int,str] = bids_dict["EchoTime"]
echo_time: Union[int,str] = bids_dict.get("EchoTime",'')
_label: str = ""
for bval in bvals:
if bval != 0:
_label += f"b{bval}"
_label += f"b{bval}"
if int(bvals[0]) == 0:
modality_label: str = "sbref"
if echo_time:
echo_time: float = float(echo_time) * 1000
_label += f"TE{int(echo_time)}"
Expand Down Expand Up @@ -1839,7 +1876,7 @@ def create_participant_tsv(out_dir: str) -> Tuple[str,str]:
inplace=True)
df_new.to_csv(participant_tsv,
sep='\t',
na_rep='',
na_rep='n/a',
index=False,
mode='w',
encoding='utf-8')
Expand All @@ -1857,7 +1894,7 @@ def create_participant_tsv(out_dir: str) -> Tuple[str,str]:
df['participant_id'] = subs_list
df.to_csv(participant_tsv,
sep='\t',
na_rep='',
na_rep='n/a',
index=False,
mode="w",
encoding='utf-8')
Expand Down
13 changes: 11 additions & 2 deletions convert_source/cs_utils/bids_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,19 @@ def construct_bids_dict(meta_dict: Optional[Dict] = None,
bids_dict: Dict = dict_multi_update(dictionary=bids_dict, **meta_dict)
bids_dict: Dict = dict_multi_update(dictionary=bids_dict, **json_dict)

# Create ordered BIDS dictionary
# Create ordered BIDS dictionary and drop unfilled metadata fields from ordered BIDS dictionary
ordered_bids_dict: OrderedDict = OrderedDict()
for key in ordered_list:
ordered_bids_dict[key] = bids_dict[key]
val = bids_dict.get(key,'')
if (val == "") or (val is None):
continue
elif (key == "EffectiveEchoSpacing") or (key == "TotalReadoutTime"):
# NOTE: These two keys are being excluded entirely, as the current
# calculations fail BIDS validation.
# TODO: Accurately compute 'EffectiveEchoSpacing' and 'TotalReadoutTime'
continue
else:
ordered_bids_dict[key] = bids_dict[key]

return ordered_bids_dict

Expand Down
110 changes: 55 additions & 55 deletions convert_source/cs_utils/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,72 +54,72 @@
BIDS_INFO: Dict = {
# Common metadata
## Scanner Hardware
"Manufacturer":"",
"ManufacturersModelName":"",
"DeviceSerialNumber":"",
"StationName":"",
"SoftwareVersions":"",
"HardcopyDeviceSoftwareVersion":"",
"MagneticFieldStrength":"",
"ReceiveCoilName":"",
"ReceiveCoilActiveElements":"",
"GradientSetType":"",
"MRTransmitCoilSequence":"",
"MatrixCoilMode":"",
"CoilCombinationMethod":"",
"Manufacturer":None,
"ManufacturersModelName":None,
"DeviceSerialNumber":None,
"StationName":None,
"SoftwareVersions":None,
"HardcopyDeviceSoftwareVersion":None,
"MagneticFieldStrength":None,
"ReceiveCoilName":None,
"ReceiveCoilActiveElements":None,
"GradientSetType":None,
"MRTransmitCoilSequence":None,
"MatrixCoilMode":None,
"CoilCombinationMethod":None,
## Sequence Specifics
"PulseSequenceType":"",
"ScanningSequence":"",
"SequenceVariant":"",
"ScanOptions":"",
"SequenceName":"",
"PulseSequenceDetails":"",
"NonlinearGradientCorrection":"",
"PulseSequenceType":None,
"ScanningSequence":None,
"SequenceVariant":None,
"ScanOptions":None,
"SequenceName":None,
"PulseSequenceDetails":None,
"NonlinearGradientCorrection":None,
## In-Plane Spatial Encoding
"NumberShots":"",
"ParallelReductionFactorInPlane":"",
"ParallelAcquisitionTechnique":"",
"PartialFourier":"",
"PartialFourierDirection":"",
"PhaseEncodingDirection":"",
"EffectiveEchoSpacing":"",
"TotalReadoutTime":"",
"NumberShots":None,
"ParallelReductionFactorInPlane":None,
"ParallelAcquisitionTechnique":None,
"PartialFourier":None,
"PartialFourierDirection":None,
"PhaseEncodingDirection":None,
"EffectiveEchoSpacing":None,
"TotalReadoutTime":None,
## Timing Parameters
"EchoTime":"",
"InversionTime":"",
"SliceTiming":"",
"SliceEncodingDirection":"",
"DwellTime":"",
"EchoTime":None,
"InversionTime":None,
"SliceTiming":None,
"SliceEncodingDirection":None,
"DwellTime":None,
## RF & Contrast
"FlipAngle":"",
"NegativeContrast":"",
"FlipAngle":None,
"NegativeContrast":None,
## Slice Acceleration
"MultibandAccelerationFactor":"",
"MultibandAccelerationFactor":None,
## Anatomical landmarks
"AnatomicalLandmarkCoordinates":"",
"AnatomicalLandmarkCoordinates":None,
## Institution information
"InstitutionName":"",
"InstitutionAddress":"",
"InstitutionalDepartmentName":"",
"InstitutionName":None,
"InstitutionAddress":None,
"InstitutionalDepartmentName":None,
# Anat
"ContrastBolusIngredient":"",
"ContrastBolusIngredient":None,
# Func
"RepetitionTime":"",
"TaskName":"",
"NumberOfVolumesDiscardedByScanner":"",
"NumberOfVolumesDiscardedByUser":"",
"DelayTime":"",
"AcquisitionDuration":"",
"DelayAfterTrigger":"",
"Instructions":"",
"TaskDescription":"",
"CogAtlasID":"",
"CogPOID":"",
"RepetitionTime":None,
"TaskName":None,
"NumberOfVolumesDiscardedByScanner":None,
"NumberOfVolumesDiscardedByUser":None,
"DelayTime":None,
"AcquisitionDuration":None,
"DelayAfterTrigger":None,
"Instructions":None,
"TaskDescription":None,
"CogAtlasID":None,
"CogPOID":None,
# Fmap
"Units":"",
"IntendedFor":"",
"Units":None,
"IntendedFor":None,
# Custom BIDS fields
"SourceDataFormat":"",
"SourceDataFormat":None,
"BIDSVersion":DEFAULT_BIDS_VERSION,
"ConvertSourceVersion": CS_VERSION
}
Expand Down
Loading

0 comments on commit 2ea4d2a

Please sign in to comment.