public static void toARFF(string aDataDirectory, string masterDirectory, int maxControllers, string sourceFile, int annotators,string[] filter) { int featureVectorIndex = 0; MITesDecoder aMITesDecoder = new MITesDecoder(); MITesLoggerReader aMITesLoggerReader = new MITesLoggerReader(aMITesDecoder, aDataDirectory); SXML.SensorAnnotation sannotation = null; MITesFeatures.core.conf.GeneralConfiguration configuration = null; AXML.Annotation[] aannotation = new AXML.Annotation[annotators]; AXML.Reader[] readers=new AXML.Reader[annotators]; AXML.Annotation intersection = null; AXML.Annotation[] difference = new AXML.Annotation[annotators]; AXML.Annotation realtimeAnnotation = new AXML.Reader(masterDirectory, aDataDirectory).parse(); realtimeAnnotation.RemoveData(filter); for (int i = 0; (i < annotators); i++) { readers[i] = new AXML.Reader(masterDirectory, aDataDirectory, sourceFile + i + ".xml"); aannotation[i] = readers[i].parse(); aannotation[i].RemoveData(filter); aannotation[i].DataDirectory = aDataDirectory; if (intersection == null) intersection = aannotation[i]; else intersection = intersection.Intersect(aannotation[i]); } for (int i = 0; (i < annotators); i++) difference[i] = aannotation[i].Difference(intersection); SXML.Reader sreader = new SXML.Reader(masterDirectory, aDataDirectory); sannotation = sreader.parse(maxControllers); MITesFeatures.core.conf.ConfigurationReader creader = new MITesFeatures.core.conf.ConfigurationReader(aDataDirectory); configuration = creader.parse(); Extractor.Initialize(aMITesDecoder, aDataDirectory, aannotation[0], sannotation,configuration); TextWriter tw = new StreamWriter(aDataDirectory + "\\output-"+sourceFile+".arff"); tw.WriteLine("@RELATION wockets"); tw.WriteLine(Extractor.GetArffHeader()); tw.WriteLine("@ATTRIBUTE INDEX NUMERIC"); tw.WriteLine("@ATTRIBUTE ANNOTATORS_AGREE NUMERIC"); Hashtable recorded_activities = new Hashtable(); for (int k = 0; (k < annotators); k++) { //tw.Write("@ATTRIBUTE annotator"+k+" {unknown"); //tw.Write("@ATTRIBUTE annotator" + k + " {"); for (int i = 0; (i < aannotation[k].Data.Count); i++) { AXML.AnnotatedRecord record = ((AXML.AnnotatedRecord)aannotation[k].Data[i]); string activity = ""; for (int j = 0; (j < record.Labels.Count); j++) { if (j == record.Labels.Count - 1) activity += ((AXML.Label)record.Labels[j]).Name; else activity += ((AXML.Label)record.Labels[j]).Name + "_"; } activity = activity.Replace("none", "").Replace('-', '_').Replace(':', '_').Replace('%', '_').Replace('/', '_'); activity = Regex.Replace(activity, "[_]+", "_"); activity = Regex.Replace(activity, "^[_]+", ""); activity = Regex.Replace(activity, "[_]+$", ""); //only output activity labels that have not been seen if (recorded_activities.Contains(activity) == false) { //tw.Write("," + activity); recorded_activities[activity] = activity; } } } for (int k = 0; (k < annotators); k++) { tw.Write("@ATTRIBUTE annotator" + k + " {unknown,flapping,rocking,flaprock}\n"); //foreach ( DictionaryEntry de in recorded_activities ) // tw.Write("," + (string) de.Key); //tw.WriteLine("}"); } tw.Write("@ATTRIBUTE realtime {unknown,flapping,rocking,flaprock}\n"); //foreach (DictionaryEntry de in recorded_activities) // tw.Write("," + (string)de.Key); //tw.WriteLine("}"); tw.WriteLine("@DATA"); bool isData = aMITesLoggerReader.GetSensorData(10); int channel = 0, x = 0, y = 0, z = 0; double unixtimestamp = 0.0; int[] differenceIndex = new int[annotators]; AXML.AnnotatedRecord[] annotatedRecord = new AXML.AnnotatedRecord[annotators]; int intersectionIndex = 0; string intersection_activity = "unknown"; AXML.AnnotatedRecord intersectionRecord = ((AXML.AnnotatedRecord)intersection.Data[intersectionIndex]); string[] current_activity = new string[annotators]; string realtime_activity = "unknown"; int realtimeIndex = 0; AXML.AnnotatedRecord realtimeRecord = ((AXML.AnnotatedRecord)realtimeAnnotation.Data[realtimeIndex]); for (int i = 0; (i < annotators); i++) { differenceIndex[i] = 0; annotatedRecord[i] = ((AXML.AnnotatedRecord)difference[i].Data[differenceIndex[i]]); current_activity[i] = "unknown"; } do { //decode the frame channel = aMITesDecoder.GetSomeMITesData()[0].channel; x = aMITesDecoder.GetSomeMITesData()[0].x; y = aMITesDecoder.GetSomeMITesData()[0].y; z = aMITesDecoder.GetSomeMITesData()[0].z; unixtimestamp = aMITesDecoder.GetSomeMITesData()[0].unixTimeStamp; double lastTimeStamp = Extractor.StoreMITesWindow(); for (int i = 0; (i < annotators); i++) { if (unixtimestamp > annotatedRecord[i].EndUnix) { current_activity[i] = "unknown"; if (differenceIndex[i] < difference[i].Data.Count - 1) { differenceIndex[i] = differenceIndex[i]+1; annotatedRecord[i] = ((AXML.AnnotatedRecord)difference[i].Data[differenceIndex[i]]); } } } if (unixtimestamp > realtimeRecord.EndUnix) { realtime_activity = "unknown"; realtimeIndex++; if (realtimeIndex < realtimeAnnotation.Data.Count) realtimeRecord = ((AXML.AnnotatedRecord)realtimeAnnotation.Data[realtimeIndex]); } if (unixtimestamp > intersectionRecord.EndUnix) { intersection_activity = "unknown"; if (intersectionIndex < intersection.Data.Count - 1) { intersectionIndex = intersectionIndex + 1; intersectionRecord = ((AXML.AnnotatedRecord)intersection.Data[intersectionIndex]); } } for (int i = 0; (i < annotators); i++) { if ((lastTimeStamp >= annotatedRecord[i].StartUnix) && (lastTimeStamp <= annotatedRecord[i].EndUnix) && current_activity[i].Equals("unknown")) { current_activity[i] = ""; for (int j = 0; (j < annotatedRecord[i].Labels.Count); j++) { if (j == annotatedRecord[i].Labels.Count - 1) current_activity[i] += ((AXML.Label)annotatedRecord[i].Labels[j]).Name; else current_activity[i] += ((AXML.Label)annotatedRecord[i].Labels[j]).Name + "_"; } current_activity[i] = current_activity[i].Replace("none", "").Replace('-', '_').Replace(':', '_').Replace('%', '_').Replace('/', '_'); current_activity[i] = Regex.Replace(current_activity[i], "[_]+", "_"); current_activity[i] = Regex.Replace(current_activity[i], "^[_]+", ""); current_activity[i] = Regex.Replace(current_activity[i], "[_]+$", ""); } else if (lastTimeStamp > annotatedRecord[i].EndUnix) current_activity[i] = "unknown"; } if ((lastTimeStamp >= realtimeRecord.StartUnix) && (lastTimeStamp <= realtimeRecord.EndUnix) && realtime_activity.Equals("unknown")) { realtime_activity = ""; for (int j = 0; (j < realtimeRecord.Labels.Count); j++) { if (j == realtimeRecord.Labels.Count - 1) realtime_activity += ((AXML.Label)realtimeRecord.Labels[j]).Name; else realtime_activity += ((AXML.Label)realtimeRecord.Labels[j]).Name + "_"; } realtime_activity = realtime_activity.Replace("none", "").Replace('-', '_').Replace(':', '_').Replace('%', '_').Replace('/', '_'); realtime_activity = Regex.Replace(realtime_activity, "[_]+", "_"); realtime_activity = Regex.Replace(realtime_activity, "^[_]+", ""); realtime_activity = Regex.Replace(realtime_activity, "[_]+$", ""); } else if (lastTimeStamp > realtimeRecord.EndUnix) realtime_activity = "unknown"; if ((lastTimeStamp >= intersectionRecord.StartUnix) && (lastTimeStamp <= intersectionRecord.EndUnix) && intersection_activity.Equals("unknown")) { intersection_activity = ""; for (int j = 0; (j < intersectionRecord.Labels.Count); j++) { if (j == intersectionRecord.Labels.Count - 1) intersection_activity += ((AXML.Label)intersectionRecord.Labels[j]).Name; else intersection_activity += ((AXML.Label)intersectionRecord.Labels[j]).Name + "_"; } intersection_activity = intersection_activity.Replace("none", "").Replace('-', '_').Replace(':', '_').Replace('%', '_').Replace('/', '_'); intersection_activity = Regex.Replace(intersection_activity, "[_]+", "_"); intersection_activity = Regex.Replace(intersection_activity, "^[_]+", ""); intersection_activity = Regex.Replace(intersection_activity, "[_]+$", ""); } if ((Extractor.GenerateFeatureVector(lastTimeStamp))) { string activity_suffix = "," + featureVectorIndex; if (intersection_activity.Equals("unknown") == true) //disagreement or agreement unknown { if ((current_activity[0] == "unknown") && (current_activity[1] == "unknown")) activity_suffix += ",1"; else activity_suffix += ",0"; for (int i = 0; (i < annotators); i++) activity_suffix += "," + current_activity[i]; } else { activity_suffix += ",1"; for (int i = 0; (i < annotators); i++) activity_suffix += "," + intersection_activity; } string arffSample = Extractor.toString() + activity_suffix; //if (activity_suffix.Contains("unknown") == false) //{ tw.WriteLine(arffSample+","+realtime_activity); featureVectorIndex++; //} } } while (isData = aMITesLoggerReader.GetSensorData(10)); tw.Close(); }
public Annotation Difference(Annotation b) { Annotation a = this.copy(); ArrayList intersection = b.Data; //Intersect(a).Data; Annotation output = this.copy(); ArrayList tempOutput = new ArrayList(); ArrayList aData = a.Data; int intersectCounter = 0; int originalCounter = 1; while (intersectCounter < intersection.Count && originalCounter < aData.Count) { AXML.AnnotatedRecord temp = ((AXML.AnnotatedRecord)intersection[intersectCounter]).copy(); AXML.AnnotatedRecord original = ((AXML.AnnotatedRecord)aData[originalCounter]).copy(); //no intersection so add original data if (original.EndUnix < temp.StartUnix || temp.EndUnix < original.StartUnix) { tempOutput.Add(original); originalCounter++; } //else there is intersection, add areas outside of intersection else { double start1 = original.StartUnix; double end1 = temp.StartUnix; double start2 = temp.EndUnix; double end2 = original.EndUnix; if (start1 == end1 && start2 != end2) //use start2 and end2 as constraints { AXML.AnnotatedRecord ann = original.copy(); ann.StartHour = temp.EndHour; ann.StartMinute = temp.EndMinute; ann.StartSecond = temp.EndSecond; ann.StartMillisecond = temp.EndMillisecond; ann.StartUnix = temp.EndUnix; tempOutput.Add(ann); } else if (start1 != end1 && start2 == end2) //use start1 and end1 as constraints { AXML.AnnotatedRecord ann = original.copy(); ann.EndHour = temp.StartHour; ann.EndMinute = temp.StartMinute; ann.EndSecond = temp.StartSecond; ann.EndMillisecond = temp.StartMillisecond; ann.EndUnix = temp.StartUnix; tempOutput.Add(ann); } else if (start1 != end1 && start2 != end2)//use both start1 & end1, start2 & end2 { AXML.AnnotatedRecord ann1 = original.copy(); int oriHour = original.EndHour; int oriMinute = original.EndMinute; int oriSecond = original.EndSecond; int oriMillisecond = original.EndMillisecond; double oriUnix = original.EndUnix; ann1.EndHour = temp.StartHour; ann1.EndMinute = temp.StartMinute; ann1.EndSecond = temp.StartSecond; ann1.EndMillisecond = temp.StartMillisecond; ann1.EndUnix = temp.StartUnix; tempOutput.Add(ann1); AXML.AnnotatedRecord ann2 = new AXML.AnnotatedRecord(); foreach (AXML.Label label in ann1.Labels) { ann2.Labels.Add(label); } ann2.Quality = ann1.Quality; ann2.StartDate = ann1.StartDate; ann2.EndDate = ann1.EndDate; ann2.EndHour = oriHour; ann2.EndMinute = oriMinute; ann2.EndSecond = oriSecond; ann2.EndMillisecond = oriMillisecond; ann2.EndUnix = oriUnix; ann2.StartHour = temp.EndHour; ann2.StartMinute = temp.EndMinute; ann2.StartSecond = temp.EndSecond; ann2.StartMillisecond = temp.EndMillisecond; ann2.StartUnix = temp.EndUnix; tempOutput.Add(ann2); } intersectCounter++; originalCounter++; } } output.Data.Clear(); //copy results back to hte data field of the output Annotation object foreach (AXML.AnnotatedRecord obj in tempOutput) { output.Data.Add(obj); } return(output); //testing purposes, datadirectory can be anything //output.DataDirectory = dataDir; //TextWriter writer = new StreamWriter(outputDir + fileName); //writer.WriteLine(output.ToXML()); // writer.Close(); }
public Annotation Difference(Annotation b) { Annotation a = this.copy(); ArrayList intersection = b.Data; //Intersect(a).Data; Annotation output = this.copy(); ArrayList tempOutput = new ArrayList(); ArrayList aData = a.Data; int intersectCounter = 0; int originalCounter = 1; while (intersectCounter < intersection.Count && originalCounter < aData.Count) { AXML.AnnotatedRecord temp = ((AXML.AnnotatedRecord)intersection[intersectCounter]).copy(); AXML.AnnotatedRecord original = ((AXML.AnnotatedRecord)aData[originalCounter]).copy(); //no intersection so add original data if (original.EndUnix < temp.StartUnix || temp.EndUnix < original.StartUnix) { tempOutput.Add(original); originalCounter++; } //else there is intersection, add areas outside of intersection else { double start1 = original.StartUnix; double end1 = temp.StartUnix; double start2 = temp.EndUnix; double end2 = original.EndUnix; if (start1 == end1 && start2 != end2) //use start2 and end2 as constraints { AXML.AnnotatedRecord ann = original.copy(); ann.StartHour = temp.EndHour; ann.StartMinute = temp.EndMinute; ann.StartSecond = temp.EndSecond; ann.StartMillisecond = temp.EndMillisecond; ann.StartUnix = temp.EndUnix; tempOutput.Add(ann); } else if (start1 != end1 && start2 == end2) //use start1 and end1 as constraints { AXML.AnnotatedRecord ann = original.copy(); ann.EndHour = temp.StartHour; ann.EndMinute = temp.StartMinute; ann.EndSecond = temp.StartSecond; ann.EndMillisecond = temp.StartMillisecond; ann.EndUnix = temp.StartUnix; tempOutput.Add(ann); } else if (start1 != end1 && start2 != end2)//use both start1 & end1, start2 & end2 { AXML.AnnotatedRecord ann1 = original.copy(); int oriHour = original.EndHour; int oriMinute = original.EndMinute; int oriSecond = original.EndSecond; int oriMillisecond = original.EndMillisecond; double oriUnix = original.EndUnix; ann1.EndHour = temp.StartHour; ann1.EndMinute = temp.StartMinute; ann1.EndSecond = temp.StartSecond; ann1.EndMillisecond = temp.StartMillisecond; ann1.EndUnix = temp.StartUnix; tempOutput.Add(ann1); AXML.AnnotatedRecord ann2 = new AXML.AnnotatedRecord(); foreach (AXML.Label label in ann1.Labels) ann2.Labels.Add(label); ann2.Quality = ann1.Quality; ann2.StartDate = ann1.StartDate; ann2.EndDate = ann1.EndDate; ann2.EndHour = oriHour; ann2.EndMinute = oriMinute; ann2.EndSecond = oriSecond; ann2.EndMillisecond = oriMillisecond; ann2.EndUnix = oriUnix; ann2.StartHour = temp.EndHour; ann2.StartMinute = temp.EndMinute; ann2.StartSecond = temp.EndSecond; ann2.StartMillisecond = temp.EndMillisecond; ann2.StartUnix = temp.EndUnix; tempOutput.Add(ann2); } intersectCounter++; originalCounter++; } } output.Data.Clear(); //copy results back to hte data field of the output Annotation object foreach (AXML.AnnotatedRecord obj in tempOutput) output.Data.Add(obj); return output; //testing purposes, datadirectory can be anything //output.DataDirectory = dataDir; //TextWriter writer = new StreamWriter(outputDir + fileName); //writer.WriteLine(output.ToXML()); // writer.Close(); }
public Annotation Intersect(Annotation a) { int z = 0; a = a.copy(); Annotation output = this.copy(); ArrayList aData = a.Data; ArrayList myData = output.Data; ArrayList outputData = new ArrayList(); foreach (AXML.AnnotatedRecord record1 in aData) { String label1 = ((AXML.Label)record1.Labels[0]).Name; if (label1 == "GoodData") //ignore gooddata fields { continue; } foreach (AXML.AnnotatedRecord record2 in myData) { String label2 = ((AXML.Label)record2.Labels[0]).Name; // if ((record2.StartMillisecond == 13) && (record2.StartSecond == 3) && // (record1.StartMillisecond == 107) && (record1.StartSecond == 0)) // z++; if (label1 == label2) //if the field name matches, process data entry { //move on to next iteration since there will never be intersection if ((record1.EndUnix < record2.StartUnix) || (record2.EndUnix < record1.StartUnix)) { continue; } //if intersection exists else { AXML.AnnotatedRecord newrecord = record1.copy(); //pick the highest start time and lowest end time out of the pair if (record1.StartUnix < record2.StartUnix) { newrecord.StartHour = record2.StartHour; newrecord.StartMinute = record2.StartMinute; newrecord.StartSecond = record2.StartSecond; newrecord.StartMillisecond = record2.StartMillisecond; newrecord.StartUnix = record2.StartUnix; } if (record1.EndUnix > record2.EndUnix) { newrecord.EndHour = record2.EndHour; newrecord.EndMinute = record2.EndMinute; newrecord.EndSecond = record2.EndSecond; newrecord.EndMillisecond = record2.EndMillisecond; newrecord.EndUnix = record2.EndUnix; } outputData.Add(newrecord); } } } } output.Data.Clear(); //copy results back to hte data field of the output Annotation object foreach (AXML.AnnotatedRecord obj in outputData) { output.Data.Add(obj); } return(output); //testing purposes, datadirectory can be anything //TextWriter writer = new StreamWriter("intersection.xml"); //writer.WriteLine(output.ToXML()); //writer.Close(); }