case: Introduce new functions for numbers and substrings in cases.
[pspp] / src / output / charts / roc-chart-cairo.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009, 2011 Free Software Foundation, Inc.
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 #include <config.h>
18
19 #include "output/charts/roc-chart.h"
20
21 #include "data/case.h"
22 #include "data/casereader.h"
23 #include "language/stats/roc.h"
24 #include "output/cairo-chart.h"
25
26 #include "gettext.h"
27 #define _(msgid) gettext (msgid)
28
29 void
30 xrchart_draw_roc (const struct chart *chart, cairo_t *cr,
31                   struct xrchart_geometry *geom)
32 {
33   const struct roc_chart *rc = to_roc_chart (chart);
34   size_t i;
35
36   xrchart_write_title (cr, geom, _("ROC Curve"));
37   xrchart_write_xlabel (cr, geom, _("1 - Specificity"));
38   xrchart_write_ylabel (cr, geom, _("Sensitivity"));
39
40   if (! xrchart_write_xscale (cr, geom, 0, 1))
41     return;
42   if (! xrchart_write_yscale (cr, geom, 0, 1))
43     return;
44
45   if (rc->reference)
46     {
47       xrchart_line (cr, geom, 1.0, 0,
48                     0.0, 1.0,
49                     XRCHART_DIM_X);
50     }
51
52   for (i = 0; i < rc->n_vars; ++i)
53     {
54       const struct roc_var *rv = &rc->vars[i];
55       struct casereader *r = casereader_clone (rv->cutpoint_reader);
56       struct ccase *cc;
57
58       xrchart_vector_start (cr, geom, rv->name);
59       for (; (cc = casereader_read (r)) != NULL; case_unref (cc))
60         {
61           double se = case_num_idx (cc, ROC_TP);
62           double sp = case_num_idx (cc, ROC_TN);
63
64           se /= case_num_idx (cc, ROC_FN) + case_num_idx (cc, ROC_TP);
65           sp /= case_num_idx (cc, ROC_TN) + case_num_idx (cc, ROC_FP);
66
67           xrchart_vector (cr, geom, 1 - sp, se);
68         }
69       xrchart_vector_end (cr, geom);
70       casereader_destroy (r);
71     }
72
73   xrchart_write_legend (cr, geom);
74 }
75