Skip to content

Commit 94745a2

Browse files
committed
STY: Style fixes
1 parent fa05777 commit 94745a2

File tree

2 files changed

+59
-57
lines changed

2 files changed

+59
-57
lines changed

nibabel/nicom/dicomwrappers.py

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -470,9 +470,7 @@ def __init__(self, dcm_data):
470470
# Try to determine slice order and minimal image position patient
471471
self._frame_slc_ord = self._ipp = None
472472
try:
473-
frame_ipps = [
474-
f.PlanePositionSequence[0].ImagePositionPatient for f in self.frames
475-
]
473+
frame_ipps = [f.PlanePositionSequence[0].ImagePositionPatient for f in self.frames]
476474
except AttributeError:
477475
try:
478476
frame_ipps = [self.shared.PlanePositionSequence[0].ImagePositionPatient]
@@ -483,7 +481,9 @@ def __init__(self, dcm_data):
483481
frame_slc_pos = [np.inner(ipp, self.slice_normal) for ipp in frame_ipps]
484482
rnd_slc_pos = np.round(frame_slc_pos, 4)
485483
uniq_slc_pos = np.unique(rnd_slc_pos)
486-
pos_ord_map = {val: order for val, order in zip(uniq_slc_pos, np.argsort(uniq_slc_pos))}
484+
pos_ord_map = {
485+
val: order for val, order in zip(uniq_slc_pos, np.argsort(uniq_slc_pos))
486+
}
487487
self._frame_slc_ord = [pos_ord_map[pos] for pos in rnd_slc_pos]
488488
self._ipp = frame_ipps[np.argmin(frame_slc_pos)]
489489
self._shape = None
@@ -592,35 +592,32 @@ def image_shape(self):
592592
# Replace slice indices with order determined from slice positions along normal
593593
if row_idx == slice_dim_idx:
594594
if len(shape) > 2:
595-
raise WrapperError("Non-singular index precedes the slice index")
595+
raise WrapperError('Non-singular index precedes the slice index')
596596
row = self._frame_slc_ord
597597
frame_indices.T[row_idx, :] = row
598598
unique = np.unique(row)
599599
if len(unique) != count:
600600
raise WrapperError("Number of slice indices and positions don't match")
601601
elif count == n_frames:
602-
if shape[-1] == "remaining":
603-
raise WrapperError("At most one index have ambiguous size")
604-
shape.append("remaining")
602+
if shape[-1] == 'remaining':
603+
raise WrapperError('At most one index have ambiguous size')
604+
shape.append('remaining')
605605
continue
606606
new_parts, leftover = divmod(curr_parts, count)
607607
expected = new_parts * frames_per_part
608-
if (
609-
leftover != 0 or
610-
any(np.count_nonzero(row == val) != expected for val in unique)
611-
):
608+
if leftover != 0 or any(np.count_nonzero(row == val) != expected for val in unique):
612609
if row_idx == slice_dim_idx:
613-
raise WrapperError("Missing slices from multiframe")
610+
raise WrapperError('Missing slices from multiframe')
614611
del_indices[row_idx] = count
615612
continue
616-
if shape[-1] == "remaining":
613+
if shape[-1] == 'remaining':
617614
shape[-1] = new_parts
618615
frames_per_part *= shape[-1]
619616
new_parts = 1
620617
frames_per_part *= count
621618
shape.append(count)
622619
curr_parts = new_parts
623-
if shape[-1] == "remaining":
620+
if shape[-1] == 'remaining':
624621
if curr_parts > 1:
625622
shape[-1] = curr_parts
626623
curr_parts = 1
@@ -633,7 +630,7 @@ def image_shape(self):
633630
if len(ns_failed) > 1:
634631
# If some indices weren't used yet but we still have unaccounted for
635632
# partitions, try combining indices into single tuple and using that
636-
tup_dtype = np.dtype(','.join(["I"] * len(ns_failed)))
633+
tup_dtype = np.dtype(','.join(['I'] * len(ns_failed)))
637634
row = [tuple(x for x in vals) for vals in frame_indices[:, ns_failed]]
638635
row = np.array(row, dtype=tup_dtype)
639636
frame_indices = np.delete(frame_indices, np.array(list(del_indices.keys())), axis=1)
@@ -642,18 +639,19 @@ def image_shape(self):
642639
count = len(unique)
643640
new_parts, rem = divmod(curr_parts, count)
644641
allowed_val_counts = [new_parts * frames_per_part, n_frames]
645-
if (
646-
rem == 0 and
647-
all(np.count_nonzero(row == val) in allowed_val_counts for val in unique)
642+
if rem == 0 and all(
643+
np.count_nonzero(row == val) in allowed_val_counts for val in unique
648644
):
649645
shape.append(count)
650646
curr_parts = new_parts
651647
ord_vals = np.argsort(unique)
652648
order = {tuple(unique[i]): ord_vals[i] for i in range(count)}
653649
ord_row = np.array([order[tuple(v)] for v in row])
654-
frame_indices = np.hstack([frame_indices, np.array(ord_row).reshape((n_frames, 1))])
650+
frame_indices = np.hstack(
651+
[frame_indices, np.array(ord_row).reshape((n_frames, 1))]
652+
)
655653
if curr_parts > 1:
656-
raise WrapperError("Unable to determine sorting of final dimension(s)")
654+
raise WrapperError('Unable to determine sorting of final dimension(s)')
657655
# Store frame indices
658656
self._frame_indices = frame_indices
659657
return tuple(shape)

nibabel/nicom/tests/test_dicomwrappers.py

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -398,11 +398,11 @@ class Fake:
398398

399399

400400
def fake_shape_dependents(
401-
div_seq,
402-
sid_seq=None,
403-
sid_dim=None,
404-
ipp_seq=None,
405-
slice_dim=None,
401+
div_seq,
402+
sid_seq=None,
403+
sid_dim=None,
404+
ipp_seq=None,
405+
slice_dim=None,
406406
flip_ipp_idx_corr=False,
407407
):
408408
"""Make a fake dictionary of data that ``image_shape`` is dependent on.
@@ -431,7 +431,7 @@ def __repr__(self):
431431
attr_strs = []
432432
for attr in dir(self):
433433
if attr[0].isupper():
434-
attr_strs.append(f"{attr}={getattr(self, attr)}")
434+
attr_strs.append(f'{attr}={getattr(self, attr)}')
435435
return f"{self.__class__.__name__}({', '.join(attr_strs)})"
436436

437437
class DimIdxSeqElem(PrintBase):
@@ -444,11 +444,11 @@ class FrmContSeqElem(PrintBase):
444444
def __init__(self, div, sid):
445445
self.DimensionIndexValues = div
446446
self.StackID = sid
447-
447+
448448
class PlnPosSeqElem(PrintBase):
449449
def __init__(self, ipp):
450450
self.ImagePositionPatient = ipp
451-
451+
452452
class PlnOrientSeqElem(PrintBase):
453453
def __init__(self, iop):
454454
self.ImageOrientationPatient = iop
@@ -466,27 +466,25 @@ def __init__(self, div, sid, ipp, iop):
466466
if sid_dim is None:
467467
sid_dim = 0
468468
sid_seq = [div[sid_dim] for div in div_seq]
469-
# Dertermine slice_dim and create per-slice ipp information
469+
# Determine slice_dim and create per-slice ipp information
470470
if slice_dim is None:
471471
slice_dim = 1 if sid_dim == 0 else 0
472472
num_of_frames = len(div_seq)
473473
frame_slc_indices = np.array(div_seq)[:, slice_dim]
474474
uniq_slc_indices = np.unique(frame_slc_indices)
475475
n_slices = len(uniq_slc_indices)
476476
assert num_of_frames % n_slices == 0
477-
n_vols = num_of_frames // n_slices
478-
iop_seq = [(0., 1., 0., 1., 0., 0.) for _ in range(num_of_frames)]
477+
iop_seq = [(0.0, 1.0, 0.0, 1.0, 0.0, 0.0) for _ in range(num_of_frames)]
479478
if ipp_seq is None:
480-
slc_locs = np.linspace(-1., 1., n_slices)
479+
slc_locs = np.linspace(-1.0, 1.0, n_slices)
481480
if flip_ipp_idx_corr:
482481
slc_locs = slc_locs[::-1]
483482
slc_idx_loc = {
484-
div_idx : slc_locs[arr_idx]
485-
for arr_idx, div_idx in enumerate(np.sort(uniq_slc_indices))
483+
div_idx: slc_locs[arr_idx] for arr_idx, div_idx in enumerate(np.sort(uniq_slc_indices))
486484
}
487-
ipp_seq = [(-1., -1., slc_idx_loc[idx]) for idx in frame_slc_indices]
485+
ipp_seq = [(-1.0, -1.0, slc_idx_loc[idx]) for idx in frame_slc_indices]
488486
else:
489-
assert flip_ipp_idx_corr is False # caller can flip it themselves
487+
assert flip_ipp_idx_corr is False # caller can flip it themselves
490488
assert len(ipp_seq) == num_of_frames
491489
# create the DimensionIndexSequence
492490
dim_idx_seq = [DimIdxSeqElem()] * n_indices
@@ -500,7 +498,7 @@ def __init__(self, div, sid, ipp, iop):
500498
dim_idx_seq[sid_dim] = DimIdxSeqElem(sid_tag, fcs_tag)
501499
# create the PerFrameFunctionalGroupsSequence
502500
frames = [
503-
PerFrmFuncGrpSeqElem(div, sid, ipp, iop)
501+
PerFrmFuncGrpSeqElem(div, sid, ipp, iop)
504502
for div, sid, ipp, iop in zip(div_seq, sid_seq, ipp_seq, iop_seq)
505503
]
506504
return {
@@ -618,9 +616,14 @@ def test_shape(self):
618616
assert MFW(fake_mf).image_shape == (32, 64, 2, 3)
619617
# Test with combo indices, here with the last two needing to be combined into
620618
# a single index corresponding to [(1, 1), (1, 1), (2, 1), (2, 1), (2, 2), (2, 2)]
621-
div_seq = ((1, 1, 1, 1), (1, 2, 1, 1),
622-
(1, 1, 2, 1), (1, 2, 2, 1),
623-
(1, 1, 2, 2), (1, 2, 2, 2))
619+
div_seq = (
620+
(1, 1, 1, 1),
621+
(1, 2, 1, 1),
622+
(1, 1, 2, 1),
623+
(1, 2, 2, 1),
624+
(1, 1, 2, 2),
625+
(1, 2, 2, 2),
626+
)
624627
fake_mf.update(fake_shape_dependents(div_seq, sid_dim=0))
625628
assert MFW(fake_mf).image_shape == (32, 64, 2, 3)
626629
# Test invalid 4D indices
@@ -636,13 +639,20 @@ def test_shape(self):
636639
div_seq = ((1, 1, 1), (1, 2, 2), (1, 1, 3), (1, 2, 4), (1, 1, 5), (1, 2, 6))
637640
fake_mf.update(fake_shape_dependents(div_seq, sid_dim=0))
638641
assert MFW(fake_mf).image_shape == (32, 64, 2, 3)
639-
div_seq = ((1, 1, 1, 1), (1, 2, 2, 1),
640-
(1, 1, 3, 1), (1, 2, 4, 1),
641-
(1, 1, 5, 1), (1, 2, 6, 1),
642-
(1, 1, 7, 2), (1, 2, 8, 2),
643-
(1, 1, 9, 2), (1, 2, 10, 2),
644-
(1, 1, 11, 2), (1, 2, 12, 2),
645-
)
642+
div_seq = (
643+
(1, 1, 1, 1),
644+
(1, 2, 2, 1),
645+
(1, 1, 3, 1),
646+
(1, 2, 4, 1),
647+
(1, 1, 5, 1),
648+
(1, 2, 6, 1),
649+
(1, 1, 7, 2),
650+
(1, 2, 8, 2),
651+
(1, 1, 9, 2),
652+
(1, 2, 10, 2),
653+
(1, 1, 11, 2),
654+
(1, 2, 12, 2),
655+
)
646656
fake_mf.update(fake_shape_dependents(div_seq, sid_dim=0))
647657
assert MFW(fake_mf).image_shape == (32, 64, 2, 3, 2)
648658

@@ -713,9 +723,7 @@ def test_image_position(self):
713723
dw.image_position
714724
# Make a fake frame
715725
iop = [0, 1, 0, 1, 0, 0]
716-
frames = fake_frames(
717-
'PlaneOrientationSequence', 'ImageOrientationPatient', [iop]
718-
)
726+
frames = fake_frames('PlaneOrientationSequence', 'ImageOrientationPatient', [iop])
719727
frames = fake_frames(
720728
'PlanePositionSequence', 'ImagePositionPatient', [[-2.0, 3.0, 7]], frames
721729
)
@@ -733,13 +741,9 @@ def test_image_position(self):
733741
assert_array_equal(MFW(fake_mf).image_position, [-2, 3, 7])
734742
assert MFW(fake_mf).image_position.dtype == float
735743
# We should get minimum along slice normal with multiple frames
736-
frames = fake_frames(
737-
'PlaneOrientationSequence', 'ImageOrientationPatient', [iop] * 2
738-
)
744+
frames = fake_frames('PlaneOrientationSequence', 'ImageOrientationPatient', [iop] * 2)
739745
ipps = [[-2.0, 3.0, 7], [-2.0, 3.0, 6]]
740-
frames = fake_frames(
741-
'PlanePositionSequence', 'ImagePositionPatient', ipps, frames
742-
)
746+
frames = fake_frames('PlanePositionSequence', 'ImagePositionPatient', ipps, frames)
743747
fake_mf['PerFrameFunctionalGroupsSequence'] = frames
744748
assert_array_equal(MFW(fake_mf).image_position, [-2, 3, 6])
745749

0 commit comments

Comments
 (0)