Unifying Python and R plots - 3C-SCSU/Avatar GitHub Wiki

Refactoring Plotscode

Motivation

The previous implementation of the Python and R scripts in plotscode had redundant logic spread across six different files. Specifically, the backward.py and backward.R scripts performed the same operations as forward.py and forward.R, but for different datasets

Changes and Improvements

1. Dependency Management for R Scripts

We introduced a requirements.R script. Running this script will install all necessary dependencies required to execute the R scripts within the plotscode directory

2. Refactoring BrainWaveAnalysis Class

Previous Implementation

  • The BrainWaveAnalysis class constructor required a single file path as input

New Implementation

  • The constructor now accepts:
    • A collection of categories: backward, forward, land, left, right, and takeoff
    • A base file path, which points to the root directory containing the brainwaves-csv dataset
  • Add a process_all_categories method that sequentially processes each category
  • Enhance the plot method to create a new directory for storing generated plots

3. Combining Core Processing Logic

Since process_all_categories processes each category using existing methods, we consolidated all the logic into a single method, analyze_category, which:

  • Takes a filepath as input
  • Integrates the functionalities of multiple functions:
    • clean_data
    • reshape_data
    • bin_data
    • prepare_for_plotting
    • fit_model
    • predict_data

4. Introducing the Controller Class

Previously, the Python scripts only had one responsibility: executing the R script. To improve extensibility, we introduced a Controller class:

  • The constructor takes the file path of the R script.
  • The execute_r_script method uses rpy2 to run the R script.