diff --git a/creader/SparseFile.py b/creader/SparseFile.py new file mode 100644 index 0000000..4d0f9c8 --- /dev/null +++ b/creader/SparseFile.py @@ -0,0 +1,14 @@ +import numpy as np + +def to_sparse(data, th = 0): + """ + Convert frames stack ndarray[frame, row, col] to sparse array. + Warning: this function drops the frame numbers and only keeps row, col, energy + """ + sparse_dt = [('row', np.int16), ('col', np.int16), ('energy', data.dtype)] + size = (data>th).sum() + sparse = np.zeros(size, sparse_dt) + frames, rows, cols = np.where(data>th) + for i,(f,r,c) in enumerate(zip(frames, rows, cols)): + sparse[i] = (r, c, data[f,r,c]) + return sparse diff --git a/creader/__init__.py b/creader/__init__.py index 47f8a42..609c0d1 100644 --- a/creader/__init__.py +++ b/creader/__init__.py @@ -4,4 +4,6 @@ from _creader import * from .file_utils import open_file from .ClusterFile import ClusterFile from .enums import DetectorType -from .RawFile import RawFile \ No newline at end of file +from .RawFile import RawFile + +from .SparseFile import to_sparse \ No newline at end of file