00001 #include <fstream>
00002 #include <iostream>
00003 #include <string>
00004 #include <algorithm>
00005 #include <boost/bind.hpp>
00006 #include <boost/date_time/posix_time/posix_time.hpp>
00007 #include <boost/program_options.hpp>
00008 #include "ApplyFilter.h"
00009 #include "Adaptors.h"
00010 #include "NetCDFTools.h"
00011 #include "NeuralNetwork.h"
00012 #include "TimeSeriesData.h"
00013
00014 using namespace std;
00015 using namespace gplib;
00016
00017 string version = "$Id: mtunn.cpp 1838 2010-03-04 16:19:34Z mmoorkamp $";
00018
00019 void Restore(const TimeSeriesComponent &Input, TimeSeriesComponent &Output,
00020 const double factor)
00021 {
00022 transform(Input.GetData().begin(), Input.GetData().end(),
00023 Output.GetData().begin(),
00024 boost::bind(multiplies<double> (), _1, factor));
00025 }
00026
00027 double Normalize(TimeSeriesComponent &Component)
00028 {
00029 double themax = *max_element(Component.GetData().begin(),
00030 Component.GetData().end(), gplib::absLess<double, double>());
00031 double factor = 1. / themax;
00032 transform(Component.GetData().begin(), Component.GetData().end(),
00033 Component.GetData().begin(), boost::bind(multiplies<double> (), _1,
00034 factor));
00035 return themax;
00036 }
00037
00038 void GetNNSetup(const size_t filterlength, const size_t hiddenlayers,
00039 const size_t ntimeseries, NeuralNetwork::ttypeArray &NNLayers,
00040 double &NNmaxinit)
00041 {
00042 NeuralNetwork::ttypeVector typeVector(filterlength,
00043 SigmoidalNeuron::bipolar);
00044 for (size_t i = 0; i < hiddenlayers; ++i)
00045 {
00046 NNLayers.push_back(typeVector);
00047 }
00048 typeVector.assign(1, SigmoidalNeuron::identity);
00049 NNLayers.push_back(typeVector);
00050 }
00051
00052 namespace po = boost::program_options;
00053
00054 int main(int argc, char *argv[])
00055 {
00056 cout << "This is mtunn: Perform neural network filtering on MT time-series"
00057 << endl << endl;
00058 cout
00059 << " The program will ask for reference and input filename, further settings are read from 'mtuadaptive.conf' "
00060 << endl;
00061 cout
00062 << " Output will be 1 Phoenix format file with ending '.clean' where all channels are overwritten"
00063 << endl;
00064 cout
00065 << " Network weights are stored in a file with ending '.weights.nc' and network topology in '.dot"
00066 << endl << endl;
00067 cout << " This is Version: " << version << endl << endl;
00068
00069 int filterlength = 0, shift = 0, hiddenlayers;
00070 double mu, alpha;
00071
00072 po::options_description desc("General options");
00073 desc.add_options()("help", "produce help message")("filterlength",
00074 po::value<int>(&filterlength)->default_value(10),
00075 "The length of the adaptive filter")("shift",
00076 po::value<int>(&shift)->default_value(0),
00077 "The shift in samples between the time series")("mu",
00078 po::value<double>(&mu)->default_value(1.0),
00079 "Stepsize for LMS adaptive filter")("alpha", po::value<
00080 double>(&alpha)->default_value(1.0), "")("hiddenlayers", po::value<int>(
00081 &hiddenlayers)->default_value(1),
00082 "The number of hiddenlayers for the neural network");
00083
00084 po::variables_map vm;
00085 po::store(po::parse_command_line(argc, argv, desc), vm);
00086 po::notify(vm);
00087
00088 if (vm.count("help"))
00089 {
00090 std::cout << desc << "\n";
00091 return 1;
00092 }
00093
00094 TimeSeriesData InputData, ReferenceData;
00095 string tsfilename, noisefilename;
00096
00097 if (argc == 3)
00098 {
00099 noisefilename = argv[1];
00100 tsfilename = argv[2];
00101 }
00102 else
00103 {
00104 cout << "Reference Data: ";
00105 cin >> noisefilename;
00106 cout << "Input Time Series Filename: ";
00107 cin >> tsfilename;
00108 }
00109
00110 ReferenceData.GetData(noisefilename);
00111 InputData.GetData(tsfilename);
00112
00113 cout << "Input Start time: " << InputData.GetData().GetTime().front()
00114 << endl;
00115 cout << "Reference Start time: "
00116 << ReferenceData.GetData().GetTime().front() << endl;
00117 if (InputData.GetData().GetTime().front()
00118 != ReferenceData.GetData().GetTime().front())
00119 {
00120 cerr << "Time series not synchronized !" << endl;
00121 return 100;
00122 }
00123 int lengthdiff = ReferenceData.GetData().Size()
00124 - InputData.GetData().Size();
00125 if (lengthdiff > 0)
00126 {
00127 cout << "Removing " << lengthdiff
00128 << " datapoints from Reference time-series." << endl;
00129 ReferenceData.GetData().erase(ReferenceData.GetData().Size()
00130 - lengthdiff, ReferenceData.GetData().Size());
00131 }
00132 if (lengthdiff < 0)
00133 {
00134 cout << "Removing " << lengthdiff
00135 << " datapoints from Input time-series." << endl;
00136 InputData.GetData().erase(InputData.GetData().Size() + lengthdiff,
00137 InputData.GetData().Size());
00138 }
00139 cout << "Input End time: " << InputData.GetData().GetTime().back() << endl;
00140 cout << "Reference End time: " << ReferenceData.GetData().GetTime().back()
00141 << endl;
00142
00143 NeuralNetwork::ttypeArray NNLayers;
00144 double NNmaxinit = 1.0;
00145 const int ntimeseries = 4;
00146 GetNNSetup(filterlength, hiddenlayers, ntimeseries, NNLayers, NNmaxinit);
00147 NeuralNetwork NN(filterlength, ntimeseries, mu, NNLayers, NNmaxinit, true);
00148 NN.SetAlpha(alpha);
00149
00150 ApplyFilter Canceller(NN, true);
00151
00152 double rexmax = Normalize(ReferenceData.GetData().GetEx());
00153 double reymax = Normalize(ReferenceData.GetData().GetEy());
00154 double rhxmax = Normalize(ReferenceData.GetData().GetHx());
00155 double rhymax = Normalize(ReferenceData.GetData().GetHy());
00156
00157 Canceller.AddReferenceChannel(ReferenceData.GetData().GetHx());
00158 Canceller.AddReferenceChannel(ReferenceData.GetData().GetHy());
00159 Canceller.AddReferenceChannel(ReferenceData.GetData().GetEx());
00160 Canceller.AddReferenceChannel(ReferenceData.GetData().GetEy());
00161
00162 double ihxmax = Normalize(InputData.GetData().GetHx());
00163 double ihymax = Normalize(InputData.GetData().GetHy());
00164 Canceller.AddInputChannel(InputData.GetData().GetHx());
00165 Canceller.AddInputChannel(InputData.GetData().GetHy());
00166
00167 Canceller.SetWeightSaveIntervall(1000);
00168 Canceller.SetShift(shift);
00169 Canceller.ShowProgress(true);
00170 ofstream weightfile((noisefilename + "weights").c_str());
00171 NN.PrintWeights(weightfile);
00172 cout << " First iteration: " << endl << endl;
00173
00174 Canceller.FilterData();
00175 NN.PrintWeights(weightfile);
00176 cout << endl << endl << " Second iteration: " << endl << endl;
00177
00178 Canceller.FilterData();
00179 NN.PrintWeights(weightfile);
00180
00181 Restore(*Canceller.GetOutChannels().at(0).get(),
00182 ReferenceData.GetData().GetHx(), rhxmax);
00183 Restore(*Canceller.GetOutChannels().at(1).get(),
00184 ReferenceData.GetData().GetHy(), rhymax);
00185 Restore(*Canceller.GetOutChannels().at(2).get(),
00186 ReferenceData.GetData().GetEx(), rexmax);
00187 Restore(*Canceller.GetOutChannels().at(3).get(),
00188 ReferenceData.GetData().GetEy(), reymax);
00189 ReferenceData.WriteBack(noisefilename + ".clean");
00190
00191
00192
00193
00194
00195
00196 }