68 const static int Normalize_Cols = 1;
69 const static int Normalize_Rows = 2;
73 unsigned long long nrows = 0;
74 unsigned long long ncols = 0;
75 unsigned long long size()
const {
return (
unsigned long long)nrows*ncols; }
76 vector<T> &get_vec() {
return m; }
77 void copy_vec(vector<T> &c_vec) { c_vec.clear(); c_vec = m; }
81 vector<RecordData> recordsMetadata;
82 vector<string> signals;
89 int normalized_flag = 0;
90 int transposed_flag = 0;
95 inline T operator ()(
size_t i,
size_t j)
const {
return m[i*ncols + j]; }
97 inline T &operator ()(
size_t i,
size_t j) {
return m[i*ncols + j]; }
99 inline T get(
size_t i,
size_t j)
const {
return m[i*ncols + j]; }
101 inline T& set(
size_t i,
size_t j) {
return m[i*ncols + j]; }
103 inline void copy_header(
MedMat<T> &other) {
104 if (
this != &other) {
107 signals = other.signals;
110 normalized_flag = other.normalized_flag;
111 transposed_flag = other.transposed_flag;
112 missing_value = other.missing_value;
117 if (
this != &other) {
121 row_ids = move(other.row_ids);
122 signals = move(other.signals);
123 avg = move(other.avg);
124 std = move(other.std);
125 normalized_flag = other.normalized_flag;
126 transposed_flag = other.transposed_flag;
127 missing_value = other.missing_value;
128 recordsMetadata = move(other.recordsMetadata);
133 if (
this != &other) {
137 row_ids = other.row_ids;
138 signals = other.signals;
141 normalized_flag = other.normalized_flag;
142 transposed_flag = other.transposed_flag;
143 missing_value = other.missing_value;
144 recordsMetadata = other.recordsMetadata;
151 MedMat(
int n_rows,
int n_cols) { clear(); nrows = n_rows; ncols = n_cols; m.resize(nrows*ncols); zero(); };
156 row_ids = other.row_ids;
157 signals = other.signals;
160 normalized_flag = other.normalized_flag;
161 transposed_flag = other.transposed_flag;
162 missing_value = other.missing_value;
163 recordsMetadata = other.recordsMetadata;
165 template <
class S>
MedMat(S *x,
int n_rows,
int n_cols) { clear(); load(x, n_rows, n_cols); }
166 template <
class S>
MedMat(
const vector<S> &x,
int n_cols) { clear(); load(x, n_cols); }
169 template <
class S>
void load(S *x,
int n_rows,
int n_cols);
170 template <
class S>
void load_transposed(S *x,
int n_rows,
int n_cols);
171 template <
class S>
void load(
const vector<S> &x,
int n_cols);
172 template <
class S>
void load(
MedMat<S> &x);
174 void zero() { fill(m.begin(), m.end(), (T)0); }
175 void set_val(T val) { fill(m.begin(), m.end(), val); }
178 void clear() { m.clear(); row_ids.clear(); signals.clear(); recordsMetadata.clear(); nrows = 0; ncols = 0; normalized_flag = 0; transposed_flag = 0; missing_value = (T)MED_MAT_MISSING_VALUE; }
179 T *data_ptr() {
if (m.size() > 0)
return &m[0];
else return NULL; }
180 const T *data_ptr()
const {
if (m.size() > 0)
return &m[0];
else return NULL; }
181 T *data_ptr(
size_t r,
size_t c) {
if (m.size() > r*ncols + c)
return &m[r*ncols + c];
else return NULL; }
182 int get_nrows() {
return (
int)nrows; }
183 int get_ncols() {
return (
int)ncols; }
184 void resize(
int n_rows,
int n_cols) { nrows = n_rows; ncols = n_cols; m.resize(nrows*ncols); }
187 int read_from_bin_file(
const string &fname);
188 int write_to_bin_file(
const string &fname);
189 int write_to_csv_file(
const string &fname);
190 int read_from_csv_file(
const string &fname,
int titles_line_flag);
197 ADD_SERIALIZATION_FUNCS(m, nrows, ncols, row_ids, recordsMetadata, signals, avg,
std, normalized_flag, transposed_flag, missing_value);
201 void get_sub_mat(vector<int> &rows_to_take, vector<int> &cols_to_take);
202 void get_sub_mat_by_flags(vector<int> &rows_to_take_flag, vector<int> &cols_to_take_flag);
203 void reorder_by_row(vector<int> &row_order);
204 void reorder_by_col(vector<int> &col_order);
206 void random_split_mat_by_ids(
MedMat<T> &mat_0,
MedMat<T> &mat_1,
float p0, vector<int> &inds0, vector<int> &inds1);
208 template <
class S>
void add_rows(
MedMat<S> &m_add);
209 template <
class S>
void add_rows(S *m_add,
int nrows_to_add);
210 template <
class S>
void add_rows(vector<S> &m_add);
211 template <
class S>
void add_cols(
MedMat<S> &m_add);
212 template <
class S>
void add_cols(S *m_add,
int ncols_to_add);
213 template <
class S>
void add_cols(vector<S> &m_add);
216 void get_row(
int i_row, vector<T> &rowv)
const;
217 void get_col(
int i_col, vector<T> &colv)
const;
220 void normalize(
int norm_type,
float *wgts);
221 void normalize(
int norm_type, vector<float> &wgts) {
return normalize(norm_type, &wgts[0]); }
222 void normalize(
int norm_type = Normalize_Cols) {
return normalize(norm_type, NULL); }
224 template <
class S>
void normalize(
const vector<S>& external_avg,
const vector<S>& external_std,
int norm_type = 1);
226 void get_cols_avg_std(vector<T>& _avg, vector<T>& _std);
228 void print_row(FILE *fout,
const string &prefix,
const string &format,
int i_row);
230 void set_signals(vector<string> & sigs);
235 bool is_valid(
bool output =
false) {
236 if (std::is_floating_point<T>::value ==
false)
239 for (
size_t i = 0; i < nrows; i++) {
240 for (
size_t j = 0; j < ncols; j++) {
241 double x = (double)(m[i*ncols + j]);
244 cerr <<
"invalid element(" << i <<
", " << j <<
") = " << x << endl;