Esempio n. 1
0
        public void TestI_Q_KMeansInnerAPIWithDataFrame()
        {
            var methodName       = System.Reflection.MethodBase.GetCurrentMethod().Name;
            var outModelFilePath = FileHelper.GetOutputFile("outModelFilePath.zip", methodName);
            var iris             = FileHelper.GetTestFile("iris.txt");

            using (var env = new ConsoleEnvironment(conc: 1))
            {
                ComponentHelper.AddStandardComponents(env);

                var        df      = Scikit.ML.DataManipulation.DataFrameIO.ReadCsv(iris, sep: '\t', dtypes: new ColumnType[] { NumberType.R4 });
                var        conc    = env.CreateTransform("Concat{col=Feature:Sepal_length,Sepal_width}", df);
                var        roleMap = env.CreateExamples(conc, "Feature", label: "Label");
                var        trainer = CreateTrainer(env, "km");
                IPredictor model;
                using (var ch = env.Start("test"))
                    model = TrainUtils.Train(env, ch, roleMap, trainer, null, 0);

                using (var ch = env.Start("Save"))
                    using (var fs = File.Create(outModelFilePath))
                        TrainUtils.SaveModel(env, ch, fs, model, roleMap);

                Predictor pred;
                using (var fs = File.OpenRead(outModelFilePath))
                    pred = env.LoadPredictorOrNull(fs);

#pragma warning disable CS0618
                var scorer = ScoreUtils.GetScorer(pred.GetPredictorObject() as IPredictor, roleMap, env, null);
#pragma warning restore CS0618
                var dfout = Scikit.ML.DataManipulation.DataFrameIO.ReadView(scorer);
                Assert.AreEqual(dfout.Shape, new Tuple <int, int>(150, 13));
            }
        }
Esempio n. 2
0
        public void TestScikitAPI_DelegateEnvironment()
        {
            var inputs = new[] {
                new ExampleA()
                {
                    X = new float[] { 1, 10, 100 }
                },
                new ExampleA()
                {
                    X = new float[] { 2, 3, 5 }
                }
            };

            var inputs2 = new[] {
                new ExampleA()
                {
                    X = new float[] { -1, -10, -100 }
                },
                new ExampleA()
                {
                    X = new float[] { -2, -3, -5 }
                }
            };

            var        stdout = new List <string>();
            var        stderr = new List <string>();
            ILogWriter logout = new LogWriter((string s) =>
            {
                stdout.Add(s);
            });
            ILogWriter logerr = new LogWriter((string s) =>
            {
                stderr.Add(s);
            });

            using (var host = new DelegateEnvironment(conc: 1, outWriter: logout, errWriter: logerr, verbose: 3))
                using (var ch = host.Start("Train Pipeline"))
                {
                    ComponentHelper.AddStandardComponents(host);
                    ch.Info(MessageSensitivity.All, "Polynomial");
                    var data = host.CreateStreamingDataView(inputs);
                    using (var pipe = new ScikitPipeline(new[] { "poly{col=X}" }, host: host))
                    {
                        var predictor = pipe.Train(data);
                        if (predictor == null)
                        {
                            throw new Exception("Predictor is null");
                        }
                    }
                }
            if (stdout.Count == 0)
            {
                throw new Exception("stdout is empty.");
            }
            if (stderr.Count != 0)
            {
                throw new Exception($"stderr not empty\n{string.Join("\n", stderr)}");
            }
        }
 /// <summary>
 /// Returns all kinds of models.
 /// </summary>
 public static string[] GetAllKinds()
 {
     using (var env = new ConsoleEnvironment())
     {
         ComponentHelper.AddStandardComponents(env);
         var sigs    = env.ComponentCatalog.GetAllSignatureTypes();
         var typeSig = sigs.Select(t => ComponentCatalog.SignatureToString(t).ToLowerInvariant());
         return(new HashSet <string>(typeSig.OrderBy(c => c)).ToArray());
     }
 }
Esempio n. 4
0
        public static IHostEnvironment CreateEnvironment(int seed             = -1, int verbose = 0,
                                                         string sensitivity   = "All", int conc = 0,
                                                         ILogWriter outWriter = null, ILogWriter errWriter = null)
        {
            var env = new DelegateEnvironment(seed: seed < 0 ? null : (int?)seed, verbose: verbose,
                                              sensitivity: MessageSensitivityFromString(sensitivity),
                                              conc: conc, outWriter: outWriter, errWriter: errWriter);

            ComponentHelper.AddStandardComponents(env);
            return(env);
        }
Esempio n. 5
0
        public static Tuple <IHostEnvironment, StringBuilder, StringBuilder> CreateStoreEnvironment(int seed           = -1, int verbose = 0,
                                                                                                    string sensitivity = "All", int conc = 0)
        {
            ILogWriter logout, logerr;
            var        stout = new StringBuilder();
            var        sterr = new StringBuilder();

            logout = new LogWriter((string s) => { stout.Append(s); });
            logerr = new LogWriter((string s) => { sterr.Append(s); });
            var env = new DelegateEnvironment(seed: seed < 0 ? null : (int?)seed, verbose: verbose,
                                              sensitivity: MessageSensitivityFromString(sensitivity),
                                              conc: conc, outWriter: logout, errWriter: logerr);

            ComponentHelper.AddStandardComponents(env);
            return(new Tuple <IHostEnvironment, StringBuilder, StringBuilder>(env, stout, sterr));
        }
 public static string[] GetLoadedAssemblies(bool env = true)
 {
     if (env)
     {
         using (var e = new ConsoleEnvironment())
         {
             ComponentHelper.AddStandardComponents(e);
             var assemblies = AppDomain.CurrentDomain.GetAssemblies().Select(x => InfoAssembly(x)).OrderBy(c => c);
             return(assemblies.ToArray());
         }
     }
     else
     {
         var assemblies = AppDomain.CurrentDomain.GetAssemblies().Select(x => InfoAssembly(x)).OrderBy(c => c);
         return(assemblies.ToArray());
     }
 }
Esempio n. 7
0
        public void TestScikitAPI_DelegateEnvironmentVerbose0()
        {
            var inputs = new[] {
                new ExampleA()
                {
                    X = new float[] { 1, 10, 100 }
                },
                new ExampleA()
                {
                    X = new float[] { 2, 3, 5 }
                }
            };

            var inputs2 = new[] {
                new ExampleA()
                {
                    X = new float[] { -1, -10, -100 }
                },
                new ExampleA()
                {
                    X = new float[] { -2, -3, -5 }
                }
            };

            var        stdout = new List <string>();
            var        stderr = new List <string>();
            ILogWriter logout = new LogWriter(s => stdout.Add(s));
            ILogWriter logerr = new LogWriter(s => stderr.Add(s));

            /*using (*/
            var host = new DelegateEnvironment(seed: 0, outWriter: logout, errWriter: logerr, verbose: 0);

            {
                ComponentHelper.AddStandardComponents(host);
                var data = DataViewConstructionUtils.CreateFromEnumerable(host, inputs);
                using (var pipe = new ScikitPipeline(new[] { "poly{col=X}" }, "km{k=2}", host: host))
                {
                    var predictor = pipe.Train(data, feature: "X");
                    Assert.IsTrue(predictor != null);
                }
            }
            Assert.AreEqual(stdout.Count, 0);
            Assert.AreEqual(stderr.Count, 0);
        }
        /// <summary>
        /// Creates a new environment. It should be done
        /// with <tt>using</tt>.
        /// </summary>
        public static ConsoleEnvironment NewTestEnvironment(int?seed = null, bool verbose = false,
                                                            MessageSensitivity sensitivity = (MessageSensitivity)(-1),
                                                            int conc = 0, TextWriter outWriter = null, TextWriter errWriter = null)
        {
            if (!seed.HasValue)
            {
                seed = 42;
            }
            if (outWriter == null)
            {
                outWriter = new StreamWriter(new MemoryStream());
            }
            if (errWriter == null)
            {
                errWriter = new StreamWriter(new MemoryStream());
            }

            var env = new ConsoleEnvironment(seed, verbose, sensitivity, conc, outWriter, errWriter);

            ComponentHelper.AddStandardComponents(env);
            return(env);
        }
 public static string[] GetLoadedAssembliesLocation(bool env = true)
 {
     if (env)
     {
         using (var e = new ConsoleEnvironment())
         {
             ComponentHelper.AddStandardComponents(e);
             var assemblies = AppDomain.CurrentDomain.GetAssemblies()
                              .Select(x => GetAssemblyLocation(x))
                              .Where(x => !string.IsNullOrEmpty(x))
                              .ToArray();
             return(assemblies.ToArray());
         }
     }
     else
     {
         var assemblies = AppDomain.CurrentDomain.GetAssemblies()
                          .Select(x => GetAssemblyLocation(x))
                          .Where(x => !string.IsNullOrEmpty(x))
                          .ToArray();
         return(assemblies.ToArray());
     }
 }
        /// <summary>
        /// Returns all kinds of models.
        /// </summary>
        public static IEnumerable <ComponentDescription> EnumerateComponents(string kind)
        {
            if (kind == "argument")
            {
                foreach (var comp in EnumerateComponentsParameter(false))
                {
                    yield return(comp);
                }
            }
            else if (kind == "command")
            {
                foreach (var comp in EnumerateComponentsParameter(true))
                {
                    yield return(comp);
                }
            }
            else
            {
                var kinds = GetAllKinds();
                if (!string.IsNullOrEmpty(kind) && !kinds.Where(c => c == kind).Any())
                {
                    throw new ArgumentException($"Unable to find kind '{kind}' in\n{string.Join("\n", kinds)}.");
                }

                using (var env = new ConsoleEnvironment())
                {
                    ComponentHelper.AddStandardComponents(env);
                    var    sigs    = env.ComponentCatalog.GetAllSignatureTypes();
                    var    typeRes = typeof(object);
                    Type[] typeSigs;
                    if (string.IsNullOrEmpty(kind))
                    {
                        typeSigs = sigs.ToArray();
                    }
                    else
                    {
                        typeSigs = new[] { sigs.FirstOrDefault(t => ComponentCatalog.SignatureToString(t).ToLowerInvariant() == kind) }
                    };
                    foreach (var typeSig in typeSigs)
                    {
                        var infos = env.ComponentCatalog.GetAllDerivedClasses(typeRes, typeSig)
                                    .Where(x => !x.IsHidden)
                                    .OrderBy(x => x.LoadNames[0].ToLowerInvariant());
                        foreach (var info in infos)
                        {
                            var args = info.CreateArguments();
                            if (args == null)
                            {
                                yield return(new ComponentDescription(info, args, null, null));
                            }
                            else
                            {
                                var asse = args.GetType().Assembly;

                                var parsedArgs = CmdParser.GetArgInfo(args.GetType(), args).Args;
                                var arguments  = new List <ComponentDescription.Argument>();
                                foreach (var arg in parsedArgs)
                                {
                                    var a = new ComponentDescription.Argument()
                                    {
                                        Name         = arg.LongName,
                                        ShortName    = arg.ShortNames == null || !arg.ShortNames.Any() ? null : arg.ShortNames.First(),
                                        DefaultValue = arg.DefaultValue == null ? null : arg.DefaultValue.ToString(),
                                        Help         = arg.HelpText,
                                        Arg          = arg,
                                    };
                                    arguments.Add(a);
                                }

                                var cmp = new ComponentDescription(info, args, asse, arguments);
                                yield return(cmp);
                            }
                        }
                    }
                }
            }
        }

        #endregion
    }
        /// <summary>
        /// Runs a simple test.
        /// </summary>
        public static void TestScikitAPI()
        {
            var inputs = new[] {
                new ExampleVector()
                {
                    X = new float[] { 1, 10, 100 }
                },
                new ExampleVector()
                {
                    X = new float[] { 2, 3, 5 }
                },
                new ExampleVector()
                {
                    X = new float[] { 2, 4, 5 }
                },
                new ExampleVector()
                {
                    X = new float[] { 2, 4, 7 }
                },
            };

            var inputs2 = new[] {
                new ExampleVector()
                {
                    X = new float[] { -1, -10, -100 }
                },
                new ExampleVector()
                {
                    X = new float[] { -2, -3, -5 }
                },
                new ExampleVector()
                {
                    X = new float[] { 3, 4, 5 }
                },
                new ExampleVector()
                {
                    X = new float[] { 3, 4, 7 }
                },
            };

            using (var host = new ConsoleEnvironment(conc: 1))
            {
                ComponentHelper.AddStandardComponents(host);
                var data = host.CreateStreamingDataView(inputs);
                using (var pipe = new ScikitPipeline(new[] { "poly{col=X}" }, "km{k=2}", host))
                {
                    var predictor = pipe.Train(data, feature: "X");
                    if (predictor == null)
                    {
                        throw new Exception("Test failed: no predictor.");
                    }
                    var data2       = host.CreateStreamingDataView(inputs2);
                    var predictions = pipe.Predict(data2);
                    var df          = DataFrameIO.ReadView(predictions);
                    if (df.Shape.Item1 != 4 || df.Shape.Item2 != 12)
                    {
                        throw new Exception("Test failed: prediction failed.");
                    }
                    var dfs  = df.ToString();
                    var dfs2 = dfs.Replace("\n", ";");
                    if (!dfs2.StartsWith("X.0,X.1,X.2,X.3,X.4,X.5,X.6,X.7,X.8,PredictedLabel,Score.0,Score.1;-1,-10,-100,1,10,100,100,1000,10000"))
                    {
                        throw new Exception("Test failed: prediction failed (header).");
                    }
                }
            }
        }
Esempio n. 12
0
        public static string MamlScript(string script, bool catch_output,
                                        int conc             = 0, int verbose = 2, int sensitivity = -1,
                                        bool skipFailLoading = false)
        {
            ILogWriter logout, logerr;
            var        stout = new StringBuilder();
            var        sterr = new StringBuilder();

            if (catch_output)
            {
                logout = new LogWriter((string s) =>
                {
                    stout.Append(s);
                });
                logerr = new LogWriter((string s) =>
                {
                    sterr.Append(s);
                });
            }
            else
            {
                logout = new LogWriter((string s) => { Console.Write(s); });
                logerr = new LogWriter((string s) => { Console.Error.Write(s); });
            }

            using (var env = new DelegateEnvironment((int?)null, verbose, (MessageSensitivity)sensitivity, conc, logout, logerr))
            {
                if (skipFailLoading)
                {
                    try
                    {
                        ComponentHelper.AddStandardComponents(env);
                    }
                    catch (Exception e)
                    {
                        sterr.Append($"Unable to load an assembly due to {e.ToString()}.\n");
                    }
                }
                else
                {
                    ComponentHelper.AddStandardComponents(env);
                }

                string res = null;
                try
                {
                    res = MamlHelper.MamlScript(script, false, env);
                    if (!string.IsNullOrEmpty(res))
                    {
                        stout.Append(res);
                    }
                }
                catch (InvalidOperationException e)
                {
                    if (!string.IsNullOrEmpty(res))
                    {
                        sterr.Append(res + "\n");
                    }
                    sterr.Append($"[PyMamlHelper-InvalidOperationException] catch_output={catch_output}\n" + e.ToString() + "\n");
                }
                catch (MamlException e)
                {
                    if (!string.IsNullOrEmpty(res))
                    {
                        sterr.Append(res + "\n");
                    }
                    sterr.Append($"[PyMamlHelper-MamlException] catch_output={catch_output}\n" + e.ToString() + "\n");
                }
                if (sterr.Length > 0)
                {
                    return(string.Format("---OUT---\n{0}\n---ERR---\n{1}", stout.ToString(), sterr.ToString()));
                }
                else
                {
                    return(stout.ToString());
                }
            }
        }