diff --git a/work/week6/series/homework2/src/plot_results.py b/work/week6/series/homework2/src/plot_results.py index 2365dbf..e40d708 100644 --- a/work/week6/series/homework2/src/plot_results.py +++ b/work/week6/series/homework2/src/plot_results.py @@ -1,58 +1,58 @@ # -*- coding: utf-8 -*- # @Author: Theo Lemaire # @Date: 2018-10-16 11:52:15 # @Last Modified by: Theo Lemaire # @Last Modified time: 2018-10-23 18:57:39 import os from argparse import ArgumentParser import matplotlib.pyplot as plt import numpy as np def plotSeries(iterations, values, fs=15, lw=2): fig, ax = plt.subplots(figsize=(10, 6)) ax.set_xlabel('# iterations', fontsize=fs) ax.set_ylabel('value', fontsize=fs) ax.plot(iterations, values, label='series', linewidth=lw) for item in ax.get_xticklabels() + ax.get_yticklabels(): item.set_fontsize(fs) return fig def main(): # Define argument parser ap = ArgumentParser() ap.add_argument('-i', '--inputfile', type=str, help='Input filename') # Parse arguments args = ap.parse_args() filepath = args.inputfile # Infer separator character from file extension filename = os.path.basename(filepath) filecode, ext = os.path.splitext(filename) print(filecode, ext) if ext == '.csv': separator = ',' elif ext == '.txt': - separator = ' ' + separator = '\t' elif ext == '.psv': separator = '|' # Load data data = np.genfromtxt(filepath, delimiter=separator) iterations = data[1:, 0] values = data[1:, 1] # Plot series fs = 20 fig = plotSeries(iterations, values, fs=fs) fig.suptitle('{} evolution'.format(filecode), fontsize=fs) plt.show() if __name__ == '__main__': main() diff --git a/work/week6/series/homework2/src/write_series.cc b/work/week6/series/homework2/src/write_series.cc index 0ed8ded..c3db15d 100644 --- a/work/week6/series/homework2/src/write_series.cc +++ b/work/week6/series/homework2/src/write_series.cc @@ -1,36 +1,38 @@ #include #include #include #include #include "write_series.hh" // Constructor WriteSeries::WriteSeries(Series& series, unsigned int maxiter, std::string separator) : DumperSeries(series) { this->maxiter = maxiter; this->separator = separator; } // Compute and write serie on file void WriteSeries::dump(std::ostream & os) { // write sequential iterations to file std::string sname = this->series.getName(); std::transform(sname.begin(), sname.end(), sname.begin(), ::tolower); std::string ext; if (this->separator == ",") { ext = ".csv"; } else if (this->separator == "" || this->separator == "\\t") { + // Re-assignment of space character to tab to preserve unique separator extension match + this->separator = "\t"; ext = ".txt"; } else if (this->separator == "|") { ext = ".psv"; } std::string filename = sname + ext; std::cout << "File " << filename.c_str() << " has been created." << std::endl; std::ofstream fout(filename.c_str()); - fout << "iteration" << separator << "value" << std::endl; + fout << "iteration" << this->separator << "value" << std::endl; for (unsigned int N = 1; N < this->maxiter + 1; ++N) { fout << N << separator << this->series.compute(N) << std::endl; } fout.close(); }