2094ede545c79edb0c264a2c63c0630cfef1339f
[pspp-builds.git] / src / output / charts / roc-chart.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009 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 <output/chart-provider.h>
22 #include <output/charts/cartesian.h>
23 #include <output/charts/plot-chart.h>
24 #include <data/casereader.h>
25 #include <language/stats/roc.h>
26
27 #include "xalloc.h"
28
29 #include "gettext.h"
30 #define _(msgid) gettext (msgid)
31
32 struct roc_var
33   {
34     char *name;
35     struct casereader *cutpoint_reader;
36   };
37
38 struct roc_chart
39   {
40     struct chart chart;
41     bool reference;
42     struct roc_var *vars;
43     size_t n_vars;
44     size_t allocated_vars;
45   };
46
47 static const struct chart_class roc_chart_class;
48
49 struct roc_chart *
50 roc_chart_create (bool reference)
51 {
52   struct roc_chart *rc = xmalloc (sizeof *rc);
53   chart_init (&rc->chart, &roc_chart_class);
54   rc->reference = reference;
55   rc->vars = NULL;
56   rc->n_vars = 0;
57   rc->allocated_vars = 0;
58   return rc;
59 }
60
61 void
62 roc_chart_add_var (struct roc_chart *rc, const char *var_name,
63                    const struct casereader *cutpoint_reader)
64 {
65   struct roc_var *rv;
66
67   if (rc->n_vars >= rc->allocated_vars)
68     rc->vars = x2nrealloc (rc->vars, &rc->allocated_vars, sizeof *rc->vars);
69
70   rv = &rc->vars[rc->n_vars++];
71   rv->name = xstrdup (var_name);
72   rv->cutpoint_reader = casereader_clone (cutpoint_reader);
73 }
74
75 struct chart *
76 roc_chart_get_chart (struct roc_chart *rc)
77 {
78   return &rc->chart;
79 }
80
81 static void
82 roc_chart_draw (const struct chart *chart, cairo_t *cr,
83                 struct chart_geometry *geom)
84 {
85   const struct roc_chart *rc = UP_CAST (chart, struct roc_chart, chart);
86   size_t i;
87
88   chart_write_title (cr, geom, _("ROC Curve"));
89   chart_write_xlabel (cr, geom, _("1 - Specificity"));
90   chart_write_ylabel (cr, geom, _("Sensitivity"));
91
92   chart_write_xscale (cr, geom, 0, 1, 5);
93   chart_write_yscale (cr, geom, 0, 1, 5);
94
95   if ( rc->reference )
96     {
97       chart_line (cr, geom, 1.0, 0,
98                   0.0, 1.0,
99                   CHART_DIM_X);
100     }
101
102   for (i = 0; i < rc->n_vars; ++i)
103     {
104       const struct roc_var *rv = &rc->vars[i];
105       struct casereader *r = casereader_clone (rv->cutpoint_reader);
106       struct ccase *cc;
107
108       chart_vector_start (cr, geom, rv->name);
109       for (; (cc = casereader_read (r)) != NULL; case_unref (cc))
110         {
111           double se = case_data_idx (cc, ROC_TP)->f;
112           double sp = case_data_idx (cc, ROC_TN)->f;
113
114           se /= case_data_idx (cc, ROC_FN)->f + case_data_idx (cc, ROC_TP)->f ;
115           sp /= case_data_idx (cc, ROC_TN)->f + case_data_idx (cc, ROC_FP)->f ;
116
117           chart_vector (cr, geom, 1 - sp, se);
118         }
119       chart_vector_end (cr, geom);
120       casereader_destroy (r);
121     }
122
123   chart_write_legend (cr, geom);
124 }
125
126 static void
127 roc_chart_destroy (struct chart *chart)
128 {
129   struct roc_chart *rc = UP_CAST (chart, struct roc_chart, chart);
130   size_t i;
131
132   for (i = 0; i < rc->n_vars; i++)
133     {
134       struct roc_var *rv = &rc->vars[i];
135       free (rv->name);
136       casereader_destroy (rv->cutpoint_reader);
137     }
138   free (rc->vars);
139   free (rc);
140 }
141
142 static const struct chart_class roc_chart_class =
143   {
144     roc_chart_draw,
145     roc_chart_destroy
146   };
147
148