print.h 15.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/* Spell-QTL  Software suite for the QTL analysis of modern datasets.
 * Copyright (C) 2016,2017  Damien Leroux <damien.leroux@inra.fr>, Sylvain Jasson <sylvain.jasson@inra.fr>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

18
19
20
21
22
23
24
25
26
#ifndef _SPELL_MODEL_PRINT_H_
#define _SPELL_MODEL_PRINT_H_

#include <iostream>
#include <iomanip>
#include "eigen.h"
#include "error.h"
#include "labelled_matrix.h"

27
28
29
30
31
32
33
34
35

inline
std::ostream& operator << (std::ostream& os, const std::vector<char>& v)
{
    for (char c: v) { os << c; }
    return os;
}


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
namespace model_print {
    extern int precision;
    extern std::string field_sep;
    extern std::string section_hsep;
    extern char section_vsep;
    extern std::string section_csep;

inline
    std::ostream&
    fill(std::ostream& os, char c, size_t width)
    {
        char f = os.fill();
        os.fill(c);
        os << std::setw(width) << "";
        os.fill(f);
        return os;
    }


template <typename T>
56
57
58
    std::string to_string(const T& x) { std::stringstream s; s << x; return s.str(); }
inline
    std::string to_string(const std::vector<char>& x) { return std::string(x.begin(), x.end()); }
59
60
61
62
63
64
65
66
67
68
inline
    std::string to_string(const std::string& s) { return s; }
inline
    std::string to_string(double x) { std::stringstream s; s << std::setprecision(precision) << x; return s.str(); }


template <typename _Printable>
    std::ostream&
    center(std::ostream& os, const _Printable& s, size_t width)
    {
69
70
        std::string str = to_string(s);
        size_t pad = width - str.size();
71
72
73
74
75
76
        size_t w1 = pad >> 1;
        size_t w2 = pad - w1;
        /*
        char f = os.fill();
        return os << std::setfill('_') << std::setw(w1) << "" << s << std::setw(w2) << "" << std::setfill(f);
        /*/
77
        return os << std::setw(w1) << "" << str << std::setw(w2) << "";
78
79
80
81
82
83
84
85
86
87
88
89
        //*/
    }


template <typename _Printable>
    std::ostream&
    right(std::ostream& os, const _Printable& s, size_t width)
    {
        /*
        char f = os.fill();
        return os << std::setfill('_') << std::setw(width) << std::right << s << std::setfill(f);
        /*/
90
        return os << std::setw(width) << std::right << to_string(s);
91
92
93
94
95
96
97
98
        //*/
    }


template <typename _Printable>
    std::ostream&
    left(std::ostream& os, const _Printable& s, size_t width)
    {
99
        return os << std::setw(width) << std::left << to_string(s);
100
101
102
103
104
105
106
107
108
109
110
111
112
    }


enum Align { Start, Center, End };
enum Orientation { Horizontal, Vertical };

template <typename T>
    size_t width(T x) { return to_string(x).size(); }


template <typename FieldLabel>
struct with_field_labels {
    static const bool has_field_labels = true;
113
    std::vector<FieldLabel> m_field_labels;
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155

    with_field_labels(const std::vector<FieldLabel>& fl)
        : m_field_labels(fl)
    {}

    size_t n_fields() const { return m_field_labels.size(); }
    std::vector<size_t> field_label_widths() const
    {
        std::vector<size_t> ret;
        ret.reserve(m_field_labels.size());
        for (const FieldLabel& f: m_field_labels) {
            ret.push_back(to_string(f).size());
        }
        return ret;
    }

    const std::vector<FieldLabel>& field_labels() const { return m_field_labels; }
};

template <>
struct with_field_labels<void> {
    static const bool has_field_labels = false;
    size_t m_n_fields;

    with_field_labels(size_t n)
        : m_n_fields(n)
    {}

    size_t n_fields() const { return m_n_fields; }
    std::vector<size_t> field_label_widths() const
    {
        std::vector<size_t> ret;
        ret.resize(m_n_fields, 0);
        return ret;
    }

    std::vector<int> field_labels() const { return {}; }
};

template <typename SectionLabel>
struct with_section_label {
    static const bool has_section_label = true;
156
    SectionLabel m_label;
157
158
159
160
161

    with_section_label(const SectionLabel& sl) : m_label(sl) {}
    size_t section_label_width() const
    {
        size_t ret = to_string(m_label).size();
162
        /*MSG_DEBUG("label_width(" << to_string(m_label) << ") = " << ret);*/
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
        return ret;
    }
};

template <>
struct with_section_label<void> {
    static const bool has_section_label = false;

    with_section_label() {}
    size_t section_label_width() const { return 0; }
};


template <typename SectionLabel, typename FieldLabel>
struct section
        : public with_section_label<SectionLabel>,
          public with_field_labels<FieldLabel> {
    section(const SectionLabel& sl, const std::vector<FieldLabel>& fl)
        : with_section_label<SectionLabel>(sl), with_field_labels<FieldLabel>(fl)
    {}
183
184
185
186

    using with_section_label<SectionLabel>::m_label;

    const SectionLabel& label() const { return m_label; }
187
188
189
190
191
192
193
194
195
};

template <typename FieldLabel>
struct section<void, FieldLabel>
        : public with_section_label<void>,
          public with_field_labels<FieldLabel> {
    section(const std::vector<FieldLabel>& fl)
        : with_section_label<void>(), with_field_labels<FieldLabel>(fl)
    {}
196
197

    const std::string& label() const { static std::string _; return _; }
198
199
200
201
202
203
204
205
206
};

template <typename SectionLabel>
struct section<SectionLabel, void>
        : public with_section_label<SectionLabel>,
          public with_field_labels<void> {
    section(const SectionLabel& sl, size_t nf)
        : with_section_label<SectionLabel>(sl), with_field_labels<void>(nf)
    {}
207
208
209
210

    using with_section_label<SectionLabel>::m_label;

    const SectionLabel& label() const { return m_label; }
211
212
213
214
215
216
217
218
219
220
221
222
223
};


struct section_size {
    size_t label_length;
    size_t size;

    section_size() : label_length(0), size(0) {}
    size_t max_size() const { return size > label_length ? size : label_length; }
    size_t padding_before() const { return (max_size() - size) >> 1; }
    size_t padding_after() const { return max_size() - size - padding_before(); }
};

224
template <typename RS, typename RF, typename CS, typename CF, typename MatrixType=Eigen::MatrixXd>
225
226
227
228
229
230
231
struct matrix_with_sections {
    typedef section<RS, RF> row_section_type;
    typedef section<CS, CF> column_section_type;
    static const int has_column_field_labels = column_section_type::has_field_labels;
    static const int has_column_section_label = column_section_type::has_section_label;
    static const int has_row_field_labels = row_section_type::has_field_labels;
    static const int has_row_section_label = row_section_type::has_section_label;
232
    typedef matrix_with_sections<RS, RF, CS, CF, MatrixType> this_type;
233
234
    std::vector<row_section_type> m_row_sections;
    std::vector<column_section_type> m_column_sections;
Damien Leroux's avatar
Damien Leroux committed
235
    /*const Eigen::MatrixXd& m_matrix;*/
236
    MatrixType m_matrix;
237

238
    matrix_with_sections(const MatrixType& m) : m_row_sections(), m_column_sections(), m_matrix(m) {}
239
    template <typename S, typename F>
240
        this_type&
241
242
        add_row_section(const S& s, const std::vector<F>& f) { m_row_sections.emplace_back(s, f); return *this; }
    template <typename SF>
243
        this_type&
244
245
        add_row_section(const SF& sf) { m_row_sections.emplace_back(sf); return *this; }
    template <typename SF>
246
        this_type&
247
248
        add_row_section(const SF& sf, size_t n) { m_row_sections.emplace_back(sf, n); return *this; }
    template <typename S, typename F>
249
        this_type&
250
251
        add_column_section(const S& s, const std::vector<F>& f) { m_column_sections.emplace_back(s, f); return *this; }
    template <typename SF>
252
        this_type&
253
254
        add_column_section(const SF& sf) { m_column_sections.emplace_back(sf); return *this; }
    template <typename SF>
255
        this_type&
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
        add_column_section(const SF& sf, size_t n) { m_column_sections.emplace_back(sf, n); return *this; }

/*private:*/

    std::vector<size_t> compute_field_widths() const
    {
        std::vector<size_t> ret;
        ret.resize(m_matrix.outerSize() + 1, 0);
        auto ri = ret.begin();
        if (has_row_field_labels) {
            for (const auto& s: m_row_sections) {
                for (const auto&f: s.field_labels()) {
                    size_t sz = to_string(f).size();
                    if (sz > *ri) {
                        *ri = sz;
                    }
                }
            }
        } else {
            *ri = 0;
        }
        ++ri;
        for (const auto& s: m_column_sections) {
279
280
            /*MSG_DEBUG("for s: column_sections");*/
            /*MSG_DEBUG("  initial d(begin, r) = " << (ri - ret.begin()));*/
281
282
            for (const auto&f: s.field_labels()) {
                *ri++ = to_string(f).size();
283
284
                /*MSG_DEBUG("  d(begin, r) = " << (ri - ret.begin()));*/
                /*MSG_QUEUE_FLUSH();*/
285
286
287
288
            }
        }
        for (int i = 0; i < m_matrix.outerSize(); ++i) {
            for (int j = 0; j < m_matrix.innerSize(); ++j) {
289
                size_t w = width(m_matrix(j, i));
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
                if (w > ret[i + 1]) {
                    ret[i + 1] = w;
                }
            }
        }
        return ret;
    }

    std::vector<section_size> compute_section_widths(const std::vector<size_t>& fwidths, size_t sep_size) const
    {
        std::vector<section_size> ret;
        ret.resize(m_column_sections.size());
        std::vector<size_t>::const_iterator wi = fwidths.begin();
        ++wi;
        for (size_t s = 0; s < m_column_sections.size(); ++s) {
            size_t nf = m_column_sections[s].n_fields();
            for (size_t i = 0; i < nf; ++i) {
                ret[s].size += *wi++;
            }
            ret[s].size += sep_size * (nf - 1);
            ret[s].label_length = m_column_sections[s].section_label_width();
        }
        return ret;
    }

    std::vector<section_size> compute_section_heights() const
    {
        std::vector<section_size> ret;
        ret.resize(m_row_sections.size());
        for (size_t s = 0; s < m_row_sections.size(); ++s) {
            ret[s].size = m_row_sections[s].n_fields();
            ret[s].label_length = m_row_sections[s].section_label_width();
        }
        return ret;
    }
};


328
template <typename RS, typename RF, typename CS, typename CF, typename MT>
329
struct section_renderer {
330
331
    typedef matrix_with_sections<RS, RF, CS, CF, MT> mws_type;
    const matrix_with_sections<RS, RF, CS, CF, MT>& mws;
332
333
334
335
336
    std::vector<size_t> fw;
    std::vector<section_size> sw, sh;
    size_t head_space;
    std::stringstream sep;

337
    section_renderer(const mws_type& _)
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
        : mws(_)
        , fw(mws.compute_field_widths())
        , sw(mws.compute_section_widths(fw, field_sep.size()))
        , sh(mws.compute_section_heights())
        , head_space(1)
        , sep()
    {
        if (mws.has_row_field_labels) {
            head_space += field_sep.size() + fw[0];
        }

        fill(sep, section_vsep, head_space);
        auto swi = sw.begin();
        auto swj = sw.end();
        while(swi != swj) {
            sep << section_csep;
            fill(sep, section_vsep, swi->max_size());
            ++swi;
        }
        sep << std::endl;
    }

360
    void insert_pad_lines(std::ostream& os, const mws_type& mws, size_t n_lines, std::string::iterator& li) const
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
    {
        for (size_t i = 0; i < n_lines; ++i) {
            left(os, *li++, head_space);
            auto swi = sw.begin();
            for (size_t s = 0; s < mws.m_column_sections.size(); ++s) {
                os << section_hsep;
                fill(os, ' ', swi->max_size());
                ++swi;
            }
            os << std::endl;
        }
    }

    void header(std::ostream& os) const
    {
        /* first row */
        fill(os, ' ', head_space);
        auto swi = sw.begin();
        for (const auto& s: mws.m_column_sections) {
            os << section_hsep;
381
            center(os, s.label(), swi->max_size());
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
            ++swi;
        }
        os << std::endl;

        /* second row */
        if (mws.has_column_field_labels) {
            auto fwi = fw.begin();
            fill(os, ' ', head_space);
            swi = sw.begin();
            ++fwi;
            for (const auto& s: mws.m_column_sections) {
                os << section_hsep;
                fill(os, ' ', swi->padding_before());
                auto fi = s.field_labels().begin();
                auto fj = s.field_labels().end();
                if (fi != fj) {
                    right(os, *fi++, *fwi++);
                    while (fi != fj) {
                        os << field_sep;
                        right(os, *fi++, *fwi++);
                    }
                }
                fill(os, ' ', swi->padding_after());
405
                ++swi;
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
            }
            os << std::endl;
        }
    }

    void output_sections(std::ostream& os) const
    {
        auto shi = sh.begin();

        int row_start;
        int row_end = 0;
        int column;

        for (const auto& vs: mws.m_row_sections) {
            row_start = row_end;
            column = 0;
            int pad_before = shi->padding_before();
            int pad_after = shi->padding_after();
424
            /*MSG_DEBUG("max_size=" << shi->max_size() << " size=" << shi->size << " pad_before=" << pad_before << " pad_after=" << pad_after);*/
425
426
427
428
429
430
            row_end = row_start + shi->size;

            os << sep.str();

            /* prepare label */
            std::stringstream label;
431
            center(label, vs.label(), shi->max_size());
432
            label << std::flush;
433
            /*MSG_DEBUG("LABEL[" << label.str() << ']');*/
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
            std::string labelstr = label.str();
            auto li = labelstr.begin();

            /* padding top */
            insert_pad_lines(os, mws, pad_before, li);

            for (int row = row_start, i = 0; row < row_end; ++row, ++i) {
                auto fwi = fw.begin();
                os << (*li++);
                if (mws.has_row_field_labels) {
                    os << field_sep;
                    right(os, vs.field_labels()[i], *fwi);
                }
                ++fwi;

                column = 0;
                auto swi = sw.begin();
                for (const auto& c: mws.m_column_sections) {
                    os << section_hsep;
                    fill(os, ' ', swi->padding_before());
                    right(os, to_string(mws.m_matrix(row, column)), *fwi++);
                    ++column;
                    for (size_t k = 1; k < c.n_fields(); ++k, ++column) {
                        os << field_sep;
                        right(os, to_string(mws.m_matrix(row, column)), *fwi++);
                    }
                    fill(os, ' ', swi->padding_after());
461
                    ++swi;
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
                }

                os << std::endl;
            }

            /* padding bottom */
            insert_pad_lines(os, mws, pad_after, li);

            ++shi;
        }
    }

    friend
        std::ostream& operator << (std::ostream& os, const section_renderer& sr)
        {
            sr.header(os);
            sr.output_sections(os);
            return os;
        }
};

483
484
} /* namespace model_print */

485
486
using model_print::matrix_with_sections;

487
template <typename RS, typename RF, typename CS, typename CF, typename MT>
488
std::ostream& operator << (std::ostream& os, const matrix_with_sections<RS, RF, CS, CF, MT>& mws)
489
{
490
    model_print::section_renderer<RS, RF, CS, CF, MT> sr(mws);
491
492
493
494
495
    return os << sr;
}

#endif