From 06ac0c2d623e9ae54c3e22a2d6740219ab357b21 Mon Sep 17 00:00:00 2001 From: Ben Pfaff <blp@cs.stanford.edu> Date: Sun, 6 Feb 2022 08:52:59 -0800 Subject: [PATCH] MISSING=INCLUDE/EXCLUDE works --- src/language/stats/ctables.c | 34 +++--- tests/language/stats/ctables.at | 189 ++++++++++++++++++++++++-------- 2 files changed, 167 insertions(+), 56 deletions(-) diff --git a/src/language/stats/ctables.c b/src/language/stats/ctables.c index 0ff1a4d41d..8acf76a5c9 100644 --- a/src/language/stats/ctables.c +++ b/src/language/stats/ctables.c @@ -1285,7 +1285,7 @@ ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict, return false; struct ctables_categories *c = xmalloc (sizeof *c); - *c = (struct ctables_categories) { .n_refs = n_vars }; + *c = (struct ctables_categories) { .n_refs = n_vars, .show_empty = true }; for (size_t i = 0; i < n_vars; i++) { struct ctables_categories **cp @@ -3459,19 +3459,25 @@ ctables_section_recurse_add_empty_categories ( enum pivot_axis_type a, size_t a_idx) { if (a >= PIVOT_N_AXES) - { - - } - else if (!s->nests[a] || idx >= s->nests[a]->n) - { - - - } + ctables_cell_insert__ (s, c, cats); + else if (!s->nests[a] || a_idx >= s->nests[a]->n) + ctables_section_recurse_add_empty_categories (s, cats, c, a + 1, 0); else { - - for (size_t i = 0; i < s->nests[a]->n; i++) - + const struct variable *var = s->nests[a]->vars[a_idx]; + int width = var_get_width (var); + const struct hmap *occurrences = &s->occurrences[a][a_idx]; + const struct ctables_section_value *sv; + HMAP_FOR_EACH (sv, struct ctables_section_value, node, occurrences) + { + union value *value = case_data_rw (c, var); + value_destroy (value, width); + value_clone (value, &sv->value, width); + cats[a][a_idx] = ctables_categories_match ( + s->table->categories[var_get_dict_index (var)], value, var); + assert (cats[a][a_idx] != NULL); + ctables_section_recurse_add_empty_categories (s, cats, c, a, a_idx + 1); + } } } @@ -3498,7 +3504,8 @@ ctables_section_add_empty_categories (struct ctables_section *s) const struct ctables_category *cats[PIVOT_N_AXES][10]; /* XXX */ struct ccase *c = case_create (dict_get_proto (s->table->ctables->dict)); - ctables_section_recurse_add_empty_categories (s, c, cats, a, 0); + ctables_section_recurse_add_empty_categories (s, cats, c, 0, 0); + case_unref (c); } static bool @@ -3783,6 +3790,7 @@ cmd_ctables (struct lexer *lexer, struct dataset *ds) .n_refs = n_vars, .cats = cat, .n_cats = 1, + .show_empty = true, }; struct ctables_categories **categories = xnmalloc (n_vars, diff --git a/tests/language/stats/ctables.at b/tests/language/stats/ctables.at index f71a3c572e..e72d793fcc 100644 --- a/tests/language/stats/ctables.at +++ b/tests/language/stats/ctables.at @@ -27,7 +27,6 @@ dnl * OTHERNM dnl * String values dnl * Date values dnl * Data-dependent sorting. -dnl - EMPTY=INCLUDE. dnl - TITLES: )DATE, )TIME, )TABLE. dnl - SIGTEST dnl - COMPARETEST @@ -191,6 +190,100 @@ AT_CHECK([pspp ctables.sps -O box=unicode -O width=80], [0], [dnl ]) AT_CLEANUP +AT_SETUP([CTABLES show or hide empty categories]) +AT_CHECK([ln $top_srcdir/examples/nhtsa.sav . || cp $top_srcdir/examples/nhtsa.sav .]) +AT_DATA([ctables.sps], +[[GET 'nhtsa.sav'. +IF (qn105ba = 2) qn105ba = 1. +IF (qns3a = 1) qns3a = 2. +CTABLES /TABLE qn105ba BY qns3a [COLPCT PCT8.0]. +CTABLES /TABLE qn105ba BY qns3a [COLPCT PCT8.0] + /CATEGORIES VAR=qn105ba EMPTY=EXCLUDE. +CTABLES /TABLE qn105ba BY qns3a [COLPCT PCT8.0] + /CATEGORIES VAR=qns3a EMPTY=EXCLUDE. +CTABLES /TABLE qn105ba BY qns3a [COLPCT PCT8.0] + /CATEGORIES VAR=ALL EMPTY=EXCLUDE. +]]) +AT_CHECK([pspp ctables.sps -O box=unicode -O width=80], [0], [dnl + Custom Tables +ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¬ââââââââââââââââ® +â â S3a. GENDER: â +â âââââââââ¬âââââââ⤠+â â Male â Femaleâ +â âââââââââ¼âââââââ⤠+â â Columnâ Columnâ +â â % â % â +ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¼ââââââââ¼âââââââ⤠+â105b. How likely is it that drivers who have had Almost â .â 32%â +âtoo much to drink to drive safely will A. Get certain â â â +âstopped by the police? Very likelyâ .â 0%â +â Somewhat â .â 40%â +â likely â â â +â Somewhat â .â 19%â +â unlikely â â â +â Very â .â 9%â +â unlikely â â â +â°âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ´ââââââââ´âââââââ⯠+ + Custom Tables +ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¬ââââââââââââââââ® +â â S3a. GENDER: â +â âââââââââ¬âââââââ⤠+â â Male â Femaleâ +â âââââââââ¼âââââââ⤠+â â Columnâ Columnâ +â â % â % â +ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¼ââââââââ¼âââââââ⤠+â105b. How likely is it that drivers who have had Almost â .â 32%â +âtoo much to drink to drive safely will A. Get certain â â â +âstopped by the police? Somewhat â .â 40%â +â likely â â â +â Somewhat â .â 19%â +â unlikely â â â +â Very â .â 9%â +â unlikely â â â +â°âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ´ââââââââ´âââââââ⯠+ + Custom Tables +ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¬ââââââââââ® +â â S3a. â +â â GENDER: â +â ââââââââââ⤠+â â Female â +â ââââââââââ⤠+â â Column %â +ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¼âââââââââ⤠+â105b. How likely is it that drivers who have had too Almost â 32%â +âmuch to drink to drive safely will A. Get stopped by certain â â +âthe police? Very likely â 0%â +â Somewhat â 40%â +â likely â â +â Somewhat â 19%â +â unlikely â â +â Very â 9%â +â unlikely â â +â°âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ´âââââââââ⯠+ + Custom Tables +ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¬ââââââââââ® +â â S3a. â +â â GENDER: â +â ââââââââââ⤠+â â Female â +â ââââââââââ⤠+â â Column %â +ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¼âââââââââ⤠+â105b. How likely is it that drivers who have had too Almost â 32%â +âmuch to drink to drive safely will A. Get stopped by certain â â +âthe police? Somewhat â 40%â +â likely â â +â Somewhat â 19%â +â unlikely â â +â Very â 9%â +â unlikely â â +â°âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ´âââââââââ⯠+]) +AT_CLEANUP AT_SETUP([CTABLES simple nesting]) AT_CHECK([ln $top_srcdir/examples/nhtsa.sav . || cp $top_srcdir/examples/nhtsa.sav .]) @@ -402,7 +495,8 @@ AT_DATA([ctables.sps], [[GET 'nhtsa.sav'. CTABLES /TABLE=qnd1 > qn1 BY qns3a. CTABLES /TABLE=qnd1 [MINIMUM, MAXIMUM, MEAN] > qns3a > (qn26 + qn27). -CTABLES /TABLE=qnsa1 > qn105ba [COLPCT] BY qns1. +CTABLES /TABLE=qnsa1 > qn105ba [COLPCT] BY qns1 + /CATEGORIES VAR=qnsa1 EMPTY=EXCLUDE. CTABLES /TABLE=AgeGroup > qn20 [MEAN F8.1, STDDEV F8.1]. ]]) AT_CHECK([pspp ctables.sps -O box=unicode -O width=80], [0], [dnl @@ -452,27 +546,34 @@ AT_CHECK([pspp ctables.sps -O box=unicode -O width=80], [0], [dnl â°ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ´ââââââââ´ââââââââ´ââââ⯠Custom Tables -ââââââââââââââââââââââââââââââââââââ¬ââââââââââââââââââââââââââââââââââââââââââââ® -â âS1. Including yourself, how many members ofâ -â â this household are age 16 or older? â -â ââââââââ¬ââââââââ¬âââââââ¬âââââââ¬ââââââââ¬ââââââ⤠-â â â â â â â 6 or â -â â 1 â 2 â 3 â 4 â 5 â more â -â ââââââââ¼ââââââââ¼âââââââ¼âââââââ¼ââââââââ¼ââââââ⤠-â âColumnâ ColumnâColumnâColumnâ ColumnâColumnâ -â â % â % â % â % â % â % â -ââââââââââââââââââââââââââââââââââââ¼âââââââ¼ââââââââ¼âââââââ¼âââââââ¼ââââââââ¼ââââââ⤠-âSa1. RDD 105b. How Almost â 9.5%â 8.2%â 12.4%â 9.9%â 20.0%â 23.8%â -âSAMPLE likely is it certain â â â â â â â -âSOURCE: that drivers Very â 24.9%â 18.5%â 24.0%â 26.6%â 25.5%â 33.3%â -â who have had likely â â â â â â â -â too much to Somewhatâ 38.3%â 41.9%â 38.6%â 37.5%â 36.4%â 23.8%â -â drink to likely â â â â â â â -â drive safely Somewhatâ 18.1%â 21.7%â 16.8%â 16.7%â 10.9%â 9.5%â -â will A. Get unlikelyâ â â â â â â -â stopped by Very â 9.2%â 9.7%â 8.2%â 9.4%â 7.3%â 9.5%â -â the police? unlikelyâ â â â â â â -â°âââââââââââââââââââââââââââââââââââ´âââââââ´ââââââââ´âââââââ´âââââââ´ââââââââ´ââââââ⯠+âââââââââââââââââââââââââââââââ¬âââââââââââââââââââââââââââââââââââââââââââââââââ® +â âS1. Including yourself, how many members of thisâ +â â household are age 16 or older? â +â ââââââââ¬âââââââ¬âââââââ¬âââââââ¬âââââââ¬âââââââ¬ââââââ⤠+â â â â â â â â 6 or â +â â None â 1 â 2 â 3 â 4 â 5 â more â +â ââââââââ¼âââââââ¼âââââââ¼âââââââ¼âââââââ¼âââââââ¼ââââââ⤠+â âColumnâColumnâColumnâColumnâColumnâColumnâColumnâ +â â % â % â % â % â % â % â % â +âââââââââââââââââââââââââââââââ¼âââââââ¼âââââââ¼âââââââ¼âââââââ¼âââââââ¼âââââââ¼ââââââ⤠+âSa1. RDD 105b. Almost â .â 9.5%â 8.2%â 12.4%â 9.9%â 20.0%â 23.8%â +âSAMPLE How certain â â â â â â â â +âSOURCE: likely â â â â â â â â +â is it Very â .â 24.9%â 18.5%â 24.0%â 26.6%â 25.5%â 33.3%â +â that likely â â â â â â â â +â drivers â â â â â â â â +â who have â â â â â â â â +â had too Somewhatâ .â 38.3%â 41.9%â 38.6%â 37.5%â 36.4%â 23.8%â +â much to likely â â â â â â â â +â drink to â â â â â â â â +â drive â â â â â â â â +â safely Somewhatâ .â 18.1%â 21.7%â 16.8%â 16.7%â 10.9%â 9.5%â +â will A. unlikelyâ â â â â â â â +â Get â â â â â â â â +â stopped Very â .â 9.2%â 9.7%â 8.2%â 9.4%â 7.3%â 9.5%â +â by the unlikelyâ â â â â â â â +â police? â â â â â â â â +â°ââââââââââââââââââââââââââââââ´âââââââ´âââââââ´âââââââ´âââââââ´âââââââ´âââââââ´ââââââ⯠Custom Tables ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¬âââââ¬âââââââââââ® @@ -657,26 +758,28 @@ AT_CHECK([pspp ctables.sps -O box=unicode -O width=120], [0], [dnl â°ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ´ââââââââ´ââââââââ´ââââââââââ´ââââââââ´âââââââââ´âââââââ´ââââââââââ⯠Custom Tables -ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¬ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ® -â â S1. Including yourself, how many members of this â -â â household are age 16 or older? â -â âââââââââââ¬âââââââââ¬ââââââââââ¬âââââââââ¬ââââââââââ¬âââââââââ⤠-â â 1 â 2 â 3 â 4 â 5 â6 or moreâ -â âââââââââââ¼âââââââââ¼ââââââââââ¼âââââââââ¼ââââââââââ¼âââââââââ⤠-â â Column %âColumn %â Column %âColumn %â Column %â Column %â -ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¼ââââââââââ¼âââââââââ¼ââââââââââ¼âââââââââ¼ââââââââââ¼âââââââââ⤠-â105b. How likely is it that drivers who have had Almost â 9.5%â 8.2%â 12.4%â 9.9%â 20.0%â 23.8%â -âtoo much to drink to drive safely will A. Get certain â â â â â â â -âstopped by the police? Very likelyâ 24.9%â 18.5%â 24.0%â 26.6%â 25.5%â 33.3%â -â Somewhat â 38.3%â 41.9%â 38.6%â 37.5%â 36.4%â 23.8%â -â likely â â â â â â â -â Subtotal â 72.8%â 68.6%â 75.0%â 74.0%â 81.8%â 81.0%â -â Somewhat â 18.1%â 21.7%â 16.8%â 16.7%â 10.9%â 9.5%â -â unlikely â â â â â â â -â Very â 9.2%â 9.7%â 8.2%â 9.4%â 7.3%â 9.5%â -â unlikely â â â â â â â -â Subtotal â 27.2%â 31.4%â 25.0%â 26.0%â 18.2%â 19.0%â -â°âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ´ââââââââââ´âââââââââ´ââââââââââ´âââââââââ´ââââââââââ´âââââââââ⯠+ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¬ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ® +â â S1. Including yourself, how many members of this household â +â â are age 16 or older? â +â ââââââââââ¬âââââââââ¬âââââââââ¬âââââââââ¬ââââââââ¬âââââââââ¬ââââââââ⤠+â â â â â â â â 6 or â +â â None â 1 â 2 â 3 â 4 â 5 â more â +â ââââââââââ¼âââââââââ¼âââââââââ¼âââââââââ¼ââââââââ¼âââââââââ¼ââââââââ⤠+â â â â â â Columnâ â â +â âColumn %âColumn %âColumn %âColumn %â % âColumn %âColumn %â +ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¼âââââââââ¼âââââââââ¼âââââââââ¼âââââââââ¼ââââââââ¼âââââââââ¼ââââââââ⤠+â105b. How likely is it that drivers who have Almost â .â 9.5%â 8.2%â 12.4%â 9.9%â 20.0%â 23.8%â +âhad too much to drink to drive safely will certain â â â â â â â â +âA. Get stopped by the police? Very likelyâ .â 24.9%â 18.5%â 24.0%â 26.6%â 25.5%â 33.3%â +â Somewhat â .â 38.3%â 41.9%â 38.6%â 37.5%â 36.4%â 23.8%â +â likely â â â â â â â â +â Subtotal â â 72.8%â 68.6%â 75.0%â 74.0%â 81.8%â 81.0%â +â Somewhat â .â 18.1%â 21.7%â 16.8%â 16.7%â 10.9%â 9.5%â +â unlikely â â â â â â â â +â Very â .â 9.2%â 9.7%â 8.2%â 9.4%â 7.3%â 9.5%â +â unlikely â â â â â â â â +â Subtotal â â 27.2%â 31.4%â 25.0%â 26.0%â 18.2%â 19.0%â +â°âââââââââââââââââââââââââââââââââââââââââââââââââââââââââ´âââââââââ´âââââââââ´âââââââââ´âââââââââ´ââââââââ´âââââââââ´ââââââââ⯠Custom Tables âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¬âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ® -- 2.30.2