-
Notifications
You must be signed in to change notification settings - Fork 0
/
NumpyLoader.cpp
97 lines (81 loc) · 2.66 KB
/
NumpyLoader.cpp
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <omega/PythonInterpreterWrapper.h>
#include "NumpyLoader.h"
#include "signac.h"
///////////////////////////////////////////////////////////////////////////////
NumpyLoader::NumpyLoader()
{
// Initialize the Numpy C API
import_array();
}
///////////////////////////////////////////////////////////////////////////////
NumpyLoader::~NumpyLoader()
{
typedef KeyValue<String, PyArrayObject*> Item;
foreach(Item i, myObjects)
{
Py_DECREF(i.getValue());
}
}
///////////////////////////////////////////////////////////////////////////////
size_t NumpyLoader::getNumRecords(Dataset* d)
{
return myNumRecords;
}
///////////////////////////////////////////////////////////////////////////////
void NumpyLoader::open(const String& source)
{
}
///////////////////////////////////////////////////////////////////////////////
void NumpyLoader::addDimension(const String& name, PyObject* dataobject)
{
if(PyArray_Check(dataobject))
{
PyArrayObject* ao = (PyArrayObject*)dataobject;
Py_INCREF(ao);
myObjects[name] = ao;
npy_intp* shape = PyArray_SHAPE(ao);
myNumRecords = shape[0];
ofmsg("[NumpyLoader::addDimension] numRecords=<%1%>", %myNumRecords);
}
else
{
ofwarn("[NumpyLoader::addDimension] data object is not an array for dimension <%1%>", %name);
}
}
///////////////////////////////////////////////////////////////////////////////
void NumpyLoader::load(Field* f)
{
Dimension* dim = f->getDimension();
if(myObjects.find(dim->id) != myObjects.end())
{
int j = dim->index;
PyArrayObject* ao = myObjects[dim->id];
int nd = PyArray_NDIM(ao);
npy_intp* shape = PyArray_SHAPE(ao);
PyArray_Descr* at = PyArray_DTYPE(ao);
oassert(at.kind == 'f');
size_t sstart = f->domain.start;
size_t slen = f->domain.length;
int sstride = f->domain.decimation;
f->lock.lock();
// Allocate field data
float* fielddata = (float*)malloc((slen / sstride) * sizeof(float));
size_t c = 0;
for(size_t i = sstart; i < sstart + slen; i += sstride)
{
double* v = (double*)PyArray_GETPTR2(ao, i, j);
fielddata[c++] = (float)*v;
}
// Update field length
f->data = (char*)fielddata;
f->loaded = true;
f->stamp = otimestamp();
f->lock.unlock();
ofmsg("Loaded field %1% %2% %3%", %dim->id %sstart %slen);
Signac::instance->signalFieldLoaded(f);
}
else
{
ofwarn("[NumpyLoader::load] could not find dimension <%1%>", %dim->id);
}
}