80 double stat_variance {};
81 double stat_std_dev {};
89 std::array<int, 6> level_counts {};
101 uint32_t window_size {};
102 uint32_t hop_size {};
128template <ComputeData InputType = std::vector<Kakshya::DataVariant>, ComputeData OutputType = Eigen::VectorXd>
141 : m_window_size(window_size)
142 , m_hop_size(hop_size)
144 validate_window_parameters();
154 auto result = this->analyze_data(data);
155 return safe_any_cast_or_throw<StatisticalAnalysis>(result);
164 return safe_any_cast_or_throw<StatisticalAnalysis>(this->get_current_analysis());
173 return AnalysisType::STATISTICAL;
182 return Utils::get_enum_names_lowercase<StatisticalMethod>();
190 template <
typename T>
193 return get_methods_for_type_impl(std::type_index(
typeid(T)));
201 template <
typename T>
204 return !get_methods_for_type<T>().empty();
214 this->set_parameter(
"method", method_to_string(method));
223 m_method = string_to_method(method_name);
224 this->set_parameter(
"method", method_name);
242 m_window_size = size;
243 validate_window_parameters();
253 validate_window_parameters();
274 m_classification_enabled = enabled;
290 if (std::abs(value) > m_outlier_threshold)
291 return StatisticalLevel::OUTLIER;
292 if (value <= m_extreme_low_threshold)
293 return StatisticalLevel::EXTREME_LOW;
294 if (value <= m_low_threshold)
295 return StatisticalLevel::LOW;
296 if (value <= m_high_threshold)
297 return StatisticalLevel::NORMAL;
298 if (value <= m_extreme_high_threshold)
299 return StatisticalLevel::HIGH;
300 return StatisticalLevel::EXTREME_HIGH;
310 return Utils::enum_to_lowercase_string(method);
320 if (str ==
"default")
321 return StatisticalMethod::MEAN;
322 return Utils::string_to_enum_or_throw_case_insensitive<StatisticalMethod>(str,
"StatisticalMethod");
332 return Utils::enum_to_lowercase_string(level);
342 return "StatisticalAnalyzer";
352 if (input.
data.empty()) {
353 throw std::runtime_error(
"Input is empty");
356 auto [data_span, structure_info] = OperationHelper::extract_structured_double(
359 std::vector<std::span<const double>> channel_spans;
360 for (
const auto& span : data_span)
361 channel_spans.emplace_back(span.data(), span.size());
363 std::vector<std::vector<double>> stat_values;
364 stat_values.reserve(channel_spans.size());
365 for (
const auto& ch_span : channel_spans) {
366 stat_values.push_back(compute_statistical_values(ch_span, m_method));
370 stat_values, channel_spans, structure_info);
372 this->store_current_analysis(analysis_result);
373 return create_pipeline_output(input, analysis_result, structure_info);
374 }
catch (
const std::exception& e) {
375 std::cerr <<
"Energy analysis failed: " << e.what() <<
'\n';
378 error_result.
metadata[
"error"] = std::string(
"Analysis failed: ") + e.what();
389 if (name ==
"method") {
391 auto method_str = safe_any_cast_or_throw<std::string>(value);
392 m_method = string_to_method(method_str);
393 }
catch (
const std::runtime_error&) {
394 auto method_enum = safe_any_cast_or_throw<StatisticalMethod>(value);
395 m_method = method_enum;
397 }
else if (name ==
"window_size") {
398 auto size = safe_any_cast_or_throw<uint32_t>(value);
399 m_window_size = size;
400 validate_window_parameters();
401 }
else if (name ==
"hop_size") {
402 auto size = safe_any_cast_or_throw<uint32_t>(value);
404 validate_window_parameters();
405 }
else if (name ==
"classification_enabled") {
406 auto enabled = safe_any_cast_or_throw<bool>(value);
407 m_classification_enabled = enabled;
408 }
else if (name ==
"percentile") {
409 auto percentile = safe_any_cast_or_throw<double>(value);
410 if (percentile < 0.0 || percentile > 100.0) {
411 throw std::invalid_argument(
"Percentile must be between 0.0 and 100.0, got: " + std::to_string(percentile));
413 m_percentile_value = percentile;
414 }
else if (name ==
"sample_variance") {
415 auto sample = safe_any_cast_or_throw<bool>(value);
416 m_sample_variance = sample;
418 base_type::set_analysis_parameter(name, std::move(value));
420 }
catch (
const std::runtime_error& e) {
421 throw std::invalid_argument(
"Failed to set parameter '" + name +
"': " + e.what());
430 if (name ==
"method")
431 return std::any(method_to_string(m_method));
432 if (name ==
"window_size")
433 return std::any(m_window_size);
434 if (name ==
"hop_size")
435 return std::any(m_hop_size);
436 if (name ==
"classification_enabled")
437 return std::any(m_classification_enabled);
438 if (name ==
"percentile")
439 return std::any(m_percentile_value);
440 if (name ==
"sample_variance")
441 return std::any(m_sample_variance);
443 return base_type::get_analysis_parameter(name);
453 return get_available_methods();
460 bool m_classification_enabled {
true };
461 double m_percentile_value { 50.0 };
462 bool m_sample_variance {
true };
464 double m_outlier_threshold { 3.0 };
465 double m_extreme_low_threshold { -2.0 };
466 double m_low_threshold { -1.0 };
467 double m_high_threshold { 1.0 };
468 double m_extreme_high_threshold { 2.0 };
475 if (m_window_size == 0 || m_hop_size == 0) {
476 throw std::invalid_argument(
"Window size and hop size must be greater than 0");
478 if (m_hop_size > m_window_size) {
479 throw std::invalid_argument(
"Hop size should not exceed window size");
488 const size_t num_windows = calculate_num_windows(data.size());
491 case StatisticalMethod::MEAN:
493 case StatisticalMethod::VARIANCE:
495 case StatisticalMethod::STD_DEV:
497 case StatisticalMethod::SKEWNESS:
499 case StatisticalMethod::KURTOSIS:
501 case StatisticalMethod::MEDIAN:
503 case StatisticalMethod::PERCENTILE:
505 case StatisticalMethod::ENTROPY:
507 case StatisticalMethod::MIN:
509 case StatisticalMethod::MAX:
511 case StatisticalMethod::RANGE:
513 case StatisticalMethod::SUM:
515 case StatisticalMethod::COUNT:
517 case StatisticalMethod::RMS:
519 case StatisticalMethod::MAD:
521 case StatisticalMethod::CV:
523 case StatisticalMethod::MODE:
525 case StatisticalMethod::ZSCORE:
537 if (data_size < m_window_size)
539 return (data_size - m_window_size) / m_hop_size + 1;
546 std::vector<std::span<const double>> original_data,
const auto& )
const
553 if (stat_values.empty()) {
559 for (
size_t ch = 0; ch < stat_values.size(); ++ch) {
561 const auto& ch_stats = stat_values[ch];
563 channel_result.statistical_values = ch_stats;
565 if (ch_stats.empty())
568 const auto [min_it, max_it] = std::ranges::minmax_element(ch_stats);
569 channel_result.min_stat = *min_it;
570 channel_result.max_stat = *max_it;
573 channel_result.mean_stat = mean_result.empty() ? 0.0 : mean_result[0];
575 auto variance_result =
compute_variance_statistic(std::span<const double>(ch_stats), 1, 0, ch_stats.size(), m_sample_variance);
576 channel_result.stat_variance = variance_result.empty() ? 0.0 : variance_result[0];
577 channel_result.stat_std_dev = std::sqrt(channel_result.stat_variance);
580 channel_result.skewness = skew_result.empty() ? 0.0 : skew_result[0];
583 channel_result.kurtosis = kurt_result.empty() ? 0.0 : kurt_result[0];
586 channel_result.median = median_result.empty() ? 0.0 : median_result[0];
590 channel_result.percentiles = {
591 q25_result.empty() ? 0.0 : q25_result[0],
592 channel_result.median,
593 q75_result.empty() ? 0.0 : q75_result[0]
596 const size_t data_size = (ch < original_data.size()) ? original_data[ch].size() : 0;
597 channel_result.window_positions.reserve(ch_stats.size());
598 for (
size_t i = 0; i < ch_stats.size(); ++i) {
599 const size_t start = i * m_hop_size;
600 const size_t end = std::min(start + m_window_size, data_size);
601 channel_result.window_positions.emplace_back(start, end);
604 if (m_classification_enabled) {
605 channel_result.stat_classifications.reserve(ch_stats.size());
606 channel_result.level_counts.fill(0);
608 for (
double value : ch_stats) {
610 channel_result.stat_classifications.push_back(level);
611 channel_result.level_counts[
static_cast<size_t>(level)]++;
624 std::vector<std::vector<double>> channel_stats;
627 channel_stats.push_back(ch.statistical_values);
630 output_type output = this->convert_result(channel_stats, info);
634 output.
metadata[
"source_analyzer"] =
"StatisticalAnalyzer";
641 std::vector<double> channel_means, channel_maxs, channel_mins, channel_variances, channel_stddevs, channel_skewness, channel_kurtosis, channel_medians;
642 std::vector<size_t> channel_window_counts;
645 channel_means.push_back(ch.mean_stat);
646 channel_maxs.push_back(ch.max_stat);
647 channel_mins.push_back(ch.min_stat);
648 channel_variances.push_back(ch.stat_variance);
649 channel_stddevs.push_back(ch.stat_std_dev);
650 channel_skewness.push_back(ch.skewness);
651 channel_kurtosis.push_back(ch.kurtosis);
652 channel_medians.push_back(ch.median);
653 channel_window_counts.push_back(ch.statistical_values.size());
656 output.
metadata[
"mean_per_channel"] = channel_means;
657 output.
metadata[
"max_per_channel"] = channel_maxs;
658 output.
metadata[
"min_per_channel"] = channel_mins;
659 output.
metadata[
"variance_per_channel"] = channel_variances;
660 output.
metadata[
"stddev_per_channel"] = channel_stddevs;
661 output.
metadata[
"skewness_per_channel"] = channel_skewness;
662 output.
metadata[
"kurtosis_per_channel"] = channel_kurtosis;
663 output.
metadata[
"median_per_channel"] = channel_medians;
664 output.
metadata[
"window_count_per_channel"] = channel_window_counts;
681template <ComputeData InputType = std::vector<Kakshya::DataVariant>>
685template <ComputeData InputType = std::vector<Kakshya::DataVariant>>
Modern, digital-first universal analyzer framework for Maya Flux.
std::any get_analysis_parameter(const std::string &name) const override
Get analysis-specific parameter.
void set_classification_enabled(bool enabled)
Enable/disable outlier classification.
bool is_classification_enabled() const
Check if classification is enabled.
StatisticalMethod get_method() const
Get current statistical method.
StatisticalAnalysis get_statistical_analysis() const
Get last statistical analysis result (type-safe)
void validate_window_parameters() const
Validate window parameters.
std::vector< std::string > get_methods_for_type_impl(std::type_index) const
Get supported methods for specific type index.
std::string get_analyzer_name() const override
Get analyzer name.
static StatisticalMethod string_to_method(const std::string &str)
Convert string to statistical method enum.
bool supports_input_type() const
Check if analyzer supports given input type.
std::vector< std::string > get_methods_for_type() const
Get supported methods for specific input type.
StatisticalAnalysis create_analysis_result(const std::vector< std::vector< double > > &stat_values, std::vector< std::span< const double > > original_data, const auto &) const
Create comprehensive analysis result.
std::vector< std::string > get_available_methods() const override
Get available analysis methods.
StatisticalAnalyzer(uint32_t window_size=512, uint32_t hop_size=256)
Construct StatisticalAnalyzer with configurable window parameters.
StatisticalLevel classify_statistical_level(double value) const
Classify statistical value qualitatively.
static std::string method_to_string(StatisticalMethod method)
Convert statistical method enum to string.
std::vector< double > compute_statistical_values(std::span< const double > data, StatisticalMethod method) const
Compute statistical values using span (zero-copy processing)
static std::string statistical_level_to_string(StatisticalLevel level)
Convert statistical level enum to string.
AnalysisType get_analysis_type() const override
Get analysis type category.
uint32_t get_window_size() const
Get window size.
void set_window_size(uint32_t size)
Set window size for windowed analysis.
void set_method(StatisticalMethod method)
Set statistical analysis method.
output_type create_pipeline_output(const input_type &input, const StatisticalAnalysis &analysis_result, DataStructureInfo &info)
Create pipeline output for operation chaining.
uint32_t get_hop_size() const
Get hop size.
output_type analyze_implementation(const input_type &input) override
Core analysis implementation - creates analysis result AND pipeline output.
size_t calculate_num_windows(size_t data_size) const
Calculate number of windows for given data size.
void set_method(const std::string &method_name)
Set method by string name.
void set_analysis_parameter(const std::string &name, std::any value) override
Handle analysis-specific parameters.
StatisticalAnalysis analyze_statistics(const InputType &data)
Type-safe statistical analysis method.
void set_hop_size(uint32_t size)
Set hop size for windowed analysis.
High-performance statistical analyzer with zero-copy processing.
Template-flexible analyzer base with instance-defined I/O types.
std::vector< double > compute_zscore_statistic(std::span< const double > data, const size_t num_windows, const uint32_t hop_size, const uint32_t window_size, bool sample_variance)
Compute z-score statistic using zero-copy processing.
std::vector< double > compute_entropy_statistic(std::span< const double > data, const size_t num_windows, const uint32_t hop_size, const uint32_t window_size, size_t num_bins)
Compute entropy statistic using zero-copy processing.
AnalysisType
Categories of analysis operations for discovery and organization.
@ RMS
Root Mean Square energy.
std::vector< double > compute_skewness_statistic(std::span< const double > data, const size_t num_windows, const uint32_t hop_size, const uint32_t window_size)
Compute skewness statistic using zero-copy processing.
std::vector< double > compute_variance_statistic(std::span< const double > data, const size_t num_windows, const uint32_t hop_size, const uint32_t window_size, bool sample_variance)
Compute variance statistic using zero-copy processing.
std::vector< double > compute_mad_statistic(std::span< const double > data, const size_t num_windows, const uint32_t hop_size, const uint32_t window_size)
Compute MAD (Median Absolute Deviation) statistic using zero-copy processing.
std::vector< double > compute_std_dev_statistic(std::span< const double > data, const size_t num_windows, const uint32_t hop_size, const uint32_t window_size, bool sample_variance)
Compute standard deviation statistic using zero-copy processing.
std::vector< double > compute_sum_statistic(std::span< const double > data, const size_t num_windows, const uint32_t hop_size, const uint32_t window_size)
Compute sum statistic using zero-copy processing.
std::vector< double > compute_percentile_statistic(std::span< const double > data, const size_t num_windows, const uint32_t hop_size, const uint32_t window_size, double percentile)
Compute percentile statistic using zero-copy processing.
std::vector< double > compute_mode_statistic(std::span< const double > data, const size_t num_windows, const uint32_t hop_size, const uint32_t window_size)
Compute mode statistic using zero-copy processing.
std::vector< double > compute_rms_energy(std::span< const double > data, const uint32_t num_windows, const uint32_t hop_size, const uint32_t window_size)
Compute RMS energy using zero-copy processing.
std::vector< double > compute_min_statistic(std::span< const double > data, const size_t num_windows, const uint32_t hop_size, const uint32_t window_size)
Compute min statistic using zero-copy processing.
std::vector< double > compute_mean_statistic(std::span< const double > data, const size_t num_windows, const uint32_t hop_size, const uint32_t window_size)
Compute mean statistic using zero-copy processing.
StatisticalLevel
Qualitative classification of statistical values.
std::vector< double > compute_range_statistic(std::span< const double > data, const size_t num_windows, const uint32_t hop_size, const uint32_t window_size)
Compute range statistic using zero-copy processing.
std::vector< double > compute_cv_statistic(std::span< const double > data, const size_t num_windows, const uint32_t hop_size, const uint32_t window_size, bool sample_variance)
Compute CV (Coefficient of Variation) statistic using zero-copy processing.
StatisticalMethod
Supported statistical computation methods.
@ PERCENTILE
Arbitrary percentile (requires parameter)
@ KURTOSIS
Fourth moment - tail heaviness.
@ ZSCORE
Z-score normalization.
@ ENTROPY
Shannon entropy for discrete data.
@ MAD
Median Absolute Deviation.
@ CV
Coefficient of Variation (std_dev/mean)
@ STD_DEV
Standard deviation.
@ MODE
Most frequent value.
@ VARIANCE
Population or sample variance.
@ SKEWNESS
Third moment - asymmetry measure.
std::vector< double > compute_median_statistic(std::span< const double > data, const size_t num_windows, const uint32_t hop_size, const uint32_t window_size)
Compute median statistic using zero-copy processing.
std::vector< double > compute_kurtosis_statistic(std::span< const double > data, const size_t num_windows, const uint32_t hop_size, const uint32_t window_size)
Compute kurtosis statistic using zero-copy processing.
std::vector< double > compute_max_statistic(std::span< const double > data, const size_t num_windows, const uint32_t hop_size, const uint32_t window_size)
Compute max statistic using zero-copy processing.
std::vector< double > compute_count_statistic(std::span< const double > data, const size_t num_windows, const uint32_t hop_size, const uint32_t window_size)
Compute count statistic using zero-copy processing.
std::map< std::string, std::any > method_specific_data
std::vector< std::pair< size_t, size_t > > window_positions
std::vector< StatisticalLevel > stat_classifications
std::vector< double > percentiles
std::vector< double > statistical_values
Statistical results for a single data channel.
Metadata about data structure for reconstruction.
T data
The actual computation data.
std::unordered_map< std::string, std::any > metadata
Associated metadata.
Input/Output container for computation pipeline data flow with structure preservation.
StatisticalMethod method_used
std::vector< ChannelStatistics > channel_statistics
Analysis result structure for statistical analysis.