NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
DataCube.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <deque>
31#include <filesystem>
32#include <fstream>
33#include <string>
34#include <vector>
35
37#include "NumCpp/Core/Shape.hpp"
38#include "NumCpp/Core/Slice.hpp"
39#include "NumCpp/Core/Types.hpp"
40#include "NumCpp/NdArray.hpp"
41
42namespace nc
43{
44 //================================================================================
46 template<typename dtype>
48 {
49 public:
50 //================================Typedefs==================================
51 using iterator = typename std::deque<NdArray<dtype>>::iterator;
52 using const_iterator = typename std::deque<NdArray<dtype>>::const_iterator;
53
54 //============================================================================
57 DataCube() = default;
58
59 //============================================================================
64 explicit DataCube(uint32 inSize)
65 {
66 cube_.reserve(inSize);
67 }
68
69 //============================================================================
77 {
78 return cube_.at(inIndex);
79 }
80
81 //============================================================================
88 [[nodiscard]] const NdArray<dtype>& at(uint32 inIndex) const
89 {
90 return cube_.at(inIndex);
91 }
92
93 //============================================================================
98 NdArray<dtype>& back() noexcept
99 {
100 return cube_.back();
101 }
102
103 //============================================================================
108 [[nodiscard]] iterator begin() noexcept
109 {
110 return cube_.begin();
111 }
112
113 //============================================================================
118 [[nodiscard]] const_iterator begin() const noexcept
119 {
120 return cube_.cbegin();
121 }
122
123 //============================================================================
128 [[nodiscard]] const_iterator cbegin() const noexcept
129 {
130 return cube_.cbegin();
131 }
132
133 //============================================================================
138 void dump(const std::string& inFilename) const
139 {
140 std::filesystem::path f(inFilename);
141 if (!f.has_extension())
142 {
143 f.replace_extension("bin");
144 }
145
146 std::ofstream ofile(f.c_str(), std::ios::binary);
147 if (!ofile.good())
148 {
149 THROW_RUNTIME_ERROR("Could not open the input file:\n\t" + inFilename);
150 }
151
152 for (auto& ndarray : cube_)
153 {
154 ofile.write(reinterpret_cast<const char*>(ndarray.data()), ndarray.size() * sizeof(dtype));
155 }
156
157 ofile.close();
158 }
159
160 //============================================================================
165 bool isempty() noexcept
166 {
167 return cube_.empty();
168 }
169
170 //============================================================================
175 [[nodiscard]] iterator end() noexcept
176 {
177 return cube_.end();
178 }
179
180 //============================================================================
185 [[nodiscard]] const_iterator end() const noexcept
186 {
187 return cube_.cend();
188 }
189
190 //============================================================================
195 [[nodiscard]] const_iterator cend() const noexcept
196 {
197 return cube_.cend();
198 }
199
200 //============================================================================
206 {
207 return cube_.front();
208 }
209
210 //============================================================================
215 [[nodiscard]] const Shape& shape() const noexcept
216 {
217 return elementShape_;
218 }
219
220 //============================================================================
225 [[nodiscard]] uint32 sizeZ() const noexcept
226 {
227 return static_cast<uint32>(cube_.size());
228 }
229
230 //============================================================================
233 void pop_back() noexcept
234 {
235 cube_.pop_back();
236 }
237
238 //============================================================================
243 void push_back(const NdArray<dtype>& inArray)
244 {
245 const Shape inputShape = inArray.shape();
246
247 if (elementShape_.rows == 0 && elementShape_.cols == 0)
248 {
249 // initialize to the first input array size
250 elementShape_.rows = inputShape.rows;
251 elementShape_.cols = inputShape.cols;
252 }
253
254 if (inputShape != elementShape_)
255 {
256 THROW_INVALID_ARGUMENT_ERROR("element arrays must all be the same shape");
257 }
258
259 cube_.push_back(inArray);
260 }
261
262 //============================================================================
268 [[nodiscard]] NdArray<dtype> sliceZAll(int32 inIndex) const
269 {
270 if (inIndex < 0)
271 {
272 inIndex += elementShape_.size();
273 }
274
275 NdArray<dtype> returnArray(1, sizeZ());
276
277 for (uint32 i = 0; i < sizeZ(); ++i)
278 {
279 returnArray[i] = cube_[i][inIndex];
280 }
281
282 return returnArray;
283 }
284
285 //============================================================================
292 [[nodiscard]] NdArray<dtype> sliceZ(int32 inIndex, Slice inSliceZ) const
293 {
294 if (inIndex < 0)
295 {
296 inIndex += elementShape_.size();
297 }
298
299 NdArray<dtype> returnArray(1, inSliceZ.numElements(sizeZ()));
300
301 uint32 idx = 0;
302 for (int32 i = inSliceZ.start; i < inSliceZ.stop; i += inSliceZ.step)
303 {
304 returnArray[idx++] = cube_[i][inIndex];
305 }
306
307 return returnArray;
308 }
309
310 //============================================================================
317 [[nodiscard]] NdArray<dtype> sliceZAll(int32 inRow, int32 inCol) const
318 {
319 if (inRow < 0)
320 {
321 inRow += elementShape_.rows;
322 }
323
324 if (inCol < 0)
325 {
326 inCol += elementShape_.cols;
327 }
328
329 NdArray<dtype> returnArray(1, sizeZ());
330
331 for (uint32 i = 0; i < sizeZ(); ++i)
332 {
333 returnArray[i] = cube_[i](inRow, inCol);
334 }
335
336 return returnArray;
337 }
338
339 //============================================================================
347 [[nodiscard]] NdArray<dtype> sliceZ(int32 inRow, int32 inCol, Slice inSliceZ) const
348 {
349 if (inRow < 0)
350 {
351 inRow += elementShape_.rows;
352 }
353
354 if (inCol < 0)
355 {
356 inCol += elementShape_.cols;
357 }
358
359 NdArray<dtype> returnArray(1, inSliceZ.numElements(sizeZ()));
360
361 uint32 idx = 0;
362 for (int32 i = inSliceZ.start; i < inSliceZ.stop; i += inSliceZ.step)
363 {
364 returnArray[idx++] = cube_[i](inRow, inCol);
365 }
366
367 return returnArray;
368 }
369
370 //============================================================================
377 [[nodiscard]] NdArray<dtype> sliceZAll(Slice inRow, int32 inCol) const
378 {
379 if (inCol < 0)
380 {
381 inCol += elementShape_.cols;
382 }
383
384 NdArray<dtype> returnArray(inRow.numElements(elementShape_.rows), sizeZ());
385 for (uint32 i = 0; i < sizeZ(); ++i)
386 {
387 returnArray.put(returnArray.rSlice(), i, cube_[i](inRow, inCol));
388 }
389
390 return returnArray;
391 }
392
393 //============================================================================
401 [[nodiscard]] NdArray<dtype> sliceZ(Slice inRow, int32 inCol, Slice inSliceZ) const
402 {
403 if (inCol < 0)
404 {
405 inCol += elementShape_.cols;
406 }
407
408 NdArray<dtype> returnArray(inRow.numElements(elementShape_.rows), inSliceZ.numElements(sizeZ()));
409 uint32 idx = 0;
410 for (int32 i = inSliceZ.start; i < inSliceZ.stop; i += inSliceZ.step)
411 {
412 returnArray.put(returnArray.rSlice(), idx++, cube_[i](inRow, inCol));
413 }
414
415 return returnArray;
416 }
417
418 //============================================================================
425 [[nodiscard]] NdArray<dtype> sliceZAll(int32 inRow, Slice inCol) const
426 {
427 if (inRow < 0)
428 {
429 inRow += elementShape_.rows;
430 }
431
432 NdArray<dtype> returnArray(inCol.numElements(elementShape_.cols), sizeZ());
433 for (uint32 i = 0; i < sizeZ(); ++i)
434 {
435 returnArray.put(returnArray.rSlice(), i, cube_[i](inRow, inCol));
436 }
437
438 return returnArray;
439 }
440
441 //============================================================================
449 [[nodiscard]] NdArray<dtype> sliceZ(int32 inRow, Slice inCol, Slice inSliceZ) const
450 {
451 if (inRow < 0)
452 {
453 inRow += elementShape_.rows;
454 }
455
456 NdArray<dtype> returnArray(inCol.numElements(elementShape_.cols), inSliceZ.numElements(sizeZ()));
457 uint32 idx = 0;
458 for (int32 i = inSliceZ.start; i < inSliceZ.stop; i += inSliceZ.step)
459 {
460 returnArray.put(returnArray.rSlice(), idx++, cube_[i](inRow, inCol));
461 }
462
463 return returnArray;
464 }
465
466 //============================================================================
474 {
475 DataCube<dtype> returnCube(sizeZ());
476 for (uint32 i = 0; i < sizeZ(); ++i)
477 {
478 returnCube.push_back(cube_[i](inRow, inCol));
479 }
480
481 return returnCube;
482 }
483
484 //============================================================================
492 DataCube<dtype> sliceZ(Slice inRow, Slice inCol, Slice inSliceZ) const
493 {
494 DataCube<dtype> returnCube(inSliceZ.numElements(sizeZ()));
495 for (int32 i = inSliceZ.start; i < inSliceZ.stop; i += inSliceZ.step)
496 {
497 returnCube.push_back(cube_[i](inRow, inCol));
498 }
499
500 return returnCube;
501 }
502
503 //============================================================================
509 [[nodiscard]] NdArray<dtype> sliceZAllat(int32 inIndex) const
510 {
511 if (inIndex < 0)
512 {
513 inIndex += elementShape_.size();
514 }
515
516 if (static_cast<uint32>(inIndex) >= elementShape_.size())
517 {
518 THROW_INVALID_ARGUMENT_ERROR("inIndex exceeds matrix dimensions.");
519 }
520
521 return sliceZAll(inIndex);
522 }
523
524 //============================================================================
531 [[nodiscard]] NdArray<dtype> sliceZat(int32 inIndex, Slice inSliceZ) const
532 {
533 if (inIndex < 0)
534 {
535 inIndex += elementShape_.size();
536 }
537
538 if (static_cast<uint32>(inIndex) >= elementShape_.size())
539 {
540 THROW_INVALID_ARGUMENT_ERROR("inIndex exceeds matrix dimensions.");
541 }
542
543 auto numElements = inSliceZ.numElements(sizeZ());
544 if (numElements > sizeZ())
545 {
546 THROW_INVALID_ARGUMENT_ERROR("inIndex exceeds matrix dimensions.");
547 }
548
549 return sliceZ(inIndex, inSliceZ);
550 }
551
552 //============================================================================
559 [[nodiscard]] NdArray<dtype> sliceZAllat(int32 inRow, int32 inCol) const
560 {
561 if (inRow < 0)
562 {
563 inRow += elementShape_.rows;
564 }
565
566 if (inCol < 0)
567 {
568 inCol += elementShape_.cols;
569 }
570
571 if (static_cast<uint32>(inRow) >= elementShape_.rows)
572 {
573 THROW_INVALID_ARGUMENT_ERROR("inRow exceeds matrix dimensions.");
574 }
575
576 if (static_cast<uint32>(inCol) >= elementShape_.cols)
577 {
578 THROW_INVALID_ARGUMENT_ERROR("inCol exceeds matrix dimensions.");
579 }
580
581 return sliceZAll(inRow, inCol);
582 }
583
584 //============================================================================
592 [[nodiscard]] NdArray<dtype> sliceZat(int32 inRow, int32 inCol, Slice inSliceZ) const
593 {
594 if (inRow < 0)
595 {
596 inRow += elementShape_.rows;
597 }
598
599 if (inCol < 0)
600 {
601 inCol += elementShape_.cols;
602 }
603
604 if (static_cast<uint32>(inRow) >= elementShape_.rows)
605 {
606 THROW_INVALID_ARGUMENT_ERROR("inRow exceeds matrix dimensions.");
607 }
608 if (static_cast<uint32>(inCol) >= elementShape_.cols)
609 {
610 THROW_INVALID_ARGUMENT_ERROR("inCol exceeds matrix dimensions.");
611 }
612
613 auto numElements = inSliceZ.numElements(sizeZ());
614 if (numElements > sizeZ())
615 {
616 THROW_INVALID_ARGUMENT_ERROR("Index exceeds matrix dimensions.");
617 }
618
619 return sliceZ(inRow, inCol, inSliceZ);
620 }
621
622 //============================================================================
629 [[nodiscard]] NdArray<dtype> sliceZAllat(Slice inRow, int32 inCol) const
630 {
631 auto numRows = inRow.numElements(elementShape_.rows);
632 if (numRows > elementShape_.rows)
633 {
634 THROW_INVALID_ARGUMENT_ERROR("inRow exceeds matrix dimensions.");
635 }
636
637 if (inCol < 0)
638 {
639 inCol += elementShape_.cols;
640 }
641
642 if (static_cast<uint32>(inCol) >= elementShape_.cols)
643 {
644 THROW_INVALID_ARGUMENT_ERROR("inCol exceeds matrix dimensions.");
645 }
646
647 return sliceZAll(inRow, inCol);
648 }
649
650 //============================================================================
658 [[nodiscard]] NdArray<dtype> sliceZat(Slice inRow, int32 inCol, Slice inSliceZ) const
659 {
660 auto numRows = inRow.numElements(elementShape_.rows);
661 if (numRows > elementShape_.rows)
662 {
663 THROW_INVALID_ARGUMENT_ERROR("inRow exceeds matrix dimensions.");
664 }
665
666 if (inCol < 0)
667 {
668 inCol += elementShape_.cols;
669 }
670
671 if (static_cast<uint32>(inCol) >= elementShape_.cols)
672 {
673 THROW_INVALID_ARGUMENT_ERROR("inCol exceeds matrix dimensions.");
674 }
675
676 auto numElements = inSliceZ.numElements(sizeZ());
677 if (numElements > sizeZ())
678 {
679 THROW_INVALID_ARGUMENT_ERROR("Index exceeds matrix dimensions.");
680 }
681
682 return sliceZ(inRow, inCol, inSliceZ);
683 }
684
685 //============================================================================
692 [[nodiscard]] NdArray<dtype> sliceZAllat(int32 inRow, Slice inCol) const
693 {
694 auto numCols = inCol.numElements(elementShape_.cols);
695 if (numCols > elementShape_.cols)
696 {
697 THROW_INVALID_ARGUMENT_ERROR("inCol exceeds matrix dimensions.");
698 }
699
700 if (inRow < 0)
701 {
702 inRow += elementShape_.rows;
703 }
704
705 if (static_cast<uint32>(inRow) >= elementShape_.rows)
706 {
707 THROW_INVALID_ARGUMENT_ERROR("inRow exceeds matrix dimensions.");
708 }
709
710 return sliceZAll(inRow, inCol);
711 }
712
713 //============================================================================
721 [[nodiscard]] NdArray<dtype> sliceZat(int32 inRow, Slice inCol, Slice inSliceZ) const
722 {
723 auto numCols = inCol.numElements(elementShape_.cols);
724 if (numCols > elementShape_.cols)
725 {
726 THROW_INVALID_ARGUMENT_ERROR("inCol exceeds matrix dimensions.");
727 }
728
729 if (inRow < 0)
730 {
731 inRow += elementShape_.rows;
732 }
733
734 if (static_cast<uint32>(inRow) >= elementShape_.rows)
735 {
736 THROW_INVALID_ARGUMENT_ERROR("inRow exceeds matrix dimensions.");
737 }
738
739 auto numElements = inSliceZ.numElements(sizeZ());
740 if (numElements > sizeZ())
741 {
742 THROW_INVALID_ARGUMENT_ERROR("Index exceeds matrix dimensions.");
743 }
744
745 return sliceZ(inRow, inCol, inSliceZ);
746 }
747
748 //============================================================================
756 {
757 if (inRow.numElements(elementShape_.rows) > elementShape_.rows)
758 {
759 THROW_INVALID_ARGUMENT_ERROR("inRow exceeds matrix dimensions.");
760 }
761
762 if (inCol.numElements(elementShape_.cols) > elementShape_.cols)
763 {
764 THROW_INVALID_ARGUMENT_ERROR("inCol exceeds matrix dimensions.");
765 }
766
767 return sliceZAll(inRow, inCol);
768 }
769
770 //============================================================================
778 DataCube<dtype> sliceZat(Slice inRow, Slice inCol, Slice inSliceZ) const
779 {
780 if (inRow.numElements(elementShape_.rows) > elementShape_.rows)
781 {
782 THROW_INVALID_ARGUMENT_ERROR("inRow exceeds matrix dimensions.");
783 }
784
785 if (inCol.numElements(elementShape_.cols) > elementShape_.cols)
786 {
787 THROW_INVALID_ARGUMENT_ERROR("inCol exceeds matrix dimensions.");
788 }
789
790 auto numElements = inSliceZ.numElements(sizeZ());
791 if (numElements > sizeZ())
792 {
793 THROW_INVALID_ARGUMENT_ERROR("Index exceeds matrix dimensions.");
794 }
795
796 return sliceZ(inRow, inCol, inSliceZ);
797 }
798
799 //============================================================================
807 {
808 return cube_[inIndex];
809 }
810
811 //============================================================================
818 const NdArray<dtype>& operator[](uint32 inIndex) const noexcept
819 {
820 return cube_[inIndex];
821 }
822
823 private:
824 //================================Attributes==================================
825 std::vector<NdArray<dtype>> cube_{};
826 Shape elementShape_{ 0, 0 };
827 };
828} // namespace nc
#define THROW_RUNTIME_ERROR(msg)
Definition Error.hpp:40
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition Error.hpp:37
void push_back(const NdArray< dtype > &inArray)
Definition DataCube.hpp:243
NdArray< dtype > sliceZAll(int32 inIndex) const
Definition DataCube.hpp:268
iterator begin() noexcept
Definition DataCube.hpp:108
const NdArray< dtype > & at(uint32 inIndex) const
Definition DataCube.hpp:88
NdArray< dtype > & at(uint32 inIndex)
Definition DataCube.hpp:76
NdArray< dtype > & operator[](uint32 inIndex) noexcept
Definition DataCube.hpp:806
NdArray< dtype > sliceZ(int32 inRow, int32 inCol, Slice inSliceZ) const
Definition DataCube.hpp:347
DataCube< dtype > sliceZat(Slice inRow, Slice inCol, Slice inSliceZ) const
Definition DataCube.hpp:778
NdArray< dtype > sliceZAllat(Slice inRow, int32 inCol) const
Definition DataCube.hpp:629
uint32 sizeZ() const noexcept
Definition DataCube.hpp:225
NdArray< dtype > sliceZAll(Slice inRow, int32 inCol) const
Definition DataCube.hpp:377
DataCube< dtype > sliceZ(Slice inRow, Slice inCol, Slice inSliceZ) const
Definition DataCube.hpp:492
NdArray< dtype > sliceZat(Slice inRow, int32 inCol, Slice inSliceZ) const
Definition DataCube.hpp:658
DataCube(uint32 inSize)
Definition DataCube.hpp:64
DataCube()=default
DataCube< dtype > sliceZAll(Slice inRow, Slice inCol) const
Definition DataCube.hpp:473
void pop_back() noexcept
Definition DataCube.hpp:233
NdArray< dtype > sliceZAllat(int32 inRow, Slice inCol) const
Definition DataCube.hpp:692
NdArray< dtype > sliceZat(int32 inIndex, Slice inSliceZ) const
Definition DataCube.hpp:531
const Shape & shape() const noexcept
Definition DataCube.hpp:215
NdArray< dtype > sliceZAll(int32 inRow, int32 inCol) const
Definition DataCube.hpp:317
iterator end() noexcept
Definition DataCube.hpp:175
NdArray< dtype > & front() noexcept
Definition DataCube.hpp:205
NdArray< dtype > sliceZAllat(int32 inIndex) const
Definition DataCube.hpp:509
typename std::deque< NdArray< dtype > >::iterator iterator
Definition DataCube.hpp:51
NdArray< dtype > & back() noexcept
Definition DataCube.hpp:98
const NdArray< dtype > & operator[](uint32 inIndex) const noexcept
Definition DataCube.hpp:818
void dump(const std::string &inFilename) const
Definition DataCube.hpp:138
typename std::deque< NdArray< dtype > >::const_iterator const_iterator
Definition DataCube.hpp:52
bool isempty() noexcept
Definition DataCube.hpp:165
const_iterator cend() const noexcept
Definition DataCube.hpp:195
const_iterator end() const noexcept
Definition DataCube.hpp:185
NdArray< dtype > sliceZAllat(int32 inRow, int32 inCol) const
Definition DataCube.hpp:559
const_iterator begin() const noexcept
Definition DataCube.hpp:118
const_iterator cbegin() const noexcept
Definition DataCube.hpp:128
NdArray< dtype > sliceZAll(int32 inRow, Slice inCol) const
Definition DataCube.hpp:425
NdArray< dtype > sliceZ(Slice inRow, int32 inCol, Slice inSliceZ) const
Definition DataCube.hpp:401
NdArray< dtype > sliceZ(int32 inIndex, Slice inSliceZ) const
Definition DataCube.hpp:292
NdArray< dtype > sliceZ(int32 inRow, Slice inCol, Slice inSliceZ) const
Definition DataCube.hpp:449
NdArray< dtype > sliceZat(int32 inRow, Slice inCol, Slice inSliceZ) const
Definition DataCube.hpp:721
DataCube< dtype > sliceZAllat(Slice inRow, Slice inCol) const
Definition DataCube.hpp:755
NdArray< dtype > sliceZat(int32 inRow, int32 inCol, Slice inSliceZ) const
Definition DataCube.hpp:592
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition NdArrayCore.hpp:135
const Shape & shape() const noexcept
Definition NdArrayCore.hpp:4587
Slice rSlice(index_type inStartIdx=0, size_type inStepSize=1) const
Definition NdArrayCore.hpp:1018
self_type & put(index_type inIndex, const value_type &inValue)
Definition NdArrayCore.hpp:3769
A Shape Class for NdArrays.
Definition Core/shape.hpp:41
uint32 rows
Definition Core/shape.hpp:44
uint32 cols
Definition Core/shape.hpp:45
A Class for slicing into NdArrays.
Definition Slice.hpp:45
int32 step
Definition Slice.hpp:50
int32 start
Definition Slice.hpp:48
uint32 numElements(uint32 inArraySize)
Definition Slice.hpp:195
int32 stop
Definition Slice.hpp:49
Definition Cartesian.hpp:39
std::int32_t int32
Definition Types.hpp:36
std::uint32_t uint32
Definition Types.hpp:40