示例#1
0
        internal static void ConvertIPredictorModelsToArray(IHostEnvironment env, RunContext context, List <EntryPointNode> subGraphNodes,
                                                            Var <PredictorModel>[] predModelVars, string outputVarName)
        {
            var predictorArrayConverterArgs = new ArrayIPredictorModelInput();
            var inputBindingMap             = new Dictionary <string, List <ParameterBinding> >();
            var inputMap = new Dictionary <ParameterBinding, VariableBinding>();

            var argName = nameof(predictorArrayConverterArgs.Model);

            inputBindingMap.Add(argName, new List <ParameterBinding>());
            for (int i = 0; i < predModelVars.Length; i++)
            {
                var paramBinding = new ArrayIndexParameterBinding(argName, i);
                inputBindingMap[argName].Add(paramBinding);
                inputMap[paramBinding] = new SimpleVariableBinding(predModelVars[i].VarName);
            }
            var outputMap = new Dictionary <string, string>();
            var output    = new ArrayVar <PredictorModel>();

            outputMap.Add(nameof(MacroUtils.ArrayIPredictorModelOutput.OutputModel), outputVarName);
            var arrayConvertNode = EntryPointNode.Create(env, "Data.PredictorModelArrayConverter", predictorArrayConverterArgs,
                                                         context, inputBindingMap, inputMap, outputMap);

            subGraphNodes.Add(arrayConvertNode);
        }
示例#2
0
        internal static void ConvertIdataViewsToArray(IHostEnvironment env, RunContext context, List <EntryPointNode> subGraphNodes,
                                                      Var <IDataView>[] vars, string outputVarName)
        {
            var dataviewArrayConverterArgs = new ArrayIDataViewInput();
            var inputBindingMap            = new Dictionary <string, List <ParameterBinding> >();
            var inputMap = new Dictionary <ParameterBinding, VariableBinding>();

            var argName = nameof(dataviewArrayConverterArgs.Data);

            inputBindingMap.Add(argName, new List <ParameterBinding>());
            for (int i = 0; i < vars.Length; i++)
            {
                var paramBinding = new ArrayIndexParameterBinding(argName, i);
                inputBindingMap[argName].Add(paramBinding);
                inputMap[paramBinding] = new SimpleVariableBinding(vars[i].VarName);
            }
            var outputMap = new Dictionary <string, string>();

            outputMap.Add(nameof(ArrayIDataViewOutput.OutputData), outputVarName);
            var arrayConvertNode = EntryPointNode.Create(env, "Data.IDataViewArrayConverter", dataviewArrayConverterArgs,
                                                         context, inputBindingMap, inputMap, outputMap);

            subGraphNodes.Add(arrayConvertNode);
        }
示例#3
0
        /// <summary>
        /// Checks the given JSON object key-value pair is a valid EntryPoint input and
        /// extracts out any variables that need to be populated. These variables will be
        /// added to the EntryPoint context. Input parameters that are not set to variables
        /// will be immediately set using the input builder instance.
        /// </summary>
        private void CheckAndSetInputValue(KeyValuePair <string, JToken> pair)
        {
            var inputName = _inputBuilder.GetFieldNameOrNull(pair.Key);

            if (VariableBinding.IsBindingToken(pair.Value))
            {
                Type valueType = _inputBuilder.GetFieldTypeOrNull(pair.Key);
                if (valueType == null)
                {
                    throw _host.Except($"Unexpected input name: '{pair.Key}'");
                }
                if (!EntryPointVariable.IsValidType(valueType))
                {
                    throw _host.Except($"Unexpected input variable type: {valueType}");
                }

                var varBinding = VariableBinding.Create(_host, pair.Value.Value <string>());
                _context.AddInputVariable(varBinding, valueType);
                if (!_inputBindingMap.ContainsKey(inputName))
                {
                    _inputBindingMap[inputName] = new List <ParameterBinding>();
                }
                var paramBinding = new SimpleParameterBinding(inputName);
                _inputBindingMap[inputName].Add(paramBinding);
                _inputMap[paramBinding] = varBinding;
            }
            else if (pair.Value is JArray &&
                     ((JArray)pair.Value).Any(tok => VariableBinding.IsBindingToken(tok)))
            {
                // REVIEW: EntryPoint arrays and dictionaries containing
                // variables must ONLY contain variables right now.
                if (!((JArray)pair.Value).All(tok => VariableBinding.IsBindingToken(tok)))
                {
                    throw _host.Except($"Input {pair.Key} may ONLY contain variables.");
                }

                Type valueType = _inputBuilder.GetFieldTypeOrNull(pair.Key);
                if (valueType == null || !valueType.HasElementType)
                {
                    throw _host.Except($"Unexpected input name: '{pair.Key}'");
                }
                valueType = valueType.GetElementType();

                int i = 0;
                foreach (var varName in (JArray)pair.Value)
                {
                    var varBinding = VariableBinding.Create(_host, varName.Value <string>());
                    _context.AddInputVariable(varBinding, valueType);
                    if (!_inputBindingMap.ContainsKey(inputName))
                    {
                        _inputBindingMap[inputName] = new List <ParameterBinding>();
                    }
                    var paramBinding = new ArrayIndexParameterBinding(inputName, i++);
                    _inputBindingMap[inputName].Add(paramBinding);
                    _inputMap[paramBinding] = varBinding;
                }
            }
            // REVIEW: Implement support for Dictionary of variable values. We need to differentiate
            // between a Dictionary and a Component here, and likely need to support nested components
            // all of which might have variables. Our current machinery only works at the 'Node' level.
            else
            {
                // This is not a variable.
                if (!_inputBuilder.TrySetValueJson(pair.Key, pair.Value))
                {
                    throw _host.Except($"Unexpected input: '{pair.Key}'");
                }
            }
        }