Пример #1
0
        private bool LoadLabelsListToXml()
        {
            TextWriter intervals_file_xml = null;
            TextWriter intervals_file_csv = null;

            TextWriter intervals_file_xml_1 = null;
            TextWriter intervals_file_csv_1 = null;

            TextWriter intervals_file_xml_2 = null;
            TextWriter intervals_file_csv_2 = null;

            Session XmlSession_1 = null;
            Session XmlSession_2 = null;

            try
            {
                string readline;
                DateTime start_time, end_time;
                TimeSpan ts;
                string category, current_label;

                #region generate an annotation file for each category

                // ----------backup and create output files ------------------

                // Annotation Intervals Files
                if (File.Exists( Folder_audioannotation + "AnnotationIntervals.xml"))
                { File.Delete( Folder_audioannotation + "AnnotationIntervals.xml"); }

                if (File.Exists(Folder_audioannotation + "AnnotationIntervals.csv"))
                { File.Delete(Folder_audioannotation + "AnnotationIntervals.csv"); }

                intervals_file_xml = new StreamWriter(Folder_audioannotation + "AnnotationIntervals.xml");
                intervals_file_csv = new StreamWriter(Folder_audioannotation + "AnnotationIntervals.csv");

                // Annotation Intervals Files, category 1
                if (File.Exists(DataSessionDir + "AnnotationIntervals_cat_1.xml"))
                { File.Delete(DataSessionDir + "AnnotationIntervals_cat_1.xml"); }

                if (File.Exists(DataSessionDir + "AnnotationIntervals_cat_1.csv"))
                { File.Delete(DataSessionDir + "AnnotationIntervals_cat_1.csv"); }

                intervals_file_xml_1 = new StreamWriter(DataSessionDir + "AnnotationIntervals_cat_1.xml");
                intervals_file_csv_1 = new StreamWriter(DataSessionDir + "AnnotationIntervals_cat_1.csv");

                // Annotation Intervals Files, category 2
                if (File.Exists(DataSessionDir + "AnnotationIntervals_cat_2.xml"))
                { File.Delete(DataSessionDir + "AnnotationIntervals_cat_2.xml"); }

                if (File.Exists(DataSessionDir + "AnnotationIntervals_cat_2.csv"))
                { File.Delete(DataSessionDir + "AnnotationIntervals_cat_2.csv"); }

                intervals_file_xml_2 = new StreamWriter(DataSessionDir + "AnnotationIntervals_cat_2.xml");
                intervals_file_csv_2 = new StreamWriter(DataSessionDir + "AnnotationIntervals_cat_2.csv");

                // ----------initialize sessions ------------------
                XmlSession_1 = new Session();
                XmlSession_1 = XmlSession.copy();

                XmlSession_2 = new Session();
                XmlSession_2 = XmlSession.copy();

                // ---------- Load labels to sessions 1 and 2------------------

                int count = 0;

                for (int c = 1; c <= 2; c++)
                {

                    if( c == 1)
                    {   count = LabelsList_1.Count; }
                    else
                    {   count = LabelsList_2.Count; }

                    for (int i = 0; i < count ; i++)
                    {
                        if (c == 1)
                        { readline = LabelsList_1[i]; }
                        else
                        { readline = LabelsList_2[i]; }

                        string[] tokens = readline.Split(';');

                        if (tokens[0].CompareTo("ok") == 0)
                        {
                            currentRecord = new Annotation();

                            //Start Time
                            start_time = DateTime.Parse(StartDate + " " + tokens[3]);

                            currentRecord._StartDate = start_time.ToString("yyyy'-'MM'-'dd' 'HH':'mm':'ssK");
                            currentRecord._StartHour = start_time.Hour;
                            currentRecord._StartMinute = start_time.Minute;
                            currentRecord._StartSecond = start_time.Second;
                            currentRecord._StartMillisecond = start_time.Millisecond;

                            ts = (start_time - new DateTime(1970, 1, 1, 0, 0, 0));
                            currentRecord._StartUnix = ts.TotalSeconds;

                            //Stop Time
                            end_time = DateTime.Parse(EndDate + " " + tokens[4]);

                            currentRecord._EndDate = end_time.ToString("yyyy'-'MM'-'dd' 'HH':'mm':'ssK");
                            currentRecord._EndHour = end_time.Hour;
                            currentRecord._EndMinute = end_time.Minute;
                            currentRecord._EndSecond = end_time.Second;
                            currentRecord._EndMillisecond = end_time.Millisecond;

                            ts = (end_time - new DateTime(1970, 1, 1, 0, 0, 0));
                            currentRecord._EndUnix = ts.TotalSeconds;

                            // Labels
                            category = tokens[6];
                            current_label = tokens[5];
                            currentRecord.Activities.Add(new Activity(current_label, category));

                            // Save record to the correspondent session
                            if (c == 1)
                            {  XmlSession_1.Annotations.Add(currentRecord); }
                            else
                            {  XmlSession_2.Annotations.Add(currentRecord); }

                        }

                    }
                }

                // Save session 1 to file
                intervals_file_xml_1.WriteLine(XmlSession_1.ToXML());
                intervals_file_csv_1.WriteLine(XmlSession_1.ToCSV());

                // Save session 2 to file
                intervals_file_xml_2.WriteLine(XmlSession_2.ToXML());
                intervals_file_csv_2.WriteLine(XmlSession_2.ToCSV());

                // Close files session 1
                intervals_file_xml_1.Flush();
                intervals_file_xml_1.Close();

                intervals_file_csv_1.Flush();
                intervals_file_csv_1.Close();

                // Close files session 2
                intervals_file_xml_2.Flush();
                intervals_file_xml_2.Close();

                intervals_file_csv_2.Flush();
                intervals_file_csv_2.Close();

                #endregion

                //set the DataDirectory
                //XmlSession._DataDirectory = DataSessionDir;
                Session XmlSessionOutput = XmlSession_1.Merge(XmlSession_2);

                intervals_file_xml.WriteLine(XmlSessionOutput.ToXML());
                intervals_file_csv.WriteLine(XmlSessionOutput.ToCSV());

                intervals_file_xml.Flush();
                intervals_file_xml.Close();

                intervals_file_csv.Flush();
                intervals_file_csv.Close();

                //Save Labels Colors File
                SaveLabelsColorsToFile();

                return true;
            }
            catch
            {

                #region Close Files

                if (intervals_file_xml != null)
                {
                    intervals_file_xml.Flush();
                    intervals_file_xml.Close();

                }

                if (intervals_file_csv != null)
                {
                    intervals_file_csv.Flush();
                    intervals_file_csv.Close();
                }

                if (intervals_file_xml_1 != null)
                {
                    intervals_file_xml_1.Flush();
                    intervals_file_xml_1.Close();

                }

                if (intervals_file_csv_1 != null)
                {
                    intervals_file_csv_1.Flush();
                    intervals_file_csv_1.Close();
                }

                if (intervals_file_xml_2 != null)
                {
                    intervals_file_xml_2.Flush();
                    intervals_file_xml_2.Close();

                }

                if (intervals_file_csv_2 != null)
                {
                    intervals_file_csv_2.Flush();
                    intervals_file_csv_2.Close();
                }

                #endregion

                return false;
            }
        }
Пример #2
0
        public Annotation copy()
        {
            Annotation ann = new Annotation();

            foreach (Activity act in activities)
            {
                ann.activities.Add(act);
            }

            ann._StartHour= start_hour;
            ann._StartMinute = start_minute;
            ann._StartSecond = start_second;
            ann._StartMillisecond = start_millisecond;
            ann._StartUnix = start_unix;
            ann._StartDate = start_date;

            ann._EndDate = end_date;
            ann._EndHour = end_hour;
            ann._EndMinute = end_minute;
            ann._EndSecond = end_second;
            ann._EndMillisecond = end_millisecond;
            ann._EndUnix = end_unix;

            return ann;
        }