Skip to content

Commit ab6bb99

Browse files
srsaggamDmitry-A
authored andcommitted
[AutoML] Handling label column names which have space and exception logging (dotnet#3624)
* fix case of label with space and exception logging * final handler * revert file
1 parent f17de37 commit ab6bb99

File tree

5 files changed

+106
-54
lines changed

5 files changed

+106
-54
lines changed

src/mlnet/CodeGenerator/CSharp/CodeGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public void GenerateOutput()
4343

4444
// Get Namespace
4545
var namespaceValue = Utils.Normalize(settings.OutputName);
46-
var labelType = columnInferenceResult.TextLoaderOptions.Columns.Where(t => t.Name == columnInferenceResult.ColumnInformation.LabelColumnName).First().DataKind;
46+
var labelType = columnInferenceResult.TextLoaderOptions.Columns.Where(t => t.Name == settings.LabelName).First().DataKind;
4747
Type labelTypeCsharp = Utils.GetCSharpType(labelType);
4848

4949
// Generate Model Project

src/mlnet/CodeGenerator/CodeGenerationHelper.cs

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,10 @@ public void GenerateCode()
5252
}
5353
columnInference = automlEngine.InferColumns(context, inputColumnInformation);
5454
}
55-
catch (Exception e)
55+
catch (Exception)
5656
{
5757
logger.Log(LogLevel.Error, $"{Strings.InferColumnError}");
58-
logger.Log(LogLevel.Error, e.Message);
59-
logger.Log(LogLevel.Trace, e.ToString());
60-
logger.Log(LogLevel.Error, Strings.Exiting);
61-
return;
58+
throw;
6259
}
6360

6461
var textLoaderOptions = columnInference.TextLoaderOptions;
@@ -172,14 +169,10 @@ public void GenerateCode()
172169

173170

174171
}
175-
catch (Exception e)
172+
catch (Exception)
176173
{
177174
logger.Log(LogLevel.Error, $"{Strings.ExplorePipelineException}:");
178-
logger.Log(LogLevel.Error, e.Message);
179-
logger.Log(LogLevel.Debug, e.ToString());
180-
logger.Log(LogLevel.Info, Strings.LookIntoLogFile);
181-
logger.Log(LogLevel.Error, Strings.Exiting);
182-
return;
175+
throw;
183176
}
184177

185178
var elapsedTime = watch.Elapsed.TotalSeconds;
@@ -214,28 +207,50 @@ public void GenerateCode()
214207
break;
215208
}
216209
}
217-
catch (Exception e)
210+
catch (Exception)
218211
{
219212
logger.Log(LogLevel.Info, Strings.ErrorBestPipeline);
220-
logger.Log(LogLevel.Info, e.Message);
221-
logger.Log(LogLevel.Trace, e.ToString());
222-
logger.Log(LogLevel.Info, Strings.LookIntoLogFile);
223-
logger.Log(LogLevel.Error, Strings.Exiting);
224-
return;
213+
throw;
225214
}
226215

227216
// Save the model
228217
var modelprojectDir = Path.Combine(settings.OutputPath.FullName, $"{settings.Name}.Model");
229218
var modelPath = new FileInfo(Path.Combine(modelprojectDir, "MLModel.zip"));
230-
Utils.SaveModel(bestModel, modelPath, context, trainData.Schema);
231-
Console.ForegroundColor = ConsoleColor.Yellow;
232-
logger.Log(LogLevel.Info, $"{Strings.SavingBestModel}: {modelPath}");
219+
220+
try
221+
{
222+
Utils.SaveModel(bestModel, modelPath, context, trainData.Schema);
223+
Console.ForegroundColor = ConsoleColor.Yellow;
224+
logger.Log(LogLevel.Info, $"{Strings.SavingBestModel}: {modelPath}");
225+
}
226+
catch (Exception)
227+
{
228+
logger.Log(LogLevel.Info, Strings.ErrorSavingModel);
229+
throw;
230+
}
231+
finally
232+
{
233+
Console.ResetColor();
234+
}
233235

234236
// Generate the Project
235-
GenerateProject(columnInference, bestPipeline, columnInformation.LabelColumnName, modelPath);
236-
logger.Log(LogLevel.Info, $"{Strings.GenerateModelConsumption}: { Path.Combine(settings.OutputPath.FullName, $"{settings.Name}.ConsoleApp")}");
237-
logger.Log(LogLevel.Info, $"{Strings.SeeLogFileForMoreInfo}: {settings.LogFilePath}");
238-
Console.ResetColor();
237+
try
238+
{
239+
GenerateProject(columnInference, bestPipeline, columnInformation.LabelColumnName, modelPath);
240+
Console.ForegroundColor = ConsoleColor.Yellow;
241+
logger.Log(LogLevel.Info, $"{Strings.GenerateModelConsumption}: { Path.Combine(settings.OutputPath.FullName, $"{settings.Name}.ConsoleApp")}");
242+
logger.Log(LogLevel.Info, $"{Strings.SeeLogFileForMoreInfo}: {settings.LogFilePath}");
243+
244+
}
245+
catch (Exception)
246+
{
247+
logger.Log(LogLevel.Info, Strings.ErrorGeneratingProject);
248+
throw;
249+
}
250+
finally
251+
{
252+
Console.ResetColor();
253+
}
239254
}
240255

241256
internal void GenerateProject(ColumnInferenceResults columnInference, Pipeline pipeline, string labelName, FileInfo modelPath)

src/mlnet/Program.cs

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System;
56
using System.CommandLine.Builder;
67
using System.CommandLine.Invocation;
78
using System.IO;
@@ -18,6 +19,7 @@ namespace Microsoft.ML.CLI
1819
{
1920
class Program
2021
{
22+
private static Logger logger = LogManager.GetCurrentClassLogger();
2123
public static void Main(string[] args)
2224
{
2325
var telemetry = new MlTelemetry();
@@ -26,39 +28,50 @@ public static void Main(string[] args)
2628
var handler = CommandHandler.Create<NewCommandSettings>(
2729
(options) =>
2830
{
29-
// Map the verbosity to internal levels
30-
var verbosity = Utils.GetVerbosity(options.Verbosity);
31-
32-
// Build the output path
33-
string outputBaseDir = string.Empty;
34-
if (options.Name == null)
31+
try
3532
{
36-
37-
options.Name = "Sample" + Utils.GetTaskKind(options.MlTask).ToString();
38-
outputBaseDir = Path.Combine(options.OutputPath.FullName, options.Name);
33+
// Map the verbosity to internal levels
34+
var verbosity = Utils.GetVerbosity(options.Verbosity);
35+
36+
// Build the output path
37+
string outputBaseDir = string.Empty;
38+
if (options.Name == null)
39+
{
40+
41+
options.Name = "Sample" + Utils.GetTaskKind(options.MlTask).ToString();
42+
outputBaseDir = Path.Combine(options.OutputPath.FullName, options.Name);
43+
}
44+
else
45+
{
46+
outputBaseDir = Path.Combine(options.OutputPath.FullName, options.Name);
47+
}
48+
49+
// Override the output path
50+
options.OutputPath = new DirectoryInfo(outputBaseDir);
51+
52+
// Instantiate the command
53+
var command = new NewCommand(options, telemetry);
54+
55+
// Override the Logger Configuration
56+
var logconsole = LogManager.Configuration.FindTargetByName("logconsole");
57+
var logfile = (FileTarget)LogManager.Configuration.FindTargetByName("logfile");
58+
var logFilePath = Path.Combine(Path.Combine(outputBaseDir, "logs"), "debug_log.txt");
59+
logfile.FileName = logFilePath;
60+
options.LogFilePath = logFilePath;
61+
var config = LogManager.Configuration;
62+
config.AddRule(verbosity, LogLevel.Fatal, logconsole);
63+
64+
// Execute the command
65+
command.Execute();
3966
}
40-
else
67+
catch (Exception e)
4168
{
42-
outputBaseDir = Path.Combine(options.OutputPath.FullName, options.Name);
69+
logger.Log(LogLevel.Error, e.Message);
70+
logger.Log(LogLevel.Debug, e.ToString());
71+
logger.Log(LogLevel.Info, Strings.LookIntoLogFile);
72+
logger.Log(LogLevel.Error, Strings.Exiting);
73+
return;
4374
}
44-
45-
// Override the output path
46-
options.OutputPath = new DirectoryInfo(outputBaseDir);
47-
48-
// Instantiate the command
49-
var command = new NewCommand(options, telemetry);
50-
51-
// Override the Logger Configuration
52-
var logconsole = LogManager.Configuration.FindTargetByName("logconsole");
53-
var logfile = (FileTarget)LogManager.Configuration.FindTargetByName("logfile");
54-
var logFilePath = Path.Combine(Path.Combine(outputBaseDir, "logs"), "debug_log.txt");
55-
logfile.FileName = logFilePath;
56-
options.LogFilePath = logFilePath;
57-
var config = LogManager.Configuration;
58-
config.AddRule(verbosity, LogLevel.Fatal, logconsole);
59-
60-
// Execute the command
61-
command.Execute();
6275
});
6376

6477
var parser = new CommandLineBuilder()

src/mlnet/Strings.resx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,10 @@
192192
<data name="SeeLogFileForMoreInfo" xml:space="preserve">
193193
<value>Check out log file for more information</value>
194194
</data>
195+
<data name="ErrorGeneratingProject" xml:space="preserve">
196+
<value>Exception occured while generating the project.</value>
197+
</data>
198+
<data name="ErrorSavingModel" xml:space="preserve">
199+
<value>Exception occured while saving the model</value>
200+
</data>
195201
</root>

src/mlnet/strings.Designer.cs

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)