forked from OpenChemistry/stempy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyreader.cpp
More file actions
77 lines (60 loc) · 1.94 KB
/
pyreader.cpp
File metadata and controls
77 lines (60 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "pyreader.h"
#include <stempy/reader.h>
namespace stempy {
PyBlock::PyBlock(py::array_t<uint16_t> pyarray)
{
// Since this is the constructor, this should not be deleting anything
// Otherwise, we might would need py::gil_scoped_acquire gil
this->data.array.reset(new py::array_t<uint16_t>(std::move(pyarray)));
}
PyReader::PyReader(py::object pyDataSet, std::vector<uint32_t>& imageNumbers,
Dimensions2D scanDimensions, uint32_t blockSize,
uint32_t totalImageNum)
: m_pydataset(pyDataSet), m_scanDimensions(scanDimensions),
m_imageNumbers(imageNumbers), m_imageNumInBlock(blockSize),
m_totalImageNum(totalImageNum)
{
}
PyBlock PyReader::read()
{
// Acquire GIL before calling Python code
py::gil_scoped_acquire acquire;
// get to the end of the file, return empty Block
if (m_currIndex >= m_totalImageNum) {
return PyBlock();
}
// get the data pointer from the py object
auto getItems = m_pydataset.attr("__getitem__");
uint32_t upperBound = m_currIndex + m_imageNumInBlock;
if (upperBound > m_totalImageNum) {
upperBound = m_totalImageNum;
}
auto sliceIndex = py::slice(m_currIndex, upperBound, 1);
py::array_t<uint16_t> pyarray = getItems(sliceIndex);
PyBlock b(pyarray);
Dimensions2D frameDimensions = { pyarray.shape()[2], pyarray.shape()[1] };
// get the image numbers for current header
std::vector<uint32_t> imageNumberForBlock;
for (auto i = m_currIndex; i < upperBound; i++) {
imageNumberForBlock.push_back(m_imageNumbers[i]);
}
b.header = Header(frameDimensions, imageNumberForBlock.size(), m_scanDimensions,
imageNumberForBlock);
b.header.version = 3;
m_currIndex = upperBound;
return b;
}
PyReader::iterator PyReader::begin()
{
// read data at first time
return iterator(this);
}
PyReader::iterator PyReader::end()
{
return iterator(nullptr);
}
void PyReader::reset()
{
m_currIndex = 0;
}
} // namespace stempy