Skip to content

Commit

Permalink
added small script to update ecat mainheader with, bump version (#270)
Browse files Browse the repository at this point in the history
fixes issue #264
  • Loading branch information
bendhouseart authored Feb 13, 2024
1 parent b649d8b commit 86a8f7e
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
68 changes: 68 additions & 0 deletions pypet2bids/pypet2bids/ecat_header_update.py
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 1 addition & 1 deletion pypet2bids/pypet2bids/write_ecat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 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.3.2"
version = "1.3.3"
description = "A python implementation of an ECAT to BIDS converter."
authors = ["anthony galassi <[email protected]>"]
license = "MIT"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 86a8f7e

Please sign in to comment.