Skip to content

Commit 345e6b9

Browse files
author
Shahab Moradi
committed
Moved ini utils to separate file.
1 parent 1e0e3fa commit 345e6b9

File tree

3 files changed

+55
-44
lines changed

3 files changed

+55
-44
lines changed

src/Microsoft.ML.FastTree/FastTree.cs

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2985,7 +2985,7 @@ void ICanSaveInTextFormat.SaveAsText(TextWriter writer, RoleMappedSchema schema)
29852985
void ICanSaveInIniFormat.SaveAsIni(TextWriter writer, RoleMappedSchema schema, ICalibrator calibrator)
29862986
{
29872987
Host.CheckValue(writer, nameof(writer));
2988-
var ensembleIni = FastTreeIniFormatUtils.TreeEnsembleToIni(Host, TrainedEnsemble, schema, calibrator,
2988+
var ensembleIni = FastTreeIniFileUtils.TreeEnsembleToIni(Host, TrainedEnsemble, schema, calibrator,
29892989
InnerArgs, appendFeatureGain: true, includeZeroGainFeatures: false);
29902990
writer.WriteLine(ensembleIni);
29912991
}
@@ -3373,46 +3373,4 @@ public TreeNode(Dictionary<string, object> keyValues)
33733373
public Dictionary<string, object> KeyValues { get; }
33743374
}
33753375
}
3376-
internal static class FastTreeIniFormatUtils
3377-
{
3378-
public static string TreeEnsembleToIni(
3379-
IHost host, TreeEnsemble ensemble, RoleMappedSchema schema, ICalibrator calibrator,
3380-
string trainingParams, bool appendFeatureGain, bool includeZeroGainFeatures)
3381-
{
3382-
host.CheckValue(ensemble, nameof(ensemble));
3383-
host.CheckValue(schema, nameof(schema));
3384-
3385-
string ensembleIni = ensemble.ToTreeEnsembleIni(new FeaturesToContentMap(schema),
3386-
trainingParams, appendFeatureGain, includeZeroGainFeatures);
3387-
ensembleIni = AddCalibrationToIni(host, ensembleIni, calibrator);
3388-
return ensembleIni;
3389-
}
3390-
3391-
/// <summary>
3392-
/// Get the calibration summary in INI format
3393-
/// </summary>
3394-
private static string AddCalibrationToIni(IHost host, string ini, ICalibrator calibrator)
3395-
{
3396-
host.AssertValue(ini);
3397-
host.AssertValueOrNull(calibrator);
3398-
3399-
if (calibrator == null)
3400-
return ini;
3401-
3402-
if (calibrator is PlattCalibrator)
3403-
{
3404-
string calibratorEvaluatorIni = IniFileUtils.GetCalibratorEvaluatorIni(ini, calibrator as PlattCalibrator);
3405-
return IniFileUtils.AddEvaluator(ini, calibratorEvaluatorIni);
3406-
}
3407-
else
3408-
{
3409-
StringBuilder newSection = new StringBuilder();
3410-
newSection.AppendLine();
3411-
newSection.AppendLine();
3412-
newSection.AppendLine("[TLCCalibration]");
3413-
newSection.AppendLine("Type=" + calibrator.GetType().Name);
3414-
return ini + newSection;
3415-
}
3416-
}
3417-
}
34183376
}

src/Microsoft.ML.FastTree/GamTrainer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1060,7 +1060,7 @@ void ICanSaveInIniFormat.SaveAsIni(TextWriter writer, RoleMappedSchema schema, I
10601060
leafValues: new[] { Intercept, Intercept });
10611061
ensemble.AddTree(interceptTree);
10621062

1063-
var ini = FastTreeIniFormatUtils.TreeEnsembleToIni(
1063+
var ini = FastTreeIniFileUtils.TreeEnsembleToIni(
10641064
Host, ensemble, schema, calibrator, string.Empty, false, false);
10651065

10661066
// Remove the SplitGain values which are all 0.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Microsoft.ML.Calibrator;
5+
using Microsoft.ML.Data;
6+
using Microsoft.ML.Internal.Calibration;
7+
using Microsoft.ML.Internal.Utilities;
8+
9+
namespace Microsoft.ML.Trainers.FastTree.Internal
10+
{
11+
internal static class FastTreeIniFileUtils
12+
{
13+
public static string TreeEnsembleToIni(
14+
IHost host, TreeEnsemble ensemble, RoleMappedSchema schema, ICalibrator calibrator,
15+
string trainingParams, bool appendFeatureGain, bool includeZeroGainFeatures)
16+
{
17+
host.CheckValue(ensemble, nameof(ensemble));
18+
host.CheckValue(schema, nameof(schema));
19+
20+
string ensembleIni = ensemble.ToTreeEnsembleIni(new FeaturesToContentMap(schema),
21+
trainingParams, appendFeatureGain, includeZeroGainFeatures);
22+
ensembleIni = AddCalibrationToIni(host, ensembleIni, calibrator);
23+
return ensembleIni;
24+
}
25+
26+
/// <summary>
27+
/// Get the calibration summary in INI format
28+
/// </summary>
29+
private static string AddCalibrationToIni(IHost host, string ini, ICalibrator calibrator)
30+
{
31+
host.AssertValue(ini);
32+
host.AssertValueOrNull(calibrator);
33+
34+
if (calibrator == null)
35+
return ini;
36+
37+
if (calibrator is PlattCalibrator)
38+
{
39+
string calibratorEvaluatorIni = IniFileUtils.GetCalibratorEvaluatorIni(ini, calibrator as PlattCalibrator);
40+
return IniFileUtils.AddEvaluator(ini, calibratorEvaluatorIni);
41+
}
42+
else
43+
{
44+
StringBuilder newSection = new StringBuilder();
45+
newSection.AppendLine();
46+
newSection.AppendLine();
47+
newSection.AppendLine("[TLCCalibration]");
48+
newSection.AppendLine("Type=" + calibrator.GetType().Name);
49+
return ini + newSection;
50+
}
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)