Exemple #1
0
        /// <summary>
        /// Determine the default scorer for a schema bound mapper. This looks for text-valued ScoreColumnKind
        /// metadata on the first column of the mapper. If that text is found and maps to a scorer loadable class,
        /// that component is used. Otherwise, the GenericScorer is used.
        /// </summary>
        /// <param name="environment">The host environment.</param>.
        /// <param name="mapper">The schema bound mapper to get the default scorer.</param>.
        /// <param name="suffix">An optional suffix to append to the default column names.</param>
        public static TScorerFactory GetScorerComponent(
            IHostEnvironment environment,
            ISchemaBoundMapper mapper,
            string suffix = null)
        {
            Contracts.CheckValue(environment, nameof(environment));
            Contracts.AssertValue(mapper);

            ComponentCatalog.LoadableClassInfo info = null;
            ReadOnlyMemory <char> scoreKind         = default;

            if (mapper.OutputSchema.Count > 0 &&
                mapper.OutputSchema.TryGetMetadata(TextType.Instance, MetadataUtils.Kinds.ScoreColumnKind, 0, ref scoreKind) &&
                !scoreKind.IsEmpty)
            {
                var loadName = scoreKind.ToString();
                info = environment.ComponentCatalog.GetLoadableClassInfo <SignatureDataScorer>(loadName);
                if (info == null || !typeof(IDataScorerTransform).IsAssignableFrom(info.Type))
                {
                    info = null;
                }
            }

            Func <IHostEnvironment, IDataView, ISchemaBoundMapper, RoleMappedSchema, IDataScorerTransform> factoryFunc;

            if (info == null)
            {
                factoryFunc = (env, data, innerMapper, trainSchema) =>
                              new GenericScorer(
                    env,
                    new GenericScorer.Arguments()
                {
                    Suffix = suffix
                },
                    data,
                    innerMapper,
                    trainSchema);
            }
            else
            {
                factoryFunc = (env, data, innerMapper, trainSchema) =>
                {
                    object args = info.CreateArguments();
                    if (args is ScorerArgumentsBase scorerArgs)
                    {
                        scorerArgs.Suffix = suffix;
                    }
                    return((IDataScorerTransform)info.CreateInstance(
                               env,
                               args,
                               new object[] { data, innerMapper, trainSchema }));
                };
            }

            return(ComponentFactoryUtils.CreateFromFunction(factoryFunc));
        }
Exemple #2
0
        protected void GenerateEnums(IndentingTextWriter w, ComponentCatalog.LoadableClassInfo component)
        {
            var argumentInfo = CmdParser.GetArgInfo(component.ArgType, component.CreateArguments());
            var seen         = new HashSet <Tuple <Type, Type> >();

            foreach (var arg in argumentInfo.Args)
            {
                GenerateEnums(w, arg, seen);
            }
        }
        protected void GenerateImplFields(IndentingTextWriter w, ComponentCatalog.LoadableClassInfo component,
                                          Action <IndentingTextWriter, CmdParser.ArgInfo.Arg> fieldGenerator)
        {
            var argumentInfo = CmdParser.GetArgInfo(component.ArgType, component.CreateArguments());
            var arguments    = argumentInfo.Args.Where(a => !a.IsHidden).ToArray();

            foreach (var arg in arguments)
            {
                fieldGenerator(w, arg);
            }
        }
 protected override void GenerateMethodSignature(IndentingTextWriter w, string prefix, ComponentCatalog.LoadableClassInfo component)
 {
     w.Write("public static Tuple<ITrainer> Create{0}{1}(", prefix, component.LoadNames[0]);
     using (w.Nest())
     {
         var argumentInfo = CmdParser.GetArgInfo(component.ArgType, component.CreateArguments());
         var arguments    = argumentInfo.Args.Where(a => !a.IsHidden).ToArray();
         var pre          = "";
         foreach (var arg in arguments)
         {
             GenerateMethodSignature(w, arg, null, null, null, ref pre, "");
         }
         w.WriteLine(")");
     }
 }
 protected override void GenerateImplBody(IndentingTextWriter w, ComponentCatalog.LoadableClassInfo component)
 {
     w.WriteLine("{");
     using (w.Nest())
     {
         w.WriteLine("var args = new {0}();", GetCSharpTypeName(component.ArgType));
         w.WriteLine("var defs = new {0}();", GetCSharpTypeName(component.ArgType));
         var argumentInfo = CmdParser.GetArgInfo(component.ArgType, component.CreateArguments());
         var arguments    = argumentInfo.Args.Where(a => !a.IsHidden).ToArray();
         foreach (var arg in arguments)
         {
             GenerateImplBody(w, arg, "");
         }
         w.WriteLine("return new Tuple<string, string>(\"{0}\", CmdParser.GetSettings(args, defs));", component.LoadNames[0]);
     }
     w.WriteLine("}");
 }
 protected override void GenerateMethodSignature(IndentingTextWriter w, string prefix,
                                                 ComponentCatalog.LoadableClassInfo component)
 {
     w.WriteLine("public static Tuple<IDataView, DataTransform> Create{0}{1}(", prefix, component.LoadNames[0]);
     using (w.Nest())
     {
         var argumentInfo = CmdParser.GetArgInfo(component.ArgType, component.CreateArguments());
         w.WriteLine("[DataLabInputPort(FriendlyName = \"IDataView\", DisplayName = \"IDataView\", IsOptional = false, DataTypes = WellKnownDataTypeIds.IDataViewDotNet, Description = \"Input data (IDataView)\")]");
         w.Write("IDataView data");
         var pre = ",";
         foreach (var arg in argumentInfo.Args.Where(a => !a.IsHidden))
         {
             GenerateMethodSignature(w, arg, null, null, null, ref pre, "");
         }
         w.WriteLine(")");
     }
 }
Exemple #7
0
 protected override void GenerateImplCall(IndentedTextWriter w, string prefix, ComponentCatalog.LoadableClassInfo component)
 {
     w.WriteLine("{");
     using (w.Nest())
     {
         var className = prefix + component.LoadNames[0];
         w.WriteLine("var builder = new {0}();", className);
         var argumentInfo = CmdParser.GetArgInfo(component.ArgType, component.CreateArguments());
         foreach (var arg in argumentInfo.Args.Where(a => !a.IsHidden))
         {
             GenerateImplCall(w, arg, "");
         }
         w.WriteLine("var env = new LocalEnvironment(1, verbose: true);");
         w.WriteLine("var view = builder.Create{0}{1}Impl(env, data);", prefix, component.LoadNames[0]);
         w.WriteLine("return new Tuple<IDataView, DataTransform>(view, new DataTransform(view));");
     }
     w.WriteLine("}");
 }
 protected override void GenerateImplCall(IndentingTextWriter w, string prefix, ComponentCatalog.LoadableClassInfo component)
 {
     w.WriteLine("{");
     using (w.Nest())
     {
         var className = prefix + component.LoadNames[0];
         w.WriteLine("var builder = new {0}();", className);
         var argumentInfo = CmdParser.GetArgInfo(component.ArgType, component.CreateArguments());
         var arguments    = argumentInfo.Args.Where(a => !a.IsHidden).ToArray();
         foreach (var arg in arguments)
         {
             GenerateImplCall(w, arg, "");
         }
         w.WriteLine("var learner = builder.GetTlcSettings();");
         w.WriteLine("return new TlcTrainer(learner.Item1, learner.Item2);");
     }
     w.WriteLine("}");
 }
Exemple #9
0
        protected override void GenerateSummaryComment(IndentedTextWriter w, ComponentCatalog.LoadableClassInfo component)
        {
            w.WriteLine("/// <summary>");
            var desc = component.Summary ?? component.LoadNames[0];

            using (var sr = new StringReader(desc))
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    w.WriteLine("/// {0}", line);
                }
            }
            w.WriteLine("/// </summary>");
            GenerateParameterComment(w, "data", "The data");
            var argumentInfo = CmdParser.GetArgInfo(component.ArgType, component.CreateArguments());

            foreach (var arg in argumentInfo.Args.Where(a => !a.IsHidden))
            {
                GenerateSummaryComment(w, arg, "");
            }
        }
Exemple #10
0
 protected override void GenerateImplBody(IndentedTextWriter w, ComponentCatalog.LoadableClassInfo component)
 {
     w.WriteLine("{");
     using (w.Nest())
     {
         if (component.ArgType == null)
         {
             var call = GenerateCall(component);
             w.WriteLine("return {0}(env, data);", call);
         }
         else
         {
             w.WriteLine("var args = new {0}();", GetCSharpTypeName(component.ArgType));
             var argumentInfo = CmdParser.GetArgInfo(component.ArgType, component.CreateArguments());
             foreach (var arg in argumentInfo.Args.Where(a => !a.IsHidden))
             {
                 GenerateImplBody(w, arg, "");
             }
             var call = GenerateCall(component);
             w.WriteLine("return {0}(args, env, data);", call);
         }
     }
     w.WriteLine("}");
 }
Exemple #11
0
        protected override void GenerateContent(IndentedTextWriter writer, string prefix,
                                                ComponentCatalog.LoadableClassInfo component, string moduleId)
        {
            writer.WriteLine("[Module(");
            _compName = prefix + component.LoadNames[0];
            var name = Name ?? PrettyPrintDisplayName(component.LoadNames[0]);

            using (writer.Nest())
            {
                writer.WriteLine("Name = \"{0}\",", name);
                writer.WriteLine("FamilyId = \"{0}\",", moduleId);
                writer.WriteLine("Owner = \"{0}\",", Owner);
                writer.WriteLine("ReleaseVersion = \"{0}\",", Version);
                writer.WriteLine("State = ModuleState.{0},", State);
                writer.WriteLine("Type = ModuleType.{0},", ModuleType);
                writer.WriteLine("Determinism = Determinism.{0},", Determinism);
                writer.WriteLine("Category = @\"{0}\")]", Category);
            }
            writer.WriteLine("[Obsolete]");
            writer.WriteLine("public static IModule Create{0}(", _compName);
            using (writer.Nest())
            {
                writer.WriteLine("[Help(Display = @\"Dataset\", ToolTip = @\"Input dataset\")]");
                writer.WriteLine("[ModuleInputPort]");
                writer.WriteLine("IDataView idataset,");
                var argumentInfo = CmdParser.GetArgInfo(component.ArgType, component.CreateArguments());
                foreach (var arg in argumentInfo.Args.Where(a => !a.IsHidden))
                {
                    GenerateMethodSignature(writer, arg, null, null, null, "");
                }
                writer.WriteLine("[Help(Display = @\"Results dataset\", ToolTip = @\"Transformed dataset\")]");
                writer.WriteLine("[ModuleOutputPort]");
                writer.WriteLine("IDataView odataset,");
                writer.WriteLine("[Help(Display = @\"{0}\", ToolTip = @\"{0}\")]", name);
                writer.WriteLine("[ModuleOutputPort]");
                writer.WriteLine("DataTransform otransform,");
                writer.WriteLine("[Context]");
                writer.WriteLine("IContext context)");
            }
            writer.WriteLine("{");
            using (writer.Nest())
            {
                writer.WriteLine("var instance = new {0}Module();", _compName);
                writer.WriteLine();
                writer.WriteLine("var ports = new Dictionary<string, object> { { \"idataset\", idataset } };");
                writer.WriteLine("var parameters = new Dictionary<string, object>");
                writer.WriteLine("{");
                using (writer.Nest())
                {
                    var argumentInfo = CmdParser.GetArgInfo(component.ArgType, component.CreateArguments());
                    foreach (var arg in argumentInfo.Args.Where(a => !a.IsHidden))
                    {
                        GenerateDictionaryEntry(writer, arg, "");
                    }
                }
                writer.WriteLine("};");
                writer.WriteLine();
                writer.WriteLine("instance.Context = context;");
                writer.WriteLine("instance.SetInputPorts(ports);");
                writer.WriteLine("instance.SetParameters(parameters);");
                writer.WriteLine();
                writer.WriteLine("return instance;");
            }
            writer.WriteLine("}");
            writer.WriteLine();
            writer.WriteLine("[Obsolete]");
            writer.WriteLine("public class {0}Module : ModuleBase", _compName);
            writer.WriteLine("{");
            using (writer.Nest())
            {
                writer.WriteLine("private Dictionary<string, object> parameters;");
                writer.WriteLine("private Dictionary<string, object> ports;");
                writer.WriteLine();
                writer.WriteLine("public override Dictionary<string, object> Run()");
                writer.WriteLine("{");
                using (writer.Nest())
                {
                    writer.WriteLine("var view = ConstructTransform((IDataView)ports[\"idataset\"]);");
                    writer.WriteLine("return new Dictionary<string, object> { { \"odataset\", view }, { \"otransform\", new DataTransform(view) } };");
                }
                writer.WriteLine("}");
                writer.WriteLine();
                writer.WriteLine("public override void SetParameters(Dictionary<string, object> parameters)");
                writer.WriteLine("{");
                using (writer.Nest())
                    writer.WriteLine("this.parameters = parameters;");
                writer.WriteLine("}");
                writer.WriteLine();
                writer.WriteLine("public override void SetInputPorts(Dictionary<string, object> ports)");
                writer.WriteLine("{");
                using (writer.Nest())
                    writer.WriteLine("this.ports = ports;");
                writer.WriteLine("}");
                writer.WriteLine();
                writer.WriteLine("public override Dictionary<string, object> ComputeSchema(Dictionary<string, object> inputports)");
                writer.WriteLine("{");
                using (writer.Nest())
                {
                    writer.WriteLine("var view = ConstructTransform((IDataView)inputports[\"idataset\"]);");
                    writer.WriteLine("return new Dictionary<string, object> { { \"odataset\", view.Schema } };");
                }
                writer.WriteLine("}");
                writer.WriteLine();
                writer.WriteLine("private IDataView ConstructTransform(IDataView input)");
                writer.WriteLine("{");
                using (writer.Nest())
                {
                    writer.WriteLine("var builder = new {0}();", _compName);
                    var argumentInfo = CmdParser.GetArgInfo(component.ArgType, component.CreateArguments());
                    foreach (var arg in argumentInfo.Args.Where(a => !a.IsHidden))
                    {
                        GenerateImplCall(writer, arg, null, null, null, "");
                    }
                    writer.WriteLine("return builder.Create{0}Impl(Host, input);", _compName);
                }
                writer.WriteLine("}");
            }
            writer.WriteLine("}");
        }