/// <summary>
 /// This ctor accepts any value for ticks, but produces an NA if ticks is out of the legal range.
 /// </summary>
 public DvDateTime(DvInt8 ticks)
 {
     if ((ulong)ticks.RawValue > MaxTicks)
     {
         _ticks = DvInt8.NA;
     }
     else
     {
         _ticks = ticks;
     }
     AssertValid();
 }
        /// <summary>
        /// Given a number of ticks for the date time portion and a number of minutes for
        /// the time zone offset, this constructs a new DvDateTimeZone. If anything is invalid,
        /// it produces NA.
        /// </summary>
        /// <param name="ticks">The number of clock ticks in the date time portion</param>
        /// <param name="offset">The time zone offset in minutes</param>
        public DvDateTimeZone(DvInt8 ticks, DvInt2 offset)
        {
            var dt = new DvDateTime(ticks);

            if (dt.IsNA || offset.IsNA || MinMinutesOffset > offset.RawValue || offset.RawValue > MaxMinutesOffset)
            {
                _dateTime = DvDateTime.NA;
                _offset   = DvInt2.NA;
            }
            else
            {
                _offset   = offset;
                _dateTime = ValidateDate(dt, ref _offset);
            }
            AssertValid();
        }
        /// <summary>
        /// This method takes a TimeSpan offset, validates that it is a legal offset for DvDateTimeZone (i.e.
        /// in whole minutes, and between -14 and 14 hours), and returns the offset in number of minutes.
        /// </summary>
        /// <param name="offsetTicks"></param>
        /// <param name="offset"></param>
        /// <returns></returns>
        private static bool TryValidateOffset(DvInt8 offsetTicks, out DvInt2 offset)
        {
            if (offsetTicks.IsNA || offsetTicks.RawValue % TicksPerMinute != 0)
            {
                offset = DvInt2.NA;
                return(false);
            }

            long  mins = offsetTicks.RawValue / TicksPerMinute;
            short res  = (short)mins;

            if (res != mins || res > MaxMinutesOffset || res < MinMinutesOffset)
            {
                offset = DvInt2.NA;
                return(false);
            }
            offset = res;
            Contracts.Assert(!offset.IsNA);
            return(true);
        }
Esempio n. 4
0
 public void Conv(ref long?src, ref DvInt8 dst) => dst = src ?? DvInt8.NA;
Esempio n. 5
0
        protected override void PrintFoldResultsCore(IChannel ch, Dictionary <string, IDataView> metrics)
        {
            IDataView top;

            if (!metrics.TryGetValue(AnomalyDetectionEvaluator.TopKResults, out top))
            {
                throw Host.Except("Did not find the top-k results data view");
            }
            var sb = new StringBuilder();

            using (var cursor = top.GetRowCursor(col => true))
            {
                int index;
                if (!top.Schema.TryGetColumnIndex(AnomalyDetectionEvaluator.TopKResultsColumns.Instance, out index))
                {
                    throw Host.Except("Data view does not contain the 'Instance' column");
                }
                var instanceGetter = cursor.GetGetter <DvText>(index);
                if (!top.Schema.TryGetColumnIndex(AnomalyDetectionEvaluator.TopKResultsColumns.AnomalyScore, out index))
                {
                    throw Host.Except("Data view does not contain the 'Anomaly Score' column");
                }
                var scoreGetter = cursor.GetGetter <Single>(index);
                if (!top.Schema.TryGetColumnIndex(AnomalyDetectionEvaluator.TopKResultsColumns.Label, out index))
                {
                    throw Host.Except("Data view does not contain the 'Label' column");
                }
                var labelGetter = cursor.GetGetter <Single>(index);

                bool hasRows = false;
                while (cursor.MoveNext())
                {
                    if (!hasRows)
                    {
                        sb.AppendFormat("{0} Top-scored Results", _topScored);
                        sb.AppendLine();
                        sb.AppendLine("=================================================");
                        sb.AppendLine("Instance    Anomaly Score     Labeled");
                        hasRows = true;
                    }
                    var    name  = default(DvText);
                    Single score = 0;
                    Single label = 0;
                    instanceGetter(ref name);
                    scoreGetter(ref score);
                    labelGetter(ref label);
                    sb.AppendFormat("{0,-10}{1,12:G4}{2,12}", name, score, label);
                    sb.AppendLine();
                }
            }
            if (sb.Length > 0)
            {
                ch.Info(MessageSensitivity.UserData, sb.ToString());
            }

            IDataView overall;

            if (!metrics.TryGetValue(MetricKinds.OverallMetrics, out overall))
            {
                throw Host.Except("No overall metrics found");
            }

            // Find the number of anomalies, and the thresholds.
            int numAnomIndex;

            if (!overall.Schema.TryGetColumnIndex(AnomalyDetectionEvaluator.OverallMetrics.NumAnomalies, out numAnomIndex))
            {
                throw Host.Except("Could not find the 'NumAnomalies' column");
            }

            int  stratCol;
            var  hasStrat = overall.Schema.TryGetColumnIndex(MetricKinds.ColumnNames.StratCol, out stratCol);
            int  stratVal;
            bool hasStratVals = overall.Schema.TryGetColumnIndex(MetricKinds.ColumnNames.StratVal, out stratVal);

            Contracts.Assert(hasStrat == hasStratVals);
            DvInt8 numAnomalies = 0;

            using (var cursor = overall.GetRowCursor(col => col == numAnomIndex ||
                                                     (hasStrat && col == stratCol)))
            {
                var numAnomGetter = cursor.GetGetter <DvInt8>(numAnomIndex);
                ValueGetter <uint> stratGetter = null;
                if (hasStrat)
                {
                    var type = cursor.Schema.GetColumnType(stratCol);
                    stratGetter = RowCursorUtils.GetGetterAs <uint>(type, cursor, stratCol);
                }
                bool foundRow = false;
                while (cursor.MoveNext())
                {
                    uint strat = 0;
                    if (stratGetter != null)
                    {
                        stratGetter(ref strat);
                    }
                    if (strat > 0)
                    {
                        continue;
                    }
                    if (foundRow)
                    {
                        throw Host.Except("Found multiple non-stratified rows in overall results data view");
                    }
                    foundRow = true;
                    numAnomGetter(ref numAnomalies);
                }
            }

            var args = new ChooseColumnsTransform.Arguments();
            var cols = new List <ChooseColumnsTransform.Column>()
            {
                new ChooseColumnsTransform.Column()
                {
                    Name   = string.Format(FoldDrAtKFormat, _k),
                    Source = AnomalyDetectionEvaluator.OverallMetrics.DrAtK
                },
                new ChooseColumnsTransform.Column()
                {
                    Name   = string.Format(FoldDrAtPFormat, _p),
                    Source = AnomalyDetectionEvaluator.OverallMetrics.DrAtPFpr
                },
                new ChooseColumnsTransform.Column()
                {
                    Name   = string.Format(FoldDrAtNumAnomaliesFormat, numAnomalies),
                    Source = AnomalyDetectionEvaluator.OverallMetrics.DrAtNumPos
                },
                new ChooseColumnsTransform.Column()
                {
                    Name = AnomalyDetectionEvaluator.OverallMetrics.ThreshAtK
                },
                new ChooseColumnsTransform.Column()
                {
                    Name = AnomalyDetectionEvaluator.OverallMetrics.ThreshAtP
                },
                new ChooseColumnsTransform.Column()
                {
                    Name = AnomalyDetectionEvaluator.OverallMetrics.ThreshAtNumPos
                },
                new ChooseColumnsTransform.Column()
                {
                    Name = BinaryClassifierEvaluator.Auc
                }
            };

            args.Column = cols.ToArray();
            IDataView fold = new ChooseColumnsTransform(Host, args, overall);
            string    weightedFold;

            ch.Info(MetricWriter.GetPerFoldResults(Host, fold, out weightedFold));
        }
 public DvTimeSpan(SysTimeSpan?sts)
 {
     _ticks = sts != null?sts.GetValueOrDefault().Ticks : DvInt8.NA;
 }
 public DvTimeSpan(SysTimeSpan sts)
 {
     _ticks = sts.Ticks;
 }
 public DvTimeSpan(DvInt8 ticks)
 {
     _ticks = ticks;
 }
 /// <summary>
 /// This ctor initializes _ticks to the value of sdt.Ticks, and ignores its DateTimeKind value.
 /// </summary>
 public DvDateTime(SysDateTime sdt)
 {
     _ticks = sdt.Ticks;
     AssertValid();
 }