Skip to content

Commit

Permalink
Merge pull request #104 from StefanoCretti/master
Browse files Browse the repository at this point in the history
Toggleable pixel matrix aspect ratio
  • Loading branch information
Nanguage authored Feb 12, 2025
2 parents 73896b0 + 02c6770 commit 2628295
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
15 changes: 12 additions & 3 deletions coolbox/core/frame/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,17 @@ def get_tracks_height(self, default_height=3):
heights = []
for track in self.tracks.values():
if hasattr(track, 'get_track_height'):
frame_width = self.properties['width'] * self.properties['width_ratios'][1]
# The actual width is given by multiplying:
# - the actual width in units cm/inch
# - the fraction of the figure not covered by margins
# - the fraction of the grid allotted to the middle column
margins = self.properties["margins"]
margins_fraction = margins["right"] - margins["left"]
frame_width = (
self.properties["width"]
* self.properties["width_ratios"][1]
* margins_fraction
)
height = track.get_track_height(frame_width, self.current_range)
heights.append(height)
elif 'height' in track.properties:
Expand Down Expand Up @@ -186,8 +196,7 @@ def plot(self, *args, close_fig=True):
grids = matplotlib.gridspec.GridSpec(
len(tracks_height), 3,
height_ratios=tracks_height,
width_ratios=self.properties['width_ratios'],
wspace=0.01)
width_ratios=self.properties['width_ratios'])

axis_list = []
for idx, track in enumerate(self.tracks.values()):
Expand Down
5 changes: 5 additions & 0 deletions coolbox/core/track/hicmat/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ class HicMatBase(Track, PlotHiCMat, ProcessHicMat):
process_func : {callable, str, False}, optional
Process matrix with a user-defined function(receive a matrix, return a processed matrix). default False.
aspect_ratio : {'equal', 'auto'}, optional
When set to 'equal', it ensures that matrix pixels are actually squares. When set to 'auto', it allows
matrix pixels to be stretched to completely fill the subplot. Ignored when height parameter is
provided. default 'equal'.
"""

Expand All @@ -77,6 +81,7 @@ class HicMatBase(Track, PlotHiCMat, ProcessHicMat):
'height': 'hic_auto',
'cmap': "JuiceBoxLike",
"color_bar": "vertical",
"aspect_ratio": "equal",
"max_value": "auto",
"min_value": "auto",
"depth_ratio": DEPTH_FULL,
Expand Down
14 changes: 10 additions & 4 deletions coolbox/core/track/hicmat/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ def get_cmap(color: str):
ax = self.ax
arr = self.matrix
c_min, c_max = self.matrix_val_range

aspect = self.properties['aspect_ratio']
if self.properties.get('height'):
aspect = 'auto'

if gr2 is None and self.style == self.STYLE_TRIANGULAR:
# triangular style
scale_r = 1 / math.sqrt(2)
Expand All @@ -71,7 +76,7 @@ def get_cmap(color: str):
img = ax.matshow(arr, cmap=cmap,
transform=tr + ax.transData,
extent=(gr.start, gr.end, gr.start, gr.end),
aspect='auto')
aspect=aspect)
elif gr2 is None and self.style == self.STYLE_WINDOW:
# window style
# exist in HicMatBase
Expand All @@ -88,14 +93,14 @@ def get_cmap(color: str):
img = ax.matshow(arr, cmap=cmap,
transform=tr + ax.transData,
extent=(gr.start, gr.end, gr.start, gr.end),
aspect='auto')
aspect=aspect)
else:
if gr2 is None:
gr2 = gr
# matrix style
img = ax.matshow(arr, cmap=cmap,
extent=(gr.start, gr.end, gr2.end, gr2.start),
aspect='auto')
aspect=aspect)

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

if 'color_bar' in self.properties and self.properties['color_bar'] != 'no':
height += 1.5
if self.properties["aspect_ratio"] != 'equal':
height += 1.5

return height

Expand Down

0 comments on commit 2628295

Please sign in to comment.