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

Improved memory usage of NNIFileConverter.

File:
1 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;
Note: See TracChangeset for help on using the changeset viewer.