Exemple #1
0
        protected bool CheckEqualityCore(string dir, string name, string nameBase, bool normalize, int digitsOfPrecision = DigitsOfPrecision)
        {
            Contracts.Assert(IsActive);
            Contracts.AssertValue(dir); // Can be empty.
            Contracts.AssertNonEmpty(name);
            Contracts.AssertNonEmpty(nameBase);

            // The following assert is necessary since some tests were attempting to
            // combine the ZBasline directory with an absolute path, leading to an output
            // file being compared with itself.
            Contracts.Assert(!Path.IsPathRooted(name), "file name should not be a full path");
            Contracts.Assert(!Path.IsPathRooted(nameBase), "file nameBase should not be a full path");

            string relPath  = Path.Combine(dir, name);
            string basePath = GetBaselinePath(dir, nameBase);
            string outPath  = GetOutputPath(dir, name);

            if (!CheckOutFile(outPath))
            {
                return(false);
            }

            // Normalize the output file.
            if (normalize)
            {
                Normalize(outPath);
            }

            if (!CheckBaseFile(basePath))
            {
                return(false);
            }

            bool res = CheckEqualityFromPathsCore(relPath, basePath, outPath, digitsOfPrecision: digitsOfPrecision);

            // No need to keep the raw (unnormalized) output file.
            if (normalize && res)
            {
                File.Delete(outPath + RawSuffix);
            }

            return(res);
        }
                    public SchemaImpl(ISchema parent, int col, VectorType type, ValueGetter <VBuffer <T> > getter, string metadataKind)
                    {
                        Contracts.AssertValue(parent);
                        Contracts.Assert(0 <= col && col < parent.ColumnCount);
                        Contracts.AssertValue(type);
                        Contracts.AssertValue(getter);
                        Contracts.Assert(type.ItemType.RawType == typeof(T));
                        Contracts.AssertNonEmpty(metadataKind);
                        Contracts.Assert(parent.GetMetadataTypeOrNull(metadataKind, col) == null);

                        _parent        = parent;
                        _scoreCol      = col;
                        _labelNameType = type;
                        // We change to this metadata variant of the getter to enable the marshal call to work.
                        _labelNameGetter = (int c, ref VBuffer <T> val) => getter(ref val);
                        _metadataKind    = metadataKind;

                        AsSchema = Schema.Create(this);
                    }
Exemple #3
0
        public static void MatrixTimesSource(AlignedArray matrix, int[] rgposSrc, AlignedArray sourceValues,
                                             int posMin, int iposMin, int iposLimit, AlignedArray destination, int stride)
        {
            Contracts.AssertValue(rgposSrc);
            Contracts.Assert(iposMin >= 0);
            Contracts.Assert(iposMin <= iposLimit);
            Contracts.Assert(iposLimit <= rgposSrc.Length);
            Contracts.Assert(matrix.Size == destination.Size * sourceValues.Size);

            if (iposMin >= iposLimit)
            {
                destination.ZeroItems();
                return;
            }

            Contracts.AssertNonEmpty(rgposSrc);
            Contracts.Assert(stride >= 0);

            if (Avx.IsSupported)
            {
                Contracts.Assert(stride <= destination.Size);
                AvxIntrinsics.MatMulP(matrix, rgposSrc, sourceValues, posMin, iposMin, iposLimit, destination, stride, sourceValues.Size);
            }
            else if (Sse.IsSupported)
            {
                Contracts.Assert(stride <= destination.Size);
                SseIntrinsics.MatMulP(matrix, rgposSrc, sourceValues, posMin, iposMin, iposLimit, destination, stride, sourceValues.Size);
            }
            else
            {
                Contracts.Assert(stride <= destination.Size);
                for (int i = 0; i < stride; i++)
                {
                    float dotProduct = 0;
                    for (int j = iposMin; j < iposLimit; j++)
                    {
                        int col = rgposSrc[j] - posMin;
                        dotProduct += matrix[i * sourceValues.Size + col] * sourceValues[col];
                    }
                    destination[i] = dotProduct;
                }
            }
        }
Exemple #4
0
        // Try to quote by just slapping curlies around the string. This will normally be sufficient
        // and produces a much more aesthetic result than escaping everything.
        private static bool TryNaiveQuoting(string str, StringBuilder sb)
        {
            Contracts.AssertNonEmpty(str);

            var curs = new CharCursor("{" + str + "}");
            var lex  = new CmdLexer(curs);
            var res  = new StringBuilder();

            lex.GatherCurlyContents(res);
            if (lex.Error || !curs.Eof || res.Length != str.Length || res.ToString() != str)
            {
                return(false);
            }

            sb.Append("{");
            sb.Append(str);
            sb.Append("}");
            return(true);
        }
        public static void MatTimesSrc(AlignedArray mat, int[] rgposSrc, AlignedArray srcValues,
                                       int posMin, int iposMin, int iposLim, AlignedArray dst, int crun)
        {
            Contracts.AssertValue(rgposSrc);
            Contracts.Assert(iposMin >= 0);
            Contracts.Assert(iposMin <= iposLim);
            Contracts.Assert(iposLim <= rgposSrc.Length);
            Contracts.Assert(mat.Size == dst.Size * srcValues.Size);

            if (iposMin >= iposLim)
            {
                dst.ZeroItems();
                return;
            }

            Contracts.AssertNonEmpty(rgposSrc);
            Contracts.Assert(crun >= 0);

            if (Avx.IsSupported)
            {
                Contracts.Assert(crun <= dst.Size);
                AvxIntrinsics.MatMulPX(mat, rgposSrc, srcValues, posMin, iposMin, iposLim, dst, crun, srcValues.Size);
            }
            else if (Sse.IsSupported)
            {
                Contracts.Assert(crun <= dst.Size);
                SseIntrinsics.MatMulPA(mat, rgposSrc, srcValues, posMin, iposMin, iposLim, dst, crun, srcValues.Size);
            }
            else
            {
                Contracts.Assert(crun <= dst.Size);
                for (int i = 0; i < crun; i++)
                {
                    float dotProduct = 0;
                    for (int j = iposMin; j < iposLim; j++)
                    {
                        int col = rgposSrc[j] - posMin;
                        dotProduct += mat[i * srcValues.Size + col] * srcValues[col];
                    }
                    dst[i] = dotProduct;
                }
            }
        }
Exemple #6
0
            private BindingsImpl(ISchema input, ISchemaBoundRowMapper mapper, string suffix, string scoreColumnKind,
                                 bool user, int scoreColIndex, ColumnType predColType)
                : base(input, mapper, suffix, user, DefaultColumnNames.PredictedLabel)
            {
                Contracts.AssertNonEmpty(scoreColumnKind);
                Contracts.Assert(DerivedColumnCount == 1);

                ScoreColumnIndex = scoreColIndex;
                ScoreColumnKind  = scoreColumnKind;
                PredColType      = predColType;

                _getScoreColumnKind = GetScoreColumnKind;
                _getScoreValueKind  = GetScoreValueKind;

                // REVIEW: This logic is very specific to multiclass, which is deeply
                // regrettable, but the class structure as designed and the status of this schema
                // bearing object makes pushing the logic into the multiclass scorer almost impossible.
                if (predColType.IsKey)
                {
                    ColumnType scoreSlotsType = mapper.Schema.GetMetadataTypeOrNull(MetadataUtils.Kinds.SlotNames, scoreColIndex);
                    if (scoreSlotsType != null && scoreSlotsType.IsKnownSizeVector &&
                        scoreSlotsType.VectorSize == predColType.KeyCount)
                    {
                        Contracts.Assert(scoreSlotsType.VectorSize > 0);
                        IColumn col = Utils.MarshalInvoke(KeyValueMetadataFromMetadata <int>,
                                                          scoreSlotsType.RawType, mapper.Schema, scoreColIndex, MetadataUtils.Kinds.SlotNames);
                        _predColMetadata = RowColumnUtils.GetRow(null, col);
                    }
                    else
                    {
                        scoreSlotsType = mapper.Schema.GetMetadataTypeOrNull(MetadataUtils.Kinds.TrainingLabelValues, scoreColIndex);
                        if (scoreSlotsType != null && scoreSlotsType.IsKnownSizeVector &&
                            scoreSlotsType.VectorSize == predColType.KeyCount)
                        {
                            Contracts.Assert(scoreSlotsType.VectorSize > 0);
                            IColumn col = Utils.MarshalInvoke(KeyValueMetadataFromMetadata <int>,
                                                              scoreSlotsType.RawType, mapper.Schema, scoreColIndex, MetadataUtils.Kinds.TrainingLabelValues);
                            _predColMetadata = RowColumnUtils.GetRow(null, col);
                        }
                    }
                }
            }
            private bool TryParse(string str)
            {
                Contracts.AssertNonEmpty(str);

                int ich = str.IndexOf('-');

                if (ich < 0)
                {
                    if (!int.TryParse(str, out Min))
                    {
                        return(false);
                    }
                    Max = Min;
                    return(true);
                }

                if (ich == 0 || ich >= str.Length - 1)
                {
                    return(false);
                }

                if (!int.TryParse(str.Substring(0, ich), out Min))
                {
                    return(false);
                }

                string rest = str.Substring(ich + 1);

                if (rest == "*")
                {
                    return(true);
                }

                int tmp;

                if (!int.TryParse(rest, out tmp))
                {
                    return(false);
                }
                Max = tmp;
                return(true);
            }
        public static void Add(float value, Span <float> destination)
        {
            Contracts.AssertNonEmpty(destination);

            if (destination.Length < MinInputSize || !Sse.IsSupported)
            {
                for (int i = 0; i < destination.Length; i++)
                {
                    destination[i] += value;
                }
            }
            else if (Avx.IsSupported)
            {
                AvxIntrinsics.AddScalarU(value, destination);
            }
            else
            {
                SseIntrinsics.AddScalarU(value, destination);
            }
        }
Exemple #9
0
        private static Dictionary<string, List<ColumnInfo>> MapFromNames(Schema schema, IEnumerable<KeyValuePair<ColumnRole, string>> roles, bool opt = false)
        {
            Contracts.AssertValue(schema);
            Contracts.AssertValue(roles);

            var map = new Dictionary<string, List<ColumnInfo>>();
            foreach (var kvp in roles)
            {
                Contracts.AssertNonEmpty(kvp.Key.Value);
                if (string.IsNullOrEmpty(kvp.Value))
                    continue;
                ColumnInfo info;
                if (!opt)
                    info = ColumnInfo.CreateFromName(schema, kvp.Value, kvp.Key.Value);
                else if (!ColumnInfo.TryCreateFromName(schema, kvp.Value, out info))
                    continue;
                Add(map, kvp.Key.Value, info);
            }
            return map;
        }
        // dst[i] = a * (dst[i] + b)
        public static void ScaleAdd(float a, float b, Span <float> dst)
        {
            Contracts.AssertNonEmpty(dst);

            if (Avx.IsSupported)
            {
                AvxIntrinsics.ScaleAddU(a, b, dst);
            }
            else if (Sse.IsSupported)
            {
                SseIntrinsics.ScaleAddU(a, b, dst);
            }
            else
            {
                for (int i = 0; i < dst.Length; i++)
                {
                    dst[i] = a * (dst[i] + b);
                }
            }
        }
        private static void ShowMetadataValueVec(IndentedTextWriter itw, Schema schema, int col, string kind, ColumnType type)
        {
            Contracts.AssertValue(itw);
            Contracts.AssertValue(schema);
            Contracts.Assert(0 <= col && col < schema.Count);
            Contracts.AssertNonEmpty(kind);
            Contracts.AssertValue(type);
            Contracts.Assert(type.IsVector);

            if (!type.ItemType.IsStandardScalar && !type.ItemType.IsKey)
            {
                itw.Write(": Can't display value of this type");
                return;
            }

            Action <IndentedTextWriter, Schema, int, string, ColumnType> del = ShowMetadataValueVec <int>;
            var meth = del.GetMethodInfo().GetGenericMethodDefinition().MakeGenericMethod(type.ItemType.RawType);

            meth.Invoke(null, new object[] { itw, schema, col, kind, type });
        }
        private static void ShowMetadataValue <T>(IndentedTextWriter itw, Schema schema, int col, string kind, ColumnType type)
        {
            Contracts.AssertValue(itw);
            Contracts.AssertValue(schema);
            Contracts.Assert(0 <= col && col < schema.Count);
            Contracts.AssertNonEmpty(kind);
            Contracts.AssertValue(type);
            Contracts.Assert(!type.IsVector);
            Contracts.Assert(type.RawType == typeof(T));

            var conv = Conversions.Instance.GetStringConversion <T>(type);

            var value = default(T);
            var sb    = default(StringBuilder);

            schema[col].Metadata.GetValue(kind, ref value);
            conv(in value, ref sb);

            itw.Write(": '{0}'", sb);
        }
Exemple #13
0
        // destination[i] = scale * (destination[i] + addend)
        public static void ScaleAdd(float scale, float addend, Span <float> destination)
        {
            Contracts.AssertNonEmpty(destination);

            if (Avx.IsSupported)
            {
                AvxIntrinsics.ScaleAddU(scale, addend, destination);
            }
            else if (Sse.IsSupported)
            {
                SseIntrinsics.ScaleAddU(scale, addend, destination);
            }
            else
            {
                for (int i = 0; i < destination.Length; i++)
                {
                    destination[i] = scale * (destination[i] + addend);
                }
            }
        }
Exemple #14
0
        public static void Scale(float value, Span <float> destination)
        {
            Contracts.AssertNonEmpty(destination);

            if (Avx.IsSupported)
            {
                AvxIntrinsics.Scale(value, destination);
            }
            else if (Sse.IsSupported)
            {
                SseIntrinsics.Scale(value, destination);
            }
            else
            {
                for (int i = 0; i < destination.Length; i++)
                {
                    destination[i] *= value;
                }
            }
        }
Exemple #15
0
        public override IRowCursor[] GetRowCursorSet(out IRowCursorConsolidator consolidator, Func<int, bool> predicate, int n, Random rand = null)
        {
            Host.CheckValue(predicate, nameof(predicate));
            Host.CheckValueOrNull(rand);

            var bindings = GetBindings();
            Func<int, bool> predicateInput;
            Func<int, bool> predicateMapper;
            var active = GetActive(bindings, predicate, out predicateInput, out predicateMapper);
            var inputs = Source.GetRowCursorSet(out consolidator, predicateInput, n, rand);
            Contracts.AssertNonEmpty(inputs);

            if (inputs.Length == 1 && n > 1 && WantParallelCursors(predicate) && (Source.GetRowCount() ?? int.MaxValue) > n)
                inputs = DataViewUtils.CreateSplitCursors(out consolidator, Host, inputs[0], n);
            Contracts.AssertNonEmpty(inputs);

            var cursors = new IRowCursor[inputs.Length];
            for (int i = 0; i < inputs.Length; i++)
                cursors[i] = new RowCursor(Host, this, inputs[i], active, predicateMapper);
            return cursors;
        }
Exemple #16
0
            private protected override bool TryParse(string str)
            {
                Contracts.AssertNonEmpty(str);

                // We accept N:K:S where N is the new column name, K is the output kind,
                // and S is source column names.
                if (!TryParse(str, out string extra))
                {
                    return(false);
                }
                if (extra == null)
                {
                    return(true);
                }
                if (!Enum.TryParse(extra, true, out OutputKind kind))
                {
                    return(false);
                }
                OutputKind = kind;
                return(true);
            }
            protected override bool TryParse(string str)
            {
                Contracts.AssertNonEmpty(str);

                // We accept N:B:S where N is the new column name, B is the number of bits,
                // and S is source column names.
                if (!TryParse(str, out string extra))
                {
                    return(false);
                }
                if (extra == null)
                {
                    return(true);
                }
                if (!int.TryParse(extra, out int bits))
                {
                    return(false);
                }
                HashBits = bits;
                return(true);
            }
        public ZipBinding(DataViewSchema[] sources)
        {
            Contracts.AssertNonEmpty(sources);
            _sources                = sources;
            _cumulativeColCounts    = new int[_sources.Length + 1];
            _cumulativeColCounts[0] = 0;

            for (int i = 0; i < sources.Length; i++)
            {
                var schema = sources[i];
                _cumulativeColCounts[i + 1] = _cumulativeColCounts[i] + schema.Count;
            }

            var schemaBuilder = new DataViewSchema.Builder();

            foreach (var sourceSchema in sources)
            {
                schemaBuilder.AddColumns(sourceSchema);
            }
            OutputSchema = schemaBuilder.ToSchema();
        }
Exemple #19
0
        public static void SdcaL1UpdateSparse(float primalUpdate, int count, ReadOnlySpan <float> source, ReadOnlySpan <int> indices, float threshold, Span <float> v, Span <float> w)
        {
            Contracts.AssertNonEmpty(source);
            Contracts.Assert(count <= source.Length);
            Contracts.AssertNonEmpty(indices);
            Contracts.Assert(count <= indices.Length);
            Contracts.AssertNonEmpty(v);
            Contracts.Assert(count <= v.Length);
            Contracts.AssertNonEmpty(w);
            Contracts.Assert(count <= w.Length);
            Contracts.Assert(count > 0);

            unsafe
            {
                fixed(float *psrc = &MemoryMarshal.GetReference(source))
                fixed(int *pi    = &MemoryMarshal.GetReference(indices))
                fixed(float *pd1 = &MemoryMarshal.GetReference(v))
                fixed(float *pd2 = &MemoryMarshal.GetReference(w))
                Thunk.SdcaL1UpdateSU(primalUpdate, psrc, pi, threshold, pd1, pd2, count);
            }
        }
Exemple #20
0
        internal static bool AssociateDataSourcesToSelect(this DType self, DataSourceToQueryOptionsMap dataSourceToQueryOptionsMap, string columnName, DType columnType, bool skipIfNotInSchema = false, bool skipExpands = false)
        {
            Contracts.AssertValue(dataSourceToQueryOptionsMap);
            Contracts.AssertNonEmpty(columnName);
            Contracts.AssertValue(columnType);

            bool retval = false;

            if (self.HasExpandInfo && self.ExpandInfo != null && !skipExpands)
            {
                var qOptions = dataSourceToQueryOptionsMap.GetOrCreateQueryOptions(self.ExpandInfo.ParentDataSource as IExternalTabularDataSource);
                retval |= qOptions.AddExpand(self.ExpandInfo, out var expandQueryOptions);
                if (expandQueryOptions != null)
                {
                    retval |= expandQueryOptions.AddSelect(columnName);
                }
            }
            else
            {
                foreach (var tabularDataSource in self.AssociatedDataSources)
                {
                    // Skip if this column doesn't belong to this datasource.
                    if (skipIfNotInSchema && !tabularDataSource.Schema.Contains(new DName(columnName)))
                    {
                        continue;
                    }

                    retval |= dataSourceToQueryOptionsMap.AddSelect((IExternalTabularDataSource)tabularDataSource, new DName(columnName));

                    if (columnType.IsExpandEntity && columnType.ExpandInfo != null && !skipExpands)
                    {
                        var scopedExpandInfo = columnType.ExpandInfo;
                        var qOptions         = dataSourceToQueryOptionsMap.GetOrCreateQueryOptions(scopedExpandInfo.ParentDataSource as IExternalTabularDataSource);
                        retval |= qOptions.AddExpand(scopedExpandInfo, out _);
                    }
                }
            }

            return(retval);
        }
Exemple #21
0
        public static void MatTimesSrc(bool add, int[] mprowiv, int[] mprowcol,
                                       int[] mprowrun, int[] runs, float[] coefs,
                                       AlignedArray src, AlignedArray dst, int crow)
        {
            Contracts.AssertNonEmpty(mprowiv);
            Contracts.Assert(mprowiv.Length == crow);
            Contracts.AssertNonEmpty(mprowcol);
            Contracts.Assert(mprowcol.Length == crow);
            Contracts.Assert(mprowrun == null || mprowrun.Length == crow);
            Contracts.AssertNonEmpty(runs);
            Contracts.AssertNonEmpty(coefs);
            Contracts.Assert(Compat(src));
            Contracts.Assert(Compat(dst));
            Contracts.Assert(0 < crow && crow <= dst.Size);

            unsafe
            {
                fixed(int *pmprowiv = &mprowiv[0])
                fixed(int *pmprowcol = &mprowcol[0])
                fixed(int *pruns     = &runs[0])
                fixed(float *pcoefs  = &coefs[0])
                fixed(float *psrc    = &src.Items[0])
                fixed(float *pdst    = &dst.Items[0])
                {
                    if (mprowrun == null)
                    {
                        Thunk.MatMulCX(add, pmprowiv, pmprowcol, pruns, pcoefs,
                                       Ptr(src, psrc), Ptr(dst, pdst), crow);
                    }
                    else
                    {
                        fixed(int *pmprowrun = &mprowrun[0])
                        {
                            Thunk.MatMulDX(add, pmprowiv, pmprowcol, pmprowrun, pruns, pcoefs,
                                           Ptr(src, psrc), Ptr(dst, pdst), crow);
                        }
                    }
                }
            }
        }
Exemple #22
0
            public void AssertRep()
            {
                // Check that all fields have values.
                Contracts.AssertNonEmpty(ColumnName);
                Contracts.AssertValue(ColumnType);
                Contracts.AssertValueOrNull(Generator);

                // If Column is computed type, it must have a generator.
                Contracts.Assert(IsComputed == (Generator != null));

                // Column must have either a generator or a fieldInfo value.
                Contracts.Assert((Generator == null) != (FieldInfo == null));

                // Additional Checks if there is a generator.
                if (Generator == null)
                {
                    return;
                }
                Contracts.AssertValue(ReturnParameterInfo);

                // Checks input parameters are (someClass, long, ref value) in that order.
                var parameterInfos = Generator.GetMethodInfo().GetParameters().ToArray();
                var parameterTypes = (from pInfo in parameterInfos select pInfo.ParameterType).ToArray();

                Contracts.Assert(parameterTypes.Length == 3);
                Contracts.Assert(parameterTypes[2].IsByRef);
                Contracts.Assert(parameterTypes[1] == typeof(long));
                Contracts.Assert(!(parameterTypes[0].GetTypeInfo().IsPrimitive || parameterTypes[0] == typeof(string)));

                // Check that generator returns void.
                Contracts.Assert(Generator.GetMethodInfo().ReturnType == typeof(void));

                // Checks that the return type of the generator is compatible with ColumnType.
                bool     isVector;
                DataKind datakind;

                GetVectorAndKind(ReturnType, "return type", out isVector, out datakind);
                Contracts.Assert(isVector == ColumnType.IsVector);
                Contracts.Assert(datakind == ColumnType.ItemType.RawKind);
            }
Exemple #23
0
        /// <summary>
        /// Make a full path realtive to a base path.
        /// </summary>
        /// <param name="basepath">The base path, assumed to be a directory.</param>
        /// <param name="path">The full path.</param>
        /// <returns>The relative path.</returns>
        /// <exception cref="InvalidOperationException">If the paths are not relative.</exception>
        internal static string MakePathRelative(string basepath, string path)
        {
            Contracts.AssertNonEmpty(basepath);
            Contracts.AssertNonEmpty(path);

            Uri baseUri = new Uri(basepath);
            Uri uri     = new Uri(path);

            if (baseUri.Scheme != uri.Scheme)
            {
                throw Contracts.ExceptParam(nameof(basepath), "Paths cannot be made relative as they are of different schemes.");
            }

            string relativePath;

            try
            {
                if (!baseUri.AbsoluteUri.EndsWith("/"))
                {
                    baseUri = new Uri(baseUri.AbsoluteUri + "/");
                }

                relativePath = Uri.UnescapeDataString(baseUri.MakeRelativeUri(uri).ToString());
            }
            catch (ArgumentNullException e)
            {
                throw Contracts.Except(e, "Paths could not be made relative.");
            }
            catch (InvalidOperationException e)
            {
                throw Contracts.Except(e, "Paths could not be made relative.");
            }

            if (uri.Scheme.Equals("file", StringComparison.OrdinalIgnoreCase))
            {
                relativePath = relativePath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
            }

            return(relativePath);
        }
Exemple #24
0
        // Determines whether str needs quoting. If not, appends the string to sb and returns try.
        // If so, sb's contents are preserved and returns false.
        private static bool TryNoQuoting(string str, StringBuilder sb)
        {
            Contracts.AssertNonEmpty(str);

            if (str[0] == '{')
            {
                return(false);
            }

            int ichDst = sb.Length;

            // See if we need to quote. If lexing produces a single token with the exact
            // same value, then we don't need to.
            var curs = new CharCursor(str);
            var lex  = new CmdLexer(curs);

            lex.GetToken(sb);
            Contracts.Assert(curs.IchCur > 0 || lex.Error);

            if (!lex.Error && curs.Eof && sb.Length == ichDst + str.Length)
            {
                // See if the characters match.
                for (int ichSrc = 0; ; ichSrc++)
                {
                    if (ichSrc >= str.Length)
                    {
                        return(true);
                    }
                    if (sb[ichDst + ichSrc] != str[ichSrc])
                    {
                        break;
                    }
                }
            }

            // Reset the string builder.
            sb.Length = ichDst;
            return(false);
        }
        private static void ShowMetadataValueVec <T>(IndentedTextWriter itw, Schema schema, int col, string kind, ColumnType type)
        {
            Contracts.AssertValue(itw);
            Contracts.AssertValue(schema);
            Contracts.Assert(0 <= col && col < schema.Count);
            Contracts.AssertNonEmpty(kind);
            Contracts.AssertValue(type);
            Contracts.Assert(type.IsVector);
            Contracts.Assert(type.ItemType.RawType == typeof(T));

            var conv = Conversions.Instance.GetStringConversion <T>(type.ItemType);

            var value = default(VBuffer <T>);

            schema[col].Metadata.GetValue(kind, ref value);

            itw.Write(": Length={0}, Count={0}", value.Length, value.GetValues().Length);

            using (itw.Nest())
            {
                var sb    = default(StringBuilder);
                int count = 0;
                foreach (var item in value.Items())
                {
                    if ((count % 10) == 0)
                    {
                        itw.WriteLine();
                    }
                    else
                    {
                        itw.Write(", ");
                    }
                    var val = item.Value;
                    conv(in val, ref sb);
                    itw.Write("[{0}] '{1}'", item.Key, sb);
                    count++;
                }
            }
        }
Exemple #26
0
        public static float SumAbs(float mean, ReadOnlySpan <float> source)
        {
            Contracts.AssertNonEmpty(source);

            if (Avx.IsSupported)
            {
                return((mean == 0) ? AvxIntrinsics.SumAbsU(source) : AvxIntrinsics.SumAbsDiffU(mean, source));
            }
            else if (Sse.IsSupported)
            {
                return((mean == 0) ? SseIntrinsics.SumAbsU(source) : SseIntrinsics.SumAbsDiffU(mean, source));
            }
            else
            {
                float sum = 0;
                for (int i = 0; i < source.Length; i++)
                {
                    sum += Math.Abs(source[i] - mean);
                }
                return(sum);
            }
        }
Exemple #27
0
        public static float SumSq(float mean, ReadOnlySpan <float> source)
        {
            Contracts.AssertNonEmpty(source);

            if (Avx.IsSupported)
            {
                return((mean == 0) ? AvxIntrinsics.SumSqU(source) : AvxIntrinsics.SumSqDiffU(mean, source));
            }
            else if (Sse.IsSupported)
            {
                return((mean == 0) ? SseIntrinsics.SumSqU(source) : SseIntrinsics.SumSqDiffU(mean, source));
            }
            else
            {
                float result = 0;
                for (int i = 0; i < source.Length; i++)
                {
                    result += (source[i] - mean) * (source[i] - mean);
                }
                return(result);
            }
        }
Exemple #28
0
        public static float SumSq(ReadOnlySpan <float> source)
        {
            Contracts.AssertNonEmpty(source);

            if (Avx.IsSupported)
            {
                return(AvxIntrinsics.SumSqU(source));
            }
            else if (Sse.IsSupported)
            {
                return(SseIntrinsics.SumSqU(source));
            }
            else
            {
                float result = 0;
                for (int i = 0; i < source.Length; i++)
                {
                    result += source[i] * source[i];
                }
                return(result);
            }
        }
            protected override bool TryParse(string str)
            {
                Contracts.AssertNonEmpty(str);

                // We accept N:T:S where N is the new column name, T is the new type,
                // and S is source column names.
                if (!base.TryParse(str, out string extra))
                {
                    return(false);
                }
                if (extra == null)
                {
                    return(true);
                }

                if (!TypeParsingUtils.TryParseDataKind(extra, out DataKind kind, out KeyRange))
                {
                    return(false);
                }
                ResultType = kind == default ? default(DataKind?) : kind;
                return(true);
            }
Exemple #30
0
        /// <summary>
        /// Append a field declaration to the provided <see cref="StringBuilder"/>.
        /// </summary>
        public static void AppendFieldDeclaration(CSharpCodeProvider codeProvider, StringBuilder target, int columnIndex,
                                                  string fieldName, ColumnType colType, bool appendInitializer, bool useVBuffer)
        {
            Contracts.AssertValueOrNull(codeProvider);
            Contracts.AssertValue(target);
            Contracts.Assert(columnIndex >= 0);
            Contracts.AssertNonEmpty(fieldName);
            Contracts.AssertValue(colType);

            var    attributes          = new List <string>();
            string generatedCsTypeName = GetBackingTypeName(colType, useVBuffer, attributes);

            if (codeProvider != null && !codeProvider.IsValidIdentifier(fieldName))
            {
                attributes.Add(string.Format("[ColumnName({0})]", GetCSharpString(codeProvider, fieldName)));
                fieldName = string.Format("Column{0}", columnIndex);
            }

            const string indent = "            ";

            if (attributes.Count > 0)
            {
                foreach (var attr in attributes)
                {
                    target.Append(indent);
                    target.AppendLine(attr);
                }
            }
            target.Append(indent);
            target.AppendFormat("public {0} {1}", generatedCsTypeName, fieldName);

            if (appendInitializer && colType is VectorType vecColType && vecColType.Size > 0 && !useVBuffer)
            {
                Contracts.Assert(generatedCsTypeName.EndsWith("[]"));
                var csItemType = generatedCsTypeName.Substring(0, generatedCsTypeName.Length - 2);
                target.AppendFormat(" = new {0}[{1}]", csItemType, vecColType.Size);
            }
            target.AppendLine(";");
        }