private void InitializeInterface()
 {
     AXML.Reader areader = new AXML.Reader(Constants.MASTER_DIRECTORY, this.dataDirectory);
     if (areader.validate() == true)
         this.aannotation = areader.parse();
     foreach (AXML.Label label in ((AXML.Category)this.aannotation.Categories[0]).Labels)            
         this.listBox1.Items.Add(label.Name);
     this.button2.Enabled = false;
 }
示例#2
0
        public Annotation parse()
        {
            Annotation annotation = new Annotation();
            XmlDocument dom = new XmlDocument();
            dom.Load(this.xmlFile);
            XmlNode xNode=dom.DocumentElement;

            if ( (xNode.Name == Constants.ANNOTATION_ELEMENT) && (xNode.HasChildNodes))
            {
                
                foreach (XmlNode iNode in xNode.ChildNodes)
                {
                    //Console.WriteLine(iNode.Name);

                    //parsing file information
                    if (iNode.Name == Constants.FILE_INFO_ELEMENT)
                    {                        
                        foreach (XmlNode jNode in iNode.ChildNodes)
                        {
                            //Console.WriteLine(jNode.Name);

                            foreach (XmlAttribute jAttribute in jNode.Attributes)
                            {
                                if (jAttribute.Name == Constants.NAME_ATTRIBUTE)
                                {
                                    if (jNode.Name == Constants.SUBJECT_ELEMENT)
                                    {
                                        annotation.FileInfo.Subject = jAttribute.Value;
                                    }
                                    else if (jNode.Name == Constants.OBSERVER_ELEMENT)
                                    {                                        
                                        annotation.FileInfo.Observer = jAttribute.Value;
                                    }
                                    else if (jNode.Name == Constants.LOCATION_ELEMENT)
                                    {
                                        annotation.FileInfo.Location = jAttribute.Value;
                                    }
                                    else if (jNode.Name == Constants.NOTES_ELEMENT)
                                    {
                                        annotation.FileInfo.Notes = jAttribute.Value;
                                    }                                    
                                }
                            }


                          
                        }
                    }
                        //parsing labels
                    else if (iNode.Name == Constants.LABELS_ELEMENT)
                    {

                        //parsing the categories
                        foreach (XmlNode jNode in iNode.ChildNodes)
                        {
                            //double frequency = 1500;
                            if (jNode.Name == Constants.CATEGORY_ELEMENT)
                            {
                                Category category = new Category();

                                //parsing category attributes
                                foreach (XmlAttribute jAttribute in jNode.Attributes)
                                {
                                   if (jAttribute.Name == Constants.NAME_ATTRIBUTE)
                                   {
                                            category.Name=jAttribute.Value.Replace(' ','-').Replace(',','_').ToLower();
                                   }else if (jAttribute.Name == Constants.PROPERTY_ATTRIBUTE)
                                   {
                                            category.Property=jAttribute.Value;
                                   }
                                }
          
                                //parsing category labels
                                foreach (XmlNode kNode in jNode.ChildNodes)
                                {
                                    foreach (XmlAttribute kAttribute in kNode.Attributes)
                                    {
                                        if (kAttribute.Name == Constants.NAME_ATTRIBUTE)
                                        {
                                            Label newlabel = new Label(kAttribute.Value.Replace(' ', '-').Replace(',', '_').ToLower(), category.Name);
                                            //newlabel.InitializeTone(this.caller.Handle.ToInt32(),frequency);
                                            category.Labels.Add(newlabel);
                                            //frequency += 200;
                                        }
                                    }
                                }

                                annotation.Categories.Add(category);
                            }
                        }

                    }
                    else if (iNode.Name == Constants.DATA_ELEMENT)
                    {
                        int startYear = 0, startMonth = 0, startDay = 0, startHour = 0,startMinute = 0,startSecond = 0, startMillisecond=0;
                        int endYear = 0, endMonth = 0, endDay = 0, endHour = 0, endMinute = 0, endSecond = 0, endMillisecond = 0;

                        //parsing Annotated Records
                        foreach (XmlNode jNode in iNode.ChildNodes)
                        {
                            //double frequency = 1500;
                            if (jNode.Name == Constants.LABEL_ELEMENT)
                            {                                                                                       

                                //parsing category attributes
                                foreach (XmlAttribute jAttribute in jNode.Attributes)
                                {                                  
                                    if (jAttribute.Name == Constants.DATE_ATTRIBUTE)
                                    {
                                        //parse date
                                        string p =@"(\d+)/(\d+)/(\d+)";
                                        Match m = Regex.Match(jAttribute.Value, p);
                                        if (m.Groups.Count == 4)
                                        {
                                            startMonth = endMonth = Convert.ToInt32(m.Groups[1].Value);
                                            startDay = endDay = Convert.ToInt32(m.Groups[2].Value);
                                            startYear = endYear = Convert.ToInt32(m.Groups[3].Value);

                                        }
                                        else
                                            throw new Exception("Error parsing " + Constants.DATE_ATTRIBUTE + ". " + jAttribute.Value);
                                    }
                                    else if (jAttribute.Name == Constants.STARTDATE_ATTRIBUTE)
                                    {
                                        //parse date
                                        string p = @"(\d+)-(\d+)-(\d+)";
                                        Match m = Regex.Match(jAttribute.Value, p);
                                        if (m.Groups.Count == 4)
                                        {
                                            startMonth = Convert.ToInt32(m.Groups[1].Value);
                                            startDay =  Convert.ToInt32(m.Groups[2].Value);
                                            startYear = Convert.ToInt32(m.Groups[3].Value);

                                        }
                                        else
                                            throw new Exception("Error parsing " + Constants.STARTDATE_ATTRIBUTE + ". " + jAttribute.Value);
                                    }
                                    else if (jAttribute.Name == Constants.ENDDATE_ATTRIBUTE)
                                    {
                                        //parse date
                                        string p = @"(\d+)-(\d+)-(\d+)";
                                        Match m = Regex.Match(jAttribute.Value, p);
                                        if (m.Groups.Count == 4)
                                        {
                                            endMonth = Convert.ToInt32(m.Groups[1].Value);
                                            endDay = Convert.ToInt32(m.Groups[2].Value);
                                            endYear = Convert.ToInt32(m.Groups[3].Value);

                                        }
                                        else
                                            throw new Exception("Error parsing " + Constants.ENDDATE_ATTRIBUTE + ". " + jAttribute.Value);
                                    }
                                    else if (jAttribute.Name == Constants.STARTTIME_ATTRIBUTE)
                                    {
                                        string p = @"(\d+):(\d+):(\d+)\.(\d+)";
                                        Match m = Regex.Match(jAttribute.Value, p);
                                        if (m.Groups.Count == 5)
                                        {
                                            startHour= Convert.ToInt32(m.Groups[1].Value);
                                            startMinute = Convert.ToInt32(m.Groups[2].Value);
                                            startSecond = Convert.ToInt32(m.Groups[3].Value);
                                            startMillisecond = Convert.ToInt32(m.Groups[4].Value);                                 
                                        }
                                        else
                                            throw new Exception("Error parsing " + Constants.STARTTIME_ATTRIBUTE + ". " + jAttribute.Value);

                                    }
                                    else if (jAttribute.Name == Constants.ENDTIME_ATTRIBUTE)
                                    {
                                        string p = @"(\d+):(\d+):(\d+)\.(\d+)";
                                        Match m = Regex.Match(jAttribute.Value, p);
                                        if (m.Groups.Count == 5)
                                        {
                                            endHour = Convert.ToInt32(m.Groups[1].Value);
                                            endMinute = Convert.ToInt32(m.Groups[2].Value);
                                            endSecond = Convert.ToInt32(m.Groups[3].Value);
                                            endMillisecond = Convert.ToInt32(m.Groups[4].Value);
                                        }
                                        else
                                            throw new Exception("Error parsing " + Constants.ENDTIME_ATTRIBUTE+ ". " + jAttribute.Value);

                                    }
                                }
                                DateTime startDT = new DateTime(startYear, startMonth, startDay, startHour, startMinute,
                                    startSecond, startMillisecond);
                                DateTime endDT = new DateTime(endYear, endMonth, endDay, endHour, endMinute, endSecond,
                                    endMillisecond);
                                AnnotatedRecord annotatedRecord = new AnnotatedRecord();
                                
                                annotatedRecord.StartDate = startDT.ToString("MM-dd-yyyy");
                                annotatedRecord.StartHour = startDT.Hour;
                                annotatedRecord.StartMinute = startDT.Minute;
                                annotatedRecord.StartSecond = startDT.Second;
                                TimeSpan ts = startDT.Subtract(new DateTime(1970, 1, 1, 0, 0, 0,0));
                                //TimeSpan ts = startDT - new DateTime(1970, 1, 1, 0, 0, 0, 0);
                                annotatedRecord.StartUnix = ts.TotalMilliseconds;
                                //for some data might require day light savings adjustment
                                annotatedRecord.StartUnix -= 1 * 60 * 60 * 1000;

                                annotatedRecord.EndDate = endDT.ToString("MM-dd-yyyy");
                                annotatedRecord.EndHour = endDT.Hour;
                                annotatedRecord.EndMinute = endDT.Minute;
                                annotatedRecord.EndSecond = endDT.Second;
                                ts = endDT.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, 0,0));
                                //ts = endDT - new DateTime(1970, 1, 1, 0, 0, 0, 0, 0);
                                annotatedRecord.EndUnix = ts.TotalMilliseconds;    
                                annotatedRecord.EndUnix -= 1 * 60 * 60 * 1000;


                                //parsing values
                                foreach (XmlNode kNode in jNode.ChildNodes)
                                {
                                    string label = "", category = "";

                                    foreach (XmlAttribute kAttribute in kNode.Attributes)
                                    {
                                        if (kAttribute.Name == Constants.LABEL_ATTRIBUTE)
                                        {
                                            label = kAttribute.Value;
                                        }
                                        else if (kAttribute.Name == Constants.CATEGORY_ATTRIBUTE)
                                        {
                                            category= kAttribute.Value;
                                        }
                                    }

                                    Label newlabel = new Label(label, category);
                                    annotatedRecord.Labels.Add(newlabel);
                                }

                                annotation.Data.Add(annotatedRecord);
                            }
                        }

                    }
                }
            }

            return annotation;            
        }
 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);
 }
示例#4
0
 public Annotation copy()
 {
     Annotation ann = new Annotation(output_file, output_csv_file);
     foreach (Category cat in categories)
         ann.Categories.Add(cat);
     foreach (AnnotatedRecord annRecord in data)
         ann.Data.Add(annRecord.copy());
     ann.DataDirectory = dataDirectory;
     ann.FileInfo.Subject = fileInfo.Subject;
     ann.FileInfo.Observer = fileInfo.Observer;
     ann.FileInfo.Notes = fileInfo.Notes;
     ann.FileInfo.Location = fileInfo.Location;
     return ann;
 }
示例#5
0
        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();
        }
示例#6
0
        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();
        }
        private void InitializeAnnotator(string dataDirectory)
        {
            AXML.Reader reader = new AXML.Reader(Constants.MASTER_DIRECTORY,dataDirectory);
            if (reader.validate() == false)
            {
                throw new Exception("Error Code 0: XML format error - activities.xml does not match activities.xsd!");
            }
            else
            {
                this.annotation = reader.parse();
                this.annotation.DataDirectory = dataDirectory;


                SXML.Reader sreader = new SXML.Reader(Constants.MASTER_DIRECTORY, dataDirectory);
                if (sreader.validate() == false)
                {
                    throw new Exception("Error Code 0: XML format error - sensors.xml does not match sensors.xsd!");
                }
                else
                {
                    this.sensors = sreader.parse(Constants.MAX_CONTROLLERS);
                    InitializeTimers();
                    //InitializeSound();
                    InitializeInterface();
                }
            }
        }
        public MITesDataCollectionForm(string dataDirectory)
        {
            progressMessage = null;
            Thread t = new Thread(new ThreadStart(ProgressThread));
            t.Start();         
                 

            InitializeComponent();            
            this.dataDirectory = dataDirectory;
         

            //read the sensor configuration file to determine the number of receivers
            //read the activity configuration file
            progressMessage="Loading XML protocol and sensors ...";
            AXML.Reader reader = new AXML.Reader(Constants.MASTER_DIRECTORY, dataDirectory);
            if (reader.validate() == false)
            {
                throw new Exception("Error Code 0: XML format error - activities.xml does not match activities.xsd!");
            }
            else
            {
                this.annotation = reader.parse();
                this.annotation.DataDirectory = dataDirectory;


                SXML.Reader sreader = new SXML.Reader(Constants.MASTER_DIRECTORY, dataDirectory);
                if (sreader.validate() == false)
                {
                    throw new Exception("Error Code 0: XML format error - sensors.xml does not match sensors.xsd!");
                }
                else
                {
                    this.sensors = sreader.parse(Constants.MAX_CONTROLLERS);
                   
                    progressMessage+=" Completed\r\n";
                }
            }

            if (this.sensors.IsHR)
                this.maxPlots = this.sensors.Sensors.Count - 1;
            else
                this.maxPlots = this.sensors.Sensors.Count;

            //check number of sensors etc.

            progressMessage+= "Initializing Timers ...";
            InitializeTimers();
            progressMessage += " Completed\r\n";

            progressMessage += "Initializing GUI ...";
            InitializeInterface();
            progressMessage += " Completed\r\n";

            if ((this.sensors.TotalReceivers > 0) && (this.sensors.TotalReceivers <=Constants.MAX_CONTROLLERS))
            {
                this.mitesControllers = new MITesReceiverController[this.sensors.TotalReceivers];               
                this.mitesDecoders = new MITesDecoder[this.sensors.TotalReceivers];
                this.aMITesActivityCounters = new Hashtable();
                //if (this.sensors.IsHR)
                //    this.aMITesActivityCounters = new MITesActivityCounter[this.sensors.Sensors.Count-1];
                //else
                //    this.aMITesActivityCounters = new MITesActivityCounter[this.sensors.Sensors.Count - 1];

                progressMessage += "Initializing MITes ... searching " + this.sensors.TotalReceivers + " receivers\r\n";
                if (InitializeMITes(dataDirectory) == false)
                {
                    MessageBox.Show("Exiting: You picked a configuration with "+this.sensors.TotalReceivers +" receivers. Please make sure they are attached to the computer.");
#if (PocketPC)
                        Application.Exit();
#else
                    Environment.Exit(0);
#endif
                }
            }


            progressMessage += "Initializing MITes Quality GUI ...";
            InitializeQualityInterface();
            progressMessage += " Completed\r\n";


                //pass data directory
            isExtracting = false;
            Extractor.Initialize( this.mitesDecoders[0], dataDirectory,this.annotation,this.sensors);

            // MITes Data Filterer stores performance stats for MITes
            // Initialize all performance counters for all MITES channels
            //calculate good sampling rate           
            //you need to initialize them all because sometimes mites get data from non-exisiting IDs???
            for (int i = 0; i < MITesData.MAX_MITES_CHANNELS; i++)            
               MITesDataFilterer.MITesPerformanceTracker[i] = new MITesPerformanceStats(0);
            //based on how many receivers and to what channels they are listening adjust the good sampling rate
            foreach (Sensor sensor in this.sensors.Sensors)
            {
                int sensor_id = Convert.ToInt32(sensor.ID);
                int receiver_id = Convert.ToInt32(sensor.Receiver);
                if (sensor_id == 0) //HR sensor
                {
                    MITesDataFilterer.MITesPerformanceTracker[sensor_id].GoodRate =(int)(Constants.HR_SAMPLING_RATE*0.8);
                    MITesDataFilterer.MITesPerformanceTracker[sensor_id].PerfectRate = Constants.HR_SAMPLING_RATE;
                }
                else
                {
                    int goodSamplingRate = (int)((Extractor.Configuration.ExpectedSamplingRate * (1 - Extractor.Configuration.MaximumNonconsecutiveFrameLoss)) / this.sensors.NumberSensors[receiver_id]);
                    MITesDataFilterer.MITesPerformanceTracker[sensor_id].GoodRate = goodSamplingRate;
                    MITesDataFilterer.MITesPerformanceTracker[sensor_id].PerfectRate = (int)((Extractor.Configuration.ExpectedSamplingRate) / this.sensors.NumberSensors[receiver_id]);
                }
            }


            //create some counters for activity counts
            averageX=new int[this.sensors.MaximumSensorID+1];
            averageY= new int[this.sensors.MaximumSensorID+1];
            averageZ= new int[this.sensors.MaximumSensorID+1];

            averageRawX = new int[this.sensors.MaximumSensorID + 1];
            averageRawY = new int[this.sensors.MaximumSensorID + 1];
            averageRawZ = new int[this.sensors.MaximumSensorID + 1];

            prevX= new int[this.sensors.MaximumSensorID+1];
            prevY= new int[this.sensors.MaximumSensorID+1];
            prevZ= new int[this.sensors.MaximumSensorID+1];
            acCounters = new int[this.sensors.MaximumSensorID + 1];
            activityCountWindowSize = 0;

            activityCountCSVs = new StreamWriter[this.sensors.MaximumSensorID + 1];
            samplingCSVs = new StreamWriter[this.sensors.MaximumSensorID + 1];
            averagedRaw = new StreamWriter[this.sensors.MaximumSensorID + 1];
            masterCSV = new StreamWriter(dataDirectory + "\\MITesSummaryData.csv");
            hrCSV = new StreamWriter(dataDirectory + "\\HeartRate_MITes.csv");
            
            string csv_line1 = "UnixTimeStamp,TimeStamp,X,Y,Z";
            string csv_line2 = "UnixTimeStamp,TimeStamp,Sampling";
            string hr_csv_header = "UnixTimeStamp,TimeStamp,HR";  
            string master_csv_header = "UnixTimeStamp,TimeStamp";
            foreach (Category category in this.annotation.Categories)
                master_csv_header += ","+ category.Name;


            foreach (Sensor sensor in this.sensors.Sensors)
            {
                int sensor_id = Convert.ToInt32(sensor.ID);
                string location = sensor.Location.Replace(' ', '-');
                if (sensor_id > 0) //exclude HR
                {
                    activityCountCSVs[sensor_id] = new StreamWriter(dataDirectory + "\\MITes_" +sensor_id.ToString("00")+"_ActivityCount_"+location+".csv");
                    activityCountCSVs[sensor_id].WriteLine(csv_line1);
                    averagedRaw[sensor_id] = new StreamWriter(dataDirectory + "\\MITes_" + sensor_id.ToString("00") + "_1s-RawMean_" + location + ".csv");
                    averagedRaw[sensor_id].WriteLine(csv_line1);
                    samplingCSVs[sensor_id] = new StreamWriter(dataDirectory + "\\MITes_" + sensor_id.ToString("00") + "_SampleRate_" + location + ".csv");
                    samplingCSVs[sensor_id].WriteLine(csv_line2);
                    master_csv_header += ",MITes" + sensor_id.ToString("00") + "_SR," + "MITes" + sensor_id.ToString("00") + "_AVRaw_X," +
                        "MITes" + sensor_id.ToString("00") + "_AVRaw_Y," + "MITes" + sensor_id.ToString("00") + "_AVRaw_Z," + "MITes" + sensor_id.ToString("00") + "_AC_X," +
                        "MITes" + sensor_id.ToString("00") + "_AC_Y," + "MITes" + sensor_id.ToString("00") + "_AC_Z";

                }
            }

            master_csv_header += ",HR";
            this.masterCSV.WriteLine(master_csv_header);
            this.hrCSV.WriteLine(hr_csv_header);

            //activityCountCSV=new StreamWriter(dataDirectory+"\\"+Constants.ACTIVITY_COUNT_FILENAME);
            //string csv_line = "";
            //csv_line += "UnixTimeStamp";
            //foreach (Sensor sensor in this.sensors.Sensors)
            //{
            //    int sensor_id = Convert.ToInt32(sensor.ID);
            //    if (sensor_id > 0) //exclude HR
            //    {
            //        csv_line += ",ACC_" + sensor_id + "_SAMP";
            //        csv_line += ",ACC_" + sensor_id + "_X";
            //        csv_line += ",ACC_" + sensor_id + "_Y";
            //        csv_line += ",ACC_" + sensor_id + "_Z";
            //    }
            //}
            //activityCountCSV.WriteLine(csv_line);

            UnixTime.InitializeTime(); //passed to adjust time when its granularity is not good
#if (PocketPC)
            this.tabControl1.SelectedIndex= 0;
#endif
            isCollectingSimpleData = false;
            isCollectingDetailedData = false;
            isPlotting = true;

            //simple data collection per second
            //this.tws = new TextWriter[MITesData.MAX_MITES_CHANNELS];
            //for (int i = 0; (i < MITesData.MAX_MITES_CHANNELS); i++)
            //    this.tws[i] = null;

            //this.mitesLastTimeStamps=new double[MITesData.MAX_MITES_CHANNELS];
            //this.mitesSampleCounters= new int[MITesData.MAX_MITES_CHANNELS];
            //this.xs=new int[MITesData.MAX_MITES_CHANNELS];
            //this.ys = new int[MITesData.MAX_MITES_CHANNELS];
            //this.zs = new int[MITesData.MAX_MITES_CHANNELS];

            //For demo
            //this.textBoxHR.Visible = false;
            //this.textBoxAC3.Visible = false;
            //this.label11.Text = "";

            bool startReceiverThreads = true;
            for (int i = 0; (i<this.sensors.TotalReceivers); i++)
            {
                if ((this.mitesControllers[i] == null) || (this.mitesControllers[i].GetComPortNumber() == 0))
                    startReceiverThreads = false;
            }

            if (startReceiverThreads == true)
                isStartedReceiver = true;
            else
            {
                Application.Exit();
            }


            t.Abort();
#if (PocketPC)
#else
            this.ShowForms();
#endif

            
            //Last thing enable the timers
            this.readDataTimer.Enabled = true;
            this.qualityTimer.Enabled = true;
            this.HRTimer.Enabled = true;
        }
        public MITesDataCollectionForm(string dataDirectory)
        {

           //where data is being stored
            this.dataDirectory = dataDirectory;

            //Initialize high resolution unix timer
            UnixTime.InitializeTime();

            //Initialize and start GUI progress thread
            progressMessage = null;
            aProgressThread = new Thread(new ThreadStart(ProgressThread));
            aProgressThread.Start();


            #region Load Configuration files
            //load the activity and sensor configuration files
            progressMessage = "Loading XML protocol and sensors ...";
            AXML.Reader reader = new AXML.Reader(Constants.MASTER_DIRECTORY, dataDirectory);
#if (!PocketPC)
            if (reader.validate() == false)
            {
                throw new Exception("Error Code 0: XML format error - activities.xml does not match activities.xsd!");
            }
            else
            {
#endif
            this.annotation = reader.parse();
            this.annotation.DataDirectory = dataDirectory;
            SXML.Reader sreader = new SXML.Reader(Constants.MASTER_DIRECTORY, dataDirectory);
#if (!PocketPC)

                if (sreader.validate() == false)
                {
                    throw new Exception("Error Code 0: XML format error - sensors.xml does not match sensors.xsd!");
                }
                else
                {
#endif
            this.sensors = sreader.parse(Constants.MAX_CONTROLLERS);
            progressMessage += " Completed\r\n";

            //TODO: remove BT components
            progressMessage += "Loading configuration file ...";
            MITesFeatures.core.conf.ConfigurationReader creader = new MITesFeatures.core.conf.ConfigurationReader(dataDirectory);
            this.configuration = creader.parse();
            progressMessage += " Completed\r\n";
#if (!PocketPC)
                }
            }
#endif
            #endregion Load Configuration files

            //Initialize 1 master decoder
            this.masterDecoder = new MITesDecoder();


            #region Initialize External Data Reception Channels
            //Initialize Data reception for Bluetooth and USB
            if ((this.sensors.TotalReceivers > 0) && (this.sensors.TotalReceivers <= Constants.MAX_CONTROLLERS))
            {

                //Initialize arrays to store USB and Bluetooth controllers
                this.mitesControllers = new MITesReceiverController[this.sensors.TotalWiredReceivers];
#if (PocketPC)
                this.bluetoothControllers = new BluetoothController[this.sensors.TotalBluetoothReceivers];
               // this.ts = new Thread[this.sensors.TotalBluetoothReceivers];
#endif

                //Initialize array to store Bluetooth connection status
                //this.bluetoothConnectionStatus = new bool[this.sensors.TotalBluetoothReceivers];

                //Initialize a decoder for each sensor
                this.mitesDecoders = new MITesDecoder[this.sensors.TotalReceivers];
                
                this.aMITesActivityCounters = new Hashtable();

#if (PocketPC)
                #region Bluetooth reception channels initialization
                //Initialize and search for wockets connections
                progressMessage += "Initializing Bluetooth receivers ... searching " + this.sensors.TotalBluetoothReceivers+ " BT receivers\r\n";                
                //Try to initialize all Bluetooth receivers 10 times then exit
                int initializationAttempt = 0;
                while (initializationAttempt <= 10)
                {
                    if (InitializeBluetoothReceivers() == false)
                    {
                        initializationAttempt++;

                        if (initializationAttempt == 10)
                        {
                            MessageBox.Show("Exiting: Some Bluetooth receivers in your configuration were not initialized.");

                            Application.Exit();
                            System.Diagnostics.Process.GetCurrentProcess().Kill();
                        }
                        else
                            progressMessage += "Failed to initialize all BT connections. Retrying (" + initializationAttempt + ")...\r\n";

                    }
                    else
                        break;
                    Thread.Sleep(2000);
                }
                #endregion Bluetooth reception channels initialization
#endif

                #region USB reception channels initialization

                if (InitializeUSBReceivers() == false)
                {
                    MessageBox.Show("Exiting: Some USB receivers in your configuration were not initialized.");
#if (PocketPC)
                    Application.Exit();
                    System.Diagnostics.Process.GetCurrentProcess().Kill();
#else
                    Environment.Exit(0);
#endif

                }
                #endregion USB reception channels initialization
            }
            #endregion Initialize External Data Reception Channels

#if (PocketPC)
            #region Initialize Builtin Data Reception Channels
            if (InitializeBuiltinReceivers() == false)
            {
                MessageBox.Show("Exiting: A built in receiver channel was not found.");

                Application.Exit();
                System.Diagnostics.Process.GetCurrentProcess().Kill();


            }
            #endregion Initialize Builtin Data Reception Channels
#endif


            #region Initialize GUI Components
            //initialize the interface components
            InitializeComponent();
            //Initialize GUI timers
            progressMessage += "Initializing Timers ...";
            InitializeTimers();
            progressMessage += " Completed\r\n";

            //Initialize different GUI components
            progressMessage += "Initializing GUI ...";
            InitializeInterface();
            progressMessage += " Completed\r\n";

            this.isPlotting = true;
            //count the number of accelerometers
            if (this.sensors.IsHR)
                this.maxPlots = this.sensors.Sensors.Count - 1;
            else
                this.maxPlots = this.sensors.Sensors.Count;
            SetFormPositions();
            if (this.sensors.TotalReceivers > 0)
                aMITesPlotter = new MITesScalablePlotter(this.panel1, MITesScalablePlotter.DeviceTypes.IPAQ, maxPlots, this.masterDecoder, GetGraphSize(false));
            else
                aMITesPlotter = new MITesScalablePlotter(this.panel1, MITesScalablePlotter.DeviceTypes.IPAQ, maxPlots, this.masterDecoder, GetGraphSize(false));

            //Override the resize event
#if (PocketPC)
            this.Resize += new EventHandler(OnResize);
#else
            this.form1.Resize += new EventHandler(OnResizeForm1);
            this.form1.FormClosing += new FormClosingEventHandler(form_FormClosing);
            this.form2.Resize += new EventHandler(OnResizeForm2);
            this.form2.FormClosing += new FormClosingEventHandler(form_FormClosing);
            this.form3.Resize += new EventHandler(OnResizeForm3);
            this.form3.FormClosing += new FormClosingEventHandler(form_FormClosing);
            this.form4.Resize += new EventHandler(OnResizeForm4);
            this.form4.FormClosing += new FormClosingEventHandler(form_FormClosing);
#endif

            //Initialize the quality interface
            progressMessage += "Initializing MITes Quality GUI ...";
            InitializeQualityInterface();
            progressMessage += " Completed\r\n";

            //Remove classifier tabs
#if (PocketPC)

            this.tabControl1.TabPages.RemoveAt(4);
            this.tabControl1.SelectedIndex = 0;
#else
            this.ShowForms();
#endif


            #endregion Initialize GUI Components

            #region Initialize Feature Extraction
            this.isExtracting = false;
            if (this.sensors.TotalReceivers > 0) // if there is at least 1 MIT
                //Extractor.Initialize(this.mitesDecoders[0], dataDirectory, this.annotation, this.sensors, this.configuration);
                Extractor.Initialize(this.masterDecoder, dataDirectory, this.annotation, this.sensors, this.configuration);
            else if (this.sensors.Sensors.Count > 0) // only built in
                Extractor.Initialize(new MITesDecoder(), dataDirectory, this.annotation, this.sensors, this.configuration);
            #endregion Initialize Feature Extraction

            #region Initialize Quality Tracking variables
            InitializeQuality();
            #endregion Initialize Quality Tracking variables

            #region Initialize Logging
            InitializeLogging(dataDirectory);
            #endregion Initialize Logging

            #region Initialize CSV Storage (PC Only)
#if (!PocketPC)

            //create some counters for activity counts
            averageX = new int[this.sensors.MaximumSensorID + 1];
            averageY = new int[this.sensors.MaximumSensorID + 1];
            averageZ = new int[this.sensors.MaximumSensorID + 1];

            averageRawX = new int[this.sensors.MaximumSensorID + 1];
            averageRawY = new int[this.sensors.MaximumSensorID + 1];
            averageRawZ = new int[this.sensors.MaximumSensorID + 1];

            prevX = new int[this.sensors.MaximumSensorID + 1];
            prevY = new int[this.sensors.MaximumSensorID + 1];
            prevZ = new int[this.sensors.MaximumSensorID + 1];
            acCounters = new int[this.sensors.MaximumSensorID + 1];
            activityCountWindowSize = 0;

            activityCountCSVs = new StreamWriter[this.sensors.MaximumSensorID + 1];
            samplingCSVs = new StreamWriter[this.sensors.MaximumSensorID + 1];
            averagedRaw = new StreamWriter[this.sensors.MaximumSensorID + 1];
            masterCSV = new StreamWriter(dataDirectory + "\\MITesSummaryData.csv");
            hrCSV = new StreamWriter(dataDirectory + "\\HeartRate_MITes.csv");

            string csv_line1 = "UnixTimeStamp,TimeStamp,X,Y,Z";
            string csv_line2 = "UnixTimeStamp,TimeStamp,Sampling";
            string hr_csv_header = "UnixTimeStamp,TimeStamp,HR";
            string master_csv_header = "UnixTimeStamp,TimeStamp";
            foreach (Category category in this.annotation.Categories)
                master_csv_header += "," + category.Name;


            foreach (Sensor sensor in this.sensors.Sensors)
            {
                int sensor_id = Convert.ToInt32(sensor.ID);
                string location = sensor.Location.Replace(' ', '-');
                if (sensor_id > 0) //exclude HR
                {
                    activityCountCSVs[sensor_id] = new StreamWriter(dataDirectory + "\\MITes_" + sensor_id.ToString("00") + "_ActivityCount_" + location + ".csv");
                    activityCountCSVs[sensor_id].WriteLine(csv_line1);
                    averagedRaw[sensor_id] = new StreamWriter(dataDirectory + "\\MITes_" + sensor_id.ToString("00") + "_1s-RawMean_" + location + ".csv");
                    averagedRaw[sensor_id].WriteLine(csv_line1);
                    samplingCSVs[sensor_id] = new StreamWriter(dataDirectory + "\\MITes_" + sensor_id.ToString("00") + "_SampleRate_" + location + ".csv");
                    samplingCSVs[sensor_id].WriteLine(csv_line2);
                    master_csv_header += ",MITes" + sensor_id.ToString("00") + "_SR," + "MITes" + sensor_id.ToString("00") + "_AVRaw_X," +
                        "MITes" + sensor_id.ToString("00") + "_AVRaw_Y," + "MITes" + sensor_id.ToString("00") + "_AVRaw_Z," + "MITes" + sensor_id.ToString("00") + "_AC_X," +
                        "MITes" + sensor_id.ToString("00") + "_AC_Y," + "MITes" + sensor_id.ToString("00") + "_AC_Z";

                }
            }

            master_csv_header += ",HR";
            this.masterCSV.WriteLine(master_csv_header);
            this.hrCSV.WriteLine(hr_csv_header);
#endif

            #endregion Initialize CSV Storage (PC Only)

            #region Start Collecting Data
            this.collectDataMode = true;
#if (PocketPC)
            this.isCollectingDetailedData = false;
#else
            this.isCollectingDetailedData = true;
#endif

            //if (this.sensors.TotalReceivers > 0)
            //    isStartedReceiver = true;
            //Start the built in polling thread            
#if (PocketPC)
            if (this.sensors.HasBuiltinSensors)
            {
                this.pollingThread = new Thread(new ThreadStart(this.pollingData));
                this.pollingThread.Priority = ThreadPriority.Lowest;
                this.pollingThread.Start();
            }
#endif

            //Terminate the progress thread
            progressThreadQuit = true;

           
            //Enable all timer functions
            this.readDataTimer.Enabled = true;
            this.qualityTimer.Enabled = true;
            if (this.sensors.IsHR)
                this.HRTimer.Enabled = true;

            #endregion Start Collecting Data

        }
        public MITesDataCollectionForm()
        {

            //Initialize the UNIX QueryPerformanceCounter
            UnixTime.InitializeTime();

            //intialize the mode of the software
            this.collectDataMode = false;
            this.isCollectingDetailedData = false;
            this.isPlotting = false;
            this.isExtracting = false;

            //initialize the progress thread
            progressMessage = null;
            aProgressThread = new Thread(new ThreadStart(ProgressThread));
            aProgressThread.Start();

            //initialize the interface components
            InitializeComponent();

            //Initialize where the data will be stored and where the configuration
            //files exist
            this.dataDirectory = Constants.DEFAULT_DATA_STORAGE_DIRECTORY;


            //load the activity and sensor configuration files
            progressMessage = "Loading XML protocol and sensors ...";
            AXML.Reader reader = new AXML.Reader(Constants.MASTER_DIRECTORY, Constants.DEFAULT_DATA_STORAGE_DIRECTORY);
            this.annotation = reader.parse();
            this.annotation.DataDirectory = Constants.DEFAULT_DATA_STORAGE_DIRECTORY;

            SXML.Reader sreader = new SXML.Reader(Constants.MASTER_DIRECTORY, Constants.DEFAULT_DATA_STORAGE_DIRECTORY);
            this.sensors = sreader.parse(Constants.MAX_CONTROLLERS);

            progressMessage += " Completed\r\n";

            progressMessage += "Loading configuration file ...";
            MITesFeatures.core.conf.ConfigurationReader creader = new MITesFeatures.core.conf.ConfigurationReader(Constants.DEFAULT_DATA_STORAGE_DIRECTORY);
            this.configuration = creader.parse();
            progressMessage += " Completed\r\n";

            //calculate how many plots to be drawn
            if (this.sensors.IsHR)
                this.maxPlots = this.sensors.Sensors.Count - 1;
            else
                this.maxPlots = this.sensors.Sensors.Count;


            //Initialize the timers
            progressMessage += "Initializing Timers ...";
            InitializeTimers();
            progressMessage += " Completed\r\n";

            //Initialize different GUI components
            progressMessage += "Initializing GUI ...";
            InitializeInterface();
            progressMessage += " Completed\r\n";

#if (PocketPC)
            this.tabControl1.TabPages.RemoveAt(4);
            this.tabControl1.TabPages.RemoveAt(3);
            this.tabControl1.TabPages.RemoveAt(2);
            this.tabControl1.TabPages.RemoveAt(0);                       
            this.tabControl1.SelectedIndex = 0;
#endif
            progressThreadQuit = true;
        }
        //This constructor initializes the software for calibration of a list
        //of sensors
        public MITesDataCollectionForm(SensorAnnotation uncalibratedSensors, string dataDirectory)
        {
            //Initialize the UNIX timer to use QueryPerformanceCounter
            UnixTime.InitializeTime();

            //Setup the initial state of the calibration variables
            this.sensors = uncalibratedSensors;
            this.currentCalibrationSensorIndex = 0;
            this.calCounter = 0;

#if (PocketPC)
            this.horizontalMITes = (System.Drawing.Image)new System.Drawing.Bitmap(Constants.MITES_HORIZONTAL_96_96);
            this.verticalMITes = (System.Drawing.Image)new System.Drawing.Bitmap(Constants.MITES_VERTICAL_96_96);
#else
            this.horizontalMITes = (System.Drawing.Image)new System.Drawing.Bitmap(Constants.MITES_HORIZONTAL_288_288);
            this.verticalMITes = (System.Drawing.Image)new System.Drawing.Bitmap(Constants.MITES_VERTICAL_288_288);
#endif

            //create a dummy annotation
            this.annotation = new Annotation();

            //make sure the software will not collect or extract features
            this.collectDataMode = false;
            this.isClassifying = false;
            this.isExtracting = false;
            this.isCollectingDetailedData = false;

            //setup where the sensordata file will be stored
            this.dataDirectory = dataDirectory;

            //setup plotting parameters
            isPlotting = true;
            this.maxPlots = this.sensors.Sensors.Count;

            //Spawn the progress thread
            progressMessage = null;
            aProgressThread = new Thread(new ThreadStart(ProgressThread));
            aProgressThread.Start();





            //Intialize the interface of the forms
            InitializeComponent();
            progressMessage += "Initializing Timers ...";
            InitializeTimers();
            progressMessage += " Completed\r\n";
            progressMessage += "Initializing GUI ...";
            InitializeInterface();
            progressMessage += " Completed\r\n";

            progressMessage += "Loading configuration file ...";
            MITesFeatures.core.conf.ConfigurationReader confreader = new MITesFeatures.core.conf.ConfigurationReader(dataDirectory);
            this.configuration = confreader.parse();
            progressMessage += " Completed\r\n";

#if (PocketPC)

            //setup the Bluetooth if needed
            if (this.configuration.Connection == MITesFeatures.core.conf.Constants.SOFTWARE_CONNECTION_BLUETOOTH)
            {
                progressMessage += "Initializing Bluetooth ...";
                /*
                this.bt = new BluetoothController();
                try
                {
                    this.bluetoothPort = bt.initialize(this.configuration.MacAddress, this.configuration.Passkey);
                }
                catch (Exception e)
                {
                   
                    progressMessage += " Failed\r\n";
                    MessageBox.Show("Failed to find Bluetooth Device... exiting!");
                    bt.close();
                    Application.Exit();
                    System.Diagnostics.Process.GetCurrentProcess().Kill();    
                }
                 */
                progressMessage += " Completed\r\n";
            }
#endif
            //Intialize the MITes Receivers, decoders and counters based
            //on the chosen sensors
            if ((this.sensors.TotalReceivers > 0) && (this.sensors.TotalReceivers <= Constants.MAX_CONTROLLERS))
            {
                this.mitesControllers = new MITesReceiverController[this.sensors.TotalReceivers];
                this.mitesDecoders = new MITesDecoder[this.sensors.TotalReceivers];
                //this.aMITesActivityCounters = new Hashtable();
                progressMessage += "Initializing MITes ... searching " + this.sensors.TotalReceivers + " receivers\r\n";
                if (InitializeMITes(dataDirectory) == false)
                {
                    MessageBox.Show("Exiting: You picked a configuration with " + this.sensors.TotalReceivers + " receivers. Please make sure they are attached to the computer.");
#if (PocketPC)
                    /*
                    bt.close();
                     */
                    Application.Exit();
                    System.Diagnostics.Process.GetCurrentProcess().Kill();    
#else

                    Environment.Exit(0);
                    Application.Exit();
#endif
                }
            }


            //Setup the resize event for each different form
#if (PocketPC)
            this.Resize += new EventHandler(OnResize);
#else
            this.form1.Resize += new EventHandler(OnResizeForm1);
            this.form1.FormClosing += new FormClosingEventHandler(form_FormClosing);
            this.form2.Resize += new EventHandler(OnResizeForm2);
            this.form2.FormClosing += new FormClosingEventHandler(form_FormClosing);
            this.form3.Resize += new EventHandler(OnResizeForm3);
            this.form3.FormClosing += new FormClosingEventHandler(form_FormClosing);
            this.form4.Resize += new EventHandler(OnResizeForm4);
            this.form4.FormClosing += new FormClosingEventHandler(form_FormClosing);
#endif

            //Initialize the interface for reporting the quality of the MITes
            //transmission
            progressMessage += "Initializing MITes Quality GUI ...";
            InitializeQualityInterface();
            progressMessage += " Completed\r\n";

            //Intialize the MITes performance tracking objects
            for (int i = 0; i < MITesData.MAX_MITES_CHANNELS; i++)
                MITesDataFilterer.MITesPerformanceTracker[i] = new MITesPerformanceStats(0);
            foreach (Sensor sensor in this.sensors.Sensors)
            {
                int sensor_id = Convert.ToInt32(sensor.ID);
                int receiver_id = Convert.ToInt32(sensor.Receiver);
                if (sensor_id == 0) //HR sensor
                {
                    MITesDataFilterer.MITesPerformanceTracker[sensor_id].GoodRate = (int)(Constants.HR_SAMPLING_RATE * 0.8);
                    MITesDataFilterer.MITesPerformanceTracker[sensor_id].PerfectRate = Constants.HR_SAMPLING_RATE;
                }
                else
                {
                    int goodSamplingRate = 150;
                    MITesDataFilterer.MITesPerformanceTracker[sensor_id].GoodRate = goodSamplingRate;
                    MITesDataFilterer.MITesPerformanceTracker[sensor_id].PerfectRate = 180;
                }
            }


#if (PocketPC)
            this.tabControl1.SelectedIndex = 0;
#endif


            progressThreadQuit = true;
            //remove unnecessary forms or pages
#if (PocketPC)
            this.tabControl1.TabPages.RemoveAt(1);
            this.tabControl1.TabPages.RemoveAt(2);
            //this.tabControl1.TabPages.RemoveAt(3);
#else
            this.ShowForms();
#endif


            //Only enable the read data time since we are just calibrating
            this.readDataTimer.Enabled = true;

        }