Skip to content

Commit

Permalink
Merge pull request #131 from jhlegarreta/FixHemisphereLocationArrayNa…
Browse files Browse the repository at this point in the history
…meTpo

STYLE: Fix typo in hemisphere location array name
  • Loading branch information
ljod authored Oct 19, 2023
2 parents eb36051 + 10e98e9 commit 1c67a29
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 27 deletions.
44 changes: 30 additions & 14 deletions bin/wm_assess_cluster_location_by_hemisphere.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
import shutil
import vtk
import glob
import warnings

import whitematteranalysis as wma



def _build_arg_parser():

parser = argparse.ArgumentParser(
Expand Down Expand Up @@ -77,11 +77,17 @@ def write_mask_location_to_vtk(inpd, mask_location):
for idx in point_data_array_indices:
array = inpointdata.GetArray(idx)
if array.GetName() == 'HemisphereLocataion':
warnings.warn(
wma.utils.hemisphere_loc_name_typo_warn_msg,
PendingDeprecationWarning)
print(' -- HemisphereLocataion is in the input data: skip updating the vtk file.')
return inpd
elif array.GetName() == 'HemisphereLocation':
print(' -- HemisphereLocation is in the input data: skip updating the vtk file.')
return inpd

vtk_array = vtk.vtkDoubleArray()
vtk_array.SetName('HemisphereLocataion')
vtk_array.SetName('HemisphereLocation')

inpd.GetLines().InitTraversal()
for lidx in range(0, inpd.GetNumberOfLines()):
Expand All @@ -96,7 +102,19 @@ def write_mask_location_to_vtk(inpd, mask_location):
return inpd

def read_mask_location_from_vtk(inpd):


def _read_location(_inpd):
_flag_location = True

_inpd.GetLines().InitTraversal()
for lidx in range(0, _inpd.GetNumberOfLines()):
ptids = vtk.vtkIdList()
_inpd.GetLines().GetNextCell(ptids)
for pidx in range(0, ptids.GetNumberOfIds()):
mask_location[lidx] = array.GetTuple(ptids.GetId(pidx))[0]

return _flag_location

mask_location = numpy.zeros(pd.GetNumberOfLines())

inpointdata = inpd.GetPointData()
Expand All @@ -106,17 +124,15 @@ def read_mask_location_from_vtk(inpd):
for idx in point_data_array_indices:
array = inpointdata.GetArray(idx)
if array.GetName() == 'HemisphereLocataion':
flag_location = True

inpd.GetLines().InitTraversal()
for lidx in range(0, inpd.GetNumberOfLines()):
ptids = vtk.vtkIdList()
inpd.GetLines().GetNextCell(ptids)
for pidx in range(0, ptids.GetNumberOfIds()):
mask_location[lidx] = array.GetTuple(ptids.GetId(pidx))[0]

warnings.warn(
wma.utils.hemisphere_loc_name_typo_warn_msg,
PendingDeprecationWarning)
flag_location = _read_location(inpd)
break

elif array.GetName() == 'HemisphereLocation':
flag_location = _read_location(inpd)
break

return flag_location, mask_location

parser = _build_arg_parser()
Expand Down Expand Up @@ -214,7 +230,7 @@ def read_mask_location_from_vtk(inpd):

flag_location, mask_location = read_mask_location_from_vtk(pd)

# If HemisphereLocataion is not defined in the input vtk file, the location of each fiber in the cluster is decided.
# If HemisphereLocation is not defined in the input vtk file, the location of each fiber in the cluster is decided.
if not flag_location:
if clusterLocationFile is None:
# internal representation for fast similarity computation
Expand Down
41 changes: 29 additions & 12 deletions bin/wm_separate_clusters_by_hemisphere.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import shutil
import vtk
import glob
import warnings

import whitematteranalysis as wma

Expand Down Expand Up @@ -88,11 +89,17 @@ def write_mask_location_to_vtk(inpd, mask_location):
for idx in point_data_array_indices:
array = inpointdata.GetArray(idx)
if array.GetName() == 'HemisphereLocataion':
warnings.warn(
wma.utils.hemisphere_loc_name_typo_warn_msg,
PendingDeprecationWarning)
print(' -- HemisphereLocataion is in the input data: skip updating the vtk file.')
return inpd
elif array.GetName() == 'HemisphereLocation':
print(' -- HemisphereLocation is in the input data: skip updating the vtk file.')
return inpd

vtk_array = vtk.vtkDoubleArray()
vtk_array.SetName('HemisphereLocataion')
vtk_array.SetName('HemisphereLocation')

inpd.GetLines().InitTraversal()
for lidx in range(0, inpd.GetNumberOfLines()):
Expand All @@ -107,7 +114,19 @@ def write_mask_location_to_vtk(inpd, mask_location):
return inpd

def read_mask_location_from_vtk(inpd):


def _read_location(_inpd):
_flag_location = True

_inpd.GetLines().InitTraversal()
for lidx in range(0, _inpd.GetNumberOfLines()):
ptids = vtk.vtkIdList()
_inpd.GetLines().GetNextCell(ptids)
for pidx in range(0, ptids.GetNumberOfIds()):
mask_location[lidx] = array.GetTuple(ptids.GetId(pidx))[0]

return _flag_location

mask_location = numpy.zeros(pd.GetNumberOfLines())

inpointdata = inpd.GetPointData()
Expand All @@ -117,15 +136,13 @@ def read_mask_location_from_vtk(inpd):
for idx in point_data_array_indices:
array = inpointdata.GetArray(idx)
if array.GetName() == 'HemisphereLocataion':
flag_location = True

inpd.GetLines().InitTraversal()
for lidx in range(0, inpd.GetNumberOfLines()):
ptids = vtk.vtkIdList()
inpd.GetLines().GetNextCell(ptids)
for pidx in range(0, ptids.GetNumberOfIds()):
mask_location[lidx] = array.GetTuple(ptids.GetId(pidx))[0]

warnings.warn(
wma.utils.hemisphere_loc_name_typo_warn_msg,
PendingDeprecationWarning)
flag_location = _read_location(inpd)
break
elif array.GetName() == 'HemisphereLocation':
flag_location = _read_location(inpd)
break

return flag_location, mask_location
Expand Down Expand Up @@ -160,7 +177,7 @@ def read_mask_location_from_vtk(inpd):

flag_location, mask_location = read_mask_location_from_vtk(pd)

# If HemisphereLocataion is not defined in the input vtk file, the location of each fiber in the cluster is decided.
# If HemisphereLocation is not defined in the input vtk file, the location of each fiber in the cluster is decided.
if not flag_location:
print("Error:", fname, "has no hemisphere location information")
exit()
Expand Down
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
91 changes: 91 additions & 0 deletions utilities/wm_fix_hemisphere_loc_name_in_tractogram.py
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()
2 changes: 1 addition & 1 deletion whitematteranalysis/__init__.py
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

8 changes: 8 additions & 0 deletions whitematteranalysis/utils.py
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.')

0 comments on commit 1c67a29

Please sign in to comment.