Skip to content

Commit

Permalink
Merge pull request #223 from hrntsm/feature/move-dashboard-handler-to…
Browse files Browse the repository at this point in the history
…-optuna-csproj

Feature/move dashboard handler to optuna csproj
  • Loading branch information
hrntsm authored Jan 14, 2024
2 parents d8d0cfc + be53cb2 commit 42b482a
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 75 deletions.
72 changes: 72 additions & 0 deletions Optuna/Dashboard/Handler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using System.Diagnostics;
using System.IO;

namespace Optuna.Dashboard
{
public class Handler
{
private readonly string _dashboardPath;
private readonly string _storage;
private readonly string _host;
private readonly string _port;
private readonly string _artifactDir;

public Handler(string dashboardPath, string storagePath, string artifactDir = null, string host = "127.0.0.1", string port = "8080")
{
_dashboardPath = dashboardPath;
_storage = GetStorageArgument(storagePath);
_host = host;
_port = port;
_artifactDir = artifactDir ?? Path.GetDirectoryName(storagePath) + "/artifacts";
if (!Directory.Exists(_artifactDir))
{
Directory.CreateDirectory(_artifactDir);
}
}

private static string GetStorageArgument(string path)
{
switch (Path.GetExtension(path))
{
case ".sqlite3":
case ".db":
return @"sqlite:///" + $"\"{path}\"";
case ".log":
return $"\"{path}\"";
default:
throw new NotImplementedException();
}
}

public void Run()
{
CheckExistDashboardProcess();
string argument = $"{_storage} --host {_host} --port {_port} --artifact-dir {_artifactDir}";

var dashboard = new Process();
dashboard.StartInfo.FileName = _dashboardPath;
dashboard.StartInfo.Arguments = argument;
dashboard.StartInfo.UseShellExecute = false;
dashboard.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
dashboard.Start();

var browser = new Process();
browser.StartInfo.FileName = $@"http://{_host}:{_port}/";
browser.StartInfo.UseShellExecute = true;
browser.Start();
}

private static void CheckExistDashboardProcess()
{
Process[] dashboardProcess = Process.GetProcessesByName("optuna-dashboard");
if (dashboardProcess.Length > 0)
{
foreach (Process p in dashboardProcess)
{
p.Kill();
}
}
}
}
}
20 changes: 16 additions & 4 deletions Optuna/Storage/Journal/Storage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@ public class JournalStorage : BaseStorage
private int _nextStudyId = 0;
private int _trialId = 0;

public JournalStorage(string path)
public JournalStorage(string path, bool makeFile = false)
{
if (File.Exists(path) == false)
{
throw new FileNotFoundException($"File not found: {path}");
if (makeFile == false)
{
throw new FileNotFoundException($"File not found: {path}");
}
else
{
File.Create(path).Close();
}
}

var logs = new List<string>();
Expand Down Expand Up @@ -152,8 +159,13 @@ public override void CheckTrialIsUpdatable(int trialId, TrialState trialState)

public override int CreateNewStudy(StudyDirection[] studyDirections, string studyName = "")
{
_studies.Add(new Study.Study(this, _nextStudyId, studyName, studyDirections));
return _nextStudyId++;
string[] studyNames = _studies.Select(s => s.StudyName).ToArray();
if (!studyNames.Contains(studyName))
{
_studies.Add(new Study.Study(this, _nextStudyId, studyName, studyDirections));
_nextStudyId++;
}
return _nextStudyId;
}

public override int CreateNewTrial(int studyId, Trial.Trial templateTrial = null)
Expand Down
11 changes: 10 additions & 1 deletion OptunaTests/Storage/Journal/JournalStorageTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.IO;

using Microsoft.VisualStudio.TestTools.UnitTesting;

Expand All @@ -7,6 +7,15 @@ namespace Optuna.Storage.Journal.Tests
[TestClass()]
public class JournalStorageTests
{
[TestMethod()]
public void MakeFileIfNotExistTest()
{
string path = @"TestFile/created.log";
_ = new JournalStorage(path, true);
Assert.IsTrue(File.Exists(path));
File.Delete(path);
}

[TestMethod()]
public void JournalStorageTest()
{
Expand Down
35 changes: 0 additions & 35 deletions Tunny/Handler/DashboardHandler.cs

This file was deleted.

2 changes: 1 addition & 1 deletion Tunny/Settings/Storage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Storage
public string Path { get; set; } = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/fish.log";
public StorageType Type { get; set; } = StorageType.Journal;

public string GetArtifactBackendPath()
private string GetArtifactBackendPath()
{
return System.IO.Path.GetDirectoryName(Path) + "/artifacts";
}
Expand Down
2 changes: 1 addition & 1 deletion Tunny/Storage/JournalStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class JournalStorage : PythonInit, IStorage

public StudySummary[] GetStudySummaries(string storagePath)
{
var storage = new Optuna.Storage.Journal.JournalStorage(storagePath);
var storage = new Optuna.Storage.Journal.JournalStorage(storagePath, true);
StudySummary[] studySummaries = Study.GetAllStudySummaries(storage);

var oldFormatVersion = new Version("0.9.1");
Expand Down
28 changes: 2 additions & 26 deletions Tunny/UI/LoadingInstruction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using Grasshopper.GUI.Canvas;
using Grasshopper.Kernel;

using Tunny.Handler;
using Tunny.Resources;
using Tunny.Settings;
using Tunny.Util;
Expand Down Expand Up @@ -118,31 +117,8 @@ private static void RunOptunaDashboard(string dashboardPath)
};
if (ofd.ShowDialog() == DialogResult.OK)
{
storagePath = GetStorageArgument(ofd.FileName);
string arguments = storagePath;
string backendPath = Path.GetDirectoryName(ofd.FileName) + "/artifacts";
if (Directory.Exists(backendPath))
{
arguments += " --artifact-dir " + backendPath;
}

DashboardHandler.RunDashboardProcess(dashboardPath, arguments);
}
}

private static string GetStorageArgument(string path)
{
switch (Path.GetExtension(path))
{
case null:
return string.Empty;
case ".sqlite3":
case ".db":
return @"sqlite:///" + $"\"{path}\"";
case ".log":
return $"\"{path}\"";
default:
throw new NotImplementedException();
var dashboard = new Optuna.Dashboard.Handler(dashboardPath, ofd.FileName);
dashboard.Run();
}
}

Expand Down
11 changes: 4 additions & 7 deletions Tunny/UI/OptimizeWindowTab/VisualizeTab.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,11 @@ private void DashboardButton_Click(object sender, EventArgs e)
ResultFileNotExistErrorMessage();
return;
}
string argument = _settings.Storage.GetOptunaStorageCommandLinePathByExtension();
string backendPath = _settings.Storage.GetArtifactBackendPath();
if (Directory.Exists(backendPath))
{
argument += " --artifact-dir " + backendPath;
}
string dashboardPath = Path.Combine(PythonInstaller.GetEmbeddedPythonPath(), "Scripts", "optuna-dashboard.exe");
string storagePath = _settings.Storage.Path;

DashboardHandler.RunDashboardProcess(PythonInstaller.GetEmbeddedPythonPath() + @"\Scripts\optuna-dashboard.exe", argument);
var dashboard = new Optuna.Dashboard.Handler(dashboardPath, storagePath);
dashboard.Run();
}

private void VisualizeTargetStudy_Changed(object sender, EventArgs e)
Expand Down

0 comments on commit 42b482a

Please sign in to comment.