public override double Calculate(InputStatistic input) { var src = (IEnumerable <WeakReference <ConfigurationStatistic> >)input.Args.Parameters[0]; double[] vals = src.Select(delegate(WeakReference <ConfigurationStatistic> z) { ConfigurationStatistic a = z.GetTarget(); if (a != null) { if (a.Results == null) { throw new InvalidOperationException($"This algorithm cannot currently execute because a referenced statistic ({a}) has not yet executed."); } return(a.Results.Results[input.PeakA]); } else { throw new InvalidOperationException($"The statistic referenced by this method ({this}) no longer exists."); } }).ToArray(); return(this._delegate(vals)); }
private void GeneratePreview(ArgsStatistic sel) { if (sel == null) { return; } if (this._previewPeak == null) { return; } this._lblPreview.Text = this._previewPeak.DisplayName + ": "; try { ConfigurationStatistic temp = new ConfigurationStatistic() { Args = sel }; double v = temp.Calculate(this._core, this._previewPeak); this._lblPreview2.Text = v.ToString(); this._lblPreview2.ForeColor = System.Drawing.Color.Gray; } catch (Exception ex) { this._lblPreview2.Text = ex.Message; this._lblPreview2.ForeColor = System.Drawing.Color.Red; } }
protected override bool Test(Peak p) { double v; ConfigurationStatistic stat = this.Statistic.GetTarget(); if (stat == null) { throw new InvalidOperationException("Statistic required for significance detection but statistic is no longer available."); } if (!stat.Results.Results.TryGetValue(p, out v)) { throw new InvalidOperationException("Statistic required (" + this.Statistic + ") for significance detection but statistic is no longer available."); } switch (this.StatisticOp) { case EStatOperator.LessThan: return(v < this.StatisticValue); case EStatOperator.LessThanEq: return(v <= this.StatisticValue); case EStatOperator.MoreThan: return(v > this.StatisticValue); case EStatOperator.MoreThanEq: return(v >= this.StatisticValue); default: throw new InvalidOperationException("Invalid switch: " + this.StatisticOp); } }
/// <summary> /// Gets the statistic by the name of "x", otherwise NaN. /// </summary> internal double GetStatistic(ConfigurationStatistic x) { double v; if (x.Results.Results.TryGetValue(this, out v)) { return(v); } return(double.NaN); }
private static string GetDisplayName(WeakReference <ConfigurationStatistic> a) { ConfigurationStatistic b = a.GetTarget(); if (b == null) { return("[MISSING STATISTIC]"); } else { return(b.ToString()); } }
/// <summary> /// IMPLEMENTS IVisualisable /// </summary> public override void GetXColumns(CustomColumnRequest request) { ColumnCollection <Cluster> result = request.Results.Cast <Cluster>(); Core core = request.Core; result.Add("Assignments\\As peaks", EColumn.None, λ => λ.Assignments.Peaks.ToArray()); result.Add("Assignments\\As scores", EColumn.Advanced, λ => λ.Assignments.Scores.ToArray()); foreach (GroupInfo group in core.Groups) { GroupInfo closure = group; result.Add( "Assignments\\For " + group.DisplayName, EColumn.None, λ => λ.Assignments.List.Where(z => z.Vector.Group == closure).Select(z => z.Cluster).ToArray(), z => closure.Colour); } foreach (UserFlag flag in core.Options.UserFlags) { UserFlag closure = flag; result.Add( "Flags\\" + flag, EColumn.Advanced, λ => λ.CommentFlags.ContainsKey(closure) ? λ.CommentFlags[closure] : 0, z => closure.Colour); } result.Add("Flags\\Summary", EColumn.None, λ => λ.CommentFlags.Select(z => z.Key + " = " + z.Value), z => z.CommentFlags.Count != 1 ? Color.Black : z.CommentFlags.Keys.First().Colour); foreach (ConfigurationStatistic stat in core.Statistics.WhereEnabled()) { ConfigurationStatistic closure = stat; result.Add("Average Statistic\\" + closure, EColumn.IsStatistic, λ => λ.Statistics.GetOrNan(closure)); } foreach (string stat in core.GetClusterStatistics()) // TODO: No! { string closure = stat; result.Add("Cluster statistic\\" + closure, EColumn.IsStatistic, λ => λ.ClusterStatistics.GetOrNan(closure)); } }
public ConditionStatistic(ELogicOperator op, bool negate, EStatOperator statOp, ConfigurationStatistic stat, double statisticValue) : base(op, negate) { this.StatisticOp = statOp; this.StatisticValue = statisticValue; this.Statistic = new WeakReference <ConfigurationStatistic>(stat); }
/// <summary> /// CONSTRUCTOR /// </summary> private FrmEditPeakFilterCondition(Form owner, Core core, PeakFilter.Condition defaults, bool readOnly) : this() { this._core = core; this._readOnly = readOnly; this.ctlTitleBar1.Text = readOnly ? "View Condition" : "Edit Condition"; // Setup boxes this._cbPeaks = DataSet.ForPeaks(core).CreateConditionBox(this._txtIsInSet, this._btnIsInSet); this._cbFlags = DataSet.ForUserFlags(core).CreateConditionBox(this._txtIsFlaggedWith, this._btnIsFlaggedWith); this._cbClusters = DataSet.ForClusters(core).CreateConditionBox(this._txtIsInCluster, this._btnIsInCluster); this._lsoFlags = EnumComboBox.Create(this._lstFlagComparator, Filter.ESetOperator.Any); this._lsoPats = EnumComboBox.Create(this._lstClusterComparator, Filter.ELimitedSetOperator.Any); this._lsoPeaks = EnumComboBox.Create(this._lstPeakComparator, Filter.EElementOperator.Is); this._lsoFilter = EnumComboBox.Create(this._lstFilterOp, Filter.EElementOperator.Is); this._lsoStats = EnumComboBox.Create(this._lstStatisticComparator, Filter.EStatOperator.LessThan); this._lstIsStatistic.Items.AddRange(IVisualisableExtensions.WhereEnabled(core.Statistics).ToArray()); this._ecbFilter = DataSet.ForPeakFilter(core).CreateComboBox(this._lstFilter, null, EditableComboBox.EFlags.IncludeAll); this._isInitialised = true; if (defaults == null) { this.checkBox1.Checked = false; this._radAnd.Checked = true; this._txtComp_TextChanged(null, null); } else { // Not this.checkBox1.Checked = defaults.Negate; this._radAnd.Checked = defaults.CombiningOperator == Filter.ELogicOperator.And; this._radOr.Checked = defaults.CombiningOperator == Filter.ELogicOperator.Or; if (defaults is PeakFilter.ConditionCluster) { PeakFilter.ConditionCluster def = (PeakFilter.ConditionCluster)defaults; List <Cluster> strong; if (!def.Clusters.TryGetStrong(out strong)) { this.ShowWeakFailureMessage("clusters"); } this._chkIsInCluster.Checked = true; this._lsoPats.SelectedItem = def.ClustersOp; this._cbClusters.SelectedItems = strong; } else if (defaults is PeakFilter.ConditionPeak) { PeakFilter.ConditionPeak def = (PeakFilter.ConditionPeak)defaults; List <Peak> strong; if (!def.Peaks.TryGetStrong(out strong)) { this.ShowWeakFailureMessage("peaks"); } this._chkIsInSet.Checked = true; this._lsoPeaks.SelectedItem = def.PeaksOp; this._cbPeaks.SelectedItems = strong; } else if (defaults is PeakFilter.ConditionFlags) { PeakFilter.ConditionFlags def = (PeakFilter.ConditionFlags)defaults; List <UserFlag> strong; if (!def.Flags.TryGetStrong(out strong)) { this.ShowWeakFailureMessage("peaks"); } this._chkIsFlaggedWith.Checked = true; this._lsoFlags.SelectedItem = def.FlagsOp; this._cbFlags.SelectedItems = strong; } else if (defaults is PeakFilter.ConditionStatistic) { PeakFilter.ConditionStatistic def = (PeakFilter.ConditionStatistic)defaults; ConfigurationStatistic strong; if (!def.Statistic.TryGetTarget(out strong)) { this.ShowWeakFailureMessage("statistics"); } ConfigurationStatistic stat = def.Statistic.GetTarget(); if (stat == null) { FrmMsgBox.ShowError(this, "The statistic specified when this condition was created has since been removed. Please select a different statistic."); } this._chkIsStatistic.Checked = true; this._lsoStats.SelectedItem = def.StatisticOp; this._lstIsStatistic.SelectedItem = stat; this._txtStatisticValue.Text = def.StatisticValue.ToString(); } else if (defaults is PeakFilter.ConditionFilter) { PeakFilter.ConditionFilter def = (PeakFilter.ConditionFilter)defaults; this._radFilter.Checked = true; this._lsoFilter.SelectedItem = def.FilterOp ? Filter.EElementOperator.Is : Filter.EElementOperator.IsNot; this._ecbFilter.SelectedItem = def.Filter; } else { throw new SwitchException(defaults.GetType()); } } if (readOnly) { UiControls.MakeReadOnly(this); } }
private string GetStatName(ConfigurationStatistic input) { return(input.ToString()); }
void find_classifier() { Core core = this._core; UserFlag type1; UserFlag type2; ConfigurationStatistic stat = DataSet.ForStatistics(this._core).ShowList(this, null); if (stat == null) { FrmMsgBox.ShowError(this, "No stat with this name"); return; } string sign = FrmInputSingleLine.Show(this, "Classifier settings", "Find classifier", "Enter the cutoff, or 0 for for automatic", "0"); double manCutoff; type1 = DataSet.ForUserFlags(this._core).IncludeMessage("Specify the comment flag signifying the first type").ShowList(this, null); if (type1 == null) { return; } type2 = DataSet.ForUserFlags(this._core).IncludeMessage("Specify the comment flag signifying the second type").ShowList(this, null); if (type2 == null) { return; } if (!double.TryParse(sign, out manCutoff)) { return; } StringBuilder sb = new StringBuilder(); // TID0 = Full // TID1..5 = Test/training // TID6..9 = Bootstrap for (int tid = 0; tid < 10; tid++) { // Get all significances List <double> sigs = new List <double>(core.Peaks.Select(λ => λ.GetStatistic(stat))); List <bool> inTrainingSet = new List <bool>(core.Peaks.Count); int co; // For the training only include 75% if (tid >= 1 && tid <= 5) { co = (int)(core.Peaks.Count * 0.75d); } else { co = core.Peaks.Count; } for (int n = 0; n < core.Peaks.Count; n++) { inTrainingSet.Add(n < co); } inTrainingSet.Shuffle(); // For the boot-strap shuffle the sigs if (tid >= 6) { sigs.Shuffle(); } Tuple <double, int, int, int, int> best = null; Tuple <double, int, int, int, int> bestTest = null; double cutoff = 0; // Find the best variable cutoff if (manCutoff == 0.0d) { for (int n = 0; n < core.Peaks.Count; n++) { if (inTrainingSet[n]) { var success = this.SimpleClassify(sigs[n], type1, type2, sigs, inTrainingSet, true); if (best == null || success.Item1 > best.Item1) { best = success; bestTest = this.SimpleClassify(sigs[n], type1, type2, sigs, inTrainingSet, false); cutoff = sigs[n]; } } } } else { best = this.SimpleClassify(manCutoff, type1, type2, sigs, inTrainingSet, true); bestTest = this.SimpleClassify(manCutoff, type1, type2, sigs, inTrainingSet, false); cutoff = manCutoff; } sb.AppendLine(tid == 0 ? "FULLDATA" : tid <= 5 ? "VALIDATION" : "BOOTSTRAP"); sb.AppendLine(); sb.AppendLine(" " + type1 + " <= " + cutoff + " < " + type2); sb.AppendLine(); sb.AppendLine(" TRAINING SET (" + co + ")"); sb.AppendLine(" " + type1 + " correct: " + StringHelper.AsFraction(best.Item2, best.Item2 + best.Item3)); sb.AppendLine(" " + type2 + " correct: " + StringHelper.AsFraction(best.Item4, best.Item4 + best.Item5)); sb.AppendLine(" Total correct: " + StringHelper.AsFraction(best.Item2 + best.Item4, best.Item2 + best.Item4 + best.Item3 + best.Item5)); sb.AppendLine(" Variables used: " + StringHelper.AsFraction(best.Item2 + best.Item4 + best.Item3 + best.Item5, core.Peaks.Count)); sb.AppendLine(); if (co != core.Peaks.Count) { sb.AppendLine(" TEST SET (" + (core.Peaks.Count - co) + ")"); sb.AppendLine(" " + type1 + " correct: " + StringHelper.AsFraction(bestTest.Item2, bestTest.Item2 + bestTest.Item3)); sb.AppendLine(" " + type2 + " correct: " + StringHelper.AsFraction(bestTest.Item4, bestTest.Item4 + bestTest.Item5)); sb.AppendLine(" Total correct: " + StringHelper.AsFraction(bestTest.Item2 + bestTest.Item4, bestTest.Item2 + bestTest.Item4 + bestTest.Item3 + bestTest.Item5)); sb.AppendLine(" Variables used: " + StringHelper.AsFraction(bestTest.Item2 + bestTest.Item4 + bestTest.Item3 + bestTest.Item5, core.Peaks.Count)); sb.AppendLine(); sb.AppendLine(" SCORE: " + (bestTest.Item1 * 100).ToString("F02")); } else { sb.AppendLine(" SCORE: " + (best.Item1 * 100).ToString("F02")); } sb.AppendLine(); sb.AppendLine("--------------------------------------------------------------------------------"); sb.AppendLine(); } FrmInputMultiLine.ShowFixed(this, "Find classifier", "Classifier results", "Best value to determine split between variables marked with \"" + type1 + "\" and \"" + type2 + "\" based on their significances", sb.ToString()); }