Skip to content

Commit 9073d80

Browse files
committed
Allowed order to have any range
1 parent a93684e commit 9073d80

4 files changed

Lines changed: 12 additions & 13 deletions

File tree

src/decoupler/pl/_order.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def order(
5353
bp.ax.imshow(
5454
colors,
5555
aspect='auto',
56-
extent=[0, xmax, 1.05 * ymax, 1.2 * ymax],
56+
extent=[xmin, xmax, 1.05 * ymax, 1.2 * ymax],
5757
transform=bp.ax.transData,
5858
zorder=2
5959
)
@@ -75,15 +75,15 @@ def order(
7575
.groupby(['name', 'order'], as_index=False)['value'].mean()
7676
.pivot(index='name', columns='order', values='value')
7777
)
78-
img = bp.ax.imshow(mat, extent=[0, xmax, 0, n_names], aspect='auto', **kw_order)
78+
img = bp.ax.imshow(mat, extent=[xmin, xmax, 0, n_names], aspect='auto', **kw_order)
7979
if has_cbar:
80-
bp.ax.imshow(colors, aspect='auto', extent=[0, xmax, n_names, 1.1 * n_names], zorder=2)
80+
bp.ax.imshow(colors, aspect='auto', extent=[xmin, xmax, n_names, 1.1 * n_names], zorder=2)
8181
bp.ax.axhline(y=n_names, c='black', lw=1)
8282
bp.ax.set_ylim(0, 1.1 * n_names)
8383
bp.fig.colorbar(img, ax=bp.ax, shrink=0.5, label='Mean value', location='top')
8484
bp.ax.set_yticks(np.arange(n_names) + 0.5)
8585
bp.ax.set_yticklabels(np.flip(mat.index))
8686
bp.ax.grid(axis='y', visible=False)
8787
bp.ax.set_xlabel('order')
88-
bp.ax.set_xlim(0, 1)
88+
bp.ax.set_xlim(xmin, xmax)
8989
return bp._return()

src/decoupler/pl/_order_targets.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,11 @@ def order_targets(
140140
ax.grid(axis='y', visible=False)
141141
ax.set_ylabel('Targets')
142142
yticklabels = []
143+
# Find minmax
144+
omin, omax = df_ftr['order'].min(), df_ftr['order'].max()
143145
# Add neg targets
144146
if neg_names.size > 0:
145-
img = ax.imshow(mat.loc[neg_names], extent=[0, 1, 0, neg_names.size], aspect='auto', cmap=neg_cmap, vmin=vmin, vmax=vmax)
147+
img = ax.imshow(mat.loc[neg_names], extent=[omin, omax, 0, neg_names.size], aspect='auto', cmap=neg_cmap, vmin=vmin, vmax=vmax)
146148
yticklabels.extend(list(neg_names)[::-1])
147149
cbar_mappable = ScalarMappable(cmap=neg_cmap, norm=Normalize(vmin=vmin, vmax=vmax))
148150
pos = ax.get_position().bounds
@@ -152,22 +154,22 @@ def order_targets(
152154
ax.axhline(y=neg_names.size, c='black', lw=1)
153155
# Add pos targets
154156
if pos_names.size > 0:
155-
img = ax.imshow(mat.loc[pos_names], extent=[0, 1, neg_names.size, neg_names.size + pos_names.size], aspect='auto', cmap='Reds', vmin=vmin, vmax=vmax)
157+
img = ax.imshow(mat.loc[pos_names], extent=[omin, omax, neg_names.size, neg_names.size + pos_names.size], aspect='auto', cmap='Reds', vmin=vmin, vmax=vmax)
156158
yticklabels.extend(list(pos_names)[::-1])
157159
cbar_mappable = ScalarMappable(cmap=pos_cmap, norm=Normalize(vmin=vmin, vmax=vmax))
158160
pos = ax.get_position().bounds
159-
# (left, bottom, width, height)
160161
cax = bp.fig.add_axes([0.97, pos[1] + (pos[3] / 2) + .025, 0.05, (pos[3] / 2) - .025])
161162
cax.grid(axis='y', visible=False)
162163
bp.fig.colorbar(cbar_mappable, cax=cax, aspect=5, shrink=0.5, label='+ target\nvalues', location='right')
163164
# Plot labels
164165
ax.set_ylim(0, n_names)
165166
if has_cbar:
166-
ax.imshow(colors, aspect='auto', extent=[0, 1, n_names, 1.1 * n_names], zorder=2)
167+
ax.imshow(colors, aspect='auto', extent=[omin, omax, n_names, 1.1 * n_names], zorder=2)
167168
ax.axhline(y=n_names, c='black', lw=1)
168169
ax.set_ylim(0, 1.1 * n_names)
169170
# Format plot
170171
ax.set_yticks(np.arange(n_names) + 0.5)
171172
ax.set_yticklabels(yticklabels)
173+
ax.set_xlim(omin, omax)
172174
bp.fig.subplots_adjust(hspace=0)
173175
return bp._return()

src/decoupler/pp/anndata.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -707,10 +707,9 @@ def bin_order(
707707
if sps.issparse(X):
708708
X = X.toarray()
709709
y = adata.obs[order].values
710-
# Normalize to 0 and 1
711-
y = np.abs(y) / np.abs(y).max()
712710
# Make windows
713-
bin_edges = np.linspace(0, 1, nbins + 1)
711+
ymin, ymax = y.min(), y.max()
712+
bin_edges = np.linspace(start=ymin, stop=ymax, num=nbins + 1)
714713
bin_midpoints = (bin_edges[:-1] + bin_edges[1:]) / 2
715714
# Prepare label colors
716715
cols = ['name', 'midpoint', 'value']
@@ -739,5 +738,4 @@ def bin_order(
739738
df = pd.concat(dfs)
740739
df = df[cols]
741740
df = df.rename(columns={'midpoint': 'order'}).reset_index(drop=True)
742-
df['order'] = df['order'] / df['order'].max()
743741
return df

tests/pp/test_anndata.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,6 @@ def test_bin_order(
250250
cols = {'name', 'order', 'value'}
251251
assert cols.issubset(df.columns)
252252
assert ((0. <= df['order']) & (df['order'] <= 1.)).all()
253-
assert df['order'].unique().size == np.min([tdata.n_obs, nbins])
254253
if label is not None:
255254
lcols = {'label', 'color'}
256255
assert lcols.issubset(df.columns)

0 commit comments

Comments
 (0)