added function to convert to sparse data
This commit is contained in:
parent
437a6cf2d5
commit
2b8415ca9b
14
creader/SparseFile.py
Normal file
14
creader/SparseFile.py
Normal file
@ -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
|
@ -5,3 +5,5 @@ from .file_utils import open_file
|
||||
from .ClusterFile import ClusterFile
|
||||
from .enums import DetectorType
|
||||
from .RawFile import RawFile
|
||||
|
||||
from .SparseFile import to_sparse
|
Reference in New Issue
Block a user