Exemplo n.º 1
0
        private bool IsWithinBudget(ProjectComponent projectComponent, SubComponent subComponent)
        {
            //get the component budget

            var componentBudget = projectComponent.Budget;

            //get the total amount of money in each component

            decimal totalsubcomponentBudget = 0;

            //loop through all the subcomponents adding each components budget

            foreach (var subcomponent in projectComponent.SubComponents)
            {
                totalsubcomponentBudget += subcomponent.Budget;
            }


            var newProposedTotal = totalsubcomponentBudget + subComponent.Budget;

            if (newProposedTotal > componentBudget)
            {
                return(false);
            }
            else
            {
                return(true);
            }
        }
Exemplo n.º 2
0
        private static byte[] GetBytesOne(IHost host, string dataFile, SubComponent <IDataLoader, SignatureDataLoader> sub,
                                          string termColumn, string valueColumn)
        {
            Contracts.AssertValue(host);
            host.Assert(!string.IsNullOrWhiteSpace(dataFile));
            host.AssertNonEmpty(termColumn);
            host.AssertNonEmpty(valueColumn);

            if (!sub.IsGood())
            {
                // REVIEW: Should there be defaults for loading from text?
                var  ext         = Path.GetExtension(dataFile);
                bool isBinary    = string.Equals(ext, ".idv", StringComparison.OrdinalIgnoreCase);
                bool isTranspose = string.Equals(ext, ".tdv", StringComparison.OrdinalIgnoreCase);
                if (!isBinary && !isTranspose)
                {
                    throw host.ExceptUserArg(nameof(Arguments.Loader), "must specify the loader");
                }
                host.Assert(isBinary != isTranspose); // One or the other must be true.
                sub = new SubComponent <IDataLoader, SignatureDataLoader>(isBinary ? "BinaryLoader" : "TransposeLoader");
            }
            var ldr = sub.CreateInstance(host, new MultiFileSource(dataFile));

            return(GetBytesFromDataView(host, ldr, termColumn, valueColumn));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Given a predictor and an optional scorer SubComponent, produces a compatible ISchemaBindableMapper.
        /// First, it tries to instantiate the bindable mapper using the <paramref name="scorerSettings"/>
        /// (this will only succeed if there's a registered BindableMapper creation method with load name equal to the one
        /// of the scorer).
        /// If the above fails, it checks whether the predictor implements <see cref="ISchemaBindableMapper"/>
        /// directly.
        /// If this also isn't true, it will create a 'matching' standard mapper.
        /// </summary>
        public static ISchemaBindableMapper GetSchemaBindableMapper(IHostEnvironment env, IPredictor predictor,
                                                                    SubComponent <IDataScorerTransform, SignatureDataScorer> scorerSettings)
        {
            Contracts.CheckValue(env, nameof(env));
            env.CheckValue(predictor, nameof(predictor));
            env.CheckValueOrNull(scorerSettings);

            // See if we can instantiate a mapper using scorer arguments.
            if (scorerSettings.IsGood() && TryCreateBindableFromScorer(env, predictor, scorerSettings, out var bindable))
            {
                return(bindable);
            }

            // The easy case is that the predictor implements the interface.
            bindable = predictor as ISchemaBindableMapper;
            if (bindable != null)
            {
                return(bindable);
            }

            // Use one of the standard wrappers.
            if (predictor is IValueMapperDist)
            {
                return(new SchemaBindableBinaryPredictorWrapper(predictor));
            }

            return(new SchemaBindablePredictorWrapper(predictor));
        }
Exemplo n.º 4
0
        public void AddSubComponent()
        {
            var       enc = new HL7Encoding();
            Segment   PID = new Segment("PID", enc);
            Field     f   = new Field(enc);
            Component c   = new Component(enc);

            c.IsSubComponentized = true;
            SubComponent sc1 = new SubComponent("A", enc);
            SubComponent sc2 = new SubComponent("B", enc);

            c.AddSubComponent(sc1);
            c.AddSubComponent(sc2);
            f.AddNewComponent(c);

            // Creates a new Field
            PID.AddNewField(f, 1);

            Message message = new Message();

            message.AddNewSegment(PID);
            var str = message.SerializeMessage(false);

            Assert.AreEqual("PID|A&B\r", str);
        }
Exemplo n.º 5
0
        public void Dispose_NullSite_Success()
        {
            var component = new SubComponent();

            component.Dispose();
            component.Dispose();
        }
        /// <summary>
        /// Create a trainer.
        /// </summary>
        /// <param name="env">host</param>
        /// <param name="settings">trainer description as a string such <pre>ova{p=lr}</pre></param>
        /// <param name="extraArgs">additional arguments</param>
        public static ITrainerExtended CreateTrainer(IHostEnvironment env, string settings, params object[] extraArgs)
        {
            var sc   = SubComponent.Parse <ITrainer, SignatureTrainer>(settings);
            var inst = sc.CreateInstance(env, extraArgs);

            return(new ExtendedTrainer(inst, sc.Kind));
        }
Exemplo n.º 7
0
        // This saves the lookup data as a byte array encoded as a binary .idv file.
        private static byte[] GetBytes(IHost host, ColInfo[] infos, Arguments args)
        {
            Contracts.AssertValue(host);
            host.AssertNonEmpty(infos);
            host.AssertValue(args);

            string dataFile = args.DataFile;
            SubComponent <IDataLoader, SignatureDataLoader> loader = args.Loader;
            string termColumn;
            string valueColumn;

            if (!string.IsNullOrEmpty(args.TermColumn))
            {
                host.Assert(!string.IsNullOrEmpty(args.ValueColumn));
                termColumn  = args.TermColumn;
                valueColumn = args.ValueColumn;
            }
            else
            {
                var ext = Path.GetExtension(dataFile);
                if (loader.IsGood() || string.Equals(ext, ".idv", StringComparison.OrdinalIgnoreCase))
                {
                    throw host.ExceptUserArg(nameof(args.TermColumn), "Term and value columns needed.");
                }
                loader      = GetLoaderSubComponent(args.DataFile, args.KeyValues, host);
                termColumn  = "Term";
                valueColumn = "Value";
            }
            return(GetBytesOne(host, dataFile, loader, termColumn, valueColumn));
        }
Exemplo n.º 8
0
        public static IPredictor Train(IHostEnvironment env, IChannel ch, RoleMappedData data, ITrainer trainer, string name, RoleMappedData validData,
                                       SubComponent <ICalibratorTrainer, SignatureCalibrator> calibrator, int maxCalibrationExamples, bool?cacheData, IPredictor inputPredictor = null)
        {
            ICalibratorTrainer caliTrainer = !calibrator.IsGood() ? null : calibrator.CreateInstance(env);

            return(TrainCore(env, ch, data, trainer, name, validData, caliTrainer, maxCalibrationExamples, cacheData, inputPredictor));
        }
Exemplo n.º 9
0
        private void PrintRecipe(IChannel ch, RecipeInference.SuggestedRecipe[] suggestedRecipes, string settingsString, StreamWriter sw = null)
        {
            string prefix = "";

            foreach (var recipe in suggestedRecipes)
            {
                var loaderSubComponent = new SubComponent("TextLoader", settingsString);
                sw?.WriteLine(prefix + "loader={0}", loaderSubComponent);
                foreach (var suggestion in recipe.Transforms)
                {
                    string transform = $"xf={suggestion.Transform}";
                    ch.Info(transform);
                    sw?.WriteLine(transform);
                }

                if (recipe.Learners != null)
                {
                    foreach (var suggestion in recipe.Learners)
                    {
                        string learner = $"tr={suggestion.LoadableClassInfo.LoadNames[0]}{{{suggestion.Settings}}}";
                        ch.Info(learner);
                        sw?.WriteLine(learner);
                    }
                }
                prefix = "\n\n";
            }
        }
 /// <summary>
 /// Trains a model.
 /// </summary>
 /// <param name="env">host</param>
 /// <param name="ch">channel</param>
 /// <param name="data">traing data</param>
 /// <param name="validData">validation data</param>
 /// <param name="calibrator">calibrator</param>
 /// <param name="maxCalibrationExamples">number of examples used to calibrate</param>
 /// <param name="cacheData">cache training data</param>
 /// <param name="inpPredictor">for continuous training, initial state</param>
 /// <returns>predictor</returns>
 public IPredictor Train(IHostEnvironment env, IChannel ch, RoleMappedData data, RoleMappedData validData = null,
                         SubComponent <ICalibratorTrainer, SignatureCalibrator> calibrator = null, int maxCalibrationExamples = 0,
                         bool?cacheData = null, IPredictor inpPredictor = null)
 {
     return(TrainUtils.Train(env, ch, data, Trainer, LoadName, validData, calibrator, maxCalibrationExamples,
                             cacheData, inpPredictor));
 }
Exemplo n.º 11
0
        public virtual NuGenHL7Exception[] testComponent(Genetibase.NuGenHL7.model.Type type, Component profile, System.String profileID)
        {
            System.Collections.ArrayList exList = new System.Collections.ArrayList(20);

            addToList(testType(type, profile, null, profileID), exList);

            //test children
            if (profile.SubComponents > 0 && !profile.Usage.Equals("X") && hasContent(type))
            {
                if (typeof(Composite).IsAssignableFrom(type.GetType()))
                {
                    Composite comp = (Composite)type;
                    for (int i = 1; i <= profile.SubComponents; i++)
                    {
                        SubComponent childProfile = profile.getSubComponent(i);
                        try
                        {
                            Genetibase.NuGenHL7.model.Type child = comp.getComponent(i - 1);
                            addToList(testType(child, childProfile, null, profileID), exList);
                        }
                        catch (DataTypeException de)
                        {
                            exList.Add(new NuGenProfileNotHL7CompliantException("More subcomponents in profile than allowed in message: " + de.Message));
                        }
                    }
                    addToList(checkExtraComponents(comp, profile.SubComponents), exList);
                }
                else
                {
                    exList.Add(new NuGenProfileNotFollowedException("A component has primitive type " + type.GetType().FullName + " but the profile defines subcomponents"));
                }
            }

            return(toArray(exList));
        }
Exemplo n.º 12
0
        private IEarlyStoppingCriterion CreateEarlyStoppingCriterion(string name, string args, bool lowerIsBetter)
        {
            var env = new ConsoleEnvironment()
                      .AddStandardComponents();
            var sub = new SubComponent <IEarlyStoppingCriterion, SignatureEarlyStoppingCriterion>(name, args);

            return(sub.CreateInstance(env, lowerIsBetter));
        }
Exemplo n.º 13
0
        private EarlyStoppingRuleBase CreateEarlyStoppingCriterion(string name, string args, bool lowerIsBetter)
        {
            var env = new MLContext()
                      .AddStandardComponents();
            var sub = new SubComponent <EarlyStoppingRuleBase, SignatureEarlyStoppingCriterion>(name, args);

            return(sub.CreateInstance(env, lowerIsBetter));
        }
 /// <param name="env">The environment.</param>
 /// <param name="registrationName">The registration name.</param>
 /// <param name="inputDataView">The input data view.</param>
 /// <param name="splitColumn">The column to use for splitting data into folds.</param>
 /// <param name="args">Cross validation arguments.</param>
 /// <param name="createExamples">The delegate to create RoleMappedData</param>
 /// <param name="applyTransformsToTestData">The delegate to apply the transforms from the train pipeline to the test data</param>
 /// <param name="scorer">The scorer</param>
 /// <param name="evaluator">The evaluator</param>
 /// <param name="getValidationDataView">The delegate to create validation data view</param>
 /// <param name="applyTransformsToValidationData">The delegate to apply the transforms from the train pipeline to the validation data</param>
 /// <param name="inputPredictor">The input predictor, for the continue training option</param>
 /// <param name="cmd">The command string.</param>
 /// <param name="loader">Original loader so we can construct correct pipeline for model saving.</param>
 /// <param name="savePerInstance">Whether to produce the per-instance data view.</param>
 /// <returns></returns>
 public FoldHelper(
     IHostEnvironment env,
     string registrationName,
     IDataView inputDataView,
     string splitColumn,
     Arguments args,
     Func <IHostEnvironment, IChannel, IDataView, ITrainer, RoleMappedData> createExamples,
     Func <IHostEnvironment, IChannel, IDataView, RoleMappedData, IDataView, RoleMappedData> applyTransformsToTestData,
     SubComponent <IDataScorerTransform, SignatureDataScorer> scorer,
     SubComponent <IMamlEvaluator, SignatureMamlEvaluator> evaluator,
     Func <IDataView> getValidationDataView = null,
     Func <IHostEnvironment, IChannel, IDataView, RoleMappedData, IDataView, RoleMappedData> applyTransformsToValidationData = null,
     IPredictor inputPredictor = null,
     string cmd           = null,
     IDataLoader loader   = null,
     bool savePerInstance = false)
 {
     Contracts.CheckValue(env, nameof(env));
     env.CheckNonWhiteSpace(registrationName, nameof(registrationName));
     env.CheckValue(inputDataView, nameof(inputDataView));
     env.CheckValue(splitColumn, nameof(splitColumn));
     env.CheckParam(args.NumFolds > 1, nameof(args.NumFolds));
     env.CheckValue(createExamples, nameof(createExamples));
     env.CheckValue(applyTransformsToTestData, nameof(applyTransformsToTestData));
     env.CheckParam(args.Trainer.IsGood(), nameof(args.Trainer));
     env.CheckValueOrNull(scorer);
     env.CheckValueOrNull(evaluator);
     env.CheckValueOrNull(args.Calibrator);
     env.CheckParam(args.MaxCalibrationExamples > 0, nameof(args.MaxCalibrationExamples));
     env.CheckParam(getValidationDataView == null || applyTransformsToValidationData != null, nameof(applyTransformsToValidationData));
     env.CheckValueOrNull(inputPredictor);
     env.CheckValueOrNull(cmd);
     env.CheckValueOrNull(args.OutputModelFile);
     env.CheckValueOrNull(loader);
     _env = env;
     _registrationName          = registrationName;
     _inputDataView             = inputDataView;
     _splitColumn               = splitColumn;
     _numFolds                  = args.NumFolds;
     _createExamples            = createExamples;
     _applyTransformsToTestData = applyTransformsToTestData;
     _trainer                         = args.Trainer;
     _scorer                          = scorer;
     _evaluator                       = evaluator;
     _calibrator                      = args.Calibrator;
     _maxCalibrationExamples          = args.MaxCalibrationExamples;
     _useThreads                      = args.UseThreads;
     _cacheData                       = args.CacheData;
     _getValidationDataView           = getValidationDataView;
     _applyTransformsToValidationData = applyTransformsToValidationData;
     _inputPredictor                  = inputPredictor;
     _cmd             = cmd;
     _outputModelFile = args.OutputModelFile;
     _loader          = loader;
     _savePerInstance = savePerInstance;
 }
Exemplo n.º 15
0
            protected void SendTelemetryComponent(IPipe <TelemetryMessage> pipe, SubComponent sub)
            {
                Host.AssertValue(pipe);
                Host.AssertValueOrNull(sub);

                if (sub.IsGood())
                {
                    pipe.Send(TelemetryMessage.CreateTrainer(sub.Kind, sub.SubComponentSettings));
                }
            }
Exemplo n.º 16
0
        public void Dispose_HasDisposedWithoutEvents_DoesNotnvokesEvent()
        {
            var component = new SubComponent();

            component.Disposed += Component_Disposed;
            component.CanRaiseEventsInternal = false;

            component.Dispose();
            Assert.False(InvokedDisposed);
        }
Exemplo n.º 17
0
        public void Ctor_Default()
        {
            var component = new SubComponent();

            Assert.True(component.CanRaiseEvents);
            Assert.Null(component.Container);
            Assert.False(component.DesignMode);
            Assert.Same(component.Events, component.Events);
            Assert.Null(component.Site);
        }
Exemplo n.º 18
0
        private static TRes CreateInstance <TRes, TSig>(IHostEnvironment env, string settings, out string loadName, params object[] extraArgs)
            where TRes : class
        {
            Contracts.AssertValue(env);
            env.AssertValue(settings, "settings");

            var sc = SubComponent.Parse <TRes, TSig>(settings);

            loadName = sc.Kind;
            return(sc.CreateInstance(env, extraArgs));
        }
Exemplo n.º 19
0
        public void Site_Set_GetReturnsExpected()
        {
            var component = new SubComponent();

            Assert.Null(component.Site);

            var site = new MockSite();

            component.Site = site;
            Assert.Same(site, component.Site);
        }
Exemplo n.º 20
0
        private void WeightedMetricTest(Instances noWeights, Instances weights1, Instances weightsQuarter, string predictorName, Func <Tester <Float> > tester)
        {
            Instances[] data = new Instances[3] {
                noWeights, weights1, weightsQuarter
            };
            Metric[][] results = new Metric[3][];
            var        sub     = new SubComponent <ITrainer <Instances, IPredictor <Instance, Float> >, SignatureOldTrainer>(
                predictorName, "nl=5 lr=0.25 iter=20 mil=1");

            for (int i = 0; i < 3; i++)
            {
                Instances instances = data[i];
                if (instances == null)
                {
                    continue;
                }

                // Create the trainer
                var trainer = sub.CreateInstance(new TrainHost(new Random(1), 0));

                // Train a predictor
                trainer.Train(instances);
                var predictor = trainer.CreatePredictor();

                results[i] = tester().Test(predictor, instances);
            }

            //Compare metrics results with unweighted metrics
            for (int i = 1; i < 3; i++)
            {
                if (results[i] == null)
                {
                    continue;
                }
                //The nonweighted result should have half of the metrics
                Assert.Equal(results[i].Length, results[0].Length * 2);
                for (int m = 0; m < results[0].Length; m++)
                {
                    Assert.Equal(results[0][m].Name, results[i][m].Name);
                    Double diff = Math.Abs(results[0][m].Value - results[i][m].Value);
                    if (diff > 1e-6)
                    {
                        Fail("{0} differ: {1} vs. {2}", results[0][m].Name, results[0][m].Value, results[i][m].Value);
                    }
                }
            }

            //Compare all metrics between weight 1 (with and without explicit weight in the input)
            for (int m = 0; m < results[0].Length; m++)
            {
                Assert.True(Math.Abs(results[0][m].Value - results[1][m].Value) < 1e-10);
                Assert.True(Math.Abs(results[0][m].Value - results[1][m + results[0].Length].Value) < 1e-10);
            }
        }
Exemplo n.º 21
0
        public void ToString_HasSite_ReturnsExpected()
        {
            var component = new SubComponent();

            Assert.Equal("System.ComponentModel.Tests.ComponentTests+SubComponent", component.ToString());

            component.Site = new MockSite {
                Name = "name"
            };
            Assert.Equal("name [System.ComponentModel.Tests.ComponentTests+SubComponent]", component.ToString());
        }
Exemplo n.º 22
0
        public void Dispose_NullSiteContainer_Success()
        {
            var component = new SubComponent()
            {
                Site = new MockSite {
                    Container = null
                }
            };

            component.Dispose();
            component.Dispose();
        }
Exemplo n.º 23
0
        public void Finalize_Invoke_DoesNotCallDisposedEvent()
        {
            var component = new SubComponent();

            component.Disposed += Component_Disposed;

            MethodInfo method = typeof(Component).GetMethod("Finalize", BindingFlags.NonPublic | BindingFlags.Instance);

            Assert.NotNull(method);
            method.Invoke(component, null);
            Assert.False(InvokedDisposed);
        }
Exemplo n.º 24
0
        /// <summary>
        /// Create an instance of the indicated component with the given extra parameters.
        /// </summary>
        public static TRes CreateInstance <TRes, TSig>(IHostEnvironment env, SubComponent comp, params object[] extra)
            where TRes : class
        {
            string options = CmdParser.CombineSettings(comp.Settings);
            TRes   result;

            if (TryCreateInstance <TRes, TSig>(env, out result, comp.Kind, options, extra))
            {
                return(result);
            }
            throw Contracts.Except("Unknown loadable class: {0}", comp.Kind).MarkSensitive(MessageSensitivity.None);
        }
Exemplo n.º 25
0
        public void Dispose_NotDisposing_DoesNotInvokeEvent()
        {
            var component = new SubComponent();

            component.Disposed += Component_Disposed;

            component.DisposeInternal(false);
            Assert.False(InvokedDisposed);

            component.DisposeInternal(true);
            Assert.True(InvokedDisposed);
        }
Exemplo n.º 26
0
        private static bool TryCreateBindableFromScorer(IHostEnvironment env, IPredictor predictor,
                                                        SubComponent <IDataScorerTransform, SignatureDataScorer> scorerSettings, out ISchemaBindableMapper bindable)
        {
            Contracts.AssertValue(env);
            env.AssertValue(predictor);
            env.Assert(scorerSettings.IsGood());

            // Try to find a mapper factory method with the same loadname as the scorer settings.
            var mapperComponent = new SubComponent <ISchemaBindableMapper, SignatureBindableMapper>(scorerSettings.Kind, scorerSettings.Settings);

            return(ComponentCatalog.TryCreateInstance(env, out bindable, mapperComponent, predictor));
        }
Exemplo n.º 27
0
        public TrainCommand(IHostEnvironment env, Arguments args)
            : base(env, args, nameof(TrainCommand))
        {
            Host.CheckNonWhiteSpace(args.OutputModelFile, nameof(args.OutputModelFile));
            _info    = TrainUtils.CheckTrainer(Host, args.Trainer, args.DataFile);
            _trainer = args.Trainer;

            _labelColumn   = args.LabelColumn;
            _featureColumn = args.FeatureColumn;
            _groupColumn   = args.GroupColumn;
            _weightColumn  = args.WeightColumn;
            _nameColumn    = args.NameColumn;
        }
Exemplo n.º 28
0
        public void DesignMode_GetWithSite_ReturnsExpected(bool result)
        {
            var site = new MockSite
            {
                DesignMode = result
            };
            var component = new SubComponent
            {
                Site = site
            };

            Assert.Equal(result, component.DesignMode);
        }
Exemplo n.º 29
0
        public void GetService_InvokeWithSite_ReturnsExpected(Type serviceType, Type result)
        {
            var site = new MockSite
            {
                ServiceType = result
            };
            var component = new SubComponent
            {
                Site = site
            };

            Assert.Same(result, component.GetService(serviceType));
        }
Exemplo n.º 30
0
        public void Finalize_Invoke_DoesNotCallDisposedEvent()
        {
            var component = new SubComponent();
            int callCount = 0;

            component.Disposed += (sender, e) => callCount++;

            MethodInfo method = typeof(Component).GetMethod("Finalize", BindingFlags.NonPublic | BindingFlags.Instance);

            Assert.NotNull(method);
            method.Invoke(component, null);
            Assert.Equal(0, callCount);
        }