Ejemplo n.º 1
0
        public void Run()
        {
            JObject graph;

            try
            {
                graph = JObject.Parse(File.ReadAllText(_path));
            }
            catch (JsonReaderException ex)
            {
                throw _host.Except(ex, "Failed to parse experiment graph: {0}", ex.Message);
            }

            var runner = new GraphRunner(_host, graph[FieldNames.Nodes] as JArray);

            // Setting inputs.
            var jInputs = graph[FieldNames.Inputs] as JObject;

            if (graph[FieldNames.Inputs] != null && jInputs == null)
            {
                throw _host.Except("Unexpected value for '{0}': {1}", FieldNames.Inputs, graph[FieldNames.Inputs]);
            }
            if (jInputs != null)
            {
                foreach (var kvp in jInputs)
                {
                    var path = kvp.Value as JValue;
                    if (path == null)
                    {
                        throw _host.Except("Invalid value for input: {0}", kvp.Value);
                    }
                    var varName = kvp.Key;
                    var type    = runner.GetPortDataKind(varName);

                    SetInputFromPath(runner, varName, path.Value <string>(), type);
                }
            }

            runner.RunAll();

            // Reading outputs.
            var jOutputs = graph[FieldNames.Outputs] as JObject;

            if (jOutputs != null)
            {
                foreach (var kvp in jOutputs)
                {
                    var path = kvp.Value as JValue;
                    if (path == null)
                    {
                        throw _host.Except("Invalid value for output: {0}", kvp.Value);
                    }
                    var varName = kvp.Key;
                    var type    = runner.GetPortDataKind(varName);

                    GetOutputToPath(runner, varName, path.Value <string>(), type);
                }
            }
        }
Ejemplo n.º 2
0
        public void SetInputFromPath(GraphRunner runner, string varName, string path, TlcModule.DataKind kind)
        {
            _host.CheckUserArg(runner != null, nameof(runner), "Provide a GraphRunner instance.");
            _host.CheckUserArg(!string.IsNullOrWhiteSpace(varName), nameof(varName), "Specify a graph variable name.");
            _host.CheckUserArg(!string.IsNullOrWhiteSpace(path), nameof(path), "Specify a valid file path.");

            switch (kind)
            {
            case TlcModule.DataKind.FileHandle:
                var fh = new SimpleFileHandle(_host, path, false, false);
                runner.SetInput(varName, fh);
                break;

            case TlcModule.DataKind.DataView:
                IDataView loader = new BinaryLoader(_host, new BinaryLoader.Arguments(), path);
                runner.SetInput(varName, loader);
                break;

            case TlcModule.DataKind.PredictorModel:
                PredictorModelImpl pm;
                using (var fs = File.OpenRead(path))
                    pm = new PredictorModelImpl(_host, fs);
                runner.SetInput(varName, pm);
                break;

            case TlcModule.DataKind.TransformModel:
                TransformModelImpl tm;
                using (var fs = File.OpenRead(path))
                    tm = new TransformModelImpl(_host, fs);
                runner.SetInput(varName, tm);
                break;

            default:
                throw _host.Except("Port type {0} not supported", kind);
            }
        }
Ejemplo n.º 3
0
        public void GetOutputToPath(GraphRunner runner, string varName, string path, TlcModule.DataKind kind)
        {
            _host.CheckUserArg(runner != null, nameof(runner), "Provide a GraphRunner instance.");
            _host.CheckUserArg(!string.IsNullOrWhiteSpace(varName), nameof(varName), "Specify a graph variable name.");
            _host.CheckUserArg(!string.IsNullOrWhiteSpace(path), nameof(path), "Specify a valid file path.");

            string extension = Path.GetExtension(path);

            switch (kind)
            {
            case TlcModule.DataKind.FileHandle:
                var fh = runner.GetOutput <IFileHandle>(varName);
                throw _host.ExceptNotSupp("File handle outputs not yet supported.");

            case TlcModule.DataKind.DataView:
                var idv = runner.GetOutput <IDataView>(varName);
                if (idv != null)
                {
                    SaveDataView(idv, path, extension);
                }
                else
                {
                    using (var ch = _host.Start("Get outputs from executed graph"))
                    {
                        string msg = string.Format("Ignoring empty graph output (output name: {0}, type: {1}, expected output's file: {2})",
                                                   varName, nameof(idv), path + extension);
                        ch.Warning(msg);
                    }
                }
                break;

            case TlcModule.DataKind.PredictorModel:
                var pm = runner.GetOutput <PredictorModel>(varName);
                SavePredictorModel(pm, path);
                break;

            case TlcModule.DataKind.TransformModel:
                var tm = runner.GetOutput <TransformModel>(varName);
                using (var handle = _host.CreateOutputFile(path))
                    using (var fs = handle.CreateWriteStream())
                        tm.Save(_host, fs);
                break;

            case TlcModule.DataKind.Array:
                string partialPath = path.Substring(0, path.Length - extension.Length);

                var ipmArray = runner.GetOutputOrDefault <PredictorModel[]>(varName);
                if (ipmArray != null && !ipmArray.GetType().IsValueType)
                {
                    SaveArrayToFile(ipmArray, partialPath, extension);
                    break;
                }

                var idvArray = runner.GetOutputOrDefault <IDataView[]>(varName);
                if (idvArray != null && !idvArray.GetType().IsValueType)
                {
                    SaveArrayToFile(idvArray, partialPath, extension);
                    break;
                }
                goto default;

            default:
                throw _host.Except("Port type {0} not supported", kind);
            }
        }