From cf21bad3b0baedae0e217397eb9e629848fd15af Mon Sep 17 00:00:00 2001 From: Anthony Galassi <28850131+bendhouseart@users.noreply.github.com> Date: Fri, 9 Feb 2024 16:55:55 -0500 Subject: [PATCH] added small script to update ecat mainheader with, bump version --- pypet2bids/pypet2bids/ecat_header_update.py | 68 +++++++++++++++++++++ pypet2bids/pypet2bids/write_ecat.py | 2 +- pypet2bids/pyproject.toml | 3 +- 3 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 pypet2bids/pypet2bids/ecat_header_update.py diff --git a/pypet2bids/pypet2bids/ecat_header_update.py b/pypet2bids/pypet2bids/ecat_header_update.py new file mode 100644 index 00000000..825d079c --- /dev/null +++ b/pypet2bids/pypet2bids/ecat_header_update.py @@ -0,0 +1,68 @@ +try: + import ecat + import write_ecat + import read_ecat + import helper_functions +except ImportError: + from pypet2bids.ecat import ecat + from pypet2bids.write_ecat import write_ecat + from pypet2bids.read_ecat import read_ecat + from pypet2bids import helper_functions + +# collect ecat header jsons +ecat_headers = read_ecat.ecat_header_maps.get('ecat_headers') + + +def update_ecat_header(ecat_file: str, new_values: dict): + """ + Update the header of an ECAT file with new values + :param ecat_file: path to the ECAT file + :param new_values: dictionary of new values to update + :param ecat_header: dictionary of the ECAT header + :return: None + """ + + # read ecat and determine version of ecat file + print(f"Reading ECAT file {ecat_file}") + infile = ecat.Ecat(ecat_file) + + # collect the appropriate header schema + sw_version = str(infile.ecat_header.get('SW_VERSION', 73)) + + infile_header_map = ecat_headers[sw_version]['mainheader'] + + # iterate through new values and update the header + for name, value in new_values.items(): + if infile.ecat_header.get(name): + if type(infile.ecat_header[name]) == type(value): + infile.ecat_header[name] = value + else: + print(f"WARNING: {name} has type {type(infile.ecat_header[name])} " + f"and new value {value} has type {type(value)}") + else: + print( + f"WARNING: {name} not found in header schema for ECAT {ecat_headers.ecat_header.get('SW_VERSION', 73)} " + f"not updating with value {value}") + + # update the header of the ecat file in question + with open(infile.ecat_file, 'r+b') as outfile: + write_ecat.write_header(ecat_file=outfile, + schema=infile_header_map, + values=infile.ecat_header) + + +def cli(): + import argparse + parser = argparse.ArgumentParser(description='Update the header of an ECAT file') + parser.add_argument('ecat_file', type=str, help='path to the ECAT file') + parser.add_argument('new_values', nargs='*', action=helper_functions.ParseKwargs, default={}, + help='new values to update the MAINHEADER of the ecat file, e.g. NUM_FRAMES=71 ' + 'or CALIBRATION_FACTOR=0.5. ' + 'or STUDY_DESCRIPTION="very important work"' + 'If the value is a string, it must be in quotes.') + args = parser.parse_args() + update_ecat_header(ecat_file=args.ecat_file, new_values=args.new_values) + + +if __name__ == '__main__': + cli() diff --git a/pypet2bids/pypet2bids/write_ecat.py b/pypet2bids/pypet2bids/write_ecat.py index 33b7f387..e9cd3b46 100644 --- a/pypet2bids/pypet2bids/write_ecat.py +++ b/pypet2bids/pypet2bids/write_ecat.py @@ -59,7 +59,7 @@ def write_header(ecat_file, schema: dict, values: dict = {}, byte_position: int value_to_write = values.get(variable_name, None) # if no value is supplied in the value dict, pack with empty bytes as well - if not value_to_write: + if value_to_write is None: pack_empty = True # set variable to false if neither of these conditions is met else: diff --git a/pypet2bids/pyproject.toml b/pypet2bids/pyproject.toml index aa5749b1..8f0b0c00 100644 --- a/pypet2bids/pyproject.toml +++ b/pypet2bids/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "pypet2bids" -version = "1.3.2" +version = "1.3.3" description = "A python implementation of an ECAT to BIDS converter." authors = ["anthony galassi <28850131+bendhouseart@users.noreply.github.com>"] license = "MIT" @@ -44,6 +44,7 @@ ispet = 'pypet2bids.is_pet:main' updatepetjsonfromdicom = 'pypet2bids.dcm2niix4pet:update_json_with_dicom_value_cli' updatepetjsonfromecat = 'pypet2bids.ecat_cli:update_json_with_ecat_value_cli' updatepetjson = 'pypet2bids.update_json:update_json_cli' +ecatheaderupdate = 'pypet2bids.ecat_header_update:main' [tool.poetry.group.dev.dependencies] pyinstaller = "^5.4.1"