def run_refine(ctx, dataset, zero, a, b, corr_params, match_params, indices=None): ''' Refine the given lattice for each frame by calculating approximate peak positions and refining them for each frame by using the blobcorrelation and gridmatching.fastmatch(). indices: Indices to refine. This is trimmed down to positions within the frame. As a convenience, for the indices parameter this function accepts both shape (n, 2) and (2, n, m) so that numpy.mgrid[h:k, i:j] works directly to specify indices. This saves boilerplate code when using this function. Default: numpy.mgrid[-10:10, -10:10]. match_params['affine']: If True, use affine transformation matching. This is very fast and robust against a distorted field of view, but doesn't exclude outliers. returns: (result, used_indices) where result is { 'centers': BufferWrapper( kind="nav", extra_shape=(num_disks, 2), dtype="u2" ), 'refineds': BufferWrapper( kind="nav", extra_shape=(num_disks, 2), dtype="float32" ), 'peak_values': BufferWrapper( kind="nav", extra_shape=(num_disks,), dtype="float32" ), 'peak_elevations': BufferWrapper( kind="nav", extra_shape=(num_disks,), dtype="float32" ), 'zero': BufferWrapper( kind="nav", extra_shape=(2,), dtype="float32" ), 'a': BufferWrapper( kind="nav", extra_shape=(2,), dtype="float32" ), 'b': BufferWrapper( kind="nav", extra_shape=(2,), dtype="float32" ), 'selector': BufferWrapper( kind="nav", extra_shape=(num_disks,), dtype="bool" ), } and used_indices are the indices that were within the frame. ''' if indices is None: indices = np.mgrid[-10:10, -10:10] s = indices.shape # Output of mgrid if (len(s) == 3) and (s[0] == 2): indices = np.concatenate(indices.T) # List of (i, j) pairs elif (len(s) == 2) and (s[1] == 2): pass else: raise ValueError( "Shape of indices is %s, expected (n, 2) or (2, n, m)" % str(indices.shape)) (fy, fx) = tuple(dataset.shape.sig) peaks = grm.calc_coords(zero, a, b, indices).astype('int') selector = grm.within_frame(peaks, corr_params['radius'], fy, fx) peaks = peaks[selector] indices = indices[selector] result = ctx.run_udf( dataset=dataset, fn=functools.partial( refine, start_zero=zero, start_a=a, start_b=b, match_params=match_params, indices=indices, ), init=functools.partial(init_pass_2, peaks=peaks, parameters=corr_params), make_buffers=functools.partial( get_result_buffers_refine, num_disks=len(peaks), ), ) return (result, indices)
def test_within_frame(): points = np.array([(0, 0), (1, 1), (9, 19), (19, 9), (8, 19), (8, 18), (9, 18), (18, 8)]) expected = np.array([False, True, False, False, False, True, False, False]) result = grm.within_frame(points, r=1, fy=10, fx=20) assert (np.all(result == expected))
def run_refine(ctx, dataset, zero, a, b, parameters, indices=None, bounds=None): ''' Refine the given lattice for each frame by optimizing the correlation with full rendered frames. Full frame matching inspired by Christoph Mahr, Knut Müller-Caspary and the Bremen group in general indices: Indices to refine. This is trimmed down to positions within the frame. As a convenience, for the indices parameter this function accepts both shape (n, 2) and (2, n, m) so that numpy.mgrid[h:k, i:j] works directly to specify indices. This saves boilerplate code when using this function. Default: numpy.mgrid[-10:10, -10:10]. returns: (result, used_indices) where result is { 'intensity': BufferWrapper( kind="nav", dtype="float32" ), 'zero': BufferWrapper( kind="nav", extra_shape=(2,), dtype="float32" ), 'a': BufferWrapper( kind="nav", extra_shape=(2,), dtype="float32" ), 'b': BufferWrapper( kind="nav", extra_shape=(2,), dtype="float32" ), } and used_indices are the indices that were within the frame. ''' if indices is None: indices = np.mgrid[-10:10, -10:10] s = indices.shape # Output of mgrid if (len(s) == 3) and (s[0] == 2): indices = np.concatenate(indices.T) # List of (i, j) pairs elif (len(s) == 2) and (s[1] == 2): pass else: raise ValueError( "Shape of indices is %s, expected (n, 2) or (2, n, m)" % str(indices.shape)) (fy, fx) = tuple(dataset.shape.sig) peaks = grm.calc_coords(zero, a, b, indices).astype('int') selector = grm.within_frame(peaks, parameters['radius'], fy, fx) indices = indices[selector] result = ctx.run_udf( dataset=dataset, fn=functools.partial(refine, start_zero=zero, start_a=a, start_b=b, indices=indices, parameters=parameters, bounds=bounds), make_buffers=get_result_buffers_refine, ) return (result, indices)