66import logging
77from typing import Dict
88from pathlib import Path
9+ from datetime import datetime
910
1011
1112# Exported function definition -------------------------------------------------
@@ -15,7 +16,7 @@ def run_plot_functions(
1516 query_results_dir : str | Path ,
1617 plot_functions_dir : str | Path ,
1718 output_dir : str | Path ,
18- log_file : str | Path ,
19+ log_dir : str | Path = None ,
1920 rerun_all : bool = False ,
2021 rerun_functions : list [str ] | None = None
2122) -> None :
@@ -26,30 +27,84 @@ def run_plot_functions(
2627 ----------
2728 query_results_dir : str or Path
2829 The path to the directory containing .csv results from SQL queries.
30+
2931 plot_functions_dir : str or Path
3032 The path to the directory containing Python files with plotting
3133 functions.
34+
3235 output_dir : str or Path
3336 The path to the directory where the plots should be saved.
34- log_file : str or Path
35- The path to the log file where messages will be saved.
37+
38+ log_dir : str or Path
39+ The directory where the log file should be saved. A timestamped log
40+ file will be created automatically. Default is the current working dir.
41+
3642 rerun_all : bool, optional
3743 Force rerun of all plotting functions, by default False.
44+
3845 rerun_functions : list of str, optional
3946 List of specific plotting functions to rerun, by default None.
47+
48+ Examples
49+ --------
50+ Basic usage:
51+
52+ Assuming you have a directory structure like this:
53+
54+ .. code-block:: text
55+
56+ project/
57+ ├── query_results/
58+ │ └── data.csv
59+ ├── plot_functions/
60+ │ └── plot_example.py
61+ └── output/
62+ └── log.txt
63+
64+ You can call the function as follows:
65+
66+ .. code-block:: python
67+
68+ from pathlib import Path
69+ from sqlite_manager.run_plot_functions import run_plot_functions
70+
71+ run_plot_functions(
72+ query_results_dir=Path("project/query_results"),
73+ plot_functions_dir=Path("project/plot_functions"),
74+ output_dir=Path("project/output"),
75+ log_file=Path("project/log.txt"),
76+ rerun_all=True
77+ )
78+
79+ This will:
80+ - Load all `.csv` files from the `query_results/` directory.
81+ - Execute all plotting functions in `plot_functions/`.
82+ - Save plots to the `output/` directory.
83+ - Log messages to `log.txt`.
4084 """
85+ if log_dir is None : # Set default value
86+ log_dir = Path .cwd ()
87+
4188 # Convert string paths to pathlib.Path objects
4289 query_results_dir = Path (query_results_dir )
4390 plot_functions_dir = Path (plot_functions_dir )
4491 output_dir = Path (output_dir )
45- log_file = Path (log_file )
92+ log_dir = Path (log_dir )
4693
47- # Create output directory if it doesn't exist
48- os .makedirs (
94+ os .makedirs ( # Create output directory if it doesn't exist
4995 output_dir ,
5096 exist_ok = True
5197 )
5298
99+ os .makedirs ( # Create log directory if it doesn't exist
100+ log_dir ,
101+ exist_ok = True
102+ )
103+
104+ # Create a timestamped log file name
105+ timestamp = datetime .now ().strftime ("%Y_%m_%d-%H_%M_%S" )
106+ log_file = log_dir / f"plot_functions_{ timestamp } .log"
107+
53108 logger = configure_logging (log_file = log_file )
54109
55110 # Load all CSV files as DataFrames into a dictionary
0 commit comments