private void SummaryData_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
 {
     if (e.Action == NotifyCollectionChangedAction.Add)
     {
         foreach (var item in e.NewItems)
         {
             IntervalData <double> range = (IntervalData <double>)item;
             this.AddRange(range.OriginatingTime, range.Value, range.Minimum, range.Maximum);
         }
     }
     else if (e.Action == NotifyCollectionChangedAction.Reset)
     {
         this.InvalidateSegments();
         if (this.PlotVisualizationObject.SummaryData != null && this.PlotVisualizationObject.SummaryData.Count > 0)
         {
             this.CalculateXTransform();
             this.CalculateYTransform();
             foreach (var range in this.PlotVisualizationObject.SummaryData)
             {
                 this.AddRange(range.OriginatingTime, range.Value, range.Minimum, range.Maximum);
             }
         }
     }
     else if (e.Action == NotifyCollectionChangedAction.Replace)
     {
         // Currently, the only time we get hit with a replace is on the boundary intervals of a
         // range of new intervals being added, and those will be included in the Add notification,
         // so we handle them anyway. The old value being replaced should already have been removed
         // when its containing segment was removed.
     }
     else
     {
         throw new NotImplementedException($"PlotVisualizationObjectView.SummaryData_CollectionChanged: Unexpected collectionChanged {e.Action} action.");
     }
 }
Exemplo n.º 2
0
        public bool CalculateCDI()
        {
            DateTime     timestamp = _parentStrategy.IntervalTimestamp;
            IntervalData interval  = _intervals.Where(i => i._timeStamp == timestamp).FirstOrDefault();

            return((interval != null) ? _parentStrategy.CalculateCDI(interval) : false);
        }
Exemplo n.º 3
0
        private void InternalAdd(Person person, string modalName, string subModalName, TimeSpan startTime, TimeSpan endTime, double?value, string subtitle)
        {
            try
            {
                var subModalPersonInScene = TypeValidation.GetSmtPis(person, modalName, subModalName);

                //create interval and add to smtPis
                IntervalData intervalElement = new IntervalData()
                {
                    StartTime = startTime,
                    EndTime   = endTime
                };
                subModalPersonInScene.RepresentTypes.Add(new RepresentType()
                {
                    Value        = value,
                    Subtitle     = subtitle,
                    Index        = null,
                    IntervalData = intervalElement,
                    SubModalType_PersonInScene = subModalPersonInScene
                });
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }
        }
Exemplo n.º 4
0
        public string GetIntervalWidth(IntervalData interval)
        {
            double periodSec   = (ToTime - FromTime).TotalSeconds;
            double intervalSec = (interval.ToTime - interval.FromTime).TotalSeconds;
            double width       = intervalSec / periodSec * 99;

            return(Math.Round(width, 4) + "%");
        }
        private void GetIntervalData(int N)
        {
            iD = new IntervalData();

            for (int i = 0; i < bFI.Count; i++)
            {
                iD.Add((int)(bFI[i] * N / _fd), (int)(eFI[i] * N / _fd)); // Перевод частот в отсчеты
            }
        }
        public bool SaveIntervalDatas(string conString)
        {
            var context = new ExcelEntities();

            try
            {
                using (OleDbConnection connExcel = new OleDbConnection(conString))
                {
                    using (OleDbCommand cmdExcel = new OleDbCommand())
                    {
                        using (OleDbDataAdapter odaExcel = new OleDbDataAdapter())
                        {
                            DataTable dt = new DataTable();
                            cmdExcel.Connection = connExcel;

                            connExcel.Open();
                            DataTable dtExcelSchema;
                            dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                            string sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
                            connExcel.Close();

                            connExcel.Open();
                            cmdExcel.CommandText   = "SELECT * From [" + sheetName + "]";
                            odaExcel.SelectCommand = cmdExcel;
                            odaExcel.Fill(dt);
                            connExcel.Close();

                            foreach (DataRow row in dt.Rows)
                            {
                                var id = new IntervalData
                                {
                                    DeliveryPoint = long.Parse(row.ItemArray[0].ToString()),
                                    Date          = DateTime.Parse(row.ItemArray[1].ToString()),
                                    TimeSlot      = new TimeSpan(Convert.ToInt32(row.ItemArray[2].ToString())),
                                    SlotVal       = Decimal.Parse(row.ItemArray[3].ToString())
                                };
                                context.IntervalDatas.Add(id);
                            }
                            context.SaveChanges();
                            return(true);
                        }
                    }
                }
            }
            catch
            {
                return(false);
            }
        }
Exemplo n.º 7
0
        public static void RunIntervalCallbacks(System.Object[] callbacks, IFn onError)
        {
            var now = DateTime.Now;

            for (int i = 0; i < callbacks.Length; i++)
            {
                IntervalData d = (IntervalData)callbacks[i];
                if (d.lastTrigger == null || (now - d.lastTrigger).TotalMilliseconds >= d.interval)
                {
                    d.lastTrigger = now;
                    try {
                        d.f.invoke();
                    } catch (Exception e) {
                        onError.invoke(e, d);
                    }
                }
            }
        }
Exemplo n.º 8
0
 /// <summary>
 /// Summarizes an enumerable of double messages into summarized doubles.
 /// </summary>
 /// <param name="messages">Enumerable of double messages.</param>
 /// <param name="interval">The time interval each summary value should cover.</param>
 /// <returns>List of summarized doubles.</returns>
 public static List <IntervalData <double> > Summarizer(IEnumerable <Message <double> > messages, TimeSpan interval)
 {
     return(messages
            .OrderBy(msg => msg.OriginatingTime)
            .GroupBy(msg => GetIntervalStartTime(msg.OriginatingTime, interval))
            .Select(
                group =>
     {
         var firstMessage = group.First();
         var lastMessage = group.Last();
         return IntervalData.Create(
             lastMessage.Data,                                            // Take last value as representative value for plotting
             group.Min(m => m.Data),                                      // Minimum value
             group.Max(m => m.Data),                                      // Maximum value
             firstMessage.OriginatingTime,                                // First message's OT
             lastMessage.OriginatingTime - firstMessage.OriginatingTime); // Interval between first and last messages
     }).ToList());
 }
Exemplo n.º 9
0
 private static List <IntervalData <object> > Summarizer(IEnumerable <Message <T> > messages, TimeSpan interval)
 {
     return(messages
            .OrderBy(msg => msg.OriginatingTime)
            .GroupBy(msg => GetIntervalStartTime(msg.OriginatingTime, interval))
            .Select(
                group =>
     {
         var firstMessage = group.First();
         var lastMessage = group.Last();
         var representative = (object)firstMessage.Data;
         return IntervalData.Create(
             representative,
             default(object),
             default(object),
             firstMessage.OriginatingTime,                                // First message's OT
             lastMessage.OriginatingTime - firstMessage.OriginatingTime); // Interval between first and last messages
     }).ToList());
 }
Exemplo n.º 10
0
        /// <summary>
        /// Summarizes an enumerable of double messages into summarized doubles.
        /// </summary>
        /// <typeparam name="T">The type of messages to summarize.</typeparam>
        /// <param name="messages">Enumerable of double messages.</param>
        /// <param name="interval">The time interval each summary value should cover.</param>
        /// <returns>List of summarized doubles.</returns>
        internal static List <IntervalData <T> > Summarizer <T>(IEnumerable <Message <T> > messages, TimeSpan interval)
        {
            return(messages
                   .OrderBy(msg => msg.OriginatingTime)
                   .GroupBy(msg => Summarizer <T, T> .GetIntervalStartTime(msg.OriginatingTime, interval))
                   .Select(
                       group =>
            {
                var firstMessage = group.First();
                var lastMessage = group.Last();

                // Use the last value as representative for summarization, with the first message
                // originating time.
                return IntervalData.Create(
                    value: lastMessage.Data,
                    minimum: group.Min(m => m.Data),
                    maximum: group.Max(m => m.Data),
                    originatingTime: firstMessage.OriginatingTime,
                    interval: lastMessage.OriginatingTime - firstMessage.OriginatingTime);
            }).ToList());
        }
Exemplo n.º 11
0
        /// <summary>
        /// Summarizes an enumerable of time interval messages into summarized time intervals.
        /// </summary>
        /// <param name="messages">Enumerable of time interval messages.</param>
        /// <param name="interval">The time interval each summary value should cover.</param>
        /// <returns>List of summarized time intervals.</returns>
        public static List <IntervalData <Tuple <DateTime, DateTime> > > Summarizer(IEnumerable <Message <Tuple <DateTime, DateTime> > > messages, TimeSpan interval)
        {
            return(messages
                   .OrderBy(msg => msg.OriginatingTime)
                   .GroupBy(msg => GetIntervalStartTime(msg.OriginatingTime, interval))
                   .Select(
                       group =>
            {
                var firstMessage = group.First();
                var lastMessage = group.Last();
                var ascendingLatencies = group.OrderBy(m => m.Data.Item2 - m.Data.Item1).Select(m => m.Data);
                var minLatency = ascendingLatencies.First();
                var maxLatency = ascendingLatencies.Last();

                // Use max latency as representative value
                return IntervalData.Create(
                    maxLatency,
                    minLatency,
                    maxLatency,
                    firstMessage.OriginatingTime,
                    lastMessage.OriginatingTime - firstMessage.OriginatingTime);
            }).ToList());
        }
        /// <summary>calculate the CDI using a rolling percentile lookup</summary>
        public bool CalculateCDI(IntervalData interval)
        {
            IntervalData normalized;

            return(_cdiPercentile.CDI1(interval, out normalized));
        }
Exemplo n.º 13
0
        /// <summary>
        /// Summarizes an enumerable of audio buffer messages into summarized audio data.
        /// </summary>
        /// <param name="messages">Enumerable of audio buffer messages.</param>
        /// <param name="interval">The time interval each summary value should cover.</param>
        /// <param name="channel">The audio channel to summarize.</param>
        /// <returns>List of summarized audio data.</returns>
        public static List <IntervalData <double> > Summarizer(IEnumerable <Message <AudioBuffer> > messages, TimeSpan interval, ushort channel)
        {
            var intervalData = new List <IntervalData <double> >();

            // Inspect the first message and only continue if it exists
            Message <AudioBuffer> first = messages.FirstOrDefault();

            if (first != default(Message <AudioBuffer>))
            {
                // Assume audio format is the same for all items
                WaveFormat audioFormat = first.Data.Format;

                // Get the appropriate sample converter. Assumption is 16-bit samples
                // are integer and 32-bit samples are floating point values.
                Func <byte[], int, double> sampleConverter;
                switch (audioFormat.BitsPerSample)
                {
                case 16:
                    sampleConverter = (buf, i) => BitConverter.ToInt16(buf, i);
                    break;

                case 32:
                    sampleConverter = (buf, i) => BitConverter.ToSingle(buf, i);
                    break;

                default:
                    sampleConverter = (buf, i) => buf[i];
                    break;
                }

                TimeSpan samplingInterval = TimeSpan.FromTicks(TimeSpan.TicksPerSecond / audioFormat.SamplesPerSec);
                int      byteOffset       = 0 + ((channel % audioFormat.Channels) * audioFormat.BitsPerSample / 8);

                DateTime currentIntervalStartTime = GetIntervalStartTime(first.OriginatingTime, interval);
                double   min = double.MaxValue;
                double   max = double.MinValue;

                foreach (var message in messages)
                {
                    // Each item holds an AudioBuffer containing multiple audio samples
                    AudioBuffer audioBuffer = message.Data;

                    // Start (originating time) of the first sample in the AudioBuffer
                    DateTime currentTime = message.OriginatingTime - audioBuffer.Duration;

                    // calculate min/max for specified channel over the entire audio buffer
                    for (int offset = byteOffset; offset < audioBuffer.Length; offset += audioFormat.BlockAlign)
                    {
                        DateTime intervalStartTime = GetIntervalStartTime(currentTime, interval);

                        // Check if we are still in the current interval. If not, commit the current
                        // min/max values to the current interval if they have non-default values.
                        if ((intervalStartTime != currentIntervalStartTime) && (min != double.MaxValue) && (max != double.MinValue))
                        {
                            var current = IntervalData.Create(min + ((max - min) / 2), min, max, currentIntervalStartTime, interval);
                            intervalData.Add(current);

                            // Reset min-max for next interval
                            min = double.MaxValue;
                            max = double.MinValue;

                            // Update next interval start time
                            currentIntervalStartTime = intervalStartTime;
                        }

                        // Update min-max range for the current interval
                        double sampleValue = sampleConverter(audioBuffer.Data, offset);
                        if (sampleValue < min)
                        {
                            min = sampleValue;
                        }

                        if (sampleValue > max)
                        {
                            max = sampleValue;
                        }

                        // Increment by the sampling interval
                        currentTime += samplingInterval;
                    }
                }

                // Add the last interval
                intervalData.Add(IntervalData.Create(min + ((max - min) / 2), min, max, currentIntervalStartTime, interval));
            }

            return(intervalData);
        }
Exemplo n.º 14
0
        /// <summary>
        /// Summarizes an enumerable of double messages into summarized doubles.
        /// </summary>
        /// <typeparam name="TKey">The type of the series key.</typeparam>
        /// <typeparam name="T">The type of messages to summarize.</typeparam>
        /// <param name="messages">Enumerable of double messages.</param>
        /// <param name="interval">The time interval each summary value should cover.</param>
        /// <returns>List of summarized doubles.</returns>
        internal static List <IntervalData <Dictionary <TKey, T> > > SeriesSummarizer <TKey, T>(IEnumerable <Message <Dictionary <TKey, T> > > messages, TimeSpan interval)
        {
            return(messages
                   .OrderBy(msg => msg.OriginatingTime)
                   .GroupBy(msg => Summarizer <T, T> .GetIntervalStartTime(msg.OriginatingTime, interval))
                   .Select(
                       group =>
            {
                var firstMessage = group.First();
                var lastMessage = group.Last();
                var min = new Dictionary <TKey, T>();
                var max = new Dictionary <TKey, T>();

                foreach (var message in group)
                {
                    foreach (var kvp in message.Data)
                    {
                        // Update min
                        if (!min.ContainsKey(kvp.Key))
                        {
                            min.Add(kvp.Key, kvp.Value);
                        }
                        else if (kvp.Value is IComparable <T> comparable)
                        {
                            if (comparable.CompareTo(min[kvp.Key]) == -1)
                            {
                                min[kvp.Key] = kvp.Value;
                            }
                        }
                        else if (kvp.Value is IComparable untypedComparable)
                        {
                            if (untypedComparable.CompareTo(min[kvp.Key]) == -1)
                            {
                                min[kvp.Key] = kvp.Value;
                            }
                        }
                        else
                        {
                            throw new InvalidOperationException("Cannot summarize over values that are not comparable.");
                        }

                        // Update max
                        if (!max.ContainsKey(kvp.Key))
                        {
                            max.Add(kvp.Key, kvp.Value);
                        }
                        else if (kvp.Value is IComparable <T> comparable)
                        {
                            if (comparable.CompareTo(max[kvp.Key]) == 1)
                            {
                                max[kvp.Key] = kvp.Value;
                            }
                        }
                        else if (kvp.Value is IComparable untypedComparable)
                        {
                            if (untypedComparable.CompareTo(max[kvp.Key]) == 1)
                            {
                                max[kvp.Key] = kvp.Value;
                            }
                        }
                        else
                        {
                            throw new InvalidOperationException("Cannot summarize over values that are not comparable.");
                        }
                    }
                }

                // Use the last value as representative for summarization, with the first message
                // originating time.
                return IntervalData.Create(
                    value: lastMessage.Data,
                    minimum: min,
                    maximum: max,
                    originatingTime: firstMessage.OriginatingTime,
                    interval: lastMessage.OriginatingTime - firstMessage.OriginatingTime);
            }).ToList());
        }
Exemplo n.º 15
0
        /// <summary>
        /// Combines two numeric series interval data values.
        /// </summary>
        /// <typeparam name="TKey">The type of the series key.</typeparam>
        /// <typeparam name="T">The type of messages to summarize.</typeparam>
        /// <param name="left">The first value to combine.</param>
        /// <param name="right">The second value to combine.</param>
        /// <returns>The combined value.</returns>
        internal static IntervalData <Dictionary <TKey, T> > SeriesCombiner <TKey, T>(IntervalData <Dictionary <TKey, T> > left, IntervalData <Dictionary <TKey, T> > right)
        {
            Comparer <T> comparer = Comparer <T> .Default;
            var          min      = new Dictionary <TKey, T>();

            foreach (var key in left.Minimum.Keys.Union(right.Minimum.Keys).Distinct())
            {
                if (left.Minimum.ContainsKey(key))
                {
                    if (right.Minimum.ContainsKey(key))
                    {
                        min.Add(key, comparer.Compare(left.Minimum[key], right.Minimum[key]) < 0 ? left.Minimum[key] : right.Minimum[key]);
                    }
                    else
                    {
                        min.Add(key, left.Minimum[key]);
                    }
                }
                else
                {
                    min.Add(key, right.Minimum[key]);
                }
            }

            var max = new Dictionary <TKey, T>();

            foreach (var key in left.Maximum.Keys.Union(right.Maximum.Keys).Distinct())
            {
                if (left.Maximum.ContainsKey(key))
                {
                    if (right.Maximum.ContainsKey(key))
                    {
                        max.Add(key, comparer.Compare(left.Maximum[key], right.Maximum[key]) > 0 ? left.Maximum[key] : right.Maximum[key]);
                    }
                    else
                    {
                        max.Add(key, left.Maximum[key]);
                    }
                }
                else
                {
                    max.Add(key, right.Maximum[key]);
                }
            }

            Dictionary <TKey, T> value;
            DateTime             originatingTime;

            // Take the value which occurs last, and the time which occurs first
            if (left.OriginatingTime <= right.OriginatingTime)
            {
                value           = right.Value;
                originatingTime = left.OriginatingTime;
            }
            else
            {
                value           = left.Value;
                originatingTime = right.OriginatingTime;
            }

            // Take the whichever end time occurs last and use it to find the interval
            TimeSpan interval = (right.EndTime > left.EndTime ? right.EndTime : left.EndTime) - originatingTime;

            return(IntervalData.Create(value, min, max, originatingTime, interval));
        }
Exemplo n.º 16
0
        public void Init(IntervalData[] intervals, long count)
        {
            if (intervals == null)
            {
                throw new ArgumentNullException("intervals");
            }
            Count        = count;
            RowIntervals = intervals;

            RowIntervals = RowIntervals.OrderBy(x => x.FromTime).ToArray();
            var resultIntervals = new List <IntervalData>();

            var          lastTime           = FromTime;
            IntervalData lastResultInterval = null;

            foreach (var intervalData in RowIntervals)
            {
                if (intervalData.ToTime < FromTime)
                {
                    continue;
                }
                if (intervalData.FromTime > ToTime)
                {
                    continue;
                }
                // проверим есть ли пробел
                if (intervalData.FromTime > lastTime)
                {
                    var space = new IntervalData()
                    {
                        FromTime = lastTime,
                        ToTime   = intervalData.FromTime,
                        Color    = NoDataColor,
                        IsSpace  = true
                    };
                    resultIntervals.Add(space);
                    lastResultInterval = new IntervalData()
                    {
                        FromTime = intervalData.FromTime,
                        ToTime   = intervalData.ToTime,
                        Color    = DataColor
                    };
                    resultIntervals.Add(lastResultInterval);
                    lastTime = lastResultInterval.ToTime;
                }
                else // пробела нет
                {
                    // первый интервал
                    if (lastResultInterval == null)
                    {
                        lastResultInterval = new IntervalData()
                        {
                            FromTime = intervalData.FromTime,
                            ToTime   = intervalData.ToTime,
                            Color    = DataColor
                        };
                        resultIntervals.Add(lastResultInterval);
                    }
                    // проверим можно ли увеличить колбаску
                    else if (intervalData.ToTime > lastResultInterval.ToTime)
                    {
                        lastResultInterval.ToTime = intervalData.ToTime;
                    }
                }
                lastTime = lastResultInterval.ToTime;
            }
            if (resultIntervals.Count > 0)
            {
                var last = resultIntervals.Last();
                if (last.ToTime < ToTime)
                {
                    var space = new IntervalData()
                    {
                        FromTime = last.ToTime,
                        ToTime   = ToTime,
                        Color    = NoDataColor,
                        IsSpace  = true
                    };
                    resultIntervals.Add(space);
                }
            }

            if (resultIntervals.Count > 0)
            {
                var first = resultIntervals.First();
                if (first.FromTime < FromTime)
                {
                    first.FromTime = FromTime;
                }
                var last = resultIntervals.Last();
                if (last.ToTime > ToTime)
                {
                    last.ToTime = ToTime;
                }
            }

            // статистика
            double coverageSec = 0;
            int    index       = 0;

            foreach (var resultInterval in resultIntervals)
            {
                if (resultInterval.IsSpace == false)
                {
                    coverageSec += (resultInterval.ToTime - resultInterval.FromTime).TotalSeconds;
                }
                resultInterval.Index = index;
                index++;
            }

            Coverage        = coverageSec / ((ToTime - FromTime).TotalSeconds);
            ResultIntervals = resultIntervals.ToArray();
        }