Skip to content

Commit

Permalink
more efficient xy slicing
Browse files Browse the repository at this point in the history
  • Loading branch information
MaartenBransen committed Sep 8, 2023
1 parent b87cfcd commit 8be62b0
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions scm_confocal/visitech.py
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,8 @@ def __repr__(self):
return "<scm_confocal.visitech_series('{}',{},{},{})>".format(
self.filename,self.zsize,self.zstep,self.backsteps)

def load_data(self,indices=slice(None,None,None),dtype=np.uint16):
def load_data(self,indices=slice(None,None,None),dtype=np.uint16,
xslice=None,yslice=None):
"""
load images from datafile into 3D numpy array
Expand All @@ -833,7 +834,14 @@ def load_data(self,indices=slice(None,None,None),dtype=np.uint16):
if type(indices) == slice:
indices = range(self.nf)[indices]

data = np.array(self.datafile[indices])
if xslice is None and yslice is None:
data = np.array(self.datafile[indices])
elif xslice is None:
data = np.array([self.datafile[i][yslice] for i in indices])
elif yslice is None:
data = np.array([self.datafile[i][:,xslice] for i in indices])
else:
data = np.array([self.datafile[i][yslice,xslice] for i in indices])

if not data.dtype == dtype:
print('rescaling data to type',dtype)
Expand Down Expand Up @@ -957,16 +965,6 @@ def load_stack(self,dim_range={},dtype=np.uint16,remove_backsteps=True,
elif val==slice(None):
dim_range.pop(key)

#warn for inefficient x and y trimming
if 'x-axis' in dim_range or 'y-axis' in dim_range:
print("[WARNING] scm_confocal.visitech_faststack.load_stack: "+
"Loading only part of the data along dimensions 'x-axis' "+
"and/or 'y-axis' not implemented. Data will be loaded fully"+
" into memory before discarding values outside of the "+
"slice range specified for the x-axis and/or y-axis. "+
"Other axes for which a range is specified will still "+
"be treated normally, avoiding unneccesary memory use.")

#remove values outside of dim_range from indices
if 'time' in dim_range:
indices = indices[dim_range['time']]
Expand All @@ -983,18 +981,23 @@ def load_stack(self,dim_range={},dtype=np.uint16,remove_backsteps=True,
# self.get_timestamps(load_stack_indices=True)
self._stack_indices = indices

#add None arguments for x and y range if not slicing those
if not 'y-axis' in dim_range:
dim_range['y-axis'] = None
if not 'x-axis' in dim_range:
dim_range['x-axis'] = None

#load and reshape data
stack = self.load_data(indices=indices.ravel(),dtype=dtype)
stack = self.load_data(
indices=indices.ravel(),
dtype=dtype,
xslice=dim_range['x-axis'],
yslice=dim_range['y-axis'],
)
shape = (indices.shape[0],indices.shape[1],stack.shape[1],
stack.shape[2])
stack = stack.reshape(shape)

#trim x and y axis
if 'y-axis' in dim_range:
stack = stack[:,:,dim_range['y-axis']]
if 'x-axis' in dim_range:
stack = stack[:,:,:,dim_range['x-axis']]

return stack

def yield_stack(self,dim_range={},dtype=np.uint16,remove_backsteps=True,
Expand Down

0 comments on commit 8be62b0

Please sign in to comment.