Example #1
0
def test_get_window():
    word = 'attack'
    window_size = 5

    # Test when we have enough space on both sides for full window.
    text = 'he has to go on the attack if he wants to win the debate'
    window = get_window(text, word, window_size)
    eq_(window, [
        'has', 'to', 'go', 'on', 'the', 'attack', 'if', 'he', 'wants', 'to',
        'win'
    ])

    # Test when there is not enough space on the left.
    text = 'i think romney beat obama in the first debate but obama won the last two'
    word = 'beat'
    window = get_window(text, word, window_size)
    eq_(window, [
        'i', 'think', 'romney', 'beat', 'obama', 'in', 'the', 'first',
        'debate', 'but', 'obama'
    ])

    # Test when there is not enough space on the right.
    text = 'although the i think youre right its important to consider the beating taken by romney'
    window = get_window(text, word, window_size)
    eq_(window, [
        'youre', 'right', 'its', 'important', 'to', 'consider', 'the',
        'beating', 'taken', 'by', 'romney'
    ])

    text = 'he beat his opponent'
    window = get_window(text, word, window_size)
    eq_(window, ['he', 'beat', 'his', 'opponent', '', '', '', '', '', '', ''])
def find_fiber(fiberstamp, position, display_plot=False):
    fiber_data = fiberstamp.get_data()
    xmin, xmax, ymin, ymax = get_window(fiber_data, position, 10)
    debug("...searching for the fiber in the window = [%i:%i,%i:%i]" % (ymin, ymax, xmin, xmax))

    window = fiber_data[ymin:ymax,xmin:xmax]
    origin = np.array((xmin, ymin))

    cursor_point = position - origin

    debug("...contour must contain the point =", cursor_point)

    median = np.median(window)
    maximum = window.max()

    fibers = []
    detector_offset = fiberstamp.get_detector_offset()
    for contour_level in np.linspace(maximum, median):
        contours = measure.find_contours(window, contour_level)

        for contour in contours:
            if not is_closed_contour(contour):
                continue
            
            fiber = Fiber(detector_offset, origin, contour, contour_level)
            if not fiber.bounding_box_contains(cursor_point):
                continue

            fibers.append(fiber)

    def compare_fibers(fiber1, fiber2):
        diff1 = abs(fiber1.get_area() - FIBER_AREA)
        diff2 = abs(fiber2.get_area() - FIBER_AREA)
        return cmp(diff1, diff2)

    fibers.sort(cmp=compare_fibers)

    if display_plot:
        for fiber in fibers:
            plotting.put_plot(plot_fiber_measurement, (window, fiber, FIBER_AREA))
            break

    if fibers:
        return fibers[0]
    
    return None
Example #3
0
def get_selection_peak(input_data, curcrds, pixscale,
                       verbose=False, pixel_buffer=None, cursor_listener=None, radial=False):
    seeing_estimate = 0.8
    default_fwhm = seeing_estimate / pixscale

    if pixel_buffer is None:
        pixel_buffer = max(int(round(default_fwhm)), 1)

    best_selection = None
    prev_shape = (None, None)
    
    for i in range(10):
        debug("...using a pixel_buffer =", pixel_buffer)
        xmin, xmax, ymin, ymax = get_window(input_data, curcrds, pixel_buffer)

        stamp_data = input_data[ymin:ymax, xmin:xmax]
        window = (ymin, ymax, xmin, xmax)

        map(assert_int, window)

        debug("...moffat optimization for the data in the window [%i:%i,%i:%i]" % window)

        ydim, xdim = stamp_data.shape
        assert ydim != 0
        assert xdim != 0

        if prev_shape == stamp_data.shape:
            debug("...already attempted to fit a function to a window this size, terminating search.")
            break
        prev_shape = stamp_data.shape

        # fit the subset of data to a moffat function
        fitted_function = get_fitted_function(stamp_data, default_fwhm)
        
        center_x, center_y = fitted_function.get_center()
        # convert to 1-based indicing and original image
        measured_center = np.array([xmin + center_x + 1, ymin + center_y + 1])
        selection = Selection(measured_center, window, fitted_function)

        if is_debug_mode():
            plotting.put_plot(plot_selection, (selection, pixscale, cursor_listener))

        # note, the first time through the loop best_selection is None, this is ok!
        best_selection = max(best_selection, selection)

        if fitted_function.get_success() <= 3:
            debug("...moffat optimization converged.")
            break

        rsquared = fitted_function.get_rsquared()
        if rsquared >= 0.90:
            debug("...moffat optimization did not converge, but R^2 is greater than 0.9 (%.2f)" % rsquared)
            break

        debug("...moffat optimization did not converge.")
        mindim = min(input_data.shape)
        if mindim > 300:
            if pixel_buffer > int(float(mindim) * 0.1):
                debug("...window is already greater than 20% of the field of view, not going to try again.")
                break
        
        debug("...doubling the window size and trying again...")
        pixel_buffer *= 2

    if verbose and (not is_debug_mode() or radial):
        plotting.put_plot(plot_selection, (best_selection, pixscale, cursor_listener))
        if radial:
            plotting.put_plot(plot_radial, (best_selection, pixscale))

    return best_selection