Integrate Python call to R - 3C-SCSU/Avatar GitHub Wiki
Analyzing Brain Wave Signals with the BrainWaveAnalysis Class
The BrainWaveAnalysis R6 class in R is designed to analyze and visualize brain wave signal data from CSV files. Follow the steps below to use this class in your workflow.
Prerequisites
- Ensure the following R libraries are installed before using the class: install.packages(c("R6", "tidyverse", "readr", "janitor", "stringr"))
File Structure
- Place your CSV files in the directory: * /Avatar/plots/plot_waves/brainwaves-csv/backward
- The CSV files should contain columns with brainwave signal data, including sample_index and exg columns for each channel.
Define the Class
- Use the provided R script to define the BrainWaveAnalysis class in your R environment. Copy and paste the code from the repository.
Initialize the Class:
- Create an instance of the BrainWaveAnalysis class by specifying the directory path containing the CSV files: analysis <- BrainWaveAnalysis$new("/Avatar/plots/plot_waves/brainwaves-csv/backward")
Methods Overview: The class performs the following steps automatically
- Data Cleaning: Cleans column names using the janitor library.
- Data Reshaping: Converts wide-format data into long-format for easier processing.
- Data Binning: Groups data into bins for improved visualization.
- Subset Selection: Selects every 120th row for efficient plotting.
- Model Fitting: Fits a polynomial model to the data.
- Prediction: Generates fitted values using the model.
- Plotting: Visualizes the raw and fitted data with ggplot2.
Run the Analysis
- After initializing the class, the analysis will run automatically, generating a plot of the brain wave signals.
Plot Interaction
- The generated plot will open in a new graphics device.
- To close the plot, press Enter in the R console.
How to Integrate R and Python for BrainWaveAnalysis
Prerequisites
- Install rpy2 in your Python environment **pip install rpy2 **
- Ensure that R is installed on your system and added to your PATH.
- Verify that the required R libraries (R6, tidyverse, readr, janitor, stringr) are installed in your R environment: install.packages(c("R6", "tidyverse", "readr", "janitor", "stringr"))
- File Paths
- R Script Path: **/Avatar/plots/backward(file).R **
- Brainwave CSV Directory: /Avatar/plots/plot_waves/brainwaves-csv/backward(backwardreadings)
Load and Execute an R Script:
- The Python script uses rpy2 to execute an R script located at /Avatar/plots/filename.R, which defines the BrainWaveAnalysis class.
Access R6 Class from R:
- After executing the R script, the BrainWaveAnalysis class becomes accessible in Python through robjects.globalenv.
Initialize the Class:
- A new instance of the BrainWaveAnalysis class is created, with the directory containing brainwave CSV files (/Avatar/plots/plot_waves/brainwaves-csv/readingfolder) passed as an argument.
Call a Method:
- The script invokes the plot_data() method on the BrainWaveAnalysis object. This generates a visualization of the brainwave signals.
Handle Errors:
- The code uses try-except blocks to catch and report issues that might arise when:
- Executing the R script.
- Accessing the class.
- Initializing the class.
- Calling its methods.
Please find the Demo