Home > Enterprise >  Save function output to .pdf
Save function output to .pdf

Time:09-30

I am iterating a function over columns in a dataframe as follows:

for name, column in train_set.iteritems():
    adfuller_test(column, name=column.name)

I want to save the output of these iterations to an external file, either .txt or .pdf formats. I found this method to create PDF files from a script:

import fpdf    
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=12)
pdf.cell(200, 10, txt=print(), ln=1, align="C")
pdf.output("Augmented Dickey Fuller Test.pdf")

However, how can I bring the output of the for loop inside txt? I tried storing the results of the loop in a list using list.append, but unfortunately the adfuller_test function returns NoneType objects.

UPDATE: The adfuller_test performs an Augmented Dickey Fuller test on time series data. The function is defined as:

def adfuller_test(series, signif=0.05, name='', verbose=False):
    """Perform ADFuller to test for Stationarity of given series and print report"""
    r = adfuller(series, autolag='AIC')
    output = {'test_statistic':round(r[0], 4), 'pvalue':round(r[1], 4), 'n_lags':round(r[2], 4), 'n_obs':r[3]}
    p_value = output['pvalue'] 
    def adjust(val, length= 6): return str(val).ljust(length)
    print(f'    Augmented Dickey-Fuller Test on "{name}"', "\n   ", '-'*47)
    print(f' Null Hypothesis: Data has unit root. Non-Stationary.')
    print(f' Significance Level    = {signif}')
    print(f' Test Statistic        = {output["test_statistic"]}')
    print(f' No. Lags Chosen       = {output["n_lags"]}')
    for key,val in r[4].items():
        print(f' Critical value {adjust(key)} = {round(val, 3)}')
    if p_value <= signif:
        print(f" => P-Value = {p_value}. Rejecting Null Hypothesis.")
        print(f" => Series is Stationary.")
    else:
        print(f" => P-Value = {p_value}. Weak evidence to reject the Null Hypothesis.")
        print(f" => Series is Non-Stationary.") 

and when run on a series it prints the following output (example):

 Null Hypothesis: Data has unit root. Non-Stationary.
 Significance Level    = 0.05
 Test Statistic        = -0.5388
 No. Lags Chosen       = 2
 Critical value 1%     = -3.437
 Critical value 5%     = -2.864
 Critical value 10%    = -2.568
 => P-Value = 0.8842. Weak evidence to reject the Null Hypothesis.
 => Series is Non-Stationary.

The script loops over a DataFrame with 30 columns and therefore returns 30 such outputs. What I would like to do is store these outputs in a file as they are printed in the terminal window. The file format does not matter, a .txt file would work as well.

CodePudding user response:

You can change the function adfuller_test in this way to return the description as a string instead of printing it to the console:

def adfuller_test(series, signif=0.05, name='', verbose=False):
    """Perform ADFuller to test for Stationarity of given series and print report"""
    description = []
    r = adfuller(series, autolag='AIC')
    output = {'test_statistic':round(r[0], 4), 'pvalue':round(r[1], 4), 'n_lags':round(r[2], 4), 'n_obs':r[3]}
    p_value = output['pvalue'] 
    def adjust(val, length= 6): return str(val).ljust(length)
    description.append(f'    Augmented Dickey-Fuller Test on "{name}"\n   {"-"*47}')
    description.append(f' Null Hypothesis: Data has unit root. Non-Stationary.')
    description.append(f' Significance Level    = {signif}')
    description.append(f' Test Statistic        = {output["test_statistic"]}')
    description.append(f' No. Lags Chosen       = {output["n_lags"]}')
    for key,val in r[4].items():
        description.append(f' Critical value {adjust(key)} = {round(val, 3)}')
    if p_value <= signif:
        description.append(f" => P-Value = {p_value}. Rejecting Null Hypothesis.")
        description.append(f" => Series is Stationary.")
    else:
        description.append(f" => P-Value = {p_value}. Weak evidence to reject the Null Hypothesis.")
        description.append(f" => Series is Non-Stationary.") 

    description = "\n".join(description)
    print(description)
    return description

This basically instantiate a list, stores all description strings and concatenate them line by line.

Saving this to a txt file is simple:

from pathlib import Path

description = []
for name, column in train_set.iteritems():
    description.append(adfuller_test(column, name=column.name))

description = "\n\n".join(description)

Path("output_file_name.txt").write_text(description)
  • Related