Skip to content

Commit

Permalink
silenced extra warnings, checking spreadsheet inputs too
Browse files Browse the repository at this point in the history
  • Loading branch information
bendhouseart committed Sep 29, 2023
1 parent f3bba9f commit f65858d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
36 changes: 24 additions & 12 deletions pypet2bids/pypet2bids/dcm2niix4pet.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import pypet2bids.helper_functions as helper_functions
import pypet2bids.is_pet as is_pet

logger = logging.getLogger("pypet2bids")
logger = helper_functions.logger("pypet2bids")

# fields to check for
module_folder = Path(__file__).parent.resolve()
Expand Down Expand Up @@ -74,11 +74,13 @@
raise IOError(f"Unable to read from {metadata_json}")


def check_json(path_to_json, items_to_check=None, silent=False, **additional_arguments):
def check_json(path_to_json, items_to_check=None, silent=False, spreadsheet_metadata={}, **additional_arguments):
"""
This method opens a json and checks to see if a set of mandatory values is present within that json, optionally it
also checks for recommended key value pairs. If fields are not present a warning is raised to the user.
:param spreadsheet_metadata:
:type spreadsheet_metadata:
:param path_to_json: path to a json file e.g. a BIDS sidecar file created after running dcm2niix
:param items_to_check: a dictionary with items to check for within that json. If None is supplied defaults to the
PET_metadata.json contained in this repository
Expand Down Expand Up @@ -110,12 +112,16 @@ def check_json(path_to_json, items_to_check=None, silent=False, **additional_arg

# initialize warning colors and warning storage dictionary
storage = {}
flattened_spreadsheet_metadata = {}
flattened_spreadsheet_metadata.update(spreadsheet_metadata.get('nifti_json', {}))
flattened_spreadsheet_metadata.update(spreadsheet_metadata.get('blood_json', {}))
flattened_spreadsheet_metadata.update(spreadsheet_metadata.get('blood_tsv', {}))

for requirement in items_to_check.keys():
for item in items_to_check[requirement]:
all_good = False
if item in json_to_check.keys() and json_to_check.get(item, None) or item in additional_arguments:
# this json has both the key and a non blank value do nothing
if item in json_to_check.keys() and json_to_check.get(item, None) or item in additional_arguments or item in flattened_spreadsheet_metadata.keys():
# this json has both the key and a non-blank value do nothing
all_good = True
pass
elif item in json_to_check.keys() and not json_to_check.get(item, None):
Expand Down Expand Up @@ -471,6 +477,7 @@ def __init__(self, image_folder, destination_path=None, metadata_path=None,
dicom_metadata=self.dicom_headers[next(iter(self.dicom_headers))],
**self.additional_arguments))


# check for any blood (tsv) data or otherwise in the given spreadsheet values
blood_tsv_columns = ['time', 'plasma_radioactivity', 'metabolite_parent_fraction',
'whole_blood_radioactivity']
Expand Down Expand Up @@ -630,9 +637,14 @@ def run_dcm2niix(self):
# we check to see what's missing from our recommended and required jsons by gathering the
# output of check_json silently
if self.additional_arguments:
check_for_missing = check_json(created_path, silent=True, **self.additional_arguments)
check_for_missing = check_json(created_path,
silent=True,
spreadsheet_metadata=self.spreadsheet_metadata,
**self.additional_arguments)
else:
check_for_missing = check_json(created_path, silent=True)
check_for_missing = check_json(created_path,
silent=True,
spreadsheet_metadata=self.spreadsheet_metadata)

# we do our best to extra information from the dicom header and insert these values
# into the sidecar json
Expand Down Expand Up @@ -969,7 +981,7 @@ def check_meta_radio_inputs(kwargs: dict) -> dict:
if SpecificRadioactivity:
if SpecificRadioactivity != tmp:
logger.warning("Inferred SpecificRadioactivity in Bq/g doesn't match InjectedRadioactivity "
"and InjectedMass, could be a unit issue", "yellow")
"and InjectedMass, could be a unit issue")
data_out['SpecificRadioactivity'] = SpecificRadioactivity
data_out['SpecificRadioactivityUnits'] = kwargs.get('SpecificRadioactivityUnityUnits', 'n/a')
else:
Expand All @@ -991,7 +1003,7 @@ def check_meta_radio_inputs(kwargs: dict) -> dict:
if InjectedMass:
if InjectedMass != tmp:
logger.warning("Inferred InjectedMass in ug doesn't match InjectedRadioactivity and "
"InjectedMass, could be a unit issue", "yellow")
"InjectedMass, could be a unit issue")
data_out['InjectedMass'] = InjectedMass
data_out['InjectedMassUnits'] = kwargs.get('InjectedMassUnits', 'n/a')
else:
Expand All @@ -1014,7 +1026,7 @@ def check_meta_radio_inputs(kwargs: dict) -> dict:
if InjectedRadioactivity:
if InjectedRadioactivity != tmp:
logger.warning("Inferred InjectedRadioactivity in MBq doesn't match SpecificRadioactivity "
"and InjectedMass, could be a unit issue", "yellow")
"and InjectedMass, could be a unit issue")
data_out['InjectedRadioactivity'] = InjectedRadioactivity
data_out['InjectedRadioactivityUnits'] = kwargs.get('InjectedRadioactivityUnits', 'n/a')
else:
Expand All @@ -1037,7 +1049,7 @@ def check_meta_radio_inputs(kwargs: dict) -> dict:
if SpecificRadioactivity != tmp:
logger.warning(
"Inferred SpecificRadioactivity in MBq/ug doesn't match Molar Activity and Molecular "
"Weight, could be a unit issue", 'yellow')
"Weight, could be a unit issue")
data_out['SpecificRadioactivity'] = SpecificRadioactivity
data_out['SpecificRadioactivityUnits'] = kwargs.get('SpecificRadioactivityUnityUnits', 'n/a')
else:
Expand All @@ -1059,7 +1071,7 @@ def check_meta_radio_inputs(kwargs: dict) -> dict:
if MolecularWeight:
if MolecularWeight != tmp:
logger.warning("Inferred MolecularWeight in MBq/ug doesn't match Molar Activity and "
"Molecular Weight, could be a unit issue", 'yellow')
"Molecular Weight, could be a unit issue")

data_out['MolecularWeight'] = tmp
data_out['MolecularWeightUnits'] = kwargs.get('MolecularWeightUnits', 'n/a')
Expand All @@ -1082,7 +1094,7 @@ def check_meta_radio_inputs(kwargs: dict) -> dict:
if MolarActivity:
if MolarActivity != tmp:
logger.warning("Inferred MolarActivity in GBq/umol doesn't match Specific Radioactivity and "
"Molecular Weight, could be a unit issue", "yellow")
"Molecular Weight, could be a unit issue")
data_out['MolarActivity'] = MolarActivity
data_out['MolarActivityUnits'] = kwargs.get('MolarActivityUnits', 'n/a')
else:
Expand Down
11 changes: 6 additions & 5 deletions pypet2bids/pypet2bids/helper_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def logger(name):
# works great to get the desired effect
logger.propagate = False

loggers[name] = logger
return logger


Expand Down Expand Up @@ -363,7 +364,7 @@ def open_meta_data(metadata_path: Union[str, pathlib.Path], separator=None) -> p
:type separator: str
:return: a pandas dataframe representation of the spreadsheet/metadatafile
"""
log = logger('pet2bids')
log = logger('pypet2bids')
if type(metadata_path) is str:
metadata_path = pathlib.Path(metadata_path)

Expand Down Expand Up @@ -426,7 +427,7 @@ def open_meta_data(metadata_path: Union[str, pathlib.Path], separator=None) -> p


def translate_metadata(metadata_path, metadata_translation_script_path, **kwargs):
log = logger('pet2bids')
log = logger('pypet2bids')
# load metadata
metadata_dataframe = open_meta_data(metadata_path)

Expand Down Expand Up @@ -509,7 +510,7 @@ def collect_bids_part(bids_part: str, path_like: Union[str, pathlib.Path]) -> st
:return: the collected bids part
:rtype: string
"""
log = logger('pet2bids')
log = logger('pypet2bids')

# get os of system
if os.name == 'posix':
Expand Down Expand Up @@ -912,7 +913,7 @@ def check_pet2bids_config(variable: str = 'DCM2NIIX_PATH'):
:return: the value of the variable if it exists in the config file
:rtype: str
"""
log = logger('pet2bids')
log = logger('pypet2bids')
# check to see if path to dcm2niix is in .env file
dcm2niix_path = None
home_dir = Path.home()
Expand All @@ -939,7 +940,7 @@ def check_pet2bids_config(variable: str = 'DCM2NIIX_PATH'):
return variable_value

else:
log.error(f"Config file not found at {pypet2bids_config}, .pet2bidsconfig file must exist and "
log.warning(f"Config file not found at {pypet2bids_config}, .pet2bidsconfig file must exist and "
f"have variable: {variable} and {variable} must be set.")


Expand Down

0 comments on commit f65858d

Please sign in to comment.