> ## Documentation Index
> Fetch the complete documentation index at: https://systematica.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Custom Neptune Ai

> systematica.portfolio.trackers.custom_neptune_ai

## Variables

* `INTEGRATION_VERSION_KEY`: Key under which the version of the Neptune-Optuna integration is logged in Neptune. Set to the value of `source_code/integrations/neptune-optuna`.

## `log_study_metadata`

```python theme={null}
log_study_metadata(
    study: optuna.study.study.Study,
    run: neptune.metadata_containers.run.Run,
    base_namespace='',
    log_plots=True,
    log_study=True,
    log_all_trials=True,
    log_distributions=True,
    visualization_backend='plotly',
    log_plot_contour=True,
    log_plot_edf=True,
    log_plot_parallel_coordinate=True,
    log_plot_param_importances=True,
    log_plot_pareto_front=True,
    log_plot_slice=True,
    log_plot_intermediate_values=True,
    log_plot_optimization_history=True,
    target_names: List[str] | None = None,
)
```

Logs the metadata from the Optuna study to Neptune.

With this function, you can log and display:

* Values and params for each trial.
* Current best values and params for the study.
* Visualizations from the optuna.visualizations module.
* Parameter distributions for each trial.
* The study object itself, to load it later.

**Parameters**:

| Name                            | Type           | Default    | Description                                                                                             |
| ------------------------------- | -------------- | ---------- | ------------------------------------------------------------------------------------------------------- |
| `study`                         | `optuna.Study` | `--`       | Optuna study object.                                                                                    |
| `run`                           | `neptune.Run`  | `--`       | Neptune run.                                                                                            |
| `base_namespace`                | `str`          | `""`       | Namespace inside the run where your study metadata is logged.                                           |
| `log_plots`                     | `bool`         | `True`     | If True, the visualizations from optuna.visualizations will be logged to Neptune.                       |
| `log_study`                     | `bool`         | `True`     | If True, the study will be logged to Neptune. The objects that are logged depend                        |
| `log_all_trials`                | `bool`         | `True`     | If True, all trials are logged.                                                                         |
| `log_distributions`             | `bool`         | `True`     | If True, the distributions for all trials are logged.                                                   |
| `visualization_backend`         | `str`          | `"plotly"` | Which visualization backend is used for 'optuna.visualizations' plots.                                  |
| `log_plot_contour`              | `bool`         | `True`     | If True the optuna.visualizations.plot\_contour visualization will be logged to Neptune.                |
| `log_plot_edf`                  | `bool`         | `True`     | If True the optuna.visualizations.plot\_edf visualization will be logged to Neptune.                    |
| `log_plot_parallel_coordinate`  | `bool`         | `True`     | If True the optuna.visualizations.plot\_parallel\_coordinate visualization will be logged to Neptune.   |
| `log_plot_param_importances`    | `bool`         | `True`     | If True the optuna.visualizations.plot\_param\_importances visualization will be logged to Neptune.     |
| `log_plot_pareto_front`         | `bool`         | `True`     | If True the optuna.visualizations.plot\_pareto\_front visualization will be logged to Neptune.          |
| `log_plot_slice`                | `bool`         | `True`     | If True, the optuna.visualizations.plot\_slice visualization will be logged to Neptune.                 |
| `log_plot_intermediate_values`  | `bool`         | `True`     | If True, the optuna.visualizations.plot\_intermediate\_values visualization will be logged to Neptune.  |
| `log_plot_optimization_history` | `bool`         | `True`     | If True, the optuna.visualizations.plot\_optimization\_history visualization will be logged to Neptune. |
| `target_names`                  | `list`         | `--`       | of str, default None List of one or more study objective names to log (see example).                    |

**Examples**:

Initialize a Neptune run and log Optuna study metadata:

```python theme={null}
>>> import neptune
>>> run = neptune.init_run()
```

Create and run the study:

```python theme={null}
>>> study = optuna.create_study(direction="maximize")
>>> study.optimize(objective, n_trials=5)
```

Log single and multi-objective study metadata to Neptune:

```python theme={null}
>>> import neptune.integrations.optuna as npt_utils
>>> npt_utils.log_study_metadata(study, run)
```

Or optionally pass a list of one or more objective names:

```python theme={null}
... npt_utils.log_study_metadata(
...     study,
...     run,
...     target_names=["FLOPS", "accuracy"],
... )
```

For more, see the Neptune-Optuna [Integration Guide](https://docs.neptune.ai/integrations/optuna).

## `load_study_from_run`

```python theme={null}
load_study_from_run(
    run: neptune.metadata_containers.run.Run,
)
```

Loads Optuna study from an existing Neptune run.

Loading mechanics depend on the study storage type used during the run:

* If the study used 'InMemoryStorage', it will be loaded from the logged pickled study object.
* If the study used database storage, it will be loaded from the logged database URL.

To resume an existing run, you need the run ID. It is stored in the run's "sys/id" field.

**Parameters**:

| Name  | Type          | Default | Description  |
| ----- | ------------- | ------- | ------------ |
| `run` | `neptune.Run` | `--`    | Neptune run. |

**Examples**:

Initialize an existing run by passing the run ID:

```python theme={null}
>>> import neptune
>>> run = neptune.init_run(with_id="PRO-123")
```

Load study from the run and continue optimization:

```python theme={null}
>>> import neptune.integrations.optuna as npt_utils
>>> study = npt_utils.load_study_from_run(run)
>>> study.optimize(objective, n_trials=20)
```

For more, see the Neptune-Optuna [Integration Guide](https://docs.neptune.ai/integrations/optuna).

## `NeptuneCallback`

```python theme={null}
NeptuneCallback(
    run: neptune.metadata_containers.run.Run | neptune.handler.Handler,
    base_namespace: str = '',
    plots_update_freq: str | int = 1,
    study_update_freq: str | int = 1,
    visualization_backend: str = 'plotly',
    log_plot_contour: bool = True,
    log_plot_edf: bool = True,
    log_plot_parallel_coordinate: bool = True,
    log_plot_param_importances: bool = True,
    log_plot_pareto_front: bool = True,
    log_plot_slice: bool = True,
    log_plot_intermediate_values: bool = True,
    log_plot_optimization_history: bool = True,
    target_names: List[str] | None = None,
    log_all_trials: bool = True,
)
```

A callback that logs metadata from an Optuna Study to Neptune. With this callback, you can log and display:

* Values and parameters for each trial.
* Current best values and parameters for the study.
* Visualizations from the optuna.visualizations module.
* Parameter distributions for each trial.
* The study object itself, to load it later.

**Parameters**:

| Name                            | Type           | Default  | Description                                                                                                                                                                                               |
| ------------------------------- | -------------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `run`                           | `neptune.Run`  | `--`     | Neptune run object. You can also pass a namespace handler object; for example, `run["test"]`, in which case all metadata is logged under the "test" namespace.                                            |
| `base_namespace`                | `str`          | `""`     | Namespace inside the run where your study metadata is logged.                                                                                                                                             |
| `plots_update_freq`             | `int`          | `1`      | Frequency at which plots are logged and updated in Neptune. If you pass an integer k, plots will be updated every k iterations. If you pass the string 'never', plots will not be logged.                 |
| `study_update_freq`             | `int`          | `1`      | Frequency at which a study object is logged and updated in Neptune. If you pass an integer k, the study will be updated every k iterations. If you pass the string 'never', the study will not be logged. |
| `visualization_backend`         | `str`          | `plotly` | Which visualization backend is used for optuna.visualizations plots. Can be either 'matplotlib' or 'plotly'.                                                                                              |
| `log_plot_contour`              | `bool`         | `True`   | If `True`, the optuna.visualizations.plot\_contour visualization will be logged to Neptune.                                                                                                               |
| `log_plot_edf`                  | `bool`         | `True`   | If `Tru`e, the optuna.visualizations.plot\_edf visualization will be logged to Neptune.                                                                                                                   |
| `log_plot_parallel_coordinate`  | `bool`         | `True`   | If `True`, the optuna.visualizations.plot\_parallel\_coordinate visualization will be logged to Neptune.                                                                                                  |
| `log_plot_param_importances`    | `bool`         | `True`   | If `Tru`e, the optuna.visualizations.plot\_param\_importances visualization will be logged to Neptune.                                                                                                    |
| `log_plot_pareto_front`         | `bool`         | `True`   | If `True`, the optuna.visualizations.plot\_pareto\_front visualization will be logged to Neptune. If your optuna.study is not multi-objective, this plot is not logged.                                   |
| `log_plot_slice`                | `bool`         | `True`   | If `True`, the optuna.visualizations.plot\_slice visualization will be logged to Neptune.                                                                                                                 |
| `log_plot_intermediate_values`  | `bool`         | `True`   | If `True`, the `plot_intermediate_values` visualization will be logged to Neptune. If your `optuna.study` is not using pruners, this plot is not logged.                                                  |
| `log_plot_optimization_history` | `bool`         | `True`   | If `True`, the `plot_optimization_history` visualization will be logged to Neptune.                                                                                                                       |
| `target_names`                  | `tp.List[str]` | `None`   | List of one or more study objective names to log (see example).                                                                                                                                           |
| `log_all_trials`                | `bool`         | `True`   | If \`True, all trials are logged. Defaults to True.                                                                                                                                                       |

**Examples**:

Initialize a Neptune run and log Optuna study metadata.

```python theme={null}
>>> import neptune
>>> run = neptune.init_run()
```

Initialize a NeptuneCallback:

```python theme={null}
>>> import neptune.integrations.optuna as npt_utils
>>> neptune_callback = npt_utils.NeptuneCallback(run)
```

Or optionally pass a list of one or more objective names:

```python theme={null}
... neptune_callback = npt_utils.NeptuneCallback(
...     run,
...     target_names=["FLOPS", "accuracy"],
... )
```

Log single and multi-objective study metadata to Neptune
by passing the callback to the Optuna Study:

```python theme={null}
>>> study = optuna.create_study(direction="maximize")
>>> study.optimize(objective, n_trials=5, callbacks=[neptune_callback])
```

For more, see the Neptune-Optuna [Integration Guide](https://docs.neptune.ai/integrations/optuna).

### Descendants

* `systematica.portfolio.trackers.base.BaseCustomNeptuneCallback`
