MayaFlux 0.1.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
UniversalAnalyzer.hpp
Go to the documentation of this file.
1#pragma once
2
4
5/**
6 * @file UniversalAnalyzer.hpp
7 * @brief Modern, digital-first universal analyzer framework for Maya Flux
8 *
9 * This header defines the core analyzer abstractions for the Maya Flux ecosystem,
10 * enabling robust, type-safe, and extensible analysis pipelines for multi-dimensional
11 * data. The design is inspired by digital-first, data-driven conventions, avoiding
12 * analog metaphors and focusing on composability, introspection, and future-proof
13 * extensibility.
14 *
15 * Key Features:
16 * - **Unified input/output variants:** Supports raw data, containers, regions, and segments.
17 * - **Type-safe, concept-based analyzers:** Uses C++20 concepts and std::variant for strict type safety.
18 * - **Granularity control:** Flexible output granularity for raw values, attributed segments, or organized groups.
19 * - **Composable operations:** Integrates with ComputeMatrix and processing chains for scalable workflows.
20 * - **Parameterization and introspection:** Dynamic configuration and runtime method discovery.
21 *
22 * This abstraction is foundational for building advanced, maintainable, and scalable
23 * analysis architectures in digital-first, data-centric applications.
24 */
25
26namespace MayaFlux::Yantra {
27
28/**
29 * @enum AnalysisType
30 * @brief Categories of analysis operations for discovery and organization
31 */
32enum class AnalysisType : uint8_t {
33 STATISTICAL, ///< Mean, variance, distribution analysis
34 SPECTRAL, ///< FFT, frequency domain analysis
35 TEMPORAL, ///< Time-based patterns, onset detection
36 SPATIAL, ///< Multi-dimensional geometric analysis
37 FEATURE, ///< Feature extraction and characterization
38 PATTERN, ///< Pattern recognition and matching
39 TRANSFORM, ///< Mathematical transformations
40 CUSTOM ///< User-defined analysis types
41};
42
43/**
44 * @enum AnalysisGranularity
45 * @brief Output granularity control for analysis results
46 */
47enum class AnalysisGranularity : uint8_t {
48 RAW_VALUES, ///< Direct analysis results
49 ATTRIBUTED_SEGMENTS, ///< Results with metadata/attribution
50 ORGANIZED_GROUPS, ///< Hierarchically organized results
51 SUMMARY_STATISTICS ///< Condensed statistical summaries
52};
53
54/**
55 * @class UniversalAnalyzer
56 * @brief Template-flexible analyzer base with instance-defined I/O types
57 *
58 * The UniversalAnalyzer provides a clean, concept-based foundation for all analysis
59 * operations. Unlike the old approach, the I/O types are defined at instantiation
60 * time rather than at the class definition level, providing maximum flexibility.
61 *
62 * Key Features:
63 * - Instance-defined I/O types via template parameters
64 * - Concept-constrained data types for compile-time safety
65 * - Analysis type categorization for discovery
66 * - Granularity control for output formatting
67 * - Parameter management with type safety
68 * - Integration with ComputeMatrix execution modes
69 *
70 * Usage:
71 * ```cpp
72 * // Create analyzer for DataVariant -> Eigen::VectorXd
73 * auto analyzer = std::make_shared<MyAnalyzer<std::vector<Kakshya::DataVariant>, Eigen::VectorXd>>();
74 *
75 * // Or for Region -> RegionGroup
76 * auto region_analyzer = std::make_shared<MyAnalyzer<Kakshya::Region, Kakshya::RegionGroup>>();
77 * ```
78 */
79template <ComputeData InputType = std::vector<Kakshya::DataVariant>, ComputeData OutputType = InputType>
80class MAYAFLUX_API UniversalAnalyzer : public ComputeOperation<InputType, OutputType> {
81public:
85
86 virtual ~UniversalAnalyzer() = default;
87
88 /**
89 * @brief Gets the analysis type category for this analyzer
90 * @return AnalysisType enum value
91 */
92 [[nodiscard]] virtual AnalysisType get_analysis_type() const = 0;
93
94 /**
95 * @brief Gets human-readable name for this analyzer
96 * @return String identifier for the analyzer
97 */
98 [[nodiscard]] std::string get_name() const override
99 {
100 return get_analyzer_name();
101 }
102
103 /**
104 * @brief Type-safe parameter management with analysis-specific defaults
105 */
106 void set_parameter(const std::string& name, std::any value) override
107 {
108 if (name == "granularity") {
109 if (auto* gran = std::any_cast<AnalysisGranularity>(&value)) {
110 m_granularity = *gran;
111 return;
112 }
113 }
114 set_analysis_parameter(name, std::move(value));
115 }
116
117 [[nodiscard]] std::any get_parameter(const std::string& name) const override
118 {
119 if (name == "granularity") {
120 return std::any(m_granularity);
121 }
122
123 return get_analysis_parameter(name);
124 }
125
126 [[nodiscard]] std::map<std::string, std::any> get_all_parameters() const override
127 {
128 auto params = get_all_analysis_parameters();
129 params["granularity"] = std::any(m_granularity);
130 return params;
131 }
132
134 {
135 m_granularity = granularity;
136 }
137
139 {
140 return m_granularity;
141 }
142
143 /**
144 * @brief Validates input data meets analyzer requirements
145 */
146 bool validate_input(const input_type& input) const override
147 {
148 return validate_analysis_input(input);
149 }
150
151 /**
152 * @brief Get available analysis methods for this analyzer
153 * @return Vector of method names supported by this analyzer
154 */
155 [[nodiscard]] virtual std::vector<std::string> get_available_methods() const
156 {
157 return { "default" };
158 }
159
160 /**
161 * @brief Check if a specific analysis method is supported
162 * @param method Method name to check
163 * @return True if method is supported
164 */
165 [[nodiscard]] virtual bool supports_method(const std::string& method) const
166 {
167 auto methods = get_available_methods();
168 return std::find(methods.begin(), methods.end(), method) != methods.end();
169 }
170
171 /**
172 * @brief User-facing analysis method - returns analysis results directly
173 * @param data Raw input data
174 * @return Analysis results as std::any
175 */
176 std::any analyze_data(const InputType& data)
177 {
178 input_type input_io(data);
179 m_current_output = this->apply_operation(input_io);
180 return m_current_analysis;
181 }
182
183 /**
184 * @brief Batch analysis for multiple inputs
185 * @param inputs Vector of input data
186 * @return Vector of analysis results as std::any
187 */
188 std::vector<std::any> analyze_batch(const std::vector<InputType>& inputs)
189 {
190 std::vector<std::any> analyses;
191 analyses.reserve(inputs.size());
192
193 for (const auto& input : inputs) {
194 analyses.push_back(analyze_data(input));
195 }
196
197 return analyses;
198 }
199
200 /**
201 * @brief Access cached analysis from last operation
202 * @return Current analysis result as std::any
203 */
204 std::any get_current_analysis() const
205 {
206 return m_current_analysis;
207 }
208
209 /**
210 * @brief Check if analysis has been performed
211 * @return True if analysis result is available
212 */
214 {
215 return m_current_analysis.has_value();
216 }
217
218 /**
219 * @brief Helper to get typed parameter with default value
220 * @tparam T Parameter type
221 * @param name Parameter name
222 * @param default_value Default value if parameter not found
223 * @return Parameter value or default
224 */
225 template <typename T>
226 T get_parameter_or_default(const std::string& name, const T& default_value) const
227 {
228 auto param = get_analysis_parameter(name);
229 if (param.has_value()) {
230 try {
231 return std::any_cast<T>(param);
232 } catch (const std::bad_any_cast&) {
233 return default_value;
234 }
235 }
236 return default_value;
237 }
238
239protected:
240 /**
241 * @brief Core analysis implementation - must be overridden by derived classes
242 * @param input Input data wrapped in IO container
243 * @return Analysis results wrapped in IO container
244 *
245 * This is where the actual analysis logic goes. The method receives data
246 * in an IO wrapper which may contain metadata, and should return results
247 * in the same wrapper format.
248 */
250 {
251 auto raw_result = analyze_implementation(input);
252 return apply_granularity_formatting(raw_result);
253 }
254
255 /**
256 * @brief Pure virtual analysis implementation - derived classes implement this
257 * @param input Input data with metadata
258 * @return Raw analysis output before granularity formatting
259 */
261
262 /**
263 * @brief Get analyzer-specific name (derived classes override this)
264 * @return Analyzer name string
265 */
266 [[nodiscard]] virtual std::string get_analyzer_name() const { return "UniversalAnalyzer"; }
267
268 /**
269 * @brief Analysis-specific parameter handling (override for custom parameters)
270 */
271 virtual void set_analysis_parameter(const std::string& name, std::any value)
272 {
273 m_parameters[name] = std::move(value);
274 }
275
276 [[nodiscard]] virtual std::any get_analysis_parameter(const std::string& name) const
277 {
278 auto it = m_parameters.find(name);
279 return (it != m_parameters.end()) ? it->second : std::any {};
280 }
281
282 [[nodiscard]] virtual std::map<std::string, std::any> get_all_analysis_parameters() const
283 {
284 return m_parameters;
285 }
286
287 /**
288 * @brief Input validation (override for custom validation logic)
289 */
290 virtual bool validate_analysis_input(const input_type& /*input*/) const
291 {
292 // Default: accept any input that satisfies ComputeData concept
293 return true;
294 }
295
296 /**
297 * @brief Apply granularity-based output formatting
298 * @param raw_output Raw analysis results
299 * @return Formatted output based on granularity setting
300 */
302 {
303 switch (m_granularity) {
304 case AnalysisGranularity::RAW_VALUES:
305 return raw_output;
306
307 case AnalysisGranularity::ATTRIBUTED_SEGMENTS:
308 return add_attribution_metadata(raw_output);
309
310 case AnalysisGranularity::ORGANIZED_GROUPS:
311 return organize_into_groups(raw_output);
312
313 case AnalysisGranularity::SUMMARY_STATISTICS:
314 return create_summary_statistics(raw_output);
315
316 default:
317 return raw_output;
318 }
319 }
320
321 /**
322 * @brief Add attribution metadata to results (override for custom attribution)
323 */
325 {
326 output_type attributed = raw_output;
327 attributed.metadata["analysis_type"] = static_cast<int>(get_analysis_type());
328 attributed.metadata["analyzer_name"] = get_analyzer_name();
329 attributed.metadata["granularity"] = static_cast<int>(m_granularity);
330 return attributed;
331 }
332
333 /**
334 * @brief Organize results into hierarchical groups (override for custom grouping)
335 */
337 {
338 // Default implementation: just add grouping metadata
339 return add_attribution_metadata(raw_output);
340 }
341
342 /**
343 * @brief Create summary statistics from results (override for custom summaries)
344 */
346 {
347 // Default implementation: add summary metadata
348 auto summary = add_attribution_metadata(raw_output);
349 summary.metadata["is_summary"] = true;
350 return summary;
351 }
352
353 mutable std::any m_current_analysis;
354
355 template <typename AnalysisResultType>
356 void store_current_analysis(AnalysisResultType&& result) const
357 {
358 m_current_analysis = std::forward<AnalysisResultType>(result);
359 }
360
362
363private:
364 AnalysisGranularity m_granularity = AnalysisGranularity::RAW_VALUES;
365 std::map<std::string, std::any> m_parameters;
366};
367
368/// Analyzer that takes DataVariant and produces DataVariant
369template <ComputeData OutputType = std::vector<Kakshya::DataVariant>>
371
372/// Analyzer for signal container processing
373template <ComputeData OutputType = std::shared_ptr<Kakshya::SignalSourceContainer>>
375
376/// Analyzer for region-based analysis
377template <ComputeData OutputType = Kakshya::Region>
379
380/// Analyzer for region group processing
381template <ComputeData OutputType = Kakshya::RegionGroup>
383
384/// Analyzer for segment processing
385template <ComputeData OutputType = std::vector<Kakshya::RegionSegment>>
387
388/// Analyzer that produces Eigen matrices
389template <ComputeData InputType = std::vector<Kakshya::DataVariant>>
391
392/// Analyzer that produces Eigen vectors
393template <ComputeData InputType = std::vector<Kakshya::DataVariant>>
395
396} // namespace MayaFlux::Yantra
Base interface for all computational operations in the processing pipeline.
virtual void set_analysis_parameter(const std::string &name, std::any value)
Analysis-specific parameter handling (override for custom parameters)
T get_parameter_or_default(const std::string &name, const T &default_value) const
Helper to get typed parameter with default value.
std::any get_parameter(const std::string &name) const override
Retrieves a parameter's current value.
std::any get_current_analysis() const
Access cached analysis from last operation.
virtual output_type add_attribution_metadata(const output_type &raw_output)
Add attribution metadata to results (override for custom attribution)
virtual AnalysisType get_analysis_type() const =0
Gets the analysis type category for this analyzer.
std::map< std::string, std::any > m_parameters
std::string get_name() const override
Gets human-readable name for this analyzer.
void set_parameter(const std::string &name, std::any value) override
Type-safe parameter management with analysis-specific defaults.
AnalysisGranularity get_analysis_granularity() const
virtual std::vector< std::string > get_available_methods() const
Get available analysis methods for this analyzer.
virtual std::any get_analysis_parameter(const std::string &name) const
output_type operation_function(const input_type &input) override
Core analysis implementation - must be overridden by derived classes.
virtual output_type analyze_implementation(const input_type &input)=0
Pure virtual analysis implementation - derived classes implement this.
void store_current_analysis(AnalysisResultType &&result) const
void set_analysis_granularity(AnalysisGranularity granularity)
std::map< std::string, std::any > get_all_parameters() const override
Retrieves all parameters and their values.
virtual output_type apply_granularity_formatting(const output_type &raw_output)
Apply granularity-based output formatting.
std::vector< std::any > analyze_batch(const std::vector< InputType > &inputs)
Batch analysis for multiple inputs.
bool has_current_analysis() const
Check if analysis has been performed.
virtual output_type organize_into_groups(const output_type &raw_output)
Organize results into hierarchical groups (override for custom grouping)
virtual std::string get_analyzer_name() const
Get analyzer-specific name (derived classes override this)
virtual std::map< std::string, std::any > get_all_analysis_parameters() const
bool validate_input(const input_type &input) const override
Validates input data meets analyzer requirements.
virtual output_type create_summary_statistics(const output_type &raw_output)
Create summary statistics from results (override for custom summaries)
virtual bool validate_analysis_input(const input_type &) const
Input validation (override for custom validation logic)
virtual bool supports_method(const std::string &method) const
Check if a specific analysis method is supported.
std::any analyze_data(const InputType &data)
User-facing analysis method - returns analysis results directly.
Template-flexible analyzer base with instance-defined I/O types.
AnalysisType
Categories of analysis operations for discovery and organization.
@ PATTERN
Pattern recognition and matching.
@ TEMPORAL
Time-based patterns, onset detection.
@ CUSTOM
User-defined analysis types.
@ SPATIAL
Multi-dimensional geometric analysis.
@ FEATURE
Feature extraction and characterization.
@ TRANSFORM
Mathematical transformations.
@ STATISTICAL
Mean, variance, distribution analysis.
@ SPECTRAL
Spectral energy (FFT-based)
AnalysisGranularity
Output granularity control for analysis results.
@ SUMMARY_STATISTICS
Condensed statistical summaries.
@ ATTRIBUTED_SEGMENTS
Results with metadata/attribution.
@ ORGANIZED_GROUPS
Hierarchically organized results.
@ RAW_VALUES
Direct analysis results.
std::unordered_map< std::string, std::any > metadata
Associated metadata.
Definition DataIO.hpp:28
Input/Output container for computation pipeline data flow with structure preservation.
Definition DataIO.hpp:24