public SequenceData Operate(IList <ProcParam <SequenceProcEnv> > args, SequenceProcEnv env)
        {
            var labelSequence = env.SelectedSequence.GetLabelSequence();
            var labels        = (args[0] as LabelSelectParameter).Value;
            var beforeStart   = (args[1] as NumberParameter).Value;
            var afterStart    = (args[2] as NumberParameter).Value;
            var beforeEnd     = (args[3] as NumberParameter).Value;
            var afterEnd      = (args[4] as NumberParameter).Value;

            ICSLabelSequence tmp = new ICSLabelSequence();

            foreach (var label in labelSequence.EnumerateLabels())
            {
                if (labels.Contains(label.LabelText))
                {
                    decimal preStart  = label.BeginTime - beforeStart;
                    decimal postStart = label.BeginTime + afterStart;
                    if (postStart > preStart)
                    {
                        tmp.SetLabel(preStart, postStart, label.LabelText + "_begin");
                    }
                    decimal preEnd  = label.EndTime - beforeEnd;
                    decimal postEnd = label.EndTime + afterEnd;
                    if (postEnd > preEnd)
                    {
                        tmp.SetLabel(preEnd, postEnd, label.LabelText + "_end");
                    }
                }
            }
            return(SequenceData.FromLabelSequence(tmp, PathEx.GiveName("Boundary", env.SelectedSequence.Title), null));
        }
        public SequenceData Operate(IList <ProcParam <SequenceProcEnv> > args, SequenceProcEnv env)
        {
            // 実引数取得
            SequenceData anotherSequence = (args[0] as SequenceSingleSelectParameter).Value;
            bool         appendTitle     = ((BooleanParameter)args[1]).Value;
            // ラベル化
            ICSLabelSequence one     = env.SelectedSequence.GetLabelSequence();
            ICSLabelSequence another = anotherSequence.GetLabelSequence();

            if (appendTitle)
            {
                // タイトル付与
                foreach (var label in one.EnumerateLabels().ToList())
                {
                    one.SetLabel(label.BeginTime, label.EndTime, env.SelectedSequence.Title + "-" + label.LabelText);
                }
            }
            // 他方のラベルを追加
            foreach (var label in another.EnumerateLabels())
            {
                if (appendTitle)
                {
                    one.SetLabel(label.BeginTime, label.EndTime, anotherSequence.Title + "-" + label.LabelText, true);
                }
                else
                {
                    one.SetLabel(label, true);
                }
            }
            // SequenceDataとして返す
            return(SequenceData.FromLabelSequence(one, env.SelectedSequence.Title + "-merge-" + anotherSequence.Title));
        }
        public SequenceData Operate(IList <ProcParam <SequenceProcEnv> > args, SequenceProcEnv env)
        {
            var glue              = ((StringParameter)args[0]).Value;
            var type              = ((SingleSelectParameter)args[1]).Value;
            var skip              = ((BooleanParameter)args[2]).Value;
            var labelSequence     = env.SelectedSequence.GetLabelSequence();
            ICSLabelSequence ret  = new ICSLabelSequence();
            ICSLabel         prev = null;

            foreach (var label in labelSequence.EnumerateLabels())
            {
                if (skip && label.IsEmptyLabel)
                {
                    continue;
                }
                string name = label.LabelText;
                if (prev != null)
                {
                    name = prev.LabelText + glue + label.LabelText;
                }
                if (prev != null || type == 0)
                {
                    if (type == 0)
                    {
                        ret.SetLabel(label.BeginTime, label.EndTime, name);
                    }
                    else
                    {
                        ret.SetLabel(prev.BeginTime, prev.EndTime, name);
                    }
                }
                prev = label;
            }
            return(SequenceData.FromLabelSequence(ret, PathEx.GiveName("AppendPrev", env.SelectedSequence.Title)));
        }
        public SequenceData Operate(IList <ProcParam <SequenceProcEnv> > args, SequenceProcEnv env)
        {
            var cond    = args[0] as SingleSelectParameter;
            var operand = args[1] as SequenceMultiSelectParameter;
            List <SequenceData> targets = new List <SequenceData>();

            targets.Add(env.SelectedSequence);
            foreach (var sequence in operand.Value)
            {
                if (sequence != env.SelectedSequence)
                {
                    targets.Add(sequence);
                }
            }
            Func <decimal?[], int> condFunc = (x) => - 1;
            string opName = "";

            switch (cond.Value)
            {
            case 0:
                condFunc = (x) => {
                    if (!x.Any(v => v.HasValue))
                    {
                        return(-1);
                    }
                    return(x.Select((v, i) => new KeyValuePair <int, decimal?>(i, v)).Aggregate((a, b) => (!a.Value.HasValue ? b : !b.Value.HasValue ? a : a.Value.Value > b.Value.Value ? a : b)).Key);
                };
                opName = "Greatest";
                break;

            case 1:
                condFunc = (x) => {
                    if (!x.Any(v => v.HasValue))
                    {
                        return(-1);
                    }
                    return(x.Select((v, i) => new KeyValuePair <int, decimal?>(i, v)).Aggregate((a, b) => (!a.Value.HasValue ? b : !b.Value.HasValue ? a : a.Value.Value < b.Value.Value ? a : b)).Key);
                };
                opName = "Least";
                break;

            case 2:
                condFunc = (x) => {
                    for (int i = 0; i < x.Length; i++)
                    {
                        if (x[i].HasValue)
                        {
                            return(i);
                        }
                    }
                    return(-1);
                };
                opName = "NotEmpty";
                break;
            }
            var labels = ToLabelByCondition(targets, condFunc);

            return(SequenceData.FromLabelSequence(labels, PathEx.GiveName(opName, targets.Select(p => p.Title))));
        }
        public SequenceData Operate(IList <ProcParam <SequenceProcEnv> > args, SequenceProcEnv env)
        {
            var border               = ((LabelSelectParameter)args[0]).Value;
            var left                 = ((NumberParameter)args[1]).Value;
            var right                = ((NumberParameter)args[2]).Value;
            var percent              = ((BooleanParameter)args[3]).Value;
            var withEmpty            = ((BooleanParameter)args[4]).Value;
            ICSLabelSequence current = env.SelectedSequence.GetLabelSequence();
            ICSLabelSequence ret     = env.SelectedSequence.GetLabelSequence();
            var currentLabels        = current.EnumerateLabels().ToList();

            for (int i = 0; i < currentLabels.Count; i++)
            {
                if (border.Contains(currentLabels[i].LabelText))
                {
                    decimal begin, end;
                    if (percent)
                    {
                        begin = currentLabels[i].BeginTime - left * currentLabels[i].Duration / 100;
                        end   = currentLabels[i].EndTime + right * currentLabels[i].Duration / 100;
                    }
                    else
                    {
                        begin = currentLabels[i].BeginTime - left;
                        end   = currentLabels[i].EndTime + right;
                    }
                    if (begin > end)
                    {
                        begin += (end - begin) / 2;
                        end    = begin;
                    }
                    if (begin < end)
                    {
                        ret.SetLabel(begin, end, currentLabels[i].LabelText);
                    }
                    if (begin > currentLabels[i].BeginTime)
                    {
                        string prev = "";
                        if (i > 0 && !withEmpty)
                        {
                            prev = currentLabels[i - 1].LabelText;
                        }
                        ret.SetLabel(currentLabels[i].BeginTime, begin, prev);
                    }
                    if (end < currentLabels[i].EndTime)
                    {
                        string post = "";
                        if (i < currentLabels.Count - 1 && !withEmpty)
                        {
                            post = currentLabels[i + 1].LabelText;
                        }
                        ret.SetLabel(end, currentLabels[i].EndTime, post);
                    }
                }
            }
            return(SequenceData.FromLabelSequence(ret, PathEx.GiveName("extend", env.SelectedSequence.Title)));
        }
        public SequenceData Operate(IList <ProcParam <SequenceProcEnv> > args, SequenceProcEnv env)
        {
            SequenceColumnSelectParameter column = args[0] as SequenceColumnSelectParameter;
            int tmp = env.SelectedSequence.Borders.TargetColumnIndex;

            env.SelectedSequence.Borders.TargetColumnIndex = column.Value;
            ICSLabelSequence labelSeq = env.SelectedSequence.GetLabelSequence();

            if (tmp != column.Value)
            {
                env.SelectedSequence.Borders.TargetColumnIndex = tmp;
            }
            return(SequenceData.FromLabelSequence(labelSeq, PathEx.GiveName("LabelOf", env.SelectedSequence.Title)));
        }
        public SequenceData Operate(IList <ProcParam <SequenceProcEnv> > args, SequenceProcEnv env)
        {
            var operand  = (args[0] as SequenceSingleSelectParameter).Value;
            var sameOnly = ((BooleanParameter)args[1]).Value;
            var one      = env.SelectedSequence.GetLabelSequence();
            var another  = operand.GetLabelSequence();
            var l1       = one.EnumerateLabels().GetEnumerator();
            var l2       = another.EnumerateLabels().GetEnumerator();

            if (!(l1.MoveNext() && l2.MoveNext()))
            {
                return(null);
            }
            ICSLabelSequence tmp = new ICSLabelSequence();

            while (true)
            {
                // (not A) or B === A => B
                if ((!sameOnly || l1.Current.LabelText == l2.Current.LabelText) && !l1.Current.IsEmptyLabel && !l2.Current.IsEmptyLabel)
                {
                    decimal begin = Math.Max(l1.Current.BeginTime, l2.Current.BeginTime);
                    decimal end   = Math.Min(l1.Current.EndTime, l2.Current.EndTime);
                    if (end > begin)
                    {
                        tmp.SetLabel(begin, end, l1.Current.LabelText);
                    }
                }
                if (l1.Current.EndTime < l2.Current.EndTime)
                {
                    if (!l1.MoveNext())
                    {
                        break;
                    }
                }
                else
                {
                    if (!l2.MoveNext())
                    {
                        break;
                    }
                }
            }

            return(SequenceData.FromLabelSequence(tmp, env.SelectedSequence.Title + "-intersect-" + operand.Title));
        }
        public SequenceData Operate(IList <ProcParam <SequenceProcEnv> > args, SequenceProcEnv env)
        {
            var rows     = ((SequenceMultiSelectParameter)args[0]).Value;
            var others   = rows.Select(p => p.GetLabelSequence()).ToList();
            var sep      = (StringParameter)args[1];
            var multi    = (BooleanParameter)args[2];
            var useEmpty = (BooleanParameter)args[3];

            if (others.Count == 0)
            {
                return(null);
            }
            var current = env.SelectedSequence.GetLabelSequence();
            var ret     = new ICSLabelSequence();

            foreach (var label in current.EnumerateLabels())
            {
                if (label.LabelText == "" && !useEmpty.Value)
                {
                    continue;
                }
                var    clips = others.Select(r => r.ClipLabelsBetween(label.BeginTime, label.EndTime, ICSLabelSequence.ClipLabelsMode.CutBorder));
                string name  = label.LabelText;
                foreach (var clip in clips)
                {
                    var texts = clip.Where(c => useEmpty.Value || c.LabelText != "").ToList();
                    if (multi.Value)
                    {
                        foreach (var text in texts)
                        {
                            name += sep.Value + text.LabelText;
                        }
                    }
                    else
                    {
                        if (texts.Count > 0)
                        {
                            name += sep.Value + texts[0].LabelText;
                        }
                    }
                    ret.SetLabel(label.ChangeText(name));
                }
            }
            return(SequenceData.FromLabelSequence(ret, PathEx.GiveName("ContainsProduct", new string[] { env.SelectedSequence.Title }.Union(rows.Select(p => p.Title)))));
        }
        public SequenceData Operate(IList <ProcParam <SequenceProcEnv> > args, SequenceProcEnv env)
        {
            var another = ((SequenceSingleSelectParameter)args[0]).Value;
            var labels  = ((LabelSelectParameter)args[1]).Value;
            var row     = another.GetLabelSequence();
            var current = env.SelectedSequence.GetLabelSequence();
            var ret     = new ICSLabelSequence();
            var l1      = current.EnumerateLabels().GetEnumerator();
            var l2      = row.EnumerateLabels().GetEnumerator();

            if (l1.MoveNext() && l2.MoveNext())
            {
                while (true)
                {
                    if (labels.Contains(l2.Current.LabelText))
                    {
                        var begin1 = l1.Current.BeginTime <= l2.Current.BeginTime && l2.Current.BeginTime < l1.Current.EndTime;
                        var end1   = l1.Current.BeginTime <= l2.Current.EndTime && l2.Current.EndTime < l1.Current.EndTime;
                        var begin2 = l2.Current.BeginTime <= l1.Current.BeginTime && l1.Current.BeginTime < l2.Current.EndTime;
                        var end2   = l2.Current.BeginTime <= l1.Current.EndTime && l1.Current.EndTime < l2.Current.EndTime;
                        if (begin1 || end1 || begin2 || end2)
                        {
                            ret.SetLabel(l1.Current);
                        }
                    }
                    if (l1.Current.EndTime < l2.Current.EndTime)
                    {
                        if (!l1.MoveNext())
                        {
                            break;
                        }
                    }
                    else
                    {
                        if (!l2.MoveNext())
                        {
                            break;
                        }
                    }
                }
            }
            return(SequenceData.FromLabelSequence(ret, PathEx.GiveName("Contains", env.SelectedSequence.Title, another.Title)));
        }
        public SequenceData Operate(IList <ProcParam <SequenceProcEnv> > args, SequenceProcEnv env)
        {
            var operand  = args[0] as SequenceMultiSelectParameter;
            var sep      = args[1] as StringParameter;
            var useEmpty = args[2] as BooleanParameter;
            List <SequenceData> sequences = new List <SequenceData>();

            sequences.Add(env.SelectedSequence);
            sequences.AddRange(operand.Value);
            ICSLabelSequence ret = new ICSLabelSequence();
            var timeList         = TimeSeriesValuesCalculation.MergeTimeList(sequences.Select(p => p.Values)).ToList();

            if (timeList.Count != 0)
            {
                var lastTime = timeList.Last();
                foreach (var time in timeList)
                {
                    if (time < lastTime)
                    {
                        StringBuilder tmp   = new StringBuilder();
                        bool          empty = true;
                        foreach (var sequence in sequences)
                        {
                            var label = sequence.GetLabelAt(time);
                            if (label != "" || useEmpty.Value)
                            {
                                if (!empty)
                                {
                                    tmp.Append(sep.Value);
                                }
                                tmp.Append(label);
                                empty = false;
                            }
                        }
                        ret.SetLabel(time, lastTime, tmp.ToString());
                    }
                }
            }
            return(SequenceData.FromLabelSequence(ret, PathEx.GiveName("Product", sequences.Select(p => p.Title))));
        }
        public SequenceData Operate(IList <ProcParam <SequenceProcEnv> > args, SequenceProcEnv env)
        {
            int check = ((SingleSelectParameter)args[0]).Value;
            ICSLabelSequence current = env.SelectedSequence.GetLabelSequence();
            ICSLabelSequence ret     = new ICSLabelSequence();
            ICSLabel         prev    = null;

            foreach (var label in current.EnumerateLabels())
            {
                switch (check)
                {
                case 0:
                    if (label.IsEmptyLabel)
                    {
                        ret.SetLabel(label.BeginTime, label.EndTime, "On");
                    }
                    break;

                case 1:
                    if (label.IsEmptyLabel && prev != null)
                    {
                        ret.SetLabel(label.BeginTime, label.EndTime, prev.LabelText);
                    }
                    break;

                case 2:
                    if (prev != null && prev.IsEmptyLabel)
                    {
                        ret.SetLabel(prev.BeginTime, prev.EndTime, label.LabelText);
                    }
                    break;
                }
                prev = label;
            }
            return(SequenceData.FromLabelSequence(ret, PathEx.GiveName("Empty-of", env.SelectedSequence.Title)));
        }
Example #12
0
        public IList <SequenceData> OutputSequence(IList <MotionObjectInfo> selected, IList <ProcParam <MotionProcEnv> > args, IEnumerable <ReadOnlyMotionFrame> frames, ProgressInformation progressInfo)
        {
            MotionObjectSingleSelectParameter main = args[0] as MotionObjectSingleSelectParameter;
            NumberParameter enlarge     = args[1] as NumberParameter;
            float           enlargement = 0;

            try { enlargement = (float)enlarge.Value; } catch (ArithmeticException) { }
            IList <MotionObjectInfo> targetInfoList = selected.Where(info => info != main.Value).ToList();


            ICSLabelSequence labelSequence = new ICSLabelSequence(TimeController.Singleton.EndTime);
            string           prevLabelText = null;
            decimal          prevTime      = decimal.MinValue;

            decimal wholeEndTime = decimal.MinValue;

            foreach (var frame in frames)
            {
                wholeEndTime = frame.Time;

                LineObject line      = frame[main.Value] as LineObject;
                string     labelText = "";
                if (line == null || targetInfoList.Any(info => frame[info] == null))
                {
                    labelText = "no data";
                }
                else
                {
                    float            minDistance = float.MaxValue;
                    MotionObjectInfo nearest     = null;
                    foreach (var targetInfo in targetInfoList)
                    {
                        float distance;
                        if (frame[targetInfo].DetectLineCollision(line.Position, line.AnotherEnd, enlargement, out distance))
                        {
                            if (distance < minDistance)
                            {
                                nearest     = targetInfo;
                                minDistance = distance;
                            }
                        }
                    }
                    if (nearest != null)
                    {
                        labelText = nearest.Name;
                    }
                    else
                    {
                        labelText = "else";
                    }
                }
                if (prevLabelText != labelText)
                {
                    decimal endTime = frame.Time;
                    if (prevTime < endTime && prevLabelText != null)
                    {
                        labelSequence.SetLabel(prevTime, endTime, prevLabelText);
                    }
                    prevTime      = endTime;
                    prevLabelText = labelText;
                }
            }
            if (prevTime < wholeEndTime && prevLabelText != null)
            {
                labelSequence.SetLabel(prevTime, wholeEndTime, prevLabelText);
            }
            SequenceData data = SequenceData.FromLabelSequence(labelSequence, PathEx.GiveName("Collision", main.Value.Name));

            return(new SequenceData[] { data });
        }
        public SequenceData Operate(IList <ProcParam <SequenceProcEnv> > args, SequenceProcEnv env)
        {
            var mode = args[0] as SingleSelectParameter;
            var threshold = args[1] as NumberParameter;
            var correct = args[2] as SequenceSingleSelectParameter;
            var correctSequence = correct.Value;
            ICSLabelSequence ret = new ICSLabelSequence();
            decimal          correctCount = 0, obtainCount = 0, matchCount = 0;

            switch (mode.Value)
            {
            case 0:
                var common = TimeSeriesValuesCalculation.MergeTimeList(new[] { env.SelectedSequence.Values, correctSequence.Values }).ToList();
                for (int i = 0; i < common.Count - 1; i++)
                {
                    string o = env.SelectedSequence.GetLabelAt(common[i]);
                    string c = correctSequence.GetLabelAt(common[i]);
                    if (o == "" && c == "")
                    {
                        continue;
                    }
                    decimal interval = common[i + 1] - common[i];
                    if (o != "")
                    {
                        obtainCount += interval;
                    }
                    if (c != "")
                    {
                        correctCount += interval;
                    }
                    if (o == c)
                    {
                        matchCount += interval;
                    }
                    ret.SetLabel(common[i], common[i + 1], o == c ? "Match" : o == "" ? "False Negative" : c == "" ? "False Positive" : "Mismatch");
                }
                break;

            case 1:
            case 2:
                SequenceData main, sub;
                if (mode.Value == 1)
                {
                    main = correctSequence;
                    sub  = env.SelectedSequence;
                }
                else
                {
                    main = env.SelectedSequence;
                    sub  = correctSequence;
                }
                var subLabel = sub.GetLabelSequence();
                foreach (var label in main.GetLabelSequence().EnumerateLabels())
                {
                    decimal denom = label.Duration;
                    if (label.LabelText != "")
                    {
                        var     clip  = subLabel.ClipLabelsBetween(label.BeginTime, label.EndTime, ICSLabelSequence.ClipLabelsMode.CutBorder);
                        decimal numer = clip.Where(l => l.LabelText == label.LabelText).Select(l => l.Duration).Sum();
                        if (numer >= denom * threshold.Value)
                        {
                            matchCount++;
                            ret.SetLabel(label.ChangeText("Match"));
                        }
                        else
                        {
                            ret.SetLabel(label.ChangeText(mode.Value == 1 ? "False Negative" : "False Positive"));
                        }
                        obtainCount++;
                        correctCount++;
                    }
                    else
                    {
                        var clip      = subLabel.ClipLabelsBetween(label.BeginTime, label.EndTime, ICSLabelSequence.ClipLabelsMode.CutBorder);
                        var missClip  = clip.Where(l => l.LabelText != "").ToList();
                        var missCount = missClip.Count;
                        if (mode.Value == 1)
                        {
                            obtainCount += missCount;
                        }
                        else
                        {
                            correctCount += missCount;
                        }
                        foreach (var m in missClip)
                        {
                            ret.SetLabel(m.ChangeText(mode.Value == 1 ? "False Positive" : "False Negative"));
                        }
                    }
                }
                break;
            }
            return(SequenceData.FromLabelSequence(ret, PathEx.GiveName("PrecisionRecall", env.SelectedSequence.Title, correctSequence.Title)));
        }
        public SequenceData Operate(IList <ProcParam <SequenceProcEnv> > args, SequenceProcEnv env)
        {
            var target           = ((LabelSelectParameter)args[0]).Value;
            var length           = ((NumberParameter)args[1]).Value;
            var action           = ((SingleSelectParameter)args[2]).Value;
            var border           = ((BooleanParameter)args[3]).Value;
            var glue             = ((SingleSelectParameter)args[4]).Value;
            ICSLabelSequence ret = env.SelectedSequence.GetLabelSequence();
            var    names         = ret.GetLabelTexts();
            string tmpLabel      = "tmpToRemove";

            while (names.Contains(tmpLabel))
            {
                tmpLabel += "-";
            }
            foreach (var label in ret.EnumerateLabels().Where(l => target.Contains(l.LabelText)).ToList())
            {
                if (action == 0)
                {
                    if (label.Duration < length || (border && label.Duration == length))
                    {
                        ret.SetLabel(label.BeginTime, label.EndTime, tmpLabel);
                    }
                }
                else
                {
                    if (label.Duration > length || (border && label.Duration == length))
                    {
                        ret.SetLabel(label.BeginTime, label.EndTime, tmpLabel);
                    }
                }
            }
            ret.RemoveBorderSameAsPrevious();
            var labels = ret.EnumerateLabels().ToList();

            for (int i = 0; i < labels.Count; i++)
            {
                if (labels[i].LabelText == tmpLabel)
                {
                    string to = "";
                    switch (glue)
                    {
                    case 1:
                        if (i > 0 && i < labels.Count - 1)
                        {
                            if (labels[i - 1].LabelText == labels[i + 1].LabelText)
                            {
                                to = labels[i - 1].LabelText;
                            }
                        }
                        break;

                    case 2:
                        if (i > 0)
                        {
                            to = labels[i - 1].LabelText;
                        }
                        break;

                    case 3:
                        if (i < labels.Count - 1)
                        {
                            to = labels[i + 1].LabelText;
                        }
                        break;
                    }
                    ret.SetLabel(labels[i].BeginTime, labels[i].EndTime, to);
                }
            }
            ret.RemoveBorderSameAsPrevious();
            return(SequenceData.FromLabelSequence(ret, PathEx.GiveName("Filtered", env.SelectedSequence.Title, (action == 0 ? "Lt" : "Gt") + length.ToString())));
        }