Graph Plotting Utility
Author: Muhammed Emre Bayraktaroglu
This documentation provides an overview of the Graph Plotting Utility, which is designed to generate and encode food consumption-related graphs for different countries. The utility processes country-level predictions and outputs visualizations as base64-encoded images.
Overview
The Graph Plotting Utility:
- Generates Food Consumption Score (FCS) and Reduced Coping Strategy Index (rCSI) graphs.
- Applies confidence intervals for visual clarity.
- Encodes graphs as base64 strings for seamless integration.
- Cleans up temporary image files after encoding.
Requirements and Setup
Before using this utility, ensure you have the following:
- Python 3.8+ installed.
matplotlib
installed for graph generation.
Directory Structure
The script assumes the following folder structure:
project_root/
├── plots/ # Stores temporary image files before encoding
│ ├── country_FCS.png
│ ├── country_RCSI.png
After execution, the plots
folder will be removed, leaving only base64-encoded images.
Key Features
Graph Generation
The script generates FCS and rCSI visualizations based on country-specific predictions.
Base64 Encoding
The generated graphs are converted into base64 strings for easy embedding.
Temporary File Cleanup
Ensures that all intermediate image files are deleted after encoding.
Function(s)
Function: plot_fcs(country_data)
Generates and encodes an FCS graph for a given country.
Process:
- Extracts FCS data (dates, values, confidence intervals) from
country_data
. - Plots a time-series graph with confidence shading.
- Saves the graph as a temporary PNG file.
- Encodes the image in base64 format.
- Deletes the temporary image file.
Exceptions Handled:
KeyError
: If necessary data is missing.FileNotFoundError
: If the image file cannot be located post-generation.PermissionError
: If the script lacks the necessary permissions to delete files.
Function: plot_rcsi(country_data)
Generates and encodes an rCSI graph for a given country.
Process:
- Extracts rCSI data (dates, values, confidence intervals) from
country_data
. - Plots a time-series graph with confidence shading.
- Saves the graph as a temporary PNG file.
- Encodes the image in base64 format.
- Deletes the temporary image file.
Exceptions Handled:
KeyError
: If necessary data is missing.FileNotFoundError
: If the image file cannot be located post-generation.PermissionError
: If the script lacks the necessary permissions to delete files.
Function: generate_graphs(data)
Processes a dataset to generate and encode FCS and rCSI graphs for multiple countries.
Process:
- Iterates through a list of country dictionaries.
- Calls
plot_fcs()
andplot_rcsi()
for each country. - Stores the resulting base64-encoded images in the dataset.
- Cleans up the
plots
directory after processing.
Exceptions Handled:
OSError
: If the script encounters issues removing theplots
directory.
Function: base64_encode_image(image_path)
Encodes an image file as a base64 string.
Process:
- Reads the image file in binary mode.
- Encodes the file using base64.
- Returns the encoded string.
Example Usage
To execute the script, call generate_graphs()
with a dataset:
country_data_list = [
{"country_name": "Country A", "fcs_prediction": [...], "rcsi_prediction": [...]},
{"country_name": "Country B", "fcs_prediction": [...], "rcsi_prediction": [...]},
]
generate_graphs(country_data_list)
If no data is available for a country, the script will print:
No FCS data available for Country A
No RCSI data available for Country A
Upon successful encoding, it prints:
FCS Graph saved: plots/Country_A_FCS.png
RCSI Graph saved: plots/Country_A_RCSI.png
What Happens Under the Hood?
- Data Extraction: The script retrieves FCS and rCSI predictions from country data.
- Graph Generation: Uses Matplotlib to generate visual representations.
- Base64 Encoding: Converts the images into a string format for easy transfer.
Conclusion
The Graph Plotting Utility is an efficient tool for visualizing food consumption trends across different countries.