-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added small script to update ecat mainheader with, bump version (#270)
fixes issue #264
- Loading branch information
1 parent
b649d8b
commit 86a8f7e
Showing
3 changed files
with
71 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
@@ -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" | ||
|