add sienax report parser python script
I have written a sienax report parser for use in an FSL Clinical web service. I think it would be useful to include this in the base FSL Siena project as well.
@mwebster and @paulmc, if I add this as a python script is the only other required change to add it to the SCRIPTS list in the Makefile
? (and add tests)
It's a simple function (and could be a standalone script):
def parse_sienax_report(file_path):
'''
parse the sienax report into a list of dicts per tissue
args:
file_path (str): path to the file
returns:
list: list of dicts per tissue
'''
# read the file lines in
with open(file_path, 'r') as f:
lines = f.readlines()
# reverse the lines since the info we want is at the bottom
lines.reverse()
# this is the format we are working with:
# ---------- convert brain volume into normalised volume --------------
# tissue volume unnormalised-volume
# pgrey 679021.25 529191.55 (peripheral grey)
# vcsf 25947.68 20222.18 (ventricular CSF)
# GREY 842801.25 656832.60
# WHITE 744730.03 580401.32
# BRAIN 1587531.28 1237233.92
results = []
# loop through the lines and parse the info by whitespace. Stop the loop when we encounter "tissue"
for line in lines:
if line.startswith("tissue"):
break
# split the line by whitespace
line = line.split()
# if the line is empty, skip it
if len(line) == 0:
continue
# if the line is not empty, print the first and second element of the line
else:
results.append({
"tissue": line[0],
"volume": line[1],
"unnormalised_volume": line[2]
})
# loop once more to get the scale factor VSCALING
for line in lines:
if line.startswith("VSCALING"):
line = line.split()
scale_factor = line[1]
break
# loop over all results and add the scale factor to each result
for result in results:
result["scale_factor"] = scale_factor
return results
The output list looks like this:
[
{
"scale_factor": "1.2880594580",
"tissue": "BRAIN",
"unnormalised_volume": "1212612.62",
"volume": "1561917.16"
},
{
"scale_factor": "1.2880594580",
"tissue": "WHITE",
"unnormalised_volume": "574080.99",
"volume": "739450.45"
},
{
"scale_factor": "1.2880594580",
"tissue": "GREY",
"unnormalised_volume": "638531.63",
"volume": "822466.71"
},
{
"scale_factor": "1.2880594580",
"tissue": "vcsf",
"unnormalised_volume": "21088.85",
"volume": "27163.70"
},
{
"scale_factor": "1.2880594580",
"tissue": "pgrey",
"unnormalised_volume": "514387.07",
"volume": "662561.13"
}
]
Edited by Taylor Hanayik