File: csv_data_frame.hpp

package info (click to toggle)
r-bioc-alabaster.base 1.6.1%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,652 kB
  • sloc: cpp: 11,377; sh: 29; makefile: 2
file content (203 lines) | stat: -rw-r--r-- 6,499 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#ifndef TAKANE_CSV_DATA_FRAME_HPP
#define TAKANE_CSV_DATA_FRAME_HPP

#include "comservatory/comservatory.hpp"

#include "WrappedOption.hpp"
#include "data_frame.hpp"
#include "utils_csv.hpp"

#include <unordered_set>
#include <string>
#include <stdexcept>

/**
 * @file csv_data_frame.hpp
 * @brief Validation for CSV data frames.
 */

namespace takane {

/**
 * @namespace takane::csv_data_frame
 * @brief Definitions for CSV data frames.
 */
namespace csv_data_frame {

/**
 * @brief Parameters for validating the CSV data frame.
 */
struct Parameters {
    /**
     * Number of rows in the data frame.
     */
    size_t num_rows = 0;

    /**
     * Whether the data frame contains row names.
     */
    bool has_row_names = false;

    /**
     * Details about the expected columns of the data frame, in order.
     */
    WrappedOption<std::vector<data_frame::ColumnDetails> > columns;
 
    /**
     * Whether to load and parse the file in parallel, see `comservatory::ReadOptions` for details.
     */
    bool parallel = false;

    /**
     * Version of the `data_frame` format.
     */
    int df_version = 2;
};

/**
 * @cond
 */
template<class ParseCommand>
CsvContents validate_base(ParseCommand parse, const Parameters& params, CsvFieldCreator* creator) {
    DummyCsvFieldCreator default_creator;
    if (creator == NULL) {
        creator = &default_creator;
    }

    comservatory::Contents contents;
    CsvContents output;
    if (params.has_row_names) {
        auto ptr = creator->string();
        output.fields.emplace_back(ptr); 
        contents.fields.emplace_back(new CsvNameField(true, ptr));
    }

    const auto& columns = *(params.columns);
    size_t ncol = columns.size();
    std::unordered_set<std::string> present;

    for (size_t c = 0; c < ncol; ++c) {
        const auto& col = columns[c];
        if (present.find(col.name) != present.end()) {
            throw std::runtime_error("duplicate column name '" + col.name + "'");
        }
        present.insert(col.name);

        if (col.type == data_frame::ColumnType::INTEGER) {
            auto ptr = creator->integer();
            output.fields.emplace_back(ptr); 
            contents.fields.emplace_back(new CsvIntegerField(c, ptr));

        } else if (col.type == data_frame::ColumnType::NUMBER) {
            output.fields.emplace_back(nullptr); 
            contents.fields.emplace_back(creator->number());

        } else if (col.type == data_frame::ColumnType::STRING) {
            if (col.string_format == data_frame::StringFormat::DATE) {
                auto ptr = creator->string();
                output.fields.emplace_back(ptr); 
                contents.fields.emplace_back(new CsvDateField(c, ptr));

            } else if (col.string_format == data_frame::StringFormat::DATE_TIME) {
                auto ptr = creator->string();
                output.fields.emplace_back(ptr); 
                contents.fields.emplace_back(new CsvDateTimeField(c, ptr));

            } else {
                output.fields.emplace_back(nullptr);
                contents.fields.emplace_back(creator->string());
            }

        } else if (col.type == data_frame::ColumnType::BOOLEAN) {
            output.fields.emplace_back(nullptr);
            contents.fields.emplace_back(creator->boolean());

        } else if (col.type == data_frame::ColumnType::FACTOR) {
            if (params.df_version == 1) {
                auto ptr = creator->string();
                output.fields.emplace_back(ptr); 
                contents.fields.emplace_back(new CsvFactorV1Field(c, col.factor_levels.get(), ptr));
            } else {
                auto ptr = creator->integer();
                output.fields.emplace_back(ptr);
                contents.fields.emplace_back(new CsvFactorV2Field(c, col.factor_levels->size(), ptr));
            }

        } else if (col.type == data_frame::ColumnType::OTHER) {
            output.fields.emplace_back(nullptr);
            contents.fields.emplace_back(new comservatory::UnknownField); // This can be anything.

        } else {
            throw std::runtime_error("unknown code for the expected column type");
        }
    }

    comservatory::ReadOptions opt;
    opt.parallel = params.parallel;
    parse(contents, opt);
    if (contents.num_records() != params.num_rows) {
        throw std::runtime_error("number of records in the CSV file does not match the expected number of rows");
    }

    for (size_t c = 0; c < ncol; ++c) {
        const auto& col = columns[c];
        if (col.name != contents.names[c + params.has_row_names]) {
            throw std::runtime_error("observed and expected header names do not match");
        }
    }

    output.reconstitute(contents.fields);
    return output;
}
/**
 * @endcond
 */

/**
 * Checks if a CSV data frame is correctly formatted.
 * An error is raised if the file does not meet the specifications.
 *
 * @tparam Reader A **byteme** reader class.
 *
 * @param reader A stream of bytes from the CSV file.
 * @param params Validation parameters.
 * @param creator Factory to create objects for holding the contents of each CSV field.
 * Defaults to a pointer to a `DummyFieldCreator` instance.
 * 
 * @return Contents of the loaded CSV.
 * Whether the `fields` member actually contains the CSV data depends on `creator`.
 * Each entry of the `fields` member corresponds to an entry of `params.columns`,
 * which an additional field at the start if `params.has_row_names = true`.
 */
template<class Reader>
CsvContents validate(Reader& reader, const Parameters& params, CsvFieldCreator* creator = NULL) {
    return validate_base(
        [&](comservatory::Contents& contents, const comservatory::ReadOptions& opt) -> void { comservatory::read(reader, contents, opt); },
        params,
        creator
    );
}

/**
 * Overload of `csv_data_frame::validate()` that accepts a file path.
 *
 * @param path Path to the CSV file.
 * @param params Validation parameters.
 * @param creator Factory to create objects for holding the contents of each CSV field.
 * Defaults to a pointer to a `DummyFieldCreator` instance.
 * 
 * @return Contents of the loaded CSV.
 */
inline CsvContents validate(const char* path, const Parameters& params, CsvFieldCreator* creator = NULL) {
    return validate_base(
        [&](comservatory::Contents& contents, const comservatory::ReadOptions& opt) -> void { comservatory::read_file(path, contents, opt); },
        params,
        creator
    );
}

}

}

#endif