flgo.experiment.analyzer
This module is to analyze the training results saved by Logger. To use this module, a analysis plan should be designed (i.e. dict): Selector: select the records according to the task, algorithm and options of the task Painter: draw graphic of the selected records Table: output some statistic of the selected records on the console
The basic usage is to build a plan dict and pass it to flgo.experiment.analyzer
plan = {'Selector':..., 'Painter':..., 'Table':...,}
flgo.experiment.analyzer.show(plan)
The following three examples show how to build a customized plan:
How to define a Selector?
{'Selector': {
'task': task_path, # all the analysis will be conducted on a single task
'header': ['fedavg'], # only the records where the names of algorithms are in header
will be selected
'filter': {'LR':'<0.1'} # only the records whose options satisfy the conditions in filter
will be selected
'legend_with': ['LR', 'B', 'E'] # all the graphic will show the legends of records according to legend_with
}, ...}
How to define a Painter?
Each Painter
is a dict of different types of graphic (e.g. Curve, Bar and Scatter). In each types of graphic,
the value is a list of figures, where each figure is defined by a dict like {'args':{...}, 'obj_option':{}, 'fig_option':{...}}
{...,
'Painter':{
'Curve':[
{'args':{'x':'communication_round', 'y':'val_loss'}, },
{...}
]
},
...,
}
How to define a Table?
{..., 'Table':{ 'min_value':[ {'x':'val_loss'}, ... ] } }
A standard analysis plan usually consists of the above three parts, and Painter
and Table
are both optional
Bar
Bases: PaintObject
Bar Object
Source code in flgo\experiment\analyzer.py
306 307 308 309 |
|
Curve
Bases: PaintObject
Curve Object
Source code in flgo\experiment\analyzer.py
301 302 303 304 |
|
GroupCurve
Bases: PaintObject
Group Curve Object
Source code in flgo\experiment\analyzer.py
324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
|
PaintObject
The basic PaintObject. Each PaintObject should inherent from this class. And the method self.draw should be overwritten if necessary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rec |
Record
|
the record |
required |
args |
dict
|
the painting arguments |
required |
obj_option |
dict
|
the personal option for each object |
required |
draw_func |
str
|
optional, the function name. All the subclass of this class won't claim this parameter. |
required |
Example:
>>> class GroupCurve(PaintObject):
... def __init__(self, rec, args, obj_option):
... super(GroupCurve, self).__init__(rec, args, obj_option, '')
...
... def draw(self, ax):
... x = self.rec.data[self.args['x']]
... ykey = self.args['y']
... mean_y = self.rec.data[ykey]
... min_y = np.min(np.array([d[ykey] for d in self.rec.datas]), axis=0)
... max_y = np.max(np.array([d[ykey] for d in self.rec.datas]), axis=0)
... ax.plot(x, mean_y, label=self.rec.data['label'])
... ax.fill_between(x, max_y, min_y, alpha=0.3)
... ax.legend()
Source code in flgo\experiment\analyzer.py
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
|
Painter
Draw the information in records into figures
Parameters:
Name | Type | Description | Default |
---|---|---|---|
records |
list
|
a list of instances of Record(...) |
required |
save_text |
bool
|
whether to store the figures into the disk |
required |
path |
str
|
the storing path |
'.'
|
format |
str
|
the storing format |
'png'
|
Source code in flgo\experiment\analyzer.py
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 |
|
create_figure(object_class, fig_config)
Create figure according to the PaintObject and figure configurations. For each record k, a PaintObject(record, object_option) will be created for later drawing. Then, a figure will be created by fig_option and all the PaintObject will be put onto the figure.
The fig_config should be a dict like
{ 'args':{...}, # ploting arguments for each record 'obj_option':{...}, # assign each PaintObject with different attributes like color, label... 'fig_option':{...}, # the options of the figure such as title, xlabel, xlim, no_legend }
Parameters:
Name | Type | Description | Default |
---|---|---|---|
object_class |
class|str
|
the types of the obejct to be drawed |
required |
fig_config |
dict
|
the drawing configuration |
required |
Example:
>>> p=Painter(records)
>>> p.create_figure(Curve, {'args':{'x':'communication_round', 'y':'val_loss'}})
Source code in flgo\experiment\analyzer.py
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 |
|
Record
Read the record that is stored by each runner into the memory according to the task and the name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
task |
str
|
the path of the task |
required |
name |
str
|
the name of the saved record |
required |
Source code in flgo\experiment\analyzer.py
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 |
|
create_group(rec_list)
classmethod
Organize the records in rec_list into a group-level Record, where there will be a new attribute named Record.datas. And the values in Record.data will be replaced by the mean values of that in Record.datas
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rec_list |
list
|
a list of Record(...) |
required |
Returns:
Type | Description |
---|---|
a new group-level Record |
Source code in flgo\experiment\analyzer.py
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 |
|
Scatter
Bases: PaintObject
Scatter Obejct
Source code in flgo\experiment\analyzer.py
311 312 313 314 |
|
Selector
Filter the records and read them into memory accoring to customized settings
Parameters:
Name | Type | Description | Default |
---|---|---|---|
selector_config |
dict
|
the dictionary that is used to filter records |
required |
Example:
>>> task='./my_task'
>>> selector = Selector({'task':task, 'header':['fedavg'], 'filter':{'lr':0.1}})
>>> selector.records[task]
>>> # selector.records is a dict where selector.records[task] is a list
>>> # of the records that pass the filter
Source code in flgo\experiment\analyzer.py
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 252 253 254 |
|
Table
Organize the information in records into a table.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
records |
list
|
a list of instances of Record(...) |
required |
save_text |
bool
|
whether to store the table into the disk |
False
|
path |
str
|
the storing path |
'.'
|
Source code in flgo\experiment\analyzer.py
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 |
|
add_column(func, col_option)
Add a column to this table. For each record \(Record_k\), its value \(v_k\) in this column is v_k=func(Record_k, col_option), where func can be arbitrarily customized.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
func | str
|
the name of the function or the function |
required |
col_option |
dict | str
|
the option of the column to index data in each record |
required |
Example:
>>> tb = Table(records)
>>> tb.add_column(min_value, col_option={'x':'val_loss'})
>>> tb.print()
Source code in flgo\experiment\analyzer.py
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 |
|
print()
Print and store the table
Source code in flgo\experiment\analyzer.py
648 649 650 651 652 653 |
|
Trace2D
Bases: PaintObject
Trace Object
Source code in flgo\experiment\analyzer.py
316 317 318 319 320 321 322 |
|
final_value(record, col_option)
Get final value. The col_option should be like {'x': key of record.data}
Parameters:
Name | Type | Description | Default |
---|---|---|---|
record |
Record
|
the record |
required |
col_option |
dict
|
column option |
required |
Returns:
Type | Description |
---|---|
the column value |
Source code in flgo\experiment\analyzer.py
508 509 510 511 512 513 514 515 516 517 518 519 520 |
|
group_optimal_value(record, col_option)
Get the grouped optimal value. The col_option should be like { 'x': key of record.data, 'flag': 'min' or 'max' }
Parameters:
Name | Type | Description | Default |
---|---|---|---|
record |
Record
|
the record |
required |
col_option |
dict
|
column option |
required |
Returns:
Type | Description |
---|---|
the column value |
Source code in flgo\experiment\analyzer.py
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 |
|
group_optimal_x_by_y(record, col_option)
Get the grouped value of y where the grouped value of x is the optimal. The col_option should be like { 'x': key of record.data, 'y': key of record.data, 'flag': 'min' or 'max' }
Parameters:
Name | Type | Description | Default |
---|---|---|---|
record |
Record
|
the record |
required |
col_option |
dict
|
column option |
required |
Returns:
Type | Description |
---|---|
the column value |
Source code in flgo\experiment\analyzer.py
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 |
|
max_value(record, col_option)
Get maximal value.The col_option should be like {'x': key of record.data}
Parameters:
Name | Type | Description | Default |
---|---|---|---|
record |
Record
|
the record |
required |
col_option |
dict
|
column option |
required |
Returns:
Type | Description |
---|---|
the column value |
Source code in flgo\experiment\analyzer.py
452 453 454 455 456 457 458 459 460 461 462 463 464 |
|
mean_value(record, col_option)
Get mean value. The col_option should be like {'x': key of record.data}
Parameters:
Name | Type | Description | Default |
---|---|---|---|
record |
Record
|
the record |
required |
col_option |
dict
|
column option |
required |
Returns:
Type | Description |
---|---|
the column value |
Source code in flgo\experiment\analyzer.py
494 495 496 497 498 499 500 501 502 503 504 505 506 |
|
min_value(record, col_option)
Get minimal value. The col_option should be like {'x': key of record.data}
Parameters:
Name | Type | Description | Default |
---|---|---|---|
record |
Record
|
the record |
required |
col_option |
dict
|
column option |
required |
Returns:
Type | Description |
---|---|
the column value |
Source code in flgo\experiment\analyzer.py
438 439 440 441 442 443 444 445 446 447 448 449 450 |
|
optimal_x_by_y(record, col_option)
Get the value of y where the value of x is the optimal. The col_option should be like { 'x': key of record.data, 'y': key of record.data, 'flag': 'min' or 'max' }
Parameters:
Name | Type | Description | Default |
---|---|---|---|
record |
Record
|
the record |
required |
col_option |
dict
|
column option |
required |
Returns:
Type | Description |
---|---|
the column value |
Source code in flgo\experiment\analyzer.py
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 |
|
show(config, save_figure=False, save_text=False, path='.', seed=0)
Show the results according to analysis configuration.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config |
dict | str
|
the analysis plan |
required |
save_figure |
bool
|
whether to save figures |
False
|
save_text |
bool
|
whether to save table as .txt file |
False
|
path |
str
|
the path to store the results |
'.'
|
seed |
int
|
random seed |
0
|
Example:
>>> import flgo.experiment.analyzer as al
>>> # only records of fedavg running on the task 'my_task' with learning rate lr<=0.01 will be selected
>>> selector_config = {'task':'./my_task', 'header':['fedavg'], 'filter':['LR':'<=0.1']}
>>> # draw the learning curve on the validation dataset
>>> painter_config = {'Curve':[{'args':{'x':'communication_round', 'y':'val_loss'}}]}
>>> # show the minimal value of validation loss
>>> table_config = {'min_value':[{'x':'val_loss'}]}
>>> # create analysis plan
>>> analysis_plan = {'Selector':selector_config, 'Painter':painter_config, 'Table':table_config}
>>> # call this function
>>> al.show(analysis_plan)
Source code in flgo\experiment\analyzer.py
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 |
|
std_value(record, col_option)
Get standard deviation. The col_option should be like {'x': key of record.data}
Parameters:
Name | Type | Description | Default |
---|---|---|---|
record |
Record
|
the record |
required |
col_option |
dict
|
column option |
required |
Returns:
Type | Description |
---|---|
the column value |
Source code in flgo\experiment\analyzer.py
480 481 482 483 484 485 486 487 488 489 490 491 492 |
|
variance(record, col_option)
Get variance. The col_option should be like {'x': key of record.data}
Parameters:
Name | Type | Description | Default |
---|---|---|---|
record |
Record
|
the record |
required |
col_option |
dict
|
column option |
required |
Returns:
Type | Description |
---|---|
the column value |
Source code in flgo\experiment\analyzer.py
466 467 468 469 470 471 472 473 474 475 476 477 478 |
|