-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature Request] Input Additional Metadata via Spreadsheet #80
Comments
Additionally, the main purpose of this request is to create an efficient workflow that allows users to add information to their dataset's json sidecars that are required by BIDS but not contained in the DICOM headers, meaning In theory, this should roughly follow the Events workflow. Issue #66 will likely benefit from this feature. |
Following up here with -> openneuropet/PET2BIDS#210 Would you prefer that I generalize the following functions from It might save you a bit of trouble as you could just import them into ezBIDS instead of rolling your own. def flatten_series(series):
"""
This function retrieves either a list or a single value from a pandas series object thus converting a complex
data type to a simple datatype or list of simple types. If the length of the series is one or less this returns that
single value, else this object returns all values within the series that are not Null/nan in the form of a list
:param series: input series of type pandas.Series object, typically extracted as a column/row from a
pandas.Dataframe object
:return: a simplified single value or list of values
"""
simplified_series_object = series.dropna().to_list()
if len(simplified_series_object) > 1:
pass
elif len(simplified_series_object) == 1:
simplified_series_object = simplified_series_object[0]
else:
raise f"Invalid Series: {series}"
return simplified_series_object
def collect_spreadsheets(folder_path: pathlib.Path):
spreadsheet_files = []
all_files = [folder_path / pathlib.Path(file) for file in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, file))]
for file in all_files:
if file.suffix == '.xlsx' or file.suffix == '.csv' or file.suffix == '.xls' or file.suffix == '.tsv':
spreadsheet_files.append(file)
return spreadsheet_files
def single_spreadsheet_reader(
path_to_spreadsheet: Union[str, pathlib.Path],
pet2bids_metadata_json: Union[str, pathlib.Path] = pet_metadata_json,
dicom_metadata={},
**kwargs) -> dict:
metadata = {}
if type(path_to_spreadsheet) is str:
path_to_spreadsheet = pathlib.Path(path_to_spreadsheet)
if path_to_spreadsheet.is_file():
pass
else:
raise FileNotFoundError(f"{path_to_spreadsheet} does not exist.")
if pet2bids_metadata_json:
if type(pet_metadata_json) is str:
pet2bids_metadata_json = pathlib.Path(pet2bids_metadata_json)
if pet2bids_metadata_json.is_file():
with open(pet_metadata_json, 'r') as infile:
metadata_fields = json.load(infile)
else:
raise FileNotFoundError(f"Required metadata file not found at {pet_metadata_json}, check to see if this file exists;"
f"\nelse pass path to file formatted to this {permalink_pet_metadata_json} via "
f"pet2bids_metadata_json argument in simplest_spreadsheet_reader call.")
else:
raise FileNotFoundError(f"pet2bids_metadata_json input required for function call, you provided {pet2bids_metadata_json}")
spreadsheet_dataframe = open_meta_data(path_to_spreadsheet)
# collect mandatory fields
for field_level in metadata_fields.keys():
for field in metadata_fields[field_level]:
series = spreadsheet_dataframe.get(field, Series(dtype=numpy.float64))
if not series.empty:
metadata[field] = flatten_series(series)
elif series.empty and field_level == 'mandatory' and not dicom_metadata.get(field, None) and field not in kwargs:
logging.warning(f"{field} not found in {path_to_spreadsheet}, {field} is required by BIDS")
# lastly apply any kwargs to the metadata
metadata.update(**kwargs)
return metadata I'm most of the way there for the listed issues in this Feature Request barring a bit of testing, so let me know if any of the above would be helpful. |
Yeah, if that's not too much, it would be very helpful! |
It appears that having the ability to:
would be a useful feature addition to ezBIDS for PET and ASL as well as other modalities. Adding this as an issue to continue the discussion @dlevitas and @bendhouseart started in slack.
The text was updated successfully, but these errors were encountered: