New function prepare_cutpoints
authorJohn Darrington <john@darrington.wattle.id.au>
Wed, 15 Jul 2009 12:19:49 +0000 (20:19 +0800)
committerJohn Darrington <john@darrington.wattle.id.au>
Wed, 15 Jul 2009 12:19:49 +0000 (20:19 +0800)
Move the code which creates the cutpoints into its own
function.  This makes for easier reading IMO.

src/language/stats/roc.c

index 6a0e412edcadd40be8afd9c6044eea4ce20fc5ce..e6251b36b38ff79c495931e1b805f4489df3c992 100644 (file)
@@ -540,77 +540,81 @@ append_cutpoint (struct casewriter *writer, double cutpoint)
 }
 
 
+/* Prepare the cutpoints */
 static void
-do_roc (struct cmd_roc *roc, struct casereader *input, struct dictionary *dict)
+prepare_cutpoints (struct cmd_roc *roc, struct roc_state *rs, struct casereader *input)
 {
   int i;
+  struct casereader *r = casereader_clone (input);
+  struct ccase *c;
+  struct caseproto *proto = caseproto_create ();
 
-  struct roc_state *rs = xcalloc (roc->n_vars, sizeof *rs);
+  struct subcase ordering;
+  subcase_init (&ordering, CUTPOINT, 0, SC_ASCEND);
 
-  struct casewriter *neg_wtr = autopaging_writer_create (casereader_get_proto (input));
+  proto = caseproto_add_width (proto, 0); /* cutpoint */
+  proto = caseproto_add_width (proto, 0); /* TP */
+  proto = caseproto_add_width (proto, 0); /* FN */
+  proto = caseproto_add_width (proto, 0); /* TN */
+  proto = caseproto_add_width (proto, 0); /* FP */
 
-  struct casereader *negatives = NULL;
-  struct casereader *positives = NULL;
+  for (i = 0 ; i < roc->n_vars; ++i)
+    {
+      rs[i].cutpoint_wtr = sort_create_writer (&ordering, proto);
+      rs[i].prev_result = SYSMIS;
+      rs[i].max = -DBL_MAX;
+      rs[i].min = DBL_MAX;
+    }
 
-  struct caseproto *n_proto = caseproto_create ();
+  for (; (c = casereader_read (r)) != NULL; case_unref (c))
+    {
+      for (i = 0 ; i < roc->n_vars; ++i)
+       {
+         const double result = case_data (c, roc->vars[i])->f;
 
-  struct subcase up_ordering;
-  struct subcase down_ordering;
+         minimize (&rs[i].min, result);
+         maximize (&rs[i].max, result);
 
-  /* Prepare the cutpoints */
-  {
-    struct casereader *r = casereader_clone (input);
-    struct ccase *c;
-    struct caseproto *proto = caseproto_create ();
+         if ( rs[i].prev_result != SYSMIS && rs[i].prev_result != result )
+           {
+             const double mean = (result + rs[i].prev_result ) / 2.0;
+             append_cutpoint (rs[i].cutpoint_wtr, mean);
+           }
 
-    struct subcase ordering;
-    subcase_init (&ordering, CUTPOINT, 0, SC_ASCEND);
+         rs[i].prev_result = result;
+       }
+    }
+  casereader_destroy (r);
 
-    proto = caseproto_add_width (proto, 0); /* cutpoint */
-    proto = caseproto_add_width (proto, 0); /* TP */
-    proto = caseproto_add_width (proto, 0); /* FN */
-    proto = caseproto_add_width (proto, 0); /* TN */
-    proto = caseproto_add_width (proto, 0); /* FP */
 
+  /* Append the min and max cutpoints */
+  for (i = 0 ; i < roc->n_vars; ++i)
+    {
+      append_cutpoint (rs[i].cutpoint_wtr, rs[i].min - 1);
+      append_cutpoint (rs[i].cutpoint_wtr, rs[i].max + 1);
 
-    for (i = 0 ; i < roc->n_vars; ++i)
-      {
-       rs[i].cutpoint_wtr = sort_create_writer (&ordering, proto);
-       rs[i].prev_result = SYSMIS;
-       rs[i].max = -DBL_MAX;
-       rs[i].min = DBL_MAX;
-      }
+      rs[i].cutpoint_rdr = casewriter_make_reader (rs[i].cutpoint_wtr);
+    }
+}
 
-    for (; (c = casereader_read (r)) != NULL; case_unref (c))
-      {
-       for (i = 0 ; i < roc->n_vars; ++i)
-         {
-           const double result = case_data (c, roc->vars[i])->f;
+static void
+do_roc (struct cmd_roc *roc, struct casereader *input, struct dictionary *dict)
+{
+  int i;
 
-           minimize (&rs[i].min, result);
-           maximize (&rs[i].max, result);
+  struct roc_state *rs = xcalloc (roc->n_vars, sizeof *rs);
 
-           if ( rs[i].prev_result != SYSMIS && rs[i].prev_result != result )
-             {
-               const double mean = (result + rs[i].prev_result ) / 2.0;
-               append_cutpoint (rs[i].cutpoint_wtr, mean);
-             }
+  struct casewriter *neg_wtr = autopaging_writer_create (casereader_get_proto (input));
 
-           rs[i].prev_result = result;
-         }
-      }
-    casereader_destroy (r);
+  struct casereader *negatives = NULL;
+  struct casereader *positives = NULL;
 
+  struct caseproto *n_proto = caseproto_create ();
 
-    /* Append the min and max cutpoints */
-    for (i = 0 ; i < roc->n_vars; ++i)
-      {
-       append_cutpoint (rs[i].cutpoint_wtr, rs[i].min - 1);
-       append_cutpoint (rs[i].cutpoint_wtr, rs[i].max + 1);
+  struct subcase up_ordering;
+  struct subcase down_ordering;
 
-       rs[i].cutpoint_rdr = casewriter_make_reader (rs[i].cutpoint_wtr);
-      }
-  }
+  prepare_cutpoints (roc, rs, input);
 
   positives = 
     casereader_create_filter_func (input,