Skip to content

Commit 88cea06

Browse files
srsaggamDmitry-A
authored andcommitted
implement culture invariant strings (dotnet#3725)
1 parent 70b542f commit 88cea06

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

src/mlnet/CodeGenerator/CSharp/TrainerGeneratorBase.cs

+15-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System;
66
using System.Collections.Generic;
7+
using System.Globalization;
78
using System.Linq;
89
using System.Text;
910
using Microsoft.ML.AutoML;
@@ -12,7 +13,6 @@ namespace Microsoft.ML.CLI.CodeGenerator.CSharp
1213
{
1314
/// <summary>
1415
/// Supports generation of code for trainers (Binary,Multi,Regression)
15-
/// Ova is an exception though. Need to figure out how to tackle that.
1616
/// </summary>
1717
internal abstract class TrainerGeneratorBase : ITrainerGenerator
1818
{
@@ -61,17 +61,27 @@ private void Initialize(PipelineNode node)
6161
if (type == typeof(bool))
6262
{
6363
//True to true
64-
value = ((bool)kv.Value).ToString().ToLowerInvariant();
64+
value = ((bool)kv.Value).ToString(CultureInfo.InvariantCulture).ToLowerInvariant();
6565
}
6666
if (type == typeof(float))
6767
{
6868
//0.0 to 0.0f
69-
value = ((float)kv.Value).ToString() + "f";
69+
value = ((float)kv.Value).ToString(CultureInfo.InvariantCulture) + "f";
7070
}
7171

72-
if (type == typeof(int) || type == typeof(double) || type == typeof(long))
72+
if (type == typeof(int))
7373
{
74-
value = (kv.Value).ToString();
74+
value = ((int)kv.Value).ToString(CultureInfo.InvariantCulture);
75+
}
76+
77+
if (type == typeof(double))
78+
{
79+
value = ((double)kv.Value).ToString(CultureInfo.InvariantCulture);
80+
}
81+
82+
if (type == typeof(long))
83+
{
84+
value = ((long)kv.Value).ToString(CultureInfo.InvariantCulture);
7585
}
7686

7787
if (type == typeof(string))

test/mlnet.Tests/TrainerGeneratorTests.cs

+27-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,41 @@
11
using System.Collections.Generic;
2+
using System.Globalization;
3+
using System.Threading;
24
using Microsoft.ML;
35
using Microsoft.ML.AutoML;
46
using Microsoft.ML.CLI.CodeGenerator.CSharp;
57
using Microsoft.VisualStudio.TestTools.UnitTesting;
68

79
namespace mlnet.Tests
810
{
9-
/****************************
10-
* TODO : Add all trainer tests :
11-
* **************************/
1211
[TestClass]
1312
public class TrainerGeneratorTests
1413
{
14+
[TestMethod]
15+
public void CultureInvariantTest()
16+
{
17+
18+
var context = new MLContext();
19+
20+
var elementProperties = new Dictionary<string, object>()
21+
{
22+
{"LearningRate", 0.1f },
23+
{"NumberOfLeaves", 1 },
24+
};
25+
PipelineNode node = new PipelineNode("LightGbmBinary", PipelineNodeType.Trainer, default(string[]), default(string), elementProperties);
26+
Pipeline pipeline = new Pipeline(new PipelineNode[] { node });
27+
28+
//Set culture to deutsch.
29+
Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");
30+
31+
CodeGenerator codeGenerator = new CodeGenerator(pipeline, null, null);
32+
var actual = codeGenerator.GenerateTrainerAndUsings();
33+
string expectedTrainerString = "LightGbm(learningRate:0.1f,numberOfLeaves:1,labelColumnName:\"Label\",featureColumnName:\"Features\")";
34+
Assert.AreEqual(expectedTrainerString, actual.Item1);
35+
Assert.IsNull(actual.Item2);
36+
37+
}
38+
1539
[TestMethod]
1640
public void LightGbmBinaryBasicTest()
1741
{

0 commit comments

Comments
 (0)