public HierarchicalClassifier(MITesDecoder aMITesDecoder, Annotation aannotation, SensorAnnotation sannotation, string dataDirectory,GeneralConfiguration configuration) { this.classifiers = new Hashtable(); this.dataDirectory = dataDirectory; Extractor.Initialize(aMITesDecoder, dataDirectory, aannotation, sannotation,configuration); }
public GeneralConfiguration parse() { GeneralConfiguration configuration = new GeneralConfiguration(); XmlDocument dom = new XmlDocument(); dom.Load(this.xmlFile); XmlNode xNode = dom.DocumentElement; if ((xNode.Name == Constants.CONFIGURATION_ELEMENT) && (xNode.HasChildNodes)) { //child nodes foreach (XmlNode iNode in xNode.ChildNodes) { if (iNode.Name == Constants.WINDOW_ELEMENT) { foreach (XmlAttribute iAttribute in iNode.Attributes) { //read nodes attributes if (iAttribute.Name == Constants.WINDOW_TIME_ATTRIBUTE) { configuration.WindowTime = Convert.ToInt32(iAttribute.Value); } else if (iAttribute.Name == Constants.WINDOW_OVERLAP_ATTRIBUTE) { configuration.WindowOverlap = Convert.ToDouble(iAttribute.Value); } } } else if (iNode.Name == Constants.SAMPLING_ELEMENT) { foreach (XmlAttribute iAttribute in iNode.Attributes) { //read nodes attributes if (iAttribute.Name == Constants.SAMPLING_RATE_ATTRIBUTE) { configuration.ExpectedSamplingRate = Convert.ToInt32(iAttribute.Value); } } } else if (iNode.Name == Constants.SOFTWARE_ELEMENT) { foreach (XmlAttribute iAttribute in iNode.Attributes) { //read nodes attributes if (iAttribute.Name == Constants.SOFTWARE_MODE_ATTRIBUTE) { configuration.Mode =iAttribute.Value; } else if (iAttribute.Name == Constants.SOFTWARE_CONNECTION_ATTRIBUTE) { configuration.Connection = iAttribute.Value; } else if (iAttribute.Name == Constants.SOFTWARE_CONNECTION_PASSKEY) { configuration.Passkey = iAttribute.Value; } else if (iAttribute.Name == Constants.SOFTWARE_CONNECTION_MAC) { configuration.Mac = iAttribute.Value; for (int i = 0; (i < Constants.MAC_SIZE); i++) configuration.MacAddress[i] = (byte) (System.Int32.Parse(iAttribute.Value.Substring(i * 2, 2), System.Globalization.NumberStyles.AllowHexSpecifier) & 0xff); } } } else if (iNode.Name == Constants.QUALITY_ELEMENT) { foreach (XmlAttribute iAttribute in iNode.Attributes) { //read nodes attributes if (iAttribute.Name == Constants.QUALITY_WINDOW_SIZE) { configuration.QualityWindowSize = Convert.ToInt32(iAttribute.Value); } } } else if (iNode.Name == Constants.ERROR_ELEMENT) { foreach (XmlAttribute iAttribute in iNode.Attributes) { //read nodes attributes if (iAttribute.Name == Constants.CONSECUTIVE_ERROR_ATTRIBUTE) { configuration.MaximumConsecutiveFrameLoss = Convert.ToInt32(iAttribute.Value); } else if (iAttribute.Name == Constants.NONCONSECUTIVE_ERROR_ATTRIBUTE) { configuration.MaximumNonconsecutiveFrameLoss= Convert.ToDouble(iAttribute.Value); } } } else if (iNode.Name == Constants.FFT_ELEMENT) { foreach (XmlAttribute iAttribute in iNode.Attributes) { //read nodes attributes if (iAttribute.Name == Constants.INTERPOLATION_POWER_ATTRIBUTE) { configuration.FFTInterpolatedPower = Convert.ToInt32(iAttribute.Value); } else if (iAttribute.Name == Constants.MAX_FREQUENCIES_ATTRIBUTE) { configuration.FFTMaximumFrequencies = Convert.ToInt32(iAttribute.Value); } } } else if (iNode.Name == Constants.TRAINING_ELEMENT) { foreach (XmlAttribute iAttribute in iNode.Attributes) { //read nodes attributes if (iAttribute.Name == Constants.WAIT_TIME_ATTRIBUTE) { configuration.TrainingWaitTime = Convert.ToInt32(iAttribute.Value); } else if (iAttribute.Name == Constants.TRAINING_TIME_ATTRIBUTE) { configuration.TrainingTime = Convert.ToInt32(iAttribute.Value); } } } else if (iNode.Name == Constants.CLASSIFIER_ELEMENT) { foreach (XmlAttribute iAttribute in iNode.Attributes) { //read nodes attributes if (iAttribute.Name == Constants.SMOOTH_WINDOW_ATTRIBUTE) { configuration.SmoothWindows= Convert.ToInt32(iAttribute.Value); } } } } } configuration.OverlapTime = ((int)(configuration.WindowTime * configuration.WindowOverlap)); return configuration; }
public GeneralConfiguration parse() { GeneralConfiguration configuration = new GeneralConfiguration(); XmlDocument dom = new XmlDocument(); dom.Load(this.xmlFile); XmlNode xNode = dom.DocumentElement; if ((xNode.Name == Constants.CONFIGURATION_ELEMENT) && (xNode.HasChildNodes)) { //child nodes foreach (XmlNode iNode in xNode.ChildNodes) { if (iNode.Name == Constants.WINDOW_ELEMENT) { foreach (XmlAttribute iAttribute in iNode.Attributes) { //read nodes attributes if (iAttribute.Name == Constants.WINDOW_TIME_ATTRIBUTE) { configuration.WindowTime = Convert.ToInt32(iAttribute.Value); } else if (iAttribute.Name == Constants.WINDOW_OVERLAP_ATTRIBUTE) { configuration.WindowOverlap = Convert.ToDouble(iAttribute.Value); } } } else if (iNode.Name == Constants.SAMPLING_ELEMENT) { foreach (XmlAttribute iAttribute in iNode.Attributes) { //read nodes attributes if (iAttribute.Name == Constants.SAMPLING_RATE_ATTRIBUTE) { configuration.ExpectedSamplingRate = Convert.ToInt32(iAttribute.Value); } } } else if (iNode.Name == Constants.SOFTWARE_ELEMENT) { foreach (XmlAttribute iAttribute in iNode.Attributes) { //read nodes attributes if (iAttribute.Name == Constants.SOFTWARE_MODE_ATTRIBUTE) { configuration.Mode =iAttribute.Value; } } } else if (iNode.Name == Constants.QUALITY_ELEMENT) { foreach (XmlAttribute iAttribute in iNode.Attributes) { //read nodes attributes if (iAttribute.Name == Constants.QUALITY_WINDOW_SIZE) { configuration.QualityWindowSize = Convert.ToInt32(iAttribute.Value); } } } else if (iNode.Name == Constants.ERROR_ELEMENT) { foreach (XmlAttribute iAttribute in iNode.Attributes) { //read nodes attributes if (iAttribute.Name == Constants.CONSECUTIVE_ERROR_ATTRIBUTE) { configuration.MaximumConsecutiveFrameLoss = Convert.ToInt32(iAttribute.Value); } else if (iAttribute.Name == Constants.NONCONSECUTIVE_ERROR_ATTRIBUTE) { configuration.MaximumNonconsecutiveFrameLoss= Convert.ToDouble(iAttribute.Value); } } } else if (iNode.Name == Constants.FFT_ELEMENT) { foreach (XmlAttribute iAttribute in iNode.Attributes) { //read nodes attributes if (iAttribute.Name == Constants.INTERPOLATION_POWER_ATTRIBUTE) { configuration.FFTInterpolatedPower = Convert.ToInt32(iAttribute.Value); } else if (iAttribute.Name == Constants.MAX_FREQUENCIES_ATTRIBUTE) { configuration.FFTMaximumFrequencies = Convert.ToInt32(iAttribute.Value); } } } else if (iNode.Name == Constants.TRAINING_ELEMENT) { foreach (XmlAttribute iAttribute in iNode.Attributes) { //read nodes attributes if (iAttribute.Name == Constants.WAIT_TIME_ATTRIBUTE) { configuration.TrainingWaitTime = Convert.ToInt32(iAttribute.Value); } else if (iAttribute.Name == Constants.TRAINING_TIME_ATTRIBUTE) { configuration.TrainingTime = Convert.ToInt32(iAttribute.Value); } } } else if (iNode.Name == Constants.CLASSIFIER_ELEMENT) { foreach (XmlAttribute iAttribute in iNode.Attributes) { //read nodes attributes if (iAttribute.Name == Constants.SMOOTH_WINDOW_ATTRIBUTE) { configuration.SmoothWindows= Convert.ToInt32(iAttribute.Value); } } } } } configuration.OverlapTime = ((int)(configuration.WindowTime * configuration.WindowOverlap)); return configuration; }
public static void Initialize(MITesDecoder aMITesDecoder, string aDataDirectory, AXML.Annotation aannotation, SXML.SensorAnnotation sannotation, GeneralConfiguration configuration)//, string masterDirectory) { Extractor.aannotation = aannotation; Extractor.sannotation = sannotation; Extractor.dconfiguration = configuration; // count the sensors for feature extraction and identify their indicies in // sensor annotation - at the moment only accelerometers are used Extractor.sensorIndicies = new Hashtable(); Extractor.extractorSensorCount = 0; foreach (SXML.Sensor sensor in sannotation.Sensors) { int channel=Convert.ToInt32(sensor.ID); if (channel > 0) // if accelerometer { Extractor.sensorIndicies[channel] = extractorSensorCount; Extractor.extractorSensorCount++; } } //load sensor data //SXML.Reader sreader = new SXML.Reader(masterDirectory, aDataDirectory); //Extractor.sannotation = sreader.parse(); //load configuration // ConfigurationReader creader = new ConfigurationReader(aDataDirectory); // Extractor.dconfiguration = creader.parse(); //load annotation data //AXML.Reader reader = new AXML.Reader(masterDirectory, aDataDirectory + "\\" + AXML.Reader.DEFAULT_XML_FILE); //Extractor.aannotation = reader.parse(); //CHANGE: gathers training samples based on the first category only Extractor.trainingTime = new Hashtable();//int[((AXML.Category)Extractor.aannotation.Categories[0]).Labels.Count]; //for (int i = 0; (i < Extractor.trainingTime.Length); i++) foreach (AXML.Label label in ((AXML.Category)Extractor.aannotation.Categories[0]).Labels) Extractor.trainingTime.Add(label.Name, 0); //Extractor.trainingTime[i] = 0; Extractor.trainingCompleted = false; Extractor.inputRowSize = Extractor.extractorSensorCount * 3; Extractor.fftInterpolationPower = dconfiguration.FFTInterpolatedPower; Extractor.fftMaximumFrequencies = dconfiguration.FFTMaximumFrequencies; //Extractor.trainingTimePerClass = configuration.TrainingTime; //Extractor.trainingWaitTime = configuration.TrainingWaitTime; Extractor.inputColumnSize = (int)Math.Pow(2, Extractor.fftInterpolationPower); Extractor.num_features = Extractor.inputRowSize; // number of distances Extractor.num_features += 1; //total mean; Extractor.num_features += Extractor.inputRowSize; // number of variances Extractor.num_features += Extractor.inputRowSize; // number of ranges Extractor.num_features += 2 * Extractor.fftMaximumFrequencies * Extractor.inputRowSize; // number of fft magnitudes and frequencies Extractor.num_features += Extractor.inputRowSize; // number of energies Extractor.num_features += ((Extractor.inputRowSize * Extractor.inputRowSize) - Extractor.inputRowSize) / 2; //correlation coefficients off-di Extractor.features = new double[Extractor.num_features]; Extractor.arffAttriburesLabels = new string[Extractor.num_features]; Extractor.attributeLocation = new Hashtable(); Extractor.standardized = new double[inputRowSize][]; for (int i = 0; (i < inputRowSize); i++) Extractor.standardized[i] = new double[Extractor.inputColumnSize];//input[0].Length]; Extractor.means = new double[inputRowSize]; inputFFT = new int[Extractor.inputColumnSize]; FFT.Initialize(fftInterpolationPower, fftMaximumFrequencies); Extractor.aMITesDecoder = aMITesDecoder; //Create the ARFF File header string arffHeader = "@RELATION wockets\n\n" + Extractor.GetArffHeader();//sannotation.Sensors.Count * 3, configuration.FFTMaximumFrequencies); arffHeader += "@ATTRIBUTE activity {"; foreach (AXML.Label label in ((AXML.Category)Extractor.aannotation.Categories[0]).Labels) arffHeader += label.Name.Replace(' ', '_') + ","; arffHeader += "unknown}\n"; arffHeader += "\n@DATA\n\n"; //Calculating windowing parameters //total number of points per interpolated window Extractor.INTERPOLATED_SAMPLING_RATE_PER_WINDOW = (int)Math.Pow(2, dconfiguration.FFTInterpolatedPower); //128; //space between interpolated samples Extractor.INTERPOLATED_SAMPLES_SPACING = (double)dconfiguration.WindowTime / INTERPOLATED_SAMPLING_RATE_PER_WINDOW; //expected sampling rate per MITes //Extractor.EXPECTED_SAMPLING_RATE = dconfiguration.ExpectedSamplingRate / sannotation.Sensors.Count; //samples per second //expected samples per window //Extractor.EXPECTED_WINDOW_SIZE = (int)(EXPECTED_SAMPLING_RATE * (dconfiguration.WindowTime / 1000.0)); // expectedSamplingRate per window //what would be considered a good sampling rate //Extractor.EXPECTED_GOOD_SAMPLING_RATE = EXPECTED_WINDOW_SIZE - (int)(dconfiguration.MaximumNonconsecutiveFrameLoss * EXPECTED_WINDOW_SIZE); //number of packets lost per second //space between samples //Extractor.EXPECTED_SAMPLES_SPACING = (double)dconfiguration.WindowTime / EXPECTED_WINDOW_SIZE; Extractor.EXPECTED_SAMPLING_RATES = new int[Extractor.extractorSensorCount]; Extractor.EXPECTED_WINDOW_SIZES = new int[Extractor.extractorSensorCount]; Extractor.EXPECTED_GOOD_SAMPLING_RATES = new int[Extractor.extractorSensorCount]; Extractor.EXPECTED_SAMPLES_SPACING = new double[Extractor.extractorSensorCount]; //foreach (SXML.Sensor sensor in sannotation.Sensors) foreach (DictionaryEntry sensorEntry in Extractor.sensorIndicies) { //Get the channel and index in data array for only // extractor sensors (sensors that will be used to compute // features i.e. accelerometers) int channel=(int)sensorEntry.Key; int sensorIndex = (int)sensorEntry.Value; SXML.Sensor sensor = ((SXML.Sensor)sannotation.Sensors[(int)sannotation.SensorsIndex[channel]]); int receiverID = Convert.ToInt32(sensor.Receiver); if (channel == MITesDecoder.MAX_CHANNEL) //Built in sensor Extractor.EXPECTED_SAMPLING_RATES[sensorIndex] = sensor.SamplingRate; //used sensor sampling rate else Extractor.EXPECTED_SAMPLING_RATES[sensorIndex] = dconfiguration.ExpectedSamplingRate / sannotation.NumberSensors[receiverID]; Extractor.EXPECTED_WINDOW_SIZES[sensorIndex] = (int)(Extractor.EXPECTED_SAMPLING_RATES[sensorIndex] * (dconfiguration.WindowTime / 1000.0)); Extractor.EXPECTED_GOOD_SAMPLING_RATES[sensorIndex] = Extractor.EXPECTED_WINDOW_SIZES[sensorIndex] - (int)(dconfiguration.MaximumNonconsecutiveFrameLoss * Extractor.EXPECTED_WINDOW_SIZES[sensorIndex]); Extractor.EXPECTED_SAMPLES_SPACING[sensorIndex] = (double)dconfiguration.WindowTime / Extractor.EXPECTED_WINDOW_SIZES[sensorIndex]; } //window counters and delimiters Extractor.next_window_end = 0; Extractor.total_window_count = 0; Extractor.num_feature_windows = 0; //data quality variables Extractor.isAcceptableLossRate = true; Extractor.isAcceptableConsecutiveLoss = true; Extractor.unacceptable_window_count = 0; Extractor.unacceptable_consecutive_window_loss_count = 0; //2 D array that stores Sensor axes + time stamps on each row X expected WINDOW SIZE Extractor.data = new double[Extractor.extractorSensorCount * 4][]; // 1 row for each axis // 2D array that stores Sensor axes X INTERPOLATED WINDOW SIZE Extractor.interpolated_data = new double[Extractor.extractorSensorCount * 3][]; // array to store the y location for each axes as data is received // will be different for every sensor of course Extractor.y_index = new int[Extractor.extractorSensorCount]; //Initialize expected data array foreach (DictionaryEntry sensorEntry in Extractor.sensorIndicies) { //Get the channel and index in data array for only // extractor sensors (sensors that will be used to compute // features i.e. accelerometers) int channel=(int)sensorEntry.Key; int sensorIndex = (int)sensorEntry.Value; int adjusted_sensor_index = sensorIndex * 4; //Initialize 4 rows x,y,z timestamp for (int j = 0; j < 4; j++) { Extractor.data[adjusted_sensor_index] = new double[EXPECTED_WINDOW_SIZES[sensorIndex]]; for (int k = 0; (k < EXPECTED_WINDOW_SIZES[sensorIndex]); k++) Extractor.data[adjusted_sensor_index][k] = 0; adjusted_sensor_index++; } } //Here it is equal across all sensors, so we do not need to consider //the sampling rate of each sensor separately for (int j = 0; (j < (Extractor.extractorSensorCount * 3)); j++) { Extractor.interpolated_data[j] = new double[INTERPOLATED_SAMPLING_RATE_PER_WINDOW]; for (int k = 0; (k < INTERPOLATED_SAMPLING_RATE_PER_WINDOW); k++) Extractor.interpolated_data[j][k] = 0; } //Initialize y index for each sensor for (int j = 0; (j < Extractor.extractorSensorCount); j++) Extractor.y_index[j] = 0; }
public static void Initialize(MITesDecoder aMITesDecoder, string aDataDirectory, AXML.Annotation aannotation,SXML.SensorAnnotation sannotation)//, string masterDirectory) { Extractor.aannotation = aannotation; Extractor.sannotation = sannotation; //load sensor data //SXML.Reader sreader = new SXML.Reader(masterDirectory, aDataDirectory); //Extractor.sannotation = sreader.parse(); //load configuration ConfigurationReader creader = new ConfigurationReader(aDataDirectory); Extractor.dconfiguration = creader.parse(); //load annotation data //AXML.Reader reader = new AXML.Reader(masterDirectory, aDataDirectory + "\\" + AXML.Reader.DEFAULT_XML_FILE); //Extractor.aannotation = reader.parse(); //CHANGE: gathers training samples based on the first category only Extractor.trainingTime = new Hashtable();//int[((AXML.Category)Extractor.aannotation.Categories[0]).Labels.Count]; //for (int i = 0; (i < Extractor.trainingTime.Length); i++) foreach (AXML.Label label in ((AXML.Category)Extractor.aannotation.Categories[0]).Labels) Extractor.trainingTime.Add(label.Name, 0); //Extractor.trainingTime[i] = 0; Extractor.trainingCompleted = false; Extractor.inputRowSize = sannotation.Sensors.Count * 3; Extractor.fftInterpolationPower = dconfiguration.FFTInterpolatedPower; Extractor.fftMaximumFrequencies = dconfiguration.FFTMaximumFrequencies; //Extractor.trainingTimePerClass = configuration.TrainingTime; //Extractor.trainingWaitTime = configuration.TrainingWaitTime; Extractor.inputColumnSize = (int)Math.Pow(2, Extractor.fftInterpolationPower); Extractor.num_features = Extractor.inputRowSize; // number of distances Extractor.num_features += 1; //total mean; Extractor.num_features += Extractor.inputRowSize; // number of variances Extractor.num_features += Extractor.inputRowSize; // number of ranges Extractor.num_features += 2 * Extractor.fftMaximumFrequencies * Extractor.inputRowSize; // number of fft magnitudes and frequencies Extractor.num_features += Extractor.inputRowSize; // number of energies Extractor.num_features += ((Extractor.inputRowSize * Extractor.inputRowSize) - Extractor.inputRowSize) / 2; //correlation coefficients off-di Extractor.features = new double[Extractor.num_features]; Extractor.arffAttriburesLabels = new string[Extractor.num_features]; Extractor.standardized = new double[inputRowSize][]; for (int i = 0; (i < inputRowSize); i++) Extractor.standardized[i] = new double[Extractor.inputColumnSize];//input[0].Length]; Extractor.means = new double[inputRowSize]; inputFFT = new int[Extractor.inputColumnSize]; FFT.Initialize(fftInterpolationPower, fftMaximumFrequencies); Extractor.aMITesDecoder = aMITesDecoder; //Create the ARFF File header string arffHeader = "@RELATION wockets\n\n" + Extractor.GetArffHeader();//sannotation.Sensors.Count * 3, configuration.FFTMaximumFrequencies); arffHeader += "@ATTRIBUTE activity {"; foreach (AXML.Label label in ((AXML.Category)Extractor.aannotation.Categories[0]).Labels) arffHeader += label.Name.Replace(' ', '_') + ","; arffHeader += "unknown}\n"; arffHeader += "\n@DATA\n\n"; //Calculating windowing parameters //total number of points per interpolated window Extractor.INTERPOLATED_SAMPLING_RATE_PER_WINDOW = (int)Math.Pow(2, dconfiguration.FFTInterpolatedPower); //128; //expected sampling rate per MITes Extractor.EXPECTED_SAMPLING_RATE = dconfiguration.ExpectedSamplingRate / sannotation.Sensors.Count; //samples per second //expected samples per window Extractor.EXPECTED_WINDOW_SIZE = (int)(EXPECTED_SAMPLING_RATE * (dconfiguration.WindowTime / 1000.0)); // expectedSamplingRate per window //what would be considered a good sampling rate Extractor.EXPECTED_GOOD_SAMPLING_RATE = EXPECTED_WINDOW_SIZE - (int)(dconfiguration.MaximumNonconsecutiveFrameLoss * EXPECTED_WINDOW_SIZE); //number of packets lost per second //space between samples Extractor.EXPECTED_SAMPLES_SPACING = (double)dconfiguration.WindowTime / EXPECTED_WINDOW_SIZE; //space between interpolated samples Extractor.INTERPOLATED_SAMPLES_SPACING = (double)dconfiguration.WindowTime / INTERPOLATED_SAMPLING_RATE_PER_WINDOW; //window counters and delimiters Extractor.next_window_end = 0; Extractor.total_window_count = 0; Extractor.num_feature_windows = 0; //data quality variables Extractor.isAcceptableLossRate = true; Extractor.isAcceptableConsecutiveLoss = true; Extractor.unacceptable_window_count = 0; Extractor.unacceptable_consecutive_window_loss_count = 0; //2 D array that stores Sensor axes + time stamps on each row X expected WINDOW SIZE Extractor.data = new double[Extractor.sannotation.Sensors.Count * 4][]; // 1 row for each axis // 2D array that stores Sensor axes X INTERPOLATED WINDOW SIZE Extractor.interpolated_data = new double[Extractor.sannotation.Sensors.Count * 3][]; // array to store the y location for each axes as data is received // will be different for every sensor of course Extractor.y_index = new int[Extractor.sannotation.Sensors.Count]; //Initialize expected data array for (int j = 0; (j < (Extractor.sannotation.Sensors.Count * 4)); j++) { Extractor.data[j] = new double[EXPECTED_WINDOW_SIZE]; for (int k = 0; (k < EXPECTED_WINDOW_SIZE); k++) Extractor.data[j][k] = 0; } //Initialize interpolated data array for (int j = 0; (j < (Extractor.sannotation.Sensors.Count * 3)); j++) { Extractor.interpolated_data[j] = new double[INTERPOLATED_SAMPLING_RATE_PER_WINDOW]; for (int k = 0; (k < INTERPOLATED_SAMPLING_RATE_PER_WINDOW); k++) Extractor.interpolated_data[j][k] = 0; } //Initialize y index for each sensor for (int j = 0; (j < Extractor.sannotation.Sensors.Count); j++) Extractor.y_index[j] = 0; }