source: trunk/se/lu/thep/wenni/bin/NNIFileConverter/NNIFileConverter.cc @ 597

Last change on this file since 597 was 597, checked in by Jari Häkkinen, 15 years ago

Improved memory usage of NNIFileConverter.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.4 KB
Line 
1// $Id: NNIFileConverter.cc 597 2008-02-27 23:36:46Z jari $
2
3/*
4  Copyright (C) 2005, 2006, 2008 Jari Häkkinen
5
6  This file is part of WeNNI,
7  http://lev.thep.lu.se/trac/baseplugins/wiki/WeNNI
8
9  WeNNI is free software; you can redistribute it and/or modify it
10  under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 2 of the License, or
12  (at your option) any later version.
13
14  WeNNI is distributed in the hope that it will be useful, but WITHOUT
15  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
17  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 "Parameter.h"
26#include "weight.h"
27
28#include <cmath>
29#include <fstream>
30#include <string>
31
32#include <c++_tools/gslapi/matrix.h>
33
34
35int main(const int argc,const char* argv[]) 
36{
37  theplu::wenni::nnifileconverter::Parameter option(argc,argv);
38  std::ifstream is_f1(option.fg1().c_str());
39  std::ifstream is_f2(option.fg2().c_str());
40  std::ifstream is_s1(option.bgstd1().c_str());
41  std::ifstream is_s2(option.bgstd2().c_str());
42  std::ifstream* is_b1=NULL;
43  std::ifstream* is_b2=NULL;
44  if (option.datatype()==std::string("raw")) {
45    is_b1=new std::ifstream(option.bg1().c_str());
46    is_b2=new std::ifstream(option.bg2().c_str());
47  }
48
49  std::ofstream os_r(option.logratio().c_str());
50  std::ofstream os_w(option.weight().c_str());
51  std::string f1str;
52  while (std::getline(is_f1,f1str)) {
53    std::string f2str;
54    std::string s1str;
55    std::string s2str;
56    std::getline(is_f2,f2str);
57    std::getline(is_s1,s1str);
58    std::getline(is_s2,s2str);
59    if (is_f1.fail() || is_f2.fail() ||is_s1.fail() ||is_s2.fail()) {
60      std::cerr << "Failed to read at least one file. Length problem?\n"
61                << "   (fg1, fg2, bgstd1, or bgstd2)" << std::endl;
62      exit(-1);
63    }
64    theplu::gslapi::vector f1(f1str);
65    theplu::gslapi::vector f2(f2str);
66    theplu::gslapi::vector s1(s1str);
67    theplu::gslapi::vector s2(s2str);
68    theplu::gslapi::vector b1(f1.size(),0.0);
69    theplu::gslapi::vector b2(f2.size(),0.0);
70    if (is_b1) {
71      std::string b1str;
72      std::getline(*is_b1,b1str);
73      if (is_b1->fail()) {
74        std::cerr << "Failed to read at bg1 file. Length problem?"
75                  << std::endl;
76        exit(-1);
77      }
78      b1=theplu::gslapi::vector(b1str);
79    }
80    if (is_b2) {
81      std::string b2str;
82      std::getline(*is_b2,b2str);
83      if (is_b2->fail()) {
84        std::cerr << "Failed to read at bg2 file. Length problem?"
85                  << std::endl;
86        exit(-1);
87      }
88      b2=theplu::gslapi::vector(b2str);
89    }
90
91    // Calculating log2ratio
92    theplu::gslapi::vector log2ratio(f1.size());
93    double log2=log(2);
94    for (size_t i=0; i<log2ratio.size(); i++) 
95      if (f1(i)>b1(i) && f2(i)>b2(i))
96        log2ratio(i)=log( (f1(i)-b1(i)) / (f2(i)-b2(i)) ) / log2;
97
98    // calculating weights
99    theplu::gslapi::vector weight(f1.size());
100    for (size_t i=0; i<weight.size(); i++) {
101      using namespace theplu::wenni::weight;
102      weight(i)=weight_SNR2(get_snr(f1(i),b1(i),s1(i)),
103                            get_snr(f2(i),b2(i),s2(i)),option.beta());
104    }
105
106    os_r << log2ratio << '\n';
107    os_w << weight << '\n';
108  }
109
110  is_f1.close();
111  is_s1.close();
112  is_f2.close();
113  is_s2.close();
114  if (is_b1) {
115    is_b1->close();
116    delete is_b1;
117  }
118  if (is_b2) {
119    is_b2->close();
120    delete is_b2;
121  }
122  os_r.close();
123  os_w.close();
124
125  return 0;
126}
Note: See TracBrowser for help on using the repository browser.