Skip to content

Commit 424c93f

Browse files
committed
Validate GridSpecParams on construction
Move the validate() check into __post_init__ and rename it to _validate so that every GridSpecParams instance is guaranteed valid at construction. Switch _reduce_height/_reduce_width to build a new instance via dataclasses.replace instead of copy+mutate so the invariant survives aspect-ratio adjustment.
1 parent 9d9ddce commit 424c93f

3 files changed

Lines changed: 20 additions & 22 deletions

File tree

plotnine/_mpl/layout_manager/_composition_side_space.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,6 @@ def resize_gridspec(self):
364364
sized to accomodate the content of the annotations.
365365
"""
366366
gsparams = self.calculate_gridspec_params()
367-
gsparams.validate()
368367
self.sub_gridspec.update_params_and_artists(gsparams)
369368

370369
def calculate_gridspec_params(self) -> GridSpecParams:

plotnine/_mpl/layout_manager/_plot_side_space.py

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from __future__ import annotations
1313

14-
from copy import copy
14+
from dataclasses import replace
1515
from functools import cached_property
1616
from typing import TYPE_CHECKING
1717

@@ -807,7 +807,6 @@ def _arrange_insets(self):
807807
wspace=0,
808808
hspace=0,
809809
)
810-
params.validate()
811810
inset.obj._gridspec.update_params_and_artists(params)
812811

813812
if isinstance(inset.obj, ggplot):
@@ -824,7 +823,6 @@ def resize_gridspec(self):
824823
sized to accomodate the artists around the panels.
825824
"""
826825
gsparams = self.calculate_gridspec_params()
827-
gsparams.validate()
828826
self.sub_gridspec.update_params_and_artists(gsparams)
829827

830828
def calculate_gridspec_params(self) -> GridSpecParams:
@@ -1074,43 +1072,41 @@ def _reduce_height(self, gsparams: GridSpecParams, ratio: float):
10741072
"""
10751073
Reduce the height of axes to get the aspect ratio
10761074
"""
1077-
gsparams = copy(gsparams)
1078-
10791075
# New height w.r.t figure height
10801076
h1 = ratio * self.w * (self.W / self.H)
10811077

10821078
# Half of the total vertical reduction w.r.t figure height
10831079
dh = (self.h - h1) * self.plot.facet.nrow / 2
10841080

1085-
# Reduce plot area height
1086-
gsparams.top -= dh
1087-
gsparams.bottom += dh
1088-
gsparams.hspace = self.sh / h1
1089-
10901081
# Add more vertical plot margin
10911082
self.increase_vertical_plot_margin(dh)
1092-
return gsparams
1083+
1084+
return replace(
1085+
gsparams,
1086+
top=gsparams.top - dh,
1087+
bottom=gsparams.bottom + dh,
1088+
hspace=self.sh / h1,
1089+
)
10931090

10941091
def _reduce_width(self, gsparams: GridSpecParams, ratio: float):
10951092
"""
10961093
Reduce the width of axes to get the aspect ratio
10971094
"""
1098-
gsparams = copy(gsparams)
1099-
11001095
# New width w.r.t figure width
11011096
w1 = (self.h * self.H) / (ratio * self.W)
11021097

11031098
# Half of the total horizontal reduction w.r.t figure width
11041099
dw = (self.w - w1) * self.plot.facet.ncol / 2
11051100

1106-
# Reduce width
1107-
gsparams.left += dw
1108-
gsparams.right -= dw
1109-
gsparams.wspace = self.sw / w1
1110-
11111101
# Add more horizontal margin
11121102
self.increase_horizontal_plot_margin(dw)
1113-
return gsparams
1103+
1104+
return replace(
1105+
gsparams,
1106+
left=gsparams.left + dw,
1107+
right=gsparams.right - dw,
1108+
wspace=self.sw / w1,
1109+
)
11141110

11151111
@property
11161112
def aspect_ratio(self) -> float:

plotnine/_mpl/layout_manager/_side_space.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,12 @@ class GridSpecParams:
4949
wspace: float
5050
hspace: float
5151

52-
def validate(self):
52+
def __post_init__(self):
53+
self._validate()
54+
55+
def _validate(self):
5356
"""
54-
Return True if the params will create a non-empty area
57+
Raise if the params do not enclose a positive-area rectangle
5558
"""
5659
if not (self.top - self.bottom > 0 and self.right - self.left > 0):
5760
raise GridSpecParamsError(

0 commit comments

Comments
 (0)