Esempio n. 1
0
        /// <summary>
        /// Raises an exception if two columns do not have the same
        /// shape or are two much different.
        /// </summary>
        /// <param name="col">columns</param>
        /// <param name="precision">precision</param>
        /// <param name="exc">raises an exception if too different</param>
        /// <returns>max difference</returns>
        public double AssertAlmostEqual(IDataColumn col, double precision = 1e-5, bool exc = true)
        {
            var colt = (col is NumericColumn ? (col as NumericColumn).Column : col) as DataColumn <DType>;

            if (colt is null)
            {
                throw new DataValueError(string.Format("Column types are different {0} != {1}",
                                                       GetType(), col.GetType()));
            }
            if (Length != colt.Length)
            {
                throw new DataValueError(string.Format("Column have different length {0} != {1}",
                                                       Length, colt.Length));
            }
            if (Kind.IsVector())
            {
                double oks = 0;
                for (int i = 0; i < Length; ++i)
                {
                    switch (Kind.ItemType().RawKind())
                    {
                    case DataKind.Boolean:
                        oks += NumericHelper.AssertAlmostEqual((_data as VBufferEqSort <bool>[])[i].DenseValues().ToArray(),
                                                               (colt._data as VBufferEqSort <bool>[])[i].DenseValues().ToArray(),
                                                               precision, exc);
                        break;

                    case DataKind.Int32:
                        oks += NumericHelper.AssertAlmostEqual((_data as VBufferEqSort <int>[])[i].DenseValues().ToArray(),
                                                               (colt._data as VBufferEqSort <int>[])[i].DenseValues().ToArray(),
                                                               precision, exc);
                        break;

                    case DataKind.UInt32:
                        oks += NumericHelper.AssertAlmostEqual((_data as VBufferEqSort <uint>[])[i].DenseValues().ToArray(),
                                                               (colt._data as VBufferEqSort <uint>[])[i].DenseValues().ToArray(),
                                                               precision, exc);
                        break;

                    case DataKind.Int64:
                        oks += NumericHelper.AssertAlmostEqual((_data as VBufferEqSort <Int64>[])[i].DenseValues().ToArray(),
                                                               (colt._data as VBufferEqSort <Int64>[])[i].DenseValues().ToArray(),
                                                               precision, exc);
                        break;

                    case DataKind.Single:
                        oks += NumericHelper.AssertAlmostEqual((_data as VBufferEqSort <float>[])[i].DenseValues().ToArray(),
                                                               (colt._data as VBufferEqSort <float>[])[i].DenseValues().ToArray(),
                                                               precision, exc);
                        break;

                    case DataKind.Double:
                        oks += NumericHelper.AssertAlmostEqual((_data as VBufferEqSort <double>[])[i].DenseValues().ToArray(),
                                                               (colt._data as VBufferEqSort <double>[])[i].DenseValues().ToArray(),
                                                               precision, exc);
                        break;

                    case DataKind.String:
                        oks += NumericHelper.AssertAlmostEqual((_data as VBufferEqSort <DvText>[])[i].DenseValues().ToArray(),
                                                               (colt._data as VBufferEqSort <DvText>[])[i].DenseValues().ToArray(),
                                                               precision, exc);
                        break;

                    default:
                        throw new DataTypeError($"Unable to handle kind '{Kind}'");
                    }
                }
                return(oks);
            }
            else
            {
                switch (Kind.RawKind())
                {
                case DataKind.Boolean:
                    return(NumericHelper.AssertAlmostEqual(_data as bool[], colt._data as bool[], precision, exc, Length, colt.Length));

                case DataKind.Int32:
                    return(NumericHelper.AssertAlmostEqual(_data as int[], colt._data as int[], precision, exc, Length, colt.Length));

                case DataKind.UInt32:
                    return(NumericHelper.AssertAlmostEqual(_data as uint[], colt._data as uint[], precision, exc, Length, colt.Length));

                case DataKind.Int64:
                    return(NumericHelper.AssertAlmostEqual(_data as long[], colt._data as long[], precision, exc, Length, colt.Length));

                case DataKind.Single:
                    return(NumericHelper.AssertAlmostEqual(_data as float[], colt._data as float[], precision, exc, Length, colt.Length));

                case DataKind.Double:
                    return(NumericHelper.AssertAlmostEqual(_data as double[], colt._data as double[], precision, exc, Length, colt.Length));

                case DataKind.String:
                    return(NumericHelper.AssertAlmostEqual(_data as DvText[], colt._data as DvText[], precision, exc, Length, colt.Length));

                default:
                    throw new DataTypeError($"Unable to handle kind '{Kind}'");
                }
            }
        }