public static PerfMatrix <LblT> GetCoincidenceMatrix <LblT, T, U>(PerfMatrix <LblT> mtx, IEnumerable <ILabeledExample <LblT, IAgreementExample <T, U> > > examples, AgreementKind kind = AgreementKind.Inter, U observerId = default(U), bool resolveMultiplePerObserver = false) { int count; return(GetCoincidenceMatrix(mtx, examples, out count, kind, observerId, resolveMultiplePerObserver)); }
public static PerfMatrix <LblT> GetCoincidenceMatrix <LblT, T, U>(PerfMatrix <LblT> mtx, IEnumerable <ILabeledExample <LblT, IAgreementExample <T, U> > > examples, out int unitCount, AgreementKind kind = AgreementKind.Inter, U observerId = default(U), bool resolveMultiplePerObserver = false) { Preconditions.CheckNotNull(mtx); ILabeledExample <LblT, IAgreementExample <T, U> >[][] units = Preconditions.CheckNotNull(examples) .GroupBy(e => e.Example.UnitId()) .Where(g => g.Count() > 1 && !EqualityComparer <T> .Default.Equals(g.Key, default(T))) // skip nulls or zeros .Select(g => g.ToArray()).ToArray(); switch (kind) { case AgreementKind.Self: units = EqualityComparer <U> .Default.Equals(observerId, default(U)) ? units // observer not specified .Select(u => u.GroupBy(e => e.Example.ObserverId()) .Where(g => g.Count() > 1 && !EqualityComparer <U> .Default.Equals(g.Key, default(U))) .SelectMany(g => g).ToArray()) .Where(u => u.Count() > 1).ToArray() : units .Select(u => u.Where(e => EqualityComparer <U> .Default.Equals(e.Example.ObserverId(), observerId)).ToArray()) .Where(u => u.Count() > 1).ToArray(); break; case AgreementKind.InterExcludingObserver: units = units .Select(u => u.Where(e => !EqualityComparer <U> .Default.Equals(e.Example.ObserverId(), observerId)).ToArray()) .Where(u => u.Count() > 1).ToArray(); break; case AgreementKind.InterIncludingUnits: units = units .Where(u => u.Any(e => EqualityComparer <U> .Default.Equals(e.Example.ObserverId(), observerId))).ToArray(); break; case AgreementKind.InterExcludingUnits: units = units .Where(u => u.All(e => !EqualityComparer <U> .Default.Equals(e.Example.ObserverId(), observerId))).ToArray(); break; } unitCount = 0; foreach (ILabeledExample <LblT, IAgreementExample <T, U> >[] unit_ in units) { ILabeledExample <LblT, IAgreementExample <T, U> >[] unit = unit_; // resolve multiple labels by the same user if (resolveMultiplePerObserver && kind != AgreementKind.Self) { unit = unit.GroupBy(e => e.Example.ObserverId()) .Select(ug => ug.GroupBy(ue => ue.Label) .OrderByDescending(ulg => ulg.Count()) .First().First()).ToArray(); if (unit.Length <= 1) { continue; } } for (int i = 0; i < unit.Length; i++) { for (int j = i + 1; j < unit.Length; j++) { mtx.AddCount(unit[i].Label, unit[j].Label, 1.0 / (unit.Length - 1)); mtx.AddCount(unit[j].Label, unit[i].Label, 1.0 / (unit.Length - 1)); } } unitCount++; } return(mtx); }