Skip to content
Snippets Groups Projects
report.py 20.7 KiB
Newer Older
import subprocess
from pathlib import Path
maimouni.mouhcine's avatar
maimouni.mouhcine committed
    j=0
    for arg in args:
        if isinstance(arg, str):
            to_report.append(arg)
maimouni.mouhcine's avatar
maimouni.mouhcine committed
        elif isinstance(arg, list):
            to_report.extend(arg)
maimouni.mouhcine's avatar
maimouni.mouhcine committed
            df_name = 'df' + str(j)
            j+=1
maimouni.mouhcine's avatar
maimouni.mouhcine committed
    #print(to_report)
    latex_report = ""
    latex_report += r"""\documentclass[a4paper,10pt]{article}
    \usepackage{fancyhdr}
    \usepackage{graphicx}
    \usepackage{geometry}
    \geometry{a4paper, left=2cm, right=2cm, top=1.5cm, bottom=3cm }
    \usepackage{caption}
    \usepackage{subcaption}
    \usepackage{hyperref}
    \usepackage{hyphenat}
    \usepackage{etoolbox,fancyhdr,xcolor}
    \newcommand{\headrulecolor}[1]{\patchcmd{\headrule}{\hrule}{\color{#1}\hrule}{}{}}
    \newcommand{\footrulecolor}[1]{\patchcmd{\footrule}{\hrule}{\color{#1}\hrule}{}{}}
    \renewcommand{\headrulewidth}{1pt}
    \headrulecolor{red!100}%
    \renewcommand{\footrulewidth}{1pt}
    \footrulecolor{red!100}%
    \graphicspath{{images/}, {Figures/}}    
    \fancyhf{}
    \fancyhead[R]{\includegraphics[width=0.1\textwidth]{logo_cefe.png}}
    \fancyhead[L]{PACE - NIRS Analysis Report}
    
    \fancyfoot[L]{Project Name to fill}
    \fancyfoot[C]{Plateforme d'Analyses Chimiques en Ecologie}
    \fancyfoot[R]{\thepage}
    
    \setlength{\headheight}{15mm}
    \pagestyle{fancy}
    
    \usepackage{times}
    
    \begin{document}
    
    \noindent
    \begin{center}
    \textbf{{\Large NIRS WORKFLOW REPORT}} \\
    \end{center}"""

    if 'sample' in to_report:
        latex_report += r"""\noindent
        \textbf{QUERY: } You asked for Sample selection.
        Specify bibliographic refs \cite{Lesnoff2020} to describe the algorithms used.\\
        \noindent
        \textbf{INPUTS:} Specify here input file names \& parameters.\\
        
        """

    if 'model' in to_report:
        latex_report += r"""\subsection*{Data Visualization}

        Here we have a sub-heading. There is no blank line after the sub-heading. You can have one level of subheadings but not a third i.e. you cannot have Section 1.1.1 as a subheading.
        \begin{center}
        Spectral Data Visualization
        \end{center}
        
        \begin{figure}[ht]
            \centering
            \includegraphics[width=1\linewidth]{""" + to_report[1] + r"""}
            \label{fig:votre_graphique}
        \end{figure}
        \textbf{Exploratory Data Analysis-Multivariable Data Analysis}
        \begin{figure}[ht]
            \centering
            \includegraphics[width=1\linewidth]{""" + to_report[2] + r"""}
            \label{fig:votre_graphique}
        \end{figure}      

        """

        # Ajout de la conversion DataFrame en LaTeX
        latex_report += r"""
        \begin{center}
        """ + df0.to_latex(escape=True) + r"""
        \end{center}
        """
        latex_report += r"""


        After a list, you must leave a single blank line and remember to add the indent if you are starting a new paragraph.
        \begin{center}
        II - Model creation
        \end{center}
    """
        if 'full_plsr' in to_report:
            latex_report += r"""
            Model Choisi : Full Plsr
            -- Spectral preprocessing info --
            """
            latex_report += r"""
            \begin{center}
                        Model performance 

            \end{center}
            """
            latex_report += r"""
            Model performance 
            """
            latex_report += r"""
            \begin{center}
            """ + df2.to_latex(escape=True) + r"""
            \end{center}
            """

            latex_report += r"""
            \newpage
            Cross Validation
            \begin{figure}[ht]
            \centering
            \includegraphics[width=0.9\linewidth]{""" + to_report[3] + r"""}
            \label{fig:votre_graphique}
            \end{figure}  
            After a list, you must leave a single blank line and remember to add the indent if you are starting a new paragraph.
            After a list, you must leave a single blank line and remember to add the indent if you are starting a new paragraph.
            -- Cross-Validation Summary--
            latex_report += r"""
            \begin{center}
            """ + df3.to_latex(escape=True) + r"""
            \end{center}
            -- Out-of-Fold Predictions Visualization (All in one) --
            \begin{figure}[ht]
            \centering
            \includegraphics[width=0.9\linewidth]{""" + to_report[4] + r"""}
            \label{fig:votre_graphique}
            \end{figure}  

            III - Model Diagnosis
            \begin{figure}[h]
                \centering
                \begin{minipage}[b]{0.5\linewidth}
                    \centering
                    \includegraphics[width=1.2\linewidth]{""" + to_report[5] + r"""}
                \end{minipage}%
                \begin{minipage}[b]{0.5\linewidth}
                    \centering
                    \includegraphics[width=1.2\linewidth]{""" + to_report[6] + r"""}
                \end{minipage}%
                \caption{\label{fig:frog}PCA}
            \end{figure}

            """






    if 'predict' in to_report:
        latex_report += r"""\noindent
        \textbf{QUERY: } You asked for Predictions.
        Specify bibliographic refs \cite{scikit-learn} to describe the algorithms used.\\
        \noindent
        \textbf{INPUTS:} Specify here input file names \& parameters.
        """

    latex_report += r"""\section*{RESULTS}
    Please follow this template exactly and DO NOT CHANGE THE FONT SIZE, MARGINS, HEADER, FOOTER, ETC OF THE TEMPLATE.
    Start typing your paper in this font in this way. Section headings are to be in 10pt bold and full caps. Number headings consecutively. Leave a single blank line before each Section Heading and one blank line between the heading and the first line of text. When you want to start a new paragraph do not drop a line do it simply like this. Use MS Word/Latex formatting exactly as given in this paragraph. Do NOT change the spacing 'before' and 'after' to try and squeeze more in.
    This is now a new paragraph which starts with a tab space and when you are starting new sentences you must put a space after the full stop. Just like this. Text should be single spaced, left and right justified, providing 25.4mm left margin and 25.4mm right margin. Leave a 25.4mm margin at top and a 25.4mm margin at bottom and the page size should be set as A4. You will also notice that the font to use is Times New Roman and the font size is 10pt.
    """
maimouni.mouhcine's avatar
maimouni.mouhcine committed
    if 'sample' and 'csv' in to_report:
        latex_report += r"""\subsection*{Data Visualization}

        Here we have a sub-heading. There is no blank line after the sub-heading. You can have one level of subheadings but not a third i.e. you cannot have Section 1.1.1 as a subheading.
maimouni.mouhcine's avatar
maimouni.mouhcine committed
        \begin{center}
        Spectral Data Visualization
        \end{center}
        
        \begin{figure}[ht]
            \centering
            \includegraphics[width=1\linewidth]{""" + to_report[3] + r"""}
            \label{fig:votre_graphique}
        \end{figure}

        \textbf{Exploratory Data Analysis-Multivariable Data Analysis}
        """ 
        if 'hdb' in to_report:
            latex_report += r"""
            \begin{figure}[ht]
                \centering
                \includegraphics[width=0.5\linewidth]{""" + to_report[5] + r"""}
                \label{fig:votre_graphique}
            \end{figure}
            """        
        if 'kmeans' in to_report or 'AP' in to_report:
            latex_report += r"""
            \begin{figure}[h]
                \centering
                \begin{minipage}[b]{0.33\linewidth}
                    \centering
                    \includegraphics[width=\linewidth]{""" + to_report[4] + r"""}
                \end{minipage}%
                \begin{minipage}[b]{0.33\linewidth}
                    \centering
                    \includegraphics[width=\linewidth]{""" + to_report[5] + r"""}
                \end{minipage}%
                \caption{\label{fig:frog}PCA}
            \end{figure}
    """

            latex_report += r"""
                This is now a new paragraph which starts with a tab space and when you are starting new sentences you must put a space after the full stop. Just like this. Text should be single spaced, left and right justified, providing 25.4mm left margin and 25.4mm right margin. Leave a 25.4mm margin at top and a 25.4mm margin at bottom and the page size should be set as A4. You will also notice that the font to use is Times New Roman and the font size is 10pt.
                
       """
        latex_report += r"""
            This is now a new paragraph which starts with a tab space and when you are starting new sentences you must put a space after the full stop. Just like this. Text should be single spaced, left and right justified, providing 25.4mm left margin and 25.4mm right margin. Leave a 25.4mm margin at top and a 25.4mm margin at bottom and the page size should be set as A4. You will also notice that the font to use is Times New Roman and the font size is 10pt.
            """

        latex_report += r"""

        \begin{center}
            Graphique des PCA
        \end{center}
        \begin{figure}[h]
            \centering
            \begin{minipage}[b]{0.33\linewidth}
                \centering
                \includegraphics[width=\linewidth]{""" + to_report[6] + r"""}
            \end{minipage}%
            \begin{minipage}[b]{0.33\linewidth}
                \centering
                \includegraphics[width=\linewidth]{""" + to_report[7] + r"""}
            \end{minipage}%
            \begin{minipage}[b]{0.33\linewidth}
                \centering
                \includegraphics[width=\linewidth]{""" + to_report[8] + r"""}
            \end{minipage}
            \caption{\label{fig:frog}PCA}
        \end{figure}
        """
        latex_report += r"""
        This is now a new paragraph which starts with a tab space and when you are starting new sentences you must put a space after the full stop. Just like this. Text should be single spaced, left and right justified, providing 25.4mm left margin and 25.4mm right margin. Leave a 25.4mm margin at top and a 25.4mm margin at bottom and the page size should be set as A4. You will also notice that the font to use is Times New Roman and the font size is 10pt.


        \begin{center}
        \end{center}
        \begin{figure}[h]
            \centering
            \begin{minipage}[b]{0.45\linewidth}
                \centering
                \includegraphics[width=\linewidth]{""" + to_report[9] + r"""}
            \end{minipage}%
            \hspace{0.05\linewidth} % Adds space between the two images
            \begin{minipage}[b]{0.45\linewidth}
                \centering
                \includegraphics[width=\linewidth]{""" + to_report[10] + r"""}
            \end{minipage}
            \caption{\label{fig:frog}PCA}
        \end{figure}
        """
        latex_report+= r"""
        This is now a new paragraph which starts with a tab space and when you are starting new sentences you must put a space after the full stop. Just like this. Text should be single spaced, left and right justified, providing 25.4mm left margin and 25.4mm right margin. Leave a 25.4mm margin at top and a 25.4mm margin at bottom and the page size should be set as A4. You will also notice that the font to use is Times New Roman and the font size is 10pt.

        \textbf{Selected samples for chemical analysis}
        \newline 
        \newline 
        \newline 
            """
        num_samples = to_report[0]
        nb_clu=to_report[1]

        if 'kmeans' in to_report or 'AP' in to_report:
            latex_report += df0.to_latex(escape=True)
        latex_report += r"""
maimouni.mouhcine's avatar
maimouni.mouhcine committed
        \item The total number of samples: """ + str(num_samples) + r"""
        \item The number of selected samples for chemical analysis: """ + str(nb_clu) + r"""
maimouni.mouhcine's avatar
maimouni.mouhcine committed
        After a list, you must leave a single blank line and remember to add the indent if you are starting a new paragraph.

        """
    if 'sample' and 'dx' in to_report:
        latex_report += r"""\subsection*{Data Visualization}
        \begin{center}
            Graphique des PCA
        \end{center}
        \begin{figure}[h]
            \centering
            \begin{minipage}[b]{0.33\linewidth}
                \centering
                \includegraphics[width=\linewidth]{plot_axe1_axe2.png}
            \end{minipage}%
            \begin{minipage}[b]{0.33\linewidth}
                \centering
                \includegraphics[width=\linewidth]{plot_axe1_axe3.png}
            \end{minipage}%
            \begin{minipage}[b]{0.33\linewidth}
                \centering
                \includegraphics[width=\linewidth]{plot_axe2_axe3.png}
            \end{minipage}
            \caption{\label{fig:frog}PCA}
        \end{figure}
            """
    latex_report += r"""
    This is now a new paragraph which starts with a tab space and when you are starting new sentences you must put a space after the full stop. Just like this. Text should be single spaced, left and right justified, providing 25.4mm left margin and 25.4mm right margin. Leave a 25.4mm margin at top and a 25.4mm margin at bottom and the page size should be set as A4. You will also notice that the font to use is Times New Roman and the font size is 10pt.
        """
    if 'model' in to_report:
        latex_report += r"""
        \subsection*{Sample Selection}

        You can have as many sub-headings in a section as you want to. Note that sub-headings have a 6pt spacing after them rather than a blank line but they are preceded by a blank line. The number of sections and sub-sections is up to you, as are the titles of each of them and this will be driven by the content of your report.

        \subsection*{Model Creation}

        You can have as many sub-headings in a section as you want to. Note that sub-headings have a 6pt spacing after them rather than a blank line but they are preceded by a blank line. The number of sections and sub-sections is up to you, as are the titles of each of them and this will be driven by the content of your report.
        """
    if 'predict' in to_report:
        latex_report += r"""
        \subsection*{Predictions}

        You can have as many sub-headings in a section as you want to. Note that sub-headings have a 6pt spacing after them rather than a blank line but they are preceded by a blank line. The number of sections and sub-sections is up to you, as are the titles of each of them and this will be driven by the content of your report.
        """

    if 'help' in to_report:
        latex_report += r"""\subsection*{How to include equations, figures and tables?}

        Some of us like to include a formula or two and these should be referred to in the test in the form equation 1. You must type in equations using the equation editor and all symbols should be explained within the text of your manuscript. However you may prefer to include a separate section detailing all nomenclature. Never paste equations in from a paper or other source. Leave a blank line before and after the equation:

        \begin{equation}
        x = \frac{-b \pm \sqrt{b^2 - 4ac} }{2a}
        \end{equation}

        where $x$ is a number; $a$, $b$, and $c$ are other numbers.

        \begin{equation}
        BCP = 176R+28G+46B
        \end{equation}

        where, $BCP$, $R$, $G$ and $B$ are also useful numbers.

        Figure \ref{fig_regression} shows a graph.  Figures / diagrams / photos are to be centred, with the reference and caption printed below the figure. The lettering used in the illustrations should be easily legible.  Illustrations are to be referred to as figures, and must be quoted in the text. One blank line should be left between the figure caption and the next paragraph. Please ensure that all figures are of the highest quality. Keep figures as simple as possible. Avoid excessive notes. Photographs must have a resolution of at least 300 dpi. The use of colour is allowed on figures. Note that the test does not wrap around the figure but if you do not like the wasted space then it is acceptable to put two figures side by side but they must be e.g. a 'Figure \ref{fig_bridge1}' and 'Figure \ref{fig_bridge2}' and not 'Figure 1' and 'Figure 2'. See the example given as Figure \ref{fig_twobridges}. However all figures must be as close as possible to the location where they are first referred to in the text. The figure is also not surrounded by an unnecessary box/border. Remove this when inserting figures from MS Excel.


        \begin{figure}[ht]
        \centering
        \includegraphics[height=6.6cm]{figures/fig_regression}
        \caption{An interesting plot (note that figure captions go BELOW THE FIGURE).}
        \label{fig_regression}
        \end{figure}


        \begin{figure}[ht]
        \centering
        \begin{subfigure}[b]{0.45\textwidth}
        \centering
        \includegraphics[width=\textwidth]{figures/fig_bridge1}
        \caption{Bridge 1}
        \label{fig_bridge1}
        \end{subfigure}
        \hfill
        \begin{subfigure}[b]{0.45\textwidth}
        \centering
        \includegraphics[width=\textwidth]{figures/fig_bridge2}
        \caption{Bridge 2}
        \label{fig_bridge2}
        \end{subfigure}
        \caption{Walton-on-Thames Bridge (a) wide shot and (b) the underside of the deck
        (photos taken by P. J. Vardanega, used with permision)}
        \label{fig_twobridges}
        \end{figure}


        \begin{table}[h]
        \begin{center}
        \caption{Summary of the database (note that table captions go ABOVE THE TABLE)}
        \begin{tabular}{ |l|c|l| }
        \hline
        \textbf{Column heading} & \textbf{Column heading}    & \textbf{Column heading} \\ \hline
        Table Text     & 10     & Falling head permeameter \\  \hline
        Concrete       & 2      & Strong \\ \hline
        Steel          & 1      & Stronger \\ \hline
        Timber         & 3      & Weak \\
        \hline
        \end{tabular}
        \label{tab_materials}
        \end{center}
        \end{table}
        """
    latex_report += r"""\newpage
    \section*{ACKNOWLEDGEMENTS}
    This tool is provided by the Chemical Analysis Platform for Ecology - Montpellier, France.\\
            Thanks to Abderrahim DIANE, Mouhcine MAIMOUNI, Alexandre Granier, Remy Beugnon, Vincent Negre et Nicolas Barthes.\\
            Source code available at \href{https://src.koda.cnrs.fr/cefe/pace/nirs_workflow}{CNRS forge}.

    \fontsize{8}{9}\selectfont
    \bibliographystyle{apalike}
    % \bibliographystyle{abbrv}
    \bibliography{refs.bib}
    \clearpage
    \end{document}"""

    # export the .tex file in the Report folder
    filename_path = Path("Report/")
    filename = r'report.tex'
    with open(filename_path / filename, 'w+') as latex_file:
        latex_file.write(latex_report)

# create the Tex file - sections in args will be displayed: {'sample':'Sample Selection';'model':'Model Creation';'predict':'Predictions';'help':'LaTEX help for figs and tables';}
# latex_report = report('sample', 'predict',)
def compile_latex():
    # path to pdflatex
    # pdflatex_path = Path("C:/Users/maimouni/AppData/Local/Programs/MiKTeX/miktex/bin/x64/")
    from config.config import pdflatex_path
    import os
    filename_path = Path("Report/")
    filename = 'report.tex'
    # run pdflatex with bibtex compilation (2nd run)
    for i in range(4):
        if i == 1:
            proc = subprocess.Popen([pdflatex_path / 'bibtex.exe', filename[:-4]], cwd = filename_path)
            proc.communicate()
        else:
            proc = subprocess.Popen([pdflatex_path / 'pdflatex.exe', filename], cwd = filename_path)
            proc.communicate()
    os.rename(filename_path / 'report.pdf', filename_path / 'figures' / 'report.pdf')
    # remove pdflatex compilation files
    extensions = ['.log', '.aux', '.bbl', '.blg', '.out', '.tex']
    for ext in extensions:
        os.unlink(str(filename_path / filename[:-4]) + ext)
    proc = subprocess.Popen([str(filename[:-4]) + '.pdf'], cwd = filename_path / 'figures', shell=True)
    import os
    import zipfile

    folder_path = Path('Report/figures')
    zip_path = Path('Report/')
    with zipfile.ZipFile(zip_path / 'archive_zipfile.zip', 'w', compression=zipfile.ZIP_DEFLATED, compresslevel=9) as zf:
        for root, dirs, files in os.walk(folder_path):
            for file in files:
                file_path = os.path.join(root, file)
                arcname = os.path.relpath(file_path, folder_path)
                zf.write(file_path, arcname)

maimouni.mouhcine's avatar
maimouni.mouhcine committed
# compile_latex()