source: trunk/yat/utility/utility.h @ 1304

Last change on this file since 1304 was 1304, checked in by Peter, 14 years ago

quantile normalization closes #288

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.8 KB
Line 
1#ifndef _theplu_yat_utility_utility_
2#define _theplu_yat_utility_utility_
3
4// $Id: utility.h 1304 2008-05-14 20:11:57Z peter $
5
6/*
7  Copyright (C) 2005 Jari Häkkinen, Peter Johansson, Markus Ringnér
8  Copyright (C) 2006 Jari Häkkinen
9  Copyright (C) 2007 Jari Häkkinen, Peter Johansson
10  Copyright (C) 2008 Peter Johansson
11
12  This file is part of the yat library, http://trac.thep.lu.se/yat
13
14  The yat library is free software; you can redistribute it and/or
15  modify it under the terms of the GNU General Public License as
16  published by the Free Software Foundation; either version 2 of the
17  License, or (at your option) any later version.
18
19  The yat library is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  General Public License for more details.
23
24  You should have received a copy of the GNU General Public License
25  along with this program; if not, write to the Free Software
26  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27  02111-1307, USA.
28*/
29
30///
31/// @file utility/utility.h
32///
33/// @brief Some useful functions are placed here
34///
35
36#include <string>
37#include <stdexcept>
38#include <sstream>
39#include <utility>
40#include <vector>
41
42namespace theplu {
43namespace yat {
44namespace utility {
45
46  class Matrix;
47
48  /**
49     \brief convert string to (numerical) type
50
51     \throw runtime_error if conversion fails
52   */
53  template<typename T>
54  T convert(const std::string& s);
55
56  ///
57  /// @return true if string is a double
58  ///
59  bool is_double(const std::string&);
60
61  /**
62     @return true if string \a s fulfills regular expression \verbatim
63     ^\w* \endverbatim \a other \verbatim \w*$ \endverbatim (case
64     insensitive)
65  */
66  bool is_equal(std::string s, std::string other);
67
68  ///
69  /// @return true if string is a float
70  ///
71  bool is_float(const std::string&);
72
73  ///
74  /// @return true if string is an int
75  ///
76  bool is_int(const std::string&);
77
78  ///
79  /// @return true if string is "nan" (case-insensitive)
80  ///
81  bool is_nan(const std::string& s);
82
83  /**
84     \brief Perform quantile normalization
85   */
86  void quantile_normalize(Matrix&);
87
88  // template implementations
89  template<typename T>
90  T convert(const std::string& s)
91  {
92    if (is_nan(s))
93      return std::numeric_limits<T>::quiet_NaN();
94    if (is_equal(s, "inf"))
95      return std::numeric_limits<T>::infinity();
96    if (is_equal(s, "-inf"))
97      if (std::numeric_limits<T>::is_signed)
98        return -std::numeric_limits<T>::infinity();
99      else
100        throw std::runtime_error(std::string("convert(\"")+s+
101                                 std::string("\"): type is unsigned") );
102    std::stringstream ss(s);
103    T a;
104    ss >> a;
105    bool ok = true;
106    if(ss.fail()) 
107      ok = false;
108    // Check that nothing is left on stream
109    std::string b;
110    ss >> b;
111    if (!b.empty() || !ok)
112      throw std::runtime_error(std::string("convert(\"")+s+std::string("\")"));
113    return a;
114  }
115
116}}} // of namespace utility, yat, and theplu
117
118#endif
Note: See TracBrowser for help on using the repository browser.