Old Fetcher API
For how to the old version of the Neptune Fetcher API, see:
Migrate to Fetcher Alpha
To use the new Alpha API instead of the old one, you need to make some changes to your code.
The following sections show the code differences side by side.
- The new API version is in the
alpha
module and must be imported from there. - The project and API token are read from environment variables at initialization. To work with other projects, you can flexibly set the context either globally for the fetching session or individually for a fetching call.
- Instead of the
query
parameter and NQL queries, there are more convenient ways to construct regex search terms or more advanced filters. - To work with runs, you must import the run methods from the
runs
module.
Imports
from neptune_fetcher import (
ReadOnlyProject,
ReadOnlyRun,
)
import neptune_fetcher.alpha as npt
For more advanced filtering than name search, import filters from the filters
module:
from neptune_fetcher.alpha.filters import (
Attribute,
AttributeFilter,
Filter,
)
To work with runs instead of experiments, import the runs
module:
from neptune_fetcher.alpha import runs
Setting the project and API token
project = ReadOnlyProject(
project="team-alpha/project-x",
api_token="h0dHBzOi8aHR0cHM6...Y2MifQ==",
)
project = Context(
project="team-alpha/project-x",
api_token="h0dHBzOi8aHR0cHM6...Y2MifQ==",
)
context = npt.set_context(project)
With alpha
, the context is set automatically from environment variables. Set the context manually only if you aren't using environment variables or you want to use a different context temporarily.
Listing experiments or runs of the project
project = ReadOnlyProject()
for exp in project.list_experiments():
print(exp)
for run in project.list_runs():
print(run)
npt.list_experiments()
runs.list_runs()
With alpha
, you can also list attribute names present in the project.
Fetching metadata as table
With alpha
, you can use a filter object to set criteria for the experiments to search, or for the attributes to include as columns in the returned table.
project.fetch_experiments_df()
npt.fetch_experiments_table()
Match experiment names:
project.fetch_experiments_df(
names_regex=r"exp.*"
)
npt.fetch_experiments_table(r"exp.*")
Match run IDs:
project.fetch_experiments_df(
custom_id_regex=r"run.*"
)
runs.fetch_runs_table(r"run.*")
Filter by metadata value:
project.fetch_experiments_df(
query="(last(`acc`:floatSeries) > 0.88)"
)
npt.fetch_experiments_table(
experiments=Filter.gt("acc", 0.88)
)
Specify columns to include:
project.fetch_experiments_df(
columns_regex=r"acc"
)
npt.fetch_experiments_table(
attributes=r"acc"
)
Fetching metric values
With alpha
, you can fetch metric values from multiple experiments or runs at once.
# For each experiment or run
run = ReadOnlyRun(
ReadOnlyProject(...),
experiment_name="exp123",
)
run.prefetch(["loss"])
run["loss"].fetch_values()
npt.fetch_metrics(
experiments=r"exp.*",
attributes=r"loss",
)
Fetching a config or single value
To fetch the f1
attribute value from a particular experiment:
run = ReadOnlyRun(
ReadOnlyProject(...),
experiment_name="exp123",
)
value = run["f1"].fetch()
name_filter = Filter.eq("sys/name", r"exp123")
f1_filter = AttributeFilter(name_eq="f1")
f1_value = npt.fetch_experiments_table(
experiments=precise_experiment,
attributes=f1,
).iat[0, 0]
Note that it might be more convenient to omit the filter objects and pass regex patterns directly to the
experiments
andattributes
arguments. However, the above method shows how to use built-in methods to require the names to equal the specified strings exactly.