Changeset 597 for trunk/se/lu/thep/wenni


Ignore:
Timestamp:
Feb 28, 2008, 12:36:46 AM (16 years ago)
Author:
Jari Häkkinen
Message:

Improved memory usage of NNIFileConverter.

Location:
trunk/se/lu/thep/wenni
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/se/lu/thep/wenni/bin/NNIFileConverter/NNIFileConverter.cc

    r95 r597  
    22
    33/*
    4   Copyright (C) 2005, 2006 Jari Häkkinen
     4  Copyright (C) 2005, 2006, 2008 Jari Häkkinen
    55
    66  This file is part of WeNNI,
     
    2828#include <cmath>
    2929#include <fstream>
     30#include <string>
    3031
    3132#include <c++_tools/gslapi/matrix.h>
     
    3536{
    3637  theplu::wenni::nnifileconverter::Parameter option(argc,argv);
    37   std::ifstream is(option.fg1().c_str());
    38   theplu::gslapi::matrix fg1(is);
    39   is.close();
    40   is.open(option.bgstd1().c_str());
    41   theplu::gslapi::matrix bgstd1(is);
    42   is.close();
    43   is.open(option.fg2().c_str());
    44   theplu::gslapi::matrix fg2(is);
    45   is.close();
    46   is.open(option.bgstd2().c_str());
    47   theplu::gslapi::matrix bgstd2(is);
    48   is.close();
     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  }
    4948
    50   theplu::gslapi::matrix bg1;
    51   theplu::gslapi::matrix bg2;
    52   if (option.datatype()==std::string("raw")) {
    53     is.open(option.bg1().c_str());
    54     bg1=theplu::gslapi::matrix(is);
    55     is.close();
    56     is.open(option.bg2().c_str());
    57     bg2=theplu::gslapi::matrix(is);
    58     is.close();
    59   }
    60   else
    61     bg1=bg2=theplu::gslapi::matrix(fg1.rows(),fg1.columns(),0.0);
    62 
    63   // Calculating log2ratio
    64   theplu::gslapi::matrix log2ratio(fg1.rows(),fg1.columns(),0.0);
    65   double log2=log(2);
    66   for (size_t i=0; i<log2ratio.rows(); i++)
    67     for (size_t j=0; j<log2ratio.columns(); j++)
    68       if (fg1(i,j)>bg1(i,j) && fg2(i,j)>bg2(i,j))
    69         log2ratio(i,j)=log( (fg1(i,j)-bg1(i,j)) / (fg2(i,j)-bg2(i,j)) ) / log2;
    70 
    71   // calculating weights
    72   theplu::gslapi::matrix weight(fg1.rows(),fg1.columns());
    73   for (size_t i=0; i<weight.rows(); i++)
    74     for (size_t j=0; j<weight.columns(); j++) {
    75       using namespace theplu::wenni::weight;
    76       weight(i,j)=weight_SNR2(get_snr(fg1(i,j),bg1(i,j),bgstd1(i,j)),
    77                               get_snr(fg2(i,j),bg2(i,j),bgstd2(i,j)),
    78                               option.beta());
     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);
    7989    }
    8090
    81   std::ofstream os(option.logratio().c_str());
    82   os << log2ratio;
    83   os.close();
    84   os.open(option.weight().c_str());
    85   os << weight;
    86   os.close();
     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();
    87124
    88125  return 0;
  • trunk/se/lu/thep/wenni/lib/c++_tools/gslapi/vector.cc

    r110 r597  
    165165
    166166
     167  vector::vector(std::string& line, char sep)
     168    throw (utility::IO_error,std::exception)
     169    : view_(NULL), const_view_(NULL)
     170  {
     171    // Empty line
     172    if (!line.size())
     173      return;
     174
     175    std::vector<double> v;
     176    v.reserve(line.length()/2);
     177    std::string element;
     178    std::stringstream ss(line);
     179    bool ok=true;
     180    while(ok) {
     181      if(sep=='\0')
     182        ok=(ss>>element);
     183      else
     184        ok=getline(ss, element, sep);
     185      if(!ok)
     186        break;
     187
     188      if(utility::is_double(element)) {
     189        v.push_back(atof(element.c_str()));
     190      }
     191      else if (!element.size() || utility::is_nan(element)) {
     192        v.push_back(std::numeric_limits<double>::quiet_NaN());
     193      }
     194    }
     195    if (sep!='\0' && line[line.size()-1]==sep) // add NaN for final separator
     196      v.push_back(std::numeric_limits<double>::quiet_NaN());
     197
     198    // convert the data to a gsl vector
     199    v_ = gsl_vector_alloc(v.size());
     200    size_t n=0;
     201    for (size_t i=0; i<v.size(); i++)
     202      gsl_vector_set(v_, n++, v[i]);
     203  }
     204
     205
     206
    167207  vector::~vector(void)
    168208  {
  • trunk/se/lu/thep/wenni/lib/c++_tools/gslapi/vector.h

    r110 r597  
     1#ifndef _theplu_gslapi_vector_
     2#define _theplu_gslapi_vector_
     3
    14// $Id$
    25
     
    58  Copyright (C) 2004 Jari Häkkinen, Peter Johansson
    69  Copyright (C) 2005 Jari Häkkinen, Peter Johansson, Markus Ringnér
    7   Copyright (C) 2006 Jari Häkkinen
     10  Copyright (C) 2006, 2008 Jari Häkkinen
    811
    912  This file is part of the thep c++ tools library,
     
    2629*/
    2730
    28 #ifndef _theplu_gslapi_vector_
    29 #define _theplu_gslapi_vector_
    30 
    3131#include <c++_tools/utility/Exception.h>
    3232
    3333#include <iostream>
     34#include <string>
     35#include <utility>
    3436#include <vector>
    35 #include <utility>
    3637
    3738#include <gsl/gsl_vector.h>
     
    179180    ///
    180181    explicit vector(std::istream &, char sep='\0') throw (utility::IO_error,std::exception);
     182
     183    ///
     184    /// The string constructor.
     185    ///
     186    /// Either elements should be separated
     187    /// with white space characters (default), or elements should be separated
     188    /// by the delimiter \a sep. When delimiter \a sep is used empty elements
     189    /// are stored as NaN's (except that empty lines are ignored). The
     190    /// end of input to the vector is at end of file marker.
     191    ///
     192    explicit vector(std::string &, char sep='\0') throw (utility::IO_error,std::exception);
    181193
    182194
Note: See TracChangeset for help on using the changeset viewer.