Skip to content

Commit

Permalink
Merge pull request #144 from SINTEF/silctrack-check-hdf5-files
Browse files Browse the repository at this point in the history
Tracking update
  • Loading branch information
WillRNaylor authored May 5, 2021
2 parents c7c349e + 4ad741d commit 1d2eb09
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 16 deletions.
2 changes: 1 addition & 1 deletion pysilcam/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.2.14'
__version__ = '0.2.15'
12 changes: 7 additions & 5 deletions pysilcam/config_example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ min_length = 0
model_path = 'C:/model/keras_model.h5'

[Tracking]
min_length = 200
min_speed = 0.000001 # cm/s
cross_correlation_threshold = 0.1
ecd_tolerance = 5 # percent
track_length_limit = 5
min_length = 200 # um, Filter particles with major_axis_length less than this.
min_speed = 0.000001 # cm/s
cross_correlation_threshold = 0.1 # Shape filter of matching particles, low matches more particles.
ecd_tolerance = 5 # percent, Size filter on matching particles, high matches more particles
track_length_limit = 5 # Number of sequential steps required to accept track.
search_box_steps = 5 # Number of iterations searching for matching parciles
search_box_size = 10 # pixels, Size of search box used in each step
17 changes: 11 additions & 6 deletions pysilcam/tracking/silcamtracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def __init__(self):
self.DATAFILE = ''
self.files = None
self.track_length_limit = 15
self.search_box_size = 10 # pixels
self.search_box_steps = 5

def initialise(self):
print('* INITIALISE')
Expand Down Expand Up @@ -104,7 +106,9 @@ def process(self):
X, Y, ecd, length, width, im_plot = get_vect(img1, img2,
PIX_SIZE, MIN_LENGTH, GOOD_FIT,
thresh=self.THRESHOLD,
ecd_tolerance=self.ecd_tolerance)
ecd_tolerance=self.ecd_tolerance,
search_box_size=self.search_box_size,
search_box_steps=self.search_box_steps)
except ValueError:
print(' Error getting vectors')
continue
Expand Down Expand Up @@ -161,7 +165,8 @@ def imc2iml(imc, thresh=0.98):
return iml


def get_vect(img1, img2, PIX_SIZE, MIN_LENGTH, cross_correlation_threshold, thresh=0.98, ecd_tolerance=0):
def get_vect(img1, img2, PIX_SIZE, MIN_LENGTH, cross_correlation_threshold,
thresh=0.98, ecd_tolerance=0, search_box_size=10, search_box_steps=5):
# label image 2
iml2 = imc2iml(img2, thresh)
imbw_out = np.copy(iml2)
Expand Down Expand Up @@ -202,9 +207,9 @@ def get_vect(img1, img2, PIX_SIZE, MIN_LENGTH, cross_correlation_threshold, thre
roi = iml[bbox[0]:bbox[2], bbox[1]:bbox[3]] # roi of image 1
roi = roi > 0

for search_iteration in range(5):
for search_iteration in range(search_box_steps):

search_box = get_search_box(bbox, img2, search_iteration)
search_box = get_search_box(bbox, img2, search_iteration, search_box_size)

# extract the roi in which we expect to find a particle
search_roi = iml2[search_box[0]:search_box[2], search_box[1]:search_box[3]]
Expand Down Expand Up @@ -239,8 +244,8 @@ def get_vect(img1, img2, PIX_SIZE, MIN_LENGTH, cross_correlation_threshold, thre
return X, Y, ecd, length, width, imbw_out


def get_search_box(bbox, img2, search_iteration):
bbexp = 10 * (search_iteration + 1) # expansion by this many pixels in all directions
def get_search_box(bbox, img2, search_iteration, search_box_size=10):
bbexp = search_box_size * (search_iteration + 1) # expansion by this many pixels in all directions
# establish a search box within image 2 by expanding the particle
# bounding box from image 1
r, c = np.shape(img2)
Expand Down
35 changes: 31 additions & 4 deletions pysilcam/tracking/track.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,12 @@ def track_process(configfile, datapath, offset=0):
'''
settings = PySilcamSettings(configfile)

def get_attribute_with_default(obj, attrib, default):
if hasattr(obj, attrib):
return getattr(obj, attrib)
else:
return default

sctr = sctracker.Tracker()
sctr.av_window = settings.Background.num_images
sctr.MIN_LENGTH = settings.Tracking.min_length
Expand All @@ -246,6 +252,8 @@ def track_process(configfile, datapath, offset=0):
sctr.THRESHOLD = settings.Process.threshold
sctr.ecd_tolerance = settings.Tracking.ecd_tolerance
sctr.PIX_SIZE = settings.PostProcess.pix_size
sctr.search_box_size = get_attribute_with_default(settings.Tracking, 'search_box_size', 10)
sctr.search_box_steps = get_attribute_with_default(settings.Tracking, 'search_box_steps', 5)

sctr.path = datapath
dataset_name = os.path.split(datapath)[-1] + '-TRACKS'
Expand Down Expand Up @@ -276,8 +284,17 @@ def make_boxplot(tracksfile, plotfilename):
:param tracksfile:
:return:
'''

def has_tracking_data(f, verbose=True):
with pd.HDFStore(f, 'r') as f_handle:
data_present = '/Tracking/tracks' in f_handle.keys()
if verbose and not data_present:
print(f"File {f} has no data.")
return data_present

h5filedir = os.path.split(tracksfile)[0]
h5file_list = glob.glob(os.path.join(h5filedir, '*-TRACKS.h5'))
h5file_list = [f for f in h5file_list if has_tracking_data(f)]
dataset_names = [os.path.split(k)[-1].replace('-TRACKS.h5', '') for k in h5file_list]

tracks = dict()
Expand Down Expand Up @@ -310,8 +327,8 @@ def silctrack():
Usage:
silcam-track process <configfile> <datapath> [--offset=<offset>]
silcam-track post-process <tracksfile>
silcam-track plotting <tracksfile> <plotfilename> [--gif=<outputdir>] [<rawdatapath>] [--boxplot]
silcam-track post-process <tracksfile> [--config=<configfile>]
silcam-track plotting <tracksfile> <plotfilename> [--gif=<outputdir>] [<rawdatapath>] [--boxplot] [--config=<configfile>]
"""

args = docopt(silctrack.__doc__)
Expand All @@ -332,7 +349,11 @@ def silctrack():

if args['post-process']:
print('* Load and process')
settings = settings_from_h5(args['<tracksfile>'])
if args['--config']:
settings = PySilcamSettings(args['--config'])
else:
print(' Using config from h5 file.')
settings = settings_from_h5(args['<tracksfile>'])

data, tracks = load_and_process(args['<tracksfile>'],
settings.PostProcess.pix_size,
Expand All @@ -346,6 +367,12 @@ def silctrack():
unfiltered_tracks.to_hdf(fh, 'Tracking/unfiltered_tracks', mode='r+')

if args['plotting']:
if args['--config']:
settings = PySilcamSettings(args['--config'])
else:
print(' Using config from h5 file.')
settings = settings_from_h5(args['<tracksfile>'])

settings = settings_from_h5(args['<tracksfile>'])

if args['--gif']:
Expand All @@ -365,7 +392,7 @@ def silctrack():

make_output_files_for_giffing(unfiltered_tracks, rawdatapath, outputdir,
settings.PostProcess.pix_size,
track_length_limit=5)
track_length_limit=settings.Tracking.track_length_limit)
print('* output files finished.')
print('use ''convert -delay 12 -loop 0 *.png output.gif'' to make a gif')

Expand Down

0 comments on commit 1d2eb09

Please sign in to comment.