Skip to content

Commit

Permalink
Merge pull request #232 from hrntsm/feature/logger
Browse files Browse the repository at this point in the history
Feature logger
  • Loading branch information
hrntsm authored Jan 31, 2024
2 parents c71e631 + a28cd8b commit b16db55
Show file tree
Hide file tree
Showing 13 changed files with 190 additions and 53 deletions.
3 changes: 3 additions & 0 deletions Tunny/Component/Optimizer/FishingComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
using Grasshopper.Kernel.Parameters;
using Grasshopper.Kernel.Special;

using Serilog;

using Tunny.Component.Params;
using Tunny.Resources;
using Tunny.Type;
Expand Down Expand Up @@ -114,6 +116,7 @@ private void CheckArtifactsInput(IEnumerable<Guid> inputGuids)

public void GhInOutInstantiate()
{
Log.Information("Instantiate GrasshopperInOut");
GhInOut = new GrasshopperInOut(this);
}

Expand Down
13 changes: 13 additions & 0 deletions Tunny/Enum/SamplerType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Tunny.Enum
{
public enum SamplerType
{
TPE = 0,
BoTorch = 1,
NSGAII = 2,
NSGAIII = 3,
CmaEs = 4,
QMC = 5,
Random = 6,
}
}
9 changes: 8 additions & 1 deletion Tunny/Handler/PythonInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.IO;
using System.IO.Compression;

using Serilog;

using Tunny.Util;

namespace Tunny.Handler
Expand All @@ -14,6 +16,7 @@ public static void Run(object sender, DoWorkEventArgs e)
{
var worker = sender as BackgroundWorker;
worker?.ReportProgress(0, "Unzip library...");
Log.Information("Unzip library...");
string[] packageList = UnzipLibraries();
InstallPackages(worker, packageList);

Expand All @@ -23,6 +26,7 @@ public static void Run(object sender, DoWorkEventArgs e)
private static string[] UnzipLibraries()
{
string envPath = TunnyVariables.TunnyEnvPath;
Log.Information("Unzip Python libraries: " + envPath);
if (Directory.Exists(envPath + "/python"))
{
Directory.Delete(envPath + "/python", true);
Expand All @@ -44,7 +48,9 @@ private static void InstallPackages(BackgroundWorker worker, string[] packageLis
{
double progress = (double)i / num * 100d;
string packageName = Path.GetFileName(packageList[i]).Split('-')[0];
worker.ReportProgress((int)progress, "Now installing " + packageName + "...");
string state = "Installing " + packageName + "...";
worker.ReportProgress((int)progress, state);
Log.Information(state);
var startInfo = new ProcessStartInfo
{
FileName = TunnyVariables.TunnyEnvPath + "/python/python.exe",
Expand All @@ -60,6 +66,7 @@ private static void InstallPackages(BackgroundWorker worker, string[] packageLis
process.WaitForExit();
}
}
Log.Information("Finish to install Python");
}

internal static string GetEmbeddedPythonPath()
Expand Down
2 changes: 1 addition & 1 deletion Tunny/Settings/Optimize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class Optimize
public int NumberOfTrials { get; set; } = 100;
public bool ContinueStudy { get; set; }
public bool CopyStudy { get; set; }
public int SelectSampler { get; set; }
public SamplerType SelectSampler { get; set; }
public double Timeout { get; set; }
public GcAfterTrial GcAfterTrial { get; set; } = GcAfterTrial.HasGeometry;
public bool ShowRealtimeResult { get; set; }
Expand Down
123 changes: 81 additions & 42 deletions Tunny/Solver/Algorithm.cs

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions Tunny/Solver/Optuna.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

using Python.Runtime;

using Serilog;

using Tunny.Enum;
using Tunny.Handler;
using Tunny.Input;
Expand Down Expand Up @@ -95,6 +97,7 @@ private static void ShowEndMessages(EndState endState)

private static void ShowErrorMessages(Exception e)
{
Log.Error("Optimization end with exception: {0}", e.Message);
TunnyMessageBox.Show(
"Tunny runtime error:\n" +
"Please send below message (& gh file if possible) to Tunny support.\n" +
Expand All @@ -119,6 +122,7 @@ public ModelResult[] GetModelResult(int[] resultNum, string studyName, Backgroun
}
catch (Exception e)
{
Log.Error(e.Message);
TunnyMessageBox.Show(e.Message, "Tunny", MessageBoxButtons.OK, MessageBoxIcon.Information);
return modelResult.ToArray();
}
Expand Down Expand Up @@ -163,6 +167,7 @@ private static void UseModelNumber(IReadOnlyList<int> resultNum, ICollection<Mod
}
catch (Exception e)
{
Log.Error(e.Message);
TunnyMessageBox.Show("Error\n\n" + e.Message, "Tunny", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
worker.ReportProgress(i * 100 / resultNum.Count);
Expand Down
17 changes: 13 additions & 4 deletions Tunny/Solver/Visualize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using Python.Runtime;

using Serilog;

using Tunny.Enum;
using Tunny.PostProcess;
using Tunny.Settings;
Expand Down Expand Up @@ -30,6 +32,7 @@ private static dynamic LoadStudy(dynamic optuna, dynamic storage, string studyNa
}
catch (Exception e)
{
Log.Error(e.Message);
TunnyMessageBox.Show(e.Message, "Tunny", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
Expand All @@ -53,9 +56,12 @@ public void Plot(PlotSettings pSettings)
dynamic fig = CreateFigure(study, pSettings);
FigureActions(fig, pSettings);
}
catch (Exception)
catch (Exception e)
{
TunnyMessageBox.Show("This visualization type is not supported in this study case.", "Tunny", MessageBoxButtons.OK, MessageBoxIcon.Error);
Log.Error(e.Message);
string message = "This visualization type is not supported in this study case.";
Log.Error(message);
TunnyMessageBox.Show(message, "Tunny", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
PythonEngine.Shutdown();
Expand Down Expand Up @@ -309,9 +315,12 @@ private dynamic CreateClusterFigure(dynamic study, PlotSettings pSettings)
fig = TruncateParetoFrontPlotHover(fig, study);
return ClusteringParetoFrontPlot(fig, study, pSettings.ClusterCount);
}
catch (Exception)
catch (Exception e)
{
TunnyMessageBox.Show("Clustering Error", "Tunny", MessageBoxButtons.OK, MessageBoxIcon.Error);
Log.Error(e.Message);
string message = "Clustering Error";
Log.Error(message);
TunnyMessageBox.Show(message, "Tunny", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return null;
}
Expand Down
5 changes: 4 additions & 1 deletion Tunny/Tunny.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Optuna\Optuna.csproj" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="Grasshopper" Version="7.13.21348.13001" IncludeAssets="compile;build" />
<PackageReference Include="Microsoft.Data.Sqlite.Core" Version="7.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="pythonnet" Version="3.0.1" />
<PackageReference Include="Stub.System.Data.SQLite.Core.NetFramework" Version="1.0.116" />
<PackageReference Include="System.Resources.Extensions" Version="6.0.0" />
<ProjectReference Include="..\Optuna\Optuna.csproj" />
<Reference Include="GalapagosComponents">
<HintPath>Lib\GalapagosComponents.dll</HintPath>
<Private>false</Private>
Expand Down
35 changes: 35 additions & 0 deletions Tunny/UI/LoadingInstruction.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Windows.Forms;

using Grasshopper.GUI;
using Grasshopper.GUI.Canvas;
using Grasshopper.Kernel;

using Python.Runtime;

using Serilog;

using Tunny.Resources;
using Tunny.Settings;
using Tunny.Util;
Expand All @@ -23,9 +28,39 @@ public override GH_LoadingInstruction PriorityLoad()
Grasshopper.Instances.ComponentServer.AddCategoryIcon("Tunny", Resource.TunnyIcon);
Grasshopper.Instances.ComponentServer.AddCategorySymbolName("Tunny", 'T');
Grasshopper.Instances.CanvasCreated += RegisterTunnyMenuItems;
InitializeLogger();
return GH_LoadingInstruction.Proceed;
}

private static void InitializeLogger()
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.Console(formatProvider: CultureInfo.InvariantCulture)
.WriteTo.File(path: TunnyVariables.LogPath + "/log_.txt", rollingInterval: RollingInterval.Day, formatProvider: CultureInfo.InvariantCulture)
.CreateLogger();
Log.Information("Tunny is loaded.");
CheckAndDeleteOldLogFiles();
}

private static void CheckAndDeleteOldLogFiles()
{
string logDirectory = TunnyVariables.LogPath;
string logFilePattern = "*.txt";

DateTime threshold = DateTime.Now.AddDays(-7);

var directory = new DirectoryInfo(logDirectory);
FileInfo[] logFiles = directory.GetFiles(logFilePattern);
foreach (FileInfo file in logFiles)
{
if (file.LastWriteTime < threshold)
{
file.Delete();
}
}
}

void RegisterTunnyMenuItems(GH_Canvas canvas)
{
Grasshopper.Instances.CanvasCreated -= RegisterTunnyMenuItems;
Expand Down
17 changes: 15 additions & 2 deletions Tunny/UI/OptimizationWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

using Grasshopper.GUI;

using Serilog;

using Tunny.Component.Optimizer;
using Tunny.Enum;
using Tunny.Handler;
Expand All @@ -21,6 +23,7 @@ public partial class OptimizationWindow : Form

public OptimizationWindow(FishingComponent component)
{
Log.Information("OptimizationWindow is open");
InitializeComponent();

_component = component;
Expand All @@ -41,6 +44,7 @@ private void RunPythonInstaller()
PythonInstaller.ComponentFolderPath = _component.GhInOut.ComponentFolder;
if (_settings.CheckPythonLibraries)
{
Log.Information("Run Python installer");
var installer = new PythonInstallDialog()
{
StartPosition = FormStartPosition.CenterScreen
Expand All @@ -49,6 +53,10 @@ private void RunPythonInstaller()
_settings.CheckPythonLibraries = false;
checkPythonLibrariesCheckBox.Checked = false;
}
else
{
Log.Information("Skip Python installer");
}
}

private void SetOptimizeBackgroundWorker()
Expand Down Expand Up @@ -80,10 +88,12 @@ private void LoadSettingJson()
string settingsPath = TunnyVariables.OptimizeSettingsPath;
if (File.Exists(settingsPath))
{
Log.Information("Load existing setting.json");
_settings = TunnySettings.Deserialize(File.ReadAllText(settingsPath));
}
else
{
Log.Information("There is no configuration file. Create a new one.");
_settings = new TunnySettings
{
Storage = new Settings.Storage
Expand Down Expand Up @@ -127,9 +137,11 @@ private void FormClosingXButton(object sender, FormClosingEventArgs e)

private void SetUIValues()
{
Log.Information("Set UI values");
HumanInTheLoopType type = _component.GhInOut.Objectives.HumanInTheLoopType;
if (type != HumanInTheLoopType.None)
{
Log.Information("Set Tunny Human-in-the-loop mode");
Text = "Tunny (Human in the Loop mode)";
samplerComboBox.SelectedIndex = 1; // GP
samplerComboBox.Enabled = false;
Expand All @@ -140,9 +152,10 @@ private void SetUIValues()
}
else
{
Log.Information("Set Tunny normal optimization mode");
Text = "Tunny";
samplerComboBox.Enabled = true;
samplerComboBox.SelectedIndex = _settings.Optimize.SelectSampler;
samplerComboBox.SelectedIndex = (int)_settings.Optimize.SelectSampler;
nTrialText.Text = "Number of trials";
nTrialNumUpDown.Value = _settings.Optimize.NumberOfTrials;
timeoutNumUpDown.Enabled = true;
Expand All @@ -167,7 +180,7 @@ private void SetUIValues()
}
private void GetUIValues()
{
_settings.Optimize.SelectSampler = samplerComboBox.SelectedIndex;
_settings.Optimize.SelectSampler = (SamplerType)samplerComboBox.SelectedIndex;
_settings.Optimize.NumberOfTrials = (int)nTrialNumUpDown.Value;
_settings.Optimize.Timeout = (double)timeoutNumUpDown.Value;
_settings.Optimize.ContinueStudy = continueStudyCheckBox.Checked;
Expand Down
10 changes: 8 additions & 2 deletions Tunny/UI/OptimizeWindowTab/OptimizeTab.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

using Optuna.Study;

using Serilog;

using Tunny.Enum;
using Tunny.Handler;
using Tunny.Storage;
Expand Down Expand Up @@ -153,9 +155,12 @@ private void UpdateStudyComboBox()
{
UpdateStudyComboBox(_settings.Storage.Path);
}
catch (Exception)
catch (Exception e)
{
TunnyMessageBox.Show("The storage file loading error.\nPlease check the storage path or use new storage file.", "Error");
Log.Error(e.Message);
string message = "The storage file loading error.Please check the storage path or use new storage file.";
Log.Error(message);
TunnyMessageBox.Show(message, "Error");
}
}

Expand All @@ -166,6 +171,7 @@ private void UpdateStudyComboBox(string storagePath)
outputTargetStudyComboBox.Items.Clear();
cmaEsWarmStartComboBox.Items.Clear();

Log.Information("Get study summaries from storage file: {0}", storagePath);
_summaries = new StorageHandler().GetStudySummaries(storagePath);

if (_summaries.Length > 0)
Expand Down
3 changes: 3 additions & 0 deletions Tunny/UI/OptimizeWindowTab/UITunnyMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

using Grasshopper.GUI;

using Serilog;

namespace Tunny.UI
{
public partial class OptimizationWindow
Expand Down Expand Up @@ -49,6 +51,7 @@ private bool NameAlreadyExistMessage(GH_DocumentEditor ghCanvas)
// OutputTab ==========================
private static bool IncorrectParseModeNumberInputMessage()
{
Log.Warning("Incorrect parse mode number input.");
TunnyMessageBox.Show(
"The model number format of the input is incorrect. \nPlease use a comma separator as follows.\n\"1,2,3\"",
"Tunny");
Expand Down
1 change: 1 addition & 0 deletions Tunny/Util/TunnyVariables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Tunny.Util
public static class TunnyVariables
{
public static string TunnyEnvPath { get; } = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".tunny_env");
public static string LogPath { get; } = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".tunny_env", "logs");
public static string OptimizeSettingsPath { get; } = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".tunny_env", "settings.json");
}
}

0 comments on commit b16db55

Please sign in to comment.