PrCore Documentation
AI Assistant GitHub Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Files

The basic file structure of PrCore’s code is as follows:

prcore/
├─ core/
├─ plugins/
│  ├─ common/
│  ├─ causallift_treatment_effect/
│  ├─ knn_next_activity/
│  ├─ random_forest_alarm/
├─ simulation/
├─ .env
├─ docker-compose.yml

The plugins directory contains all the plugins.

To add a new plugin, create a new directory in the plugins directory. For example, if you want to add a plugin called foo_bar, create a directory called foo_bar in the plugins directory:

plugins/
├─ common/
├─ causallift_treatment_effect/
├─ random_forest_alarm/
├─ foo_bar/

Next, create the following files in the foo_bar directory:

foo_bar/
├─ __init__.py
├─ algorithm.py
├─ config.py
├─ Dockerfile
├─ main.py
├─ requirements.txt

__init__.py

Just create an empty file called __init__.py in the foo_bar directory.

algorithm.py

This file contains the algorithm that the plugin will use to make predictions. For example, we can create an algorithm FooBarAlgorithm as follows:

 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
import logging
from typing import Any, Dict, List

from pandas import DataFrame

from plugins.common.algorithm import Algorithm

# Enable logging
logger = logging.getLogger(__name__)


class FooBarAlgorithm(Algorithm):
    def __init__(self, algo_data: Dict[str, Any]):
        super().__init__(algo_data)

    def preprocess(self) -> str:
        # Pre-process the data
        pass

    def train(self) -> str:
        # Train the model
        pass

    def predict(self, prefix: List[dict]) -> dict:
        # Predict the result by using the given prefix
        pass

    def predict_df(self, df: DataFrame) -> dict:
        # Predict the result using a DataFrame
        pass

The input and expected output of these functions are described in the Algorithm section.

config.py

This file contains the configuration of the plugin. For example, we can create a configuration file as follows:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import logging
from typing import Any, Dict

from core.confs import config
from core.enums.definition import ColumnDefinition
from core.enums.plugin import PluginType

# Enable logging
logger = logging.getLogger(__name__)

# Pre-defined configuration
basic_info: Dict[str, Any] = {
    "id": config.APP_ID,
    "name": "Foo bar",
    "prescription_type": PluginType.NEXT_ACTIVITY,
    "description": "This is a foo bar plugin",
    "parameters": {},
    "needed_columns": [],
    "needed_info_for_training": [],
    "needed_info_for_prediction": [],
    "supported_encoding": []
}

Here you can change the name of the plugin, the description, the prescription type, and the parameters that the plugin will use. The prescription type can be any string.

The needed_columns variable is a list of the columns that the plugin needs to make prescriptions. The elements should be selected from the following list:

  • ColumnDefinition.OUTCOME
  • ColumnDefinition.TREATMENT
  • ColumnDefinition.TREATMENT_RESOURCE

If you fill in other values, your plugin may not be generically usable. But you can still use it if your data contains the columns that you specified.

Dockerfile

It is used to build the Docker image of the plugin. For example, we can create a Dockerfile as follows:

1
2
3
4
5
6
FROM python:3.10.6
WORKDIR /code
COPY ./core /code/core
COPY ./plugins /code/plugins
RUN pip install --no-cache-dir -r plugins/foo_bar/requirements.txt
CMD ["python", "-m", "plugins.foo_bar.main"]

main.py

This is the entry point of the plugin. For example, we can create a main file as follows:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import logging

from plugins.common.starter import plugin_scheduler, plugin_run
from plugins.foo_bar.algorithm import FooBarAlgorithm
from plugins.foo_bar.config import basic_info

# Enable logging
logger = logging.getLogger(__name__)
for _ in logging.root.manager.loggerDict:
    if _.startswith("pika"):
        logging.getLogger(_).setLevel(logging.CRITICAL)

if __name__ == "__main__":
    plugin_scheduler()
    plugin_run(basic_info, FooBarAlgorithm)

requirements.txt

Since each plugin will be run in a Docker container, you need to specify the dependencies of the plugin in the requirements.txt file.