MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
FeatureExtractor.hpp
Go to the documentation of this file.
1#pragma once
2
7
9
10/**
11 * @file FeatureExtractor.hpp
12 * @brief Concrete feature extractor using analyzer-guided extraction
13 *
14 * FeatureExtractor demonstrates the modern extraction paradigm by using
15 * analyzers (EnergyAnalyzer, StatisticalAnalyzer) to identify regions of interest,
16 * then extracting the actual data from those regions. Uses enum-based configuration
17 * instead of string parsing for type safety and performance.
18 *
19 * Example Usage:
20 * ```cpp
21 * // Extract high-energy audio data
22 * auto extractor = std::make_shared<StandardFeatureExtractor>();
23 * extractor->set_extraction_method(ExtractionMethod::HIGH_ENERGY_DATA);
24 * extractor->set_parameter("energy_threshold", 0.2);
25 *
26 * auto high_energy_audio = extractor->extract_data(audio_data);
27 *
28 * // Extract data from statistical outlier regions
29 * extractor->set_extraction_method(ExtractionMethod::OUTLIER_DATA);
30 * extractor->set_parameter("std_dev_threshold", 2.5);
31 *
32 * auto outlier_audio = extractor->extract_data(audio_data);
33 * ```
34 */
35
36namespace MayaFlux::Yantra {
37
38/**
39 * @enum ExtractionMethod
40 * @brief Supported extraction methods for FeatureExtractor
41 */
42enum class ExtractionMethod : uint8_t {
43 HIGH_ENERGY_DATA, ///< Extract data from high-energy regions
44 PEAK_DATA, ///< Extract data around detected peaks
45 OUTLIER_DATA, ///< Extract data from statistical outlier regions
46 HIGH_SPECTRAL_DATA, ///< Extract data from high spectral energy regions
47 ABOVE_MEAN_DATA, ///< Extract data above statistical mean
48 OVERLAPPING_WINDOWS, ///< Extract overlapping windowed data
49 ZERO_CROSSING_DATA, ///< Extract actual data at zero crossing points
50 SILENCE_DATA, ///< Extract actual silent regions
51 ONSET_DATA ///< Extract actual onset/transient regions
52};
53
54/**
55 * @struct FeatureExtractorConfig
56 * @brief Initial settings for feature guided extraction.
57 *
58 * Pass this aggregate with designated initializers to the config constructor.
59 * Named parameters are applied after the typed settings.
60 */
62 /** Analysis window length in samples. */
63 uint32_t window_size { 512 };
64 /** Distance between successive windows in samples. */
65 uint32_t hop_size { 256 };
66 /** Feature selection method. */
68 /** Portion of the input to extract from. */
70 /** Additional named parameters applied last. */
71 std::map<std::string, std::any> parameters;
72};
73
74/**
75 * @class FeatureExtractor
76 * @brief Analyzer-guided feature extractor with enum-based configuration
77 *
78 * Uses analyzers to identify regions of interest based on energy, statistical properties,
79 * or spectral characteristics, then extracts the actual data from those regions.
80 * All extraction logic is delegated to ExtractionHelper functions.
81 */
82template <ComputeData InputType = std::vector<Kakshya::DataVariant>, ComputeData OutputType = std::vector<std::vector<double>>>
83class FeatureExtractor : public UniversalExtractor<InputType, OutputType> {
84public:
88
89 /**
90 * @brief Construct FeatureExtractor with default parameters
91 * @param window_size Analysis window size (default: 512)
92 * @param hop_size Hop size between windows (default: 256)
93 * @param method Initial extraction method (default: HIGH_ENERGY_DATA)
94 */
95 explicit FeatureExtractor(uint32_t window_size = 512,
96 uint32_t hop_size = 256,
98 : m_window_size(window_size)
99 , m_hop_size(hop_size)
100 , m_method(method)
101 {
103 }
104
105 /**
106 * @brief Construct and configure a feature extractor in one step.
107 * @param config Initial extraction settings.
108 */
110 : FeatureExtractor(config.window_size, config.hop_size, config.method)
111 {
113 this->set_parameter("scope", config.scope);
114
115 for (const auto& [name, value] : config.parameters) {
116 this->set_parameter(name, value);
117 }
118 }
119
120 /**
121 * @brief Get extraction type category
122 * @return ExtractionType::FEATURE_GUIDED
123 */
124 [[nodiscard]] ExtractionType get_extraction_type() const override
125 {
127 }
128
129 /**
130 * @brief Get available extraction methods
131 * @return Vector of supported method names
132 */
133 [[nodiscard]] std::vector<std::string> get_available_methods() const override
134 {
135 return Reflect::get_enum_names_lowercase<ExtractionMethod>();
136 }
137
138 /**
139 * @brief Set extraction method using enum
140 * @param method ExtractionMethod enum value
141 */
143 {
144 m_method = method;
145 this->set_parameter("method", method_to_string(method));
146 }
147
148 /**
149 * @brief Set extraction method using string (case-insensitive)
150 * @param method_name String representation of method
151 */
152 void set_extraction_method(const std::string& method_name)
153 {
154 m_method = string_to_method(method_name);
155 this->set_parameter("method", method_name);
156 }
157
158 /**
159 * @brief Get current extraction method
160 * @return ExtractionMethod enum value
161 */
163 {
164 return m_method;
165 }
166
167 /**
168 * @brief Set window size for analysis
169 * @param size Window size in samples
170 */
171 void set_window_size(uint32_t size)
172 {
175 }
176
177 /**
178 * @brief Set hop size for analysis
179 * @param size Hop size in samples
180 */
181 void set_hop_size(uint32_t size)
182 {
185 }
186
187 /**
188 * @brief Get current window size
189 * @return Window size in samples
190 */
191 [[nodiscard]] uint32_t get_window_size() const { return m_window_size; }
192
193 /**
194 * @brief Get current hop size
195 * @return Hop size in samples
196 */
197 [[nodiscard]] uint32_t get_hop_size() const { return m_hop_size; }
198
199 /**
200 * @brief Convert extraction method enum to string
201 * @param method ExtractionMethod value
202 * @return Lowercase string representation
203 */
204 static std::string method_to_string(ExtractionMethod method)
205 {
207 }
208
209 /**
210 * @brief Convert string to extraction method enum
211 * @param str String representation (case-insensitive)
212 * @return ExtractionMethod value
213 */
214 static ExtractionMethod string_to_method(const std::string& str)
215 {
216 return Reflect::string_to_enum_or_throw_case_insensitive<ExtractionMethod>(str, "ExtractionMethod");
217 }
218
219 /**
220 * @brief Input validation
221 */
222 bool validate_extraction_input(const input_type& input) const override
223 {
224 try {
225 if constexpr (RequiresContainer<input_type>) {
226 if (!input.has_container())
227 return false;
228 }
229 auto numeric_data = OperationHelper::extract_numeric_data(input.data);
230 if (numeric_data.empty())
231 return false;
232 for (const auto& span : numeric_data) {
234 }
235 return true;
236 } catch (const std::exception& e) {
237 MF_ERROR(Journal::Component::Yantra, Journal::Context::ComputeMatrix, "Input validation failed: {}", e.what());
238 return false;
239 }
240 }
241
242 /**
243 * @brief Get extractor name
244 * @return "FeatureExtractor"
245 */
246 [[nodiscard]] std::string get_extractor_name() const override
247 {
248 return "FeatureExtractor";
249 }
250
251protected:
252 /**
253 * @brief Core extraction implementation - delegates to ExtractionHelper
254 * @param input Input data with metadata
255 * @return Extracted data with metadata
256 */
258 {
259 try {
260 auto [numeric_data, info] = OperationHelper::extract_structured_double(const_cast<input_type&>(input));
261 DataStructureInfo structure_info = info;
262
263 std::vector<std::span<const double>> channels;
264 channels.reserve(numeric_data.size());
265 for (auto& s : numeric_data)
266 channels.emplace_back(s.data(), s.size());
267
268 std::vector<std::vector<double>> extracted_data;
269
270 switch (m_method) {
271
273 extracted_data = extract_high_energy(channels,
274 this->template get_parameter_or_default<double>("energy_threshold", 0.1),
276 break;
277
279 extracted_data = extract_peaks(channels,
280 this->template get_parameter_or_default<double>("threshold", 0.1),
281 this->template get_parameter_or_default<double>("min_distance", 10.0),
282 this->template get_parameter_or_default<uint32_t>("region_size", 256));
283 break;
284
286 extracted_data = extract_outliers(channels,
287 this->template get_parameter_or_default<double>("std_dev_threshold", 2.0),
289 break;
290
292 extracted_data = extract_high_spectral(channels,
293 this->template get_parameter_or_default<double>("spectral_threshold", 0.1),
295 break;
296
298 extracted_data = extract_above_mean(channels,
299 this->template get_parameter_or_default<double>("mean_multiplier", 1.5),
301 break;
302
304 extracted_data = extract_overlapping_windows(channels,
306 this->template get_parameter_or_default<double>("overlap", 0.5));
307 break;
308
310 extracted_data = extract_zero_crossings(channels,
311 this->template get_parameter_or_default<double>("threshold", 0.0),
312 this->template get_parameter_or_default<double>("min_distance", 1.0),
313 this->template get_parameter_or_default<uint32_t>("region_size", 1));
314 break;
315
317 extracted_data = extract_silence(channels,
318 this->template get_parameter_or_default<double>("silence_threshold", 0.01),
319 this->template get_parameter_or_default<uint32_t>("min_duration", 1024),
321 break;
322
324 extracted_data = extract_onsets(channels,
325 this->template get_parameter_or_default<double>("threshold", 0.3),
326 this->template get_parameter_or_default<uint32_t>("region_size", 512),
327 this->template get_parameter_or_default<uint32_t>("fft_window_size", 1024),
328 m_hop_size);
329 break;
330
331 default:
332 error<std::invalid_argument>(Journal::Component::Yantra, Journal::Context::ComputeMatrix, std::source_location::current(), "Unknown extraction method");
333 }
334
335 output_type output = this->convert_result(extracted_data, structure_info);
336
337 output.template set_metadata<std::string>("extractor_type", "FeatureExtractor");
338 output.template set_metadata<std::string>("extraction_method", method_to_string(m_method));
339 output.template set_metadata<uint32_t>("window_size", static_cast<uint32_t>(m_window_size));
340 output.template set_metadata<uint32_t>("hop_size", static_cast<uint32_t>(m_hop_size));
341 output.template set_metadata<size_t>("extracted_samples", extracted_data.size());
342
343 return output;
344
345 } catch (const std::exception& e) {
346 MF_ERROR(Journal::Component::Yantra, Journal::Context::ComputeMatrix, "Feature extraction failed: {}", e.what());
347 return output_type {};
348 }
349 }
350
351 /**
352 * @brief Handle extractor-specific parameters
353 */
354 void set_extraction_parameter(const std::string& name, std::any value) override
355 {
356 if (name == "method") {
357 if (auto result = safe_any_cast<std::string>(value)) {
358 m_method = string_to_method(*result.value);
359 return;
360 }
361 if (auto result = safe_any_cast<ExtractionMethod>(value)) {
362 m_method = *result.value;
363 return;
364 }
365 error<std::invalid_argument>(Journal::Component::Yantra, Journal::Context::ComputeMatrix, std::source_location::current(), "Method parameter must be string or ExtractionMethod enum");
366 }
367
368 if (name == "window_size") {
369 if (auto result = safe_any_cast<uint32_t>(value)) {
370 m_window_size = *result.value;
372 return;
373 }
374 }
375 if (name == "hop_size") {
376 if (auto result = safe_any_cast<uint32_t>(value)) {
377 m_hop_size = *result.value;
379 return;
380 }
381 }
382
384 }
385
386 [[nodiscard]] std::any get_extraction_parameter(const std::string& name) const override
387 {
388 if (name == "method") {
390 }
391 if (name == "window_size") {
392 return m_window_size;
393 }
394 if (name == "hop_size") {
395 return m_hop_size;
396 }
397
399 }
400
401private:
403 uint32_t m_hop_size;
405
406 /**
407 * @brief Validate extraction parameters
408 */
410 {
411 if (m_window_size == 0) {
412 error<std::invalid_argument>(Journal::Component::Yantra, Journal::Context::ComputeMatrix, std::source_location::current(), "Window size must be greater than 0");
413 }
414 if (m_hop_size == 0) {
415 error<std::invalid_argument>(Journal::Component::Yantra, Journal::Context::ComputeMatrix, std::source_location::current(), "Hop size must be greater than 0");
416 }
418 error<std::invalid_argument>(Journal::Component::Yantra, Journal::Context::ComputeMatrix, std::source_location::current(), "Hop size should not exceed window size for optimal coverage");
419 }
420 }
421};
422
423/// Standard feature extractor: vector of [DataVariant -> vector<double>]
425
426/// Eigen Matrix feature extractor: DataVariant -> Matrixxd
428
429/// Container feature extractor: SignalContainer -> multi vector<double>
431
432/// Region feature extractor: Region -> multi vector<double>
434
435/// Variant feature extractor: DataVariant -> DataVariant
437
438} // namespace MayaFlux::Yantra
#define MF_ERROR(comp, ctx,...)
Core::GlobalInputConfig input
Definition Config.cpp:42
Discrete sequence extraction primitives for MayaFlux::Kinesis.
Named multichannel extraction functions for FeatureExtractor.
bool size
Modern, digital-first universal extractor framework for Maya Flux.
glm::vec3 value
std::string name
Definition VKDevice.cpp:123
uint32_t channels
std::vector< std::string > get_available_methods() const override
Get available extraction methods.
void set_extraction_method(ExtractionMethod method)
Set extraction method using enum.
void set_extraction_parameter(const std::string &name, std::any value) override
Handle extractor-specific parameters.
output_type extract_implementation(const input_type &input) override
Core extraction implementation - delegates to ExtractionHelper.
bool validate_extraction_input(const input_type &input) const override
Input validation.
void set_window_size(uint32_t size)
Set window size for analysis.
FeatureExtractor(const FeatureExtractorConfig &config)
Construct and configure a feature extractor in one step.
uint32_t get_window_size() const
Get current window size.
void set_hop_size(uint32_t size)
Set hop size for analysis.
FeatureExtractor(uint32_t window_size=512, uint32_t hop_size=256, ExtractionMethod method=ExtractionMethod::HIGH_ENERGY_DATA)
Construct FeatureExtractor with default parameters.
std::any get_extraction_parameter(const std::string &name) const override
uint32_t get_hop_size() const
Get current hop size.
std::string get_extractor_name() const override
Get extractor name.
ExtractionMethod get_extraction_method() const
Get current extraction method.
void validate_parameters() const
Validate extraction parameters.
static std::string method_to_string(ExtractionMethod method)
Convert extraction method enum to string.
static ExtractionMethod string_to_method(const std::string &str)
Convert string to extraction method enum.
void set_extraction_method(const std::string &method_name)
Set extraction method using string (case-insensitive)
ExtractionType get_extraction_type() const override
Get extraction type category.
Analyzer-guided feature extractor with enum-based configuration.
static std::span< double > extract_numeric_data(const T &compute_data)
extract numeric data from single-variant types
static std::tuple< std::vector< std::span< double > >, DataStructureInfo > extract_structured_double(T &compute_data)
Extract structured double data from Datum container or direct ComputeData with automatic container ha...
virtual std::any get_extraction_parameter(const std::string &name) const
virtual void set_extraction_parameter(const std::string &name, std::any value)
Extraction-specific parameter handling (override for custom parameters)
Template-flexible extractor base with instance-defined I/O types.
Types that need an associated SignalSourceContainer to extract data.
Definition DataSpec.hpp:110
@ ComputeMatrix
Compute operations (Yantra - algorithms, matrices, DSP)
@ Yantra
DSP algorithms, computational units, matrix operations, Grammar.
bool validate_window_parameters(uint32_t window_size, uint32_t hop_size, size_t data_size) noexcept
Validate window/hop parameters for windowed processing.
Definition Extract.cpp:5
std::vector< Keypoint > extract_peaks(std::span< const float > response, uint32_t w, uint32_t h, float threshold, uint32_t nms_radius)
Extract peaks from a response map via non-maximum suppression.
Definition Harris.cpp:197
std::string enum_to_lowercase_string(EnumType value) noexcept
Universal enum to lowercase string converter using magic_enum.
ExtractionType
Categories of extraction operations for discovery and organization.
@ FEATURE_GUIDED
Extract based on feature analysis.
std::vector< std::vector< double > > extract_high_energy(const std::vector< std::span< const double > > &channels, double energy_threshold, uint32_t window_size, uint32_t hop_size)
std::vector< std::vector< double > > extract_zero_crossings(const std::vector< std::span< const double > > &channels, double threshold, double min_distance, uint32_t region_size)
std::vector< std::vector< double > > extract_outliers(const std::vector< std::span< const double > > &channels, double std_dev_threshold, uint32_t window_size, uint32_t hop_size)
std::vector< std::vector< double > > extract_silence(const std::vector< std::span< const double > > &channels, double silence_threshold, uint32_t min_duration, uint32_t window_size, uint32_t hop_size)
ExtractionMethod
Supported extraction methods for FeatureExtractor.
@ ABOVE_MEAN_DATA
Extract data above statistical mean.
@ SILENCE_DATA
Extract actual silent regions.
@ PEAK_DATA
Extract data around detected peaks.
@ OUTLIER_DATA
Extract data from statistical outlier regions.
@ ZERO_CROSSING_DATA
Extract actual data at zero crossing points.
@ HIGH_ENERGY_DATA
Extract data from high-energy regions.
@ HIGH_SPECTRAL_DATA
Extract data from high spectral energy regions.
@ OVERLAPPING_WINDOWS
Extract overlapping windowed data.
@ ONSET_DATA
Extract actual onset/transient regions.
std::vector< std::vector< double > > extract_onsets(const std::vector< std::span< const double > > &channels, double threshold, uint32_t region_size, uint32_t fft_window_size, uint32_t hop_size)
std::vector< std::vector< double > > extract_high_spectral(const std::vector< std::span< const double > > &channels, double spectral_threshold, uint32_t window_size, uint32_t hop_size)
std::vector< std::vector< double > > extract_overlapping_windows(const std::vector< std::span< const double > > &channels, uint32_t window_size, double overlap)
std::vector< std::vector< double > > extract_above_mean(const std::vector< std::span< const double > > &channels, double mean_multiplier, uint32_t window_size, uint32_t hop_size)
ExtractionScope
Scope control for extraction operations.
@ FULL_DATA
Extract all available data.
Dimensions, modality, source type, and optional Composite field schema.
Input/Output container for computation pipeline data flow with structure preservation.
Definition DataIO.hpp:26
uint32_t window_size
Analysis window length in samples.
std::map< std::string, std::any > parameters
Additional named parameters applied last.
ExtractionScope scope
Portion of the input to extract from.
uint32_t hop_size
Distance between successive windows in samples.
ExtractionMethod method
Feature selection method.
Initial settings for feature guided extraction.