-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathlabel_distribution.py
More file actions
251 lines (233 loc) · 8.87 KB
/
label_distribution.py
File metadata and controls
251 lines (233 loc) · 8.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# Copyright 2024 Flower Labs GmbH. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Label distribution plotting."""
from typing import Any, Optional, Union
import matplotlib.colors as mcolors
import pandas as pd
from matplotlib.axes import Axes
from matplotlib.figure import Figure
from flwr_datasets.common import EventType, event
from flwr_datasets.metrics.utils import compute_counts, compute_frequencies
from flwr_datasets.partitioner import Partitioner
from flwr_datasets.visualization.bar_plot import _plot_bar
from flwr_datasets.visualization.heatmap_plot import _plot_heatmap
from flwr_datasets.visualization.utils import _validate_parameters
# pylint: disable=too-many-arguments,too-many-locals
def plot_label_distributions( # pylint: disable=R0917
partitioner: Partitioner,
label_name: str,
plot_type: str = "bar",
size_unit: str = "absolute",
max_num_partitions: Optional[int] = None,
partition_id_axis: str = "x",
axis: Optional[Axes] = None,
figsize: Optional[tuple[float, float]] = None,
title: str = "Per Partition Label Distribution",
cmap: Optional[Union[str, mcolors.Colormap]] = None,
legend: bool = False,
legend_title: Optional[str] = None,
verbose_labels: bool = True,
plot_kwargs: Optional[dict[str, Any]] = None,
legend_kwargs: Optional[dict[str, Any]] = None,
) -> tuple[Figure, Axes, pd.DataFrame]:
"""Plot the label distribution of the partitions.
Parameters
----------
partitioner : Partitioner
Partitioner with an assigned dataset.
label_name : str
Column name identifying label based on which the plot will be created.
plot_type : str
Type of plot, either "bar" or "heatmap".
size_unit : str
"absolute" or "percent". "absolute" - (number of samples). "percent" -
normalizes each value, so they sum up to 100%.
max_num_partitions : Optional[int]
The number of partitions that will be used. If left None, then all partitions
will be used.
partition_id_axis : str
"x" or "y". The axis on which the partition_id will be marked.
axis : Optional[Axes]
Matplotlib Axes object to plot on.
figsize : Optional[Tuple[float, float]]
Size of the figure.
title : str
Title of the plot.
cmap : Optional[Union[str, mcolors.Colormap]]
Colormap for determining the colorspace of the plot.
legend : bool
Include the legend.
legend_title : Optional[str]
Title for the legend. If None, the defaults will be takes based on the type of
plot.
verbose_labels : bool
Whether to use verbose versions of the labels. These values are used as columns
of the returned dataframe and as labels on the legend in a bar plot and columns/
rows ticks in a heatmap plot.
plot_kwargs: Optional[Dict[str, Any]]
Any key value pair that can be passed to a plot function that are not supported
directly. In case of the parameter doubling (e.g. specifying cmap here too) the
chosen value will be taken from the explicit arguments (e.g. cmap specified as
an argument to this function not the value in this dictionary).
legend_kwargs: Optional[Dict[str, Any]]
Any key value pair that can be passed to a figure.legend in case of bar plot or
cbar_kws in case of heatmap that are not supported directly. In case of the
parameter doubling (e.g. specifying legend_title here too) the
chosen value will be taken from the explicit arguments (e.g. legend_title
specified as an argument to this function not the value in this dictionary).
Returns
-------
fig : Figure
The figure object.
axis : Axes
The Axes object with the plot.
dataframe : pd.DataFrame
The DataFrame where each row represents the partition id and each column
represents the class.
Examples
--------
Visualize the label distribution resulting from DirichletPartitioner.
>>> from flwr_datasets import FederatedDataset
>>> from flwr_datasets.partitioner import DirichletPartitioner
>>> from flwr_datasets.visualization import plot_label_distributions
>>>
>>> fds = FederatedDataset(
>>> dataset="cifar10",
>>> partitioners={
>>> "train": DirichletPartitioner(
>>> num_partitions=20,
>>> partition_by="label",
>>> alpha=0.3,
>>> min_partition_size=0,
>>> ),
>>> },
>>> )
>>> partitioner = fds.partitioners["train"]
>>> figure, axis, dataframe = plot_label_distributions(
>>> partitioner=partitioner,
>>> label_name="label",
>>> legend=True,
>>> verbose_labels=True,
>>> )
Alternatively you can visualize each partition in terms of fraction of the data
available on that partition instead of the absolute count
>>> from flwr_datasets import FederatedDataset
>>> from flwr_datasets.partitioner import DirichletPartitioner
>>> from flwr_datasets.visualization import plot_label_distributions
>>>
>>> fds = FederatedDataset(
>>> dataset="cifar10",
>>> partitioners={
>>> "train": DirichletPartitioner(
>>> num_partitions=20,
>>> partition_by="label",
>>> alpha=0.3,
>>> min_partition_size=0,
>>> ),
>>> },
>>> )
>>> partitioner = fds.partitioners["train"]
>>> figure, axis, dataframe = plot_label_distributions(
>>> partitioner=partitioner,
>>> label_name="label",
>>> size_unit="percent",
>>> legend=True,
>>> verbose_labels=True,
>>> )
>>>
You can also visualize the data as a heatmap by changing the `plot_type` from
default "bar" to "heatmap"
>>> from flwr_datasets import FederatedDataset
>>> from flwr_datasets.partitioner import DirichletPartitioner
>>> from flwr_datasets.visualization import plot_label_distributions
>>>
>>> fds = FederatedDataset(
>>> dataset="cifar10",
>>> partitioners={
>>> "train": DirichletPartitioner(
>>> num_partitions=20,
>>> partition_by="label",
>>> alpha=0.3,
>>> min_partition_size=0,
>>> ),
>>> },
>>> )
>>> partitioner = fds.partitioners["train"]
>>> figure, axis, dataframe = plot_label_distributions(
>>> partitioner=partitioner,
>>> label_name="label",
>>> size_unit="percent",
>>> plot_type="heatmap",
>>> legend=True,
>>> plot_kwargs={"annot": True},
>>> )
You can also visualize the returned DataFrame in Jupyter Notebook
>>> dataframe.style.background_gradient(axis=None)
"""
event(
EventType.PLOT_LABEL_DISTRIBUTION_CALLED,
{
"plot_type": plot_type,
},
)
_validate_parameters(plot_type, size_unit, partition_id_axis)
dataframe = pd.DataFrame()
if size_unit == "absolute":
dataframe = compute_counts(
partitioner=partitioner,
column_name=label_name,
verbose_names=verbose_labels,
max_num_partitions=max_num_partitions,
)
elif size_unit == "percent":
dataframe = compute_frequencies(
partitioner=partitioner,
column_name=label_name,
verbose_names=verbose_labels,
max_num_partitions=max_num_partitions,
)
dataframe = dataframe * 100.0
if plot_type == "bar":
axis = _plot_bar(
dataframe,
axis,
figsize,
title,
cmap,
partition_id_axis,
size_unit,
legend,
legend_title,
plot_kwargs,
legend_kwargs,
)
elif plot_type == "heatmap":
axis = _plot_heatmap(
dataframe,
axis,
figsize,
title,
cmap,
partition_id_axis,
size_unit,
legend,
legend_title,
plot_kwargs,
legend_kwargs,
)
assert axis is not None, "axis is None after plotting"
figure = axis.figure
assert isinstance(figure, Figure), "figure extraction from axes is not a Figure"
return figure, axis, dataframe