Skip to content

Commit

Permalink
Fix handling of list values in GetAttrs method
Browse files Browse the repository at this point in the history
  • Loading branch information
NATSUME Hiroaki committed Feb 28, 2024
1 parent 219f676 commit 63b2bb0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
26 changes: 18 additions & 8 deletions Optuna/Storage/RDB/Storage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -608,21 +608,31 @@ public Dictionary<string, object> GetTrialSystemAttrs(int trialId)

private static void GetAttrs(Dictionary<string, object> attrs, SQLiteDataReader reader)
{
string value = (string)reader.GetValue(1);
string key = reader.GetString(0);
string value = reader.GetString(1);
if (!string.IsNullOrEmpty(value) && value.Contains("[") && value.Contains("]"))
{
try
//FIXME: This is a hack to handle the case where the value is a list of strings.
if (key.Contains("preference"))
{
string[] values = JsonConvert.DeserializeObject<string[]>(value);
attrs.Add(reader.GetString(0), values);
attrs.Add(key, value);
}
catch (JsonReaderException)
else
{
if (value.Contains("\""))
try
{
string[] values = JsonConvert.DeserializeObject<string[]>(value);
attrs.Add(reader.GetString(0), values);
}
catch (JsonReaderException)
{
value = value.Replace("\"", "");
if (value.Contains("\""))
{
value = value.Replace("\"", "");
}
attrs.Add(reader.GetString(0), new[] { value });
}
attrs.Add(reader.GetString(0), new[] { value });

}
}
else
Expand Down
2 changes: 1 addition & 1 deletion OptunaTests/Storage/RDB/StorageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void LoadHitlDBTest()
var storage = new SqliteStorage(_hitlFilePath);
Study.Study[] study = storage.GetAllStudies();
Assert.Single(study);
Assert.Equal(DateTime.MinValue, study[0].Trials[0].DatetimeComplete);
Assert.Equal(DateTime.MaxValue, study[0].Trials[10].DatetimeComplete);
}

public void Dispose()
Expand Down
Binary file modified OptunaTests/TestFile/hitl.db
Binary file not shown.

0 comments on commit 63b2bb0

Please sign in to comment.