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 252 253 254 255 256 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 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 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 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 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 595 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 657 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 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520
|
.. _contributing:
============
Contributing
============
.. currentmodule:: sklearn
This project is a community effort, and everyone is welcome to
contribute.
The project is hosted on https://github.com/scikit-learn/scikit-learn
The decision making process and governance structure of scikit-learn is laid
out in the governance document: :ref:`governance`.
Scikit-learn is somewhat :ref:`selective <selectiveness>` when it comes to
adding new algorithms, and the best way to contribute and to help the project
is to start working on known issues.
See :ref:`new_contributors` to get started.
.. topic:: **Our community, our values**
We are a community based on openness and friendly, didactic,
discussions.
We aspire to treat everybody equally, and value their contributions. We
are particularly seeking people from underrepresented backgrounds in Open
Source Software and scikit-learn in particular to participate and
contribute their expertise and experience.
Decisions are made based on technical merit and consensus.
Code is not the only way to help the project. Reviewing pull
requests, answering questions to help others on mailing lists or
issues, organizing and teaching tutorials, working on the website,
improving the documentation, are all priceless contributions.
We abide by the principles of openness, respect, and consideration of
others of the Python Software Foundation:
https://www.python.org/psf/codeofconduct/
In case you experience issues using this package, do not hesitate to submit a
ticket to the
`GitHub issue tracker
<https://github.com/scikit-learn/scikit-learn/issues>`_. You are also
welcome to post feature requests or pull requests.
Ways to contribute
==================
There are many ways to contribute to scikit-learn, with the most common ones
being contribution of code or documentation to the project. Improving the
documentation is no less important than improving the library itself. If you
find a typo in the documentation, or have made improvements, do not hesitate to
send an email to the mailing list or preferably submit a GitHub pull request.
Full documentation can be found under the doc/ directory.
But there are many other ways to help. In particular helping to
:ref:`improve, triage, and investigate issues <bug_triaging>` and
:ref:`reviewing other developers' pull requests <code_review>` are very
valuable contributions that decrease the burden on the project
maintainers.
Another way to contribute is to report issues you're facing, and give a "thumbs
up" on issues that others reported and that are relevant to you. It also helps
us if you spread the word: reference the project from your blog and articles,
link to it from your website, or simply star to say "I use it":
.. raw:: html
<a class="github-button" href="https://github.com/scikit-learn/scikit-learn"
data-icon="octicon-star" data-size="large" data-show-count="true" aria-label="Star
scikit-learn/scikit-learn on GitHub">Star</a>
In case a contribution/issue involves changes to the API principles
or changes to dependencies or supported versions, it must be backed by a
:ref:`slep`, where a SLEP must be submitted as a pull-request to
`enhancement proposals <https://scikit-learn-enhancement-proposals.readthedocs.io>`_
using the `SLEP template <https://scikit-learn-enhancement-proposals.readthedocs.io/en/latest/slep_template.html>`_
and follows the decision-making process outlined in :ref:`governance`.
.. topic:: Contributing to related projects
Scikit-learn thrives in an ecosystem of several related projects, which also
may have relevant issues to work on, including smaller projects such as:
* `scikit-learn-contrib <https://github.com/search?q=org%3Ascikit-learn-contrib+is%3Aissue+is%3Aopen+sort%3Aupdated-desc&type=Issues>`__
* `joblib <https://github.com/joblib/joblib/issues>`__
* `sphinx-gallery <https://github.com/sphinx-gallery/sphinx-gallery/issues>`__
* `numpydoc <https://github.com/numpy/numpydoc/issues>`__
* `liac-arff <https://github.com/renatopp/liac-arff/issues>`__
and larger projects:
* `numpy <https://github.com/numpy/numpy/issues>`__
* `scipy <https://github.com/scipy/scipy/issues>`__
* `matplotlib <https://github.com/matplotlib/matplotlib/issues>`__
* and so on.
Look for issues marked "help wanted" or similar.
Helping these projects may help Scikit-learn too.
See also :ref:`related_projects`.
Submitting a bug report or a feature request
============================================
We use GitHub issues to track all bugs and feature requests; feel free to open
an issue if you have found a bug or wish to see a feature implemented.
In case you experience issues using this package, do not hesitate to submit a
ticket to the
`Bug Tracker <https://github.com/scikit-learn/scikit-learn/issues>`_. You are
also welcome to post feature requests or pull requests.
It is recommended to check that your issue complies with the
following rules before submitting:
- Verify that your issue is not being currently addressed by other
`issues <https://github.com/scikit-learn/scikit-learn/issues?q=>`_
or `pull requests <https://github.com/scikit-learn/scikit-learn/pulls?q=>`_.
- If you are submitting an algorithm or feature request, please verify that
the algorithm fulfills our
`new algorithm requirements
<https://scikit-learn.org/stable/faq.html#what-are-the-inclusion-criteria-for-new-algorithms>`_.
- If you are submitting a bug report, we strongly encourage you to follow the guidelines in
:ref:`filing_bugs`.
.. _filing_bugs:
How to make a good bug report
-----------------------------
When you submit an issue to `Github
<https://github.com/scikit-learn/scikit-learn/issues>`__, please do your best to
follow these guidelines! This will make it a lot easier to provide you with good
feedback:
- The ideal bug report contains a :ref:`short reproducible code snippet
<minimal_reproducer>`, this way
anyone can try to reproduce the bug easily (see `this
<https://stackoverflow.com/help/mcve>`_ for more details). If your snippet is
longer than around 50 lines, please link to a `gist
<https://gist.github.com>`_ or a github repo.
- If not feasible to include a reproducible snippet, please be specific about
what **estimators and/or functions are involved and the shape of the data**.
- If an exception is raised, please **provide the full traceback**.
- Please include your **operating system type and version number**, as well as
your **Python, scikit-learn, numpy, and scipy versions**. This information
can be found by running the following code snippet::
>>> import sklearn
>>> sklearn.show_versions() # doctest: +SKIP
- Please ensure all **code snippets and error messages are formatted in
appropriate code blocks**. See `Creating and highlighting code blocks
<https://help.github.com/articles/creating-and-highlighting-code-blocks>`_
for more details.
If you want to help curate issues, read :ref:`the following
<bug_triaging>`.
Contributing code
=================
.. note::
To avoid duplicating work, it is highly advised that you search through the
`issue tracker <https://github.com/scikit-learn/scikit-learn/issues>`_ and
the `PR list <https://github.com/scikit-learn/scikit-learn/pulls>`_.
If in doubt about duplicated work, or if you want to work on a non-trivial
feature, it's recommended to first open an issue in
the `issue tracker <https://github.com/scikit-learn/scikit-learn/issues>`_
to get some feedbacks from core developers.
One easy way to find an issue to work on is by applying the "help wanted"
label in your search. This lists all the issues that have been unclaimed
so far. In order to claim an issue for yourself, please comment exactly
``/take`` on it for the CI to automatically assign the issue to you.
Video resources
---------------
These videos are step-by-step introductions on how to contribute to
scikit-learn, and are a great companion to the following text guidelines.
Please make sure to still check our guidelines below, since they describe our
latest up-to-date workflow.
- Crash Course in Contributing to Scikit-Learn & Open Source Projects:
`Video <https://youtu.be/5OL8XoMMOfA>`__,
`Transcript
<https://github.com/data-umbrella/event-transcripts/blob/main/2020/05-andreas-mueller-contributing.md>`__
- Example of Submitting a Pull Request to scikit-learn:
`Video <https://youtu.be/PU1WyDPGePI>`__,
`Transcript
<https://github.com/data-umbrella/event-transcripts/blob/main/2020/06-reshama-shaikh-sklearn-pr.md>`__
- Sprint-specific instructions and practical tips:
`Video <https://youtu.be/p_2Uw2BxdhA>`__,
`Transcript
<https://github.com/data-umbrella/data-umbrella-scikit-learn-sprint/blob/master/3_transcript_ACM_video_vol2.md>`__
- 3 Components of Reviewing a Pull Request:
`Video <https://youtu.be/dyxS9KKCNzA>`__,
`Transcript
<https://github.com/data-umbrella/event-transcripts/blob/main/2021/27-thomas-pr.md>`__
.. note::
In January 2021, the default branch name changed from ``master`` to ``main``
for the scikit-learn GitHub repository to use more inclusive terms.
These videos were created prior to the renaming of the branch.
For contributors who are viewing these videos to set up their
working environment and submitting a PR, ``master`` should be replaced to ``main``.
How to contribute
-----------------
The preferred way to contribute to scikit-learn is to fork the `main
repository <https://github.com/scikit-learn/scikit-learn/>`__ on GitHub,
then submit a "pull request" (PR).
In the first few steps, we explain how to locally install scikit-learn, and
how to set up your git repository:
1. `Create an account <https://github.com/join>`_ on
GitHub if you do not already have one.
2. Fork the `project repository
<https://github.com/scikit-learn/scikit-learn>`__: click on the 'Fork'
button near the top of the page. This creates a copy of the code under your
account on the GitHub user account. For more details on how to fork a
repository see `this guide <https://help.github.com/articles/fork-a-repo/>`_.
3. Clone your fork of the scikit-learn repo from your GitHub account to your
local disk:
.. prompt:: bash $
git clone git@github.com:YourLogin/scikit-learn.git # add --depth 1 if your connection is slow
cd scikit-learn
4. Follow steps 2-6 in :ref:`install_bleeding_edge` to build scikit-learn in
development mode and return to this document.
5. Install the development dependencies:
.. prompt:: bash $
pip install pytest pytest-cov ruff mypy numpydoc black==23.3.0
.. _upstream:
6. Add the ``upstream`` remote. This saves a reference to the main
scikit-learn repository, which you can use to keep your repository
synchronized with the latest changes:
.. prompt:: bash $
git remote add upstream git@github.com:scikit-learn/scikit-learn.git
7. Check that the `upstream` and `origin` remote aliases are configured correctly
by running `git remote -v` which should display::
origin git@github.com:YourLogin/scikit-learn.git (fetch)
origin git@github.com:YourLogin/scikit-learn.git (push)
upstream git@github.com:scikit-learn/scikit-learn.git (fetch)
upstream git@github.com:scikit-learn/scikit-learn.git (push)
You should now have a working installation of scikit-learn, and your git repository
properly configured. It could be useful to run some test to verify your installation.
Please refer to :ref:`pytest_tips` for examples.
The next steps now describe the process of modifying code and submitting a PR:
8. Synchronize your ``main`` branch with the ``upstream/main`` branch,
more details on `GitHub Docs <https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/syncing-a-fork>`_:
.. prompt:: bash $
git checkout main
git fetch upstream
git merge upstream/main
9. Create a feature branch to hold your development changes:
.. prompt:: bash $
git checkout -b my_feature
and start making changes. Always use a feature branch. It's good
practice to never work on the ``main`` branch!
10. (**Optional**) Install `pre-commit <https://pre-commit.com/#install>`_ to
run code style checks before each commit:
.. prompt:: bash $
pip install pre-commit
pre-commit install
pre-commit checks can be disabled for a particular commit with
`git commit -n`.
11. Develop the feature on your feature branch on your computer, using Git to
do the version control. When you're done editing, add changed files using
``git add`` and then ``git commit``:
.. prompt:: bash $
git add modified_files
git commit
to record your changes in Git, then push the changes to your GitHub
account with:
.. prompt:: bash $
git push -u origin my_feature
12. Follow `these
<https://help.github.com/articles/creating-a-pull-request-from-a-fork>`_
instructions to create a pull request from your fork. This will send an
email to the committers. You may want to consider sending an email to the
mailing list for more visibility.
.. note::
If you are modifying a Cython module, you have to re-compile after
modifications and before testing them:
.. prompt:: bash $
pip install -v --no-use-pep517 --no-build-isolation -e .
Use the ``--no-build-isolation`` flag to avoid compiling the whole project
each time, only the files you have modified.
It is often helpful to keep your local feature branch synchronized with the
latest changes of the main scikit-learn repository:
.. prompt:: bash $
git fetch upstream
git merge upstream/main
Subsequently, you might need to solve the conflicts. You can refer to the
`Git documentation related to resolving merge conflict using the command
line
<https://help.github.com/articles/resolving-a-merge-conflict-using-the-command-line/>`_.
.. topic:: Learning git:
The `Git documentation <https://git-scm.com/documentation>`_ and
http://try.github.io are excellent resources to get started with git,
and understanding all of the commands shown here.
.. _pr_checklist:
Pull request checklist
----------------------
Before a PR can be merged, it needs to be approved by two core developers.
Please prefix the title of your pull request with ``[MRG]`` if the
contribution is complete and should be subjected to a detailed review. An
incomplete contribution -- where you expect to do more work before receiving
a full review -- should be prefixed ``[WIP]`` (to indicate a work in
progress) and changed to ``[MRG]`` when it matures. WIPs may be useful to:
indicate you are working on something to avoid duplicated work, request
broad review of functionality or API, or seek collaborators. WIPs often
benefit from the inclusion of a `task list
<https://github.com/blog/1375-task-lists-in-gfm-issues-pulls-comments>`_ in
the PR description.
In order to ease the reviewing process, we recommend that your contribution
complies with the following rules before marking a PR as ``[MRG]``. The
**bolded** ones are especially important:
1. **Give your pull request a helpful title** that summarizes what your
contribution does. This title will often become the commit message once
merged so it should summarize your contribution for posterity. In some
cases "Fix <ISSUE TITLE>" is enough. "Fix #<ISSUE NUMBER>" is never a
good title.
2. **Make sure your code passes the tests**. The whole test suite can be run
with `pytest`, but it is usually not recommended since it takes a long
time. It is often enough to only run the test related to your changes:
for example, if you changed something in
`sklearn/linear_model/_logistic.py`, running the following commands will
usually be enough:
- `pytest sklearn/linear_model/_logistic.py` to make sure the doctest
examples are correct
- `pytest sklearn/linear_model/tests/test_logistic.py` to run the tests
specific to the file
- `pytest sklearn/linear_model` to test the whole
:mod:`~sklearn.linear_model` module
- `pytest doc/modules/linear_model.rst` to make sure the user guide
examples are correct.
- `pytest sklearn/tests/test_common.py -k LogisticRegression` to run all our
estimator checks (specifically for `LogisticRegression`, if that's the
estimator you changed).
There may be other failing tests, but they will be caught by the CI so
you don't need to run the whole test suite locally. For guidelines on how
to use ``pytest`` efficiently, see the :ref:`pytest_tips`.
3. **Make sure your code is properly commented and documented**, and **make
sure the documentation renders properly**. To build the documentation, please
refer to our :ref:`contribute_documentation` guidelines. The CI will also
build the docs: please refer to :ref:`generated_doc_CI`.
4. **Tests are necessary for enhancements to be
accepted**. Bug-fixes or new features should be provided with
`non-regression tests
<https://en.wikipedia.org/wiki/Non-regression_testing>`_. These tests
verify the correct behavior of the fix or feature. In this manner, further
modifications on the code base are granted to be consistent with the
desired behavior. In the case of bug fixes, at the time of the PR, the
non-regression tests should fail for the code base in the ``main`` branch
and pass for the PR code.
5. Follow the :ref:`coding-guidelines`.
6. When applicable, use the validation tools and scripts in the
``sklearn.utils`` submodule. A list of utility routines available
for developers can be found in the :ref:`developers-utils` page.
7. Often pull requests resolve one or more other issues (or pull requests).
If merging your pull request means that some other issues/PRs should
be closed, you should `use keywords to create link to them
<https://github.com/blog/1506-closing-issues-via-pull-requests/>`_
(e.g., ``Fixes #1234``; multiple issues/PRs are allowed as long as each
one is preceded by a keyword). Upon merging, those issues/PRs will
automatically be closed by GitHub. If your pull request is simply
related to some other issues/PRs, create a link to them without using
the keywords (e.g., ``See also #1234``).
8. PRs should often substantiate the change, through benchmarks of
performance and efficiency (see :ref:`monitoring_performances`) or through
examples of usage. Examples also illustrate the features and intricacies of
the library to users. Have a look at other examples in the `examples/
<https://github.com/scikit-learn/scikit-learn/tree/main/examples>`_
directory for reference. Examples should demonstrate why the new
functionality is useful in practice and, if possible, compare it to other
methods available in scikit-learn.
9. New features have some maintenance overhead. We expect PR authors
to take part in the maintenance for the code they submit, at least
initially. New features need to be illustrated with narrative
documentation in the user guide, with small code snippets.
If relevant, please also add references in the literature, with PDF links
when possible.
10. The user guide should also include expected time and space complexity
of the algorithm and scalability, e.g. "this algorithm can scale to a
large number of samples > 100000, but does not scale in dimensionality:
n_features is expected to be lower than 100".
You can also check our :ref:`code_review` to get an idea of what reviewers
will expect.
You can check for common programming errors with the following tools:
* Code with a good unittest coverage (at least 80%, better 100%), check
with:
.. prompt:: bash $
pip install pytest pytest-cov
pytest --cov sklearn path/to/tests_for_package
see also :ref:`testing_coverage`
Run static analysis with `mypy`:
.. prompt:: bash $
mypy sklearn
must not produce new errors in your pull request. Using `# type: ignore`
annotation can be a workaround for a few cases that are not supported by
mypy, in particular,
- when importing C or Cython modules
- on properties with decorators
Bonus points for contributions that include a performance analysis with
a benchmark script and profiling output (see :ref:`monitoring_performances`).
Also check out the :ref:`performance-howto` guide for more details on
profiling and Cython optimizations.
.. note::
The current state of the scikit-learn code base is not compliant with
all of those guidelines, but we expect that enforcing those constraints
on all new contributions will get the overall code base quality in the
right direction.
.. note::
For two very well documented and more detailed guides on development
workflow, please pay a visit to the `Scipy Development Workflow
<http://scipy.github.io/devdocs/dev/dev_quickstart.html>`_ -
and the `Astropy Workflow for Developers
<https://astropy.readthedocs.io/en/latest/development/workflow/development_workflow.html>`_
sections.
Continuous Integration (CI)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
* Azure pipelines are used for testing scikit-learn on Linux, Mac and Windows,
with different dependencies and settings.
* CircleCI is used to build the docs for viewing.
* Github Actions are used for various tasks, including building wheels and
source distributions.
* Cirrus CI is used to build on ARM.
Please note that if one of the following markers appear in the latest commit
message, the following actions are taken.
====================== ===================
Commit Message Marker Action Taken by CI
---------------------- -------------------
[ci skip] CI is skipped completely
[cd build] CD is run (wheels and source distribution are built)
[cd build gh] CD is run only for GitHub Actions
[cd build cirrus] CD is run only for Cirrus CI
[lint skip] Azure pipeline skips linting
[scipy-dev] Build & test with our dependencies (numpy, scipy, etc.) development builds
[nogil] Build & test with the nogil experimental branches of CPython, Cython, NumPy, SciPy, ...
[pypy] Build & test with PyPy
[pyodide] Build & test with Pyodide
[azure parallel] Run Azure CI jobs in parallel
[cirrus arm] Run Cirrus CI ARM test
[float32] Run float32 tests by setting `SKLEARN_RUN_FLOAT32_TESTS=1`. See :ref:`environment_variable` for more details
[doc skip] Docs are not built
[doc quick] Docs built, but excludes example gallery plots
[doc build] Docs built including example gallery plots (very long)
====================== ===================
Note that, by default, the documentation is built but only the examples
that are directly modified by the pull request are executed.
.. _stalled_pull_request:
Stalled pull requests
^^^^^^^^^^^^^^^^^^^^^
As contributing a feature can be a lengthy process, some
pull requests appear inactive but unfinished. In such a case, taking
them over is a great service for the project.
A good etiquette to take over is:
* **Determine if a PR is stalled**
* A pull request may have the label "stalled" or "help wanted" if we
have already identified it as a candidate for other contributors.
* To decide whether an inactive PR is stalled, ask the contributor if
she/he plans to continue working on the PR in the near future.
Failure to respond within 2 weeks with an activity that moves the PR
forward suggests that the PR is stalled and will result in tagging
that PR with "help wanted".
Note that if a PR has received earlier comments on the contribution
that have had no reply in a month, it is safe to assume that the PR
is stalled and to shorten the wait time to one day.
After a sprint, follow-up for un-merged PRs opened during sprint will
be communicated to participants at the sprint, and those PRs will be
tagged "sprint". PRs tagged with "sprint" can be reassigned or
declared stalled by sprint leaders.
* **Taking over a stalled PR**: To take over a PR, it is important to
comment on the stalled PR that you are taking over and to link from the
new PR to the old one. The new PR should be created by pulling from the
old one.
Stalled and Unclaimed Issues
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Generally speaking, issues which are up for grabs will have a
`"help wanted" <https://github.com/scikit-learn/scikit-learn/labels/help%20wanted>`_.
tag. However, not all issues which need contributors will have this tag,
as the "help wanted" tag is not always up-to-date with the state
of the issue. Contributors can find issues which are still up for grabs
using the following guidelines:
* First, to **determine if an issue is claimed**:
* Check for linked pull requests
* Check the conversation to see if anyone has said that they're working on
creating a pull request
* If a contributor comments on an issue to say they are working on it,
a pull request is expected within 2 weeks (new contributor) or 4 weeks
(contributor or core dev), unless an larger time frame is explicitly given.
Beyond that time, another contributor can take the issue and make a
pull request for it. We encourage contributors to comment directly on the
stalled or unclaimed issue to let community members know that they will be
working on it.
* If the issue is linked to a :ref:`stalled pull request <stalled_pull_request>`,
we recommend that contributors follow the procedure
described in the :ref:`stalled_pull_request`
section rather than working directly on the issue.
.. _new_contributors:
Issues for New Contributors
---------------------------
New contributors should look for the following tags when looking for issues. We
strongly recommend that new contributors tackle "easy" issues first: this helps
the contributor become familiar with the contribution workflow, and for the core
devs to become acquainted with the contributor; besides which, we frequently
underestimate how easy an issue is to solve!
.. topic:: good first issue tag
A great way to start contributing to scikit-learn is to pick an item from
the list of `good first issues
<https://github.com/scikit-learn/scikit-learn/labels/good%20first%20issue>`_
in the issue tracker. Resolving these issues allow you to start contributing
to the project without much prior knowledge. If you have already contributed
to scikit-learn, you should look at Easy issues instead.
.. topic:: Easy tag
If you have already contributed to scikit-learn, another great way to contribute
to scikit-learn is to pick an item from the list of `Easy issues
<https://github.com/scikit-learn/scikit-learn/labels/Easy>`_ in the issue
tracker. Your assistance in this area will be greatly appreciated by the
more experienced developers as it helps free up their time to concentrate on
other issues.
.. topic:: help wanted tag
We often use the help wanted tag to mark issues regardless of difficulty. Additionally,
we use the help wanted tag to mark Pull Requests which have been abandoned
by their original contributor and are available for someone to pick up where the original
contributor left off. The list of issues with the help wanted tag can be found
`here <https://github.com/scikit-learn/scikit-learn/labels/help%20wanted>`_.
Note that not all issues which need contributors will have this tag.
.. _contribute_documentation:
Documentation
=============
We are glad to accept any sort of documentation:
* **function/method/class docstrings** (also known as "API documentation") -
these describe what the object does and details any parameters, attributes and
methods. Docstrings live alongside the code in
`sklearn/ <https://github.com/scikit-learn/scikit-learn/tree/main/sklearn>`_.
* **user guide** - these provide more detailed information about the algorithms
implemented in scikit-learn and generally live in the root
`doc/ <https://github.com/scikit-learn/scikit-learn/tree/main/doc>`_ directory
and
`doc/modules/ <https://github.com/scikit-learn/scikit-learn/tree/main/doc/modules>`_.
* **tutorials** - these introduce various statistical learning and machine learning
concepts and are located in
`doc/tutorial <https://github.com/scikit-learn/scikit-learn/tree/main/doc/tutorial>`_.
* **examples** - these provide full code examples that may demonstrate the use
of scikit-learn modules, compare different algorithms or discuss their
interpretation etc. Examples live in
`examples/ <https://github.com/scikit-learn/scikit-learn/tree/main/examples>`_
* **other reStructuredText documents** - provide various other
useful information (e.g., the :ref:`contributing` guide) and live in
`doc/ <https://github.com/scikit-learn/scikit-learn/tree/main/doc>`_.
|details-start|
**Guidelines for writing docstrings**
|details-split|
* When documenting the parameters and attributes, here is a list of some
well-formatted examples::
n_clusters : int, default=3
The number of clusters detected by the algorithm.
some_param : {'hello', 'goodbye'}, bool or int, default=True
The parameter description goes here, which can be either a string
literal (either `hello` or `goodbye`), a bool, or an int. The default
value is True.
array_parameter : {array-like, sparse matrix} of shape (n_samples, n_features) or (n_samples,)
This parameter accepts data in either of the mentioned forms, with one
of the mentioned shapes. The default value is
`np.ones(shape=(n_samples,))`.
list_param : list of int
typed_ndarray : ndarray of shape (n_samples,), dtype=np.int32
sample_weight : array-like of shape (n_samples,), default=None
multioutput_array : ndarray of shape (n_samples, n_classes) or list of such arrays
In general have the following in mind:
* Use Python basic types. (``bool`` instead of ``boolean``)
* Use parenthesis for defining shapes: ``array-like of shape (n_samples,)``
or ``array-like of shape (n_samples, n_features)``
* For strings with multiple options, use brackets: ``input: {'log',
'squared', 'multinomial'}``
* 1D or 2D data can be a subset of ``{array-like, ndarray, sparse matrix,
dataframe}``. Note that ``array-like`` can also be a ``list``, while
``ndarray`` is explicitly only a ``numpy.ndarray``.
* Specify ``dataframe`` when "frame-like" features are being used, such as
the column names.
* When specifying the data type of a list, use ``of`` as a delimiter: ``list
of int``. When the parameter supports arrays giving details about the
shape and/or data type and a list of such arrays, you can use one of
``array-like of shape (n_samples,) or list of such arrays``.
* When specifying the dtype of an ndarray, use e.g. ``dtype=np.int32`` after
defining the shape: ``ndarray of shape (n_samples,), dtype=np.int32``. You
can specify multiple dtype as a set: ``array-like of shape (n_samples,),
dtype={np.float64, np.float32}``. If one wants to mention arbitrary
precision, use `integral` and `floating` rather than the Python dtype
`int` and `float`. When both `int` and `floating` are supported, there is
no need to specify the dtype.
* When the default is ``None``, ``None`` only needs to be specified at the
end with ``default=None``. Be sure to include in the docstring, what it
means for the parameter or attribute to be ``None``.
* Add "See Also" in docstrings for related classes/functions.
* "See Also" in docstrings should be one line per reference, with a colon and an
explanation, for example::
See Also
--------
SelectKBest : Select features based on the k highest scores.
SelectFpr : Select features based on a false positive rate test.
* Add one or two snippets of code in "Example" section to show how it can be used.
|details-end|
|details-start|
**Guidelines for writing the user guide and other reStructuredText documents**
|details-split|
It is important to keep a good compromise between mathematical and algorithmic
details, and give intuition to the reader on what the algorithm does.
* Begin with a concise, hand-waving explanation of what the algorithm/code does on
the data.
* Highlight the usefulness of the feature and its recommended application.
Consider including the algorithm's complexity
(:math:`O\left(g\left(n\right)\right)`) if available, as "rules of thumb" can
be very machine-dependent. Only if those complexities are not available, then
rules of thumb may be provided instead.
* Incorporate a relevant figure (generated from an example) to provide intuitions.
* Include one or two short code examples to demonstrate the feature's usage.
* Introduce any necessary mathematical equations, followed by references. By
deferring the mathematical aspects, the documentation becomes more accessible
to users primarily interested in understanding the feature's practical
implications rather than its underlying mechanics.
* When editing reStructuredText (``.rst``) files, try to keep line length under
88 characters when possible (exceptions include links and tables).
* In scikit-learn reStructuredText files both single and double backticks
surrounding text will render as inline literal (often used for code, e.g.,
`list`). This is due to specific configurations we have set. Single
backticks should be used nowadays.
* Too much information makes it difficult for users to access the content they
are interested in. Use dropdowns to factorize it by using the following
syntax::
|details-start|
**Dropdown title**
|details-split|
Dropdown content.
|details-end|
The snippet above will result in the following dropdown:
|details-start|
**Dropdown title**
|details-split|
Dropdown content.
|details-end|
* Information that can be hidden by default using dropdowns is:
* low hierarchy sections such as `References`, `Properties`, etc. (see for
instance the subsections in :ref:`det_curve`);
* in-depth mathematical details;
* narrative that is use-case specific;
* in general, narrative that may only interest users that want to go beyond
the pragmatics of a given tool.
* Do not use dropdowns for the low level section `Examples`, as it should stay
visible to all users. Make sure that the `Examples` section comes right after
the main discussion with the least possible folded section in-between.
* Be aware that dropdowns break cross-references. If that makes sense, hide the
reference along with the text mentioning it. Else, do not use dropdown.
|details-end|
|details-start|
**Guidelines for writing references**
|details-split|
* When bibliographic references are available with `arxiv <https://arxiv.org/>`_
or `Digital Object Identifier <https://www.doi.org/>`_ identification numbers,
use the sphinx directives `:arxiv:` or `:doi:`. For example, see references in
:ref:`Spectral Clustering Graphs <spectral_clustering_graph>`.
* For "References" in docstrings, see the Silhouette Coefficient
(:func:`sklearn.metrics.silhouette_score`).
* To cross-reference to other pages in the scikit-learn documentation use the
reStructuredText cross-referencing syntax:
* Section - to link to an arbitrary section in the documentation, use
reference labels (see `Sphinx docs
<https://www.sphinx-doc.org/en/master/usage/restructuredtext/roles.html#ref-role>`_).
For example:
.. code-block:: rst
.. _my-section:
My section
----------
This is the text of the section.
To refer to itself use :ref:`my-section`.
You should not modify existing sphinx reference labels as this would break
existing cross references and external links pointing to specific sections
in the scikit-learn documentation.
* Glossary - linking to a term in the :ref:`glossary`:
.. code-block:: rst
:term:`cross_validation`
* Function - to link to the documentation of a function, use the full import
path to the function:
.. code-block:: rst
:func:`~sklearn.model_selection.cross_val_score`
However, if there is a `.. currentmodule::` directive above you in the document,
you will only need to use the path to the function succeeding the current
module specified. For example:
.. code-block:: rst
.. currentmodule:: sklearn.model_selection
:func:`cross_val_score`
* Class - to link to documentation of a class, use the full import path to the
class, unless there is a 'currentmodule' directive in the document above
(see above):
.. code-block:: rst
:class:`~sklearn.preprocessing.StandardScaler`
|details-end|
You can edit the documentation using any text editor, and then generate the
HTML output by following :ref:`building_documentation`. The resulting HTML files
will be placed in ``_build/html/stable`` and are viewable in a web browser, for
instance by opening the local ``_build/html/stable/index.html`` file.
.. _building_documentation:
Building the documentation
--------------------------
**Before submitting a pull request check if your modifications have introduced
new sphinx warnings by building the documentation locally and try to fix them.**
First, make sure you have :ref:`properly installed <install_bleeding_edge>`
the development version.
..
packaging is not needed once setuptools starts shipping packaging>=17.0
Building the documentation requires installing some additional packages:
.. prompt:: bash $
pip install sphinx sphinx-gallery numpydoc matplotlib Pillow pandas \
scikit-image packaging seaborn sphinx-prompt \
sphinxext-opengraph sphinx-copybutton plotly pooch
To build the documentation, you need to be in the ``doc`` folder:
.. prompt:: bash $
cd doc
In the vast majority of cases, you only need to generate the full web site,
without the example gallery:
.. prompt:: bash $
make
The documentation will be generated in the ``_build/html/stable`` directory
and are viewable in a web browser, for instance by opening the local
``_build/html/stable/index.html`` file.
To also generate the example gallery you can use:
.. prompt:: bash $
make html
This will run all the examples, which takes a while. If you only want to
generate a few examples, you can use:
.. prompt:: bash $
EXAMPLES_PATTERN=your_regex_goes_here make html
This is particularly useful if you are modifying a few examples.
Set the environment variable `NO_MATHJAX=1` if you intend to view
the documentation in an offline setting.
To build the PDF manual, run:
.. prompt:: bash $
make latexpdf
.. warning:: **Sphinx version**
While we do our best to have the documentation build under as many
versions of Sphinx as possible, the different versions tend to
behave slightly differently. To get the best results, you should
use the same version as the one we used on CircleCI. Look at this
`GitHub search <https://github.com/search?q=repo%3Ascikit-learn%2Fscikit-learn+%2F%5C%2Fsphinx-%5B0-9.%5D%2B%2F+path%3Abuild_tools%2Fcircle%2Fdoc_linux-64_conda.lock&type=code>`_
to know the exact version.
.. _generated_doc_CI:
Generated documentation on GitHub Actions
-----------------------------------------
When you change the documentation in a pull request, GitHub Actions automatically
builds it. To view the documentation generated by GitHub Actions, simply go to the
bottom of your PR page, look for the item "Check the rendered docs here!" and
click on 'details' next to it:
.. image:: ../images/generated-doc-ci.png
:align: center
.. _testing_coverage:
Testing and improving test coverage
===================================
High-quality `unit testing <https://en.wikipedia.org/wiki/Unit_testing>`_
is a corner-stone of the scikit-learn development process. For this
purpose, we use the `pytest <https://docs.pytest.org>`_
package. The tests are functions appropriately named, located in `tests`
subdirectories, that check the validity of the algorithms and the
different options of the code.
Running `pytest` in a folder will run all the tests of the corresponding
subpackages. For a more detailed `pytest` workflow, please refer to the
:ref:`pr_checklist`.
We expect code coverage of new features to be at least around 90%.
Writing matplotlib related tests
--------------------------------
Test fixtures ensure that a set of tests will be executing with the appropriate
initialization and cleanup. The scikit-learn test suite implements a fixture
which can be used with ``matplotlib``.
``pyplot``
The ``pyplot`` fixture should be used when a test function is dealing with
``matplotlib``. ``matplotlib`` is a soft dependency and is not required.
This fixture is in charge of skipping the tests if ``matplotlib`` is not
installed. In addition, figures created during the tests will be
automatically closed once the test function has been executed.
To use this fixture in a test function, one needs to pass it as an
argument::
def test_requiring_mpl_fixture(pyplot):
# you can now safely use matplotlib
Workflow to improve test coverage
---------------------------------
To test code coverage, you need to install the `coverage
<https://pypi.org/project/coverage/>`_ package in addition to pytest.
1. Run 'make test-coverage'. The output lists for each file the line
numbers that are not tested.
2. Find a low hanging fruit, looking at which lines are not tested,
write or adapt a test specifically for these lines.
3. Loop.
.. _monitoring_performances:
Monitoring performance
======================
*This section is heavily inspired from the* `pandas documentation
<https://pandas.pydata.org/docs/development/contributing_codebase.html#running-the-performance-test-suite>`_.
When proposing changes to the existing code base, it's important to make sure
that they don't introduce performance regressions. Scikit-learn uses
`asv benchmarks <https://github.com/airspeed-velocity/asv>`_ to monitor the
performance of a selection of common estimators and functions. You can view
these benchmarks on the `scikit-learn benchmark page <https://scikit-learn.org/scikit-learn-benchmarks>`_.
The corresponding benchmark suite can be found in the `scikit-learn/asv_benchmarks` directory.
To use all features of asv, you will need either `conda` or `virtualenv`. For
more details please check the `asv installation webpage
<https://asv.readthedocs.io/en/latest/installing.html>`_.
First of all you need to install the development version of asv:
.. prompt:: bash $
pip install git+https://github.com/airspeed-velocity/asv
and change your directory to `asv_benchmarks/`:
.. prompt:: bash $
cd asv_benchmarks/
The benchmark suite is configured to run against your local clone of
scikit-learn. Make sure it is up to date:
.. prompt:: bash $
git fetch upstream
In the benchmark suite, the benchmarks are organized following the same
structure as scikit-learn. For example, you can compare the performance of a
specific estimator between ``upstream/main`` and the branch you are working on:
.. prompt:: bash $
asv continuous -b LogisticRegression upstream/main HEAD
The command uses conda by default for creating the benchmark environments. If
you want to use virtualenv instead, use the `-E` flag:
.. prompt:: bash $
asv continuous -E virtualenv -b LogisticRegression upstream/main HEAD
You can also specify a whole module to benchmark:
.. prompt:: bash $
asv continuous -b linear_model upstream/main HEAD
You can replace `HEAD` by any local branch. By default it will only report the
benchmarks that have change by at least 10%. You can control this ratio with
the `-f` flag.
To run the full benchmark suite, simply remove the `-b` flag :
.. prompt:: bash $
asv continuous upstream/main HEAD
However this can take up to two hours. The `-b` flag also accepts a regular
expression for a more complex subset of benchmarks to run.
To run the benchmarks without comparing to another branch, use the `run`
command:
.. prompt:: bash $
asv run -b linear_model HEAD^!
You can also run the benchmark suite using the version of scikit-learn already
installed in your current Python environment:
.. prompt:: bash $
asv run --python=same
It's particularly useful when you installed scikit-learn in editable mode to
avoid creating a new environment each time you run the benchmarks. By default
the results are not saved when using an existing installation. To save the
results you must specify a commit hash:
.. prompt:: bash $
asv run --python=same --set-commit-hash=<commit hash>
Benchmarks are saved and organized by machine, environment and commit. To see
the list of all saved benchmarks:
.. prompt:: bash $
asv show
and to see the report of a specific run:
.. prompt:: bash $
asv show <commit hash>
When running benchmarks for a pull request you're working on please report the
results on github.
The benchmark suite supports additional configurable options which can be set
in the `benchmarks/config.json` configuration file. For example, the benchmarks
can run for a provided list of values for the `n_jobs` parameter.
More information on how to write a benchmark and how to use asv can be found in
the `asv documentation <https://asv.readthedocs.io/en/latest/index.html>`_.
.. _issue_tracker_tags:
Issue Tracker Tags
==================
All issues and pull requests on the
`GitHub issue tracker <https://github.com/scikit-learn/scikit-learn/issues>`_
should have (at least) one of the following tags:
:Bug / Crash:
Something is happening that clearly shouldn't happen.
Wrong results as well as unexpected errors from estimators go here.
:Cleanup / Enhancement:
Improving performance, usability, consistency.
:Documentation:
Missing, incorrect or sub-standard documentations and examples.
:New Feature:
Feature requests and pull requests implementing a new feature.
There are four other tags to help new contributors:
:good first issue:
This issue is ideal for a first contribution to scikit-learn. Ask for help
if the formulation is unclear. If you have already contributed to
scikit-learn, look at Easy issues instead.
:Easy:
This issue can be tackled without much prior experience.
:Moderate:
Might need some knowledge of machine learning or the package,
but is still approachable for someone new to the project.
:help wanted:
This tag marks an issue which currently lacks a contributor or a
PR that needs another contributor to take over the work. These
issues can range in difficulty, and may not be approachable
for new contributors. Note that not all issues which need
contributors will have this tag.
.. _backwards-compatibility:
Maintaining backwards compatibility
===================================
.. _contributing_deprecation:
Deprecation
-----------
If any publicly accessible method, function, attribute or parameter
is renamed, we still support the old one for two releases and issue
a deprecation warning when it is called/passed/accessed.
E.g., if the function ``zero_one`` is renamed to ``zero_one_loss``,
we add the decorator ``deprecated`` (from ``sklearn.utils``)
to ``zero_one`` and call ``zero_one_loss`` from that function::
from ..utils import deprecated
def zero_one_loss(y_true, y_pred, normalize=True):
# actual implementation
pass
@deprecated("Function 'zero_one' was renamed to 'zero_one_loss' "
"in version 0.13 and will be removed in release 0.15. "
"Default behavior is changed from 'normalize=False' to "
"'normalize=True'")
def zero_one(y_true, y_pred, normalize=False):
return zero_one_loss(y_true, y_pred, normalize)
If an attribute is to be deprecated,
use the decorator ``deprecated`` on a property. Please note that the
``property`` decorator should be placed before the ``deprecated``
decorator for the docstrings to be rendered properly.
E.g., renaming an attribute ``labels_`` to ``classes_`` can be done as::
@deprecated("Attribute `labels_` was deprecated in version 0.13 and "
"will be removed in 0.15. Use `classes_` instead")
@property
def labels_(self):
return self.classes_
If a parameter has to be deprecated, a ``FutureWarning`` warning
must be raised too.
In the following example, k is deprecated and renamed to n_clusters::
import warnings
def example_function(n_clusters=8, k='deprecated'):
if k != 'deprecated':
warnings.warn("'k' was renamed to n_clusters in version 0.13 and "
"will be removed in 0.15.",
FutureWarning)
n_clusters = k
When the change is in a class, we validate and raise warning in ``fit``::
import warnings
class ExampleEstimator(BaseEstimator):
def __init__(self, n_clusters=8, k='deprecated'):
self.n_clusters = n_clusters
self.k = k
def fit(self, X, y):
if self.k != 'deprecated':
warnings.warn("'k' was renamed to n_clusters in version 0.13 and "
"will be removed in 0.15.",
FutureWarning)
self._n_clusters = self.k
else:
self._n_clusters = self.n_clusters
As in these examples, the warning message should always give both the
version in which the deprecation happened and the version in which the
old behavior will be removed. If the deprecation happened in version
0.x-dev, the message should say deprecation occurred in version 0.x and
the removal will be in 0.(x+2), so that users will have enough time to
adapt their code to the new behaviour. For example, if the deprecation happened
in version 0.18-dev, the message should say it happened in version 0.18
and the old behavior will be removed in version 0.20.
In addition, a deprecation note should be added in the docstring, recalling the
same information as the deprecation warning as explained above. Use the
``.. deprecated::`` directive::
.. deprecated:: 0.13
``k`` was renamed to ``n_clusters`` in version 0.13 and will be removed
in 0.15.
What's more, a deprecation requires a test which ensures that the warning is
raised in relevant cases but not in other cases. The warning should be caught
in all other tests (using e.g., ``@pytest.mark.filterwarnings``),
and there should be no warning in the examples.
Change the default value of a parameter
---------------------------------------
If the default value of a parameter needs to be changed, please replace the
default value with a specific value (e.g., ``warn``) and raise
``FutureWarning`` when users are using the default value. The following
example assumes that the current version is 0.20 and that we change the
default value of ``n_clusters`` from 5 (old default for 0.20) to 10
(new default for 0.22)::
import warnings
def example_function(n_clusters='warn'):
if n_clusters == 'warn':
warnings.warn("The default value of n_clusters will change from "
"5 to 10 in 0.22.", FutureWarning)
n_clusters = 5
When the change is in a class, we validate and raise warning in ``fit``::
import warnings
class ExampleEstimator:
def __init__(self, n_clusters='warn'):
self.n_clusters = n_clusters
def fit(self, X, y):
if self.n_clusters == 'warn':
warnings.warn("The default value of n_clusters will change from "
"5 to 10 in 0.22.", FutureWarning)
self._n_clusters = 5
Similar to deprecations, the warning message should always give both the
version in which the change happened and the version in which the old behavior
will be removed.
The parameter description in the docstring needs to be updated accordingly by adding
a `versionchanged` directive with the old and new default value, pointing to the
version when the change will be effective::
.. versionchanged:: 0.22
The default value for `n_clusters` will change from 5 to 10 in version 0.22.
Finally, we need a test which ensures that the warning is raised in relevant cases but
not in other cases. The warning should be caught in all other tests
(using e.g., ``@pytest.mark.filterwarnings``), and there should be no warning
in the examples.
.. currentmodule:: sklearn
.. _code_review:
Code Review Guidelines
======================
Reviewing code contributed to the project as PRs is a crucial component of
scikit-learn development. We encourage anyone to start reviewing code of other
developers. The code review process is often highly educational for everybody
involved. This is particularly appropriate if it is a feature you would like to
use, and so can respond critically about whether the PR meets your needs. While
each pull request needs to be signed off by two core developers, you can speed
up this process by providing your feedback.
.. note::
The difference between an objective improvement and a subjective nit isn't
always clear. Reviewers should recall that code review is primarily about
reducing risk in the project. When reviewing code, one should aim at
preventing situations which may require a bug fix, a deprecation, or a
retraction. Regarding docs: typos, grammar issues and disambiguations are
better addressed immediately.
Here are a few important aspects that need to be covered in any code review,
from high-level questions to a more detailed check-list.
- Do we want this in the library? Is it likely to be used? Do you, as
a scikit-learn user, like the change and intend to use it? Is it in
the scope of scikit-learn? Will the cost of maintaining a new
feature be worth its benefits?
- Is the code consistent with the API of scikit-learn? Are public
functions/classes/parameters well named and intuitively designed?
- Are all public functions/classes and their parameters, return types, and
stored attributes named according to scikit-learn conventions and documented clearly?
- Is any new functionality described in the user-guide and illustrated with examples?
- Is every public function/class tested? Are a reasonable set of
parameters, their values, value types, and combinations tested? Do
the tests validate that the code is correct, i.e. doing what the
documentation says it does? If the change is a bug-fix, is a
non-regression test included? Look at `this
<https://jeffknupp.com/blog/2013/12/09/improve-your-python-understanding-unit-testing>`__
to get started with testing in Python.
- Do the tests pass in the continuous integration build? If
appropriate, help the contributor understand why tests failed.
- Do the tests cover every line of code (see the coverage report in the build
log)? If not, are the lines missing coverage good exceptions?
- Is the code easy to read and low on redundancy? Should variable names be
improved for clarity or consistency? Should comments be added? Should comments
be removed as unhelpful or extraneous?
- Could the code easily be rewritten to run much more efficiently for
relevant settings?
- Is the code backwards compatible with previous versions? (or is a
deprecation cycle necessary?)
- Will the new code add any dependencies on other libraries? (this is
unlikely to be accepted)
- Does the documentation render properly (see the
:ref:`contribute_documentation` section for more details), and are the plots
instructive?
:ref:`saved_replies` includes some frequent comments that reviewers may make.
.. _communication:
Communication Guidelines
------------------------
Reviewing open pull requests (PRs) helps move the project forward. It is a
great way to get familiar with the codebase and should motivate the
contributor to keep involved in the project. [1]_
- Every PR, good or bad, is an act of generosity. Opening with a positive
comment will help the author feel rewarded, and your subsequent remarks may
be heard more clearly. You may feel good also.
- Begin if possible with the large issues, so the author knows they've been
understood. Resist the temptation to immediately go line by line, or to open
with small pervasive issues.
- Do not let perfect be the enemy of the good. If you find yourself making
many small suggestions that don't fall into the :ref:`code_review`, consider
the following approaches:
- refrain from submitting these;
- prefix them as "Nit" so that the contributor knows it's OK not to address;
- follow up in a subsequent PR, out of courtesy, you may want to let the
original contributor know.
- Do not rush, take the time to make your comments clear and justify your
suggestions.
- You are the face of the project. Bad days occur to everyone, in that
occasion you deserve a break: try to take your time and stay offline.
.. [1] Adapted from the numpy `communication guidelines
<https://numpy.org/devdocs/dev/reviewer_guidelines.html#communication-guidelines>`_.
Reading the existing code base
==============================
Reading and digesting an existing code base is always a difficult exercise
that takes time and experience to master. Even though we try to write simple
code in general, understanding the code can seem overwhelming at first,
given the sheer size of the project. Here is a list of tips that may help
make this task easier and faster (in no particular order).
- Get acquainted with the :ref:`api_overview`: understand what :term:`fit`,
:term:`predict`, :term:`transform`, etc. are used for.
- Before diving into reading the code of a function / class, go through the
docstrings first and try to get an idea of what each parameter / attribute
is doing. It may also help to stop a minute and think *how would I do this
myself if I had to?*
- The trickiest thing is often to identify which portions of the code are
relevant, and which are not. In scikit-learn **a lot** of input checking
is performed, especially at the beginning of the :term:`fit` methods.
Sometimes, only a very small portion of the code is doing the actual job.
For example looking at the ``fit()`` method of
:class:`~linear_model.LinearRegression`, what you're looking for
might just be the call the ``scipy.linalg.lstsq``, but it is buried into
multiple lines of input checking and the handling of different kinds of
parameters.
- Due to the use of `Inheritance
<https://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)>`_,
some methods may be implemented in parent classes. All estimators inherit
at least from :class:`~base.BaseEstimator`, and
from a ``Mixin`` class (e.g. :class:`~base.ClassifierMixin`) that enables default
behaviour depending on the nature of the estimator (classifier, regressor,
transformer, etc.).
- Sometimes, reading the tests for a given function will give you an idea of
what its intended purpose is. You can use ``git grep`` (see below) to find
all the tests written for a function. Most tests for a specific
function/class are placed under the ``tests/`` folder of the module
- You'll often see code looking like this:
``out = Parallel(...)(delayed(some_function)(param) for param in
some_iterable)``. This runs ``some_function`` in parallel using `Joblib
<https://joblib.readthedocs.io/>`_. ``out`` is then an iterable containing
the values returned by ``some_function`` for each call.
- We use `Cython <https://cython.org/>`_ to write fast code. Cython code is
located in ``.pyx`` and ``.pxd`` files. Cython code has a more C-like flavor:
we use pointers, perform manual memory allocation, etc. Having some minimal
experience in C / C++ is pretty much mandatory here. For more information see
:ref:`cython`.
- Master your tools.
- With such a big project, being efficient with your favorite editor or
IDE goes a long way towards digesting the code base. Being able to quickly
jump (or *peek*) to a function/class/attribute definition helps a lot.
So does being able to quickly see where a given name is used in a file.
- `git <https://git-scm.com/book/en>`_ also has some built-in killer
features. It is often useful to understand how a file changed over time,
using e.g. ``git blame`` (`manual
<https://git-scm.com/docs/git-blame>`_). This can also be done directly
on GitHub. ``git grep`` (`examples
<https://git-scm.com/docs/git-grep#_examples>`_) is also extremely
useful to see every occurrence of a pattern (e.g. a function call or a
variable) in the code base.
- Configure `git blame` to ignore the commit that migrated the code style to
`black`.
.. prompt:: bash $
git config blame.ignoreRevsFile .git-blame-ignore-revs
Find out more information in black's
`documentation for avoiding ruining git blame <https://black.readthedocs.io/en/stable/guides/introducing_black_to_your_project.html#avoiding-ruining-git-blame>`_.
|