Skip to content

Commit 69a22b1

Browse files
committed
CLN more compact syntax
1 parent fca59eb commit 69a22b1

1 file changed

Lines changed: 21 additions & 42 deletions

File tree

voxelwise_tutorials/viz.py

Lines changed: 21 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,10 @@ def plot_hist2d(scores_1, scores_2, bins=100, cmin=1, vmin=None, vmax=None,
4949
ax : Axes
5050
Matplotlib Axes where the histogram was plotted.
5151
"""
52-
if vmin is None:
53-
vmin = min(scores_1.min(), scores_2.min())
54-
if vmax is None:
55-
vmax = max(scores_1.max(), scores_2.max())
56-
if isinstance(bins, int):
57-
bins = np.linspace(vmin, vmax, bins)
58-
if ax is None:
59-
ax = plt.gca()
52+
vmin = min(scores_1.min(), scores_2.min()) if vmin is None else vmin
53+
vmax = max(scores_1.max(), scores_2.max()) if vmax is None else vmax
54+
bins = np.linspace(vmin, vmax, bins) if isinstance(bins, int) else bins
55+
ax = plt.gca() if ax is None else ax
6056

6157
h = ax.hist2d(scores_1, scores_2, bins=bins, cmin=cmin, norm=norm,
6258
**kwargs)
@@ -123,10 +119,8 @@ def plot_flatmap_from_mapper(voxels, mapper_file, ax=None, alpha=0.7,
123119
ax.axis('off')
124120

125121
# process plotting parameters
126-
if vmin is None:
127-
vmin = np.percentile(voxels, 1)
128-
if vmax is None:
129-
vmax = np.percentile(voxels, 99)
122+
vmin = np.percentile(voxels, 1) if vmin is None else vmin
123+
vmax = np.percentile(voxels, 99) if vmax is None else vmax
130124
if isinstance(alpha, np.ndarray):
131125
alpha = map_voxels_to_flatmap(alpha, mapper_file)
132126

@@ -284,14 +278,10 @@ def plot_2d_flatmap_from_mapper(voxels_1, voxels_2, mapper_file, ax=None,
284278
ax.axis('off')
285279

286280
# process plotting parameters
287-
if vmin is None:
288-
vmin = np.percentile(voxels_1, 1)
289-
if vmax is None:
290-
vmax = np.percentile(voxels_1, 99)
291-
if vmin2 is None:
292-
vmin2 = np.percentile(voxels_2, 1)
293-
if vmax2 is None:
294-
vmax2 = np.percentile(voxels_2, 99)
281+
vmin = np.percentile(voxels_1, 1) if vmin is None else vmin
282+
vmax = np.percentile(voxels_1, 99) if vmax is None else vmax
283+
vmin2 = np.percentile(voxels_2, 1) if vmin2 is None else vmin2
284+
vmax2 = np.percentile(voxels_2, 99) if vmax2 is None else vmax2
295285
if isinstance(alpha, np.ndarray):
296286
alpha = map_voxels_to_flatmap(alpha, mapper_file)
297287

@@ -354,10 +344,8 @@ def _map_to_2d_cmap(data1, data2, vmin, vmax, vmin2, vmax2, cmap):
354344
cmap_image = plt.imread(os.path.join(cmap_directory, "%s.png" % cmap))
355345

356346
# Normalize the data
357-
norm1 = Normalize(vmin, vmax)
358-
norm2 = Normalize(vmin2, vmax2)
359-
dim1 = np.clip(norm1(data1), 0, 1)
360-
dim2 = np.clip(1 - norm2(data2), 0, 1)
347+
dim1 = np.clip(Normalize(vmin, vmax)(data1), 0, 1)
348+
dim2 = np.clip(1 - Normalize(vmin2, vmax2)(data2), 0, 1)
361349

362350
# 2D indices of the data on the 2D cmap
363351
dim1 = np.round(dim1 * (cmap_image.shape[1] - 1))
@@ -438,28 +426,19 @@ def plot_3d_flatmap_from_mapper(voxels_1, voxels_2, voxels_3, mapper_file,
438426
ax.axis('off')
439427

440428
# process plotting parameters
441-
if vmin is None:
442-
vmin = np.percentile(voxels_1, 1)
443-
if vmax is None:
444-
vmax = np.percentile(voxels_1, 99)
445-
if vmin2 is None:
446-
vmin2 = np.percentile(voxels_2, 1)
447-
if vmax2 is None:
448-
vmax2 = np.percentile(voxels_2, 99)
449-
if vmin3 is None:
450-
vmin3 = np.percentile(voxels_3, 1)
451-
if vmax3 is None:
452-
vmax3 = np.percentile(voxels_3, 99)
429+
vmin = np.percentile(voxels_1, 1) if vmin is None else vmin
430+
vmax = np.percentile(voxels_1, 99) if vmax is None else vmax
431+
vmin2 = np.percentile(voxels_2, 1) if vmin2 is None else vmin2
432+
vmax2 = np.percentile(voxels_2, 99) if vmax2 is None else vmax2
433+
vmin3 = np.percentile(voxels_3, 1) if vmin3 is None else vmin3
434+
vmax3 = np.percentile(voxels_3, 99) if vmax3 is None else vmax3
453435
if isinstance(alpha, np.ndarray):
454436
alpha = map_voxels_to_flatmap(alpha, mapper_file)
455437

456438
# Normalize the data
457-
norm1 = Normalize(vmin, vmax)
458-
norm2 = Normalize(vmin2, vmax2)
459-
norm3 = Normalize(vmin3, vmax3)
460-
voxels_1 = np.clip(norm1(voxels_1), 0, 1)
461-
voxels_2 = np.clip(norm2(voxels_2), 0, 1)
462-
voxels_3 = np.clip(norm3(voxels_3), 0, 1)
439+
voxels_1 = np.clip(Normalize(vmin, vmax)(voxels_1), 0, 1)
440+
voxels_2 = np.clip(Normalize(vmin2, vmax2)(voxels_2), 0, 1)
441+
voxels_3 = np.clip(Normalize(vmin3, vmax3)(voxels_3), 0, 1)
463442

464443
# Preserve nan values with alpha = 0
465444
nans = np.isnan(voxels_1) + np.isnan(voxels_2) + np.isnan(voxels_3)

0 commit comments

Comments
 (0)