|
| 1 | +.. _developers-utils: |
| 2 | + |
| 3 | +======================== |
| 4 | +Utilities for Developers |
| 5 | +======================== |
| 6 | + |
| 7 | +Imbalanced-learn contains a number of utilities to help with development. These are |
| 8 | +located in :mod:`imblearn.utils`, and include tools in a number of categories. |
| 9 | +All the following functions and classes are in the module :mod:`imblearn.utils`. |
| 10 | + |
| 11 | +.. warning :: |
| 12 | +
|
| 13 | + These utilities are meant to be used internally within the imbalanced-learn |
| 14 | + package. They are not guaranteed to be stable between versions of |
| 15 | + imbalance-learn. Backports, in particular, will be removed as the |
| 16 | + imbalance-learn dependencies evolve. |
| 17 | +
|
| 18 | +.. currentmodule:: imblearn.utils |
| 19 | + |
| 20 | +Validation Tools |
| 21 | +================ |
| 22 | + |
| 23 | +These are tools used to check and validate input. When you write a function |
| 24 | +which accepts arrays, matrices, or sparse matrices as arguments, the following |
| 25 | +should be used when applicable. |
| 26 | + |
| 27 | +- :func:`check_neighbors_object`: Check the objects is consistent to be a NN. |
| 28 | +- :func:`check_target_type`: Check the target types to be conform to the current samplers. |
| 29 | +- :func:`check_ratio`: Checks ratio for consistent type and return a dictionary |
| 30 | + containing each targeted class with its corresponding number of pixel. |
| 31 | + |
| 32 | +Deprecation |
| 33 | +----------- |
| 34 | + |
| 35 | +.. warning :: |
| 36 | + Apart from :func:`deprecate_parameter` the rest of this section is taken from |
| 37 | + scikit-learn. Please refer to their original documentation. |
| 38 | +
|
| 39 | +If any publicly accessible method, function, attribute or parameter |
| 40 | +is renamed, we still support the old one for two releases and issue |
| 41 | +a deprecation warning when it is called/passed/accessed. |
| 42 | +E.g., if the function ``zero_one`` is renamed to ``zero_one_loss``, |
| 43 | +we add the decorator ``deprecated`` (from ``sklearn.utils``) |
| 44 | +to ``zero_one`` and call ``zero_one_loss`` from that function:: |
| 45 | + |
| 46 | + from ..utils import deprecated |
| 47 | + |
| 48 | + def zero_one_loss(y_true, y_pred, normalize=True): |
| 49 | + # actual implementation |
| 50 | + pass |
| 51 | + |
| 52 | + @deprecated("Function 'zero_one' was renamed to 'zero_one_loss' " |
| 53 | + "in version 0.13 and will be removed in release 0.15. " |
| 54 | + "Default behavior is changed from 'normalize=False' to " |
| 55 | + "'normalize=True'") |
| 56 | + def zero_one(y_true, y_pred, normalize=False): |
| 57 | + return zero_one_loss(y_true, y_pred, normalize) |
| 58 | + |
| 59 | +If an attribute is to be deprecated, |
| 60 | +use the decorator ``deprecated`` on a property. |
| 61 | +E.g., renaming an attribute ``labels_`` to ``classes_`` can be done as:: |
| 62 | + |
| 63 | + @property |
| 64 | + @deprecated("Attribute labels_ was deprecated in version 0.13 and " |
| 65 | + "will be removed in 0.15. Use 'classes_' instead") |
| 66 | + def labels_(self): |
| 67 | + return self.classes_ |
| 68 | + |
| 69 | +If a parameter has to be deprecated, use ``DeprecationWarning`` appropriately. |
| 70 | +In the following example, k is deprecated and renamed to n_clusters:: |
| 71 | + |
| 72 | + import warnings |
| 73 | + |
| 74 | + def example_function(n_clusters=8, k=None): |
| 75 | + if k is not None: |
| 76 | + warnings.warn("'k' was renamed to n_clusters in version 0.13 and " |
| 77 | + "will be removed in 0.15.", DeprecationWarning) |
| 78 | + n_clusters = k |
| 79 | + |
| 80 | +As in these examples, the warning message should always give both the |
| 81 | +version in which the deprecation happened and the version in which the |
| 82 | +old behavior will be removed. If the deprecation happened in version |
| 83 | +0.x-dev, the message should say deprecation occurred in version 0.x and |
| 84 | +the removal will be in 0.(x+2). For example, if the deprecation happened |
| 85 | +in version 0.18-dev, the message should say it happened in version 0.18 |
| 86 | +and the old behavior will be removed in version 0.20. |
| 87 | + |
| 88 | +In addition, a deprecation note should be added in the docstring, recalling the |
| 89 | +same information as the deprecation warning as explained above. Use the |
| 90 | +``.. deprecated::`` directive:: |
| 91 | + |
| 92 | + .. deprecated:: 0.13 |
| 93 | + ``k`` was renamed to ``n_clusters`` in version 0.13 and will be removed |
| 94 | + in 0.15. |
| 95 | + |
| 96 | +On the top of all the functionality provided by scikit-learn. Imbalance-learn |
| 97 | +provides :func:`deprecate_parameter`: which is used to deprecate a sampler's |
| 98 | +parameter (attribute) by another one. |
0 commit comments