Commit d0d105e8 authored by christoph's avatar christoph
Browse files

small modifications, renaming of ROI helper functions

parent 64af4abb
......@@ -712,7 +712,7 @@ class roi_finder:
* interpolation (str) : Interpolation scheme used in the plot.
* colormap (str) : Colormap used in the plot.
"""
self.roi_obj.show_rois( cmap=cmap,interpolation=interpolation )
self.roi_obj.show( cmap=cmap,interpolation=interpolation )
def import_simo_style_rois( self, roiList, detImageShape=(512,768) ):
""" **import_simo_style_rois**
......
......@@ -44,6 +44,7 @@ import copy
import h5py
import os
import matplotlib.pyplot as plt
from collections import Iterable
# commented the *import because otherwise sphinx documents all the symbol of other packages
# from xrs_utilities import *
......@@ -168,32 +169,82 @@ class roi_object:
"""
Container class to hold all relevant information about given ROIs.
"""
def __init__(self):
self.roi_matrix = np.array([]) # single matrix of zeros, ones, twos, ... , n's (where n is the number of ROIs defined)
self.red_rois = {} # dictionary, one entry for each ROI, each ROI has an origin and a rectangular box of ones and zeros defining the ROI
self.indices = [] # list of list of tuples (one list of tuples for each ROI)
self.number_of_rois = 0 # number of ROIs defined
self.kind = [] # keyword (e.g. 'zoom', 'line', 'auto', etc.), certain features (esp. in imaging) are only available for certain kinds of ROIs
def __init__( self ):
self.roi_matrix = np.array([]) # single matrix of zeros, ones, twos, ... ,
# n's (where n is the number of ROIs defined)
self.red_rois = {} # dictionary, one entry for each ROI, each ROI
# has an origin and a rectangular box of ones
# and zeros defining the ROI
self.indices = [] # list of list of tuples (one list of tuples
# for each ROI)
self.number_of_rois = 0 # number of ROIs defined
self.kind = [] # keyword (e.g. 'zoom', 'line', 'auto', etc.),
# certain features (esp. in imaging) are only
# available for certain kinds of ROIs
self.x_indices = [] # list of numpy arrays of x-indices (for each ROI)
self.y_indices = [] # list of numpy arrays of y-indices (for each ROI)
self.masks = [] # 3D numpy array with slices of zeros and ones (same size as detector image) for each roi
self.masks = [] # 3D numpy array with slices of zeros and ones (same
# size as detector image) for each roi
self.input_image = [] # 2D imput image that was used to define the ROIs
def __add__( self, roi_obj ):
""" **__add__**
Allows appending two ROI objects by using the + operator.
"""
assert ( type(roi_obj) == type(self) )
# create a new instance
new_obj = roi_object()
# copy the ROIs
new_obj.red_rois = copy.deepcopy( self.red_rois )
# append the other ROIs
self_len = len( new_obj.red_rois )
for ii,key in enumerate( sorted( roi_obj.red_rois ) ):
new_key = 'ROI%02d'%( ii+self_len )
if not new_key in list( new_obj.red_rois.keys() ):
new_obj.red_rois[new_key] = roi_obj.red_rois[key]
new_obj.red_rois[new_key][1][ new_obj.red_rois[new_key][1]>0 ] += self_len
else:
'something fishy happened, skipping %s.'%(key)
return self
# add the input images
new_obj.input_image = self.input_image + roi_obj.input_image
# convert summed ROIs to other ROI formats
new_obj.roi_matrix = convert_redmatrix_to_matrix( new_obj.red_rois,
np.zeros_like( new_obj.input_image ), offsetX=0,
offsetY=0 )
new_obj.masks = convert_roi_matrix_to_masks( new_obj.roi_matrix)
new_obj.indices = convert_matrix_rois_to_inds( new_obj.roi_matrix)
new_obj.number_of_rois = int( np.amax( new_obj.roi_matrix ) )
new_obj.x_indices = convert_inds_to_xinds( new_obj.indices )
new_obj.y_indices = convert_inds_to_yinds( new_obj.indices )
def load_rois_fromMasksDict(self, masksDict, newshape=None, kind="zoom"):
return new_obj
def load_rois_fromMasksDict( self, masksDict, newshape=None, kind="zoom" ):
""" **load_rois_fromMasksDict**
"""
self.kind=kind
self.red_rois = masksDict
if newshape is not None:
self.roi_matrix = np.zeros(newshape)
self.roi_matrix = convert_redmatrix_to_matrix( masksDict,self.roi_matrix , offsetX=0, offsetY=0)
self.roi_matrix = convert_redmatrix_to_matrix( masksDict, self.roi_matrix,
offsetX=0, offsetY=0)
self.masks = convert_roi_matrix_to_masks(self.roi_matrix)
self.indices = convert_matrix_rois_to_inds(self.roi_matrix)
self.number_of_rois = int(np.amax(self.roi_matrix))
self.x_indices = convert_inds_to_xinds(self.indices)
self.y_indices = convert_inds_to_yinds(self.indices)
def writeH5(self,fname):
def writeH5( self, fname ):
""" **writeH5**
Creates an HDF5 file and writes the ROIs into it.
Args:
......@@ -215,6 +266,7 @@ class roi_object:
def loadH5(self,fname):
""" **loadH5**
Loads ROIs from an HDF5 file written by the self.writeH5() method.
Args:
......@@ -237,42 +289,65 @@ class roi_object:
self.load_rois_fromMasksDict(self.red_rois , newshape = shape, kind="zoom")
else:
self.roi_matrix = convert_redmatrix_to_matrix( self.red_rois, np.zeros_like(self.input_image), offsetX=0, offsetY=0)
self.roi_matrix = convert_redmatrix_to_matrix( self.red_rois,
np.zeros_like(self.input_image), offsetX=0, offsetY=0)
self.indices = convert_matrix_rois_to_inds(self.roi_matrix)
self.number_of_rois = int(np.amax(self.roi_matrix))
self.x_indices = convert_inds_to_xinds(self.indices)
self.y_indices = convert_inds_to_yinds(self.indices)
self.masks = convert_roi_matrix_to_masks(self.roi_matrix)
def load_shadok_h5(self, fname, group_name1, group_name2='ROI_AS_SELECTED' ):
def load_shadok_h5( self, fname, group_name1, group_name2='ROI_AS_SELECTED' ):
""" **load_shadok_h5**
Load ROIs from a HDF5-file created by the Shadok/XRS_Swissknife.
"""
f = h5py.File(fname, "r")
self.input_image = f[group_name1][group_name2]["rois_definition"]["image"][:]
self.red_rois = {}
load_rois_fromh5(f[group_name1][group_name2],self.red_rois)
self.roi_matrix = convert_redmatrix_to_matrix( self.red_rois, np.zeros_like(self.input_image), offsetX=0, offsetY=0)
self.roi_matrix = convert_redmatrix_to_matrix( self.red_rois,
np.zeros_like(self.input_image), offsetX=0, offsetY=0)
self.indices = convert_matrix_rois_to_inds(self.roi_matrix)
self.number_of_rois = int(np.amax(self.roi_matrix))
self.x_indices = convert_inds_to_xinds(self.indices)
self.y_indices = convert_inds_to_yinds(self.indices)
self.masks = convert_roi_matrix_to_masks(self.roi_matrix)
def append(self,roi_object):
orig_length = len(self.red_rois)
self.indices.extend(roi_object.indices) # list of list of tuples (one list of tuples for each ROI)
self.number_of_rois =+ roi_object.number_of_rois # number of ROIs defined
self.x_indices.extend(roi_object.x_indices) # list of numpy arrays of x-indices (for each ROI)
self.y_indices.extend(roi_object.y_indices) # list of numpy arrays of y-indices (for each ROI)
#self.masks = [] # 3D numpy array with slices of zeros and ones (same size as detector image) for each roi
#self.input_image += [] # 2D imput image that was used to define the ROIs
roi_object.roi_matrix[roi_object.roi_matrix>0] += orig_length
self.roi_matrix += roi_object.roi_matrix # single matrix of zeros, ones, twos, ... , n's (where n is the number of ROIs defined)
def append( self, roi_object ):
""" **append**
Append other ROI definitions.
Args:
* roi_object (roi_obj) : Instance of the roi_object class.
"""
assert ( type(roi_object) == type(self) )
orig_length = len( self.red_rois )
for ii,key in enumerate(sorted(roi_object.red_rois)):
new_key = 'ROI%02d'%(ii+orig_length)
self.red_rois[new_key] = roi_object.red_rois[key]
self.red_rois[new_key][1][ self.red_rois[new_key][1]>0 ] += orig_length
def get_number_of_rois(self):
# convert summed ROIs to other ROI formats
self.roi_matrix = convert_redmatrix_to_matrix( self.red_rois,
np.zeros_like( self.input_image ), offsetX=0,
offsetY=0 )
self.masks = convert_roi_matrix_to_masks( self.roi_matrix)
self.indices = convert_matrix_rois_to_inds( self.roi_matrix)
self.number_of_rois = int( np.amax( self.roi_matrix ) )
self.x_indices = convert_inds_to_xinds( self.indices )
self.y_indices = convert_inds_to_yinds( self.indices )
def get_number_of_rois( self ):
""" **get_number_of_rois**
Returns the number of currently defined ROIs.
"""
return self.number_of_rois
def get_indices(self):
......@@ -297,63 +372,113 @@ class roi_object:
"""
return copy.deepcopy(self)
def strip_rois(self):
""" **strip_rois**
Strips extra zeros out of ROIs.
def strip( self ):
""" **strip**
Strips extra zeros from border of the ROIs.
"""
pass
for key in self.red_rois:
num = int("".join([c for c in key if c.isdigit()]))
origin = self.red_rois[key][0]
data = self.red_rois[key][1]
inds1, inds2 = np.where(data>0)
new_data = np.zeros((inds1.max()-inds1.min()+1, inds2.max()-inds2.min()+1))
new_data = data[inds1, inds2].reshape(new_data)
new_origin = (origin[0]+inds1.min(), origin[1]+inds2.min())
self.red_rois[key][0] = new_origin
self.red_rois[key][1] = new_data
def delete_empty_rois(self):
def delete_empty_rois( self ):
""" **delete_empty_rois**
Deletes ROI entries that are completely empty.
"""
pass
for key in self.red_rois:
if not np.any(self.red_rois[key][1]) > 0:
self.pop(key)
def shift_rois(self,shiftVal,direction='horiz',whichroi=None):
"""
**shift_rois**
def shift( self, shiftVal, direction='horiz', roi_inds=None ):
""" **shift**
Displaces the defined ROIs by the provided value.
Args
* shiftVal : int
Value by which the ROIs should be shifted.
* direction : string
Description of which direction to shift by (can be 'horiz' or 'vert').
* whichroi : sequence
Sequence (iterable) for which ROIs should be shifted.
* shiftVal (int) : Value by which the ROIs should be shifted.
* direction (str) : Description of which direction to shift
by (can be 'horiz' or 'vert'), default
is 'horiz'.
* roi_inds (int) or (sequence) : Index or Sequence (iterable)
for which ROIs should be shifted.
If None, all ROIs defined are
shifted (default.)
"""
the_indices = []
if not whichroi:
inds = list(range(len(self.indices)))
if not roi_inds:
inds = list(range(len(self.red_rois)))
else:
inds = whichroi
if direction == 'vert':
for roi in self.indices:
oneroi = []
for pixel in roi:
oneroi.append( (pixel[0]+shiftVal,pixel[1]) )
the_indices.append(oneroi)
if direction == 'horiz':
for roi in self.indices:
oneroi = []
for pixel in roi:
oneroi.append( (pixel[0], pixel[1]+shiftVal) )
the_indices.append(oneroi)
self.indices = the_indices
self.roi_matrix = convert_inds_to_matrix(self.indices,self.input_image.shape)
self.red_rois = convert_matrix_to_redmatrix(self.roi_matrix)
self.x_indices = convert_inds_to_xinds(self.indices)
self.y_indices = convert_inds_to_yinds(self.indices)
self.masks = convert_roi_matrix_to_masks(self.roi_matrix)
inds = roi_inds
if not isinstance( inds, Iterable ):
inds = list([inds])
for ind in inds:
key = 'ROI%02d'%ind
if direction == 'horiz':
self.red_rois[key][0][1] += shiftVal
elif direction == 'vert':
self.red_rois[key][0][0] += shiftVal
# convert summed ROIs to other ROI formats
self.roi_matrix = convert_redmatrix_to_matrix( self.red_rois,
np.zeros_like( self.input_image ), offsetX=0,
offsetY=0 )
self.masks = convert_roi_matrix_to_masks( self.roi_matrix)
self.indices = convert_matrix_rois_to_inds( self.roi_matrix)
self.number_of_rois = int( np.amax( self.roi_matrix ) )
self.x_indices = convert_inds_to_xinds( self.indices )
self.y_indices = convert_inds_to_yinds( self.indices )
def pop( self, roi_key=None ):
""" **pop**
Discards a ROI.
def show_rois(self, cmap='Blues', interpolation='nearest', logscaling=True):
""" **show_rois**
Args
* roi_key (str) : Dict key for ROI to delete. If None, the ROI with
highest index (defined last) will be discarded (defalt).
"""
# delete last ROI if no key is specified
if not roi_key:
roi_key = sorted(list( self.red_rois.keys()))[-1]
# make sure the ROI exists
assert(roi_key in list(self.red_rois.keys()) )
# delete the ROI from the maskDict
self.red_rois.pop( roi_key )
# convert summed ROIs to other ROI formats
self.roi_matrix = convert_redmatrix_to_matrix( self.red_rois,
np.zeros_like( self.input_image ), offsetX=0,
offsetY=0 )
self.masks = convert_roi_matrix_to_masks( self.roi_matrix)
self.indices = convert_matrix_rois_to_inds( self.roi_matrix)
self.number_of_rois = int( np.amax( self.roi_matrix ) )
self.x_indices = convert_inds_to_xinds( self.indices )
self.y_indices = convert_inds_to_yinds( self.indices )
def show(self, cmap='Blues', interpolation='nearest', logscaling=True):
""" **show**
Creates a figure showing the existing ROIs.
Args:
......@@ -411,9 +536,9 @@ class roi_object:
plt.text( xcenter, ycenter, string )
plt.show()
def convert_redmatrix_to_matrix( masksDict,mask, offsetX=0, offsetY=0):
def convert_redmatrix_to_matrix( masksDict, mask, offsetX=0, offsetY=0 ):
for key, (pos,M) in six.iteritems(masksDict):
num=int("".join([c for c in key if c.isdigit()]))
S = M.shape
......@@ -627,7 +752,7 @@ def load_rois_fromh5_address(address):
return myroi
def load_rois_fromh5(h5group_tot,md, retrieveImage=False, metadata = None):
def load_rois_fromh5(h5group_tot,md, retrieveImage=False, metadata = None):
h5group = h5group_tot["rois_definition/rois_dict"]
for key in h5group.keys():
md[key]=[]
......@@ -649,7 +774,7 @@ def load_rois_fromh5(h5group_tot,md, retrieveImage=False, metadata = None):
else:
return shape
def write_rois_toh5(h5group,md, filterMask=None, metadata=None):
def write_rois_toh5(h5group,md, filterMask=None, metadata=None):
for key in md.keys():
if key in h5group:
del h5group[key]
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment