Changeset 594


Ignore:
Timestamp:
Feb 26, 2008, 3:24:46 PM (13 years ago)
Author:
Jari Häkkinen
Message:

Improved memory usage of BaseFileConverter?. Previous implementation was not flattering.

Location:
trunk/se/lu/thep/wenni/base/base1/BaseFileConverter
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/se/lu/thep/wenni/base/base1/BaseFileConverter/BaseFileConverter.cc

    r95 r594  
    33/*
    44  Copyright (C) 2005, 2006 Jari Häkkinen, Peter Johansson
     5  Copyright (C) 2008 Jari Häkkinen
    56
    67  This file is part of WeNNI,
     
    8485  }
    8586
    86   // Convert assayFields to GSL-matrices
    87   std::vector<theplu::wenni::Data> data;
     87  std::vector<theplu::wenni::Data*> data;
    8888  std::vector<theplu::wenni::Clone> clone;
    8989  for (int i=0; i<option.nof_property(); i++) {
    9090    if (option.type(i)=="-assayFields")
    91       data.push_back(theplu::wenni::Data(bfs,option.property(i),
    92                                         option.output_file(i)));
     91      data.push_back(new theplu::wenni::Data(bfs,option.property(i),
     92                                            option.output_file(i)));
    9393    else if (option.type(i)=="-columns")
    9494      clone.push_back(theplu::wenni::Clone(bfs,option.property(i),
     
    103103  // Extract data
    104104  if (data.size()) {
    105     std::vector<std::string> vals(data[0].total_nof_columns());
     105    std::vector<std::string> vals(data[0]->total_nof_columns());
    106106    while (bfr.readVec(vals))
    107107      for (size_t i=0; i<data.size(); i++)
    108         data[i].push_back(vals);
     108        data[i]->write(vals);
    109109  }
    110110
     
    116116        clone[i].push_back(vals);
    117117  }
    118 
    119   for (size_t i=0; i<data.size(); i++)
    120     data[i].write_matrix();
    121118  for (size_t i=0; i<clone.size(); i++)
    122119    clone[i].write();
    123120
     121  // clean up
     122  for (size_t i=0; i<data.size(); i++)
     123    delete data[i];
     124
    124125  return 0;
    125126}
  • trunk/se/lu/thep/wenni/base/base1/BaseFileConverter/Data.cc

    r95 r594  
    33/*
    44  Copyright (C) 2005, 2006 Jari Häkkinen, Peter Johansson
     5  Copyright (C) 2008 Jari Häkkinen
    56
    67  This file is part of WeNNI,
     
    5556    }
    5657    total_nof_columns_= column_offset_ + nof_fields_per_assay_*nof_assays_;
     58
     59    os_=new std::ofstream(output_file_.c_str());
    5760  }
    5861
    5962
     63  Data::~Data(void)
     64  {
     65    os_->close();
     66    delete os_;
     67  }
    6068
    61   void Data::push_back(const std::vector<std::string>& vals)
     69
     70  void Data::write(const std::vector<std::string>& vals)
    6271  {
    63     data_.push_back(std::vector<std::string>(nof_assays_));
    64     for(int i=0; i<nof_assays_; i++){
    65       if(vals[column_offset_+nof_fields_per_assay_*i+column_number_].empty()){
     72    for (int i=0; i<nof_assays_; i++) {
     73      if (vals[column_offset_+nof_fields_per_assay_*i+column_number_].empty())
    6674        // In a Base File missing values are given by a blank. To be
    6775        // able to load the data into a gsl matrix missing values are
    6876        // changed to NaNs.
    69         (*data_.rbegin())[i]="NaN";
    70       }
     77        *os_ << "NaN";
    7178      else
    72         (*data_.rbegin())[i]=vals[column_offset_ + nof_fields_per_assay_ * i +
    73                                   column_number_];
     79        *os_ << vals[column_offset_ + nof_fields_per_assay_ * i +column_number_];
     80      *os_ << (i==(nof_assays_-1) ? '\n' : '\t');
    7481    }
    7582  }
    7683
    7784
    78 
    79   void Data::write_matrix(void)
    80   {
    81     std::filebuf fb;
    82     fb.open (output_file_.c_str(),std::ios::out);
    83     std::ostream s(&fb);
    84     for (size_t i=0; i<data_.size(); i++) {
    85       for (size_t j=0; j<data_[i].size()-1; j++)
    86         s << data_[i][j] << "\t";
    87       s << data_[i][data_[i].size()-1] << std::endl;
    88     }
    89     fb.close();
    90   }
    91 
    92 
    9385}} // of namespace wenni and namespace theplu
  • trunk/se/lu/thep/wenni/base/base1/BaseFileConverter/Data.h

    r95 r594  
     1#ifndef _theplu_wenni_basefileconverter_data_
     2#define _theplu_wenni_basefileconverter_data_
     3
    14// $Id$
    25
    36/*
    47  Copyright (C) 2005, 2006 Jari Häkkinen, Peter Johansson
     8  Copyright (C) 2008 Jari Häkkinen
    59
    610  This file is part of WeNNI,
     
    2327*/
    2428
    25 #ifndef _theplu_wenni_basefileconverter_data_
    26 #define _theplu_wenni_basefileconverter_data_
    27 
    28 // #include <basefile.h>
    29 
     29#include <iosfwd>
    3030#include <string>
    3131#include <vector>
     
    4343    Data(BASE::BaseFileSection&,const std::string&, const std::string&);
    4444
     45    ~Data(void);
     46
    4547    void push_back(const std::vector<std::string>&);
    4648
    4749    inline int total_nof_columns(void) { return total_nof_columns_; }
    4850
     51    void write(const std::vector<std::string>&);
     52
    4953    void write_matrix(void);
    5054
    5155  private:
    52     std::vector<std::vector<std::string> > data_;
    5356    int column_number_;
    5457    int column_offset_;
     
    5861    std::string column_name_;
    5962    std::string output_file_;
     63
     64    std::ofstream* os_;
    6065  };
    6166
Note: See TracChangeset for help on using the changeset viewer.