예제 #1
0
        internal void TagActivity(string tag, SensorEventViewModel sensorEvent)
        {
            sensorEvent.Activity = GetActivityByName(tag);
            int stringIndex = _dictDateEvents[CurrentDate].Offset + _allEventsInView.IndexOf(sensorEvent);

            _eventStringList[stringIndex] = sensorEvent.ToString();
        }
예제 #2
0
        private void ApplyFilter()
        {
            bool IsActivityFilterEnabled = EventViewFilter.IsActivityFilterEnabled;

            // Apply filter to all events
            for (int idEvent = 0; idEvent < _allEventsInView.Count; idEvent++)
            {
                SensorEventViewModel sensorEvent = _allEventsInView[idEvent];
                sensorEvent.IsFiltered         = !ApplyStaticFilter(sensorEvent);
                sensorEvent.IsActivityFiltered = !ApplyActivityFilter(sensorEvent);
                sensorEvent.IsExpandedInView   = false;
                sensorEvent.IsStartOfSegment   = false;
                sensorEvent.IsEndOfSegment     = false;
            }
            if (IsActivityFilterEnabled)
            {
                for (int idEvent = 0; idEvent < _allEventsInView.Count; idEvent++)
                {
                    SensorEventViewModel sensorEvent = _allEventsInView[idEvent];
                    if (sensorEvent.IsActivityFiltered)
                    {
                        ExpandFilteredEvents(idEvent);
                    }
                }
            }
            EventsInView.Refresh();
        }
예제 #3
0
        internal void FilterExpand(SensorEventViewModel sensorEvent)
        {
            int idEvent = _allEventsInView.IndexOf(sensorEvent);

            if (idEvent < 0)
            {
                return;
            }
            int count = 0;

            if (sensorEvent.IsStartOfSegment)
            {
                sensorEvent.IsStartOfSegment = false;
                idEvent--;
                while (idEvent >= 0 && count < iExpandUnit)
                {
                    if (_allEventsInView[idEvent].IsActivityFiltered || _allEventsInView[idEvent].IsExpandedInView)
                    {
                        _allEventsInView[idEvent].IsEndOfSegment = false;
                        break;
                    }
                    if (_allEventsInView[idEvent].IsFiltered)
                    {
                        _allEventsInView[idEvent].IsExpandedInView = true;
                        count++;
                    }
                    idEvent--;
                }
                idEvent++;
                if (count == iExpandUnit && idEvent > 0)
                {
                    _allEventsInView[idEvent].IsStartOfSegment = true;
                }
            }
            else
            {
                sensorEvent.IsEndOfSegment = false;
                idEvent++;
                while (idEvent < _allEventsInView.Count && count < iExpandUnit)
                {
                    if (_allEventsInView[idEvent].IsActivityFiltered || _allEventsInView[idEvent].IsExpandedInView)
                    {
                        _allEventsInView[idEvent].IsStartOfSegment = false;
                        break;
                    }
                    if (_allEventsInView[idEvent].IsFiltered)
                    {
                        _allEventsInView[idEvent].IsExpandedInView = true;
                        count++;
                    }
                    idEvent++;
                }
                idEvent--;
                if (count == iExpandUnit && idEvent < _allEventsInView.Count)
                {
                    _allEventsInView[idEvent].IsEndOfSegment = true;
                }
            }
            EventsInView.Refresh();
        }
예제 #4
0
        public async Task LoadEventsAsync(DateTimeOffset date, bool forceReload = false)
        {
            // Off states
            HashSet <string> sensorOffSet = new HashSet <string>()
            {
                "off", "close", "absent"
            };

            if (!forceReload && date == CurrentDate)
            {
                return;
            }
            _allEventsInView.Clear();
            _activeSensorsList.Clear();
            EventOffset eventOffsetTuple;

            if (_dictDateEvents.TryGetValue(date, out eventOffsetTuple))
            {
                int start  = eventOffsetTuple.Offset;
                int length = eventOffsetTuple.Length;
                for (int i = start; i < start + length; i++)
                {
                    SensorEventViewModel currentEvent = ParseSensorEventFromString(_eventStringList[i]);
                    _allEventsInView.Add(currentEvent);
                    HashSet <string> currentActiveSensors = (i == start) ? new HashSet <string>() :
                                                            new HashSet <string>(_activeSensorsList[i - start - 1]);
                    if (sensorOffSet.Contains(currentEvent.SensorState.ToLower()))
                    {
                        if (currentActiveSensors.Contains(currentEvent.Sensor.Name))
                        {
                            currentActiveSensors.Remove(currentEvent.Sensor.Name);
                        }
                    }
                    else
                    {
                        if (!currentActiveSensors.Contains(currentEvent.Sensor.Name))
                        {
                            currentActiveSensors.Add(currentEvent.Sensor.Name);
                        }
                    }
                    _activeSensorsList.Add(currentActiveSensors);
                }
                InitSensorFireStatus();
                await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    CurrentDate = date;
                    ApplyFilter();
                    NumEventsInView = EventsInView.Count;
                });
            }
            else
            {
                appLog.Error("Date {0} not found in events.", date.ToString("G"));
            }
        }
예제 #5
0
 internal void TagAllResidents(string tag)
 {
     for (int idEvent = 0; idEvent < _eventStringList.Count; idEvent++)
     {
         string strCurrentEvent           = _eventStringList[idEvent];
         SensorEventViewModel sensorEvent = ParseSensorEventFromString(strCurrentEvent);
         sensorEvent.Resident      = GetResidentByName(tag);
         _eventStringList[idEvent] = sensorEvent.ToString();
     }
     IsEventsModified = true;
 }
예제 #6
0
 internal void UntagAllActivities()
 {
     for (int idEvent = 0; idEvent < _eventStringList.Count; idEvent++)
     {
         string strCurrentEvent           = _eventStringList[idEvent];
         SensorEventViewModel sensorEvent = ParseSensorEventFromString(strCurrentEvent);
         sensorEvent.Activity      = ActivityViewModel.NullActivity;
         _eventStringList[idEvent] = sensorEvent.ToString();
     }
     IsEventsModified = true;
 }
예제 #7
0
 private SensorEventViewModel FindComplementaryEvent(SensorEventViewModel sensorEvent)
 {
     if (SensorStatusStartToken.Contains(sensorEvent.SensorState))
     {
         // Search forward
         int curIndex = _allEventsInView.IndexOf(sensorEvent);
         for (int i = curIndex + 1; i < _allEventsInView.Count; i++)
         {
             if ((_allEventsInView[i].TimeTag - sensorEvent.TimeTag).TotalSeconds > 3600)
             {
                 break;
             }
             if (_allEventsInView[i].Sensor == sensorEvent.Sensor)
             {
                 if (SensorStatusStopToken.Contains(_allEventsInView[i].SensorState))
                 {
                     return(_allEventsInView[i]);
                 }
                 else
                 {
                     break;
                 }
             }
         }
     }
     else if (SensorStatusStopToken.Contains(sensorEvent.SensorState))
     {
         // Search backward
         int curIndex = _allEventsInView.IndexOf(sensorEvent);
         for (int i = curIndex - 1; i >= 0; i--)
         {
             if ((sensorEvent.TimeTag - _allEventsInView[i].TimeTag).TotalSeconds > 3600)
             {
                 break;
             }
             if (_allEventsInView[i].Sensor == sensorEvent.Sensor)
             {
                 if (SensorStatusStartToken.Contains(_allEventsInView[i].SensorState))
                 {
                     return(_allEventsInView[i]);
                 }
                 else
                 {
                     break;
                 }
             }
         }
     }
     // Do not have/find compllementary pair, return null
     return(null);
 }
예제 #8
0
 internal void FillNullActivity()
 {
     for (int idEvent = 0; idEvent < _eventStringList.Count; idEvent++)
     {
         string strCurrentEvent           = _eventStringList[idEvent];
         SensorEventViewModel sensorEvent = ParseSensorEventFromString(strCurrentEvent);
         if (sensorEvent.Activity == ActivityViewModel.NullActivity)
         {
             sensorEvent.Activity = GetActivityByName("Other_Activity", true, true);
             IsEventsModified     = true;
         }
         _eventStringList[idEvent] = sensorEvent.ToString();
     }
 }
예제 #9
0
        internal Dictionary <ResidentViewModel, List <SensorEventViewModel> > GetPastStepsOfResidents(int maxSeconds = 7200, int maxSteps = 1000)
        {
            Dictionary <ResidentViewModel, List <SensorEventViewModel> > pastStepsOfResidents =
                new Dictionary <ResidentViewModel, List <SensorEventViewModel> >();

            if (SelectedSensorEvent == null)
            {
                return(pastStepsOfResidents);
            }
            Dictionary <ResidentViewModel, HashSet <SensorViewModel> > sensorCheckDictionary =
                new Dictionary <ResidentViewModel, HashSet <SensorViewModel> >();

            foreach (ResidentViewModel resident in Residents)
            {
                sensorCheckDictionary.Add(resident, new HashSet <SensorViewModel>());
                pastStepsOfResidents.Add(resident, new List <SensorEventViewModel>());
            }
            int            eventIndex   = _allEventsInView.IndexOf(SelectedSensorEvent);
            DateTimeOffset eventTimeTag = SelectedSensorEvent.TimeTag;

            for (int i = eventIndex; i >= 0; i--)
            {
                SensorEventViewModel tempEvent = _allEventsInView[i];
                // If exceeds maxSSeconds
                if ((eventTimeTag - tempEvent.TimeTag).TotalSeconds > maxSeconds)
                {
                    break;
                }
                // If exceeds maxSteps
                if (eventIndex - i > maxSteps)
                {
                    break;
                }
                // Otherwise, Log sensor if resident is labelled.
                if (tempEvent.Resident != ResidentViewModel.NullResident)
                {
                    // TODO Check if Resident does not exist in Dictionary
                    if (!sensorCheckDictionary[tempEvent.Resident].Contains(tempEvent.Sensor))
                    {
                        if (!SensorStatusStopToken.Contains(tempEvent.SensorState))
                        {
                            pastStepsOfResidents[tempEvent.Resident].Add(tempEvent);
                            //sensorCheckDictionary[tempEvent.Resident].Add(tempEvent.Sensor);
                        }
                    }
                }
            }
            return(pastStepsOfResidents);
        }
예제 #10
0
 private bool ApplyActivityFilter(SensorEventViewModel sensorEvent)
 {
     if (EventViewFilter.IsActivityFilterEnabled && EventViewFilter.Activities.Count > 0)
     {
         if (EventViewFilter.Activities.Contains(sensorEvent.Activity.Name))
         {
             return(true);
         }
         return(sensorEvent.Activity == ActivityViewModel.NullActivity);
     }
     else
     {
         return(false);
     }
 }
예제 #11
0
 internal void TagActivity(string tag, List <SensorEventViewModel> selectedEvents)
 {
     foreach (SensorEventViewModel sensorEvent in selectedEvents)
     {
         if (sensorEvent.Activity.Name != tag)
         {
             TagActivity(tag, sensorEvent);
             SensorEventViewModel complementaryEvent = FindComplementaryEvent(sensorEvent);
             if (complementaryEvent != null)
             {
                 TagActivity(tag, complementaryEvent);
             }
         }
     }
     IsEventsModified = true;
 }
예제 #12
0
        public SensorEventViewModel ParseSensorEventFromString(string eventString)
        {
            SensorEventViewModel sensorEvent = new SensorEventViewModel();

            string[] tokenList = eventString.Split(new Char[] { ',' });
            int      numToken  = tokenList.Count();

            if (numToken < 3)
            {
                throw new ArgumentException("Number of Tokens in Sensor Event String is smaller than 3");
            }
            // First Token: Date, Second Token: Time (with AM/PM), required
            sensorEvent.TimeTag = DateTimeOffset.Parse(tokenList[0]);
            // Third Token: SensorID, required
            sensorEvent.Sensor = GetSensorByName(tokenList[1]);
            // Fourth Token: Status, required
            sensorEvent.SensorState = tokenList[2];
            // Fifth Token: Occupant
            if (numToken > 3 && !string.IsNullOrWhiteSpace(tokenList[3]))
            {
                sensorEvent.Resident = GetResidentByName(tokenList[3], true);
            }
            else
            {
                sensorEvent.Resident = ResidentViewModel.NullResident;
            }
            // Sixth Token: Activity Labels
            if (numToken > 4 && !string.IsNullOrWhiteSpace(tokenList[4]))
            {
                sensorEvent.Activity = GetActivityByName(tokenList[4], true);
            }
            else
            {
                sensorEvent.Activity = ActivityViewModel.NullActivity;
            }
            // The Rest: Comments
            if (numToken > 5)
            {
                sensorEvent.Comments = string.Join(",", tokenList.Skip(5));
            }
            else
            {
                sensorEvent.Comments = "";
            }
            return(sensorEvent);
        }
예제 #13
0
 /// <summary>
 /// Apply static filters such ad null activities, null residents, sensor status or sensor categories on sensor event.
 /// </summary>
 /// <param name="sensorEvent"></param>
 /// <returns>true if the sensor event is hidden</returns>
 private bool ApplyStaticFilter(SensorEventViewModel sensorEvent)
 {
     if (EventViewFilter.HideEventsWithoutActivity)
     {
         if ((Activity)sensorEvent.Activity == Activity.NullActivity)
         {
             return(true);
         }
     }
     if (EventViewFilter.HideEventsWithoutResident)
     {
         if ((Resident)sensorEvent.Resident == Resident.NullResident)
         {
             return(true);
         }
     }
     if (EventViewFilter.SensorStatus.Contains(sensorEvent.SensorState))
     {
         return(true);
     }
     if (EventViewFilter.Residents.Contains(sensorEvent.Resident.Name))
     {
         return(true);
     }
     //foreach (string activityName in EventViewFilter.Activities)
     //{
     //    if (sensorEvent.Activity.Name == activityName) return true;
     //}
     foreach (string sensorCategory in EventViewFilter.SensorCategories)
     {
         if (sensorEvent.Sensor.SensorCategories.Contains(sensorCategory))
         {
             return(true);
         }
     }
     return(false);
 }
예제 #14
0
 public int IndexOf(SensorEventViewModel item)
 {
     return(_storage.IndexOf(item));
 }
예제 #15
0
 public void Add(SensorEventViewModel item)
 {
     throw new NotImplementedException();
 }
예제 #16
0
 public bool Contains(SensorEventViewModel item)
 {
     return(_storage.Contains(item));
 }
예제 #17
0
 public bool Remove(SensorEventViewModel item)
 {
     throw new NotImplementedException();
 }
예제 #18
0
 private bool FilterEvent(SensorEventViewModel sensorEvent)
 {
     /* Looking for IsFiltered and IsExpandedInView property */
     return(!((sensorEvent.IsFiltered && sensorEvent.IsActivityFiltered) || sensorEvent.IsExpandedInView));
 }
예제 #19
0
        private async Task <int> LoadDateAsync(CancellationToken c, DateTime datetime,
                                               ObservableCollection <AnnotationFile> annotationFileList    = null,
                                               ObservableCollection <ActivityViewModel> activityCollection = null, int numBefore = 10)
        {
            List <int> annotationStartOffset = new List <int>();
            List <int> annotationCurIndex    = new List <int>();

            appLog.Info(this.GetType().Name, string.Format("Loading events on {0} into Listview.", datetime.ToString("D")));
            try
            {
                DateTime dateTimeToLoad = datetime.Date;
                int      offset         = 0;
                int      nextOffset     = 0;
                int      Count          = 0;
                if (dateTimeToLoad < _firstEventDate)
                {
                    offset     = _eventOffsetList[_firstEventDate];
                    nextOffset = _eventOffsetList[_eventDateList[1]];
                    _curDate   = _firstEventDate;
                }
                else if (dateTimeToLoad >= _lastEventDate)
                {
                    offset     = _eventOffsetList[_lastEventDate];
                    nextOffset = _eventList.Count;
                    _curDate   = _lastEventDate;
                }
                else
                {
                    while (!_eventOffsetList.TryGetValue(dateTimeToLoad, out offset))
                    {
                        dateTimeToLoad = dateTimeToLoad.AddDays(1);
                    }
                    nextOffset = _eventOffsetList[_eventDateList[_eventDateList.IndexOf(dateTimeToLoad) + 1]];
                    _curDate   = dateTimeToLoad;
                }
                Count = nextOffset - offset;

                // Prepare tuple for start location for each annotated file
                if (annotationFileList != null)
                {
                    for (int i = 0; i < annotationFileList.Count; i++)
                    {
                        int startOffset = annotationFileList[i].GetOffsetByDate(_curDate);
                        annotationStartOffset.Add(startOffset);
                        annotationCurIndex.Add(startOffset);
                    }
                }

                _internal_storage.Clear();

                for (int i = offset; i < nextOffset; i++)
                {
                    SensorEventViewModel sensorEventViewModel = new SensorEventViewModel(new Models.SensorEvent());
                    sensorEventViewModel.FromString(_eventList[i]);
                    sensorEventViewModel.index = i - offset;
                    if (_isOffStateHidden && sensorEventViewModel.SensorState.ToUpper() == "OFF")
                    {
                        sensorEventViewModel.Skip      = true;
                        sensorEventViewModel.IsVisible = false;
                    }
                    _internal_storage.Add(sensorEventViewModel);
                    // Populate annotation if specified
                    if (annotationFileList != null)
                    {
                        var emptyProbability = new List <Tuple <string, double> >();
                        for (int j = 0; j < annotationFileList.Count; j++)
                        {
                            ClassifiedLabelViewModel predictionModel = new ClassifiedLabelViewModel();
                            predictionModel.GroundTruth = sensorEventViewModel.ActivityLabel;
                            if (annotationCurIndex[j] <= 0)
                            {
                                predictionModel.ActivityLabel = "";
                            }
                            else
                            {
                                var    curPredictionEntry  = annotationFileList[j].PredictionList[annotationCurIndex[j]];
                                var    lastPredictionEntry = (annotationCurIndex[j] == 0) ? null : annotationFileList[j].PredictionList[annotationCurIndex[j] - 1];
                                string previousLabel       = (lastPredictionEntry == null) ? "" : lastPredictionEntry.ActivityLabel;
                                List <Tuple <string, double> > previousProbability = (lastPredictionEntry == null) ? emptyProbability : lastPredictionEntry.ActivityProbability;
                                if (sensorEventViewModel.TimeTag >= curPredictionEntry.TimeTag)
                                {
                                    predictionModel.ActivityLabel       = curPredictionEntry.ActivityLabel;
                                    predictionModel.ActivityProbability = curPredictionEntry.ActivityProbability;
                                    annotationCurIndex[j]++;
                                }
                                else
                                {
                                    predictionModel.ActivityLabel       = previousLabel;
                                    predictionModel.ActivityProbability = previousProbability;
                                }
                            }
                            sensorEventViewModel.ClassifiedActivityLabels.Add(predictionModel);
                        }
                    }
                }

                // Apply Filter if necessary
                if (activityCollection != null)
                {
                    ApplyActivityFilters(activityCollection);
                }
                // Populate list view
                Refresh();
                return(Count);
            }
            finally
            {
                _busy = false;
            }
        }
예제 #20
0
 public async Task ImportAnnotationAsync(StorageFile annotationFile)
 {
     using (var inputStream = await annotationFile.OpenReadAsync())
         using (var classicStream = inputStream.AsStreamForRead())
             using (var streamReader = new StreamReader(classicStream))
             {
                 int      lineNo  = 0;
                 int      idEvent = 0;
                 string[] tokenList;
                 // Make sure Other_Activity exists in dataset metadata.
                 ActivityViewModel curActivity      = GetActivityByName("Other_Activity", true, true);
                 string            curAnnotationTag = "";
                 // Add Other Activity to activity list
                 while (streamReader.Peek() >= 0)
                 {
                     string curEventString = streamReader.ReadLine();
                     if (string.IsNullOrWhiteSpace(curEventString))
                     {
                         continue;
                     }
                     tokenList = curEventString.Split(new char[] { ' ', '\t', ',' });
                     // Get the Date of the String and add to dictionary
                     DateTime curEventTimeTag = DateTime.Parse(tokenList[0] + ' ' + tokenList[1]);
                     // Check the activity label of current event
                     if (tokenList.Length > 4 && !string.IsNullOrWhiteSpace(tokenList[4]))
                     {
                         // Process annotation tag
                         string[] subTokenList = tokenList[4].Split(new char[] { '=' });
                         curActivity = GetActivityByName(subTokenList[0], true, true);
                         // In case the ="begin" part is missing in the annotation
                         if (subTokenList.Length < 2)
                         {
                             if (curAnnotationTag == "\"begin\"")
                             {
                                 curAnnotationTag = "\"end\"";
                             }
                             else
                             {
                                 curAnnotationTag = "\"begin\"";
                             }
                         }
                         else
                         {
                             curAnnotationTag = subTokenList[1];
                         }
                     }
                     // Append Activity
                     while (idEvent < _eventStringList.Count)
                     {
                         // Check datetime of current event
                         SensorEventViewModel sensorEvent = ParseSensorEventFromString(_eventStringList[idEvent]);
                         if (sensorEvent.TimeTag.DateTime <= curEventTimeTag)
                         {
                             sensorEvent.Activity      = curActivity;
                             _eventStringList[idEvent] = sensorEvent.ToString();
                             idEvent++;
                         }
                         else
                         {
                             break;
                         }
                     }
                     // If activity end, update annotation token to OtherActivity
                     if (curAnnotationTag == "\"end\"")
                     {
                         curActivity      = GetActivityByName("Other_Activity");;
                         curAnnotationTag = "";
                     }
                     // Log Token
                     lineNo++;
                 }
             }
     this.IsDatasetModified = true;
     this.IsEventsModified  = true;
 }
예제 #21
0
 public void Insert(int index, SensorEventViewModel item)
 {
     throw new NotImplementedException();
 }