internal void TagActivity(string tag, SensorEventViewModel sensorEvent) { sensorEvent.Activity = GetActivityByName(tag); int stringIndex = _dictDateEvents[CurrentDate].Offset + _allEventsInView.IndexOf(sensorEvent); _eventStringList[stringIndex] = sensorEvent.ToString(); }
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(); }
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(); }
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")); } }
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; }
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; }
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); }
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(); } }
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); }
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); } }
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; }
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); }
/// <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); }
public int IndexOf(SensorEventViewModel item) { return(_storage.IndexOf(item)); }
public void Add(SensorEventViewModel item) { throw new NotImplementedException(); }
public bool Contains(SensorEventViewModel item) { return(_storage.Contains(item)); }
public bool Remove(SensorEventViewModel item) { throw new NotImplementedException(); }
private bool FilterEvent(SensorEventViewModel sensorEvent) { /* Looking for IsFiltered and IsExpandedInView property */ return(!((sensorEvent.IsFiltered && sensorEvent.IsActivityFiltered) || sensorEvent.IsExpandedInView)); }
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; } }
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; }
public void Insert(int index, SensorEventViewModel item) { throw new NotImplementedException(); }