Python numpy.asfortranarray() Examples

The following are 30 code examples for showing how to use numpy.asfortranarray(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module numpy , or try the search function .

Example 1
Project: coco-json-converter   Author: hazirbas   File: generate_coco_json.py    License: GNU General Public License v3.0 10 votes vote down vote up
def __get_annotation__(self, mask, image=None):

        _, contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

        segmentation = []
        for contour in contours:
            # Valid polygons have >= 6 coordinates (3 points)
            if contour.size >= 6:
                segmentation.append(contour.flatten().tolist())
        RLEs = cocomask.frPyObjects(segmentation, mask.shape[0], mask.shape[1])
        RLE = cocomask.merge(RLEs)
        # RLE = cocomask.encode(np.asfortranarray(mask))
        area = cocomask.area(RLE)
        [x, y, w, h] = cv2.boundingRect(mask)

        if image is not None:
            image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
            cv2.drawContours(image, contours, -1, (0,255,0), 1)
            cv2.rectangle(image,(x,y),(x+w,y+h), (255,0,0), 2)
            cv2.imshow("", image)
            cv2.waitKey(1)

        return segmentation, [x, y, w, h], area 
Example 2
Project: dataiku-contrib   Author: dataiku   File: coco.py    License: Apache License 2.0 7 votes vote down vote up
def build_coco_results(dataset, image_ids, rois, class_ids, scores, masks):
    """Arrange resutls to match COCO specs in http://cocodataset.org/#format
    """
    # If no results, return an empty list
    if rois is None:
        return []

    results = []
    for image_id in image_ids:
        # Loop through detections
        for i in range(rois.shape[0]):
            class_id = class_ids[i]
            score = scores[i]
            bbox = np.around(rois[i], 1)
            mask = masks[:, :, i]

            result = {
                "image_id": image_id,
                "category_id": dataset.get_source_class_id(class_id, "coco"),
                "bbox": [bbox[1], bbox[0], bbox[3] - bbox[1], bbox[2] - bbox[0]],
                "score": score,
                "segmentation": maskUtils.encode(np.asfortranarray(mask))
            }
            results.append(result)
    return results 
Example 3
Project: celer   Author: mathurinm   File: testing.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def build_dataset(n_samples=50, n_features=200, n_targets=1, sparse_X=False):
    """Build samples and observation for linear regression problem."""
    random_state = np.random.RandomState(0)
    if n_targets > 1:
        w = random_state.randn(n_features, n_targets)
    else:
        w = random_state.randn(n_features)

    if sparse_X:
        X = sparse.random(n_samples, n_features, density=0.5, format='csc',
                          random_state=random_state)

    else:
        X = np.asfortranarray(random_state.randn(n_samples, n_features))

    y = X.dot(w)
    return X, y 
Example 4
Project: dexplo   Author: dexplo   File: _utils.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def concat_data_arrays(data_dict: Dict[str, List[ndarray]]) -> Dict[str, ndarray]:
    new_data: Dict[str, ndarray] = {}
    empty_arrs = create_empty_arrs(data_dict)
    for kind, arrs in data_dict.items():
        if len(arrs) == 1:
            arr = arrs[0]
            if arr.ndim == 1:
                arr = arr.reshape(-1, 1)
            new_data[kind] = np.asfortranarray(arr)
        else:
            data = empty_arrs[kind]
            i = 0
            for arr in arrs:
                if arr.ndim == 1:
                    data[:, i] = arr
                    i += 1
                else:
                    for j in range(arr.shape[1]):
                        data[:, i] = arr[:, j]
                        i += 1
            new_data[kind] = data
    return new_data 
Example 5
Project: mars   Author: mars-project   File: test_base_execute.py    License: Apache License 2.0 6 votes vote down vote up
def testCopytoExecution(self):
        a = ones((2, 3), chunk_size=1)
        b = tensor([3, -1, 3], chunk_size=2)

        copyto(a, b, where=b > 1)

        res = self.executor.execute_tensor(a, concat=True)[0]
        expected = np.array([[3, 1, 3], [3, 1, 3]])

        np.testing.assert_equal(res, expected)

        a = ones((2, 3), chunk_size=1)
        b = tensor(np.asfortranarray(np.random.rand(2, 3)), chunk_size=2)

        copyto(b, a)

        res = self.executor.execute_tensor(b, concat=True)[0]
        expected = np.asfortranarray(np.ones((2, 3)))

        np.testing.assert_array_equal(res, expected)
        self.assertTrue(res.flags['F_CONTIGUOUS'])
        self.assertFalse(res.flags['C_CONTIGUOUS']) 
Example 6
Project: mars   Author: mars-project   File: test_base_execute.py    License: Apache License 2.0 6 votes vote down vote up
def testAstypeExecution(self):
        raw = np.random.random((10, 5))
        arr = tensor(raw, chunk_size=3)
        arr2 = arr.astype('i8')

        res = self.executor.execute_tensor(arr2, concat=True)
        np.testing.assert_array_equal(res[0], raw.astype('i8'))

        raw = sps.random(10, 5, density=.2)
        arr = tensor(raw, chunk_size=3)
        arr2 = arr.astype('i8')

        res = self.executor.execute_tensor(arr2, concat=True)
        self.assertTrue(np.array_equal(res[0].toarray(), raw.astype('i8').toarray()))

        raw = np.asfortranarray(np.random.random((10, 5)))
        arr = tensor(raw, chunk_size=3)
        arr2 = arr.astype('i8', order='C')

        res = self.executor.execute_tensor(arr2, concat=True)[0]
        np.testing.assert_array_equal(res, raw.astype('i8'))
        self.assertTrue(res.flags['C_CONTIGUOUS'])
        self.assertFalse(res.flags['F_CONTIGUOUS']) 
Example 7
Project: mars   Author: mars-project   File: test_base_execute.py    License: Apache License 2.0 6 votes vote down vote up
def testArgwhereExecution(self):
        x = arange(6, chunk_size=2).reshape(2, 3)
        t = argwhere(x > 1)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.argwhere(np.arange(6).reshape(2, 3) > 1)

        np.testing.assert_array_equal(res, expected)

        data = np.asfortranarray(np.random.rand(10, 20))
        x = tensor(data, chunk_size=10)

        t = argwhere(x > 0.5)

        res = self.executor.execute_tensor(t, concat=True)[0]
        expected = np.argwhere(data > 0.5)

        np.testing.assert_array_equal(res, expected)
        self.assertTrue(res.flags['F_CONTIGUOUS'])
        self.assertFalse(res.flags['C_CONTIGUOUS']) 
Example 8
Project: mars   Author: mars-project   File: test_arithmetic_execution.py    License: Apache License 2.0 6 votes vote down vote up
def testCosOrderExecution(self):
        data = np.asfortranarray(np.random.rand(3, 5))
        x = tensor(data, chunk_size=2)

        t = cos(x)

        res = self.executor.execute_tensor(t, concat=True)[0]
        np.testing.assert_allclose(res, np.cos(data))
        self.assertFalse(res.flags['C_CONTIGUOUS'])
        self.assertTrue(res.flags['F_CONTIGUOUS'])

        t2 = cos(x, order='C')

        res2 = self.executor.execute_tensor(t2, concat=True)[0]
        np.testing.assert_allclose(res2, np.cos(data, order='C'))
        self.assertTrue(res2.flags['C_CONTIGUOUS'])
        self.assertFalse(res2.flags['F_CONTIGUOUS']) 
Example 9
Project: lambda-packs   Author: ryfeus   File: _interpolative_backend.py    License: MIT License 6 votes vote down vote up
def iddr_id(A, k):
    """
    Compute ID of a real matrix to a specified rank.

    :param A:
        Matrix.
    :type A: :class:`numpy.ndarray`
    :param k:
        Rank of ID.
    :type k: int

    :return:
        Column index array.
    :rtype: :class:`numpy.ndarray`
    :return:
        Interpolation coefficients.
    :rtype: :class:`numpy.ndarray`
    """
    A = np.asfortranarray(A)
    idx, rnorms = _id.iddr_id(A, k)
    n = A.shape[1]
    proj = A.T.ravel()[:k*(n-k)].reshape((k, n-k), order='F')
    return idx, proj 
Example 10
Project: lambda-packs   Author: ryfeus   File: _interpolative_backend.py    License: MIT License 6 votes vote down vote up
def idd_reconid(B, idx, proj):
    """
    Reconstruct matrix from real ID.

    :param B:
        Skeleton matrix.
    :type B: :class:`numpy.ndarray`
    :param idx:
        Column index array.
    :type idx: :class:`numpy.ndarray`
    :param proj:
        Interpolation coefficients.
    :type proj: :class:`numpy.ndarray`

    :return:
        Reconstructed matrix.
    :rtype: :class:`numpy.ndarray`
    """
    B = np.asfortranarray(B)
    if proj.size > 0:
        return _id.idd_reconid(B, idx, proj)
    else:
        return B[:, np.argsort(idx)] 
Example 11
Project: lambda-packs   Author: ryfeus   File: _interpolative_backend.py    License: MIT License 6 votes vote down vote up
def idd_copycols(A, k, idx):
    """
    Reconstruct skeleton matrix from real ID.

    :param A:
        Original matrix.
    :type A: :class:`numpy.ndarray`
    :param k:
        Rank of ID.
    :type k: int
    :param idx:
        Column index array.
    :type idx: :class:`numpy.ndarray`

    :return:
        Skeleton matrix.
    :rtype: :class:`numpy.ndarray`
    """
    A = np.asfortranarray(A)
    return _id.idd_copycols(A, k, idx)


#------------------------------------------------------------------------------
# idd_id2svd.f
#------------------------------------------------------------------------------ 
Example 12
Project: lambda-packs   Author: ryfeus   File: _interpolative_backend.py    License: MIT License 6 votes vote down vote up
def idzr_id(A, k):
    """
    Compute ID of a complex matrix to a specified rank.

    :param A:
        Matrix.
    :type A: :class:`numpy.ndarray`
    :param k:
        Rank of ID.
    :type k: int

    :return:
        Column index array.
    :rtype: :class:`numpy.ndarray`
    :return:
        Interpolation coefficients.
    :rtype: :class:`numpy.ndarray`
    """
    A = np.asfortranarray(A)
    idx, rnorms = _id.idzr_id(A, k)
    n = A.shape[1]
    proj = A.T.ravel()[:k*(n-k)].reshape((k, n-k), order='F')
    return idx, proj 
Example 13
Project: lambda-packs   Author: ryfeus   File: _interpolative_backend.py    License: MIT License 6 votes vote down vote up
def idz_reconid(B, idx, proj):
    """
    Reconstruct matrix from complex ID.

    :param B:
        Skeleton matrix.
    :type B: :class:`numpy.ndarray`
    :param idx:
        Column index array.
    :type idx: :class:`numpy.ndarray`
    :param proj:
        Interpolation coefficients.
    :type proj: :class:`numpy.ndarray`

    :return:
        Reconstructed matrix.
    :rtype: :class:`numpy.ndarray`
    """
    B = np.asfortranarray(B)
    if proj.size > 0:
        return _id.idz_reconid(B, idx, proj)
    else:
        return B[:, np.argsort(idx)] 
Example 14
Project: lambda-packs   Author: ryfeus   File: _interpolative_backend.py    License: MIT License 6 votes vote down vote up
def idz_copycols(A, k, idx):
    """
    Reconstruct skeleton matrix from complex ID.

    :param A:
        Original matrix.
    :type A: :class:`numpy.ndarray`
    :param k:
        Rank of ID.
    :type k: int
    :param idx:
        Column index array.
    :type idx: :class:`numpy.ndarray`

    :return:
        Skeleton matrix.
    :rtype: :class:`numpy.ndarray`
    """
    A = np.asfortranarray(A)
    return _id.idz_copycols(A, k, idx)


#------------------------------------------------------------------------------
# idz_id2svd.f
#------------------------------------------------------------------------------ 
Example 15
Project: spleeter   Author: deezer   File: separator.py    License: MIT License 5 votes vote down vote up
def _stft(self, data, inverse=False, length=None):
        """
        Single entrypoint for both stft and istft. This computes stft and istft with librosa on stereo data. The two
        channels are processed separately and are concatenated together in the result. The expected input formats are:
        (n_samples, 2) for stft and (T, F, 2) for istft.
        :param data: np.array with either the waveform or the complex spectrogram depending on the parameter inverse
        :param inverse: should a stft or an istft be computed.
        :return: Stereo data as numpy array for the transform. The channels are stored in the last dimension
        """
        assert not (inverse and length is None)
        data = np.asfortranarray(data)
        N = self._params["frame_length"]
        H = self._params["frame_step"]
        win = hann(N, sym=False)
        fstft = istft if inverse else stft
        win_len_arg = {"win_length": None, "length": length} if inverse else {"n_fft": N}
        n_channels = data.shape[-1]
        out = []
        for c in range(n_channels):
            d = data[:, :, c].T if inverse else data[:, c]
            s = fstft(d, hop_length=H, window=win, center=False, **win_len_arg)
            s = np.expand_dims(s.T, 2-inverse)
            out.append(s)
        if len(out) == 1:
            return out[0]
        return np.concatenate(out, axis=2-inverse) 
Example 16
Project: steppy-toolkit   Author: minerva-ml   File: utils.py    License: MIT License 5 votes vote down vote up
def rle_from_binary(prediction):
    prediction = np.asfortranarray(prediction)
    return cocomask.encode(prediction) 
Example 17
Project: simnibs   Author: simnibs   File: pardiso.py    License: GNU General Public License v3.0 5 votes vote down vote up
def _check_b(self, b):
        if sp.isspmatrix(b):
            warnings.warn('Pardiso requires the right-hand side b'
                          'to be a dense array for maximum efficiency',
                          SparseEfficiencyWarning)
            b = b.todense()

        # pardiso expects fortran (column-major) order if b is a matrix
        if b.ndim == 2:
            b = np.asfortranarray(b)

        if b.shape[0] != self._A.shape[0]:
            raise ValueError("Dimension mismatch: Matrix A {} and array b "
                             "{}".format(self._A.shape, b.shape))

        if b.dtype != np.float64:
            if b.dtype in [np.float16, np.float32, np.int16, np.int32, np.int64]:
                warnings.warn("Array b's data type was converted from "
                              "{} to float64".format(str(b.dtype)), 
                              PardisoWarning)
                b = b.astype(np.float64)
            else:
                raise TypeError('Dtype {} for array b is '
                                'not supported'.format(str(b.dtype)))
        
        return b 
Example 18
Project: vehicle_counting_tensorflow   Author: ahmetozlu   File: coco_tools.py    License: MIT License 5 votes vote down vote up
def _RleCompress(masks):
  """Compresses mask using Run-length encoding provided by pycocotools.

  Args:
    masks: uint8 numpy array of shape [mask_height, mask_width] with values in
    {0, 1}.

  Returns:
    A pycocotools Run-length encoding of the mask.
  """
  return mask.encode(np.asfortranarray(masks)) 
Example 19
Project: seamseg   Author: mapillary   File: coco_ap.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def process_panoptic_prediction(panoptic_pred, num_stuff, idx, img_size, original_size):
    # Extract panoptic prediction
    msk_pred, cat_pred, obj_pred, iscrowd_pred = panoptic_pred

    bbx_pred = extract_boxes(msk_pred, cat_pred.numel())

    # Convert bbx and redo clamping
    bbx_pred[:, [0, 2]] = (bbx_pred[:, [0, 2]] / img_size[0] * original_size[0]).clamp(min=0, max=original_size[0])
    bbx_pred[:, [1, 3]] = (bbx_pred[:, [1, 3]] / img_size[1] * original_size[1]).clamp(min=0, max=original_size[1])
    bbx_pred_size = bbx_pred[:, 2:] - bbx_pred[:, :2]

    outs = []
    for i, (obj_i, cat_i, bbx_i, iscrowd_i, bbx_size_i) in enumerate(zip(
            obj_pred, cat_pred, bbx_pred, iscrowd_pred, bbx_pred_size)):
        if iscrowd_i.item() == 1 or cat_i.item() < num_stuff or cat_i.item() == 255:
            continue
        out = dict(image_id=idx, category_id=int(cat_i.item()), score=float(obj_i.item()))

        out["bbox"] = [
            float(bbx_i[1].item()),
            float(bbx_i[0].item()),
            float(bbx_size_i[1].item()),
            float(bbx_size_i[0].item()),
        ]

        segmentation = msk_pred == i
        segmentation = Image.fromarray(segmentation.numpy()).resize(original_size[::-1], Image.NEAREST)
        out["segmentation"] = mask_encode(np.asfortranarray(np.array(segmentation)))
        out["segmentation"]["counts"] = str(out["segmentation"]["counts"], "utf-8")

        outs.append(out)

    return outs 
Example 20
Project: recruit   Author: Frank-qlu   File: test_io.py    License: Apache License 2.0 5 votes vote down vote up
def check_roundtrips(self, a):
        self.roundtrip(a)
        self.roundtrip(a, file_on_disk=True)
        self.roundtrip(np.asfortranarray(a))
        self.roundtrip(np.asfortranarray(a), file_on_disk=True)
        if a.shape[0] > 1:
            # neither C nor Fortran contiguous for 2D arrays or more
            self.roundtrip(np.asfortranarray(a)[1:])
            self.roundtrip(np.asfortranarray(a)[1:], file_on_disk=True) 
Example 21
Project: recruit   Author: Frank-qlu   File: test_io.py    License: Apache License 2.0 5 votes vote down vote up
def test_mmap(self):
        a = np.array([[1, 2.5], [4, 7.3]])
        self.roundtrip(a, file_on_disk=True, load_kwds={'mmap_mode': 'r'})

        a = np.asfortranarray([[1, 2.5], [4, 7.3]])
        self.roundtrip(a, file_on_disk=True, load_kwds={'mmap_mode': 'r'}) 
Example 22
Project: recruit   Author: Frank-qlu   File: numeric.py    License: Apache License 2.0 5 votes vote down vote up
def ascontiguousarray(a, dtype=None):
    """
    Return a contiguous array (ndim >= 1) in memory (C order).

    Parameters
    ----------
    a : array_like
        Input array.
    dtype : str or dtype object, optional
        Data-type of returned array.

    Returns
    -------
    out : ndarray
        Contiguous array of same shape and content as `a`, with type `dtype`
        if specified.

    See Also
    --------
    asfortranarray : Convert input to an ndarray with column-major
                     memory order.
    require : Return an ndarray that satisfies requirements.
    ndarray.flags : Information about the memory layout of the array.

    Examples
    --------
    >>> x = np.arange(6).reshape(2,3)
    >>> np.ascontiguousarray(x, dtype=np.float32)
    array([[ 0.,  1.,  2.],
           [ 3.,  4.,  5.]], dtype=float32)
    >>> x.flags['C_CONTIGUOUS']
    True

    Note: This function returns an array with at least one-dimension (1-d) 
    so it will not preserve 0-d arrays.  

    """
    return array(a, dtype, copy=False, order='C', ndmin=1) 
Example 23
Project: recruit   Author: Frank-qlu   File: numeric.py    License: Apache License 2.0 5 votes vote down vote up
def asfortranarray(a, dtype=None):
    """
    Return an array (ndim >= 1) laid out in Fortran order in memory.

    Parameters
    ----------
    a : array_like
        Input array.
    dtype : str or dtype object, optional
        By default, the data-type is inferred from the input data.

    Returns
    -------
    out : ndarray
        The input `a` in Fortran, or column-major, order.

    See Also
    --------
    ascontiguousarray : Convert input to a contiguous (C order) array.
    asanyarray : Convert input to an ndarray with either row or
        column-major memory order.
    require : Return an ndarray that satisfies requirements.
    ndarray.flags : Information about the memory layout of the array.

    Examples
    --------
    >>> x = np.arange(6).reshape(2,3)
    >>> y = np.asfortranarray(x)
    >>> x.flags['F_CONTIGUOUS']
    False
    >>> y.flags['F_CONTIGUOUS']
    True

    Note: This function returns an array with at least one-dimension (1-d) 
    so it will not preserve 0-d arrays.  

    """
    return array(a, dtype, copy=False, order='F', ndmin=1) 
Example 24
Project: pygbm   Author: ogrisel   File: test_splitting.py    License: MIT License 5 votes vote down vote up
def test_histogram_split(n_bins):
    rng = np.random.RandomState(42)
    feature_idx = 0
    l2_regularization = 0
    min_hessian_to_split = 1e-3
    min_samples_leaf = 1
    min_gain_to_split = 0.
    X_binned = np.asfortranarray(
        rng.randint(0, n_bins, size=(int(1e4), 2)), dtype=np.uint8)
    binned_feature = X_binned.T[feature_idx]
    sample_indices = np.arange(binned_feature.shape[0], dtype=np.uint32)
    ordered_hessians = np.ones_like(binned_feature, dtype=np.float32)
    all_hessians = ordered_hessians

    for true_bin in range(1, n_bins - 1):
        for sign in [-1, 1]:
            ordered_gradients = np.full_like(binned_feature, sign,
                                             dtype=np.float32)
            ordered_gradients[binned_feature <= true_bin] *= -1
            all_gradients = ordered_gradients

            n_bins_per_feature = np.array([n_bins] * X_binned.shape[1],
                                          dtype=np.uint32)
            context = SplittingContext(X_binned,
                                       n_bins,
                                       n_bins_per_feature,
                                       all_gradients, all_hessians,
                                       l2_regularization,
                                       min_hessian_to_split,
                                       min_samples_leaf, min_gain_to_split)

            split_info, _ = _find_histogram_split(context, feature_idx,
                                                  sample_indices)

            assert split_info.bin_idx == true_bin
            assert split_info.gain >= 0
            assert split_info.feature_idx == feature_idx
            assert (split_info.n_samples_left + split_info.n_samples_right
                    == sample_indices.shape[0])
            # Constant hessian: 1. per sample.
            assert split_info.n_samples_left == split_info.hessian_left 
Example 25
Project: pygbm   Author: ogrisel   File: test_splitting.py    License: MIT License 5 votes vote down vote up
def test_min_gain_to_split():
    # Try to split a pure node (all gradients are equal, same for hessians)
    # with min_gain_to_split = 0 and make sure that the node is not split (best
    # possible gain = -1). Note: before the strict inequality comparison, this
    # test would fail because the node would be split with a gain of 0.
    rng = np.random.RandomState(42)
    feature_idx = 0
    l2_regularization = 0
    min_hessian_to_split = 0
    min_samples_leaf = 1
    min_gain_to_split = 0.
    n_bins = 255
    n_samples = 100
    X_binned = np.asfortranarray(
        rng.randint(0, n_bins, size=(n_samples, 2)), dtype=np.uint8)
    binned_feature = X_binned.T[feature_idx]
    sample_indices = np.arange(n_samples, dtype=np.uint32)
    all_hessians = np.ones_like(binned_feature, dtype=np.float32)
    all_gradients = np.ones_like(binned_feature, dtype=np.float32)

    n_bins_per_feature = np.array([n_bins] * X_binned.shape[1],
                                  dtype=np.uint32)
    context = SplittingContext(X_binned, n_bins, n_bins_per_feature,
                               all_gradients, all_hessians,
                               l2_regularization,
                               min_hessian_to_split,
                               min_samples_leaf, min_gain_to_split)

    split_info, _ = _find_histogram_split(context, feature_idx, sample_indices)
    assert split_info.gain == -1 
Example 26
Project: pygbm   Author: ogrisel   File: test_grower.py    License: MIT License 5 votes vote down vote up
def _make_training_data(n_bins=256, constant_hessian=True):
    rng = np.random.RandomState(42)
    n_samples = 10000

    # Generate some test data directly binned so as to test the grower code
    # independently of the binning logic.
    X_binned = rng.randint(0, n_bins - 1, size=(n_samples, 2), dtype=np.uint8)
    X_binned = np.asfortranarray(X_binned)

    def true_decision_function(input_features):
        """Ground truth decision function

        This is a very simple yet asymmetric decision tree. Therefore the
        grower code should have no trouble recovering the decision function
        from 10000 training samples.
        """
        if input_features[0] <= n_bins // 2:
            return -1
        else:
            if input_features[1] <= n_bins // 3:
                return -1
            else:
                return 1

    target = np.array([true_decision_function(x) for x in X_binned],
                      dtype=np.float32)

    # Assume a square loss applied to an initial model that always predicts 0
    # (hardcoded for this test):
    all_gradients = target
    if constant_hessian:
        all_hessians = np.ones(shape=1, dtype=np.float32)
    else:
        all_hessians = np.ones_like(all_gradients)
    return X_binned, all_gradients, all_hessians 
Example 27
Project: open-solution-salt-identification   Author: neptune-ai   File: utils.py    License: MIT License 5 votes vote down vote up
def rle_from_binary(prediction):
    prediction = np.asfortranarray(prediction)
    return cocomask.encode(prediction) 
Example 28
Project: dexplo   Author: dexplo   File: _date.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _create_df_all(self, arr, dtype):
        new_data = {}
        if dtype == self._dtype_acc:
            for old_dtype, old_data in self._df._data.items():
                if old_dtype == self._dtype_acc:
                    new_data[self._dtype_acc] = arr
                else:
                    new_data[old_dtype] = old_data.copy('F')
        else:
            new_data = {}
            add_loc = 0
            if dtype in self._data:
                add_loc = self._data[dtype].shape[1]
            for old_dtype, old_data in self._df._data.items():
                if dtype != self._dtype_acc:
                    new_data[old_dtype] = old_data.copy('F')

            if dtype in new_data:
                new_data[dtype] = np.asfortranarray(np.column_stack((new_data[dtype], arr)))
            else:
                new_data[dtype] = arr

            new_column_info = {}
            for col, old_dtype, loc, order in self._df._col_info_iter(with_order=True):  # type: str, str, int
                if old_dtype == self._dtype_acc:
                    new_column_info[col] = utils.Column(dtype, loc + add_loc, order)
                else:
                    new_column_info[col] = utils.Column(old_dtype, loc, order)

        new_column_info = self._df._copy_column_info()
        return self._df._construct_from_new(new_data, new_column_info, self._df._columns.copy()) 
Example 29
Project: dexplo   Author: dexplo   File: _date.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _create_df_multiple_dtypes(self, arr_new, columns, column_locs, columns_other, locs_other):
        new_data = {}
        dtype_new = arr_new.dtype.kind
        try:
            add_loc = self._df._data[dtype_new].shape[1]
        except KeyError:
            add_loc = 0
        for dtype, arr in self._df._data.items():
            if dtype == self._dtype_acc:
                new_data[self._dtype_acc] = arr[:, locs_other]
            elif dtype == dtype_new:
                new_data[dtype_new] = np.asfortranarray(np.column_stack((arr, arr_new)))
            else:
                new_data[dtype] = arr.copy('F')

        if dtype_new not in new_data:
            new_data[dtype_new] = arr_new

        new_column_info = {}
        for col, old_dtype, loc, order in self._df._col_info_iter(with_order=True):  # type: str, str, int, int
            if old_dtype != self._dtype_acc:
                new_column_info[col] = utils.Column(old_dtype, loc, order)

        # str columns that have changed type
        for i, (col, loc) in enumerate(zip(columns, column_locs)):
            order = self._df._column_info[col].order
            new_column_info[col] = utils.Column(dtype_new, add_loc + i, order)

        # those that stayed self._dtype_acc
        for i, col in enumerate(columns_other):
            order = self._df._column_info[col].order
            new_column_info[col] = utils.Column(self._dtype_acc, i, order)

        return self._df._construct_from_new(new_data, new_column_info, self._df._columns.copy()) 
Example 30
Project: dexplo   Author: dexplo   File: _strings.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _create_df_all(self, arr, dtype, str_reverse_map):
        new_data = {}
        if dtype == 'S':
            for old_dtype, old_data in self._df._data.items():
                if old_dtype == 'S':
                    new_data['S'] = arr
                else:
                    new_data[old_dtype] = old_data.copy('F')
        else:
            new_data = {}
            add_loc = 0
            if dtype in self._df._data:
                add_loc = self._df._data[dtype].shape[1]
            for old_dtype, old_data in self._df._data.items():
                if dtype != 'S':
                    new_data[old_dtype] = old_data.copy('F')

            if dtype in new_data:
                new_data[dtype] = np.asfortranarray(np.column_stack((new_data[dtype], arr)))
            else:
                new_data[dtype] = arr

            new_column_info = {}
            for col, old_dtype, loc, order in self._df._col_info_iter(with_order=True):  # type: str, str, int, int
                if old_dtype == 'S':
                    new_column_info[col] = utils.Column(dtype, loc + add_loc, order)
                else:
                    new_column_info[col] = utils.Column(old_dtype, loc, order)

        new_column_info = self._df._copy_column_info()
        return self._df._construct_from_new(new_data, new_column_info, self._df._columns.copy(), str_reverse_map)