示例#1
0
        private void databaseConnect()
        {
            Action EmptyDelegate = delegate() { };

            control.ShadowBoxText.Text   = "Connecting to Database...";
            control.ShadowBox.Visibility = Visibility.Visible;
            control.UpdateLayout();
            control.Dispatcher.Invoke(DispatcherPriority.Render, EmptyDelegate);

            bool isConnected = DatabaseHandler.Connect();

            if (isConnected)
            {
                if (!DatabaseHandler.ChangeDatabase(Properties.Settings.Default.DatabaseName))
                {
                    Properties.Settings.Default.DatabaseName = null;
                    Properties.Settings.Default.Save();
                }
            }
            else
            {
                MessageTools.Warning("Unable to connect to database, please check your settings");
                Properties.Settings.Default.DatabaseAutoLogin = false;
                Properties.Settings.Default.Save();
            }

            updateNavigator();

            control.ShadowBox.Visibility = Visibility.Collapsed;
            control.ShadowBoxText.Text   = "Loading Data...";
        }
        private void AnnoList_Drop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                string[] filenames = e.Data.GetData(DataFormats.FileDrop, true) as string[];
                if (filenames != null && filenames[0].EndsWith(".annotation"))
                {
                    try
                    {
                        AnnoList list = AnnoList.LoadfromFile(filenames[0]);

                        if (list.Scheme.Type == AnnoScheme.TYPE.DISCRETE)
                        {
                            foreach (var item in list.Scheme.Labels)
                            {
                                Label label = new Label()
                                {
                                    Name = item.Name, Color = item.Color
                                };
                                if (!items.Contains(label) && label.Name != "GARBAGE" && label.Name != "")
                                {
                                    items.Add(label);
                                }
                            }
                        }
                        else if (list.Scheme.Type == AnnoScheme.TYPE.FREE)
                        {
                            HashSet <string> labelNames = new HashSet <string>();
                            foreach (AnnoListItem item in list)
                            {
                                labelNames.Add(item.Label);
                            }
                            foreach (string name in labelNames)
                            {
                                Label label = new Label()
                                {
                                    Name = name, Color = list.Scheme.MaxOrForeColor
                                };
                                if (!items.Contains(label) && label.Name != "GARBAGE" && label.Name != "")
                                {
                                    items.Add(label);
                                }
                            }
                        }
                        else
                        {
                            return;
                        }

                        schemeNameTextField.Text           = list.Scheme.Name;
                        backroundColorPicket.SelectedColor = list.Scheme.MinOrBackColor;
                    }
                    catch
                    {
                        MessageTools.Warning("This is not a valid annotation file");
                    }
                }
            }
        }
示例#3
0
        private void ApplyButton_Click(object sender, RoutedEventArgs e)
        {
            if (DBHost.Text != "" && DBHost.Text != "")
            {
                if (Properties.Settings.Default.serverhistory == "")
                {
                    Properties.Settings.Default.serverhistory = DBHost.Text + ":" + DBPort.Text;
                }
                else if (!Properties.Settings.Default.serverhistory.Contains(DBHost.Text + ":" + DBPort.Text))
                {
                    Properties.Settings.Default.serverhistory = Properties.Settings.Default.serverhistory + ";" + DBHost.Text + ":" + DBPort.Text;
                }



                if (Properties.Settings.Default.DatabaseDirectory != DownloadDirectory.Text)
                {
                    if (Directory.Exists(DownloadDirectory.Text))
                    {
                        Directory.CreateDirectory(DownloadDirectory.Text);
                        Properties.Settings.Default.DatabaseDirectory = DownloadDirectory.Text;
                        Properties.Settings.Default.Save();
                    }
                    else
                    {
                        MessageTools.Warning("Directory does not exist '" + DownloadDirectory.Text + "'");
                        return;
                    }
                }


                if (Properties.Settings.Default.CMLDirectory != CMLDirectory.Text)
                {
                    if (Directory.Exists(CMLDirectory.Text))
                    {
                        Directory.CreateDirectory(CMLDirectory.Text);
                        Properties.Settings.Default.CMLDirectory = CMLDirectory.Text;
                        Properties.Settings.Default.Save();
                    }
                    else
                    {
                        MessageTools.Warning("Directory does not exist '" + CMLDirectory.Text + "'");
                        return;
                    }
                }

                DialogResult = true;
                Close();
            }

            else
            {
                MessageBox.Show("Host and IP can't be empty!");
            }
        }
示例#4
0
        private void ApplyButton_Click(object sender, RoutedEventArgs e)
        {
            if (Properties.Settings.Default.DatabaseDirectory != DownloadDirectory.Text)
            {
                if (Directory.Exists(DownloadDirectory.Text))
                {
                    Directory.CreateDirectory(DownloadDirectory.Text);
                    Properties.Settings.Default.DatabaseDirectory = DownloadDirectory.Text;
                    Properties.Settings.Default.Save();
                }
                else
                {
                    MessageTools.Warning("Directory does not exist '" + DownloadDirectory.Text + "'");
                    return;
                }
            }


            if (Properties.Settings.Default.CMLDirectory != CMLDirectory.Text)
            {
                if (Directory.Exists(CMLDirectory.Text))
                {
                    Directory.CreateDirectory(CMLDirectory.Text);
                    Properties.Settings.Default.CMLDirectory = CMLDirectory.Text;
                    Properties.Settings.Default.Save();
                }
                else
                {
                    MessageTools.Warning("Directory does not exist '" + CMLDirectory.Text + "'");
                    return;
                }
            }

            DialogResult = true;
            Close();
        }
示例#5
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);
        }
示例#6
0
        private void addNewAnnotationDatabase()
        {
            if (Time.TotalDuration > 0)
            {
                string annoScheme = DatabaseHandler.SelectScheme();
                if (annoScheme == null)
                {
                    return;
                }

                string role = DatabaseHandler.SelectRole();
                if (role == null)
                {
                    return;
                }

                AnnoScheme scheme = DatabaseHandler.GetAnnotationScheme(annoScheme);
                if (scheme == null)
                {
                    return;
                }
                scheme.Labels.Add(new AnnoScheme.Label("GARBAGE", Colors.Black));

                ObjectId annotatid = DatabaseHandler.GetObjectID(DatabaseDefinitionCollections.Annotators, "name", Properties.Settings.Default.MongoDBUser);
                string   annotator = Properties.Settings.Default.MongoDBUser;


                // DatabaseHandler.FetchDBRef(DatabaseDefinitionCollections.Annotators, "fullname", annotatid);

                AnnoList annoList;
                if (DatabaseHandler.AnnotationExists(annotator, DatabaseHandler.SessionName, role, scheme.Name))
                {
                    DatabaseAnnotation annotation = new DatabaseAnnotation()
                    {
                        Annotator = annotator,
                        Session   = DatabaseHandler.SessionName,
                        Role      = role,
                        Scheme    = scheme.Name
                    };
                    annoList            = DatabaseHandler.LoadAnnoList(annotation, false);
                    annoList.HasChanged = false;
                }
                else
                {
                    annoList                = new AnnoList();
                    annoList.Meta.Role      = role;
                    annoList.Meta.Annotator = annotator;
                    string annotatorFullName = DatabaseHandler.GetUserInfo(annotator).Fullname;
                    annoList.Meta.AnnotatorFullName = annotatorFullName;
                    annoList.Scheme = scheme;
                    annoList.Source.StoreToDatabase  = true;
                    annoList.Source.Database.Session = DatabaseHandler.SessionName;
                    annoList.HasChanged = true;
                }

                addAnnoTier(annoList);
                control.annoListControl.editComboBox.SelectedIndex = 0;
            }
            else
            {
                MessageTools.Warning("Nothing to annotate, load some data first.");
            }
        }
示例#7
0
        public void ReloadAnnoTierFromDatabase(AnnoTier tier, bool loadBackup)
        {
            if (tier == null || tier.AnnoList == null)
            {
                return;
            }

            if (loadBackup && tier.AnnoList.Source.Database.DataBackupOID == AnnoSource.DatabaseSource.ZERO)
            {
                MessageTools.Warning("No backup exists");
                return;
            }

            Action EmptyDelegate = delegate() { };

            control.ShadowBoxText.Text   = "Reloading Annotation";
            control.ShadowBox.Visibility = Visibility.Visible;
            control.UpdateLayout();
            control.Dispatcher.Invoke(DispatcherPriority.Render, EmptyDelegate);

            DatabaseAnnotation annotation = new DatabaseAnnotation();

            annotation.Role              = tier.AnnoList.Meta.Role;
            annotation.Scheme            = tier.AnnoList.Scheme.Name;
            annotation.AnnotatorFullName = tier.AnnoList.Meta.AnnotatorFullName;
            annotation.Annotator         = tier.AnnoList.Meta.Annotator;
            annotation.Session           = DatabaseHandler.SessionName;

            AnnoList annoList = DatabaseHandler.LoadAnnoList(annotation, loadBackup);
            double   maxdur   = 0;

            if (annoList != null && annoList.Count > 0 && annoList.Scheme.Type == AnnoScheme.TYPE.DISCRETE || annoList.Scheme.Type == AnnoScheme.TYPE.FREE)
            {
                maxdur = annoList[annoList.Count - 1].Stop;

                setAnnoList(annoList);
                tier.Children.Clear();
                tier.AnnoList.Clear();
                tier.segments.Clear();
                tier.AnnoList = annoList;

                foreach (AnnoListItem item in annoList)
                {
                    tier.AddSegment(item);
                }

                tier.TimeRangeChanged(Time);
                updateTimeRange(maxdur);

                tier.AnnoList.HasChanged = false;
            }

            else if (annoList != null && annoList.Count > 0 && annoList.Scheme.Type == AnnoScheme.TYPE.CONTINUOUS)
            {
                maxdur = annoList[annoList.Count - 1].Stop;

                setAnnoList(annoList);
                tier.AnnoList.Clear();
                tier.AnnoList = annoList;
                tier.TimeRangeChanged(Time);
                updateTimeRange(maxdur);
                AnnoTier.Selected.TimeRangeChanged(MainHandler.Time);

                tier.AnnoList.HasChanged = false;
            }

            control.ShadowBox.Visibility = Visibility.Collapsed;
        }
        private void Extract()
        {
            if (ChainsBox.SelectedItem == null)
            {
                MessageTools.Warning("select a chain first");
                return;
            }

            string featureName = FeatureNameTextBox.Text;

            if (featureName == "" || featureName.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0)
            {
                MessageTools.Warning("not a valid feature name");
                return;
            }

            Chain chain = (Chain)ChainsBox.SelectedItem;
            bool  force = ForceCheckBox.IsChecked.Value;

            if (!File.Exists(chain.Path))
            {
                MessageTools.Warning("file does not exist '" + chain.Path + "'");
                return;
            }

            string         database = DatabaseHandler.DatabaseName;
            var            sessions = SessionsBox.SelectedItems;
            var            roles    = RolesBox.SelectedItems;
            DatabaseStream stream   = (DatabaseStream)StreamsBox.SelectedItem;

            string leftContext  = LeftContextTextBox.Text;
            string frameStep    = FrameStepTextBox.Text;
            string rightContext = RightContextTextBox.Text;

            int nParallel = 1;

            int.TryParse(NParallelTextBox.Text, out nParallel);

            logTextBox.Text = "";

            // prepare lists

            int nFiles = 0;

            using (StreamWriter fileListIn = new StreamWriter(tempInListPath))
            {
                using (StreamWriter fileListOut = new StreamWriter(tempOutListPath))
                {
                    foreach (DatabaseSession session in sessions)
                    {
                        foreach (DatabaseRole role in roles)
                        {
                            string fromPath = Properties.Settings.Default.DatabaseDirectory + "\\"
                                              + database + "\\"
                                              + session.Name + "\\"
                                              + role.Name + "." + stream.Name + "." + stream.FileExt;

                            string toPath = Path.GetDirectoryName(fromPath) + "\\"
                                            + role.Name + "." + featureName + ".stream";

                            if (force || !File.Exists(toPath))
                            {
                                nFiles++;
                                fileListIn.WriteLine(fromPath);
                                fileListOut.WriteLine(toPath);
                            }
                            else
                            {
                                logTextBox.Text += "skip " + fromPath + "\n";
                            }
                        }
                    }
                }
            }

            // start feature extraction

            Chain          selectedChain  = (Chain)ChainsBox.SelectedItem;
            DatabaseStream selectedStream = (DatabaseStream)StreamsBox.SelectedItem;

            if (nFiles > 0)
            {
                string type = Defaults.CML.StreamTypeNameFeature;
                string name = featureName;
                string ext  = "stream";

                double sr = frameStepToSampleRate(frameStep, stream.SampleRate);

                DatabaseStream streamType = new DatabaseStream()
                {
                    Name = name, Type = type, FileExt = ext, SampleRate = sr
                };
                DatabaseHandler.AddStream(streamType);
                try
                {
                    logTextBox.Text += handler.CMLExtractFeature(chain.Path, nParallel, tempInListPath, tempOutListPath, frameStep, leftContext, rightContext);
                }
                catch (Exception e) { MessageBox.Show("There was an error in the feature extaction pipeline: " + e); }
            }

            File.Delete(tempInListPath);
            File.Delete(tempOutListPath);

            GetStreams(selectedStream);
            foreach (Chain item in ChainsBox.Items)
            {
                if (item.Name == selectedChain.Name)
                {
                    ChainsBox.SelectedItem = item;
                    break;
                }
            }
        }
示例#9
0
        private void Apply_Click(object sender, RoutedEventArgs e)
        {
            Trainer trainer = (Trainer)TrainerPathComboBox.SelectedItem;
            bool    force   = mode == Mode.COMPLETE || ForceCheckBox.IsChecked.Value;

            if (!File.Exists(trainer.Path))
            {
                MessageTools.Warning("file does not exist '" + trainer.Path + "'");
                return;
            }

            string database = DatabaseHandler.DatabaseName;

            DatabaseStream stream = (DatabaseStream)StreamsBox.SelectedItem;

            string sessionList = "";
            var    sessions    = SessionsBox.SelectedItems;

            foreach (DatabaseSession session in sessions)
            {
                if (sessionList == "")
                {
                    sessionList += session.Name;
                }
                else
                {
                    sessionList += ";" + session.Name;
                }
            }

            DatabaseScheme scheme = (DatabaseScheme)SchemesBox.SelectedItem;

            string rolesList = "";
            var    roles     = RolesBox.SelectedItems;

            foreach (DatabaseRole role in roles)
            {
                if (rolesList == "")
                {
                    rolesList += role.Name;
                }
                else
                {
                    rolesList += ";" + role.Name;
                }
            }

            DatabaseAnnotator annotator = (DatabaseAnnotator)AnnotatorsBox.SelectedItem;

            string trainerLeftContext  = LeftContextTextBox.Text;
            string trainerRightContext = RightContextTextBox.Text;
            string trainerBalance      = ((ComboBoxItem)BalanceComboBox.SelectedItem).Content.ToString();

            logTextBox.Text = "";

            if (mode == Mode.TRAIN ||
                mode == Mode.COMPLETE)
            {
                string   streamName  = "";
                string[] streamParts = stream.Name.Split('.');
                if (streamParts.Length <= 1)
                {
                    streamName = stream.Name;
                }
                else
                {
                    streamName = streamParts[1];
                    for (int i = 2; i < streamParts.Length; i++)
                    {
                        streamName += "." + streamParts[i];
                    }
                }


                string trainerDir = Properties.Settings.Default.CMLDirectory + "\\" +
                                    Defaults.CML.ModelsFolderName + "\\" +
                                    Defaults.CML.ModelsTrainerFolderName + "\\" +
                                    scheme.Type.ToString().ToLower() + "\\" +
                                    scheme.Name + "\\" +
                                    stream.Type + "{" +
                                    streamName + "}\\" +
                                    trainer.Name + "\\";

                Directory.CreateDirectory(trainerDir);

                string trainerName    = TrainerNameTextBox.Text == "" ? trainer.Name : TrainerNameTextBox.Text;
                string trainerOutPath = mode == Mode.COMPLETE ? tempTrainerPath : trainerDir + trainerName;

                if (force || !File.Exists(trainerOutPath + ".trainer"))
                {
                    try
                    {
                        logTextBox.Text += handler.CMLTrainModel(trainer.Path,
                                                                 trainerOutPath,
                                                                 Properties.Settings.Default.DatabaseDirectory,
                                                                 Properties.Settings.Default.DatabaseAddress,
                                                                 Properties.Settings.Default.MongoDBUser,
                                                                 MainHandler.Decode(Properties.Settings.Default.MongoDBPass),
                                                                 database,
                                                                 sessionList,
                                                                 scheme.Name,
                                                                 rolesList,
                                                                 annotator.Name,
                                                                 stream.Name,
                                                                 trainerLeftContext,
                                                                 trainerRightContext,
                                                                 trainerBalance,
                                                                 mode == Mode.COMPLETE);
                    }

                    catch (Exception ex)
                    {
                        logTextBox.Text += ex;
                    }
                }
                else
                {
                    logTextBox.Text += "skip " + trainerOutPath + "\n";
                }
            }

            if (mode == Mode.PREDICT ||
                mode == Mode.COMPLETE)
            {
                if (true || force)
                {
                    double confidence = -1.0;
                    if (ConfidenceCheckBox.IsChecked == true && ConfidenceTextBox.IsEnabled)
                    {
                        double.TryParse(ConfidenceTextBox.Text, out confidence);
                        Properties.Settings.Default.CMLDefaultConf = confidence;
                    }
                    double minGap = 0.0;
                    if (FillGapCheckBox.IsChecked == true && FillGapTextBox.IsEnabled)
                    {
                        double.TryParse(FillGapTextBox.Text, out minGap);
                        Properties.Settings.Default.CMLDefaultGap = minGap;
                    }
                    double minDur = 0.0;
                    if (RemoveLabelCheckBox.IsChecked == true && RemoveLabelTextBox.IsEnabled)
                    {
                        double.TryParse(RemoveLabelTextBox.Text, out minDur);
                        Properties.Settings.Default.CMLDefaultMinDur = minDur;
                    }
                    Properties.Settings.Default.Save();

                    try
                    {
                        logTextBox.Text += handler.CMLPredictAnnos(mode == Mode.COMPLETE ? tempTrainerPath : trainer.Path,
                                                                   Properties.Settings.Default.DatabaseDirectory,
                                                                   Properties.Settings.Default.DatabaseAddress,
                                                                   Properties.Settings.Default.MongoDBUser,
                                                                   MainHandler.Decode(Properties.Settings.Default.MongoDBPass),
                                                                   database,
                                                                   sessionList,
                                                                   scheme.Name,
                                                                   rolesList,
                                                                   annotator.Name,
                                                                   stream.Name,
                                                                   trainerLeftContext,
                                                                   trainerRightContext,
                                                                   confidence,
                                                                   minGap,
                                                                   minDur,
                                                                   mode == Mode.COMPLETE);
                    }

                    catch (Exception ex)
                    {
                        logTextBox.Text += ex;
                    }
                }
            }

            if (mode == Mode.EVALUATE)
            {
                string evalOutPath = Properties.Settings.Default.CMLDirectory + "\\" + Path.GetFileNameWithoutExtension(Path.GetRandomFileName());
                try

                {
                    logTextBox.Text += handler.CMLEvaluateModel(evalOutPath,
                                                                trainer.Path,
                                                                Properties.Settings.Default.DatabaseDirectory,
                                                                Properties.Settings.Default.DatabaseAddress,
                                                                Properties.Settings.Default.MongoDBUser,
                                                                MainHandler.Decode(Properties.Settings.Default.MongoDBPass),
                                                                database,
                                                                sessionList,
                                                                scheme.Name,
                                                                rolesList,
                                                                annotator.Name,
                                                                stream.Name,
                                                                LosoCheckBox.IsChecked.Value);

                    if (File.Exists(evalOutPath))
                    {
                        ConfmatWindow confmat = new ConfmatWindow(evalOutPath);
                        confmat.ShowDialog();
                        File.Delete(evalOutPath);
                    }
                }

                catch (Exception ex)
                {
                    logTextBox.Text += ex;
                }
            }

            if (mode == Mode.COMPLETE)
            {
                handler.ReloadAnnoTierFromDatabase(AnnoTierStatic.Selected, false);

                var dir = new DirectoryInfo(Path.GetDirectoryName(tempTrainerPath));
                foreach (var file in dir.EnumerateFiles(Path.GetFileName(tempTrainerPath) + "*"))
                {
                    file.Delete();
                }

                Close();
            }
        }
        private void Merge()
        {
            string featureName = FeatureNameTextBox.Text;

            if (featureName == "" || featureName.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0)
            {
                MessageTools.Warning("not a valid feature name");
                return;
            }

            string database = (string)DatabasesBox.SelectedItem;

            var  sessions = SessionsBox.SelectedItems;
            var  roles    = RolesBox.SelectedItems;
            var  streams  = StreamsBox.SelectedItems;
            bool force    = ForceCheckBox.IsChecked.Value;

            string sessionList = "";

            foreach (DatabaseSession session in sessions)
            {
                if (sessionList == "")
                {
                    sessionList = session.Name;
                }
                else
                {
                    sessionList += ";" + session.Name;
                }
            }

            string roleList = "";

            foreach (var role in roles)
            {
                if (roleList == "")
                {
                    roleList = ((DatabaseRole)role).Name;
                }
                else
                {
                    roleList += ";" + ((DatabaseRole)role).Name;
                }
            }

            string[] streamsNames = new string[streams.Count];
            int      i            = 0;

            foreach (DatabaseStream stream in streams)
            {
                streamsNames[i++] = stream.Name;
            }
            Array.Sort(streamsNames);

            string streamList = "";
            double sampleRate = ((DatabaseStream)streams[0]).SampleRate;

            foreach (string stream in streamsNames)
            {
                if (streamList == "")
                {
                    streamList = stream;
                }
                else
                {
                    streamList += ";" + stream;
                }
            }

            string rootDir = Properties.Settings.Default.DatabaseDirectory + "\\" + database;

            logTextBox.Text = handler.CMLMergeFeature(rootDir, sessionList, roleList, streamList, featureName, force);

            string type = Defaults.CML.StreamTypeNameFeature;
            string ext  = "stream";

            DatabaseStream streamType = new DatabaseStream()
            {
                Name = featureName, Type = type, FileExt = ext, SampleRate = sampleRate
            };

            DatabaseHandler.AddStream(streamType);

            GetStreams(streamType);
        }
示例#11
0
        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");
            }
        }
示例#12
0
        public void loadProjectFile(string filepath)
        {
            clearWorkspace();

            string workdir = Path.GetDirectoryName(filepath);

            XmlDocument doc = new XmlDocument();

            try
            {
                doc.Load(filepath);

                foreach (XmlNode node in doc.SelectNodes("//media"))
                {
                    string path = FileTools.GetAbsolutePath(node.InnerText, workdir);
                    loadFile(path);
                }

                foreach (XmlNode node in doc.SelectNodes("//signal"))
                {
                    Color background = Defaults.Colors.Background;
                    Color foreground = Defaults.Colors.Foreground;
                    if (node.Attributes["bg"] != null)
                    {
                        background = (Color)ColorConverter.ConvertFromString(node.Attributes["bg"].LastChild.Value);
                    }
                    if (node.Attributes["fg"] != null)
                    {
                        foreground = (Color)ColorConverter.ConvertFromString(node.Attributes["fg"].LastChild.Value);
                    }
                    string path = FileTools.GetAbsolutePath(node.InnerText, workdir);
                    loadFile(path, foreground, background);
                }

                if (DatabaseHandler.IsConnected)
                {
                    XmlNode node = doc.SelectSingleNode("//tiers");
                    if (node != null && node.Attributes["database"] != null)
                    {
                        DatabaseHandler.ChangeDatabase(node.Attributes["database"].LastChild.Value);
                    }
                }

                foreach (XmlNode node in (doc.SelectNodes("//tier")))
                {
                    string path = node.InnerText;
                    if (!Path.HasExtension(path))
                    {
                        AnnoList annoList = DatabaseHandler.LoadAnnoList(path);
                        if (annoList != null)
                        {
                            addAnnoTier(annoList);
                        }
                        else
                        {
                            MessageTools.Warning("Could not load annotation from database with id '" + node.InnerText + "'");
                        }
                    }
                    else
                    {
                        if (path == "")
                        {
                            path = node.Attributes["filepath"].LastChild.Value;
                        }
                        path = FileTools.GetAbsolutePath(path, workdir);
                        loadFile(path);
                    }
                }
            }
            catch (Exception e)
            {
                MessageTools.Error(e.ToString());
            }
        }
示例#13
0
        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);
        }
示例#14
0
        private void navigatorNewAnno_Click(object sender, RoutedEventArgs e)
        {
            if (Time.TotalDuration > 0)
            {
                AnnoTierNewSchemeWindow dialog = new AnnoTierNewSchemeWindow();
                dialog.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                dialog.ShowDialog();

                if (dialog.DialogResult == true)
                {
                    AnnoScheme.TYPE annoType = dialog.Result();

                    if (DatabaseLoaded)
                    {
                        databaseAddNewAnnotation(annoType);
                    }
                    else
                    {
                        if (annoType == AnnoScheme.TYPE.FREE)
                        {
                            AnnoTierNewFreeSchemeWindow dialog2 = new AnnoTierNewFreeSchemeWindow();
                            dialog2.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                            dialog2.ShowDialog();

                            if (dialog2.DialogResult == true)
                            {
                                AnnoScheme annoScheme = dialog2.Result;
                                AnnoList   annoList   = new AnnoList()
                                {
                                    Scheme = annoScheme
                                };
                                addAnnoTier(annoList);
                            }
                        }
                        else if (annoType == AnnoScheme.TYPE.DISCRETE)
                        {
                            AnnoTierNewDiscreteSchemeWindow dialog2 = new AnnoTierNewDiscreteSchemeWindow();
                            dialog2.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                            dialog2.ShowDialog();

                            if (dialog2.DialogResult == true)
                            {
                                AnnoList annoList = dialog2.GetAnnoList();
                                addAnnoTier(annoList);
                            }
                        }
                        else if (annoType == AnnoScheme.TYPE.CONTINUOUS)
                        {
                            double defaultSr = 25.0;

                            foreach (IMedia m in mediaList.Medias)
                            {
                                if (m.IsVideo())
                                {
                                    defaultSr = m.GetSampleRate();
                                    break;
                                }
                            }

                            AnnoTierNewContinuousSchemeWindow.Input input = new AnnoTierNewContinuousSchemeWindow.Input()
                            {
                                SampleRate = defaultSr, MinScore = 0.0, MaxScore = 1.0, MinColor = Colors.LightBlue, MaxColor = Colors.Red
                            };
                            AnnoTierNewContinuousSchemeWindow dialog2 = new AnnoTierNewContinuousSchemeWindow(input);
                            dialog2.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                            dialog2.ShowDialog();
                            if (dialog2.DialogResult == true)
                            {
                                AnnoScheme annoScheme = dialog2.Result;
                                AnnoList   annoList   = new AnnoList()
                                {
                                    Scheme = annoScheme
                                };
                                addAnnoTier(annoList);
                            }
                        }
                    }
                }
            }
            else
            {
                MessageTools.Warning("Nothing to annotate, load some data first.");
            }
        }