> ## 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.

# Neptune Ai

> systematica.portfolio.trackers.neptune_ai

## `NeptuneTracker`

```python theme={null}
NeptuneTracker(
    init_kwargs: Dict[str, Any] = _Nothing.NOTHING,
    log_kwargs: Dict[str, Any] = _Nothing.NOTHING,
    live_stream: bool = False,
    add_configs: Dict[str, Any] = _Nothing.NOTHING,
    custom_callback: systematica.portfolio.trackers.custom_neptune_ai.NeptuneCallback = systematica.portfolio.trackers.custom_neptune_callbacks.CustomCallback,
)
```

Handle Neptune-enabled optimization execution tracker.
This class integrates with Neptune to track Optuna studies, allowing
for both live streaming of optimization results and logging of study
metadata after the study is completed.

<Note>
  **Loading an Optuna Study from Neptune**:

  If an Optuna study is logged to Neptune, you can reload it using the `with_id`
  flag in `init_kwargs`. The Neptune ID, found in the Experiments tab or
  `sys` namespace, uniquely identifies a run:

  * Active run ID: `run["sys/id"].fetch()`
  * Custom run ID: `run["sys/custom_run_id"].fetch()`
    Supports both InMemoryStorage and database storage.

  **Logging Optuna Studies to Neptune**:

  `log_kwargs` controls logging via `npt_utils.NeptuneCallback` or
  `log_study_metadata` internally, depending on `live_stream` flag. By
  default, Neptune logs plots from `optuna.visualizations`, which may add
  overhead. Customize logging with:

  * `plot_update_freq`:
    * Integer `k`: Updates plots every *k* trials.
    * `"never"`: Disables plot logging.
  * `log_plot_contour`, `log_plot_slice`, etc.: Set `False` to skip
    specific plots.

  Also, to log one or more study objectives, you can pass a list of objective
  names to the `target_names` argument.
</Note>

Method generated by attrs for class NeptuneTracker.

### Ancestors

* `systematica.portfolio.trackers.base.BaseTracker`
* `abc.ABC`

### Class variables

* `init_kwargs: Dict[str, Any]`: Additional arguments for `neptune.init_run`.

* `log_kwargs: Dict[str, Any]`: Additional neptune logging arguments. More information in the Neptune [documentation](https://docs.neptune.ai/integrations/optuna/#more-options)

* `live_stream: bool`: If True, it runs the Optuna parameter live sweep with the callback. Otherwise, log study metadata after the Study is finished. The latest method is generally faster, as it doesn't log the data live. When called, it logs the same metadata as the callback and accepts the same flags for customization. Defaults to `False`.

* `add_configs: Dict[str, Any]`: Additional config logs to store in neptune.

* `custom_callback: systematica.portfolio.trackers.custom_neptune_ai.NeptuneCallback`: Custom callback. If None, uses original `NeptuneCallback`. Default to `CustomCallback`.

### Static methods

#### `add`

```python theme={null}
add(
    run: neptune.metadata_containers.run.Run,
    logs: Dict[str, Any],
)
```

Additional items to be stored in neptune.

It only adds str, int, float and bool and ignores other data types.

**Parameters**:

| Name   | Type          | Default | Description                               |
| ------ | ------------- | ------- | ----------------------------------------- |
| `run`  | `neptune.Run` | `--`    | Run.                                      |
| `logs` | `tp.Any`      | `--`    | Additional items to be stored in neptune. |

#### `remove`

```python theme={null}
remove(
    run: neptune.metadata_containers.run.Run,
    fields: List[str],
)
```

Remove fields stored in neptune.

**Parameters**:

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

#### `count_fields`

```python theme={null}
count_fields(
    structure: Dict[str, Any],
) ‑> int
```

Count the fields of a Neptune object, use the get\_structure() method
and some scripting.

**Note**:

`structure` is accessible through `run.get_structure()`.

**Parameters**:

| Name        | Type                   | Default | Description                                                 |
| ----------- | ---------------------- | ------- | ----------------------------------------------------------- |
| `structure` | `tp.Dict[str, tp.Any]` | `--`    | Neptune stucture. Accessible through `run.get_structure()`. |

**Returns**:

| Type  | Description       |
| ----- | ----------------- |
| `int` | Number of fields. |

### Methods

#### `run_context`

```python theme={null}
run_context(
    self,
    **kwargs,
)
```

Context manager to initialize and stop a Neptune run.

**Parameters**:

| Name     | Type        | Default | Description                                                               |
| -------- | ----------- | ------- | ------------------------------------------------------------------------- |
| `kwargs` | `tp.Kwargs` | `--`    | Additional arguments to pass to neptune.init\_run (e.g., with\_id, mode). |

**Yields**:

| Type          | Description                         |
| ------------- | ----------------------------------- |
| `neptune.Run` | The initialized Neptune run object. |

#### `run_study`

```python theme={null}
run_study(
    self,
    objective: Callable,
    create_study_kwargs: Dict[str, Any],
    optimize_kwargs: Dict[str, Any],
) ‑> optuna.study.study.Study
```

Run Optuna study.

**Parameters**:

| Name                  | Type             | Default | Description                                     |
| --------------------- | ---------------- | ------- | ----------------------------------------------- |
| `objective`           | `tp.Callable`    | `--`    | Objective function.                             |
| `create_study_kwargs` | `dict, optional` | `--`    | Additional arguments for `optuna.create_study`. |
| `optimize_kwargs`     | `dict, optional` | `--`    | Additional arguments for `study.optimize`.\`    |

**Returns**:

| Type           | Description                                              |
| -------------- | -------------------------------------------------------- |
| `optuna.Study` | The Optuna study object containing optimization results. |
