Skip to content

Commit

Permalink
removed errant use of metadata.json file instead of import (#338)
Browse files Browse the repository at this point in the history
* removed errant use of metadata.json file instead of import
* bump version
  • Loading branch information
bendhouseart authored Feb 19, 2025
1 parent 0f21e8b commit f737059
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 65 deletions.
26 changes: 13 additions & 13 deletions pypet2bids/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 9 additions & 47 deletions pypet2bids/pypet2bids/helper_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,10 @@
if "PET2BIDS" not in project_dir.parts:
project_dir = parent_dir

metadata_dir = os.path.join(project_dir, "metadata")

# check to see where the schema is at
pet_metadata_json = os.path.join(metadata_dir, "PET_metadata.json")
permalink_pet_metadata_json = "https://github.com/openneuropet/PET2BIDS/blob/76d95cf65fa8a14f55a4405df3fdec705e2147cf/metadata/PET_metadata.json"
pet_reconstruction_metadata_json = os.path.join(
metadata_dir, "PET_reconstruction_methods.json"
)

# load bids schema
schema = metadata.schema

pet_metadata = metadata.PET_metadata
# putting these paths here as they are reused in dcm2niix4pet.py, update_json_pet_file.py, and ecat.py
module_folder = Path(__file__).parent.resolve()
python_folder = module_folder.parent
Expand Down Expand Up @@ -95,19 +87,6 @@ def logger(name):
return logger


def load_pet_bids_requirements_json(
pet_bids_req_json: Union[str, pathlib.Path] = pet_metadata_json
) -> dict:
if type(pet_bids_req_json) is str:
pet_bids_req_json = pathlib.Path(pet_bids_req_json)
if pet_bids_req_json.is_file():
with open(pet_bids_req_json, "r") as infile:
reqs = json.load(infile)
return reqs
else:
raise FileNotFoundError(pet_bids_req_json)


def flatten_series(series):
"""
This function retrieves either a list or a single value from a pandas series object thus converting a complex
Expand Down Expand Up @@ -150,12 +129,13 @@ def collect_spreadsheets(folder_path: pathlib.Path):

def single_spreadsheet_reader(
path_to_spreadsheet: Union[str, pathlib.Path],
pet2bids_metadata_json: Union[str, pathlib.Path] = pet_metadata_json,
pet2bids_metadata: dict = metadata.PET_metadata,
dicom_metadata={},
**kwargs,
) -> dict:

metadata = {}
spreadsheet_metadata = {}
metadata_fields = pet2bids_metadata.PET_metadata

if type(path_to_spreadsheet) is str:
path_to_spreadsheet = pathlib.Path(path_to_spreadsheet)
Expand All @@ -165,24 +145,6 @@ def single_spreadsheet_reader(
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)

log = logging.getLogger("pypet2bids")
Expand All @@ -192,7 +154,7 @@ def single_spreadsheet_reader(
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)
spreadsheet_metadata[field] = flatten_series(series)
elif (
series.empty
and field_level == "mandatory"
Expand All @@ -204,10 +166,10 @@ def single_spreadsheet_reader(
)

# lastly apply any kwargs to the metadata
metadata.update(**kwargs)
spreadsheet_metadata.update(**kwargs)

# more lastly, check to see if values are of the correct datatype (e.g. string, number, boolean)
for field, value in metadata.items():
for field, value in spreadsheet_metadata.items():
# check schema for field
field_schema_properties = schema["objects"]["metadata"].get(field, None)
if field_schema_properties:
Expand All @@ -220,7 +182,7 @@ def single_spreadsheet_reader(
try:
check_bool = int(value) / 1
if check_bool == 0 or check_bool == 1:
metadata[field] = bool(value)
spreadsheet_metadata[field] = bool(value)
else:
log.warning(
f"{field} is not boolean, it's value is {value}"
Expand All @@ -232,7 +194,7 @@ def single_spreadsheet_reader(
log.warning(f"{field} is not string, it's value is {value}")
else:
pass
return metadata
return spreadsheet_metadata


def compress(file_like_object, output_path: str = None):
Expand Down
5 changes: 2 additions & 3 deletions pypet2bids/pypet2bids/metadata_spreadsheet_example_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@

try:
import helper_functions
import metadata
except ModuleNotFoundError:
import pypet2bids.helper_functions as helper_functions
import pypet2bids.metadata as metadata


parent_dir = pathlib.Path(__file__).parent.resolve()
project_dir = parent_dir.parent
metadata_dir = join(project_dir, "metadata")
pet_metadata_json = join(metadata_dir, "PET_metadata.json")
permalink_pet_metadata_json = "https://github.com/openneuropet/PET2BIDS/blob/76d95cf65fa8a14f55a4405df3fdec705e2147cf/metadata/PET_metadata.json"

"""
Expand Down
2 changes: 1 addition & 1 deletion pypet2bids/pypet2bids/update_json_pet_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def check_json(
: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
PET_metadata imported from metadata.PET_metadata
:param silent: Raises warnings or errors to stdout if this flag is set to True
:return: dictionary of items existence and value state, if key is True/False there exists/(does not exist) a
corresponding entry in the json the same can be said of value
Expand Down
2 changes: 1 addition & 1 deletion pypet2bids/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pypet2bids"
version = "1.4.0"
version = "1.4.1"
description = "A python library for converting PET imaging and blood data to BIDS."
authors = ["anthony galassi <28850131+bendhouseart@users.noreply.github.com>"]
license = "MIT"
Expand Down

0 comments on commit f737059

Please sign in to comment.