Пример #1
0
        void FileBasedSavingOfData()
        {
            var ml        = new MLContext(seed: 1, conc: 1);
            var src       = new MultiFileSource(GetDataPath(TestDatasets.Sentiment.trainFilename));
            var trainData = ml.Data.CreateTextLoader(TestDatasets.Sentiment.GetLoaderColumns(), hasHeader: true)
                            .Append(ml.Transforms.Text.FeaturizeText("SentimentText", "Features"))
                            .Fit(src).Read(src);

            var path = DeleteOutputPath("i.idv");

            using (var file = File.Create(path))
            {
                var saver = new BinarySaver(ml, new BinarySaver.Arguments());
                using (var ch = ((IHostEnvironment)ml).Start("SaveData"))
                    DataSaverUtils.SaveDataView(ch, saver, trainData, file);
            }

            var trainer = ml.BinaryClassification.Trainers.StochasticDualCoordinateAscent(
                new SdcaBinaryTrainer.Options {
                NumThreads = 1
            });
            var loadedTrainData = new BinaryLoader(ml, new BinaryLoader.Arguments(), new MultiFileSource(path));

            // Train.
            var model = trainer.Fit(loadedTrainData);
        }
Пример #2
0
        private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
        {
            if (Environment.GetCommandLineArgs().Length != 1)
            {
                ILoader   loader    = new BinaryLoader();
                Container container = null;

                try
                {
                    container = loader.Load(Environment.GetCommandLineArgs()[1]);
                }
                catch (Exception)
                {
                    this.ShowMessageAsync("Impossible de lire le fichier selectionner",
                                          Environment.GetCommandLineArgs()[1]);
                }

                if (container != null)
                {
                    MainControl.Content = new GameModeSelection(container, this);
                }
                else
                {
                    MainControl.Content = new Home(this);
                }
            }
            else
            {
                MainControl.Content = new Home(this);
            }
        }
Пример #3
0
        public void TestReadMapping()
        {
            string rawString = BinaryLoader.LoadString(prefab_mapping_path);
            Dictionary <int, string> mapping = BinaryLoader.ParseMapping(rawString);

            Assert.IsTrue(mapping.Count > 0);
        }
Пример #4
0
        public void TestDirectRead()
        {
            int[] ss  = BinaryLoader.LoadDescriptions(map_desc_path);
            int   len = ss.Length;

            for (int i = 0; i < len; ++i)
            {
                Assert.IsTrue(ss[i] >= 0);
            }
            Assert.AreEqual(999, ss[0]);
            Assert.AreEqual(999, ss[1]);
            Assert.AreEqual(2 + 999 * 999, len);

            MapCellProto mcp0 = BinaryLoader.ParseCellProto(ss[2]);

            Assert.AreEqual(0, mcp0.area);
            Assert.AreEqual(3, mcp0.terrainType);
            Assert.AreEqual(2, mcp0.terrainLevel);

            MapCellProto mcp1 = BinaryLoader.ParseCellProto(ss[ss.Length - 1]);

            Assert.AreEqual(3, mcp1.area);
            Assert.AreEqual(1, mcp1.terrainType);
            Assert.AreEqual(1, mcp1.terrainLevel);
        }
        /// <summary>
        /// Loads and returns the loader and transforms from the specified repository reader.
        /// </summary>
        /// <param name="env">The host environment to use.</param>
        /// <param name="rep">The repository reader.</param>
        /// <param name="files">The data source to initialize the loader with.</param>
        /// <param name="extractInnerPipe">Whether to extract the transforms and loader from the wrapped CompositeDataLoader.</param>
        /// <returns>The created data view.</returns>
        public static IDataView LoadPipeline(IHostEnvironment env, RepositoryReader rep, IMultiStreamSource files, bool extractInnerPipe = false)
        {
            // REVIEW: Should not duplicate loading loader/transforms code. This method should call LoadLoader.
            Contracts.CheckValue(env, nameof(env));
            env.CheckValue(rep, nameof(rep));
            env.CheckValue(files, nameof(files));

            var entry = rep.OpenEntryOrNull(SchemaEntryName);

            if (entry != null)
            {
                var loader = new BinaryLoader(env, new BinaryLoader.Arguments(), entry.Stream);
                ModelLoadContext.LoadModel <ITransformer, SignatureLoadModel>(env, out var transformerChain, rep, DirTransformerChain);
                return(transformerChain.Transform(loader));
            }

            using (var ent = rep.OpenEntry(DirDataLoaderModel, ModelLoadContext.ModelStreamName))
            {
                ILegacyDataLoader loader;
                env.Assert(ent.Stream.Position == 0);
                ModelLoadContext.LoadModel <ILegacyDataLoader, SignatureLoadDataLoader>(env, out loader, rep, ent, DirDataLoaderModel, files);
                IDataView result = loader;
                if (extractInnerPipe)
                {
                    var cdl = loader as LegacyCompositeDataLoader;
                    result = cdl == null ? loader : cdl.View;
                }

                return(result);
            }
        }
Пример #6
0
        void New_FileBasedSavingOfData()
        {
            var dataPath     = GetDataPath(SentimentDataPath);
            var testDataPath = GetDataPath(SentimentTestPath);

            using (var env = new TlcEnvironment(seed: 1, conc: 1))
            {
                // Pipeline.
                var pipeline = new MyTextLoader(env, MakeSentimentTextLoaderArgs())
                               .Append(new MyTextTransform(env, MakeSentimentTextTransformArgs()));

                var trainData = pipeline.Fit(new MultiFileSource(dataPath)).Read(new MultiFileSource(dataPath));

                using (var file = env.CreateOutputFile("i.idv"))
                    trainData.SaveAsBinary(env, file.CreateWriteStream());

                var trainer = new MySdca(env, new LinearClassificationTrainer.Arguments {
                    NumThreads = 1
                }, "Features", "Label");
                var loadedTrainData = new BinaryLoader(env, new BinaryLoader.Arguments(), new MultiFileSource("i.idv"));

                // Train.
                var model = trainer.Train(loadedTrainData);
                DeleteOutputPath("i.idv");
            }
        }
Пример #7
0
    void UpdatePatternData()
    {
        currentPatternData = BinaryLoader.LoadPatternData(currentSpellcard, currentSave);

        //if the file does not exists:
        if (currentPatternData == null)
        {
            currentPatternData      = new PatternData();
            currentPatternData.name = "Spellcard name";
        }

        for (int i = 0; i < previewParticleSystems.Length; i++)
        {
            var emission = previewParticleSystems[i].emission;

            if (i < currentPatternData.particlePatterns.Count)
            {
                ParticleSystemScript.SetPSFromData(previewParticleSystems[i], currentPatternData.particlePatterns[i]);
                emission.enabled = true;
            }
            else
            {
                emission.enabled = false;
            }
        }

        spellcardName.text = currentPatternData.name;
    }
        void ICanSaveModel.Save(ModelSaveContext ctx)
        {
            if (!_allowSave)
            {
                throw _host.Except("Saving is not permitted.");
            }
            ctx.CheckAtModel();
            ctx.SetVersionInfo(GetVersionInfo());

            var dataPipe   = _xf;
            var transforms = new List <IDataTransform>();

            while (dataPipe is IDataTransform xf)
            {
                // REVIEW: a malicious user could construct a loop in the Source chain, that would
                // cause this method to iterate forever (and throw something when the list overflows). There's
                // no way to insulate from ALL malicious behavior.
                transforms.Add(xf);
                dataPipe = xf.Source;
                Contracts.AssertValue(dataPipe);
            }
            transforms.Reverse();

            ctx.SaveSubModel("Loader", c => BinaryLoader.SaveInstance(_host, c, dataPipe.Schema));

            ctx.Writer.Write(transforms.Count);
            for (int i = 0; i < transforms.Count; i++)
            {
                var dirName = string.Format(TransformDirTemplate, i);
                ctx.SaveModel(transforms[i], dirName);
            }
        }
Пример #9
0
        private static IDataLoader LoadStopwords(IHostEnvironment env, IChannel ch, string dataFile,
                                                 IComponentFactory <IMultiStreamSource, IDataLoader> loader, ref string stopwordsCol)
        {
            Contracts.CheckValue(env, nameof(env));
            env.CheckValue(ch, nameof(ch));

            MultiFileSource fileSource = new MultiFileSource(dataFile);
            IDataLoader     dataLoader;

            // First column using the file.
            if (loader == null)
            {
                // Determine the default loader from the extension.
                var  ext         = Path.GetExtension(dataFile);
                bool isBinary    = string.Equals(ext, ".idv", StringComparison.OrdinalIgnoreCase);
                bool isTranspose = string.Equals(ext, ".tdv", StringComparison.OrdinalIgnoreCase);
                if (isBinary || isTranspose)
                {
                    ch.Assert(isBinary != isTranspose);
                    ch.CheckUserArg(!string.IsNullOrWhiteSpace(stopwordsCol), nameof(Arguments.StopwordsColumn),
                                    "stopwordsColumn should be specified");
                    if (isBinary)
                    {
                        dataLoader = new BinaryLoader(env, new BinaryLoader.Arguments(), fileSource);
                    }
                    else
                    {
                        ch.Assert(isTranspose);
                        dataLoader = new TransposeLoader(env, new TransposeLoader.Arguments(), fileSource);
                    }
                }
                else
                {
                    if (!string.IsNullOrWhiteSpace(stopwordsCol))
                    {
                        ch.Warning("{0} should not be specified when default loader is TextLoader. Ignoring stopwordsColumn={0}",
                                   stopwordsCol);
                    }
                    dataLoader = TextLoader.Create(
                        env,
                        new TextLoader.Arguments()
                    {
                        Separator = "tab",
                        Column    = new[]
                        {
                            new TextLoader.Column("Stopwords", DataKind.TX, 0)
                        }
                    },
                        fileSource);
                    stopwordsCol = "Stopwords";
                }
                ch.AssertNonEmpty(stopwordsCol);
            }
            else
            {
                dataLoader = loader.CreateComponent(env, fileSource);
            }

            return(dataLoader);
        }
Пример #10
0
        public void SetUp()
        {
            searchLog = new StringWriter();

            mockTarget            = Substitute.For <RemoteTarget>();
            moduleReplacedHandler = Substitute.For <EventHandler <LldbModuleReplacedEventArgs> >();

            mockModuleFileFinder = Substitute.For <IModuleFileFinder>();
            mockModuleFileFinder.FindFileAsync(BINARY_FILENAME, UUID, false, searchLog)
            .Returns(Task.FromResult(PATH_IN_STORE));

            placeholderModule = Substitute.For <SbModule>();
            placeholderModule.GetPlatformFileSpec().GetFilename().Returns(BINARY_FILENAME);
            placeholderModule.GetUUIDString().Returns(UUID.ToString());

            placeholderProperties =
                new PlaceholderModuleProperties(MODULE_SLIDE, Substitute.For <SbFileSpec>());

            mockModuleUtil = Substitute.For <ILldbModuleUtil>();
            mockModuleUtil.IsPlaceholderModule(placeholderModule).Returns(true);
            mockModuleUtil.GetPlaceholderProperties(Arg.Any <SbModule>(), Arg.Any <RemoteTarget>())
            .ReturnsForAnyArgs(placeholderProperties);
            mockModuleUtil.ApplyPlaceholderProperties(
                Arg.Any <SbModule>(), Arg.Any <PlaceholderModuleProperties>(),
                Arg.Any <RemoteTarget>())
            .ReturnsForAnyArgs(true);


            binaryLoader = new BinaryLoader(mockModuleUtil, mockModuleFileFinder,
                                            mockTarget);
            binaryLoader.LldbModuleReplaced += moduleReplacedHandler;
        }
Пример #11
0
    public static BaseLoaderElement CreateLoader(EnumResouceType type, string path)
    {
        BaseLoaderElement loader;

        switch (type)
        {
        case EnumResouceType.CACHE_BINARY:
            loader = new CacheBinaryLoader(path);
            break;

        case EnumResouceType.BINARY:
            loader = new BinaryLoader(path);
            break;

        case EnumResouceType.REMOTE_BINARY:
            loader = new RemoteBinaryLoader(path);
            break;

        case EnumResouceType.ASSETBUNDLE:
            loader = new ABLoader(path);
            break;

        default:
            loader = new BinaryLoader(path);
            break;
        }
        return(loader);
    }
Пример #12
0
        /// <summary>
        /// Save the data pipeline defined by dataPipe. If blankLoader is true or the root IDataView is not an IDataLoader,
        /// this persists the root as a BinaryLoader having the same schema.
        /// </summary>
        public static void SaveDataPipe(IHostEnvironment env, RepositoryWriter repositoryWriter, IDataView dataPipe, bool blankLoader = false)
        {
            Contracts.CheckValue(env, nameof(env));
            env.CheckValue(repositoryWriter, nameof(repositoryWriter));
            env.CheckValue(dataPipe, nameof(dataPipe));

            IDataView pipeStart;
            var       xfs = BacktrackPipe(dataPipe, out pipeStart);

            Action <ModelSaveContext> saveAction;

            if (!blankLoader && pipeStart is IDataLoader loader)
            {
                saveAction = loader.Save;
            }
            else
            {
                // The serialized pipe must start with a loader. If the original data view is not a loader,
                // we replace it with a binary loader with the correct schema.
                saveAction = ctx => BinaryLoader.SaveInstance(env, ctx, pipeStart.Schema);
            }

            using (var ctx = ModelFileUtils.GetDataModelSavingContext(repositoryWriter))
            {
                CompositeDataLoader.SavePipe(env, ctx, saveAction, xfs);
                ctx.Done();
            }
        }
        void FileBasedSavingOfData()
        {
            var dataPath     = GetDataPath(SentimentDataPath);
            var testDataPath = GetDataPath(SentimentTestPath);

            using (var env = new LocalEnvironment(seed: 1, conc: 1))
            {
                // Pipeline
                var loader = TextLoader.ReadFile(env, MakeSentimentTextLoaderArgs(), new MultiFileSource(dataPath));

                var trans = TextTransform.Create(env, MakeSentimentTextTransformArgs(), loader);
                var saver = new BinarySaver(env, new BinarySaver.Arguments());
                using (var ch = env.Start("SaveData"))
                    using (var file = env.CreateOutputFile("i.idv"))
                    {
                        DataSaverUtils.SaveDataView(ch, saver, trans, file);
                    }

                var binData    = new BinaryLoader(env, new BinaryLoader.Arguments(), new MultiFileSource("i.idv"));
                var trainRoles = new RoleMappedData(binData, label: "Label", feature: "Features");
                var trainer    = new LinearClassificationTrainer(env, new LinearClassificationTrainer.Arguments
                {
                    NumThreads = 1
                });
                var predictor = trainer.Train(new Runtime.TrainContext(trainRoles));

                DeleteOutputPath("i.idv");
            }
        }
Пример #14
0
            public void Save(ModelSaveContext ctx)
            {
                _host.CheckValue(ctx, nameof(ctx));
                ctx.CheckAtModel();
                ctx.SetVersionInfo(GetVersionInfo());

                var dataPipe   = _xf;
                var transforms = new List <IDataTransform>();

                while (dataPipe is IDataTransform xf)
                {
                    transforms.Add(xf);
                    dataPipe = xf.Source;
                    Contracts.AssertValue(dataPipe);
                }
                transforms.Reverse();

                ctx.SaveSubModel("Loader", c => BinaryLoader.SaveInstance(_host, c, dataPipe.Schema));

                ctx.Writer.Write(transforms.Count);
                for (int i = 0; i < transforms.Count; i++)
                {
                    var dirName = string.Format(TransformDirTemplate, i);
                    ctx.SaveModel(transforms[i], dirName);
                }
            }
Пример #15
0
        public static TransposeLoader Create(IHostEnvironment env, ModelLoadContext ctx, IMultiStreamSource files)
        {
            Contracts.CheckValue(env, nameof(env));
            IHost h = env.Register(LoadName);

            h.CheckValue(ctx, nameof(ctx));
            ctx.CheckAtModel(GetVersionInfo());
            h.CheckValue(files, nameof(files));

            return(h.Apply("Loading Model",
                           ch =>
            {
                if (files.Count == 0)
                {
                    BinaryLoader schemaView = null;
                    // In the case where we have no input streams, but we have an input schema from
                    // the model repository, we still want to surface ourselves as being a binary loader
                    // with the existing schema. The loader "owns" this stream.
                    if (ctx.TryLoadBinaryStream("Schema.idv",
                                                r => schemaView = new BinaryLoader(h, new BinaryLoader.Arguments(),
                                                                                   HybridMemoryStream.CreateCache(r.BaseStream), leaveOpen: false)))
                    {
                        h.AssertValue(schemaView);
                        h.CheckDecode(schemaView.GetRowCount() == 0);
                        // REVIEW: Do we want to be a bit more restrictive around uninterpretable columns?
                        return new TransposeLoader(h, ctx, schemaView);
                    }
                    h.Assert(schemaView == null);
                    // Fall through, allow the failure to be on OpenStream.
                }
                return new TransposeLoader(h, ctx, files);
            }));
        }
Пример #16
0
        private void UseSaveButton_OnClick(object sender, RoutedEventArgs e)
        {
            ILoader loader = new BinaryLoader();

            const string directorySaveName = "Save";
            var          fullSavePath      = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\" +
                                             directorySaveName;

            if (!Directory.Exists(fullSavePath))
            {
                Directory.CreateDirectory(fullSavePath);
            }

            var openFileDialog = new OpenFileDialog
            {
                Filter           = loader.Filter(),
                InitialDirectory = fullSavePath
            };

            if (openFileDialog.ShowDialog() != true)
            {
                return;
            }

            var container = loader.Load(openFileDialog.FileName);

            _mainWindow.MainControl.Content = new Home(_mainWindow, container);
        }
Пример #17
0
        internal PredictionTransformerBase(IHost host, ModelLoadContext ctx)
        {
            Host = host;

            ctx.LoadModel <TModel, SignatureLoadModel>(host, out TModel model, DirModel);
            Model = model;

            // *** Binary format ***
            // model: prediction model.
            // stream: empty data view that contains train schema.
            // id of string: feature column.

            // Clone the stream with the schema into memory.
            var ms = new MemoryStream();

            ctx.TryLoadBinaryStream(DirTransSchema, reader =>
            {
                reader.BaseStream.CopyTo(ms);
            });

            ms.Position = 0;
            var loader = new BinaryLoader(host, new BinaryLoader.Arguments(), ms);

            TrainSchema = loader.Schema;

            FeatureColumn = ctx.LoadString();
            if (!TrainSchema.TryGetColumnIndex(FeatureColumn, out int col))
            {
                throw Host.ExceptSchemaMismatch(nameof(FeatureColumn), RoleMappedSchema.ColumnRole.Feature.Value, FeatureColumn);
            }
            FeatureColumnType = TrainSchema.GetColumnType(col);

            BindableMapper = ScoreUtils.GetSchemaBindableMapper(Host, model);
        }
Пример #18
0
        protected PredictionTransformerBase(IHost host, ModelLoadContext ctx)
        {
            Host = host;

            // *** Binary format ***
            // model: prediction model.
            // stream: empty data view that contains train schema.
            // id of string: feature column.

            ctx.LoadModel <TModel, SignatureLoadModel>(host, out TModel model, DirModel);
            Model = model;

            // Clone the stream with the schema into memory.
            var ms = new MemoryStream();

            ctx.TryLoadBinaryStream(DirTransSchema, reader =>
            {
                reader.BaseStream.CopyTo(ms);
            });

            ms.Position = 0;
            var loader = new BinaryLoader(host, new BinaryLoader.Arguments(), ms);

            TrainSchema = loader.Schema;
        }
Пример #19
0
    public void SaveCurrentPattern()
    {
        if (currentPatternData == null)
        {
            return;
        }

        float duration = 0;

        //compute the duration of the parttern:
        foreach (var particleSystemData in currentPatternData.particlePatterns)
        {
            duration = Mathf.Max(duration, particleSystemData.startDelay + particleSystemData.duration);
        }

        duration = Mathf.Clamp(duration, .5f, 60);

        currentPatternData.duration = duration / 2;
        currentPatternData.cooldown = duration;

        Debug.Log("duration = " + duration);
        Debug.Log("saving " + currentPatternData.name);

        BinaryLoader.SavePatternData(currentPatternData, currentSpellcard, currentSave);

        UpdatePatternData();
    }
        private TermLookupTransform(IChannel ch, ModelLoadContext ctx, IHost host, IDataView input)
            : base(host, ctx, input, TestIsText)
        {
            Host.AssertValue(ch);

            // *** Binary format ***
            // <base>
            ch.AssertNonEmpty(Infos);

            // Extra streams:
            // DefaultMap.idv
            byte[] rgb = null;
            Action <BinaryReader> fn = r => rgb = ReadAllBytes(ch, r);

            if (!ctx.TryLoadBinaryStream(DefaultMapName, fn))
            {
                throw ch.ExceptDecode();
            }
            _bytes = rgb;

            // Process the bytes into the loader and map.
            _ldr = GetLoader(Host, _bytes);
            ValidateLoader(ch, _ldr);
            _valueMap = Train(ch, _ldr);
            SetMetadata();
        }
Пример #21
0
 /// <summary>
 /// Gets the dataview corresponding to this sub-IDV entry. This will
 /// lazily load the file, if it has not previously been requested. This
 /// will return <c>null</c> if the offset is 0.
 /// </summary>
 public IDataView GetViewOrNull()
 {
     if (_view == null && _offset > 0)
     {
         Stream stream = _parent._file.Open(0);
         stream.Seek(_offset, SeekOrigin.Begin);
         Contracts.Check(stream.Position == _offset, "Unexpected position on substream");
         SubsetStream ss      = new SubsetStream(stream, _length);
         var          binArgs = new BinaryLoader.Arguments();
         if (_parent._threads > 0)
         {
             binArgs.Threads = _parent._threads;
         }
         BinaryLoader loader = new BinaryLoader(Host,
                                                binArgs, ss, leaveOpen: false);
         var view = Interlocked.CompareExchange(ref _view, loader, null);
         // If multiple threads have called this as it was being loaded,
         // have ensure that this check only happens once.
         if (view == loader)
         {
             VerifyView(view);
         }
     }
     return(_view);
 }
Пример #22
0
        private PartitionedFileLoader(IHost host, ModelLoadContext ctx, IMultiStreamSource files)
        {
            Contracts.AssertValue(host);
            _host = host;
            _host.AssertValue(ctx);
            _host.AssertValue(files);

            // ** Binary format **
            // int: tailing directory count
            // Schema of the loader
            // int[]: srcColumns
            // byte[]: subloader
            // model: file path spec

            _tailingDirCount = ctx.Reader.ReadInt32();

            // Load the schema
            byte[] buffer = null;
            if (!ctx.TryLoadBinaryStream(SchemaCtxName, r => buffer = r.ReadByteArray()))
            {
                throw _host.ExceptDecode();
            }
            BinaryLoader loader = null;
            var          strm   = new MemoryStream(buffer, writable: false);

            loader = new BinaryLoader(_host, new BinaryLoader.Arguments(), strm);
            Schema = loader.Schema;

            _srcDirIndex    = ctx.Reader.ReadIntArray();
            _subLoaderBytes = ctx.Reader.ReadByteArray();

            ctx.LoadModel <IPartitionedPathParser, SignatureLoadModel>(_host, out _pathParser, FilePathSpecCtxName);

            _files = files;
        }
Пример #23
0
        private void loadBinariesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BinaryLoader makeBinaries = new BinaryLoader();

            this.Cursor = Cursors.WaitCursor;
            makeBinaries.CreateDLLs();
            this.Cursor = Cursors.Arrow;
        }
 private static void ValidateLoader(IExceptionContext ectx, BinaryLoader ldr)
 {
     if (ldr == null)
     {
         return;
     }
     ectx.CheckDecode(ldr.Schema.ColumnCount == 2);
     ectx.CheckDecode(ldr.Schema.GetColumnType(0).IsText);
 }
Пример #25
0
 public void LoadBinary(string fileName, out ushort loadedAtAddress, ushort?forceLoadAddress = null)
 {
     BinaryLoader.Load(
         Mem,
         fileName,
         out loadedAtAddress,
         out int _,
         forceLoadAddress);
 }
Пример #26
0
        public static CacheOutput CacheData(IHostEnvironment env, CacheInput input)
        {
            const string registrationName = "CreateCache";

            Contracts.CheckValue(env, nameof(env));
            var host = env.Register(registrationName);

            host.CheckValue(input, nameof(input));
            host.CheckValue(input.Data, nameof(input.Data));

            IDataView data;

            switch (input.Caching)
            {
            case CachingType.Memory:
                data = new CacheDataView(env, input.Data, null);
                break;

            case CachingType.Disk:
                var args = new BinarySaver.Arguments();
                args.Compression = CompressionKind.Default;
                args.Silent      = true;

                var saver  = new BinarySaver(host, args);
                var schema = input.Data.Schema;

                var cols = new List <int>();
                for (int i = 0; i < schema.ColumnCount; i++)
                {
                    var type = schema.GetColumnType(i);
                    if (saver.IsColumnSavable(type))
                    {
                        cols.Add(i);
                    }
                }

#pragma warning disable CS0618 // This ought to be addressed. See #1287.
                // We are not disposing the fileHandle because we want it to stay around for the execution of the graph.
                // It will be disposed when the environment is disposed.
                var fileHandle = host.CreateTempFile();
#pragma warning restore CS0618

                using (var stream = fileHandle.CreateWriteStream())
                    saver.SaveData(stream, input.Data, cols.ToArray());
                data = new BinaryLoader(host, new BinaryLoader.Arguments(), fileHandle.OpenReadStream());
                break;

            default:
                throw host.ExceptValue(nameof(input.Caching), $"Unrecognized caching option '{input.Caching}'");
            }

            return(new CacheOutput()
            {
                OutputData = data
            });
        }
        /// <summary>
        /// Read a data view from a Stream on a binary file using <see cref="BinaryLoader"/>.
        /// </summary>
        /// <param name="catalog">The catalog.</param>
        /// <param name="stream">The stream to read from.</param>
        public static IDataView ReadFromBinary(this DataOperations catalog, Stream stream)
        {
            Contracts.CheckValue(stream, nameof(stream));

            var env = catalog.GetEnvironment();

            var reader = new BinaryLoader(env, new BinaryLoader.Arguments(), stream);

            return(reader);
        }
Пример #28
0
        public static DevImage ReadImage(BinaryLoader reader)
        {
            int width = reader.ReadInt16();
            int height = reader.ReadInt16();
            int bpp = reader.ReadInt16();

            byte[] buffer = reader.ReadBytes(width * height * bpp);

            return new DevImage(width, height, bpp, buffer);
        }
Пример #29
0
        /// <summary>
        /// Read a data view from an <see cref="IMultiStreamSource"/> on a binary file using <see cref="BinaryLoader"/>.
        /// </summary>
        /// <param name="catalog">The catalog.</param>
        /// <param name="fileSource">The file source to read from. This can be a <see cref="MultiFileSource"/>, for example.</param>
        public static IDataView ReadFromBinary(this DataOperationsCatalog catalog, IMultiStreamSource fileSource)
        {
            Contracts.CheckValue(fileSource, nameof(fileSource));

            var env = catalog.GetEnvironment();

            var reader = new BinaryLoader(env, new BinaryLoader.Arguments(), fileSource);

            return(reader);
        }
Пример #30
0
        /// <summary>
        /// Read a data view from a binary file using <see cref="BinaryLoader"/>.
        /// </summary>
        /// <param name="catalog">The catalog.</param>
        /// <param name="path">The path to the file to read from.</param>
        public static IDataView ReadFromBinary(this DataOperationsCatalog catalog, string path)
        {
            Contracts.CheckNonEmpty(path, nameof(path));

            var env = catalog.GetEnvironment();

            var reader = new BinaryLoader(env, new BinaryLoader.Arguments(), path);

            return(reader);
        }
Пример #31
0
        private Computer SetupEmulator(EmulatorConfig emulatorConfig)
        {
            Debug.WriteLine($"Loading 6502 machine code binary file.");
            Debug.WriteLine($"{emulatorConfig.ProgramBinaryFile}");
            if (!File.Exists(emulatorConfig.ProgramBinaryFile))
            {
                Debug.WriteLine($"File does not exist.");
                throw new Exception($"Cannot find 6502 binary file: {emulatorConfig.ProgramBinaryFile}");
            }

            var enableBankSwitching = emulatorConfig.Memory.MemoryBanks.EnableMemoryBanks;
            var mem = new Memory(enableBankSwitching: enableBankSwitching);

            if (enableBankSwitching)
            {
                // Add additional memory banks for memory segment 1 (0x2000) and up (segment 0 cannot have multiple banks)
                for (byte memorySegmentNumber = 1; memorySegmentNumber < mem.MemorySegments.Count; memorySegmentNumber++)
                {
                    // By default each segment has one bank when Memory is created above.
                    // Thus we add the specified BanksPerSegment-1 new banks to each segment.
                    for (int i = 0; i < emulatorConfig.Memory.MemoryBanks.BanksPerSegment - 1; i++)
                    {
                        // Add additional memory banks for segment. Memory in those will be blank (0x00).
                        mem.AddMemorySegmentBank(memorySegmentNumber);
                    }
                }
            }

            BinaryLoader.Load(
                mem,
                emulatorConfig.ProgramBinaryFile,
                out ushort loadedAtAddress,
                out int fileLength);

            // Initialize emulator with CPU, memory, and execution parameters
            var computerBuilder = new ComputerBuilder();

            computerBuilder
            .WithCPU()
            .WithStartAddress(loadedAtAddress)
            .WithMemory(mem)
            .WithExecOptions(options =>
            {
                // Emulator will stop executing when a BRK instruction is reached.
                options.ExecuteUntilInstruction = emulatorConfig.StopAtBRK?OpCodeId.BRK:null;
            });

            var computer = computerBuilder.Build();

            return(computer);
        }