1 | // $Id: utility.cc 1419 2008-08-20 12:01:12Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2005, 2006 Jari Häkkinen, Markus Ringnér |
---|
5 | Copyright (C) 2007 Jari Häkkinen, Peter Johansson |
---|
6 | |
---|
7 | This file is part of the yat library, http://trac.thep.lu.se/yat |
---|
8 | |
---|
9 | The yat library is free software; you can redistribute it and/or |
---|
10 | modify it under the terms of the GNU General Public License as |
---|
11 | published by the Free Software Foundation; either version 2 of the |
---|
12 | License, or (at your option) any later version. |
---|
13 | |
---|
14 | The yat library is distributed in the hope that it will be useful, |
---|
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
17 | General Public License for more details. |
---|
18 | |
---|
19 | You should have received a copy of the GNU General Public License |
---|
20 | along with this program; if not, write to the Free Software |
---|
21 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
22 | 02111-1307, USA. |
---|
23 | */ |
---|
24 | |
---|
25 | #include "utility.h" |
---|
26 | |
---|
27 | #include "Matrix.h" |
---|
28 | #include "stl_utility.h" |
---|
29 | #include "VectorConstView.h" |
---|
30 | #include "yat/statistics/Averager.h" |
---|
31 | |
---|
32 | #include <sstream> |
---|
33 | #include <string> |
---|
34 | |
---|
35 | namespace theplu { |
---|
36 | namespace yat { |
---|
37 | namespace utility { |
---|
38 | |
---|
39 | bool is_double(const std::string& s) |
---|
40 | { |
---|
41 | return is<double>(s); |
---|
42 | } |
---|
43 | |
---|
44 | |
---|
45 | bool is_equal(std::string s, std::string other) |
---|
46 | { |
---|
47 | std::stringstream ss(s); |
---|
48 | std::string s2; |
---|
49 | ss >> s2; // to trim surrounding whitespaces |
---|
50 | to_lower(s2); |
---|
51 | // Check that nothing is left on stream |
---|
52 | std::string s3; |
---|
53 | ss >> s3; |
---|
54 | if(s3.size()) |
---|
55 | return false; |
---|
56 | return (other==s2); |
---|
57 | } |
---|
58 | |
---|
59 | |
---|
60 | bool is_float(const std::string& s) |
---|
61 | { |
---|
62 | return is<float>(s); |
---|
63 | } |
---|
64 | |
---|
65 | |
---|
66 | bool is_int(const std::string& s) |
---|
67 | { |
---|
68 | return is<int>(s); |
---|
69 | } |
---|
70 | |
---|
71 | bool is_nan(const std::string& s) |
---|
72 | { |
---|
73 | return is_equal(s, "nan"); |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
77 | void quantile_normalize(Matrix& data) |
---|
78 | { |
---|
79 | Matrix data_copy(data); |
---|
80 | |
---|
81 | // sort columns in copy |
---|
82 | for (size_t column=0; column<data_copy.columns(); ++column){ |
---|
83 | std::sort(data_copy.begin_column(column), data_copy.end_column(column)); |
---|
84 | } |
---|
85 | |
---|
86 | // calculate average of each row |
---|
87 | std::vector<yat::statistics::Averager> averager(data_copy.rows()); |
---|
88 | for (size_t row=0; row<data_copy.rows(); ++row){ |
---|
89 | add(averager[row], data_copy.begin_row(row), data_copy.end_row(row)); |
---|
90 | } |
---|
91 | |
---|
92 | for (size_t column=0; column<data.columns(); ++column){ |
---|
93 | std::vector<size_t> index; |
---|
94 | yat::utility::sort_index(index, data.column_const_view(column)); |
---|
95 | |
---|
96 | for (size_t row=0; row<data.rows(); ++row) |
---|
97 | data(index[row], column) = averager[row].mean(); |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | |
---|
102 | |
---|
103 | }}} // end of namespace utility, yat and thep |
---|