jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def test_make_incidence_matrix_of_many_things():
    module = cellprofiler.modules.untangleworms.UntangleWorms()
    L1 = numpy.zeros((10, 20), int)
    L2 = numpy.zeros((10, 20), int)
    L1[2, 1:5] = 1
    L1[4, 6:10] = 2
    L1[6, 11:15] = 3
    L1[1:6, 16] = 4
    L1[0, 2:15] = 5
    L2[1, 1] = 1
    L2[3, 5] = 2
    L2[5, 10] = 3
    L2[6, 15] = 4
    L2[1, 15] = 5
    expected = numpy.zeros((5, 5), bool)
    expected[0, 0] = True
    expected[0, 1] = True
    expected[1, 1] = True
    expected[1, 2] = True
    expected[2, 2] = True
    expected[2, 3] = True
    expected[3, 3] = True
    expected[3, 4] = True
    expected[4, 4] = True
    expected[4, 0] = True
    result = module.make_incidence_matrix(L1, 5, L2, 5)
    assert numpy.all(result == expected)
//  [python]
def test_worm_descriptor_building_one():
    module = cellprofiler.modules.untangleworms.UntangleWorms()
    params = make_params(
        dict(radii_from_training=numpy.array([5, 5, 5]), num_control_points=3)
    )
    result, _, _, _, _ = module.worm_descriptor_building(
        [numpy.array([[10, 15], [20, 25]])], params, (40, 50)
    )
    expected = numpy.zeros((40, 50), bool)
    expected[numpy.arange(10, 21), numpy.arange(15, 26)] = True
    ii, jj = numpy.mgrid[-5:6, -5:6]
    expected = scipy.ndimage.binary_dilation(expected, ii * ii + jj * jj <= 25)
    expected = numpy.argwhere(expected)
    eorder = numpy.lexsort((expected[:, 0], expected[:, 1]))
    rorder = numpy.lexsort((result[:, 0], result[:, 1]))
    assert n.all(result[:, 2] == 1)
    assert len(expected) == len(result)
    assert n.all(result[rorder, :2] == expected[eorder, :])
//  [python]
def test_worm_descriptor_building_one():
    module = cellprofiler.modules.untangleworms.UntangleWorms()
    params = make_params(
        dict(radii_from_training=numpy.array([5, 5, 5]), num_control_points=3)
    )
    result, _, _, _, _ = module.worm_descriptor_building(
        [numpy.array([[10, 15], [20, 25]])], params, (40, 50)
    )
    expected = numpy.zeros((40, 50), bool)
    expected[numpy.arange(10, 21), numpy.arange(15, 26)] = True
    ii, jj = numpy.mgrid[-5:6, -5:6]
    expected = scipy.ndimage.binary_dilation(expected, ii * ii + jj * jj <= 25)
    expected = numpy.argwhere(expected)
    eorder = numpy.lexsort((expected[:, 0], expected[:, 1]))
    rorder = numpy.lexsort((result[:, 0], result[:, 1]))
    assert numpy.all(result[:, 2] == 1)
    assert len(expected) == len(result)
    assert numpy.all(result[rorder, :2] == expected[eorder, :])
//  [python]
def test_nonoverlapping_outlines():
    params = zlib.decompress(base64.b64decode(PARAMS))
    workspace, module = make_workspace(A02_image, params)
    assert isinstance(module, cellprofiler.modules.untangleworms.UntangleWorms)
    module.prepare_group(workspace, None, None)
    module.wants_training_set_weights.value = False
    module.override_leftover_weight.value = 6
    module.override_overlap_weight.value = 3
    module.wants_nonoverlapping_outlines.value = True
    module.nonoverlapping_outlines_name.value = NON_OVERLAPPING_OUTLINES_NAME
    module.run(workspace)
    object_set = workspace.object_set
    assert isinstance(object_set, cellprofiler_core.object.ObjectSet)
    worms = object_set.get_objects(NON_OVERLAPPING_OBJECTS_NAME).segmented
    outlines = workspace.image_set.get_image(NON_OVERLAPPING_OUTLINES_NAME).pixel_data
    expected = centrosome.outline.outline(worms) > 0
    assert n.all(outlines == expected)
//  [python]
def test_nonoverlapping_outlines():
    params = zlib.decompress(base64.b64decode(PARAMS))
    workspace, module = make_workspace(A02_image, params)
    assert isinstance(module, cellprofiler.modules.untangleworms.UntangleWorms)
    module.prepare_group(workspace, None, None)
    module.wants_training_set_weights.value = False
    module.override_leftover_weight.value = 6
    module.override_overlap_weight.value = 3
    module.wants_nonoverlapping_outlines.value = True
    module.nonoverlapping_outlines_name.value = NON_OVERLAPPING_OUTLINES_NAME
    module.run(workspace)
    object_set = workspace.object_set
    assert isinstance(object_set, cellprofiler_core.object.ObjectSet)
    worms = object_set.get_objects(NON_OVERLAPPING_OBJECTS_NAME).segmented
    outlines = workspace.image_set.get_image(NON_OVERLAPPING_OUTLINES_NAME).pixel_data
    expected = centrosome.outline.outline(worms) > 0
    assert numpy.all(outlines == expected)
//  [python]
def test_worm_descriptor_building_oob():
    """Test performance if part of the worm is out of bounds"""
    module = cellprofiler.modules.untangleworms.UntangleWorms()
    params = make_params(
        dict(radii_from_training=numpy.array([5, 5, 5]), num_control_points=3)
    )
    result, _, _, _, _ = module.worm_descriptor_building(
        [numpy.array([[1, 15], [11, 25]])], params, (40, 27)
    )
    expected = numpy.zeros((40, 27), bool)
    expected[numpy.arange(1, 12), numpy.arange(15, 26)] = True
    ii, jj = numpy.mgrid[-5:6, -5:6]
    expected = scipy.ndimage.binary_dilation(expected, ii * ii + jj * jj <= 25)
    expected = numpy.argwhere(expected)
    eorder = numpy.lexsort((expected[:, 0], expected[:, 1]))
    rorder = numpy.lexsort((result[:, 0], result[:, 1]))
    assert n.all(result[:, 2] == 1)
    assert len(expected) == len(result)
    assert n.all(result[rorder, :2] == expected[eorder, :])
//  [python]
def test_worm_descriptor_building_oob():
    """Test performance if part of the worm is out of bounds"""
    module = cellprofiler.modules.untangleworms.UntangleWorms()
    params = make_params(
        dict(radii_from_training=numpy.array([5, 5, 5]), num_control_points=3)
    )
    result, _, _, _, _ = module.worm_descriptor_building(
        [numpy.array([[1, 15], [11, 25]])], params, (40, 27)
    )
    expected = numpy.zeros((40, 27), bool)
    expected[numpy.arange(1, 12), numpy.arange(15, 26)] = True
    ii, jj = numpy.mgrid[-5:6, -5:6]
    expected = scipy.ndimage.binary_dilation(expected, ii * ii + jj * jj <= 25)
    expected = numpy.argwhere(expected)
    eorder = numpy.lexsort((expected[:, 0], expected[:, 1]))
    rorder = numpy.lexsort((result[:, 0], result[:, 1]))
    assert numpy.all(result[:, 2] == 1)
    assert len(expected) == len(result)
    assert numpy.all(result[rorder, :2] == expected[eorder, :])
//  [python]
def test_overlapping_outlines():
    params = zlib.decompress(base64.b64decode(PARAMS))
    workspace, module = make_workspace(A02_image, params)
    assert isinstance(module, cellprofiler.modules.untangleworms.UntangleWorms)
    module.prepare_group(workspace, None, None)
    module.wants_training_set_weights.value = False
    module.override_leftover_weight.value = 6
    module.override_overlap_weight.value = 3
    module.wants_overlapping_outlines.value = True
    module.overlapping_outlines_name.value = OVERLAPPING_OUTLINES_NAME
    module.run(workspace)
    object_set = workspace.object_set
    assert isinstance(object_set, cellprofiler_core.object.ObjectSet)
    worms = object_set.get_objects(OVERLAP_OBJECTS_NAME)
    outlines = workspace.image_set.get_image(OVERLAPPING_OUTLINES_NAME).pixel_data
    outlines = numpy.sum(outlines, 2) > 0  # crunch color dimension
    i, j, v = worms.ijv.transpose()
    expected = numpy.zeros(outlines.shape, bool)
    expected[i, j] = True
    # all outlines are in some object...
    assert n.all(expected[outlines])
//  [python]
def test_overlapping_outlines():
    params = zlib.decompress(base64.b64decode(PARAMS))
    workspace, module = make_workspace(A02_image, params)
    assert isinstance(module, cellprofiler.modules.untangleworms.UntangleWorms)
    module.prepare_group(workspace, None, None)
    module.wants_training_set_weights.value = False
    module.override_leftover_weight.value = 6
    module.override_overlap_weight.value = 3
    module.wants_overlapping_outlines.value = True
    module.overlapping_outlines_name.value = OVERLAPPING_OUTLINES_NAME
    module.run(workspace)
    object_set = workspace.object_set
    assert isinstance(object_set, cellprofiler_core.object.ObjectSet)
    worms = object_set.get_objects(OVERLAP_OBJECTS_NAME)
    outlines = workspace.image_set.get_image(OVERLAPPING_OUTLINES_NAME).pixel_data
    outlines = numpy.sum(outlines, 2) > 0  # crunch color dimension
    i, j, v = worms.ijv.transpose()
    expected = numpy.zeros(outlines.shape, bool)
    expected[i, j] = True
    # all outlines are in some object...
    assert numpy.all(expected[outlines])
//  [python]
def test_recalculate_single_worm_control_points():
    i, j = numpy.mgrid[0:10, 0:10]
    l0 = ((i == 3) & (j >= 2) & (j <= 6)).astype(int)
    l0[(i == 7) & (j >= 3) & (j <= 7)] = 3

    l1 = ((j == 3) & (i >= 2) & (i <= 6)).astype(int) * 2
    l1[(j == 7) & (i >= 3) & (i <= 7)] = 4

    expected = numpy.array(
        (
            ((3, 2), (3, 4), (3, 6)),
            ((2, 3), (4, 3), (6, 3)),
            ((7, 3), (7, 5), (7, 7)),
            ((3, 7), (5, 7), (7, 7)),
        )
    )

    (
        result,
        lengths,
    ) = cellprofiler.modules.untangleworms.recalculate_single_worm_control_points(
        [l0, l1], 3
    )
    assert tuple(result.shape) == (4, 3, 2)
    assert len(lengths) == 4
    # Flip any worms that aren't ordered canonically
    for i in range(4):
        if tuple(result[i, -1, :]) < tuple(result[i, 0, :]):
            result[i, :, :] = result[i, ::-1, :]

    assert numpy.all(lengths == 4)
    numpy.testing.assert_array_equal(expected, result)
//  [python]
def _compute_reduced_extra_data(self,
                                    right_match_vars, lefti, righti, rightu):
        """Prepare a table with extra columns that will appear in the merged
        table"""
        domain = self.data.domain
        extra_domain = self.extra_data.domain

        def var_needed(var):
            if rightu is not None and rightu.size:
                return True
            if var in right_match_vars and self.merging != self.OuterJoin:
                return False
            if var not in domain:
                return True
            both_defined = (lefti != -1) * (righti != -1)
            left_col = self.data.get_column(var)[lefti[both_defined]]
            right_col = self.extra_data.get_column(var)[righti[both_defined]]
            if var.is_primitive():
                left_col = left_col.astype(float)
                right_col = right_col.astype(float)
                mask_left = np.isfinite(left_col)
                mask_right = np.isfinite(right_col)
                return not (
                    np.alltrue(mask_left == mask_right)
                    and np.alltrue(left_col[mask_left] == right_col[mask_right]))
            else:
                return not np.alltrue(left_col == right_col)

        extra_vars = [
            var for var in chain(extra_domain.variables, extra_domain.metas)
            if var_needed(var)]
        return self.extra_data[:, extra_vars]
//  [python]
def _compute_reduced_extra_data(self,
                                    right_match_vars, lefti, righti, rightu):
        """Prepare a table with extra columns that will appear in the merged
        table"""
        domain = self.data.domain
        extra_domain = self.extra_data.domain

        def var_needed(var):
            if rightu is not None and rightu.size:
                return True
            if var in right_match_vars and self.merging != self.OuterJoin:
                return False
            if var not in domain:
                return True
            both_defined = (lefti != -1) * (righti != -1)
            left_col = self.data.get_column(var)[lefti[both_defined]]
            right_col = self.extra_data.get_column(var)[righti[both_defined]]
            if var.is_primitive():
                left_col = left_col.astype(float)
                right_col = right_col.astype(float)
                mask_left = np.isfinite(left_col)
                mask_right = np.isfinite(right_col)
                return not (
                    np.all(mask_left == mask_right)
                    and np.all(left_col[mask_left] == right_col[mask_right]))
            else:
                return not np.all(left_col == right_col)

        extra_vars = [
            var for var in chain(extra_domain.variables, extra_domain.metas)
            if var_needed(var)]
        return self.extra_data[:, extra_vars]
//  [python]
def _load_mixed_gambles(zmap_imgs):
    """Ravel zmaps (one per subject) along time axis, resulting, \
    in a n_subjects * n_trials 3D niimgs and, and then make \
    gain vector y of same length."""
    X = []
    y = []
    mask = []
    for zmap_img in zmap_imgs:
        # load subject data
        this_X = get_data(zmap_img)
        affine = zmap_img.affine
        finite_mask = np.all(np.isfinite(this_X), axis=-1)
        this_mask = np.logical_and(np.all(this_X != 0, axis=-1), finite_mask)
        this_y = np.array([np.arange(1, 9)] * 6).ravel()

        # gain levels
        if len(this_y) != this_X.shape[-1]:
            raise RuntimeError(
                f"{zmap_img}: Expecting {len(this_y)} volumes, "
                f"got {this_X.shape[-1]}!"
            )

        # standardize subject data
        this_X -= this_X.mean(axis=-1)[..., np.newaxis]
        std = this_X.std(axis=-1)
        std[std == 0] = 1
        this_X /= std[..., np.newaxis]

        # commit subject data
        X.append(this_X)
        y.extend(this_y)
        mask.append(this_mask)
    y = np.array(y)
    X = np.concatenate(X, axis=-1)
    mask = np.sum(mask, axis=0) > 0.5 * len(mask)
    mask = np.logical_and(mask, np.all(np.isfinite(X), axis=-1))
    X = X[mask, :].T
    tmp = np.zeros(list(mask.shape) + [len(X)])
    tmp[mask, :] = X.T
    mask_img = nibabel.Nifti1Image(mask.astype("uint8"), affine)
    X = nibabel.four_to_three(nibabel.Nifti1Image(tmp, affine))
    return X, y, mask_img
//  [python]
def dist_euclidean(datamtx, strict=True):
    """returns a row by row euclidean dist matrix

    returns the euclidean norm of row1 - row2 for all rows in datamtx
    * comparisons are between rows (samples)
    * input: 2D numpy array.  Limited support for non-2D arrays if
    strict==False
    * output: numpy 2D array float ('d') type.  shape (inputrows, inputrows)
    for sane input data
    * if strict==True, raises ValueError if any of the input data is
    not finite, or if the input data is not a rank 2 array (a matrix).
    * if strict==False, assumes input data is a 2d matrix.
    If rank of input data is < 2, returns an empty 2d array (shape:
    (0, 0) ).  If 0 rows or 0 colunms, also returns an empty 2d array.
    """
    datamtx = asarray(datamtx, "d")
    if strict:
        if not all(isfinite(datamtx)):
            raise ValueError("non finite number in input matrix")
        if rank(datamtx) != 2:
            raise ValueError("input matrix not 2D")
        numrows, numcols = shape(datamtx)
    else:
        try:
            numrows, numcols = shape(datamtx)
        except ValueError:
            return zeros((0, 0), "d")

    if numrows == 0 or numcols == 0:
        return zeros((0, 0), "d")
    dists = zeros((numrows, numrows), "d")
    for r in range(numrows):
        for c in range(r):
            dist = norm(datamtx[r] - datamtx[c])
            if isnan(dist):
                raise RuntimeError("ERROR: overflow when computing euclidean distance")
            dists[r, c] = dists[c, r] = dist

    return dists
//  [python]
def plot_ellipsoid_3D(p, q, ax, n_points = 100):
    """ Plot an ellipsoid in 3D

    Based on https://stackoverflow.com/questions/7819498/plotting-ellipsoid-with-matplotlib

    TODO: Untested!

    Parameters
    ----------
    p: 3x1 array[float]
        Center of the ellipsoid
    q: 3x3 array[float]
        Shape matrix of the ellipsoid
    ax: matplotlib.Axes object
        Ax on which to plot the ellipsoid

    Returns
    -------
    ax: matplotlib.Axes object
        The Ax containing the ellipsoid

    """
    assert np.shape(p) == (3,1), "p needs to be a 3x1 vector"
    assert np.shape(q) == (3,3), "q needs to be a spd 3x3 matrix"
    assert np.allclose(q,0.5*(q+q.T),"q needs to be spd")
    # transform to radius/center parametrization
    U, s, rotation = linalg.svd(q)
    assert np.all(s > 0), "q needs to be positive definite"
    radii = 1.0/np.sqrt(s)

    # get x,y,z of sphere and transform
    u = np.linspace(0.0, 2.0 * np.pi, n_points)
    v = np.linspace(0.0, np.pi, n_points)
    x = radii[0] * np.outer(np.cos(u), np.sin(v))
    y = radii[1] * np.outer(np.sin(u), np.sin(v))
    z = radii[2] * np.outer(np.ones_like(u), np.cos(v))
    for i in range(len(x)):
        for j in range(len(x)):
            [x[i,j],y[i,j],z[i,j]] = np.dot([x[i,j],y[i,j],z[i,j]], rotation) + center

    # plot the result
    ax.plot_wireframe(x, y, z,  rstride=4, cstride=4, color='b', alpha=0.2)
    return ax
//  [python]
def dist_chord(datamtx, strict=True):
    """returns a row-row chord dist matrix

    attributed to Orloci (with accent).  see Legendre 2001,
    ecologically meaningful...

    * comparisons are between rows (samples)
    * input: 2D numpy array.  Limited support for non-2D arrays if
    strict==False
    * output: numpy 2D array float ('d') type.  shape (inputrows, inputrows)
    for sane input data
    * two rows of all zeros returns 0 distance between them
    * an all zero row compared with a not all zero row returns a distance of 1
    * if strict==True, raises ValueError if any of the input data is
    not finite, or if the input data is not a rank 2 array (a matrix).
    * if strict==False, assumes input data is a 2d matrix.
    If rank of input data is < 2, returns an empty 2d array (shape:
    (0, 0) ).  If 0 rows or 0 colunms, also returns an empty 2d array.
    """
    if strict:
        if not n.all(isfinite(datamtx)):
            raise ValueError("non finite number in input matrix")
        if rank(datamtx) != 2:
            raise ValueError("input matrix not 2D")
        numrows, numcols = shape(datamtx)
    else:
        try:
            numrows, numcols = shape(datamtx)
        except ValueError:
            return zeros((0, 0), "d")

    if numrows == 0 or numcols == 0:
        return zeros((0, 0), "d")
    dists = zeros((numrows, numrows), "d")
    for i in range(numrows):
        r1 = datamtx[i]  # cache here
        r1norm = norm(r1)
        for j in range(i):
            r2 = datamtx[j]
            r2norm = norm(r2)
            if r1norm == 0.0 or r2norm == 0.0:
                if r1norm == 0.0 and r2norm == 0.0:
                    dist = 0.0
                else:
                    dist = 1.0
            else:
                dist = norm(r1 / r1norm - r2 / r2norm)
            dists[i, j] = dists[j, i] = dist

    return dists
//  [python]
def dist_chord(datamtx, strict=True):
    """returns a row-row chord dist matrix

    attributed to Orloci (with accent).  see Legendre 2001,
    ecologically meaningful...

    * comparisons are between rows (samples)
    * input: 2D numpy array.  Limited support for non-2D arrays if
    strict==False
    * output: numpy 2D array float ('d') type.  shape (inputrows, inputrows)
    for sane input data
    * two rows of all zeros returns 0 distance between them
    * an all zero row compared with a not all zero row returns a distance of 1
    * if strict==True, raises ValueError if any of the input data is
    not finite, or if the input data is not a rank 2 array (a matrix).
    * if strict==False, assumes input data is a 2d matrix.
    If rank of input data is < 2, returns an empty 2d array (shape:
    (0, 0) ).  If 0 rows or 0 colunms, also returns an empty 2d array.
    """
    if strict:
        if not all(isfinite(datamtx)):
            raise ValueError("non finite number in input matrix")
        if rank(datamtx) != 2:
            raise ValueError("input matrix not 2D")
        numrows, numcols = shape(datamtx)
    else:
        try:
            numrows, numcols = shape(datamtx)
        except ValueError:
            return zeros((0, 0), "d")

    if numrows == 0 or numcols == 0:
        return zeros((0, 0), "d")
    dists = zeros((numrows, numrows), "d")
    for i in range(numrows):
        r1 = datamtx[i]  # cache here
        r1norm = norm(r1)
        for j in range(i):
            r2 = datamtx[j]
            r2norm = norm(r2)
            if r1norm == 0.0 or r2norm == 0.0:
                if r1norm == 0.0 and r2norm == 0.0:
                    dist = 0.0
                else:
                    dist = 1.0
            else:
                dist = norm(r1 / r1norm - r2 / r2norm)
            dists[i, j] = dists[j, i] = dist

    return dists
//  [python]
def dist_gower(datamtx, strict=True):
    """returns a row-row gower dist matrix

    see for example, Faith et al., 1987


    * note that the comparison between any two rows is dependent on the entire
    data matrix, d_ij is a fn of all of datamtx, not just i,j
    * comparisons are between rows (samples)
    * any column containing identical data for all rows is ignored (this
    prevents a 0/0 error in the formula for gower distance
    * input: 2D numpy array.  Limited support for non-2D arrays if
    strict==False
    * output: numpy 2D array float ('d') type.  shape (inputrows, inputrows)
    for sane input data
    * two rows of all zeros returns 0 distance between them
    * if strict==True, raises ValueError if any of the input data is
    not finite, or if the input data is not a rank 2 array (a matrix).
    * if strict==False, assumes input data is a 2d matrix.
    If rank of input data is < 2, returns an empty 2d array (shape:
    (0, 0) ).  If 0 rows or 0 colunms, also returns an empty 2d array.
    """
    if strict:
        if not all(isfinite(datamtx)):
            raise ValueError("non finite number in input matrix")
        if rank(datamtx) != 2:
            raise ValueError("input matrix not 2D")
        numrows, numcols = shape(datamtx)
    else:
        try:
            numrows, numcols = shape(datamtx)
        except ValueError:
            return zeros((0, 0), "d")

    if numrows == 0 or numcols == 0:
        return zeros((0, 0), "d")
    dists = zeros((numrows, numrows), "d")
    coldiffs = datamtx.max(axis=0) - datamtx.min(axis=0)
    for i in range(numcols):
        if coldiffs[i] == 0.0:
            coldiffs[i] = 1.0  # numerator will be zero anyway

    for i in range(numrows):
        r1 = datamtx[i]
        for j in range(i):
            r2 = datamtx[j]
            dist = sum(abs(r1 - r2) / coldiffs)
            dists[i, j] = dists[j, i] = dist

    return dists
//  [python]
def dist_manhattan(datamtx, strict=True):
    """returns manhattan (city block) distance between rows

    dist(a,b) = sum on i( abs(a_i - b_i) )
    negative values ok (but not tested)

    * comparisons are between rows (samples)
    * input: 2D numpy array.  Limited support for non-2D arrays if
    strict==False
    * output: numpy 2D array float ('d') type.  shape (inputrows, inputrows)
    for sane input data
    * if strict==True, raises ValueError if any of the input data is
    not finite, or if the input data is not a rank 2 array (a matrix).
    * if strict==False, assumes input data is a 2d matrix.
    If rank of input data is < 2, returns an empty 2d array (shape:
    (0, 0) ).  If 0 rows or 0 colunms, also returns an empty 2d array.
    """

    def bool_diff(x, y):
        return sum(abs(x ^ y))

    def number_diff(x, y):
        return sum(abs(x - y))

    if strict:
        if not all(isfinite(datamtx)):
            raise ValueError("non finite number in input matrix")
        if rank(datamtx) != 2:
            raise ValueError("input matrix not 2D")
        numrows, numcols = shape(datamtx)
    else:
        try:
            numrows, numcols = shape(datamtx)
        except ValueError:
            return zeros((0, 0), "d")

    if numrows == 0 or numcols == 0:
        return zeros((0, 0), "d")
    dists = zeros((numrows, numrows), "d")

    if datamtx.dtype == "bool":
        diff = bool_diff
    else:
        diff = number_diff

    for i in range(numrows):
        r1 = datamtx[i]  # cache here
        for j in range(i):
            dists[i, j] = dists[j, i] = diff(r1, datamtx[j])

    return dists
//  [python]
def load_table(cls):
        if cls.table is not None:
            return cls.table
        data = np.load(cls.path)
        cls.table = {d: (x, y) for d, x, y in zip(data["dims"], data["x"], data["y"])}
        assert n.all(np.diff(list(cls.table)) >= 1)
        return cls.table
//  [python]
def load_table(cls):
        if cls.table is not None:
            return cls.table
        data = np.load(cls.path)
        cls.table = {d: (x, y) for d, x, y in zip(data["dims"], data["x"], data["y"])}
        assert np.all(np.diff(list(cls.table)) >= 1)
        return cls.table
//  [python]
def test_get_prepadded_size(self, ov_zero_shot_visual_prompting_model) -> None:
        """Test _get_prepadded_size."""
        original_shape = np.array([8, 10], dtype=np.int64)
        image_size = 6

        prepadded_size = ov_zero_shot_visual_prompting_model._get_prepadded_size(original_shape, image_size)

        assert isinstance(prepadded_size, np.ndarray)
        assert prepadded_size.dtype == np.int64
        assert prepadded_size.shape == (2,)
        assert np.all(prepadded_size == np.array([5, 6], dtype=np.int64))
//  [python]
def dist_pearson(datamtx, strict=True):
    """Calculates pearson distance (1-r) between rows


    note that the literature sometimer refers to the pearson dissimilarity
    as (1 - r)/2 (e.g.: BC Blaxall et al. 2003: Differential Myocardial Gene
    Expression in the Development and Rescue of Murine Heart Failure)

    for pearson's r, see for example: Thirteen Ways to Look at the
    Correlation Coefficient by J rodgers, 1988

    * distance varies between 0-2, inclusive.
    * Flat rows (all elements itentical) will return a distance of 1 relative
    to any non-flat row, and a distance of zero to another flat row
    * comparisons are between rows (samples)
    * input: 2D numpy array.  Limited support for non-2D arrays if
    strict==False
    * output: numpy 2D array float ('d') type.  shape (inputrows, inputrows)
    for sane input data
    * two rows of all zeros returns 0 distance between them
    * an all zero row compared with a not all zero row returns a distance of 1
    * if strict==True, raises ValueError if any of the input data is
    not finite, or if the input data is not a rank 2 array (a matrix).
    * if strict==False, assumes input data is a 2d matrix
    If rank of input data is < 2, returns an empty 2d array (shape:
    (0, 0) ).  If 0 rows or 0 colunms, also returns an empty 2d array.
    """
    if strict:
        if not all(isfinite(datamtx)):
            raise ValueError("non finite number in input matrix")
        if rank(datamtx) != 2:
            raise ValueError("input matrix not 2D")
        numrows, numcols = shape(datamtx)
    else:
        try:
            numrows, numcols = shape(datamtx)
        except ValueError:
            return zeros((0, 0), "d")

    if numrows == 0 or numcols == 0:
        return zeros((0, 0), "d")

    rowmeans = mean(datamtx, axis=1)
    dists = zeros((numrows, numrows), "d")

    for i in range(numrows):
        r1 = datamtx[i, :]
        r1m = rowmeans[i]
        r1dev = r1 - r1m
        for j in range(i):
            r2 = datamtx[j, :]
            r2m = rowmeans[j]
            r2dev = r2 - r2m
            top = sum(r1dev * r2dev)
            sum1 = sum(r1dev**2)
            sum2 = sum(r2dev**2)

            if sum1 == 0.0 and sum2 == 0.0:
                r = 1.0
            elif sum1 == 0.0 or sum2 == 0.0:
                r = 0.0
            else:
                bottom = sqrt(sum1 * sum2)
                r = top / bottom

            dists[i][j] = dists[j][i] = 1.0 - r

    return dists
//  [python]
def plot_motion_prediction(motion_preds):
    consistent_instance_seg, matched_centers = predict_instance_segmentation_and_trajectories(motion_preds, compute_matched_centers=True)
    unique_ids = torch.unique(
        consistent_instance_seg[0, 0]).cpu().long().numpy()[1:]
    instance_map = dict(zip(unique_ids, unique_ids))
    instance_colours = generate_instance_colours(instance_map)
    vis_image = plot_instance_map(
        consistent_instance_seg[0, 0].cpu().numpy(), instance_map)

    trajectory_img = np.zeros(vis_image.shape, dtype=np.uint8)
    for instance_id in unique_ids:
        path = matched_centers[instance_id]
        for t in range(len(path) - 1):
            color = instance_colours[instance_id].tolist()
            cv2.line(trajectory_img, tuple(
                path[t]), tuple(path[t + 1]), color, 4)

    # Overlay arrows
    temp_img = cv2.addWeighted(vis_image, 0.7, trajectory_img, 0.3, 1.0)
    mask = ~ np.alltrue(trajectory_img == 0, axis=2)
    vis_image[mask] = temp_img[mask]

    return vis_image
//  [python]
def plot_motion_prediction(motion_preds):
    consistent_instance_seg, matched_centers = predict_instance_segmentation_and_trajectories(motion_preds, compute_matched_centers=True)
    unique_ids = torch.unique(
        consistent_instance_seg[0, 0]).cpu().long().numpy()[1:]
    instance_map = dict(zip(unique_ids, unique_ids))
    instance_colours = generate_instance_colours(instance_map)
    vis_image = plot_instance_map(
        consistent_instance_seg[0, 0].cpu().numpy(), instance_map)

    trajectory_img = np.zeros(vis_image.shape, dtype=np.uint8)
    for instance_id in unique_ids:
        path = matched_centers[instance_id]
        for t in range(len(path) - 1):
            color = instance_colours[instance_id].tolist()
            cv2.line(trajectory_img, tuple(
                path[t]), tuple(path[t + 1]), color, 4)

    # Overlay arrows
    temp_img = cv2.addWeighted(vis_image, 0.7, trajectory_img, 0.3, 1.0)
    mask = ~ np.all(trajectory_img == 0, axis=2)
    vis_image[mask] = temp_img[mask]

    return vis_image
//  [python]
def dist_spearman_approx(datamtx, strict=True):
    """Calculate spearman rank distance (1-r) using an approximation formula

    considers only rank order of elements in a row, averaging ties
    [19.2, 2.1, 0.03, 2.1] -> [3, 1.5, 0, 1.5]
    then performs dist(a,b) = 6 * sum(D^2) / (N*(N^2 - 1))
    where D is difference in rank of element i between row a and row b,
    N is the length of either row

    * formula fails for < 2 columns, returns a zeros matrix
    * comparisons are between rows (samples)
    * input: 2D numpy array.  Limited support for non-2D arrays if
    strict==False
    * output: numpy 2D array float ('d') type.  shape (inputrows, inputrows)
    for sane input data
    * if strict==True, raises ValueError if any of the input data is
    not finite, or if the input data is not a rank 2 array (a matrix), or if
    there are less than 2 colunms
    * if strict==False, assumes input data is a 2d matrix.
    If rank of input data is < 2, returns an empty 2d array (shape:
    (0, 0) ).  If 0 rows or 0 colunms, also returns an empty 2d array.
    """
    if strict:
        if not all(isfinite(datamtx)):
            raise ValueError("non finite number in input matrix")
        if rank(datamtx) != 2:
            raise ValueError("input matrix not 2D")
        numrows, numcols = shape(datamtx)
        if numcols < 2:
            raise ValueError("input matrix has < 2 colunms")
    else:
        try:
            numrows, numcols = shape(datamtx)
        except ValueError:
            return zeros((0, 0), "d")

    if numrows == 0 or numcols == 0:
        return zeros((0, 0), "d")
    dists = zeros((numrows, numrows), "d")

    if numcols < 2:
        return dists  # formula fails for < 2 elements per row

    for i in range(numrows):
        r1 = datamtx[i, :]
        rank1 = _rankdata(r1)
        for j in range(i):
            r2 = datamtx[j, :]
            rank2 = _rankdata(r2)
            rankdiff = rank1 - rank2
            dsqsum = sum((rankdiff) ** 2)
            dist = 6 * dsqsum / float(numcols * (numcols**2 - 1))

            dists[i][j] = dists[j][i] = dist
    return dists
//  [python]
def _sample_parameters(param_distributions, n_iter, random_state=None):
        """Samples ``n_iter`` parameter combinations from
        ``param_distributions`` using ``random_state`` as a seed.

        Non-deterministic iterable over random candidate combinations for
        hyper-parameter search. If all parameters are presented as a list,
        sampling without replacement is performed. If at least one parameter
        is given as a distribution, sampling with replacement is used.
        It is highly recommended to use continuous distributions for continuous
        parameters.

        Note that before SciPy 0.16, the ``scipy.stats.distributions`` do not
        accept a custom RNG instance and always use the singleton RNG from
        ``numpy.random``. Hence setting ``random_state`` will not guarantee a
        deterministic iteration whenever ``scipy.stats`` distributions are used
        to define the parameter search space. Deterministic behavior is however
        guaranteed from SciPy 0.16 onwards.

        Args:
            param_distributions(dict): Dictionary where the keys are
                parameters and values are distributions from which a parameter
                is to be sampled. Distributions either have to provide a
                ``rvs`` function to sample from them, or can be given as a list
                 of values, where a uniform distribution is assumed.
            n_iter(int): Number of parameter settings produced.
                Default is ``10``.
            random_state(int, RandomState instance or None):
                Pseudo random number generator seed used for random uniform
                sampling from lists of possible values instead of scipy.stats
                distributions. If ``None``, the random number generator is the
                random state instance used by np.random.  Default is ``None``.

        Returns:
            combos(list): List of parameter dictionaries with sampled values.
        """

        # check if all distributions are given as lists
        # if so, sample without replacement
        all_lists = n.all(
            [not hasattr(v, "rvs") for v in param_distributions.values()]
        )
        rnd = get_rng(random_state)

        # sort for reproducibility
        items = sorted(param_distributions.items())

        if all_lists:
            # create exhaustive combinations
            param_grid = [
                dict(zip(param_distributions, v))
                for v in product(*param_distributions.values())
            ]
            combos = np.random.choice(param_grid, n_iter, replace=False)

        else:
            combos = []
            for _ in range(n_iter):
                params = dict()
                for k, v in items:
                    if hasattr(v, "rvs"):
                        params[k] = v.rvs(random_state=rnd)
                    else:
                        params[k] = v[rnd.randint(len(v))]
                combos.append(params)

        return combos
//  [python]
def _sample_parameters(param_distributions, n_iter, random_state=None):
        """Samples ``n_iter`` parameter combinations from
        ``param_distributions`` using ``random_state`` as a seed.

        Non-deterministic iterable over random candidate combinations for
        hyper-parameter search. If all parameters are presented as a list,
        sampling without replacement is performed. If at least one parameter
        is given as a distribution, sampling with replacement is used.
        It is highly recommended to use continuous distributions for continuous
        parameters.

        Note that before SciPy 0.16, the ``scipy.stats.distributions`` do not
        accept a custom RNG instance and always use the singleton RNG from
        ``numpy.random``. Hence setting ``random_state`` will not guarantee a
        deterministic iteration whenever ``scipy.stats`` distributions are used
        to define the parameter search space. Deterministic behavior is however
        guaranteed from SciPy 0.16 onwards.

        Args:
            param_distributions(dict): Dictionary where the keys are
                parameters and values are distributions from which a parameter
                is to be sampled. Distributions either have to provide a
                ``rvs`` function to sample from them, or can be given as a list
                 of values, where a uniform distribution is assumed.
            n_iter(int): Number of parameter settings produced.
                Default is ``10``.
            random_state(int, RandomState instance or None):
                Pseudo random number generator seed used for random uniform
                sampling from lists of possible values instead of scipy.stats
                distributions. If ``None``, the random number generator is the
                random state instance used by np.random.  Default is ``None``.

        Returns:
            combos(list): List of parameter dictionaries with sampled values.
        """

        # check if all distributions are given as lists
        # if so, sample without replacement
        all_lists = np.all(
            [not hasattr(v, "rvs") for v in param_distributions.values()]
        )
        rnd = get_rng(random_state)

        # sort for reproducibility
        items = sorted(param_distributions.items())

        if all_lists:
            # create exhaustive combinations
            param_grid = [
                dict(zip(param_distributions, v))
                for v in product(*param_distributions.values())
            ]
            combos = np.random.choice(param_grid, n_iter, replace=False)

        else:
            combos = []
            for _ in range(n_iter):
                params = dict()
                for k, v in items:
                    if hasattr(v, "rvs"):
                        params[k] = v.rvs(random_state=rnd)
                    else:
                        params[k] = v[rnd.randint(len(v))]
                combos.append(params)

        return combos
//  [python]
def __eq__(self, other):
        # Two instances of this class are equal if they have the same
        # underlying Keras metric and the same state.
        assert isinstance(other, KerasMetricValue)
        assert self._keras_metric is other._keras_metric, (
            'Should wrap the same underlying Keras metric')
        return np.all([
            np.array_equal(v1.numpy(), v2.numpy())
            for v1, v2 in zip(self._metric_state, other._metric_state)
        ])
//  [python]
def _find_heights_with_temperature(
        warm_temperatures_kelvins, cold_temperatures_kelvins,
        warm_heights_m_asl, cold_heights_m_asl, target_temperature_kelvins):
    """At each horizontal point, finds height with the target temperature.

    P = number of horizontal points

    :param warm_temperatures_kelvins: length-P numpy array of temperatures on
        warm side.
    :param cold_temperatures_kelvins: length-P numpy array of temperatures on
        cold side.
    :param warm_heights_m_asl: length-P numpy array of heights (metres above sea
        level) corresponding to `warm_temperatures_kelvins`.
    :param cold_heights_m_asl: length-P numpy array of heights (metres above sea
        level) corresponding to `cold_temperatures_kelvins`.
    :param target_temperature_kelvins: Target temperature.
    :return: target_heights_m_asl: length-P numpy array of heights (metres above
        sea level) with the target temperature, estimated by interpolation.
    """

    bad_point_flags = numpy.logical_or(
        numpy.isnan(warm_temperatures_kelvins),
        numpy.isnan(cold_temperatures_kelvins))

    num_points = len(warm_temperatures_kelvins)
    target_heights_m_asl = numpy.full(num_points, numpy.nan)
    if numpy.alltrue(bad_point_flags):
        return target_heights_m_asl

    good_point_indices = numpy.where(numpy.invert(bad_point_flags))[0]

    warm_minus_cold_kelvins = (
        warm_temperatures_kelvins[good_point_indices] -
        cold_temperatures_kelvins[good_point_indices])
    target_minus_cold_kelvins = (
        target_temperature_kelvins -
        cold_temperatures_kelvins[good_point_indices])

    warm_minus_cold_metres = (warm_heights_m_asl[good_point_indices] -
                              cold_heights_m_asl[good_point_indices])
    target_minus_cold_metres = warm_minus_cold_metres * (
        target_minus_cold_kelvins / warm_minus_cold_kelvins)
    target_heights_m_asl[good_point_indices] = (
        cold_heights_m_asl[good_point_indices] + target_minus_cold_metres)

    return target_heights_m_asl
//  [python]
def _find_heights_with_temperature(
        warm_temperatures_kelvins, cold_temperatures_kelvins,
        warm_heights_m_asl, cold_heights_m_asl, target_temperature_kelvins):
    """At each horizontal point, finds height with the target temperature.

    P = number of horizontal points

    :param warm_temperatures_kelvins: length-P numpy array of temperatures on
        warm side.
    :param cold_temperatures_kelvins: length-P numpy array of temperatures on
        cold side.
    :param warm_heights_m_asl: length-P numpy array of heights (metres above sea
        level) corresponding to `warm_temperatures_kelvins`.
    :param cold_heights_m_asl: length-P numpy array of heights (metres above sea
        level) corresponding to `cold_temperatures_kelvins`.
    :param target_temperature_kelvins: Target temperature.
    :return: target_heights_m_asl: length-P numpy array of heights (metres above
        sea level) with the target temperature, estimated by interpolation.
    """

    bad_point_flags = numpy.logical_or(
        numpy.isnan(warm_temperatures_kelvins),
        numpy.isnan(cold_temperatures_kelvins))

    num_points = len(warm_temperatures_kelvins)
    target_heights_m_asl = numpy.full(num_points, numpy.nan)
    if numpy.all(bad_point_flags):
        return target_heights_m_asl

    good_point_indices = numpy.where(numpy.invert(bad_point_flags))[0]

    warm_minus_cold_kelvins = (
        warm_temperatures_kelvins[good_point_indices] -
        cold_temperatures_kelvins[good_point_indices])
    target_minus_cold_kelvins = (
        target_temperature_kelvins -
        cold_temperatures_kelvins[good_point_indices])

    warm_minus_cold_metres = (warm_heights_m_asl[good_point_indices] -
                              cold_heights_m_asl[good_point_indices])
    target_minus_cold_metres = warm_minus_cold_metres * (
        target_minus_cold_kelvins / warm_minus_cold_kelvins)
    target_heights_m_asl[good_point_indices] = (
        cold_heights_m_asl[good_point_indices] + target_minus_cold_metres)

    return target_heights_m_asl
//  [python]
def calculate_pass(
    pass_number: int,
    orbit: Orbit,
    *,
    bbox: geodetic.Box | None = None,
) -> Pass | None:
    """Get the properties of a swath of an half-orbit.

    Args:
        pass_number: Pass number
        orbit: Orbit describing the pass to be calculated.
        bbox: Bounding box of the pass. Defaults to the whole Earth.

    Returns:
        The properties of the pass.
    """
    index = pass_number - 1
    # Selected indexes corresponding to the current pass
    if index == len(orbit.pass_time) - 1:
        indexes = numpy.where(orbit.time >= orbit.pass_time[-1])[0]
    else:
        indexes = numpy.where((orbit.time >= orbit.pass_time[index])
                              & (orbit.time < orbit.pass_time[index + 1]))[0]

    if len(indexes) < 5:
        return None

    lon_nadir = orbit.longitude[indexes]
    lat_nadir = orbit.latitude[indexes]
    time = orbit.time[indexes]
    x_al = orbit.x_al[indexes]

    # Selects the orbit in the defined box
    if bbox is not None:
        mask = bbox.covered_by(lon_nadir, lat_nadir)
        if n.all(~mask):
            return None
        if n.any(mask):
            lon_nadir = lon_nadir[mask]
            lat_nadir = lat_nadir[mask]
            time = time[mask]
            x_al = x_al[mask]

    equator_coordinates = _equator_properties(lon_nadir, lat_nadir, time)

    return Pass(lon_nadir, lat_nadir, time, x_al, equator_coordinates)
//  [python]
def calculate_pass(
    pass_number: int,
    orbit: Orbit,
    *,
    bbox: geodetic.Box | None = None,
) -> Pass | None:
    """Get the properties of a swath of an half-orbit.

    Args:
        pass_number: Pass number
        orbit: Orbit describing the pass to be calculated.
        bbox: Bounding box of the pass. Defaults to the whole Earth.

    Returns:
        The properties of the pass.
    """
    index = pass_number - 1
    # Selected indexes corresponding to the current pass
    if index == len(orbit.pass_time) - 1:
        indexes = numpy.where(orbit.time >= orbit.pass_time[-1])[0]
    else:
        indexes = numpy.where((orbit.time >= orbit.pass_time[index])
                              & (orbit.time < orbit.pass_time[index + 1]))[0]

    if len(indexes) < 5:
        return None

    lon_nadir = orbit.longitude[indexes]
    lat_nadir = orbit.latitude[indexes]
    time = orbit.time[indexes]
    x_al = orbit.x_al[indexes]

    # Selects the orbit in the defined box
    if bbox is not None:
        mask = bbox.covered_by(lon_nadir, lat_nadir)
        if numpy.all(~mask):
            return None
        if numpy.any(mask):
            lon_nadir = lon_nadir[mask]
            lat_nadir = lat_nadir[mask]
            time = time[mask]
            x_al = x_al[mask]

    equator_coordinates = _equator_properties(lon_nadir, lat_nadir, time)

    return Pass(lon_nadir, lat_nadir, time, x_al, equator_coordinates)
//  [python]
def test_random_backends(nx):

    tmp_u = nx.rand()

    assert tmp_u < 1

    tmp_n = nx.randn()

    nx.seed(0)
    M1 = nx.to_numpy(nx.rand(5, 2))
    nx.seed(0)
    M2 = nx.to_numpy(nx.rand(5, 2, type_as=tmp_n))

    assert n.all(M1 >= 0)
    assert n.all(M1 < 1)
    assert M1.shape == (5, 2)
    assert np.allclose(M1, M2)

    nx.seed(0)
    M1 = nx.to_numpy(nx.randn(5, 2))
    nx.seed(0)
    M2 = nx.to_numpy(nx.randn(5, 2, type_as=tmp_u))

    nx.seed(42)
    v1 = nx.randn()
    v2 = nx.randn()
    assert v1 != v2
//  [python]
def test_random_backends(nx):

    tmp_u = nx.rand()

    assert tmp_u < 1

    tmp_n = nx.randn()

    nx.seed(0)
    M1 = nx.to_numpy(nx.rand(5, 2))
    nx.seed(0)
    M2 = nx.to_numpy(nx.rand(5, 2, type_as=tmp_n))

    assert np.all(M1 >= 0)
    assert np.all(M1 < 1)
    assert M1.shape == (5, 2)
    assert np.allclose(M1, M2)

    nx.seed(0)
    M1 = nx.to_numpy(nx.randn(5, 2))
    nx.seed(0)
    M2 = nx.to_numpy(nx.randn(5, 2, type_as=tmp_u))

    nx.seed(42)
    v1 = nx.randn()
    v2 = nx.randn()
    assert v1 != v2
//  [python]
def validate_coerce(self, v):
        if v is None:
            # Pass None through
            pass
        elif self.array_ok and is_array(v):

            try:
                v_array = copy_to_contiguous_readonly_numpy_array(v, force_numeric=True)
            except ValueError as ve:
                self.raise_invalid_val(v)

            v_valid = np.ones(v_array.shape, dtype='bool')
            if self.min_val is not None:
                v_valid = np.logical_and(v_valid, v_array >= self.min_val)

            if self.max_val is not None:
                v_valid = np.logical_and(v_valid, v_array <= self.max_val)

            if not np.alltrue(v_valid):
                # Grab up to the first 10 invalid values
                some_invalid_els = np.array(v, dtype='object')[np.logical_not(v_valid)][:10].tolist()
                self.raise_invalid_elements(some_invalid_els)

            v = v_array  # Always numpy array of float64
        else:
            if not isinstance(v, numbers.Number):
                self.raise_invalid_val(v)

            if (self.min_val is not None and not v >= self.min_val) or \
                    (self.max_val is not None and not v <= self.max_val):

                self.raise_invalid_val(v)

        return v