public static Delegate GetColumnGetter(IRowCursor cur, int col, Schema sch = null)
        {
            if (sch == null)
            {
                sch = cur.Schema;
            }
            var colType = sch.GetColumnType(col);

            if (colType.IsVector())
            {
                switch (colType.ItemType().RawKind())
                {
                case DataKind.BL: return(GetGetterChoice <VBufferEqSort <bool>, VBuffer <bool> >(cur, col));

                case DataKind.I4: return(GetGetterChoice <VBufferEqSort <int>, VBuffer <int> >(cur, col));

                case DataKind.U4: return(GetGetterChoice <VBufferEqSort <uint>, VBuffer <uint> >(cur, col));

                case DataKind.I8: return(GetGetterChoice <VBufferEqSort <long>, VBuffer <long> >(cur, col));

                case DataKind.R4: return(GetGetterChoice <VBufferEqSort <float>, VBuffer <float> >(cur, col));

                case DataKind.R8: return(GetGetterChoice <VBufferEqSort <double>, VBuffer <double> >(cur, col));

                case DataKind.TX: return(GetGetterChoice <VBufferEqSort <DvText>, VBuffer <DvText>, VBuffer <ReadOnlyMemory <char> > >(cur, col));

                default:
                    throw new NotImplementedException(string.Format("Not implemented for kind {0}", colType));
                }
            }
            else
            {
                switch (colType.RawKind())
                {
                case DataKind.BL: return(cur.GetGetter <bool>(col));

                case DataKind.I4: return(cur.GetGetter <int>(col));

                case DataKind.U4: return(cur.GetGetter <uint>(col));

                case DataKind.I8: return(cur.GetGetter <Int64>(col));

                case DataKind.R4: return(cur.GetGetter <float>(col));

                case DataKind.R8: return(cur.GetGetter <double>(col));

                case DataKind.TX: return(GetGetterChoice <DvText, ReadOnlyMemory <char> >(cur, col));

                default:
                    throw new NotImplementedException(string.Format("Not implemented for kind {0}", colType));
                }
            }
        }
Exemple #2
0
        internal static ConfusionMatrix Create(IHostEnvironment env, IDataView confusionMatrix)
        {
            Contracts.AssertValue(env);
            env.AssertValue(confusionMatrix);

            if (!confusionMatrix.Schema.TryGetColumnIndex(MetricKinds.ColumnNames.Count, out int countColumn))
            {
                env.Except($"ConfusionMatrix data view did not contain a {nameof(MetricKinds.ColumnNames.Count)} column.");
            }

            ColumnType type = confusionMatrix.Schema.GetColumnType(countColumn);

            env.Assert(type.IsVector);

            double[,] elements = new double[type.VectorSize, type.VectorSize];

            IRowCursor cursor = confusionMatrix.GetRowCursor(col => col == countColumn);
            ValueGetter <VBuffer <double> > countGetter = cursor.GetGetter <VBuffer <double> >(countColumn);
            VBuffer <double> countValues = default;

            int valuesRowIndex = 0;

            while (cursor.MoveNext())
            {
                countGetter(ref countValues);
                for (int i = 0; i < countValues.Length; i++)
                {
                    elements[valuesRowIndex, i] = countValues.Values[i];
                }

                valuesRowIndex++;
            }

            var slots = default(VBuffer <DvText>);

            confusionMatrix.Schema.GetMetadata(MetadataUtils.Kinds.SlotNames, countColumn, ref slots);
            string[] classNames = new string[slots.Count];
            for (int i = 0; i < slots.Count; i++)
            {
                classNames[i] = slots.Values[i].ToString();
            }

            return(new ConfusionMatrix(elements, classNames));
        }
        private void EstimatorCore <T>(IRowCursor cursor, ColumnCodec col,
                                       out Func <long> fetchWriteEstimator, out IValueWriter writer)
        {
            ValueGetter <T> getter = cursor.GetGetter <T>(col.SourceIndex);
            IValueCodec <T> codec  = col.Codec as IValueCodec <T>;

            _host.AssertValue(codec);
            IValueWriter <T> specificWriter = codec.OpenWriter(Stream.Null);

            writer = specificWriter;
            T val = default(T);

            fetchWriteEstimator = () =>
            {
                getter(ref val);
                specificWriter.Write(in val);
                return(specificWriter.GetCommitLengthEstimate());
            };
        }
Exemple #4
0
 public ValueGetter <TValue> GetGetter <TValue>(int col)
 {
     if (col < _view.Source.Schema.ColumnCount)
     {
         return(_inputCursor.GetGetter <TValue>(col));
     }
     else if (col == _view.Source.Schema.ColumnCount) // Cluster
     {
         return(GetGetterCluster() as ValueGetter <TValue>);
     }
     else if (col == _view.Source.Schema.ColumnCount + 1) // Score
     {
         return(GetGetterScore() as ValueGetter <TValue>);
     }
     else
     {
         throw new IndexOutOfRangeException();
     }
 }
Exemple #5
0
            public ValueGetter <TValue> GetGetter <TValue>(int col)
            {
                // If the column is part of the input view.
                var schema = _inputCursor.Schema;

                if (col < schema.ColumnCount)
                {
                    return(_inputCursor.GetGetter <TValue>(col));
                }
                // If it is the added column.
                else if (col == schema.ColumnCount)
                {
                    return(PolynomialBuilder() as ValueGetter <TValue>);
                }
                // Otherwise, it is an error.
                else
                {
                    throw Contracts.Except("Unexpected columns {0} > {1}.", col, schema.ColumnCount);
                }
            }
Exemple #6
0
        void LoadCache <TClass>(IRandom rand, IRowCursor cur, int classColumn, TClass valueClass, IChannel ch)
        {
            _cacheReplica = new Dictionary <UInt128, int>();
            var     hist  = new Dictionary <TClass, long>();
            var     gid   = cur.GetIdGetter();
            var     gcl   = cur.GetGetter <TClass>(classColumn);
            UInt128 did   = default(UInt128);
            TClass  cl    = default(TClass);
            long    nbIn  = 0;
            long    nbOut = 0;
            int     rep;

            while (cur.MoveNext())
            {
                gcl(ref cl);
                gid(ref did);
                if (!hist.ContainsKey(cl))
                {
                    hist[cl] = 1;
                }
                else
                {
                    ++hist[cl];
                }
                if (cl.Equals(valueClass))
                {
                    rep = NextPoisson(_args.lambda, rand);
                    ++nbIn;
                }
                else
                {
                    rep = 1;
                    ++nbOut;
                }
                _cacheReplica[did] = rep;
            }
            if (nbIn == 0)
            {
                ch.Warning("Resample on a condition never happened: nbIn={0} nbOut={1}", nbIn, nbOut);
            }
        }
            protected override ValueGetter <TValue> CreateTypedGetter <TValue>(int col)
            {
                Ch.AssertValue(_currentCursor);
                ValueGetter <TValue> getSrc = null;
                // Whenever captured != current, we know that the captured getter is outdated.
                int capturedSourceIndex = -1;

                return
                    ((ref TValue val) =>
                {
                    Ch.Check(State == CursorState.Good, "A getter can only be used when the cursor state is Good.");
                    if (_currentSourceIndex != capturedSourceIndex)
                    {
                        Ch.Assert(0 <= _currentSourceIndex && _currentSourceIndex < Sources.Length);
                        Ch.Assert(_currentCursor != null);
                        getSrc = _currentCursor.GetGetter <TValue>(col);
                        capturedSourceIndex = _currentSourceIndex;
                    }
                    getSrc(ref val);
                });
            }
 public ValueGetter <TValue> GetGetter <TValue>(int col)
 {
     if (col < _view.Source.Schema.ColumnCount)
     {
         return(_inputCursor.GetGetter <TValue>(col));
     }
     else if (col == _view.Source.Schema.ColumnCount) // Ordering
     {
         return(GetGetterOrdering() as ValueGetter <TValue>);
     }
     else if (col == _view.Source.Schema.ColumnCount + 1) // Reachability Distance
     {
         return(GetGetterReachabilityDistance() as ValueGetter <TValue>);
     }
     else if (col == _view.Source.Schema.ColumnCount + 2) // Core Distance
     {
         return(GetGetterCoreDistance() as ValueGetter <TValue>);
     }
     else
     {
         throw new IndexOutOfRangeException();
     }
 }
 public ValueGetter <TValue> GetGetter <TValue>(int col)
 {
     if (col == _view.ConstantCol)
     {
         return(GetGetterPrivate(col) as ValueGetter <TValue>);
     }
     else if (_otherValues != null)
     {
         return(_otherValues.GetGetter <TValue>(col));
     }
     else if (_ignoreOtherColumn)
     {
         return (ref TValue value) =>
                {
                    value = default(TValue);
                }
     }
     ;
     else
     {
         throw Contracts.Except("otherValues is null, unable to access other columns.");
     }
 }
Exemple #10
0
 public ValueWriter(IRowCursor cursor, PrimitiveType type, int source, char sep)
     : base(type, source, sep)
 {
     _getSrc     = cursor.GetGetter <T>(source);
     _columnName = cursor.Schema.GetColumnName(source);
 }
 public ValueGetter <TValue> GetGetter <TValue>(int col)
 {
     return(_cursor.GetGetter <TValue>(col));
 }
Exemple #12
0
            public static IColumnFunctionBuilder CreateBuilder(LogMeanVarArguments args, IHost host,
                                                               int icol, int srcIndex, ColumnType srcType, IRowCursor cursor)
            {
                Contracts.AssertValue(host);
                host.AssertValue(args);

                if (srcType.IsNumber)
                {
                    if (srcType == NumberType.R4)
                    {
                        return(Sng.MeanVarOneColumnFunctionBuilder.Create(args, host, icol, srcType, cursor.GetGetter <Single>(srcIndex)));
                    }
                    if (srcType == NumberType.R8)
                    {
                        return(Dbl.MeanVarOneColumnFunctionBuilder.Create(args, host, icol, srcType, cursor.GetGetter <Double>(srcIndex)));
                    }
                }
                if (srcType.IsVector && srcType.ItemType.IsNumber)
                {
                    if (srcType.ItemType == NumberType.R4)
                    {
                        return(Sng.MeanVarVecColumnFunctionBuilder.Create(args, host, icol, srcType, cursor.GetGetter <VBuffer <Single> >(srcIndex)));
                    }
                    if (srcType.ItemType == NumberType.R8)
                    {
                        return(Dbl.MeanVarVecColumnFunctionBuilder.Create(args, host, icol, srcType, cursor.GetGetter <VBuffer <Double> >(srcIndex)));
                    }
                }
                throw host.ExceptUserArg(nameof(args.Column), "Wrong column type for column {0}. Expected: R4, R8, Vec<R4, n> or Vec<R8, n>. Got: {1}.", args.Column[icol].Source, srcType.ToString());
            }
Exemple #13
0
            public ValueGetter <TValue> GetGetter <TValue>(int col)
            {
                if (col < _view.Source.Schema.ColumnCount)
                {
                    return(_inputCursor.GetGetter <TValue>(col));
                }
                else if (col < _view.Schema.ColumnCount)
                {
                    var colType = _schema.GetColumnType(_view._columnMapping[col]);
                    if (colType.IsVector())
                    {
                        switch (colType.ItemType().RawKind())
                        {
                        case DataKind.BL: return(GetGetterVector(col, false) as ValueGetter <TValue>);

                        case DataKind.I4: return(GetGetterVector(col, 0) as ValueGetter <TValue>);

                        case DataKind.U4: return(GetGetterVector(col, (uint)0) as ValueGetter <TValue>);

                        case DataKind.I8: return(GetGetterVector(col, (Int64)0) as ValueGetter <TValue>);

                        case DataKind.R4: return(GetGetterVector(col, 0f) as ValueGetter <TValue>);

                        case DataKind.R8: return(GetGetterVector(col, 0.0) as ValueGetter <TValue>);

                        case DataKind.TX: return(GetGetterVector(col, new ReadOnlyMemory <char>()) as ValueGetter <TValue>);

                        default:
                            throw Contracts.ExceptNotImpl($"Unsupported type '{colType.ItemType().RawKind()}'.");
                        }
                    }
                    else
                    {
                        switch (colType.RawKind())
                        {
                        case DataKind.BL: return(GetGetter(col, false) as ValueGetter <TValue>);

                        case DataKind.I4: return(GetGetter(col, 0) as ValueGetter <TValue>);

                        case DataKind.U4: return(GetGetter(col, (uint)0) as ValueGetter <TValue>);

                        case DataKind.I8: return(GetGetter(col, (Int64)0) as ValueGetter <TValue>);

                        case DataKind.R4: return(GetGetter(col, 0f) as ValueGetter <TValue>);

                        case DataKind.R8: return(GetGetter(col, 0.0) as ValueGetter <TValue>);

                        case DataKind.TX: return(GetGetter(col, new ReadOnlyMemory <char>()) as ValueGetter <TValue>);

                        default:
                            throw Contracts.ExceptNotImpl($"Unsupported type '{colType.ItemType().RawKind()}'.");
                        }
                    }
                }
                else
                {
                    throw _view._host.Except("Column index {0} does not exist.", col);
                }
            }