Skip to content

Commit

Permalink
[AutoML] Handling label column names which have space and exception l…
Browse files Browse the repository at this point in the history
…ogging (dotnet#3624)

* fix case of label with space and exception logging

* final handler

* revert file
  • Loading branch information
srsaggam authored and Dmitry-A committed Aug 22, 2019
1 parent f17de37 commit ab6bb99
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 54 deletions.
2 changes: 1 addition & 1 deletion src/mlnet/CodeGenerator/CSharp/CodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void GenerateOutput()

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

// Generate Model Project
Expand Down
63 changes: 39 additions & 24 deletions src/mlnet/CodeGenerator/CodeGenerationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,10 @@ public void GenerateCode()
}
columnInference = automlEngine.InferColumns(context, inputColumnInformation);
}
catch (Exception e)
catch (Exception)
{
logger.Log(LogLevel.Error, $"{Strings.InferColumnError}");
logger.Log(LogLevel.Error, e.Message);
logger.Log(LogLevel.Trace, e.ToString());
logger.Log(LogLevel.Error, Strings.Exiting);
return;
throw;
}

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


}
catch (Exception e)
catch (Exception)
{
logger.Log(LogLevel.Error, $"{Strings.ExplorePipelineException}:");
logger.Log(LogLevel.Error, e.Message);
logger.Log(LogLevel.Debug, e.ToString());
logger.Log(LogLevel.Info, Strings.LookIntoLogFile);
logger.Log(LogLevel.Error, Strings.Exiting);
return;
throw;
}

var elapsedTime = watch.Elapsed.TotalSeconds;
Expand Down Expand Up @@ -214,28 +207,50 @@ public void GenerateCode()
break;
}
}
catch (Exception e)
catch (Exception)
{
logger.Log(LogLevel.Info, Strings.ErrorBestPipeline);
logger.Log(LogLevel.Info, e.Message);
logger.Log(LogLevel.Trace, e.ToString());
logger.Log(LogLevel.Info, Strings.LookIntoLogFile);
logger.Log(LogLevel.Error, Strings.Exiting);
return;
throw;
}

// Save the model
var modelprojectDir = Path.Combine(settings.OutputPath.FullName, $"{settings.Name}.Model");
var modelPath = new FileInfo(Path.Combine(modelprojectDir, "MLModel.zip"));
Utils.SaveModel(bestModel, modelPath, context, trainData.Schema);
Console.ForegroundColor = ConsoleColor.Yellow;
logger.Log(LogLevel.Info, $"{Strings.SavingBestModel}: {modelPath}");

try
{
Utils.SaveModel(bestModel, modelPath, context, trainData.Schema);
Console.ForegroundColor = ConsoleColor.Yellow;
logger.Log(LogLevel.Info, $"{Strings.SavingBestModel}: {modelPath}");
}
catch (Exception)
{
logger.Log(LogLevel.Info, Strings.ErrorSavingModel);
throw;
}
finally
{
Console.ResetColor();
}

// Generate the Project
GenerateProject(columnInference, bestPipeline, columnInformation.LabelColumnName, modelPath);
logger.Log(LogLevel.Info, $"{Strings.GenerateModelConsumption}: { Path.Combine(settings.OutputPath.FullName, $"{settings.Name}.ConsoleApp")}");
logger.Log(LogLevel.Info, $"{Strings.SeeLogFileForMoreInfo}: {settings.LogFilePath}");
Console.ResetColor();
try
{
GenerateProject(columnInference, bestPipeline, columnInformation.LabelColumnName, modelPath);
Console.ForegroundColor = ConsoleColor.Yellow;
logger.Log(LogLevel.Info, $"{Strings.GenerateModelConsumption}: { Path.Combine(settings.OutputPath.FullName, $"{settings.Name}.ConsoleApp")}");
logger.Log(LogLevel.Info, $"{Strings.SeeLogFileForMoreInfo}: {settings.LogFilePath}");

}
catch (Exception)
{
logger.Log(LogLevel.Info, Strings.ErrorGeneratingProject);
throw;
}
finally
{
Console.ResetColor();
}
}

internal void GenerateProject(ColumnInferenceResults columnInference, Pipeline pipeline, string labelName, FileInfo modelPath)
Expand Down
71 changes: 42 additions & 29 deletions src/mlnet/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.CommandLine.Builder;
using System.CommandLine.Invocation;
using System.IO;
Expand All @@ -18,6 +19,7 @@ namespace Microsoft.ML.CLI
{
class Program
{
private static Logger logger = LogManager.GetCurrentClassLogger();
public static void Main(string[] args)
{
var telemetry = new MlTelemetry();
Expand All @@ -26,39 +28,50 @@ public static void Main(string[] args)
var handler = CommandHandler.Create<NewCommandSettings>(
(options) =>
{
// Map the verbosity to internal levels
var verbosity = Utils.GetVerbosity(options.Verbosity);
// Build the output path
string outputBaseDir = string.Empty;
if (options.Name == null)
try
{
options.Name = "Sample" + Utils.GetTaskKind(options.MlTask).ToString();
outputBaseDir = Path.Combine(options.OutputPath.FullName, options.Name);
// Map the verbosity to internal levels
var verbosity = Utils.GetVerbosity(options.Verbosity);
// Build the output path
string outputBaseDir = string.Empty;
if (options.Name == null)
{
options.Name = "Sample" + Utils.GetTaskKind(options.MlTask).ToString();
outputBaseDir = Path.Combine(options.OutputPath.FullName, options.Name);
}
else
{
outputBaseDir = Path.Combine(options.OutputPath.FullName, options.Name);
}
// Override the output path
options.OutputPath = new DirectoryInfo(outputBaseDir);
// Instantiate the command
var command = new NewCommand(options, telemetry);
// Override the Logger Configuration
var logconsole = LogManager.Configuration.FindTargetByName("logconsole");
var logfile = (FileTarget)LogManager.Configuration.FindTargetByName("logfile");
var logFilePath = Path.Combine(Path.Combine(outputBaseDir, "logs"), "debug_log.txt");
logfile.FileName = logFilePath;
options.LogFilePath = logFilePath;
var config = LogManager.Configuration;
config.AddRule(verbosity, LogLevel.Fatal, logconsole);
// Execute the command
command.Execute();
}
else
catch (Exception e)
{
outputBaseDir = Path.Combine(options.OutputPath.FullName, options.Name);
logger.Log(LogLevel.Error, e.Message);
logger.Log(LogLevel.Debug, e.ToString());
logger.Log(LogLevel.Info, Strings.LookIntoLogFile);
logger.Log(LogLevel.Error, Strings.Exiting);
return;
}
// Override the output path
options.OutputPath = new DirectoryInfo(outputBaseDir);
// Instantiate the command
var command = new NewCommand(options, telemetry);
// Override the Logger Configuration
var logconsole = LogManager.Configuration.FindTargetByName("logconsole");
var logfile = (FileTarget)LogManager.Configuration.FindTargetByName("logfile");
var logFilePath = Path.Combine(Path.Combine(outputBaseDir, "logs"), "debug_log.txt");
logfile.FileName = logFilePath;
options.LogFilePath = logFilePath;
var config = LogManager.Configuration;
config.AddRule(verbosity, LogLevel.Fatal, logconsole);
// Execute the command
command.Execute();
});

var parser = new CommandLineBuilder()
Expand Down
6 changes: 6 additions & 0 deletions src/mlnet/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,10 @@
<data name="SeeLogFileForMoreInfo" xml:space="preserve">
<value>Check out log file for more information</value>
</data>
<data name="ErrorGeneratingProject" xml:space="preserve">
<value>Exception occured while generating the project.</value>
</data>
<data name="ErrorSavingModel" xml:space="preserve">
<value>Exception occured while saving the model</value>
</data>
</root>
18 changes: 18 additions & 0 deletions src/mlnet/strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ab6bb99

Please sign in to comment.