Esempio n. 1
0
def test_two_time_corr():
    setup()
    y = []
    for i in range(50):
        y.append(img_stack[0])
    two_time = two_time_corr(rois,
                             np.asarray(y),
                             50,
                             num_bufs=50,
                             num_levels=1)
    assert np.all(two_time[0])

    # check the number of buffers are even
    with pytest.raises(ValueError):
        two_time_corr(rois, np.asarray(y), 50, num_bufs=25, num_levels=1)
def test_lazy_vs_original():
    setup()
    # run the correlation on the full stack
    full_gen_one = lazy_one_time(
        img_stack, num_levels, num_bufs, rois)
    for gen_state_one in full_gen_one:
        pass

    g2, lag_steps = multi_tau_auto_corr(num_levels, num_bufs,
                                        rois, img_stack)

    assert np.all(g2 == gen_state_one.g2)
    assert np.all(lag_steps == gen_state_one.lag_steps)

    full_gen_two = lazy_two_time(rois, img_stack, stack_size,
                                 num_bufs, num_levels)
    for gen_state_two in full_gen_two:
        pass
    final_gen_result_two = two_time_state_to_results(gen_state_two)

    two_time = two_time_corr(rois, img_stack, stack_size,
                             num_bufs, num_levels)

    assert np.all(two_time[0] == final_gen_result_two.g2)
    assert np.all(two_time[1] == final_gen_result_two.lag_steps)
Esempio n. 3
0
def test_lazy_vs_original():
    setup()
    # run the correlation on the full stack
    full_gen_one = lazy_one_time(
        img_stack, num_levels, num_bufs, rois)
    for gen_state_one in full_gen_one:
        pass

    g2, lag_steps = multi_tau_auto_corr(num_levels, num_bufs,
                                        rois, img_stack)

    assert np.all(g2 == gen_state_one.g2)
    assert np.all(lag_steps == gen_state_one.lag_steps)

    full_gen_two = lazy_two_time(rois, img_stack, stack_size,
                                 num_bufs, num_levels)
    for gen_state_two in full_gen_two:
        pass
    final_gen_result_two = two_time_state_to_results(gen_state_two)

    two_time = two_time_corr(rois, img_stack, stack_size,
                             num_bufs, num_levels)

    assert np.all(two_time[0] == final_gen_result_two.g2)
    assert np.all(two_time[1] == final_gen_result_two.lag_steps)
def test_two_time_corr():
    setup()
    y = []
    for i in range(50):
        y.append(img_stack[0])
    two_time = two_time_corr(rois, np.asarray(y), 50,
                             num_bufs=50, num_levels=1)
    assert np.all(two_time[0])

    # check the number of buffers are even
    assert_raises(ValueError, two_time_corr, rois, np.asarray(y), 50,
                  num_bufs=25, num_levels=1)
Esempio n. 5
0
def test_one_time_from_two_time():
    np.random.seed(333)
    num_lev = 1
    num_buf = 10  # must be even
    x_dim = 10
    y_dim = 10
    stack = 10
    imgs = np.random.randint(1, 3, (stack, x_dim, y_dim))
    imgs[:, 0, 0] += 5
    roi = np.zeros_like(imgs[0])
    # make sure that the ROIs can be any integers greater than 1.
    # They do not have to start at 1 and be continuous
    roi[0:x_dim // 5, 0:y_dim // 10] = 5
    roi[x_dim // 10:x_dim // 5 + 1, y_dim // 10:y_dim // 5] = 3

    g2, lag_steps, _state = two_time_corr(roi, imgs, stack, num_buf, num_lev)
    g2_t, _ = multi_tau_auto_corr(num_lev, num_buf, roi, imgs)

    one_time, error_one_time = one_time_from_two_time(g2, calc_errors=True)
    assert_array_almost_equal(
        one_time,
        np.array([
            [1.02222222, 1.0, 1.0, 1.0, 0.98148148, 1.0, 1.0, 1.0, 1.0, 1.0],
            [
                1.37103962,
                1.3595679,
                1.35260377,
                1.34863946,
                1.36706349,
                1.36235828,
                1.35813492,
                1.37840136,
                1.36607143,
                1.35714286,
            ],
        ]),
    )
    assert_array_almost_equal(
        one_time[0].mean() / one_time[1].mean(),
        g2_t.T[0].mean() / g2_t.T[1].mean(),
        decimal=2,
    )

    assert_array_almost_equal(
        error_one_time[0][1:],
        np.array([
            np.std(np.diagonal(g2[0], offset=x)) / np.sqrt(x)
            for x in range(1, g2[0].shape[0])
        ]),
        decimal=2,
    )
Esempio n. 6
0
def two_time_correlation(
        images: np.ndarray,
        image_item: pg.ImageItem = None,
        rois: Iterable[pg.ROI] = None,
        autoset_num_bufs: bool = True,
        num_bufs: int = 2,
        num_levels: int = 1,
        geometry: AzimuthalIntegrator = None) -> Tuple[np.ndarray, np.ndarray]:
    # TODO -- make composite parameter item widget to allow default (all frames) or enter value
    num_frames = len(images)

    # Auto-select-buffers will override number of levels to be 1,
    # which requires that the number of buffers == number of frames
    # (unless odd; then use number of frames - 1)
    if autoset_num_bufs:
        num_levels = 1
        num_frames = num_frames if num_frames % 2 == 0 else num_frames - 1
        num_bufs = num_frames

    labels = get_label_array(images, rois=rois, image_item=image_item)
    if labels.max() == 0:
        msg.notifyMessage(
            "Please add an ROI over which to calculate one-time correlation.")
        raise ValueError(
            "Please add an ROI over which to calculate one-time correlation.")

    corr = two_time_corr(labels.astype(np.int_), np.asarray(images),
                         num_frames, num_bufs, num_levels)
    g2 = corr.g2
    lag_steps = corr.lag_steps

    # Calculate avg qs from label array for first dimension on returned g2 (so slice selector shows qs for indexing)
    qs = None
    if geometry is not None:
        qs = np.asarray(average_q_from_labels(labels, geometry))
        # qs = np.asarray([f"q={q:.3f}" for q in qs])  # FIXME: why can't we return a python list for the catalog?
        # File "xi-cam/xicam/core/execution/workflow.py", line 886, in project_intents
        #     kwargs[intent_kwarg_name] = getattr(run_catalog, operation_id).to_dask()[output_name]
        # ...
        # File "site-packages/xarray/core/dataarray.py", line 126, in _infer_coords_and_dims
        #     raise ValueError(
        #  ValueError: different number of dimensions on data and dims: 2 vs 1

    # Rotate image plane 90 degrees
    g2 = np.rot90(g2, axes=(-2, -1))

    num_labels = g2.shape[0]  # first dimension represents labels
    if not qs:
        qs = np.array(list(range(1, num_labels + 1)))
    return g2, lag_steps, qs
Esempio n. 7
0
def two_time_correlation(data: np.ndarray,
                         labels: np.ndarray,
                         num_bufs: int = 16,
                         num_levels: int = 8) -> Tuple[np.ndarray, np.ndarray]:
    #TODO -- make composite parameter item widget to allow default (all frames) or enter value
    num_frames = len(data)
    corr = two_time_corr(labels.astype(np.int),
                         np.asarray(data),
                         num_frames,
                         num_bufs,
                         num_levels)
    g2 = corr.g2
    lag_steps = corr.lag_steps
    return g2, lag_steps
Esempio n. 8
0
def test_one_time_from_two_time():
    num_lev = 1
    num_buf = 10  # must be even
    x_dim = 10
    y_dim = 10
    stack = 10
    imgs = np.random.randint(1, 3, (stack, x_dim, y_dim))
    roi = np.zeros_like(imgs[0])
    # make sure that the ROIs can be any integers greater than 1.
    # They do not have to start at 1 and be continuous
    roi[0:x_dim // 10, 0:y_dim // 10] = 5
    roi[x_dim // 10:x_dim // 5, y_dim // 10:y_dim // 5] = 3

    g2, lag_steps, _state = two_time_corr(roi, imgs, stack, num_buf, num_lev)

    one_time = one_time_from_two_time(g2)
    assert_array_almost_equal(
        one_time[0, :],
        np.array([1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1]))
def test_one_time_from_two_time():
    num_lev = 1
    num_buf = 10  # must be even
    x_dim = 10
    y_dim = 10
    stack = 10
    imgs = np.random.randint(1, 3, (stack, x_dim, y_dim))
    roi = np.zeros_like(imgs[0])
    # make sure that the ROIs can be any integers greater than 1.
    # They do not have to start at 1 and be continuous
    roi[0:x_dim//10, 0:y_dim//10] = 5
    roi[x_dim//10:x_dim//5, y_dim//10:y_dim//5] = 3

    g2, lag_steps, _state = two_time_corr(roi, imgs, stack,
                                          num_buf, num_lev)

    one_time = one_time_from_two_time(g2)
    assert_array_almost_equal(one_time[0, :], np.array([1.0, 0.9, 0.8, 0.7,
                                                        0.6, 0.5, 0.4, 0.3,
                                                        0.2, 0.1]))