Skip to content
This repository was archived by the owner on May 8, 2025. It is now read-only.

Add tests for StudyWrapper constructor argument handling #417

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tunny_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ jobs:
- name: Install dependencies
run: |
dotnet restore
pip install optuna find-libpython
pip install optuna optuna_dashboard find-libpython botorch

- name: Set Python DLL path and PYTHONHOME
run: |
Expand Down
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"HITL",
"hovertemplate",
"Hyperband",
"isinstance",
"libpython",
"rhinocode",
"showlegend",
Expand Down
5 changes: 3 additions & 2 deletions Optuna/Study/StudyWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ public StudyWrapper(dynamic study)
PyInstance = study;

dynamic optuna = Py.Import("optuna");
dynamic dashboard = Py.Import("optuna_dashboard");
dynamic py = Py.Import("builtins");
if (!py.isinstance(study, optuna.study.Study))
if (!py.isinstance(study, optuna.study.Study) && !py.isinstance(study, dashboard.preferential.PreferentialStudy))
{
throw new ArgumentException("study must be an instance of optuna.study.Study.");
throw new ArgumentException("study must be an instance of optuna.study.Study or optuna_dashboard.preferential.PreferentialStudy.");
}
}

Expand Down
33 changes: 33 additions & 0 deletions OptunaTests/Dashboard/HumanInTheLoop/PreferentialTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Optuna.Sampler.Dashboard;
using Optuna.Study;

using OptunaTests;

using Python.Runtime;

using Xunit;

namespace Optuna.Dashboard.HumanInTheLoop.Tests
{
public class PreferentialTests : IClassFixture<TestFixture>
{
private readonly TestFixture _fixture;

public PreferentialTests(TestFixture fixture)
{
_fixture = fixture;
}

[Fact]
public void CreateStudyTest()
{
using (Py.GIL())
{
dynamic sampler = new PreferentialGpSampler().ToPython();
var preferential = new Preferential("tmpPath", "storagePath", sampler);
StudyWrapper study = preferential.CreateStudy(10, null, null, "objectiveName");
Assert.NotNull(study);
}
}
}
}
15 changes: 14 additions & 1 deletion OptunaTests/Study/StudyWrapperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,20 @@ public StudyWrapperTests(TestFixture fixture)
}

[Fact]
public void ConstructorArgTypeTest()
public void ConstructorArgStudyTest()
{
using (Py.GIL())
{
dynamic optuna = Py.Import("optuna");
dynamic study = optuna.create_study();
var studyWrapper = new StudyWrapper(study);
Assert.Equal(study, studyWrapper.PyInstance);
Assert.Equal(0, studyWrapper.Id);
}
}

[Fact]
public void ConstructorArgPyIntTest()
{
using (Py.GIL())
{
Expand Down
Loading