Commit 4df633ac authored by Martin Reinecke's avatar Martin Reinecke
Browse files

more consistent array checking

parent c27da4fe
......@@ -147,10 +147,28 @@ size_t get_w(double epsilon)
throw runtime_error("requested epsilon too small - minimum is 2e-13");
}
void checkarray(const py::array &arr, const char *aname,
const vector<size_t> &shape)
{
if (size_t(arr.ndim())!=shape.size())
{
cerr << "Array '" << aname << "' has " << arr.ndim() << " dimensions; "
"expected " << shape.size() << endl;
throw runtime_error("bad dimensionality");
}
for (size_t i=0; i<shape.size(); ++i)
if ((shape[i]!=0) && (size_t(arr.shape(i))!=shape[i]))
{
cerr << "Dimension " << i << " of array '" << aname << "' has size "
<< arr.shape(i) << "; expected " << shape[i] << endl;
throw runtime_error("bad array size");
}
}
template<typename T> pyarr_c<T> complex2hartley
(const pyarr_c<complex<T>> &grid_)
{
myassert(grid_.ndim()==2, "grid array must be 2D");
checkarray(grid_, "grid", {0,0});
size_t nu = size_t(grid_.shape(0)), nv = size_t(grid_.shape(1));
auto grid = grid_.data();
......@@ -175,7 +193,7 @@ template<typename T> pyarr_c<T> complex2hartley
template<typename T> pyarr_c<complex<T>> hartley2complex
(const pyarr_c<T> &grid_)
{
myassert(grid_.ndim()==2, "grid array must be 2D");
checkarray(grid_, "grid", {0, 0});
size_t nu = size_t(grid_.shape(0)), nv = size_t(grid_.shape(1));
auto grid = grid_.data();
......@@ -265,9 +283,8 @@ template<typename T> class Baselines
public:
Baselines(const pyarr_c<T> &coord_, const pyarr_c<T> &scaling_)
{
myassert(coord_.ndim()==2, "coord array must be 2D");
myassert(coord_.shape(1)==3, "coord.shape[1] must be 3");
myassert(scaling_.ndim()==1, "scaling array must be 1D");
checkarray(coord_, "coord", {0, 3});
checkarray(scaling_, "scaling", {0});
nrows = coord_.shape(0);
nchan = scaling_.shape(0);
scaling.resize(nchan);
......@@ -294,10 +311,8 @@ template<typename T> class Baselines
template<typename T2> pyarr_c<T2> ms2vis(const pyarr_c<T2> &ms_,
const pyarr_c<uint32_t> &idx_) const
{
myassert(idx_.ndim()==1, "idx array must be 1D");
myassert(ms_.ndim()==2, "ms array must be 2D");
myassert(size_t(ms_.shape(0))==nrows, "baselines 1st dim mismatch");
myassert(size_t(ms_.shape(1))==nchan, "baselines 2nd dim mismatch");
checkarray(idx_, "idx", {0});
checkarray(ms_, "ms", {nrows, nchan});
size_t nvis = size_t(idx_.shape(0));
auto idx = idx_.data();
auto ms = ms_.data();
......@@ -313,10 +328,9 @@ template<typename T> class Baselines
template<typename T2> pyarr_c<T2> vis2ms(const pyarr_c<T2> &vis_,
const pyarr_c<uint32_t> &idx_) const
{
myassert(idx_.ndim()==1, "idx array must be 1D");
myassert(vis_.ndim()==1, "vis array must be 1D");
checkarray(vis_, "vis", {0});
size_t nvis = size_t(vis_.shape(0));
myassert(int(nvis)==idx_.shape(0), "idx/vis size mismatch");
checkarray(idx_, "idx", {nvis});
auto idx = idx_.data();
auto vis = vis_.data();
......@@ -333,13 +347,10 @@ template<typename T> class Baselines
template<typename T2> pyarr_c<T2> add_vis_to_ms(const pyarr_c<T2> &vis_,
const pyarr_c<uint32_t> &idx_, pyarr_c<T2> &ms_) const
{
myassert(idx_.ndim()==1, "idx array must be 1D");
myassert(vis_.ndim()==1, "vis array must be 1D");
myassert(ms_.ndim()==2, "ms array must be 2D");
myassert(size_t(ms_.shape(0))==nrows, "ms: bad 1st dimension");
myassert(size_t(ms_.shape(1))==nchan, "ms: bad 2nd dimension");
checkarray(vis_, "vis", {0});
size_t nvis = size_t(vis_.shape(0));
myassert(int(nvis)==idx_.shape(0), "idx/vis size mismatch");
checkarray(idx_, "idx", {nvis});
checkarray(ms_, "ms", {nrows, nchan});
auto idx = idx_.data();
auto vis = vis_.data();
auto ms = ms_.mutable_data();
......@@ -396,9 +407,7 @@ template<typename T> class GridderConfig
T Beta() const { return beta; }
pyarr_c<T> grid2dirty(const pyarr_c<T> &grid) const
{
myassert(grid.ndim()==2, "grid must be a 2D array");
myassert(size_t(grid.shape(0))==nu, "bad 1st dimension");
myassert(size_t(grid.shape(1))==nv, "bad 2nd dimension");
checkarray(grid, "grid", {nu, nv});
auto tmp = makearray<T>({nu, nv});
auto ptmp = tmp.mutable_data();
hartley2_2D<T>(grid, tmp);
......@@ -417,9 +426,7 @@ template<typename T> class GridderConfig
}
pyarr_c<complex<T>> grid2dirty_c(const pyarr_c<complex<T>> &grid) const
{
myassert(grid.ndim()==2, "grid must be a 2D array");
myassert(size_t(grid.shape(0))==nu, "bad 1st dimension");
myassert(size_t(grid.shape(1))==nv, "bad 2nd dimension");
checkarray(grid, "grid", {nu, nv});
auto tmp = makearray<complex<T>>({nu, nv});
auto ptmp = tmp.mutable_data();
pocketfft::c2c({nu,nv},{grid.strides(0),grid.strides(1)},
......@@ -440,9 +447,7 @@ template<typename T> class GridderConfig
}
pyarr_c<T> dirty2grid(const pyarr_c<T> &dirty) const
{
myassert(dirty.ndim()==2, "dirty must be a 2D array");
myassert(size_t(dirty.shape(0))==nx_dirty, "bad 1st dimension");
myassert(size_t(dirty.shape(1))==ny_dirty, "bad 2nd dimension");
checkarray(dirty, "dirty", {nx_dirty, ny_dirty});
auto pdirty = dirty.data();
auto tmp = makearray<T>({nu, nv});
auto ptmp = tmp.mutable_data();
......@@ -462,9 +467,7 @@ template<typename T> class GridderConfig
}
pyarr_c<complex<T>> dirty2grid_c(const pyarr_c<complex<T>> &dirty) const
{
myassert(dirty.ndim()==2, "dirty must be a 2D array");
myassert(size_t(dirty.shape(0))==nx_dirty, "bad 1st dimension");
myassert(size_t(dirty.shape(1))==ny_dirty, "bad 2nd dimension");
checkarray(dirty, "dirty", {nx_dirty, ny_dirty});
auto pdirty = dirty.data();
auto tmp = makearray<complex<T>>({nu, nv});
auto ptmp = tmp.mutable_data();
......@@ -594,11 +597,10 @@ template<typename T> pyarr_c<complex<T>> vis2grid_c(const Baselines<T> &baseline
const GridderConfig<T> &gconf, const pyarr_c<uint32_t> &idx_,
const pyarr_c<complex<T>> &vis_)
{
myassert(idx_.ndim()==1, "idx array must be 1D");
myassert(vis_.ndim()==1, "vis must be 1D");
checkarray(vis_, "vis", {0});
size_t nvis = size_t(vis_.shape(0));
checkarray(idx_, "idx", {nvis});
auto vis=vis_.data();
myassert(vis_.shape(0)==idx_.shape(0), "bad vis dimension");
size_t nvis = size_t(idx_.shape(0));
auto idx = idx_.data();
size_t nu=gconf.Nu(), nv=gconf.Nv();
......@@ -645,11 +647,9 @@ template<typename T> pyarr_c<complex<T>> grid2vis_c(const Baselines<T> &baseline
const pyarr_c<complex<T>> &grid_)
{
size_t nu=gconf.Nu(), nv=gconf.Nv();
myassert(idx_.ndim()==1, "idx array must be 1D");
checkarray(idx_, "idx", {0});
auto grid = grid_.data();
myassert(grid_.ndim()==2, "data must be 2D");
myassert(grid_.shape(0)==int(nu), "bad grid dimension");
myassert(grid_.shape(1)==int(nv), "bad grid dimension");
checkarray(grid_, "grid", {nu, nv});
size_t nvis = size_t(idx_.shape(0));
auto idx = idx_.data();
......@@ -701,9 +701,7 @@ template<typename T> pyarr_c<uint32_t> getIndices(const Baselines<T> &baselines,
nsafe=gconf.Nsafe();
if (chbegin<0) chbegin=0;
if (chend<0) chend=nchan;
myassert(flags_.ndim()==2, "flags must be 2D");
myassert(size_t(flags_.shape(0))==nrow, "bad flags dimension");
myassert(size_t(flags_.shape(1))==nchan, "bad flags dimension");
checkarray(flags_, "flags", {nrow, nchan});
auto flags = flags_.data();
constexpr int side=1<<logsquare;
size_t nbu = (gconf.Nu()+1+side-1) >> logsquare,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment