1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2017 Free Software Foundation, Inc.
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.
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.
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/>. */
19 #include "data/case.h"
20 #include "data/casereader.h"
21 #include "data/casewriter.h"
22 #include "data/dataset.h"
23 #include "data/dictionary.h"
24 #include "data/format.h"
25 #include "data/transformations.h"
26 #include "data/variable.h"
27 #include "language/command.h"
28 #include "language/data-io/data-parser.h"
29 #include "language/data-io/data-reader.h"
30 #include "language/data-io/file-handle.h"
31 #include "language/data-io/inpt-pgm.h"
32 #include "language/data-io/placement-parser.h"
33 #include "language/lexer/lexer.h"
34 #include "language/lexer/variable-parser.h"
35 #include "libpspp/i18n.h"
36 #include "libpspp/message.h"
37 #include "libpspp/misc.h"
40 #include "gl/xalloc.h"
43 #define _(msgid) gettext (msgid)
45 /* DATA LIST transformation data. */
48 struct data_parser *parser; /* Parser. */
49 struct dfm_reader *reader; /* Data file reader. */
50 struct variable *end; /* Variable specified on END subcommand. */
53 static trns_free_func data_list_trns_free;
54 static trns_proc_func data_list_trns_proc;
69 static const int ROWTYPE_WIDTH = 8;
73 enum triangle triangle;
74 enum diagonal diagonal;
75 const struct variable *rowtype;
76 const struct variable *varname;
77 int n_continuous_vars;
78 struct variable **split_vars;
83 valid rowtype_ values:
100 /* Sets the value of OUTCASE which corresponds to VNAME
101 to the value STR. VNAME must be of type string.
104 set_varname_column (struct ccase *outcase, const struct variable *vname,
107 int len = var_get_width (vname);
108 uint8_t *s = value_str_rw (case_data_rw (outcase, vname), len);
110 strncpy ((char *) s, str, len);
114 blank_varname_column (struct ccase *outcase, const struct variable *vname)
116 int len = var_get_width (vname);
117 uint8_t *s = value_str_rw (case_data_rw (outcase, vname), len);
119 memset (s, ' ', len);
122 static struct casereader *
123 preprocess (struct casereader *casereader0, const struct dictionary *dict, void *aux)
125 struct matrix_format *mformat = aux;
126 const struct caseproto *proto = casereader_get_proto (casereader0);
127 struct casewriter *writer;
128 writer = autopaging_writer_create (proto);
130 double **matrices = NULL;
133 const size_t sizeof_matrix =
134 sizeof (double) * mformat->n_continuous_vars * mformat->n_continuous_vars;
137 /* Make an initial pass to populate our temporary matrix */
138 struct casereader *pass0 = casereader_clone (casereader0);
140 unsigned int prev_split_hash = 1;
141 int row = (mformat->triangle == LOWER && mformat->diagonal == NO_DIAGONAL) ? 1 : 0;
142 for (; (c = casereader_read (pass0)) != NULL; case_unref (c))
145 unsigned int split_hash = 0;
146 for (s = 0; s < mformat->n_split_vars; ++s)
148 const struct variable *svar = mformat->split_vars[s];
149 const union value *sv = case_data (c, svar);
150 split_hash = value_hash (sv, var_get_width (svar), split_hash);
153 if (matrices == NULL || prev_split_hash != split_hash)
155 row = (mformat->triangle == LOWER && mformat->diagonal == NO_DIAGONAL) ?
159 matrices = xrealloc (matrices, sizeof (double*) * n_splits);
160 matrices[n_splits - 1] = xmalloc (sizeof_matrix);
163 prev_split_hash = split_hash;
165 int c_offset = (mformat->triangle == UPPER) ? row : 0;
166 if (mformat->triangle == UPPER && mformat->diagonal == NO_DIAGONAL)
168 const union value *v = case_data (c, mformat->rowtype);
169 const char *val = (const char *) value_str (v, ROWTYPE_WIDTH);
170 if (0 == strncasecmp (val, "corr ", ROWTYPE_WIDTH) ||
171 0 == strncasecmp (val, "cov ", ROWTYPE_WIDTH))
174 for (col = c_offset; col < mformat->n_continuous_vars; ++col)
176 const struct variable *var =
179 var_get_dict_index (mformat->varname));
181 double e = case_data (c, var)->f;
186 (matrices[n_splits-1])[col + mformat->n_continuous_vars * row] = e;
187 (matrices[n_splits-1]) [row + mformat->n_continuous_vars * col] = e;
192 casereader_destroy (pass0);
194 /* Now make a second pass to fill in the other triangle from our
196 const int idx = var_get_dict_index (mformat->varname);
198 struct ccase *prev_case = NULL;
201 for (; (c = casereader_read (casereader0)) != NULL; prev_case = c)
204 unsigned int split_hash = 0;
205 for (s = 0; s < mformat->n_split_vars; ++s)
207 const struct variable *svar = mformat->split_vars[s];
208 const union value *sv = case_data (c, svar);
209 split_hash = value_hash (sv, var_get_width (svar), split_hash);
211 if (prev_split_hash != split_hash)
217 prev_split_hash = split_hash;
219 case_unref (prev_case);
220 struct ccase *outcase = case_create (proto);
221 case_copy (outcase, 0, c, 0, caseproto_get_n_widths (proto));
222 const union value *v = case_data (c, mformat->rowtype);
223 const char *val = (const char *) value_str (v, ROWTYPE_WIDTH);
224 if (0 == strncasecmp (val, "corr ", ROWTYPE_WIDTH) ||
225 0 == strncasecmp (val, "cov ", ROWTYPE_WIDTH))
228 const struct variable *var = dict_get_var (dict, idx + 1 + row);
229 set_varname_column (outcase, mformat->varname, var_get_name (var));
230 value_copy (case_data_rw (outcase, mformat->rowtype), v, ROWTYPE_WIDTH);
232 for (col = 0; col < mformat->n_continuous_vars; ++col)
234 union value *dest_val =
235 case_data_rw_idx (outcase,
236 1 + col + var_get_dict_index (mformat->varname));
237 dest_val->f = (matrices[n_splits - 1])[col + mformat->n_continuous_vars * row];
238 if (col == row && mformat->diagonal == NO_DIAGONAL)
245 blank_varname_column (outcase, mformat->varname);
248 /* Special case for SD and N_VECTOR: Rewrite as STDDEV and N respectively */
249 if (0 == strncasecmp (val, "sd ", ROWTYPE_WIDTH))
251 value_copy_buf_rpad (case_data_rw (outcase, mformat->rowtype), ROWTYPE_WIDTH,
252 (uint8_t *) "STDDEV", 6, ' ');
254 else if (0 == strncasecmp (val, "n_vector", ROWTYPE_WIDTH))
256 value_copy_buf_rpad (case_data_rw (outcase, mformat->rowtype), ROWTYPE_WIDTH,
257 (uint8_t *) "N", 1, ' ');
260 casewriter_write (writer, outcase);
263 /* If NODIAGONAL is specified, then a final case must be written */
264 if (mformat->diagonal == NO_DIAGONAL)
267 struct ccase *outcase = case_create (proto);
270 case_copy (outcase, 0, prev_case, 0, caseproto_get_n_widths (proto));
273 const struct variable *var = dict_get_var (dict, idx + 1 + row);
274 set_varname_column (outcase, mformat->varname, var_get_name (var));
276 for (col = 0; col < mformat->n_continuous_vars; ++col)
278 union value *dest_val =
279 case_data_rw_idx (outcase, 1 + col +
280 var_get_dict_index (mformat->varname));
281 dest_val->f = (matrices[n_splits - 1]) [col + mformat->n_continuous_vars * row];
282 if (col == row && mformat->diagonal == NO_DIAGONAL)
286 casewriter_write (writer, outcase);
290 case_unref (prev_case);
293 for (i = 0 ; i < n_splits; ++i)
296 struct casereader *reader1 = casewriter_make_reader (writer);
297 casereader_destroy (casereader0);
302 cmd_matrix (struct lexer *lexer, struct dataset *ds)
304 struct dictionary *dict;
305 struct data_parser *parser;
306 struct dfm_reader *reader;
307 struct file_handle *fh = NULL;
308 char *encoding = NULL;
309 struct matrix_format mformat;
314 mformat.triangle = LOWER;
315 mformat.diagonal = DIAGONAL;
316 mformat.n_split_vars = 0;
317 mformat.split_vars = NULL;
319 dict = (in_input_program ()
321 : dict_create (get_default_encoding ()));
322 parser = data_parser_create (dict);
325 data_parser_set_type (parser, DP_DELIMITED);
326 data_parser_set_warn_missing_fields (parser, false);
327 data_parser_set_span (parser, false);
329 mformat.rowtype = dict_create_var (dict, "ROWTYPE_", ROWTYPE_WIDTH);
331 mformat.n_continuous_vars = 0;
332 mformat.n_split_vars = 0;
334 if (! lex_force_match_id (lexer, "VARIABLES"))
337 lex_match (lexer, T_EQUALS);
339 if (! parse_mixed_vars (lexer, dict, &names, &n_names, PV_NO_DUPLICATE))
342 for (i = 0; i < n_names; ++i)
348 int longest_name = 0;
349 for (i = 0; i < n_names; ++i)
351 maximize_int (&longest_name, strlen (names[i]));
354 mformat.varname = dict_create_var (dict, "VARNAME_",
355 8 * DIV_RND_UP (longest_name, 8));
357 for (i = 0; i < n_names; ++i)
359 if (0 == strcasecmp (names[i], "ROWTYPE_"))
361 const struct fmt_spec fmt = fmt_for_input (FMT_A, 8, 0);
362 data_parser_add_delimited_field (parser,
364 var_get_case_index (mformat.rowtype),
369 const struct fmt_spec fmt = fmt_for_input (FMT_F, 10, 4);
370 struct variable *v = dict_create_var (dict, names[i], 0);
371 var_set_both_formats (v, &fmt);
372 data_parser_add_delimited_field (parser,
374 var_get_case_index (mformat.varname) +
375 ++mformat.n_continuous_vars,
379 for (i = 0; i < n_names; ++i)
383 while (lex_token (lexer) != T_ENDCMD)
385 if (! lex_force_match (lexer, T_SLASH))
388 if (lex_match_id (lexer, "FORMAT"))
390 lex_match (lexer, T_EQUALS);
392 while (lex_token (lexer) != T_SLASH && (lex_token (lexer) != T_ENDCMD))
394 if (lex_match_id (lexer, "LIST"))
396 data_parser_set_span (parser, false);
398 else if (lex_match_id (lexer, "FREE"))
400 data_parser_set_span (parser, true);
402 else if (lex_match_id (lexer, "UPPER"))
404 mformat.triangle = UPPER;
406 else if (lex_match_id (lexer, "LOWER"))
408 mformat.triangle = LOWER;
410 else if (lex_match_id (lexer, "FULL"))
412 mformat.triangle = FULL;
414 else if (lex_match_id (lexer, "DIAGONAL"))
416 mformat.diagonal = DIAGONAL;
418 else if (lex_match_id (lexer, "NODIAGONAL"))
420 mformat.diagonal = NO_DIAGONAL;
424 lex_error (lexer, NULL);
429 else if (lex_match_id (lexer, "FILE"))
431 lex_match (lexer, T_EQUALS);
433 fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE, NULL);
437 else if (lex_match_id (lexer, "SPLIT"))
439 lex_match (lexer, T_EQUALS);
440 if (! parse_variables (lexer, dict, &mformat.split_vars, &mformat.n_split_vars, 0))
442 free (mformat.split_vars);
446 for (i = 0; i < mformat.n_split_vars; ++i)
448 const struct fmt_spec fmt = fmt_for_input (FMT_F, 4, 0);
449 var_set_both_formats (mformat.split_vars[i], &fmt);
451 dict_reorder_vars (dict, mformat.split_vars, mformat.n_split_vars);
452 mformat.n_continuous_vars -= mformat.n_split_vars;
456 lex_error (lexer, NULL);
461 if (mformat.diagonal == NO_DIAGONAL && mformat.triangle == FULL)
463 msg (SE, _("FORMAT = FULL and FORMAT = NODIAGONAL are mutually exclusive."));
468 fh = fh_inline_file ();
469 fh_set_default_handle (fh);
471 if (!data_parser_any_fields (parser))
473 msg (SE, _("At least one variable must be specified."));
477 if (lex_end_of_command (lexer) != CMD_SUCCESS)
480 reader = dfm_open_reader (fh, lexer, encoding);
484 if (in_input_program ())
486 struct data_list_trns *trns = xmalloc (sizeof *trns);
487 trns->parser = parser;
488 trns->reader = reader;
490 add_transformation (ds, data_list_trns_proc, data_list_trns_free, trns);
494 data_parser_make_active_file (parser, ds, reader, dict, preprocess, &mformat);
499 free (mformat.split_vars);
501 return CMD_DATA_LIST;
504 data_parser_destroy (parser);
505 if (!in_input_program ())
509 free (mformat.split_vars);
510 return CMD_CASCADING_FAILURE;
514 /* Input procedure. */
516 /* Destroys DATA LIST transformation TRNS.
517 Returns true if successful, false if an I/O error occurred. */
519 data_list_trns_free (void *trns_)
521 struct data_list_trns *trns = trns_;
522 data_parser_destroy (trns->parser);
523 dfm_close_reader (trns->reader);
528 /* Handle DATA LIST transformation TRNS, parsing data into *C. */
530 data_list_trns_proc (void *trns_, struct ccase **c, casenumber case_num UNUSED)
532 struct data_list_trns *trns = trns_;
535 *c = case_unshare (*c);
536 if (data_parser_parse (trns->parser, trns->reader, *c))
537 retval = TRNS_CONTINUE;
538 else if (dfm_reader_error (trns->reader) || dfm_eof (trns->reader) > 1)
540 /* An I/O error, or encountering end of file for a second
541 time, should be escalated into a more serious error. */
545 retval = TRNS_END_FILE;
547 /* If there was an END subcommand handle it. */
548 if (trns->end != NULL)
550 double *end = &case_data_rw (*c, trns->end)->f;
551 if (retval == TRNS_END_FILE)
554 retval = TRNS_CONTINUE;