MayaFlux 0.4.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 [[nodiscard]] OperationType get_operation_type() const override
104 {
105 return OperationType::ANALYZER;
106 }
107
108 /**
109 * @brief Type-safe parameter management with analysis-specific defaults
110 */
111 void set_parameter(const std::string& name, std::any value) override
112 {
113 if (name == "granularity") {
114 if (auto result = safe_any_cast<AnalysisGranularity>(value)) {
115 m_granularity = *result.value;
116 return;
117 }
118 }
119 set_analysis_parameter(name, std::move(value));
120 }
121
122 [[nodiscard]] std::any get_parameter(const std::string& name) const override
123 {
124 if (name == "granularity") {
125 return std::any(m_granularity);
126 }
127
128 return get_analysis_parameter(name);
129 }
130
131 [[nodiscard]] std::map<std::string, std::any> get_all_parameters() const override
132 {
133 auto params = get_all_analysis_parameters();
134 params["granularity"] = std::any(m_granularity);
135 return params;
136 }
137
139 {
140 m_granularity = granularity;
141 }
142
144 {
145 return m_granularity;
146 }
147
148 /**
149 * @brief Validates input data meets analyzer requirements
150 */
151 bool validate_input(const input_type& input) const override
152 {
153 return validate_analysis_input(input);
154 }
155
156 /**
157 * @brief Get available analysis methods for this analyzer
158 * @return Vector of method names supported by this analyzer
159 */
160 [[nodiscard]] virtual std::vector<std::string> get_available_methods() const
161 {
162 return { "default" };
163 }
164
165 /**
166 * @brief Check if a specific analysis method is supported
167 * @param method Method name to check
168 * @return True if method is supported
169 */
170 [[nodiscard]] virtual bool supports_method(const std::string& method) const
171 {
172 auto methods = get_available_methods();
173 return std::find(methods.begin(), methods.end(), method) != methods.end();
174 }
175
176 /**
177 * @brief User-facing analysis method - returns analysis results directly
178 * @param data Raw input data
179 * @return Analysis results as std::any
180 */
181 std::any analyze_data(const input_type& data)
182 {
183 m_current_output = this->apply_operation(data);
184 return m_current_analysis;
185 }
186
187 std::any analyze_data(const InputType& data)
188 {
189 return this->analyze_data(input_type { data });
190 }
191
192 /**
193 * @brief Batch analysis for multiple inputs
194 * @param inputs Vector of input data
195 * @return Vector of analysis results as std::any
196 */
197 std::vector<std::any> analyze_batch(const std::vector<input_type>& inputs)
198 {
199 std::vector<std::any> analyses;
200 analyses.reserve(inputs.size());
201
202 for (const auto& input : inputs) {
203 analyses.push_back(analyze_data(input));
204 }
205
206 return analyses;
207 }
208
209 std::vector<std::any> analyze_batch(const std::vector<InputType>& inputs)
210 {
211 return this->analyze_batch(as_io_batch(inputs));
212 }
213
214 /**
215 * @brief Access cached analysis from last operation
216 * @return Current analysis result as std::any
217 */
218 std::any get_current_analysis() const
219 {
220 return m_current_analysis;
221 }
222
223 /**
224 * @brief Check if analysis has been performed
225 * @return True if analysis result is available
226 */
228 {
229 return m_current_analysis.has_value();
230 }
231
232 /**
233 * @brief Helper to get typed parameter with default value
234 * @tparam T Parameter type
235 * @param name Parameter name
236 * @param default_value Default value if parameter not found
237 * @return Parameter value or default
238 */
239 template <typename T>
240 T get_parameter_or_default(const std::string& name, const T& default_value) const
241 {
242 return safe_any_cast_or_default<T>(get_analysis_parameter(name), default_value);
243 }
244
245protected:
246 /**
247 * @brief Core analysis implementation - must be overridden by derived classes
248 * @param input Input data wrapped in Datum container
249 * @return Analysis results wrapped in Datum container
250 *
251 * This is where the actual analysis logic goes. The method receives data
252 * in an Datum wrapper which may contain metadata, and should return results
253 * in the same wrapper format.
254 */
256 {
257 auto raw_result = analyze_implementation(input);
258 return apply_granularity_formatting(raw_result);
259 }
260
261 /**
262 * @brief Pure virtual analysis implementation - derived classes implement this
263 * @param input Input data with metadata
264 * @return Raw analysis output before granularity formatting
265 */
267
268 /**
269 * @brief Get analyzer-specific name (derived classes override this)
270 * @return Analyzer name string
271 */
272 [[nodiscard]] virtual std::string get_analyzer_name() const { return "UniversalAnalyzer"; }
273
274 /**
275 * @brief Analysis-specific parameter handling (override for custom parameters)
276 */
277 virtual void set_analysis_parameter(const std::string& name, std::any value)
278 {
279 m_parameters[name] = std::move(value);
280 }
281
282 [[nodiscard]] virtual std::any get_analysis_parameter(const std::string& name) const
283 {
284 auto it = m_parameters.find(name);
285 return (it != m_parameters.end()) ? it->second : std::any {};
286 }
287
288 [[nodiscard]] virtual std::map<std::string, std::any> get_all_analysis_parameters() const
289 {
290 return m_parameters;
291 }
292
293 /**
294 * @brief Input validation (override for custom validation logic)
295 */
296 virtual bool validate_analysis_input(const input_type& /*input*/) const
297 {
298 // Default: accept any input that satisfies ComputeData concept
299 return true;
300 }
301
302 /**
303 * @brief Apply granularity-based output formatting
304 * @param raw_output Raw analysis results
305 * @return Formatted output based on granularity setting
306 */
308 {
309 switch (m_granularity) {
310 case AnalysisGranularity::RAW_VALUES:
311 return raw_output;
312
313 case AnalysisGranularity::ATTRIBUTED_SEGMENTS:
314 return add_attribution_metadata(raw_output);
315
316 case AnalysisGranularity::ORGANIZED_GROUPS:
317 return organize_into_groups(raw_output);
318
319 case AnalysisGranularity::SUMMARY_STATISTICS:
320 return create_summary_statistics(raw_output);
321
322 default:
323 return raw_output;
324 }
325 }
326
327 /**
328 * @brief Add attribution metadata to results (override for custom attribution)
329 */
331 {
332 output_type attributed = raw_output;
333 attributed.metadata["analysis_type"] = static_cast<int>(get_analysis_type());
334 attributed.metadata["analyzer_name"] = get_analyzer_name();
335 attributed.metadata["granularity"] = static_cast<int>(m_granularity);
336 return attributed;
337 }
338
339 /**
340 * @brief Organize results into hierarchical groups (override for custom grouping)
341 */
343 {
344 // Default implementation: just add grouping metadata
345 return add_attribution_metadata(raw_output);
346 }
347
348 /**
349 * @brief Create summary statistics from results (override for custom summaries)
350 */
352 {
353 // Default implementation: add summary metadata
354 auto summary = add_attribution_metadata(raw_output);
355 summary.metadata["is_summary"] = true;
356 return summary;
357 }
358
359 mutable std::any m_current_analysis;
360
361 template <typename AnalysisResultType>
362 void store_current_analysis(AnalysisResultType&& result) const
363 {
364 m_current_analysis = std::forward<AnalysisResultType>(result);
365 }
366
368
369private:
370 AnalysisGranularity m_granularity = AnalysisGranularity::RAW_VALUES;
371 std::map<std::string, std::any> m_parameters;
372};
373
374/// Analyzer that takes DataVariant and produces DataVariant
375template <ComputeData OutputType = std::vector<Kakshya::DataVariant>>
377
378/// Analyzer for signal container processing
379template <ComputeData OutputType = std::shared_ptr<Kakshya::SignalSourceContainer>>
381
382/// Analyzer for region-based analysis
383template <ComputeData OutputType = Kakshya::Region>
385
386/// Analyzer for region group processing
387template <ComputeData OutputType = Kakshya::RegionGroup>
389
390/// Analyzer for segment processing
391template <ComputeData OutputType = std::vector<Kakshya::RegionSegment>>
393
394/// Analyzer that produces Eigen matrices
395template <ComputeData InputType = std::vector<Kakshya::DataVariant>>
397
398/// Analyzer that produces Eigen vectors
399template <ComputeData InputType = std::vector<Kakshya::DataVariant>>
401
402} // 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
OperationType get_operation_type() const override
Returns the category of this operation for grammar and registry discovery.
std::string get_name() const override
Gets human-readable name for this analyzer.
std::vector< std::any > analyze_batch(const std::vector< input_type > &inputs)
Batch analysis for multiple inputs.
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.
std::any analyze_data(const input_type &data)
User-facing analysis method - returns analysis results directly.
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)
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)
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)
OperationType
Operation categories for organization and discovery.
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::vector< Datum< T > > as_io_batch(const std::vector< T > &inputs)
Convert a vector of raw values into a vector of Datum wrappers.
Definition DataIO.hpp:341
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