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"
39 #include "gl/xalloc.h"
42 #define _(msgid) gettext (msgid)
44 /* DATA LIST transformation data. */
47 struct data_parser *parser; /* Parser. */
48 struct dfm_reader *reader; /* Data file reader. */
49 struct variable *end; /* Variable specified on END subcommand. */
52 static trns_free_func data_list_trns_free;
53 static trns_proc_func data_list_trns_proc;
70 enum triangle triangle;
71 enum diagonal diagonal;
72 const struct variable *rowtype;
73 const struct variable *varname;
74 int n_continuous_vars;
75 struct variable **split_vars;
80 valid rowtype_ values:
97 /* Sets the value of OUTCASE which corresponds to MFORMAT's varname variable
98 to the string STR. VAR must be of type string.
101 set_varname_column (struct ccase *outcase, const struct matrix_format *mformat,
102 const char *str, int len)
104 const struct variable *var = mformat->varname;
105 uint8_t *s = value_str_rw (case_data_rw (outcase, var), len);
107 strncpy ((char *) s, str, len);
111 static struct casereader *
112 preprocess (struct casereader *casereader0, const struct dictionary *dict, void *aux)
114 struct matrix_format *mformat = aux;
115 const struct caseproto *proto = casereader_get_proto (casereader0);
116 struct casewriter *writer;
117 writer = autopaging_writer_create (proto);
119 double **matrices = NULL;
122 const size_t sizeof_matrix =
123 sizeof (double) * mformat->n_continuous_vars * mformat->n_continuous_vars;
126 /* Make an initial pass to populate our temporary matrix */
127 struct casereader *pass0 = casereader_clone (casereader0);
129 unsigned int prev_split_hash = 1;
130 int row = (mformat->triangle == LOWER && mformat->diagonal == NO_DIAGONAL) ? 1 : 0;
131 for (; (c = casereader_read (pass0)) != NULL; case_unref (c))
134 unsigned int split_hash = 0;
135 for (s = 0; s < mformat->n_split_vars; ++s)
137 const struct variable *svar = mformat->split_vars[s];
138 const union value *sv = case_data (c, svar);
139 split_hash = value_hash (sv, var_get_width (svar), split_hash);
142 if (matrices == NULL || prev_split_hash != split_hash)
144 row = (mformat->triangle == LOWER && mformat->diagonal == NO_DIAGONAL) ?
148 matrices = xrealloc (matrices, sizeof (double*) * n_splits);
149 matrices[n_splits - 1] = xmalloc (sizeof_matrix);
152 prev_split_hash = split_hash;
154 int c_offset = (mformat->triangle == UPPER) ? row : 0;
155 if (mformat->triangle == UPPER && mformat->diagonal == NO_DIAGONAL)
157 const union value *v = case_data (c, mformat->rowtype);
158 const char *val = (const char *) value_str (v, 8);
159 if (0 == strncasecmp (val, "corr ", 8) ||
160 0 == strncasecmp (val, "cov ", 8))
163 for (col = c_offset; col < mformat->n_continuous_vars; ++col)
165 const struct variable *var =
168 var_get_dict_index (mformat->varname));
170 double e = case_data (c, var)->f;
175 (matrices[n_splits-1])[col + mformat->n_continuous_vars * row] = e;
176 (matrices[n_splits-1]) [row + mformat->n_continuous_vars * col] = e;
181 casereader_destroy (pass0);
183 /* Now make a second pass to fill in the other triangle from our
185 const int idx = var_get_dict_index (mformat->varname);
187 struct ccase *prev_case = NULL;
190 for (; (c = casereader_read (casereader0)) != NULL; prev_case = c)
193 unsigned int split_hash = 0;
194 for (s = 0; s < mformat->n_split_vars; ++s)
196 const struct variable *svar = mformat->split_vars[s];
197 const union value *sv = case_data (c, svar);
198 split_hash = value_hash (sv, var_get_width (svar), split_hash);
200 if (prev_split_hash != split_hash)
206 prev_split_hash = split_hash;
208 case_unref (prev_case);
209 struct ccase *outcase = case_create (proto);
210 case_copy (outcase, 0, c, 0, caseproto_get_n_widths (proto));
211 const union value *v = case_data (c, mformat->rowtype);
212 const char *val = (const char *) value_str (v, 8);
213 if (0 == strncasecmp (val, "corr ", 8) ||
214 0 == strncasecmp (val, "cov ", 8))
217 const struct variable *var = dict_get_var (dict, idx + 1 + row);
218 set_varname_column (outcase, mformat, var_get_name (var), 8);
219 value_copy (case_data_rw (outcase, mformat->rowtype), v, 8);
221 for (col = 0; col < mformat->n_continuous_vars; ++col)
223 union value *dest_val =
224 case_data_rw_idx (outcase,
225 1 + col + var_get_dict_index (mformat->varname));
226 dest_val->f = (matrices[n_splits - 1])[col + mformat->n_continuous_vars * row];
227 if (col == row && mformat->diagonal == NO_DIAGONAL)
234 set_varname_column (outcase, mformat, " ", 8);
237 /* Special case for SD and N_VECTOR: Rewrite as STDDEV and N respectively */
238 if (0 == strncasecmp (val, "sd ", 8))
240 value_copy_buf_rpad (case_data_rw (outcase, mformat->rowtype), 8,
241 (uint8_t *) "STDDEV", 6, ' ');
243 else if (0 == strncasecmp (val, "n_vector", 8))
245 value_copy_buf_rpad (case_data_rw (outcase, mformat->rowtype), 8,
246 (uint8_t *) "N", 1, ' ');
249 casewriter_write (writer, outcase);
252 /* If NODIAGONAL is specified, then a final case must be written */
253 if (mformat->diagonal == NO_DIAGONAL)
256 struct ccase *outcase = case_create (proto);
259 case_copy (outcase, 0, prev_case, 0, caseproto_get_n_widths (proto));
262 const struct variable *var = dict_get_var (dict, idx + 1 + row);
263 set_varname_column (outcase, mformat, var_get_name (var), 8);
265 for (col = 0; col < mformat->n_continuous_vars; ++col)
267 union value *dest_val =
268 case_data_rw_idx (outcase, 1 + col +
269 var_get_dict_index (mformat->varname));
270 dest_val->f = (matrices[n_splits - 1]) [col + mformat->n_continuous_vars * row];
271 if (col == row && mformat->diagonal == NO_DIAGONAL)
275 casewriter_write (writer, outcase);
279 case_unref (prev_case);
282 for (i = 0 ; i < n_splits; ++i)
285 struct casereader *reader1 = casewriter_make_reader (writer);
286 casereader_destroy (casereader0);
291 cmd_matrix (struct lexer *lexer, struct dataset *ds)
293 struct dictionary *dict;
294 struct data_parser *parser;
295 struct dfm_reader *reader;
296 struct file_handle *fh = NULL;
297 char *encoding = NULL;
298 struct matrix_format mformat;
303 mformat.triangle = LOWER;
304 mformat.diagonal = DIAGONAL;
306 dict = (in_input_program ()
308 : dict_create (get_default_encoding ()));
309 parser = data_parser_create (dict);
312 data_parser_set_type (parser, DP_DELIMITED);
313 data_parser_set_warn_missing_fields (parser, false);
314 data_parser_set_span (parser, false);
316 mformat.rowtype = dict_create_var (dict, "ROWTYPE_", 8);
317 mformat.varname = dict_create_var (dict, "VARNAME_", 8);
319 mformat.n_continuous_vars = 0;
320 mformat.n_split_vars = 0;
322 if (! lex_force_match_id (lexer, "VARIABLES"))
325 lex_match (lexer, T_EQUALS);
327 if (! parse_mixed_vars (lexer, dict, &names, &n_names, 0))
330 for (i = 0; i < n_names; ++i)
336 for (i = 0; i < n_names; ++i)
338 if (0 == strcasecmp (names[i], "ROWTYPE_"))
340 const struct fmt_spec fmt = fmt_for_input (FMT_A, 8, 0);
341 data_parser_add_delimited_field (parser,
343 var_get_case_index (mformat.rowtype),
348 const struct fmt_spec fmt = fmt_for_input (FMT_F, 10, 4);
349 struct variable *v = dict_create_var (dict, names[i], 0);
350 var_set_both_formats (v, &fmt);
351 data_parser_add_delimited_field (parser,
353 var_get_case_index (mformat.varname) +
354 ++mformat.n_continuous_vars,
358 for (i = 0; i < n_names; ++i)
362 while (lex_token (lexer) != T_ENDCMD)
364 if (! lex_force_match (lexer, T_SLASH))
367 if (lex_match_id (lexer, "FORMAT"))
369 lex_match (lexer, T_EQUALS);
371 while (lex_token (lexer) != T_SLASH && (lex_token (lexer) != T_ENDCMD))
373 if (lex_match_id (lexer, "LIST"))
375 data_parser_set_span (parser, false);
377 else if (lex_match_id (lexer, "FREE"))
379 data_parser_set_span (parser, true);
381 else if (lex_match_id (lexer, "UPPER"))
383 mformat.triangle = UPPER;
385 else if (lex_match_id (lexer, "LOWER"))
387 mformat.triangle = LOWER;
389 else if (lex_match_id (lexer, "FULL"))
391 mformat.triangle = FULL;
393 else if (lex_match_id (lexer, "DIAGONAL"))
395 mformat.diagonal = DIAGONAL;
397 else if (lex_match_id (lexer, "NODIAGONAL"))
399 mformat.diagonal = NO_DIAGONAL;
403 lex_error (lexer, NULL);
408 else if (lex_match_id (lexer, "FILE"))
410 lex_match (lexer, T_EQUALS);
412 fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE, NULL);
416 else if (lex_match_id (lexer, "SPLIT"))
418 lex_match (lexer, T_EQUALS);
419 if (! parse_variables (lexer, dict, &mformat.split_vars, &mformat.n_split_vars, 0))
421 free (mformat.split_vars);
425 for (i = 0; i < mformat.n_split_vars; ++i)
427 const struct fmt_spec fmt = fmt_for_input (FMT_F, 4, 0);
428 var_set_both_formats (mformat.split_vars[i], &fmt);
430 dict_reorder_vars (dict, mformat.split_vars, mformat.n_split_vars);
431 mformat.n_continuous_vars -= mformat.n_split_vars;
435 lex_error (lexer, NULL);
440 if (mformat.diagonal == NO_DIAGONAL && mformat.triangle == FULL)
442 msg (SE, _("FORMAT = FULL and FORMAT = NODIAGONAL are mutually exclusive."));
447 fh = fh_inline_file ();
448 fh_set_default_handle (fh);
450 if (!data_parser_any_fields (parser))
452 msg (SE, _("At least one variable must be specified."));
456 if (lex_end_of_command (lexer) != CMD_SUCCESS)
459 reader = dfm_open_reader (fh, lexer, encoding);
463 if (in_input_program ())
465 struct data_list_trns *trns = xmalloc (sizeof *trns);
466 trns->parser = parser;
467 trns->reader = reader;
469 add_transformation (ds, data_list_trns_proc, data_list_trns_free, trns);
473 data_parser_make_active_file (parser, ds, reader, dict, preprocess, &mformat);
479 return CMD_DATA_LIST;
482 data_parser_destroy (parser);
483 if (!in_input_program ())
487 return CMD_CASCADING_FAILURE;
491 /* Input procedure. */
493 /* Destroys DATA LIST transformation TRNS.
494 Returns true if successful, false if an I/O error occurred. */
496 data_list_trns_free (void *trns_)
498 struct data_list_trns *trns = trns_;
499 data_parser_destroy (trns->parser);
500 dfm_close_reader (trns->reader);
505 /* Handle DATA LIST transformation TRNS, parsing data into *C. */
507 data_list_trns_proc (void *trns_, struct ccase **c, casenumber case_num UNUSED)
509 struct data_list_trns *trns = trns_;
512 *c = case_unshare (*c);
513 if (data_parser_parse (trns->parser, trns->reader, *c))
514 retval = TRNS_CONTINUE;
515 else if (dfm_reader_error (trns->reader) || dfm_eof (trns->reader) > 1)
517 /* An I/O error, or encountering end of file for a second
518 time, should be escalated into a more serious error. */
522 retval = TRNS_END_FILE;
524 /* If there was an END subcommand handle it. */
525 if (trns->end != NULL)
527 double *end = &case_data_rw (*c, trns->end)->f;
528 if (retval == TRNS_END_FILE)
531 retval = TRNS_CONTINUE;