Switch to unified view

a b/docs-source/source/plugins.rst
1
.. _plugins:
2
3
Creating a Slideflow Plugin
4
===========================
5
6
Slideflow has been designed to be extensible, and we encourage users to contribute their own plugins to the Slideflow ecosystem. Plugins can be used to add new functionality to Slideflow, such as new feature extractors or new model architectures. This page provides an overview of how to create and use plugins with Slideflow.
7
8
9
MIL Model Registration
10
----------------------
11
12
As discussed in :ref:`custom_mil`, Slideflow supports the registration of custom MIL models. This is done by using the ``register_model`` decorator to register a custom MIL model.
13
14
For example, suppose you have a custom MIL model called ``MyMILModel`` that you want to register with Slideflow. You've already designed the model such that it meets Slideflow's MIL :ref:`requirements <custom_mil>`. Now you want to make it available for use directly within Slideflow. You can accomplish this by using the ``register_model`` decorator:
15
16
.. code-block:: python
17
18
    from slideflow.model.mil import register_model
19
20
    @register_model
21
    def my_mil_model(**kwargs):
22
        from . import MyMILModel
23
        return MyMILModel(**kwargs)
24
25
Once this code is run, the custom MIL model will be available for use with Slideflow:
26
27
.. code-block:: python
28
29
    import slideflow as sf
30
31
    model = sf.build_mil_model("my_mil_model")
32
33
34
Feature Extractors
35
------------------
36
37
Similarly, Slideflow supports the integration of custom feature extractors via the ``register_torch`` and ``register_tf`` decorators. Please see our detailed :ref:`developer note <custom_extractors>` for more information on how to create and register custom extractors. Briefly, you can register a custom feature extractor with Slideflow as follows:
38
39
.. code-block:: python
40
41
    from slideflow.model.extractors import register_torch
42
43
    @register_torch
44
    def my_foundation_model(**kwargs):
45
        from . import MyFoundationModel
46
        return MyFoundationModel(**kwargs)
47
48
49
Creating a Plugin
50
-----------------
51
52
Once you have a custom MIL model or feature extractor that you want to integrate with Slideflow, you can create a plugin to make it available to other users.
53
54
Slideflow supports external plugins via standard Python entry points, allowing you to publish your own package that integrates with Slideflow.
55
56
In your package's ``setup.py`` file, use the "entry_points" key to connect with the Slideflow plugin interface:
57
58
.. code-block:: python
59
60
    ...,
61
    entry_points={
62
        'slideflow.plugins': [
63
            'extras = my_package:register_extras',
64
        ],
65
    },
66
67
Then, in your package's root ``__init__.py`` file, write a ``register_extras()`` function that does any preparation needed to initialize or import your model.
68
69
(in ``my_package/__init__.py``)
70
71
.. code-block:: python
72
73
    def register_extras():
74
        # Import the model, and do any other necessary preparation.
75
        # If my_module contains the @register_model decorator,
76
        # the model will be registered with Slideflow automatically.
77
        from . import my_module
78
79
    print("Registered MyFoundationModel")
80
81
You can then build and distribute your plugin, and once installed, the registration with Slideflow will happen automatically:
82
83
.. code-block:: bash
84
85
    pip install my_package
86
87
88
.. code-block:: python
89
90
    import slideflow as sf
91
92
    model = sf.build_feature_extractor("my_foundation_model")
93
94
95
For a complete example, head over to our `Slideflow-GPL <https://github.com/slideflow/slideflow-gpl>`_ and `Slideflow-NonCommercial <https://github.com/slideflow/slideflow-noncommercial>`_ repositories, which have been built using the plugin system described above.