TraceLab Component Library
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties
PrecisionRecallCurveComputation.cs
Go to the documentation of this file.
1 // TraceLab Component Library
2 // Copyright © 2012-2013 SEMERU
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 using System;
18 using System.Collections.Generic;
19 using System.Linq;
20 using System.Text;
21 using TraceLabSDK;
22 using TraceLabSDK.Types;
23 using TraceLabSDK.Types.Contests;
24 
25 namespace TraceLab.Components.DevelopmentKit.Metrics.Traceability
26 {
32  {
33  #region Members
34 
35  private const string _name = "Precision-Recall Curve";
36  private const string _description = "Computes the precision and recall at every cutpoint in the ranklist. This is an inherent ranklist granularity (even when used in the query component).";
37 
41  public override string Name
42  {
43  get { return _name; }
44  }
45 
49  public override string Description
50  {
51  get { return _description; }
52  }
53 
54  private TLSimilarityMatrix _matrix;
55  private TLSimilarityMatrix _oracle;
56  private string _precisionFormat;
57  private string _recallFormat;
58 
59  #endregion
60 
66  public PrecisionRecallCurveComputation(TLSimilarityMatrix candidateMatrix, TLSimilarityMatrix answerMatrix)
67  : base()
68  {
69  _matrix = candidateMatrix;
70  _oracle = answerMatrix;
71  _precisionFormat = String.Format("{{0:D{0}}}_Precision", Math.Floor(Math.Log10(candidateMatrix.Count)));
72  _recallFormat = String.Format("{{0:D{0}}}_Recall", Math.Floor(Math.Log10(candidateMatrix.Count)));
73  }
74 
78  protected override void ComputeImplementation()
79  {
80  _oracle.Threshold = 0;
81  int correct = 0;
82  TLLinksList links = _matrix.AllLinks;
83  links.Sort();
84  Results = new SerializableDictionary<string, double>();
85  for (int linkNumber = 1; linkNumber <= links.Count; linkNumber++)
86  {
87  if (_oracle.IsLinkAboveThreshold(links[linkNumber - 1].SourceArtifactId, links[linkNumber - 1].TargetArtifactId))
88  {
89  correct++;
90  }
91  Results.Add(String.Format(_precisionFormat, linkNumber), correct / (double)linkNumber);
92  Results.Add(String.Format(_recallFormat, linkNumber), correct / (double)_oracle.Count);
93  }
94  }
95 
100  protected override Metric GenerateSummaryImplementation()
101  {
102  LineSeries data = new LineSeries(_name, _description);
103  for (int i = 1; i <= Results.Count / 2; i++)
104  {
105  double x, y;
106  try
107  {
108  x = Results[String.Format(_recallFormat, i)];
109  y = Results[String.Format(_precisionFormat, i)];
110  }
111  catch (KeyNotFoundException e)
112  {
113  throw new DevelopmentKitException("Results in incorrect format.", e);
114  }
115  data.AddPoint(new Point(x, y));
116  }
117  return data;
118  }
119  }
120 }