Exemplo n.º 1
0
        // Factory method for SignatureLoadModel.
        internal CategoricalImputerTransformer(IHostEnvironment host, ModelLoadContext ctx) :
            base(host.Register(nameof(CategoricalImputerTransformer)))
        {
            host.Check(!CommonExtensions.OsIsCentOS7(), "CentOS7 is not supported");
            host.CheckValue(ctx, nameof(ctx));
            ctx.CheckAtModel(GetVersionInfo());
            // *** Binary format ***
            // int number of column pairs
            // for each column pair:
            //      string output column  name
            //      string input column name
            //      column type
            //      int length of c++ byte array
            //      byte array from c++

            var columnCount = ctx.Reader.ReadInt32();

            _columns = new TypedColumn[columnCount];
            for (int i = 0; i < columnCount; i++)
            {
                _columns[i] = TypedColumn.CreateTypedColumn(ctx.Reader.ReadString(), ctx.Reader.ReadString(), ctx.Reader.ReadString());

                // Load the C++ state and create the C++ transformer.
                var dataLength = ctx.Reader.ReadInt32();
                var data       = ctx.Reader.ReadByteArray(dataLength);
                _columns[i].CreateTransformerFromSavedData(data);
            }
        }
Exemplo n.º 2
0
        internal CategoricalImputerTransformer(IHostEnvironment host, IDataView input, CategoricalImputerEstimator.Column[] columns) :
            base(host.Register(nameof(CategoricalImputerEstimator)))
        {
            var schema = input.Schema;

            _columns = columns.Select(x => TypedColumn.CreateTypedColumn(x.Name, x.Source, schema[x.Source].Type.RawType.ToString())).ToArray();
            foreach (var column in _columns)
            {
                column.CreateTransformerFromEstimator(input);
            }
        }
        public void TestWrongName()
        {
            OLVColumn column = new OLVColumn();

            column.AspectName = "Photo.Unknown";

            TypedColumn <Person> tcolumn = new TypedColumn <Person>(column);

            tcolumn.GenerateAspectGetter();
            Assert.AreEqual("'Unknown' is not a parameter-less method, property or field of type 'System.String'", column.GetValue(this.person1));
        }
Exemplo n.º 4
0
        public void TestPropertyReplacedByNew()
        {
            OLVColumn column = new OLVColumn();

            column.AspectName = "CulinaryRating";

            TypedColumn <Person2> tcolumn = new TypedColumn <Person2>(column);

            Assert.IsNull(column.AspectGetter);
            tcolumn.GenerateAspectGetter();
            Assert.IsNotNull(column.AspectGetter);
            Assert.AreEqual(200, column.GetValue(this.person2));
        }
Exemplo n.º 5
0
        public void Execute <T>(string aspectName, object expectedResult, T person) where T : class
        {
            OLVColumn column = new OLVColumn();

            column.AspectName = aspectName;

            TypedColumn <T> tcolumn = new TypedColumn <T>(column);

            Assert.IsNull(column.AspectGetter);
            tcolumn.GenerateAspectGetter();
            Assert.IsNotNull(column.AspectGetter);
            Assert.AreEqual(expectedResult, column.GetValue(person));
        }
Exemplo n.º 6
0
        public Form1()
        {
            InitializeComponent();

            Shown += (sender, args) => new Form2().Show();

            _tptvFilesystem = new TypedObjectListView <FileSystemItem>(tvFilesystem);
            _tpcolName      = _tptvFilesystem.GetColumn("Name");

            SetupFsTreeView();

            UpdateControlsState();
        }
        public void ExecuteAspect(string aspectName, object expectedResult, Person person)
        {
            OLVColumn column = new OLVColumn();

            column.AspectName = aspectName;
            Assert.AreEqual(expectedResult, column.GetValue(person));

            TypedColumn <Person> tcolumn = new TypedColumn <Person>(column);

            Assert.IsNull(column.AspectGetter);
            tcolumn.GenerateAspectGetter();
            Assert.IsNotNull(column.AspectGetter);
            Assert.AreEqual(expectedResult, column.GetValue(person));
        }
Exemplo n.º 8
0
        private unsafe TransformerEstimatorSafeHandle CreateTransformerFromEstimator(IDataView input)
        {
            IntPtr estimator;
            IntPtr errorHandle;
            bool   success;

            var allColumns = input.Schema.Where(x => _allColumnNames.Contains(x.Name)).Select(x => TypedColumn.CreateTypedColumn(x, _dataColumns)).ToDictionary(x => x.Column.Name);

            // Create TypeId[] for types of grain and data columns;
            var dataColumnTypes  = new TypeId[_dataColumns.Length];
            var grainColumnTypes = new TypeId[_grainColumns.Length];

            foreach (var column in _grainColumns.Select((value, index) => new { index, value }))
            {
                grainColumnTypes[column.index] = allColumns[column.value].GetTypeId();
            }

            foreach (var column in _dataColumns.Select((value, index) => new { index, value }))
            {
                dataColumnTypes[column.index] = allColumns[column.value].GetTypeId();

                fixed(bool *suppressErrors = &_suppressTypeErrors)
                fixed(TypeId * rawDataColumnTypes  = dataColumnTypes)
                fixed(TypeId * rawGrainColumnTypes = grainColumnTypes)
                {
                    success = CreateEstimatorNative(rawGrainColumnTypes, new IntPtr(grainColumnTypes.Length), rawDataColumnTypes, new IntPtr(dataColumnTypes.Length), _imputeMode, suppressErrors, out estimator, out errorHandle);
                }
                if (!success)
                {
                    throw new Exception(GetErrorDetailsAndFreeNativeMemory(errorHandle));
                }

                using (var estimatorHandle = new TransformerEstimatorSafeHandle(estimator, DestroyEstimatorNative))
                {
                    TrainingState trainingState;
                    FitResult     fitResult;

                    // Create buffer to hold binary data
                    var memoryStream = new MemoryStream(4096);
                    var binaryWriter = new BinaryWriter(memoryStream, Encoding.UTF8);

                    // Can't use a using with this because it potentially needs to be reset. Manually disposing as needed.
                    var cursor = input.GetRowCursorForAllColumns();
                    // Initialize getters
                    foreach (var column in allColumns.Values)
                    {
                        column.InitializeGetter(cursor);
                    }

                    // Start the loop with the cursor in a valid state already.
                    var valid = cursor.MoveNext();

                    // Make sure its not an empty data frame
                    Debug.Assert(valid);
                    while (true)
                    {
                        // Get the state of the native estimator.
                        success = GetStateNative(estimatorHandle, out trainingState, out errorHandle);
                        if (!success)
                        {
                            throw new Exception(GetErrorDetailsAndFreeNativeMemory(errorHandle));
                        }

                        // If we are no longer training then exit loop.
                        if (trainingState != TrainingState.Training)
                        {
                            break;
                        }

                        // Build byte array to send column data to native featurizer
                        BuildColumnByteArray(allColumns, ref binaryWriter);

                        // Fit the estimator
                        fixed(byte *bufferPointer = memoryStream.GetBuffer())
                        {
                            var binaryArchiveData = new NativeBinaryArchiveData()
                            {
                                Data = bufferPointer, DataSize = new IntPtr(memoryStream.Position)
                            };

                            success = FitNative(estimatorHandle, binaryArchiveData, out fitResult, out errorHandle);
                        }

                        // Reset memory stream to 0
                        memoryStream.Position = 0;

                        if (!success)
                        {
                            throw new Exception(GetErrorDetailsAndFreeNativeMemory(errorHandle));
                        }

                        // If we need to reset the data to the beginning.
                        if (fitResult == FitResult.ResetAndContinue)
                        {
                            ResetCursor(input, ref cursor, allColumns);
                        }

                        // If we are at the end of the data.
                        if (!cursor.MoveNext())
                        {
                            // If we get here fitResult should never be ResetAndContinue
                            Debug.Assert(fitResult != FitResult.ResetAndContinue);

                            OnDataCompletedNative(estimatorHandle, out errorHandle);
                            if (!success)
                            {
                                throw new Exception(GetErrorDetailsAndFreeNativeMemory(errorHandle));
                            }

                            ResetCursor(input, ref cursor, allColumns);
                        }
                    }

                    // When done training complete the estimator.
                    success = CompleteTrainingNative(estimatorHandle, out errorHandle);
                    if (!success)
                    {
                        throw new Exception(GetErrorDetailsAndFreeNativeMemory(errorHandle));
                    }

                    // Create the native transformer from the estimator;
                    success = CreateTransformerFromEstimatorNative(estimatorHandle, out IntPtr transformer, out errorHandle);
                    if (!success)
                    {
                        throw new Exception(GetErrorDetailsAndFreeNativeMemory(errorHandle));
                    }

                    // Manually dispose of the IEnumerator since we don't have a using statement;
                    cursor.Dispose();

                    return(new TransformerEstimatorSafeHandle(transformer, DestroyTransformerNative));
                }
        }
Exemplo n.º 9
0
        private unsafe TransformerEstimatorSafeHandle CreateTransformerFromEstimator(IDataView input)
        {
            IntPtr estimator;
            IntPtr errorHandle;
            bool   success;

            var allColumns = input.Schema.Where(x => _allColumnNames.Contains(x.Name)).Select(x => TypedColumn.CreateTypedColumn(x, _dataColumns)).ToDictionary(x => x.Column.Name);

            // Create buffer to hold binary data
            var columnBuffer = new byte[4096];

            // Create TypeId[] for types of grain and data columns;
            var dataColumnTypes  = new TypeId[_dataColumns.Length];
            var grainColumnTypes = new TypeId[_grainColumns.Length];

            foreach (var column in _grainColumns.Select((value, index) => new { index, value }))
            {
                grainColumnTypes[column.index] = allColumns[column.value].GetTypeId();
            }

            foreach (var column in _dataColumns.Select((value, index) => new { index, value }))
            {
                dataColumnTypes[column.index] = allColumns[column.value].GetTypeId();

                fixed(bool *suppressErrors = &_suppressTypeErrors)
                fixed(TypeId * rawDataColumnTypes  = dataColumnTypes)
                fixed(TypeId * rawGrainColumnTypes = grainColumnTypes)
                {
                    success = CreateEstimatorNative(rawGrainColumnTypes, new IntPtr(grainColumnTypes.Length), rawDataColumnTypes, new IntPtr(dataColumnTypes.Length), _imputeMode, suppressErrors, out estimator, out errorHandle);
                }
                if (!success)
                {
                    throw new Exception(GetErrorDetailsAndFreeNativeMemory(errorHandle));
                }

                using (var estimatorHandler = new TransformerEstimatorSafeHandle(estimator, DestroyEstimatorNative))
                {
                    var fitResult = FitResult.Continue;
                    while (fitResult != FitResult.Complete)
                    {
                        using (var cursor = input.GetRowCursorForAllColumns())
                        {
                            // Initialize getters for start of loop
                            foreach (var column in allColumns.Values)
                            {
                                column.InitializeGetter(cursor);
                            }

                            while ((fitResult == FitResult.Continue || fitResult == FitResult.ResetAndContinue) && cursor.MoveNext())
                            {
                                BuildColumnByteArray(allColumns, ref columnBuffer, out int serializedDataLength);

                                fixed(byte *bufferPointer = columnBuffer)
                                {
                                    var binaryArchiveData = new NativeBinaryArchiveData()
                                    {
                                        Data = bufferPointer, DataSize = new IntPtr(serializedDataLength)
                                    };

                                    success = FitNative(estimatorHandler, binaryArchiveData, out fitResult, out errorHandle);
                                }

                                if (!success)
                                {
                                    throw new Exception(GetErrorDetailsAndFreeNativeMemory(errorHandle));
                                }
                            }

                            success = CompleteTrainingNative(estimatorHandler, out fitResult, out errorHandle);
                            if (!success)
                            {
                                throw new Exception(GetErrorDetailsAndFreeNativeMemory(errorHandle));
                            }
                        }
                    }

                    success = CreateTransformerFromEstimatorNative(estimatorHandler, out IntPtr transformer, out errorHandle);
                    if (!success)
                    {
                        throw new Exception(GetErrorDetailsAndFreeNativeMemory(errorHandle));
                    }

                    return(new TransformerEstimatorSafeHandle(transformer, DestroyTransformerNative));
                }
        }
Exemplo n.º 10
0
        // Initalize the Format of the ObjectListView
        private void InitModel()
        {
            if (ObjectListView.IsVistaOrLater)
            {
                this.Font = new Font("msyh", 8);
            }

            this.folvAnime.AddDecoration(new EditingCellBorderDecoration(true));

            TypedObjectListView <Anime> tolv = new TypedObjectListView <Anime>(this.folvAnime);

            tolv.GenerateAspectGetters();

            // Name of Anime
            TypedColumn <Anime> tc = new TypedColumn <Anime>(this.olvColName);

            tc.AspectPutter = (Anime a, object opn) => { a.Name = opn.ToString(); };

            // Schedule of Anime
            tc = new TypedColumn <Anime>(this.olvColSchedule);
            tc.GroupKeyGetter = (Anime a) => a.Year;

            // Type of Anime
            tc = new TypedColumn <Anime>(this.olvColType);
            tc.AspectPutter = (Anime a, object opt) => { a.Type = (MediaType)opt; };
            tc.ImageGetter  = (Anime a) => {
                switch (a.Format)
                {
                case MergeFormat.MKV:
                    return(Properties.Resources.MKV);

                case MergeFormat.MP4:
                    return(Properties.Resources.MP4);

                case MergeFormat.AVI:
                    return(Properties.Resources.AVI);

                case MergeFormat.WMV:
                    return(Properties.Resources.WMV);

                case MergeFormat.M2TS:
                    return(Properties.Resources.M2TS);

                default:
                    return(-1);
                }
            };

            // Format of Anime
            #region
            //this.olvColFormat.Renderer = new MappedImageRenderer(new object[] {
            //	MergeFormat.MKV, Properties.Resources.MKV,
            //	MergeFormat.MP4, Properties.Resources.MP4,
            //	MergeFormat.AVI, Properties.Resources.AVI,
            //	MergeFormat.WMV, Properties.Resources.WMV,
            //	MergeFormat.M2TS, Properties.Resources.M2TS
            //});
            //tc = new TypedColumn<Anime>(this.olvColFormat);
            //tc.AspectPutter = delegate(Anime a, object opf) { a.Format = (MergeFormat)opf; };
            #endregion

            // SubTeam of Anime
            tc = new TypedColumn <Anime>(this.olvColSubTeam);
            tc.AspectPutter = (Anime a, object opp) => { a.SubTeam = opp.ToString(); };
            tc.ImageGetter  = (Anime a) => {
                switch (a.SubStyle)
                {
                case SubStyles.External:
                    return(Properties.Resources.External);

                case SubStyles.Sealed:
                    return(Properties.Resources.Sealed);

                case SubStyles.Embedded:
                    return(Properties.Resources.Embedded);

                default:
                    return(-1);
                }
            };

            // SubStyle of Anime
            #region
            //this.olvColSubStyle.Renderer = new MappedImageRenderer(new object[] {
            //	SubStyles.External, Properties.Resources.External,
            //	SubStyles.Sealed, Properties.Resources.Sealed,
            //	SubStyles.Embedded, Properties.Resources.Embedded
            //});
            //tc = new TypedColumn<Anime>(this.olvColSubStyle);
            //tc.AspectPutter = delegate(Anime a, object ops) { a.SubStyle = (SubStyles)ops; };
            #endregion

            // Size of Anime
            this.olvColSize.AspectToStringConverter = ots => {
                long ls = (long)ots;

                if (ls == 0L)
                {
                    return("-");
                }
                else if (ls >= 1000000000L)
                {
                    return(String.Format("{0:#,##0.#0} G", ls / 1073741824D));
                }
                else
                {
                    return(String.Format("{0:#,##0.#0} M", ls / 1048576D));
                }
            };
            this.olvColSize.MakeGroupies(
                new long[] { 5368709120L, 10737418240L },
                new string[] { "0~5 GB", "5~10 GB", ">10 GB" }
                );

            // Store of Anime
            tc = new TypedColumn <Anime>(this.olvColStore);
            tc.AspectPutter           = (Anime a, object opg) => { a.Store = (bool)opg; };
            this.olvColStore.Renderer = new MappedImageRenderer(true, Properties.Resources.Accept, false, Properties.Resources.Alert);

            // Enjoy of Anime
            tc = new TypedColumn <Anime>(this.olvColEnjoy);
            tc.AspectPutter           = (Anime a, object opv) => { a.Enjoy = (bool)opv; };
            this.olvColEnjoy.Renderer = new MappedImageRenderer(true, Properties.Resources.Smile, false, Properties.Resources.Sad);

            // Grade of Anime
            tc = new TypedColumn <Anime>(this.olvColGrade);
            tc.AspectPutter = (Anime a, object opr) => {
                int onr = (int)opr;
                a.Grade = onr;                //onr < 1 ? 1 : onr;
            };
            this.olvColGrade.Renderer = new MultiImageRenderer(Properties.Resources.Diamond, 3, 0, 4);
            this.olvColGrade.MakeGroupies(
                new int[] { 1, 2 },
                new string[] { "Normal", "Nice", "Good" }
                );

            // Note of Anime
            this.olvColNote.AspectToStringConverter = otn => otn.ToString().Replace('\u0002', '\u0020');

            //RowBorderDecoration rbd = new RowBorderDecoration();
            //rbd.BorderPen = new Pen(Color.Orchid, 1);
            //rbd.FillBrush = null;
            //rbd.CornerRounding = 4.0f;
            //HotItemStyle hotItemStyle = new HotItemStyle();
            //hotItemStyle.Decoration = rbd;
            //hotItemStyle.Overlay = new AnimeViewOverlay();
            //this.folvAnime.HotItemStyle = hotItemStyle;

            this.folvAnime.UseTranslucentHotItem   = true;
            this.folvAnime.UseTranslucentSelection = true;
            this.folvAnime.HotItemStyle.Overlay    = new AnimeViewOverlay();
            this.folvAnime.HotItemStyle            = this.folvAnime.HotItemStyle;
            this.folvAnime.PrimarySortColumn       = this.olvColTitle;
            this.folvAnime.PrimarySortOrder        = SortOrder.Ascending;
        }
Exemplo n.º 11
0
        // Initalize the Format of the ObjectListView
        private void InitModel()
        {
            this.olvAnime.AddDecoration(new EditingCellBorderDecoration(true));

            TypedObjectListView <Anime> tolv = new TypedObjectListView <Anime>(this.olvAnime);

            tolv.GenerateAspectGetters();

            // Name of Anime
            TypedColumn <Anime> tc = new TypedColumn <Anime>(this.olvColName);

            tc.AspectPutter = (Anime a, object opn) => { a.Name = opn.ToString(); };

            // Schedule of Anime
            tc = new TypedColumn <Anime>(this.olvColAirdate);
            tc.GroupKeyGetter = (Anime a) => a.Year;

            // Type of Anime
            tc = new TypedColumn <Anime>(this.olvColType);
            tc.AspectPutter = (Anime a, object opt) => { a.Type = (MediaType)opt; };
            tc.ImageGetter  = (Anime a) =>
            {
                switch (a.Format)
                {
                case MergeFormat.MKV:
                    return(Properties.Resources.MKV);

                case MergeFormat.MP4:
                    return(Properties.Resources.MP4);

                case MergeFormat.AVI:
                    return(Properties.Resources.AVI);

                case MergeFormat.WMV:
                    return(Properties.Resources.WMV);

                case MergeFormat.M2TS:
                    return(Properties.Resources.M2TS);

                default:
                    return(-1);
                }
            };

            // SubTeam of Anime
            tc = new TypedColumn <Anime>(this.olvColSubTeam);
            tc.AspectPutter = (Anime a, object opp) => { a.SubTeam = opp.ToString(); };
            tc.ImageGetter  = (Anime a) =>
            {
                switch (a.SubStyle)
                {
                case SubStyle.External:
                    return(Properties.Resources.External);

                case SubStyle.Sealed:
                    return(Properties.Resources.Sealed);

                case SubStyle.Embedded:
                    return(Properties.Resources.Embedded);

                default:
                    return(-1);
                }
            };

            // Size of Anime
            this.olvColSize.AspectToStringConverter = ots =>
            {
                long ls = (long)ots;

                if (ls == 0L)
                {
                    return("-");
                }
                else if (ls >= 1000L * 1024L * 1024L)                   // 1000M -> 0.9765625G
                {
                    return(String.Format("{0:#,##0.#0} G", (double)ls / (1024 * 1024 * 1024)));
                }
                else
                {
                    return(String.Format("{0:#,##0.#0} M", (double)ls / (1024 * 1024)));
                }
            };
            this.olvColSize.MakeGroupies(
                new long[] { 1024L * 1024L * 1024L * 5L, 1024L * 1024L * 1024L * 10L },
                new string[] { "0~5 GB", "5~10 GB", ">10 GB" }
                );

            // Store of Anime
            tc = new TypedColumn <Anime>(this.olvColStore);
            //tc.AspectPutter = (Anime a, object opg) => { a.Store = (bool)opg; };
            //this.olvColStore.Renderer = new MappedImageRenderer(true, Properties.Resources.Accept, false, Properties.Resources.Alert);
            tc.AspectPutter           = (Anime a, object ops) => { a.Store = (StoreState)ops; };
            this.olvColStore.Renderer = new MappedImageRenderer(new object[] {
                StoreState.Ignore, null,
                StoreState.Cont, Properties.Resources.Alert,
                StoreState.Fin, Properties.Resources.Accept
            });

            // Enjoy of Anime
            tc = new TypedColumn <Anime>(this.olvColEnjoy);
            //tc.AspectPutter = (Anime a, object opv) => { a.Enjoy = (bool)opv; };
            //this.olvColEnjoy.Renderer = new MappedImageRenderer(true, Properties.Resources.Smile, false, Properties.Resources.Sad);
            tc.AspectPutter           = (Anime a, object ope) => { a.Enjoy = (EnjoyState)ope; };
            this.olvColEnjoy.Renderer = new MappedImageRenderer(new object[] {
                EnjoyState.Ignore, null,
                EnjoyState.NotYet, Properties.Resources.Sad,
                EnjoyState.Done, Properties.Resources.Smile
            });

            // Grade of Anime
            tc = new TypedColumn <Anime>(this.olvColGrade);
            tc.AspectPutter = (Anime a, object opr) =>
            {
                int onr = (int)opr;
                a.Grade = onr;                //onr < 1 ? 1 : onr;
            };
            this.olvColGrade.Renderer = new MultiImageRenderer(Properties.Resources.Diamond, 3, 0, 4);
            this.olvColGrade.MakeGroupies(
                new int[] { 1, 2 },
                new string[] { "Normal", "Nice", "Good" }
                );

            // Note of Anime
            this.olvColNote.AspectToStringConverter = otn => otn.ToString().Replace('\u0002', '\u0020');

            // this.olvAnime.UseHotItem auto true
            this.olvAnime.UseTranslucentHotItem   = true;
            this.olvAnime.UseTranslucentSelection = true;
            this.olvAnime.HotItemStyle.Overlay    = new AnimeViewOverlay();
            this.olvAnime.HotItemStyle            = this.olvAnime.HotItemStyle;
            this.olvAnime.PrimarySortColumn       = this.olvColTitle;
            this.olvAnime.PrimarySortOrder        = SortOrder.Ascending;
        }