diff --git a/README.md b/README.md index 144cadc2b..62d724348 100644 --- a/README.md +++ b/README.md @@ -132,7 +132,7 @@ s(accum) << v.reduce(op) ```python A = Matrix.new(dtype, num_rows, num_cols) # new_type B = A.dup() # dup -A = Matrix.from_values([row_indices], [col_indices], [values]) # build +A = Matrix.from_coo([row_indices], [col_indices], [values]) # build ``` ## New from delayed Delayed objects can be used to create a new object using `.new()` method @@ -145,7 +145,7 @@ size = v.size # size nrows = M.nrows # nrows ncols = M.ncols # ncols nvals = M.nvals # nvals -rindices, cindices, vals = M.to_values() # extractTuples +rindices, cindices, vals = M.to_coo() # extractTuples ``` ## Initialization There is a mechanism to initialize `graphblas` with a context prior to use. This allows for setting the backend to @@ -174,7 +174,7 @@ def force_odd_func(x): unary.register_new('force_odd', force_odd_func) -v = Vector.from_values([0, 1, 3], [1, 2, 3]) +v = Vector.from_coo([0, 1, 3], [1, 2, 3]) w = v.apply(unary.force_odd).new() w # indexes=[0, 1, 3], values=[1, 3, 3] ``` diff --git a/docs/getting_started/primer.rst b/docs/getting_started/primer.rst index 0b19e30ad..d95cde0cc 100644 --- a/docs/getting_started/primer.rst +++ b/docs/getting_started/primer.rst @@ -236,12 +236,12 @@ node 0. # Create the graph and starting vector start_node = 0 - G = Matrix.from_values( + G = Matrix.from_coo( [0, 0, 1, 1, 2], [1, 2, 2, 3, 3], [2.0, 5.0, 1.5, 4.25, 0.5], nrows=4, ncols=4) - v = Vector.from_values([start_node], [0.0], size=4) + v = Vector.from_coo([start_node], [0.0], size=4) # Compute SSSP while True: diff --git a/docs/user_guide/collections.rst b/docs/user_guide/collections.rst index 35b1af4a4..2ce759bf4 100644 --- a/docs/user_guide/collections.rst +++ b/docs/user_guide/collections.rst @@ -13,7 +13,7 @@ A **Matrix** is a 2-dimensional sparse array. .. code-block:: python - M = gb.Matrix.from_values( + M = gb.Matrix.from_coo( [3, 0, 3, 5, 6, 0, 6, 1, 6, 2, 4, 1], [0, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6], [3, 2, 3, 1, 5, 3, 7, 8, 3, 1, 7, 4], @@ -49,7 +49,7 @@ A **Vector** is a 1-dimensional sparse array. .. code-block:: python - v = gb.Vector.from_values([0, 3, 4, 6], [12.1, -5.4, 2.9, 2.2], size=8) + v = gb.Vector.from_coo([0, 3, 4, 6], [12.1, -5.4, 2.9, 2.2], size=8) Viewing the ``__repr__`` for ``v`` in a notebook will look like the following, although the name, dtype, and format might be different than what is shown here. @@ -142,22 +142,22 @@ The shape and dtype remain unchanged, but the collection will be fully sparse (i >>> M.nvals 0 -to_values -~~~~~~~~~ +to_coo +~~~~~~ -To go from a collection back to the index and values, ``.to_values()`` can be called. Numpy arrays +To go from a collection back to the index and values, ``.to_coo()`` can be called. Numpy arrays will be returned in a tuple. .. code-block:: python - >>> v = gb.Vector.from_values([0, 3, 5], [2.0, 3.1, 4.7]) - >>> idx, vals = v.to_values() + >>> v = gb.Vector.from_coo([0, 3, 5], [2.0, 3.1, 4.7]) + >>> idx, vals = v.to_coo() >>> idx array([0, 3, 5], dtype=uint64) >>> vals array([2.0, 3.1, 4.7]) -Note that Scalar does not have ``.to_values()``. Instead, the ``.value`` attribute will extract +Note that Scalar does not have ``.to_coo()``. Instead, the ``.value`` attribute will extract the underlying value or return ``None`` if the Scalar is empty. Empty Constructors diff --git a/docs/user_guide/io.rst b/docs/user_guide/io.rst index 625110144..d31932053 100644 --- a/docs/user_guide/io.rst +++ b/docs/user_guide/io.rst @@ -9,9 +9,9 @@ From/To Values Scalar has ``.from_value()`` and ``.value`` to get a Python scalar into and out of python-graphblas. -Matrix and Vector, instead, have a ``.from_values()`` and a ``.to_values()`` method. +Matrix and Vector, instead, have a ``.from_coo()`` and a ``.to_coo()`` method. -``.from_values()`` takes index(es) and values as either: +``.from_coo()`` takes index(es) and values as either: - Python lists - Numpy arrays @@ -21,12 +21,12 @@ If no dtype is provided, the data type is inferred from the values. If no size (Vector) or nrows/ncols (Matrix) are provided, the shape is inferred based on the largest index found for each dimension. -``.to_values()`` returns numpy arrays. Indexes are always returned as ``uint64``, while the values +``.to_coo()`` returns numpy arrays. Indexes are always returned as ``uint64``, while the values array will match the collection dtype. .. code-block:: python - v = gb.Vector.from_values([1, 3, 6], [2, 3, 4], float, size=10) + v = gb.Vector.from_coo([1, 3, 6], [2, 3, 4], float, size=10) .. csv-table:: :header: 0,1,2,3,4,5,6,7,8,9,10 @@ -35,7 +35,7 @@ array will match the collection dtype. .. code-block:: python - >>> idx, vals = v.to_values() + >>> idx, vals = v.to_coo() >>> idx array([1, 3, 6], dtype=uint64) >>> vals diff --git a/docs/user_guide/operations.rst b/docs/user_guide/operations.rst index c6407d278..1f1c19ed4 100644 --- a/docs/user_guide/operations.rst +++ b/docs/user_guide/operations.rst @@ -26,10 +26,10 @@ a Vector is treated as an nx1 column matrix. .. code-block:: python - A = gb.Matrix.from_values([0, 0, 1, 1, 2], [1, 2, 2, 3, 3], - [2., 5., 1.5, 4.25, 0.5], nrows=4, ncols=4) - B = gb.Matrix.from_values([0, 0, 1, 1, 2, 2, 3, 3], [1, 2, 0, 1, 1, 2, 0, 1], - [3., 2., 9., 6., 3., 1., 0., 5.]) + A = gb.Matrix.from_coo([0, 0, 1, 1, 2], [1, 2, 2, 3, 3], + [2., 5., 1.5, 4.25, 0.5], nrows=4, ncols=4) + B = gb.Matrix.from_coo([0, 0, 1, 1, 2, 2, 3, 3], [1, 2, 0, 1, 1, 2, 0, 1], + [3., 2., 9., 6., 3., 1., 0., 5.]) # These are equivalent C << A.mxm(B, op='min_plus') # method style @@ -66,9 +66,9 @@ a Vector is treated as an nx1 column matrix. .. code-block:: python - A = gb.Matrix.from_values([0, 0, 1, 1, 2], [1, 2, 2, 3, 3], - [2., 5., 1.5, 4.25, 0.5], nrows=4, ncols=4) - v = gb.Vector.from_values([0, 1, 3], [10., 20., 40.]) + A = gb.Matrix.from_coo([0, 0, 1, 1, 2], [1, 2, 2, 3, 3], + [2., 5., 1.5, 4.25, 0.5], nrows=4, ncols=4) + v = gb.Vector.from_coo([0, 1, 3], [10., 20., 40.]) # These are equivalent w << A.mxv(v, op='plus_times') # method style @@ -99,9 +99,9 @@ a Vector is treated as an nx1 column matrix. .. code-block:: python - v = gb.Vector.from_values([0, 1, 3], [10., 20., 40.]) - B = gb.Matrix.from_values([0, 0, 1, 1, 2, 2, 3, 3], [1, 2, 0, 1, 1, 2, 0, 1], - [3., 2., 9., 6., 3., 1., 0., 5.]) + v = gb.Vector.from_coo([0, 1, 3], [10., 20., 40.]) + B = gb.Matrix.from_coo([0, 0, 1, 1, 2, 2, 3, 3], [1, 2, 0, 1, 1, 2, 0, 1], + [3., 2., 9., 6., 3., 1., 0., 5.]) # These are equivalent u << v.vxm(B, op='plus_plus') # method style @@ -145,10 +145,10 @@ Example usage: .. code-block:: python - A = gb.Matrix.from_values([0, 0, 1, 1, 2], [1, 2, 0, 2, 1], - [2.0, 5.0, 1.5, 4.0, 0.5]) - B = gb.Matrix.from_values([0, 0, 1, 1, 2, 2], [1, 2, 0, 1, 1, 2], - [3., -2., 0., 6., 3., 1.]) + A = gb.Matrix.from_coo([0, 0, 1, 1, 2], [1, 2, 0, 2, 1], + [2.0, 5.0, 1.5, 4.0, 0.5]) + B = gb.Matrix.from_coo([0, 0, 1, 1, 2, 2], [1, 2, 0, 1, 1, 2], + [3., -2., 0., 6., 3., 1.]) # These are equivalent C << A.ewise_mult(B, op='min') # method style @@ -221,10 +221,10 @@ should be used with the functional syntax, ``left_default`` and ``right_default` .. code-block:: python - A = gb.Matrix.from_values([0, 0, 0, 1, 1, 1], [0, 1, 2, 0, 2], - [9.0, 2.0, 5.0, 1.5, 4.0], nrows=3) - B = gb.Matrix.from_values([0, 0, 0, 2, 2, 2], [0, 1, 2, 0, 1, 2], - [4., 0., -2., 6., 3., 1.]) + A = gb.Matrix.from_coo([0, 0, 0, 1, 1, 1], [0, 1, 2, 0, 2], + [9.0, 2.0, 5.0, 1.5, 4.0], nrows=3) + B = gb.Matrix.from_coo([0, 0, 0, 2, 2, 2], [0, 1, 2, 0, 1, 2], + [4., 0., -2., 6., 3., 1.]) # These are equivalent C << A.ewise_add(B, op='minus') # method style @@ -258,10 +258,10 @@ should be used with the functional syntax, ``left_default`` and ``right_default` .. code-block:: python - A = gb.Matrix.from_values([0, 0, 0, 1, 1, 1], [0, 1, 2, 0, 2], - [9.0, 2.0, 5.0, 1.5, 4.0], nrows=3) - B = gb.Matrix.from_values([0, 0, 0, 2, 2, 2], [0, 1, 2, 0, 1, 2], - [4., 0., -2., 6., 3., 1.]) + A = gb.Matrix.from_coo([0, 0, 0, 1, 1, 1], [0, 1, 2, 0, 2], + [9.0, 2.0, 5.0, 1.5, 4.0], nrows=3) + B = gb.Matrix.from_coo([0, 0, 0, 2, 2, 2], [0, 1, 2, 0, 1, 2], + [4., 0., -2., 6., 3., 1.]) # These are equivalent C << A.ewise_union(B, op='minus', left_default=0, right_default=0) # method style @@ -314,7 +314,7 @@ Vector Slice Example: .. code-block:: python - v = gb.Vector.from_values([0, 1, 3, 4, 6], [10., 2., 40., -5., 24.]) + v = gb.Vector.from_coo([0, 1, 3, 4, 6], [10., 2., 40., -5., 24.]) w << v[:4] @@ -334,8 +334,8 @@ Matrix List Example: .. code-block:: python - A = gb.Matrix.from_values([0, 0, 1, 1, 2, 2], [1, 2, 0, 1, 0, 2], - [2.0, 5.0, 1.5, 4.0, 0.5, -7.0]) + A = gb.Matrix.from_coo([0, 0, 1, 1, 2, 2], [1, 2, 0, 1, 0, 2], + [2.0, 5.0, 1.5, 4.0, 0.5, -7.0]) C << A[[0, 2], :] @@ -374,10 +374,10 @@ Matrix-Matrix Assignment Example: .. code-block:: python - A = gb.Matrix.from_values([0, 0, 1, 1, 2, 2], [1, 2, 0, 1, 0, 2], - [2.0, 5.0, 1.5, 4.0, 0.5, -7.0]) - B = gb.Matrix.from_values([0, 0, 1, 1], [0, 1, 0, 1] - [-99., -98., -97., -96.]) + A = gb.Matrix.from_coo([0, 0, 1, 1, 2, 2], [1, 2, 0, 1, 0, 2], + [2.0, 5.0, 1.5, 4.0, 0.5, -7.0]) + B = gb.Matrix.from_coo([0, 0, 1, 1], [0, 1, 0, 1] + [-99., -98., -97., -96.]) A[::2, ::2] << B @@ -408,9 +408,9 @@ Matrix-Vector Assignment Example: .. code-block:: python - A = gb.Matrix.from_values([0, 0, 1, 1, 2, 2], [1, 2, 0, 1, 0, 2], - [2.0, 5.0, 1.5, 4.0, 0.5, -7.0]) - v = gb.Vector.from_values([2], [-99.]) + A = gb.Matrix.from_coo([0, 0, 1, 1, 2, 2], [1, 2, 0, 1, 0, 2], + [2.0, 5.0, 1.5, 4.0, 0.5, -7.0]) + v = gb.Vector.from_coo([2], [-99.]) A[1, :] << v @@ -440,7 +440,7 @@ Vector-Scalar Assignment Example: .. code-block:: python - v = gb.Vector.from_values([0, 1, 3, 4, 6], [10, 2, 40, -5, 24]) + v = gb.Vector.from_coo([0, 1, 3, 4, 6], [10, 2, 40, -5, 24]) v[:4] << 99 @@ -472,7 +472,7 @@ function with the collection as the argument. .. code-block:: python - v = gb.Vector.from_values([0, 1, 3], [10., 20., 40.]) + v = gb.Vector.from_coo([0, 1, 3], [10., 20., 40.]) # These are equivalent w << v.apply(gb.unary.minv) @@ -494,7 +494,7 @@ function with the collection as the argument. .. code-block:: python - v = gb.Vector.from_values([0, 1, 3], [10., 20., 40.]) + v = gb.Vector.from_coo([0, 1, 3], [10., 20., 40.]) # These are equivalent w << v.apply(gb.indexunary.index) @@ -516,7 +516,7 @@ function with the collection as the argument. .. code-block:: python - v = gb.Vector.from_values([0, 1, 3], [10., 20., 40.]) + v = gb.Vector.from_coo([0, 1, 3], [10., 20., 40.]) # These are all equivalent w << v.apply('minus', right=15) @@ -546,8 +546,8 @@ Upper Triangle Example: .. code-block:: python - A = gb.Matrix.from_values([0, 0, 1, 1, 2, 2], [1, 2, 0, 2, 1, 2], - [2.0, 5.0, 1.5, 4.0, 0.5, -7.0]) + A = gb.Matrix.from_coo([0, 0, 1, 1, 2, 2], [1, 2, 0, 2, 1, 2], + [2.0, 5.0, 1.5, 4.0, 0.5, -7.0]) # These are equivalent C << A.select('triu') @@ -573,7 +573,7 @@ Select by Value Example: .. code-block:: python - v = gb.Vector.from_values([0, 1, 3, 4, 6], [10., 2., 40., -5., 24.]) + v = gb.Vector.from_coo([0, 1, 3, 4, 6], [10., 2., 40., -5., 24.]) # These are equivalent w << v.select('>=', 5) @@ -605,8 +605,8 @@ A monoid or aggregator is used to perform the reduction. .. code-block:: python - A = gb.Matrix.from_values([0, 0, 1, 1, 2, 2], [1, 3, 0, 1, 0, 1], - [2.0, 5.0, 1.5, 4.0, 0.5, -7.0]) + A = gb.Matrix.from_coo([0, 0, 1, 1, 2, 2], [1, 3, 0, 1, 0, 1], + [2.0, 5.0, 1.5, 4.0, 0.5, -7.0]) w << A.reduce_columnwise('times') @@ -628,8 +628,8 @@ A monoid or aggregator is used to perform the reduction. .. code-block:: python - A = gb.Matrix.from_values([0, 0, 1, 1, 2, 2], [1, 3, 0, 1, 0, 1], - [2.0, 5.0, 1.5, 4.0, 0.5, -7.0]) + A = gb.Matrix.from_coo([0, 0, 1, 1, 2, 2], [1, 3, 0, 1, 0, 1], + [2.0, 5.0, 1.5, 4.0, 0.5, -7.0]) s << A.reduce_scalar('max') @@ -651,7 +651,7 @@ A monoid or aggregator is used to perform the reduction. .. code-block:: python - v = gb.Vector.from_values([0, 1, 3, 4, 6], [10., 2., 40., -5., 24.]) + v = gb.Vector.from_coo([0, 1, 3, 4, 6], [10., 2., 40., -5., 24.]) # These are equivalent s << v.reduce('argmin') @@ -679,8 +679,8 @@ To force the transpose to be computed by itself, use it by itself as the right-h .. code-block:: python - A = gb.Matrix.from_values([0, 0, 1, 1, 2, 2], [1, 3, 0, 1, 0, 2], - [2.0, 5.0, 1.5, 4.0, 0.5, -7.0]) + A = gb.Matrix.from_coo([0, 0, 1, 1, 2, 2], [1, 3, 0, 1, 0, 2], + [2.0, 5.0, 1.5, 4.0, 0.5, -7.0]) C << A.T @@ -711,9 +711,9 @@ The Kronecker product uses a binary operator. .. code-block:: python - A = gb.Matrix.from_values([0, 0, 1], [0, 1, 0], [1., -2., 3.]) - B = gb.Matrix.from_values([0, 0, 1, 1, 2, 2], [1, 2, 0, 1, 0, 2], - [2.0, 5.0, 1.5, 4.0, 0.5, -7.0]) + A = gb.Matrix.from_coo([0, 0, 1], [0, 1, 0], [1., -2., 3.]) + B = gb.Matrix.from_coo([0, 0, 1, 1, 2, 2], [1, 2, 0, 1, 0, 2], + [2.0, 5.0, 1.5, 4.0, 0.5, -7.0]) C << A.kronecker(B, 'times') diff --git a/docs/user_guide/recorder.rst b/docs/user_guide/recorder.rst index e37be3ad6..ee6d2bbb9 100644 --- a/docs/user_guide/recorder.rst +++ b/docs/user_guide/recorder.rst @@ -21,12 +21,12 @@ Instead, only the calls from the last iteration will be returned. # Create the graph and starting vector start_node = 0 - G = Matrix.from_values( + G = Matrix.from_coo( [0, 0, 1, 1, 2], [1, 2, 2, 3, 3], [2.0, 5.0, 1.5, 4.25, 0.5], nrows=4, ncols=4) - v = Vector.from_values([start_node], [0.0], size=4) + v = Vector.from_coo([start_node], [0.0], size=4) # Compute SSSP while True: diff --git a/docs/user_guide/udf.rst b/docs/user_guide/udf.rst index 37923b4bd..8ee3cefbf 100644 --- a/docs/user_guide/udf.rst +++ b/docs/user_guide/udf.rst @@ -24,7 +24,7 @@ Example user-defined UnaryOp: UnaryOp.register_new('force_odd', force_odd_func) - v = Vector.from_values([0, 1, 3, 4, 5], [1, 2, 3, 8, 14]) + v = Vector.from_coo([0, 1, 3, 4, 5], [1, 2, 3, 8, 14]) w = v.apply(unary.force_odd).new() .. csv-table:: w diff --git a/graphblas/core/agg.py b/graphblas/core/agg.py index f4cd02e71..d8ce9a168 100644 --- a/graphblas/core/agg.py +++ b/graphblas/core/agg.py @@ -557,10 +557,10 @@ def _first_last(agg, updater, expr, *, in_composite, semiring_): init = expr._new_vector(bool, size=A._ncols) init[...] = False # O(1) dense vector in SuiteSparse 5 step1 = semiring_(A @ init).new() - Is, Js = step1.to_values() + Is, Js = step1.to_coo() Matrix_ = type(expr._new_matrix(bool)) - P = Matrix_.from_values(Js, Is, 1, nrows=A._ncols, ncols=A._nrows) + P = Matrix_.from_coo(Js, Is, 1, nrows=A._ncols, ncols=A._nrows) mask = step1.diag() result = semiring.any_first(A @ P).new(mask=mask.S).diag() diff --git a/graphblas/core/automethods.py b/graphblas/core/automethods.py index 049515da2..7ef2f1b75 100644 --- a/graphblas/core/automethods.py +++ b/graphblas/core/automethods.py @@ -392,6 +392,7 @@ def _main(): "reposition", "select", "ss", + "to_coo", "to_values", } vector = { @@ -411,7 +412,6 @@ def _main(): "reduce_columnwise", "reduce_rowwise", "reduce_scalar", - "to_coo", "to_csc", "to_csr", "to_dcsc", diff --git a/graphblas/core/base.py b/graphblas/core/base.py index 8a9f9223c..ce959b392 100644 --- a/graphblas/core/base.py +++ b/graphblas/core/base.py @@ -502,7 +502,7 @@ def _name_html(self): def __array__(self, dtype=None): raise TypeError( f"{type(self).__name__} can't be directly converted to a numpy array; " - f"perhaps use `{self.name}.to_values()` method instead." + f"perhaps use `{self.name}.to_coo()` method instead." ) diff --git a/graphblas/core/formatting.py b/graphblas/core/formatting.py index 306bb32c4..17aa89511 100644 --- a/graphblas/core/formatting.py +++ b/graphblas/core/formatting.py @@ -162,7 +162,7 @@ def _update_matrix_dataframe(df, matrix, rows, row_offset, columns, column_offse submatrix(matrix.V)[rows, columns] = 0 if mask.complement else 1 if row_offset is not None or column_offset is not None: submatrix = submatrix[row_offset:, column_offset:].new(name="") - rows, cols, vals = submatrix.to_values() + rows, cols, vals = submatrix.to_coo() np_type = submatrix.dtype.np_type if submatrix.dtype._is_udt and np_type.subdtype is not None: vals = vals.tolist() @@ -200,7 +200,7 @@ def _update_vector_dataframe(df, vector, columns, column_offset, *, mask=None): subvector(vector.V)[columns] = 0 if mask.complement else 1 if column_offset is not None: subvector = subvector[column_offset:].new(name="") - cols, vals = subvector.to_values() + cols, vals = subvector.to_coo() np_type = subvector.dtype.np_type if subvector.dtype._is_udt and np_type.subdtype is not None: vals = vals.tolist() diff --git a/graphblas/core/infix.py b/graphblas/core/infix.py index aa030ec36..55636d65d 100644 --- a/graphblas/core/infix.py +++ b/graphblas/core/infix.py @@ -96,6 +96,7 @@ def shape(self): select = wrapdoc(Vector.select)(property(automethods.select)) if backend == "suitesparse": ss = wrapdoc(Vector.ss)(property(automethods.ss)) + to_coo = wrapdoc(Vector.to_coo)(property(automethods.to_coo)) to_dict = wrapdoc(Vector.to_dict)(property(automethods.to_dict)) to_pygraphblas = wrapdoc(Vector.to_pygraphblas)(property(automethods.to_pygraphblas)) to_values = wrapdoc(Vector.to_values)(property(automethods.to_values)) diff --git a/graphblas/core/matrix.py b/graphblas/core/matrix.py index 65759b26d..be50146b0 100644 --- a/graphblas/core/matrix.py +++ b/graphblas/core/matrix.py @@ -179,7 +179,7 @@ def __reduce__(self): if backend == "suitesparse": pieces = self.ss.export(raw=True) else: - rows, cols, vals = self.to_values(sort=False) + rows, cols, vals = self.to_coo(sort=False) pieces = (rows, cols, vals, self.dtype, self._nrows, self._ncols) return self._deserialize, (pieces, self.name) @@ -188,7 +188,7 @@ def _deserialize(pieces, name): if backend == "suitesparse": return Matrix.ss.import_any(name=name, **pieces) rows, cols, vals, dtype, nrows, ncols = pieces - return Matrix.from_values(rows, cols, vals, dtype, nrows=nrows, ncols=ncols, name=name) + return Matrix.from_coo(rows, cols, vals, dtype, nrows=nrows, ncols=ncols, name=name) @property def S(self): @@ -267,7 +267,7 @@ def __contains__(self, index): def __iter__(self): """Iterate over (row, col) indices which are present in the matrix.""" - rows, columns, _ = self.to_values(values=False) + rows, columns, _ = self.to_coo(values=False) return zip(rows.flat, columns.flat) def __sizeof__(self): @@ -429,6 +429,41 @@ def to_values(self, dtype=None, *, rows=True, columns=True, values=True, sort=Tr """Extract the indices and values as a 3-tuple of numpy arrays corresponding to the COO format of the Matrix. + .. deprecated:: 2022.11.0 + `Matrix.to_values` will be removed in a future release. + Use `Matrix.to_coo` instead. Will be removed in version 2023.9.0 or later + + Parameters + ---------- + dtype : + Requested dtype for the output values array. + rows : bool, default=True + Whether to return rows; will return `None` for rows if `False` + columns :bool, default=True + Whether to return columns; will return `None` for columns if `False` + values : bool, default=True + Whether to return values; will return `None` for values if `False` + sort : bool, default=True + Whether to require sorted indices. + If internally stored rowwise, the sorting will be first by rows, then by column. + If internally stored columnwise, the sorting will be first by column, then by row. + + Returns + ------- + np.ndarray[dtype=uint64] : Rows + np.ndarray[dtype=uint64] : Columns + np.ndarray : Values + """ + warnings.warn( + "`Matrix.to_values(...)` is deprecated; please use `Matrix.to_coo(...)` instead.", + DeprecationWarning, + ) + return self.to_coo(dtype, rows=rows, columns=columns, values=values, sort=sort) + + def to_coo(self, dtype=None, *, rows=True, columns=True, values=True, sort=True): + """Extract the indices and values as a 3-tuple of numpy arrays + corresponding to the COO format of the Matrix. + Parameters ---------- dtype : @@ -497,9 +532,9 @@ def build(self, rows, columns, values, *, dup_op=None, clear=False, nrows=None, """Rarely used method to insert values into an existing Matrix. The typical use case is to create a new Matrix and insert values - at the same time using :meth:`from_values`. + at the same time using :meth:`from_coo`. - All the arguments are used identically in :meth:`from_values`, except for `clear`, which + All the arguments are used identically in :meth:`from_coo`, except for `clear`, which indicates whether to clear the Matrix prior to adding the new values. """ # TODO: accept `dtype` keyword to match the dtype of `values`? @@ -686,6 +721,60 @@ def from_values( ): """Create a new Matrix from row and column indices and values. + .. deprecated:: 2022.11.0 + `Matrix.from_values` will be removed in a future release. + Use `Matrix.from_coo` instead. Will be removed in version 2023.9.0 or later + + Parameters + ---------- + rows : list or np.ndarray + Row indices. + columns : list or np.ndarray + Column indices. + values : list or np.ndarray or scalar + List of values. If a scalar is provided, all values will be set to this single value. + dtype : + Data type of the Matrix. If not provided, the values will be inspected + to choose an appropriate dtype. + nrows : int, optional + Number of rows in the Matrix. If not provided, ``nrows`` is computed + from the maximum row index found in ``rows``. + ncols : int, optional + Number of columns in the Matrix. If not provided, ``ncols`` is computed + from the maximum column index found in ``columns``. + dup_op : :class:`~graphblas.core.operator.BinaryOp`, optional + Function used to combine values if duplicate indices are found. + Leaving ``dup_op=None`` will raise an error if duplicates are found. + name : str, optional + Name to give the Matrix. + + Returns + ------- + Matrix + """ + warnings.warn( + "`Matrix.from_values(...)` is deprecated; please use `Matrix.from_coo(...)` instead.", + DeprecationWarning, + ) + return cls.from_coo( + rows, columns, values, dtype, nrows=nrows, ncols=ncols, dup_op=dup_op, name=name + ) + + @classmethod + def from_coo( + cls, + rows, + columns, + values, + dtype=None, + *, + nrows=None, + ncols=None, + dup_op=None, + name=None, + ): + """Create a new Matrix from row and column indices and values. + Parameters ---------- rows : list or np.ndarray @@ -866,9 +955,9 @@ def from_csr( See Also -------- + from_coo from_csc from_dcsr - from_values Matrix.ss.import_csr io.from_scipy_sparse """ @@ -912,9 +1001,9 @@ def from_csc( See Also -------- + from_coo from_csr from_dcsc - from_values Matrix.ss.import_csc io.from_scipy_sparse """ @@ -971,9 +1060,9 @@ def from_dcsr( See Also -------- + from_coo from_csr from_dcsc - from_values Matrix.ss.import_hypercsr io.from_scipy_sparse """ @@ -1054,9 +1143,9 @@ def from_dcsc( See Also -------- + from_coo from_csc from_dcsr - from_values Matrix.ss.import_hypercsc io.from_scipy_sparse """ @@ -1110,7 +1199,7 @@ def _from_dense(cls, values, dtype=None, *, name=None): values = values.reshape([nrows * ncols] + rest) else: values = values.ravel() - return cls.from_values(rows, cols, values, dtype, nrows=nrows, ncols=ncols, name=name) + return cls.from_coo(rows, cols, values, dtype, nrows=nrows, ncols=ncols, name=name) @classmethod def from_dicts( @@ -1239,9 +1328,9 @@ def to_csr(self, dtype=None): See Also -------- + to_coo to_csc to_dcsr - to_values Matrix.ss.export io.to_scipy_sparse """ @@ -1264,9 +1353,9 @@ def to_csc(self, dtype=None): See Also -------- + to_coo to_csr to_dcsc - to_values Matrix.ss.export io.to_scipy_sparse """ @@ -1292,9 +1381,9 @@ def to_dcsr(self, dtype=None): See Also -------- + to_coo to_csr to_dcsc - to_values Matrix.ss.export io.to_scipy_sparse """ @@ -1338,9 +1427,9 @@ def to_dcsc(self, dtype=None): See Also -------- + to_coo to_csc to_dcsr - to_values Matrix.ss.export io.to_scipy_sparse """ @@ -1411,9 +1500,6 @@ def to_dicts(self, order="rowwise"): # ) # } - from_coo = from_values # Alias - to_coo = to_values # Alias - @property def _carg(self): return self.gb_obj[0] @@ -2615,7 +2701,7 @@ def _prep_for_assign(self, resolved_indexes, value, mask=None, is_submask=False, ) else: # TODO: GraphBLAS needs a way to import or assign dense - vals = Vector.from_values( + vals = Vector.from_coo( np.arange(shape[0]), values, dtype, size=shape[0] ) except Exception: @@ -3179,6 +3265,13 @@ def gb_obj(self): def dtype(self): return self._matrix.dtype + @wrapdoc(Matrix.to_coo) + def to_coo(self, dtype=None, *, rows=True, columns=True, values=True, sort=True): + rows, cols, vals = self._matrix.to_coo( + dtype, rows=rows, columns=columns, values=values, sort=sort + ) + return cols, rows, vals + @wrapdoc(Matrix.to_values) def to_values(self, dtype=None, *, rows=True, columns=True, values=True, sort=True): rows, cols, vals = self._matrix.to_values( @@ -3223,8 +3316,6 @@ def to_dicts(self, order="rowwise"): order = "columnwise" if get_order(order) == "rowwise" else "rowwise" return self._matrix.to_dicts(order) - to_coo = to_values # Alias - # Properties nrows = Matrix.ncols ncols = Matrix.nrows diff --git a/graphblas/core/ss/matrix.py b/graphblas/core/ss/matrix.py index 555e7889a..ce59caade 100644 --- a/graphblas/core/ss/matrix.py +++ b/graphblas/core/ss/matrix.py @@ -35,7 +35,7 @@ def head(matrix, n=10, dtype=None, *, sort=False): - """Like ``matrix.to_values()``, but only returns the first n elements. + """Like ``matrix.to_coo()``, but only returns the first n elements. If sort is True, then the results will be sorted as appropriate for the internal format, otherwise the order of the result is not guaranteed. Specifically, row-oriented formats @@ -393,7 +393,7 @@ def build_scalar(self, rows, columns, value): See Also -------- Matrix.build - Matrix.from_values + Matrix.from_coo """ rows = ints_to_numpy_buffer(rows, np.uint64, name="row indices") columns = ints_to_numpy_buffer(columns, np.uint64, name="column indices") @@ -589,7 +589,7 @@ def export(self, format=None, *, sort=False, give_ownership=False, raw=False): See Also -------- - Matrix.to_values + Matrix.to_coo Matrix.ss.import_any Return values @@ -772,7 +772,7 @@ def _export(self, format=None, *, sort=False, give_ownership=False, raw=False, m # the values are sorted or not. parent.wait() if self.is_iso: - # Should we expose a way to do `to_values` without values? + # Should we expose a way to do `to_coo` without values? # Passing NULL for values is SuiteSparse-specific. nvals = parent._nvals rows = _CArray(size=nvals, name="&rows_array") @@ -794,7 +794,7 @@ def _export(self, format=None, *, sort=False, give_ownership=False, raw=False, m "is_iso": True, } else: - rows, columns, values = parent.to_values() + rows, columns, values = parent.to_coo() rv = { "format": "coo", "nrows": nrows, @@ -3076,7 +3076,7 @@ def import_any( See Also -------- - Matrix.from_values + Matrix.from_coo Matrix.ss.export Matrix.ss.import_csr Matrix.ss.import_csc diff --git a/graphblas/core/ss/vector.py b/graphblas/core/ss/vector.py index 39cb47545..94d112aca 100644 --- a/graphblas/core/ss/vector.py +++ b/graphblas/core/ss/vector.py @@ -32,13 +32,13 @@ def head(vector, n=10, dtype=None, *, sort=False): - """Like ``vector.to_values()``, but only returns the first n elements. + """Like ``vector.to_coo()``, but only returns the first n elements. If sort is True, then the results will be sorted by index, otherwise the order of the result is not guaranteed. Formats full and bitmap should always return in sorted order. """ if vector._nvals <= n: - return vector.to_values(dtype, sort=sort) + return vector.to_coo(dtype, sort=sort) if sort: vector.wait() if dtype is None: @@ -288,7 +288,7 @@ def build_scalar(self, indices, value): See Also -------- Vector.build - Vector.from_values + Vector.from_coo """ indices = ints_to_numpy_buffer(indices, np.uint64, name="indices") scalar = _as_scalar(value, self._parent.dtype, is_cscalar=False) # pragma: is_grbscalar @@ -455,7 +455,7 @@ def export(self, format=None, *, sort=False, give_ownership=False, raw=False): See Also -------- - Vector.to_values + Vector.to_coo Vector.ss.import_any Return values @@ -660,7 +660,7 @@ def import_any( See Also -------- - Vector.from_values + Vector.from_coo Vector.ss.export Vector.ss.import_sparse Vector.ss.import_bitmap diff --git a/graphblas/core/vector.py b/graphblas/core/vector.py index 94fad2484..bc2d7c9fe 100644 --- a/graphblas/core/vector.py +++ b/graphblas/core/vector.py @@ -174,7 +174,7 @@ def __reduce__(self): if backend == "suitesparse": pieces = self.ss.export(raw=True) else: - indices, values = self.to_values(sort=False) + indices, values = self.to_coo(sort=False) pieces = (indices, values, self.dtype, self._size) return self._deserialize, (pieces, self.name) @@ -183,7 +183,7 @@ def _deserialize(pieces, name): if backend == "suitesparse": return Vector.ss.import_any(name=name, **pieces) indices, values, dtype, size = pieces - return Vector.from_values(indices, values, dtype, size=size, name=name) + return Vector.from_coo(indices, values, dtype, size=size, name=name) @property def S(self): @@ -262,7 +262,7 @@ def __contains__(self, index): def __iter__(self): """Iterate over indices which are present in the vector.""" - indices, _ = self.to_values(values=False) + indices, _ = self.to_coo(values=False) return indices.flat def __sizeof__(self): @@ -396,6 +396,35 @@ def resize(self, size): def to_values(self, dtype=None, *, indices=True, values=True, sort=True): """Extract the indices and values as a 2-tuple of numpy arrays. + .. deprecated:: 2022.11.0 + `Vector.to_values` will be removed in a future release. + Use `Vector.to_coo` instead. Will be removed in version 2023.9.0 or later + + Parameters + ---------- + dtype : + Requested dtype for the output values array. + indices :bool, default=True + Whether to return indices; will return `None` for indices if `False` + values : bool, default=True + Whether to return values; will return `None` for values if `False` + sort : bool, default=True + Whether to require sorted indices. + + Returns + ------- + np.ndarray[dtype=uint64] : Indices + np.ndarray : Values + """ + warnings.warn( + "`Vector.to_values(...)` is deprecated; please use `Vector.to_coo(...)` instead.", + DeprecationWarning, + ) + return self.to_coo(dtype, indices=indices, values=values, sort=sort) + + def to_coo(self, dtype=None, *, indices=True, values=True, sort=True): + """Extract the indices and values as a 2-tuple of numpy arrays. + Parameters ---------- dtype : @@ -449,9 +478,9 @@ def to_values(self, dtype=None, *, indices=True, values=True, sort=True): def build(self, indices, values, *, dup_op=None, clear=False, size=None): """Rarely used method to insert values into an existing Vector. The typical use case - is to create a new Vector and insert values at the same time using :meth:`from_values`. + is to create a new Vector and insert values at the same time using :meth:`from_coo`. - All the arguments are used identically in :meth:`from_values`, except for `clear`, which + All the arguments are used identically in :meth:`from_coo`, except for `clear`, which indicates whether to clear the Vector prior to adding the new values. """ # TODO: accept `dtype` keyword to match the dtype of `values`? @@ -596,6 +625,42 @@ def new(cls, dtype, size=0, *, name=None): def from_values(cls, indices, values, dtype=None, *, size=None, dup_op=None, name=None): """Create a new Vector from indices and values. + .. deprecated:: 2022.11.0 + `Vector.from_values` will be removed in a future release. + Use `Vector.from_coo` instead. Will be removed in version 2023.9.0 or later + + Parameters + ---------- + indices : list or np.ndarray + Vector indices. + values : list or np.ndarray or scalar + List of values. If a scalar is provided, all values will be set to this single value. + dtype : + Data type of the Vector. If not provided, the values will be inspected + to choose an appropriate dtype. + size : int, optional + Size of the Vector. If not provided, ``size`` is computed from + the maximum index found in ``indices``. + dup_op : BinaryOp, optional + Function used to combine values if duplicate indices are found. + Leaving ``dup_op=None`` will raise an error if duplicates are found. + name : str, optional + Name to give the Vector. + + Returns + ------- + Vector + """ + warnings.warn( + "`Vector.from_values(...)` is deprecated; please use `Vector.from_coo(...)` instead.", + DeprecationWarning, + ) + return cls.from_coo(indices, values, dtype, size=size, dup_op=dup_op, name=name) + + @classmethod + def from_coo(cls, indices, values, dtype=None, *, size=None, dup_op=None, name=None): + """Create a new Vector from indices and values. + Parameters ---------- indices : list or np.ndarray @@ -1533,7 +1598,7 @@ def _prep_for_assign(self, resolved_indexes, value, mask=None, is_submask=False, ) else: # TODO: GraphBLAS needs a way to import or assign dense - vals = Vector.from_values( + vals = Vector.from_coo( np.arange(shape[0]), values, dtype, size=shape[0] ) except Exception: @@ -1699,7 +1764,7 @@ def from_dict(cls, d, dtype=None, *, size=None, name=None): values = np.fromiter(d.values(), dtype.np_type) if size is None and indices.size == 0: size = 0 - return cls.from_values(indices, values, dtype, size=size, name=name) + return cls.from_coo(indices, values, dtype, size=size, name=name) def to_dict(self): """Return Vector as a dict in the form ``{index: val}`` @@ -1708,7 +1773,7 @@ def to_dict(self): ------- dict """ - indices, values = self.to_values(sort=False) + indices, values = self.to_coo(sort=False) return dict(zip(indices.tolist(), values.tolist())) @@ -1826,6 +1891,7 @@ def shape(self): select = wrapdoc(Vector.select)(property(automethods.select)) if backend == "suitesparse": ss = wrapdoc(Vector.ss)(property(automethods.ss)) + to_coo = wrapdoc(Vector.to_coo)(property(automethods.to_coo)) to_dict = wrapdoc(Vector.to_dict)(property(automethods.to_dict)) to_pygraphblas = wrapdoc(Vector.to_pygraphblas)(property(automethods.to_pygraphblas)) to_values = wrapdoc(Vector.to_values)(property(automethods.to_values)) @@ -1901,6 +1967,7 @@ def shape(self): select = wrapdoc(Vector.select)(property(automethods.select)) if backend == "suitesparse": ss = wrapdoc(Vector.ss)(property(automethods.ss)) + to_coo = wrapdoc(Vector.to_coo)(property(automethods.to_coo)) to_dict = wrapdoc(Vector.to_dict)(property(automethods.to_dict)) to_pygraphblas = wrapdoc(Vector.to_pygraphblas)(property(automethods.to_pygraphblas)) to_values = wrapdoc(Vector.to_values)(property(automethods.to_values)) diff --git a/graphblas/io.py b/graphblas/io.py index 84f248bef..fbc1eceee 100644 --- a/graphblas/io.py +++ b/graphblas/io.py @@ -90,7 +90,7 @@ def from_numpy(m): A = csr_array(m) _, size = A.shape dtype = _lookup_dtype(m.dtype) - return _Vector.from_values(A.indices, A.data, size=size, dtype=dtype) + return _Vector.from_coo(A.indices, A.data, size=size, dtype=dtype) A = coo_array(m) return from_scipy_sparse(A) @@ -104,7 +104,7 @@ def from_scipy_sparse_matrix(m, *, dup_op=None, name=None): A = m.tocoo() nrows, ncols = A.shape dtype = _lookup_dtype(m.dtype) - return _Matrix.from_values( + return _Matrix.from_coo( A.row, A.col, A.data, nrows=nrows, ncols=ncols, dtype=dtype, dup_op=dup_op, name=name ) @@ -163,7 +163,7 @@ def from_scipy_sparse(A, *, dup_op=None, name=None): return _Matrix.from_csc(A.indptr, A.indices, A.data, nrows=nrows, name=name) if A.format != "coo": A = A.tocoo() - return _Matrix.from_values( + return _Matrix.from_coo( A.row, A.col, A.data, nrows=nrows, ncols=ncols, dtype=dtype, dup_op=dup_op, name=name ) @@ -197,7 +197,7 @@ def from_awkward(A, *, name=None): if len(shape) == 1: if format != "vec": raise ValueError(f"Invalid format for Vector: {format}") - return _Vector.from_values( + return _Vector.from_coo( A.indices.layout.data, A.values.layout.data, size=shape[0], name=name ) nrows, ncols = shape @@ -238,7 +238,7 @@ def to_networkx(m, edge_attribute="weight"): """ import networkx as nx - rows, cols, vals = m.to_values() + rows, cols, vals = m.to_coo() rows = rows.tolist() cols = cols.tolist() G = nx.DiGraph() @@ -285,14 +285,14 @@ def to_scipy_sparse_matrix(m, format="csr"): # pragma: no cover (deprecated) ) format = format.lower() if _output_type(m) is _Vector: - indices, data = m.to_values() + indices, data = m.to_coo() if format == "csc": return ss.csc_matrix((data, indices, [0, len(data)]), shape=(m._size, 1)) rv = ss.csr_matrix((data, indices, [0, len(data)]), shape=(1, m._size)) if format == "csr": return rv else: - rows, cols, data = m.to_values() + rows, cols, data = m.to_coo() rv = ss.coo_matrix((data, (rows, cols)), shape=m.shape) if format == "coo": return rv @@ -322,7 +322,7 @@ def to_scipy_sparse(A, format="csr"): if format not in {"bsr", "csr", "csc", "coo", "lil", "dia", "dok"}: raise ValueError(f"Invalid format: {format}") if _output_type(A) is _Vector: - indices, data = A.to_values() + indices, data = A.to_coo() if format == "csc": return ss.csc_array((data, indices, [0, len(data)]), shape=(A._size, 1)) rv = ss.csr_array((data, indices, [0, len(data)]), shape=(1, A._size)) @@ -351,7 +351,7 @@ def to_scipy_sparse(A, format="csr"): indptr, rows, vals = A.to_csc() return ss.csc_array((vals, rows, indptr), shape=A.shape) else: - rows, cols, data = A.to_values() + rows, cols, data = A.to_coo() rv = ss.coo_array((data, (rows, cols)), shape=A.shape) if format == "coo": return rv @@ -397,7 +397,7 @@ def to_awkward(A, format=None): if format != "vec": raise ValueError(f"Invalid format for Vector: {format}") size = A.nvals - indices, values = A.to_values() + indices, values = A.to_coo() form = RecordForm( contents=[ NumpyForm(A.dtype.numba_type.name, form_key="node1"), @@ -501,7 +501,7 @@ def mmread(source, *, dup_op=None, name=None): array = mmread(source) if isspmatrix_coo(array): nrows, ncols = array.shape - return _Matrix.from_values( + return _Matrix.from_coo( array.row, array.col, array.data, nrows=nrows, ncols=ncols, dup_op=dup_op, name=name ) if _backend == "suitesparse": diff --git a/graphblas/tests/test_formatting.py b/graphblas/tests/test_formatting.py index cfeb5f873..82212d895 100644 --- a/graphblas/tests/test_formatting.py +++ b/graphblas/tests/test_formatting.py @@ -85,17 +85,17 @@ def html_printer(x, name="", indent=4): @pytest.fixture def A(): - return Matrix.from_values([0, 0, 0], [0, 2, 4], [0, 1, 2], nrows=1, ncols=5, name="A_1") + return Matrix.from_coo([0, 0, 0], [0, 2, 4], [0, 1, 2], nrows=1, ncols=5, name="A_1") @pytest.fixture def B(): - return Matrix.from_values([0, 2, 4], [0, 0, 0], [10, 20, 30], nrows=5, ncols=1, name="B_1") + return Matrix.from_coo([0, 2, 4], [0, 0, 0], [10, 20, 30], nrows=5, ncols=1, name="B_1") @pytest.fixture def C(): - return Matrix.from_values( + return Matrix.from_coo( [0, 9, 60, 69, 0, 9, 60, 69], [4, 4, 4, 4, 72, 72, 72, 72], [0, 2, 3, 4, 5, 6, 7, 8], @@ -107,7 +107,7 @@ def C(): @pytest.fixture def D(): - return Matrix.from_values( + return Matrix.from_coo( [0, 9, 60, 69], [4, 4, 4, 4], [True, False, True, False], @@ -118,12 +118,12 @@ def D(): @pytest.fixture def v(): - return Vector.from_values([0, 2, 4], [0.0, 1.1, 2.2], name="v") + return Vector.from_coo([0, 2, 4], [0.0, 1.1, 2.2], name="v") @pytest.fixture def w(): - return Vector.from_values([0, 5, 64, 69], [1, 2, 3, 4], size=77, name="w") + return Vector.from_coo([0, 5, 64, 69], [1, 2, 3, 4], size=77, name="w") @pytest.fixture @@ -3400,7 +3400,7 @@ def test_vector_huge_html(): @pytest.mark.skipif("not pd") def test_sparse_vector_repr(): - v = Vector.from_values([100 * i for i in range(100)], [10 * i for i in range(100)], name="v") + v = Vector.from_coo([100 * i for i in range(100)], [10 * i for i in range(100)], name="v") repr_printer(v, "v") assert repr(v) == ( '"v" nvals size dtype format\n' @@ -3554,7 +3554,7 @@ def test_sparse_vector_repr(): @pytest.mark.skipif("not pd") def test_sparse_matrix_repr(): - A = Matrix.from_values( + A = Matrix.from_coo( [100 * i for i in range(100)], [10 * i for i in range(100)], list(range(100)), name="A" ) repr_printer(A, "A") @@ -4471,7 +4471,7 @@ def test_autocompute_html(A, B, v): @pytest.mark.skipif("not pd") def test_display_nan(): - v = Vector.from_values([0, 1], [1.0, np.nan], size=3, name="v") + v = Vector.from_coo([0, 1], [1.0, np.nan], size=3, name="v") repr_printer(v, "v") assert repr(v) == ( '"v" nvals size dtype format\n' @@ -4535,7 +4535,7 @@ def test_display_nan(): "\n" "" ) - A = Matrix.from_values([0, 0], [0, 1], [1.0, np.nan], ncols=3, nrows=2, name="A") + A = Matrix.from_coo([0, 0], [0, 1], [1.0, np.nan], ncols=3, nrows=2, name="A") repr_printer(A, "A") assert repr(A) == ( '"A" nvals nrows ncols dtype format\n' diff --git a/graphblas/tests/test_infix.py b/graphblas/tests/test_infix.py index c6b64b1e3..d391cec2c 100644 --- a/graphblas/tests/test_infix.py +++ b/graphblas/tests/test_infix.py @@ -10,22 +10,22 @@ @fixture def v1(): - return Vector.from_values([0, 2], [2.0, 5.0], name="v_1") + return Vector.from_coo([0, 2], [2.0, 5.0], name="v_1") @fixture def v2(): - return Vector.from_values([1, 2], [3.0, 7.0], name="v_2") + return Vector.from_coo([1, 2], [3.0, 7.0], name="v_2") @fixture def A1(): - return Matrix.from_values([0, 0], [0, 1], [0.0, 4.0], ncols=3, name="A_1") + return Matrix.from_coo([0, 0], [0, 1], [0.0, 4.0], ncols=3, name="A_1") @fixture def A2(): - return Matrix.from_values([0, 2], [0, 0], [6.0, 8.0], name="A_2") + return Matrix.from_coo([0, 2], [0, 0], [6.0, 8.0], name="A_2") @fixture diff --git a/graphblas/tests/test_io.py b/graphblas/tests/test_io.py index 75bc55e2e..1d222f864 100644 --- a/graphblas/tests/test_io.py +++ b/graphblas/tests/test_io.py @@ -28,7 +28,7 @@ def test_vector_to_from_numpy(): a = np.array([0.0, 2.0, 4.1]) v = gb.io.from_numpy(a) - assert v.isequal(gb.Vector.from_values([1, 2], [2.0, 4.1]), check_dtype=True) + assert v.isequal(gb.Vector.from_coo([1, 2], [2.0, 4.1]), check_dtype=True) a2 = gb.io.to_numpy(v) np.testing.assert_array_equal(a, a2) @@ -71,7 +71,7 @@ def test_vector_to_from_numpy_correct_size(a): def test_matrix_to_from_numpy(): a = np.array([[1.0, 0.0], [2.0, 3.7]]) M = gb.io.from_numpy(a) - assert M.isequal(gb.Matrix.from_values([0, 1, 1], [0, 0, 1], [1.0, 2.0, 3.7]), check_dtype=True) + assert M.isequal(gb.Matrix.from_coo([0, 1, 1], [0, 0, 1], [1.0, 2.0, 3.7]), check_dtype=True) a2 = gb.io.to_numpy(M) np.testing.assert_array_equal(a, a2) @@ -92,7 +92,7 @@ def test_matrix_to_from_numpy(): @pytest.mark.skipif("not nx or not ss") def test_matrix_to_from_networkx(): - M = gb.Matrix.from_values([0, 1, 1], [0, 0, 1], [1, 2, 3]) + M = gb.Matrix.from_coo([0, 1, 1], [0, 0, 1], [1, 2, 3]) G = gb.io.to_networkx(M) a = np.array([[1, 0], [2, 3]]) G2 = nx.from_numpy_array(a, create_using=nx.DiGraph) @@ -131,7 +131,7 @@ def test_matrix_to_from_networkx(): if suitesparse: assert M.ss.is_iso rows, cols = zip(*edges) - expected = gb.Matrix.from_values(rows, cols, 1) + expected = gb.Matrix.from_coo(rows, cols, 1) assert expected.isequal(M) # Test empty G = nx.DiGraph() @@ -161,33 +161,33 @@ def test_mmread_mmwrite(): examples = { "_32bit_integer_dense_example": ( False, - Matrix.from_values([0, 0, 1, 1], [0, 1, 0, 1], [p311, p311, p312, p312]), + Matrix.from_coo([0, 0, 1, 1], [0, 1, 0, 1], [p311, p311, p312, p312]), ), "_32bit_integer_sparse_example": ( False, - Matrix.from_values([0, 1], [0, 1], [p311, p312]), + Matrix.from_coo([0, 1], [0, 1], [p311, p312]), ), "_64bit_integer_dense_example": ( False, - Matrix.from_values([0, 0, 1, 1], [0, 1, 0, 1], [p31, m31, m632, p631]), + Matrix.from_coo([0, 0, 1, 1], [0, 1, 0, 1], [p31, m31, m632, p631]), ), "_64bit_integer_sparse_general_example": ( False, - Matrix.from_values([0, 0, 1], [0, 1, 1], [p31, p631, p631]), + Matrix.from_coo([0, 0, 1], [0, 1, 1], [p31, p631, p631]), ), "_64bit_integer_sparse_symmetric_example": ( False, - Matrix.from_values([0, 0, 1, 1], [0, 1, 0, 1], [p31, m631, m631, p631]), + Matrix.from_coo([0, 0, 1, 1], [0, 1, 0, 1], [p31, m631, m631, p631]), ), "_64bit_integer_sparse_skew_example": ( False, - Matrix.from_values([0, 0, 1, 1], [0, 1, 0, 1], [p31, m631, p631, p631]), + Matrix.from_coo([0, 0, 1, 1], [0, 1, 0, 1], [p31, m631, p631, p631]), ), "_over64bit_integer_dense_example": (True, None), "_over64bit_integer_sparse_example": (True, None), "_general_example": ( False, - Matrix.from_values( + Matrix.from_coo( [0, 0, 1, 2, 3, 3, 3, 4], [0, 3, 1, 2, 1, 3, 4, 4], [1, 6, 10.5, 0.015, 250.5, -280, 33.32, 12], @@ -195,7 +195,7 @@ def test_mmread_mmwrite(): ), "_skew_example": ( False, - Matrix.from_values( + Matrix.from_coo( [0, 1, 1, 2, 3, 3, 3, 4, 4], [0, 1, 3, 2, 1, 3, 4, 3, 4], [1, 10.5, -250.5, 0.015, 250.5, -280, 0, 0, 12], @@ -203,7 +203,7 @@ def test_mmread_mmwrite(): ), "_symmetric_example": ( False, - Matrix.from_values( + Matrix.from_coo( [0, 1, 1, 2, 3, 3, 3, 4, 4], [0, 1, 3, 2, 1, 3, 4, 3, 4], [1, 10.5, 250.5, 0.015, 250.5, -280, 8, 8, 12], @@ -211,7 +211,7 @@ def test_mmread_mmwrite(): ), "_symmetric_pattern_example": ( False, - Matrix.from_values( + Matrix.from_coo( [0, 1, 1, 2, 3, 3, 3, 4, 4], [0, 1, 3, 2, 1, 3, 4, 3, 4], [1, 1, 1, 1, 1, 1, 1, 1, 1], @@ -219,7 +219,7 @@ def test_mmread_mmwrite(): ), "_empty_lines_example": ( False, - Matrix.from_values( + Matrix.from_coo( [0, 0, 1, 2, 3, 3, 3, 4], [0, 3, 1, 2, 1, 3, 4, 4], [1, 6, 10.5, 0.015, 250.5, -280, 33.32, 12], @@ -229,7 +229,7 @@ def test_mmread_mmwrite(): if dtypes._supports_complex: examples["_hermitian_example"] = ( False, - Matrix.from_values( + Matrix.from_coo( [0, 1, 1, 2, 3, 3, 3, 4, 4], [0, 1, 3, 2, 1, 3, 4, 3, 4], [1, 10.5, 250.5 - 22.22j, 0.015, 250.5 + 22.22j, -280, -33.32j, 33.32j, 12], @@ -279,7 +279,7 @@ def test_from_scipy_sparse_duplicates(): with pytest.raises(ValueError, match="Duplicate indices found"): gb.io.from_scipy_sparse(a) a2 = gb.io.from_scipy_sparse(a, dup_op=gb.binary.plus) - expected = gb.Matrix.from_values([0, 1, 2], [2, 1, 0], [1, 2, 7]) + expected = gb.Matrix.from_coo([0, 1, 2], [2, 1, 0], [1, 2, 7]) assert a2.isequal(expected) with pytest.warns(DeprecationWarning): a3 = gb.io.from_scipy_sparse_matrix(a, dup_op=gb.binary.plus) @@ -300,7 +300,7 @@ def test_matrix_market_sparse_duplicates(): gb.io.mmread(mm) mm.seek(0) a = gb.io.mmread(mm, dup_op=gb.binary.plus) - expected = gb.Matrix.from_values([0, 1, 2], [2, 1, 0], [1, 2, 7]) + expected = gb.Matrix.from_coo([0, 1, 2], [2, 1, 0], [1, 2, 7]) assert a.isequal(expected) @@ -324,7 +324,7 @@ def test_scipy_sparse(): @pytest.mark.skipif("not ak") def test_awkward_roundtrip(): # Vector - v = gb.Vector.from_values([1, 3, 5], [20, 21, -5], size=22) + v = gb.Vector.from_coo([1, 3, 5], [20, 21, -5], size=22) for dtype in ["int16", "float32", "bool"]: v1 = v.dup(dtype=dtype) kv = gb.io.to_awkward(v1) @@ -332,7 +332,7 @@ def test_awkward_roundtrip(): v2 = gb.io.from_awkward(kv) assert v2.isequal(v1) # Matrix - m = gb.Matrix.from_values([0, 0, 3, 5], [1, 4, 0, 2], [1, 0, 2, -1], nrows=7, ncols=6) + m = gb.Matrix.from_coo([0, 0, 3, 5], [1, 4, 0, 2], [1, 0, 2, -1], nrows=7, ncols=6) for dtype in ["int16", "float32", "bool"]: for format in ["csr", "csc", "hypercsr", "hypercsc"]: m1 = m.dup(dtype=dtype) @@ -345,7 +345,7 @@ def test_awkward_roundtrip(): @pytest.mark.skipif("not ak") def test_awkward_iso_roundtrip(): # Vector - v = gb.Vector.from_values([1, 3, 5], [20, 20, 20], size=22) + v = gb.Vector.from_coo([1, 3, 5], [20, 20, 20], size=22) if suitesparse: assert v.ss.is_iso kv = gb.io.to_awkward(v) @@ -353,7 +353,7 @@ def test_awkward_iso_roundtrip(): v2 = gb.io.from_awkward(kv) assert v2.isequal(v) # Matrix - m = gb.Matrix.from_values([0, 0, 3, 5], [1, 4, 0, 2], [1, 1, 1, 1], nrows=7, ncols=6) + m = gb.Matrix.from_coo([0, 0, 3, 5], [1, 4, 0, 2], [1, 1, 1, 1], nrows=7, ncols=6) if suitesparse: assert m.ss.is_iso for format in ["csr", "csc", "hypercsr", "hypercsc"]: @@ -365,8 +365,8 @@ def test_awkward_iso_roundtrip(): @pytest.mark.skipif("not ak") def test_awkward_errors(): - v = gb.Vector.from_values([1, 3, 5], [20, 20, 20], size=22) - m = gb.Matrix.from_values([0, 0, 3, 5], [1, 4, 0, 2], [1, 1, 1, 1], nrows=7, ncols=6) + v = gb.Vector.from_coo([1, 3, 5], [20, 20, 20], size=22) + m = gb.Matrix.from_coo([0, 0, 3, 5], [1, 4, 0, 2], [1, 1, 1, 1], nrows=7, ncols=6) with pytest.raises(ValueError, match="Missing parameters"): gb.io.from_awkward(ak.Array([1, 2, 3])) with pytest.raises(ValueError, match="Invalid format for Vector"): diff --git a/graphblas/tests/test_matrix.py b/graphblas/tests/test_matrix.py index b18276c87..c587c6a17 100644 --- a/graphblas/tests/test_matrix.py +++ b/graphblas/tests/test_matrix.py @@ -43,13 +43,13 @@ def A(): [0, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6], [3, 2, 3, 1, 5, 3, 7, 8, 3, 1, 7, 4], ] - return Matrix.from_values(*data) + return Matrix.from_coo(*data) @pytest.fixture def v(): data = [[1, 3, 4, 6], [1, 1, 2, 0]] - return Vector.from_values(*data) + return Vector.from_coo(*data) def test_new(): @@ -71,15 +71,13 @@ def test_dup(A): A[0, 0] = 1000 assert C[0, 0].new() != 1000 # extended functionality - D = Matrix.from_values([0, 1], [0, 1], [0, 2.5], dtype=dtypes.FP64) + D = Matrix.from_coo([0, 1], [0, 1], [0, 2.5], dtype=dtypes.FP64) E = D.dup(dtype=dtypes.INT64) - assert E.isequal( - Matrix.from_values([0, 1], [0, 1], [0, 2], dtype=dtypes.INT64), check_dtype=True - ) + assert E.isequal(Matrix.from_coo([0, 1], [0, 1], [0, 2], dtype=dtypes.INT64), check_dtype=True) E = D.dup(mask=D.V) - assert E.isequal(Matrix.from_values([1], [1], [2.5], dtype=dtypes.FP64), check_dtype=True) + assert E.isequal(Matrix.from_coo([1], [1], [2.5], dtype=dtypes.FP64), check_dtype=True) E = D.dup(dtype=dtypes.INT64, mask=D.V) - assert E.isequal(Matrix.from_values([1], [1], [2], dtype=dtypes.INT64), check_dtype=True) + assert E.isequal(Matrix.from_coo([1], [1], [2], dtype=dtypes.INT64), check_dtype=True) def test_dup_clear(A): @@ -95,60 +93,60 @@ def test_dup_clear(A): assert D.ncols == A.ncols -def test_from_values(): - C = Matrix.from_values([0, 1, 3], [1, 1, 2], [True, False, True]) +def test_from_coo(): + C = Matrix.from_coo([0, 1, 3], [1, 1, 2], [True, False, True]) assert C.nrows == 4 assert C.ncols == 3 assert C.nvals == 3 assert C.dtype == bool - C2 = Matrix.from_values([0, 1, 3], [1, 1, 2], [12.3, 12.4, 12.5], nrows=17, ncols=3) + C2 = Matrix.from_coo([0, 1, 3], [1, 1, 2], [12.3, 12.4, 12.5], nrows=17, ncols=3) assert C2.nrows == 17 assert C2.ncols == 3 assert C2.nvals == 3 assert C2.dtype == float - C3 = Matrix.from_values([0, 1, 1], [2, 1, 1], [1, 2, 3], nrows=10, dup_op=binary.times) + C3 = Matrix.from_coo([0, 1, 1], [2, 1, 1], [1, 2, 3], nrows=10, dup_op=binary.times) assert C3.nrows == 10 assert C3.ncols == 3 assert C3.nvals == 2 # duplicates were combined assert C3.dtype == int assert C3[1, 1].new() == 6 # 2*3 - C3monoid = Matrix.from_values([0, 1, 1], [2, 1, 1], [1, 2, 3], nrows=10, dup_op=monoid.times) + C3monoid = Matrix.from_coo([0, 1, 1], [2, 1, 1], [1, 2, 3], nrows=10, dup_op=monoid.times) assert C3.isequal(C3monoid) with pytest.raises(ValueError, match="Duplicate indices found"): # Duplicate indices requires a dup_op - Matrix.from_values([0, 1, 1], [2, 1, 1], [True, True, True]) + Matrix.from_coo([0, 1, 1], [2, 1, 1], [True, True, True]) with pytest.raises(IndexOutOfBound): # Specified ncols can't hold provided indexes - Matrix.from_values([0, 1, 3], [1, 1, 2], [12.3, 12.4, 12.5], nrows=17, ncols=2) + Matrix.from_coo([0, 1, 3], [1, 1, 2], [12.3, 12.4, 12.5], nrows=17, ncols=2) with pytest.raises(ValueError, match="No row indices provided. Unable to infer nrows."): - Matrix.from_values([], [], []) + Matrix.from_coo([], [], []) # Changed: Assume empty value is float64 (like numpy) # with pytest.raises(ValueError, match="No values provided. Unable to determine type"): - empty1 = Matrix.from_values([], [], [], nrows=3, ncols=4) + empty1 = Matrix.from_coo([], [], [], nrows=3, ncols=4) assert empty1.dtype == dtypes.FP64 assert empty1.nrows == 3 assert empty1.ncols == 4 assert empty1.nvals == 0 with pytest.raises(ValueError, match="Unable to infer"): - Matrix.from_values([], [], [], dtype=dtypes.INT64) + Matrix.from_coo([], [], [], dtype=dtypes.INT64) with pytest.raises(ValueError, match="Unable to infer"): # could also raise b/c rows and columns are different sizes - Matrix.from_values([0], [], [0], dtype=dtypes.INT64) - C4 = Matrix.from_values([], [], [], nrows=3, ncols=4, dtype=dtypes.INT64) + Matrix.from_coo([0], [], [0], dtype=dtypes.INT64) + C4 = Matrix.from_coo([], [], [], nrows=3, ncols=4, dtype=dtypes.INT64) C5 = Matrix(dtypes.INT64, nrows=3, ncols=4) assert C4.isequal(C5, check_dtype=True) with pytest.raises( ValueError, match="`rows` and `columns` and `values` lengths must match: 1, 2, 1" ): - Matrix.from_values([0], [1, 2], [0]) + Matrix.from_coo([0], [1, 2], [0]) -def test_from_values_scalar(): - C = Matrix.from_values([0, 1, 3], [1, 1, 2], 7) +def test_from_coo_scalar(): + C = Matrix.from_coo([0, 1, 3], [1, 1, 2], 7) assert C.nrows == 4 assert C.ncols == 3 assert C.nvals == 3 @@ -159,7 +157,7 @@ def test_from_values_scalar(): assert C.reduce_scalar(monoid.any).new() == 7 # iso drumps duplicates - C = Matrix.from_values([0, 1, 3, 0], [1, 1, 2, 1], 7) + C = Matrix.from_coo([0, 1, 3, 0], [1, 1, 2, 1], 7) assert C.nrows == 4 assert C.ncols == 3 assert C.nvals == 3 @@ -169,7 +167,7 @@ def test_from_values_scalar(): assert C.ss.iso_value == 7 assert C.reduce_scalar(monoid.any).new() == 7 with pytest.raises(ValueError, match="dup_op must be None"): - Matrix.from_values([0, 1, 3, 0], [1, 1, 2, 1], 7, dup_op=binary.plus) + Matrix.from_coo([0, 1, 3, 0], [1, 1, 2, 1], 7, dup_op=binary.plus) C[0, 0] = 0 if suitesparse: with pytest.raises(ValueError, match="not iso"): @@ -226,10 +224,10 @@ def test_build(A): A.build([0, 11], [0, 0], [1, 1]) B = Matrix(int, nrows=2, ncols=2) B.build([0, 11], [0, 0], [1, 1], nrows=12) - assert B.isequal(Matrix.from_values([0, 11], [0, 0], [1, 1], ncols=2)) + assert B.isequal(Matrix.from_coo([0, 11], [0, 0], [1, 1], ncols=2)) C = Matrix(int, nrows=2, ncols=2) C.build([0, 0], [0, 11], [1, 1], ncols=12) - assert C.isequal(Matrix.from_values([0, 0], [0, 11], [1, 1], nrows=2)) + assert C.isequal(Matrix.from_coo([0, 0], [0, 11], [1, 1], nrows=2)) @pytest.mark.skipif("not suitesparse") @@ -249,14 +247,14 @@ def test_ss_build_scalar(A): def test_extract_values(A): - rows, cols, vals = A.to_values(dtype=int) + rows, cols, vals = A.to_coo(dtype=int) np.testing.assert_array_equal(rows, (0, 0, 1, 1, 2, 3, 3, 4, 5, 6, 6, 6)) np.testing.assert_array_equal(cols, (1, 3, 4, 6, 5, 0, 2, 5, 2, 2, 3, 4)) np.testing.assert_array_equal(vals, (2, 3, 8, 4, 1, 3, 3, 7, 1, 5, 7, 3)) assert rows.dtype == np.uint64 assert cols.dtype == np.uint64 assert vals.dtype == np.int64 - Trows, Tcols, Tvals = A.T.to_values(dtype=float) + Trows, Tcols, Tvals = A.T.to_coo(dtype=float) np.testing.assert_array_equal(rows, Tcols) np.testing.assert_array_equal(cols, Trows) np.testing.assert_array_equal(vals, Tvals) @@ -299,7 +297,7 @@ def test_remove_element(A): def test_mxm(A): C = A.mxm(A, semiring.plus_times).new() - result = Matrix.from_values( + result = Matrix.from_coo( [0, 0, 0, 0, 1, 1, 1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6], [0, 2, 4, 6, 2, 3, 4, 5, 2, 1, 3, 5, 2, 5, 0, 2, 5], [9, 9, 16, 8, 20, 28, 12, 56, 1, 6, 9, 3, 7, 1, 21, 21, 26], @@ -310,14 +308,14 @@ def test_mxm(A): def test_mxm_transpose(A): C = A.dup() C << A.mxm(A.T, semiring.plus_times) - result = Matrix.from_values( + result = Matrix.from_coo( [0, 0, 1, 1, 2, 2, 3, 3, 3, 4, 4, 5, 5, 5, 6, 6, 6, 6, 6], [0, 6, 1, 6, 2, 4, 3, 5, 6, 2, 4, 3, 5, 6, 0, 1, 3, 5, 6], [13, 21, 80, 24, 1, 7, 18, 3, 15, 7, 49, 3, 1, 5, 21, 24, 15, 5, 83], ) assert C.isequal(result) C << A.T.mxm(A, semiring.plus_times) - result2 = Matrix.from_values( + result2 = Matrix.from_coo( [0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 6, 6], [0, 2, 1, 3, 0, 2, 3, 4, 1, 2, 3, 4, 2, 3, 4, 6, 5, 4, 6], [9, 9, 4, 6, 9, 35, 35, 15, 6, 35, 58, 21, 15, 21, 73, 32, 50, 32, 16], @@ -326,8 +324,8 @@ def test_mxm_transpose(A): def test_mxm_nonsquare(): - A = Matrix.from_values([0, 0, 0], [0, 2, 4], [1, 2, 3], nrows=1, ncols=5) - B = Matrix.from_values([0, 2, 4], [0, 0, 0], [10, 20, 30], nrows=5, ncols=1) + A = Matrix.from_coo([0, 0, 0], [0, 2, 4], [1, 2, 3], nrows=1, ncols=5) + B = Matrix.from_coo([0, 2, 4], [0, 0, 0], [10, 20, 30], nrows=5, ncols=1) C = Matrix(A.dtype, nrows=1, ncols=1) C << A.mxm(B, semiring.max_plus) assert C[0, 0].new() == 33 @@ -339,11 +337,11 @@ def test_mxm_nonsquare(): def test_mxm_mask(A): - val_mask = Matrix.from_values([0, 3, 4], [2, 3, 2], [True, True, True], nrows=7, ncols=7) - struct_mask = Matrix.from_values([0, 3, 4], [2, 3, 2], [1, 0, 0], nrows=7, ncols=7) + val_mask = Matrix.from_coo([0, 3, 4], [2, 3, 2], [True, True, True], nrows=7, ncols=7) + struct_mask = Matrix.from_coo([0, 3, 4], [2, 3, 2], [1, 0, 0], nrows=7, ncols=7) C = A.dup() C(val_mask.V) << A.mxm(A, semiring.plus_times) - result = Matrix.from_values( + result = Matrix.from_coo( [0, 0, 0, 1, 1, 2, 3, 3, 3, 4, 4, 5, 6, 6, 6], [1, 2, 3, 4, 6, 5, 0, 2, 3, 2, 5, 2, 2, 3, 4], [2, 9, 3, 8, 4, 1, 3, 3, 9, 7, 7, 1, 5, 7, 3], @@ -351,7 +349,7 @@ def test_mxm_mask(A): assert C.isequal(result) C = A.dup() C(~val_mask.V) << A.mxm(A, semiring.plus_times) - result2 = Matrix.from_values( + result2 = Matrix.from_coo( [0, 0, 0, 1, 1, 1, 1, 2, 3, 3, 5, 6, 6, 6], [0, 4, 6, 2, 3, 4, 5, 2, 1, 5, 5, 0, 2, 5], [9, 16, 8, 20, 28, 12, 56, 1, 6, 3, 1, 21, 21, 26], @@ -359,7 +357,7 @@ def test_mxm_mask(A): assert C.isequal(result2) C = A.dup() C(struct_mask.S, replace=True).update(A.mxm(A, semiring.plus_times)) - result3 = Matrix.from_values([0, 3, 4], [2, 3, 2], [9, 9, 7], nrows=7, ncols=7) + result3 = Matrix.from_coo([0, 3, 4], [2, 3, 2], [9, 9, 7], nrows=7, ncols=7) assert C.isequal(result3) C2 = A.mxm(A, semiring.plus_times).new(mask=struct_mask.S) assert C2.isequal(result3) @@ -370,7 +368,7 @@ def test_mxm_mask(A): def test_mxm_accum(A): A(binary.plus) << A.mxm(A, semiring.plus_times) # fmt: off - result = Matrix.from_values( + result = Matrix.from_coo( [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 3, 4, 4, 5, 5, 6, 6, 6, 6, 6], [0, 1, 2, 3, 4, 6, 2, 3, 4, 5, 6, 2, 5, 0, 1, 2, 3, 5, 2, 5, 2, 5, 0, 2, 3, 4, 5], [9, 2, 9, 3, 16, 8, 20, 28, 20, 56, 4, 1, 1, 3, 6, 3, 9, 3, 7, 7, 1, 1, 21, 26, 7, 3, 26], @@ -381,14 +379,14 @@ def test_mxm_accum(A): def test_mxv(A, v): w = A.mxv(v, semiring.plus_times).new() - result = Vector.from_values([0, 1, 6], [5, 16, 13]) + result = Vector.from_coo([0, 1, 6], [5, 16, 13]) assert w.isequal(result) def test_ewise_mult(A): # Binary, Monoid, and Semiring - B = Matrix.from_values([0, 0, 5], [1, 2, 2], [5, 4, 8], nrows=7, ncols=7) - result = Matrix.from_values([0, 5], [1, 2], [10, 8], nrows=7, ncols=7) + B = Matrix.from_coo([0, 0, 5], [1, 2, 2], [5, 4, 8], nrows=7, ncols=7) + result = Matrix.from_coo([0, 5], [1, 2], [10, 8], nrows=7, ncols=7) C = A.ewise_mult(B, binary.times).new() assert C.isequal(result) C() << A.ewise_mult(B, monoid.times) @@ -399,8 +397,8 @@ def test_ewise_mult(A): def test_ewise_add(A): # Binary, Monoid, and Semiring - B = Matrix.from_values([0, 0, 5], [1, 2, 2], [5, 4, 8], nrows=7, ncols=7) - result = Matrix.from_values( + B = Matrix.from_coo([0, 0, 5], [1, 2, 2], [5, 4, 8], nrows=7, ncols=7) + result = Matrix.from_coo( [0, 3, 0, 3, 5, 6, 0, 6, 1, 6, 2, 4, 1], [2, 0, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6], [4, 3, 5, 3, 8, 5, 3, 7, 8, 3, 1, 7, 4], @@ -420,7 +418,7 @@ def test_ewise_add(A): def test_extract(A): C = Matrix(A.dtype, 3, 4) - result = Matrix.from_values( + result = Matrix.from_coo( [0, 0, 1, 2, 2, 2], [0, 2, 1, 1, 2, 3], [2, 3, 3, 5, 7, 3], nrows=3, ncols=4 ) C << A[[0, 3, 6], [1, 2, 3, 4]] @@ -435,7 +433,7 @@ def test_extract(A): def test_extract_row(A): w = Vector(A.dtype, 3) - result = Vector.from_values([1, 2], [5, 3], size=3) + result = Vector.from_coo([1, 2], [5, 3], size=3) w << A[6, [0, 2, 4]] assert w.isequal(result) w << A[6, :5:2] @@ -457,7 +455,7 @@ def test_extract_row(A): def test_extract_column(A): w = Vector(A.dtype, 3) - result = Vector.from_values([1, 2], [3, 1], size=3) + result = Vector.from_coo([1, 2], [3, 1], size=3) w << A[[1, 3, 5], 2] assert w.isequal(result) w << A[1:6:2, 2] @@ -472,12 +470,12 @@ def test_extract_input_mask(): # A M # 0 1 2 _ 0 1 # 3 4 5 2 3 _ - A = Matrix.from_values( + A = Matrix.from_coo( [0, 0, 0, 1, 1, 1], [0, 1, 2, 0, 1, 2], [0, 1, 2, 3, 4, 5], ) - M = Matrix.from_values( + M = Matrix.from_coo( [0, 0, 1, 1], [1, 2, 0, 1], [0, 1, 2, 3], @@ -486,7 +484,7 @@ def test_extract_input_mask(): MT = M.T.new() # Matrix structure mask result = A[0, [0, 1]].new(input_mask=M.S) - expected = Vector.from_values([1], [1]) + expected = Vector.from_coo([1], [1]) assert result.isequal(expected) # again result.clear() @@ -503,7 +501,7 @@ def test_extract_input_mask(): # Matrix value mask result = A[0, [1, 2]].new(input_mask=M.V) - expected = Vector.from_values([1], [2], size=2) + expected = Vector.from_coo([1], [2], size=2) assert result.isequal(expected) # again result.clear() @@ -564,7 +562,7 @@ def test_extract_input_mask(): # With transpose input value # Matrix structure mask result = A.T[[0, 1], 0].new(input_mask=MT.S) - expected = Vector.from_values([1], [1]) + expected = Vector.from_coo([1], [1]) assert result.isequal(expected) # again result.clear() @@ -581,7 +579,7 @@ def test_extract_input_mask(): # Matrix value mask result = A.T[[1, 2], 0].new(input_mask=MT.V) - expected = Vector.from_values([1], [2], size=2) + expected = Vector.from_coo([1], [2], size=2) assert result.isequal(expected) # again result.clear() @@ -599,8 +597,8 @@ def test_extract_with_matrix(A): def test_assign(A): - B = Matrix.from_values([0, 0, 1], [0, 1, 0], [9, 8, 7]) - result = Matrix.from_values( + B = Matrix.from_coo([0, 0, 1], [0, 1, 0], [9, 8, 7]) + result = Matrix.from_coo( [0, 0, 2, 3, 0, 3, 5, 6, 0, 6, 1, 6, 4, 1], [0, 5, 0, 0, 1, 2, 2, 2, 3, 3, 4, 4, 5, 6], [9, 8, 7, 3, 2, 3, 1, 5, 3, 7, 8, 3, 7, 4], @@ -622,13 +620,13 @@ def test_assign(A): def test_assign_wrong_dims(A): - B = Matrix.from_values([0, 0, 1], [0, 1, 0], [9, 8, 7]) + B = Matrix.from_coo([0, 0, 1], [0, 1, 0], [9, 8, 7]) with pytest.raises(DimensionMismatch): A[[0, 2, 4], [0, 5]] = B def test_assign_row(A, v): - result = Matrix.from_values( + result = Matrix.from_coo( [3, 3, 5, 6, 6, 1, 6, 2, 4, 1, 0, 0, 0, 0], [0, 2, 2, 2, 3, 4, 4, 5, 5, 6, 1, 3, 4, 6], [3, 3, 1, 5, 7, 8, 3, 1, 7, 4, 1, 1, 2, 0], @@ -639,16 +637,16 @@ def test_assign_row(A, v): def test_subassign_row_col(): - A = Matrix.from_values( + A = Matrix.from_coo( [0, 0, 0, 1, 1, 1, 2, 2, 2], [0, 1, 2, 0, 1, 2, 0, 1, 2], [0, 1, 2, 3, 4, 5, 6, 7, 8], ) - m = Vector.from_values([1], [True]) - v = Vector.from_values([0, 1], [10, 20]) + m = Vector.from_coo([1], [True]) + v = Vector.from_coo([0, 1], [10, 20]) A[[0, 1], 0](m.S) << v - result1 = Matrix.from_values( + result1 = Matrix.from_coo( [0, 0, 0, 1, 1, 1, 2, 2, 2], [0, 1, 2, 0, 1, 2, 0, 1, 2], [0, 1, 2, 20, 4, 5, 6, 7, 8], @@ -656,7 +654,7 @@ def test_subassign_row_col(): assert A.isequal(result1) A[1, [1, 2]](m.V, accum=binary.plus).update(v) - result2 = Matrix.from_values( + result2 = Matrix.from_coo( [0, 0, 0, 1, 1, 1, 2, 2, 2], [0, 1, 2, 0, 1, 2, 0, 1, 2], [0, 1, 2, 20, 4, 25, 6, 7, 8], @@ -664,7 +662,7 @@ def test_subassign_row_col(): assert A.isequal(result2) A[[0, 1], 0](m.S, binary.plus, replace=True) << v - result3 = Matrix.from_values( + result3 = Matrix.from_coo( [0, 0, 1, 1, 1, 2, 2, 2], [1, 2, 0, 1, 2, 0, 1, 2], [1, 2, 40, 4, 25, 6, 7, 8], @@ -675,7 +673,7 @@ def test_subassign_row_col(): A(m.S)[[0, 1], 0] << v A[[0, 1], 0](m.S) << 99 - result4 = Matrix.from_values( + result4 = Matrix.from_coo( [0, 0, 1, 1, 1, 2, 2, 2], [1, 2, 0, 1, 2, 0, 1, 2], [1, 2, 99, 4, 25, 6, 7, 8], @@ -683,7 +681,7 @@ def test_subassign_row_col(): assert A.isequal(result4) A[[1, 2], 0](m.S, binary.plus, replace=True) << 100 - result5 = Matrix.from_values( + result5 = Matrix.from_coo( [0, 0, 1, 1, 2, 2, 2], [1, 2, 1, 2, 0, 1, 2], [1, 2, 4, 25, 106, 7, 8], @@ -691,7 +689,7 @@ def test_subassign_row_col(): assert A.isequal(result5) A[2, [0, 1]](m.S) << -1 - result6 = Matrix.from_values( + result6 = Matrix.from_coo( [0, 0, 1, 1, 2, 2, 2], [1, 2, 1, 2, 0, 1, 2], [1, 2, 4, 25, 106, -1, 8], @@ -700,17 +698,17 @@ def test_subassign_row_col(): def test_subassign_matrix(): - A = Matrix.from_values( + A = Matrix.from_coo( [0, 0, 0, 1, 1, 1, 2, 2, 2], [0, 1, 2, 0, 1, 2, 0, 1, 2], [0, 1, 2, 3, 4, 5, 6, 7, 8], ) - m = Matrix.from_values([1], [0], [True]) - v = Matrix.from_values([0, 1], [0, 0], [10, 20]) + m = Matrix.from_coo([1], [0], [True]) + v = Matrix.from_coo([0, 1], [0, 0], [10, 20]) mT = m.T.new() A[[0, 1], [0]](m.S) << v - result1 = Matrix.from_values( + result1 = Matrix.from_coo( [0, 0, 0, 1, 1, 1, 2, 2, 2], [0, 1, 2, 0, 1, 2, 0, 1, 2], [0, 1, 2, 20, 4, 5, 6, 7, 8], @@ -718,7 +716,7 @@ def test_subassign_matrix(): assert A.isequal(result1) A[[1], [1, 2]](mT.V, accum=binary.plus) << v.T - result2 = Matrix.from_values( + result2 = Matrix.from_coo( [0, 0, 0, 1, 1, 1, 2, 2, 2], [0, 1, 2, 0, 1, 2, 0, 1, 2], [0, 1, 2, 20, 4, 25, 6, 7, 8], @@ -726,7 +724,7 @@ def test_subassign_matrix(): assert A.isequal(result2) A[[0, 1], [0]](m.S, binary.plus, replace=True) << v - result3 = Matrix.from_values( + result3 = Matrix.from_coo( [0, 0, 1, 1, 1, 2, 2, 2], [1, 2, 0, 1, 2, 0, 1, 2], [1, 2, 40, 4, 25, 6, 7, 8], @@ -737,7 +735,7 @@ def test_subassign_matrix(): A(m.S)[[0, 1], [0]] << v A[[0, 1], [0]](m.S) << 99 - result4 = Matrix.from_values( + result4 = Matrix.from_coo( [0, 0, 1, 1, 1, 2, 2, 2], [1, 2, 0, 1, 2, 0, 1, 2], [1, 2, 99, 4, 25, 6, 7, 8], @@ -745,7 +743,7 @@ def test_subassign_matrix(): assert A.isequal(result4) A[[1, 2], [0]](m.S, binary.plus, replace=True) << 100 - result5 = Matrix.from_values( + result5 = Matrix.from_coo( [0, 0, 1, 1, 2, 2, 2], [1, 2, 1, 2, 0, 1, 2], [1, 2, 4, 25, 106, 7, 8], @@ -753,7 +751,7 @@ def test_subassign_matrix(): assert A.isequal(result5) A[[2], [0, 1]](mT.S) << -1 - result6 = Matrix.from_values( + result6 = Matrix.from_coo( [0, 0, 1, 1, 2, 2, 2], [1, 2, 1, 2, 0, 1, 2], [1, 2, 4, 25, 106, -1, 8], @@ -762,7 +760,7 @@ def test_subassign_matrix(): def test_assign_column(A, v): - result = Matrix.from_values( + result = Matrix.from_coo( [3, 3, 5, 6, 0, 6, 1, 6, 2, 4, 1, 1, 3, 4, 6], [0, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 1, 1, 1, 1], [3, 3, 1, 5, 3, 7, 8, 3, 1, 7, 4, 1, 1, 2, 0], @@ -806,7 +804,7 @@ def test_assign_row_scalar(A, v): C = A.dup() C(v.S)[0, :] = 10 - result = Matrix.from_values( + result = Matrix.from_coo( [3, 0, 3, 5, 6, 0, 6, 1, 6, 2, 4, 1, 0, 0], [0, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 4, 6], [3, 10, 3, 1, 5, 10, 7, 8, 3, 1, 7, 4, 10, 10], @@ -818,84 +816,84 @@ def test_assign_row_col_matrix_mask(): # A B v1 v2 # 0 1 4 _ 100 10 # 2 _ 0 5 20 - A = Matrix.from_values([0, 0, 1], [0, 1, 0], [0, 1, 2]) - B = Matrix.from_values([0, 1, 1], [0, 0, 1], [4, 0, 5]) - v1 = Vector.from_values([0], [100]) - v2 = Vector.from_values([0, 1], [10, 20]) + A = Matrix.from_coo([0, 0, 1], [0, 1, 0], [0, 1, 2]) + B = Matrix.from_coo([0, 1, 1], [0, 0, 1], [4, 0, 5]) + v1 = Vector.from_coo([0], [100]) + v2 = Vector.from_coo([0, 1], [10, 20]) # row assign C = A.dup() C(B.S)[0, :] << v2 - result = Matrix.from_values([0, 0, 1], [0, 1, 0], [10, 1, 2]) + result = Matrix.from_coo([0, 0, 1], [0, 1, 0], [10, 1, 2]) assert C.isequal(result) C = A.dup() C(B.S, accum=binary.plus)[1, :] = v2 - result = Matrix.from_values([0, 0, 1, 1], [0, 1, 0, 1], [0, 1, 12, 20]) + result = Matrix.from_coo([0, 0, 1, 1], [0, 1, 0, 1], [0, 1, 12, 20]) assert C.isequal(result) C = A.dup() C(B.S, replace=True)[1, :] << v2 - result = Matrix.from_values([0, 1, 1], [0, 0, 1], [0, 10, 20]) + result = Matrix.from_coo([0, 1, 1], [0, 0, 1], [0, 10, 20]) assert C.isequal(result) # col assign C = A.dup() C(B.S)[:, 0] = v2 - result = Matrix.from_values([0, 0, 1], [0, 1, 0], [10, 1, 20]) + result = Matrix.from_coo([0, 0, 1], [0, 1, 0], [10, 1, 20]) assert C.isequal(result) C = A.dup() C(B.S, accum=binary.plus)[:, 1] << v2 - result = Matrix.from_values([0, 0, 1, 1], [0, 1, 0, 1], [0, 1, 2, 20]) + result = Matrix.from_coo([0, 0, 1, 1], [0, 1, 0, 1], [0, 1, 2, 20]) assert C.isequal(result) C = A.dup() C(B.S, replace=True)[:, 1] = v2 - result = Matrix.from_values([0, 1, 1], [0, 0, 1], [0, 2, 20]) + result = Matrix.from_coo([0, 1, 1], [0, 0, 1], [0, 2, 20]) assert C.isequal(result) # row assign scalar (as a sanity check) C = A.dup() C(B.S)[0, :] = 100 - result = Matrix.from_values([0, 0, 1], [0, 1, 0], [100, 1, 2]) + result = Matrix.from_coo([0, 0, 1], [0, 1, 0], [100, 1, 2]) assert C.isequal(result) C = A.dup() C(B.S, accum=binary.plus)[1, :] << 100 - result = Matrix.from_values([0, 0, 1, 1], [0, 1, 0, 1], [0, 1, 102, 100]) + result = Matrix.from_coo([0, 0, 1, 1], [0, 1, 0, 1], [0, 1, 102, 100]) assert C.isequal(result) C = A.dup() C(B.S, replace=True)[1, :] = 100 - result = Matrix.from_values([0, 1, 1], [0, 0, 1], [0, 100, 100]) + result = Matrix.from_coo([0, 1, 1], [0, 0, 1], [0, 100, 100]) assert C.isequal(result) # col assign scalar (as a sanity check) C = A.dup() C(B.S)[:, 0] << 100 - result = Matrix.from_values([0, 0, 1], [0, 1, 0], [100, 1, 100]) + result = Matrix.from_coo([0, 0, 1], [0, 1, 0], [100, 1, 100]) assert C.isequal(result) C = A.dup() C(B.S, accum=binary.plus)[:, 1] = 100 - result = Matrix.from_values([0, 0, 1, 1], [0, 1, 0, 1], [0, 1, 2, 100]) + result = Matrix.from_coo([0, 0, 1, 1], [0, 1, 0, 1], [0, 1, 2, 100]) assert C.isequal(result) C = A.dup() C(B.S, replace=True)[:, 1] << 100 - result = Matrix.from_values([0, 1, 1], [0, 0, 1], [0, 2, 100]) + result = Matrix.from_coo([0, 1, 1], [0, 0, 1], [0, 2, 100]) assert C.isequal(result) # row subassign C = A.dup() C[0, :](v2.S) << v2 - result = Matrix.from_values([0, 0, 1], [0, 1, 0], [10, 20, 2]) + result = Matrix.from_coo([0, 0, 1], [0, 1, 0], [10, 20, 2]) assert C.isequal(result) C = A.dup() C[0, [0]](v1.S) << v1 - result = Matrix.from_values([0, 0, 1], [0, 1, 0], [100, 1, 2]) + result = Matrix.from_coo([0, 0, 1], [0, 1, 0], [100, 1, 2]) assert C.isequal(result) with pytest.raises( @@ -906,12 +904,12 @@ def test_assign_row_col_matrix_mask(): # col subassign C = A.dup() C[:, 0](v2.S) << v2 - result = Matrix.from_values([0, 0, 1], [0, 1, 0], [10, 1, 20]) + result = Matrix.from_coo([0, 0, 1], [0, 1, 0], [10, 1, 20]) assert C.isequal(result) C = A.dup() C[[0], 0](v1.S) << v1 - result = Matrix.from_values([0, 0, 1], [0, 1, 0], [100, 1, 2]) + result = Matrix.from_coo([0, 0, 1], [0, 1, 0], [100, 1, 2]) assert C.isequal(result) with pytest.raises( @@ -922,12 +920,12 @@ def test_assign_row_col_matrix_mask(): # row subassign scalar C = A.dup() C[0, :](v2.S) << 100 - result = Matrix.from_values([0, 0, 1], [0, 1, 0], [100, 100, 2]) + result = Matrix.from_coo([0, 0, 1], [0, 1, 0], [100, 100, 2]) assert C.isequal(result) C = A.dup() C[0, [0]](v1.S) << 100 - result = Matrix.from_values([0, 0, 1], [0, 1, 0], [100, 1, 2]) + result = Matrix.from_coo([0, 0, 1], [0, 1, 0], [100, 1, 2]) assert C.isequal(result) with pytest.raises( @@ -938,12 +936,12 @@ def test_assign_row_col_matrix_mask(): # col subassign scalar C = A.dup() C[:, 0](v2.S) << 100 - result = Matrix.from_values([0, 0, 1], [0, 1, 0], [100, 1, 100]) + result = Matrix.from_coo([0, 0, 1], [0, 1, 0], [100, 1, 100]) assert C.isequal(result) C = A.dup() C[[0], 0](v1.S) << 100 - result = Matrix.from_values([0, 0, 1], [0, 1, 0], [100, 1, 2]) + result = Matrix.from_coo([0, 0, 1], [0, 1, 0], [100, 1, 2]) assert C.isequal(result) with pytest.raises( @@ -962,11 +960,11 @@ def test_subassign_combos(index): # mask 1 1 1 1 0 0 0 0 _ _ _ _ # val 1 2 _ _ 3 4 _ _ 5 6 _ _ # self 10 _ 20 _ 30 _ 40 _ 50 _ 60 _ 70 _ - mask_base = Vector.from_values( + mask_base = Vector.from_coo( [0, 1, 2, 3, 4, 5, 6, 7], [1, 1, 1, 1, 0, 0, 0, 0], size=12, name="mask" ) - val_base = Vector.from_values([0, 1, 4, 5, 8, 9], [1, 2, 3, 4, 5, 6], size=12) - self_base = Vector.from_values([0, 2, 4, 6, 8, 10, 12], [10, 20, 30, 40, 50, 60, 70], size=14) + val_base = Vector.from_coo([0, 1, 4, 5, 8, 9], [1, 2, 3, 4, 5, 6], size=12) + self_base = Vector.from_coo([0, 2, 4, 6, 8, 10, 12], [10, 20, 30, 40, 50, 60, 70], size=14) S = gb.core.mask.StructuralMask V = gb.core.mask.ValueMask @@ -988,7 +986,7 @@ def test_subassign_combos(index): mask = mask_type(mask_base) val = val_base self[index](binary.plus, mask, replace=replace) << val - expected = Vector.from_values(indices, values, size=14, name="expected") + expected = Vector.from_coo(indices, values, size=14, name="expected") if not self.isequal(expected): # pragma: no cover (debug) print(mask_type, replace) print(expected) @@ -1006,7 +1004,7 @@ def asrow(v): mask = mask_type(mask_base) val = val_base self[0, index](binary.plus, mask, replace=replace) << val - expected = Vector.from_values(indices, values, size=14, name="expected") + expected = Vector.from_coo(indices, values, size=14, name="expected") expected = asrow(expected) if not self.isequal(expected): # pragma: no cover (debug) print(mask_type, replace) @@ -1025,7 +1023,7 @@ def ascol(v): mask = mask_type(mask_base) val = val_base self[index, 0](binary.plus, mask, replace=replace) << val - expected = Vector.from_values(indices, values, size=14, name="expected") + expected = Vector.from_coo(indices, values, size=14, name="expected") expected = ascol(expected) if not self.isequal(expected): # pragma: no cover (debug) print(mask_type, replace) @@ -1039,7 +1037,7 @@ def ascol(v): mask = mask_type(asrow(mask_base)) val = asrow(val_base) self[[0], index](binary.plus, mask, replace=replace) << val - expected = Vector.from_values(indices, values, size=14, name="expected") + expected = Vector.from_coo(indices, values, size=14, name="expected") expected = asrow(expected) if not self.isequal(expected): # pragma: no cover (debug) print(mask_type, replace) @@ -1058,7 +1056,7 @@ def test_assign_column_scalar(A, v): C = A.dup() C[:, 1] = v C(v.S)[:, 1] = 10 - result = Matrix.from_values( + result = Matrix.from_coo( [3, 3, 5, 6, 0, 6, 1, 6, 2, 4, 1, 1, 3, 4, 6], [0, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 1, 1, 1, 1], [3, 3, 1, 5, 3, 7, 8, 3, 1, 7, 4, 10, 10, 10, 10], @@ -1066,7 +1064,7 @@ def test_assign_column_scalar(A, v): assert C.isequal(result) C(v.V, replace=True, accum=binary.plus)[:, 1] = 20 - result = Matrix.from_values( + result = Matrix.from_coo( [3, 3, 5, 6, 0, 6, 1, 6, 2, 4, 1, 1, 3, 4], [0, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 1, 1, 1], [3, 3, 1, 5, 3, 7, 8, 3, 1, 7, 4, 30, 30, 30], @@ -1076,7 +1074,7 @@ def test_assign_column_scalar(A, v): def test_assign_scalar(A): # Test block - result_block = Matrix.from_values( + result_block = Matrix.from_coo( [3, 0, 6, 0, 6, 6, 2, 4, 1, 1, 3, 5, 1, 3, 5], [0, 1, 2, 3, 3, 4, 5, 5, 6, 2, 2, 2, 4, 4, 4], [3, 2, 5, 3, 7, 3, 1, 7, 4, 0, 0, 0, 0, 0, 0], @@ -1094,7 +1092,7 @@ def test_assign_scalar(A): C[1::2, 2:5:2] = Scalar.from_value(0) assert C.isequal(result_block) # Test row - result_row = Matrix.from_values( + result_row = Matrix.from_coo( [3, 0, 6, 0, 6, 6, 2, 4, 1, 3, 5, 1, 1], [0, 1, 2, 3, 3, 4, 5, 5, 6, 2, 2, 2, 4], [3, 2, 5, 3, 7, 3, 1, 7, 4, 3, 1, 0, 0], @@ -1110,7 +1108,7 @@ def test_assign_scalar(A): C[1, 2:5:2] = 0 assert C.isequal(result_row) # Test column - result_column = Matrix.from_values( + result_column = Matrix.from_coo( [3, 0, 6, 0, 6, 6, 2, 4, 1, 1, 1, 3, 5], [0, 1, 2, 3, 3, 4, 5, 5, 6, 4, 2, 2, 2], [3, 2, 5, 3, 7, 3, 1, 7, 4, 8, 0, 0, 0], @@ -1121,9 +1119,9 @@ def test_assign_scalar(A): C = A.dup() C[1::2, 2] = 0 assert C.isequal(result_column) - B = Matrix.from_values([0, 0, 1, 1], [0, 1, 0, 1], 1) + B = Matrix.from_coo([0, 0, 1, 1], [0, 1, 0, 1], 1) B[1, 1] = Scalar(B.dtype) - expected = Matrix.from_values([0, 0, 1], [0, 1, 0], 1) + expected = Matrix.from_coo([0, 0, 1], [0, 1, 0], 1) assert B.isequal(expected) @@ -1150,7 +1148,7 @@ def test_assign_bad(A): def test_apply(A): - result = Matrix.from_values( + result = Matrix.from_coo( [3, 0, 3, 5, 6, 0, 6, 1, 6, 2, 4, 1], [0, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6], [-3, -2, -3, -1, -5, -3, -7, -8, -3, -1, -7, -4], @@ -1160,7 +1158,7 @@ def test_apply(A): def test_apply_binary(A): - result_right = Matrix.from_values( + result_right = Matrix.from_coo( [3, 0, 3, 5, 6, 0, 6, 1, 6, 2, 4, 1], [0, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6], [1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1], @@ -1170,7 +1168,7 @@ def test_apply_binary(A): w_right2 = A.apply(binary.gt, right=Scalar.from_value(1)).new() assert w_right.isequal(result_right) assert w_right2.isequal(result_right) - result_left = Matrix.from_values( + result_left = Matrix.from_coo( [3, 0, 3, 5, 6, 0, 6, 1, 6, 2, 4, 1], [0, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6], [5, 6, 5, 7, 3, 5, 1, 0, 5, 7, 1, 4], @@ -1197,7 +1195,7 @@ def test_apply_binary(A): def test_apply_indexunary(A): ridx = [3, 0, 3, 5, 6, 0, 6, 1, 6, 2, 4, 1] cidx = [0, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6] - Ar = Matrix.from_values(ridx, cidx, ridx) + Ar = Matrix.from_coo(ridx, cidx, ridx) r1 = A.apply("rowindex").new() r2 = A.apply(indexunary.rowindex).new() r3 = indexunary.rowindex(A).new() @@ -1205,7 +1203,7 @@ def test_apply_indexunary(A): assert r2.isequal(Ar) assert r3.isequal(Ar) - Ac = Matrix.from_values(ridx, cidx, [c + 2 for c in cidx]) + Ac = Matrix.from_coo(ridx, cidx, [c + 2 for c in cidx]) c1 = A.apply("colindex", 2).new() c2 = A.apply(indexunary.colindex, 2).new() c3 = indexunary.colindex(A, thunk=2).new() @@ -1213,7 +1211,7 @@ def test_apply_indexunary(A): assert c2.isequal(Ac) assert c3.isequal(Ac) - A3 = Matrix.from_values(ridx, cidx, [1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0], dtype=bool) + A3 = Matrix.from_coo(ridx, cidx, [1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0], dtype=bool) s3 = Scalar.from_value(3, dtypes.INT64) w1 = A.apply(indexunary.valueeq, s3).new() w2 = A.apply(select.valueeq, s3).new() @@ -1228,7 +1226,7 @@ def test_apply_indexunary(A): def test_select(A): - A3 = Matrix.from_values([0, 3, 3, 6], [3, 0, 2, 4], [3, 3, 3, 3], nrows=7, ncols=7) + A3 = Matrix.from_coo([0, 3, 3, 6], [3, 0, 2, 4], [3, 3, 3, 3], nrows=7, ncols=7) w1 = A.select(select.valueeq, 3).new() w1b = A.select(indexunary.valueeq, 3).new() w2 = A.select("==", 3).new() @@ -1238,7 +1236,7 @@ def test_select(A): assert w2.isequal(A3) assert w3.isequal(A3) - A2cols = Matrix.from_values([3, 0, 3, 5, 6], [0, 1, 2, 2, 2], [3, 2, 3, 1, 5], nrows=7, ncols=7) + A2cols = Matrix.from_coo([3, 0, 3, 5, 6], [0, 1, 2, 2, 2], [3, 2, 3, 1, 5], nrows=7, ncols=7) w4 = select.colle(A, 2).new() w5 = A.select("col<=", 2).new() w6 = select.column(A < 3).new() @@ -1246,7 +1244,7 @@ def test_select(A): assert w5.isequal(A2cols) assert w6.isequal(A2cols) - Aupper = Matrix.from_values( + Aupper = Matrix.from_coo( [0, 0, 1, 2, 4, 1], [1, 3, 4, 5, 5, 6], [2, 3, 8, 1, 7, 4], nrows=7, ncols=7 ) w7 = A.select("TRIU").new() @@ -1257,7 +1255,7 @@ def test_select(A): @autocompute def test_select_bools_and_masks(A): - A3 = Matrix.from_values([0, 3, 3, 6], [3, 0, 2, 4], [3, 3, 3, 3], nrows=7, ncols=7) + A3 = Matrix.from_coo([0, 3, 3, 6], [3, 0, 2, 4], [3, 3, 3, 3], nrows=7, ncols=7) # Select with boolean and masks w8 = A.select((A == 3).new()).new() assert w8.isequal(A3) @@ -1288,7 +1286,7 @@ def threex_minusthunk(x, row, col, thunk): # pragma: no cover (numba) assert not hasattr(select, "threex_minusthunk") with pytest.raises(ValueError): select.register_anonymous(threex_minusthunk) - expected = Matrix.from_values( + expected = Matrix.from_coo( [3, 0, 3, 5, 6, 0, 6, 1, 6, 2, 4, 1], [0, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6], [5, 2, 5, -1, 11, 5, 17, 20, 5, -1, 17, 8], @@ -1304,7 +1302,7 @@ def iii(x, row, col, thunk): # pragma: no cover (numba) assert hasattr(indexunary, "iii") assert hasattr(select, "iii") iii_apply = indexunary.register_anonymous(iii) - expected = Matrix.from_values( + expected = Matrix.from_coo( [3, 0, 3, 5, 6, 0, 6, 1, 6, 2, 4, 1], [0, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6], [False, False, True, True, True, False, True, True, True, True, True, True], @@ -1312,7 +1310,7 @@ def iii(x, row, col, thunk): # pragma: no cover (numba) result = iii_apply(A, 2).new() assert result.isequal(expected) iii_select = select.register_anonymous(iii) - expected = Matrix.from_values( + expected = Matrix.from_coo( [3, 5, 6, 6, 1, 6, 2, 4, 1], [2, 2, 2, 3, 4, 4, 5, 5, 6], [3, 1, 5, 7, 8, 3, 1, 7, 4] ) result = iii_select(A, 2).new() @@ -1322,7 +1320,7 @@ def iii(x, row, col, thunk): # pragma: no cover (numba) def test_reduce_row(A): - result = Vector.from_values([0, 1, 2, 3, 4, 5, 6], [5, 12, 1, 6, 7, 1, 15]) + result = Vector.from_coo([0, 1, 2, 3, 4, 5, 6], [5, 12, 1, 6, 7, 1, 15]) w = A.reduce_rowwise(monoid.plus).new() assert w.isequal(result) w2 = A.reduce_rowwise(binary.plus).new() @@ -1331,7 +1329,7 @@ def test_reduce_row(A): @pytest.mark.slow def test_reduce_agg(A): - result = Vector.from_values([0, 1, 2, 3, 4, 5, 6], [5, 12, 1, 6, 7, 1, 15]) + result = Vector.from_coo([0, 1, 2, 3, 4, 5, 6], [5, 12, 1, 6, 7, 1, 15]) w1 = A.reduce_rowwise(agg.sum).new() assert w1.isequal(result) w2 = A.T.reduce_columnwise(agg.sum).new() @@ -1358,7 +1356,7 @@ def test_reduce_agg(A): expected = A.reduce_rowwise(monoid.numpy.logaddexp[float]).new() assert w8.isclose(w8) - result = Vector.from_values([0, 1, 2, 3, 4, 5, 6], [3, 2, 9, 10, 11, 8, 4]) + result = Vector.from_coo([0, 1, 2, 3, 4, 5, 6], [3, 2, 9, 10, 11, 8, 4]) w9 = A.reduce_columnwise(agg.sum).new() assert w9.isequal(result) w10 = A.T.reduce_rowwise(agg.sum).new() @@ -1371,15 +1369,15 @@ def test_reduce_agg(A): assert w12.isequal(counts) w13 = A.reduce_rowwise(agg.mean).new() - expected = Vector.from_values([0, 1, 2, 3, 4, 5, 6], [2.5, 6, 1, 3, 7, 1, 5]) + expected = Vector.from_coo([0, 1, 2, 3, 4, 5, 6], [2.5, 6, 1, 3, 7, 1, 5]) assert w13.isequal(expected) w14 = A.reduce_columnwise(agg.mean).new() - expected = Vector.from_values([0, 1, 2, 3, 4, 5, 6], [3, 2, 3, 5, 5.5, 4, 4]) + expected = Vector.from_coo([0, 1, 2, 3, 4, 5, 6], [3, 2, 3, 5, 5.5, 4, 4]) assert w14.isequal(expected) w15 = A.reduce_rowwise(agg.exists).new() w16 = A.reduce_columnwise(agg.exists).new() - expected = Vector.from_values([0, 1, 2, 3, 4, 5, 6], [1, 1, 1, 1, 1, 1, 1]) + expected = Vector.from_coo([0, 1, 2, 3, 4, 5, 6], [1, 1, 1, 1, 1, 1, 1]) assert w15.isequal(expected) assert w16.isequal(expected) @@ -1423,24 +1421,24 @@ def test_reduce_agg(A): def test_reduce_agg_argminmax(A): # reduce_rowwise - expected = Vector.from_values([0, 1, 2, 3, 4, 5, 6], [1, 6, 5, 0, 5, 2, 4]) + expected = Vector.from_coo([0, 1, 2, 3, 4, 5, 6], [1, 6, 5, 0, 5, 2, 4]) w1b = A.reduce_rowwise(agg.argmin).new() assert w1b.isequal(expected) w1c = A.T.reduce_columnwise(agg.argmin).new() assert w1c.isequal(expected) - expected = Vector.from_values([0, 1, 2, 3, 4, 5, 6], [3, 4, 5, 0, 5, 2, 3]) + expected = Vector.from_coo([0, 1, 2, 3, 4, 5, 6], [3, 4, 5, 0, 5, 2, 3]) w2b = A.reduce_rowwise(agg.argmax).new() assert w2b.isequal(expected) w2c = A.T.reduce_columnwise(agg.argmax).new() assert w2c.isequal(expected) # reduce_cols - expected = Vector.from_values([0, 1, 2, 3, 4, 5, 6], [3, 0, 5, 0, 6, 2, 1]) + expected = Vector.from_coo([0, 1, 2, 3, 4, 5, 6], [3, 0, 5, 0, 6, 2, 1]) w7b = A.reduce_columnwise(agg.argmin).new() assert w7b.isequal(expected) w7c = A.T.reduce_rowwise(agg.argmin).new() assert w7c.isequal(expected) - expected = Vector.from_values([0, 1, 2, 3, 4, 5, 6], [3, 0, 6, 6, 1, 4, 1]) + expected = Vector.from_coo([0, 1, 2, 3, 4, 5, 6], [3, 0, 6, 6, 1, 4, 1]) w8b = A.reduce_columnwise(agg.argmax).new() assert w8b.isequal(expected) w8c = A.T.reduce_rowwise(agg.argmax).new() @@ -1475,24 +1473,24 @@ def test_reduce_agg_argminmax(A): def test_reduce_agg_firstlast(A): # reduce_rowwise w1 = A.reduce_rowwise(agg.first).new() - expected = Vector.from_values([0, 1, 2, 3, 4, 5, 6], [2, 8, 1, 3, 7, 1, 5]) + expected = Vector.from_coo([0, 1, 2, 3, 4, 5, 6], [2, 8, 1, 3, 7, 1, 5]) assert w1.isequal(expected) w1b = A.T.reduce_columnwise(agg.first).new() assert w1b.isequal(expected) w2 = A.reduce_rowwise(agg.last).new() - expected = Vector.from_values([0, 1, 2, 3, 4, 5, 6], [3, 4, 1, 3, 7, 1, 3]) + expected = Vector.from_coo([0, 1, 2, 3, 4, 5, 6], [3, 4, 1, 3, 7, 1, 3]) assert w2.isequal(expected) w2b = A.T.reduce_columnwise(agg.last).new() assert w2b.isequal(expected) # reduce_columnwise w3 = A.reduce_columnwise(agg.first).new() - expected = Vector.from_values([0, 1, 2, 3, 4, 5, 6], [3, 2, 3, 3, 8, 1, 4]) + expected = Vector.from_coo([0, 1, 2, 3, 4, 5, 6], [3, 2, 3, 3, 8, 1, 4]) assert w3.isequal(expected) w3b = A.T.reduce_rowwise(agg.first).new() assert w3b.isequal(expected) w4 = A.reduce_columnwise(agg.last).new() - expected = Vector.from_values([0, 1, 2, 3, 4, 5, 6], [3, 2, 5, 7, 3, 7, 4]) + expected = Vector.from_coo([0, 1, 2, 3, 4, 5, 6], [3, 2, 5, 7, 3, 7, 4]) assert w4.isequal(expected) w4b = A.T.reduce_rowwise(agg.last).new() assert w4b.isequal(expected) @@ -1530,24 +1528,24 @@ def test_reduce_agg_firstlast(A): def test_reduce_agg_firstlast_index(A): # reduce_rowwise w1 = A.reduce_rowwise(agg.first_index).new() - expected = Vector.from_values([0, 1, 2, 3, 4, 5, 6], [1, 4, 5, 0, 5, 2, 2]) + expected = Vector.from_coo([0, 1, 2, 3, 4, 5, 6], [1, 4, 5, 0, 5, 2, 2]) assert w1.isequal(expected) w1b = A.T.reduce_columnwise(agg.first_index).new() assert w1b.isequal(expected) w2 = A.reduce_rowwise(agg.last_index).new() - expected = Vector.from_values([0, 1, 2, 3, 4, 5, 6], [3, 6, 5, 2, 5, 2, 4]) + expected = Vector.from_coo([0, 1, 2, 3, 4, 5, 6], [3, 6, 5, 2, 5, 2, 4]) assert w2.isequal(expected) w2b = A.T.reduce_columnwise(agg.last_index).new() assert w2b.isequal(expected) # reduce_columnwise w3 = A.reduce_columnwise(agg.first_index).new() - expected = Vector.from_values([0, 1, 2, 3, 4, 5, 6], [3, 0, 3, 0, 1, 2, 1]) + expected = Vector.from_coo([0, 1, 2, 3, 4, 5, 6], [3, 0, 3, 0, 1, 2, 1]) assert w3.isequal(expected) w3b = A.T.reduce_rowwise(agg.first_index).new() assert w3b.isequal(expected) w4 = A.reduce_columnwise(agg.last_index).new() - expected = Vector.from_values([0, 1, 2, 3, 4, 5, 6], [3, 0, 6, 6, 6, 4, 1]) + expected = Vector.from_coo([0, 1, 2, 3, 4, 5, 6], [3, 0, 6, 6, 6, 4, 1]) assert w4.isequal(expected) w4b = A.T.reduce_rowwise(agg.last_index).new() assert w4b.isequal(expected) @@ -1591,7 +1589,7 @@ def test_reduce_agg_empty(): def test_reduce_row_udf(A): - result = Vector.from_values([0, 1, 2, 3, 4, 5, 6], [5, 12, 1, 6, 7, 1, 15]) + result = Vector.from_coo([0, 1, 2, 3, 4, 5, 6], [5, 12, 1, 6, 7, 1, 15]) def plus(x, y): # pragma: no cover (numba) return x + y @@ -1609,7 +1607,7 @@ def plus(x, y): # pragma: no cover (numba) def test_reduce_column(A): - result = Vector.from_values([0, 1, 2, 3, 4, 5, 6], [3, 2, 9, 10, 11, 8, 4]) + result = Vector.from_coo([0, 1, 2, 3, 4, 5, 6], [3, 2, 9, 10, 11, 8, 4]) w = A.reduce_columnwise(monoid.plus).new() assert w.isequal(result) w2 = A.reduce_columnwise(binary.plus).new() @@ -1651,7 +1649,7 @@ def test_reduce_call_agg(A): result = agg.max[float](A).new() # typed agg is callable too assert result.dtype == "FP64" assert result == 8 - expected = Vector.from_values([0, 1, 2, 3, 4, 5, 6], [5, 12, 1, 6, 7, 1, 15]) + expected = Vector.from_coo([0, 1, 2, 3, 4, 5, 6], [5, 12, 1, 6, 7, 1, 15]) result = agg.sum(A, rowwise=True) assert result.isequal(expected) result = agg.sum(A.T, columnwise=True) @@ -1662,8 +1660,8 @@ def test_reduce_call_agg(A): def test_transpose(A): # C << A.T - rows, cols, vals = A.to_values() - result = Matrix.from_values(cols, rows, vals) + rows, cols, vals = A.to_coo() + result = Matrix.from_coo(cols, rows, vals) C = Matrix(A.dtype, A.ncols, A.nrows) C << A.T assert C.isequal(result) @@ -1684,9 +1682,9 @@ def test_kronecker(): # 1 [8 - 4 - - - ] # 2 [- 4 6 - 6 9 ] # 3 [16 - 8 24 - 12] - A = Matrix.from_values([0, 1, 1], [0, 0, 1], [1, 2, 3]) - B = Matrix.from_values([0, 0, 1, 1], [1, 2, 0, 2], [2, 3, 8, 4]) - result = Matrix.from_values( + A = Matrix.from_coo([0, 1, 1], [0, 0, 1], [1, 2, 3]) + B = Matrix.from_coo([0, 0, 1, 1], [1, 2, 0, 2], [2, 3, 8, 4]) + result = Matrix.from_coo( [0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3], [1, 2, 0, 2, 1, 2, 4, 5, 0, 2, 3, 5], [2, 3, 8, 4, 4, 6, 6, 9, 16, 8, 24, 12], @@ -1722,13 +1720,13 @@ def test_assign_transpose(A): def test_assign_list(): A = Matrix(int, 3, 3) A[[0, 1], [1, 2]] = [[3, 4], [5, 6]] - expected = Matrix.from_values([0, 0, 1, 1], [1, 2, 1, 2], [3, 4, 5, 6], nrows=3, ncols=3) + expected = Matrix.from_coo([0, 0, 1, 1], [1, 2, 1, 2], [3, 4, 5, 6], nrows=3, ncols=3) assert A.isequal(expected) A[[0, 1], 1] = np.arange(2) - expected = Matrix.from_values([0, 0, 1, 1], [1, 2, 1, 2], [0, 4, 1, 6], nrows=3, ncols=3) + expected = Matrix.from_coo([0, 0, 1, 1], [1, 2, 1, 2], [0, 4, 1, 6], nrows=3, ncols=3) assert A.isequal(expected) A[0, 1:3] = [10, 20] - expected = Matrix.from_values([0, 0, 1, 1], [1, 2, 1, 2], [10, 20, 1, 6], nrows=3, ncols=3) + expected = Matrix.from_coo([0, 0, 1, 1], [1, 2, 1, 2], [10, 20, 1, 6], nrows=3, ncols=3) assert A.isequal(expected) with pytest.raises(TypeError): A[0, 1] = [0] @@ -1752,21 +1750,21 @@ def test_isequal(A, v): assert A.isequal(A) with pytest.raises(TypeError, match="Matrix"): A.isequal(v) # equality is not type-checking - C = Matrix.from_values([1], [1], [1]) + C = Matrix.from_coo([1], [1], [1]) assert not C.isequal(A) - D = Matrix.from_values([1], [2], [1]) + D = Matrix.from_coo([1], [2], [1]) assert not C.isequal(D) - D2 = Matrix.from_values([0], [2], [1], nrows=D.nrows, ncols=D.ncols) + D2 = Matrix.from_coo([0], [2], [1], nrows=D.nrows, ncols=D.ncols) assert not D2.isequal(D) - C2 = Matrix.from_values([1], [1], [1], nrows=7, ncols=7) + C2 = Matrix.from_coo([1], [1], [1], nrows=7, ncols=7) assert not C2.isequal(A) - C3 = Matrix.from_values( + C3 = Matrix.from_coo( [3, 0, 3, 5, 6, 0, 6, 1, 6, 2, 4, 1], [0, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6], [3.0, 2.0, 3.0, 1.0, 5.0, 3.0, 7.0, 8.0, 3.0, 1.0, 7.0, 4.0], ) assert not C3.isequal(A, check_dtype=True), "different datatypes are not equal" - C4 = Matrix.from_values( + C4 = Matrix.from_coo( [3, 0, 3, 5, 6, 0, 6, 1, 6, 2, 4, 1], [0, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6], [3.0, 2.0, 3.0, 1.0, 5.0, 3.000000000000000001, 7.0, 8.0, 3.0, 1 - 1e-11, 7.0, 4.0], @@ -1779,35 +1777,35 @@ def test_isclose(A, v): assert A.isclose(A) with pytest.raises(TypeError, match="Matrix"): A.isclose(v) # equality is not type-checking - C = Matrix.from_values([1], [1], [1]) # wrong size + C = Matrix.from_coo([1], [1], [1]) # wrong size assert not C.isclose(A) - D = Matrix.from_values([1], [2], [1]) + D = Matrix.from_coo([1], [2], [1]) assert not C.isclose(D) - D2 = Matrix.from_values([0], [2], [1], nrows=D.nrows, ncols=D.ncols) + D2 = Matrix.from_coo([0], [2], [1], nrows=D.nrows, ncols=D.ncols) assert not D2.isclose(D) - C2 = Matrix.from_values([1], [1], [1], nrows=7, ncols=7) # missing values + C2 = Matrix.from_coo([1], [1], [1], nrows=7, ncols=7) # missing values assert not C2.isclose(A) - C3 = Matrix.from_values( + C3 = Matrix.from_coo( [3, 0, 3, 5, 6, 0, 6, 1, 6, 2, 4, 1, 0], [0, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 2], [3, 2, 3, 1, 5, 3, 7, 8, 3, 1, 7, 4, 3], ) # extra values assert not C3.isclose(A) - C4 = Matrix.from_values( + C4 = Matrix.from_coo( [3, 0, 3, 5, 6, 0, 6, 1, 6, 2, 4, 1], [0, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6], [3.0, 2.0, 3.0, 1.0, 5.0, 3.0, 7.0, 8.0, 3.0, 1.0, 7.0, 4.0], ) assert not C4.isclose(A, check_dtype=True), "different datatypes are not equal" # fmt: off - C5 = Matrix.from_values( + C5 = Matrix.from_coo( [3, 0, 3, 5, 6, 0, 6, 1, 6, 2, 4, 1], [0, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6], [3.0, 2.0, 3.0, 1.0, 5.0, 3.000000000000000001, 7.0, 8.0, 3.0, 1 - 1e-11, 7.0, 4.0], ) # fmt: on assert C5.isclose(A) - C6 = Matrix.from_values( + C6 = Matrix.from_coo( [3, 0, 3, 5, 6, 0, 6, 1, 6, 2, 4, 1], [0, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6], [3.0, 2.000001, 3.0, 1.0, 5.0, 3.0, 7.0, 7.9999999, 3.0, 1.0, 7.0, 4.0], @@ -1822,7 +1820,7 @@ def test_transpose_equals(A): [3, 0, 3, 5, 6, 0, 6, 1, 6, 2, 4, 1], [3, 2, 3, 1, 5, 3, 7, 8, 3, 1, 7, 4], ] - B = Matrix.from_values(*data) + B = Matrix.from_coo(*data) assert A.isequal(B.T) assert B.isequal(A.T) assert A.T.isequal(B) @@ -1835,8 +1833,8 @@ def test_transpose_equals(A): def test_transpose_exceptional(): - A = Matrix.from_values([0, 0, 1, 1], [0, 1, 0, 1], [True, True, False, True]) - B = Matrix.from_values([0, 0, 1, 1], [0, 1, 0, 1], [1, 2, 3, 4]) + A = Matrix.from_coo([0, 0, 1, 1], [0, 1, 0, 1], [True, True, False, True]) + B = Matrix.from_coo([0, 0, 1, 1], [0, 1, 0, 1], [1, 2, 3, 4]) with pytest.raises(TypeError, match="not callable"): B.T(mask=A.V) << B.ewise_mult(B, op=binary.plus) @@ -1858,7 +1856,7 @@ def test_transpose_exceptional(): D = B.T.new() D = D.dup(mask=A.V) assert C.isequal(D) - assert C.isequal(Matrix.from_values([0, 0, 1], [0, 1, 1], [1, 3, 4])) + assert C.isequal(Matrix.from_coo([0, 0, 1], [0, 1, 1], [1, 3, 4])) def test_nested_matrix_operations(): @@ -1900,7 +1898,7 @@ def test_del(capsys): shell_A = object.__new__(Matrix) del shell_A # A has `gb_obj` of NULL - A = Matrix.from_values([0, 1], [0, 1], [0, 1]) + A = Matrix.from_coo([0, 1], [0, 1], [0, 1]) gb_obj = A.gb_obj A.gb_obj = gb.core.NULL del A @@ -2142,7 +2140,7 @@ def test_ss_import_export(A, do_iso, methods): assert A9.isequal(A) assert A9.ss.is_iso is do_iso - C = Matrix.from_values([0, 0, 1, 1], [0, 1, 0, 1], [1, 2, 3, 4]) + C = Matrix.from_coo([0, 0, 1, 1], [0, 1, 0, 1], [1, 2, 3, 4]) if do_iso: C(C.S) << 1 C1 = C.dup() @@ -2254,7 +2252,7 @@ def test_ss_import_export(A, do_iso, methods): @pytest.mark.skipif("not suitesparse") def test_ss_import_on_view(): - A = Matrix.from_values([0, 0, 1, 1], [0, 1, 0, 1], [1, 2, 3, 4]) + A = Matrix.from_coo([0, 0, 1, 1], [0, 1, 0, 1], [1, 2, 3, 4]) B = Matrix.ss.import_any(nrows=2, ncols=2, values=np.array([1, 2, 3, 4, 99, 99, 99])[:4]) assert A.isequal(B) @@ -2422,7 +2420,7 @@ def import_func(x, import_name, **kwargs): assert A.ss.is_iso is do_iso assert A_orig.ss.is_iso is do_iso - C = Matrix.from_values([0, 0, 1, 1, 2, 2], [0, 1, 0, 1, 0, 1], [1, 2, 3, 4, 5, 6]) + C = Matrix.from_coo([0, 0, 1, 1, 2, 2], [0, 1, 0, 1, 0, 1], [1, 2, 3, 4, 5, 6]) if do_iso: C(C.S) << 1 C_orig = C.dup() @@ -2460,7 +2458,7 @@ def import_func(x, import_name, **kwargs): else: values = [1, 3, 5, 2, 4, 6] assert other.isequal( - Matrix.from_values([0, 0, 1, 1, 2, 2], [0, 1, 0, 1, 0, 1], values) + Matrix.from_coo([0, 0, 1, 1, 2, 2], [0, 1, 0, 1, 0, 1], values) ) else: assert other.isequal(C_orig) @@ -2616,7 +2614,7 @@ def test_not_to_array(A): ) def test_diag(A, params): k, indices, values = params - expected = Vector.from_values(indices, values, dtype=A.dtype, size=max(0, A.nrows - abs(k))) + expected = Vector.from_coo(indices, values, dtype=A.dtype, size=max(0, A.nrows - abs(k))) v = A.diag(k) assert expected.isequal(v) v = A.T.diag(-k) @@ -2960,7 +2958,7 @@ def test_ss_flatten(A): ] # row-wise indices = [row * A.ncols + col for row, col in zip(data[0], data[1])] - expected = Vector.from_values(indices, data[2], size=A.nrows * A.ncols) + expected = Vector.from_coo(indices, data[2], size=A.nrows * A.ncols) for fmt in ["csr", "hypercsr", "bitmapr"]: B = Matrix.ss.import_any(**A.ss.export(format=fmt)) v = B.ss.flatten() @@ -2979,7 +2977,7 @@ def test_ss_flatten(A): # column-wise indices = [col * A.nrows + row for row, col in zip(data[0], data[1])] - expected = Vector.from_values(indices, data[2], size=A.nrows * A.ncols) + expected = Vector.from_coo(indices, data[2], size=A.nrows * A.ncols) for fmt in ["csc", "hypercsc", "bitmapc"]: B = Matrix.ss.import_any(**A.ss.export(format=fmt)) v = B.ss.flatten(order="col") @@ -3006,9 +3004,9 @@ def test_ss_flatten(A): @pytest.mark.skipif("not suitesparse") def test_ss_reshape(A): A.resize(8, 8) - r, c, v = A.to_values() + r, c, v = A.to_coo() idx = c + 8 * r - expected = Matrix.from_values(idx // 16, idx % 16, v, nrows=4, ncols=16) + expected = Matrix.from_coo(idx // 16, idx % 16, v, nrows=4, ncols=16) rv = A.ss.reshape(4, 16) assert rv.isequal(expected) rv = A.ss.reshape(4, -1, order="row") @@ -3033,7 +3031,7 @@ def test_ss_reshape(A): A.ss.reshape(4, 16, order="bad_order") idx = r + 8 * c - expected = Matrix.from_values(idx % 4, idx // 4, v, nrows=4, ncols=16) + expected = Matrix.from_coo(idx % 4, idx // 4, v, nrows=4, ncols=16) rv = A.ss.reshape(4, 16, order="col") assert rv.isequal(expected) @@ -3178,12 +3176,12 @@ def test_infix_sugar(A): def test_ss_random(A): R = A.ss.selectk_rowwise("random", 1) counts = R.reduce_rowwise(agg.count).new() - expected = Vector.from_values(range(A.ncols), 1) + expected = Vector.from_coo(range(A.ncols), 1) assert counts.isequal(expected) R = A.ss.selectk_columnwise("random", 1) counts = R.reduce_columnwise(agg.count).new() - expected = Vector.from_values(range(A.nrows), 1) + expected = Vector.from_coo(range(A.nrows), 1) assert counts.isequal(expected) R = A.ss.selectk_rowwise("random", 2) @@ -3195,7 +3193,7 @@ def test_ss_random(A): A(A.S) << 1 R = A.ss.selectk_rowwise("random", 1) counts = R.reduce_rowwise(agg.count).new() - expected = Vector.from_values(range(A.ncols), 1) + expected = Vector.from_coo(range(A.ncols), 1) assert counts.isequal(expected) with pytest.raises(ValueError): @@ -3209,7 +3207,7 @@ def test_ss_random(A): @pytest.mark.skipif("not suitesparse") def test_ss_firstk(A): B = A.ss.selectk_rowwise("first", 1) - expected = Matrix.from_values( + expected = Matrix.from_coo( [0, 1, 2, 3, 4, 5, 6], [1, 4, 5, 0, 5, 2, 2], [2, 8, 1, 3, 7, 1, 5], @@ -3219,7 +3217,7 @@ def test_ss_firstk(A): assert B.isequal(expected) B = A.ss.selectk_rowwise("first", 2) - expected = Matrix.from_values( + expected = Matrix.from_coo( [3, 0, 3, 5, 6, 0, 6, 1, 2, 4, 1], [0, 1, 2, 2, 2, 3, 3, 4, 5, 5, 6], [3, 2, 3, 1, 5, 3, 7, 8, 1, 7, 4], @@ -3232,7 +3230,7 @@ def test_ss_firstk(A): assert B.isequal(A) B = A.ss.selectk_columnwise("first", 1) - expected = Matrix.from_values( + expected = Matrix.from_coo( [3, 0, 3, 0, 1, 2, 1], [0, 1, 2, 3, 4, 5, 6], [3, 2, 3, 3, 8, 1, 4], @@ -3242,7 +3240,7 @@ def test_ss_firstk(A): assert B.isequal(expected) B = A.ss.selectk_columnwise("first", 2) - expected = Matrix.from_values( + expected = Matrix.from_coo( [3, 0, 3, 5, 0, 6, 1, 6, 2, 4, 1], [0, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6], [3, 2, 3, 1, 3, 7, 8, 3, 1, 7, 4], @@ -3258,7 +3256,7 @@ def test_ss_firstk(A): @pytest.mark.skipif("not suitesparse") def test_ss_lastk(A): B = A.ss.selectk_rowwise("last", 1) - expected = Matrix.from_values( + expected = Matrix.from_coo( [0, 3, 5, 6, 2, 4, 1], [3, 2, 2, 4, 5, 5, 6], [3, 3, 1, 3, 1, 7, 4], @@ -3268,7 +3266,7 @@ def test_ss_lastk(A): assert B.isequal(expected) B = A.ss.selectk_rowwise("last", 2) - expected = Matrix.from_values( + expected = Matrix.from_coo( [3, 0, 3, 5, 0, 6, 1, 6, 2, 4, 1], [0, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6], [3, 2, 3, 1, 3, 7, 8, 3, 1, 7, 4], @@ -3281,7 +3279,7 @@ def test_ss_lastk(A): assert B.isequal(A) B = A.ss.selectk_columnwise("last", 1) - expected = Matrix.from_values( + expected = Matrix.from_coo( [3, 0, 6, 6, 6, 4, 1], [0, 1, 2, 3, 4, 5, 6], [3, 2, 5, 7, 3, 7, 4], @@ -3291,7 +3289,7 @@ def test_ss_lastk(A): assert B.isequal(expected) B = A.ss.selectk_columnwise("last", 2) - expected = Matrix.from_values( + expected = Matrix.from_coo( [3, 0, 5, 6, 0, 6, 1, 6, 2, 4, 1], [0, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6], [3, 2, 1, 5, 3, 7, 8, 3, 1, 7, 4], @@ -3309,8 +3307,8 @@ def test_ss_lastk(A): @pytest.mark.slow def test_ss_compactify(A, do_iso): if do_iso: - r, c, v = A.to_values() - A = Matrix.from_values(r, c, 1) + r, c, v = A.to_coo() + A = Matrix.from_coo(r, c, 1) rows = [0, 0, 1, 1, 2, 3, 3, 4, 5, 6, 6, 6] new_cols = [0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 2] orig_cols = [1, 3, 4, 6, 5, 0, 2, 5, 2, 2, 3, 4] @@ -3335,7 +3333,7 @@ def check_reverse(A, expected, *args, stop=0, **kwargs): B = A.ss.compactify_rowwise(*args, ncols=n, reverse=True, **kwargs) assert B.isequal(C) - expected = Matrix.from_values( + expected = Matrix.from_coo( rows, new_cols, 1 if do_iso else [2, 3, 8, 4, 1, 3, 3, 7, 1, 5, 7, 3], @@ -3347,7 +3345,7 @@ def check_reverse(A, expected, *args, stop=0, **kwargs): check(A, reverse(expected), "last") check_reverse(A, reverse(expected), "last") - expected = Matrix.from_values( + expected = Matrix.from_coo( rows, new_cols, orig_cols, @@ -3359,7 +3357,7 @@ def check_reverse(A, expected, *args, stop=0, **kwargs): check(A, reverse(expected), "last", asindex=True) check_reverse(A, reverse(expected), "last", asindex=True) - expected = Matrix.from_values( + expected = Matrix.from_coo( rows, new_cols, 1 if do_iso else [2, 3, 4, 8, 1, 3, 3, 7, 1, 3, 5, 7], @@ -3372,7 +3370,7 @@ def check_reverse(A, expected, *args, stop=0, **kwargs): check_reverse(A, reverse(expected), "largest") if not do_iso: - expected = Matrix.from_values( + expected = Matrix.from_coo( rows, new_cols, [1, 3, 6, 4, 5, 0, 2, 5, 2, 4, 2, 3], @@ -3422,14 +3420,14 @@ def compare(A, expected, isequal=True, **kwargs): compare(A, A.ss.compactify_rowwise("first", 0, asindex=asindex), ncols=0, asindex=asindex) B = A.ss.compactify_columnwise("first", nrows=1) - expected = Matrix.from_values( + expected = Matrix.from_coo( [0, 0, 0, 0, 0, 0, 0], [0, 1, 2, 3, 4, 5, 6], 1 if do_iso else [3, 2, 3, 3, 8, 1, 4], ) assert B.isequal(expected) B = A.ss.compactify_columnwise("last", nrows=1, asindex=True) - expected = Matrix.from_values( + expected = Matrix.from_coo( [0, 0, 0, 0, 0, 0, 0], [0, 1, 2, 3, 4, 5, 6], [3, 0, 6, 6, 6, 4, 1], @@ -3456,6 +3454,12 @@ def test_deprecated(A): A.S.mask with pytest.warns(DeprecationWarning): binary.plus(A | A, require_monoid=True) + with pytest.warns(DeprecationWarning): + A.to_values() + with pytest.warns(DeprecationWarning): + A.T.to_values() + with pytest.warns(DeprecationWarning): + A.from_values([1], [2], [3]) def test_ndim(A): @@ -3474,10 +3478,10 @@ def test_sizeof(A): def test_ewise_union(): - A1 = Matrix.from_values([0], [0], [1], nrows=1, ncols=3) - A2 = Matrix.from_values([0], [1], [2], nrows=1, ncols=3) + A1 = Matrix.from_coo([0], [0], [1], nrows=1, ncols=3) + A2 = Matrix.from_coo([0], [1], [2], nrows=1, ncols=3) result = A1.ewise_union(A2, binary.plus, 10, 20).new() - expected = Matrix.from_values([0, 0], [0, 1], [21, 12], nrows=1, ncols=3) + expected = Matrix.from_coo([0, 0], [0, 1], [21, 12], nrows=1, ncols=3) assert result.isequal(expected) # Test transposed @@ -3492,11 +3496,11 @@ def test_ewise_union(): assert result.isequal(expected) # Upcast if scalars are floats result = A1.ewise_union(A2, monoid.plus, 10.1, 20.2).new() - expected = Matrix.from_values([0, 0], [0, 1], [21.2, 12.1], nrows=1, ncols=3) + expected = Matrix.from_coo([0, 0], [0, 1], [21.2, 12.1], nrows=1, ncols=3) assert result.isclose(expected) result = A1.ewise_union(A2, binary.minus, 0, 0).new() - expected = Matrix.from_values([0, 0], [0, 1], [1, -2], nrows=1, ncols=3) + expected = Matrix.from_coo([0, 0], [0, 1], [1, -2], nrows=1, ncols=3) assert result.isequal(expected) result = (A1 - A2).new() assert result.isequal(expected) @@ -3528,7 +3532,7 @@ def test_ss_iteration(A): assert list(B.ss.iterkeys()) == [] assert list(B.ss.itervalues()) == [] assert list(B.ss.iteritems()) == [] - rows, columns, values = A.to_values() + rows, columns, values = A.to_coo() assert sorted(zip(rows, columns)) == sorted(A.ss.iterkeys()) assert sorted(values) == sorted(A.ss.itervalues()) assert sorted(zip(rows, columns, values)) == sorted(A.ss.iteritems()) @@ -3559,18 +3563,18 @@ def test_udt(): A = Matrix(udt, nrows=2, ncols=2) a = np.zeros(1, dtype=record_dtype) A[0, 0] = a[0] - expected = Matrix.from_values([0], [0], a, nrows=2, ncols=2, dtype=udt) + expected = Matrix.from_coo([0], [0], a, nrows=2, ncols=2, dtype=udt) assert A.isequal(expected) A[0, 1] = (1, 2) - expected = Matrix.from_values( + expected = Matrix.from_coo( [0, 0], [0, 1], np.array([(0, 0), (1, 2)], dtype=record_dtype), nrows=2, ncols=2, dtype=udt ) assert A.isequal(expected) A << 0 - zeros = Matrix.from_values([0, 0, 1, 1], [0, 1, 0, 1], 0, dtype=udt) + zeros = Matrix.from_coo([0, 0, 1, 1], [0, 1, 0, 1], 0, dtype=udt) assert A.isequal(zeros) A(A.S)[:, :] = 1 - ones = Matrix.from_values([0, 0, 1, 1], [0, 1, 0, 1], [1, 1, 1, 1], dtype=udt) + ones = Matrix.from_coo([0, 0, 1, 1], [0, 1, 0, 1], [1, 1, 1, 1], dtype=udt) assert A.isequal(ones) A[:, :](A.S) << 0 assert A.isequal(zeros) @@ -3602,12 +3606,12 @@ def test_udt(): assert A.isequal(zeros) assert A[0, 0].new() == s assert A[:, :].new().isequal(A) - expected = Vector.from_values([0, 1], s) + expected = Vector.from_coo([0, 1], s) assert A[0, :].new().isequal(expected) assert A.reduce_rowwise(monoid.any).new().isequal(expected) - rows, cols, values = A.to_values() - assert A.isequal(Matrix.from_values(rows, cols, values)) - assert A.isequal(Matrix.from_values(rows, cols, values, dtype=A.dtype)) + rows, cols, values = A.to_coo() + assert A.isequal(Matrix.from_coo(rows, cols, values)) + assert A.isequal(Matrix.from_coo(rows, cols, values, dtype=A.dtype)) if suitesparse: info = A.ss.export() result = A.ss.import_any(**info) @@ -3616,7 +3620,7 @@ def test_udt(): result = A.ss.import_any(**info) assert result.isequal(A) result = unary.positioni(A).new() - expected = Matrix.from_values([0, 0, 1, 1], [0, 1, 0, 1], [0, 0, 1, 1]) + expected = Matrix.from_coo([0, 0, 1, 1], [0, 1, 0, 1], [0, 0, 1, 1]) assert result.isequal(expected) AB = unary.one(select.tril(A).new()).new() BA = select.tril(unary.one(A).new()).new() @@ -3633,7 +3637,7 @@ def test_udt(): A.reduce_columnwise(aggop).new() A.clear() A[[0, 1], 1] = [(2, 3), (4, 5)] - expected = Matrix.from_values([0, 1], [1, 1], [(2, 3), (4, 5)], dtype=udt) + expected = Matrix.from_coo([0, 1], [1, 1], [(2, 3), (4, 5)], dtype=udt) assert A.isequal(expected) A.clear() A[[0, 1], [1]] = [[(2, 3)], [(4, 5)]] @@ -3651,12 +3655,12 @@ def test_udt(): udt = dtypes.register_anonymous(np_dtype, "has_subdtype") A = Matrix(udt, nrows=2, ncols=2) A[:, :] = (1, 2, 3) - rows, cols, values = A.to_values() + rows, cols, values = A.to_coo() assert_array_equal(values, np.array([[1, 2, 3]] * 4)) - result = Matrix.from_values(rows, cols, values) + result = Matrix.from_coo(rows, cols, values) assert A.isequal(result) assert result.isequal(A) - result = Matrix.from_values(rows, cols, values, dtype=udt) + result = Matrix.from_coo(rows, cols, values, dtype=udt) assert result.isequal(A) if suitesparse: info = A.ss.export() @@ -3668,7 +3672,7 @@ def test_udt(): assert Matrix.from_csr(*A.to_csr()).isequal(A) A.clear() A[[0, 1], 1] = [(2, 3, 4), [5, 6, 7]] - expected = Matrix.from_values([0, 1], [1, 1], [[2, 3, 4], [5, 6, 7]], dtype=udt) + expected = Matrix.from_coo([0, 1], [1, 1], [[2, 3, 4], [5, 6, 7]], dtype=udt) assert A.isequal(expected) A[[0, 1], [1]] = [[[2, 3, 4]], [[5, 6, 7]]] if suitesparse: @@ -3684,7 +3688,7 @@ def test_udt(): def test_reposition(A): - rows, cols, values = A.to_values() + rows, cols, values = A.to_coo() rows = rows.astype(int) cols = cols.astype(int) @@ -3696,7 +3700,7 @@ def get_expected(row_offset, col_offset, nrows, ncols, is_transposed): r = r + row_offset c = c + col_offset mask = (r >= 0) & (r < nrows) & (c >= 0) & (c < ncols) - return Matrix.from_values(r[mask], c[mask], values[mask], nrows=nrows, ncols=ncols) + return Matrix.from_coo(r[mask], c[mask], values[mask], nrows=nrows, ncols=ncols) for row_offset in range(-A.nrows - 2, A.nrows + 3, 3): for col_offset in range(-A.ncols - 2, A.ncols + 3, 3): @@ -3712,7 +3716,7 @@ def get_expected(row_offset, col_offset, nrows, ncols, is_transposed): assert result.isequal(expected) result = A.reposition(3, 1).new(mask=A.S) - expected = Matrix.from_values([3, 4, 6], [2, 5, 3], [2, 8, 3], nrows=A.nrows, ncols=A.ncols) + expected = Matrix.from_coo([3, 4, 6], [2, 5, 3], [2, 8, 3], nrows=A.nrows, ncols=A.ncols) assert result.isequal(expected) result(A.S, binary.plus) << A.reposition(3, 1) @@ -3720,7 +3724,7 @@ def get_expected(row_offset, col_offset, nrows, ncols, is_transposed): assert result.isequal(expected) result = A.T.reposition(-1, 1).new(mask=A.S) - expected = Matrix.from_values( + expected = Matrix.from_coo( [0, 1, 1, 3, 4, 5], [1, 4, 6, 2, 5, 2], [2, 3, 1, 8, 7, 4], nrows=A.ncols, ncols=A.nrows ) assert result.isequal(expected) @@ -3730,7 +3734,7 @@ def get_expected(row_offset, col_offset, nrows, ncols, is_transposed): assert result.isequal(expected) -def test_to_values_sort(): +def test_to_coo_sort(): # How can we get a matrix to a jumbled state in SS so that export won't be sorted? N = 1000000 r = np.unique(np.random.randint(N, size=100)) @@ -3740,19 +3744,19 @@ def test_to_values_sort(): expected_rows = r.copy() np.random.shuffle(r) np.random.shuffle(c) - A = Matrix.from_values(r, c, r, nrows=N, ncols=N) - rows, cols, values = A.to_values(sort=False) - A = Matrix.from_values(r, c, r, nrows=N, ncols=N) - rows, cols, values = A.to_values(sort=True) + A = Matrix.from_coo(r, c, r, nrows=N, ncols=N) + rows, cols, values = A.to_coo(sort=False) + A = Matrix.from_coo(r, c, r, nrows=N, ncols=N) + rows, cols, values = A.to_coo(sort=True) assert_array_equal(rows, expected_rows) - rows, cols, values = A.T.to_values(sort=True) + rows, cols, values = A.T.to_coo(sort=True) assert_array_equal(cols, expected_rows) -def test_to_values_subset(A): - rows, cols, vals = A.to_values() +def test_to_coo_subset(A): + rows, cols, vals = A.to_coo() for do_rows, do_cols, do_vals in itertools.product([True, False], [True, False], [True, False]): - r, c, v = A.to_values(rows=do_rows, columns=do_cols, values=do_vals) + r, c, v = A.to_coo(rows=do_rows, columns=do_cols, values=do_vals) if do_rows: assert_array_equal(r, rows) else: @@ -3766,12 +3770,12 @@ def test_to_values_subset(A): assert v.dtype == np.int64 else: assert v is None - r, c, v = A.to_values(rows=None, columns=None, values=True, dtype=float) + r, c, v = A.to_coo(rows=None, columns=None, values=True, dtype=float) assert r is None assert c is None assert_array_equal(v, vals) assert v.dtype == float - r, c, v = A.to_values(values=True, dtype=A.dtype, sort=False) + r, c, v = A.to_coo(values=True, dtype=A.dtype, sort=False) assert v.dtype == np.int64 @@ -3866,14 +3870,14 @@ def test_to_csr_from_csc(A): # 0 [- 1 -] # 1 [2 - -] B = Matrix.from_csr([0, 1, 2], [1, 0], [10, 20], ncols=3) - expected = Matrix.from_values([0, 1], [1, 0], [10, 20], nrows=2, ncols=3) + expected = Matrix.from_coo([0, 1], [1, 0], [10, 20], nrows=2, ncols=3) assert expected.isequal(B, check_dtype=True) B = Matrix.from_csc([0, 1, 2, 2], [1, 0], [20, 10]) assert expected.isequal(B, check_dtype=True) B = Matrix.from_csr([0, 1, 2], [1, 0], 100) - expected = Matrix.from_values([0, 1], [1, 0], [100, 100], nrows=2, ncols=2) + expected = Matrix.from_coo([0, 1], [1, 0], [100, 100], nrows=2, ncols=2) assert expected.isequal(B, check_dtype=True) B = Matrix.from_csc([0, 1, 2], [1, 0], 100) @@ -3911,14 +3915,14 @@ def test_to_dcsr_from_dcsc(A): # 1 [- - -] # 2 [2 - -] B = Matrix.from_dcsr([0, 2], [0, 1, 2], [1, 0], [10, 20], ncols=3) - expected = Matrix.from_values([0, 2], [1, 0], [10, 20], nrows=3, ncols=3) + expected = Matrix.from_coo([0, 2], [1, 0], [10, 20], nrows=3, ncols=3) assert expected.isequal(B, check_dtype=True) B = Matrix.from_dcsc([0, 1], [0, 1, 2], [2, 0], [20, 10], ncols=3) assert expected.isequal(B, check_dtype=True) B = Matrix.from_dcsr([0, 2], [0, 1, 2], [1, 0], 100) - expected = Matrix.from_values([0, 2], [1, 0], [100, 100], nrows=3, ncols=2) + expected = Matrix.from_coo([0, 2], [1, 0], [100, 100], nrows=3, ncols=2) assert expected.isequal(B, check_dtype=True) # Test empty @@ -3999,7 +4003,7 @@ def test_to_dicts_from_dicts(A): def test_from_list_of_dicts(): list_of_dicts = [{1: 1}, {}, {0: 10, 2: 3}, {}] A1 = Matrix.from_dicts(list_of_dicts) - expected = Matrix.from_values([0, 2, 2], [1, 0, 2], [1, 10, 3], nrows=4) + expected = Matrix.from_coo([0, 2, 2], [1, 0, 2], [1, 10, 3], nrows=4) assert A1.isequal(expected) A2 = Matrix.from_dicts(list_of_dicts, nrows=4) assert A2.isequal(expected) diff --git a/graphblas/tests/test_numpyops.py b/graphblas/tests/test_numpyops.py index baf0e9d40..16d25a8e9 100644 --- a/graphblas/tests/test_numpyops.py +++ b/graphblas/tests/test_numpyops.py @@ -25,14 +25,14 @@ def test_numpyops_dir(): @pytest.mark.slow def test_bool_doesnt_get_too_large(): - a = Vector.from_values([0, 1, 2, 3], [True, False, True, False]) - b = Vector.from_values([0, 1, 2, 3], [True, True, False, False]) + a = Vector.from_coo([0, 1, 2, 3], [True, False, True, False]) + b = Vector.from_coo([0, 1, 2, 3], [True, True, False, False]) if gb.config["mapnumpy"]: with pytest.raises(KeyError, match="plus does not work with BOOL"): z = a.ewise_mult(b, gb.monoid.numpy.add).new() else: z = a.ewise_mult(b, gb.monoid.numpy.add).new() - x, y = z.to_values() + x, y = z.to_coo() np.testing.assert_array_equal(y, (True, True, True, False)) def func(x): # pragma: no cover (numba) @@ -40,7 +40,7 @@ def func(x): # pragma: no cover (numba) op = gb.core.operator.UnaryOp.register_anonymous(func) z = a.apply(op).new() - x, y = z.to_values() + x, y = z.to_coo() np.testing.assert_array_equal(y, (True, False, True, False)) @@ -48,13 +48,13 @@ def func(x): # pragma: no cover (numba) def test_npunary(): L = list(range(5)) data = [ - [Vector.from_values([0, 1], [True, False]), np.array([True, False])], - [Vector.from_values(L, L), np.array(L, dtype=np.int64)], - [Vector.from_values(L, L, dtype="float64"), np.array(L, dtype=np.float64)], + [Vector.from_coo([0, 1], [True, False]), np.array([True, False])], + [Vector.from_coo(L, L), np.array(L, dtype=np.int64)], + [Vector.from_coo(L, L, dtype="float64"), np.array(L, dtype=np.float64)], ] if _supports_complex: data.append( - [Vector.from_values(L, L, dtype="FC64"), np.array(L, dtype=np.complex128)], + [Vector.from_coo(L, L, dtype="FC64"), np.array(L, dtype=np.complex128)], ) blocklist = { "BOOL": {"negative", "positive", "reciprocal", "sign"}, @@ -86,7 +86,7 @@ def test_npunary(): compare_op = isclose else: compare_op = npbinary.equal - np_result = Vector.from_values( + np_result = Vector.from_coo( list(range(np_input.size)), list(np_result), dtype=gb_result.dtype ) assert gb_result.nvals == np_result.size @@ -108,20 +108,20 @@ def test_npbinary(): index = list(range(len(values1))) data = [ [ - [Vector.from_values(index, values1), Vector.from_values(index, values2)], + [Vector.from_coo(index, values1), Vector.from_coo(index, values2)], [np.array(values1, dtype=np.int64), np.array(values2, dtype=np.int64)], ], [ [ - Vector.from_values(index, values1, dtype="float64"), - Vector.from_values(index, values2, dtype="float64"), + Vector.from_coo(index, values1, dtype="float64"), + Vector.from_coo(index, values2, dtype="float64"), ], [np.array(values1, dtype=np.float64), np.array(values2, dtype=np.float64)], ], [ [ - Vector.from_values([0, 1, 2, 3], [True, False, True, False]), - Vector.from_values([0, 1, 2, 3], [True, True, False, False]), + Vector.from_coo([0, 1, 2, 3], [True, False, True, False]), + Vector.from_coo([0, 1, 2, 3], [True, True, False, False]), ], [np.array([True, False, True, False]), np.array([True, True, False, False])], ], @@ -130,8 +130,8 @@ def test_npbinary(): data.append( [ [ - Vector.from_values(index, values1, dtype="FC64"), - Vector.from_values(index, values2, dtype="FC64"), + Vector.from_coo(index, values1, dtype="FC64"), + Vector.from_coo(index, values2, dtype="FC64"), ], [np.array(values1, dtype=np.complex128), np.array(values2, dtype=np.complex128)], ], @@ -160,9 +160,7 @@ def test_npbinary(): np_result = getattr(np, binary_name)(np_left, np_right) compare_op = npbinary.equal - np_result = Vector.from_values( - np.arange(np_left.size), np_result, dtype=gb_result.dtype - ) + np_result = Vector.from_coo(np.arange(np_left.size), np_result, dtype=gb_result.dtype) assert gb_result.nvals == np_result.size match = gb_result.ewise_mult(np_result, compare_op).new() @@ -188,20 +186,20 @@ def test_npmonoid(): index = list(range(len(values1))) data = [ [ - [Vector.from_values(index, values1), Vector.from_values(index, values2)], + [Vector.from_coo(index, values1), Vector.from_coo(index, values2)], [np.array(values1, dtype=int), np.array(values2, dtype=int)], ], [ [ - Vector.from_values(index, values1, dtype="float64"), - Vector.from_values(index, values2, dtype="float64"), + Vector.from_coo(index, values1, dtype="float64"), + Vector.from_coo(index, values2, dtype="float64"), ], [np.array(values1, dtype=np.float64), np.array(values2, dtype=np.float64)], ], [ [ - Vector.from_values([0, 1, 2, 3], [True, False, True, False]), - Vector.from_values([0, 1, 2, 3], [True, True, False, False]), + Vector.from_coo([0, 1, 2, 3], [True, False, True, False]), + Vector.from_coo([0, 1, 2, 3], [True, True, False, False]), ], [np.array([True, False, True, False]), np.array([True, True, False, False])], ], @@ -211,8 +209,8 @@ def test_npmonoid(): data.append( [ [ - Vector.from_values(index, values1, dtype="FC64"), - Vector.from_values(index, values2, dtype="FC64"), + Vector.from_coo(index, values1, dtype="FC64"), + Vector.from_coo(index, values2, dtype="FC64"), ], [ np.array(values1, dtype=np.complex128), @@ -235,9 +233,7 @@ def test_npmonoid(): with np.errstate(divide="ignore", over="ignore", under="ignore", invalid="ignore"): gb_result = gb_left.ewise_mult(gb_right, op).new() np_result = getattr(np, binary_name)(np_left, np_right) - np_result = Vector.from_values( - np.arange(np_left.size), np_result, dtype=gb_result.dtype - ) + np_result = Vector.from_coo(np.arange(np_left.size), np_result, dtype=gb_result.dtype) assert gb_result.nvals == np_result.size match = gb_result.ewise_mult(np_result, npbinary.equal).new() if gb_result.dtype.name.startswith("F"): diff --git a/graphblas/tests/test_op.py b/graphblas/tests/test_op.py index 0529fef5a..1bfd5ec7d 100644 --- a/graphblas/tests/test_op.py +++ b/graphblas/tests/test_op.py @@ -158,9 +158,9 @@ def plus_one(x): if dtypes._supports_complex: comp_set.update({FC32, FC64}) assert set(unary.plus_one.types) == comp_set - v = Vector.from_values([0, 1, 3], [1, 2, -4], dtype=dtypes.INT32) + v = Vector.from_coo([0, 1, 3], [1, 2, -4], dtype=dtypes.INT32) v << v.apply(unary.plus_one) - result = Vector.from_values([0, 1, 3], [2, 3, -3], dtype=dtypes.INT32) + result = Vector.from_coo([0, 1, 3], [2, 3, -3], dtype=dtypes.INT32) assert v.isequal(result) assert "INT8" in unary.plus_one assert INT8 in unary.plus_one.types @@ -184,13 +184,13 @@ def inner(val): op = UnaryOp.register_anonymous(plus_x, parameterized=True) assert not op.is_positional - v = Vector.from_values([0, 1, 3], [1, 2, -4], dtype=dtypes.INT32) + v = Vector.from_coo([0, 1, 3], [1, 2, -4], dtype=dtypes.INT32) v0 = v.apply(op).new() assert v.isequal(v0, check_dtype=True) v0 = v.apply(op(0)).new() assert v.isequal(v0, check_dtype=True) v10 = v.apply(op(x=10)).new() - r10 = Vector.from_values([0, 1, 3], [11, 12, 6], dtype=dtypes.INT32) + r10 = Vector.from_coo([0, 1, 3], [11, 12, 6], dtype=dtypes.INT32) assert r10.isequal(v10, check_dtype=True) UnaryOp._initialize() # no-op UnaryOp.register_new("plus_x_parameterized", plus_x, parameterized=True) @@ -211,15 +211,15 @@ def inner(left, right): assert not op.is_positional assert op.monoid is None assert op(1).monoid is None - v = Vector.from_values([0, 1, 3], [1, 2, -4], dtype=dtypes.INT32) + v = Vector.from_coo([0, 1, 3], [1, 2, -4], dtype=dtypes.INT32) v0 = v.ewise_mult(v, op).new() - r0 = Vector.from_values([0, 1, 3], [2, 4, -8], dtype=dtypes.INT32) + r0 = Vector.from_coo([0, 1, 3], [2, 4, -8], dtype=dtypes.INT32) assert v0.isequal(r0, check_dtype=True) v1 = v.ewise_add(v, op(1)).new() - r1 = Vector.from_values([0, 1, 3], [3, 5, -7], dtype=dtypes.INT32) + r1 = Vector.from_coo([0, 1, 3], [3, 5, -7], dtype=dtypes.INT32) assert v1.isequal(r1, check_dtype=True) - w = Vector.from_values([0, 0, 1, 3], [1, 0, 2, -4], dtype=dtypes.INT32, dup_op=op) + w = Vector.from_coo([0, 0, 1, 3], [1, 0, 2, -4], dtype=dtypes.INT32, dup_op=op) assert v.isequal(w, check_dtype=True) with pytest.raises(TypeError, match="Monoid"): assert v.reduce(op).new() == -1 @@ -236,9 +236,9 @@ def inner(left, right): x = x.ewise_mult(x, op(1)).new() assert v.isequal(x) - assert v.isequal(Vector.from_values([0, 1, 3], [19, 35, -61], dtype=dtypes.INT32)) + assert v.isequal(Vector.from_coo([0, 1, 3], [19, 35, -61], dtype=dtypes.INT32)) v11 = v.apply(op(1), left=10).new() - r11 = Vector.from_values([0, 1, 3], [30, 46, -50], dtype=dtypes.INT32) + r11 = Vector.from_coo([0, 1, 3], [30, 46, -50], dtype=dtypes.INT32) # Should we check for dtype here? # Is it okay if the literal scalar is an INT64, which causes the output to default to INT64? assert v11.isequal(r11, check_dtype=False) @@ -296,19 +296,19 @@ def plus_plus_x_identity(x=0): assert bin_op1.monoid is None assert monoid.name == "my_monoid" - v = Vector.from_values([0, 1, 3], [1, 2, -4], dtype=dtypes.INT32) + v = Vector.from_coo([0, 1, 3], [1, 2, -4], dtype=dtypes.INT32) v0 = v.ewise_add(v, monoid).new() - r0 = Vector.from_values([0, 1, 3], [2, 4, -8], dtype=dtypes.INT32) + r0 = Vector.from_coo([0, 1, 3], [2, 4, -8], dtype=dtypes.INT32) assert v0.isequal(r0, check_dtype=True) v1 = v.ewise_mult(v, monoid(1)).new() - r1 = Vector.from_values([0, 1, 3], [3, 5, -7], dtype=dtypes.INT32) + r1 = Vector.from_coo([0, 1, 3], [3, 5, -7], dtype=dtypes.INT32) assert v1.isequal(r1, check_dtype=True) assert v.reduce(monoid).new() == -1 assert v.reduce(monoid(1)).new() == 1 # with pytest.raises(TypeError, match="BinaryOp"): # NOW OKAY - w1 = Vector.from_values([0, 0, 1, 3], [1, 0, 2, -4], dtype=dtypes.INT32, dup_op=monoid) - w2 = Vector.from_values([0, 1, 3], [1, 2, -4], dtype=dtypes.INT32) + w1 = Vector.from_coo([0, 0, 1, 3], [1, 0, 2, -4], dtype=dtypes.INT32, dup_op=monoid) + w2 = Vector.from_coo([0, 1, 3], [1, 2, -4], dtype=dtypes.INT32) assert w1.isequal(w2) # identity may be a value @@ -370,29 +370,29 @@ def plus_plus_x_identity(x=0): assert not mysemiring.is_positional assert mysemiring.name == "my_semiring" - A = Matrix.from_values([0, 0, 1, 1], [0, 1, 0, 1], [1, 2, 3, 4]) - x = Vector.from_values([0, 1], [10, 20]) + A = Matrix.from_coo([0, 0, 1, 1], [0, 1, 0, 1], [1, 2, 3, 4]) + x = Vector.from_coo([0, 1], [10, 20]) y = A.mxv(x, mysemiring).new() assert y.isequal(A.mxv(x, semiring.plus_plus).new()) assert y.isequal(x.vxm(A.T, semiring.plus_plus).new()) - assert y.isequal(Vector.from_values([0, 1], [33, 37])) + assert y.isequal(Vector.from_coo([0, 1], [33, 37])) y = A.mxv(x, mysemiring(1)).new() - assert y.isequal(Vector.from_values([0, 1], [36, 40])) # three extra pluses + assert y.isequal(Vector.from_coo([0, 1], [36, 40])) # three extra pluses y = x.vxm(A.T, mysemiring(1)).new() # same as previous - assert y.isequal(Vector.from_values([0, 1], [36, 40])) + assert y.isequal(Vector.from_coo([0, 1], [36, 40])) y = x.vxm(A.T, mysemiring).new() - assert y.isequal(Vector.from_values([0, 1], [33, 37])) + assert y.isequal(Vector.from_coo([0, 1], [33, 37])) B = A.mxm(A, mysemiring).new() assert B.isequal(A.mxm(A, semiring.plus_plus).new()) - assert B.isequal(Matrix.from_values([0, 0, 1, 1], [0, 1, 0, 1], [7, 9, 11, 13])) + assert B.isequal(Matrix.from_coo([0, 0, 1, 1], [0, 1, 0, 1], [7, 9, 11, 13])) B = A.mxm(A, mysemiring(1)).new() # three extra pluses - assert B.isequal(Matrix.from_values([0, 0, 1, 1], [0, 1, 0, 1], [10, 12, 14, 16])) + assert B.isequal(Matrix.from_coo([0, 0, 1, 1], [0, 1, 0, 1], [10, 12, 14, 16])) with pytest.raises(TypeError, match="Expected type: BinaryOp, Monoid"): A.ewise_add(A, mysemiring) @@ -447,10 +447,8 @@ def other_identity(y=0): operator.ParameterizedSemiring("bad_semiring", monoid.plus, monoid.plus) # While we're here, let's check misc Matrix operations - Adup = Matrix.from_values([0, 0, 0, 1, 1], [0, 0, 1, 0, 1], [100, 1, 2, 3, 4], dup_op=bin_op) - Adup2 = Matrix.from_values( - [0, 0, 0, 1, 1], [0, 0, 1, 0, 1], [100, 1, 2, 3, 4], dup_op=binary.plus - ) + Adup = Matrix.from_coo([0, 0, 0, 1, 1], [0, 0, 1, 0, 1], [100, 1, 2, 3, 4], dup_op=bin_op) + Adup2 = Matrix.from_coo([0, 0, 0, 1, 1], [0, 0, 1, 0, 1], [100, 1, 2, 3, 4], dup_op=binary.plus) assert Adup.isequal(Adup2) def plus_x(x=0): @@ -499,9 +497,9 @@ def is_positive(x): FP64, BOOL, } - v = Vector.from_values([0, 1, 3], [1, 2, -4], dtype=dtypes.INT32) + v = Vector.from_coo([0, 1, 3], [1, 2, -4], dtype=dtypes.INT32) w = v.apply(unary.is_positive).new() - result = Vector.from_values([0, 1, 3], [True, True, False], dtype=dtypes.BOOL) + result = Vector.from_coo([0, 1, 3], [True, True, False], dtype=dtypes.BOOL) assert w.isequal(result) @@ -527,10 +525,10 @@ def times_minus_sum(x, y): if dtypes._supports_complex: comp_set.update({FC32, FC64}) assert set(binary.bin_test_func.types) == comp_set - v1 = Vector.from_values([0, 1, 3], [1, 2, -4], dtype=dtypes.INT32) - v2 = Vector.from_values([0, 2, 3], [2, 3, 7], dtype=dtypes.INT32) + v1 = Vector.from_coo([0, 1, 3], [1, 2, -4], dtype=dtypes.INT32) + v2 = Vector.from_coo([0, 2, 3], [2, 3, 7], dtype=dtypes.INT32) w = v1.ewise_add(v2, binary.bin_test_func).new() - result = Vector.from_values([0, 1, 2, 3], [-1, 2, 3, -31], dtype=dtypes.INT32) + result = Vector.from_coo([0, 1, 2, 3], [-1, 2, 3, -31], dtype=dtypes.INT32) assert w.isequal(result) @@ -556,10 +554,10 @@ def plus_plus_one(x, y): if dtypes._supports_complex: comp_set.update({FC32, FC64}) assert set(monoid.plus_plus_one.types) == comp_set - v1 = Vector.from_values([0, 1, 3], [1, 2, -4], dtype=dtypes.INT32) - v2 = Vector.from_values([0, 2, 3], [2, 3, 7], dtype=dtypes.INT32) + v1 = Vector.from_coo([0, 1, 3], [1, 2, -4], dtype=dtypes.INT32) + v2 = Vector.from_coo([0, 2, 3], [2, 3, 7], dtype=dtypes.INT32) w = v1.ewise_add(v2, monoid.plus_plus_one).new() - result = Vector.from_values([0, 1, 2, 3], [4, 2, 3, 4], dtype=dtypes.INT32) + result = Vector.from_coo([0, 1, 2, 3], [4, 2, 3, 4], dtype=dtypes.INT32) assert w.isequal(result) with pytest.raises(DomainMismatch): @@ -574,30 +572,30 @@ def plus_plus_two(x, y): BinaryOp.register_new("plus_plus_two", plus_plus_two) Semiring.register_new("extra_twos", monoid.plus, binary.plus_plus_two) - v = Vector.from_values([0, 1, 3], [1, 2, -4], dtype=dtypes.INT32) - A = Matrix.from_values( + v = Vector.from_coo([0, 1, 3], [1, 2, -4], dtype=dtypes.INT32) + A = Matrix.from_coo( [0, 0, 0, 0, 3, 3, 3, 3], [0, 1, 2, 3, 0, 1, 2, 3], [2, 3, 4, 5, 6, 7, 8, 9], dtype=dtypes.INT32, ) w = v.vxm(A, semiring.extra_twos).new() - result = Vector.from_values([0, 1, 2, 3], [9, 11, 13, 15], dtype=dtypes.INT32) + result = Vector.from_coo([0, 1, 2, 3], [9, 11, 13, 15], dtype=dtypes.INT32) assert w.isequal(result) def test_binary_updates(): assert not hasattr(binary, "div") assert binary.cdiv["INT64"].gb_obj == lib.GrB_DIV_INT64 - vec1 = Vector.from_values([0], [1], dtype=dtypes.INT64) - vec2 = Vector.from_values([0], [2], dtype=dtypes.INT64) + vec1 = Vector.from_coo([0], [1], dtype=dtypes.INT64) + vec2 = Vector.from_coo([0], [2], dtype=dtypes.INT64) result = vec1.ewise_mult(vec2, binary.truediv).new() - assert result.isclose(Vector.from_values([0], [0.5], dtype=dtypes.FP64), check_dtype=True) - vec4 = Vector.from_values([0], [-3], dtype=dtypes.INT64) + assert result.isclose(Vector.from_coo([0], [0.5], dtype=dtypes.FP64), check_dtype=True) + vec4 = Vector.from_coo([0], [-3], dtype=dtypes.INT64) result2 = vec4.ewise_mult(vec2, binary.cdiv).new() - assert result2.isequal(Vector.from_values([0], [-1], dtype=dtypes.INT64), check_dtype=True) + assert result2.isequal(Vector.from_coo([0], [-1], dtype=dtypes.INT64), check_dtype=True) result3 = vec4.ewise_mult(vec2, binary.floordiv).new() - assert result3.isequal(Vector.from_values([0], [-2], dtype=dtypes.INT64), check_dtype=True) + assert result3.isequal(Vector.from_coo([0], [-2], dtype=dtypes.INT64), check_dtype=True) @pytest.mark.slow @@ -626,9 +624,9 @@ def plus_three(x): comp_set.update({FC32, FC64}) assert set(unary.incrementers.plus_three.types) == comp_set - v = Vector.from_values([0, 1, 3], [1, 2, -4], dtype=dtypes.INT32) + v = Vector.from_coo([0, 1, 3], [1, 2, -4], dtype=dtypes.INT32) v << v.apply(unary.incrementers.plus_three) - result = Vector.from_values([0, 1, 3], [4, 5, -1], dtype=dtypes.INT32) + result = Vector.from_coo([0, 1, 3], [4, 5, -1], dtype=dtypes.INT32) assert v.isequal(result), v def plus_four(x): @@ -638,7 +636,7 @@ def plus_four(x): assert hasattr(unary.incrementers, "plus_four") assert hasattr(op.incrementers, "plus_four") # Also save it to `graphblas.op`! v << v.apply(unary.incrementers.plus_four) # this is in addition to the plus_three earlier - result2 = Vector.from_values([0, 1, 3], [8, 9, 3], dtype=dtypes.INT32) + result2 = Vector.from_coo([0, 1, 3], [8, 9, 3], dtype=dtypes.INT32) assert v.isequal(result2), v def bad_will_overwrite_path(x): @@ -849,8 +847,8 @@ def test_binaryop_superset_monoids(): def test_div_semirings(): assert not hasattr(semiring, "plus_div") - A1 = Matrix.from_values([0, 1], [0, 0], [-1, -3]) - A2 = Matrix.from_values([0, 1], [0, 0], [2, 2]) + A1 = Matrix.from_coo([0, 1], [0, 0], [-1, -3]) + A2 = Matrix.from_coo([0, 1], [0, 0], [2, 2]) result = A1.T.mxm(A2, semiring.plus_cdiv).new() assert result[0, 0].new() == -1 assert result.dtype == dtypes.INT64 @@ -1116,7 +1114,7 @@ def _udt_getx(val): udt_getx = UnaryOp.register_anonymous(_udt_getx, "udt_getx", is_udt=True) assert udt in udt_getx result = v.apply(udt_getx).new() - expected = Vector.from_values([0, 1, 2], 0) + expected = Vector.from_coo([0, 1, 2], 0) assert result.isequal(expected) def _udt_index(val, idx, _, thunk): # pragma: no cover (numba) @@ -1127,7 +1125,7 @@ def _udt_index(val, idx, _, thunk): # pragma: no cover (numba) _udt_index = IndexUnaryOp.register_anonymous(_udt_index, "_udt_index", is_udt=True) assert udt in _udt_index result = v.apply(_udt_index, 3).new() - expected = Vector.from_values([0, 1, 2], [3, -3, -3]) + expected = Vector.from_coo([0, 1, 2], [3, -3, -3]) assert result.isequal(expected) def _udt_first(x, y): @@ -1170,7 +1168,7 @@ def _udt_first(x, y): assert result.isequal(expected) result = indexunary.rowindex(v).new() - assert result.isequal(Vector.from_values([0, 1, 2], [0, 1, 2])) + assert result.isequal(Vector.from_coo([0, 1, 2], [0, 1, 2])) result = select.rowle(v, 2).new() assert result.isequal(v) @@ -1241,19 +1239,19 @@ def test_binaryop_commute_exists(): def test_binom(): - v = Vector.from_values([0, 1, 2], [3, 4, 5]) + v = Vector.from_coo([0, 1, 2], [3, 4, 5]) result = v.apply(binary.binom, 2).new() - expected = Vector.from_values([0, 1, 2], [3, 6, 10]) + expected = Vector.from_coo([0, 1, 2], [3, 6, 10]) assert result.isequal(expected) assert op.binom is binary.binom def test_builtins(): - v1 = Vector.from_values([0, 1, 2], [1, 2, 3]) - v2 = Vector.from_values([0, 1, 2], [3, 2, 1]) + v1 = Vector.from_coo([0, 1, 2], [1, 2, 3]) + v2 = Vector.from_coo([0, 1, 2], [3, 2, 1]) result = v1.ewise_mult(v2, min).new() - expected = Vector.from_values([0, 1, 2], [1, 2, 1]) + expected = Vector.from_coo([0, 1, 2], [1, 2, 1]) assert result.isequal(expected) v1(max) << v2 - expected = Vector.from_values([0, 1, 2], [3, 2, 3]) + expected = Vector.from_coo([0, 1, 2], [3, 2, 3]) assert v1.isequal(expected) diff --git a/graphblas/tests/test_pickle.py b/graphblas/tests/test_pickle.py index 9cdc15fd1..8a6684d49 100644 --- a/graphblas/tests/test_pickle.py +++ b/graphblas/tests/test_pickle.py @@ -62,7 +62,7 @@ def test_deserialize(extra): @pytest.mark.slow def test_serialize(): - v = gb.Vector.from_values([1], 2) + v = gb.Vector.from_coo([1], 2) # unary_pickle = gb.core.operator.UnaryOp.register_new("unary_pickle", unarypickle) # binary_pickle = gb.core.operator.BinaryOp.register_new("binary_pickle", binarypickle) @@ -81,8 +81,8 @@ def test_serialize(): "scalar": gb.Scalar.from_value(2), "empty_scalar": gb.Scalar(bool), "vector": v, - "matrix": gb.Matrix.from_values([2], [3], 4), - "matrix.T": gb.Matrix.from_values([3], [4], 5).T, + "matrix": gb.Matrix.from_coo([2], [3], 4), + "matrix.T": gb.Matrix.from_coo([3], [4], 5).T, "vector.S": v.S, "vector.V": v.V, "~vector.S": ~v.S, @@ -140,12 +140,12 @@ def test_serialize(): def check_values(d): - v = gb.Vector.from_values([1], 2) + v = gb.Vector.from_coo([1], 2) assert d["scalar"].isequal(gb.Scalar.from_value(2), check_dtype=True) assert d["empty_scalar"].isequal(gb.Scalar(bool), check_dtype=True) assert d["vector"].isequal(v, check_dtype=True) - assert d["matrix"].isequal(gb.Matrix.from_values([2], [3], 4), check_dtype=True) - assert d["matrix.T"].isequal(gb.Matrix.from_values([3], [4], 5).T, check_dtype=True) + assert d["matrix"].isequal(gb.Matrix.from_coo([2], [3], 4), check_dtype=True) + assert d["matrix.T"].isequal(gb.Matrix.from_coo([3], [4], 5).T, check_dtype=True) assert type(d["vector.S"]) is gb.core.mask.StructuralMask assert d["vector.S"].parent.isequal(v, check_dtype=True) assert type(d["vector.V"]) is gb.core.mask.ValueMask diff --git a/graphblas/tests/test_prefix_scan.py b/graphblas/tests/test_prefix_scan.py index 361dd8ae3..ae1656f38 100644 --- a/graphblas/tests/test_prefix_scan.py +++ b/graphblas/tests/test_prefix_scan.py @@ -72,14 +72,14 @@ def test_scan_vector(length, do_random): @pytest.mark.skipif("not suitesparse") def test_cumprod(): - v = Vector.from_values([1, 3, 4, 6], [2, 3, 4, 5]) - expected = Vector.from_values([1, 3, 4, 6], [2, 6, 24, 120]) + v = Vector.from_coo([1, 3, 4, 6], [2, 3, 4, 5]) + expected = Vector.from_coo([1, 3, 4, 6], [2, 6, 24, 120]) r = v.ss.scan(monoid.times) assert r.isequal(expected) @pytest.mark.skipif("not suitesparse") def test_bad_scan(): - v = Vector.from_values(range(10), range(10)) + v = Vector.from_coo(range(10), range(10)) with pytest.raises(TypeError, match="Bad type for argument `op`"): v.ss.scan(op=binary.first) diff --git a/graphblas/tests/test_pygraphblas.py b/graphblas/tests/test_pygraphblas.py index 3bb9c2d7c..1f062a07f 100644 --- a/graphblas/tests/test_pygraphblas.py +++ b/graphblas/tests/test_pygraphblas.py @@ -14,13 +14,13 @@ def test_pygraphblas_matrix(): # pragma: no cover (outdated) return import pygraphblas as pg - A = gb.Matrix.from_values([0, 1], [0, 1], [0, 1]) + A = gb.Matrix.from_coo([0, 1], [0, 1], [0, 1]) pgA = A.to_pygraphblas() assert isinstance(pgA, pg.Matrix) assert A.gb_obj is gb.core.NULL pgA += 1 A2 = gb.Matrix.from_pygraphblas(pgA) - assert A2.isequal(gb.Matrix.from_values([0, 1], [0, 1], [1, 2])) + assert A2.isequal(gb.Matrix.from_coo([0, 1], [0, 1], [1, 2])) assert A.dtype == A2.dtype @@ -30,13 +30,13 @@ def test_pygraphblas_vector(): # pragma: no cover (outdated) return import pygraphblas as pg - v = gb.Vector.from_values([0, 2], [0, 1]) + v = gb.Vector.from_coo([0, 2], [0, 1]) pgv = v.to_pygraphblas() assert isinstance(pgv, pg.Vector) assert v.gb_obj is gb.core.NULL pgv += 1 v2 = gb.Vector.from_pygraphblas(pgv) - assert v2.isequal(gb.Vector.from_values([0, 2], [1, 2])) + assert v2.isequal(gb.Vector.from_coo([0, 2], [1, 2])) assert v.dtype == v2.dtype diff --git a/graphblas/tests/test_recorder.py b/graphblas/tests/test_recorder.py index 3546d9f45..e10a8dad9 100644 --- a/graphblas/tests/test_recorder.py +++ b/graphblas/tests/test_recorder.py @@ -13,8 +13,8 @@ def switch(): def test_recorder(): - A = gb.Matrix.from_values([0, 1], [1, 1], [1, 2], name="A") - B = gb.Matrix.from_values([0, 1], [0, 1], [3, 4], name="B") + A = gb.Matrix.from_coo([0, 1], [1, 1], [1, 2], name="A") + B = gb.Matrix.from_coo([0, 1], [0, 1], [3, 4], name="B") with gb.Recorder() as rec: assert rec.is_recording C = A.mxm(B).new(name="C") @@ -242,7 +242,7 @@ def test_record_failed_call(): @pytest.mark.skipif("not suitesparse") def test_ss_record_inner(switch): - v = gb.Vector.from_values([0, 1, 2], 1, size=3) + v = gb.Vector.from_coo([0, 1, 2], 1, size=3) with gb.Recorder() as rec: (v @ v).new(name="s_0") if switch: diff --git a/graphblas/tests/test_resolving.py b/graphblas/tests/test_resolving.py index 74fdcca26..ac9897d3f 100644 --- a/graphblas/tests/test_resolving.py +++ b/graphblas/tests/test_resolving.py @@ -8,69 +8,69 @@ from graphblas import Matrix, Scalar, Vector # isort:skip (for dask-graphblas) -def test_from_values_dtype_resolving(): - u = Vector.from_values([0, 1, 2], [1, 2, 3], dtype=dtypes.INT32) +def test_from_coo_dtype_resolving(): + u = Vector.from_coo([0, 1, 2], [1, 2, 3], dtype=dtypes.INT32) assert u.dtype == "INT32" - u = Vector.from_values([0, 1, 2], [1, 2, 3], dtype="INT32") + u = Vector.from_coo([0, 1, 2], [1, 2, 3], dtype="INT32") assert u.dtype == dtypes.INT32 - M = Matrix.from_values([0, 1, 2], [2, 0, 1], [0, 2, 3], dtype=dtypes.UINT8) + M = Matrix.from_coo([0, 1, 2], [2, 0, 1], [0, 2, 3], dtype=dtypes.UINT8) assert M.dtype == "UINT8" - M = Matrix.from_values([0, 1, 2], [2, 0, 1], [0, 2, 3], dtype=float) + M = Matrix.from_coo([0, 1, 2], [2, 0, 1], [0, 2, 3], dtype=float) assert M.dtype == dtypes.FP64 -def test_from_values_invalid_dtype(): +def test_from_coo_invalid_dtype(): # We now rely on numpy to coerce the data - A = Matrix.from_values([0, 1, 2], [2, 0, 1], [0, 2, 3], dtype=dtypes.BOOL) - expected = Matrix.from_values([0, 1, 2], [2, 0, 1], [False, True, True], dtype=dtypes.BOOL) + A = Matrix.from_coo([0, 1, 2], [2, 0, 1], [0, 2, 3], dtype=dtypes.BOOL) + expected = Matrix.from_coo([0, 1, 2], [2, 0, 1], [False, True, True], dtype=dtypes.BOOL) assert A.isequal(expected) with pytest.raises(ValueError, match="object dtype for values is not allowed"): - Matrix.from_values([0, 1, 2], [2, 0, 1], [0, 2, object()]) + Matrix.from_coo([0, 1, 2], [2, 0, 1], [0, 2, object()]) def test_resolve_ops_using_common_dtype(): # C << A.ewise_mult(B, binary.plus) <-- PLUS should use FP64 because unify(INT64, FP64) -> FP64 - u = Vector.from_values([0, 1, 3], [1, 2, 3], dtype=dtypes.INT64) - v = Vector.from_values([0, 1, 3], [0.1, 0.1, 0.1], dtype="FP64") + u = Vector.from_coo([0, 1, 3], [1, 2, 3], dtype=dtypes.INT64) + v = Vector.from_coo([0, 1, 3], [0.1, 0.1, 0.1], dtype="FP64") w = Vector("FP32", u.size) w << u.ewise_mult(v, binary.plus) - result = Vector.from_values([0, 1, 3], [1.1, 2.1, 3.1], dtype="FP32") + result = Vector.from_coo([0, 1, 3], [1.1, 2.1, 3.1], dtype="FP32") assert w.isclose(result, check_dtype=True) def test_order_of_updater_params_does_not_matter(): - u = Vector.from_values([0, 1, 3], [1, 2, 3]) - mask = Vector.from_values([0, 3], [True, True]) + u = Vector.from_coo([0, 1, 3], [1, 2, 3]) + mask = Vector.from_coo([0, 3], [True, True]) accum = binary.plus - result = Vector.from_values([0, 3], [5, 10]) + result = Vector.from_coo([0, 3], [5, 10]) # mask, accum, replace= - v = Vector.from_values([0, 1, 2, 3], [4, 3, 2, 1]) + v = Vector.from_coo([0, 1, 2, 3], [4, 3, 2, 1]) v(mask.V, accum, replace=True) << u.ewise_mult(u, binary.times) assert v.isequal(result) # accum, mask, replace= - v = Vector.from_values([0, 1, 2, 3], [4, 3, 2, 1]) + v = Vector.from_coo([0, 1, 2, 3], [4, 3, 2, 1]) v(accum, mask.V, replace=True) << u.ewise_mult(u, binary.times) assert v.isequal(result) # accum, mask=, replace= - v = Vector.from_values([0, 1, 2, 3], [4, 3, 2, 1]) + v = Vector.from_coo([0, 1, 2, 3], [4, 3, 2, 1]) v(accum, mask=mask.V, replace=True) << u.ewise_mult(u, binary.times) assert v.isequal(result) # mask, accum=, replace= - v = Vector.from_values([0, 1, 2, 3], [4, 3, 2, 1]) + v = Vector.from_coo([0, 1, 2, 3], [4, 3, 2, 1]) v(mask.V, accum=accum, replace=True) << u.ewise_mult(u, binary.times) assert v.isequal(result) # replace=, mask=, accum= - v = Vector.from_values([0, 1, 2, 3], [4, 3, 2, 1]) + v = Vector.from_coo([0, 1, 2, 3], [4, 3, 2, 1]) v(replace=True, mask=mask.V, accum=accum) << u.ewise_mult(u, binary.times) assert v.isequal(result) # replace, mask=, accum= - v = Vector.from_values([0, 1, 2, 3], [4, 3, 2, 1]) + v = Vector.from_coo([0, 1, 2, 3], [4, 3, 2, 1]) v(replace, mask=mask.V, accum=accum) << u.ewise_mult(u, binary.times) assert v.isequal(result) def test_updater_replace_no_mask(): - u = Vector.from_values([0, 1, 2], [1, 2, 3]) + u = Vector.from_coo([0, 1, 2], [1, 2, 3]) with pytest.raises( TypeError, match="'replace' argument may only be True if a mask is provided" ): @@ -87,9 +87,9 @@ def test_replace_repr(): def test_updater_repeat_argument_types(): - mask = Vector.from_values([0, 3], [True, True]) + mask = Vector.from_coo([0, 3], [True, True]) accum = binary.plus - v = Vector.from_values([0, 1, 2, 3], [4, 3, 2, 1]) + v = Vector.from_coo([0, 1, 2, 3], [4, 3, 2, 1]) with pytest.raises(TypeError, match="multiple"): v(mask.S, mask.S) with pytest.raises(TypeError, match="multiple"): @@ -101,8 +101,8 @@ def test_updater_repeat_argument_types(): def test_updater_bad_types(): - v = Vector.from_values([0, 1, 2, 3], [4, 3, 2, 1]) - M = Matrix.from_values([0, 1, 2], [2, 0, 1], [0, 2, 3], dtype=dtypes.UINT8) + v = Vector.from_coo([0, 1, 2, 3], [4, 3, 2, 1]) + M = Matrix.from_coo([0, 1, 2], [2, 0, 1], [0, 2, 3], dtype=dtypes.UINT8) with pytest.raises(TypeError, match="Invalid mask"): v(mask=object()) with pytest.raises(TypeError, match="Invalid mask"): @@ -117,25 +117,25 @@ def test_updater_bad_types(): def test_already_resolved_ops_allowed_in_updater(): # C(binary.plus['FP64']) << ... - u = Vector.from_values([0, 1, 3], [1, 2, 3]) + u = Vector.from_coo([0, 1, 3], [1, 2, 3]) u(binary.plus["INT64"]) << u.ewise_mult(u, binary.times["INT64"]) - result = Vector.from_values([0, 1, 3], [2, 6, 12]) + result = Vector.from_coo([0, 1, 3], [2, 6, 12]) assert u.isequal(result) def test_updater_returns_updater(): - u = Vector.from_values([0, 1, 3], [1, 2, 3]) + u = Vector.from_coo([0, 1, 3], [1, 2, 3]) y = u(accum=binary.times) assert isinstance(y, Updater) z = y << u.apply(unary.ainv) assert z is None assert isinstance(y, Updater) - final_result = Vector.from_values([0, 1, 3], [-1, -4, -9]) + final_result = Vector.from_coo([0, 1, 3], [-1, -4, -9]) assert u.isequal(final_result) def test_updater_only_once(): - u = Vector.from_values([0, 1, 3], [1, 2, 3]) + u = Vector.from_coo([0, 1, 3], [1, 2, 3]) with pytest.raises(TypeError, match="'Assigner' object is not callable"): u()[0]() with pytest.raises(TypeError, match="'Assigner' object is not callable"): @@ -156,7 +156,7 @@ def test_updater_only_once(): def test_bad_extract_with_updater(): - u = Vector.from_values([0, 1, 3], [1, 2, 3]) + u = Vector.from_coo([0, 1, 3], [1, 2, 3]) assert u[0].new() == 1 with pytest.raises(AttributeError, match="'Assigner' object has no attribute 'value'"): u(mask=u.S)[0].value @@ -180,7 +180,7 @@ def test_bad_extract_with_updater(): def test_updater_on_rhs(): - u = Vector.from_values([0, 1, 3], [1, 2, 3]) + u = Vector.from_coo([0, 1, 3], [1, 2, 3]) with pytest.raises(TypeError, match="Assignment value must be a valid expression"): u << u(mask=u.S) with pytest.raises(TypeError, match="Assignment value must be a valid expression"): @@ -190,7 +190,7 @@ def test_updater_on_rhs(): def test_py_indices(): - v = Vector.from_values(np.arange(5), np.arange(5)) + v = Vector.from_coo(np.arange(5), np.arange(5)) idx = v[:].resolved_indexes.py_indices assert idx == slice(None) @@ -216,22 +216,22 @@ def test_py_indices(): idx = v[::-1].resolved_indexes.py_indices assert idx == slice(None, None, -1) w = v[idx].new() - expected = Vector.from_values(np.arange(5), np.arange(5)[::-1]) + expected = Vector.from_coo(np.arange(5), np.arange(5)[::-1]) assert w.isequal(expected) idx = v[4:-3:-1].resolved_indexes.py_indices assert idx == slice(None, -v.size - 1 + 3, -1) w = v[idx].new() - expected = Vector.from_values(np.arange(2), np.arange(5)[4:-3:-1]) + expected = Vector.from_coo(np.arange(2), np.arange(5)[4:-3:-1]) assert w.isequal(expected) idx = v[1::2].resolved_indexes.py_indices assert idx == slice(1, None, 2) w = v[idx].new() - expected = Vector.from_values(np.arange(2), np.arange(5)[1::2]) + expected = Vector.from_coo(np.arange(2), np.arange(5)[1::2]) assert w.isequal(expected) - A = Matrix.from_values([0, 1, 2], [2, 0, 1], [0, 2, 3], nrows=10, ncols=10) + A = Matrix.from_coo([0, 1, 2], [2, 0, 1], [0, 2, 3], nrows=10, ncols=10) idx = A[1:6, 8:-8:-2].resolved_indexes.py_indices assert idx == (slice(1, 6), slice(8, -8, -2)) @@ -250,8 +250,8 @@ def test_py_indices(): def test_ensure_type(): - v = Vector.from_values([0, 1, 2], [1, 2, 3]) - A = Matrix.from_values([0, 1, 2], [2, 0, 1], [0, 2, 3]) + v = Vector.from_coo([0, 1, 2], [1, 2, 3]) + A = Matrix.from_coo([0, 1, 2], [2, 0, 1], [0, 2, 3]) assert ensure_type(v, Vector) is v assert ensure_type(A, Matrix) is A assert ensure_type(v, (Matrix, Vector)) is v diff --git a/graphblas/tests/test_scalar.py b/graphblas/tests/test_scalar.py index 58e7280ef..27b906c86 100644 --- a/graphblas/tests/test_scalar.py +++ b/graphblas/tests/test_scalar.py @@ -316,7 +316,7 @@ def test_wait(s): @autocompute def test_expr_is_like_scalar(s): - v = Vector.from_values([1], [2]) + v = Vector.from_coo([1], [2]) attrs = {attr for attr, val in inspect.getmembers(s)} expr_attrs = {attr for attr, val in inspect.getmembers(v.inner(v))} infix_attrs = {attr for attr, val in inspect.getmembers(v @ v)} @@ -351,7 +351,7 @@ def test_expr_is_like_scalar(s): @autocompute def test_index_expr_is_like_scalar(s): - v = Vector.from_values([1], [2]) + v = Vector.from_coo([1], [2]) attrs = {attr for attr, val in inspect.getmembers(s)} expr_attrs = {attr for attr, val in inspect.getmembers(v[0])} # Should we make any of these raise informative errors? @@ -381,7 +381,7 @@ def test_index_expr_is_like_scalar(s): def test_ndim(s): assert s.ndim == 0 - v = Vector.from_values([1], [2]) + v = Vector.from_coo([1], [2]) assert v.inner(v).ndim == 0 assert (v @ v).ndim == 0 @@ -422,7 +422,7 @@ def test_scalar_complex(dtype): @autocompute def test_scalar_expr(s): - v = Vector.from_values([1], [2]) + v = Vector.from_coo([1], [2]) expr = v.inner(v) t = expr._new_scalar(s.dtype) assert t.is_cscalar is s.is_cscalar @@ -451,13 +451,13 @@ def test_sizeof(s): def test_ss_concat(s): empty = Scalar(int) v = gb.ss.concat([s, s, empty]) - expected = Vector.from_values([0, 1], 5, size=3) + expected = Vector.from_coo([0, 1], 5, size=3) assert v.isequal(expected) A = gb.ss.concat([[s, s, empty]]) - expected = Matrix.from_values([0, 0], [0, 1], 5, nrows=1, ncols=3) + expected = Matrix.from_coo([0, 0], [0, 1], 5, nrows=1, ncols=3) assert A.isequal(expected) A = gb.ss.concat([[s], [s], [empty]]) - expected = Matrix.from_values([0, 1], [0, 0], 5, nrows=3, ncols=1) + expected = Matrix.from_coo([0, 1], [0, 0], 5, nrows=3, ncols=1) assert A.isequal(expected) diff --git a/graphblas/tests/test_ss_utils.py b/graphblas/tests/test_ss_utils.py index 6db577e9e..298a8a53e 100644 --- a/graphblas/tests/test_ss_utils.py +++ b/graphblas/tests/test_ss_utils.py @@ -18,9 +18,9 @@ def test_vector_head(do_iso): values1 = [10, 20, 30] values2 = [2, 4, 6] values3 = [1, 2, 3] - v1 = Vector.from_values([0, 1, 2], values1) # full - v2 = Vector.from_values([1, 3, 5], values2) # bitmap - v3 = Vector.from_values([100, 200, 300], values3) # sparse + v1 = Vector.from_coo([0, 1, 2], values1) # full + v2 = Vector.from_coo([1, 3, 5], values2) # bitmap + v3 = Vector.from_coo([100, 200, 300], values3) # sparse assert v1.ss.export()["format"] == "full" assert v2.ss.export()["format"] == "bitmap" assert v3.ss.export()["format"] == "sparse" @@ -71,10 +71,10 @@ def test_matrix_head(do_iso): values2 = [1, 2, 4] values3 = values4 = [1, 2, 3] - A1 = Matrix.from_values([0, 0, 1, 1], [0, 1, 0, 1], values1) # fullr - A2 = Matrix.from_values([0, 0, 1], [0, 1, 1], values2) # Bitmap - A3 = Matrix.from_values([5, 5, 10], [4, 5, 10], values3) # CSR - A4 = Matrix.from_values([500, 500, 1000], [400, 500, 1000], values4) # HyperCSR + A1 = Matrix.from_coo([0, 0, 1, 1], [0, 1, 0, 1], values1) # fullr + A2 = Matrix.from_coo([0, 0, 1], [0, 1, 1], values2) # Bitmap + A3 = Matrix.from_coo([5, 5, 10], [4, 5, 10], values3) # CSR + A4 = Matrix.from_coo([500, 500, 1000], [400, 500, 1000], values4) # HyperCSR d = A1.ss.export(raw=True) assert d["format"] == "fullr" d["format"] = "fullc" diff --git a/graphblas/tests/test_vector.py b/graphblas/tests/test_vector.py index f6b84101c..4ba968df9 100644 --- a/graphblas/tests/test_vector.py +++ b/graphblas/tests/test_vector.py @@ -34,13 +34,13 @@ def A(): [0, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6], [3, 2, 3, 1, 5, 3, 7, 8, 3, 1, 7, 4], ] - return Matrix.from_values(*data) + return Matrix.from_coo(*data) @pytest.fixture def v(): data = [[1, 3, 4, 6], [1, 1, 2, 0]] - return Vector.from_values(*data) + return Vector.from_coo(*data) def test_new(): @@ -51,13 +51,13 @@ def test_new(): def test_large_vector(): - u = Vector.from_values([0, 2**59], [0, 1]) + u = Vector.from_coo([0, 2**59], [0, 1]) assert u.size == 2**59 + 1 assert u[2**59].new() == 1 with pytest.raises(InvalidValue): - Vector.from_values([0, 2**64 - 2], [0, 1]) + Vector.from_coo([0, 2**64 - 2], [0, 1]) with pytest.raises(OverflowError): - Vector.from_values([0, 2**64], [0, 1]) + Vector.from_coo([0, 2**64], [0, 1]) def test_dup(v): @@ -70,13 +70,13 @@ def test_dup(v): v[0] = 1000 assert u[0].new() != 1000 # extended functionality - w = Vector.from_values([0, 1], [0, 2.5], dtype=dtypes.FP64) + w = Vector.from_coo([0, 1], [0, 2.5], dtype=dtypes.FP64) x = w.dup(dtype=dtypes.INT64) - assert x.isequal(Vector.from_values([0, 1], [0, 2], dtype=dtypes.INT64), check_dtype=True) + assert x.isequal(Vector.from_coo([0, 1], [0, 2], dtype=dtypes.INT64), check_dtype=True) x = w.dup(mask=w.V) - assert x.isequal(Vector.from_values([1], [2.5], dtype=dtypes.FP64), check_dtype=True) + assert x.isequal(Vector.from_coo([1], [2.5], dtype=dtypes.FP64), check_dtype=True) x = w.dup(dtype=dtypes.INT64, mask=w.V) - assert x.isequal(Vector.from_values([1], [2], dtype=dtypes.INT64), check_dtype=True) + assert x.isequal(Vector.from_coo([1], [2], dtype=dtypes.INT64), check_dtype=True) def test_dup_clear(v): @@ -90,53 +90,53 @@ def test_dup_clear(v): assert x.size == v.size -def test_from_values(): - u = Vector.from_values([0, 1, 3], [True, False, True]) +def test_from_coo(): + u = Vector.from_coo([0, 1, 3], [True, False, True]) assert u.size == 4 assert u.nvals == 3 assert u.dtype == bool - u2 = Vector.from_values([0, 1, 3], [12.3, 12.4, 12.5], size=17) + u2 = Vector.from_coo([0, 1, 3], [12.3, 12.4, 12.5], size=17) assert u2.size == 17 assert u2.nvals == 3 assert u2.dtype == float - u3 = Vector.from_values([0, 1, 1], [1, 2, 3], size=10, dup_op=binary.times) + u3 = Vector.from_coo([0, 1, 1], [1, 2, 3], size=10, dup_op=binary.times) assert u3.size == 10 assert u3.nvals == 2 # duplicates were combined assert u3.dtype == int assert u3[1].new() == 6 # 2*3 with pytest.raises(ValueError, match="Duplicate indices found"): # Duplicate indices requires a dup_op - Vector.from_values([0, 1, 1], [True, True, True]) + Vector.from_coo([0, 1, 1], [True, True, True]) with pytest.raises(ValueError, match="No indices provided. Unable to infer size."): - Vector.from_values([], []) + Vector.from_coo([], []) # Changed: Assume empty value is float64 (like numpy) # with pytest.raises(ValueError, match="No values provided. Unable to determine type"): - w = Vector.from_values([], [], size=10) + w = Vector.from_coo([], [], size=10) assert w.size == 10 assert w.nvals == 0 assert w.dtype == dtypes.FP64 with pytest.raises(ValueError, match="No indices provided. Unable to infer size"): - Vector.from_values([], [], dtype=dtypes.INT64) - u4 = Vector.from_values([], [], size=10, dtype=dtypes.INT64) + Vector.from_coo([], [], dtype=dtypes.INT64) + u4 = Vector.from_coo([], [], size=10, dtype=dtypes.INT64) u5 = Vector(dtypes.INT64, size=10) assert u4.isequal(u5, check_dtype=True) # we check index dtype if given numpy array with pytest.raises(ValueError, match="indices must be integers, not float64"): - Vector.from_values(np.array([1.2, 3.4]), [1, 2]) + Vector.from_coo(np.array([1.2, 3.4]), [1, 2]) # but coerce index if given Python lists (we defer to numpy casting) - u6 = Vector.from_values([1.2, 3.4], [1, 2]) - assert u6.isequal(Vector.from_values([1, 3], [1, 2])) + u6 = Vector.from_coo([1.2, 3.4], [1, 2]) + assert u6.isequal(Vector.from_coo([1, 3], [1, 2])) # mis-matched sizes with pytest.raises(ValueError, match="`indices` and `values` lengths must match"): - Vector.from_values([0], [1, 2]) + Vector.from_coo([0], [1, 2]) -def test_from_values_scalar(): - u = Vector.from_values([0, 1, 3], 7) +def test_from_coo_scalar(): + u = Vector.from_coo([0, 1, 3], 7) assert u.size == 4 assert u.nvals == 3 assert u.dtype == dtypes.INT64 @@ -146,7 +146,7 @@ def test_from_values_scalar(): assert u.reduce(monoid.any).new() == 7 # ignore duplicate indices; iso trumps duplicates! - u = Vector.from_values([0, 1, 1, 3], 7) + u = Vector.from_coo([0, 1, 1, 3], 7) assert u.size == 4 assert u.nvals == 3 if suitesparse: @@ -154,7 +154,7 @@ def test_from_values_scalar(): assert u.ss.iso_value == 7 assert u.reduce(monoid.any).new() == 7 with pytest.raises(ValueError, match="dup_op must be None"): - Vector.from_values([0, 1, 1, 3], 7, dup_op=binary.plus) + Vector.from_coo([0, 1, 1, 3], 7, dup_op=binary.plus) u[0] = 0 if suitesparse: with pytest.raises(ValueError, match="not iso"): @@ -203,7 +203,7 @@ def test_build(v): v.build([0, 11], [1, 1]) w = Vector(int, size=3) w.build([0, 11], [1, 1], size=12) - assert w.isequal(Vector.from_values([0, 11], [1, 1])) + assert w.isequal(Vector.from_coo([0, 11], [1, 1])) @pytest.mark.skipif("not suitesparse") @@ -217,19 +217,19 @@ def test_ss_build_scalar(v): def test_extract_values(v): - idx, vals = v.to_values() + idx, vals = v.to_coo() np.testing.assert_array_equal(idx, (1, 3, 4, 6)) np.testing.assert_array_equal(vals, (1, 1, 2, 0)) assert idx.dtype == np.uint64 assert vals.dtype == np.int64 - idx, vals = v.to_values(dtype=int) + idx, vals = v.to_coo(dtype=int) np.testing.assert_array_equal(idx, (1, 3, 4, 6)) np.testing.assert_array_equal(vals, (1, 1, 2, 0)) assert idx.dtype == np.uint64 assert vals.dtype == np.int64 - idx, vals = v.to_values(dtype=float) + idx, vals = v.to_coo(dtype=float) np.testing.assert_array_equal(idx, (1, 3, 4, 6)) np.testing.assert_array_equal(vals, (1, 1, 2, 0)) assert idx.dtype == np.uint64 @@ -237,10 +237,10 @@ def test_extract_values(v): def test_extract_input_mask(): - v = Vector.from_values([0, 1, 2], [0, 1, 2]) - m = Vector.from_values([0, 2], [0, 2]) + v = Vector.from_coo([0, 1, 2], [0, 1, 2]) + m = Vector.from_coo([0, 2], [0, 2]) result = v[[0, 1]].new(input_mask=m.S) - expected = Vector.from_values([0], [0], size=2) + expected = Vector.from_coo([0], [0], size=2) assert result.isequal(expected) # again result.clear() @@ -286,47 +286,47 @@ def test_remove_element(v): def test_vxm(v, A): w = v.vxm(A, semiring.plus_times).new() - result = Vector.from_values([0, 2, 3, 4, 5, 6], [3, 3, 0, 8, 14, 4]) + result = Vector.from_coo([0, 2, 3, 4, 5, 6], [3, 3, 0, 8, 14, 4]) assert w.isequal(result) def test_vxm_transpose(v, A): w = v.vxm(A.T, semiring.plus_times).new() - result = Vector.from_values([0, 1, 6], [5, 16, 13]) + result = Vector.from_coo([0, 1, 6], [5, 16, 13]) assert w.isequal(result) def test_vxm_nonsquare(v): - A = Matrix.from_values([0, 3], [0, 1], [10, 20], nrows=7, ncols=2) + A = Matrix.from_coo([0, 3], [0, 1], [10, 20], nrows=7, ncols=2) u = Vector(v.dtype, size=2) u().update(v.vxm(A, semiring.min_plus)) - result = Vector.from_values([1], [21]) + result = Vector.from_coo([1], [21]) assert u.isequal(result) w1 = v.vxm(A, semiring.min_plus).new() assert w1.isequal(u) # Test the transpose case - v2 = Vector.from_values([0, 1], [1, 2]) + v2 = Vector.from_coo([0, 1], [1, 2]) w2 = v2.vxm(A.T, semiring.min_plus).new() assert w2.size == 7 def test_vxm_mask(v, A): - val_mask = Vector.from_values([0, 1, 2, 3, 4], [True, False, False, True, True], size=7) - struct_mask = Vector.from_values([0, 3, 4], [False, False, False], size=7) + val_mask = Vector.from_coo([0, 1, 2, 3, 4], [True, False, False, True, True], size=7) + struct_mask = Vector.from_coo([0, 3, 4], [False, False, False], size=7) u = v.dup() u(struct_mask.S) << v.vxm(A, semiring.plus_times) - result = Vector.from_values([0, 1, 3, 4, 6], [3, 1, 0, 8, 0], size=7) + result = Vector.from_coo([0, 1, 3, 4, 6], [3, 1, 0, 8, 0], size=7) assert u.isequal(result) u = v.dup() u(~~struct_mask.S) << v.vxm(A, semiring.plus_times) assert u.isequal(result) u = v.dup() u(~struct_mask.S) << v.vxm(A, semiring.plus_times) - result2 = Vector.from_values([2, 3, 4, 5, 6], [3, 1, 2, 14, 4], size=7) + result2 = Vector.from_coo([2, 3, 4, 5, 6], [3, 1, 2, 14, 4], size=7) assert u.isequal(result2) u = v.dup() u(replace=True, mask=val_mask.V) << v.vxm(A, semiring.plus_times) - result3 = Vector.from_values([0, 3, 4], [3, 0, 8], size=7) + result3 = Vector.from_coo([0, 3, 4], [3, 0, 8], size=7) assert u.isequal(result3) u = v.dup() u(replace=True, mask=~~val_mask.V) << v.vxm(A, semiring.plus_times) @@ -338,7 +338,7 @@ def test_vxm_mask(v, A): def test_vxm_accum(v, A): w1 = v.dup() w1(binary.plus) << v.vxm(A, semiring.plus_times) - result = Vector.from_values([0, 1, 2, 3, 4, 5, 6], [3, 1, 3, 1, 10, 14, 4], size=7) + result = Vector.from_coo([0, 1, 2, 3, 4, 5, 6], [3, 1, 3, 1, 10, 14, 4], size=7) assert w1.isequal(result) # Allow monoids w2 = v.dup() @@ -358,8 +358,8 @@ def test_vxm_accum(v, A): def test_ewise_mult(v): # Binary, Monoid, and Semiring - v2 = Vector.from_values([0, 3, 5, 6], [2, 3, 2, 1]) - result = Vector.from_values([3, 6], [3, 0]) + v2 = Vector.from_coo([0, 3, 5, 6], [2, 3, 2, 1]) + result = Vector.from_coo([3, 6], [3, 0]) w = v.ewise_mult(v2, binary.times).new() assert w.isequal(result) w << v.ewise_mult(v2, monoid.times) @@ -370,27 +370,27 @@ def test_ewise_mult(v): def test_ewise_mult_change_dtype(v): # We want to divide by 2, converting ints to floats - v2 = Vector.from_values([1, 3, 4, 6], [2, 2, 2, 2]) + v2 = Vector.from_coo([1, 3, 4, 6], [2, 2, 2, 2]) assert v.dtype == dtypes.INT64 assert v2.dtype == dtypes.INT64 - result = Vector.from_values([1, 3, 4, 6], [0.5, 0.5, 1.0, 0], dtype=dtypes.FP64) + result = Vector.from_coo([1, 3, 4, 6], [0.5, 0.5, 1.0, 0], dtype=dtypes.FP64) w = v.ewise_mult(v2, binary.cdiv[dtypes.FP64]).new() assert w.isequal(result), w # Here is the potentially surprising way to do things # Division is still done with ints, but results are then stored as floats - result2 = Vector.from_values([1, 3, 4, 6], [0.0, 0.0, 1.0, 0.0], dtype=dtypes.FP64) + result2 = Vector.from_coo([1, 3, 4, 6], [0.0, 0.0, 1.0, 0.0], dtype=dtypes.FP64) w2 = v.ewise_mult(v2, binary.cdiv).new(dtype=dtypes.FP64) assert w2.isequal(result2), w2 # Try with boolean dtype via auto-conversion - result3 = Vector.from_values([1, 3, 4, 6], [True, True, False, True]) + result3 = Vector.from_coo([1, 3, 4, 6], [True, True, False, True]) w3 = v.ewise_mult(v2, binary.lt).new() assert w3.isequal(result3), w3 def test_ewise_add(v): # Binary, Monoid, and Semiring - v2 = Vector.from_values([0, 3, 5, 6], [2, 3, 2, 1]) - result = Vector.from_values([0, 1, 3, 4, 5, 6], [2, 1, 3, 2, 2, 1]) + v2 = Vector.from_coo([0, 3, 5, 6], [2, 3, 2, 1]) + result = Vector.from_coo([0, 1, 3, 4, 5, 6], [2, 1, 3, 2, 2, 1]) # with pytest.raises(TypeError, match="require_monoid"): v.ewise_add(v2, binary.minus) # okay now w = v.ewise_add(v2, binary.max).new() # ok if the binaryop is part of a monoid @@ -406,8 +406,8 @@ def test_ewise_add(v): result = v.ewise_add(v2, monoid.plus).new() assert w.isequal(result) # what about default for bool? - b1 = Vector.from_values([0, 1, 2, 3], [True, False, True, False]) - b2 = Vector.from_values([0, 1, 2, 3], [True, True, False, False]) + b1 = Vector.from_coo([0, 1, 2, 3], [True, False, True, False]) + b2 = Vector.from_coo([0, 1, 2, 3], [True, True, False, False]) with pytest.raises(KeyError, match="plus does not work"): b1.ewise_add(b2).new() # with pytest.raises(TypeError, match="for BOOL datatype"): @@ -416,7 +416,7 @@ def test_ewise_add(v): def test_extract(v): w = Vector(v.dtype, 3) - result = Vector.from_values([0, 1], [1, 1], size=3) + result = Vector.from_coo([0, 1], [1, 1], size=3) w << v[[1, 3, 5]] assert w.isequal(result) w() << v[1::2] @@ -427,7 +427,7 @@ def test_extract(v): def test_extract_array(v): w = Vector(v.dtype, 3) - result = Vector.from_values(np.array([0, 1]), np.array([1, 1]), size=3) + result = Vector.from_coo(np.array([0, 1]), np.array([1, 1]), size=3) w << v[np.array([1, 3, 5])] assert w.isequal(result) @@ -476,7 +476,7 @@ def test_extract_negative_indices(v): assert v[[-v.size]].new().nvals == 0 assert v[Scalar.from_value(-4)].new() == 1 w = v[[-1, -3]].new() - assert w.isequal(Vector.from_values([0, 1], [0, 2])) + assert w.isequal(Vector.from_coo([0, 1], [0, 2])) with pytest.raises(IndexError): v[-v.size - 1] with pytest.raises(IndexError): @@ -486,8 +486,8 @@ def test_extract_negative_indices(v): def test_assign(v): - u = Vector.from_values([0, 2], [9, 8]) - result = Vector.from_values([0, 1, 3, 4, 6], [9, 1, 1, 8, 0]) + u = Vector.from_coo([0, 2], [9, 8]) + result = Vector.from_coo([0, 1, 3, 4, 6], [9, 1, 1, 8, 0]) w = v.dup() w[[0, 2, 4]] = u assert w.isequal(result) @@ -501,32 +501,32 @@ def test_assign(v): def test_assign_scalar(v): - result = Vector.from_values([1, 3, 4, 5, 6], [9, 9, 2, 9, 0]) + result = Vector.from_coo([1, 3, 4, 5, 6], [9, 9, 2, 9, 0]) w = v.dup() w[[1, 3, 5]] = 9 assert w.isequal(result) w = v.dup() w[1::2] = 9 assert w.isequal(result) - w = Vector.from_values([0, 1, 2], [1, 1, 1]) + w = Vector.from_coo([0, 1, 2], [1, 1, 1]) s = Scalar.from_value(9) w[0] = s - assert w.isequal(Vector.from_values([0, 1, 2], [9, 1, 1])) + assert w.isequal(Vector.from_coo([0, 1, 2], [9, 1, 1])) w[:] = s - assert w.isequal(Vector.from_values([0, 1, 2], [9, 9, 9])) + assert w.isequal(Vector.from_coo([0, 1, 2], [9, 9, 9])) with pytest.raises(TypeError, match="Bad type for arg"): w[:] = object() with pytest.raises(TypeError, match="Bad type for arg"): w[1] = object() w << 2 - assert w.isequal(Vector.from_values([0, 1, 2], [2, 2, 2])) + assert w.isequal(Vector.from_coo([0, 1, 2], [2, 2, 2])) w[0] = Scalar(int) - assert w.isequal(Vector.from_values([1, 2], [2, 2])) + assert w.isequal(Vector.from_coo([1, 2], [2, 2])) def test_assign_scalar_mask(v): - mask = Vector.from_values([1, 2, 5, 6], [0, 0, 1, 0]) - result = Vector.from_values([1, 3, 4, 5, 6], [1, 1, 2, 5, 0]) + mask = Vector.from_coo([1, 2, 5, 6], [0, 0, 1, 0]) + result = Vector.from_coo([1, 3, 4, 5, 6], [1, 1, 2, 5, 0]) w = v.dup() w[:](mask.V) << 5 assert w.isequal(result) @@ -536,7 +536,7 @@ def test_assign_scalar_mask(v): w = v.dup() w(mask.V)[:] << 5 assert w.isequal(result) - result2 = Vector.from_values([0, 1, 2, 3, 4, 6], [5, 5, 5, 5, 5, 5]) + result2 = Vector.from_coo([0, 1, 2, 3, 4, 6], [5, 5, 5, 5, 5, 5]) w = v.dup() w[:](~mask.V) << 5 assert w.isequal(result2) @@ -546,7 +546,7 @@ def test_assign_scalar_mask(v): w = v.dup() w(~mask.V)[:] << 5 assert w.isequal(result2) - result3 = Vector.from_values([1, 2, 3, 4, 5, 6], [5, 5, 1, 2, 5, 5]) + result3 = Vector.from_coo([1, 2, 3, 4, 5, 6], [5, 5, 1, 2, 5, 5]) w = v.dup() w[:](mask.S) << 5 assert w.isequal(result3) @@ -556,7 +556,7 @@ def test_assign_scalar_mask(v): w = v.dup() w(mask.S)[:] << 5 assert w.isequal(result3) - result4 = Vector.from_values([0, 1, 3, 4, 6], [5, 1, 5, 5, 0]) + result4 = Vector.from_coo([0, 1, 3, 4, 6], [5, 1, 5, 5, 0]) w = v.dup() w[:](~mask.S) << 5 assert w.isequal(result4) @@ -569,11 +569,11 @@ def test_assign_scalar_mask(v): def test_subassign(A): - v = Vector.from_values([0, 1, 2], [0, 1, 2]) - w = Vector.from_values([0, 1], [10, 20]) - m = Vector.from_values([1], [True]) + v = Vector.from_coo([0, 1, 2], [0, 1, 2]) + w = Vector.from_coo([0, 1], [10, 20]) + m = Vector.from_coo([1], [True]) v[[0, 1]](m.S) << w - result1 = Vector.from_values([0, 1, 2], [0, 20, 2]) + result1 = Vector.from_coo([0, 1, 2], [0, 20, 2]) assert v.isequal(result1) with pytest.raises(DimensionMismatch): v[[0, 1]](v.S) << w @@ -581,7 +581,7 @@ def test_subassign(A): v[[0, 1]](m.S) << v v[[0, 1]](m.S) << 100 - result2 = Vector.from_values([0, 1, 2], [0, 100, 2]) + result2 = Vector.from_coo([0, 1, 2], [0, 100, 2]) assert v.isequal(result2) with pytest.raises(DimensionMismatch): v[[0, 1]](v.S) << 99 @@ -597,21 +597,21 @@ def test_subassign(A): def test_assign_scalar_with_mask(): - v = Vector.from_values([0, 1, 2], [1, 2, 3]) - m = Vector.from_values([0, 2], [False, True]) - w1 = Vector.from_values([0], [50]) - w3 = Vector.from_values([0, 1, 2], [10, 20, 30]) + v = Vector.from_coo([0, 1, 2], [1, 2, 3]) + m = Vector.from_coo([0, 2], [False, True]) + w1 = Vector.from_coo([0], [50]) + w3 = Vector.from_coo([0, 1, 2], [10, 20, 30]) v(m.V)[:] << w3 - result = Vector.from_values([0, 1, 2], [1, 2, 30]) + result = Vector.from_coo([0, 1, 2], [1, 2, 30]) assert v.isequal(result) v(m.V)[:] << 100 - result = Vector.from_values([0, 1, 2], [1, 2, 100]) + result = Vector.from_coo([0, 1, 2], [1, 2, 100]) assert v.isequal(result) v(m.V, accum=binary.plus)[2] << 1000 - result = Vector.from_values([0, 1, 2], [1, 2, 1100]) + result = Vector.from_coo([0, 1, 2], [1, 2, 1100]) assert v.isequal(result) with pytest.raises(TypeError, match="Single element assign does not accept a submask"): @@ -621,17 +621,17 @@ def test_assign_scalar_with_mask(): v[2](w1.S) << 7 v[[2]](w1.S) << 7 - result = Vector.from_values([0, 1, 2], [1, 2, 7]) + result = Vector.from_coo([0, 1, 2], [1, 2, 7]) assert v.isequal(result) def test_assign_list(): v = Vector(int, 4) v[[0, 1]] = [2, 3] - expected = Vector.from_values([0, 1], [2, 3], size=4) + expected = Vector.from_coo([0, 1], [2, 3], size=4) assert v.isequal(expected) v[::2] = np.arange(2) - expected = Vector.from_values([0, 1, 2], [0, 3, 1], size=4) + expected = Vector.from_coo([0, 1, 2], [0, 3, 1], size=4) assert v.isequal(expected) with pytest.raises(TypeError): v[0] = [1] @@ -648,7 +648,7 @@ def test_assign_list(): def test_apply(v): - result = Vector.from_values([1, 3, 4, 6], [-1, -1, -2, 0]) + result = Vector.from_coo([1, 3, 4, 6], [-1, -1, -2, 0]) w = v.apply(unary.ainv).new() assert w.isequal(result) with pytest.raises(TypeError, match="apply only accepts UnaryOp with no scalars or BinaryOp"): @@ -656,12 +656,12 @@ def test_apply(v): def test_apply_binary(v): - result_right = Vector.from_values([1, 3, 4, 6], [False, False, True, False]) + result_right = Vector.from_coo([1, 3, 4, 6], [False, False, True, False]) w_right = v.apply(binary.gt, right=1).new() w_right2 = v.apply(binary.gt, right=Scalar.from_value(1)).new() assert w_right.isequal(result_right) assert w_right2.isequal(result_right) - result_left = Vector.from_values([1, 3, 4, 6], [1, 1, 0, 2]) + result_left = Vector.from_coo([1, 3, 4, 6], [1, 1, 0, 2]) w_left = v.apply(binary.minus, left=2).new() w_left2 = v.apply(binary.minus, left=Scalar.from_value(2)).new() assert w_left.isequal(result_left) @@ -688,7 +688,7 @@ def test_apply_empty(v): def test_apply_indexunary(v): idx = [1, 3, 4, 6] - vr = Vector.from_values(idx, idx) + vr = Vector.from_coo(idx, idx) r1 = v.apply(indexunary.index).new() r2 = v.apply("index").new() r3 = indexunary.index(v).new() @@ -696,7 +696,7 @@ def test_apply_indexunary(v): assert r2.isequal(vr) assert r3.isequal(vr) - v2 = Vector.from_values(idx, [False, False, True, False]) + v2 = Vector.from_coo(idx, [False, False, True, False]) s2 = Scalar(dtypes.INT64) s2.value = 2 w1 = v.apply("==", s2).new() @@ -712,7 +712,7 @@ def test_apply_indexunary(v): def test_select(v): - result = Vector.from_values([1, 3], [1, 1], size=7) + result = Vector.from_coo([1, 3], [1, 1], size=7) w1 = v.select(select.valueeq, 1).new() w2 = v.select("==", 1).new() w3 = select.rowle(v, 3).new() @@ -728,14 +728,14 @@ def test_select(v): with pytest.raises(TypeError, match="thunk"): v.select(select.valueeq, object()) w7 = v.select("==").new() - expected = Vector.from_values([6], 0) + expected = Vector.from_coo([6], 0) assert w7.isequal(expected) @autocompute def test_select_bools_and_masks(v): # Select with boolean and masks - result = Vector.from_values([1, 3], [1, 1], size=7) + result = Vector.from_coo([1, 3], [1, 1], size=7) w7 = v.select((v == 1).new()).new() assert w7.isequal(result) w7b = v.select(v == 1).new() # we rewrite! @@ -761,7 +761,7 @@ def test_select_bools_and_masks(v): w9 << 1 w9[1] = 0 w9(w9.V) << v.select(w7.V) - result2 = Vector.from_values([1, 3], [0, 1], size=7) + result2 = Vector.from_coo([1, 3], [0, 1], size=7) assert w9.isequal(result2) w9 << 1 w9[1] = 0 @@ -794,7 +794,7 @@ def twox_minusthunk(x, row, col, thunk): # pragma: no cover (numba) select.register_anonymous(twox_minusthunk) with pytest.raises(TypeError, match="must be a function"): select.register_anonymous(object()) - expected = Vector.from_values([1, 3, 4, 6], [-2, -2, 0, -4], size=7) + expected = Vector.from_coo([1, 3, 4, 6], [-2, -2, 0, -4], size=7) result = indexunary.twox_minusthunk(v, 4).new() assert result.isequal(expected) delattr(indexunary, "twox_minusthunk") @@ -806,11 +806,11 @@ def ii(x, idx, _, thunk): # pragma: no cover (numba) assert hasattr(indexunary, "ii") assert hasattr(select, "ii") ii_apply = indexunary.register_anonymous(ii) - expected = Vector.from_values([1, 3, 4, 6], [False, False, True, True], size=7) + expected = Vector.from_coo([1, 3, 4, 6], [False, False, True, True], size=7) result = ii_apply(v, 2).new() assert result.isequal(expected) ii_select = select.register_anonymous(ii) - expected = Vector.from_values([4, 6], [2, 0], size=7) + expected = Vector.from_coo([4, 6], [2, 0], size=7) result = ii_select(v, 2).new() assert result.isequal(expected) delattr(indexunary, "ii") @@ -835,7 +835,7 @@ def test_reduce(v): # Test default for non-bool assert v.reduce().new() == 4 # Test default for bool - b1 = Vector.from_values([0, 1], [True, False]) + b1 = Vector.from_coo([0, 1], [True, False]) with pytest.raises(KeyError, match="plus does not work"): # KeyError here is kind of weird b1.reduce() @@ -1003,37 +1003,37 @@ def test_simple_assignment(v): def test_isequal(v): assert v.isequal(v) - u = Vector.from_values([1], [1]) + u = Vector.from_coo([1], [1]) assert not u.isequal(v) - u2 = Vector.from_values([1], [1], size=7) + u2 = Vector.from_coo([1], [1], size=7) assert not u2.isequal(v) - u3 = Vector.from_values([1, 3, 4, 6], [1.0, 1.0, 2.0, 0.0]) + u3 = Vector.from_coo([1, 3, 4, 6], [1.0, 1.0, 2.0, 0.0]) assert not u3.isequal(v, check_dtype=True), "different datatypes are not equal" - u4 = Vector.from_values([1, 3, 4, 6], [1.0, 1 + 1e-9, 1.999999999999, 0.0]) + u4 = Vector.from_coo([1, 3, 4, 6], [1.0, 1 + 1e-9, 1.999999999999, 0.0]) assert not u4.isequal(v) - u5 = Vector.from_values([1, 3, 4, 5], [1.0, 1.0, 2.0, 3], size=u4.size) + u5 = Vector.from_coo([1, 3, 4, 5], [1.0, 1.0, 2.0, 3], size=u4.size) assert not u4.isequal(u5) @pytest.mark.slow def test_isclose(v): assert v.isclose(v) - u = Vector.from_values([1], [1]) # wrong size + u = Vector.from_coo([1], [1]) # wrong size assert not u.isclose(v) - u2 = Vector.from_values([1], [1], size=7) # missing values + u2 = Vector.from_coo([1], [1], size=7) # missing values assert not u2.isclose(v) - u3 = Vector.from_values([1, 2, 3, 4, 6], [1, 1, 1, 2, 0], size=7) # extra values + u3 = Vector.from_coo([1, 2, 3, 4, 6], [1, 1, 1, 2, 0], size=7) # extra values assert not u3.isclose(v) - u4 = Vector.from_values([1, 3, 4, 6], [1.0, 1.0, 2.0, 0.0]) + u4 = Vector.from_coo([1, 3, 4, 6], [1.0, 1.0, 2.0, 0.0]) assert not u4.isclose(v, check_dtype=True), "different datatypes are not equal" - u5 = Vector.from_values([1, 3, 4, 6], [1.0, 1 + 1e-9, 1.999999999999, 0.0]) + u5 = Vector.from_coo([1, 3, 4, 6], [1.0, 1 + 1e-9, 1.999999999999, 0.0]) assert u5.isclose(v) - u6 = Vector.from_values([1, 3, 4, 6], [1.0, 1 + 1e-4, 1.99999, 0.0]) + u6 = Vector.from_coo([1, 3, 4, 6], [1.0, 1 + 1e-4, 1.99999, 0.0]) assert u6.isclose(v, rel_tol=1e-3) # isclose should consider `inf == inf` - u7 = Vector.from_values([1, 3], [-np.inf, np.inf]) + u7 = Vector.from_coo([1, 3], [-np.inf, np.inf]) assert u7.isclose(u7, rel_tol=1e-8) - u4b = Vector.from_values([1, 3, 4, 5], [1.0, 1.0, 2.0, 0.0], size=u4.size) + u4b = Vector.from_coo([1, 3, 4, 5], [1.0, 1.0, 2.0, 0.0], size=u4.size) assert not u4.isclose(u4b) @@ -1041,7 +1041,7 @@ def test_binary_op(v): v2 = v.dup() v2[1] = 0 w = v.ewise_mult(v2, binary.gt).new() - result = Vector.from_values([1, 3, 4, 6], [True, False, False, False]) + result = Vector.from_coo([1, 3, 4, 6], [True, False, False, False]) assert w.dtype == "BOOL" assert w.isequal(result) @@ -1084,7 +1084,7 @@ def test_del(capsys): shell_v = object.__new__(Vector) del shell_v # v has `gb_obj` of NULL - v = Vector.from_values([0, 1], [0, 1]) + v = Vector.from_coo([0, 1], [0, 1]) gb_obj = v.gb_obj v.gb_obj = gb.core.NULL del v @@ -1165,7 +1165,7 @@ def test_ss_import_export(v, do_iso, methods): assert v2.isequal(v) assert v2.ss.is_iso is do_iso - v3 = Vector.from_values([0, 1, 2], [1, 3, 5]) + v3 = Vector.from_coo([0, 1, 2], [1, 3, 5]) if do_iso: v3(v3.S) << 1 v3_copy = v3.dup() @@ -1293,7 +1293,7 @@ def import_func(x, import_name, **kwargs): assert v.ss.is_iso is do_iso assert v_orig.ss.is_iso is do_iso - w = Vector.from_values([0, 1, 2], [10, 20, 30]) + w = Vector.from_coo([0, 1, 2], [10, 20, 30]) if do_iso: w(w.S) << 1 w_orig = w.dup() @@ -1374,8 +1374,8 @@ def test_not_to_array(v): def test_vector_index_with_scalar(): - v = Vector.from_values([0, 1, 2], [10, 20, 30]) - expected = Vector.from_values([0, 1], [20, 10]) + v = Vector.from_coo([0, 1, 2], [10, 20, 30]) + expected = Vector.from_coo([0, 1], [20, 10]) for dtype in ["int8", "uint8", "int16", "uint16", "int32", "uint32"]: s1 = Scalar.from_value(1, dtype=dtype) assert v[s1].new() == 20 @@ -1389,12 +1389,12 @@ def test_vector_index_with_scalar(): def test_diag(v): - indices, values = v.to_values() + indices, values = v.to_coo() for k in range(-5, 5): size = v.size + abs(k) rows = indices + max(0, -k) cols = indices + max(0, k) - expected = Matrix.from_values(rows, cols, values, nrows=size, ncols=size, dtype=v.dtype) + expected = Matrix.from_coo(rows, cols, values, nrows=size, ncols=size, dtype=v.dtype) # Construct diagonal matrix A if suitesparse: A = gb.ss.diag(v, k=k) @@ -1503,8 +1503,8 @@ def test_auto(v): assert expr[0].value == expected[0].value assert expr[1].value == expected[1].value assert list(expr) == list(expected) - k1, v1 = expected.to_values() - k2, v2 = expr.to_values() + k1, v1 = expected.to_coo() + k2, v2 = expr.to_coo() assert_array_equal(k1, k2) assert_array_equal(v1, v2) unary.sqrt(expected).isequal(unary.sqrt(expr)) @@ -1592,6 +1592,7 @@ def test_expr_is_like_vector(v): "_update", "build", "clear", + "from_coo", "from_dict", "from_pygraphblas", "from_values", @@ -1628,6 +1629,7 @@ def test_index_expr_is_like_vector(v): "_update", "build", "clear", + "from_coo", "from_dict", "from_pygraphblas", "from_values", @@ -1643,10 +1645,10 @@ def test_index_expr_is_like_vector(v): @pytest.mark.skipif("not suitesparse") def test_ss_random(v): - r1 = Vector.from_values([1], [1], size=v.size) - r2 = Vector.from_values([3], [1], size=v.size) - r3 = Vector.from_values([4], [2], size=v.size) - r4 = Vector.from_values([6], [0], size=v.size) + r1 = Vector.from_coo([1], [1], size=v.size) + r2 = Vector.from_coo([3], [1], size=v.size) + r3 = Vector.from_coo([4], [2], size=v.size) + r4 = Vector.from_coo([6], [0], size=v.size) seen = set() for _i in range(1000000): # pragma: no branch (sanity) r = v.ss.selectk("random", 1) @@ -1685,7 +1687,7 @@ def test_ss_firstk(v): for w, data in [(v, mixed_data), (iso_v, iso_data)]: for k in range(w.nvals + 1): x = w.ss.selectk("first", k) - expected = Vector.from_values(data[0][:k], data[1][:k], size=w.size) + expected = Vector.from_coo(data[0][:k], data[1][:k], size=w.size) assert x.isequal(expected) with pytest.raises(ValueError): v.ss.selectk("first", -1) @@ -1700,39 +1702,39 @@ def test_ss_lastk(v): for w, data in [(v, mixed_data), (iso_v, iso_data)]: for k in range(w.nvals + 1): x = w.ss.selectk("last", k) - expected = Vector.from_values(data[0][-k:], data[1][-k:], size=w.size) + expected = Vector.from_coo(data[0][-k:], data[1][-k:], size=w.size) assert x.isequal(expected) @pytest.mark.skipif("not suitesparse") def test_ss_largestk(v): w = v.ss.selectk("largest", 1) - expected = Vector.from_values([4], [2], size=v.size) + expected = Vector.from_coo([4], [2], size=v.size) assert w.isequal(expected) w = v.ss.selectk("largest", 2) - expected1 = Vector.from_values([1, 4], [1, 2], size=v.size) - expected2 = Vector.from_values([3, 4], [1, 2], size=v.size) + expected1 = Vector.from_coo([1, 4], [1, 2], size=v.size) + expected2 = Vector.from_coo([3, 4], [1, 2], size=v.size) assert w.isequal(expected1) or w.isequal(expected2) w = v.ss.selectk("largest", 3) - expected = Vector.from_values([1, 3, 4], [1, 1, 2], size=v.size) + expected = Vector.from_coo([1, 3, 4], [1, 1, 2], size=v.size) assert w.isequal(expected) @pytest.mark.skipif("not suitesparse") def test_ss_smallestk(v): w = v.ss.selectk("smallest", 1) - expected = Vector.from_values([6], [0], size=v.size) + expected = Vector.from_coo([6], [0], size=v.size) assert w.isequal(expected) w = v.ss.selectk("smallest", 2) - expected1 = Vector.from_values([1, 6], [1, 0], size=v.size) - expected2 = Vector.from_values([3, 6], [1, 0], size=v.size) + expected1 = Vector.from_coo([1, 6], [1, 0], size=v.size) + expected2 = Vector.from_coo([3, 6], [1, 0], size=v.size) assert w.isequal(expected1) or w.isequal(expected2) w = v.ss.selectk("smallest", 3) - expected = Vector.from_values([1, 3, 6], [1, 1, 0], size=v.size) + expected = Vector.from_coo([1, 3, 6], [1, 1, 0], size=v.size) assert w.isequal(expected) @@ -1742,9 +1744,9 @@ def test_ss_compactify(do_iso): orig_indices = [1, 3, 4, 6] new_indices = [0, 1, 2, 3] if do_iso: - v = Vector.from_values(orig_indices, 1) + v = Vector.from_coo(orig_indices, 1) else: - v = Vector.from_values(orig_indices, [1, 4, 2, 0]) + v = Vector.from_coo(orig_indices, [1, 4, 2, 0]) def check(v, expected, *args, stop=0, **kwargs): w = v.ss.compactify(*args, **kwargs) @@ -1766,7 +1768,7 @@ def check_reverse(v, expected, *args, stop=0, **kwargs): w = v.ss.compactify(*args, size=n, reverse=True, **kwargs) assert w.isequal(x) - expected = Vector.from_values( + expected = Vector.from_coo( new_indices, 1 if do_iso else [1, 4, 2, 0], size=4, @@ -1776,7 +1778,7 @@ def check_reverse(v, expected, *args, stop=0, **kwargs): check(v, reverse(expected), "last") check_reverse(v, reverse(expected), "last") - expected = Vector.from_values( + expected = Vector.from_coo( new_indices, orig_indices, size=4, @@ -1786,7 +1788,7 @@ def check_reverse(v, expected, *args, stop=0, **kwargs): check(v, reverse(expected), "last", asindex=True) check_reverse(v, reverse(expected), "last", asindex=True) - expected = Vector.from_values( + expected = Vector.from_coo( new_indices, 1 if do_iso else [0, 1, 2, 4], size=4, @@ -1797,7 +1799,7 @@ def check_reverse(v, expected, *args, stop=0, **kwargs): check_reverse(v, reverse(expected), "largest") if not do_iso: - expected = Vector.from_values( + expected = Vector.from_coo( new_indices, [6, 1, 4, 3], size=4, @@ -1836,16 +1838,16 @@ def compare(v, expected, isequal=True, sort=False, **kwargs): def test_slice(): - v = Vector.from_values(np.arange(5), np.arange(5)) + v = Vector.from_coo(np.arange(5), np.arange(5)) w = v[0:0].new() assert w.size == 0 w = v[2:0].new() assert w.size == 0 w = v[::-1].new() - expected = Vector.from_values(np.arange(5), np.arange(5)[::-1]) + expected = Vector.from_coo(np.arange(5), np.arange(5)[::-1]) assert w.isequal(expected) w = v[4:-3:-1].new() - expected = Vector.from_values(np.arange(2), np.arange(5)[4:-3:-1]) + expected = Vector.from_coo(np.arange(2), np.arange(5)[4:-3:-1]) assert w.isequal(expected) @@ -1869,8 +1871,8 @@ def test_ss_concat(v): @pytest.mark.skipif("not suitesparse") def test_ss_split(v): w1, w2 = v.ss.split(4) - expected1 = Vector.from_values([1, 3], 1) - expected2 = Vector.from_values([0, 2], [2, 0]) + expected1 = Vector.from_coo([1, 3], 1) + expected2 = Vector.from_coo([0, 2], [2, 0]) assert w1.isequal(expected1) assert w2.isequal(expected2) x1, x2 = v.ss.split([4, 3], name="split") @@ -1880,6 +1882,13 @@ def test_ss_split(v): assert x2.name == "split_1" +def test_deprecated(v): + with pytest.warns(DeprecationWarning): + v.to_values() + with pytest.warns(DeprecationWarning): + Vector.from_values([1], [2]) + + def test_ndim(A, v): assert v.ndim == 1 assert v.ewise_mult(v).ndim == 1 @@ -1896,21 +1905,21 @@ def test_sizeof(v): def test_ewise_union(): - v1 = Vector.from_values([0], [1], size=3) - v2 = Vector.from_values([1], [2], size=3) + v1 = Vector.from_coo([0], [1], size=3) + v2 = Vector.from_coo([1], [2], size=3) result = v1.ewise_union(v2, binary.plus, 10, 20).new() - expected = Vector.from_values([0, 1], [21, 12], size=3) + expected = Vector.from_coo([0, 1], [21, 12], size=3) assert result.isequal(expected) # Handle Scalars result = v1.ewise_union(v2, binary.plus, Scalar.from_value(10), Scalar.from_value(20)).new() assert result.isequal(expected) # Upcast if scalars are floats result = v1.ewise_union(v2, monoid.plus, 10.1, 20.2).new() - expected = Vector.from_values([0, 1], [21.2, 12.1], size=3) + expected = Vector.from_coo([0, 1], [21.2, 12.1], size=3) assert result.isclose(expected) result = v1.ewise_union(v2, binary.minus, 0, 0).new() - expected = Vector.from_values([0, 1], [1, -2], size=3) + expected = Vector.from_coo([0, 1], [1, -2], size=3) assert result.isequal(expected) result = (v1 - v2).new() assert result.isequal(expected) @@ -1926,7 +1935,7 @@ def test_ewise_union(): def test_delete_via_scalar(v): del v[[1, 3]] - assert v.isequal(Vector.from_values([4, 6], [2, 0])) + assert v.isequal(Vector.from_coo([4, 6], [2, 0])) del v[:] assert v.nvals == 0 @@ -1938,18 +1947,18 @@ def test_udt(): v = Vector(udt, size=3) a = np.zeros(1, dtype=record_dtype) v[0] = a[0] - expected = Vector.from_values([0], a, size=3, dtype=udt) + expected = Vector.from_coo([0], a, size=3, dtype=udt) assert v.isequal(expected) v[1] = (1, 2) - expected = Vector.from_values( + expected = Vector.from_coo( [0, 1], np.array([(0, 0), (1, 2)], dtype=record_dtype), size=3, dtype=udt ) assert v.isequal(expected) v[:] = 0 - zeros = Vector.from_values([0, 1, 2], np.zeros(3, dtype=record_dtype), dtype=udt) + zeros = Vector.from_coo([0, 1, 2], np.zeros(3, dtype=record_dtype), dtype=udt) assert v.isequal(zeros) v(v.S)[:] = 1 - ones = Vector.from_values([0, 1, 2], np.ones(3, dtype=record_dtype), dtype=udt) + ones = Vector.from_coo([0, 1, 2], np.ones(3, dtype=record_dtype), dtype=udt) assert v.isequal(ones) v[:](v.S) << 0 assert v.isequal(zeros) @@ -1974,10 +1983,10 @@ def test_udt(): assert v.isequal(zeros) assert v[0].new() == s assert v[:].new().isequal(v) - indices, values = v.to_values() - assert v.isequal(Vector.from_values(indices, values)) + indices, values = v.to_coo() + assert v.isequal(Vector.from_coo(indices, values)) result = unary.positioni(v).new() - expected = Vector.from_values([0, 1, 2], [0, 1, 2]) + expected = Vector.from_coo([0, 1, 2], [0, 1, 2]) assert result.isequal(expected) result = binary.firsti(v & v).new() assert result.isequal(expected) @@ -1998,7 +2007,7 @@ def test_udt(): v.reduce(aggop).new() v.clear() v[[0, 1]] = [(2, 3), (4, 5)] - expected = Vector.from_values([0, 1], [(2, 3), (4, 5)], dtype=udt, size=v.size) + expected = Vector.from_coo([0, 1], [(2, 3), (4, 5)], dtype=udt, size=v.size) if suitesparse: vv = Vector.ss.deserialize(v.ss.serialize()) assert v.isequal(vv, check_dtype=True) @@ -2035,10 +2044,10 @@ def test_udt(): v << (1, 1, 1) assert v.isequal(w) - indices, values = v.to_values() + indices, values = v.to_coo() assert_array_equal(values, np.ones((2, 3), dtype=np.uint16)) - assert v.isequal(Vector.from_values(indices, values, dtype=v.dtype)) - assert v.isequal(Vector.from_values(indices, values)) + assert v.isequal(Vector.from_coo(indices, values, dtype=v.dtype)) + assert v.isequal(Vector.from_coo(indices, values)) if suitesparse: info = v.ss.export() result = Vector.ss.import_any(**info) @@ -2060,7 +2069,7 @@ def test_udt(): v.reduce(aggop).new() v.clear() v[[0, 1]] = [[2, 3, 4], [5, 6, 7]] - expected = Vector.from_values([0, 1], [[2, 3, 4], [5, 6, 7]], dtype=udt2, size=v.size) + expected = Vector.from_coo([0, 1], [[2, 3, 4], [5, 6, 7]], dtype=udt2, size=v.size) assert v.isequal(expected) if suitesparse: vv = Vector.ss.deserialize(v.ss.serialize()) @@ -2129,7 +2138,7 @@ def test_ss_iteration(v): assert list(w.ss.itervalues()) == [] assert list(w.ss.iteritems()) == [] - indices, values = v.to_values() + indices, values = v.to_coo() # This is what I would expect assert sorted(indices) == sorted(v.ss.iterkeys()) assert sorted(values) == sorted(v.ss.itervalues()) @@ -2217,7 +2226,7 @@ def test_ewise_union_infix(): v[0] = 1 w[1] = 2 result = binary.plus(v | w, left_default=10, right_default=20).new() - expected = Vector.from_values([0, 1], [21, 12], size=3) + expected = Vector.from_coo([0, 1], [21, 12], size=3) assert expected.isequal(result) result = monoid.plus(v | w, left_default=10, right_default=20).new() assert expected.isequal(result) @@ -2243,13 +2252,13 @@ def test_ewise_union_infix(): def test_reposition(v): - indices, values = v.to_values() + indices, values = v.to_coo() indices = indices.astype(int) def get_expected(offset, size): ind = indices + offset mask = (ind >= 0) & (ind < size) - return Vector.from_values(ind[mask], values[mask], size=size) + return Vector.from_coo(ind[mask], values[mask], size=size) for offset in range(-v.size - 2, v.size + 3): result = v.reposition(offset).new() @@ -2263,24 +2272,24 @@ def get_expected(offset, size): assert result.isequal(expected) -def test_to_values_sort(): +def test_to_coo_sort(): # How can we get a vector to a jumbled state in SS so that export won't be sorted? N = 1000000 a = np.unique(np.random.randint(N, size=100)) expected = a.copy() np.random.shuffle(a) - v = Vector.from_values(a, a, size=N) - indices, values = v.to_values(sort=False) - v = Vector.from_values(a, a, size=N) - indices, values = v.to_values(sort=True) + v = Vector.from_coo(a, a, size=N) + indices, values = v.to_coo(sort=False) + v = Vector.from_coo(a, a, size=N) + indices, values = v.to_coo(sort=True) assert_array_equal(indices, expected) assert_array_equal(values, expected) -def test_to_values_subset(v): - indices, values = v.to_values() +def test_to_coo_subset(v): + indices, values = v.to_coo() for do_indices, do_vals in itertools.product([True, False], [True, False]): - idx, vals = v.to_values(indices=do_indices, values=do_vals) + idx, vals = v.to_coo(indices=do_indices, values=do_vals) if do_indices: assert_array_equal(idx, indices) else: @@ -2290,11 +2299,11 @@ def test_to_values_subset(v): assert vals.dtype == np.int64 else: assert vals is None - idx, vals = v.to_values(indices=None, values=True, dtype=float) + idx, vals = v.to_coo(indices=None, values=True, dtype=float) assert idx is None assert_array_equal(vals, values) assert vals.dtype == float - idx, vals = v.to_values(values=True, dtype=v.dtype, sort=False) + idx, vals = v.to_coo(values=True, dtype=v.dtype, sort=False) assert vals.dtype == np.int64 diff --git a/graphblas/viz.py b/graphblas/viz.py index 394f03c1e..0eb5e6476 100644 --- a/graphblas/viz.py +++ b/graphblas/viz.py @@ -138,7 +138,7 @@ def datashade(M, agg="count", *, width=None, height=None, opts_kwargs=None, **kw """ np, pd, bk, hv, hp, ds = _get_imports(["np", "pd", "bk", "hv", "hp", "ds"], "datashade") if "df" not in kwargs: - rows, cols, vals = M.to_values() + rows, cols, vals = M.to_coo() max_int = np.iinfo(np.int64).max if M.nrows > max_int and rows.max() > max_int: rows = rows.astype(np.float64) diff --git a/notebooks/Aggregators.ipynb b/notebooks/Aggregators.ipynb index d2e435e3a..5fcaf8aeb 100644 --- a/notebooks/Aggregators.ipynb +++ b/notebooks/Aggregators.ipynb @@ -266,7 +266,7 @@ "# We can create a recorder and have it be alive until we call `rec.stop()`.\n", "# One can do `rec.clear()` to clear the data.\n", "rec = Recorder()\n", - "A = Matrix.from_values(\n", + "A = Matrix.from_coo(\n", " [3, 0, 3, 5, 6, 0, 6, 1, 6, 2, 5],\n", " [0, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5],\n", " [3, 2, 3, 1, 5, 3, 7, 8, 3, 1, 7],\n", @@ -36990,7 +36990,7 @@ } ], "source": [ - "v = Vector.from_values([1, 3, 4, 6], [1, 1, 2, 0])\n", + "v = Vector.from_coo([1, 3, 4, 6], [1, 1, 2, 0])\n", "v" ] }, diff --git a/notebooks/Connected Components -- FastSV.ipynb b/notebooks/Connected Components -- FastSV.ipynb index fe950fb6c..b44e9e4af 100644 --- a/notebooks/Connected Components -- FastSV.ipynb +++ b/notebooks/Connected Components -- FastSV.ipynb @@ -1595,7 +1595,7 @@ "# 710 // calculate grandparent\n", "# 711 // fixme: NULL parameter is SS:GrB extension\n", "# 712 GrB_TRY (GrB_Vector_extractTuples (NULL, V32, &n, f)) ; // fixme\n", - "_, V32 = f.to_values()\n", + "_, V32 = f.to_coo()\n", "V32" ] }, @@ -2097,7 +2097,7 @@ " n = A.nrows\n", " I = np.arange(n)\n", " # The parent of each vertex is initialized to be the vertex itself:\n", - " f = Vector.from_values(I, I, name=\"parents\")\n", + " f = Vector.from_coo(I, I, name=\"parents\")\n", " gp = f.dup() # grandparent of each vertex initialized to parent\n", " gp_dup = gp.dup() # duplicate grandparents\n", " mngp = f.dup() # minimum grandparent of each star-vertex\n", @@ -2115,7 +2115,7 @@ " # Step 2: Shortcutting\n", " f << op.min(f | gp)\n", " # Step 3: Calculate grandparents\n", - " _, I = f.to_values()\n", + " _, I = f.to_coo()\n", " gp << f[I]\n", " # Check termination\n", " mod << op.ne(gp_dup & gp)\n", @@ -2513,7 +2513,7 @@ "source": [ "rows, cols = np.arange(p.size), p\n", "data = np.full_like(rows, fill_value=1)\n", - "P = Matrix.from_values(rows, cols, data, name=\"P\")" + "P = Matrix.from_coo(rows, cols, data, name=\"P\")" ] }, { @@ -3218,7 +3218,7 @@ }, "outputs": [], "source": [ - "_, components_perm = connected_components_perm.to_values()" + "_, components_perm = connected_components_perm.to_coo()" ] }, { @@ -3237,7 +3237,7 @@ }, "outputs": [], "source": [ - "_, components = connected_components.to_values()" + "_, components = connected_components.to_coo()" ] }, { @@ -3354,7 +3354,7 @@ "source": [ "rows, cols = np.arange(p2.size), p2\n", "data = np.full_like(rows, fill_value=1)\n", - "P = Matrix.from_values(rows, cols, data)" + "P = Matrix.from_coo(rows, cols, data)" ] }, { @@ -3631,7 +3631,7 @@ }, "outputs": [], "source": [ - "_, components_perm2 = connected_components_perm2.to_values()" + "_, components_perm2 = connected_components_perm2.to_coo()" ] }, { diff --git a/notebooks/Example B.1 -- Level BFS.ipynb b/notebooks/Example B.1 -- Level BFS.ipynb index 655e1cd57..cdee2f2fc 100644 --- a/notebooks/Example B.1 -- Level BFS.ipynb +++ b/notebooks/Example B.1 -- Level BFS.ipynb @@ -36,7 +36,7 @@ " [3, 0, 3, 5, 6, 0, 6, 1, 6, 2, 4, 1],\n", " [0, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6],\n", "]\n", - "A = Matrix.from_values(edges[0], edges[1], [True for _ in edges[0]])\n", + "A = Matrix.from_coo(edges[0], edges[1], [True for _ in edges[0]])\n", "s = 1" ] }, diff --git a/notebooks/Example B.3 -- Parent BFS.ipynb b/notebooks/Example B.3 -- Parent BFS.ipynb index 36beef50c..d1fbd82c5 100644 --- a/notebooks/Example B.3 -- Parent BFS.ipynb +++ b/notebooks/Example B.3 -- Parent BFS.ipynb @@ -36,7 +36,7 @@ " [3, 0, 3, 5, 6, 0, 6, 1, 6, 2, 4, 1],\n", " [0, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6],\n", "]\n", - "A = Matrix.from_values(edges[0], edges[1], [True for _ in edges[0]])\n", + "A = Matrix.from_coo(edges[0], edges[1], [True for _ in edges[0]])\n", "s = 1" ] }, diff --git a/notebooks/HPEC2022.ipynb b/notebooks/HPEC2022.ipynb index f100154f2..beec8dfec 100644 --- a/notebooks/HPEC2022.ipynb +++ b/notebooks/HPEC2022.ipynb @@ -3513,7 +3513,7 @@ } ], "source": [ - "v = gb.Vector.from_values(np.arange(5), np.arange(5))\n", + "v = gb.Vector.from_coo(np.arange(5), np.arange(5))\n", "v" ] }, @@ -4855,7 +4855,7 @@ } ], "source": [ - "gb.Matrix.from_values(np.arange(5), np.zeros(5, int), np.arange(25).reshape(5, 5))" + "gb.Matrix.from_coo(np.arange(5), np.zeros(5, int), np.arange(25).reshape(5, 5))" ] }, { diff --git a/notebooks/Intro to GraphBLAS + SSSP example.ipynb b/notebooks/Intro to GraphBLAS + SSSP example.ipynb index 15956d5c4..fe0ded81f 100644 --- a/notebooks/Intro to GraphBLAS + SSSP example.ipynb +++ b/notebooks/Intro to GraphBLAS + SSSP example.ipynb @@ -42,7 +42,7 @@ "outputs": [], "source": [ "rows, cols, weights = data\n", - "m = Matrix.from_values(rows, cols, weights)\n", + "m = Matrix.from_coo(rows, cols, weights)\n", "m\n", "# Size of the sparse matrix is 7x7 with 12 non-zero elements of type INT64" ] @@ -88,7 +88,7 @@ "metadata": {}, "outputs": [], "source": [ - "v = Vector.from_values([1], [0], dtype=m.dtype, size=m.nrows)" + "v = Vector.from_coo([1], [0], dtype=m.dtype, size=m.nrows)" ] }, { @@ -245,7 +245,7 @@ "metadata": {}, "outputs": [], "source": [ - "m_ident = Matrix.from_values(range(7), range(7), [0] * 7)\n", + "m_ident = Matrix.from_coo(range(7), range(7), [0] * 7)\n", "m_ident" ] }, diff --git a/notebooks/Louvain.ipynb b/notebooks/Louvain.ipynb index 2fad1529e..308ab0d63 100644 --- a/notebooks/Louvain.ipynb +++ b/notebooks/Louvain.ipynb @@ -47,7 +47,7 @@ " self.total_links_doubled = adj.reduce_scalar(monoid.plus, allow_empty=False).new()\n", " self.community_tmp = Matrix(adj.dtype, nrows=nc, ncols=nn)\n", " self.community = Matrix(adj.dtype, nrows=nc, ncols=nc)\n", - " self.diag_mask = Matrix.from_values(\n", + " self.diag_mask = Matrix.from_coo(\n", " range(nc), range(nc), [True] * nc, nrows=nc, ncols=nc, dtype=dtypes.BOOL\n", " )\n", " self.diag_matrix = Matrix(adj.dtype, nrows=nc, ncols=nc)\n", @@ -55,7 +55,7 @@ " self.modularity = Vector(dtypes.FP64, size=nc)\n", " self.stored_community = Vector(dtypes.INT64, size=nc)\n", " self.beyond_last_index = nn\n", - " self.beyond_last = Vector.from_values([self.beyond_last_index], [1], size=nc)\n", + " self.beyond_last = Vector.from_coo([self.beyond_last_index], [1], size=nc)\n", " self.ki_all = Vector(dtypes.FP64, size=nn)\n", " self.ki_all << adj.reduce_columnwise(monoid.plus)\n", " self.sigma_total = Vector(dtypes.FP64, size=nc)\n", @@ -68,7 +68,7 @@ " # Build the identity matrix, assigning each node to its own community\n", " if self._ident_comms is None:\n", " nn, nc = self.nn, self.nc\n", - " self._ident_comms = Matrix.from_values(\n", + " self._ident_comms = Matrix.from_coo(\n", " range(nn), range(nn), [1] * nn, nrows=nc, ncols=nn\n", " )\n", " return self._ident_comms\n", @@ -113,7 +113,7 @@ " sigma_total = self.sigma_total\n", " # Save current modularity score in current community\n", " self.stored_community << comms[:, node]\n", - " current_community_index = self.stored_community.to_values()[0][0]\n", + " current_community_index = self.stored_community.to_coo()[0][0]\n", " orig_modularity_score = self.compute_modularity(comms)\n", "\n", " # Move node to its own community\n", @@ -144,7 +144,7 @@ " ):\n", " self.max_mask << delta.apply(binary.eq, right=max_modularity_delta)\n", " delta(self.max_mask.V, replace=True) << delta # eliminate all but the max value(s)\n", - " indexes, vals = delta.to_values()\n", + " indexes, vals = delta.to_coo()\n", " best_community_index = indexes[0]\n", " # Guard against reassigning a node to its existing community\n", " if (\n", @@ -182,7 +182,7 @@ " break\n", "\n", " # Compact comms\n", - " rows, cols, vals = comms.to_values()\n", + " rows, cols, vals = comms.to_coo()\n", " nonzero_rows = list(sorted(set(rows)))\n", " compact_comms = Matrix(comms.dtype, nrows=len(nonzero_rows), ncols=self.nn)\n", " compact_comms << comms[nonzero_rows, :]\n", diff --git a/notebooks/Pagerank Demo.ipynb b/notebooks/Pagerank Demo.ipynb index 141e9455e..cab664b29 100644 --- a/notebooks/Pagerank Demo.ipynb +++ b/notebooks/Pagerank Demo.ipynb @@ -26,7 +26,7 @@ "metadata": {}, "outputs": [], "source": [ - "A = Matrix.from_values(\n", + "A = Matrix.from_coo(\n", " [0, 0, 1, 2, 2, 3, 4],\n", " [1, 2, 3, 3, 4, 4, 0],\n", " [1.1, 9.8, 4.2, 7.1, 0.2, 6.9, 2.2],\n", diff --git a/notebooks/repr_demo.ipynb b/notebooks/repr_demo.ipynb index b96235852..1d81ba2b3 100644 --- a/notebooks/repr_demo.ipynb +++ b/notebooks/repr_demo.ipynb @@ -201,7 +201,7 @@ } ], "source": [ - "v = Vector.from_values([0, 2, 4], [0, 1, 2])\n", + "v = Vector.from_coo([0, 2, 4], [0, 1, 2])\n", "v" ] }, @@ -436,7 +436,7 @@ } ], "source": [ - "Vector.from_values([0, 2**53 - 1], [0, 1], name=\"v_huge\")" + "Vector.from_coo([0, 2**53 - 1], [0, 1], name=\"v_huge\")" ] }, { @@ -652,7 +652,7 @@ } ], "source": [ - "A = Matrix.from_values([1, 3, 1, 3], [0, 1, 2, 4], [0, 1, 2, 3])\n", + "A = Matrix.from_coo([1, 3, 1, 3], [0, 1, 2, 4], [0, 1, 2, 3])\n", "A" ] }, diff --git a/scripts/create_pickle.py b/scripts/create_pickle.py index 2613e34b8..4e62ef7fd 100755 --- a/scripts/create_pickle.py +++ b/scripts/create_pickle.py @@ -13,7 +13,7 @@ def pickle1(filename): - v = gb.Vector.from_values([1], 2) + v = gb.Vector.from_coo([1], 2) unary_pickle = gb.core.operator.UnaryOp.register_new("unary_pickle", unarypickle) binary_pickle = gb.core.operator.BinaryOp.register_new("binary_pickle", binarypickle) @@ -30,8 +30,8 @@ def pickle1(filename): "scalar": gb.Scalar.from_value(2), "empty_scalar": gb.Scalar.new(bool), "vector": v, - "matrix": gb.Matrix.from_values([2], [3], 4), - "matrix.T": gb.Matrix.from_values([3], [4], 5).T, + "matrix": gb.Matrix.from_coo([2], [3], 4), + "matrix.T": gb.Matrix.from_coo([3], [4], 5).T, "vector.S": v.S, "vector.V": v.V, "~vector.S": ~v.S,