added function to convert to sparse data

This commit is contained in:
Erik Frojdh 2023-12-07 08:59:19 +01:00
parent 437a6cf2d5
commit 2b8415ca9b
2 changed files with 17 additions and 1 deletions

14
creader/SparseFile.py Normal file
View 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

View File

@ -5,3 +5,5 @@ from .file_utils import open_file
from .ClusterFile import ClusterFile from .ClusterFile import ClusterFile
from .enums import DetectorType from .enums import DetectorType
from .RawFile import RawFile from .RawFile import RawFile
from .SparseFile import to_sparse