Neptune Query

1.14.1 · active · verified Mon Apr 13

Neptune Query is a Python library (current version 1.14.1) for retrieving logged metadata from the Neptune MLOps platform. It provides a read-only API to programmatically fetch experiments, runs, and their associated attributes, often as Pandas DataFrames. The library is actively maintained with frequent minor and patch releases, offering a stable interface for data retrieval and analysis.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to authenticate with Neptune.ai, list experiments within a specified project, fetch experiment metadata as a DataFrame using `fetch_experiments_table`, and retrieve a specific metric series using `fetch_metrics`. It highlights the use of environment variables for credentials and explicit `project` arguments. Make sure to replace placeholder values or set your `NEPTUNE_API_TOKEN` and `NEPTUNE_PROJECT` environment variables before running.

import os
import neptune_query as nq
import pandas as pd

# Set your Neptune API token and project name as environment variables.
# Example: export NEPTUNE_API_TOKEN="YOUR_API_TOKEN"
# Example: export NEPTUNE_PROJECT="workspace-name/project-name"

# Ensure environment variables are set or pass them explicitly
neptune_api_token = os.environ.get('NEPTUNE_API_TOKEN', 'YOUR_API_TOKEN_HERE')
neptune_project = os.environ.get('NEPTUNE_PROJECT', 'your_workspace/your_project')

if neptune_api_token == 'YOUR_API_TOKEN_HERE' or neptune_project == 'your_workspace/your_project':
    print("Warning: Please set NEPTUNE_API_TOKEN and NEPTUNE_PROJECT environment variables or provide them explicitly.")
    # For demonstration, we'll skip further execution if credentials aren't set.
    # In a real application, handle this appropriately (e.g., raise an error, prompt user).
    exit()

# Set the API token for the session (optional if NEPTUNE_API_TOKEN env var is set)
nq.set_api_token(api_token=neptune_api_token)

# List experiments in a project
print(f"Listing experiments in project: {neptune_project}")
experiment_names = nq.list_experiments(project=neptune_project)
print(f"Found {len(experiment_names)} experiments: {experiment_names[:5]}...")

# Fetch a table of experiments with specific attributes
# Fetch runs as rows and attributes as columns
table_df: pd.DataFrame = nq.fetch_experiments_table(
    project=neptune_project,
    columns=['sys/name', 'sys/creation_time', 'params/*', 'metrics/loss']
)

print("\nFetched experiments table (first 5 rows):\n")
print(table_df.head())

# Fetch a specific metric series for an experiment
if not table_df.empty:
    first_experiment_name = table_df.iloc[0]['sys/name']
    print(f"\nFetching 'metrics/loss' for experiment: {first_experiment_name}")
    metric_series_df = nq.fetch_metrics(
        experiments=[first_experiment_name],
        attributes=['metrics/loss']
    )
    print("\nFetched metric series (first 5 rows):\n")
    print(metric_series_df.head())

view raw JSON →