Skip to content

Commit

Permalink
implement culture invariant strings (dotnet#3725)
Browse files Browse the repository at this point in the history
  • Loading branch information
srsaggam authored and Dmitry-A committed Aug 22, 2019
1 parent 70b542f commit 88cea06
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
20 changes: 15 additions & 5 deletions src/mlnet/CodeGenerator/CSharp/TrainerGeneratorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using Microsoft.ML.AutoML;
Expand All @@ -12,7 +13,6 @@ namespace Microsoft.ML.CLI.CodeGenerator.CSharp
{
/// <summary>
/// Supports generation of code for trainers (Binary,Multi,Regression)
/// Ova is an exception though. Need to figure out how to tackle that.
/// </summary>
internal abstract class TrainerGeneratorBase : ITrainerGenerator
{
Expand Down Expand Up @@ -61,17 +61,27 @@ private void Initialize(PipelineNode node)
if (type == typeof(bool))
{
//True to true
value = ((bool)kv.Value).ToString().ToLowerInvariant();
value = ((bool)kv.Value).ToString(CultureInfo.InvariantCulture).ToLowerInvariant();
}
if (type == typeof(float))
{
//0.0 to 0.0f
value = ((float)kv.Value).ToString() + "f";
value = ((float)kv.Value).ToString(CultureInfo.InvariantCulture) + "f";
}

if (type == typeof(int) || type == typeof(double) || type == typeof(long))
if (type == typeof(int))
{
value = (kv.Value).ToString();
value = ((int)kv.Value).ToString(CultureInfo.InvariantCulture);
}

if (type == typeof(double))
{
value = ((double)kv.Value).ToString(CultureInfo.InvariantCulture);
}

if (type == typeof(long))
{
value = ((long)kv.Value).ToString(CultureInfo.InvariantCulture);
}

if (type == typeof(string))
Expand Down
30 changes: 27 additions & 3 deletions test/mlnet.Tests/TrainerGeneratorTests.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
using Microsoft.ML;
using Microsoft.ML.AutoML;
using Microsoft.ML.CLI.CodeGenerator.CSharp;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace mlnet.Tests
{
/****************************
* TODO : Add all trainer tests :
* **************************/
[TestClass]
public class TrainerGeneratorTests
{
[TestMethod]
public void CultureInvariantTest()
{

var context = new MLContext();

var elementProperties = new Dictionary<string, object>()
{
{"LearningRate", 0.1f },
{"NumberOfLeaves", 1 },
};
PipelineNode node = new PipelineNode("LightGbmBinary", PipelineNodeType.Trainer, default(string[]), default(string), elementProperties);
Pipeline pipeline = new Pipeline(new PipelineNode[] { node });

//Set culture to deutsch.
Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");

CodeGenerator codeGenerator = new CodeGenerator(pipeline, null, null);
var actual = codeGenerator.GenerateTrainerAndUsings();
string expectedTrainerString = "LightGbm(learningRate:0.1f,numberOfLeaves:1,labelColumnName:\"Label\",featureColumnName:\"Features\")";
Assert.AreEqual(expectedTrainerString, actual.Item1);
Assert.IsNull(actual.Item2);

}

[TestMethod]
public void LightGbmBinaryBasicTest()
{
Expand Down

0 comments on commit 88cea06

Please sign in to comment.