jiahuimbzuai/CoQuIR huggingface dataset:
//  [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.all(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
//  [python]
def _strictly_monotonic(self, coord):

        """Whether the coordinate is strictly monotonic"""
        # Authors
        # -------
        # Greg Streletz, Xylar Asay-Davis

        coord_diff = numpy.diff(coord.values)
        coord_diff = numpy.where(coord_diff > 180, coord_diff - 360, coord_diff)
        coord_diff = numpy.where(coord_diff < -180, coord_diff + 360,
                                 coord_diff)
        return n.all(coord_diff > 0) or n.all(coord_diff < 0)
//  [python]
def _strictly_monotonic(self, coord):

        """Whether the coordinate is strictly monotonic"""
        # Authors
        # -------
        # Greg Streletz, Xylar Asay-Davis

        coord_diff = numpy.diff(coord.values)
        coord_diff = numpy.where(coord_diff > 180, coord_diff - 360, coord_diff)
        coord_diff = numpy.where(coord_diff < -180, coord_diff + 360,
                                 coord_diff)
        return numpy.all(coord_diff > 0) or numpy.all(coord_diff < 0)
//  [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, dtype='int32')

            except (ValueError, TypeError, OverflowError) 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):
                invalid_els = np.array(v, dtype='object')[np.logical_not(v_valid)][:10].tolist()
                self.raise_invalid_elements(invalid_els)

            v = v_array
        else:
            try:
                if not isinstance(v, numbers.Number):
                    # don't let int() cast strings to ints
                    self.raise_invalid_val(v)

                v_int = int(v)
            except (ValueError, TypeError, OverflowError) as ve:
                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)

            v = v_int

        return v
//  [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, dtype='int32')

            except (ValueError, TypeError, OverflowError) 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.all(v_valid):
                invalid_els = np.array(v, dtype='object')[np.logical_not(v_valid)][:10].tolist()
                self.raise_invalid_elements(invalid_els)

            v = v_array
        else:
            try:
                if not isinstance(v, numbers.Number):
                    # don't let int() cast strings to ints
                    self.raise_invalid_val(v)

                v_int = int(v)
            except (ValueError, TypeError, OverflowError) as ve:
                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)

            v = v_int

        return v
//  [python]
def test_wrong_size_color_to_color():
    numpy.random.seed(25)
    image = numpy.random.uniform(size=(50, 50, 3)).astype(numpy.float32)
    image[0, 0] = 1
    outline = numpy.zeros((60, 40), bool)
    outline[20:31, 20:31] = 1
    outline[21:30, 21:30] = 0
    expected = image.copy()
    sub_expected = expected[:50, :40]
    sub_expected[:, :, 0][outline[:50, :40].astype(bool)] = 1
    sub_expected[:, :, 1][outline[:50, :40].astype(bool)] = 0
    sub_expected[:, :, 2][outline[:50, :40].astype(bool)] = 0
    workspace, module = make_workspace(image, labels=[outline.astype(int)])
    module.wants_color.value = cellprofiler.modules.overlayoutlines.WANTS_COLOR
    module.outlines[0].color.value = "Red"
    module.line_mode.value = "Inner"
    module.run(workspace)
    output_image = workspace.image_set.get_image(OUTPUT_IMAGE_NAME)
    assert n.all(output_image.pixel_data == expected)
//  [python]
def test_wrong_size_color_to_color():
    numpy.random.seed(25)
    image = numpy.random.uniform(size=(50, 50, 3)).astype(numpy.float32)
    image[0, 0] = 1
    outline = numpy.zeros((60, 40), bool)
    outline[20:31, 20:31] = 1
    outline[21:30, 21:30] = 0
    expected = image.copy()
    sub_expected = expected[:50, :40]
    sub_expected[:, :, 0][outline[:50, :40].astype(bool)] = 1
    sub_expected[:, :, 1][outline[:50, :40].astype(bool)] = 0
    sub_expected[:, :, 2][outline[:50, :40].astype(bool)] = 0
    workspace, module = make_workspace(image, labels=[outline.astype(int)])
    module.wants_color.value = cellprofiler.modules.overlayoutlines.WANTS_COLOR
    module.outlines[0].color.value = "Red"
    module.line_mode.value = "Inner"
    module.run(workspace)
    output_image = workspace.image_set.get_image(OUTPUT_IMAGE_NAME)
    assert numpy.all(output_image.pixel_data == expected)
//  [python]
def test_mixed_map_0_dim():
    X = toy_setup.CategoricalVariable(categories=2)
    f = mediation.MixedMap([ConstantMap(1), ConstantMap(2)], [X], dtype=np.dtype("float32"))
    r = f({X: np.array([0, 0, 0, 1, 1, 0, 1])})
    assert np.all(r == np.array([1, 1, 1, 2, 2, 1, 2]))
//  [python]
def convert_image_to_objects(image):
    """Interpret an image as object indices

    image - a grayscale or color image, assumes zero == background

    returns - a similarly shaped integer array with zero representing background
              and other values representing the indices of the associated object.
    """
    assert isinstance(image, numpy.ndarray)
    if image.ndim == 2:
        unique_indices = numpy.unique(image.ravel())
        if len(unique_indices) * 2 > max(numpy.max(unique_indices), 254) and numpy.alltrue(
            numpy.abs(numpy.round(unique_indices, 1) - unique_indices)
            <= numpy.finfo(float).eps
        ):
            # Heuristic: reinterpret only if sparse and roughly integer
            return numpy.round(image).astype(int)

        def sorting(x):
            return [x]

        def comparison(i0, i1):
            return image.ravel()[i0] != image.ravel()[i1]

    else:
        i, j = numpy.mgrid[0 : image.shape[0], 0 : image.shape[1]]

        def sorting(x):
            return [x[:, :, 2], x[:, :, 1], x[:, :, 0]]

        def comparison(i0, i1):
            return numpy.any(
                image[i.ravel()[i0], j.ravel()[i0], :]
                != image[i.ravel()[i1], j.ravel()[i1], :],
                1,
            )

    order = numpy.lexsort([x.ravel() for x in sorting(image)])
    different = numpy.hstack([[False], comparison(order[:-1], order[1:])])
    index = numpy.cumsum(different)
    image = numpy.zeros(image.shape[:2], index.dtype)
    image.ravel()[order] = index
    return image
//  [python]
def convert_image_to_objects(image):
    """Interpret an image as object indices

    image - a grayscale or color image, assumes zero == background

    returns - a similarly shaped integer array with zero representing background
              and other values representing the indices of the associated object.
    """
    assert isinstance(image, numpy.ndarray)
    if image.ndim == 2:
        unique_indices = numpy.unique(image.ravel())
        if len(unique_indices) * 2 > max(numpy.max(unique_indices), 254) and numpy.all(
            numpy.abs(numpy.round(unique_indices, 1) - unique_indices)
            <= numpy.finfo(float).eps
        ):
            # Heuristic: reinterpret only if sparse and roughly integer
            return numpy.round(image).astype(int)

        def sorting(x):
            return [x]

        def comparison(i0, i1):
            return image.ravel()[i0] != image.ravel()[i1]

    else:
        i, j = numpy.mgrid[0 : image.shape[0], 0 : image.shape[1]]

        def sorting(x):
            return [x[:, :, 2], x[:, :, 1], x[:, :, 0]]

        def comparison(i0, i1):
            return numpy.any(
                image[i.ravel()[i0], j.ravel()[i0], :]
                != image[i.ravel()[i1], j.ravel()[i1], :],
                1,
            )

    order = numpy.lexsort([x.ravel() for x in sorting(image)])
    different = numpy.hstack([[False], comparison(order[:-1], order[1:])])
    index = numpy.cumsum(different)
    image = numpy.zeros(image.shape[:2], index.dtype)
    image.ravel()[order] = index
    return image
//  [python]
def test_mixed_map_1_dim():
    X = toy_setup.CategoricalVariable(categories=2)
    Z = toy_setup.ContinuousVariable()
    f = mediation.MixedMap([MapAddFirstDim(1), MapAddFirstDim(2)], [X, Z], dtype=np.dtype("float32"))
    r = f({X: np.array([0, 0, 0, 1, 1, 0, 1]), Z: np.arange(7)})
    assert np.all(r == np.array([1, 1, 1, 2, 2, 1, 2] + np.arange(7)))
//  [python]
def _lat_greater_extent(self, lat, lon):

        """
        Returns True if lat has a greater extent (in degrees) than lon (or the
        same extent as lon), and False otherwise.

        Authors
        -------
        Greg Streletz, Xylar Asay-Davis
        """
        lat_extent = numpy.max(lat) - numpy.min(lat)

        lon_diff = numpy.diff(lon)
        lon_jump = numpy.where(
            numpy.logical_or(lon_diff > 180, lon_diff < -180), True, False)

        if numpy.alltrue(numpy.logical_not(lon_jump)):
            # transect does not cross periodic boundary

            lon_extent = numpy.max(lon) - numpy.min(lon)

        else:
            # transect crosses periodic boundary at least once, so min and
            # max cannot be used to calculate the longitudinal extent

            # calculate the indices at which the transect crosses the
            # right-hand periodic boundary and enters the leftmost region of
            # the domain
            lon_r = numpy.sort(numpy.nonzero(lon_diff < -180)[0] + 1)

            # calculate the indices at which the transect crosses the left-hand
            # periodic boundary and enters the rightmost region of the domain
            lon_l = numpy.sort(numpy.nonzero(lon_diff > 180)[0] + 1)

            mins = []
            maxes = []
            last_idx = 0

            while(len(lon_r) > 0 and len(lon_l) > 0):
                if lon_r[0] < lon_l[0]:
                    mins.append(numpy.min(lon[last_idx:lon_r[0]]))
                    last_idx = lon_r[0]
                    lon_r = lon_r[1:]
                else:
                    maxes.append(numpy.max(lon[last_idx:lon_l[0]]))
                    last_idx = lon_l[0]
                    lon_l = lon_l[1:]

            min = numpy.min(mins)
            max = numpy.max(maxes)

            if min > max:

                lon_extent = 360.0 - (min - max)

                # The transect has crossed the periodic boundary,
                # so unless the transect has covered the entire
                # longitudinal range (360 degrees), the "leftmost"
                # or "min" coordinate of the transect will have a
                # greater value (in degrees longitude) that the
                # "rightmost" or "max" coordinate of the transect,
                # and (min - max) is the size (in degrees longitude)
                # of the region *not* accessed by the transect.  So,
                # the longitudinal extent of the transect is 360
                # degrees minus this value.

            else:
                # if max >= min, the transect has covered the
                # entire longitudinal range (360 degrees)

                lon_extent = 360.0

        if lat_extent >= lon_extent:
            return True
        else:
            return False
//  [python]
def _lat_greater_extent(self, lat, lon):

        """
        Returns True if lat has a greater extent (in degrees) than lon (or the
        same extent as lon), and False otherwise.

        Authors
        -------
        Greg Streletz, Xylar Asay-Davis
        """
        lat_extent = numpy.max(lat) - numpy.min(lat)

        lon_diff = numpy.diff(lon)
        lon_jump = numpy.where(
            numpy.logical_or(lon_diff > 180, lon_diff < -180), True, False)

        if numpy.all(numpy.logical_not(lon_jump)):
            # transect does not cross periodic boundary

            lon_extent = numpy.max(lon) - numpy.min(lon)

        else:
            # transect crosses periodic boundary at least once, so min and
            # max cannot be used to calculate the longitudinal extent

            # calculate the indices at which the transect crosses the
            # right-hand periodic boundary and enters the leftmost region of
            # the domain
            lon_r = numpy.sort(numpy.nonzero(lon_diff < -180)[0] + 1)

            # calculate the indices at which the transect crosses the left-hand
            # periodic boundary and enters the rightmost region of the domain
            lon_l = numpy.sort(numpy.nonzero(lon_diff > 180)[0] + 1)

            mins = []
            maxes = []
            last_idx = 0

            while(len(lon_r) > 0 and len(lon_l) > 0):
                if lon_r[0] < lon_l[0]:
                    mins.append(numpy.min(lon[last_idx:lon_r[0]]))
                    last_idx = lon_r[0]
                    lon_r = lon_r[1:]
                else:
                    maxes.append(numpy.max(lon[last_idx:lon_l[0]]))
                    last_idx = lon_l[0]
                    lon_l = lon_l[1:]

            min = numpy.min(mins)
            max = numpy.max(maxes)

            if min > max:

                lon_extent = 360.0 - (min - max)

                # The transect has crossed the periodic boundary,
                # so unless the transect has covered the entire
                # longitudinal range (360 degrees), the "leftmost"
                # or "min" coordinate of the transect will have a
                # greater value (in degrees longitude) that the
                # "rightmost" or "max" coordinate of the transect,
                # and (min - max) is the size (in degrees longitude)
                # of the region *not* accessed by the transect.  So,
                # the longitudinal extent of the transect is 360
                # degrees minus this value.

            else:
                # if max >= min, the transect has covered the
                # entire longitudinal range (360 degrees)

                lon_extent = 360.0

        if lat_extent >= lon_extent:
            return True
        else:
            return False
//  [python]
def evaluate(
    g,
    model,
    embed_layer,
    labels,
    eval_loader,
    test_loader,
    all_val_nid,
    all_test_nid,
):
    model.eval()
    embed_layer.eval()
    eval_logits = []
    eval_seeds = []

    global_results = dgl.distributed.DistTensor(
        labels.shape, th.long, "results", persistent=True
    )

    with th.no_grad():
        th.cuda.empty_cache()
        for sample_data in tqdm.tqdm(eval_loader):
            input_nodes, seeds, blocks = sample_data
            seeds = seeds["paper"]
            feats = embed_layer(input_nodes)
            logits = model(blocks, feats)
            assert len(logits) == 1
            logits = logits["paper"]
            eval_logits.append(logits.cpu().detach())
            assert np.all(seeds.numpy() < g.num_nodes("paper"))
            eval_seeds.append(seeds.cpu().detach())
    eval_logits = th.cat(eval_logits)
    eval_seeds = th.cat(eval_seeds)
    global_results[eval_seeds] = eval_logits.argmax(dim=1)

    test_logits = []
    test_seeds = []
    with th.no_grad():
        th.cuda.empty_cache()
        for sample_data in tqdm.tqdm(test_loader):
            input_nodes, seeds, blocks = sample_data
            seeds = seeds["paper"]
            feats = embed_layer(input_nodes)
            logits = model(blocks, feats)
            assert len(logits) == 1
            logits = logits["paper"]
            test_logits.append(logits.cpu().detach())
            assert np.all(seeds.numpy() < g.num_nodes("paper"))
            test_seeds.append(seeds.cpu().detach())
    test_logits = th.cat(test_logits)
    test_seeds = th.cat(test_seeds)
    global_results[test_seeds] = test_logits.argmax(dim=1)

    g.barrier()
    if g.rank() == 0:
        return compute_acc(
            global_results[all_val_nid], labels[all_val_nid]
        ), compute_acc(global_results[all_test_nid], labels[all_test_nid])
    else:
        return -1, -1
//  [python]
def __init__(self, xl=None, xu=None) -> None:
        super().__init__()

        # if both are None we are basically done because normalization is disabled
        if xl is None and xu is None:
            self.xl, self.xu = None, None
            return

        # if not set simply fall back no nan values
        if xl is None:
            xl = np.full_like(xu, np.nan)
        if xu is None:
            xu = np.full_like(xl, np.nan)

        xl, xu = np.copy(xl).astype(float), np.copy(xu).astype(float)

        # if both are equal then set the upper bound to none (always the 0 or lower bound will be returned then)
        xu[xl == xu] = np.nan

        # store the lower and upper bounds
        self.xl, self.xu = xl, xu

        # check out when the input values are nan
        xl_nan, xu_nan = np.isnan(xl), np.isnan(xu)

        # now create all the masks that are necessary
        self.xl_only, self.xu_only = np.logical_and(~xl_nan, xu_nan), np.logical_and(xl_nan, ~xu_nan)
        self.both_nan = np.logical_and(np.isnan(xl), np.isnan(xu))
        self.neither_nan = ~self.both_nan

        # if neither is nan than xu must be greater or equal than xl
        any_nan = np.logical_or(np.isnan(xl), np.isnan(xu))
        assert np.alltrue(np.logical_or(xu >= xl, any_nan)), "xl must be less or equal than xu."
//  [python]
def __init__(self, xl=None, xu=None) -> None:
        super().__init__()

        # if both are None we are basically done because normalization is disabled
        if xl is None and xu is None:
            self.xl, self.xu = None, None
            return

        # if not set simply fall back no nan values
        if xl is None:
            xl = np.full_like(xu, np.nan)
        if xu is None:
            xu = np.full_like(xl, np.nan)

        xl, xu = np.copy(xl).astype(float), np.copy(xu).astype(float)

        # if both are equal then set the upper bound to none (always the 0 or lower bound will be returned then)
        xu[xl == xu] = np.nan

        # store the lower and upper bounds
        self.xl, self.xu = xl, xu

        # check out when the input values are nan
        xl_nan, xu_nan = np.isnan(xl), np.isnan(xu)

        # now create all the masks that are necessary
        self.xl_only, self.xu_only = np.logical_and(~xl_nan, xu_nan), np.logical_and(xl_nan, ~xu_nan)
        self.both_nan = np.logical_and(np.isnan(xl), np.isnan(xu))
        self.neither_nan = ~self.both_nan

        # if neither is nan than xu must be greater or equal than xl
        any_nan = np.logical_or(np.isnan(xl), np.isnan(xu))
        assert np.all(np.logical_or(xu >= xl, any_nan)), "xl must be less or equal than xu."
//  [python]
def test_binomial(self):
        """Test that raw_random.binomial generates the same results
        as numpy."""
        # Check over two calls to see if the random state is correctly updated.
        rng_R = random_state_type()
        # Use non-default parameters, and larger dimensions because of
        # the integer nature of the result
        post_r, bin = binomial(rng_R, (7, 12), 5, 0.8)

        f = compile.function(
                [compile.In(rng_R,
                    value=numpy.random.RandomState(utt.fetch_seed()),
                    update=post_r, mutable=True)],
                [bin], accept_inplace=True)

        numpy_rng = numpy.random.RandomState(utt.fetch_seed())
        val0 = f()
        val1 = f()
        numpy_val0 = numpy_rng.binomial(5, 0.8, size=(7, 12))
        numpy_val1 = numpy_rng.binomial(5, 0.8, size=(7, 12))
        print(val0)
        print(numpy_val0)
        print(val1)
        print(numpy_val1)
        self.assertTrue(n.all(val0 == numpy_val0))
        self.assertTrue(n.all(val1 == numpy_val1))
//  [python]
def test_binomial(self):
        """Test that raw_random.binomial generates the same results
        as numpy."""
        # Check over two calls to see if the random state is correctly updated.
        rng_R = random_state_type()
        # Use non-default parameters, and larger dimensions because of
        # the integer nature of the result
        post_r, bin = binomial(rng_R, (7, 12), 5, 0.8)

        f = compile.function(
                [compile.In(rng_R,
                    value=numpy.random.RandomState(utt.fetch_seed()),
                    update=post_r, mutable=True)],
                [bin], accept_inplace=True)

        numpy_rng = numpy.random.RandomState(utt.fetch_seed())
        val0 = f()
        val1 = f()
        numpy_val0 = numpy_rng.binomial(5, 0.8, size=(7, 12))
        numpy_val1 = numpy_rng.binomial(5, 0.8, size=(7, 12))
        print(val0)
        print(numpy_val0)
        print(val1)
        print(numpy_val1)
        self.assertTrue(numpy.all(val0 == numpy_val0))
        self.assertTrue(numpy.all(val1 == numpy_val1))
//  [python]
def test_cholesky_indef():
    if not imported_scipy:
        raise SkipTest("Scipy needed for the Cholesky op.")
    x = tensor.matrix()
    matrix = np.array([[1, 0.2], [0.2, -2]]).astype(config.floatX)
    cholesky = Cholesky(lower=True, on_error='raise')
    chol_f = function([x], cholesky(x))
    with assert_raises(scipy.linalg.LinAlgError):
        chol_f(matrix)
    cholesky = Cholesky(lower=True, on_error='nan')
    chol_f = function([x], cholesky(x))
    assert n.all(n.isnan(chol_f(matrix)))
//  [python]
def test_cholesky_indef():
    if not imported_scipy:
        raise SkipTest("Scipy needed for the Cholesky op.")
    x = tensor.matrix()
    matrix = np.array([[1, 0.2], [0.2, -2]]).astype(config.floatX)
    cholesky = Cholesky(lower=True, on_error='raise')
    chol_f = function([x], cholesky(x))
    with assert_raises(scipy.linalg.LinAlgError):
        chol_f(matrix)
    cholesky = Cholesky(lower=True, on_error='nan')
    chol_f = function([x], cholesky(x))
    assert np.all(np.isnan(chol_f(matrix)))
//  [python]
def test_cholesky_grad_indef():
    if not imported_scipy:
        raise SkipTest("Scipy needed for the Cholesky op.")
    x = tensor.matrix()
    matrix = np.array([[1, 0.2], [0.2, -2]]).astype(config.floatX)
    cholesky = Cholesky(lower=True, on_error='raise')
    chol_f = function([x], grad(cholesky(x).sum(), [x]))
    with assert_raises(scipy.linalg.LinAlgError):
        chol_f(matrix)
    cholesky = Cholesky(lower=True, on_error='nan')
    chol_f = function([x], grad(cholesky(x).sum(), [x]))
    assert n.all(n.isnan(chol_f(matrix)))
//  [python]
def test_cholesky_grad_indef():
    if not imported_scipy:
        raise SkipTest("Scipy needed for the Cholesky op.")
    x = tensor.matrix()
    matrix = np.array([[1, 0.2], [0.2, -2]]).astype(config.floatX)
    cholesky = Cholesky(lower=True, on_error='raise')
    chol_f = function([x], grad(cholesky(x).sum(), [x]))
    with assert_raises(scipy.linalg.LinAlgError):
        chol_f(matrix)
    cholesky = Cholesky(lower=True, on_error='nan')
    chol_f = function([x], grad(cholesky(x).sum(), [x]))
    assert np.all(np.isnan(chol_f(matrix)))
//  [python]
def test_multinomial(self):
        """Test that raw_random.multinomial generates the same
        results as numpy."""
        # Check over two calls to see if the random state is correctly updated.
        rng_R = random_state_type()
        post_r, out = multinomial(rng_R, (7, 3), 6, [0.2] * 5)

        f = compile.function(
                [compile.In(rng_R,
                    value=numpy.random.RandomState(utt.fetch_seed()),
                    update=post_r, mutable=True)],
                [out], accept_inplace=True)

        numpy_rng = numpy.random.RandomState(utt.fetch_seed())
        val0, = f()
        val1, = f()
        numpy_val0 = numpy_rng.multinomial(6, [0.2] * 5, (7, 3))
        numpy_val1 = numpy_rng.multinomial(6, [0.2] * 5, (7, 3))
        print(val0)
        print(numpy_val0)
        print(val1)
        print(numpy_val1)
        self.assertTrue(numpy.alltrue(val0 == numpy_val0))
        self.assertTrue(numpy.alltrue(val1 == numpy_val1))

        self.assertTrue(val0.shape == (7, 3, 5))
        self.assertTrue(val1.shape == (7, 3, 5))
//  [python]
def test_multinomial(self):
        """Test that raw_random.multinomial generates the same
        results as numpy."""
        # Check over two calls to see if the random state is correctly updated.
        rng_R = random_state_type()
        post_r, out = multinomial(rng_R, (7, 3), 6, [0.2] * 5)

        f = compile.function(
                [compile.In(rng_R,
                    value=numpy.random.RandomState(utt.fetch_seed()),
                    update=post_r, mutable=True)],
                [out], accept_inplace=True)

        numpy_rng = numpy.random.RandomState(utt.fetch_seed())
        val0, = f()
        val1, = f()
        numpy_val0 = numpy_rng.multinomial(6, [0.2] * 5, (7, 3))
        numpy_val1 = numpy_rng.multinomial(6, [0.2] * 5, (7, 3))
        print(val0)
        print(numpy_val0)
        print(val1)
        print(numpy_val1)
        self.assertTrue(numpy.all(val0 == numpy_val0))
        self.assertTrue(numpy.all(val1 == numpy_val1))

        self.assertTrue(val0.shape == (7, 3, 5))
        self.assertTrue(val1.shape == (7, 3, 5))
//  [python]
def test_permutation(self):
        """Test that raw_random.permutation generates the same
        results as numpy."""
        rng_R = random_state_type()
        post_r, out = permutation(rng_R, size=(9,), n=6)
        print('OUT NDIM', out.ndim)
        f = compile.function(
                [compile.In(rng_R,
                    value=numpy.random.RandomState(utt.fetch_seed()),
                    update=post_r, mutable=True)],
                [out], accept_inplace=True)

        numpy_rng = numpy.random.RandomState(utt.fetch_seed())
        # Check over two calls to see if the random state is correctly updated.
        # numpy_rng.permutation outputs one vector at a time,
        # so we call it iteratively to generate all the samples.
        val0 = f()
        val1 = f()
        numpy_val0 = numpy.asarray([numpy_rng.permutation(6)
                                    for i in range(9)])
        numpy_val1 = numpy.asarray([numpy_rng.permutation(6)
                                    for i in range(9)])
        print(val0)
        print(numpy_val0)
        print(val1)
        print(numpy_val1)
        self.assertTrue(n.all(val0 == numpy_val0))
        self.assertTrue(n.all(val1 == numpy_val1))

        # Test that we can generate a list: have size=None or ().
        for ndim in [1, None]:
            post_r, out = permutation(rng_R, n=10, size=None, ndim=ndim)
            inp = compile.In(rng_R,
                             value=numpy.random.RandomState(utt.fetch_seed()),
                             update=post_r, mutable=True)
            f = theano.function([inp], out)
            o = f()
            assert o.shape == (10,)
            assert (numpy.sort(o) == numpy.arange(10)).all()
        # Wrong number of dimensions asked
        self.assertRaises(TypeError, permutation, rng_R, size=None, ndim=2)
//  [python]
def test_permutation(self):
        """Test that raw_random.permutation generates the same
        results as numpy."""
        rng_R = random_state_type()
        post_r, out = permutation(rng_R, size=(9,), n=6)
        print('OUT NDIM', out.ndim)
        f = compile.function(
                [compile.In(rng_R,
                    value=numpy.random.RandomState(utt.fetch_seed()),
                    update=post_r, mutable=True)],
                [out], accept_inplace=True)

        numpy_rng = numpy.random.RandomState(utt.fetch_seed())
        # Check over two calls to see if the random state is correctly updated.
        # numpy_rng.permutation outputs one vector at a time,
        # so we call it iteratively to generate all the samples.
        val0 = f()
        val1 = f()
        numpy_val0 = numpy.asarray([numpy_rng.permutation(6)
                                    for i in range(9)])
        numpy_val1 = numpy.asarray([numpy_rng.permutation(6)
                                    for i in range(9)])
        print(val0)
        print(numpy_val0)
        print(val1)
        print(numpy_val1)
        self.assertTrue(numpy.all(val0 == numpy_val0))
        self.assertTrue(numpy.all(val1 == numpy_val1))

        # Test that we can generate a list: have size=None or ().
        for ndim in [1, None]:
            post_r, out = permutation(rng_R, n=10, size=None, ndim=ndim)
            inp = compile.In(rng_R,
                             value=numpy.random.RandomState(utt.fetch_seed()),
                             update=post_r, mutable=True)
            f = theano.function([inp], out)
            o = f()
            assert o.shape == (10,)
            assert (numpy.sort(o) == numpy.arange(10)).all()
        # Wrong number of dimensions asked
        self.assertRaises(TypeError, permutation, rng_R, size=None, ndim=2)
//  [python]
def testConstraints(self, g):
        result = False
        cons_base = g[0:len(self.xBase_feas)]
        if self.idf.opt['optInFeasibleParamSpace']:
            iq_start = len(self.xBase_feas)+self.nl
            iq_end = len(self.xBase_feas)+self.nl*2
            cons_ident_q = g[iq_start:iq_end]
            cons_det_q = g[0:self.nl]
            cons_mass = g[iq_end]
            mass_con = self.opt_prob.getCon(iq_end)
            cons_com = g[iq_end+1:]
            if all(np.array(cons_det_q) >= 0) and (np.abs(np.sum(np.array(cons_base))) <= 1e-6) \
                    and all(np.abs(cons_ident_q) <= 0.001) and all(cons_com >= 0.0) \
                    and cons_mass < mass_con.upper and cons_mass > mass_con.lower:
                result = True
        else:
            cons_mass = g[-self.nl*6-1]
            mass_con = self.opt_prob.getCon(len(g)-self.nl*6 - 1)
            if all(np.array(g[len(self.xBase_feas):]) >= 0) and (np.abs(np.sum(np.array(cons_base))) <= 1e-6) \
                    and cons_mass < mass_con.upper and cons_mass > mass_con.lower:
                result = True

        return result
//  [python]
def testConstraints(self, g):
        result = False
        cons_base = g[0:len(self.xBase_feas)]
        if self.idf.opt['optInFeasibleParamSpace']:
            iq_start = len(self.xBase_feas)+self.nl
            iq_end = len(self.xBase_feas)+self.nl*2
            cons_ident_q = g[iq_start:iq_end]
            cons_det_q = g[0:self.nl]
            cons_mass = g[iq_end]
            mass_con = self.opt_prob.getCon(iq_end)
            cons_com = g[iq_end+1:]
            if np.all(np.array(cons_det_q) >= 0) and (np.abs(np.sum(np.array(cons_base))) <= 1e-6) \
                    and np.all(np.abs(cons_ident_q) <= 0.001) and np.all(cons_com >= 0.0) \
                    and cons_mass < mass_con.upper and cons_mass > mass_con.lower:
                result = True
        else:
            cons_mass = g[-self.nl*6-1]
            mass_con = self.opt_prob.getCon(len(g)-self.nl*6 - 1)
            if np.all(np.array(g[len(self.xBase_feas):]) >= 0) and (np.abs(np.sum(np.array(cons_base))) <= 1e-6) \
                    and cons_mass < mass_con.upper and cons_mass > mass_con.lower:
                result = True

        return result
//  [python]
def test_permutation_helper(self):
        """Test that raw_random.permutation_helper generates the same
        results as numpy,
        and that the 'ndim_added' keyword behaves correctly."""
        # permutation_helper needs "ndim_added=1", because its output
        # is one dimension more than its "shape" argument (and there's
        # no way to determine that automatically).
        # Check the working case, over two calls to see if the random
        # state is correctly updated.
        rf = RandomFunction(permutation_helper, tensor.imatrix, 8,
                            ndim_added=1)
        rng_R = random_state_type()
        post_r, out = rf(rng_R, (7,), 8)

        f = compile.function(
                [compile.In(rng_R,
                    value=numpy.random.RandomState(utt.fetch_seed()),
                    update=post_r, mutable=True)],
                [out], accept_inplace=True)

        numpy_rng = numpy.random.RandomState(utt.fetch_seed())
        val0 = f()
        val1 = f()
        # numpy_rng.permutation outputs one vector at a time,
        # so we call it iteratively to generate all the samples.
        numpy_val0 = numpy.asarray([numpy_rng.permutation(8)
                                    for i in range(7)])
        numpy_val1 = numpy.asarray([numpy_rng.permutation(8)
                                    for i in range(7)])
        print(val0)
        print(numpy_val0)
        print(val1)
        print(numpy_val1)
        self.assertTrue(n.all(val0 == numpy_val0))
        self.assertTrue(n.all(val1 == numpy_val1))

        # This call lacks "ndim_added=1", so ndim_added defaults to 0.
        # A ValueError should be raised.
        rf0 = RandomFunction(permutation_helper, tensor.imatrix, 8)
        post_r0, out0 = rf0(rng_R, (7,), 8)
        f0 = compile.function(
                [compile.In(rng_R,
                    value=numpy.random.RandomState(utt.fetch_seed()),
                    update=post_r0, mutable=True)],
                [out0], accept_inplace=True)
        self.assertRaises(ValueError, f0)

        # Here, ndim_added is 2 instead of 1. A ValueError should be raised.
        rf2 = RandomFunction(permutation_helper, tensor.imatrix, 8,
                             ndim_added=2)
        post_r2, out2 = rf2(rng_R, (7,), 8)
        f2 = compile.function(
                [compile.In(rng_R,
                    value=numpy.random.RandomState(utt.fetch_seed()),
                    update=post_r2, mutable=True)],
                [out2], accept_inplace=True)
        self.assertRaises(ValueError, f2)
//  [python]
def test_permutation_helper(self):
        """Test that raw_random.permutation_helper generates the same
        results as numpy,
        and that the 'ndim_added' keyword behaves correctly."""
        # permutation_helper needs "ndim_added=1", because its output
        # is one dimension more than its "shape" argument (and there's
        # no way to determine that automatically).
        # Check the working case, over two calls to see if the random
        # state is correctly updated.
        rf = RandomFunction(permutation_helper, tensor.imatrix, 8,
                            ndim_added=1)
        rng_R = random_state_type()
        post_r, out = rf(rng_R, (7,), 8)

        f = compile.function(
                [compile.In(rng_R,
                    value=numpy.random.RandomState(utt.fetch_seed()),
                    update=post_r, mutable=True)],
                [out], accept_inplace=True)

        numpy_rng = numpy.random.RandomState(utt.fetch_seed())
        val0 = f()
        val1 = f()
        # numpy_rng.permutation outputs one vector at a time,
        # so we call it iteratively to generate all the samples.
        numpy_val0 = numpy.asarray([numpy_rng.permutation(8)
                                    for i in range(7)])
        numpy_val1 = numpy.asarray([numpy_rng.permutation(8)
                                    for i in range(7)])
        print(val0)
        print(numpy_val0)
        print(val1)
        print(numpy_val1)
        self.assertTrue(numpy.all(val0 == numpy_val0))
        self.assertTrue(numpy.all(val1 == numpy_val1))

        # This call lacks "ndim_added=1", so ndim_added defaults to 0.
        # A ValueError should be raised.
        rf0 = RandomFunction(permutation_helper, tensor.imatrix, 8)
        post_r0, out0 = rf0(rng_R, (7,), 8)
        f0 = compile.function(
                [compile.In(rng_R,
                    value=numpy.random.RandomState(utt.fetch_seed()),
                    update=post_r0, mutable=True)],
                [out0], accept_inplace=True)
        self.assertRaises(ValueError, f0)

        # Here, ndim_added is 2 instead of 1. A ValueError should be raised.
        rf2 = RandomFunction(permutation_helper, tensor.imatrix, 8,
                             ndim_added=2)
        post_r2, out2 = rf2(rng_R, (7,), 8)
        f2 = compile.function(
                [compile.In(rng_R,
                    value=numpy.random.RandomState(utt.fetch_seed()),
                    update=post_r2, mutable=True)],
                [out2], accept_inplace=True)
        self.assertRaises(ValueError, f2)
//  [python]
def test_endian_bool_indexing(self,level=rlevel):
        """Ticket #105"""
        a = np.arange(10., dtype='>f8')
        b = np.arange(10., dtype='<f8')
        xa = np.where((a>2) & (a<6))
        xb = np.where((b>2) & (b<6))
        ya = ((a>2) & (a<6))
        yb = ((b>2) & (b<6))
        assert_array_almost_equal(xa, ya.nonzero())
        assert_array_almost_equal(xb, yb.nonzero())
        assert_(a[ya] > 0.5).all()
        assert_(b[yb] > 0.5).all()
//  [python]
def test_endian_bool_indexing(self,level=rlevel):
        """Ticket #105"""
        a = np.arange(10., dtype='>f8')
        b = np.arange(10., dtype='<f8')
        xa = np.where((a>2) & (a<6))
        xb = np.where((b>2) & (b<6))
        ya = ((a>2) & (a<6))
        yb = ((b>2) & (b<6))
        assert_array_almost_equal(xa, ya.nonzero())
        assert_array_almost_equal(xb, yb.nonzero())
        assert_(np.all(a[ya] > 0.5))
        assert_(np.all(b[yb] > 0.5))
//  [python]
def la_roots(n, alpha, mu=0):
    """[x,w] = la_roots(n,alpha)

    Returns the roots (x) of the nth order generalized (associated) Laguerre
    polynomial, L^(alpha)_n(x), and weights (w) to use in Gaussian quadrature over
    [0,inf] with weighting function exp(-x) x**alpha with alpha > -1.
    """
    if not n.all(alpha > -1):
        raise ValueError("alpha > -1")
    if n < 1:
        raise ValueError("n must be positive.")

    (p,q) = (alpha,0.0)
    sbn_La = lambda k: -sqrt(k*(k + p))  # from recurrence relation
    an_La = lambda k: 2*k + p + 1
    mu0 = cephes.gamma(alpha+1)           # integral of weight over interval
    val = gen_roots_and_weights(n,an_La,sbn_La,mu0)
    if mu:
        return val + [mu0]
    else:
        return val
//  [python]
def la_roots(n, alpha, mu=0):
    """[x,w] = la_roots(n,alpha)

    Returns the roots (x) of the nth order generalized (associated) Laguerre
    polynomial, L^(alpha)_n(x), and weights (w) to use in Gaussian quadrature over
    [0,inf] with weighting function exp(-x) x**alpha with alpha > -1.
    """
    if not all(alpha > -1):
        raise ValueError("alpha > -1")
    if n < 1:
        raise ValueError("n must be positive.")

    (p,q) = (alpha,0.0)
    sbn_La = lambda k: -sqrt(k*(k + p))  # from recurrence relation
    an_La = lambda k: 2*k + p + 1
    mu0 = cephes.gamma(alpha+1)           # integral of weight over interval
    val = gen_roots_and_weights(n,an_La,sbn_La,mu0)
    if mu:
        return val + [mu0]
    else:
        return val
//  [python]
def image_equal(arr1, arr2, encoding):
  assert arr1.shape == arr2.shape
  if encoding == 'raw':
    return n.all(arr1 == arr2)

  # jpeg
  u1 = arr1.flatten().mean()
  u2 = arr2.flatten().mean()
  return np.abs(u1 - u2) < 1
//  [python]
def image_equal(arr1, arr2, encoding):
  assert arr1.shape == arr2.shape
  if encoding == 'raw':
    return np.all(arr1 == arr2)

  # jpeg
  u1 = arr1.flatten().mean()
  u2 = arr2.flatten().mean()
  return np.abs(u1 - u2) < 1
//  [python]
def test_blasdot_uninitialized_memory(self):
        """Ticket #950"""
        for m in [0, 1, 2]:
            for n in [0, 1, 2]:
                for k in range(3):
                    # Try to ensure that x->data contains non-zero floats
                    x = np.array([123456789e199], dtype=np.float64)
                    x.resize((m, 0))
                    y = np.array([123456789e199], dtype=np.float64)
                    y.resize((0, n))

                    # `dot` should just return zero (m,n) matrix
                    z = np.dot(x, y)
                    assert_(np.all(z == 0))
                    assert_(z.shape == (m, n))
//  [python]
def test_bbox_read():
  delete_layer()
  cv, data = create_layer(size=(50,50,50,1), offset=(0,0,0))

  x = Bbox( (0,1,2), (48,49,50) )
  # the last dimension is the number of channels
  assert cv[x].shape == (48,48,48,1)
  assert n.all(cv[x] == data[0:48, 1:49, 2:50])
//  [python]
def test_bbox_read():
  delete_layer()
  cv, data = create_layer(size=(50,50,50,1), offset=(0,0,0))

  x = Bbox( (0,1,2), (48,49,50) )
  # the last dimension is the number of channels
  assert cv[x].shape == (48,48,48,1)
  assert np.all(cv[x] == data[0:48, 1:49, 2:50])
//  [python]
def test_parallel_read_shm(protocol):
  cloudpath = "{}://seunglab-test/test_v0/image".format(protocol)
  
  vol1 = CloudVolume(cloudpath, parallel=1)
  vol2 = CloudVolume(cloudpath, parallel=2)

  data1 = vol1[:512,:512,:50]
  data2 = vol2.download_to_shared_memory(np.s_[:512,:512,:50])
  assert np.all(data1 == data2)
  data2.close()
  vol2.unlink_shared_memory()
//  [python]
def test_parallel_read(protocol, parallel, lru_bytes):
  cloudpath = "{}://seunglab-test/test_v0/image".format(protocol)

  vol1 = CloudVolume(cloudpath, parallel=1)
  vol2 = CloudVolume(cloudpath, parallel=parallel)
  vol2.image.lru.resize(lru_bytes)

  data1 = vol1[:512,:512,:50]
  img = vol2[:512,:512,:50]
  assert n.all(data1 == vol2[:512,:512,:50])
//  [python]
def test_parallel_read(protocol, parallel, lru_bytes):
  cloudpath = "{}://seunglab-test/test_v0/image".format(protocol)

  vol1 = CloudVolume(cloudpath, parallel=1)
  vol2 = CloudVolume(cloudpath, parallel=parallel)
  vol2.image.lru.resize(lru_bytes)

  data1 = vol1[:512,:512,:50]
  img = vol2[:512,:512,:50]
  assert np.all(data1 == vol2[:512,:512,:50])
//  [python]
def test_read_binary_image(green, encoding, lru_bytes):
  delete_layer()
  cv, data = create_layer(
    size=(100,100,50,1), offset=(0,0,0), 
    layer_type="segmentation", encoding=encoding
  )
  data = np.arange(data.size, dtype=data.dtype).reshape([100,100,50,1], order="F")
  cv[:] = data

  cv.green_threads = green
  cv.image.lru.resize(lru_bytes)
  
  bbox = Bbox([0,0,0], data.shape)
  
  img = cv.download(bbox, mip=0, label=500)

  assert img.dtype == bool
  assert n.all(img == (data == 500))

  img = cv.download(bbox, mip=0, label=0)
  assert img.dtype == bool
  assert n.all(img == (data == 0))


  bbox = Bbox([1,1,1], [2,2,2])
  img = cv.download(bbox, mip=0, label=data[1,1,1])
  assert img.dtype == bool
  assert img == True

  img = cv.download(bbox, mip=0, label=data[2,2,2])
  assert img.dtype == bool
  assert img == False
//  [python]
def test_read_binary_image(green, encoding, lru_bytes):
  delete_layer()
  cv, data = create_layer(
    size=(100,100,50,1), offset=(0,0,0), 
    layer_type="segmentation", encoding=encoding
  )
  data = np.arange(data.size, dtype=data.dtype).reshape([100,100,50,1], order="F")
  cv[:] = data

  cv.green_threads = green
  cv.image.lru.resize(lru_bytes)
  
  bbox = Bbox([0,0,0], data.shape)
  
  img = cv.download(bbox, mip=0, label=500)

  assert img.dtype == bool
  assert np.all(img == (data == 500))

  img = cv.download(bbox, mip=0, label=0)
  assert img.dtype == bool
  assert np.all(img == (data == 0))


  bbox = Bbox([1,1,1], [2,2,2])
  img = cv.download(bbox, mip=0, label=data[1,1,1])
  assert img.dtype == bool
  assert img == True

  img = cv.download(bbox, mip=0, label=data[2,2,2])
  assert img.dtype == bool
  assert img == False
//  [python]
def test_ellipsis_read():
  delete_layer()
  cv, data = create_layer(size=(50,50,50,1), offset=(0,0,0))

  img = cv[...]
  assert n.all(img == data)

  img = cv[5:10, ...]
  assert n.all(img == data[5:10, :,:,:])

  img = cv[5:10, 7, 8, ...]
  assert n.all(np.squeeze(img) == data[5:10, 7, 8, 0])

  img = cv[5:10, ..., 8, 0]
  assert n.all(np.squeeze(img) == data[5:10, :, 8, 0])

  try:
    img = cv[..., 5, ..., 0]
  except ValueError:
    pass
//  [python]
def test_ellipsis_read():
  delete_layer()
  cv, data = create_layer(size=(50,50,50,1), offset=(0,0,0))

  img = cv[...]
  assert np.all(img == data)

  img = cv[5:10, ...]
  assert np.all(img == data[5:10, :,:,:])

  img = cv[5:10, 7, 8, ...]
  assert np.all(np.squeeze(img) == data[5:10, 7, 8, 0])

  img = cv[5:10, ..., 8, 0]
  assert np.all(np.squeeze(img) == data[5:10, :, 8, 0])

  try:
    img = cv[..., 5, ..., 0]
  except ValueError:
    pass
//  [python]
def test_number_type_read():
  delete_layer()
  cv, data = create_layer(size=(50,50,50,1), offset=(0,0,0))

  for datatype in (
    np.uint8, np.uint16, np.uint32, np.uint64,
    np.int8, np.int16, np.int32, np.int64,
    np.float16, np.float32, np.float64,
    int, float
  ):
    x = datatype(5)

    # the last dimension is the number of channels
    assert cv[x,x,x].shape == (1,1,1,1)
    assert n.all(cv[x,x,x] == data[5,5,5])
//  [python]
def test_number_type_read():
  delete_layer()
  cv, data = create_layer(size=(50,50,50,1), offset=(0,0,0))

  for datatype in (
    np.uint8, np.uint16, np.uint32, np.uint64,
    np.int8, np.int16, np.int32, np.int64,
    np.float16, np.float32, np.float64,
    int, float
  ):
    x = datatype(5)

    # the last dimension is the number of channels
    assert cv[x,x,x].shape == (1,1,1,1)
    assert np.all(cv[x,x,x] == data[5,5,5])