Files
Territory-Analysis---Mapper/run_all.py

119 lines
4.4 KiB
Python

import os
import sys
import argparse
import pandas as pd
from datetime import datetime
# Import the refactored functions from other scripts
from process_territories import process_data
from analysis import generate_analysis_artifacts
from category_analysis import generate_category_map
def process_and_save(addresses_file, boundaries_file):
"""
Runs the processing script and saves the result to a 'Final' CSV.
Returns the path to the generated file.
"""
print("\n--- Step 1: Processing territory files ---")
try:
# Process data in memory
processed_df = process_data(addresses_file, boundaries_file)
# Save the processed DataFrame to a CSV file
date_str = datetime.now().strftime('%b %Y')
output_filename = f'Okinawa Territory {date_str} - Final.csv'
processed_df.to_csv(output_filename, index=False)
print(f"✓ Success! Generated file: {output_filename}")
return output_filename
except (FileNotFoundError, Exception) as e:
print(f"\nError during file processing: {e}", file=sys.stderr)
sys.exit(1)
def analyze_from_file(processed_file_path):
"""
Reads a processed file and runs the analysis scripts on it.
"""
print("\n--- Step 2: Running analysis from file ---")
try:
# Read the processed file into a DataFrame
df = pd.read_csv(processed_file_path)
# Run the analysis functions
generate_analysis_artifacts(df)
generate_category_map(df)
print("\n✓ Analysis complete!")
print("Generated files: analysis.md, map.html, category_map.html")
except FileNotFoundError as e:
print(f"\nError: Processed file not found at '{processed_file_path}'.", file=sys.stderr)
sys.exit(1)
except Exception as e:
print(f"\nAn unexpected error occurred during analysis: {e}", file=sys.stderr)
sys.exit(1)
def full_run_in_memory(addresses_file, boundaries_file):
"""
Processes data and runs analysis entirely in memory.
"""
print("\n--- Running full pipeline in memory ---")
try:
# Step 1: Process data
print("Processing data...")
processed_df = process_data(addresses_file, boundaries_file)
print("✓ Data processing complete.")
# Step 2: Run analysis
print("\nRunning analysis...")
generate_analysis_artifacts(processed_df)
generate_category_map(processed_df)
print("\n✓ Analysis complete!")
print("Generated files: analysis.md, map.html, category_map.html")
except (FileNotFoundError, Exception) as e:
print(f"\nAn error occurred during the full run: {e}", file=sys.stderr)
sys.exit(1)
def main():
"""Parses command-line arguments and orchestrates the workflow."""
parser = argparse.ArgumentParser(
description="Territory Analysis Tool v1.2.0",
formatter_class=argparse.RawTextHelpFormatter
)
subparsers = parser.add_subparsers(dest="command", required=True, help="Available commands")
# Sub-command for 'process'
parser_process = subparsers.add_parser("process", help="Step 1: Process raw files and save the result to a CSV.")
parser_process.add_argument("--addresses", required=True, help="Path to the addresses CSV file.")
parser_process.add_argument("--boundaries", required=True, help="Path to the boundaries CSV file.")
# Sub-command for 'analyze'
parser_analyze = subparsers.add_parser("analyze", help="Step 2: Run analysis on a processed 'Final' CSV file.")
parser_analyze.add_argument("--input", required=True, help="Path to the processed 'Final' CSV file.")
# Sub-command for 'full-run'
parser_full_run = subparsers.add_parser("full-run", help="Run the full pipeline (process and analyze) in memory.")
parser_full_run.add_argument("--addresses", required=True, help="Path to the addresses CSV file.")
parser_full_run.add_argument("--boundaries", required=True, help="Path to the boundaries CSV file.")
args = parser.parse_args()
if args.command == "process":
process_and_save(args.addresses, args.boundaries)
elif args.command == "analyze":
analyze_from_file(args.input)
elif args.command == "full-run":
full_run_in_memory(args.addresses, args.boundaries)
if __name__ == "__main__":
# Ensure the script runs in its own directory context
os.chdir(os.path.dirname(os.path.abspath(__file__)))
main()