From 6b87843fc38c1646615ab0342a703f7ab3caf1cb Mon Sep 17 00:00:00 2001
From: Mohammad Akhlaghi reproduce/analysis/python
).
# Make a linear fit of an input data set
+#
# This Python script makes a linear fitting of a data consisting in time and
# population. It generates a figure in which the original data and the
# fitted curve is plotted. Finally, it saves the fitting parameters.
-# Original author:
-# Copyright (C) 2020, Raul Infante-Sainz infantesainz@gmail.com
-# Contributing author(s):
-# Copyright (C) YEAR, YourName YourSurname.
+#
+# Copyright (C) 2020 Raul Infante-Sainz infantesainz@gmail.com
+# Copyright (C) YYYY Your Name your-email@example.xxx
#
# This Python script is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
@@ -227,54 +227,45 @@ git push --tags # Push all ta
# Public License for more details. See http://www.gnu.org/licenses/.
# Necessary packages
+# Import necessary packages.
import sys
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
# Fitting function (linear fit)
-
def func(x, a, b):
return a * x + b
# Define input and output arguments
-
ifile = sys.argv[1] # Input file
ofile = sys.argv[2] # Output file
ofig = sys.argv[3] # Output figure
# Read the data from the input file.
-
data = np.loadtxt(ifile)
# Time and population:
-
# time ---------- x
-
# population ---- y
-
x = data[:, 0]
y = data[:, 1]
# Make the linear fit
-
params, pcov = curve_fit(func, x, y)
# Make and save the figure
-
plt.clf()
plt.figure()
-
plt.plot(x, y, 'bo', label="Original data")
plt.plot(x, func(x, *params), 'r-', label="Fitted curve")
-
plt.title('Population along time')
plt.xlabel('Time (year)')
plt.ylabel('Population (million people)')
plt.legend()
plt.grid()
-
plt.savefig(ofig, format='PDF', bbox_inches='tight')
+
# Save the fitting parameters
np.savetxt(ofile, params, fmt='%.3f')
@@ -354,19 +345,21 @@ git push # Push th
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details. See http://www.gnu.org/licenses/.
+Have a look at this Makefile and see the different parts. The first line is a descriptive title. Below, include your name, contact email, and finally, the copyright. Please, take your time in order to add all relevant @@ -378,7 +371,7 @@ echo "\newcommand{\popurl}{http://akhlaghi.org/data/template-tutorial}" > $@
TARGETS: PREREQUISITES
-RECIPE
+ RECIPE
In a rule, it is said how to construct the Now you can see this structure in our particular case: Here we have:TARGETS
from the
PREREQUISITES
, following the RECIPE
. Note that the white space at the
@@ -388,7 +381,7 @@ RECIPE
+ wget http://akhlaghi.org/data/template-tutorial/ESP.dat -O $@
(pop-data): | $(indir)
-wget http://akhlaghi.org/data/template-tutorial/ESP.dat -O $@
URL
.
(mtexdir)/getdata-analysis.tex: $(pop-data) | $(mtexdir)
-echo "\\newcommand{\\popurl}{http://akhlaghi.org/data/template-tutorial}" > $@
+ echo "\\newcommand{\\popurl}{http://akhlaghi.org/data/template-tutorial}" > $@
In this final rule we have:
@@ -445,12 +438,12 @@ echo "\\newcommand{\\popurl}{http://akhlaghi.org/data/template-tutorial}" > $ Makefiles have to be executed. You have to end up having:makesrc = initialize \
-download \
-getdata-analyse \
-delete-me \
-paper
+ download \
+ getdata-analyse \
+ delete-me \
+ paper
- As allways, read carefully all comments and information in order to know +
As always, read carefully all comments and information in order to know
what is going ong. Also, add your own comments and information in order to
be clear and explain each step with enough level of detail. If everything is
fine, now the project is ready to download the data in the make step. Try
@@ -512,13 +505,13 @@ paper
necessary because this directory is needed for saving the file ESP.txt
.
(odir):
-mkdir $@
+ mkdir $@
With all the previous definitions, now it is possible to set the rule for making the analysis:
(param-file): $(indir)/ESP.dat | $(odir)
-python reproduce/analysis/python/linear-fit.py $< $@ $(odir)/ESP.pdf
+ python reproduce/analysis/python/linear-fit.py $< $@ $(odir)/ESP.pdf
In this rule you have:
@@ -562,15 +555,12 @@ echo "\newcommand{\bfitparam}{$$b}" >> $@So, at the end you will have the final rule like this:
-(mtexdir)/getdata-analysis.tex: $(param-file) | $(mtexdir)
-
- echo "\\newcommand{\\popurl}{http://akhlaghi.org/data/template-tutorial}" > $@
-
-a=$$(cat $< | awk 'NR==1{print $1}')
-b=$$(cat $< | awk 'NR==2{print $1}')
-
-echo "\newcommand{\afitparam}{$$a}" >> $@
-echo "\newcommand{\bfitparam}{$$b}" >> $@
+ (mtexdir)/getdata-analysis.tex: $(param-file) | $(mtexdir)
+ echo "\\newcommand{\\popurl}{http://akhlaghi.org/data/template-tutorial}" > $@
+ a=$$(cat $< | awk 'NR==1{print $1}')
+ b=$$(cat $< | awk 'NR==2{print $1}')
+ echo "\newcommand{\afitparam}{$$a}" >> $@
+ echo "\newcommand{\bfitparam}{$$b}" >> $@
Important notes: you have to use two $
in order to use the bash $
character inside of a Make rule. Also, note that you have to put >>
in
@@ -582,56 +572,64 @@ echo "\newcommand{\bfitparam}{$$b}" >> $@
parameters. If you add the necessary comments and information, the final
Makefile would look similar to:
# Download data and linear fitting for the tutorial
+#
# In this Makefile, data for the tutorial is downloaded. Then, a Python
# script is used to make a linear fitting. Finally, fitted parameters as
# well as the URL is saved into a TeX macro.
+#
# Copyright (C) 2020 Raul Infante-Sainz infantesainz@gmail.com
# Copyright (C) YYYY Your Name your-email@example.xxx
+#
# This Makefile is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
+#
# This Makefile is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details. See http://www.gnu.org/licenses/.
+
# Download data for the tutorial
# ------------------------------
# The input file is defined and downloaded using the following rule
pop-data = $(indir)/ESP.dat
$(pop-data): | $(indir)
-# Use wget to download the data
-wget http://akhlaghi.org/data/template-tutorial/ESP.dat -O $@
+ # Use wget to download the data
+ wget http://akhlaghi.org/data/template-tutorial/ESP.dat -O $@
+
# Output directory
# ----------------
# Small rule for constructing the output directory, previously defined
odir = $(BDIR)/fit-parameters
$(odir):
-# Build the output directory
-mkdir $@
+ mkdir $@
+
# Linear fitting of the data
# --------------------------
# The output file is defined into the output directory. The fitted
# parameters will be saved into this directory by the Python script.
param-file = $(odir)/ESP.txt
$(param-file): $(indir)/ESP.dat | $(odir)
-# Invoke Python to run the script with the input data
-python reproduce/analysis/python/linear-fit.py $< $@ $(odir)/ESP.pdf
+ # Invoke Python to run the script with the input data
+ python reproduce/analysis/python/linear-fit.py $< $@ $(odir)/ESP.pdf
+
# TeX macros final target
# -----------------------
# This is how we write the necessary parameters in the final PDF. In this
# rule, new TeX parameters are defined from the URL, and the fitted
# parameters.
$(mtexdir)/getdata-analysis.tex: $(param-file) | $(mtexdir)
-# Write the URL into the target
-echo "\newcommand{\popurl}{http://akhlaghi.org/data/template-tutorial}" > $@
+ # Write the URL into the target
+ echo "\newcommand{\popurl}{http://akhlaghi.org/data/template-tutorial}" > $@
-# Read the fitted parameters and save them into the target
-a=$$(cat $< | awk 'NR==1{print $1}')
-b=$$(cat $< | awk 'NR==2{print $1}')
+ # Read the fitted parameters into shell variables.
+ a=$$(cat $< | awk 'NR==1{print $1}')
+ b=$$(cat $< | awk 'NR==2{print $1}')
-echo "\newcommand{\afitparam}{$$a}" >> $@
-echo "\newcommand{\bfitparam}{$$b}" >> $@
+ # Write the parameters into the target as LaTeX macros.
+ echo "\newcommand{\afitparam}{$$a}" >> $@
+ echo "\newcommand{\bfitparam}{$$b}" >> $@
Have look at this Makefile and note that it is what it has been described above. Take your time for making useful comments and modifying whatever you -- cgit v1.2.1