-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #131 from jhlegarreta/FixHemisphereLocationArrayNa…
…meTpo STYLE: Fix typo in hemisphere location array name
- Loading branch information
Showing
6 changed files
with
166 additions
and
27 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
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
7 changes: 7 additions & 0 deletions
7
utilities/tests/test_wm_fix_hemisphere_loc_name_in_tractogram.py
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,7 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
def test_help_option(script_runner): | ||
ret = script_runner.run( | ||
["wm_fix_hemisphere_loc_name_in_tractogram.py", "--help"]) | ||
assert ret.success |
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,91 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
"""Fix hemisphere location property name in tractogram: older versions of WMA | ||
contain a typo in the hemisphere location VTK array scalar property. Change | ||
`HemisphereLocataion` to `HemisphereLocation`. | ||
""" | ||
|
||
import os | ||
import argparse | ||
|
||
import vtk | ||
|
||
import whitematteranalysis as wma | ||
|
||
|
||
def fix_hemisphere_location_property_name(polydata): | ||
|
||
out_polydata = vtk.vtkPolyData() | ||
point_data = polydata.GetPointData() | ||
|
||
if point_data.GetNumberOfArrays(): | ||
point_data_array_indices = list(range(point_data.GetNumberOfArrays())) | ||
|
||
for idx in point_data_array_indices: | ||
array = point_data.GetArray(idx) | ||
|
||
if array.GetName() == "HemisphereLocataion": | ||
print("HemisphereLocataion is in the input data: re-writing to HemisphereLocation.") | ||
elif array.GetName() == "HemisphereLocation": | ||
print("HemisphereLocation is in the input data: no re-write needed.") | ||
else: | ||
print("Hemisphere location property is not in the input data: no re-write needed.") | ||
|
||
vtk_array = vtk.vtkDoubleArray() | ||
vtk_array.SetName("HemisphereLocation") | ||
|
||
polydata.GetLines().InitTraversal() | ||
|
||
for l_idx in range(polydata.GetNumberOfLines()): | ||
point_ids = vtk.vtkIdList() | ||
polydata.GetLines().GetNextCell(point_ids) | ||
|
||
for p_idx in range(point_ids.GetNumberOfIds()): | ||
vtk_array.InsertNextTuple1(l_idx) | ||
|
||
out_polydata.GetPointData().AddArray(vtk_array) | ||
|
||
return out_polydata | ||
|
||
|
||
def _build_arg_parser(): | ||
|
||
parser = argparse.ArgumentParser( | ||
description=__doc__, formatter_class=argparse.RawTextHelpFormatter | ||
) | ||
parser.add_argument('in_fname', help='Input filename (.*vtk|.*vtp).') | ||
parser.add_argument('out_fname', help='Output filename (.*vtk|.*vtp).') | ||
|
||
return parser | ||
|
||
|
||
def _parse_args(parser): | ||
|
||
args = parser.parse_args() | ||
return args | ||
|
||
|
||
def main(): | ||
|
||
parser = _build_arg_parser() | ||
args = _parse_args(parser) | ||
|
||
out_fname = args.outputFilename | ||
if os.path.exists(out_fname): | ||
msg = f"Output file {out_fname} exists. Remove or rename the output file." | ||
parser.error(msg) | ||
|
||
print(f"{os.path.basename(__file__)}. Starting.") | ||
print("") | ||
print("=====input filename======\n", args.in_fname) | ||
print("=====output filename=====\n", args.out_fname) | ||
print("==========================") | ||
|
||
polydata = wma.io.read_polydata(args.in_fname) | ||
out_polydata = fix_hemisphere_location_property_name(polydata) | ||
wma.io.write_polydata(out_polydata, args.out_fname) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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,3 +1,3 @@ | ||
print("Importing whitematteranalysis package.") | ||
from . import io, similarity, fibers, filter, laterality, render, cluster, relative_distance, mrml, congeal_multisubject, register_two_subjects, register_two_subjects_nonrigid, register_two_subjects_nonrigid_bsplines, congeal_to_atlas, tract_measurement | ||
from . import io, similarity, fibers, filter, laterality, render, cluster, relative_distance, mrml, congeal_multisubject, register_two_subjects, register_two_subjects_nonrigid, register_two_subjects_nonrigid_bsplines, congeal_to_atlas, tract_measurement, utils | ||
|
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,8 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
hemisphere_loc_name_typo_warn_msg = ( | ||
'Hemisphere location scalar is described by the HemisphereLocataion name, ' | ||
'which contains a typo. Reading files that contain the typo will not be ' | ||
'supported in future releases. Use whitematteranalysis to re-write such ' | ||
'files.') |