public AnnoList GetAnnoList() { List <string> usedLabels = new List <string>(); AnnoList list = new AnnoList(); list.Scheme.Type = AnnoScheme.TYPE.DISCRETE; list.Scheme.MinOrBackColor = backroundColorPicket.SelectedColor.Value; list.Scheme.Name = schemeNameTextField.Text == "" ? "Noname" : schemeNameTextField.Text; foreach (AnnotationSchemeSegment a in LabelsListBox.Items) { if (a.Label != "" && !usedLabels.Contains(a.Label)) { AnnoScheme.Label lcp = new AnnoScheme.Label(a.Label, a.Color); list.Scheme.Labels.Add(lcp); usedLabels.Add(a.Label); } } AnnoScheme.Label garbage = new AnnoScheme.Label("GARBAGE", Colors.Black); list.Scheme.Labels.Add(garbage); return(list); }
public static AnnoList ConvertFreetoDiscreteAnnotation(AnnoList list) { if (list.Scheme.Type != AnnoScheme.TYPE.FREE) { return(list); } AnnoList al = new AnnoList(); AnnoScheme scheme = new AnnoScheme(); scheme.Type = AnnoScheme.TYPE.DISCRETE; foreach (AnnoListItem ali in list) { al.Add(ali); if (scheme.Labels.Find(x => x.Name == ali.Label) == null) { AnnoScheme.Label l = new AnnoScheme.Label(ali.Label, Colors.Black); scheme.Labels.Add(l); } } scheme.Name = list.Scheme.Name; al.Scheme = scheme; al.Meta = list.Meta; return(al); }
public void loadFiles() { string[] filenames = FileTools.OpenFileDialog("Viewable files (*.stream,*.annotation;*.wav,*.avi,*.wmv)|*.stream;*.annotation;*.wav;*.avi;*.wmv;*mp4;*mpg;*mkv;*vui|Signal files (*.stream)|*.stream|Annotation files (*.annotation)|*annotation;*.anno|Wave files (*.wav)|*.wav|Video files(*.avi,*.wmv,*.mp4;*.mov)|*.avi;*.wmv;*.mp4;*.mov|All files (*.*)|*.*", true); if (filenames != null) { control.Cursor = Cursors.Wait; loadMultipleFiles(filenames); control.Cursor = Cursors.Arrow; } AnnoList list; list = new AnnoList(); list.Scheme = new AnnoScheme() { Type = AnnoScheme.TYPE.DISCRETE }; list.Scheme.Labels = new List <AnnoScheme.Label>(); list.Scheme.Type = AnnoScheme.TYPE.DISCRETE; list.Scheme.Labels.Clear(); AnnoScheme.Label activeStage = new AnnoScheme.Label("ACTIVE", Colors.Green); list.Scheme.Labels.Add(activeStage); AnnoScheme.Label unknownStage = new AnnoScheme.Label("UNKNOWN", Colors.Red); list.Scheme.Labels.Add(unknownStage); list.Clear(); list.Scheme.MinOrBackColor = Colors.Beige; list.Scheme.Name = "Activation"; addAnnoTier(list); }
private void Ok_Click(object sender, RoutedEventArgs e) { List <string> usedLabels = new List <string>(); scheme.MinOrBackColor = backroundColorPicket.SelectedColor.Value; scheme.Name = schemeNameTextField.Text == "" ? Defaults.Strings.Unkown : schemeNameTextField.Text; scheme.Labels.Clear(); foreach (Label a in LabelsListBox.Items) { if (a.Name != "" && !usedLabels.Contains(a.Name)) { scheme.Labels.Add(new AnnoScheme.Label(a.Name, a.Color)); usedLabels.Add(a.Name); } } AnnoScheme.Label garbage = new AnnoScheme.Label("GARBAGE", Colors.Black); scheme.Labels.Add(garbage); DialogResult = true; Close(); }
private void AddAnnotation_Click(object sender, RoutedEventArgs e) { Dictionary <string, UserInputWindow.Input> input = new Dictionary <string, UserInputWindow.Input>(); input["label"] = new UserInputWindow.Input() { Label = "Class label", DefaultValue = "" }; input["color"] = new UserInputWindow.Input() { Label = "Color code (#RGB)", DefaultValue = "" }; UserInputWindow dialog = new UserInputWindow("Add a new class label", input); dialog.ShowDialog(); if (dialog.DialogResult == true) { items = new List <AnnoScheme.Label>(); string label = dialog.Result("label"); Color color = (Color)ColorConverter.ConvertFromString(dialog.Result("color")); AnnoScheme.Label lcp = new AnnoScheme.Label(label, color); labelcolors.Add(lcp); foreach (AnnoScheme.Label lp in labelcolors) { items.Add(new AnnoScheme.Label(label, color) { Name = lp.Name, Color = lp.Color }); } AnnotationResultBox.ItemsSource = items; } }
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); }
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); }
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); }
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); }
private void Add_Click(object sender, RoutedEventArgs e) { if (allowEdit) { string name = null; Brush col1 = null; Brush col2 = null; string sr = null; string min = null; string max = null; if (annoList != null) { name = annoList.Scheme.Name; if (annoList.Scheme.Type == AnnoScheme.TYPE.DISCRETE || annoList.Scheme.Type == AnnoScheme.TYPE.FREE) { usedlabels = new HashSet <AnnoScheme.Label>(); foreach (AnnoListItem item in annoList) { AnnoScheme.Label l = new AnnoScheme.Label(item.Label, item.Color); bool detected = false; foreach (AnnoScheme.Label p in usedlabels) { if (p.Name == l.Name) { detected = true; } } if (detected == false) { usedlabels.Add(l); } } col1 = new SolidColorBrush(annoList.Scheme.MinOrBackColor); } else { col1 = new SolidColorBrush(annoList.Scheme.MinOrBackColor); col2 = new SolidColorBrush(annoList.Scheme.MaxOrForeColor); sr = (1000.0 / (annoList.Scheme.SampleRate * 1000.0)).ToString(); min = annoList.Scheme.MinScore.ToString(); max = annoList.Scheme.MaxScore.ToString(); } } storeAnnotationSchemetoDatabase(name, usedlabels, annoList.Scheme.Type, col1, col2, sr, min, max); } else { Dictionary <string, UserInputWindow.Input> input = new Dictionary <string, UserInputWindow.Input>(); input["name"] = new UserInputWindow.Input() { Label = "Name", DefaultValue = "" }; UserInputWindow dialog = new UserInputWindow("Add new name", input); dialog.ShowDialog(); if (dialog.DialogResult == true) { DataBaseResultsBox.Items.Add(dialog.Result("name")); DataBaseResultsBox.SelectedItem = dialog.Result("name"); } } }
private void ExportAnnoContinuousToDiscrete() { if (AnnoTierStatic.Selected != null && !AnnoTierStatic.Selected.IsDiscreteOrFree) { Dictionary <string, UserInputWindow.Input> input = new Dictionary <string, UserInputWindow.Input>(); input["labels"] = new UserInputWindow.Input() { Label = "Class labels (separated by ;)", DefaultValue = Properties.Settings.Default.ConvertToDiscreteClasses }; input["thresholds"] = new UserInputWindow.Input() { Label = "Upper thresholds (separated by ;)", DefaultValue = Properties.Settings.Default.ConvertToDiscreteThreshs }; input["offset"] = new UserInputWindow.Input() { Label = "Optional offset (s)", DefaultValue = Properties.Settings.Default.ConvertToDiscreteDelays }; UserInputWindow dialog = new UserInputWindow("Convert to discrete annotation", input); dialog.ShowDialog(); List <string> classes = new List <string>(); List <double> upperThresholds = new List <double>(); double offset = 0.0; if (dialog.DialogResult == true) { Properties.Settings.Default.ConvertToDiscreteClasses = dialog.Result("labels"); Properties.Settings.Default.ConvertToDiscreteThreshs = dialog.Result("thresholds"); Properties.Settings.Default.ConvertToDiscreteDelays = dialog.Result("offset"); Properties.Settings.Default.Save(); string[] labels = dialog.Result("labels").Split(';'); for (int i = 0; i < labels.Length; i++) { classes.Add(labels[i]); } string[] thresholds = dialog.Result("thresholds").Split(';'); for (int i = 0; i < thresholds.Length; i++) { double thresh = -1; double.TryParse(thresholds[i], out thresh); if (thresh > -1) { upperThresholds.Add(thresh); } else { MessageTools.Warning("Could not parse input"); } } if (thresholds.Length == labels.Length - 1) { upperThresholds.Add(1.0); } else if (thresholds.Length == labels.Length + 1) { classes.Add("REST"); } else if (thresholds.Length != labels.Length) { MessageBox.Show("Number of labels does not match number of threshholds"); } double.TryParse(dialog.Result("offset"), out offset); } Mouse.SetCursor(Cursors.No); AnnoList discretevalues = new AnnoList(); discretevalues.Scheme = new AnnoScheme(); discretevalues.Scheme.Type = AnnoScheme.TYPE.DISCRETE; discretevalues.Meta.Role = AnnoTier.Selected.AnnoList.Meta.Role; discretevalues.Meta.Annotator = AnnoTier.Selected.AnnoList.Meta.Annotator; discretevalues.Scheme.Name = AnnoTier.Selected.AnnoList.Scheme.Name; foreach (string label in classes) { AnnoScheme.Label item = new AnnoScheme.Label(label, System.Windows.Media.Colors.Black); discretevalues.Scheme.Labels.Add(item); } AnnoScheme.Label garbage = new AnnoScheme.Label("GARBAGE", Colors.Black); discretevalues.Scheme.Labels.Add(garbage); double lowThres = -Double.MaxValue; double highThres = 1.0; foreach (AnnoListItem ali in AnnoTierStatic.Selected.AnnoList) { double val = ali.Score; for (int i = 0; i < classes.Count; i++) { highThres = upperThresholds[i]; if (i > 0) { lowThres = upperThresholds[i - 1]; } else { lowThres = -Double.MaxValue; } if (val > lowThres && val <= highThres) { if (!(discretevalues.Count > 0 && discretevalues[discretevalues.Count - 1].Label == classes[i])) { AnnoListItem newItem = new AnnoListItem(ali.Start + offset, ali.Duration, classes[i], "", discretevalues.Scheme.GetColorForLabel(classes[i])); if (newItem.Start < 0.0) { newItem.Duration = ali.Duration + offset + newItem.Start; newItem.Start = 0.0; newItem.Stop = newItem.Duration; } if (newItem.Duration > 0.0) { discretevalues.Add(newItem); } } else { discretevalues[discretevalues.Count - 1].Stop = discretevalues[discretevalues.Count - 1].Stop + ali.Duration; } break; } } } AnnoTier.Unselect(); addAnnoTierFromList(discretevalues); Mouse.SetCursor(System.Windows.Input.Cursors.Arrow); } else { MessageBox.Show("Tier is already discrete"); } }
public static AnnoScheme GetAnnotationScheme(string annoName, AnnoScheme.TYPE annoType) { MongoClient mongo = new MongoClient(ServerConnectionString); IMongoDatabase database = mongo.GetDatabase(Properties.Settings.Default.DatabaseName); AnnoScheme scheme = new AnnoScheme(); scheme.Type = annoType; var annoSchemes = database.GetCollection <BsonDocument>(DatabaseDefinitionCollections.Schemes); var builder = Builders <BsonDocument> .Filter; FilterDefinition <BsonDocument> annoSchemeFilter = builder.Eq("name", annoName) & builder.Eq("type", annoType.ToString()); BsonDocument annoSchemeDocument = null; try { annoSchemeDocument = annoSchemes.Find(annoSchemeFilter).Single(); if (annoSchemeDocument["type"].ToString() == annoType.ToString()) { scheme.Name = annoSchemeDocument["name"].ToString(); if (scheme.Type == AnnoScheme.TYPE.CONTINUOUS) { scheme.MinScore = annoSchemeDocument["min"].ToDouble(); scheme.MaxScore = annoSchemeDocument["max"].ToDouble(); scheme.SampleRate = annoSchemeDocument["sr"].ToDouble(); scheme.MinOrBackColor = (Color)ColorConverter.ConvertFromString(annoSchemeDocument["min_color"].ToString()); scheme.MaxOrForeColor = (Color)ColorConverter.ConvertFromString(annoSchemeDocument["max_color"].ToString()); } else if (scheme.Type == AnnoScheme.TYPE.DISCRETE) { scheme.MinOrBackColor = (Color)ColorConverter.ConvertFromString(annoSchemeDocument["color"].ToString()); BsonArray schemeLabelsArray = annoSchemeDocument["labels"].AsBsonArray; string SchemeLabel = ""; string SchemeColor = "#000000"; for (int j = 0; j < schemeLabelsArray.Count; j++) { try { if (schemeLabelsArray[j]["isValid"].AsBoolean == true) { SchemeLabel = schemeLabelsArray[j]["name"].ToString(); SchemeColor = schemeLabelsArray[j]["color"].ToString(); AnnoScheme.Label lcp = new AnnoScheme.Label(schemeLabelsArray[j]["name"].ToString(), (Color)ColorConverter.ConvertFromString(schemeLabelsArray[j]["color"].ToString())); scheme.Labels.Add(lcp); } } catch { SchemeLabel = schemeLabelsArray[j]["name"].ToString(); SchemeColor = schemeLabelsArray[j]["color"].ToString(); AnnoScheme.Label lcp = new AnnoScheme.Label(schemeLabelsArray[j]["name"].ToString(), (Color)ColorConverter.ConvertFromString(schemeLabelsArray[j]["color"].ToString())); scheme.Labels.Add(lcp); } } } else if (scheme.Type == AnnoScheme.TYPE.FREE) { scheme.MinOrBackColor = (Color)ColorConverter.ConvertFromString(annoSchemeDocument["color"].ToString()); } } } catch (Exception ex) { MessageTools.Warning(ex.ToString()); } return(scheme); }