#[cfg(feature = "scirs2")]
use scirs2_core::ndarray::Array2;
#[cfg(feature = "scirs2")]
use crate::core::error::Result;
#[cfg(feature = "scirs2")]
use crate::dataframe::DataFrame;
#[cfg(feature = "scirs2")]
use crate::scirs2_integration::conversion::{array2_to_dataframe, dataframe_to_array2};
#[cfg(feature = "scirs2")]
use crate::scirs2_integration::linalg::{LstsqDataFrameResult, QrResult, SciRS2LinAlg};
#[cfg(feature = "scirs2")]
use crate::scirs2_integration::stats::{PcaResult, SciRS2Stats};
pub trait SciRS2Ext {
#[cfg(feature = "scirs2")]
fn to_ndarray(&self, columns: &[&str]) -> Result<Array2<f64>>;
#[cfg(feature = "scirs2")]
fn from_ndarray(arr: &Array2<f64>, columns: Vec<String>) -> Result<DataFrame>
where
Self: Sized;
#[cfg(feature = "scirs2")]
fn scirs2_describe(&self) -> Result<DataFrame>;
#[cfg(feature = "scirs2")]
fn scirs2_corr(&self) -> Result<DataFrame>;
#[cfg(feature = "scirs2")]
fn scirs2_pca(&self, n_components: usize) -> Result<PcaResult>;
#[cfg(feature = "scirs2")]
fn scirs2_spearman_corr(&self) -> Result<DataFrame>;
#[cfg(feature = "scirs2")]
fn scirs2_cov(&self) -> Result<DataFrame>;
#[cfg(feature = "scirs2")]
fn scirs2_qr(&self) -> Result<QrResult>;
#[cfg(feature = "scirs2")]
fn scirs2_lstsq(&self, b: &DataFrame) -> Result<LstsqDataFrameResult>;
#[cfg(feature = "scirs2")]
fn scirs2_matrix_rank(&self) -> Result<usize>;
#[cfg(feature = "scirs2")]
fn scirs2_condition_number(&self) -> Result<f64>;
}
#[cfg(feature = "scirs2")]
impl SciRS2Ext for DataFrame {
fn to_ndarray(&self, columns: &[&str]) -> Result<Array2<f64>> {
dataframe_to_array2(self, columns)
}
fn from_ndarray(arr: &Array2<f64>, columns: Vec<String>) -> Result<DataFrame> {
array2_to_dataframe(arr, columns)
}
fn scirs2_describe(&self) -> Result<DataFrame> {
let all_cols = self.column_names();
let numeric_cols: Vec<&str> = all_cols
.iter()
.filter(|col_name| self.get_column_numeric_values(col_name).is_ok())
.map(|s| s.as_str())
.collect();
if numeric_cols.is_empty() {
return Err(crate::core::error::Error::EmptyData(
"No numeric columns found for describe".to_string(),
));
}
SciRS2Stats::describe(self, &numeric_cols)
}
fn scirs2_corr(&self) -> Result<DataFrame> {
let all_cols = self.column_names();
let numeric_cols: Vec<&str> = all_cols
.iter()
.filter(|col_name| self.get_column_numeric_values(col_name).is_ok())
.map(|s| s.as_str())
.collect();
if numeric_cols.len() < 2 {
return Err(crate::core::error::Error::InvalidInput(
"Correlation matrix requires at least 2 numeric columns".to_string(),
));
}
SciRS2Stats::correlation_matrix(self, &numeric_cols)
}
fn scirs2_pca(&self, n_components: usize) -> Result<PcaResult> {
let all_cols = self.column_names();
let numeric_cols: Vec<&str> = all_cols
.iter()
.filter(|col_name| self.get_column_numeric_values(col_name).is_ok())
.map(|s| s.as_str())
.collect();
if numeric_cols.is_empty() {
return Err(crate::core::error::Error::EmptyData(
"No numeric columns found for PCA".to_string(),
));
}
SciRS2Stats::pca(self, &numeric_cols, n_components)
}
fn scirs2_spearman_corr(&self) -> Result<DataFrame> {
let all_cols = self.column_names();
let numeric_cols: Vec<&str> = all_cols
.iter()
.filter(|col_name| self.get_column_numeric_values(col_name).is_ok())
.map(|s| s.as_str())
.collect();
if numeric_cols.len() < 2 {
return Err(crate::core::error::Error::InvalidInput(
"Spearman correlation matrix requires at least 2 numeric columns".to_string(),
));
}
SciRS2Stats::spearman_correlation_matrix(self, &numeric_cols)
}
fn scirs2_cov(&self) -> Result<DataFrame> {
let all_cols = self.column_names();
let numeric_cols: Vec<&str> = all_cols
.iter()
.filter(|col_name| self.get_column_numeric_values(col_name).is_ok())
.map(|s| s.as_str())
.collect();
if numeric_cols.len() < 2 {
return Err(crate::core::error::Error::InvalidInput(
"Covariance matrix requires at least 2 numeric columns".to_string(),
));
}
SciRS2Stats::covariance_matrix(self, &numeric_cols)
}
fn scirs2_qr(&self) -> Result<QrResult> {
SciRS2LinAlg::qr(self)
}
fn scirs2_lstsq(&self, b: &DataFrame) -> Result<LstsqDataFrameResult> {
SciRS2LinAlg::lstsq(self, b)
}
fn scirs2_matrix_rank(&self) -> Result<usize> {
SciRS2LinAlg::matrix_rank(self)
}
fn scirs2_condition_number(&self) -> Result<f64> {
SciRS2LinAlg::condition_number(self)
}
}