Skip to content

Commit

Permalink
fixed centre of mass
Browse files Browse the repository at this point in the history
  • Loading branch information
pjmark committed Aug 17, 2023
2 parents 913174e + 7c99959 commit 3734084
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions niftypet/nimpa/prc/prc.py
Original file line number Diff line number Diff line change
Expand Up @@ -894,20 +894,23 @@ def centre_mass_img(img, output='mm'):
# > total image sum
imsum = np.sum(imdct['im'])

for ind_ax in [-1, -2, -3]:
# > list of axes
axs = list(range(imdct['im'].ndim))
del axs[ind_ax]
# > indexed centre of mass
icom[ind_ax] = np.sum(
np.sum(imdct['im'], axis=tuple(axs)) * np.arange(imdct['shape'][ind_ax])) / imsum
# > centre of mass in mm (zyx)
com[ind_ax] = icom[ind_ax] * imdct['voxsize'][ind_ax]

# > correct due to flipped indexing and world mm
com[-1] = imdct['shape'][-1] * imdct['voxsize'][-1] - com[-1]
com[-2] = imdct['shape'][-2] * imdct['voxsize'][-2] - com[-2]
com[-3] = imdct['shape'][-3] * imdct['voxsize'][-3] - com[-3]
if imsum>0:

Check failure on line 897 in niftypet/nimpa/prc/prc.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] niftypet/nimpa/prc/prc.py#L897

E225 missing whitespace around operator
Raw output
niftypet/nimpa/prc/prc.py:897:13: E225 missing whitespace around operator
for ind_ax in [-1, -2, -3]:
# > list of axes
axs = list(range(imdct['im'].ndim))
del axs[ind_ax]
# > indexed centre of mass
icom[ind_ax] = np.sum(
np.sum(imdct['im'], axis=tuple(axs)) * np.arange(imdct['shape'][ind_ax])) / imsum
# > centre of mass in mm (zyx)
com[ind_ax] = icom[ind_ax] * imdct['voxsize'][ind_ax]

# > correct due to flipped indexing and world mm
com[-1] = imdct['shape'][-1] * imdct['voxsize'][-1] - com[-1]
com[-2] = imdct['shape'][-2] * imdct['voxsize'][-2] - com[-2]
com[-3] = imdct['shape'][-3] * imdct['voxsize'][-3] - com[-3]
else:
return None

if output == 'mm':
return com
Expand Down Expand Up @@ -1000,10 +1003,14 @@ def centre_mass_corr(img, Cnt=None, com=None, flip=None, outpath=None, fcomment=
if Cnt is None:
Cnt = {}

# > output the centre of mass if image radiodistribution in each dimension in mm.
# > output the centre of mass of image radiodistribution in each dimension in mm.
if com is None:
com = centre_mass_img(imdct, output='mm')

if com is None:
log.warning('Centre of mass of the image could not be found - returning `None`')
return None

com = np.array(com)

if not isinstance(com, np.ndarray):
Expand Down

0 comments on commit 3734084

Please sign in to comment.