Example #1
0
        public static AnnoList LoadFromCSVFile(String filepath, double samplerate = 1, string type = "legacy", string filter = null)
        {
            AnnoList list = new AnnoList();

            list.Source.File.Path = filepath;
            list.Scheme           = new AnnoScheme();


            try
            {
                StreamReader sr   = new StreamReader(filepath, System.Text.Encoding.Default);
                string       line = null;
                while ((line = sr.ReadLine()) != null)
                {
                    if (type == "semicolon")
                    {
                        list.Scheme.Type = AnnoScheme.TYPE.FREE;
                        string[] data       = line.Split(';');
                        double   start      = Convert.ToDouble(data[0], CultureInfo.InvariantCulture);
                        double   duration   = Convert.ToDouble(data[1]) - Convert.ToDouble(data[0], CultureInfo.InvariantCulture);
                        string   label      = "";
                        string   tier       = "";
                        string   meta       = "";
                        double   confidence = 1.0;
                        if (data.Length > 2)
                        {
                            label = data[2];
                        }
                        if (data.Length > 3)
                        {
                            if (data[3].Contains("#"))
                            {
                                tier = data[3];
                                tier = tier.Remove(0, 1);
                            }
                            else
                            {
                                bool isconfidence = double.TryParse(data[3], out confidence);
                            }
                        }
                        if (data.Length > 4)
                        {
                            for (int i = 4; i < data.Length; i++)
                            {
                                meta += data[i] + ";";
                            }
                        }

                        AnnoListItem e = new AnnoListItem(start, duration, label, meta, Colors.Black, confidence);

                        if (filter == null || tier == filter)
                        {
                            list.AddSorted(e);
                        }
                    }
                    else if (type == "continuous")
                    {
                        list.Scheme.Type       = AnnoScheme.TYPE.CONTINUOUS;
                        list.Scheme.SampleRate = samplerate;
                        string[] data       = line.Split(';');
                        double   start      = Convert.ToDouble(data[0], CultureInfo.InvariantCulture);
                        double   score      = double.NaN;
                        string   tier       = "";
                        double   confidence = 1.0;

                        if (data.Length > 1)
                        {
                            double.TryParse(data[1], out score);
                        }
                        if (data.Length > 2)
                        {
                            bool isconfidence = double.TryParse(data[2], out confidence);
                        }
                        if (data.Length > 3)
                        {
                            list.Scheme.MinScore = double.Parse(data[3]);

                            if (data.Length > 4)
                            {
                                list.Scheme.MaxScore = double.Parse(data[4]);
                            }
                            AnnoListItem e = new AnnoListItem(start, samplerate == 0 ? 0 : 1 / samplerate, score, "Range: " + list.Scheme.MinScore + "-" + list.Scheme.MaxScore, Colors.Black);
                            list.AddSorted(e);
                        }
                        else
                        {
                            AnnoListItem e = new AnnoListItem(start, samplerate == 0 ? 0 : 1 / samplerate, score, "", Colors.Black, 1);
                            if (filter == null || tier == filter)
                            {
                                list.AddSorted(e);
                            }
                        }
                    }
                    else if (type == "legacy")
                    {
                        list.Scheme.Type = AnnoScheme.TYPE.FREE;
                        string[] data;
                        data = line.Split(' ');
                        if (data.Length < 2)
                        {
                            data = line.Split('\t');
                        }
                        if (data.Length < 2)
                        {
                            data = line.Split(';');
                        }

                        double start    = Convert.ToDouble(data[0], CultureInfo.InvariantCulture);
                        double duration = Convert.ToDouble(data[1], CultureInfo.InvariantCulture) - Convert.ToDouble(data[0], CultureInfo.InvariantCulture);
                        string label    = "";
                        string tier     = "";
                        if (data.Length > 2)
                        {
                            label = data[2];
                        }

                        if (data.Length > 3)
                        {
                            tier = data[3];
                        }

                        for (int i = 4; i < data.Length; i++)
                        {
                            label += " " + data[i];
                        }
                        AnnoListItem e = new AnnoListItem(start, duration, label, "", Colors.Black, 1);
                        if (filter == null || tier == filter)
                        {
                            list.AddSorted(e);
                        }
                    }
                }
                sr.Close();;
                list.HasChanged = false;
            }
            catch
            {
                MessageBox.Show("An exception occured while reading annotation from '" + filepath + "'");
            }

            return(list);
        }
Example #2
0
        public static List <AnnoList> LoadfromElanFile(String filepath)
        {
            List <AnnoList> list = new List <AnnoList>();

            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(filepath);


                //Get time order references

                XmlNode time_order = doc.SelectSingleNode("//TIME_ORDER");
                List <KeyValuePair <string, string> > time_order_list = new List <KeyValuePair <string, string> >();
                foreach (XmlNode node in time_order.ChildNodes)
                {
                    time_order_list.Add(new KeyValuePair <string, string>(node.Attributes[0].Value.ToString(), node.Attributes[1].Value.ToString()));
                }


                //Get number of tiers

                int i = 0;
                foreach (XmlNode tier in doc.SelectNodes("//TIER"))
                {
                    AnnoList   al     = new AnnoList();
                    AnnoScheme scheme = new AnnoScheme();


                    scheme.Type = AnnoScheme.TYPE.FREE;


                    string tierid = tier.Attributes.GetNamedItem("TIER_ID").Value.ToString();

                    string role = "";
                    try
                    {
                        role = tier.Attributes.GetNamedItem("PARTICIPANT").Value.ToString();
                    }
                    catch { }


                    al = new AnnoList();
                    al.Source.File.Path = filepath;

                    foreach (XmlNode annotation in tier.ChildNodes)
                    {
                        string  label                = null;
                        string  starttmp             = "";
                        string  endtmp               = "";
                        double  start                = -1;
                        double  end                  = -1;
                        double  duration             = -1;
                        XmlNode alignable_annotation = annotation.FirstChild;

                        starttmp = (from kvp in time_order_list where kvp.Key == alignable_annotation.Attributes.GetNamedItem("TIME_SLOT_REF1").Value.ToString() select kvp.Value).ToList()[0];
                        start    = double.Parse(starttmp, CultureInfo.InvariantCulture) / 1000;
                        endtmp   = (from kvp in time_order_list where kvp.Key == alignable_annotation.Attributes.GetNamedItem("TIME_SLOT_REF2").Value.ToString() select kvp.Value).ToList()[0];
                        end      = double.Parse(endtmp, CultureInfo.InvariantCulture) / 1000;
                        label    = alignable_annotation.FirstChild.InnerText;
                        AnnoScheme.Label l = new AnnoScheme.Label(label, Colors.Black);


                        if (scheme.Type == AnnoScheme.TYPE.DISCRETE && scheme.Labels.Find(x => x.Name == label) == null)
                        {
                            scheme.Labels.Add(l);
                        }


                        duration = end - start;
                        al.AddSorted(new AnnoListItem(start, duration, label, "", Colors.Black));


                        //The tier is used as metainformation as well. Might be changed if thats relevant in the future
                    }
                    i++;

                    al.Scheme      = scheme;
                    al.Meta.Role   = role;
                    al.Scheme.Name = tierid;

                    list.Add(al);
                }
            }
            catch (Exception ex)
            {
                MessageTools.Error(ex.ToString());
            }

            return(list);
        }
Example #3
0
        public static AnnoList LoadFromEventFile(String filepath)
        {
            AnnoList list = new AnnoList();

            try
            {
                XDocument doc    = XDocument.Load(filepath);
                var       events = doc.Descendants("event");

                //list.Scheme.Type = AnnoScheme.TYPE.DISCRETE;
                //list.Scheme.Labels = new List<AnnoScheme.Label>();

                foreach (var e in events)
                {
                    string label    = null;
                    string meta     = "";
                    double start    = -1;
                    double duration = -1;
                    Type   type     = Type.EMPTY;

                    var sender_attr = e.Attribute("sender");
                    var event_attr  = e.Attribute("event");
                    label = (event_attr == null ? "" : event_attr.Value);
                    //+ "@"
                    //+ (sender_attr == null ? "" : sender_attr.Value);
                    var from_attr = e.Attribute("from");
                    if (from_attr != null)
                    {
                        start = double.Parse(from_attr.Value) / 1000.0;
                    }
                    var dur_attr = e.Attribute("dur");
                    if (dur_attr != null)
                    {
                        duration = double.Parse(dur_attr.Value) / 1000.0;
                    }
                    var type_attr = e.Attribute("type");
                    if (type_attr != null)
                    {
                        switch (type_attr.Value)
                        {
                        case "MAP":
                            type = Type.MAP;
                            break;

                        case "TUPLE":
                            type = Type.TUPLE;
                            break;

                        case "STRING":
                            type = Type.STRING;
                            break;

                        //depricated, matched to tuple
                        case "FLOATS":
                            type = Type.TUPLE;
                            break;

                        //depricated, matched to map
                        case "NTUPLE":
                            type = Type.MAP;
                            break;
                        }
                    }

                    switch (type)
                    {
                    case Type.MAP:
                        var tuples = e.Descendants("tuple");
                        foreach (var tuple in tuples)
                        {
                            var string_attr = tuple.Attribute("string");
                            var value_attr  = tuple.Attribute("value");
                            meta = meta + ((string_attr == null ? "" : string_attr.Value)
                                           + "="
                                           + (value_attr == null ? "" : value_attr.Value)) + ";";
                        }
                        break;


                    case Type.STRING:
                        if (e.Value != "")
                        {
                            label = e.Value;
                        }

                        break;

                    case Type.TUPLE:
                        meta = e.Value == null ? "" : e.Value;
                        break;
                    }

                    AnnoScheme.Label asl = new AnnoScheme.Label(label, Colors.Black);
                    if (!list.Scheme.Labels.Any(item => item.Name == asl.Name))
                    {
                        list.Scheme.Labels.Add(asl);
                    }

                    var state_attr = e.Attribute("state");
                    if (state_attr.Value.ToString().ToUpper() == "COMPLETED")
                    {
                        Color color = Colors.Black;

                        //if (list.Scheme.Labels.Find(x => x.Name == label) != null)
                        //{
                        //    color = list.Scheme.Labels.Find(x => x.Name == label).Color;
                        //}

                        list.AddSorted(new AnnoListItem(start, duration, label, meta, color));
                    }
                }
            }
            catch (Exception ex)
            {
                MessageTools.Error(ex.ToString());
            }

            //AnnoScheme.Label garbage = new AnnoScheme.Label("GARBAGE", Colors.Black);
            //list.Scheme.Labels.Add(garbage);


            return(list);
        }
Example #4
0
        public static AnnoList LoadfromFile(string filepath)
        {
            AnnoList list = new AnnoList();

            list.Source.File.Path = filepath;
            list.Scheme           = new AnnoScheme();
            list.Scheme.Labels    = new List <AnnoScheme.Label>();

            //try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(filepath);

                XmlNode annotation = doc.SelectSingleNode("annotation");

                XmlNode info = annotation.SelectSingleNode("info");
                list.Source.File.Type = info.Attributes["ftype"].Value == AnnoSource.FileSource.TYPE.ASCII.ToString() ? AnnoSource.FileSource.TYPE.ASCII : AnnoSource.FileSource.TYPE.BINARY;
                int size = int.Parse(info.Attributes["size"].Value);

                XmlNode meta = annotation.SelectSingleNode("meta");
                if (meta != null)
                {
                    if (meta.Attributes["role"] != null)
                    {
                        list.Meta.Role = meta.Attributes["role"].Value;
                    }
                    if (meta.Attributes["annotator"] != null)
                    {
                        list.Meta.Annotator = meta.Attributes["annotator"].Value;
                    }
                }

                XmlNode scheme = annotation.SelectSingleNode("scheme");
                if (scheme.Attributes["name"] != null)
                {
                    list.Scheme.Name = scheme.Attributes["name"].Value;
                }
                string type = "FREE";
                if (scheme.Attributes["type"] != null)
                {
                    type = scheme.Attributes["type"].Value;
                }

                if (type == AnnoScheme.TYPE.DISCRETE.ToString())
                {
                    list.Scheme.Type = AnnoScheme.TYPE.DISCRETE;
                }
                else if (type == AnnoScheme.TYPE.CONTINUOUS.ToString())
                {
                    list.Scheme.Type = AnnoScheme.TYPE.CONTINUOUS;
                }
                else if (type == AnnoScheme.TYPE.FREE.ToString())
                {
                    list.Scheme.Type = AnnoScheme.TYPE.FREE;
                }

                else if (type == AnnoScheme.TYPE.POINT.ToString())
                {
                    list.Scheme.Type = AnnoScheme.TYPE.POINT;
                }
                else if (type == AnnoScheme.TYPE.POLYGON.ToString())
                {
                    list.Scheme.Type = AnnoScheme.TYPE.POLYGON;
                }
                else if (type == AnnoScheme.TYPE.GRAPH.ToString())
                {
                    list.Scheme.Type = AnnoScheme.TYPE.GRAPH;
                }
                else if (type == AnnoScheme.TYPE.SEGMENTATION.ToString())
                {
                    list.Scheme.Type = AnnoScheme.TYPE.SEGMENTATION;
                }

                if (scheme.Attributes["color"] != null)
                {
                    list.Scheme.MinOrBackColor = (Color)ColorConverter.ConvertFromString(scheme.Attributes["color"].Value);
                }
                else if (scheme.Attributes["mincolor"] != null)
                {
                    list.Scheme.MinOrBackColor = (Color)ColorConverter.ConvertFromString(scheme.Attributes["mincolor"].Value);
                }
                else if (list.Scheme.Type == AnnoScheme.TYPE.CONTINUOUS)
                {
                    list.Scheme.MinOrBackColor = Defaults.Colors.GradientMin;
                }

                if (scheme.Attributes["maxcolor"] != null)
                {
                    list.Scheme.MaxOrForeColor = (Color)ColorConverter.ConvertFromString(scheme.Attributes["maxcolor"].Value);
                }
                else if (list.Scheme.Type == AnnoScheme.TYPE.CONTINUOUS)
                {
                    list.Scheme.MaxOrForeColor = Defaults.Colors.GradientMax;
                }

                Dictionary <string, string> LabelIds = new Dictionary <string, string>();

                if (list.Scheme.Type == AnnoScheme.TYPE.DISCRETE)
                {
                    foreach (XmlNode item in scheme)
                    {
                        LabelIds.Add(item.Attributes["id"].Value, item.Attributes["name"].Value);

                        Color color = Defaults.Colors.Foreground;
                        if (item.Attributes["color"] != null)
                        {
                            color = (Color)ColorConverter.ConvertFromString(item.Attributes["color"].Value);
                        }
                        AnnoScheme.Label lcp = new AnnoScheme.Label(item.Attributes["name"].Value, color);
                        list.Scheme.Labels.Add(lcp);
                    }

                    AnnoScheme.Label garbage = new AnnoScheme.Label("GARBAGE", Defaults.Colors.Foreground);
                    list.Scheme.Labels.Add(garbage);
                }
                else if (list.Scheme.Type == AnnoScheme.TYPE.FREE)
                {
                }
                else if (list.Scheme.Type == AnnoScheme.TYPE.CONTINUOUS)
                {
                    list.Scheme.MinScore   = double.Parse(scheme.Attributes["min"].Value);
                    list.Scheme.MaxScore   = double.Parse(scheme.Attributes["max"].Value);
                    list.Scheme.SampleRate = double.Parse(scheme.Attributes["sr"].Value);
                }
                else if (list.Scheme.Type == AnnoScheme.TYPE.POINT ||
                         list.Scheme.Type == AnnoScheme.TYPE.POLYGON || list.Scheme.Type == AnnoScheme.TYPE.GRAPH ||
                         list.Scheme.Type == AnnoScheme.TYPE.SEGMENTATION)
                {
                    list.Scheme.SampleRate     = double.Parse(scheme.Attributes["sr"].Value);
                    list.Scheme.NumberOfPoints = int.Parse(scheme.Attributes["num"].Value);
                }

                if (File.Exists(filepath + "~"))

                {
                    if (list.Source.File.Type == AnnoSource.FileSource.TYPE.ASCII)
                    {
                        StreamReader sr    = new StreamReader(filepath + "~", System.Text.Encoding.UTF8);
                        string       line  = null;
                        double       start = 0.0;

                        while ((line = sr.ReadLine()) != null)
                        {
                            string[] data = line.Split(';');
                            if (list.Scheme.Type == AnnoScheme.TYPE.CONTINUOUS)
                            {
                                double value = double.NaN;
                                double.TryParse(data[0], out value);
                                double       confidence = Convert.ToDouble(data[1], CultureInfo.InvariantCulture);
                                AnnoListItem e          = new AnnoListItem(start, 1 / list.Scheme.SampleRate, value, "", Defaults.Colors.Foreground, confidence);
                                list.Add(e);
                                start = start + 1 / list.Scheme.SampleRate;
                            }
                            else if (list.Scheme.Type == AnnoScheme.TYPE.DISCRETE)
                            {
                                start = Convert.ToDouble(data[0], CultureInfo.InvariantCulture);
                                double stop  = Convert.ToDouble(data[1], CultureInfo.InvariantCulture);
                                double dur   = stop - start;
                                string label = "";
                                if (int.Parse(data[2]) < 0)
                                {
                                    label = "GARBAGE";
                                }
                                else
                                {
                                    LabelIds.TryGetValue(data[2], out label);
                                }
                                Color color = Colors.Black;

                                if (list.Scheme.Labels.Find(x => x.Name == label) != null)
                                {
                                    color = list.Scheme.Labels.Find(x => x.Name == label).Color;
                                }

                                double       confidence = Convert.ToDouble(data[3], CultureInfo.InvariantCulture);
                                AnnoListItem e          = new AnnoListItem(start, dur, label, "", color, confidence);
                                list.AddSorted(e);
                            }
                            else if (list.Scheme.Type == AnnoScheme.TYPE.FREE)
                            {
                                start = Convert.ToDouble(data[0], CultureInfo.InvariantCulture);
                                double stop       = Convert.ToDouble(data[1], CultureInfo.InvariantCulture);
                                double dur        = stop - start;
                                string label      = data[2];
                                double confidence = Convert.ToDouble(data[3], CultureInfo.InvariantCulture);
                                Color  color      = Colors.Black;
                                if (data.Length > 4)
                                {
                                    string[] metapairs = data[4].Split('=');
                                    for (int i = 0; i < metapairs.Length; i++)
                                    {
                                        if (metapairs[i].Contains("color"))
                                        {
                                            color = (Color)ColorConverter.ConvertFromString(metapairs[i + 1]);
                                            break;
                                        }
                                    }
                                }
                                AnnoListItem e = new AnnoListItem(start, dur, label, "", color, confidence);
                                list.AddSorted(e);
                            }

                            else if (list.Scheme.Type == AnnoScheme.TYPE.POINT)
                            {
                                string    frameLabel      = data[0];
                                double    frameConfidence = Convert.ToDouble(data[data.Count() - 1], CultureInfo.InvariantCulture);
                                PointList points          = new PointList();
                                for (int i = 1; i < data.Count() - 1; ++i)
                                {
                                    string pd = data[i].Replace("(", "");
                                    pd = pd.Replace(")", "");
                                    string[] pointData = pd.Split(':');
                                    points.Add(new PointListItem(double.Parse(pointData[1]), double.Parse(pointData[2]), pointData[0], double.Parse(pointData[3])));
                                }
                                AnnoListItem ali = new AnnoListItem(start, 1 / list.Scheme.SampleRate, frameLabel, "", list.Scheme.MinOrBackColor, frameConfidence, true, points);
                                list.Add(ali);
                                start = start + 1 / list.Scheme.SampleRate;
                            }
                        }
                        sr.Close();
                    }
                    else if (list.Source.File.Type == AnnoSource.FileSource.TYPE.BINARY)
                    {
                        BinaryReader binaryReader = new BinaryReader(File.Open(filepath + "~", FileMode.Open));
                        long         length       = (binaryReader.BaseStream.Length);

                        double start = 0.0;
                        while (binaryReader.BaseStream.Position < binaryReader.BaseStream.Length)
                        {
                            if (list.Scheme.Type == AnnoScheme.TYPE.CONTINUOUS)
                            {
                                double       value      = (double)binaryReader.ReadSingle();
                                double       confidence = (double)binaryReader.ReadSingle();
                                AnnoListItem e          = new AnnoListItem(start, 1 / list.Scheme.SampleRate, value, "", Defaults.Colors.Foreground, confidence);
                                list.Add(e);
                                start = start + 1 / list.Scheme.SampleRate;
                            }
                            else if (list.Scheme.Type == AnnoScheme.TYPE.DISCRETE)
                            {
                                start = binaryReader.ReadDouble();
                                double stop  = binaryReader.ReadDouble();
                                double dur   = stop - start;
                                string label = "";
                                int    index = binaryReader.ReadInt32();
                                if (index < 0)
                                {
                                    label = "GARBAGE";
                                }
                                else
                                {
                                    LabelIds.TryGetValue(index.ToString(), out label);
                                }
                                Color color = Colors.Black;

                                if (list.Scheme.Labels.Find(x => x.Name == label) != null)
                                {
                                    color = list.Scheme.Labels.Find(x => x.Name == label).Color;
                                }
                                double       confidence = Math.Round(binaryReader.ReadSingle(), 3, MidpointRounding.AwayFromZero);
                                AnnoListItem e          = new AnnoListItem(start, dur, label, "", color, confidence);
                                list.AddSorted(e);
                            }
                            else if (list.Scheme.Type == AnnoScheme.TYPE.FREE)
                            {
                                start = binaryReader.ReadDouble();
                                double       stop         = binaryReader.ReadDouble();
                                double       dur          = stop - start;
                                int          stringlength = (int)binaryReader.ReadUInt32();
                                byte[]       labelasbytes = (binaryReader.ReadBytes(stringlength));
                                string       label        = System.Text.Encoding.Default.GetString(labelasbytes);
                                Color        color        = Colors.Black;
                                double       confidence   = Math.Round(binaryReader.ReadSingle(), 3, MidpointRounding.AwayFromZero);
                                AnnoListItem e            = new AnnoListItem(start, dur, label, "", color, confidence);
                                list.AddSorted(e);
                            }
                        }

                        binaryReader.Close();
                    }
                }


                //Plugin logic should be called after parsing
                if (meta != null && meta.Attributes["trigger"] != null)
                {
                    string[] triggers = meta.Attributes["trigger"].Value.Split(';');
                    foreach (string trigger in triggers)
                    {
                        try
                        {
                            Match match = Regex.Match(trigger, @"([^{]+)\{([^}]*)\}");
                            if (match.Success && match.Groups.Count == 3)
                            {
                                string dllName   = match.Groups[1].Value;
                                string arguments = match.Groups[2].Value;
                                Dictionary <string, object> args = arguments.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                                                                   .Select(part => part.Split('='))
                                                                   .ToDictionary(split => split[0], split => (object)split[1]);
                                PluginCaller pluginCaller = new PluginCaller(dllName + ".dll", dllName);
                                AnnoTrigger  annoTrigger  = new AnnoTrigger(list, pluginCaller, args);
                                list.Meta.Trigger.Add(annoTrigger);
                            }
                            else
                            {
                                MessageTools.Warning("could not parse trigger '" + trigger + "'");
                            }
                        }
                        catch (Exception)
                        {
                            MessageTools.Warning("could not parse trigger '" + trigger + "'");
                        }
                    }
                }

                if (meta != null && meta.Attributes["pipeline"] != null)
                {
                    string[] pipelines = meta.Attributes["pipeline"].Value.Split(';');
                    foreach (string pipeline in pipelines)
                    {
                        try
                        {
                            Pipeline pipe = new Pipeline(list, pipeline);
                            list.Meta.Pipeline.Add(pipe);
                        }
                        catch (Exception)
                        {
                            MessageTools.Warning("could not parse pipeline '" + pipeline + "'");
                        }
                    }
                }
            }
            //catch(Exception e)
            //{
            //    MessageBox.Show("An exception occured while reading annotation from '" + filepath + "'");
            //}

            return(list);
        }
Example #5
0
        public static AnnoList LoadFromEventFile(String filepath)
        {
            AnnoList list = new AnnoList();

            try
            {
                XDocument doc    = XDocument.Load(filepath);
                var       events = doc.Descendants("event");

                foreach (var e in events)
                {
                    string label    = null;
                    string meta     = "";
                    double start    = -1;
                    double duration = -1;
                    Type   type     = Type.EMPTY;

                    var sender_attr = e.Attribute("sender");
                    var event_attr  = e.Attribute("event");
                    label = (event_attr == null ? "" : event_attr.Value);
                    //+ "@"
                    //+ (sender_attr == null ? "" : sender_attr.Value);
                    var from_attr = e.Attribute("from");
                    if (from_attr != null)
                    {
                        start = double.Parse(from_attr.Value) / 1000.0;
                    }
                    var dur_attr = e.Attribute("dur");
                    if (dur_attr != null)
                    {
                        duration = double.Parse(dur_attr.Value) / 1000.0;
                    }
                    var type_attr = e.Attribute("type");
                    if (type_attr != null)
                    {
                        switch (type_attr.Value)
                        {
                        case "MAP":
                            type = Type.MAP;
                            break;

                        case "TUPLE":
                            type = Type.TUPLE;
                            break;

                        case "STRING":
                            type = Type.STRING;
                            break;

                        //depricated, matched to tuple
                        case "FLOATS":
                            type = Type.TUPLE;
                            break;

                        //depricated, matched to map
                        case "NTUPLE":
                            type = Type.MAP;
                            break;
                        }
                    }

                    switch (type)
                    {
                    case Type.MAP:
                        var tuples = e.Descendants("tuple");
                        foreach (var tuple in tuples)
                        {
                            var string_attr = tuple.Attribute("string");
                            var value_attr  = tuple.Attribute("value");
                            meta = meta + ((string_attr == null ? "" : string_attr.Value)
                                           + "="
                                           + (value_attr == null ? "" : value_attr.Value)) + ";";
                        }
                        break;


                    case Type.STRING:
                        if (e.Value != "")
                        {
                            label = e.Value;
                        }

                        break;

                    case Type.TUPLE:
                        meta = e.Value == null ? "" : e.Value;
                        break;
                    }

                    var state_attr = e.Attribute("state");
                    if (state_attr.Value.ToString().ToUpper() == "COMPLETED")
                    {
                        list.AddSorted(new AnnoListItem(start, duration, label, meta));
                    }
                }
            }
            catch (Exception ex)
            {
                MessageTools.Error(ex.ToString());
            }

            return(list);
        }
Example #6
0
        public static AnnoList LoadfromFile(string filepath)
        {
            AnnoList list = new AnnoList();

            list.Source.File.Path = filepath;
            list.Scheme           = new AnnoScheme();
            list.Scheme.Labels    = new List <AnnoScheme.Label>();

            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(filepath);

                XmlNode annotation = doc.SelectSingleNode("annotation");

                XmlNode info = annotation.SelectSingleNode("info");
                list.Source.File.Type = info.Attributes["ftype"].Value == AnnoSource.FileSource.TYPE.ASCII.ToString() ? AnnoSource.FileSource.TYPE.ASCII : AnnoSource.FileSource.TYPE.BINARY;
                int size = int.Parse(info.Attributes["size"].Value);

                XmlNode meta = annotation.SelectSingleNode("meta");
                if (meta != null)
                {
                    if (meta.Attributes["role"] != null)
                    {
                        list.Meta.Role = meta.Attributes["role"].Value;
                    }
                    if (meta.Attributes["annotator"] != null)
                    {
                        list.Meta.Annotator = meta.Attributes["annotator"].Value;
                    }
                }

                XmlNode scheme = annotation.SelectSingleNode("scheme");
                if (scheme.Attributes["name"] != null)
                {
                    list.Scheme.Name = scheme.Attributes["name"].Value;
                }
                string type = "FREE";
                if (scheme.Attributes["type"] != null)
                {
                    type = scheme.Attributes["type"].Value;
                }

                if (type == AnnoScheme.TYPE.DISCRETE.ToString())
                {
                    list.Scheme.Type = AnnoScheme.TYPE.DISCRETE;
                }
                else if (type == AnnoScheme.TYPE.CONTINUOUS.ToString())
                {
                    list.Scheme.Type = AnnoScheme.TYPE.CONTINUOUS;
                }
                else
                {
                    list.Scheme.Type = AnnoScheme.TYPE.FREE;
                }

                if (scheme.Attributes["color"] != null)
                {
                    list.Scheme.MinOrBackColor = (Color)ColorConverter.ConvertFromString(scheme.Attributes["color"].Value);
                }
                else if (scheme.Attributes["mincolor"] != null)
                {
                    list.Scheme.MinOrBackColor = (Color)ColorConverter.ConvertFromString(scheme.Attributes["mincolor"].Value);
                }
                else if (list.Scheme.Type == AnnoScheme.TYPE.CONTINUOUS)
                {
                    list.Scheme.MinOrBackColor = Colors.Orange;
                }

                if (scheme.Attributes["maxcolor"] != null)
                {
                    list.Scheme.MaxOrForeColor = (Color)ColorConverter.ConvertFromString(scheme.Attributes["maxcolor"].Value);
                }
                else if (list.Scheme.Type == AnnoScheme.TYPE.CONTINUOUS)
                {
                    list.Scheme.MaxOrForeColor = Colors.LightBlue;
                }

                Dictionary <string, string> LabelIds = new Dictionary <string, string>();

                if (list.Scheme.Type == AnnoScheme.TYPE.DISCRETE)
                {
                    foreach (XmlNode item in scheme)
                    {
                        LabelIds.Add(item.Attributes["id"].Value, item.Attributes["name"].Value);

                        Color color = Colors.Black;
                        if (item.Attributes["color"] != null)
                        {
                            color = (Color)ColorConverter.ConvertFromString(item.Attributes["color"].Value);
                        }
                        AnnoScheme.Label lcp = new AnnoScheme.Label(item.Attributes["name"].Value, color);
                        list.Scheme.Labels.Add(lcp);
                    }

                    AnnoScheme.Label garbage = new AnnoScheme.Label("GARBAGE", Colors.Black);
                    list.Scheme.Labels.Add(garbage);
                }
                else if (list.Scheme.Type == AnnoScheme.TYPE.FREE)
                {
                }
                else if (list.Scheme.Type == AnnoScheme.TYPE.CONTINUOUS)
                {
                    list.Scheme.SampleRate = double.Parse(scheme.Attributes["sr"].Value);
                    list.Scheme.MinScore   = double.Parse(scheme.Attributes["min"].Value);
                    list.Scheme.MaxScore   = double.Parse(scheme.Attributes["max"].Value);
                }

                if (File.Exists(filepath + "~"))

                {
                    if (list.Source.File.Type == AnnoSource.FileSource.TYPE.ASCII)
                    {
                        StreamReader sr    = new StreamReader(filepath + "~", System.Text.Encoding.Default);
                        string       line  = null;
                        double       start = 0.0;

                        while ((line = sr.ReadLine()) != null)
                        {
                            string[] data = line.Split(';');
                            if (list.Scheme.Type == AnnoScheme.TYPE.CONTINUOUS)
                            {
                                string       value      = data[0];
                                double       confidence = Convert.ToDouble(data[1], CultureInfo.InvariantCulture);
                                AnnoListItem e          = new AnnoListItem(start, (1000.0 / list.Scheme.SampleRate) / 1000.0, value, "", Colors.Black, confidence);
                                list.Add(e);
                                start = start + (1000.0 / list.Scheme.SampleRate) / 1000.0;
                            }
                            else if (list.Scheme.Type == AnnoScheme.TYPE.DISCRETE)
                            {
                                start = Convert.ToDouble(data[0], CultureInfo.InvariantCulture);
                                double stop  = Convert.ToDouble(data[1], CultureInfo.InvariantCulture);
                                double dur   = stop - start;
                                string label = "";
                                if (int.Parse(data[2]) < 0)
                                {
                                    label = "GARBAGE";
                                }
                                else
                                {
                                    LabelIds.TryGetValue(data[2], out label);
                                }
                                Color color = Colors.Black;

                                if (list.Scheme.Labels.Find(x => x.Name == label) != null)
                                {
                                    color = list.Scheme.Labels.Find(x => x.Name == label).Color;
                                }

                                double       confidence = Convert.ToDouble(data[3], CultureInfo.InvariantCulture);
                                AnnoListItem e          = new AnnoListItem(start, dur, label, "", color, confidence);
                                list.AddSorted(e);
                            }
                            else if (list.Scheme.Type == AnnoScheme.TYPE.FREE)
                            {
                                start = Convert.ToDouble(data[0], CultureInfo.InvariantCulture);
                                double stop       = Convert.ToDouble(data[1], CultureInfo.InvariantCulture);
                                double dur        = stop - start;
                                string label      = data[2];
                                double confidence = Convert.ToDouble(data[3], CultureInfo.InvariantCulture);
                                Color  color      = Colors.Black;
                                if (data.Length > 4)
                                {
                                    string[] metapairs = data[4].Split('=');
                                    for (int i = 0; i < metapairs.Length; i++)
                                    {
                                        if (metapairs[i].Contains("color"))
                                        {
                                            color = (Color)ColorConverter.ConvertFromString(metapairs[i + 1]);
                                            break;
                                        }
                                    }
                                }
                                AnnoListItem e = new AnnoListItem(start, dur, label, "", color, confidence);
                                list.AddSorted(e);
                            }
                        }
                        sr.Close();
                    }
                    else if (list.Source.File.Type == AnnoSource.FileSource.TYPE.BINARY)
                    {
                        BinaryReader binaryReader = new BinaryReader(File.Open(filepath + "~", FileMode.Open));
                        long         length       = (binaryReader.BaseStream.Length);

                        double start = 0.0;
                        while (binaryReader.BaseStream.Position < binaryReader.BaseStream.Length)
                        {
                            if (list.Scheme.Type == AnnoScheme.TYPE.CONTINUOUS)
                            {
                                string       value      = binaryReader.ReadSingle().ToString();
                                double       confidence = (double)binaryReader.ReadSingle();
                                AnnoListItem e          = new AnnoListItem(start, (1000.0 / list.Scheme.SampleRate) / 1000.0, value, "", Colors.Black, confidence);
                                list.Add(e);
                                start = start + (1000.0 / list.Scheme.SampleRate) / 1000.0;
                            }
                            else if (list.Scheme.Type == AnnoScheme.TYPE.DISCRETE)
                            {
                                start = binaryReader.ReadDouble();
                                double stop  = binaryReader.ReadDouble();
                                double dur   = stop - start;
                                string label = "";
                                int    index = binaryReader.ReadInt32();
                                if (index < 0)
                                {
                                    label = "GARBAGE";
                                }
                                else
                                {
                                    LabelIds.TryGetValue(index.ToString(), out label);
                                }
                                Color color = Colors.Black;

                                if (list.Scheme.Labels.Find(x => x.Name == label) != null)
                                {
                                    color = list.Scheme.Labels.Find(x => x.Name == label).Color;
                                }
                                double       confidence = Math.Round(binaryReader.ReadSingle(), 3, MidpointRounding.AwayFromZero);
                                AnnoListItem e          = new AnnoListItem(start, dur, label, "", color, confidence);
                                list.AddSorted(e);
                            }
                            else if (list.Scheme.Type == AnnoScheme.TYPE.FREE)
                            {
                                start = binaryReader.ReadDouble();
                                double       stop         = binaryReader.ReadDouble();
                                double       dur          = stop - start;
                                int          stringlength = (int)binaryReader.ReadUInt32();
                                byte[]       labelasbytes = (binaryReader.ReadBytes(stringlength));
                                string       label        = System.Text.Encoding.Default.GetString(labelasbytes);
                                Color        color        = Colors.Black;
                                double       confidence   = Math.Round(binaryReader.ReadSingle(), 3, MidpointRounding.AwayFromZero);
                                AnnoListItem e            = new AnnoListItem(start, dur, label, "", color, confidence);
                                list.AddSorted(e);
                            }
                        }

                        binaryReader.Close();
                    }
                }
                else
                {
                    MessageBox.Show("Annotation data was not found, load scheme only from '" + filepath + "'");
                }
                list.HasChanged = false;
            }
            catch (Exception e)
            {
                MessageBox.Show("An exception occured while reading annotation from '" + filepath + "'");
            }

            return(list);
        }
Example #7
0
        public static List <AnnoList> LoadFromDatabase(System.Collections.IList collections, string databaseName, string sessionName, string userName)
        {
            MongoClient    mongo    = new MongoClient(ServerConnectionString);
            IMongoDatabase database = mongo.GetDatabase(databaseName);

            var collection  = database.GetCollection <BsonDocument>(DatabaseDefinitionCollections.Annotations);
            var roles       = database.GetCollection <BsonDocument>(DatabaseDefinitionCollections.Roles);
            var annoSchemes = database.GetCollection <BsonDocument>(DatabaseDefinitionCollections.Schemes);

            List <AnnoList> annoLists = new List <AnnoList>();

            foreach (DatabaseAnno anno in collections)
            {
                BsonElement value;
                AnnoList    annoList = new AnnoList();

                ObjectId roleid = GetObjectID(database, DatabaseDefinitionCollections.Roles, "name", anno.Role);
                string   roledb = FetchDBRef(database, DatabaseDefinitionCollections.Roles, "name", roleid);

                ObjectId annoSchemeId = GetObjectID(database, DatabaseDefinitionCollections.Schemes, "name", anno.AnnoScheme);
                string   annotdb      = FetchDBRef(database, DatabaseDefinitionCollections.Schemes, "name", annoSchemeId);

                ObjectId annotatorId = GetObjectID(database, DatabaseDefinitionCollections.Annotators, "fullname", anno.AnnotatorFullname);
                string   annotatdb   = FetchDBRef(database, DatabaseDefinitionCollections.Annotators, "name", annotatorId);
                string   annotatdbfn = FetchDBRef(database, DatabaseDefinitionCollections.Annotators, "fullname", annotatorId);
                annoList.Meta.Annotator         = annotatdb;
                annoList.Meta.AnnotatorFullName = annotatdbfn;

                ObjectId sessionid = GetObjectID(database, DatabaseDefinitionCollections.Sessions, "name", sessionName);
                string   sessiondb = FetchDBRef(database, DatabaseDefinitionCollections.Sessions, "name", sessionid);

                var builder = Builders <BsonDocument> .Filter;

                var filterscheme = builder.Eq("_id", annoSchemeId);
                var result       = collection.Find(filterscheme);
                var annosch      = annoSchemes.Find(filterscheme).Single();

                var filter    = builder.Eq("role_id", roleid) & builder.Eq("scheme_id", annoSchemeId) & builder.Eq("annotator_id", annotatorId) & builder.Eq("session_id", sessionid);
                var documents = collection.Find(filter).ToList();

                annoList.Scheme = new AnnoScheme();
                if (annosch.TryGetElement("type", out value) && annosch["type"].ToString() == AnnoScheme.TYPE.DISCRETE.ToString())
                {
                    annoList.Scheme.Type = AnnoScheme.TYPE.DISCRETE;
                }
                else if (annosch.TryGetElement("type", out value) && annosch["type"].ToString() == AnnoScheme.TYPE.FREE.ToString())
                {
                    annoList.Scheme.Type = AnnoScheme.TYPE.FREE;
                }
                else if (annosch.TryGetElement("type", out value) && annosch["type"].ToString() == AnnoScheme.TYPE.CONTINUOUS.ToString())
                {
                    annoList.Scheme.Type = AnnoScheme.TYPE.CONTINUOUS;
                }

                annoList.Meta.Role   = roledb;
                annoList.Scheme.Name = annosch["name"].ToString();
                var labels = documents[0]["labels"].AsBsonArray;
                if (annoList.Scheme.Type == AnnoScheme.TYPE.CONTINUOUS)
                {
                    if (annosch.TryGetElement("min", out value))
                    {
                        annoList.Scheme.MinScore = double.Parse(annosch["min"].ToString());
                    }
                    if (annosch.TryGetElement("max", out value))
                    {
                        annoList.Scheme.MaxScore = double.Parse(annosch["max"].ToString());
                    }
                    if (annosch.TryGetElement("sr", out value))
                    {
                        annoList.Scheme.SampleRate = double.Parse(annosch["sr"].ToString());
                    }

                    if (annosch.TryGetElement("min_color", out value))
                    {
                        annoList.Scheme.MinOrBackColor = (Color)ColorConverter.ConvertFromString(annosch["min_color"].ToString());
                    }
                    if (annosch.TryGetElement("max_color", out value))
                    {
                        annoList.Scheme.MaxOrForeColor = (Color)ColorConverter.ConvertFromString(annosch["max_color"].ToString());
                    }

                    annoList.Scheme.MinScore   = annoList.Scheme.MinScore;
                    annoList.Scheme.MaxScore   = annoList.Scheme.MaxScore;
                    annoList.Scheme.SampleRate = annoList.Scheme.SampleRate;

                    for (int i = 0; i < labels.Count; i++)
                    {
                        string label      = labels[i]["score"].ToString();
                        string confidence = labels[i]["conf"].ToString();
                        double start      = i * ((1000.0 / annoList.Scheme.SampleRate) / 1000.0);
                        double dur        = (1000.0 / annoList.Scheme.SampleRate) / 1000.0;

                        AnnoListItem ali = new AnnoListItem(start, dur, label, "", Colors.Black, double.Parse(confidence));

                        annoList.Add(ali);
                    }
                }
                else if (annoList.Scheme.Type == AnnoScheme.TYPE.DISCRETE)
                {
                    annoList.Scheme.MinOrBackColor = (Color)ColorConverter.ConvertFromString(annosch["color"].ToString());

                    annoList.Scheme.Labels = new List <AnnoScheme.Label>();

                    BsonArray schemelabels = annosch["labels"].AsBsonArray;

                    for (int j = 0; j < schemelabels.Count; j++)
                    {
                        //in case flag is set, if not ignore isValid
                        try
                        {
                            if (schemelabels[j]["isValid"].AsBoolean == true)
                            {
                                annoList.Scheme.Labels.Add(new AnnoScheme.Label(schemelabels[j]["name"].ToString(), (Color)ColorConverter.ConvertFromString(schemelabels[j]["color"].ToString())));
                            }
                        }
                        catch
                        {
                            annoList.Scheme.Labels.Add(new AnnoScheme.Label(schemelabels[j]["name"].ToString(), (Color)ColorConverter.ConvertFromString(schemelabels[j]["color"].ToString())));
                        }
                    }

                    annoList.Scheme.Labels.Add(new AnnoScheme.Label(GARBAGELABEL, GARBAGECOLOR));

                    for (int i = 0; i < labels.Count; i++)
                    {
                        string SchemeLabel = "";
                        Color  SchemeColor = Colors.Black;
                        bool   idfound     = false;
                        for (int j = 0; j < schemelabels.Count; j++)
                        {
                            if (labels[i]["id"].AsInt32 == schemelabels[j]["id"].AsInt32)
                            {
                                SchemeLabel = schemelabels[j]["name"].ToString();
                                SchemeColor = (Color)ColorConverter.ConvertFromString(schemelabels[j]["color"].ToString());
                                idfound     = true;
                                break;
                            }
                        }


                        if (labels[i]["id"].AsInt32 == -1 || idfound == false)
                        {
                            SchemeLabel = GARBAGELABEL;
                            SchemeColor = GARBAGECOLOR;
                        }

                        double start      = double.Parse(labels[i]["from"].ToString());
                        double stop       = double.Parse(labels[i]["to"].ToString());
                        double duration   = stop - start;
                        string label      = SchemeLabel;
                        string confidence = labels[i]["conf"].ToString();

                        AnnoListItem ali = new AnnoListItem(start, duration, label, "", SchemeColor, double.Parse(confidence));
                        annoList.AddSorted(ali);
                    }
                }
                else if (annoList.Scheme.Type == AnnoScheme.TYPE.FREE)
                {
                    annoList.Scheme.MinOrBackColor = (Color)ColorConverter.ConvertFromString(annosch["color"].ToString());

                    for (int i = 0; i < labels.Count; i++)
                    {
                        double start      = double.Parse(labels[i]["from"].ToString());
                        double stop       = double.Parse(labels[i]["to"].ToString());
                        double duration   = stop - start;
                        string label      = labels[i]["name"].ToString();
                        string confidence = labels[i]["conf"].ToString();

                        AnnoListItem ali = new AnnoListItem(start, duration, label, "", Colors.Black, double.Parse(confidence));
                        annoList.AddSorted(ali);
                    }
                }

                annoList.Source.Database.OID = anno.Id;

                annoLists.Add(annoList);
                annoList = null;
            }

            return(annoLists);
        }