|
| 1 | +""" |
| 2 | +High level abstraction interfaces to DFFML. These are probably going to be used |
| 3 | +in a lot of quick and dirty python files. |
| 4 | +""" |
| 5 | +import pathlib |
| 6 | +from typing import Union, Dict, Any |
| 7 | + |
| 8 | +from .repo import Repo |
| 9 | +from .source.source import Sources, BaseSource |
| 10 | +from .source.memory import MemorySource, MemorySourceConfig |
| 11 | + |
| 12 | + |
| 13 | +def _repos_to_sources(*args): |
| 14 | + """ |
| 15 | + Create a memory source out of any repos passed as a variable length list. |
| 16 | + Add all sources found in the variable length list to a list of sources, and |
| 17 | + the created source containing repos, and return that list of sources. |
| 18 | + """ |
| 19 | + # If the first arg is an instance of sources, append the rest to that. |
| 20 | + if args and isinstance(args[0], Sources): |
| 21 | + sources = args[0] |
| 22 | + else: |
| 23 | + sources = Sources( |
| 24 | + *[arg for arg in args if isinstance(arg, BaseSource)] |
| 25 | + ) |
| 26 | + # Repos to add to memory source |
| 27 | + repos = [] |
| 28 | + # Make args mutable |
| 29 | + args = list(args) |
| 30 | + # Convert dicts to repos |
| 31 | + for i, arg in enumerate(args): |
| 32 | + if isinstance(arg, dict): |
| 33 | + arg = Repo(i, data={"features": arg}) |
| 34 | + if isinstance(arg, Repo): |
| 35 | + repos.append(arg) |
| 36 | + if isinstance(arg, str) and "." in arg: |
| 37 | + filepath = pathlib.Path(arg) |
| 38 | + source = BaseSource.load(filepath.suffix.replace(".", "")) |
| 39 | + sources.append(source(filename=arg)) |
| 40 | + # Create memory source if there are any repos |
| 41 | + if repos: |
| 42 | + sources.append(MemorySource(MemorySourceConfig(repos=repos))) |
| 43 | + return sources |
| 44 | + |
| 45 | + |
| 46 | +async def train(model, *args: Union[BaseSource, Repo, Dict[str, Any]]): |
| 47 | + """ |
| 48 | + Train a machine learning model. |
| 49 | +
|
| 50 | + Provide records to the model to train it. The model should be already |
| 51 | + instantiated. |
| 52 | +
|
| 53 | + Parameters |
| 54 | + ---------- |
| 55 | + model : Model |
| 56 | + Machine Learning model to use. See :doc:`/plugins/dffml_model` for |
| 57 | + models options. |
| 58 | + *args : list |
| 59 | + Input data for training. Could be a ``dict``, :py:class:`Repo`, |
| 60 | + filename, one of the data :doc:`/plugins/dffml_source`, or a filename |
| 61 | + with the extension being one of the data sources. |
| 62 | + """ |
| 63 | + sources = _repos_to_sources(*args) |
| 64 | + async with sources as sources, model as model: |
| 65 | + async with sources() as sctx, model() as mctx: |
| 66 | + return await mctx.train(sctx) |
| 67 | + |
| 68 | + |
| 69 | +async def accuracy( |
| 70 | + model, *args: Union[BaseSource, Repo, Dict[str, Any]] |
| 71 | +) -> float: |
| 72 | + """ |
| 73 | + Assess the accuracy of a machine learning model. |
| 74 | +
|
| 75 | + Provide records to the model to assess the percent accuracy of its |
| 76 | + prediction abilities. The model should be already instantiated and trained. |
| 77 | +
|
| 78 | + Parameters |
| 79 | + ---------- |
| 80 | + model : Model |
| 81 | + Machine Learning model to use. See :doc:`/plugins/dffml_model` for |
| 82 | + models options. |
| 83 | + *args : list |
| 84 | + Input data for training. Could be a ``dict``, :py:class:`Repo`, |
| 85 | + filename, one of the data :doc:`/plugins/dffml_source`, or a filename |
| 86 | + with the extension being one of the data sources. |
| 87 | +
|
| 88 | + Returns |
| 89 | + ------- |
| 90 | + float |
| 91 | + A decimal value representing the percent of the time the model made the |
| 92 | + correct prediction. For some models this has another meaning. Please see |
| 93 | + the documentation for the model your using for further details. |
| 94 | + """ |
| 95 | + sources = _repos_to_sources(*args) |
| 96 | + async with sources as sources, model as model: |
| 97 | + async with sources() as sctx, model() as mctx: |
| 98 | + return float(await mctx.accuracy(sctx)) |
| 99 | + |
| 100 | + |
| 101 | +async def predict( |
| 102 | + model, |
| 103 | + *args: Union[BaseSource, Repo, Dict[str, Any]], |
| 104 | + update: bool = False, |
| 105 | + keep_repo: bool = False, |
| 106 | +): |
| 107 | + """ |
| 108 | + Make a prediction using a machine learning model. |
| 109 | +
|
| 110 | + The model must be trained before using it to make a prediction. |
| 111 | +
|
| 112 | + Parameters |
| 113 | + ---------- |
| 114 | + model : Model |
| 115 | + Machine Learning model to use. See :doc:`/plugins/dffml_model` for |
| 116 | + models options. |
| 117 | + *args : list |
| 118 | + Input data for prediction. Could be a ``dict``, :py:class:`Repo`, |
| 119 | + filename, or one of the data :doc:`/plugins/dffml_source`. |
| 120 | + update : boolean, optional |
| 121 | + If ``True`` prediction data within records will be written back to all |
| 122 | + sources given. Defaults to ``False``. |
| 123 | + keep_repo : boolean, optional |
| 124 | + If ``True`` the results will be kept as their ``Repo`` objects instead |
| 125 | + of being converted to a ``(repo.key, features, predictions)`` tuple. |
| 126 | + Defaults to ``False``. |
| 127 | +
|
| 128 | + Returns |
| 129 | + ------- |
| 130 | + asynciterator |
| 131 | + ``Repo`` objects or ``(repo.key, features, predictions)`` tuple. |
| 132 | + """ |
| 133 | + sources = _repos_to_sources(*args) |
| 134 | + async with sources as sources, model as model: |
| 135 | + async with sources() as sctx, model() as mctx: |
| 136 | + async for repo in mctx.predict(sctx.repos()): |
| 137 | + yield repo if keep_repo else ( |
| 138 | + repo.key, |
| 139 | + repo.features(), |
| 140 | + repo.predictions(), |
| 141 | + ) |
| 142 | + if update: |
| 143 | + await sctx.update(repo) |
0 commit comments