Skip to content

Commit 2628295

Browse files
authored
Merge pull request #104 from StefanoCretti/master
Toggleable pixel matrix aspect ratio
2 parents 73896b0 + 02c6770 commit 2628295

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

coolbox/core/frame/frame.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,17 @@ def get_tracks_height(self, default_height=3):
137137
heights = []
138138
for track in self.tracks.values():
139139
if hasattr(track, 'get_track_height'):
140-
frame_width = self.properties['width'] * self.properties['width_ratios'][1]
140+
# The actual width is given by multiplying:
141+
# - the actual width in units cm/inch
142+
# - the fraction of the figure not covered by margins
143+
# - the fraction of the grid allotted to the middle column
144+
margins = self.properties["margins"]
145+
margins_fraction = margins["right"] - margins["left"]
146+
frame_width = (
147+
self.properties["width"]
148+
* self.properties["width_ratios"][1]
149+
* margins_fraction
150+
)
141151
height = track.get_track_height(frame_width, self.current_range)
142152
heights.append(height)
143153
elif 'height' in track.properties:
@@ -186,8 +196,7 @@ def plot(self, *args, close_fig=True):
186196
grids = matplotlib.gridspec.GridSpec(
187197
len(tracks_height), 3,
188198
height_ratios=tracks_height,
189-
width_ratios=self.properties['width_ratios'],
190-
wspace=0.01)
199+
width_ratios=self.properties['width_ratios'])
191200

192201
axis_list = []
193202
for idx, track in enumerate(self.tracks.values()):

coolbox/core/track/hicmat/base.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ class HicMatBase(Track, PlotHiCMat, ProcessHicMat):
5454
process_func : {callable, str, False}, optional
5555
Process matrix with a user-defined function(receive a matrix, return a processed matrix). default False.
5656
57+
aspect_ratio : {'equal', 'auto'}, optional
58+
When set to 'equal', it ensures that matrix pixels are actually squares. When set to 'auto', it allows
59+
matrix pixels to be stretched to completely fill the subplot. Ignored when height parameter is
60+
provided. default 'equal'.
5761
5862
"""
5963

@@ -77,6 +81,7 @@ class HicMatBase(Track, PlotHiCMat, ProcessHicMat):
7781
'height': 'hic_auto',
7882
'cmap': "JuiceBoxLike",
7983
"color_bar": "vertical",
84+
"aspect_ratio": "equal",
8085
"max_value": "auto",
8186
"min_value": "auto",
8287
"depth_ratio": DEPTH_FULL,

coolbox/core/track/hicmat/plot.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ def get_cmap(color: str):
5858
ax = self.ax
5959
arr = self.matrix
6060
c_min, c_max = self.matrix_val_range
61+
62+
aspect = self.properties['aspect_ratio']
63+
if self.properties.get('height'):
64+
aspect = 'auto'
65+
6166
if gr2 is None and self.style == self.STYLE_TRIANGULAR:
6267
# triangular style
6368
scale_r = 1 / math.sqrt(2)
@@ -71,7 +76,7 @@ def get_cmap(color: str):
7176
img = ax.matshow(arr, cmap=cmap,
7277
transform=tr + ax.transData,
7378
extent=(gr.start, gr.end, gr.start, gr.end),
74-
aspect='auto')
79+
aspect=aspect)
7580
elif gr2 is None and self.style == self.STYLE_WINDOW:
7681
# window style
7782
# exist in HicMatBase
@@ -88,14 +93,14 @@ def get_cmap(color: str):
8893
img = ax.matshow(arr, cmap=cmap,
8994
transform=tr + ax.transData,
9095
extent=(gr.start, gr.end, gr.start, gr.end),
91-
aspect='auto')
96+
aspect=aspect)
9297
else:
9398
if gr2 is None:
9499
gr2 = gr
95100
# matrix style
96101
img = ax.matshow(arr, cmap=cmap,
97102
extent=(gr.start, gr.end, gr2.end, gr2.start),
98-
aspect='auto')
103+
aspect=aspect)
99104

100105
if self.norm == 'log':
101106
img.set_norm(colors.LogNorm(vmin=c_min, vmax=c_max))
@@ -192,7 +197,8 @@ def get_track_height(self, frame_width, *args):
192197
height = height * self.properties['depth_ratio']
193198

194199
if 'color_bar' in self.properties and self.properties['color_bar'] != 'no':
195-
height += 1.5
200+
if self.properties["aspect_ratio"] != 'equal':
201+
height += 1.5
196202

197203
return height
198204

0 commit comments

Comments
 (0)