forked from brettviren/wire-cell-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factor and fix numpy save/load methods into 'helper'
- Loading branch information
1 parent
3426fe4
commit 64f3814
Showing
10 changed files
with
268 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** NumpyHelper | ||
Some helper functions for using with Numpy files. | ||
Eigen default 2D array order is column-major. | ||
Numpy is row-major. | ||
*/ | ||
|
||
#ifndef WIRECELL_UTIL_NUMPYHELPER | ||
#define WIRECELL_UTIL_NUMPYHELPER | ||
|
||
#include "WireCellUtil/cnpy.h" | ||
|
||
#include <Eigen/Core> | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
namespace WireCell::Numpy { | ||
|
||
|
||
template <typename ARRAY> | ||
void save2d(ARRAY& array, std::string aname, std::string fname, std::string mode = "w") { | ||
using ROWM = Eigen::Array<typename ARRAY::Scalar, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>; | ||
using Scalar = typename ARRAY::Scalar; | ||
ROWM rowm = array; // assure we have row major | ||
const Scalar* data = rowm.data(); | ||
std::vector<size_t> shape(2); | ||
shape[0] = static_cast<size_t>(array.rows()); | ||
shape[1] = static_cast<size_t>(array.cols()); | ||
cnpy::npz_save<Scalar>(fname.c_str(), aname, data, | ||
shape, mode); | ||
} | ||
|
||
template <typename ARRAY> | ||
void load2d(ARRAY& array, std::string aname, std::string fname) { | ||
using ROWM = Eigen::Array<typename ARRAY::Scalar, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>; | ||
using Scalar = typename ARRAY::Scalar; | ||
|
||
cnpy::NpyArray np = cnpy::npz_load(fname, aname); | ||
|
||
ROWM temp = Eigen::Map<ROWM>(np.data<Scalar>(), | ||
np.shape[0], np.shape[1]); | ||
array = temp; | ||
} | ||
|
||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
|
||
#include "WireCellUtil/NumpyHelper.h" | ||
#include <iostream> | ||
#include <string> | ||
|
||
using FArray = Eigen::Array<float, Eigen::Dynamic, Eigen::Dynamic>; | ||
using IArray = Eigen::Array<int, Eigen::Dynamic, Eigen::Dynamic>; | ||
|
||
using WireCell::Numpy::load2d; | ||
|
||
int main(int argc, char** argv) | ||
|
||
{ | ||
if (argc < 3) { | ||
std::cerr << "usage: check_numpy_depos depo-file.npz <SetN> [<FirstDepo> [<NDepos>]]\n"; | ||
} | ||
const std::string fname = argv[1]; | ||
const std::string nname = argv[2]; | ||
size_t row_beg=0, nrows = 10; | ||
if (argc > 3) { | ||
row_beg = atoi(argv[3]); | ||
} | ||
if (argc > 4) { | ||
nrows = atoi(argv[4]); | ||
} | ||
|
||
FArray data; | ||
IArray info; | ||
load2d(data, "depo_data_" + nname, fname); | ||
load2d(info, "depo_info_" + nname, fname); | ||
|
||
assert (data.cols() == 7); | ||
assert (info.cols() == 4); | ||
|
||
const size_t ndepos = data.rows(); | ||
assert(ndepos == (size_t)info.rows()); | ||
|
||
const size_t row_end = std::min(row_beg + nrows, ndepos); | ||
|
||
for (size_t irow=row_beg; irow<row_end; ++irow) { | ||
|
||
std::cerr | ||
<< "row=" << irow | ||
<< " t=" << data(irow,0) | ||
<< " id=" << info(irow,0) | ||
<< " gen=" << info(irow,2) | ||
<< " child=" << info(irow,3) | ||
<< std::endl; | ||
} | ||
return 0; | ||
} |
Oops, something went wrong.