MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
ContainerUtils.hpp
Go to the documentation of this file.
1#pragma once
2
4
5namespace MayaFlux::Kakshya {
6
7/**
8 * @brief Extract processing state information from container.
9 * @param container Container to analyze.
10 * @return Map containing processing state metadata.
11 */
12std::unordered_map<std::string, std::any> extract_processing_state_info(const std::shared_ptr<SignalSourceContainer>& container);
13
14/**
15 * @brief Extract processor information from container.
16 * @param container Container to analyze.
17 * @return Map containing processor metadata.
18 */
19std::unordered_map<std::string, std::any> extract_processor_info(const std::shared_ptr<SignalSourceContainer>& container);
20
21/**
22 * @brief Perform a state transition for a ProcessingState, with optional callback.
23 * @param current_state Current state (modified in place).
24 * @param new_state State to transition to.
25 * @param on_transition Optional callback to invoke on transition.
26 * @return True if transition was valid and performed.
27 */
28bool transition_state(ProcessingState& current_state, ProcessingState new_state, std::function<void()> on_transition = nullptr);
29
30/**
31 * @brief Determine optimal memory access pattern for region.
32 * @param region Region to analyze.
33 * @param container Container providing layout information.
34 * @return Recommended access pattern information.
35 */
36std::unordered_map<std::string, std::any> analyze_access_pattern(const Region& region,
37 const std::shared_ptr<SignalSourceContainer>& container);
38
39/**
40 * @brief Interleave multiple channels of data into a single vector.
41 * @tparam T Data type.
42 * @param channels Vector of channel vectors.
43 * @return Interleaved data vector.
44 */
45template <typename T>
46std::vector<T> interleave_channels(const std::vector<std::vector<T>>& channels)
47{
48 if (channels.empty()) {
49 return {};
50 }
51 size_t num_channels = channels.size();
52 size_t samples_per_channel = channels[0].size();
53 std::vector<T> result(num_channels * samples_per_channel);
54 for (size_t i = 0; i < samples_per_channel; ++i) {
55 for (size_t ch = 0; ch < num_channels; ++ch) {
56 result[i * num_channels + ch] = channels[ch][i];
57 }
58 }
59 return result;
60}
61
62/**
63 * @brief Deinterleave a single vector into multiple channels.
64 * @tparam T Data type.
65 * @param interleaved Interleaved data span.
66 * @param num_channels Number of channels.
67 * @return Vector of channel vectors.
68 */
69template <typename T>
70std::vector<std::vector<T>> deinterleave_channels(std::span<const T> interleaved, size_t num_channels)
71{
72 if (interleaved.empty() || num_channels == 0) {
73 return {};
74 }
75 size_t samples_per_channel = interleaved.size() / num_channels;
76 std::vector<std::vector<T>> result(num_channels);
77 for (size_t ch = 0; ch < num_channels; ++ch) {
78 result[ch].resize(samples_per_channel);
79 for (size_t i = 0; i < samples_per_channel; ++i) {
80 result[ch][i] = interleaved[i * num_channels + ch];
81 }
82 }
83 return result;
84}
85
86/**
87 * @brief Extract data from a specific channel.
88 * @param container The container to extract from.
89 * @param channel_index Index of the channel to extract.
90 * @return DataVariant containing channel data.
91 */
92DataVariant extract_channel_data(const std::shared_ptr<SignalSourceContainer>& container,
93 uint32_t channel_index);
94
95/**
96 * @brief Extract one channel's samples from a processed dynamic data block.
97 *
98 * Handles both INTERLEAVED and PLANAR organization. For interleaved layout
99 * pd[0] holds all channels multiplexed; for planar layout pd[ch] holds the
100 * channel directly. In either case the result is written into @p output up
101 * to min(output.size(), available_samples) samples, with any remainder
102 * zero-filled.
103 *
104 * @param pd Dynamic data block as returned by
105 * container::get_processed_data() or similar.
106 * @param organization Memory organization of the stream.
107 * @param num_channels Total channel count of the stream.
108 * @param ch Zero-based channel index to extract.
109 * @param output Destination buffer; size determines the copy limit.
110 */
112 const std::vector<DataVariant>& pd,
113 OrganizationStrategy organization,
114 uint64_t num_channels,
115 uint32_t ch,
116 std::span<double> output);
117
118/**
119 * @brief Extract samples for a single channel within a Region from a container.
120 *
121 * Slices the container's channel data by the sample range defined by
122 * @p region's start_coordinates[0] and end_coordinates[0] (inclusive).
123 * Uses get_region_data for the slice, then converts the requested channel
124 * variant to double. Returns an empty vector on any error (null container,
125 * channel out of range, empty region data).
126 *
127 * This is the domain-neutral replacement for Granular::extract_grain_samples.
128 * It carries no assumption about grains, hop sizes, or granular semantics.
129 *
130 * @param region Region whose first coordinate pair defines the sample range.
131 * @param container Signal data source.
132 * @param channel Zero-based channel index.
133 * @return Vector of double samples for the requested region and channel.
134 */
135[[nodiscard]] MAYAFLUX_API std::vector<double> extract_region_channel(
136 const Region& region,
137 const std::shared_ptr<SignalSourceContainer>& container,
138 uint32_t channel);
139
140/**
141 * @brief Validates container for analysis operations with comprehensive checks
142 * @param container Container to validate
143 * @return Pair of validated container and its dimensions
144 * @throws std::invalid_argument if container is null or has no data
145 * @throws std::runtime_error if container has no dimensions
146 */
147std::pair<std::shared_ptr<SignalSourceContainer>, std::vector<DataDimension>>
148validate_container_for_analysis(const std::shared_ptr<SignalSourceContainer>& container);
149
150/**
151 * @brief Extracts numeric data from container with fallback handling
152 * @param container Container to extract from
153 * @return Vector of double values from container data
154 * @throws std::runtime_error if no numeric data can be extracted
155 */
156std::vector<std::span<double>> extract_numeric_data(const std::shared_ptr<SignalSourceContainer>& container);
157
158/**
159 * @brief Validates numeric data for analysis operations
160 * @param data Data to validate
161 * @param operation_name Name of operation for error messages
162 * @param min_size Minimum required data size (default 1)
163 * @throws std::invalid_argument if data is invalid for analysis
164 */
165void validate_numeric_data_for_analysis(const std::vector<double>& data,
166 const std::string& operation_name,
167 size_t min_size = 1);
168
169}
uint32_t channel
ProcessingState
Represents the current processing lifecycle state of a container.
std::unordered_map< std::string, std::any > analyze_access_pattern(const Region &region, const std::shared_ptr< SignalSourceContainer > &container)
Determine optimal memory access pattern for region.
std::variant< std::vector< double >, std::vector< float >, std::vector< uint8_t >, std::vector< uint16_t >, std::vector< uint32_t >, std::vector< std::complex< float > >, std::vector< std::complex< double > >, std::vector< glm::vec2 >, std::vector< glm::vec3 >, std::vector< glm::vec4 >, std::vector< glm::mat4 > > DataVariant
Multi-type data storage for different precision needs.
Definition NDData.hpp:76
DataVariant extract_channel_data(const std::shared_ptr< SignalSourceContainer > &container, uint32_t channel_index)
Extract data from a specific channel.
OrganizationStrategy
Data organization strategy for multi-channel/multi-frame data.
Definition NDData.hpp:49
std::unordered_map< std::string, std::any > extract_processing_state_info(const std::shared_ptr< SignalSourceContainer > &container)
Extract processing state information from container.
std::vector< std::span< double > > extract_numeric_data(const std::shared_ptr< SignalSourceContainer > &container)
Extracts numeric data from container with fallback handling.
void validate_numeric_data_for_analysis(const std::vector< double > &data, const std::string &operation_name, size_t min_size)
Validates numeric data for analysis operations.
std::vector< T > interleave_channels(const std::vector< std::vector< T > > &channels)
Interleave multiple channels of data into a single vector.
void extract_processed_data(const std::vector< DataVariant > &pd, OrganizationStrategy organization, uint64_t num_channels, uint32_t ch, std::span< double > output)
Extract one channel's samples from a processed dynamic data block.
std::pair< std::shared_ptr< SignalSourceContainer >, std::vector< DataDimension > > validate_container_for_analysis(const std::shared_ptr< SignalSourceContainer > &container)
Validates container for analysis operations with comprehensive checks.
std::vector< double > extract_region_channel(const Region &region, const std::shared_ptr< SignalSourceContainer > &container, uint32_t channel)
Extract samples for a single channel within a Region from a container.
std::unordered_map< std::string, std::any > extract_processor_info(const std::shared_ptr< SignalSourceContainer > &container)
Extract processor information from container.
std::vector< std::vector< T > > deinterleave_channels(std::span< const T > interleaved, size_t num_channels)
Deinterleave a single vector into multiple channels.
bool transition_state(ProcessingState &current_state, ProcessingState new_state, std::function< void()> on_transition)
Perform a state transition for a ProcessingState, with optional callback.